-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathipsec.p4
290 lines (247 loc) · 5.3 KB
/
ipsec.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
Copyright 2023 Intel Corporation
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.
*/
#include <core.p4>
#include <dpdk/pna.p4>
#define inbound(meta) (istd.direction == PNA_Direction_t.NET_TO_HOST)
#define outbound(meta) (istd.direction == PNA_Direction_t.HOST_TO_NET)
//
// Packet headers.
//
header ethernet_t {
bit<48> dst_addr;
bit<48> src_addr;
bit<16> ether_type;
}
#define ETHERNET_ETHERTYPE_IPV4 0x0800
header ipv4_t {
bit<4> version;
bit<4> ihl;
bit<6> dscp;
bit<2> ecn;
bit<16> total_length;
bit<16> identification;
bit<3> flags;
bit<13> fragment_offset;
bit<8> ttl;
bit<8> protocol;
bit<16> header_checksum;
bit<32> src_addr;
bit<32> dst_addr;
}
#define IP_PROTOCOL_ESP 0x32
header esp_t {
bit<32> spi;
bit<32> seq;
}
struct headers_t {
ethernet_t ethernet;
ipv4_t ipv4;
esp_t esp;
}
//
// Packet metadata.
//
struct metadata_t {
bit<32> next_hop_id;
bool bypass;
}
//
// Extern objects.
//
ipsec_accelerator() ipsec;
//
// Parser.
//
parser MainParserImpl(
packet_in pkt,
out headers_t hdrs,
inout metadata_t meta,
in pna_main_parser_input_metadata_t istd)
{
bool from_ipsec;
ipsec_status status;
state start {
from_ipsec = ipsec.from_ipsec(status);
transition select(from_ipsec) {
true : parse_ipv4;
default : parse_ethernet;
}
}
state parse_ethernet {
pkt.extract(hdrs.ethernet);
transition select(hdrs.ethernet.ether_type) {
ETHERNET_ETHERTYPE_IPV4 : parse_ipv4;
default : accept;
}
}
state parse_ipv4 {
pkt.extract(hdrs.ipv4);
transition select(hdrs.ipv4.protocol) {
IP_PROTOCOL_ESP : parse_esp;
default : accept;
}
}
state parse_esp {
pkt.extract(hdrs.esp);
transition accept;
}
}
control PreControlImpl(
in headers_t hdr,
inout metadata_t meta,
in pna_pre_input_metadata_t istd,
inout pna_pre_output_metadata_t ostd)
{
apply {
}
}
//
// Control block.
//
control MainControlImpl(
inout headers_t hdrs,
inout metadata_t meta,
in pna_main_input_metadata_t istd,
inout pna_main_output_metadata_t ostd)
{
action ipsec_enable(bit<32> sa_index) {
ipsec.enable();
ipsec.set_sa_index<bit<32>>(sa_index);
hdrs.ethernet.setInvalid();
}
action ipsec_bypass() {
meta.bypass = true;
ipsec.disable();
}
action drop() {
drop_packet();
}
table inbound_table {
key = {
hdrs.ipv4.src_addr : exact;
hdrs.ipv4.dst_addr : exact;
hdrs.esp.spi : exact;
}
actions = {
ipsec_enable;
ipsec_bypass;
drop;
}
const default_action = drop;
}
table outbound_table {
key = {
hdrs.ipv4.src_addr : exact;
hdrs.ipv4.dst_addr : exact;
}
actions = {
ipsec_enable;
ipsec_bypass;
drop;
}
default_action = ipsec_bypass();
}
action next_hop_id_set(bit<32> next_hop_id) {
meta.next_hop_id = next_hop_id;
}
table routing_table {
key = {
hdrs.ipv4.dst_addr : lpm;
}
actions = {
next_hop_id_set;
drop;
}
default_action = next_hop_id_set(0);
}
action next_hop_set(bit<48> dst_addr, bit<48> src_addr, bit<16> ether_type, bit<32> port_id) {
hdrs.ethernet.setValid();
hdrs.ethernet.dst_addr = dst_addr;
hdrs.ethernet.src_addr = src_addr;
hdrs.ethernet.ether_type = ether_type;
send_to_port((PortId_t)port_id);
}
table next_hop_table {
key = {
meta.next_hop_id : exact;
}
actions = {
next_hop_set;
drop;
}
default_action = drop;
}
apply {
if (inbound(meta)) {
ipsec_status status;
if (!ipsec.from_ipsec(status)) {
// Pre-decrypt processing.
if (hdrs.ipv4.isValid()) {
if (hdrs.esp.isValid()) {
inbound_table.apply();
if (meta.bypass) {
routing_table.apply();
next_hop_table.apply();
}
} else {
routing_table.apply();
next_hop_table.apply();
}
} else
drop_packet();
} else {
// Post-decrypt processing.
if (status == ipsec_status.IPSEC_SUCCESS) {
routing_table.apply();
next_hop_table.apply();
} else
drop_packet();
}
} else {
ipsec_status status;
if (!ipsec.from_ipsec(status)) {
// Pre-encrypt processing.
if (hdrs.ipv4.isValid()) {
outbound_table.apply();
} else
drop_packet();
} else {
// Post-encrypt processing.
if (status == ipsec_status.IPSEC_SUCCESS) {
routing_table.apply();
next_hop_table.apply();
} else
drop_packet();
}
}
}
}
//
// Deparser.
//
control MainDeparserImpl(
packet_out pkt,
in headers_t hdrs,
in metadata_t meta,
in pna_main_output_metadata_t ostd)
{
apply {
pkt.emit(hdrs.ethernet);
pkt.emit(hdrs.ipv4);
pkt.emit(hdrs.esp);
}
}
//
// Package.
//
PNA_NIC(MainParserImpl(), PreControlImpl(), MainControlImpl(), MainDeparserImpl()) main;