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)
This commit is contained in:
Nate Foster
2018-06-01 02:54:33 -04:00
committed by GitHub
parent e7e6899d5c
commit dc08948a34
503 changed files with 1432 additions and 30666 deletions

42
utils/Makefile Normal file
View File

@@ -0,0 +1,42 @@
BUILD_DIR = build
PCAP_DIR = pcaps
LOG_DIR = logs
TOPO = topology.json
P4C = p4c-bm2-ss
RUN_SCRIPT = ../../utils/run_exercise.py
source := $(wildcard *.p4)
outfile := $(source:.p4=.json)
compiled_json := $(BUILD_DIR)/$(outfile)
# Define NO_P4 to start BMv2 without a program
ifndef NO_P4
run_args += -j $(compiled_json)
endif
# Set BMV2_SWITCH_EXE to override the BMv2 target
ifdef BMV2_SWITCH_EXE
run_args += -b $(BMV2_SWITCH_EXE)
endif
all: run
run: build
sudo python $(RUN_SCRIPT) -t $(TOPO) $(run_args)
stop:
sudo mn -c
build: dirs $(compiled_json)
$(BUILD_DIR)/%.json: %.p4
$(P4C) --p4v 16 $(P4C_ARGS) -o $@ $<
dirs:
mkdir -p $(BUILD_DIR) $(PCAP_DIR) $(LOG_DIR)
clean: stop
rm -f *.pcap
rm -rf $(BUILD_DIR) $(PCAP_DIR) $(LOG_DIR)

View File

@@ -0,0 +1,104 @@
import subprocess
from shortest_path import ShortestPath
class AppController:
def __init__(self, manifest=None, target=None, topo=None, net=None, links=None):
self.manifest = manifest
self.target = target
self.conf = manifest['targets'][target]
self.topo = topo
self.net = net
self.links = links
def read_entries(self, filename):
entries = []
with open(filename, 'r') as f:
for line in f:
line = line.strip()
if line == '': continue
entries.append(line)
return entries
def add_entries(self, thrift_port=9090, sw=None, entries=None):
assert entries
if sw: thrift_port = sw.thrift_port
print '\n'.join(entries)
p = subprocess.Popen(['simple_switch_CLI', '--thrift-port', str(thrift_port)], stdin=subprocess.PIPE)
p.communicate(input='\n'.join(entries))
def read_register(self, register, idx, thrift_port=9090, sw=None):
if sw: thrift_port = sw.thrift_port
p = subprocess.Popen(['simple_switch_CLI', '--thrift-port', str(thrift_port)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate(input="register_read %s %d" % (register, idx))
reg_val = filter(lambda l: ' %s[%d]' % (register, idx) in l, stdout.split('\n'))[0].split('= ', 1)[1]
return long(reg_val)
def start(self):
shortestpath = ShortestPath(self.links)
entries = {}
for sw in self.topo.switches():
entries[sw] = []
if 'switches' in self.conf and sw in self.conf['switches'] and 'entries' in self.conf['switches'][sw]:
extra_entries = self.conf['switches'][sw]['entries']
if type(extra_entries) == list: # array of entries
entries[sw] += extra_entries
else: # path to file that contains entries
entries[sw] += self.read_entries(extra_entries)
#entries[sw] += [
# 'table_set_default send_frame _drop',
# 'table_set_default forward _drop',
# 'table_set_default ipv4_lpm _drop']
for host_name in self.topo._host_links:
h = self.net.get(host_name)
for link in self.topo._host_links[host_name].values():
sw = link['sw']
#entries[sw].append('table_add send_frame rewrite_mac %d => %s' % (link['sw_port'], link['sw_mac']))
#entries[sw].append('table_add forward set_dmac %s => %s' % (link['host_ip'], link['host_mac']))
#entries[sw].append('table_add ipv4_lpm set_nhop %s/32 => %s %d' % (link['host_ip'], link['host_ip'], link['sw_port']))
iface = h.intfNames()[link['idx']]
# use mininet to set ip and mac to let it know the change
h.setIP(link['host_ip'], 24)
h.setMAC(link['host_mac'])
#h.cmd('ifconfig %s %s hw ether %s' % (iface, link['host_ip'], link['host_mac']))
h.cmd('arp -i %s -s %s %s' % (iface, link['sw_ip'], link['sw_mac']))
h.cmd('ethtool --offload %s rx off tx off' % iface)
h.cmd('ip route add %s dev %s' % (link['sw_ip'], iface))
h.setDefaultRoute("via %s" % link['sw_ip'])
for h in self.net.hosts:
h_link = self.topo._host_links[h.name].values()[0]
for sw in self.net.switches:
path = shortestpath.get(sw.name, h.name, exclude=lambda n: n[0]=='h')
if not path: continue
if not path[1][0] == 's': continue # next hop is a switch
sw_link = self.topo._sw_links[sw.name][path[1]]
#entries[sw.name].append('table_add send_frame rewrite_mac %d => %s' % (sw_link[0]['port'], sw_link[0]['mac']))
#entries[sw.name].append('table_add forward set_dmac %s => %s' % (h_link['host_ip'], sw_link[1]['mac']))
#entries[sw.name].append('table_add ipv4_lpm set_nhop %s/32 => %s %d' % (h_link['host_ip'], h_link['host_ip'], sw_link[0]['port']))
for h2 in self.net.hosts:
if h == h2: continue
path = shortestpath.get(h.name, h2.name, exclude=lambda n: n[0]=='h')
if not path: continue
h_link = self.topo._host_links[h.name][path[1]]
h2_link = self.topo._host_links[h2.name].values()[0]
h.cmd('ip route add %s via %s' % (h2_link['host_ip'], h_link['sw_ip']))
print "**********"
print "Configuring entries in p4 tables"
for sw_name in entries:
print
print "Configuring switch... %s" % sw_name
sw = self.net.get(sw_name)
if entries[sw_name]:
self.add_entries(sw=sw, entries=entries[sw_name])
print "Configuration complete."
print "**********"
def stop(self):
pass

70
utils/mininet/apptopo.py Normal file
View File

@@ -0,0 +1,70 @@
from mininet.topo import Topo
class AppTopo(Topo):
def __init__(self, links, latencies={}, manifest=None, target=None,
log_dir="/tmp", bws={}, **opts):
Topo.__init__(self, **opts)
nodes = sum(map(list, zip(*links)), [])
host_names = sorted(list(set(filter(lambda n: n[0] == 'h', nodes))))
sw_names = sorted(list(set(filter(lambda n: n[0] == 's', nodes))))
sw_ports = dict([(sw, []) for sw in sw_names])
self._host_links = {}
self._sw_links = dict([(sw, {}) for sw in sw_names])
for sw_name in sw_names:
self.addSwitch(sw_name, log_file="%s/%s.log" %(log_dir, sw_name))
for host_name in host_names:
host_num = int(host_name[1:])
self.addHost(host_name)
self._host_links[host_name] = {}
host_links = filter(lambda l: l[0]==host_name or l[1]==host_name, links)
sw_idx = 0
for link in host_links:
sw = link[0] if link[0] != host_name else link[1]
sw_num = int(sw[1:])
assert sw[0]=='s', "Hosts should be connected to switches, not " + str(sw)
host_ip = "10.0.%d.%d" % (sw_num, host_num)
host_mac = '00:00:00:00:%02x:%02x' % (sw_num, host_num)
delay_key = ''.join([host_name, sw])
delay = latencies[delay_key] if delay_key in latencies else '0ms'
bw = bws[delay_key] if delay_key in bws else None
sw_ports[sw].append(host_name)
self._host_links[host_name][sw] = dict(
idx=sw_idx,
host_mac = host_mac,
host_ip = host_ip,
sw = sw,
sw_mac = "00:00:00:00:%02x:%02x" % (sw_num, host_num),
sw_ip = "10.0.%d.%d" % (sw_num, 254),
sw_port = sw_ports[sw].index(host_name)+1
)
self.addLink(host_name, sw, delay=delay, bw=bw,
addr1=host_mac, addr2=self._host_links[host_name][sw]['sw_mac'])
sw_idx += 1
for link in links: # only check switch-switch links
sw1, sw2 = link
if sw1[0] != 's' or sw2[0] != 's': continue
delay_key = ''.join(sorted([sw1, sw2]))
delay = latencies[delay_key] if delay_key in latencies else '0ms'
bw = bws[delay_key] if delay_key in bws else None
self.addLink(sw1, sw2, delay=delay, bw=bw)#, max_queue_size=10)
sw_ports[sw1].append(sw2)
sw_ports[sw2].append(sw1)
sw1_num, sw2_num = int(sw1[1:]), int(sw2[1:])
sw1_port = dict(mac="00:00:00:%02x:%02x:00" % (sw1_num, sw2_num), port=sw_ports[sw1].index(sw2)+1)
sw2_port = dict(mac="00:00:00:%02x:%02x:00" % (sw2_num, sw1_num), port=sw_ports[sw2].index(sw1)+1)
self._sw_links[sw1][sw2] = [sw1_port, sw2_port]
self._sw_links[sw2][sw1] = [sw2_port, sw1_port]

View File

@@ -0,0 +1,243 @@
#!/usr/bin/env python2
# 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.
#
import signal
import os
import sys
import subprocess
import argparse
import json
import importlib
import re
from time import sleep
from mininet.net import Mininet
from mininet.topo import Topo
from mininet.link import TCLink
from mininet.log import setLogLevel, info
from mininet.cli import CLI
from p4_mininet import P4Switch, P4Host
import apptopo
import appcontroller
parser = argparse.ArgumentParser(description='Mininet demo')
parser.add_argument('--behavioral-exe', help='Path to behavioral executable',
type=str, action="store", required=True)
parser.add_argument('--thrift-port', help='Thrift server port for table updates',
type=int, action="store", default=9090)
parser.add_argument('--bmv2-log', help='verbose messages in log file', action="store_true")
parser.add_argument('--cli', help="start the mininet cli", action="store_true")
parser.add_argument('--auto-control-plane', help='enable automatic control plane population', action="store_true")
parser.add_argument('--json', help='Path to JSON config file',
type=str, action="store", required=True)
parser.add_argument('--pcap-dump', help='Dump packets on interfaces to pcap files',
action="store_true")
parser.add_argument('--manifest', '-m', help='Path to manifest file',
type=str, action="store", required=True)
parser.add_argument('--target', '-t', help='Target in manifest file to run',
type=str, action="store", required=True)
parser.add_argument('--log-dir', '-l', help='Location to save output to',
type=str, action="store", required=True)
parser.add_argument('--cli-message', help='Message to print before starting CLI',
type=str, action="store", required=False, default=False)
args = parser.parse_args()
next_thrift_port = args.thrift_port
def run_command(command):
return os.WEXITSTATUS(os.system(command))
def configureP4Switch(**switch_args):
class ConfiguredP4Switch(P4Switch):
def __init__(self, *opts, **kwargs):
global next_thrift_port
kwargs.update(switch_args)
kwargs['thrift_port'] = next_thrift_port
next_thrift_port += 1
P4Switch.__init__(self, *opts, **kwargs)
return ConfiguredP4Switch
def main():
with open(args.manifest, 'r') as f:
manifest = json.load(f)
conf = manifest['targets'][args.target]
params = conf['parameters'] if 'parameters' in conf else {}
os.environ.update(dict(map(lambda (k,v): (k, str(v)), params.iteritems())))
def formatParams(s):
for param in params:
s = re.sub('\$'+param+'(\W|$)', str(params[param]) + r'\1', s)
s = s.replace('${'+param+'}', str(params[param]))
return s
AppTopo = apptopo.AppTopo
AppController = appcontroller.AppController
if 'topo_module' in conf:
sys.path.insert(0, os.path.dirname(args.manifest))
topo_module = importlib.import_module(conf['topo_module'])
AppTopo = topo_module.CustomAppTopo
if 'controller_module' in conf:
sys.path.insert(0, os.path.dirname(args.manifest))
controller_module = importlib.import_module(conf['controller_module'])
AppController = controller_module.CustomAppController
if not os.path.isdir(args.log_dir):
if os.path.exists(args.log_dir): raise Exception('Log dir exists and is not a dir')
os.mkdir(args.log_dir)
os.environ['P4APP_LOGDIR'] = args.log_dir
links = [l[:2] for l in conf['links']]
latencies = dict([(''.join(sorted(l[:2])), l[2]) for l in conf['links'] if len(l)>=3])
bws = dict([(''.join(sorted(l[:2])), l[3]) for l in conf['links'] if len(l)>=4])
for host_name in sorted(conf['hosts'].keys()):
host = conf['hosts'][host_name]
if 'latency' not in host: continue
for a, b in links:
if a != host_name and b != host_name: continue
other = a if a != host_name else b
latencies[host_name+other] = host['latency']
for l in latencies:
if isinstance(latencies[l], (str, unicode)):
latencies[l] = formatParams(latencies[l])
else:
latencies[l] = str(latencies[l]) + "ms"
bmv2_log = args.bmv2_log or ('bmv2_log' in conf and conf['bmv2_log'])
pcap_dump = args.pcap_dump or ('pcap_dump' in conf and conf['pcap_dump'])
topo = AppTopo(links, latencies, manifest=manifest, target=args.target,
log_dir=args.log_dir, bws=bws)
switchClass = configureP4Switch(
sw_path=args.behavioral_exe,
json_path=args.json,
log_console=bmv2_log,
pcap_dump=pcap_dump)
net = Mininet(topo = topo,
link = TCLink,
host = P4Host,
switch = switchClass,
controller = None)
net.start()
sleep(1)
controller = None
if args.auto_control_plane or 'controller_module' in conf:
controller = AppController(manifest=manifest, target=args.target,
topo=topo, net=net, links=links)
controller.start()
for h in net.hosts:
h.describe()
if args.cli_message is not None:
with open(args.cli_message, 'r') as message_file:
print message_file.read()
if args.cli or ('cli' in conf and conf['cli']):
CLI(net)
stdout_files = dict()
return_codes = []
host_procs = []
def formatCmd(cmd):
for h in net.hosts:
cmd = cmd.replace(h.name, h.defaultIntf().updateIP())
return cmd
def _wait_for_exit(p, host):
print p.communicate()
if p.returncode is None:
p.wait()
print p.communicate()
return_codes.append(p.returncode)
if host_name in stdout_files:
stdout_files[host_name].flush()
stdout_files[host_name].close()
print '\n'.join(map(lambda (k,v): "%s: %s"%(k,v), params.iteritems())) + '\n'
for host_name in sorted(conf['hosts'].keys()):
host = conf['hosts'][host_name]
if 'cmd' not in host: continue
h = net.get(host_name)
stdout_filename = os.path.join(args.log_dir, h.name + '.stdout')
stdout_files[h.name] = open(stdout_filename, 'w')
cmd = formatCmd(host['cmd'])
print h.name, cmd
p = h.popen(cmd, stdout=stdout_files[h.name], shell=True, preexec_fn=os.setpgrp)
if 'startup_sleep' in host: sleep(host['startup_sleep'])
if 'wait' in host and host['wait']:
_wait_for_exit(p, host_name)
else:
host_procs.append((p, host_name))
for p, host_name in host_procs:
if 'wait' in conf['hosts'][host_name] and conf['hosts'][host_name]['wait']:
_wait_for_exit(p, host_name)
for p, host_name in host_procs:
if 'wait' in conf['hosts'][host_name] and conf['hosts'][host_name]['wait']:
continue
if p.returncode is None:
run_command('pkill -INT -P %d' % p.pid)
sleep(0.2)
rc = run_command('pkill -0 -P %d' % p.pid) # check if it's still running
if rc == 0: # the process group is still running, send TERM
sleep(1) # give it a little more time to exit gracefully
run_command('pkill -TERM -P %d' % p.pid)
_wait_for_exit(p, host_name)
if 'after' in conf and 'cmd' in conf['after']:
cmds = conf['after']['cmd'] if type(conf['after']['cmd']) == list else [conf['after']['cmd']]
for cmd in cmds:
os.system(cmd)
if controller: controller.stop()
net.stop()
# if bmv2_log:
# os.system('bash -c "cp /tmp/p4s.s*.log \'%s\'"' % args.log_dir)
# if pcap_dump:
# os.system('bash -c "cp *.pcap \'%s\'"' % args.log_dir)
bad_codes = [rc for rc in return_codes if rc != 0]
if len(bad_codes): sys.exit(1)
if __name__ == '__main__':
setLogLevel( 'info' )
main()

161
utils/mininet/p4_mininet.py Normal file
View File

@@ -0,0 +1,161 @@
# 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)

View File

@@ -0,0 +1,78 @@
class ShortestPath:
def __init__(self, edges=[]):
self.neighbors = {}
for edge in edges:
self.addEdge(*edge)
def addEdge(self, a, b):
if a not in self.neighbors: self.neighbors[a] = []
if b not in self.neighbors[a]: self.neighbors[a].append(b)
if b not in self.neighbors: self.neighbors[b] = []
if a not in self.neighbors[b]: self.neighbors[b].append(a)
def get(self, a, b, exclude=lambda node: False):
# Shortest path from a to b
return self._recPath(a, b, [], exclude)
def _recPath(self, a, b, visited, exclude):
if a == b: return [a]
new_visited = visited + [a]
paths = []
for neighbor in self.neighbors[a]:
if neighbor in new_visited: continue
if exclude(neighbor) and neighbor != b: continue
path = self._recPath(neighbor, b, new_visited, exclude)
if path: paths.append(path)
paths.sort(key=len)
return [a] + paths[0] if len(paths) else None
if __name__ == '__main__':
edges = [
(1, 2),
(1, 3),
(1, 5),
(2, 4),
(3, 4),
(3, 5),
(3, 6),
(4, 6),
(5, 6),
(7, 8)
]
sp = ShortestPath(edges)
assert sp.get(1, 1) == [1]
assert sp.get(2, 2) == [2]
assert sp.get(1, 2) == [1, 2]
assert sp.get(2, 1) == [2, 1]
assert sp.get(1, 3) == [1, 3]
assert sp.get(3, 1) == [3, 1]
assert sp.get(4, 6) == [4, 6]
assert sp.get(6, 4) == [6, 4]
assert sp.get(2, 6) == [2, 4, 6]
assert sp.get(6, 2) == [6, 4, 2]
assert sp.get(1, 6) in [[1, 3, 6], [1, 5, 6]]
assert sp.get(6, 1) in [[6, 3, 1], [6, 5, 1]]
assert sp.get(2, 5) == [2, 1, 5]
assert sp.get(5, 2) == [5, 1, 2]
assert sp.get(4, 5) in [[4, 3, 5], [4, 6, 5]]
assert sp.get(5, 4) in [[5, 3, 4], [6, 6, 4]]
assert sp.get(7, 8) == [7, 8]
assert sp.get(8, 7) == [8, 7]
assert sp.get(1, 7) == None
assert sp.get(7, 2) == None

View File

@@ -0,0 +1,133 @@
#!/usr/bin/env python2
# 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.topo import Topo
from mininet.log import setLogLevel, info
from mininet.cli import CLI
from p4_mininet import P4Switch, P4Host
import argparse
from subprocess import PIPE, Popen
from time import sleep
parser = argparse.ArgumentParser(description='Mininet demo')
parser.add_argument('--behavioral-exe', help='Path to behavioral executable',
type=str, action="store", required=True)
parser.add_argument('--thrift-port', help='Thrift server port for table updates',
type=int, action="store", default=9090)
parser.add_argument('--num-hosts', help='Number of hosts to connect to switch',
type=int, action="store", default=2)
parser.add_argument('--mode', choices=['l2', 'l3'], type=str, default='l3')
parser.add_argument('--json', help='Path to JSON config file',
type=str, action="store", required=True)
parser.add_argument('--log-file', help='Path to write the switch log file',
type=str, action="store", required=False)
parser.add_argument('--pcap-dump', help='Dump packets on interfaces to pcap files',
type=str, action="store", required=False, default=False)
parser.add_argument('--switch-config', help='simple_switch_CLI script to configure switch',
type=str, action="store", required=False, default=False)
parser.add_argument('--cli-message', help='Message to print before starting CLI',
type=str, action="store", required=False, default=False)
args = parser.parse_args()
class SingleSwitchTopo(Topo):
"Single switch connected to n (< 256) hosts."
def __init__(self, sw_path, json_path, log_file,
thrift_port, pcap_dump, n, **opts):
# Initialize topology and default options
Topo.__init__(self, **opts)
switch = self.addSwitch('s1',
sw_path = sw_path,
json_path = json_path,
log_console = True,
log_file = log_file,
thrift_port = thrift_port,
enable_debugger = False,
pcap_dump = pcap_dump)
for h in xrange(n):
host = self.addHost('h%d' % (h + 1),
ip = "10.0.%d.10/24" % h,
mac = '00:04:00:00:00:%02x' %h)
print "Adding host", str(host)
self.addLink(host, switch)
def main():
num_hosts = args.num_hosts
mode = args.mode
topo = SingleSwitchTopo(args.behavioral_exe,
args.json,
args.log_file,
args.thrift_port,
args.pcap_dump,
num_hosts)
net = Mininet(topo = topo,
host = P4Host,
switch = P4Switch,
controller = None)
net.start()
sw_mac = ["00:aa:bb:00:00:%02x" % n for n in xrange(num_hosts)]
sw_addr = ["10.0.%d.1" % n for n in xrange(num_hosts)]
for n in xrange(num_hosts):
h = net.get('h%d' % (n + 1))
if mode == "l2":
h.setDefaultRoute("dev %s" % h.defaultIntf().name)
else:
h.setARP(sw_addr[n], sw_mac[n])
h.setDefaultRoute("dev %s via %s" % (h.defaultIntf().name, sw_addr[n]))
for n in xrange(num_hosts):
h = net.get('h%d' % (n + 1))
h.describe(sw_addr[n], sw_mac[n])
sleep(1)
if args.switch_config is not None:
print
print "Reading switch configuration script:", args.switch_config
with open(args.switch_config, 'r') as config_file:
switch_config = config_file.read()
print "Configuring switch..."
proc = Popen(["simple_switch_CLI"], stdin=PIPE)
proc.communicate(input=switch_config)
print "Configuration complete."
print
print "Ready !"
if args.cli_message is not None:
with open(args.cli_message, 'r') as message_file:
print message_file.read()
CLI( net )
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
main()

21
utils/netstat.py Normal file
View File

@@ -0,0 +1,21 @@
# 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 psutil
def check_listening_on_port(port):
for c in psutil.net_connections(kind='inet'):
if c.status == 'LISTEN' and c.laddr[1] == port:
return True
return False

162
utils/p4_mininet.py Normal file
View File

@@ -0,0 +1,162 @@
# 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
import os
import tempfile
import socket
from time import sleep
from netstat import check_listening_on_port
SWITCH_START_TIMEOUT = 10 # seconds
class P4Host(Host):
def config(self, **params):
r = super(Host, self).config(**params)
self.defaultIntf().rename("eth0")
for off in ["rx", "tx", "sg"]:
cmd = "/sbin/ethtool --offload eth0 %s off" % 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):
print "**********"
print self.name
print "default interface: %s\t%s\t%s" %(
self.defaultIntf().name,
self.defaultIntf().IP(),
self.defaultIntf().MAC()
)
print "**********"
class P4Switch(Switch):
"""P4 virtual switch"""
device_id = 0
def __init__(self, name, sw_path = None, json_path = 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
logfile = "/tmp/p4s.{}.log".format(self.name)
self.output = open(logfile, 'w')
self.thrift_port = thrift_port
if check_listening_on_port(self.thrift_port):
error('%s cannot bind port %d because it is bound by another process\n' % (self.name, self.grpc_port))
exit(1)
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
if check_listening_on_port(self.thrift_port):
return True
sleep(0.5)
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")
logfile = "/tmp/p4s.{}.log".format(self.name)
info(' '.join(args) + "\n")
pid = None
with tempfile.NamedTemporaryFile() as f:
# self.cmd(' '.join(args) + ' > /dev/null 2>&1 &')
self.cmd(' '.join(args) + ' >' + logfile + ' 2>&1 & echo $! >> ' + f.name)
pid = int(f.read())
debug("P4 switch {} PID is {}.\n".format(self.name, pid))
if not self.check_switch_started(pid):
error("P4 switch {} did not start correctly.\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)

320
utils/p4apprunner.py Executable file
View File

@@ -0,0 +1,320 @@
#!/usr/bin/env python2
# 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 __future__ import print_function
import argparse
from collections import OrderedDict
import json
import os
import sys
import tarfile
parser = argparse.ArgumentParser(description='p4apprunner')
parser.add_argument('--build-dir', help='Directory to build in.',
type=str, action='store', required=False, default='/tmp')
parser.add_argument('--quiet', help='Suppress log messages.',
action='store_true', required=False, default=False)
parser.add_argument('--manifest', help='Path to manifest file.',
type=str, action='store', required=False, default='./p4app.json')
parser.add_argument('app', help='.p4app package to run.', type=str)
parser.add_argument('target', help=('Target to run. Defaults to the first target '
'in the package.'),
nargs='?', type=str)
args = parser.parse_args()
def log(*items):
if args.quiet != True:
print(*items)
def log_error(*items):
print(*items, file=sys.stderr)
def run_command(command):
log('>', command)
return os.WEXITSTATUS(os.system(command))
class Manifest:
def __init__(self, program_file, language, target, target_config):
self.program_file = program_file
self.language = language
self.target = target
self.target_config = target_config
def read_manifest(manifest_file):
manifest = json.load(manifest_file, object_pairs_hook=OrderedDict)
if 'program' not in manifest:
log_error('No program defined in manifest.')
sys.exit(1)
program_file = manifest['program']
if 'language' not in manifest:
log_error('No language defined in manifest.')
sys.exit(1)
language = manifest['language']
if 'targets' not in manifest or len(manifest['targets']) < 1:
log_error('No targets defined in manifest.')
sys.exit(1)
if args.target is not None:
chosen_target = args.target
elif 'default-target' in manifest:
chosen_target = manifest['default-target']
else:
chosen_target = manifest['targets'].keys()[0]
if chosen_target not in manifest['targets']:
log_error('Target not found in manifest:', chosen_target)
sys.exit(1)
return Manifest(program_file, language, chosen_target, manifest['targets'][chosen_target])
def run_compile_bmv2(manifest):
if 'run-before-compile' in manifest.target_config:
commands = manifest.target_config['run-before-compile']
if not isinstance(commands, list):
log_error('run-before-compile should be a list:', commands)
sys.exit(1)
for command in commands:
run_command(command)
compiler_args = []
if manifest.language == 'p4-14':
compiler_args.append('--p4v 14')
elif manifest.language == 'p4-16':
compiler_args.append('--p4v 16')
else:
log_error('Unknown language:', manifest.language)
sys.exit(1)
if 'compiler-flags' in manifest.target_config:
flags = manifest.target_config['compiler-flags']
if not isinstance(flags, list):
log_error('compiler-flags should be a list:', flags)
sys.exit(1)
compiler_args.extend(flags)
# Compile the program.
output_file = manifest.program_file + '.json'
compiler_args.append('"%s"' % manifest.program_file)
compiler_args.append('-o "%s"' % output_file)
rv = run_command('p4c-bm2-ss %s' % ' '.join(compiler_args))
if 'run-after-compile' in manifest.target_config:
commands = manifest.target_config['run-after-compile']
if not isinstance(commands, list):
log_error('run-after-compile should be a list:', commands)
sys.exit(1)
for command in commands:
run_command(command)
if rv != 0:
log_error('Compile failed.')
sys.exit(1)
return output_file
def run_mininet(manifest):
output_file = run_compile_bmv2(manifest)
# Run the program using the BMV2 Mininet simple switch.
switch_args = []
# We'll place the switch's log file in current (build) folder.
cwd = os.getcwd()
log_file = os.path.join(cwd, manifest.program_file + '.log')
print ("*** Log file %s" % log_file)
switch_args.append('--log-file "%s"' % log_file)
pcap_dir = os.path.join(cwd)
print ("*** Pcap folder %s" % pcap_dir)
switch_args.append('--pcap-dump "%s" '% pcap_dir)
# Generate a message that will be printed by the Mininet CLI to make
# interacting with the simple switch a little easier.
message_file = 'mininet_message.txt'
with open(message_file, 'w') as message:
print(file=message)
print('======================================================================',
file=message)
print('Welcome to the BMV2 Mininet CLI!', file=message)
print('======================================================================',
file=message)
print('Your P4 program is installed into the BMV2 software switch', file=message)
print('and your initial configuration is loaded. You can interact', file=message)
print('with the network using the mininet CLI below.', file=message)
print(file=message)
print('To inspect or change the switch configuration, connect to', file=message)
print('its CLI from your host operating system using this command:', file=message)
print(' simple_switch_CLI', file=message)
print(file=message)
print('To view the switch log, run this command from your host OS:', file=message)
print(' tail -f %s' % log_file, file=message)
print(file=message)
print('To view the switch output pcap, check the pcap files in %s:' % pcap_dir, file=message)
print(' for example run: sudo tcpdump -xxx -r s1-eth1.pcap', file=message)
print(file=message)
# print('To run the switch debugger, run this command from your host OS:', file=message)
# print(' bm_p4dbg' , file=message)
# print(file=message)
switch_args.append('--cli-message "%s"' % message_file)
if 'num-hosts' in manifest.target_config:
switch_args.append('--num-hosts %s' % manifest.target_config['num-hosts'])
if 'switch-config' in manifest.target_config:
switch_args.append('--switch-config "%s"' % manifest.target_config['switch-config'])
switch_args.append('--behavioral-exe "%s"' % 'simple_switch')
switch_args.append('--json "%s"' % output_file)
program = '"%s/mininet/single_switch_mininet.py"' % sys.path[0]
return run_command('python2 %s %s' % (program, ' '.join(switch_args)))
def run_multiswitch(manifest):
output_file = run_compile_bmv2(manifest)
script_args = []
cwd = os.getcwd()
log_dir = os.path.join(cwd, cwd + '/logs')
print ("*** Log directory %s" % log_dir)
script_args.append('--log-dir "%s"' % log_dir)
pcap_dir = os.path.join(cwd)
print ("*** Pcap directory %s" % cwd)
script_args.append('--manifest "%s"' % args.manifest)
script_args.append('--target "%s"' % manifest.target)
if 'auto-control-plane' in manifest.target_config and manifest.target_config['auto-control-plane']:
script_args.append('--auto-control-plane' )
script_args.append('--behavioral-exe "%s"' % 'simple_switch')
script_args.append('--json "%s"' % output_file)
#script_args.append('--cli')
# Generate a message that will be printed by the Mininet CLI to make
# interacting with the simple switch a little easier.
message_file = 'mininet_message.txt'
with open(message_file, 'w') as message:
print(file=message)
print('======================================================================',
file=message)
print('Welcome to the BMV2 Mininet CLI!', file=message)
print('======================================================================',
file=message)
print('Your P4 program is installed into the BMV2 software switch', file=message)
print('and your initial configuration is loaded. You can interact', file=message)
print('with the network using the mininet CLI below.', file=message)
print(file=message)
print('To inspect or change the switch configuration, connect to', file=message)
print('its CLI from your host operating system using this command:', file=message)
print(' simple_switch_CLI --thrift-port <switch thrift port>', file=message)
print(file=message)
print('To view a switch log, run this command from your host OS:', file=message)
print(' tail -f %s/<switchname>.log' % log_dir, file=message)
print(file=message)
print('To view the switch output pcap, check the pcap files in %s:' % pcap_dir, file=message)
print(' for example run: sudo tcpdump -xxx -r s1-eth1.pcap', file=message)
print(file=message)
# print('To run the switch debugger, run this command from your host OS:', file=message)
# print(' bm_p4dbg' , file=message)
# print(file=message)
script_args.append('--cli-message "%s"' % message_file)
program = '"%s/mininet/multi_switch_mininet.py"' % sys.path[0]
return run_command('python2 %s %s' % (program, ' '.join(script_args)))
def run_stf(manifest):
output_file = run_compile_bmv2(manifest)
if not 'test' in manifest.target_config:
log_error('No STF test file provided.')
sys.exit(1)
stf_file = manifest.target_config['test']
# Run the program using the BMV2 STF interpreter.
stf_args = []
stf_args.append('-v')
stf_args.append(os.path.join(args.build_dir, output_file))
stf_args.append(os.path.join(args.build_dir, stf_file))
program = '"%s/stf/bmv2stf.py"' % sys.path[0]
rv = run_command('python2 %s %s' % (program, ' '.join(stf_args)))
if rv != 0:
sys.exit(1)
return rv
def run_custom(manifest):
output_file = run_compile_bmv2(manifest)
python_path = 'PYTHONPATH=$PYTHONPATH:/scripts/mininet/'
script_args = []
script_args.append('--behavioral-exe "%s"' % 'simple_switch')
script_args.append('--json "%s"' % output_file)
script_args.append('--cli "%s"' % 'simple_switch_CLI')
if not 'program' in manifest.target_config:
log_error('No mininet program file provided.')
sys.exit(1)
program = manifest.target_config['program']
rv = run_command('%s python2 %s %s' % (python_path, program, ' '.join(script_args)))
if rv != 0:
sys.exit(1)
return rv
def main():
log('Entering build directory.')
os.chdir(args.build_dir)
# A '.p4app' package is really just a '.tar.gz' archive. Extract it so we
# can process its contents.
log('Extracting package.')
tar = tarfile.open(args.app)
tar.extractall()
tar.close()
log('Reading package manifest.')
with open(args.manifest, 'r') as manifest_file:
manifest = read_manifest(manifest_file)
# Dispatch to the backend implementation for this target.
backend = manifest.target
if 'use' in manifest.target_config:
backend = manifest.target_config['use']
if backend == 'mininet':
rc = run_mininet(manifest)
elif backend == 'multiswitch':
rc = run_multiswitch(manifest)
elif backend == 'stf':
rc = run_stf(manifest)
elif backend == 'custom':
rc = run_custom(manifest)
elif backend == 'compile-bmv2':
run_compile_bmv2(manifest)
rc = 0
else:
log_error('Target specifies unknown backend:', backend)
sys.exit(1)
sys.exit(rc)
if __name__ == '__main__':
main()

View File

View File

@@ -0,0 +1,30 @@
# 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.
#
from switch import SwitchConnection
from p4.tmp import p4config_pb2
def buildDeviceConfig(bmv2_json_file_path=None):
"Builds the device config for BMv2"
device_config = p4config_pb2.P4DeviceConfig()
device_config.reassign = True
with open(bmv2_json_file_path) as f:
device_config.device_data = f.read()
return device_config
class Bmv2SwitchConnection(SwitchConnection):
def buildDeviceConfig(self, **kwargs):
return buildDeviceConfig(**kwargs)

View File

@@ -0,0 +1,119 @@
# 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 re
import socket
import math
'''
This package contains several helper functions for encoding to and decoding from byte strings:
- integers
- IPv4 address strings
- Ethernet address strings
'''
mac_pattern = re.compile('^([\da-fA-F]{2}:){5}([\da-fA-F]{2})$')
def matchesMac(mac_addr_string):
return mac_pattern.match(mac_addr_string) is not None
def encodeMac(mac_addr_string):
return mac_addr_string.replace(':', '').decode('hex')
def decodeMac(encoded_mac_addr):
return ':'.join(s.encode('hex') for s in encoded_mac_addr)
ip_pattern = re.compile('^(\d{1,3}\.){3}(\d{1,3})$')
def matchesIPv4(ip_addr_string):
return ip_pattern.match(ip_addr_string) is not None
def encodeIPv4(ip_addr_string):
return socket.inet_aton(ip_addr_string)
def decodeIPv4(encoded_ip_addr):
return socket.inet_ntoa(encoded_ip_addr)
def bitwidthToBytes(bitwidth):
return int(math.ceil(bitwidth / 8.0))
def encodeNum(number, bitwidth):
byte_len = bitwidthToBytes(bitwidth)
num_str = '%x' % number
if number >= 2 ** bitwidth:
raise Exception("Number, %d, does not fit in %d bits" % (number, bitwidth))
return ('0' * (byte_len * 2 - len(num_str)) + num_str).decode('hex')
def decodeNum(encoded_number):
return int(encoded_number.encode('hex'), 16)
def encode(x, bitwidth):
'Tries to infer the type of `x` and encode it'
byte_len = bitwidthToBytes(bitwidth)
if (type(x) == list or type(x) == tuple) and len(x) == 1:
x = x[0]
encoded_bytes = None
if type(x) == str:
if matchesMac(x):
encoded_bytes = encodeMac(x)
elif matchesIPv4(x):
encoded_bytes = encodeIPv4(x)
else:
# Assume that the string is already encoded
encoded_bytes = x
elif type(x) == int:
encoded_bytes = encodeNum(x, bitwidth)
else:
raise Exception("Encoding objects of %r is not supported" % type(x))
assert(len(encoded_bytes) == byte_len)
return encoded_bytes
if __name__ == '__main__':
# TODO These tests should be moved out of main eventually
mac = "aa:bb:cc:dd:ee:ff"
enc_mac = encodeMac(mac)
assert(enc_mac == '\xaa\xbb\xcc\xdd\xee\xff')
dec_mac = decodeMac(enc_mac)
assert(mac == dec_mac)
ip = "10.0.0.1"
enc_ip = encodeIPv4(ip)
assert(enc_ip == '\x0a\x00\x00\x01')
dec_ip = decodeIPv4(enc_ip)
assert(ip == dec_ip)
num = 1337
byte_len = 5
enc_num = encodeNum(num, byte_len * 8)
assert(enc_num == '\x00\x00\x00\x05\x39')
dec_num = decodeNum(enc_num)
assert(num == dec_num)
assert(matchesIPv4('10.0.0.1'))
assert(not matchesIPv4('10.0.0.1.5'))
assert(not matchesIPv4('1000.0.0.1'))
assert(not matchesIPv4('10001'))
assert(encode(mac, 6 * 8) == enc_mac)
assert(encode(ip, 4 * 8) == enc_ip)
assert(encode(num, 5 * 8) == enc_num)
assert(encode((num,), 5 * 8) == enc_num)
assert(encode([num], 5 * 8) == enc_num)
num = 256
byte_len = 2
try:
enc_num = encodeNum(num, 8)
raise Exception("expected exception")
except Exception as e:
print e

View File

@@ -0,0 +1,193 @@
# 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 re
import google.protobuf.text_format
from p4 import p4runtime_pb2
from p4.config import p4info_pb2
from convert import encode
class P4InfoHelper(object):
def __init__(self, p4_info_filepath):
p4info = p4info_pb2.P4Info()
# Load the p4info file into a skeleton P4Info object
with open(p4_info_filepath) as p4info_f:
google.protobuf.text_format.Merge(p4info_f.read(), p4info)
self.p4info = p4info
def get(self, entity_type, name=None, id=None):
if name is not None and id is not None:
raise AssertionError("name or id must be None")
for o in getattr(self.p4info, entity_type):
pre = o.preamble
if name:
if (pre.name == name or pre.alias == name):
return o
else:
if pre.id == id:
return o
if name:
raise AttributeError("Could not find %r of type %s" % (name, entity_type))
else:
raise AttributeError("Could not find id %r of type %s" % (id, entity_type))
def get_id(self, entity_type, name):
return self.get(entity_type, name=name).preamble.id
def get_name(self, entity_type, id):
return self.get(entity_type, id=id).preamble.name
def get_alias(self, entity_type, id):
return self.get(entity_type, id=id).preamble.alias
def __getattr__(self, attr):
# Synthesize convenience functions for name to id lookups for top-level entities
# e.g. get_tables_id(name_string) or get_actions_id(name_string)
m = re.search("^get_(\w+)_id$", attr)
if m:
primitive = m.group(1)
return lambda name: self.get_id(primitive, name)
# Synthesize convenience functions for id to name lookups
# e.g. get_tables_name(id) or get_actions_name(id)
m = re.search("^get_(\w+)_name$", attr)
if m:
primitive = m.group(1)
return lambda id: self.get_name(primitive, id)
raise AttributeError("%r object has no attribute %r" % (self.__class__, attr))
def get_match_field(self, table_name, name=None, id=None):
for t in self.p4info.tables:
pre = t.preamble
if pre.name == table_name:
for mf in t.match_fields:
if name is not None:
if mf.name == name:
return mf
elif id is not None:
if mf.id == id:
return mf
raise AttributeError("%r has no attribute %r" % (table_name, name if name is not None else id))
def get_match_field_id(self, table_name, match_field_name):
return self.get_match_field(table_name, name=match_field_name).id
def get_match_field_name(self, table_name, match_field_id):
return self.get_match_field(table_name, id=match_field_id).name
def get_match_field_pb(self, table_name, match_field_name, value):
p4info_match = self.get_match_field(table_name, match_field_name)
bitwidth = p4info_match.bitwidth
p4runtime_match = p4runtime_pb2.FieldMatch()
p4runtime_match.field_id = p4info_match.id
match_type = p4info_match.match_type
if match_type == p4info_pb2.MatchField.VALID:
valid = p4runtime_match.valid
valid.value = bool(value)
elif match_type == p4info_pb2.MatchField.EXACT:
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]
elif match_type == p4info_pb2.MatchField.TERNARY:
lpm = p4runtime_match.ternary
lpm.value = encode(value[0], bitwidth)
lpm.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)
else:
raise Exception("Unsupported match type with type %r" % match_type)
return p4runtime_match
def get_match_field_value(self, match_field):
match_type = match_field.WhichOneof("field_match_type")
if match_type == 'valid':
return match_field.valid.value
elif match_type == 'exact':
return match_field.exact.value
elif match_type == 'lpm':
return (match_field.lpm.value, match_field.lpm.prefix_len)
elif match_type == 'ternary':
return (match_field.ternary.value, match_field.ternary.mask)
elif match_type == 'range':
return (match_field.range.low, match_field.range.high)
else:
raise Exception("Unsupported match type with type %r" % match_type)
def get_action_param(self, action_name, name=None, id=None):
for a in self.p4info.actions:
pre = a.preamble
if pre.name == action_name:
for p in a.params:
if name is not None:
if p.name == name:
return p
elif id is not None:
if p.id == id:
return p
raise AttributeError("action %r has no param %r, (has: %r)" % (action_name, name if name is not None else id, a.params))
def get_action_param_id(self, action_name, param_name):
return self.get_action_param(action_name, name=param_name).id
def get_action_param_name(self, action_name, param_id):
return self.get_action_param(action_name, id=param_id).name
def get_action_param_pb(self, action_name, param_name, value):
p4info_param = self.get_action_param(action_name, param_name)
p4runtime_param = p4runtime_pb2.Action.Param()
p4runtime_param.param_id = p4info_param.id
p4runtime_param.value = encode(value, p4info_param.bitwidth)
return p4runtime_param
def buildTableEntry(self,
table_name,
match_fields=None,
default_action=False,
action_name=None,
action_params=None,
priority=None):
table_entry = p4runtime_pb2.TableEntry()
table_entry.table_id = self.get_tables_id(table_name)
if priority is not None:
table_entry.priority = priority
if match_fields:
table_entry.match.extend([
self.get_match_field_pb(table_name, match_field_name, value)
for match_field_name, value in match_fields.iteritems()
])
if default_action:
table_entry.is_default_action = True
if action_name:
action = table_entry.action.action
action.action_id = self.get_actions_id(action_name)
if action_params:
action.params.extend([
self.get_action_param_pb(action_name, field_name, value)
for field_name, value in action_params.iteritems()
])
return table_entry

View File

@@ -0,0 +1,195 @@
#!/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()

View File

@@ -0,0 +1,166 @@
# 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.
#
from Queue import Queue
from abc import abstractmethod
from datetime import datetime
import grpc
from p4 import p4runtime_pb2
from p4.tmp import p4config_pb2
MSG_LOG_MAX_LEN = 1024
# List of all active connections
connections = []
def ShutdownAllSwitchConnections():
for c in connections:
c.shutdown()
class SwitchConnection(object):
def __init__(self, name=None, address='127.0.0.1:50051', device_id=0,
proto_dump_file=None):
self.name = name
self.address = address
self.device_id = device_id
self.p4info = None
self.channel = grpc.insecure_channel(self.address)
if proto_dump_file is not None:
interceptor = GrpcRequestLogger(proto_dump_file)
self.channel = grpc.intercept_channel(self.channel, interceptor)
self.client_stub = p4runtime_pb2.P4RuntimeStub(self.channel)
self.requests_stream = IterableQueue()
self.stream_msg_resp = self.client_stub.StreamChannel(iter(self.requests_stream))
self.proto_dump_file = proto_dump_file
connections.append(self)
@abstractmethod
def buildDeviceConfig(self, **kwargs):
return p4config_pb2.P4DeviceConfig()
def shutdown(self):
self.requests_stream.close()
self.stream_msg_resp.cancel()
def MasterArbitrationUpdate(self, dry_run=False, **kwargs):
request = p4runtime_pb2.StreamMessageRequest()
request.arbitration.device_id = self.device_id
request.arbitration.election_id.high = 0
request.arbitration.election_id.low = 1
if dry_run:
print "P4 Runtime MasterArbitrationUpdate: ", request
else:
self.requests_stream.put(request)
def SetForwardingPipelineConfig(self, p4info, dry_run=False, **kwargs):
device_config = self.buildDeviceConfig(**kwargs)
request = p4runtime_pb2.SetForwardingPipelineConfigRequest()
request.election_id.low = 1
request.device_id = self.device_id
config = request.config
config.p4info.CopyFrom(p4info)
config.p4_device_config = device_config.SerializeToString()
request.action = p4runtime_pb2.SetForwardingPipelineConfigRequest.VERIFY_AND_COMMIT
if dry_run:
print "P4 Runtime SetForwardingPipelineConfig:", request
else:
self.client_stub.SetForwardingPipelineConfig(request)
def WriteTableEntry(self, table_entry, dry_run=False):
request = p4runtime_pb2.WriteRequest()
request.device_id = self.device_id
request.election_id.low = 1
update = request.updates.add()
update.type = p4runtime_pb2.Update.INSERT
update.entity.table_entry.CopyFrom(table_entry)
if dry_run:
print "P4 Runtime Write:", request
else:
self.client_stub.Write(request)
def ReadTableEntries(self, table_id=None, dry_run=False):
request = p4runtime_pb2.ReadRequest()
request.device_id = self.device_id
entity = request.entities.add()
table_entry = entity.table_entry
if table_id is not None:
table_entry.table_id = table_id
else:
table_entry.table_id = 0
if dry_run:
print "P4 Runtime Read:", request
else:
for response in self.client_stub.Read(request):
yield response
def ReadCounters(self, counter_id=None, index=None, dry_run=False):
request = p4runtime_pb2.ReadRequest()
request.device_id = self.device_id
entity = request.entities.add()
counter_entry = entity.counter_entry
if counter_id is not None:
counter_entry.counter_id = counter_id
else:
counter_entry.counter_id = 0
if index is not None:
counter_entry.index.index = index
if dry_run:
print "P4 Runtime Read:", request
else:
for response in self.client_stub.Read(request):
yield response
class GrpcRequestLogger(grpc.UnaryUnaryClientInterceptor,
grpc.UnaryStreamClientInterceptor):
"""Implementation of a gRPC interceptor that logs request to a file"""
def __init__(self, log_file):
self.log_file = log_file
with open(self.log_file, 'w') as f:
# Clear content if it exists.
f.write("")
def log_message(self, method_name, body):
with open(self.log_file, 'a') as f:
ts = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
msg = str(body)
f.write("\n[%s] %s\n---\n" % (ts, method_name))
if len(msg) < MSG_LOG_MAX_LEN:
f.write(str(body))
else:
f.write("Message too long (%d bytes)! Skipping log...\n" % len(msg))
f.write('---\n')
def intercept_unary_unary(self, continuation, client_call_details, request):
self.log_message(client_call_details.method, request)
return continuation(client_call_details, request)
def intercept_unary_stream(self, continuation, client_call_details, request):
self.log_message(client_call_details.method, request)
return continuation(client_call_details, request)
class IterableQueue(Queue):
_sentinel = object()
def __iter__(self):
return iter(self.get, self._sentinel)
def close(self):
self.put(self._sentinel)

132
utils/p4runtime_switch.py Normal file
View File

@@ -0,0 +1,132 @@
# Copyright 2017-present Barefoot Networks, Inc.
# 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 sys, os, tempfile, socket
from time import sleep
from mininet.node import Switch
from mininet.moduledeps import pathCheck
from mininet.log import info, error, debug
from p4_mininet import P4Switch, SWITCH_START_TIMEOUT
from netstat import check_listening_on_port
class P4RuntimeSwitch(P4Switch):
"BMv2 switch with gRPC support"
next_grpc_port = 50051
next_thrift_port = 9090
def __init__(self, name, sw_path = None, json_path = None,
grpc_port = 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)
self.sw_path = sw_path
# make sure that the provided sw_path is valid
pathCheck(sw_path)
if json_path is not None:
# make sure that the provided JSON file exists
if not os.path.isfile(json_path):
error("Invalid JSON file.\n")
exit(1)
self.json_path = json_path
else:
self.json_path = None
if grpc_port is not None:
self.grpc_port = grpc_port
else:
self.grpc_port = P4RuntimeSwitch.next_grpc_port
P4RuntimeSwitch.next_grpc_port += 1
if thrift_port is not None:
self.thrift_port = thrift_port
else:
self.thrift_port = P4RuntimeSwitch.next_thrift_port
P4RuntimeSwitch.next_thrift_port += 1
if check_listening_on_port(self.grpc_port):
error('%s cannot bind port %d because it is bound by another process\n' % (self.name, self.grpc_port))
exit(1)
self.verbose = verbose
logfile = "/tmp/p4s.{}.log".format(self.name)
self.output = open(logfile, 'w')
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)
def check_switch_started(self, pid):
for _ in range(SWITCH_START_TIMEOUT * 2):
if not os.path.exists(os.path.join("/proc", str(pid))):
return False
if check_listening_on_port(self.grpc_port):
return True
sleep(0.5)
def start(self, controllers):
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")
if self.nanomsg:
args.extend(['--nanolog', self.nanomsg])
args.extend(['--device-id', str(self.device_id)])
P4Switch.device_id += 1
if self.json_path:
args.append(self.json_path)
else:
args.append("--no-p4")
if self.enable_debugger:
args.append("--debugger")
if self.log_console:
args.append("--log-console")
if self.thrift_port:
args.append('--thrift-port ' + str(self.thrift_port))
if self.grpc_port:
args.append("-- --grpc-server-addr 0.0.0.0:" + str(self.grpc_port))
cmd = ' '.join(args)
info(cmd + "\n")
logfile = "/tmp/p4s.{}.log".format(self.name)
pid = None
with tempfile.NamedTemporaryFile() as f:
self.cmd(cmd + ' >' + logfile + ' 2>&1 & echo $! >> ' + f.name)
pid = int(f.read())
debug("P4 switch {} PID is {}.\n".format(self.name, pid))
if not self.check_switch_started(pid):
error("P4 switch {} did not start correctly.\n".format(self.name))
exit(1)
info("P4 switch {} has been started.\n".format(self.name))

404
utils/run_exercise.py Executable file
View File

@@ -0,0 +1,404 @@
#!/usr/bin/env python2
# 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.
#
# Adapted by Robert MacDavid (macdavid@cs.princeton.edu) from scripts found in
# the p4app repository (https://github.com/p4lang/p4app)
#
# We encourage you to dissect this script to better understand the BMv2/Mininet
# environment used by the P4 tutorial.
#
import os, sys, json, subprocess, re, argparse
from time import sleep
from p4_mininet import P4Switch, P4Host
from mininet.net import Mininet
from mininet.topo import Topo
from mininet.link import TCLink
from mininet.cli import CLI
from p4runtime_switch import P4RuntimeSwitch
import p4runtime_lib.simple_controller
def configureP4Switch(**switch_args):
""" Helper class that is called by mininet to initialize
the virtual P4 switches. The purpose is to ensure each
switch's thrift server is using a unique port.
"""
if "sw_path" in switch_args and 'grpc' in switch_args['sw_path']:
# If grpc appears in the BMv2 switch target, we assume will start P4 Runtime
class ConfiguredP4RuntimeSwitch(P4RuntimeSwitch):
def __init__(self, *opts, **kwargs):
kwargs.update(switch_args)
P4RuntimeSwitch.__init__(self, *opts, **kwargs)
def describe(self):
print "%s -> gRPC port: %d" % (self.name, self.grpc_port)
return ConfiguredP4RuntimeSwitch
else:
class ConfiguredP4Switch(P4Switch):
next_thrift_port = 9090
def __init__(self, *opts, **kwargs):
global next_thrift_port
kwargs.update(switch_args)
kwargs['thrift_port'] = ConfiguredP4Switch.next_thrift_port
ConfiguredP4Switch.next_thrift_port += 1
P4Switch.__init__(self, *opts, **kwargs)
def describe(self):
print "%s -> Thrift port: %d" % (self.name, self.thrift_port)
return ConfiguredP4Switch
class ExerciseTopo(Topo):
""" The mininet topology class for the P4 tutorial exercises.
A custom class is used because the exercises make a few topology
assumptions, mostly about the IP and MAC addresses.
"""
def __init__(self, hosts, switches, links, log_dir, **opts):
Topo.__init__(self, **opts)
host_links = []
switch_links = []
self.sw_port_mapping = {}
for link in links:
if link['node1'][0] == 'h':
host_links.append(link)
else:
switch_links.append(link)
link_sort_key = lambda x: x['node1'] + x['node2']
# Links must be added in a sorted order so bmv2 port numbers are predictable
host_links.sort(key=link_sort_key)
switch_links.sort(key=link_sort_key)
for sw in switches:
self.addSwitch(sw, log_file="%s/%s.log" %(log_dir, sw))
for link in host_links:
host_name = link['node1']
host_sw = link['node2']
host_num = int(host_name[1:])
sw_num = int(host_sw[1:])
host_ip = "10.0.%d.%d" % (sw_num, host_num)
host_mac = '00:00:00:00:%02x:%02x' % (sw_num, host_num)
# Each host IP should be /24, so all exercise traffic will use the
# default gateway (the switch) without sending ARP requests.
self.addHost(host_name, ip=host_ip+'/24', mac=host_mac)
self.addLink(host_name, host_sw,
delay=link['latency'], bw=link['bandwidth'],
addr1=host_mac, addr2=host_mac)
self.addSwitchPort(host_sw, host_name)
for link in switch_links:
self.addLink(link['node1'], link['node2'],
delay=link['latency'], bw=link['bandwidth'])
self.addSwitchPort(link['node1'], link['node2'])
self.addSwitchPort(link['node2'], link['node1'])
self.printPortMapping()
def addSwitchPort(self, sw, node2):
if sw not in self.sw_port_mapping:
self.sw_port_mapping[sw] = []
portno = len(self.sw_port_mapping[sw])+1
self.sw_port_mapping[sw].append((portno, node2))
def printPortMapping(self):
print "Switch port mapping:"
for sw in sorted(self.sw_port_mapping.keys()):
print "%s: " % sw,
for portno, node2 in self.sw_port_mapping[sw]:
print "%d:%s\t" % (portno, node2),
print
class ExerciseRunner:
"""
Attributes:
log_dir : string // directory for mininet log files
pcap_dir : string // directory for mininet switch pcap files
quiet : bool // determines if we print logger messages
hosts : list<string> // list of mininet host names
switches : dict<string, dict> // mininet host names and their associated properties
links : list<dict> // list of mininet link properties
switch_json : string // json of the compiled p4 example
bmv2_exe : string // name or path of the p4 switch binary
topo : Topo object // The mininet topology instance
net : Mininet object // The mininet instance
"""
def logger(self, *items):
if not self.quiet:
print(' '.join(items))
def formatLatency(self, l):
""" Helper method for parsing link latencies from the topology json. """
if isinstance(l, (str, unicode)):
return l
else:
return str(l) + "ms"
def __init__(self, topo_file, log_dir, pcap_dir,
switch_json, bmv2_exe='simple_switch', quiet=False):
""" Initializes some attributes and reads the topology json. Does not
actually run the exercise. Use run_exercise() for that.
Arguments:
topo_file : string // A json file which describes the exercise's
mininet topology.
log_dir : string // Path to a directory for storing exercise logs
pcap_dir : string // Ditto, but for mininet switch pcap files
switch_json : string // Path to a compiled p4 json for bmv2
bmv2_exe : string // Path to the p4 behavioral binary
quiet : bool // Enable/disable script debug messages
"""
self.quiet = quiet
self.logger('Reading topology file.')
with open(topo_file, 'r') as f:
topo = json.load(f)
self.hosts = topo['hosts']
self.switches = topo['switches']
self.links = self.parse_links(topo['links'])
# Ensure all the needed directories exist and are directories
for dir_name in [log_dir, pcap_dir]:
if not os.path.isdir(dir_name):
if os.path.exists(dir_name):
raise Exception("'%s' exists and is not a directory!" % dir_name)
os.mkdir(dir_name)
self.log_dir = log_dir
self.pcap_dir = pcap_dir
self.switch_json = switch_json
self.bmv2_exe = bmv2_exe
def run_exercise(self):
""" Sets up the mininet instance, programs the switches,
and starts the mininet CLI. This is the main method to run after
initializing the object.
"""
# Initialize mininet with the topology specified by the config
self.create_network()
self.net.start()
sleep(1)
# some programming that must happen after the net has started
self.program_hosts()
self.program_switches()
# wait for that to finish. Not sure how to do this better
sleep(1)
self.do_net_cli()
# stop right after the CLI is exited
self.net.stop()
def parse_links(self, unparsed_links):
""" Given a list of links descriptions of the form [node1, node2, latency, bandwidth]
with the latency and bandwidth being optional, parses these descriptions
into dictionaries and store them as self.links
"""
links = []
for link in unparsed_links:
# make sure each link's endpoints are ordered alphabetically
s, t, = link[0], link[1]
if s > t:
s,t = t,s
link_dict = {'node1':s,
'node2':t,
'latency':'0ms',
'bandwidth':None
}
if len(link) > 2:
link_dict['latency'] = self.formatLatency(link[2])
if len(link) > 3:
link_dict['bandwidth'] = link[3]
if link_dict['node1'][0] == 'h':
assert link_dict['node2'][0] == 's', 'Hosts should be connected to switches, not ' + str(link_dict['node2'])
links.append(link_dict)
return links
def create_network(self):
""" Create the mininet network object, and store it as self.net.
Side effects:
- Mininet topology instance stored as self.topo
- Mininet instance stored as self.net
"""
self.logger("Building mininet topology.")
self.topo = ExerciseTopo(self.hosts, self.switches.keys(), self.links, self.log_dir)
switchClass = configureP4Switch(
sw_path=self.bmv2_exe,
json_path=self.switch_json,
log_console=True,
pcap_dump=self.pcap_dir)
self.net = Mininet(topo = self.topo,
link = TCLink,
host = P4Host,
switch = switchClass,
controller = None)
def program_switch_p4runtime(self, sw_name, sw_dict):
""" This method will use P4Runtime to program the switch using the
content of the runtime JSON file as input.
"""
sw_obj = self.net.get(sw_name)
grpc_port = sw_obj.grpc_port
device_id = sw_obj.device_id
runtime_json = sw_dict['runtime_json']
self.logger('Configuring switch %s using P4Runtime with file %s' % (sw_name, runtime_json))
with open(runtime_json, 'r') as sw_conf_file:
outfile = '%s/%s-p4runtime-requests.txt' %(self.log_dir, sw_name)
p4runtime_lib.simple_controller.program_switch(
addr='127.0.0.1:%d' % grpc_port,
device_id=device_id,
sw_conf_file=sw_conf_file,
workdir=os.getcwd(),
proto_dump_fpath=outfile)
def program_switch_cli(self, sw_name, sw_dict):
""" This method will start up the CLI and use the contents of the
command files as input.
"""
cli = 'simple_switch_CLI'
# get the port for this particular switch's thrift server
sw_obj = self.net.get(sw_name)
thrift_port = sw_obj.thrift_port
cli_input_commands = sw_dict['cli_input']
self.logger('Configuring switch %s with file %s' % (sw_name, cli_input_commands))
with open(cli_input_commands, 'r') as fin:
cli_outfile = '%s/%s_cli_output.log'%(self.log_dir, sw_name)
with open(cli_outfile, 'w') as fout:
subprocess.Popen([cli, '--thrift-port', str(thrift_port)],
stdin=fin, stdout=fout)
def program_switches(self):
""" This method will program each switch using the BMv2 CLI and/or
P4Runtime, depending if any command or runtime JSON files were
provided for the switches.
"""
for sw_name, sw_dict in self.switches.iteritems():
if 'cli_input' in sw_dict:
self.program_switch_cli(sw_name, sw_dict)
if 'runtime_json' in sw_dict:
self.program_switch_p4runtime(sw_name, sw_dict)
def program_hosts(self):
""" Adds static ARP entries and default routes to each mininet host.
Assumes:
- A mininet instance is stored as self.net and self.net.start() has
been called.
"""
for host_name in self.topo.hosts():
h = self.net.get(host_name)
h_iface = h.intfs.values()[0]
link = h_iface.link
sw_iface = link.intf1 if link.intf1 != h_iface else link.intf2
# phony IP to lie to the host about
host_id = int(host_name[1:])
sw_ip = '10.0.%d.254' % host_id
# Ensure each host's interface name is unique, or else
# mininet cannot shutdown gracefully
h.defaultIntf().rename('%s-eth0' % host_name)
# static arp entries and default routes
h.cmd('arp -i %s -s %s %s' % (h_iface.name, sw_ip, sw_iface.mac))
h.cmd('ethtool --offload %s rx off tx off' % h_iface.name)
h.cmd('ip route add %s dev %s' % (sw_ip, h_iface.name))
h.setDefaultRoute("via %s" % sw_ip)
def do_net_cli(self):
""" Starts up the mininet CLI and prints some helpful output.
Assumes:
- A mininet instance is stored as self.net and self.net.start() has
been called.
"""
for s in self.net.switches:
s.describe()
for h in self.net.hosts:
h.describe()
self.logger("Starting mininet CLI")
# Generate a message that will be printed by the Mininet CLI to make
# interacting with the simple switch a little easier.
print('')
print('======================================================================')
print('Welcome to the BMV2 Mininet CLI!')
print('======================================================================')
print('Your P4 program is installed into the BMV2 software switch')
print('and your initial configuration is loaded. You can interact')
print('with the network using the mininet CLI below.')
print('')
if self.switch_json:
print('To inspect or change the switch configuration, connect to')
print('its CLI from your host operating system using this command:')
print(' simple_switch_CLI --thrift-port <switch thrift port>')
print('')
print('To view a switch log, run this command from your host OS:')
print(' tail -f %s/<switchname>.log' % self.log_dir)
print('')
print('To view the switch output pcap, check the pcap files in %s:' % self.pcap_dir)
print(' for example run: sudo tcpdump -xxx -r s1-eth1.pcap')
print('')
CLI(self.net)
def get_args():
cwd = os.getcwd()
default_logs = os.path.join(cwd, 'logs')
default_pcaps = os.path.join(cwd, 'pcaps')
parser = argparse.ArgumentParser()
parser.add_argument('-q', '--quiet', help='Suppress log messages.',
action='store_true', required=False, default=False)
parser.add_argument('-t', '--topo', help='Path to topology json',
type=str, required=False, default='./topology.json')
parser.add_argument('-l', '--log-dir', type=str, required=False, default=default_logs)
parser.add_argument('-p', '--pcap-dir', type=str, required=False, default=default_pcaps)
parser.add_argument('-j', '--switch_json', type=str, required=False)
parser.add_argument('-b', '--behavioral-exe', help='Path to behavioral executable',
type=str, required=False, default='simple_switch')
return parser.parse_args()
if __name__ == '__main__':
# from mininet.log import setLogLevel
# setLogLevel("info")
args = get_args()
exercise = ExerciseRunner(args.topo, args.log_dir, args.pcap_dir,
args.switch_json, args.behavioral_exe, args.quiet)
exercise.run_exercise()