# P4 file and Mininet

**URL:** <https://forum.p4.org/t/p4-file-and-mininet/355>\
**Category:** Uncategorized\
**Created:** [June 1, 2022, 12:54pm UTC](https://forum.p4.org/t/p4-file-and-mininet/355 "2022-06-01T12:54:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickyy](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nickyy](https://forum.p4.org/u/nickyy)\
**Post date:** [June 1, 2022, 12:54pm UTC](https://forum.p4.org/t/p4-file-and-mininet/355/1 "2022-06-01T12:54:48Z")

</div>

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](https://github.com/p4lang/tutorials/tree/master/exercises/basic)) they use the `make run` command to compile the p4 file, start the topology and configure all the hosts.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ederollora](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/ederollora/32/21_2.png) [@ederollora](https://forum.p4.org/u/ederollora)\
**Post date:** [June 1, 2022, 5:01pm UTC](https://forum.p4.org/t/p4-file-and-mininet/355/2 "2022-06-01T17:01:06Z")

</div>

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:

```auto
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:

```auto
  "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,

---

<div class="post-metadata">

**Author:** ![nickyy](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nickyy](https://forum.p4.org/u/nickyy)\
**Post date:** [June 2, 2022, 6:28pm UTC](https://forum.p4.org/t/p4-file-and-mininet/355/3 "2022-06-02T18:28:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ederollora](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/ederollora/32/21_2.png) [@ederollora](https://forum.p4.org/u/ederollora)\
**Post date:** [June 2, 2022, 8:09pm UTC](https://forum.p4.org/t/p4-file-and-mininet/355/4 "2022-06-02T20:09:18Z")

</div>

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](https://github.com/nsg-ethz/p4-utils) 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](https://github.com/opennetworkinglab/ngsdn-tutorial). See the [`Makefile`](https://github.com/opennetworkinglab/ngsdn-tutorial/blob/62705c3cb64fa7c731b5255f5fc793f293ce0665/Makefile#L35), 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](https://github.com/p4lang/tutorials/blob/6cec685377e15246b57d49df7514b33805b40e2c/utils/run_exercise.py#L154) 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](https://github.com/p4lang/tutorials/blob/6cec685377e15246b57d49df7514b33805b40e2c/utils/run_exercise.py#L239) 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,
