p4tutorials-turkce/utils/p4runtime_lib/simple_controller.py
Nate Foster dc08948a34
P4 Developer Day 2018 Spring (#159)
* Repository reorganization for 2018 Spring P4 Developer Day.

* Port tutorial exercises to P4Runtime with static controller (#156)

* Switch VM to a minimal Ubuntu 16.04 desktop image

* Add commands to install Protobuf Python bindings to user_bootstrap.sh

* Implement P4Runtime static controller for use in exercises

From the exercise perspective, the main difference is that control plane
rules are now specified using JSON files instead of CLI commands. Such
JSON files define rules that use the same name for tables, keys, etc. as
in the P4Info file.

All P4Runtime requests generated as part of the make run process are
logged in the exercise's “logs” directory, making it easier for students
to see the actual P4Runtime messages sent to the switch.

Only the "basic" exercise has been ported to use P4Runtime.
The "p4runtime" exercise has been updated to work with P4Runtime
protocol changes.

Known issues:
- make run hangs in case of errors when running the P4Runtime controller
    (probably due to gRPC stream channel threads not terminated properly)
- missing support for inserting table entries with default action
    (can specify in P4 program as a workaround)

* Force install protobuf python module

* Fixing Ctrl-C hang by shutdown switches

* Moving gRPC error print to function for readability

Unforuntately, if this gets moved out of the file, the process hangs.
We'll need to figure out how why later.

* Renaming ShutdownAllSwitches -> ShutdownAllSwitchConnections

* Reverting counter index change

* Porting the ECN exercise to use P4 Runtime Static Controller

* updating the README in the ecn exercise to reflect the change in rule files

* Allow set table default action in P4Runtime static controller

* Fixed undefined match string when printing P4Runtime table entry

* Updated basic_tunnel exercise to use P4Runtime controller.

* Changed default action in the basic exercise's ipv4_lpm table to drop

* Porting the MRI exercise to use P4runtime with static controller

* Updating readme to reflect the change of controller for mri

* Update calc exercise for P4Runtime static controller

* Port source_routing to P4 Runtime static controller (#157)

* Port Load Balance to P4 Runtime Static Controller (#158)
2018-06-01 02:54:33 -04:00

196 lines
7.0 KiB
Python
Executable File

#!/usr/bin/env python2
#
# Copyright 2017-present Open Networking Foundation
#
# 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 json
import os
import sys
import bmv2
import helper
def error(msg):
print >> sys.stderr, ' - ERROR! ' + msg
def info(msg):
print >> sys.stdout, ' - ' + msg
class ConfException(Exception):
pass
def main():
parser = argparse.ArgumentParser(description='P4Runtime Simple Controller')
parser.add_argument('-a', '--p4runtime-server-addr',
help='address and port of the switch\'s P4Runtime server (e.g. 192.168.0.1:50051)',
type=str, action="store", required=True)
parser.add_argument('-d', '--device-id',
help='Internal device ID to use in P4Runtime messages',
type=int, action="store", required=True)
parser.add_argument('-p', '--proto-dump-file',
help='path to file where to dump protobuf messages sent to the switch',
type=str, action="store", required=True)
parser.add_argument("-c", '--runtime-conf-file',
help="path to input runtime configuration file (JSON)",
type=str, action="store", required=True)
args = parser.parse_args()
if not os.path.exists(args.runtime_conf_file):
parser.error("File %s does not exist!" % args.runtime_conf_file)
workdir = os.path.dirname(os.path.abspath(args.runtime_conf_file))
with open(args.runtime_conf_file, 'r') as sw_conf_file:
program_switch(addr=args.p4runtime_server_addr,
device_id=args.device_id,
sw_conf_file=sw_conf_file,
workdir=workdir,
proto_dump_fpath=args.proto_dump_file)
def check_switch_conf(sw_conf, workdir):
required_keys = ["p4info"]
files_to_check = ["p4info"]
target_choices = ["bmv2"]
if "target" not in sw_conf:
raise ConfException("missing key 'target'")
target = sw_conf['target']
if target not in target_choices:
raise ConfException("unknown target '%s'" % target)
if target == 'bmv2':
required_keys.append("bmv2_json")
files_to_check.append("bmv2_json")
for conf_key in required_keys:
if conf_key not in sw_conf or len(sw_conf[conf_key]) == 0:
raise ConfException("missing key '%s' or empty value" % conf_key)
for conf_key in files_to_check:
real_path = os.path.join(workdir, sw_conf[conf_key])
if not os.path.exists(real_path):
raise ConfException("file does not exist %s" % real_path)
def program_switch(addr, device_id, sw_conf_file, workdir, proto_dump_fpath):
sw_conf = json_load_byteified(sw_conf_file)
try:
check_switch_conf(sw_conf=sw_conf, workdir=workdir)
except ConfException as e:
error("While parsing input runtime configuration: %s" % str(e))
return
info('Using P4Info file %s...' % sw_conf['p4info'])
p4info_fpath = os.path.join(workdir, sw_conf['p4info'])
p4info_helper = helper.P4InfoHelper(p4info_fpath)
target = sw_conf['target']
info("Connecting to P4Runtime server on %s (%s)..." % (addr, target))
if target == "bmv2":
sw = bmv2.Bmv2SwitchConnection(address=addr, device_id=device_id,
proto_dump_file=proto_dump_fpath)
else:
raise Exception("Don't know how to connect to target %s" % target)
try:
sw.MasterArbitrationUpdate()
if target == "bmv2":
info("Setting pipeline config (%s)..." % sw_conf['bmv2_json'])
bmv2_json_fpath = os.path.join(workdir, sw_conf['bmv2_json'])
sw.SetForwardingPipelineConfig(p4info=p4info_helper.p4info,
bmv2_json_file_path=bmv2_json_fpath)
else:
raise Exception("Should not be here")
if 'table_entries' in sw_conf:
table_entries = sw_conf['table_entries']
info("Inserting %d table entries..." % len(table_entries))
for entry in table_entries:
info(tableEntryToString(entry))
insertTableEntry(sw, entry, p4info_helper)
finally:
sw.shutdown()
def insertTableEntry(sw, flow, p4info_helper):
table_name = flow['table']
match_fields = flow.get('match') # None if not found
action_name = flow['action_name']
default_action = flow.get('default_action') # None if not found
action_params = flow['action_params']
priority = flow.get('priority') # None if not found
table_entry = p4info_helper.buildTableEntry(
table_name=table_name,
match_fields=match_fields,
default_action=default_action,
action_name=action_name,
action_params=action_params,
priority=priority)
sw.WriteTableEntry(table_entry)
# object hook for josn library, use str instead of unicode object
# https://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-from-json
def json_load_byteified(file_handle):
return _byteify(json.load(file_handle, object_hook=_byteify),
ignore_dicts=True)
def _byteify(data, ignore_dicts=False):
# if this is a unicode string, return its string representation
if isinstance(data, unicode):
return data.encode('utf-8')
# if this is a list of values, return list of byteified values
if isinstance(data, list):
return [_byteify(item, ignore_dicts=True) for item in data]
# if this is a dictionary, return dictionary of byteified keys and values
# but only if we haven't already byteified it
if isinstance(data, dict) and not ignore_dicts:
return {
_byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True)
for key, value in data.iteritems()
}
# if it's anything else, return it in its original form
return data
def tableEntryToString(flow):
if 'match' in flow:
match_str = ['%s=%s' % (match_name, str(flow['match'][match_name])) for match_name in
flow['match']]
match_str = ', '.join(match_str)
elif 'default_action' in flow and flow['default_action']:
match_str = '(default action)'
else:
match_str = '(any)'
params = ['%s=%s' % (param_name, str(flow['action_params'][param_name])) for param_name in
flow['action_params']]
params = ', '.join(params)
return "%s: %s => %s(%s)" % (
flow['table'], match_str, flow['action_name'], params)
if __name__ == '__main__':
main()