-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
348 lines (268 loc) · 11.9 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import json
import os
import subprocess
import time
p4 = bfrt.ribosome.pipe
# Pipe where Ribosome is running
PIPE_NUM = 0
# Port defines
OUTPUT_1_PORT = 48
OUTPUT_2_PORT = 52
OUTPUT_3_PORT = 44
OUTPUT_4_PORT = 40
NF_PORT = 36
SERVER_1_PORT = 0
SERVER_2_PORT = 20
SERVER_3_PORT = 16
SERVER_4_PORT = 32
SERVER_PORT_TO_IDX = {
SERVER_1_PORT: 0,
SERVER_2_PORT: 1,
SERVER_3_PORT: 2,
SERVER_4_PORT: 3
}
# Queue Pairs and Servers information
MAX_QP_NUM = 64
NUMBER_OF_SERVERS = 4
TOTAL_QP = MAX_QP_NUM * NUMBER_OF_SERVERS
# Path where the setup.py is located, change it accordingly.
curr_path = os.path.join(os.environ['HOME'], "labs", "Ribosome-P4")
#################################
########### PORT SETUP ##########
#################################
# In this section, we set up the ports used by Ribosome.
def setup_ports():
global bfrt, OUTPUT_1_PORT, OUTPUT_2_PORT, OUTPUT_3_PORT, OUTPUT_4_PORT, NF_PORT, \
SERVER_1_PORT, SERVER_2_PORT, SERVER_3_PORT, SERVER_4_PORT
for port in [OUTPUT_1_PORT, OUTPUT_2_PORT, OUTPUT_3_PORT, OUTPUT_4_PORT, NF_PORT,
SERVER_1_PORT, SERVER_2_PORT, SERVER_3_PORT, SERVER_4_PORT]:
print("Setting Output Port: %d" % port)
bfrt.port.port.add(DEV_PORT=port, SPEED='BF_SPEED_100G', FEC='BF_FEC_TYP_REED_SOLOMON', PORT_ENABLE=True)
#################################
##### MIRROR SESSIONS TABLE #####
#################################
# In this section, we setup the mirror sessions of Ribosome.
# One session is used to truncate/send the headers to the NF.
# Other NUMBER_OF_SERVERS are used to send QP Refresh Packets to the proper RDMA server.
PKT_MIN_LENGTH = 71
HEADER_MIRROR_SESSION = 100
QP_REFRESH_MIRROR_SESSION = 200
def setup_mirror_sessions_table():
global bfrt, NF_PORT, SERVER_PORT_TO_IDX, PKT_MIN_LENGTH, HEADER_MIRROR_SESSION, QP_REFRESH_MIRROR_SESSION
mirror_cfg = bfrt.mirror.cfg
print("Setting up Header Truncate Group %d -- Egress Port %d -- Truncate at %d bytes" %
(HEADER_MIRROR_SESSION, NF_PORT, PKT_MIN_LENGTH))
mirror_cfg.entry_with_normal(
sid=HEADER_MIRROR_SESSION,
direction="BOTH",
session_enable=True,
ucast_egress_port=NF_PORT,
ucast_egress_port_valid=1,
max_pkt_len=PKT_MIN_LENGTH
).push()
for port, session in SERVER_PORT_TO_IDX.items():
print("Setting up QP Refresh Group %d -- Egress Port %d -- Truncate at %d bytes" %
(QP_REFRESH_MIRROR_SESSION + session, port, PKT_MIN_LENGTH))
mirror_cfg.entry_with_normal(
sid=QP_REFRESH_MIRROR_SESSION + session,
direction="BOTH",
session_enable=True,
ucast_egress_port=port,
ucast_egress_port_valid=1,
max_pkt_len=PKT_MIN_LENGTH
).push()
#################################
##### TRAFFIC MANAGER POOLS #####
#################################
# In this section, we enlarge the TM buffer pools to the maximum available.
def setup_tm_pools():
global bfrt
tm = bfrt.tf1.tm
tm.pool.app.mod_with_color_drop_enable(pool='EG_APP_POOL_0', green_limit_cells=20000000 // 80,
yellow_limit_cells=20000000 // 80, red_limit_cells=20000000 // 80)
tm.pool.app.mod_with_color_drop_enable(pool='IG_APP_POOL_0', green_limit_cells=20000000 // 80,
yellow_limit_cells=20000000 // 80, red_limit_cells=20000000 // 80)
tm.pool.app.mod_with_color_drop_enable(pool='EG_APP_POOL_1', green_limit_cells=20000000 // 80,
yellow_limit_cells=20000000 // 80, red_limit_cells=20000000 // 80)
tm.pool.app.mod_with_color_drop_enable(pool='IG_APP_POOL_1', green_limit_cells=20000000 // 80,
yellow_limit_cells=20000000 // 80, red_limit_cells=20000000 // 80)
######################
##### PORT STATS #####
######################
# This section creates a timer that calls a callback to dump and print port stats.
# In particular, it dumps both RX/TX bps and pps from the NF server and from RDMA Server 1.
tolog = {
b'$OctetsTransmittedTotal': 'TX',
b'$OctetsReceived': 'RX',
b'$FramesReceivedAll': 'RXPPS',
b'$FramesTransmittedAll': 'TXPPS'
}
last = {}
last_srv = {}
for k in tolog.keys():
last[k] = 0
last_srv[k] = 0
start_ts = time.time()
def dump_counters():
global bfrt, last, last_srv, start_ts, time, tolog, NF_PORT, SERVER_1_PORT
port_stats = bfrt.port.port_stat.get(regex=True, print_ents=False)
ts = time.time() - start_ts
nf_port_stats = list(filter(lambda x: x.key[b'$DEV_PORT'] == NF_PORT, port_stats))[0]
for key, name in tolog.items():
val = nf_port_stats.data[key]
diff = val - last[key]
last[key] = val
print("TOF-%f-RESULT-TOF%s %d" % (ts, name, diff))
rdma_server_stats = list(filter(lambda x: x.key[b'$DEV_PORT'] == SERVER_1_PORT, port_stats))[0]
for key, name in tolog.items():
val = rdma_server_stats.data[key]
diff = val - last_srv[key]
last_srv[key] = val
print("SRV1-%f-RESULT-SRV1%s %d" % (ts, name, diff))
def port_stats_timer():
import threading
global port_stats_timer, dump_counters
dump_counters()
threading.Timer(1, port_stats_timer).start()
######################
##### QP RESTORE #####
######################
# This section creates a timer that calls a callback to restore disabled QPs.
# In particular, it reads the enabled_qp register and filters out the entries with value = 0.
# For each of such entries, it then set to 1 the relative entry in the restore_qp register.
restore_qp_register = p4.restore_qp
restore_qp_register.symmetric_mode_set(False)
def set_restore_qp():
global bfrt, p4, PIPE_NUM, restore_qp_register
enabled_qp_register = p4.enabled_qp
queue_pairs_register = p4.qp
enabled_qp_register_entries = enabled_qp_register.get(regex=True, print_ents=False, from_hw=1)
disabled_qps = list(filter(lambda x: x.data[b"enabled_qp.f1"][PIPE_NUM] == 0, enabled_qp_register_entries))
print("There are %d disabled QPs" % len(disabled_qps))
if len(disabled_qps) > 0:
qp_register_entries = queue_pairs_register.get(regex=True, from_hw=1, print_ents=False)
bfrt.batch_begin()
for disabled_qp in disabled_qps:
register_idx = disabled_qp.key[b"$REGISTER_INDEX"]
qp_num = qp_register_entries[register_idx].data[b'qp.f1'][PIPE_NUM]
if qp_num > 0:
restore_qp_register.mod(f1=1, REGISTER_INDEX=register_idx, pipe=PIPE_NUM)
bfrt.batch_end()
def restore_qp_timer():
import threading
global restore_qp_timer, set_restore_qp
set_restore_qp()
threading.Timer(1, restore_qp_timer).start()
##############################
##### OVERLOADED SERVERS #####
##############################
# This section creates a timer that calls a callback that check RDMA servers links bandwidth utilization.
# When a link carries above a user-configured back-off RDMA threshold (PORT_THRESHOLD),
# the system stops sending payloads to the overloaded server.
PORT_THRESHOLD = 95000000000 # 95Gbps
prev_port_rate = {
key: 0 for key in SERVER_PORT_TO_IDX.keys()
}
active_server_indexes = set(SERVER_PORT_TO_IDX.values())
def disable_overloaded_servers():
global bfrt, p4, NUMBER_OF_SERVERS, MAX_QP_NUM, SERVER_PORT_TO_IDX, PORT_THRESHOLD, prev_port_rate, \
active_server_indexes
servers_port_stats = filter(
lambda x: x.key[b'$DEV_PORT'] in SERVER_PORT_TO_IDX.keys(),
bfrt.port.port_stat.get(regex=True, print_ents=False)
)
selector_entry = p4.Ingress.qp_mapping_sel.get(SELECTOR_GROUP_ID=1, print_ents=False, from_hw=1)
for stats in servers_port_stats:
server_port = int(stats.key[b'$DEV_PORT'])
server_idx = SERVER_PORT_TO_IDX[server_port]
current_rate = stats.data[b'$OctetsTransmittedTotal']
if prev_port_rate[server_port] > 0:
port_bps = (current_rate - prev_port_rate[server_port]) * 8
if port_bps > PORT_THRESHOLD:
if server_idx in active_server_indexes:
print(f"Server {server_idx} overloaded.")
active_server_indexes.remove(server_idx)
for qp_idx in range(MAX_QP_NUM):
offset_qp_idx = qp_idx + (MAX_QP_NUM * server_idx)
selector_entry.data[b'$ACTION_MEMBER_STATUS'][offset_qp_idx] = False
# You cannot push a selector entry with all entries to false, so keep one entry alive
if not any(selector_entry.data[b'$ACTION_MEMBER_STATUS']):
selector_entry.data[b'$ACTION_MEMBER_STATUS'][(MAX_QP_NUM * server_idx)] = True
elif port_bps < PORT_THRESHOLD - 5000000000:
if server_idx not in active_server_indexes:
active_server_indexes.add(server_idx)
for qp_idx in range(MAX_QP_NUM):
offset_qp_idx = qp_idx + (MAX_QP_NUM * server_idx)
selector_entry.data[b'$ACTION_MEMBER_STATUS'][offset_qp_idx] = True
print(f"Server {server_idx} restored.")
prev_port_rate[server_port] = current_rate
selector_entry.push()
def overloaded_servers_timer():
import threading
global overloaded_servers_timer, disable_overloaded_servers
disable_overloaded_servers()
threading.Timer(1, overloaded_servers_timer).start()
############################
##### QP MAPPING TABLE #####
############################
# This function setups the entries in the qp_mapping table.
def setup_qp_mapping_table():
global p4, NUMBER_OF_SERVERS, MAX_QP_NUM, TOTAL_QP
qp_mapping_profile = p4.Ingress.qp_mapping_profile
qp_mapping_sel = p4.Ingress.qp_mapping_sel
qp_mapping_table = p4.Ingress.qp_mapping
qp_mapping_table.clear()
server_profiles = []
for server_idx in range(NUMBER_OF_SERVERS):
for qp_idx in range(MAX_QP_NUM):
offset_qp_idx = qp_idx + (MAX_QP_NUM * server_idx)
qp_mapping_profile.add_with_to_qp_and_server(
ACTION_MEMBER_ID=offset_qp_idx,
selected_qp=offset_qp_idx,
selected_server=server_idx
)
server_profiles.append(offset_qp_idx)
qp_mapping_sel.entry(SELECTOR_GROUP_ID=1,
MAX_GROUP_SIZE=TOTAL_QP,
ACTION_MEMBER_ID=server_profiles,
ACTION_MEMBER_STATUS=[True] * TOTAL_QP
).push()
qp_mapping_table.add(to_split=1, SELECTOR_GROUP_ID=1)
bfrt.complete_operations()
###########################
##### BLACKLIST TABLE #####
###########################
# This function setups the entries in the blacklist table.
# You can add/edit/remove entries to disable payload splitting on specific traffic classes.
def setup_blacklist_table():
from ipaddress import ip_address
global p4, NF_PORT
blacklist_table = p4.Ingress.blacklist
blacklist_table.clear()
blacklist_table.add_with_drop(dst_addr=ip_address('224.0.0.0'), dst_addr_p_length=16)
blacklist_table.add_with_send(dst_addr=ip_address('10.0.0.1'), dst_addr_p_length=32, port=0)
##########################################
##### RUN access.txt FILE IN bfshell #####
##########################################
p = subprocess.Popen([os.path.join(os.environ['SDE'], "run_bfshell.sh"), '-f', os.path.join(curr_path, "access.txt")])
try:
p.wait(3)
except subprocess.TimeoutExpired:
p.kill()
######################################
##### SETUP PORTS, TM AND MIRROR #####
######################################
setup_ports()
setup_tm_pools()
setup_mirror_sessions_table()
#######################
##### TABLE SETUP #####
#######################
setup_blacklist_table()
setup_qp_mapping_table()
########################
##### TIMERS SETUP #####
########################
restore_qp_timer()
overloaded_servers_timer()
port_stats_timer() # Comment out to disable port stats