Rewrite of the behavioral model as a C++ project without auto-generated code (except for the PD interface)
C++ C M4 Makefile Python Thrift Shell
Latest commit 7f950b1 Jan 20, 2017 @antoninbas antoninbas Use alias declarations instead of typedef's
For the sake of consistency.
Failed to load latest commit information.
docs Accept general expressions in verify statements (#271) Jan 19, 2017
include Use alias declarations instead of typedef's Jan 20, 2017
m4 port to mac os x and g++-5 (#201) Sep 28, 2016
mininet Minor improvements to mininet example (#264) Jan 16, 2017
pdfixed Comment out printfs for bmv2 notifications (#220) Oct 18, 2016
src Use alias declarations instead of typedef's Jan 20, 2017
targets Use alias declarations instead of typedef's Jan 20, 2017
tests Use alias declarations instead of typedef's Jan 20, 2017
third_party updated googletest code in third_party (#194) Sep 20, 2016
thrift_src Add support for sharing action profiles Dec 25, 2016
tools Remove deprecated CLI command table_dump_2 Dec 25, 2016
travis changes to travis deps scripts Jun 21, 2016
.gitignore added methods to retrieve table entries and default entry May 27, 2016
.gitmodules got rid of infra Mar 16, 2015
.travis.yml start using trusty beta on travis (#206) Sep 29, 2016
CPPLINT.cfg added files, and a script to invoke cpplint in … Dec 8, 2015
Doxyfile reorganized bmv2 Apr 27, 2016 added main page for the target-design doxygen documentation Jan 22, 2016
LICENSE Added licensing info Jun 4, 2015 changes to travis deps scripts Jun 21, 2016 Minor improvements to mininet example (#264) Jan 16, 2017
VERSION changed VERSION number to 1.5.0 for release Jan 1, 2017 Add, from tm_sim May 4, 2015 Add flag to configure to skip undeterministic test (#249) Dec 20, 2016 changes to travis deps scripts Jun 21, 2016


Build Status

This is the second version of the P4 software switch (aka behavioral model), nicknamed bmv2. It is meant to replace the original version, p4c-behavioral, in the long run, although we do not have feature equivalence yet. Unlike p4c-behavioral, this new version is static (i.e. we do not need to auto-generate new code and recompile every time a modification is done to the P4 program) and written in C++11. For information on why we decided to write a new version of the behavioral model, please look at the FAQ below.


On Ubuntu 14.04, the following packages are required:

  • automake
  • cmake
  • libjudy-dev
  • libgmp-dev
  • libpcap-dev
  • libboost-dev
  • libboost-test-dev
  • libboost-program-options-dev
  • libboost-system-dev
  • libboost-filesystem-dev
  • libboost-thread-dev
  • libevent-dev
  • libtool
  • flex
  • bison
  • pkg-config
  • g++
  • libssl-dev

You also need to install thrift and nanomsg from source. Feel free to use the install scripts under travis/.

To use the CLI, you will need to install the nnpy Python package. Feel free to use travis/

To make your life easier, we provide the script, which will install all the dependencies needed on Ubuntu 14.04.

Our Travis regression tests now run on Ubuntu 14.04.

On MacOS you can use the tools/macos/ script to install all the above dependencies using homebrew. Note that in order to compile the code you need XCode 8 or later.

Building the code

1. ./
2. ./configure
3. make
4. [sudo] make install  # if you need to install bmv2

In addition, on Linux, you may have to run sudo ldconfig after installing bmv2, to refresh the shared library cache.

Debug logging is enabled by default. If you want to disable it for performance reasons, you can pass --disable-logging-macros to the configure script.

In 'debug mode', you probably want to disable compiler optimization and enable symbols in the binary:

./configure 'CXXFLAGS=-O0 -g'

The new bmv2 debugger can be enabled by passing --enable-debugger to configure.

Running the tests

To run the unit tests, simply do:

make check

If you get a nanomsg error when running the tests (make check), try running them as sudo

Running your P4 program

To run your own P4 programs in bmv2, you first need to transform the P4 code into a json representation which can be consumed by the software switch. This representation will tell bmv2 which tables to initialize, how to configure the parser, ... It is produced by the p4c-bm tool. Please take a look at the README for this repo to find out how to install it. Once this is done, you can obtain the json file as follows:

p4c-bm --json <path to JSON file> <path to P4 file>

The json file can now be 'fed' to bmv2. Assuming you are using the simple_switch target:

sudo ./simple_switch -i 0@<iface0> -i 1@<iface1> <path to JSON file>

In this example <iface0> and <iface1> are the interfaces which are bound to the switch (as ports 0 and 1).

Using the CLI to populate tables...

The CLI code can be found at tools/ It can be used like this:

./ --thrift-port 9090

The CLI connect to the Thrift RPC server running in each switch process. 9090 is the default value but of course if you are running several devices on your machine, you will need to provide a different port for each. One CLI instance can only connect to one switch device.

The CLI is realized using the Python's cmd module and supports auto-completion. If you inspect the code, you will see that the list of supported commands. This list includes:

- table_set_default <table name> <action name> <action parameters>
- table_add <table name> <action name> <match fields> => <action parameters> [priority]
- table_delete <table name> <entry handle>

The CLI include commands to program the multicast engine. Because we provide 2 different engines (SimplePre and SimplePreLAG), you have to specify which one your target is using when starting the CLI, using the --pre option. Accepted values are: None, SimplePre (default value) and SimplePreLAG. The l2_switch target uses the SimplePre engine, while the simple_switch target uses the SimplePreLAG engine.

You can take a look at the commands.txt file for l2_switch and simple_router to see how the CLI can be used.

Using the debugger

To enable the debugger, make sure that you passed the --enable-debugger flag to configure. You will also need to use the --debugger command line flag when starting the switch.

Use tools/ as follows when the switch is running to attach the debugger to the switch:

sudo ./ [--thrift-port <port>]

Displaying the event logging messages

To enable event logging when starting your switch, use the --nanolog command line option. For example, to use the ipc address ipc:///tmp/bm-log.ipc:

sudo ./simple_switch -i 0@<iface0> -i 1@<iface1> --nanolog ipc:///tmp/bm-log.ipc <path to JSON file>

Use tools/ as follows when the switch is running:

sudo ./ [--thrift-port <port>]

The script will display events of significance (table hits / misses, parser transitions, ...) for each packet.

Integrating with Mininet

We will provide more information in a separate document. However you can test the Mininet integration right away using our simple_router target.

In a first terminal, type the following:

- cd mininet
- sudo python --behavioral-exe ../targets/simple_router/simple_router --json ../targets/simple_router/simple_router.json

Then in a second terminal:

- cd targets/simple_router
- ./runtime_CLI < commands.txt

Now the switch is running and the tables have been populated. You can run pingall in Mininet or start a TCP flow with iperf between hosts h1 and h2.

When running a P4 program with simple_switch (instead of simple_router in the above example), just provide the appropriate simple_switch binary to with --behavioral-exe.


Why did we need bmv2 ?

  • The new C++ code is not auto-generated for each P4 program. This means that it becomes very easy and very fast to change your P4 program and test it again. The whole P4 development process becomes more efficient. Every time you change your P4 program, you simply need to produce the json for it using p4c-bm and feed it to the bmv2 executable.
  • Because the bmv2 code is not auto-generated, we hope it is easier to understand. We hope this will encourage the community to contribute even more to the P4 software switch.
  • Using the auto-generated PD library (which of course still needs to be recompiled for each P4 program) is now optional. We provide an intuitive CLI which can be used to program the runtime behavior of each switch device.
  • The new code is target independent. While the original p4c-behavioral assumed a fixed abstract switch model with 2 pipelines (ingress and egress), bmv2 makes no such assumption and can be used to represent many switch architectures. Three different -although similar- such architectures can be found in the targets/ directory. If you are a networking company interested in programming your device (parser, macth-action pipeline, deparser) with P4, you can use bmv2 to reproduce the behavior of your device.

How do program my own target / switch architecture using bmv2 ?

You can take a look at the targets/ directory first. We have also started writing some doxygen documentation specifically targetted at programmers who want to implement their own switch model using the bmv2 building blocks. You can generate this documentation yourself (if you have doxygen installed) by running doxygen Doxyfile. The output can be found under the doxygen-out directory. You can also browse this documentation online.

What else is new in bmv2 ?

  • Arithmetic is now possible on arbitrarily wide fields (no more limited to <= 32-bit fields) and variable-length fields are now supported.
  • We finally have unit tests!
  • While it is still incomplete, we provide a convenient 'event-logger' built on top of nanomsg. Every time a 'significant' event happens (e.g. table hit, parser transition,...) a message is broadcast on a nanomsg channel and any client can consume it.

Are all features supported yet ?

At this time, we believe that all P4 features have been implemented. The last feature to have been implemented is parse value sets, which was scarcely used in bmv1.

If you find more missing features or if you would like to request that a specific feature be added, please send us an email ( or submit an issue with the appropriate label on Github. Do not hesitate to contribute code yourself!

How do I signal a bug ?

Please submit an issue with the appropriate label on Github.

How can I contribute ?

You can fork the repo and submit a pull request in Github. For more information send us an email (

All developers must sign the CLA and return it to ( before making contributions. The CLA is available here.

Any contribution to the C++ core code (in particular the bm_sim module) must respect the coding guidelines. We rely heavily on the Google C++ Style Guide, with some differences listed in this repository's wiki. Every submitted pull request will go through our Travis tests, which include running to ensure correct style and formatting.