P4语言——链路层实战

参考文章

实现简单的交换机

文件在03-L2_Basic_forwarding
在这里插入图片描述

编写p4文件

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

/*************************************************************************
*********************** H E A D E R S  ***********************************
*************************************************************************/
typedef bit<9> egressSpec_t;
typedef bit<48> macAddr_t;
typedef bit<16> etherType_t;
//TODO 1: Define ethernet header, metadata and headers struct
header ethernet_t {
	macAddr_t dstAddr;
	macAddr_t srcAddr;
	etherType_t etherType;
}
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 forward(egressSpec_t egress_port) {
		standard_metadata.egress_spec = egress_port;

	
	//TODO 3: define a l2 forwarding table and define an action to set the egress port
	table l2_forward_table {
		key = {
			hdr.ethernet.dstAddr: exact;
		}
		actions = {
			forward;
			NoAction;
		}
		default_action = NoAction;
	}
   	}
    apply {
        //TODO 5: call the forwarding table
		l2_forward_table.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;

编写s1-commands文件

table_add l2_forward_table forward 00:00:0a:00:00:01 => 1
table_add l2_forward_table forward 00:00:0a:00:00:02 => 2
table_add l2_forward_table forward 00:00:0a:00:00:03 => 3
table_add l2_forward_table forward 00:00:0a:00:00:04 => 4

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值