| 1 |
// https://raw.githubusercontent.com/p4lang/tutorials/master/exercises/basic/basic.p4 |
| 2 |
/* -*- P4_16 -*- */ |
| 3 |
#include <core.p4> |
| 4 |
#include <v1model.p4> |
| 5 |
|
| 6 |
const bit<16> TYPE_IPV4 = 0x800; |
| 7 |
|
| 8 |
/************************************************************************* |
| 9 |
*********************** H E A D E R S *********************************** |
| 10 |
*************************************************************************/ |
| 11 |
|
| 12 |
typedef bit<9> egressSpec_t; |
| 13 |
typedef bit<48> macAddr_t; |
| 14 |
typedef bit<32> ip4Addr_t; |
| 15 |
|
| 16 |
header ethernet_t { |
| 17 |
macAddr_t dstAddr; |
| 18 |
macAddr_t srcAddr; |
| 19 |
bit<16> etherType; |
| 20 |
} |
| 21 |
|
| 22 |
header ipv4_t { |
| 23 |
bit<4> version; |
| 24 |
bit<4> ihl; |
| 25 |
bit<8> diffserv; |
| 26 |
bit<16> totalLen; |
| 27 |
bit<16> identification; |
| 28 |
bit<3> flags; |
| 29 |
bit<13> fragOffset; |
| 30 |
bit<8> ttl; |
| 31 |
bit<8> protocol; |
| 32 |
bit<16> hdrChecksum; |
| 33 |
ip4Addr_t srcAddr; |
| 34 |
ip4Addr_t dstAddr; |
| 35 |
} |
| 36 |
|
| 37 |
struct metadata { |
| 38 |
/* empty */ |
| 39 |
} |
| 40 |
|
| 41 |
struct headers { |
| 42 |
ethernet_t ethernet; |
| 43 |
ipv4_t ipv4; |
| 44 |
} |
| 45 |
|
| 46 |
/************************************************************************* |
| 47 |
*********************** P A R S E R *********************************** |
| 48 |
*************************************************************************/ |
| 49 |
|
| 50 |
parser MyParser(packet_in packet, |
| 51 |
out headers hdr, |
| 52 |
inout metadata meta, |
| 53 |
inout standard_metadata_t standard_metadata) { |
| 54 |
|
| 55 |
state start { |
| 56 |
/* TODO: add parser logic */ |
| 57 |
transition accept; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
/************************************************************************* |
| 63 |
************ C H E C K S U M V E R I F I C A T I O N ************* |
| 64 |
*************************************************************************/ |
| 65 |
|
| 66 |
control MyVerifyChecksum(inout headers hdr, inout metadata meta) { |
| 67 |
apply { } |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
/************************************************************************* |
| 72 |
************** I N G R E S S P R O C E S S I N G ******************* |
| 73 |
*************************************************************************/ |
| 74 |
|
| 75 |
control MyIngress(inout headers hdr, |
| 76 |
inout metadata meta, |
| 77 |
inout standard_metadata_t standard_metadata) { |
| 78 |
action drop() { |
| 79 |
mark_to_drop(standard_metadata); |
| 80 |
} |
| 81 |
|
| 82 |
action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) { |
| 83 |
/* TODO: fill out code in action body */ |
| 84 |
} |
| 85 |
|
| 86 |
table ipv4_lpm { |
| 87 |
key = { |
| 88 |
hdr.ipv4.dstAddr: lpm; |
| 89 |
} |
| 90 |
actions = { |
| 91 |
ipv4_forward; |
| 92 |
drop; |
| 93 |
NoAction; |
| 94 |
} |
| 95 |
size = 1024; |
| 96 |
default_action = NoAction(); |
| 97 |
} |
| 98 |
|
| 99 |
apply { |
| 100 |
/* TODO: fix ingress control logic |
| 101 |
* - ipv4_lpm should be applied only when IPv4 header is valid |
| 102 |
*/ |
| 103 |
ipv4_lpm.apply(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/************************************************************************* |
| 108 |
**************** E G R E S S P R O C E S S I N G ******************* |
| 109 |
*************************************************************************/ |
| 110 |
|
| 111 |
control MyEgress(inout headers hdr, |
| 112 |
inout metadata meta, |
| 113 |
inout standard_metadata_t standard_metadata) { |
| 114 |
apply { } |
| 115 |
} |
| 116 |
|
| 117 |
/************************************************************************* |
| 118 |
************* C H E C K S U M C O M P U T A T I O N ************** |
| 119 |
*************************************************************************/ |
| 120 |
|
| 121 |
control MyComputeChecksum(inout headers hdr, inout metadata meta) { |
| 122 |
apply { |
| 123 |
update_checksum( |
| 124 |
hdr.ipv4.isValid(), |
| 125 |
{ hdr.ipv4.version, |
| 126 |
hdr.ipv4.ihl, |
| 127 |
hdr.ipv4.diffserv, |
| 128 |
hdr.ipv4.totalLen, |
| 129 |
hdr.ipv4.identification, |
| 130 |
hdr.ipv4.flags, |
| 131 |
hdr.ipv4.fragOffset, |
| 132 |
hdr.ipv4.ttl, |
| 133 |
hdr.ipv4.protocol, |
| 134 |
hdr.ipv4.srcAddr, |
| 135 |
hdr.ipv4.dstAddr }, |
| 136 |
hdr.ipv4.hdrChecksum, |
| 137 |
HashAlgorithm.csum16); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/************************************************************************* |
| 143 |
*********************** D E P A R S E R ******************************* |
| 144 |
*************************************************************************/ |
| 145 |
|
| 146 |
control MyDeparser(packet_out packet, in headers hdr) { |
| 147 |
apply { |
| 148 |
/* TODO: add deparser logic */ |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/************************************************************************* |
| 153 |
*********************** S W I T C H ******************************* |
| 154 |
*************************************************************************/ |
| 155 |
|
| 156 |
V1Switch( |
| 157 |
MyParser(), |
| 158 |
MyVerifyChecksum(), |
| 159 |
MyIngress(), |
| 160 |
MyEgress(), |
| 161 |
MyComputeChecksum(), |
| 162 |
MyDeparser() |
| 163 |
) main; |