@@ -7,11 +7,14 @@ const bit<16> TYPE_IPV4 = 0x800;
77
88/* ************************************************************************
99*********************** H E A D E R S ***********************************
10+ * This program skeleton defines minimal Ethernet and IPv4 headers and *
11+ * a simple LPM (Longest-Prefix Match) IPv4 forwarding pipeline. *
12+ * The exercise intentionally leaves TODOs for learners to implement. *
1013*************************************************************************/
1114
12- typedef bit <9 > egressSpec_t ;
13- typedef bit <48 > macAddr_t ;
14- typedef bit <32 > ip4Addr_t ;
15+ typedef bit <9 > egressSpec_t ; // Standard BMv2 uses 9 bits for egress_spec
16+ typedef bit <48 > macAddr_t ; // Ethernet MAC address
17+ typedef bit <32 > ip4Addr_t ; // IPv4 address
1518
1619header ethernet_t {
1720 macAddr_t dstAddr ;
@@ -44,7 +47,14 @@ struct headers {
4447}
4548
4649/* ************************************************************************
47- *********************** P A R S E R ***********************************
50+ *********************** P A R S E R *************************************
51+ * New to P4? A typical parser does this:
52+ * start -> parse_ethernet
53+ * parse_ethernet:
54+ * if etherType == TYPE_IPV4 -> parse_ipv4
55+ * else accept
56+ * parse_ipv4 -> accept
57+ * This skeleton leaves the actual states as a TODO to implement later. *
4858*************************************************************************/
4959
5060parser MyParser (packet_in packet ,
@@ -53,7 +63,12 @@ parser MyParser(packet_in packet,
5363 inout standard_metadata_t standard_metadata ) {
5464
5565 state start {
56- /* TODO: add parser logic */
66+ /* TODO: add parser logic
67+ * Suggested outline:
68+ * 1) Extract Ethernet: packet.extract(hdr.ethernet);
69+ * 2) If hdr.ethernet.etherType == TYPE_IPV4 -> parse IPv4
70+ * 3) Otherwise -> transition accept
71+ */
5772 transition accept ;
5873 }
5974}
@@ -70,31 +85,54 @@ control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
7085
7186/* ************************************************************************
7287************** I N G R E S S P R O C E S S I N G *******************
88+ * High-level intent:
89+ * - Do an LPM lookup on IPv4 dstAddr
90+ * - On hit, call ipv4_forward(next-hop MAC, output port)
91+ * - Otherwise, drop or NoAction (as configured) *
7392*************************************************************************/
7493
7594control MyIngress (inout headers hdr ,
7695 inout metadata meta ,
7796 inout standard_metadata_t standard_metadata ) {
97+
7898 action drop () {
7999 mark_to_drop (standard_metadata );
80100 }
81101
102+ /* ********************************************************************
103+ * NOTE FOR NEW READERS:
104+ * 'ipv4_forward(dstAddr, port)' is invoked by table 'ipv4_lpm'.
105+ *
106+ * The values for 'dstAddr' and 'port' are *action data* supplied by
107+ * the control plane when it installs entries in 'ipv4_lpm'.
108+ *
109+ * They mean:
110+ * - dstAddr => Ethernet destination MAC for the next hop
111+ * - port => output port (ultimately written to standard_metadata.egress_spec)
112+ *
113+ * Example (BMv2 simple_switch_CLI):
114+ * table_add ipv4_lpm ipv4_forward 10.0.1.1/32 => 00:00:00:00:01:00 1
115+ * which passes MAC=00:00:00:00:01:00 and PORT=1 as action parameters
116+ * into ipv4_forward(dstAddr, port).
117+ *********************************************************************/
82118 action ipv4_forward (macAddr_t dstAddr , egressSpec_t port ) {
83119 /*
84120 Action function for forwarding IPv4 packets.
85121
86- This function is responsible for forwarding IPv4 packets to the specified
87- destination MAC address and egress port.
88-
89- Parameters:
90- - dstAddr: Destination MAC address of the packet.
91- - port: Egress port where the packet should be forwarded.
92-
93- TODO: Implement the logic for forwarding the IPv4 packet based on the
94- destination MAC address and egress port.
122+ TODO: Implement the forwarding steps, for example:
123+ - standard_metadata.egress_spec = port;
124+ - hdr.ethernet.dstAddr = dstAddr;
125+ - (optionally) set hdr.ethernet.srcAddr to the switch MAC for 'port'
126+ - adjust IPv4 TTL and checksums as needed
95127 */
96128 }
97129
130+ /* ********************************************************************
131+ * LPM table for IPv4:
132+ * - Matches on hdr.ipv4.dstAddr using longest-prefix match (lpm)
133+ * - On hit, calls ipv4_forward with *action data* populated by the
134+ * control plane when it installs the table entry.
135+ *********************************************************************/
98136 table ipv4_lpm {
99137 key = {
100138 hdr .ipv4 .dstAddr : lpm ;
@@ -110,14 +148,17 @@ control MyIngress(inout headers hdr,
110148
111149 apply {
112150 /* TODO: fix ingress control logic
113- * - ipv4_lpm should be applied only when IPv4 header is valid
151+ * - Good practice: apply ipv4_lpm only when the IPv4 header is valid, e.g.:
152+ * if (hdr.ipv4.isValid()) { ipv4_lpm.apply(); }
153+ * This skeleton currently applies unconditionally for the exercise.
114154 */
115155 ipv4_lpm .apply ();
116156 }
117157}
118158
119159/* ************************************************************************
120160**************** E G R E S S P R O C E S S I N G *******************
161+ * Often used for queue marks, mirroring, or post-routing edits. *
121162*************************************************************************/
122163
123164control MyEgress (inout headers hdr ,
@@ -128,6 +169,7 @@ control MyEgress(inout headers hdr,
128169
129170/* ************************************************************************
130171************* C H E C K S U M C O M P U T A T I O N **************
172+ * This block shows how to compute IPv4 header checksum when needed. *
131173*************************************************************************/
132174
133175control MyComputeChecksum (inout headers hdr , inout metadata meta ) {
@@ -153,28 +195,22 @@ control MyComputeChecksum(inout headers hdr, inout metadata meta) {
153195
154196/* ************************************************************************
155197*********************** D E P A R S E R *******************************
198+ * The deparser serializes headers back onto the packet in order. *
156199*************************************************************************/
157200
158201control MyDeparser (packet_out packet , in headers hdr ) {
159202 apply {
160203 /*
161- Control function for deparser.
162-
163- This function is responsible for constructing the output packet by appending
164- headers to it based on the input headers.
165-
166- Parameters:
167- - packet: Output packet to be constructed.
168- - hdr: Input headers to be added to the output packet.
169-
170- TODO: Implement the logic for constructing the output packet by appending
171- headers based on the input headers.
204+ Typical implementation (left as a TODO for learners):
205+ packet.emit(hdr.ethernet);
206+ packet.emit(hdr.ipv4); // per P4_16 spec, emit appends a header
207+ // only if it is valid; no 'if' needed.
172208 */
173209 }
174210}
175211
176212/* ************************************************************************
177- *********************** S W I T C H *******************************
213+ *********************** S W I T C H ***********************************
178214*************************************************************************/
179215
180216V1Switch (
0 commit comments