p4 learning-l2 basic forwarding

/* -*- P4_16 -*- */
#include <core.p4>
#include <v1model.p4>

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

//TODO 1: Define ethernet header, metadata and headers struct
struct ethernet_t {
	bit<48> dstAddr;
	bit<48> srcAddr;
	bit<8>  ethernetType;
}

struct metadata {
	
}

struct headers {
	ethernet_t ethernet;
}

/*************************************************************************
*********************** 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 {
        //TODO 2: parse ethernet header
		packet.extract(hdr.ethernet);
        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);
    }

    //TODO 4: define an action to set the egress port
    action set_egress_port(bit<9> port) {
	    standard_metadata.egress_spec = port;
	}
    //TODO 3: define a l2 forwarding table and define a match to set the egress port
    table l2_forward {
		key = {
			hdr.ethernet.dstAddr;
		}
		actions = {
			set_egress_port;
			NoAction;
		}
		size = 512;
		default_action = NoAction();
	}
    apply {
        //TODO 5: call the forwarding table
		l2_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 {
        //TODO 6: deparse ethernet header
		packet.emit(hdr.ethernet);
    }
}

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

//switch architecture
V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;

  

posted @ 2022-05-18 17:03  gaoren  阅读(67)  评论(0编辑  收藏  举报