# Even Odd in P4

Published on 07 October 2020 (Updated: 07 October 2020)

Welcome to the Even Odd in P4 page! Here, you'll find the source code for this program as well as a description of how the program works.

## Current Solution

/* -*- P4_16 -*- */

/*************************************************************************

This is a P4 Even Odd code:

We defined a new packet header "operation" as follow:

0                                     1
+----------------------------------+------------------------------------+
|             number           | v |                 flag               |
+----------------------------------+------------------------------------+

number = the 7 bits of the number
v      = bit 0 of the number will be used to define if it is even or odd
flag   = will be 0 if even, or 1 if odd

Let us assume that the operation header is carried over Ethernet and uses
the Ethertype 0x123 to indicate the presence of the header.

When a packet arrives to the switch it is validated the header operation.
Then it is verified the last bit of the number. Is odd if its last digit
is 1; it is even if its last digit is 0. The result will be set in flag.

@ecwolf

*************************************************************************/

#include <core.p4>
#include <v1model.p4>

const bit<16> TYPE_OPERATION = 0x123;

/*************************************************************************
*********************** H E A D E R S  ***********************************
*************************************************************************/

typedef bit<9>  egressSpec_t;

bit<16>   etherType;
}

bit<7>   number;
bit<1>   validator;
bit<8>   flag;
}

/* empty */
}

ethernet_t          ethernet;
operation_t         operation;
}

/*************************************************************************
*********************** P A R S E R  ***********************************
*************************************************************************/

parser MyParser(packet_in packet,

state start {
transition parse_ethernet;
}

state parse_ethernet {
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
TYPE_OPERATION: parse_operation;
default: accept;
}
}

state parse_operation {
packet.extract(hdr.operation);
transition accept;
}

}

/*************************************************************************
************   C H E C K S U M    V E R I F I C A T I O N   *************
*************************************************************************/

apply {  }
}

/*************************************************************************
**************  I N G R E S S   P R O C E S S I N G   *******************
*************************************************************************/

action odd(){
hdr.operation.flag = 1;
}

action even(){
hdr.operation.flag = 0;
}

table evenodd_match {
key = {
hdr.operation.validator: exact;
}
actions = {
even;
odd;
NoAction;
}

default_action = even();

const entries = {
0x1 : odd(); // match last digit with 1
}
}

apply {
evenodd_match.apply();
}
}

/*************************************************************************
****************  E G R E S S   P R O C E S S I N G   *******************
*************************************************************************/

apply {}
}

/*************************************************************************
*************   C H E C K S U M    C O M P U T A T I O N   **************
*************************************************************************/

apply {}
}

/*************************************************************************
***********************  D E P A R S E R  *******************************
*************************************************************************/

control MyDeparser(packet_out packet, in headers hdr) {
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.operation);
}
}

/*************************************************************************
***********************  S W I T C H  *******************************
*************************************************************************/

V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;

Even Odd in P4 was written by:

• Fabricio RodrÃ­guez

If you see anything you'd like to change or update, please consider contributing.

## How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

## How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.