Using Linux (Ubuntu) VM as a P4 software switch

[topology]

 

H1-----S1-----H2

 

H1, H2, and S1 are three ubuntu linux virtual machines.

 

The network setting for H1.

 

The network setting for H2.

 

The network setting for S1. (Network Adaptor 2 is for LAN Segment 1. Network Adaptor 3 is for LAN Segment 2). So that the ens38 of s1 is connected to h1 and the ens39 of s1 is connected to h2. Also, please install BMV2 and p4c in s1.

 

Before running the P4 software switch in s1, h1 cannot ping h2

 

[basic.p4]

/* -*- P4_16 -*- */

#include <core.p4>

#include <v1model.p4>

/*************************************************************************

*********************** H E A D E R S  ***********************************

*************************************************************************/

 

struct metadata {

    /* empty */

}

 

struct headers {

}

 

/*************************************************************************

*********************** P A R S E R  ***********************************

*************************************************************************/

parser MyParser(packet_in packet,

                out headers hdr,

                inout metadata meta,

                inout standard_metadata_t standard_metadata) {

    state start {

        transition accept;

    }

}

 

/*************************************************************************

************   C H E C K S U M    V E R I F I C A T I O N   *************

*************************************************************************/

 

control MyVerifyChecksum(inout headers hdr, inout metadata meta) { 

    apply {  }

}

/*************************************************************************

**************  I N G R E S S   P R O C E S S I N G   *******************

*************************************************************************/

control MyIngress(inout headers hdr,

                  inout metadata meta,

                  inout standard_metadata_t standard_metadata) {

    action drop() {

        mark_to_drop(standard_metadata);

    }

    action forward(bit<9> port) {

        standard_metadata.egress_spec = port;

    }

    table phy_forward {

        key = {

            standard_metadata.ingress_port: exact;

        }

        actions = {

            forward;

            drop;

        }

        size = 1024;

        default_action = drop();

    }

    apply {

        phy_forward.apply();

    }

}

/*************************************************************************

****************  E G R E S S   P R O C E S S I N G   *******************

*************************************************************************/

control MyEgress(inout headers hdr,

                 inout metadata meta,

                 inout standard_metadata_t standard_metadata) {

    apply {  }

}

/*************************************************************************

*************   C H E C K S U M    C O M P U T A T I O N   **************

*************************************************************************/

control MyComputeChecksum(inout headers  hdr, inout metadata meta) {

     apply {

    }

}

/*************************************************************************

***********************  D E P A R S E R  *******************************

*************************************************************************/

control MyDeparser(packet_out packet, in headers hdr) {

    apply {

    }

}

/*************************************************************************

***********************  S W I T C H  *******************************

*************************************************************************/

V1Switch(

MyParser(),

MyVerifyChecksum(),

MyIngress(),

MyEgress(),

MyComputeChecksum(),

MyDeparser()

) main;

 

[cmd.txt]

table_add phy_forward forward 1 => 2

table_add phy_forward forward 2 => 1

 

In S1, compile the p4 program

 

Run the p4 software switch and setting the rules.

 

Go to h1. (H1 can ping H2 now.)

 

Dr. Chih-Heng Ke

Department of Computer Science and Information Engineering, National Quemoy University, Kinmen, Taiwan

Email: smallko@gmail.com