-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipv6-switch-ml-bmv2.p4
173 lines (159 loc) · 4.77 KB
/
ipv6-switch-ml-bmv2.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright 2019, MNK Consulting
* http://mnkcg.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* How to test this P4 code:
* $ ./p4c-bm2-ss --std p4-16 ../testdata/p4_16_samples/ipv6-switch-ml-bmv2.p4 -o tmp.json
*
*/
#include <v1model.p4>
#include "ml-headers.p4"
const bit<32> BMV2_V1MODEL_INSTANCE_TYPE_REPLICATION = 5;
#define IS_REPLICATED(std_meta) (std_meta.instance_type == BMV2_V1MODEL_INSTANCE_TYPE_REPLICATION)
parser MyParser(packet_in packet, out headers hdr, inout metadata_t meta,
inout standard_metadata_t standard_metadata) {
state start {
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
TYPE_IPV6: ipv6;
default: accept;
}
}
state ipv6 {
packet.extract(hdr.ipv6);
transition select(hdr.ipv6.nextHdr) {
PROTO_UDP: parse_udp;
PROTO_ICMP6: icmp6;
default: accept;
}
}
state icmp6 {
packet.extract(hdr.icmp6);
transition accept;
}
state parse_udp {
packet.extract(hdr.udp);
transition parse_ml;
}
state parse_ml {
packet.extract(hdr.ml);
transition parse_ml_vector;
}
state parse_ml_vector {
packet.extract(hdr.vector.next);
transition select(hdr.vector.last.e) {
_: accept;
}
}
}
// Our switch table comprises of IPv6 addresses vs. egress_port.
// This is the table we setup here.
control ingress(inout headers hdr, inout metadata_t meta,
inout standard_metadata_t standard_metadata) {
aggregator_t[S] pool;
count_t[S] count;
action set_mcast_grp(bit<16> mcast_grp, bit<9> port) {
standard_metadata.mcast_grp = mcast_grp;
meta.egress_port = port;
}
table ipv6_tbl {
key = {
(hdr.ipv6.dstAddr[127:120] == 8w0xff) : exact @name("mcast_key");
}
actions = {set_mcast_grp;}
}
/* TODO: The paper does not mention what IP (unicast or multicast is a
* worker update sent to the switch. Once it's known, this table should
* be activated.
table ipv6_rx {
key = {
(hdr.ipv6.srcAddr[127:120] == 8w0xff) : exact @name("rx_key");
}
actions = {set_mcast_grp;}
}
*/
apply {
if (hdr.ipv6.isValid()) {
ipv6_tbl.apply();
}
/*
* A SwitchML switch provides a pool of s integer aggregators, addressable
* by index.
*/
// TODO: vector addition - P4 has no for loop.
// Need to implement later. Hard-coding
// first element in vector to compile code.
pool[hdr.ml.idx].val = pool[hdr.ml.idx].val + hdr.vector[0].e;
count[hdr.ml.idx].cnt = count[hdr.ml.idx].cnt + 1;
if (count[hdr.ml.idx].cnt == N) {
hdr.vector[0].e = pool[hdr.ml.idx].val;
pool[hdr.ml.idx] = {0};
count[hdr.ml.idx] = {0};
// multicast packet occurs in egress control.
} else
mark_to_drop(standard_metadata);
}
}
control egress(inout headers hdr, inout metadata_t meta,
inout standard_metadata_t standard_metadata) {
action set_out_bd (bit<24> bd) {
meta.fwd.out_bd = bd;
}
table get_multicast_copy_out_bd {
key = {
standard_metadata.mcast_grp : exact;
standard_metadata.egress_rid : exact;
}
actions = { set_out_bd; }
}
action drop() {
mark_to_drop(standard_metadata);
}
action rewrite_mac(bit<48> smac) {
hdr.ethernet.srcAddr = smac;
}
table send_frame {
key = {
meta.fwd.out_bd: exact;
}
actions = {rewrite_mac; drop;}
default_action = drop;
}
apply {
if (IS_REPLICATED(standard_metadata)) {
get_multicast_copy_out_bd.apply();
send_frame.apply();
}
}
}
control MyDeparser(packet_out packet, in headers hdr) {
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.ipv6);
packet.emit(hdr.icmp6);
packet.emit(hdr.udp);
}
}
control MyVerifyChecksum(inout headers hdr, inout metadata_t meta) {
apply {
}
}
control MyComputeChecksum(inout headers hdr, inout metadata_t meta) {
apply {
}
}
V1Switch(MyParser(), MyVerifyChecksum(), ingress(), egress(),
MyComputeChecksum(), MyDeparser()) main;