-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathshell.py
3060 lines (2600 loc) · 108 KB
/
shell.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
# Copyright 2019 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.
#
import argparse
import time
from collections import Counter, namedtuple, OrderedDict
import enum
import logging
from threading import Thread
from IPython import start_ipython
from traitlets.config.loader import Config
from IPython.terminal.prompts import Prompts, Token
import os.path
import sys
from p4runtime_sh.p4runtime import (P4RuntimeClient, P4RuntimeException, parse_p4runtime_error,
SSLOptions)
from p4.v1 import p4runtime_pb2
from p4.config.v1 import p4info_pb2
from . import bytes_utils
from . global_options import global_options, Options
from .context import P4RuntimeEntity, P4Type, Context
from .utils import UserError, InvalidP4InfoError
import google.protobuf.text_format
from google.protobuf import descriptor
import queue
context = Context()
client = None
def _print(*args, **kwargs):
if global_options.get_option(Options.verbose):
print(*args, **kwargs)
class UserUsageError(UserError):
def __init__(self, usage):
self.usage = usage
def __str__(self):
return "Usage: " + self.usage
class NotSupportedYet(UserError):
def __init__(self, what):
self.what = what
def __str__(self):
return "{} is not supported yet".format(self.what)
class _PrintContext:
def __init__(self):
self.skip_one = False
self.stack = []
def find_table(self):
for msg in reversed(self.stack):
if msg.DESCRIPTOR.name == "TableEntry":
try:
return context.get_name_from_id(msg.table_id)
except KeyError:
return None
return None
def find_action(self):
for msg in reversed(self.stack):
if msg.DESCRIPTOR.name == "Action":
try:
return context.get_name_from_id(msg.action_id)
except KeyError:
return None
return None
def find_controller_packet_metadata(self):
for msg in reversed(self.stack):
if msg.DESCRIPTOR.name == "PacketIn":
return "packet_in"
if msg.DESCRIPTOR.name == "PacketOut":
return "packet_out"
return None
def _sub_object(field, value, pcontext):
id_ = value
try:
return context.get_name_from_id(id_)
except KeyError:
logging.error("Unknown object id {}".format(id_))
def _sub_mf(field, value, pcontext):
id_ = value
table_name = pcontext.find_table()
if table_name is None:
logging.error("Cannot find any table in context")
return
return context.get_mf_name(table_name, id_)
def _sub_ap(field, value, pcontext):
id_ = value
action_name = pcontext.find_action()
if action_name is None:
logging.error("Cannot find any action in context")
return
return context.get_param_name(action_name, id_)
def _sub_pkt_md(field, value, pcontext):
id_ = value
ctrl_pkt_md_name = pcontext.find_controller_packet_metadata()
return context.get_packet_metadata_name_from_id(ctrl_pkt_md_name, id_)
def _gen_pretty_print_proto_field(substitutions, pcontext):
def myPrintField(self, field, value):
self._PrintFieldName(field)
self.out.write(' ')
if field.type == descriptor.FieldDescriptor.TYPE_BYTES:
# TODO(antonin): any kind of checks required?
self.out.write('\"')
self.out.write(''.join('\\\\x{:02x}'.format(b) for b in value))
self.out.write('\"')
else:
self.PrintFieldValue(field, value)
subs = None
if field.containing_type is not None:
subs = substitutions.get(field.containing_type.name, None)
if subs and field.name in subs and value != 0:
name = subs[field.name](field, value, pcontext)
self.out.write(' ("{}")'.format(name))
self.out.write(' ' if self.as_one_line else '\n')
return myPrintField
def _repr_pretty_proto(msg, substitutions):
"""A custom version of google.protobuf.text_format.MessageToString which represents Protobuf
messages with a more user-friendly string. In particular, P4Runtime ids are supplemented with
the P4 name and binary strings are displayed in hexadecimal format."""
pcontext = _PrintContext()
def message_formatter(message, indent, as_one_line):
# For each messages we do 2 passes: the first one updates the _PrintContext instance and
# calls MessageToString again. The second pass returns None immediately (default handling by
# text_format).
if pcontext.skip_one:
pcontext.skip_one = False
return
pcontext.stack.append(message)
pcontext.skip_one = True
s = google.protobuf.text_format.MessageToString(
message, indent=indent, as_one_line=as_one_line, message_formatter=message_formatter)
s = s[indent:-1]
pcontext.stack.pop()
return s
# We modify the "internals" of the text_format module which is not great as it may break in the
# future, but this enables us to keep the code fairly small.
saved_printer = google.protobuf.text_format._Printer.PrintField
google.protobuf.text_format._Printer.PrintField = _gen_pretty_print_proto_field(
substitutions, pcontext)
s = google.protobuf.text_format.MessageToString(msg, message_formatter=message_formatter)
google.protobuf.text_format._Printer.PrintField = saved_printer
return s
def _repr_pretty_p4info(msg):
substitutions = {
"Table": {"const_default_action_id": _sub_object,
"implementation_id": _sub_object,
"direct_resource_ids": _sub_object},
"ActionRef": {"id": _sub_object},
"ActionProfile": {"table_ids": _sub_object},
"DirectCounter": {"direct_table_id": _sub_object},
"DirectMeter": {"direct_table_id": _sub_object},
}
return _repr_pretty_proto(msg, substitutions)
def _repr_pretty_p4runtime(msg):
substitutions = {
"TableEntry": {"table_id": _sub_object},
"FieldMatch": {"field_id": _sub_mf},
"Action": {"action_id": _sub_object},
"Param": {"param_id": _sub_ap},
"ActionProfileMember": {"action_profile_id": _sub_object},
"ActionProfileGroup": {"action_profile_id": _sub_object},
"MeterEntry": {"meter_id": _sub_object},
"CounterEntry": {"counter_id": _sub_object},
"ValueSetEntry": {"value_set_id": _sub_object},
"RegisterEntry": {"register_id": _sub_object},
"DigestEntry": {"digest_id": _sub_object},
"DigestListAck": {"digest_id": _sub_object},
"DigestList": {"digest_id": _sub_object},
"PacketMetadata": {"metadata_id": _sub_pkt_md}
}
return _repr_pretty_proto(msg, substitutions)
class P4Object:
def __init__(self, obj_type, obj):
self.name = obj.preamble.name
self.id = obj.preamble.id
self._obj_type = obj_type
self._obj = obj
self.__doc__ = """
A wrapper around the P4Info Protobuf message for {} '{}'.
You can access any field from the message with <self>.<field name>.
You can access the name directly with <self>.name.
You can access the id directly with <self>.id.
If you need the underlying Protobuf message, you can access it with msg().
""".format(obj_type.pretty_name, self.name)
def __dir__(self):
d = ["info", "msg", "name", "id"]
if self._obj_type == P4Type.table:
d.append("actions")
return d
def _repr_pretty_(self, p, cycle):
p.text(_repr_pretty_p4info(self._obj))
def __str__(self):
return _repr_pretty_p4info(self._obj)
def __getattr__(self, name):
return getattr(self._obj, name)
def __settattr__(self, name, value):
return UserError("Operation not supported")
def msg(self):
"""Get Protobuf message object"""
return self._obj
def info(self):
print(_repr_pretty_p4info(self._obj))
def actions(self):
"""Print list of actions, only for tables and action profiles."""
if self._obj_type == P4Type.table:
for action in self._obj.action_refs:
print(context.get_name_from_id(action.id))
elif self._obj_type == P4Type.action_profile:
t_id = self._obj.table_ids[0]
t_name = context.get_name_from_id(t_id)
t = context.get_table(t_name)
for action in t.action_refs:
print(context.get_name_from_id(action.id))
else:
raise UserError("'actions' is only available for tables and action profiles")
class P4Objects:
def __init__(self, obj_type):
self._obj_type = obj_type
self._names = sorted([name for name, _ in context.get_objs(obj_type)])
self._iter = None
self.__doc__ = """
All the {pnames} in the P4 program.
To access a specific {pname}, use {p4info}['<name>'].
You can use this class to iterate over all {pname} instances:
\tfor x in {p4info}:
\t\tprint(x.id)
""".format(pname=obj_type.pretty_name, pnames=obj_type.pretty_names, p4info=obj_type.p4info_name)
def __call__(self):
for name in self._names:
print(name)
def _ipython_key_completions_(self):
return self._names
def __getitem__(self, name):
obj = context.get_obj(self._obj_type, name)
if obj is None:
raise UserError("{} '{}' does not exist".format(
self._obj_type.pretty_name, name))
return P4Object(self._obj_type, obj)
def __setitem__(self, name, value):
raise UserError("Operation not allowed")
def _repr_pretty_(self, p, cycle):
p.text(self.__doc__)
def __iter__(self):
self._iter = iter(self._names)
return self
def __next__(self):
name = next(self._iter)
return self[name]
class MatchKey:
def __init__(self, table_name, match_fields):
self._table_name = table_name
self._fields = OrderedDict()
self._fields_suffixes = {}
for mf in match_fields:
self._add_field(mf)
self._mk = OrderedDict()
self._set_docstring()
def _set_docstring(self):
self.__doc__ = "Match key fields for table '{}':\n\n".format(self._table_name)
for name, info in self._fields.items():
self.__doc__ += str(info)
self.__doc__ += """
Set a field value with <self>['<field_name>'] = '...'
* For exact match: <self>['<f>'] = '<value>'
* For ternary match: <self>['<f>'] = '<value>&&&<mask>'
* For LPM match: <self>['<f>'] = '<value>/<mask>'
* For range match: <self>['<f>'] = '<value>..<mask>'
* For optional match: <self>['<f>'] = '<value>'
If it's inconvenient to use the whole field name, you can use a unique suffix.
You may also use <self>.set(<f>='<value>')
\t(<f> must not include a '.' in this case, but remember that you can use a unique suffix)
"""
def _ipython_key_completions_(self):
return self._fields.keys()
def __dir__(self):
return ["clear"]
def _full_field_name(self, name):
if name in self._fields:
return name
if name in self._fields_suffixes:
return self._fields_suffixes[name]
raise UserError(
"'{}' is not a valid match field name, nor a valid unique suffix, "
"for table '{}'".format(name, self._table_name))
def _get_mf(self, name):
fullname = self._full_field_name(name)
return self._fields[fullname]
def __setitem__(self, name, value):
fullname = self._full_field_name(name)
field_info = self._get_mf(fullname)
self._mk[fullname] = self._parse_mf(value, field_info)
_print(self._mk[fullname])
def __getitem__(self, name):
fullname = self._full_field_name(name)
f = self._mk.get(fullname, None)
if f is None:
_print("Unset")
return f
def _parse_mf(self, s, field_info):
if type(s) is not str:
raise UserError("Match field value must be a string")
if field_info.match_type == p4info_pb2.MatchField.EXACT:
return self._parse_mf_exact(s, field_info)
elif field_info.match_type == p4info_pb2.MatchField.LPM:
return self._parse_mf_lpm(s, field_info)
elif field_info.match_type == p4info_pb2.MatchField.TERNARY:
return self._parse_mf_ternary(s, field_info)
elif field_info.match_type == p4info_pb2.MatchField.RANGE:
return self._parse_mf_range(s, field_info)
elif field_info.match_type == p4info_pb2.MatchField.OPTIONAL:
return self._parse_mf_optional(s, field_info)
else:
raise UserError("Unsupported match type for field:\n{}".format(field_info))
def _parse_mf_exact(self, s, field_info):
v = bytes_utils.parse_value(s.strip(), field_info.bitwidth)
return self._sanitize_and_convert_mf_exact(v, field_info)
def _sanitize_and_convert_mf_exact(self, value, field_info):
mf = p4runtime_pb2.FieldMatch()
mf.field_id = field_info.id
mf.exact.value = bytes_utils.make_canonical_if_option_set(value)
return mf
def _parse_mf_optional(self, s, field_info):
v = bytes_utils.parse_value(s.strip(), field_info.bitwidth)
return self._sanitize_and_convert_mf_optional(v, field_info)
def _sanitize_and_convert_mf_optional(self, value, field_info):
mf = p4runtime_pb2.FieldMatch()
mf.field_id = field_info.id
mf.optional.value = bytes_utils.make_canonical_if_option_set(value)
return mf
def _parse_mf_lpm(self, s, field_info):
try:
prefix, length = s.split('/')
prefix, length = prefix.strip(), length.strip()
except ValueError:
prefix = s
length = str(field_info.bitwidth)
prefix = bytes_utils.parse_value(prefix, field_info.bitwidth)
try:
length = int(length)
except ValueError:
raise UserError("'{}' is not a valid prefix length").format(length)
return self._sanitize_and_convert_mf_lpm(prefix, length, field_info)
def _sanitize_and_convert_mf_lpm(self, prefix, length, field_info):
if length == 0:
raise UserError(
"Ignoring LPM don't care match (prefix length of 0) as per P4Runtime spec")
mf = p4runtime_pb2.FieldMatch()
mf.field_id = field_info.id
mf.lpm.prefix_len = length
first_byte_masked = length // 8
if first_byte_masked == len(prefix):
mf.lpm.value = prefix
return mf
barray = bytearray(prefix)
transformed = False
r = length % 8
byte_mask = 0xff & ((0xff << (8 - r)))
if barray[first_byte_masked] & byte_mask != barray[first_byte_masked]:
transformed = True
barray[first_byte_masked] = barray[first_byte_masked] & byte_mask
for i in range(first_byte_masked + 1, len(prefix)):
if barray[i] != 0:
transformed = True
barray[i] = 0
if transformed:
_print("LPM value was transformed to conform to the P4Runtime spec "
"(trailing bits must be unset)")
mf.lpm.value = bytes(bytes_utils.make_canonical_if_option_set(barray))
return mf
def _parse_mf_ternary(self, s, field_info):
try:
value, mask = s.split('&&&')
value, mask = value.strip(), mask.strip()
except ValueError:
value = s.strip()
mask = "0b" + ("1" * field_info.bitwidth)
value = bytes_utils.parse_value(value, field_info.bitwidth)
mask = bytes_utils.parse_value(mask, field_info.bitwidth)
return self._sanitize_and_convert_mf_ternary(value, mask, field_info)
def _sanitize_and_convert_mf_ternary(self, value, mask, field_info):
if int.from_bytes(mask, byteorder='big') == 0:
raise UserError("Ignoring ternary don't care match (mask of 0s) as per P4Runtime spec")
mf = p4runtime_pb2.FieldMatch()
mf.field_id = field_info.id
barray = bytearray(value)
transformed = False
for i in range(len(value)):
if barray[i] & mask[i] != barray[i]:
transformed = True
barray[i] = barray[i] & mask[i]
if transformed:
_print("Ternary value was transformed to conform to the P4Runtime spec "
"(masked off bits must be unset)")
mf.ternary.value = bytes(bytes_utils.make_canonical_if_option_set(barray))
mf.ternary.mask = bytes_utils.make_canonical_if_option_set(mask)
return mf
def _parse_mf_range(self, s, field_info):
try:
start, end = s.split('..')
start, end = start.strip(), end.strip()
except ValueError:
raise UserError("'{}' does not specify a valid range, use '<start>..<end>'").format(
s)
start = bytes_utils.parse_value(start, field_info.bitwidth)
end = bytes_utils.parse_value(end, field_info.bitwidth)
return self._sanitize_and_convert_mf_range(start, end, field_info)
def _sanitize_and_convert_mf_range(self, start, end, field_info):
# It's a bit silly: the fields are converted from str to int to bytes by bytes_utils, then
# converted back to int here...
start_ = int.from_bytes(start, byteorder='big')
end_ = int.from_bytes(end, byteorder='big')
if start_ > end_:
raise UserError("Invalid range match: start is greater than end")
if start_ == 0 and end_ == ((1 << field_info.bitwidth) - 1):
raise UserError(
"Ignoring range don't care match (all possible values) as per P4Runtime spec")
mf = p4runtime_pb2.FieldMatch()
mf.field_id = field_info.id
mf.range.low = bytes_utils.make_canonical_if_option_set(start)
mf.range.high = bytes_utils.make_canonical_if_option_set(end)
return mf
def _add_field(self, field_info):
self._fields[field_info.name] = field_info
self._recompute_suffixes()
def _recompute_suffixes(self):
suffixes = {}
suffix_count = Counter()
for fname in self._fields:
suffix = None
for s in reversed(fname.split(".")):
suffix = s if suffix is None else s + "." + suffix
suffixes[suffix] = fname
suffix_count[suffix] += 1
for suffix, c in suffix_count.items():
if c > 1:
del suffixes[suffix]
self._fields_suffixes = suffixes
def __str__(self):
return '\n'.join([str(mf) for name, mf in self._mk.items()])
def _repr_pretty_(self, p, cycle):
for name, mf in self._mk.items():
p.text(str(mf))
def set(self, **kwargs):
for name, value in kwargs.items():
self[name] = value
def clear(self):
self._mk.clear()
def _count(self):
return len(self._mk)
class Action:
def __init__(self, action_name=None):
self._init = False
if action_name is None:
raise UserError("Please provide name for action")
self.action_name = action_name
action_info = context.get_action(action_name)
if action_info is None:
raise UserError("Unknown action '{}'".format(action_name))
self._action_id = action_info.preamble.id
self._params = OrderedDict()
for param in action_info.params:
self._params[param.name] = param
self._action_info = action_info
self._param_values = OrderedDict()
self._set_docstring()
self._init = True
def _set_docstring(self):
self.__doc__ = "Action parameters for action '{}':\n\n".format(self.action_name)
for name, info in self._params.items():
self.__doc__ += str(info)
self.__doc__ += "\n\n"
self.__doc__ += "Set a param value with <self>['<param_name>'] = '<value>'\n"
self.__doc__ += "You may also use <self>.set(<param_name>='<value>')\n"
def _ipython_key_completions_(self):
return self._params.keys()
def __dir__(self):
return ["action_name", "msg", "set"]
def _get_param(self, name):
if name not in self._params:
raise UserError(
"'{}' is not a valid action parameter name for action '{}'".format(
name, self.action_name))
return self._params[name]
def __setattr__(self, name, value):
if name[0] == "_" or not self._init:
super().__setattr__(name, value)
return
if name == "action_name":
raise UserError("Cannot change action name")
super().__setattr__(name, value)
def __setitem__(self, name, value):
param_info = self._get_param(name)
self._param_values[name] = self._parse_param(value, param_info)
_print(self._param_values[name])
def __getitem__(self, name):
_ = self._get_param(name)
f = self._param_values.get(name, None)
if f is None:
_print("Unset")
return f
def _parse_param(self, s, param_info):
if type(s) is not str:
raise UserError("Action parameter value must be a string")
v = bytes_utils.parse_value(s, param_info.bitwidth)
p = p4runtime_pb2.Action.Param()
p.param_id = param_info.id
p.value = bytes_utils.make_canonical_if_option_set(v)
return p
def msg(self):
msg = p4runtime_pb2.Action()
msg.action_id = self._action_id
msg.params.extend(self._param_values.values())
return msg
def _from_msg(self, msg):
assert(self._action_id == msg.action_id)
self._param_values.clear()
for p in msg.params:
p_name = context.get_param_name(self.action_name, p.param_id)
self._param_values[p_name] = p
def __str__(self):
return str(self.msg())
def _repr_pretty_(self, p, cycle):
p.text(str(self.msg()))
def set(self, **kwargs):
for name, value in kwargs.items():
self[name] = value
class _EntityBase:
def __init__(self, entity_type, p4runtime_cls, modify_only=False):
self._init = False
self._entity_type = entity_type
self._entry = p4runtime_cls()
self._modify_only = modify_only
def __dir__(self):
d = ["msg", "read"]
if self._modify_only:
d.append("modify")
else:
d.extend(["insert", "modify", "delete"])
return d
# to be called before issueing a P4Runtime request
# enforces checks that cannot be performed when setting individual fields
def _validate_msg(self):
return True
def _update_msg(self):
pass
def __str__(self):
self._update_msg()
return str(_repr_pretty_p4runtime(self._entry))
def _repr_pretty_(self, p, cycle):
self._update_msg()
p.text(_repr_pretty_p4runtime(self._entry))
def __getattr__(self, name):
raise AttributeError("'{}' object has no attribute '{}'".format(
self.__class__.__name__, name))
def msg(self):
self._update_msg()
return self._entry
def _write(self, type_):
self._update_msg()
self._validate_msg()
update = p4runtime_pb2.Update()
update.type = type_
getattr(update.entity, self._entity_type.name).CopyFrom(self._entry)
client.write_update(update)
def insert(self):
if self._modify_only:
raise NotImplementedError("Insert not supported for {}".format(self._entity_type.name))
logging.debug("Inserting entry")
self._write(p4runtime_pb2.Update.INSERT)
def delete(self):
if self._modify_only:
raise NotImplementedError("Delete not supported for {}".format(self._entity_type.name))
logging.debug("Deleting entry")
self._write(p4runtime_pb2.Update.DELETE)
def modify(self):
logging.debug("Modifying entry")
self._write(p4runtime_pb2.Update.MODIFY)
def _from_msg(self, msg):
raise NotImplementedError
def read(self, function=None):
# Entities should override this method and provide a helpful docstring
self._update_msg()
self._validate_msg()
entity = p4runtime_pb2.Entity()
getattr(entity, self._entity_type.name).CopyFrom(self._entry)
iterator = client.read_one(entity)
# Cannot use a (simpler) generator here as we need to decorate __next__ with
# @parse_p4runtime_error.
class _EntryIterator:
def __init__(self, entity, it):
self._entity = entity
self._it = it
self._entities_it = None
def __iter__(self):
return self
@parse_p4runtime_error
def __next__(self):
if self._entities_it is None:
rep = next(self._it)
self._entities_it = iter(rep.entities)
try:
entity = next(self._entities_it)
except StopIteration:
self._entities_it = None
return next(self)
if isinstance(self._entity, _P4EntityBase):
e = type(self._entity)(self._entity.name) # create new instance of same entity
else:
e = type(self._entity)()
msg = getattr(entity, self._entity._entity_type.name)
e._from_msg(msg)
# neither of these should be needed
# e._update_msg()
# e._entry.CopyFrom(msg)
return e
if function is None:
return _EntryIterator(self, iterator)
else:
for x in _EntryIterator(self, iterator):
function(x)
class _P4EntityBase(_EntityBase):
def __init__(self, p4_type, entity_type, p4runtime_cls, name=None, modify_only=False):
super().__init__(entity_type, p4runtime_cls, modify_only)
self._p4_type = p4_type
if name is None:
raise UserError("Please provide name for {}".format(p4_type.pretty_name))
self.name = name
self._info = P4Objects(p4_type)[name]
self.id = self._info.id
def __dir__(self):
return super().__dir__() + ["name", "id", "info"]
def info(self):
"""Display P4Info entry for the object"""
return self._info
class ActionProfileMember(_P4EntityBase):
def __init__(self, action_profile_name=None):
super().__init__(
P4Type.action_profile, P4RuntimeEntity.action_profile_member,
p4runtime_pb2.ActionProfileMember, action_profile_name)
self.member_id = 0
self.action = None
self._valid_action_ids = self._get_action_set()
self.__doc__ = """
An action profile member for '{}'
Use <self>.info to display the P4Info entry for the action profile.
Set the member id with <self>.member_id = <expr>.
To set the action specification <self>.action = <instance of type Action>.
To set the value of action parameters, use <self>.action['<param name>'] = <expr>.
Type <self>.action? for more details.
Typical usage to insert an action profile member:
m = action_profile_member['<action_profile_name>'](action='<action_name>', member_id=1)
m.action['<p1>'] = ...
...
m.action['<pM>'] = ...
# OR m.action.set(p1=..., ..., pM=...)
m.insert
For information about how to read members, use <self>.read?
""".format(action_profile_name)
self._init = True
def __dir__(self):
return super().__dir__() + ["member_id", "action"]
def _get_action_set(self):
t_id = self._info.table_ids[0]
t_name = context.get_name_from_id(t_id)
t = context.get_table(t_name)
return set([action.id for action in t.action_refs])
def __call__(self, **kwargs):
for name, value in kwargs.items():
if name == "action" and type(value) is str:
value = Action(value)
setattr(self, name, value)
return self
def __setattr__(self, name, value):
if name[0] == "_" or not self._init:
super().__setattr__(name, value)
return
if name == "name":
raise UserError("Cannot change action profile name")
if name == "member_id":
if type(value) is not int:
raise UserError("member_id must be an integer")
if name == "action" and value is not None:
if not isinstance(value, Action):
raise UserError("action must be an instance of Action")
if not self._is_valid_action_id(value._action_id):
raise UserError("action '{}' is not a valid action for this action profile".format(
value.action_name))
super().__setattr__(name, value)
def _is_valid_action_id(self, action_id):
return action_id in self._valid_action_ids
def _update_msg(self):
self._entry.action_profile_id = self.id
self._entry.member_id = self.member_id
if self.action is not None:
self._entry.action.CopyFrom(self.action.msg())
def _from_msg(self, msg):
self.member_id = msg.member_id
if msg.HasField('action'):
action = msg.action
action_name = context.get_name_from_id(action.action_id)
self.action = Action(action_name)
self.action._from_msg(action)
def read(self, function=None):
"""Generate a P4Runtime Read RPC. Supports wildcard reads (just leave
the appropriate fields unset).
If function is None, returns an iterator. Iterate over it to get all the
members (as ActionProfileMember instances) returned by the
server. Otherwise, function is applied to all the members returned
by the server.
"""
return super().read(function)
class GroupMember:
"""
A member in an ActionProfileGroup.
Construct with GroupMember(<member_id>, weight=<weight>, watch=<watch>,
watch_port=<watch_port>).
You can set / get attributes member_id (required), weight (default 1), watch (default 0),
watch_port (default "").
"""
def __init__(self, member_id=None, weight=1, watch=0, watch_port=b""):
if member_id is None:
raise UserError("member_id is required")
self._msg = p4runtime_pb2.ActionProfileGroup.Member()
self._msg.member_id = member_id
self._msg.weight = weight
if watch:
self._msg.watch = watch
if watch_port:
self._msg.watch_port = watch_port
def __dir__(self):
return ["member_id", "weight", "watch", "watch_port"]
def __setattr__(self, name, value):
if name[0] == "_":
super().__setattr__(name, value)
return
if name == "member_id":
if type(value) is not int:
raise UserError("member_id must be an integer")
self._msg.member_id = value
return
if name == "weight":
if type(value) is not int:
raise UserError("weight must be an integer")
self._msg.weight = value
return
if name == "watch":
if type(value) is not int:
raise UserError("watch must be an integer")
self._msg.watch = value
return
if name == "watch_port":
if type(value) is not bytes:
raise UserError("watch_port must be a byte string")
self._msg.watch_port = value
return
super().__setattr__(name, value)
def __getattr__(self, name):
if name == "member_id":
return self._msg.member_id
if name == "weight":
return self._msg.weight
if name == "watch":
return self._msg.watch
if name == "watch_port":
return self._msg.watch_port
return super().__getattr__(name)
def __str__(self):
return str(self._msg)
def _repr_pretty_(self, p, cycle):
p.text(str(p))
class ActionProfileGroup(_P4EntityBase):
def __init__(self, action_profile_name=None):
super().__init__(
P4Type.action_profile, P4RuntimeEntity.action_profile_group,
p4runtime_pb2.ActionProfileGroup, action_profile_name)
self.group_id = 0
self.max_size = 0
self.members = []
self.__doc__ = """
An action profile group for '{}'
Use <self>.info to display the P4Info entry for the action profile.
Set the group id with <self>.group_id = <expr>. Default is 0.
Set the max size with <self>.max_size = <expr>. Default is 0.
Add members to the group with <self>.add(<member_id>, weight=<weight>, watch=<watch>,
watch_port=<watch_port>).
weight, watch and watch port are optional (default to 1, 0 and "" respectively).
Typical usage to insert an action profile group:
g = action_profile_group['<action_profile_name>'](group_id=1)
g.add(<member id 1>)
g.add(<member id 2>)
# OR g.add(<member id 1>).add(<member id 2>)
For information about how to read groups, use <self>.read?
""".format(action_profile_name)
self._init = True
def __dir__(self):
return super().__dir__() + ["group_id", "max_size", "members", "add", "clear"]
def __call__(self, **kwargs):
for name, value in kwargs.items():
setattr(self, name, value)
return self
def __setattr__(self, name, value):
if name[0] == "_" or not self._init:
super().__setattr__(name, value)
return
if name == "name":
raise UserError("Cannot change action profile name")
elif name == "group_id":
if type(value) is not int:
raise UserError("group_id must be an integer")
elif name == "members":
if type(value) is not list:
raise UserError("members must be a list of GroupMember objects")
for m in value:
if type(m) is not GroupMember:
raise UserError("members must be a list of GroupMember objects")
super().__setattr__(name, value)
def add(self, member_id=None, weight=1, watch=0, watch_port=b""):
"""Add a member to the members list."""
self.members.append(GroupMember(member_id, weight, watch, watch_port))
return self