# How to implement softwared P4 switch in PC or general server to achieve better performance？

**URL:** <https://forum.p4.org/t/how-to-implement-softwared-p4-switch-in-pc-or-general-server-to-achieve-better-performance/698>\
**Category:** Getting Started with P4\
**Created:** [March 25, 2023, 10:55am UTC](https://forum.p4.org/t/how-to-implement-softwared-p4-switch-in-pc-or-general-server-to-achieve-better-performance/698 "2023-03-25T10:55:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JYT](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@JYT](https://forum.p4.org/u/JYT)\
**Post date:** [March 25, 2023, 10:55am UTC](https://forum.p4.org/t/how-to-implement-softwared-p4-switch-in-pc-or-general-server-to-achieve-better-performance/698/1 "2023-03-25T10:55:50Z")

</div>

I am new to P4 and I want to deploy P4 switches in a virtual environment (among multiple VMS, select the middle VM as the P4 switch) and I would like to ask if there is a better way to do this, for example SONiC has support for P4 (link: [SONiC P4 Software Switch · sonic-net/SONiC Wiki · GitHub](https://github.com/sonic-net/SONiC/wiki/SONiC-P4-Software-Switch)) However, it is no longer maintained, and I am not sure how to make P4 interact with network ports on bmv2. Can you help me?

Be very grateful

---

<div class="post-metadata">

**Author:** ![DavideS](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/davides/32/120_2.png) [@DavideS](https://forum.p4.org/u/DavideS)\
**Post date:** [March 25, 2023, 1:15pm UTC](https://forum.p4.org/t/how-to-implement-softwared-p4-switch-in-pc-or-general-server-to-achieve-better-performance/698/2 "2023-03-25T13:15:56Z")

</div>

Hi @JYT

1. When you deploy a BMV2 switch you can a assigni to it virtual of physical interfaces, as is written here [GitHub - p4lang/behavioral-model: The reference P4 software switch](https://github.com/p4lang/behavioral-model#running-your-p4-program) you can pass the interfaces via the flag ` -i 0@<iface0> -i 1@<iface1>`
2. P4 DPDK is under development but definetely provides better persormance respect to bmv2 here the guide made by Andy for deploying it [p4-guide/README-install-ipdk-networking-container-ubuntu-20.04-and-test.md at master · jafingerhut/p4-guide · GitHub](https://github.com/jafingerhut/p4-guide/blob/master/ipdk/23.01/README-install-ipdk-networking-container-ubuntu-20.04-and-test.md)

Best  
Davide

---

<div class="post-metadata">

**Author:** ![JYT](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@JYT](https://forum.p4.org/u/JYT)\
**Post date:** [March 28, 2023, 12:39pm UTC](https://forum.p4.org/t/how-to-implement-softwared-p4-switch-in-pc-or-general-server-to-achieve-better-performance/698/4 "2023-03-28T12:39:27Z")

</div>

Thank you for your reply  
At present, bmv2 and p4c have been deployed on an industrial PC, but there are some problems when using it as a p4 switch.I refer to the information you provide, as well as a link ([Getting Started with P4 - Open Networking Foundation](https://opennetworking.org/news-and-events/blog/getting-started-with-p4/)).  
I run the following command line:

> sudo simple\_switch --interface 0@enp2s0 --interface 1@enp4s0 test.bmv2/test.json &  
> table\_add ipv4\_match to\_port\_action 10.10.0.0/16 =\> 0  
> table\_add ipv4\_match to\_port\_action 11.11.0.0/16 =\> 1

I plan to link two windows hosts through P4siwtch, with the following topology:

> windows PC\<----wired link----\> industrial PC\<----wired link----\>windows PC  
> 10.10.1.2\_\_\_\_\_\_\_\_\_\_\_ enp2s0(P4 Switch)enp4s0\_\_\_\_\_\_\_\_\_\_\_11.11.1.2

windows PC can’t ping each other successfully,But I capture packet information on industrial PC by wireshark（It shows correct source address）.And I followed the links above to complete the whole experiment, which passed correctly…  
But two physical hosts cannot communicate. Why is this?  
Shouldn’t I bind the physical port directly to the virtual switch side?  
The experiment on a physical machine is closer to the real world ,as we intend to simulate the effect of a p4 switch in a data center to assist computing services.but faces some difficulties. 😫

---

<div class="post-metadata">

**Author:** ![DavideS](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/davides/32/120_2.png) [@DavideS](https://forum.p4.org/u/DavideS)\
**Post date:** [March 28, 2023, 2:42pm UTC](https://forum.p4.org/t/how-to-implement-softwared-p4-switch-in-pc-or-general-server-to-achieve-better-performance/698/5 "2023-03-28T14:42:13Z")

</div>

Hi @JYT ,

you should check if the packets reached the bmv2 switches, I truly believe that this not happen. You should implement workaround via a vxlan among physical switch interfaces and virtual interfaces to assign to the bmv2. Because usually the packets are not sent at usespace (where the bmv2 switch runs) so bascally they are not forwarded by the bmv2.

---

<div class="post-metadata">

**Author:** ![JYT](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@JYT](https://forum.p4.org/u/JYT)\
**Post date:** [March 29, 2023, 2:06am UTC](https://forum.p4.org/t/how-to-implement-softwared-p4-switch-in-pc-or-general-server-to-achieve-better-performance/698/6 "2023-03-29T02:06:01Z")

</div>

I kind of get it.  
Although I used wireshark to try to capture the physical network card, it clearly showed.

> [@DavideS](#):
>
> Because usually the packets are not sent at usespace (where the bmv2 switch runs) so bascally they are not forwarded by the bmv2.

the bmv2 should be on a different space from the physical NIC. (The bmv2 in user space cannot connect to the physical NIC in kernel space.)  
maybe bind veth and physical port with cmd as follw:

> sudo ip link set dev nsp2s0 master veth1

![image](https://us1.discourse-cdn.com/flex019/uploads/p4/original/1X/512c4584b8d3cc6f279655b8ab753db9d1dce1a1.png)  
Physical NIC enp2s0 reaches the bmv2 software switch through veth1,Is it right?  
I will continue to collect some information and have a try. Thank you very much for your reply.

---

<div class="post-metadata">

**Author:** ![andyfingerhut](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/andyfingerhut/32/17_2.png) [@andyfingerhut](https://forum.p4.org/u/andyfingerhut)\
**Post date:** [March 29, 2023, 3:11am UTC](https://forum.p4.org/t/how-to-implement-softwared-p4-switch-in-pc-or-general-server-to-achieve-better-performance/698/7 "2023-03-29T03:11:52Z")

</div>

I have not done it before, but if you run the `simple_switch` or `simple_switch_grpc` processes as root, you should be able to specify physical ethernet interface names on the command line, or veth interface names, to bind to numbers that are the numeric ids that the P4 program uses to identify the input port or target output port.

I know for sure you can do this with veth ports, as I do it frequently. Whatever network namespace you run the `simple_switch_grpc` process in, those interface names must be visible in that network namespace.

---

<div class="post-metadata">

**Author:** ![JYT](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@JYT](https://forum.p4.org/u/JYT)\
**Post date:** [March 29, 2023, 3:23am UTC](https://forum.p4.org/t/how-to-implement-softwared-p4-switch-in-pc-or-general-server-to-achieve-better-performance/698/8 "2023-03-29T03:23:33Z")

</div>

Thank you for your reply. I can’t agree with you more.

> [@DavideS](#):
>
> You should implement workaround via a vxlan among physical switch interfaces and virtual interfaces to assign to the bmv2.

Like DavideS said,vxlan will help enp2s0 which work in kernelspace connect to bmv2 software switch which work in userspace.  
I’m trying,reply latest work later.

---

<div class="post-metadata">

**Author:** ![JYT](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@JYT](https://forum.p4.org/u/JYT)\
**Post date:** [April 6, 2023, 3:53am UTC](https://forum.p4.org/t/how-to-implement-softwared-p4-switch-in-pc-or-general-server-to-achieve-better-performance/698/9 "2023-04-06T03:53:39Z")

</div>

Still failed…  
Just as a simple report and record, through the following records:  
Added a br0 where veth1 and physical NIC enp2s0 are added to br0. Add another br2, connecting veth3 and enp4s0.

```auto
# ip link add br0 type bridge
# ip link set veth1 master br0
# ip link set enp2s0 master br0
# br 2 like above.

```

arp enabled like this:

```auto
echo 2>xxx.xxx
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.enp2s0.arp_announce = 2

```

br0/2 forward setting,income from eth1,forward to enp2s0.
