P4交换机实现
#include <core.p4> #include <v1model.p4> const bit<16> TYPE_IPV4 = 0x800; const bit<16> TYPE_VLAN = 0x8100; #define MAX_VLAN_TAG_NUM 2 const bit<16> L2_LEARN_ETHER_TYPE = 0x1234; /************************************************************************* *********************** H E A D E R S *********************************** *************************************************************************/ typedef bit<9> egressSpec_t; typedef bit<48> macAddr_t; typedef bit<32> ip4Addr_t; header ethernet_t { macAddr_t dstAddr; macAddr_t srcAddr; bit<16> etherType; } header vlan_tag_t { bit<3> prio; bit<1> cfi; bit<12> vid; bit<16> etherType; } header ipv4_t { bit<4> version; bit<4> ihl; bit<8> diffserv; bit<16> totalLen; bit<16> identification; bit<3> flags; bit<13> fragOffset; bit<8> ttl; bit<8> protocol; bit<16> hdrChecksum; ip4Addr_t srcAddr; ip4Addr_t dstAddr; } struct metadata { /* empty */ bit<9> ingress_port; bit<12> vid; bit<1> rmac_hit; bit<1> l2_hit; } struct port_metadata { bit<4> type; } header cpu_t { bit<48> srcAddr; // 源 MAC 地址 bit<16> ingress_port; // 交换机的接收端口 } struct headers { ethernet_t ethernet; cpu_t cpu; vlan_tag_t[MAX_VLAN_TAG_NUM] vlan_tag; ipv4_t ipv4; } /************************************************************************* *********************** 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: add parser logic */ transition parse_ethernet; } state parse_ethernet { packet.extract(hdr.ethernet); transition select(hdr.ethernet.etherType) { TYPE_VLAN : parse_vlan_tag; TYPE_IPV4 : parse_ipv4; default : accept; } } state parse_vlan_tag { packet.extract(hdr.vlan_tag.next); transition select(hdr.vlan_tag.last.etherType) { TYPE_VLAN : parse_vlan_tag; TYPE_IPV4 : parse_ipv4; default : accept; } } state parse_ipv4 { packet.extract(hdr.ipv4); 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 ipv4_forward(macAddr_t dstAddr, egressSpec_t port) { standard_metadata.egress_spec = port; hdr.ethernet.srcAddr = hdr.ethernet.dstAddr; hdr.ethernet.dstAddr = dstAddr; hdr.ipv4.ttl = hdr.ipv4.ttl - 1; } action l2_forward(bit<9> egress_port) { standard_metadata.egress_spec = egress_port; meta.l2_hit = 1; } action l2_miss() { meta.l2_hit = 1; } action set_mcast_group(bit<16> mcast_grp) { standard_metadata.mcast_grp = mcast_grp; } action mac_learn() { meta.ingress_port = standard_metadata.ingress_port; //clone3(CloneType.I2E, 100, meta); } action set_default_vid(bit<12> vid) { meta.vid = vid; } action set_vlan(bit<12> vid) { meta.vid = vid; } action set_rmac_hit() { meta.rmac_hit = 0; } table ipv4_lpm { key = { hdr.ipv4.dstAddr: lpm; } actions = { ipv4_forward; drop; NoAction; } size = 1024; default_action = NoAction(); } table rmac { key = { hdr.ethernet.dstAddr:ternary; } actions = { set_rmac_hit; NoAction; } size = 512; default_action = NoAction(); } table broadcast { key = { meta.vid:exact; } actions = { set_mcast_group; NoAction; } size = 1024; default_action = NoAction(); } table dmac { key = { hdr.ethernet.dstAddr:exact; meta.vid:exact; } actions = { l2_forward; l2_miss; NoAction; } size = 1024; default_action = NoAction(); } table smac { key = { hdr.ethernet.srcAddr:exact; meta.vid:exact; } actions = { mac_learn; NoAction; } size = 1024; default_action = mac_learn; } table vlan { key = { hdr.vlan_tag[0].vid:exact; } actions = { set_vlan; drop; NoAction; } size = 512; default_action = NoAction(); } table port { key = { standard_metadata.ingress_port:exact; } actions = { set_default_vid; NoAction; } size = 128; default_action = NoAction(); } apply { port.apply(); if (hdr.ethernet.isValid()) { /*报文携带vlan时,使用报文vid*/ if (hdr.vlan_tag[0].isValid()) { vlan.apply(); } smac.apply(); rmac.apply(); if (meta.rmac_hit == 1) { ipv4_lpm.apply(); } else { dmac.apply(); if (meta.l2_hit == 1) { broadcast.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 { if (standard_metadata.instance_type == 1){ hdr.cpu.setValid(); hdr.cpu.srcAddr = hdr.ethernet.srcAddr; hdr.cpu.ingress_port = (bit<16>)meta.ingress_port; hdr.ethernet.etherType = L2_LEARN_ETHER_TYPE; truncate((bit<32>)22); } } } /************************************************************************* ************* 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 { update_checksum( hdr.ipv4.isValid(), { hdr.ipv4.version, hdr.ipv4.ihl, hdr.ipv4.diffserv, hdr.ipv4.totalLen, hdr.ipv4.identification, hdr.ipv4.flags, hdr.ipv4.fragOffset, hdr.ipv4.ttl, hdr.ipv4.protocol, hdr.ipv4.srcAddr, hdr.ipv4.dstAddr }, hdr.ipv4.hdrChecksum, HashAlgorithm.csum16); } } /************************************************************************* *********************** D E P A R S E R ******************************* *************************************************************************/ control MyDeparser(packet_out packet, in headers hdr) { apply { packet.emit(hdr.ethernet); packet.emit(hdr.cpu); packet.emit(hdr.ipv4); } } /************************************************************************* *********************** S W I T C H ******************************* *************************************************************************/ V1Switch( MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser() ) main;