[P4] basic parser
This commit is contained in:
parent
71825d21bd
commit
d20c8a0a8b
1 changed files with 72 additions and 0 deletions
72
p4src/parsers.p4
Normal file
72
p4src/parsers.p4
Normal file
|
@ -0,0 +1,72 @@
|
|||
/* -*- P4_16 -*- */
|
||||
#ifndef PARSERS_P4
|
||||
#define PARSERS_P4
|
||||
|
||||
#include <core.p4>
|
||||
#include <v1model.p4>
|
||||
|
||||
#include "headers.p4"
|
||||
|
||||
parser MyParser(packet_in packet,
|
||||
out headers hdr,
|
||||
inout metadata meta,
|
||||
inout standard_metadata_t standard_metadata) {
|
||||
|
||||
state start {
|
||||
packet.extract(hdr.ethernet);
|
||||
transition select(hdr.ethernet.ethertype){
|
||||
TYPE_IPV4: ipv4;
|
||||
TYPE_IPV6: ipv6;
|
||||
default: accept;
|
||||
}
|
||||
}
|
||||
|
||||
state ipv4 {
|
||||
packet.extract(hdr.ipv4);
|
||||
meta.tcpLength = hdr.ipv4.totalLen - 16w20;
|
||||
|
||||
transition select(hdr.ipv4.protocol){
|
||||
TYPE_TCP: tcp;
|
||||
default: accept;
|
||||
}
|
||||
}
|
||||
|
||||
state ipv6 {
|
||||
packet.extract(hdr.ipv6);
|
||||
meta.tcpLength = hdr.ipv6.payload_length;
|
||||
|
||||
transition select(hdr.ipv6.next_header){
|
||||
TYPE_TCP: tcp;
|
||||
default: accept;
|
||||
}
|
||||
}
|
||||
|
||||
state tcp {
|
||||
packet.extract(hdr.tcp);
|
||||
transition accept;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
************************ D E P A R S E R *******************************
|
||||
*************************************************************************/
|
||||
|
||||
control MyDeparser(packet_out packet, in headers hdr) {
|
||||
apply {
|
||||
packet.emit(hdr.ethernet);
|
||||
|
||||
/* either */
|
||||
packet.emit(hdr.ipv4);
|
||||
packet.emit(hdr.ipv6);
|
||||
|
||||
/* either */
|
||||
packet.emit(hdr.tcp);
|
||||
// packet.emit(hdr.udp);
|
||||
// packet.emit(hdr.icmp);
|
||||
// packet.emit(hdr.icmpv6);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue