1// Routes a packet to an interface based on its IPv4 address 2// Maintains a set of counters on the routing table 3 4header_type ethernet_t { 5 fields { 6 dstAddr : 48; 7 srcAddr : 48; 8 etherType : 16; 9 } 10} 11 12header_type ipv4_t { 13 fields { 14 version : 4; 15 ihl : 4; 16 diffserv : 8; 17 totalLen : 16; 18 identification : 16; 19 flags : 3; 20 fragOffset : 13; 21 ttl : 8; 22 protocol : 8; 23 hdrChecksum : 16; 24 srcAddr : 32; 25 dstAddr: 32; 26 } 27} 28 29parser start { 30 return parse_ethernet; 31} 32 33header ethernet_t ethernet; 34 35parser parse_ethernet { 36 extract(ethernet); 37 return select(latest.etherType) { 38 0x800 : parse_ipv4; 39 default: ingress; 40 } 41} 42 43action nop() 44{} 45 46action forward(port) 47{ 48 modify_field(standard_metadata.egress_port, port); 49} 50 51header ipv4_t ipv4; 52 53parser parse_ipv4 { 54 extract(ipv4); 55 return ingress; 56} 57 58table routing { 59 reads { 60 ipv4.dstAddr: exact; 61 } 62 actions { nop; forward; } 63 size : 512; 64} 65 66counter cnt { 67 type: bytes; 68 direct: routing; 69} 70 71control ingress 72{ 73 apply(routing); 74}