Skip to content

Commit

Permalink
utils: show error message when priority is missing (p4lang#452)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
rst0git authored Feb 24, 2022
1 parent ccc5693 commit 071b89a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
18 changes: 9 additions & 9 deletions utils/p4runtime_lib/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ def get_match_field_pb(self, table_name, match_field_name, value):
exact = p4runtime_match.exact
exact.value = encode(value, bitwidth)
elif match_type == p4info_pb2.MatchField.LPM:
lpm = p4runtime_match.lpm
lpm.value = encode(value[0], bitwidth)
lpm.prefix_len = value[1]
lpm_entry = p4runtime_match.lpm
lpm_entry.value = encode(value[0], bitwidth)
lpm_entry.prefix_len = value[1]
elif match_type == p4info_pb2.MatchField.TERNARY:
lpm = p4runtime_match.ternary
lpm.value = encode(value[0], bitwidth)
lpm.mask = encode(value[1], bitwidth)
ternary_entry = p4runtime_match.ternary
ternary_entry.value = encode(value[0], bitwidth)
ternary_entry.mask = encode(value[1], bitwidth)
elif match_type == p4info_pb2.MatchField.RANGE:
lpm = p4runtime_match.range
lpm.low = encode(value[0], bitwidth)
lpm.high = encode(value[1], bitwidth)
range_entry = p4runtime_match.range
range_entry.low = encode(value[0], bitwidth)
range_entry.high = encode(value[1], bitwidth)
else:
raise Exception("Unsupported match type with type %r" % match_type)
return p4runtime_match
Expand Down
25 changes: 24 additions & 1 deletion utils/p4runtime_lib/simple_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from . import bmv2
from . import helper

from p4.config.v1 import p4info_pb2


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


def program_switch(addr, device_id, sw_conf_file, workdir, proto_dump_fpath):
def program_switch(addr, device_id, sw_conf_file, workdir, proto_dump_fpath, runtime_json):
sw_conf = json_load_byteified(sw_conf_file)
try:
check_switch_conf(sw_conf=sw_conf, workdir=workdir)
Expand Down Expand Up @@ -126,6 +128,7 @@ def program_switch(addr, device_id, sw_conf_file, workdir, proto_dump_fpath):
info("Inserting %d table entries..." % len(table_entries))
for entry in table_entries:
info(tableEntryToString(entry))
validateTableEntry(entry, p4info_helper, runtime_json)
insertTableEntry(sw, entry, p4info_helper)

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


def validateTableEntry(flow, p4info_helper, runtime_json):
table_name = flow['table']
match_fields = flow.get('match') # None if not found
priority = flow.get('priority') # None if not found
match_types_with_priority = [
p4info_pb2.MatchField.TERNARY,
p4info_pb2.MatchField.RANGE
]
if match_fields is not None and (priority is None or priority == 0):
for match_field_name, _ in match_fields.items():
p4info_match = p4info_helper.get_match_field(
table_name, match_field_name)
match_type = p4info_match.match_type
if match_type in match_types_with_priority:
raise AssertionError(
"non-zero 'priority' field is required for all entries for table {} in {}"
.format(table_name, runtime_json)
)


def insertTableEntry(sw, flow, p4info_helper):
table_name = flow['table']
match_fields = flow.get('match') # None if not found
Expand Down
4 changes: 3 additions & 1 deletion utils/run_exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ def program_switch_p4runtime(self, sw_name, sw_dict):
device_id=device_id,
sw_conf_file=sw_conf_file,
workdir=os.getcwd(),
proto_dump_fpath=outfile)
proto_dump_fpath=outfile,
runtime_json=runtime_json
)

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

0 comments on commit 071b89a

Please sign in to comment.