P4 file and Mininet

Hello,

I have a custom topology custom.py and I wrote a file.p4 file. The problem is that I don’t know how to use the file.p4 into mininet with my topology.

For instance, in this github repository (tutorials/exercises/basic at master · p4lang/tutorials · GitHub) they use the make run command to compile the p4 file, start the topology and configure all the hosts.

Thanks in advance!

Welcome to the forum @nickyy

Short answer: Use the same name as the present P4 file you find on each exercise’s folder. Take a look into the s*-runtime.json file for the rules too of course, they need to make sense with your new program.

Long answer: This is an assumption from checking the code of tutorials now, so I might be wrong in some details. I believe that when you use the comman make for that exercise, the Makefile will use the existing P4 file in each exercise’s root folder. In your case it will take basic.p4, so you would need to have only 1 P4 file at that particular folder with, in principle, any name you want. The problem is that the exercise automates rule installation and p4info usage by hardcoding the name of some files into runtime files. To start, check this code from utils/Makefile:

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

And see this code from runtime files:

  "target": "bmv2",
  "p4info": "build/basic.p4.p4info.txt",
  "bmv2_json": "build/basic.json",

I can see that the Makefile can detect any P4 file, the p4info file and the P4 compiled file (which is also a json), no matter the name. As said, I believe the issue is related the runtime files and hardcoded p4info filenames in the runtime files (when you have a custom filename for your P4). You can always modify those runtime files by changing basic for your program’s name.

Cheers,

Sorry for the late response!!

Ok, forget about the tutorial’s example. If I wanted to execute p4 into mininet with my custom-made topology custome.py and p4 file custom.p4, what would I do?

I’ll try your “long answer” but I’m asking you this in case it is easier what I’m explaining to you above than the tutorial’s example.

Hi,

I guess you have plenty of ways to do it. GitHub - nsg-ethz/p4-utils: Extension to Mininet that makes P4 networks easier to build has many of examples, you just have to follow the same principle as the repository authors do. Modify the main scripts and the main Mininet py script.

You could base your examples in the Mininet Docker container that is used at https://github.com/opennetworkinglab/ngsdn-tutorial. See the Makefile, which shows how to run a Docker container with a custom.py-like script.

I believe the easiest is to modify an exercise of the P4 tutorial (but there might be a nicer and faster way to do it, not sure). This is because, in this way, it is easy to compile the P4 program, load the compiled P4 into the switches, automatically install rules and easy to test. The tutorial builds the scenario based on the topology.json file. So if you can create a topology.json file based on your custom.py, then you are good to go.

Let’s say you want to modify the basic exercise. In order to make it works:

  • Modify the basic.p4 code with your own.
  • Move the hosts, switches and links from your custom.py script. See the structure of the topology.json file. If you do not want to do that (I do not recommend it because it will take longer), you can modify run_exercise.py and prevent it from reading topology.json, and add your own custom.py code to run_exercise.py file. See the make_network method to learn how to add a particular host type, a P4 switch class or create a Mininet network. You can start from there to add your own method that adds your own custom.py code.
  • Change the s*-runtime.json files with the rules for your script, with the proper table names, actions and parameters. You need to do this for any Mininet example, unless you want to manually add the table rules.

I hope it helps.

Cheers,