diff --git a/CONTRIBUTORS b/CONTRIBUTORS index db1325b95..9aeb5f4b0 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -17,6 +17,7 @@ Additional Mininet-WiFi Contributors Eduardo Soares - https://github.com/eSoares Patrick Große - https://github.com/patgrosse Joaquin Alvarez - j.alvarez@uah.es +David Carrascal - https://github.com/davidcawork Thanks also to everyone who has submitted issues and pull requests on github, and to our friendly mininet-wifi-discuss diff --git a/examples/p4/basic-runtime/Makefile b/examples/p4/basic-runtime/Makefile new file mode 100644 index 000000000..a6b3a88f7 --- /dev/null +++ b/examples/p4/basic-runtime/Makefile @@ -0,0 +1,3 @@ +BMV2_SWITCH_EXE = simple_switch_grpc + +include ../util/Makefile diff --git a/examples/p4/basic-runtime/README.md b/examples/p4/basic-runtime/README.md new file mode 100644 index 000000000..2c2b83adf --- /dev/null +++ b/examples/p4/basic-runtime/README.md @@ -0,0 +1,27 @@ +# Basic Runtime Example + +Example where you can see the use of the [`P4RuntimeAP`](https://github.com/intrig-unicamp/mininet-wifi/blob/p4/mn_wifi/bmv2.py) class. This class manages the launch of the BMV2 and configures it via P4Runtime. + +As already mentioned in the previous README, it is necessary to have the dependencies associated with the p4 environment installed to carry out the execution of this example. + + +To run the example, it is first necessary to compile the P4 program: + +```bash + +sudo make + +``` + +Which will create a directory structure where useful information for the execution of the example will be stored, and it will compile our P4 program (storing it under the `build` directory). + +Once the P4 program is compiled, you can run the `test.py` script which will build the topology in Mininet-Wifi. + +```bash + +sudo python test.py + +``` + + + diff --git a/examples/p4/basic-runtime/basic.p4 b/examples/p4/basic-runtime/basic.p4 new file mode 100644 index 000000000..6f2ba9506 --- /dev/null +++ b/examples/p4/basic-runtime/basic.p4 @@ -0,0 +1,184 @@ +/* -*- P4_16 -*- */ +#include +#include + +const bit<16> TYPE_IPV4 = 0x800; + +/************************************************************************* +*********************** H E A D E R S *********************************** +*************************************************************************/ + +typedef bit<9> egressSpec_t; +typedef bit<48> macAddr_t; +typedef bit<32> ip4Addr_t; + +header ethernet_t { + macAddr_t dstAddr; + macAddr_t srcAddr; + bit<16> etherType; +} + +header ipv4_t { + bit<4> version; + bit<4> ihl; + bit<8> diffserv; + bit<16> totalLen; + bit<16> identification; + bit<3> flags; + bit<13> fragOffset; + bit<8> ttl; + bit<8> protocol; + bit<16> hdrChecksum; + ip4Addr_t srcAddr; + ip4Addr_t dstAddr; +} + +struct metadata { + /* empty */ +} + +struct headers { + ethernet_t ethernet; + ipv4_t ipv4; +} + +/************************************************************************* +*********************** P A R S E R *********************************** +*************************************************************************/ + +parser MyParser(packet_in packet, + out headers hdr, + inout metadata meta, + inout standard_metadata_t standard_metadata) { + + state start { + /* TODO: add parser logic */ + + transition parser_ethernet; + } + + state parser_ethernet { + packet.extract(hdr.ethernet); + transition select (hdr.ethernet.etherType) { + TYPE_IPV4: parser_ipv4; + default: accept; + } + } + + state parser_ipv4{ + packet.extract(hdr.ipv4); + transition accept; + } +} + + +/************************************************************************* +************ C H E C K S U M V E R I F I C A T I O N ************* +*************************************************************************/ + +control MyVerifyChecksum(inout headers hdr, inout metadata meta) { + apply { } +} + + +/************************************************************************* +************** I N G R E S S P R O C E S S I N G ******************* +*************************************************************************/ + +control MyIngress(inout headers hdr, + inout metadata meta, + inout standard_metadata_t standard_metadata) { + action drop() { + mark_to_drop(standard_metadata); + } + + action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) { + /* TODO: fill out code in action body */ + standard_metadata.egress_spec = port; + hdr.ethernet.srcAddr = hdr.ethernet.dstAddr; + hdr.ethernet.dstAddr = dstAddr; + hdr.ipv4.ttl = hdr.ipv4.ttl - 1; + + } + + table ipv4_lpm { + key = { + hdr.ipv4.dstAddr: lpm; + } + actions = { + ipv4_forward; + drop; + NoAction; + } + size = 1024; + default_action = NoAction(); + } + + apply { + /* TODO: fix ingress control logic + * - ipv4_lpm should be applied only when IPv4 header is valid + */ + if(hdr.ipv4.isValid()) + ipv4_lpm.apply(); + } +} + +/************************************************************************* +**************** E G R E S S P R O C E S S I N G ******************* +*************************************************************************/ + +control MyEgress(inout headers hdr, + inout metadata meta, + inout standard_metadata_t standard_metadata) { + apply { } +} + +/************************************************************************* +************* C H E C K S U M C O M P U T A T I O N ************** +*************************************************************************/ + +control MyComputeChecksum(inout headers hdr, inout metadata meta) { + apply { + update_checksum( + hdr.ipv4.isValid(), + { hdr.ipv4.version, + hdr.ipv4.ihl, + hdr.ipv4.diffserv, + hdr.ipv4.totalLen, + hdr.ipv4.identification, + hdr.ipv4.flags, + hdr.ipv4.fragOffset, + hdr.ipv4.ttl, + hdr.ipv4.protocol, + hdr.ipv4.srcAddr, + hdr.ipv4.dstAddr }, + hdr.ipv4.hdrChecksum, + HashAlgorithm.csum16); + } +} + + +/************************************************************************* +*********************** D E P A R S E R ******************************* +*************************************************************************/ + +control MyDeparser(packet_out packet, in headers hdr) { + apply { + /* TODO: add deparser logic */ + packet.emit(hdr.ethernet); + packet.emit(hdr.ipv4); + } +} + +/************************************************************************* +*********************** S W I T C H ******************************* +*************************************************************************/ + +V1Switch( +MyParser(), +MyVerifyChecksum(), +MyIngress(), +MyEgress(), +MyComputeChecksum(), +MyDeparser() +) main; diff --git a/examples/p4/basic-runtime/runtime/ap1-runtime.json b/examples/p4/basic-runtime/runtime/ap1-runtime.json new file mode 100644 index 000000000..78b435849 --- /dev/null +++ b/examples/p4/basic-runtime/runtime/ap1-runtime.json @@ -0,0 +1,35 @@ +{ + "target": "bmv2", + "p4info": "build/basic.p4.p4info.txt", + "bmv2_json": "build/basic.json", + "table_entries": [ + { + "table": "MyIngress.ipv4_lpm", + "default_action": true, + "action_name": "MyIngress.drop", + "action_params": { } + }, + { + "table": "MyIngress.ipv4_lpm", + "match": { + "hdr.ipv4.dstAddr": ["10.0.0.3", 32] + }, + "action_name": "MyIngress.ipv4_forward", + "action_params": { + "dstAddr": "00:00:00:00:00:03", + "port": 2 + } + }, + { + "table": "MyIngress.ipv4_lpm", + "match": { + "hdr.ipv4.dstAddr": ["10.0.0.2", 32] + }, + "action_name": "MyIngress.ipv4_forward", + "action_params": { + "dstAddr": "00:00:00:00:00:02", + "port": 1 + } + } + ] +} diff --git a/examples/p4/basic-runtime/test.py b/examples/p4/basic-runtime/test.py new file mode 100644 index 000000000..f64d90029 --- /dev/null +++ b/examples/p4/basic-runtime/test.py @@ -0,0 +1,43 @@ +#!/usr/bin/python + +import os + +from mininet.log import setLogLevel, info +from mn_wifi.cli import CLI +from mn_wifi.net import Mininet_wifi +from mn_wifi.bmv2 import P4RuntimeAP + +def topology(): + 'Create a network.' + net = Mininet_wifi() + + info('*** Adding stations/hosts\n') + sta1 = net.addStation('sta1', ip='10.0.0.2', mac='00:00:00:00:00:02') + h1 = net.addHost('h1', ip='10.0.0.3', mac='00:00:00:00:00:03') + + info('*** Adding P4RuntimeAP\n') + ap1 = net.addAccessPoint('ap1', cls=P4RuntimeAP, json_path='build/basic.json', runtime_json_path='runtime/ap1-runtime.json', + log_console = True, log_dir = os.path.abspath('logs'), log_file = 'ap1.log', pcap_dump = os.path.abspath('pcaps')) + + net.configureWifiNodes() + + + info('*** Creating links\n') + net.addLink(sta1, ap1) + net.addLink(h1, ap1) + + + info('*** Starting network\n') + net.start() + net.staticArp() + + info('*** Running CLI\n') + CLI(net) + + info('*** Stopping network\n') + net.stop() + + +if __name__ == '__main__': + setLogLevel('info') + topology() diff --git a/examples/p4/__init__.py b/examples/p4/basic-thrift/__init__.py similarity index 100% rename from examples/p4/__init__.py rename to examples/p4/basic-thrift/__init__.py diff --git a/examples/p4/ap-runtime.json b/examples/p4/basic-thrift/ap-runtime.json similarity index 100% rename from examples/p4/ap-runtime.json rename to examples/p4/basic-thrift/ap-runtime.json diff --git a/examples/p4/basic.p4 b/examples/p4/basic-thrift/basic.p4 similarity index 100% rename from examples/p4/basic.p4 rename to examples/p4/basic-thrift/basic.p4 diff --git a/examples/p4/commands_ap1.txt b/examples/p4/basic-thrift/commands_ap1.txt similarity index 94% rename from examples/p4/commands_ap1.txt rename to examples/p4/basic-thrift/commands_ap1.txt index 56b0c379f..faef7bdcf 100644 --- a/examples/p4/commands_ap1.txt +++ b/examples/p4/basic-thrift/commands_ap1.txt @@ -1,3 +1,3 @@ table_set_default ipv4_lpm drop table_add MyIngress.ipv4_lpm ipv4_forward 10.0.0.1 => 00:00:00:00:00:01 2 -table_add MyIngress.ipv4_lpm ipv4_forward 10.0.0.3 => 00:00:00:00:00:03 1 \ No newline at end of file +table_add MyIngress.ipv4_lpm ipv4_forward 10.0.0.3 => 00:00:00:00:00:03 1 diff --git a/examples/p4/commands_ap2.txt b/examples/p4/basic-thrift/commands_ap2.txt similarity index 100% rename from examples/p4/commands_ap2.txt rename to examples/p4/basic-thrift/commands_ap2.txt diff --git a/examples/p4/p4.py b/examples/p4/basic-thrift/p4.py similarity index 100% rename from examples/p4/p4.py rename to examples/p4/basic-thrift/p4.py diff --git a/examples/p4/bmv2-netns/Makefile b/examples/p4/bmv2-netns/Makefile new file mode 100644 index 000000000..a6b3a88f7 --- /dev/null +++ b/examples/p4/bmv2-netns/Makefile @@ -0,0 +1,3 @@ +BMV2_SWITCH_EXE = simple_switch_grpc + +include ../util/Makefile diff --git a/examples/p4/bmv2-netns/README.md b/examples/p4/bmv2-netns/README.md new file mode 100644 index 000000000..aa9f724a2 --- /dev/null +++ b/examples/p4/bmv2-netns/README.md @@ -0,0 +1,29 @@ +# BMv2 in Network Namespaces Example + +Example where you can see the use of the [`P4RuntimeAP`](https://github.com/intrig-unicamp/mininet-wifi/blob/p4/mn_wifi/bmv2.py) class. This class manages the launch of the BMV2 and configures it via P4Runtime. Additionally, in this example you can see how it is possible to run the BMV2 in different Network Namespaces, this has been achieved by using the class [`Netns_mgmt`](https://github.com/intrig-unicamp/mininet-wifi/blob/p4/mn_wifi/node.py). This class allows us to execute python code at runtime on different Netns, thus managing to carry out the configuration via P4Runtime on each Netns. As a future work, we propose to carry out the configuration via P4Runtime from the root-Netns, assigning a control IP to each softswitch and establishing the control plane logic so that all softswitches are reachable by a hypothetical external controller. + + + +As already mentioned in the previous README, it is necessary to have the dependencies associated with the p4 environment installed to carry out the execution of this example. + + +To run the example, it is first necessary to compile the P4 program: + +```bash + +sudo make + +``` + +Which will create a directory structure where useful information for the execution of the example will be stored, and it will compile our P4 program (storing it under the `build` directory). + +Once the P4 program is compiled, you can run the `test.py` script which will build the topology in Mininet-Wifi. + +```bash + +sudo python test.py + +``` + + + diff --git a/examples/p4/bmv2-netns/basic.p4 b/examples/p4/bmv2-netns/basic.p4 new file mode 100644 index 000000000..b9f5aef91 --- /dev/null +++ b/examples/p4/bmv2-netns/basic.p4 @@ -0,0 +1,184 @@ +/* -*- P4_16 -*- */ +#include +#include + +const bit<16> TYPE_IPV4 = 0x800; + +/************************************************************************* +*********************** H E A D E R S *********************************** +*************************************************************************/ + +typedef bit<9> egressSpec_t; +typedef bit<48> macAddr_t; +typedef bit<32> ip4Addr_t; + +header ethernet_t { + macAddr_t dstAddr; + macAddr_t srcAddr; + bit<16> etherType; +} + +header ipv4_t { + bit<4> version; + bit<4> ihl; + bit<8> diffserv; + bit<16> totalLen; + bit<16> identification; + bit<3> flags; + bit<13> fragOffset; + bit<8> ttl; + bit<8> protocol; + bit<16> hdrChecksum; + ip4Addr_t srcAddr; + ip4Addr_t dstAddr; +} + +struct metadata { + /* empty */ +} + +struct headers { + ethernet_t ethernet; + ipv4_t ipv4; +} + +/************************************************************************* +*********************** P A R S E R *********************************** +*************************************************************************/ + +parser MyParser(packet_in packet, + out headers hdr, + inout metadata meta, + inout standard_metadata_t standard_metadata) { + + state start { + /* TODO: add parser logic */ + + transition parser_ethernet; + } + + state parser_ethernet { + packet.extract(hdr.ethernet); + transition select (hdr.ethernet.etherType) { + TYPE_IPV4: parser_ipv4; + default: accept; + } + } + + state parser_ipv4{ + packet.extract(hdr.ipv4); + transition accept; + } +} + + +/************************************************************************* +************ C H E C K S U M V E R I F I C A T I O N ************* +*************************************************************************/ + +control MyVerifyChecksum(inout headers hdr, inout metadata meta) { + apply { } +} + + +/************************************************************************* +************** I N G R E S S P R O C E S S I N G ******************* +*************************************************************************/ + +control MyIngress(inout headers hdr, + inout metadata meta, + inout standard_metadata_t standard_metadata) { + action drop() { + mark_to_drop(standard_metadata); + } + + action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) { + /* TODO: fill out code in action body */ + standard_metadata.egress_spec = port; + /*hdr.ethernet.srcAddr = hdr.ethernet.dstAddr; + hdr.ethernet.dstAddr = dstAddr; + hdr.ipv4.ttl = hdr.ipv4.ttl - 1;*/ + + } + + table ipv4_lpm { + key = { + hdr.ipv4.dstAddr: lpm; + } + actions = { + ipv4_forward; + drop; + NoAction; + } + size = 1024; + default_action = NoAction(); + } + + apply { + /* TODO: fix ingress control logic + * - ipv4_lpm should be applied only when IPv4 header is valid + */ + if(hdr.ipv4.isValid()) + ipv4_lpm.apply(); + } +} + +/************************************************************************* +**************** E G R E S S P R O C E S S I N G ******************* +*************************************************************************/ + +control MyEgress(inout headers hdr, + inout metadata meta, + inout standard_metadata_t standard_metadata) { + apply { } +} + +/************************************************************************* +************* C H E C K S U M C O M P U T A T I O N ************** +*************************************************************************/ + +control MyComputeChecksum(inout headers hdr, inout metadata meta) { + apply { + update_checksum( + hdr.ipv4.isValid(), + { hdr.ipv4.version, + hdr.ipv4.ihl, + hdr.ipv4.diffserv, + hdr.ipv4.totalLen, + hdr.ipv4.identification, + hdr.ipv4.flags, + hdr.ipv4.fragOffset, + hdr.ipv4.ttl, + hdr.ipv4.protocol, + hdr.ipv4.srcAddr, + hdr.ipv4.dstAddr }, + hdr.ipv4.hdrChecksum, + HashAlgorithm.csum16); + } +} + + +/************************************************************************* +*********************** D E P A R S E R ******************************* +*************************************************************************/ + +control MyDeparser(packet_out packet, in headers hdr) { + apply { + /* TODO: add deparser logic */ + packet.emit(hdr.ethernet); + packet.emit(hdr.ipv4); + } +} + +/************************************************************************* +*********************** S W I T C H ******************************* +*************************************************************************/ + +V1Switch( +MyParser(), +MyVerifyChecksum(), +MyIngress(), +MyEgress(), +MyComputeChecksum(), +MyDeparser() +) main; diff --git a/examples/p4/bmv2-netns/runtime/ap1-runtime.json b/examples/p4/bmv2-netns/runtime/ap1-runtime.json new file mode 100644 index 000000000..1636d67d2 --- /dev/null +++ b/examples/p4/bmv2-netns/runtime/ap1-runtime.json @@ -0,0 +1,57 @@ +{ + "target": "bmv2", + "p4info": "build/basic.p4.p4info.txt", + "bmv2_json": "build/basic.json", + "table_entries": [ + { + "table": "MyIngress.ipv4_lpm", + "default_action": true, + "action_name": "MyIngress.drop", + "action_params": { } + }, + { + "table": "MyIngress.ipv4_lpm", + "match": { + "hdr.ipv4.dstAddr": ["10.0.0.1", 32] + }, + "action_name": "MyIngress.ipv4_forward", + "action_params": { + "dstAddr": "00:00:00:00:00:01", + "port": 1 + } + }, + { + "table": "MyIngress.ipv4_lpm", + "match": { + "hdr.ipv4.dstAddr": ["10.0.0.2", 32] + }, + "action_name": "MyIngress.ipv4_forward", + "action_params": { + "dstAddr": "00:00:00:00:00:02", + "port": 2 + } + }, + { + "table": "MyIngress.ipv4_lpm", + "match": { + "hdr.ipv4.dstAddr": ["10.0.0.3", 32] + }, + "action_name": "MyIngress.ipv4_forward", + "action_params": { + "dstAddr": "00:00:00:00:00:03", + "port": 3 + } + }, + { + "table": "MyIngress.ipv4_lpm", + "match": { + "hdr.ipv4.dstAddr": ["10.0.0.4", 32] + }, + "action_name": "MyIngress.ipv4_forward", + "action_params": { + "dstAddr": "00:00:00:00:00:04", + "port": 3 + } + } + ] +} diff --git a/examples/p4/bmv2-netns/runtime/ap2-runtime.json b/examples/p4/bmv2-netns/runtime/ap2-runtime.json new file mode 100644 index 000000000..3adf438ca --- /dev/null +++ b/examples/p4/bmv2-netns/runtime/ap2-runtime.json @@ -0,0 +1,57 @@ +{ + "target": "bmv2", + "p4info": "build/basic.p4.p4info.txt", + "bmv2_json": "build/basic.json", + "table_entries": [ + { + "table": "MyIngress.ipv4_lpm", + "default_action": true, + "action_name": "MyIngress.drop", + "action_params": { } + }, + { + "table": "MyIngress.ipv4_lpm", + "match": { + "hdr.ipv4.dstAddr": ["10.0.0.3", 32] + }, + "action_name": "MyIngress.ipv4_forward", + "action_params": { + "dstAddr": "00:00:00:00:00:03", + "port": 1 + } + }, + { + "table": "MyIngress.ipv4_lpm", + "match": { + "hdr.ipv4.dstAddr": ["10.0.0.4", 32] + }, + "action_name": "MyIngress.ipv4_forward", + "action_params": { + "dstAddr": "00:00:00:00:00:04", + "port": 3 + } + }, + { + "table": "MyIngress.ipv4_lpm", + "match": { + "hdr.ipv4.dstAddr": ["10.0.0.1", 32] + }, + "action_name": "MyIngress.ipv4_forward", + "action_params": { + "dstAddr": "00:00:00:00:00:01", + "port": 2 + } + }, + { + "table": "MyIngress.ipv4_lpm", + "match": { + "hdr.ipv4.dstAddr": ["10.0.0.2", 32] + }, + "action_name": "MyIngress.ipv4_forward", + "action_params": { + "dstAddr": "00:00:00:00:00:02", + "port": 2 + } + } + ] +} diff --git a/examples/p4/bmv2-netns/test.py b/examples/p4/bmv2-netns/test.py new file mode 100644 index 000000000..990651bd2 --- /dev/null +++ b/examples/p4/bmv2-netns/test.py @@ -0,0 +1,70 @@ +#!/usr/bin/python + +import os + +from mininet.log import setLogLevel, info +from mn_wifi.cli import CLI +from mn_wifi.net import Mininet_wifi +from mn_wifi.bmv2 import P4RuntimeAP + + +class MininetWifi_bmv2InNetns(Mininet_wifi): + + def configureControlNetwork(self): + """Configure control network.""" + info( '*** Configuring the intf control - controller\n' ) + for controller in self.controllers: + info( controller.name + ' ') + controller.cmd('ip link set dev lo up') + info( '\n' ) + + info( '*** Configuring the intf control for %s APs\n' % len( self.aps ) ) + for ap in self.aps: + info( ap.name + ' ') + ap.cmd( 'ip link set dev lo up' ) + + + +def topology(): + 'Create a network.' + net = MininetWifi_bmv2InNetns(inNamespace = True) + + info('*** Adding stations/hosts\n') + sta1 = net.addStation('sta1', ip='10.0.0.1', mac='00:00:00:00:00:01') + h1 = net.addHost('h1', ip='10.0.0.2', mac='00:00:00:00:00:02') + sta2 = net.addStation('sta2', ip='10.0.0.3', mac='00:00:00:00:00:03') + h2 = net.addHost('h2', ip='10.0.0.4', mac='00:00:00:00:00:04') + + info('*** Adding P4RuntimeAP\n') + ap1 = net.addAccessPoint('ap1', cls=P4RuntimeAP, json_path='build/basic.json', runtime_json_path='runtime/ap1-runtime.json', + log_console = True, log_dir = os.path.abspath('logs'), log_file = 'ap1.log', pcap_dump = os.path.abspath('pcaps')) + + ap2 = net.addAccessPoint('ap2', cls=P4RuntimeAP, json_path='build/basic.json', runtime_json_path='runtime/ap2-runtime.json', + log_console = True, log_dir = os.path.abspath('logs'), log_file = 'ap2.log', pcap_dump = os.path.abspath('pcaps')) + + + net.configureWifiNodes() + + + info('*** Creating links\n') + net.addLink(sta1, ap1) + net.addLink(h1, ap1) + net.addLink(ap1, ap2) + net.addLink(sta2, ap2) + net.addLink(h2, ap2) + + + info('*** Starting network\n') + net.start() + net.staticArp() + + info('*** Running CLI\n') + CLI(net) + + info('*** Stopping network\n') + net.stop() + + +if __name__ == '__main__': + setLogLevel('info') + topology() diff --git a/examples/p4/util/Makefile b/examples/p4/util/Makefile new file mode 100644 index 000000000..a78d0015d --- /dev/null +++ b/examples/p4/util/Makefile @@ -0,0 +1,43 @@ +BUILD_DIR = build +PCAP_DIR = pcaps +LOG_DIR = logs +P4C = p4c-bm2-ss +P4C_ARGS += --p4runtime-files $(BUILD_DIR)/$(basename $@).p4.p4info.txt +source = $(wildcard *.p4) +compiled_json := $(source:.p4=.json) + +ifndef DEFAULT_PROG +DEFAULT_PROG = $(wildcard *.p4) +endif +DEFAULT_JSON = $(BUILD_DIR)/$(DEFAULT_PROG:.p4=.json) + +# Define NO_P4 to start BMv2 without a program +ifndef NO_P4 +run_args += -j $(DEFAULT_JSON) +endif + +# Set BMV2_SWITCH_EXE to override the BMv2 target +ifdef BMV2_SWITCH_EXE +run_args += -b $(BMV2_SWITCH_EXE) +endif + + + + + +all: build + +stop: + sudo mn -c + +build: dirs $(compiled_json) + +%.json: %.p4 + $(P4C) --p4v 16 $(P4C_ARGS) -o $(BUILD_DIR)/$@ $< + +dirs: + mkdir -p $(BUILD_DIR) $(PCAP_DIR) $(LOG_DIR) + +clean: stop + rm -f *.pcap + rm -rf $(BUILD_DIR) $(PCAP_DIR) $(LOG_DIR) diff --git a/mn_wifi/bmv2.py b/mn_wifi/bmv2.py index 2dcb02e66..7299c1a3f 100644 --- a/mn_wifi/bmv2.py +++ b/mn_wifi/bmv2.py @@ -23,12 +23,16 @@ import socket import threading import urllib2 +import tempfile +import psutil from contextlib import closing import time -from mininet.log import info, warn, debug +from mininet.log import info, warn, debug, error from mininet.node import Switch, Host -from mn_wifi.node import AP, Station +from mininet.moduledeps import pathCheck +from mn_wifi.node import AP, Station, Netns_mgmt + SIMPLE_SWITCH_GRPC = 'simple_switch_grpc' SIMPLE_SWITCH_CLI = 'simple_switch_CLI' @@ -436,3 +440,327 @@ class Bmv2Switch(ONOSBmv2Switch): def __init__(self, name, **kwargs): ONOSBmv2Switch.__init__(self, name, **kwargs) self.netcfg = False + + + + +class P4AP(AP): + """P4 virtual ap""" + device_id = 0 + next_thrift_port = 9090 + + def __init__(self, name, sw_path = 'simple_switch_grpc', json_path = None, + thrift_port = None, + pcap_dump = False, + log_console = False, + log_file = None, + log_dir = None, + ourpid = None, + verbose = False, + device_id = None, + enable_debugger = False, + **kwargs): + """ + Builder of P4AP class, which supports all the common elements of the BMv2. + """ + + AP.__init__(self, name, **kwargs) + global next_thrift_port + + + # Make sure that the provided sw_path is valid + assert(sw_path) + pathCheck(sw_path) + self.sw_path = sw_path + + + # Make sure that the provided JSON file exists + if json_path is not None: + if not os.path.isfile(json_path): + error("Invalid JSON file.\n") + exit(1) + self.json_path = json_path + else: + self.json_path = None + + + self.thrift_port = P4AP.next_thrift_port + P4AP.next_thrift_port += 1 + + if P4AP.check_listening_on_port(self.thrift_port, self.pid): + 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 log_file is not None: + self.log_file = log_file + else: + self.log_file = "/tmp/p4s.{}.log".format(self.name) + + if log_dir is not None: + self.log_dir = log_dir + else: + self.log_dir = '/tmp' + + if device_id is not None: + self.device_id = device_id + P4AP.device_id = max(P4AP.device_id, device_id) + else: + self.device_id = P4AP.device_id + P4AP.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 P4AP.check_listening_on_port(self.thrift_port, self.pid): + return True + time.sleep(0.5) + + + + def start(self, controllers): + """Start up a new P4 ap""" + + info("Starting P4 ap {}.\n".format(self.name)) + args = [self.sw_path] + pid = None + + 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 %s" % self.pcap_dump) + + 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)]) + P4AP.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") + + 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 ap {} PID is {}.\n".format(self.name, pid)) + if not self.check_switch_started(pid): + error("P4 ap {} did not start correctly.\n".format(self.name)) + exit(1) + + info("P4 ap {} 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) + + @classmethod + def check_listening_on_port(self, port, _pid): + """Check if a port is listening""" + with Netns_mgmt (nspid = _pid): + for c in psutil.net_connections(kind='inet'): + if c.status == 'LISTEN' and c.laddr[1] == port: + return True + return False + + + + +class P4RuntimeAP(P4AP): + """BMv2 AP with gRPC support""" + next_grpc_port = 50051 + + def __init__(self, name, sw_path = 'simple_switch_grpc', runtime_json_path = None, + grpc_port = None, + device_id = None, + **kwargs): + """ + Builder of the P4Runtime class, this in turn inherits from the P4AP class, + which supports all the common elements of the BMv2. + """ + + P4AP.__init__(self, name, **kwargs) + + # Make sure that the BMV2-exe exists + pathCheck(sw_path) + self.sw_path = sw_path + + + # Make sure that the provided Runtime JSON file exists + if runtime_json_path is not None: + if not os.path.isfile(runtime_json_path): + error("Invalid runtime JSON file.\n") + exit(1) + self.runtime_json_path = runtime_json_path + else: + self.runtime_json_path = None + + # We assign the indicated gRPC port univocally, + # making sure that it is in use by another process + if grpc_port is not None: + self.grpc_port = grpc_port + else: + self.grpc_port = P4RuntimeAP.next_grpc_port + P4RuntimeAP.next_grpc_port += 1 + + if P4AP.check_listening_on_port( self.grpc_port, self.pid ): + error('%s cannot bind port %d because it is bound by another process\n' % (self.name, self.grpc_port)) + exit(1) + + + if device_id is not None: + self.device_id = device_id + P4AP.device_id = max(P4AP.device_id, device_id) + else: + self.device_id = P4AP.device_id + P4AP.device_id += 1 + self.nanomsg = "ipc:///tmp/bm-{}-log.ipc".format(self.device_id) + + + + def check_ap_started(self, pid): + """ + This method will check if the process is up and if the + BMv2 is listening on the indicated gRPC port. + """ + + for _ in range(SWITCH_START_TIMEOUT * 2): + if not os.path.exists(os.path.join("/proc", str(pid))): + return False + if P4AP.check_listening_on_port(int(self.grpc_port), self.pid): + return True + time.sleep(0.5) + + + + def start(self, controllers): + """ + This method will initialize the BMV2 given the parameters + passed to the class. Finally, it will configure the switch using P4Runtime + """ + + info("Starting P4RuntimeAP {}.\n".format(self.name)) + + args = [self.sw_path] + pid = None + + # It will form the execution cmd based on the class attributes. + 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 %s" % self.pcap_dump) + + if self.nanomsg: + args.extend(['--nanolog', self.nanomsg]) + + args.extend(['--device-id', str(self.device_id)]) + P4AP.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)) + + + # We put together the arguments of the cmd to execute it. + cmd = ' '.join(args) + + info(cmd + "\n") + with tempfile.NamedTemporaryFile() as f: + self.cmd(cmd + ' >'+ self.log_dir + '/' + self.log_file + ' 2>&1 & echo $! >> ' + f.name) + pid = int(f.read()) + + + # We check that the BMv2 has been started correctly. + # If so, we proceed to configure it with P4Runtime. + debug("P4RuntimeAP {} PID is {}.\n".format(self.name, pid)) + if not self.check_ap_started(pid): + error("P4 ap {} did not start correctly.\n".format(self.name)) + exit(1) + + info("P4RuntimeAP {} has been started.\n".format(self.name)) + self.program_ap_runtime() + + + + def program_ap_runtime(self): + """ This method will use P4Runtime to program the switch using the + content of the runtime JSON file as input. + """ + + from mn_wifi.p4runtime_lib.simple_controller import program_switch + grpc_port = self.grpc_port + device_id = self.device_id + runtime_json = self.runtime_json_path + + info('Configuring AP %s using P4Runtime with file %s' % (self.name, runtime_json)) + with open(runtime_json, 'r') as sw_conf_file: + outfile = '%s/%s-p4runtime-requests.txt' % (self.log_dir, self.name) + with Netns_mgmt(nspid = self.pid ): + 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) diff --git a/mn_wifi/node.py b/mn_wifi/node.py index 725272fb0..42e27e02a 100644 --- a/mn_wifi/node.py +++ b/mn_wifi/node.py @@ -39,6 +39,14 @@ from mn_wifi.propagationModels import GetSignalRange, GetPowerGivenRange from re import findall +from ctypes import cdll +""" + We make use of the setns system call from the libc library. Check this out: + + http://man7.org/linux/man-pages/man2/setns.2.html +""" +libc = cdll.LoadLibrary('libc.so.6') +setns_func = libc.setns class Node_wifi(Node): @@ -1145,3 +1153,47 @@ def __init__( self, *args, **kwargs ): see OVSSwitch for other options""" kwargs.update( failMode='standalone' ) OVSAP.__init__( self, *args, **kwargs ) + + +class Netns_mgmt(object): + + "Class to manage the Network namespace in runtime" + + + def __init__(self, nsname=None, nspath=None, nspid=None): + "The libc setns function is used to switch the current process between the different Netns." + + self.current_path = Netns_mgmt.get_netns_path(netns_pid=os.getpid()) + self.target_path = Netns_mgmt.get_netns_path(netns_path=nspath, netns_name=nsname, netns_pid=nspid) + + if not self.target_path: + raise ValueError('invalid namespace') + + + def __enter__(self): + "We enter the netns, via name or via pid" + + if os.getuid() != 0: + raise ValueError('Netns_mgmt must run as root.') + + self.current_ns = open(self.current_path) + with open(self.target_path) as fd: + setns_func(fd.fileno(), 0) + + + def __exit__(self, *args): + "We left the netns, going back to the original netns" + + setns_func(self.current_ns.fileno(), 0) + self.current_ns.close() + + + @staticmethod + def get_netns_path(netns_path=None, netns_name=None, netns_pid=None): + + if netns_name: + netns_path = '/var/run/netns/' + str(netns_name) + elif netns_pid: + netns_path = '/proc/'+ str(netns_pid) +'/ns/net' + + return netns_path diff --git a/mn_wifi/p4runtime_lib/__init__.py b/mn_wifi/p4runtime_lib/__init__.py new file mode 100644 index 000000000..c15ea6acf --- /dev/null +++ b/mn_wifi/p4runtime_lib/__init__.py @@ -0,0 +1 @@ +"Docstring to silence pylint; ignores --ignore option for __init__.py" diff --git a/mn_wifi/p4runtime_lib/bmv2_runtime.py b/mn_wifi/p4runtime_lib/bmv2_runtime.py new file mode 100644 index 000000000..7f483f476 --- /dev/null +++ b/mn_wifi/p4runtime_lib/bmv2_runtime.py @@ -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) diff --git a/mn_wifi/p4runtime_lib/convert.py b/mn_wifi/p4runtime_lib/convert.py new file mode 100644 index 000000000..0375e1747 --- /dev/null +++ b/mn_wifi/p4runtime_lib/convert.py @@ -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 diff --git a/mn_wifi/p4runtime_lib/error_utils.py b/mn_wifi/p4runtime_lib/error_utils.py new file mode 100644 index 000000000..487c98e6f --- /dev/null +++ b/mn_wifi/p4runtime_lib/error_utils.py @@ -0,0 +1,92 @@ +# 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 sys + +from google.rpc import status_pb2, code_pb2 +import grpc +from p4.v1 import p4runtime_pb2 +from p4.v1 import p4runtime_pb2_grpc + +# Used to indicate that the gRPC error Status object returned by the server has +# an incorrect format. +class P4RuntimeErrorFormatException(Exception): + def __init__(self, message): + super(P4RuntimeErrorFormatException, self).__init__(message) + + +# Parse the binary details of the gRPC error. This is required to print some +# helpful debugging information in tha case of batched Write / Read +# requests. Returns None if there are no useful binary details and throws +# P4RuntimeErrorFormatException if the error is not formatted +# properly. Otherwise, returns a list of tuples with the first element being the +# index of the operation in the batch that failed and the second element being +# the p4.Error Protobuf message. +def parseGrpcErrorBinaryDetails(grpc_error): + if grpc_error.code() != grpc.StatusCode.UNKNOWN: + return None + + error = None + # The gRPC Python package does not have a convenient way to access the + # binary details for the error: they are treated as trailing metadata. + for meta in grpc_error.trailing_metadata(): + if meta[0] == "grpc-status-details-bin": + error = status_pb2.Status() + error.ParseFromString(meta[1]) + break + if error is None: # no binary details field + return None + if len(error.details) == 0: + # binary details field has empty Any details repeated field + return None + + indexed_p4_errors = [] + for idx, one_error_any in enumerate(error.details): + p4_error = p4runtime_pb2.Error() + if not one_error_any.Unpack(p4_error): + raise P4RuntimeErrorFormatException( + "Cannot convert Any message to p4.Error") + if p4_error.canonical_code == code_pb2.OK: + continue + indexed_p4_errors += [(idx, p4_error)] + + return indexed_p4_errors + + +# P4Runtime uses a 3-level message in case of an error during the processing of +# a write batch. This means that some care is required when printing the +# exception if we do not want to end-up with a non-helpful message in case of +# failure as only the first level will be printed. In this function, we extract +# the nested error message when present (one for each operation included in the +# batch) in order to print error code + user-facing message. See P4Runtime +# documentation for more details on error-reporting. +def printGrpcError(grpc_error): + print "gRPC Error", grpc_error.details(), + status_code = grpc_error.code() + print "({})".format(status_code.name), + traceback = sys.exc_info()[2] + print "[{}:{}]".format( + traceback.tb_frame.f_code.co_filename, traceback.tb_lineno) + if status_code != grpc.StatusCode.UNKNOWN: + return + p4_errors = parseGrpcErrorBinaryDetails(grpc_error) + if p4_errors is None: + return + print "Errors in batch:" + for idx, p4_error in p4_errors: + code_name = code_pb2._CODE.values_by_number[ + p4_error.canonical_code].name + print "\t* At index {}: {}, '{}'\n".format( + idx, code_name, p4_error.message) diff --git a/mn_wifi/p4runtime_lib/helper.py b/mn_wifi/p4runtime_lib/helper.py new file mode 100644 index 000000000..8e3e20bd5 --- /dev/null +++ b/mn_wifi/p4runtime_lib/helper.py @@ -0,0 +1,200 @@ +# 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.v1 import p4runtime_pb2 +from p4.config.v1 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.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 + + def buildMulticastGroupEntry(self, multicast_group_id, replicas): + mc_entry = p4runtime_pb2.PacketReplicationEngineEntry() + mc_entry.multicast_group_entry.multicast_group_id = multicast_group_id + for replica in replicas: + r = p4runtime_pb2.Replica() + r.egress_port = replica['egress_port'] + r.instance = replica['instance'] + mc_entry.multicast_group_entry.replicas.extend([r]) + return mc_entry diff --git a/mn_wifi/p4runtime_lib/simple_controller.py b/mn_wifi/p4runtime_lib/simple_controller.py new file mode 100755 index 000000000..6a8a8c6d2 --- /dev/null +++ b/mn_wifi/p4runtime_lib/simple_controller.py @@ -0,0 +1,214 @@ +#!/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_runtime +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_runtime.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) + + if 'multicast_group_entries' in sw_conf: + group_entries = sw_conf['multicast_group_entries'] + info("Inserting %d group entries..." % len(group_entries)) + for entry in group_entries: + info(groupEntryToString(entry)) + insertMulticastGroupEntry(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) + + +def groupEntryToString(rule): + group_id = rule["multicast_group_id"] + replicas = ['%d' % replica["egress_port"] for replica in rule['replicas']] + ports_str = ', '.join(replicas) + return 'Group {0} => ({1})'.format(group_id, ports_str) + +def insertMulticastGroupEntry(sw, rule, p4info_helper): + mc_entry = p4info_helper.buildMulticastGroupEntry(rule["multicast_group_id"], rule['replicas']) + sw.WriteMulticastGroupEntry(mc_entry) + + +if __name__ == '__main__': + main() diff --git a/mn_wifi/p4runtime_lib/switch.py b/mn_wifi/p4runtime_lib/switch.py new file mode 100644 index 000000000..a7c4f7a94 --- /dev/null +++ b/mn_wifi/p4runtime_lib/switch.py @@ -0,0 +1,184 @@ +# 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.v1 import p4runtime_pb2 +from p4.v1 import p4runtime_pb2_grpc +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_grpc.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 "P4Runtime MasterArbitrationUpdate: ", request + else: + self.requests_stream.put(request) + for item in self.stream_msg_resp: + return item # just one + + 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 "P4Runtime 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() + if table_entry.is_default_action: + update.type = p4runtime_pb2.Update.MODIFY + else: + update.type = p4runtime_pb2.Update.INSERT + update.entity.table_entry.CopyFrom(table_entry) + if dry_run: + print "P4Runtime 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 "P4Runtime 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 "P4Runtime Read:", request + else: + for response in self.client_stub.Read(request): + yield response + + + def WriteMulticastGroupEntry(self, mc_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.packet_replication_engine_entry.CopyFrom(mc_entry) + if dry_run: + print "P4Runtime Write:", request + else: + self.client_stub.Write(request) + +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) diff --git a/setup.py b/setup.py index 3c324f526..10f36b358 100755 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ 'mn_wifi.examples.eap-tls', 'mn_wifi.examples.eap-tls.CA', 'mn_wifi.sumo', 'mn_wifi.sumo.sumolib', 'mn_wifi.sumo.traci', 'mn_wifi.sumo.data', 'mn_wifi.sumo.sumolib.net', 'mn_wifi.sumo.sumolib.output', - 'mn_wifi.sumo.sumolib.shapes', 'util' ], + 'mn_wifi.sumo.sumolib.shapes', 'util', 'mn_wifi.p4runtime_lib' ], package_data={'util' : ['m'], 'mn_wifi.sumo.data': ['*.xml', '*.sumocfg'], 'mn_wifi.data': ['signal_table_ieee80211ax', 'signal_table_ieee80211n_gi20',