Multiple p4 files loaded for the same topoloy

I am having this topology which has two switches and two routers. I wanted to implement in p4 the l2_forwarding in switches and routers but also functionalities which are used only in routers like IP protocol and ARP.

Firstly, i was going to implement both the functionality of switch and router with arp protocol in the same p4 file, but i was able to see that i could not distinguish the switch from the router in order the switch not to support the arp protocol.(Correct me if i am wrong!).

So my second thought was to implement the switch in different p4 file from router and then load these files using the from the tutorials of p4 and p4runtime_lib.

I changed the Makefile of these tutorials to support multiple .p4 files which will be end up to .json after compilation.
My problem now is inside in this function:

 def create_network(self):
        """ Create the mininet network object, and store it as
            Side effects:
                - Mininet topology instance stored as self.topo
                - Mininet instance stored as
        self.logger("Building mininet topology.")

        defaultSwitchClass = configureP4Switch(

        self.topo = ExerciseTopo(self.hosts, self.switches, self.links, self.log_dir, self.bmv2_exe, self.pcap_dir) = Mininet(topo = self.topo,
                      link = TCLink,
                      host = P4Host,
                      switch = defaultSwitchClass,
                      controller = None)

Because of two switches and two routers of the topology i think that i am needing two instances of configureP4Switch function one for the routers and one for the switches.
But my problem is in the Mininet(), how can i have in the same mininet enviroment both the switches and routers?
In the Mininet() the switch variable will be assigned either the router part or switch part and the other part stays undeclared.

Excuse me for the large post.
Thanks in advance!

Hi @ppavlidis,

Let me first answer your questions :slight_smile:

I saw this done before. There is a very nice website from Chih-Heng Ke. He has numerous exercises implemented, but the one you ask is here. In short, you have to define the Topology class and the associate a .json with each switch in this way:

#all imports

parser.add_argument('--json1', help='Path to JSON1 config file',type=str, action="store",required=True)
parser.add_argument('--json2', help='Path to JSON2 config file',type=str,action="store", required=True)

# mode code

class SingleSwitchTopo(Topo):
    def __init__(self, sw_path, json_path1, json_path2, thrift_port, pcap_dump, **opts):
        Topo.__init__(self, **opts)
        switch1 = self.addSwitch('s1', sw_path = sw_path, json_path = json_path1, thrift_port = thrift_port,cls = P4Switch ,pcap_dump = pcap_dump)
        switch2 = self.addSwitch('s2', sw_path = sw_path, json_path = json_path2, thrift_port = thrift_port + 1,cls = P4Switch ,pcap_dump = pcap_dump)
        # more code

def main():
    topo = SingleSwitchTopo(args.behavioral_exe, args.json1, args.json2, args.thrift_port, args.pcap_dump)
    # more code

if __name__ == '__main__':

I hope it helps.

Finally, from a student perspective, I always found interesting how we label devices as switches or routers. IMO, what the community understands as a “switch” or as a “router” varies from target to target and from the manufacturer to manufacturer (but there is a general understanding that traditional routers and switches operate at different layers). For instance, you can find L2 or L3 switches (weird, right?) Nowadays, in SDN, you can find devices that are called “switches” or forwarding devices in SDN that can to L2 to L4 (let’s say an OpenFlow switch) or a P4 switch that can do pretty much any layer (but they do worse in the application layer, especially if it involves variable size headers). So in the end you can have “switches” and “routers” in your network but if they perform tasks outside the typical ones associated with a switch or a router, the definition be sometimes confusing. So, let’s say you can to have a pure switch that does L2 forwarding based on the Ethernet header, but you want to add some functionality that related more to a router. Is it a switch anymore? Maybe others with more experience and years working in the industry can tell a little about the perception of what a switch and router is nowadays, considering the multiple functionalities they perform.


Thank you so much for your response! It helped me a lot :slight_smile: