Skip to content

Commit 071b89a

Browse files
authored
utils: show error message when priority is missing (p4lang#452)
* p4runtime_lib/helper: use more appropriate variable names Suggested-by: Antonin Bas <[email protected]> Signed-off-by: Radostin Stoyanov <[email protected]> * utils: show error message when priority is missing A 'priority' field is required to order entries when the table's match key includes an optional, ternary or range match. Suggested-by: Andy Fingerhut <[email protected]> Signed-off-by: Radostin Stoyanov <[email protected]>
1 parent ccc5693 commit 071b89a

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

utils/p4runtime_lib/helper.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,17 @@ def get_match_field_pb(self, table_name, match_field_name, value):
101101
exact = p4runtime_match.exact
102102
exact.value = encode(value, bitwidth)
103103
elif match_type == p4info_pb2.MatchField.LPM:
104-
lpm = p4runtime_match.lpm
105-
lpm.value = encode(value[0], bitwidth)
106-
lpm.prefix_len = value[1]
104+
lpm_entry = p4runtime_match.lpm
105+
lpm_entry.value = encode(value[0], bitwidth)
106+
lpm_entry.prefix_len = value[1]
107107
elif match_type == p4info_pb2.MatchField.TERNARY:
108-
lpm = p4runtime_match.ternary
109-
lpm.value = encode(value[0], bitwidth)
110-
lpm.mask = encode(value[1], bitwidth)
108+
ternary_entry = p4runtime_match.ternary
109+
ternary_entry.value = encode(value[0], bitwidth)
110+
ternary_entry.mask = encode(value[1], bitwidth)
111111
elif match_type == p4info_pb2.MatchField.RANGE:
112-
lpm = p4runtime_match.range
113-
lpm.low = encode(value[0], bitwidth)
114-
lpm.high = encode(value[1], bitwidth)
112+
range_entry = p4runtime_match.range
113+
range_entry.low = encode(value[0], bitwidth)
114+
range_entry.high = encode(value[1], bitwidth)
115115
else:
116116
raise Exception("Unsupported match type with type %r" % match_type)
117117
return p4runtime_match

utils/p4runtime_lib/simple_controller.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from . import bmv2
2323
from . import helper
2424

25+
from p4.config.v1 import p4info_pb2
26+
2527

2628
def error(msg):
2729
print(' - ERROR! ' + msg, file=sys.stderr)
@@ -88,7 +90,7 @@ def check_switch_conf(sw_conf, workdir):
8890
raise ConfException("file does not exist %s" % real_path)
8991

9092

91-
def program_switch(addr, device_id, sw_conf_file, workdir, proto_dump_fpath):
93+
def program_switch(addr, device_id, sw_conf_file, workdir, proto_dump_fpath, runtime_json):
9294
sw_conf = json_load_byteified(sw_conf_file)
9395
try:
9496
check_switch_conf(sw_conf=sw_conf, workdir=workdir)
@@ -126,6 +128,7 @@ def program_switch(addr, device_id, sw_conf_file, workdir, proto_dump_fpath):
126128
info("Inserting %d table entries..." % len(table_entries))
127129
for entry in table_entries:
128130
info(tableEntryToString(entry))
131+
validateTableEntry(entry, p4info_helper, runtime_json)
129132
insertTableEntry(sw, entry, p4info_helper)
130133

131134
if 'multicast_group_entries' in sw_conf:
@@ -146,6 +149,26 @@ def program_switch(addr, device_id, sw_conf_file, workdir, proto_dump_fpath):
146149
sw.shutdown()
147150

148151

152+
def validateTableEntry(flow, p4info_helper, runtime_json):
153+
table_name = flow['table']
154+
match_fields = flow.get('match') # None if not found
155+
priority = flow.get('priority') # None if not found
156+
match_types_with_priority = [
157+
p4info_pb2.MatchField.TERNARY,
158+
p4info_pb2.MatchField.RANGE
159+
]
160+
if match_fields is not None and (priority is None or priority == 0):
161+
for match_field_name, _ in match_fields.items():
162+
p4info_match = p4info_helper.get_match_field(
163+
table_name, match_field_name)
164+
match_type = p4info_match.match_type
165+
if match_type in match_types_with_priority:
166+
raise AssertionError(
167+
"non-zero 'priority' field is required for all entries for table {} in {}"
168+
.format(table_name, runtime_json)
169+
)
170+
171+
149172
def insertTableEntry(sw, flow, p4info_helper):
150173
table_name = flow['table']
151174
match_fields = flow.get('match') # None if not found

utils/run_exercise.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ def program_switch_p4runtime(self, sw_name, sw_dict):
273273
device_id=device_id,
274274
sw_conf_file=sw_conf_file,
275275
workdir=os.getcwd(),
276-
proto_dump_fpath=outfile)
276+
proto_dump_fpath=outfile,
277+
runtime_json=runtime_json
278+
)
277279

278280
def program_switch_cli(self, sw_name, sw_dict):
279281
""" This method will start up the CLI and use the contents of the

0 commit comments

Comments
 (0)