-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathsrv6.py
509 lines (426 loc) · 16.4 KB
/
srv6.py
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# Copyright 2019-present Open Networking Foundation
#
# 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.
#
# ------------------------------------------------------------------------------
# SRV6 TESTS
#
# To run all tests:
# make p4-test TEST=srv6
#
# To run a specific test case:
# make p4-test TEST=srv6.<TEST CLASS NAME>
#
# For example:
# make p4-test TEST=srv6.Srv6InsertTest
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Modify everywhere you see TODO
#
# When providing your solution, make sure to use the same names for P4Runtime
# entities as specified in your P4Info file.
#
# Test cases are based on the P4 program design suggested in the exercises
# README. Make sure to modify the test cases accordingly if you decide to
# implement the pipeline differently.
# ------------------------------------------------------------------------------
from ptf.testutils import group
from base_test import *
def insert_srv6_header(pkt, sid_list):
"""Applies SRv6 insert transformation to the given packet.
"""
# Set IPv6 dst to first SID...
pkt[IPv6].dst = sid_list[0]
# Insert SRv6 header between IPv6 header and payload
sid_len = len(sid_list)
srv6_hdr = IPv6ExtHdrSegmentRouting(
nh=pkt[IPv6].nh,
addresses=sid_list[::-1],
len=sid_len * 2,
segleft=sid_len - 1,
lastentry=sid_len - 1)
pkt[IPv6].nh = 43 # next IPv6 header is SR header
pkt[IPv6].payload = srv6_hdr / pkt[IPv6].payload
return pkt
def pop_srv6_header(pkt):
"""Removes SRv6 header from the given packet.
"""
pkt[IPv6].nh = pkt[IPv6ExtHdrSegmentRouting].nh
pkt[IPv6].payload = pkt[IPv6ExtHdrSegmentRouting].payload
def set_cksum(pkt, cksum):
if TCP in pkt:
pkt[TCP].chksum = cksum
if UDP in pkt:
pkt[UDP].chksum = cksum
if ICMPv6Unknown in pkt:
pkt[ICMPv6Unknown].cksum = cksum
@group("srv6")
class Srv6InsertTest(P4RuntimeTest):
"""Tests SRv6 insert behavior, where the switch receives an IPv6 packet and
inserts the SRv6 header
"""
def runTest(self):
sid_lists = (
[SWITCH2_IPV6, SWITCH3_IPV6, HOST2_IPV6],
[SWITCH2_IPV6, HOST2_IPV6],
)
next_hop_mac = SWITCH2_MAC
for sid_list in sid_lists:
for pkt_type in ["tcpv6", "udpv6", "icmpv6"]:
print_inline("%s %d SIDs ... " % (pkt_type, len(sid_list)))
pkt = getattr(testutils, "simple_%s_packet" % pkt_type)()
self.testPacket(pkt, sid_list, next_hop_mac)
@autocleanup
def testPacket(self, pkt, sid_list, next_hop_mac):
# *** TODO EXERCISE 6
# Modify names to match content of P4Info file (look for the fully
# qualified name of tables, match fields, and actions.
# ---- START SOLUTION ----
# Add entry to "My Station" table. Consider the given pkt's eth dst addr
# as myStationMac address.
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# Exact match.
"MODIFY ME": pkt[Ether].dst
},
action_name="NoAction"
))
# Insert SRv6 header when matching the pkt's IPV6 dst addr.
# Action name an params are generated based on the number of SIDs given.
# For example, with 2 SIDs:
# action_name = IngressPipeImpl.srv6_t_insert_2
# action_params = {
# "s1": sid[0],
# "s2": sid[1]
# }
sid_len = len(sid_list)
action_name = "IngressPipeImpl.srv6_t_insert_%d" % sid_len
actions_params = {"s%d" % (x + 1): sid_list[x] for x in range(sid_len)}
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# LPM match (value, prefix)
"MODIFY ME": (pkt[IPv6].dst, 128)
},
action_name=action_name,
action_params=actions_params
))
# Insert ECMP group with only one member (next_hop_mac)
self.insert(self.helper.build_act_prof_group(
act_prof_name="MODIFY ME",
group_id=1,
actions=[
# List of tuples (action name, {action param: value})
("MODIFY ME", {"MODIFY ME": next_hop_mac}),
]
))
# Now that we inserted the SRv6 header, we expect the pkt's IPv6 dst
# addr to be the first on the SID list.
# Match on L3 routing table.
first_sid = sid_list[0]
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# LPM match (value, prefix)
"hdr.ipv6.dst_addr": (first_sid, 128)
},
group_id=1
))
# Map next_hop_mac to output port
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# Exact match
"MODIFY ME": next_hop_mac
},
action_name="MODIFY ME",
action_params={
"MODIFY ME": self.port2
}
))
# ---- END SOLUTION ----
# Build expected packet from the given one...
exp_pkt = insert_srv6_header(pkt.copy(), sid_list)
# Route and decrement TTL
pkt_route(exp_pkt, next_hop_mac)
pkt_decrement_ttl(exp_pkt)
# Bonus: update P4 program to calculate correct checksum
set_cksum(pkt, 1)
set_cksum(exp_pkt, 1)
testutils.send_packet(self, self.port1, str(pkt))
testutils.verify_packet(self, exp_pkt, self.port2)
@group("srv6")
class Srv6TransitTest(P4RuntimeTest):
"""Tests SRv6 transit behavior, where the switch ignores the SRv6 header
and routes the packet normally, without applying any SRv6-related
modifications.
"""
def runTest(self):
my_sid = SWITCH1_IPV6
sid_lists = (
[SWITCH2_IPV6, SWITCH3_IPV6, HOST2_IPV6],
[SWITCH2_IPV6, HOST2_IPV6],
)
next_hop_mac = SWITCH2_MAC
for sid_list in sid_lists:
for pkt_type in ["tcpv6", "udpv6", "icmpv6"]:
print_inline("%s %d SIDs ... " % (pkt_type, len(sid_list)))
pkt = getattr(testutils, "simple_%s_packet" % pkt_type)()
pkt = insert_srv6_header(pkt, sid_list)
self.testPacket(pkt, next_hop_mac, my_sid)
@autocleanup
def testPacket(self, pkt, next_hop_mac, my_sid):
# *** TODO EXERCISE 6
# Modify names to match content of P4Info file (look for the fully
# qualified name of tables, match fields, and actions.
# ---- START SOLUTION ----
# Add entry to "My Station" table. Consider the given pkt's eth dst addr
# as myStationMac address.
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# Exact match.
"MODIFY ME": pkt[Ether].dst
},
action_name="NoAction"
))
# This should be missed, this is plain IPv6 routing.
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# Longest prefix match (value, prefix length)
"MODIFY ME": (my_sid, 128)
},
action_name="MODIFY ME"
))
# Insert ECMP group with only one member (next_hop_mac)
self.insert(self.helper.build_act_prof_group(
act_prof_name="MODIFY ME",
group_id=1,
actions=[
# List of tuples (action name, {action param: value})
("MODIFY ME", {"MODIFY ME": next_hop_mac}),
]
))
# Map pkt's IPv6 dst addr to group
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# LPM match (value, prefix)
"hdr.ipv6.dst_addr": (pkt[IPv6].dst, 128)
},
group_id=1
))
# Map next_hop_mac to output port
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# Exact match
"MODIFY ME": next_hop_mac
},
action_name="MODIFY ME",
action_params={
"MODIFY ME": self.port2
}
))
# ---- END SOLUTION ----
# Build expected packet from the given one...
exp_pkt = pkt.copy()
# Route and decrement TTL
pkt_route(exp_pkt, next_hop_mac)
pkt_decrement_ttl(exp_pkt)
# Bonus: update P4 program to calculate correct checksum
set_cksum(pkt, 1)
set_cksum(exp_pkt, 1)
testutils.send_packet(self, self.port1, str(pkt))
testutils.verify_packet(self, exp_pkt, self.port2)
@group("srv6")
class Srv6EndTest(P4RuntimeTest):
"""Tests SRv6 end behavior (without pop), where the switch forwards the
packet to the next SID found in the SRv6 header.
"""
def runTest(self):
my_sid = SWITCH2_IPV6
sid_lists = (
[SWITCH2_IPV6, SWITCH3_IPV6, HOST2_IPV6],
[SWITCH2_IPV6, SWITCH3_IPV6, SWITCH4_IPV6, HOST2_IPV6],
)
next_hop_mac = SWITCH3_MAC
for sid_list in sid_lists:
for pkt_type in ["tcpv6", "udpv6", "icmpv6"]:
print_inline("%s %d SIDs ... " % (pkt_type, len(sid_list)))
pkt = getattr(testutils, "simple_%s_packet" % pkt_type)()
pkt = insert_srv6_header(pkt, sid_list)
self.testPacket(pkt, sid_list, next_hop_mac, my_sid)
@autocleanup
def testPacket(self, pkt, sid_list, next_hop_mac, my_sid):
# *** TODO EXERCISE 6
# Modify names to match content of P4Info file (look for the fully
# qualified name of tables, match fields, and actions.
# ---- START SOLUTION ----
# Add entry to "My Station" table. Consider the given pkt's eth dst addr
# as myStationMac address.
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# Exact match.
"MODIFY ME": pkt[Ether].dst
},
action_name="NoAction"
))
# This should be matched, we want SRv6 end behavior to be applied.
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# Longest prefix match (value, prefix length)
"MODIFY ME": (my_sid, 128)
},
action_name="MODIFY ME"
))
# Insert ECMP group with only one member (next_hop_mac)
self.insert(self.helper.build_act_prof_group(
act_prof_name="MODIFY ME",
group_id=1,
actions=[
# List of tuples (action name, {action param: value})
("MODIFY ME", {"MODIFY ME": next_hop_mac}),
]
))
# After applying the srv6_end action, we expect to IPv6 dst to be the
# next SID in the list, we should route based on that.
next_sid = sid_list[1]
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# LPM match (value, prefix)
"hdr.ipv6.dst_addr": (next_sid, 128)
},
group_id=1
))
# Map next_hop_mac to output port
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# Exact match
"MODIFY ME": next_hop_mac
},
action_name="MODIFY ME",
action_params={
"MODIFY ME": self.port2
}
))
# ---- END SOLUTION ----
# Build expected packet from the given one...
exp_pkt = pkt.copy()
# Set IPv6 dst to next SID and decrement segleft...
exp_pkt[IPv6].dst = next_sid
exp_pkt[IPv6ExtHdrSegmentRouting].segleft -= 1
# Route and decrement TTL...
pkt_route(exp_pkt, next_hop_mac)
pkt_decrement_ttl(exp_pkt)
# Bonus: update P4 program to calculate correct checksum
set_cksum(pkt, 1)
set_cksum(exp_pkt, 1)
testutils.send_packet(self, self.port1, str(pkt))
testutils.verify_packet(self, exp_pkt, self.port2)
@group("srv6")
class Srv6EndPspTest(P4RuntimeTest):
"""Tests SRv6 End with Penultimate Segment Pop (PSP) behavior, where the
switch SID is the penultimate in the SID list and the switch removes the
SRv6 header before routing the packet to it's final destination (last SID in
the list).
"""
def runTest(self):
my_sid = SWITCH3_IPV6
sid_lists = (
[SWITCH3_IPV6, HOST2_IPV6],
)
next_hop_mac = HOST2_MAC
for sid_list in sid_lists:
for pkt_type in ["tcpv6", "udpv6", "icmpv6"]:
print_inline("%s %d SIDs ... " % (pkt_type, len(sid_list)))
pkt = getattr(testutils, "simple_%s_packet" % pkt_type)()
pkt = insert_srv6_header(pkt, sid_list)
self.testPacket(pkt, sid_list, next_hop_mac, my_sid)
@autocleanup
def testPacket(self, pkt, sid_list, next_hop_mac, my_sid):
# *** TODO EXERCISE 6
# Modify names to match content of P4Info file (look for the fully
# qualified name of tables, match fields, and actions.
# ---- START SOLUTION ----
# Add entry to "My Station" table. Consider the given pkt's eth dst addr
# as myStationMac address.
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# Exact match.
"MODIFY ME": pkt[Ether].dst
},
action_name="NoAction"
))
# This should be matched, we want SRv6 end behavior to be applied.
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# Longest prefix match (value, prefix length)
"MODIFY ME": (my_sid, 128)
},
action_name="MODIFY ME"
))
# Insert ECMP group with only one member (next_hop_mac)
self.insert(self.helper.build_act_prof_group(
act_prof_name="MODIFY ME",
group_id=1,
actions=[
# List of tuples (action name, {action param: value})
("MODIFY ME", {"MODIFY ME": next_hop_mac}),
]
))
# Map pkt's IPv6 dst addr to group
next_sid = sid_list[1]
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# LPM match (value, prefix)
"hdr.ipv6.dst_addr": (next_sid, 128)
},
group_id=1
))
# Map next_hop_mac to output port
self.insert(self.helper.build_table_entry(
table_name="MODIFY ME",
match_fields={
# Exact match
"MODIFY ME": next_hop_mac
},
action_name="MODIFY ME",
action_params={
"MODIFY ME": self.port2
}
))
# ---- END SOLUTION ----
# Build expected packet from the given one...
exp_pkt = pkt.copy()
# Expect IPv6 dst to be the next SID...
exp_pkt[IPv6].dst = next_sid
# Remove SRv6 header since we are performing PSP.
pop_srv6_header(exp_pkt)
# Route and decrement TTL
pkt_route(exp_pkt, next_hop_mac)
pkt_decrement_ttl(exp_pkt)
# Bonus: update P4 program to calculate correct checksum
set_cksum(pkt, 1)
set_cksum(exp_pkt, 1)
testutils.send_packet(self, self.port1, str(pkt))
testutils.verify_packet(self, exp_pkt, self.port2)