Home | History | Annotate | Download | only in testprograms
      1 // Routes a packet to an interface based on its IPv4 address
      2 // Maintains a set of counters on the routing table
      3 
      4 header_type ethernet_t {
      5     fields {
      6         dstAddr : 48;
      7         srcAddr : 48;
      8         etherType : 16;
      9     }
     10 }
     11 
     12 header_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 
     29 parser start {
     30     return parse_ethernet;
     31 }
     32 
     33 header ethernet_t ethernet;
     34 
     35 parser parse_ethernet {
     36     extract(ethernet);
     37     return select(latest.etherType) {
     38         0x800 : parse_ipv4;
     39         default: ingress;
     40     }
     41 }
     42 
     43 action nop() 
     44 {}
     45 
     46 action forward(port)
     47 {
     48    modify_field(standard_metadata.egress_port, port);
     49 }
     50 
     51 header ipv4_t ipv4;
     52 
     53 parser parse_ipv4 {
     54     extract(ipv4);
     55     return ingress;
     56 }
     57 
     58 table routing {
     59    reads {
     60       ipv4.dstAddr: exact;
     61    }
     62    actions { nop; forward; }
     63    size : 512;
     64 }
     65 
     66 counter cnt {
     67    type: bytes;
     68    direct: routing;
     69 }
     70 
     71 control ingress
     72 {
     73     apply(routing);
     74 }