Skip to content
Consolidated switch repo (API, SAI and Nettlink)
Branch: master
Clone or download
antoninbas In, move positional parameter (JSON) to front
Now that --pcap takes an optional directory, we cannot provide the JSON path just after --pcap or it confuses the option parser.
Latest commit 92afb04 Feb 1, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
CI/travis Openflow on bmv2 support. (#51) Jul 1, 2016
bmv2 In, move positional parameter (JSON) to front Feb 1, 2018
docker Fix typo in bmv2 Dockerfile (#79) Dec 9, 2016
m4 Openflow on bmv2 support. (#51) Jul 1, 2016
openflow_mapping Openflow on bmv2 support. (#51) Jul 1, 2016
p4-build @ 32c897b Add a packet out test case. (#55) Jul 21, 2016
switchlink disable acl ranges for bmv1 (for openflow) (#72) Oct 27, 2016
switchsai temporary get_attribute patch Dec 21, 2016
tests lag acl label allocation fix (#80) Dec 11, 2016
testutils integrated standalone bmv2 mode with installed PTF Mar 17, 2016
third-party clang formatting Aug 20, 2016
tools added Travis regression for bmv2 switch tests Mar 18, 2016
.clang-format clang formatting Aug 20, 2016
.gitignore integrated standalone bmv2 mode with installed PTF Mar 17, 2016
.gitmodules remove ocp sai submodule and update sai refpoint in switch Jul 27, 2016
.travis.yml Openflow on bmv2 support. (#51) Jul 1, 2016 updated switch repo in response to the changes in bmv2 and p4c-bm, an… Apr 29, 2016 Update README with how to update submodule Jan 23, 2017 new files into switch repo Nov 16, 2015 Openflow docker image and mininet demo (#58) Nov 8, 2016


The switch repository contains the switch.p4 sample P4 program along with all the library repos to manipulate the switch using SAI, SwitchAPI and switchlink.

    +-----+   +-----+   +-----+   +-----+
    |App a|   |App j|   |App n|   |App z|
    |     |...|     |...|     |...|     |
    +-----+   +-----+   +-----+   +-----+
       |         |         |         |
       |         |    +----|         |
+------------+   |    |
| Switchlink |   |    |
|            |<-----------------------------+
+------------------+  |                     |
|     SAI          |  |                     |
|                  |  |                     |
+-----------------------+                   |
|      Switch API       |                   |
|                       |                   |
+-----------------------+---------+         |
|      Resource Mgmt. API         |         |
| (auto-gen. from switch.p4)      |         | Netlink events
+---------------------------------+         |
|        Soft Switch              |         |
|  (compiled from switch.p4)      |         |
+---------------------------------+         |
|                             Kernel                             |
|                                                                |

Directory Structure

p4src - P4 sources
switchsai - SAI library
switchapi - SwitchAPI
switchlink - Linux netlink listener
tests/ptf-tests - P4 dependent(PD), SAI and API tests
tests/of-tests - Openflow tests

Make sure you pull the p4-build submodule with git submodule update --init --recursive.

Running switch in bmv2 without p4factory

You can now run switch.p4 in bmv2 without cloning p4factory. In order to do this you first need to install [bmv2] ( and its compiler [p4c-bmv2] ( on your system. Note that when running ./configure for bmv2, you need to provide the --with-pdfixed option, as switch requires the PD library. Additionally, if you plan on running the tests for switch.p4, please make sure you install [PTF] ( with sudo python install.

Once this is done, you can follow this steps:

 ./configure --with-bmv2 --with-switchsai

The --with-switchsai flag will make sure that the compiled drivers include switchapi and switchsai. If you just need switchapi, replace the flag will --with-switchapi. Replace the flag with --with-switchlink if you need switchlink as well. If you omit these flags, the drivers will only include the PD.

Note that you should be using a fresh clone for this, not the switch submodule that comes with p4factory.

Make sure to look at the output of configure to spot any missing dependency.

Once everything has compiled, you can run the tests for switch.p4 (assuming you have installed [PTF] ( Please make sure that you have all the necessary veth pairs setup (you can use [tools/] (tools/

First, start the software switch with:

   sudo ./bmv2/

Then, start the drivers with:

   sudo ./bmv2/

You can now run all the tests:

sudo ./bmv2/  # for the PD tests
sudo ./bmv2/ --test-dir tests/ptf-tests/api-tests  # for the switchapi tests
sudo ./bmv2/ --test-dir tests/ptf-tests/sai-tests  # for the switchsai tests

Running switch with an openflow agent

You can now use p4ofagent to control switch.p4 on bmv2. To do this, install p4ofagent with CPPFLAGS=-D_BMV2_, then configure switch with --with-bmv2 and --with-of options. You can run the tests with sudo ./bmv2/, and you can also play with a mininet-based l2-learning example by first building the switch docker image, then running sudo ./ --controller-ip <ip>, where <ip> is the ip address of a Ryu instance running the simple_switch_13 app.

Building switch docker image

In addition to the setps in "Running switch in bmv2 without p4factory", configure bmv2 and p4c-bm with --prefix pointing to the install directory and install. Also configure switch with --prefix pointing to the install directory and --with-switchlink.

You can build the base docker image with:

cd switch/docker/bmv2
make -f base-docker-image

This will build the docker image "p4dockerswitch".

sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED         SIZE
p4dockerswitch      latest              8835deb7979e        2 days ago      1.482 GB
ubuntu              14.04               1e0c3dd64ccd        5 weeks ago     187.9 MB

Running switch ntf-tests

Switch NTF tests require NTF (Network Test Framework) and Docker image.

You can now use NTF to build the topology required using the docker image and write tests.

Below are some NTF tests and how to run:

L2: Simple L2 topology with two switches and two hosts. The topology is loop free (no spanning tree protocol).

sudo ntf --topology switch/tests/ntf-tests/topology/l2_topology.json
--config switch/tests/ntf-tests/topology/config.json --test-dir
switch/tests/ntf-tests/ --test L2

STP: L2 topology with four switches and two hosts. It runs MSTPD to form a loop free topology.

sudo ntf --topology switch/tests/ntf-tests/topology/stp_topology.json
--config switch/tests/ntf-tests/topology/config.json --test-dir
switch/tests/ntf-tests/ --test STP

L3 Static: Simple L3 topology with two switches and two hosts. The setup is statically configured.

sudo ntf --topology switch/tests/ntf-tests/topology/l3_static_topology.json
--config switch/tests/ntf-tests/topology/config.json --test-dir
switch/tests/ntf-tests/ --test L3Static

L3 OSPF: Simple L3 topology with two switches and two hosts. The setup runs OSPF (Quagga) to learn and advertise networks.

sudo ntf --topology switch/tests/ntf-tests/topology/l3_ospf_topology.json
--config switch/tests/ntf-tests/topology/config.json --test-dir
switch/tests/ntf-tests/ --test L3OSPF

L3 BGP: Simple L3 topology with two switches and two hosts. The setup runs EBGP (Quagga) to learn and advertise networks.

sudo ntf --topology switch/tests/ntf-tests/topology/l3_bgp_topology.json
--config switch/tests/ntf-tests/topology/config.json --test-dir
switch/tests/ntf-tests/ --test L3BGP
You can’t perform that action at this time.