-
Notifications
You must be signed in to change notification settings - Fork 329
/
p4dbg.py
executable file
·1212 lines (1034 loc) · 37.4 KB
/
p4dbg.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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Copyright 2013-present Barefoot Networks, Inc.
#
# 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.
#
#
# Antonin Bas ([email protected])
#
#
import nnpy
import struct
import sys
import json
import argparse
import cmd
from collections import defaultdict
from functools import wraps
try:
import runtime_CLI
with_runtime_CLI = True
except:
with_runtime_CLI = False
parser = argparse.ArgumentParser(description='BM nanomsg debugger')
parser.add_argument('--socket', help='Nanomsg socket to which to subscribe',
action="store", required=False)
parser.add_argument('--thrift-port', help='Thrift server port for table updates',
type=int, action="store", default=9090)
parser.add_argument('--thrift-ip', help='Thrift IP address for table updates. If both --socket and --json are provided, then Thrift will not be used.',
type=str, action="store", default='localhost')
parser.add_argument('--json', help='JSON description of P4 program',
action="store", required=False)
args = parser.parse_args()
class FieldMap:
def __init__(self):
self.reset()
self.hidden_fields = [("$valid$", 1, False)]
def reset(self):
self.fields = {}
self.fields_rev = {}
def load_names(self, json_cfg):
self.reset()
header_types_map = {}
json_ = json.loads(json_cfg)
# special case where the switch was started with an empty config
if len(json_.keys()) == 0:
return
header_types = json_["header_types"]
for h in header_types:
header_type = h["name"]
header_types_map[header_type] = h
header_instances = json_["headers"]
def add_field(idx, t):
f_name, f_nbits = t[0], t[1]
e = (".".join([header, f_name]), f_nbits)
self.fields[(h["id"], idx)] = e
self.fields_rev[e[0]] = (h["id"], idx)
for h in header_instances:
header = h["name"]
header_type = header_types_map[h["header_type"]]
idx = 0
for t in header_type["fields"]:
add_field(idx, t)
idx += 1
for t in self.hidden_fields:
add_field(idx, t)
idx += 1
def get_name(self, header_id, offset):
return self.fields[(header_id, offset)][0]
def get_nbits(self, header_id, offset):
return self.fields[(header_id, offset)][1]
def get_id_from_name(self, name):
return self.fields_rev[name]
def get_all_fields(self):
return self.fields_rev.keys()
field_map = FieldMap()
def get_name(header_id, offset):
return field_map.get_name(header_id, offset)
def get_nbits(header_id, offset):
return field_map.get_nbits(header_id, offset)
def get_field_id_from_name(name):
header_id, offset = field_map.get_id_from_name(name)
return (header_id << 32) | offset
def get_name_from_field_id(fid):
header_id = fid >> 32
offset = fid & 0xffffffff
return get_name(header_id, offset)
def get_all_fields():
return field_map.get_all_fields()
def enum(type_name, *sequential, **named):
enums = dict(list(zip(sequential, list(range(len(sequential))))), **named)
reverse = dict((value, key) for key, value in enums.items())
@staticmethod
def to_str(x):
return reverse[x]
enums['to_str'] = to_str
@staticmethod
def from_str(x):
return enums[x]
enums['from_str'] = from_str
return type(type_name, (), enums)
class ObjectMap:
ObjType = enum('ObjType',
'PARSER', 'PARSE STATE', 'CONTROL', 'TABLE')
class _Map:
def __init__(self):
self.pname = None
self.names = {}
self.names_rev = {}
def add_one(self, id_, name):
self.names[id_] = name
self.names_rev[name] = id_
def __init__(self):
self.reset()
def reset(self):
self.store = defaultdict(ObjectMap._Map)
self.ids = {
1: "parser",
2: "parse_state",
3: "pipeline",
4: "table",
5: "condition",
6: "action",
7: "deparser",
}
self.ids_rev = {}
for k, v in self.ids.items():
self.ids_rev[v] = k
def load_names(self, json_cfg):
json_ = json.loads(json_cfg)
# special case where the switch was started with an empty config
if len(json_.keys()) == 0:
return
for type_ in {"parser", "deparser", "action", "pipeline"}:
_map = self.store[type_]
_map.pname = type_
json_list = json_[type_ + "s"]
for obj in json_list:
_map.add_one(obj["id"], obj["name"])
# self.store["pipeline"].pname = "control"
self.store["pipeline"].pname = "pipeline"
for pipeline in json_["pipelines"]:
_map = self.store["table"]
_map.pname = "table"
tables = pipeline["tables"]
for obj in tables:
_map.add_one(obj["id"], obj["name"])
_map = self.store["condition"]
_map.pname = "condition"
conds = pipeline["conditionals"]
for obj in conds:
_map.add_one(obj["id"], obj["name"])
for parser in json_["parsers"]:
_map = self.store["parse_state"]
_map.pname = "parse_state"
parse_states = parser["parse_states"]
for obj in parse_states:
_map.add_one(obj["id"], obj["name"])
def resolve_ctr(self, ctr):
type_id = ctr >> 24
obj_id = ctr & 0xffffff
type_id = type_id & 0x7f
assert(type_id in self.ids)
type_ = self.ids[type_id]
_map = self.store[type_]
return _map.pname, _map.names[obj_id]
def lookup_ctr(self, type_name, obj_name):
type_id = self.ids_rev[type_name]
_map = self.store[type_name]
obj_id = _map.names_rev[obj_name]
ctr = type_id << 24
ctr |= obj_id
return ctr
obj_map = ObjectMap()
def resolve_ctr(ctr):
return obj_map.resolve_ctr(ctr)
def lookup_ctr(type_name, obj_name):
return obj_map.lookup_ctr(type_name, obj_name)
MsgType = enum('MsgType',
'PACKET_IN', 'PACKET_OUT',
'FIELD_VALUE',
'CONTINUE',
'NEXT',
'GET_VALUE',
'GET_BACKTRACE', 'BACKTRACE',
'BREAK_PACKET_IN', 'REMOVE_PACKET_IN',
'STOP_PACKET_IN', 'RESUME_PACKET_IN',
'FILTER_NOTIFICATIONS',
'SET_WATCHPOINT', 'UNSET_WATCHPOINT',
'STATUS',
'RESET',
'KEEP_ALIVE',
'ATTACH', 'DETACH',)
class Msg(object):
def __init__(self, **kwargs):
self.type = None
def extract(self, msg):
return 0
def generate(self):
return b""
def __str__(self):
return ""
def make_extract_function(P, fmt):
def extract(self, msg):
# investigate why super() not working?
# offset = super(P, self).extract(msg)
offset = P.extract(self, msg)
for name, uf, src in fmt:
if type(uf) is int:
width = getattr(self, src)
f = ">%ds" % (uf * width)
else:
assert(src is None)
assert(type(uf) is str)
f = uf
width = struct.calcsize(f)
v, = struct.unpack_from(f, msg, offset)
offset += width
setattr(self, name, v)
if type(uf) is int:
if width == 0: # supports VL fields with width 0
v_str = 'Empty'
v_int = 0
else:
# only available in Python 3.8 :(
# v_str = v.hex(':')
v_str = ':'.join(['{:02x}'.format(x) for x in v])
v_int = int(v.hex(), 16)
setattr(self, name + "_str", v_str)
setattr(self, name + "_int", v_int)
return offset
return extract
def make_str_function(P, fmt):
def str_func(self):
s = P.__str__(self)
for name, uf, _ in fmt:
s += name + ": "
if type(uf) is int:
s += getattr(self, name + "_str")
s += " (" + str(getattr(self, name + "_int")) + ") "
else:
s += str(getattr(self, name))
s += "\t"
return s
return str_func
def make_generate_function(P, fmt):
def generate(self):
s = P.generate(self)
for name, uf, src in fmt:
v = getattr(self, name)
if type(uf) is int:
width = getattr(self, src)
f = ">%ds" % (uf * width)
else:
assert(src is None)
assert(type(uf) is str)
f = uf
s += struct.pack(f, v)
return s
return generate
def make_init_function(P, t, fmt):
def init_func(self, **kwargs):
P.__init__(self, **kwargs)
self.type = t
if not fmt:
return
for k, v in kwargs.items():
if k not in list(zip(*fmt))[0]:
continue
setattr(self, k, v)
return init_func
def decorate_msg(P, t, fmt):
def wrapper(K):
K.__init__ = make_init_function(P, t, fmt)
K.extract = make_extract_function(P, fmt)
K.generate = make_generate_function(P, fmt)
K.__str__ = make_str_function(P, fmt)
return K
return wrapper
@decorate_msg(
Msg, None,
[("type", "i", None), ("switch_id", "Q", None), ("req_id", "Q", None)],
)
class BasicMsg(Msg):
pass
@decorate_msg(
BasicMsg, None,
[("packet_id", "Q", None), ("copy_id", "Q", None)],
)
class EventMsg(BasicMsg):
pass
@decorate_msg(
EventMsg, MsgType.PACKET_IN,
[("port", "i", None)],
)
class Msg_PacketIn(EventMsg):
pass
@decorate_msg(
EventMsg, MsgType.PACKET_OUT,
[("port", "i", None)],
)
class Msg_PacketOut(EventMsg):
pass
@decorate_msg(
EventMsg, MsgType.FIELD_VALUE,
[("fid", "Q", None), ("nbytes", "i", None), ("bytes", 1, "nbytes")],
)
class Msg_FieldValue(EventMsg):
pass
def _test():
m = Msg_FieldValue()
# < required to prevent 8-byte alignment
req = struct.pack("<iQQQQQi", MsgType.FIELD_VALUE, 0, 99, 1, 2, 55, 3)
req += struct.pack(">3s", b"\x01\x02\x03")
m.extract(req)
s = m.generate()
assert(s == req)
_test()
@decorate_msg(BasicMsg, MsgType.CONTINUE, [])
class Msg_Continue(BasicMsg):
pass
@decorate_msg(BasicMsg, MsgType.NEXT, [])
class Msg_Next(BasicMsg):
pass
@decorate_msg(
BasicMsg, MsgType.GET_VALUE,
[("packet_id", "Q", None), ("copy_id", "Q", None), ("fid", "Q", None)],
)
class Msg_GetValue(BasicMsg):
pass
@decorate_msg(
BasicMsg, MsgType.GET_BACKTRACE,
[("packet_id", "Q", None), ("copy_id", "Q", None)],
)
class Msg_GetBacktrace(BasicMsg):
pass
@decorate_msg(
BasicMsg, MsgType.BACKTRACE,
[("packet_id", "Q", None), ("copy_id", "Q", None),
("nb", "i", None), ("ctrs", 4, "nb")],
)
class Msg_Backtrace(BasicMsg):
pass
@decorate_msg(BasicMsg, MsgType.BREAK_PACKET_IN, [])
class Msg_BreakPacketIn(BasicMsg):
pass
@decorate_msg(BasicMsg, MsgType.REMOVE_PACKET_IN, [])
class Msg_RemovePacketIn(BasicMsg):
pass
@decorate_msg(BasicMsg, MsgType.STOP_PACKET_IN, [])
class Msg_StopPacketIn(BasicMsg):
pass
@decorate_msg(BasicMsg, MsgType.RESUME_PACKET_IN, [])
class Msg_ResumePacketIn(BasicMsg):
pass
@decorate_msg(
BasicMsg, MsgType.FILTER_NOTIFICATIONS,
[("nb", "i", None), ("ids", 16, "nb")],
)
class Msg_FilterNotifications(BasicMsg):
pass
@decorate_msg(
BasicMsg, MsgType.SET_WATCHPOINT,
[("fid", "Q", None)],
)
class Msg_SetWatchpoint(BasicMsg):
pass
@decorate_msg(
BasicMsg, MsgType.UNSET_WATCHPOINT,
[("fid", "Q", None)],
)
class Msg_UnsetWatchpoint(BasicMsg):
pass
@decorate_msg(
BasicMsg, MsgType.STATUS,
[("status", "i", None), ("aux", "Q", None)],
)
class Msg_Status(BasicMsg):
pass
@decorate_msg(BasicMsg, MsgType.RESET, [])
class Msg_Reset(BasicMsg):
pass
@decorate_msg(BasicMsg, MsgType.KEEP_ALIVE, [])
class Msg_KeepAlive(BasicMsg):
pass
@decorate_msg(BasicMsg, MsgType.ATTACH, [])
class Msg_Attach(BasicMsg):
pass
@decorate_msg(BasicMsg, MsgType.DETACH, [])
class Msg_Detach(BasicMsg):
pass
MsgTypeToCLS = {
MsgType.PACKET_IN : Msg_PacketIn,
MsgType.PACKET_OUT : Msg_PacketOut,
MsgType.FIELD_VALUE : Msg_FieldValue,
MsgType.CONTINUE : Msg_Continue,
MsgType.NEXT : Msg_Next,
MsgType.GET_VALUE : Msg_GetValue,
MsgType.GET_BACKTRACE : Msg_GetBacktrace,
MsgType.BACKTRACE : Msg_Backtrace,
MsgType.BREAK_PACKET_IN : Msg_BreakPacketIn,
MsgType.REMOVE_PACKET_IN : Msg_RemovePacketIn,
MsgType.STOP_PACKET_IN : Msg_StopPacketIn,
MsgType.RESUME_PACKET_IN : Msg_ResumePacketIn,
MsgType.FILTER_NOTIFICATIONS : Msg_FilterNotifications,
MsgType.SET_WATCHPOINT : Msg_SetWatchpoint,
MsgType.UNSET_WATCHPOINT : Msg_UnsetWatchpoint,
MsgType.STATUS : Msg_Status,
MsgType.RESET : Msg_Reset,
MsgType.KEEP_ALIVE : Msg_KeepAlive,
MsgType.ATTACH : Msg_Attach,
MsgType.DETACH : Msg_Detach,
}
CLSToMsgType = {}
for k, v in MsgTypeToCLS.items():
CLSToMsgType[v] = k
FIELD_ID_CTR = 0xffffffffffffffff
FIELD_ID_COND = 0xfffffffffffffffe
FIELD_ID_ACTION = 0xfffffffffffffffd
class UIn_Error(Exception):
def __init__(self, info=""):
self.info = info
def __str__(self):
return self.info
class UIn_Warning(Exception):
def __init__(self, info=""):
self.info = info
def __str__(self):
return self.info
def handle_bad_input(f):
@wraps(f)
def handle(*args, **kwargs):
try:
return f(*args, **kwargs)
except UIn_Error as e:
print("Error:", e)
except UIn_Warning as e:
print("Error:", e)
return handle
def prompt_yes_no(question, yes_default=True):
valid = {"yes": True, "y": True, "no": False, "n": False}
if yes_default:
prompt = "[Y/n]"
else:
prompt = "[y/N]"
while True:
print(question, prompt, end=' ')
choice = input().lower()
if choice in valid:
return valid[choice]
else:
print("Please respond with 'yes' or 'no' (or 'y' or 'n').")
class DebuggerAPI(cmd.Cmd):
prompt = 'P4DBG: '
intro = "Prototype for Debugger UI"
def __init__(self, addr, standard_client=None, json_cfg=None):
cmd.Cmd.__init__(self)
self.addr = addr
self.sok = nnpy.Socket(nnpy.AF_SP, nnpy.REQ)
try:
self.sok.connect(self.addr)
self.sok.setsockopt(nnpy.SOL_SOCKET, nnpy.RCVTIMEO, 500)
except:
print("Impossible to connect to provided socket (bad format?)")
sys.exit(1)
self.req_id = 0
self.switch_id = 0
# creates new attributes
self.reset()
# creates new attributes
self.json_cfg = json_cfg
self.standard_client = standard_client
self.json_dependent_init()
def json_dependent_init(self):
if not self.json_cfg:
import bmpy_utils as utils
self.json_cfg = utils.get_json_config(
standard_client=self.standard_client)
field_map.load_names(self.json_cfg)
obj_map.load_names(self.json_cfg)
if not with_runtime_CLI or not self.standard_client:
self.with_CLI = False
else:
self.with_CLI = True
self.init_runtime_CLI()
def reset(self):
self.wps = set()
self.bps = set()
# right now we never remove elements from this
self.skips = set()
self.skips_all = set()
self.current_packet_id = None
self.current_copy_id = None
self.get_me_a_prompt = False
self.attached = False
def init_runtime_CLI(self):
runtime_CLI.load_json_str(self.json_cfg)
self.runtime_CLI = runtime_CLI.RuntimeAPI(
runtime_CLI.PreType.none, self.standard_client, mc_client=None)
def attach(self):
print("Connecting to the switch...")
self.send_attach()
self.attached = True
print("Connection established")
def send_attach(self):
req = Msg_Attach(switch_id = 0, req_id = self.get_req_id())
self.sok.send(req.generate())
try:
msg = self.wait_for_msg()
except AssertionError:
print("Unable to attach to the switch,", end=' ')
print("maybe you need to run p4dbg as root?")
sys.exit(1)
self.check_msg_CLS(msg, Msg_Status)
def do_EOF(self, line):
print("Detaching from switch")
self.say_bye()
return True
def say_bye(self):
# This whole function is kind of hacky
self.sok.setsockopt(nnpy.SOL_SOCKET, nnpy.RCVTIMEO, 500)
self.sok.setsockopt(nnpy.SOL_SOCKET, nnpy.SNDTIMEO, 500)
req = Msg_Detach(switch_id = 0, req_id = self.get_req_id())
status = self.sok.send(req.generate())
try:
msg = self.wait_for_msg()
except:
return
self.check_msg_CLS(msg, Msg_Status)
self.wps = set()
def send_reset(self):
req = Msg_Reset(switch_id = 0, req_id = self.get_req_id())
self.sok.send(req.generate())
msg = self.wait_for_msg()
self.check_msg_CLS(msg, Msg_Status)
self.reset()
def packet_id_str(self, packet_id, copy_id):
return ".".join([str(packet_id), str(copy_id)])
def at_least_n_args(self, args, n):
if len(args) < n:
raise UIn_Error("Insufficient number of args")
def exactly_n_args(self, args, n):
if len(args) != n:
raise UIn_Error(
"Wrong number of args, expected %d but got %d" % (n, len(args))
)
def handle_config_change(self):
self.reset()
print("We have been notified that the JSON config has changed on the", end=' ')
print("switch. The debugger state has therefore been reset.")
if not self.standard_client:
print("Unable to request new config from switch because Thrift is unavailable")
sys.exit(0)
print("You can request the new config and keep debugging,", end=' ')
print("or we will exit.")
v = prompt_yes_no("Do you want to request the new config?", True)
if v:
self.json_dependent_init()
else:
sys.exit(0)
def continue_or_next(self, req, is_next):
while True:
self.sok.send(req.generate())
while True:
try:
msg = self.wait_for_msg()
break
except KeyboardInterrupt:
self.get_me_a_prompt = True
except AssertionError:
# happens when there is a connection timeout
print("Connection to the switch lost,", end=' ')
print("are you sure it is still running?")
return
if msg.type == MsgType.KEEP_ALIVE:
if self.get_me_a_prompt == True:
self.get_me_a_prompt = False
return
elif msg.type == MsgType.STATUS:
assert(msg.status == 999) # config change
self.handle_config_change()
return
else:
self.check_msg_CLS(msg, EventMsg)
if msg.packet_id in self.skips_all:
continue
if (msg.packet_id, msg.copy_id) in self.skips:
continue
# a little hacky
if (not is_next) and (msg.type == MsgType.FIELD_VALUE) and\
(msg.fid == FIELD_ID_CTR) and (msg.bytes_int not in self.bps):
continue
break
self.current_packet_id = msg.packet_id
self.current_copy_id = msg.copy_id
print("New event for packet", self.packet_id_str(msg.packet_id, msg.copy_id))
return msg
def special_update(self, id_, v):
if id_ == FIELD_ID_CTR:
type_name, obj_name = resolve_ctr(v)
if 0x80 & (v >> 24):
print("Exiting", type_name, "'%s'" % obj_name)
else:
print("Entering", type_name, "'%s'" % obj_name)
elif id_ == FIELD_ID_COND:
print("Condition evaluated to", bool(v))
@handle_bad_input
def do_continue(self, line):
"Starts / resumes packet processing: continue(c)"
req = Msg_Continue(switch_id = 0, req_id = self.get_req_id())
msg = self.continue_or_next(req, False)
if msg is None:
return
if msg.type == MsgType.FIELD_VALUE:
if msg.fid in {FIELD_ID_CTR, FIELD_ID_COND, FIELD_ID_ACTION}:
self.special_update(msg.fid, msg.bytes_int)
return
assert(msg.fid in self.wps)
print("Watchpoint hit for field", get_name_from_field_id(msg.fid))
print("New value is", msg.bytes_str, "(%d)" % msg.bytes_int)
elif msg.type == MsgType.PACKET_IN:
print("Packet in!")
# elif msg.type == MsgType.PACKET_OUT:
# pass
else:
assert(0)
@handle_bad_input
def do_next(self, line):
"Go to next field update: next(n)"
req = Msg_Next(switch_id = 0, req_id = self.get_req_id())
msg = self.continue_or_next(req, True)
if msg is None:
return
if msg.type == MsgType.FIELD_VALUE:
if msg.fid in {FIELD_ID_CTR, FIELD_ID_COND, FIELD_ID_ACTION}:
self.special_update(msg.fid, msg.bytes_int)
return
print("Update for field", get_name_from_field_id(msg.fid))
print("New value is", msg.bytes_str, "(%d)" % msg.bytes_int)
elif msg.type == MsgType.PACKET_IN:
print("Packet in!")
# elif msg.type == MsgType.PACKET_OUT:
# pass
else:
assert(0)
@handle_bad_input
def do_reset(self, line):
"Resets all state, including at the switch"
self.send_reset()
def field_id_from_name(self, field_name):
if field_name == "$cond":
return FIELD_ID_COND
elif field_name == "$action":
return FIELD_ID_ACTION
else:
try:
field_id = get_field_id_from_name(field_name)
return field_id
except:
raise UIn_Error("Unknow field name '%s'" % field_name)
@handle_bad_input
def do_set_wp(self, line):
"Sets a watchpoint on a field: set_wp(w) <field_name>"
args = line.split()
self.exactly_n_args(args, 1)
field_id = self.field_id_from_name(args[0])
req = Msg_SetWatchpoint(
switch_id = 0, req_id = self.get_req_id(), fid = field_id
)
self.sok.send(req.generate())
msg = self.wait_for_msg()
self.check_msg_CLS(msg, Msg_Status)
assert(msg.status == 0)
self.wps.add(field_id)
def complete_set_wp(self, text, line, start_index, end_index):
fields = sorted(get_all_fields())
if not text:
return fields
return [f for f in fields if f.startswith(text)]
@handle_bad_input
def do_unset_wp(self, line):
"Unsets a watchpoint on a field: unset_wp <field_name>"
args = line.split()
self.exactly_n_args(args, 1)
field_id = self.field_id_from_name(args[0])
if field_id not in self.wps:
raise UIn_Warning("No watchpoint previously set for this field")
req = Msg_UnsetWatchpoint(
switch_id = 0, req_id = self.get_req_id(), fid = field_id
)
self.sok.send(req.generate())
msg = self.wait_for_msg()
self.check_msg_CLS(msg, Msg_Status)
assert(msg.status == 0)
self.wps.remove(field_id)
def complete_unset_wp(self, text, line, start_index, end_index):
fields = sorted([get_name_from_field_id(f) for f in self.wps])
if not text:
return fields
return [f for f in fields if f.startswith(text)]
@handle_bad_input
def do_show_wps(self, line):
"Shows all the watchpoints set: show_wps"
for f_name in map(get_name_from_field_id, self.wps):
print(f_name)
@handle_bad_input
def do_break(self, line):
"Sets a break point for a parser, parse_state, pipeline, table, condition, action or deparser: break(b) <obj type (e.g. parser)> <obj name (e.g. start)>"
args = line.split()
self.exactly_n_args(args, 2)
try:
ctr = lookup_ctr(args[0], args[1])
except:
raise UIn_Error("Invalid object for breakpoint")
if not self.bps:
req = Msg_SetWatchpoint(
switch_id = 0, req_id = self.get_req_id(), fid = FIELD_ID_CTR
)
self.sok.send(req.generate())
msg = self.wait_for_msg()
self.check_msg_CLS(msg, Msg_Status)
assert(msg.status == 0)
self.bps.add(ctr)
def complete_break(self, text, line, start_index, end_index):
args = line.split()
args_cnt = len(args)
obj_types = obj_map.ids.values()
fields = sorted(get_all_fields())
if (args_cnt == 1 and not text) or\
(args_cnt == 2 and text):
return [t for t in obj_types if t.startswith(text)]
if (args_cnt == 2 and not text) or\
(args_cnt == 3 and text):
obj_type = args[1]
obj_names = obj_map.store[obj_type].names.values()
return [n for n in obj_names if n.startswith(text)]
return []
@handle_bad_input
def do_delete(self, line):
"Unsets a break point, previously set with the 'break' command: delete(d) <obj_type> <obj_name>"
args = line.split()
self.exactly_n_args(args, 2)
try:
ctr = lookup_ctr(args[0], args[1])
except:
raise UIn_Error("Invalid object for breakpoint")
if ctr not in self.bps:
raise UIn_Warning("No breakpoint previously set for this object")
self.bps.remove(ctr)
def complete_delete(self, text, line, start_index, end_index):
args = line.split()
args_cnt = len(args)
if not self.bps:
return []
objs = list(map(resolve_ctr, self.bps))
if (args_cnt == 1 and not text) or\
(args_cnt == 2 and text):
return [t for t in zip(*objs)[0] if t.startswith(text)]
if (args_cnt == 2 and not text) or\
(args_cnt == 3 and text):
return [n for t, n in objs if t == args[1] and n.startswith(text)]
return []
@handle_bad_input
def do_show_bps(self, line):
"Shows all the breakpoints set: show_bps"
for t, n in map(resolve_ctr, self.bps):
print(t, "'%s'" % n)
def get_req_id(self):
req_id = self.req_id
self.req_id += 1
return req_id
def current_packet_id_str(self):
return ".".join([str(self.current_packet_id),
str(self.current_copy_id)])
def packet_id_from_str(self, id_):
try:
packet_id, copy_id = id_.split(".")
packet_id, copy_id = int(packet_id), int(copy_id)
return packet_id, copy_id
except:
raise UIn_Error("Bad packet id format '%s', should be of the form <1>.<2>" % id_)
@handle_bad_input
def do_print(self, line):
"Prints the value of a field for a packet: print(p) <packet_id> <field_name>"
args = line.split()
self.at_least_n_args(args, 1)
if len(args) == 1:
if self.current_packet_id is None:
raise UIn_Error("Cannot use this command because no packets have been observed yet")
print("Packet id not specified, assuming current packet (%s)" \
% self.current_packet_id_str())
packet_id = self.current_packet_id
copy_id = self.current_copy_id
field_name = args[0]
else:
packet_id, copy_id = self.packet_id_from_str(args[0])
field_name = args[1]
field_id = self.field_id_from_name(field_name)
req = Msg_GetValue(
switch_id = 0, req_id = self.get_req_id(),
packet_id = packet_id, copy_id = copy_id, fid = field_id
)
self.sok.send(req.generate())
msg = self.wait_for_msg()
if msg.type == MsgType.STATUS:
if msg.status == 1:
print("Unknown packet id. Maybe it left the switch already")
elif msg.status == 2:
print("We have not received a value for this field,"\
" maybe it is not valid (yet), or the value is 0")
pass
else:
assert(0)
return
self.check_msg_CLS(msg, Msg_FieldValue)
print(msg.bytes_str, "(%d)" % msg.bytes_int)
def complete_print(self, text, line, start_index, end_index):
args = line.split()
args_cnt = len(args)
fields = sorted(get_all_fields())
return [f for f in fields if f.startswith(text)]
@handle_bad_input
def do_backtrace(self, line):
"Show a backtrace of contexts entered by packet: backtrace(bt) <packet_id>"
args = line.split()
if len(args) == 0:
if self.current_packet_id is None:
raise UIn_Error("Cannot use this command because no packets have been observed yet")
print("Packet id not specified, assuming current packet (%s)" \
% self.current_packet_id_str())
packet_id = self.current_packet_id
copy_id = self.current_copy_id
else:
packet_id, copy_id = self.packet_id_from_str(args[0])