* 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)
162 lines
5.7 KiB
Python
162 lines
5.7 KiB
Python
# 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.
|
|
#
|
|
|
|
from mininet.net import Mininet
|
|
from mininet.node import Switch, Host
|
|
from mininet.log import setLogLevel, info, error, debug
|
|
from mininet.moduledeps import pathCheck
|
|
from sys import exit
|
|
from time import sleep
|
|
import os
|
|
import tempfile
|
|
import socket
|
|
|
|
class P4Host(Host):
|
|
def config(self, **params):
|
|
r = super(P4Host, self).config(**params)
|
|
|
|
for off in ["rx", "tx", "sg"]:
|
|
cmd = "/sbin/ethtool --offload %s %s off" % (self.defaultIntf().name, off)
|
|
self.cmd(cmd)
|
|
|
|
# disable IPv6
|
|
self.cmd("sysctl -w net.ipv6.conf.all.disable_ipv6=1")
|
|
self.cmd("sysctl -w net.ipv6.conf.default.disable_ipv6=1")
|
|
self.cmd("sysctl -w net.ipv6.conf.lo.disable_ipv6=1")
|
|
|
|
return r
|
|
|
|
def describe(self, sw_addr=None, sw_mac=None):
|
|
print "**********"
|
|
print "Network configuration for: %s" % self.name
|
|
print "Default interface: %s\t%s\t%s" %(
|
|
self.defaultIntf().name,
|
|
self.defaultIntf().IP(),
|
|
self.defaultIntf().MAC()
|
|
)
|
|
if sw_addr is not None or sw_mac is not None:
|
|
print "Default route to switch: %s (%s)" % (sw_addr, sw_mac)
|
|
print "**********"
|
|
|
|
class P4Switch(Switch):
|
|
"""P4 virtual switch"""
|
|
device_id = 0
|
|
|
|
def __init__(self, name, sw_path = None, json_path = None,
|
|
log_file = None,
|
|
thrift_port = None,
|
|
pcap_dump = False,
|
|
log_console = False,
|
|
verbose = False,
|
|
device_id = None,
|
|
enable_debugger = False,
|
|
**kwargs):
|
|
Switch.__init__(self, name, **kwargs)
|
|
assert(sw_path)
|
|
assert(json_path)
|
|
# make sure that the provided sw_path is valid
|
|
pathCheck(sw_path)
|
|
# make sure that the provided JSON file exists
|
|
if not os.path.isfile(json_path):
|
|
error("Invalid JSON file.\n")
|
|
exit(1)
|
|
self.sw_path = sw_path
|
|
self.json_path = json_path
|
|
self.verbose = verbose
|
|
self.log_file = log_file
|
|
if self.log_file is None:
|
|
self.log_file = "/tmp/p4s.{}.log".format(self.name)
|
|
self.output = open(self.log_file, 'w')
|
|
self.thrift_port = thrift_port
|
|
self.pcap_dump = pcap_dump
|
|
self.enable_debugger = enable_debugger
|
|
self.log_console = log_console
|
|
if device_id is not None:
|
|
self.device_id = device_id
|
|
P4Switch.device_id = max(P4Switch.device_id, device_id)
|
|
else:
|
|
self.device_id = P4Switch.device_id
|
|
P4Switch.device_id += 1
|
|
self.nanomsg = "ipc:///tmp/bm-{}-log.ipc".format(self.device_id)
|
|
|
|
@classmethod
|
|
def setup(cls):
|
|
pass
|
|
|
|
def check_switch_started(self, pid):
|
|
"""While the process is running (pid exists), we check if the Thrift
|
|
server has been started. If the Thrift server is ready, we assume that
|
|
the switch was started successfully. This is only reliable if the Thrift
|
|
server is started at the end of the init process"""
|
|
while True:
|
|
if not os.path.exists(os.path.join("/proc", str(pid))):
|
|
return False
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
sock.settimeout(0.5)
|
|
result = sock.connect_ex(("localhost", self.thrift_port))
|
|
if result == 0:
|
|
return True
|
|
|
|
def start(self, controllers):
|
|
"Start up a new P4 switch"
|
|
info("Starting P4 switch {}.\n".format(self.name))
|
|
args = [self.sw_path]
|
|
for port, intf in self.intfs.items():
|
|
if not intf.IP():
|
|
args.extend(['-i', str(port) + "@" + intf.name])
|
|
if self.pcap_dump:
|
|
args.append("--pcap")
|
|
# args.append("--useFiles")
|
|
if self.thrift_port:
|
|
args.extend(['--thrift-port', str(self.thrift_port)])
|
|
if self.nanomsg:
|
|
args.extend(['--nanolog', self.nanomsg])
|
|
args.extend(['--device-id', str(self.device_id)])
|
|
P4Switch.device_id += 1
|
|
args.append(self.json_path)
|
|
if self.enable_debugger:
|
|
args.append("--debugger")
|
|
if self.log_console:
|
|
args.append("--log-console")
|
|
info(' '.join(args) + "\n")
|
|
|
|
pid = None
|
|
with tempfile.NamedTemporaryFile() as f:
|
|
# self.cmd(' '.join(args) + ' > /dev/null 2>&1 &')
|
|
self.cmd(' '.join(args) + ' >' + self.log_file + ' 2>&1 & echo $! >> ' + f.name)
|
|
pid = int(f.read())
|
|
debug("P4 switch {} PID is {}.\n".format(self.name, pid))
|
|
sleep(1)
|
|
if not self.check_switch_started(pid):
|
|
error("P4 switch {} did not start correctly."
|
|
"Check the switch log file.\n".format(self.name))
|
|
exit(1)
|
|
info("P4 switch {} has been started.\n".format(self.name))
|
|
|
|
def stop(self):
|
|
"Terminate P4 switch."
|
|
self.output.flush()
|
|
self.cmd('kill %' + self.sw_path)
|
|
self.cmd('wait')
|
|
self.deleteIntfs()
|
|
|
|
def attach(self, intf):
|
|
"Connect a data port"
|
|
assert(0)
|
|
|
|
def detach(self, intf):
|
|
"Disconnect a data port"
|
|
assert(0)
|