-
Notifications
You must be signed in to change notification settings - Fork 445
/
port-forwarding.p4
92 lines (73 loc) · 1.79 KB
/
port-forwarding.p4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <core.p4>
#include <ubpf_model.p4>
typedef bit<48> EthernetAddress;
typedef bit<32> IPv4Address;
#define IPV4_ETHTYPE 0x800
// standard Ethernet header
header Ethernet_h
{
EthernetAddress dstAddr;
EthernetAddress srcAddr;
bit<16> etherType;
}
// IPv4 header without options
header IPv4_h {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> totalLen;
bit<16> identification;
bit<3> flags;
bit<13> fragOffset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdrChecksum;
IPv4Address srcAddr;
IPv4Address dstAddr;
}
struct Headers_t {
Ethernet_h ethernet;
IPv4_h ipv4;
}
struct metadata {}
parser prs(packet_in packet, out Headers_t hdr, inout metadata meta, inout standard_metadata std_meta) {
state start {
transition parse_ethernet;
}
state parse_ethernet {
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
IPV4_ETHTYPE: parse_ipv4;
default: accept;
}
}
state parse_ipv4 {
packet.extract(hdr.ipv4);
transition accept;
}
}
control pipe(inout Headers_t hdr, inout metadata meta, inout standard_metadata std_meta) {
action mod_nw_tos(bit<32> out_port) {
hdr.ipv4.diffserv = 12;
std_meta.output_action = ubpf_action.REDIRECT;
std_meta.output_port = out_port;
}
table test_tbl {
key = {
std_meta.input_port : exact;
}
actions = {
mod_nw_tos;
}
}
apply {
test_tbl.apply();
}
}
control dprs(packet_out packet, in Headers_t hdr) {
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.ipv4);
}
}
ubpf(prs(), pipe(), dprs()) main;