forked from p4lang/p4app-switchML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
660 lines (550 loc) · 21.7 KB
/
cli.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# Copyright 2021 Intel-KAUST-Microsoft
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import inspect
import logging
import readline
from cmd import Cmd
from colors import color #ansicolors
from common import front_panel_regex, mac_address_regex, validate_ip
class CommandError(Exception):
''' Command failed '''
pass
class Cli(Cmd, object):
doc_header = 'Available commands (type help <cmd>):'
ruler = '='
def __init__(self, *args, **kwargs):
super(Cli, self).__init__(*args, **kwargs)
self.log = logging.getLogger(__name__)
def setup(self,
controller,
prompt='',
stdin=None,
use_rawinput=None,
name=''):
self.ctrl = controller
self.prompt = '{}>'.format(prompt)
self.name = name
if stdin is not None:
self.stdin = stdin
if use_rawinput is not None:
self.use_rawinput = use_rawinput
# Remove -:/ from word delimiters
delims = readline.get_completer_delims()
delims = delims.replace('-', '')
delims = delims.replace(':', '')
delims = delims.replace('/', '')
readline.set_completer_delims(delims)
def out(self, message, clr=None):
''' Print output message '''
print(color('\n{}\n'.format(message), fg=clr))
def doc(self, message):
''' Print command documentation '''
self.out(message, 'yellow')
def error(self, message):
''' Print error message '''
self.out(message, 'red')
def default(self, line):
self.error('Unknown command: {}'.format(line))
return self.do_help('')
def emptyline(self):
# Do nothing
pass
def do_help(self, arg):
''' List available commands with 'help' or detailed help with 'help <cmd>' '''
if arg:
try:
# Get help message from docstring
method = getattr(self, 'do_' + arg)
doc_lines = inspect.getdoc(method).splitlines(True)
if doc_lines:
# Insert command on top
doc_lines.insert(0, '{}\n'.format(arg))
self.doc(' '.join(doc_lines))
return
except AttributeError:
pass
# Use default help method
super(Cli, self).do_help(arg)
def cmdloop(self, intro=None):
self.out(self.name)
while True:
try:
super(Cli, self).cmdloop(intro='')
break
except KeyboardInterrupt:
self.doc('Type exit to exit from {}'.format(self.name))
except Exception as e:
self.log.exception(e)
self.error('Unexpected error. See log for details')
def run(self):
self.cmdloop()
def do_exit(self, line):
''' Quit the CLI '''
self.out('Bye!')
return True
do_EOF = do_exit
def do_history(self, line):
''' Show commands history '''
if line:
self.error('Unknown parameter: {}'.format(line.strip()))
else:
msg = ''
for i in range(readline.get_current_history_length() - 1):
msg += '{}: {}\n'.format(i, readline.get_history_item(i + 1))
self.out(msg)
# Commands
def do_show_ports(self, line):
''' Show active ports. If a front-panel/lane is provided,
only that port will be shown.
'''
port = None
lane = None
stats = None
try:
if line:
re_match = front_panel_regex.match(line.strip())
if re_match and re_match.group(1):
port = int(re_match.group(1))
if not (1 <= port and port <= 64):
raise CommandError('Port number invalid')
else:
raise CommandError('Port number invalid')
if re_match.group(2):
lane = int(re_match.group(2))
if lane not in range(0, 4):
raise CommandError('Invalid lane')
else:
lane = 0
success, stats = self.ctrl.ports.get_stats(port, lane)
if not success:
raise CommandError(stats)
except CommandError as e:
self.error(e)
return
format_string = (
' {$PORT_NAME:^4} {$PORT_UP:^2} {$IS_VALID:^5} {$PORT_ENABLE:^7}' +
' {$SPEED:^5} {$FEC:^4} {packets_sent:^16} {bytes_sent:^16}' +
' {packets_received:^16} {bytes_received:^16}' +
' {errors_received:^16} {errors_sent:^16}' +
' {FCS_errors_received:^16}\n')
header = {
'$PORT_NAME': 'Port',
'$PORT_UP': 'Up',
'$IS_VALID': 'Valid',
'$PORT_ENABLE': 'Enabled',
'$SPEED': 'Speed',
'$FEC': ' FEC',
'bytes_received': 'Rx Bytes',
'bytes_sent': 'Tx Bytes',
'packets_received': 'Rx Packets',
'packets_sent': 'Tx Packets',
'errors_received': 'Rx Errors',
'errors_sent': 'Tx Errors',
'FCS_errors_received': 'FCS Errors'
}
msg = format_string.format(**header)
for v in stats:
msg += format_string.format(**v)
self.out(msg)
def do_set_switch_address(self, line):
''' Set switch MAC and IP to be used for SwitchML.
Usage: set_address <MAC address> <IPv4 address>
'''
try:
if not line:
raise CommandError(
'Usage: set_switch_address <MAC address> <IPv4 address>')
# Parameters validation
args = line.upper().strip().split()
if len(args) != 2:
raise CommandError(
'Usage: set_switch_address <MAC address> <IPv4 address>')
if not mac_address_regex.match(args[0]):
raise CommandError('Invalid MAC address')
if not validate_ip(args[1]):
raise CommandError('Invalid IP address')
self.ctrl.set_switch_mac_and_ip(args[0], args[1])
except CommandError as e:
self.error(e)
def do_show_switch_address(self, line):
''' Show switch MAC and IP '''
if line:
self.error('Unknown parameter: {}'.format(line))
else:
mac, ip = self.ctrl.get_switch_mac_and_ip()
self.out('Switch MAC: {} IP: {}'.format(mac, ip))
def do_show_forwarding_table(self, line):
''' Show the forwarding table. If a MAC address is provided,
only the entry for that address (if present) will be shown
'''
entries = []
try:
if line:
mac = line.strip()
re_match = mac_address_regex.match(mac)
if re_match:
success, dev_port = self.ctrl.forwarder.get_dev_port(mac)
if not success:
raise CommandError(dev_port)
success, fp_port, fp_lane = self.ctrl.ports.get_fp_port(
dev_port)
if not success:
raise CommandError(fp_port)
else:
entry = {
'mac': mac,
'port': '{}/{}'.format(fp_port, fp_lane)
}
entries.append(entry)
else:
raise CommandError('Invalid MAC address')
else:
for mac, dev_port in self.ctrl.forwarder.get_entries():
success, fp_port, fp_lane = self.ctrl.ports.get_fp_port(
dev_port)
if not success:
self.error(fp_port)
else:
entry = {
'mac': mac,
'port': '{}/{}'.format(fp_port, fp_lane)
}
entries.append(entry)
if len(entries) == 0:
self.out('No entries')
return
entries.sort(key=lambda x: x['port'])
format_string = (' {mac:^17} {port:^4}\n')
header = {'mac': 'MAC address', 'port': 'Port'}
msg = format_string.format(**header)
for v in entries:
msg += format_string.format(**v)
self.out(msg)
except CommandError as e:
self.error(e)
def do_set_drop_probabilities(self, line):
''' Set ingress and egress drop probabilities for simulated drops.
Usage: set_drop_probabilities <ingress probability> <egress probability>
'''
try:
if not line:
raise CommandError(
'Usage: set_drop_probabilities <ingress probability> <egress probability>'
)
# Parameters validation
args = line.strip().split()
if len(args) != 2:
raise CommandError(
'Usage: set_drop_probabilities <ingress probability> <egress probability>'
)
success, ig_prob, eg_prob = self.ctrl.drop_simulator.set_drop_probabilities(
float(args[0]), float(args[1]))
if not success:
self.error(ig_prob)
else:
self.out('Drop probabilities: Ingress: {:.2f}% Egress: {:.2f}%'.
format(ig_prob * 100, eg_prob * 100))
except ValueError:
self.error('Drop probabilities must be real numbers')
except CommandError as e:
self.error(e)
def do_show_drop_probabilities(self, line):
''' Show ingress and egress drop probabilities for simulated drops '''
if line:
self.error('Unknown parameter: {}'.format(line))
else:
ig_prob, eg_prob = self.ctrl.drop_simulator.get_drop_probabilities()
self.out(
'Drop probabilities: Ingress: {:.2f}% Egress: {:.2f}%'.format(
ig_prob * 100, eg_prob * 100))
def do_show_dropped_packets(self, line):
''' Show the number of packets dropped by the drop simulator per-QP.
If a queue pair number is provided, only the value for that queue pair
will be shown.
'''
values = None
if line:
try:
qpn = int(line.strip())
values = self.ctrl.drop_simulator.get_counter(qpn)
except ValueError:
self.error('The argument must be a valid queue pair number')
return
else:
values = self.ctrl.drop_simulator.get_counter()
if not values:
self.out('No matching queue pair number')
return
# Remove entries with zero packets
values = [v for v in values if v['packets'] != 0]
if not values:
self.out('All values are zero')
return
values.sort(key=lambda x: x['QP'])
format_string = ('{QP:^5} {packets:^10}\n')
header = {'QP': 'QP', 'packets': 'Packets'}
msg = format_string.format(**header)
for v in values:
msg += format_string.format(**v)
self.out(msg)
def _show_workers(self, sender, receiver, line):
''' Show the number of sent and received packets/bytes per worker.
If a worker ID is provided, only the value for that worker
will be shown.
'''
sent = recvd = None
if line:
try:
worker_id = int(line.strip())
recvd = receiver.get_workers_counter(worker_id)
sent = sender.get_workers_counter(worker_id)
except ValueError:
self.error('The argument must be a valid worker ID')
return
else:
recvd = receiver.get_workers_counter()
sent = sender.get_workers_counter()
if not sent and not recvd:
self.out('No matching workers')
return
elif (sent and not recvd) or (not sent and recvd):
self.log.error('Sender and Receiver entries mismatch')
self.error('Unexpected error. See log for details')
return
# Merge sent and received
for id in sent.keys():
if id not in recvd:
self.log.error(
'Sender and Receiver entries mismatch. Missing worker {}'.
format(id))
self.error('Unexpected error. See log for details')
return
recvd[id].update(sent[id])
ids = sorted(recvd.keys())
header1_format_string = ' {:^44} {:^19} {:^19}\n'
format_string = (
' {ID:^10} {MAC:^17} {IP:^15} {rpkts:^10}/{rbytes:^13} {spkts:^10}/{sbytes:^13}\n'
)
header2 = {
'ID': 'Worker ID',
'MAC': 'Worker MAC',
'IP': 'Worker IP',
'rpkts': 'Packets',
'rbytes': 'Bytes',
'spkts': 'Packets',
'sbytes': 'Bytes'
}
msg = header1_format_string.format(
'', 'Received', 'Sent') + format_string.format(**header2)
for id in ids:
try:
msg += format_string.format(ID=id, **recvd[id])
except KeyError:
self.log.exception('Error for worker {}: {}'.format(
id, recvd[id]))
self.out(msg)
def do_show_rdma_workers(self, line):
''' Show the number of sent and received packets/bytes per RDMA worker.
If a worker ID is provided, only the value for that worker
will be shown.
'''
self._show_workers(self.ctrl.rdma_sender, self.ctrl.rdma_receiver, line)
def do_show_queue_pairs_counters(self, line):
''' Show the number of packets and messages received per queue pair.
If one integer argument N is provided, it will show only the first
N queue pairs per worker. If two integer arguments S and N are
provided, it will show only the elements with indices [S, S+N]
per worker.
'''
values = None
if line:
start = 0
count = None
try:
args = line.strip().split()
if len(args) == 1:
count = int(args[0])
elif len(args) > 1:
start = int(args[0])
count = int(args[1])
values = self.ctrl.rdma_receiver.get_queue_pairs_counters(
start, count)
except ValueError:
self.error('The arguments must be integers')
return
else:
values = self.ctrl.rdma_receiver.get_queue_pairs_counters()
if not values:
self.out('No queue pairs currently in use')
return
# Remove entries with zero packets
values = [v for v in values if v['packets'] != 0]
if not values:
self.out('All values are zero')
return
values.sort(key=lambda x: x['qp_index'])
format_string = (
' {qp_index:^16} {worker_id:^10} {qpn:^24} {packets:^10} {messages:^10} {sequence_violations:^19}\n'
)
header = {
'qp_index': 'Queue Pair Index',
'worker_id': 'Worker ID',
'qpn': 'Worker Queue Pair Number',
'packets': 'Packets',
'messages': 'Messages',
'sequence_violations': 'Sequence violations'
}
msg = format_string.format(**header)
for v in values:
msg += format_string.format(**v)
self.out(msg)
def do_show_udp_workers(self, line):
''' Show the number of sent and received packets/bytes per UDP worker.
If a worker ID is provided, only the value for that worker
will be shown.
'''
self._show_workers(self.ctrl.udp_sender, self.ctrl.udp_receiver, line)
def do_show_bitmap(self, line):
''' Show the current bitmap values per slot index. The default is
to show only the first 8 values.
If one integer argument N is provided, it will show the first
N elements. If two integer arguments S and N are
provided, it will show the elements with indices [S, S+N].
'''
values = None
if line:
start = 0
count = None
try:
args = line.strip().split()
if len(args) == 1:
count = int(args[0])
elif len(args) > 1:
start = int(args[0])
count = int(args[1])
values = self.ctrl.bitmap_checker.get_bitmap(start, count)
except ValueError:
self.error('The arguments must be integers')
return
else:
values = self.ctrl.bitmap_checker.get_bitmap()
if not values:
self.out('No elements found')
return
# Merge entries for 2 sets
bitmap_table = {}
pipes = set()
for v in values:
pipes.add(v['pipe'])
# Convert bitmap to hex
v['bitmap'] = '0x{:08X}'.format(v['bitmap'])
if v['index'] not in bitmap_table:
bitmap_table[v['index']] = {}
bitmap_table[v['index']]['pipe{}set{}'.format(
v['pipe'], v['set'])] = v['bitmap']
pipes = sorted(pipes)
# Linearize
values = []
for index, data in bitmap_table.items():
data['index'] = index
values.append(data)
values.sort(key=lambda x: x['index'])
format_string = '{index:^9}'
header1 = format_string.format(index='')
header2 = {'index': 'Index'}
for pipe in pipes:
header1 += '{:^24s}'.format('Pipe {}'.format(pipe))
format_string += ('{{pipe{0}set0:^12}}'
'{{pipe{0}set1:^12}}').format(pipe)
header2['pipe{}set0'.format(pipe)] = 'Set 0'
header2['pipe{}set1'.format(pipe)] = 'Set 1'
header1 += '\n'
format_string += '\n'
msg = header1 + format_string.format(**header2)
for v in values:
msg += format_string.format(**v)
self.out(msg)
def do_show_statistics(self, line):
''' Show the number of SwitchML packets broadcasted, recirculated
retransmitted, and dropped per slot index.
The default is to show only the first 8 values.
If one integer argument N is provided, it will show the first
N elements. If two integer arguments S and N are
provided, it will show the elements with indices [S, S+N].
'''
values = None
if line:
start = 0
count = None
try:
args = line.strip().split()
if len(args) == 1:
count = int(args[0])
elif len(args) > 1:
start = int(args[0])
count = int(args[1])
values = self.ctrl.next_step_selector.get_counters(start, count)
except ValueError:
self.error('The arguments must be integers')
return
else:
values = self.ctrl.next_step_selector.get_counters()
if not values:
self.out('No elements found')
return
# We need the names in this exact order
counters_names = ['broadcast', 'recirculate', 'retransmit', 'drop']
# Merge entries for 2 sets
counters_table = {}
for v in values:
if v['index'] not in counters_table:
counters_table[v['index']] = {}
for name in counters_names:
counters_table[v['index']]['{}_set{}'.format(
name, v['set'])] = v[name]
# Linearize
values = []
for index, data in counters_table.items():
data['index'] = index
values.append(data)
values.sort(key=lambda x: x['index'])
header1_format_string = '{index:^9}'
format_string = '{index:^9}'
header1 = {
'index': '',
'broadcast': 'Broadcasted',
'recirculate': 'Recirculated',
'retransmit': 'Retransmitted',
'drop': 'Dropped'
}
header2 = {'index': 'Index'}
for name in counters_names:
header1_format_string += '{{{}:^20s}}'.format(name)
format_string += '{{{0}_set0:^10}}{{{0}_set1:^10}}'.format(name)
header2['{}_set0'.format(name)] = 'Set 0'
header2['{}_set1'.format(name)] = 'Set 1'
header1_format_string += '\n'
format_string += '\n'
msg = header1_format_string.format(**header1) + format_string.format(
**header2)
for v in values:
msg += format_string.format(**v)
self.out(msg)
def do_reset_workers(self, line):
''' Reset all workers state '''
if line:
self.error('Unknown parameter: {}'.format(line))
else:
self.ctrl.reset_workers()
self.out('Workers state reset')