5 from socket import AF_INET6
7 from framework import VppTestCase, VppTestRunner
8 from vpp_ip_route import VppIpRoute, VppRoutePath, DpoProto, VppIpTable
9 from vpp_srv6 import SRv6LocalSIDBehaviors, VppSRv6LocalSID, VppSRv6Policy, \
10 SRv6PolicyType, VppSRv6Steering, SRv6PolicySteeringTypes
12 from scapy.packet import Raw
13 from scapy.layers.l2 import Ether, Dot1Q
14 from scapy.layers.inet6 import IPv6, UDP, IPv6ExtHdrSegmentRouting
15 from scapy.layers.inet import IP, UDP
17 from scapy.utils import inet_pton, inet_ntop
22 class TestSRv6(VppTestCase):
23 """ SRv6 Dynamic Proxy plugin Test Case """
27 super(TestSRv6, self).setUpClass()
30 """ Perform test setup before each test case.
32 super(TestSRv6, self).setUp()
34 # packet sizes, inclusive L2 overhead
35 self.pg_packet_sizes = [64, 512, 1518, 9018]
38 self.reset_packet_infos()
41 """ Clean up test setup after each test case.
43 self.teardown_interfaces()
45 super(TestSRv6, self).tearDown()
47 def configure_interface(self,
49 ipv6=False, ipv4=False,
50 ipv6_table_id=0, ipv4_table_id=0):
51 """ Configure interface.
52 :param ipv6: configure IPv6 on interface
53 :param ipv4: configure IPv4 on interface
54 :param ipv6_table_id: FIB table_id for IPv6
55 :param ipv4_table_id: FIB table_id for IPv4
57 self.logger.debug("Configuring interface %s" % (interface.name))
59 self.logger.debug("Configuring IPv6")
60 interface.set_table_ip6(ipv6_table_id)
61 interface.config_ip6()
62 interface.resolve_ndp(timeout=5)
64 self.logger.debug("Configuring IPv4")
65 interface.set_table_ip4(ipv4_table_id)
66 interface.config_ip4()
67 interface.resolve_arp()
70 def setup_interfaces(self, ipv6=[], ipv4=[],
71 ipv6_table_id=[], ipv4_table_id=[]):
72 """ Create and configure interfaces.
74 :param ipv6: list of interface IPv6 capabilities
75 :param ipv4: list of interface IPv4 capabilities
76 :param ipv6_table_id: list of intf IPv6 FIB table_ids
77 :param ipv4_table_id: list of intf IPv4 FIB table_ids
78 :returns: List of created interfaces.
80 # how many interfaces?
85 self.logger.debug("Creating and configuring %d interfaces" % (count))
87 # fill up ipv6 and ipv4 lists if needed
88 # not enabled (False) is the default
90 ipv6 += (count - len(ipv6)) * [False]
92 ipv4 += (count - len(ipv4)) * [False]
94 # fill up table_id lists if needed
95 # table_id 0 (global) is the default
96 if len(ipv6_table_id) < count:
97 ipv6_table_id += (count - len(ipv6_table_id)) * [0]
98 if len(ipv4_table_id) < count:
99 ipv4_table_id += (count - len(ipv4_table_id)) * [0]
101 # create 'count' pg interfaces
102 self.create_pg_interfaces(range(count))
104 # setup all interfaces
105 for i in range(count):
106 intf = self.pg_interfaces[i]
107 self.configure_interface(intf,
109 ipv6_table_id[i], ipv4_table_id[i])
112 self.logger.debug(self.vapi.cli("show ip6 neighbors"))
114 self.logger.debug(self.vapi.cli("show ip arp"))
115 self.logger.debug(self.vapi.cli("show interface"))
116 self.logger.debug(self.vapi.cli("show hardware"))
118 return self.pg_interfaces
120 def teardown_interfaces(self):
121 """ Unconfigure and bring down interface.
123 self.logger.debug("Tearing down interfaces")
124 # tear down all interfaces
125 # AFAIK they cannot be deleted
126 for i in self.pg_interfaces:
127 self.logger.debug("Tear down interface %s" % (i.name))
133 def test_SRv6_End_AD_IPv6(self):
134 """ Test SRv6 End.AD behavior with IPv6 traffic.
136 self.src_addr = 'a0::'
137 self.sid_list = ['a1::', 'a2::a6', 'a3::']
138 self.test_sid_index = 1
140 # send traffic to one destination interface
141 # source and destination interfaces are IPv6 only
142 self.setup_interfaces(ipv6=[True, True])
144 # configure route to next segment
145 route = VppIpRoute(self, self.sid_list[self.test_sid_index + 1], 128,
146 [VppRoutePath(self.pg0.remote_ip6,
147 self.pg0.sw_if_index,
148 proto=DpoProto.DPO_PROTO_IP6)],
150 route.add_vpp_config()
152 # configure SRv6 localSID behavior
153 cli_str = "sr localsid address " + \
154 self.sid_list[self.test_sid_index] + \
155 " behavior end.ad" + \
156 " nh " + self.pg1.remote_ip6 + \
157 " oif " + self.pg1.name + \
158 " iif " + self.pg1.name
159 self.vapi.cli(cli_str)
162 self.logger.debug(self.vapi.cli("show sr localsid"))
164 # send one packet per packet size
165 count = len(self.pg_packet_sizes)
167 # prepare IPv6 in SRv6 headers
168 packet_header1 = self.create_packet_header_IPv6_SRH_IPv6(
169 srcaddr=self.src_addr,
170 sidlist=self.sid_list[::-1],
171 segleft=len(self.sid_list) - self.test_sid_index - 1)
173 # generate packets (pg0->pg1)
174 pkts1 = self.create_stream(self.pg0, self.pg1, packet_header1,
175 self.pg_packet_sizes, count)
177 # send packets and verify received packets
178 self.send_and_verify_pkts(self.pg0, pkts1, self.pg1,
179 self.compare_rx_tx_packet_End_AD_IPv6_out)
181 # log the localsid counters
182 self.logger.info(self.vapi.cli("show sr localsid"))
184 # prepare IPv6 header for returning packets
185 packet_header2 = self.create_packet_header_IPv6()
187 # generate returning packets (pg1->pg0)
188 pkts2 = self.create_stream(self.pg1, self.pg0, packet_header2,
189 self.pg_packet_sizes, count)
191 # send packets and verify received packets
192 self.send_and_verify_pkts(self.pg1, pkts2, self.pg0,
193 self.compare_rx_tx_packet_End_AD_IPv6_in)
195 # log the localsid counters
196 self.logger.info(self.vapi.cli("show sr localsid"))
198 # remove SRv6 localSIDs
199 cli_str = "sr localsid del address " + \
200 self.sid_list[self.test_sid_index]
201 self.vapi.cli(cli_str)
204 self.teardown_interfaces()
206 def compare_rx_tx_packet_End_AD_IPv6_out(self, tx_pkt, rx_pkt):
207 """ Compare input and output packet after passing End.AD with IPv6
209 :param tx_pkt: transmitted packet
210 :param rx_pkt: received packet
213 # get first (outer) IPv6 header of rx'ed packet
214 rx_ip = rx_pkt.getlayer(IPv6)
216 tx_ip = tx_pkt.getlayer(IPv6)
217 tx_ip2 = tx_pkt.getlayer(IPv6, 2)
219 # verify if rx'ed packet has no SRH
220 self.assertFalse(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting))
222 # the whole rx_ip pkt should be equal to tx_ip2
223 # except for the hlim field
224 # -> adjust tx'ed hlim to expected hlim
225 tx_ip2.hlim = tx_ip2.hlim - 1
227 self.assertEqual(rx_ip, tx_ip2)
229 self.logger.debug("packet verification: SUCCESS")
231 def compare_rx_tx_packet_End_AD_IPv6_in(self, tx_pkt, rx_pkt):
232 """ Compare input and output packet after passing End.AD
234 :param tx_pkt: transmitted packet
235 :param rx_pkt: received packet
238 # get first (outer) IPv6 header of rx'ed packet
239 rx_ip = rx_pkt.getlayer(IPv6)
240 # received ip.src should be equal to SR Policy source
241 self.assertEqual(rx_ip.src, self.src_addr)
242 # received ip.dst should be equal to expected sidlist next segment
243 self.assertEqual(rx_ip.dst, self.sid_list[self.test_sid_index + 1])
245 # rx'ed packet should have SRH
246 self.assertTrue(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting))
249 rx_srh = rx_pkt.getlayer(IPv6ExtHdrSegmentRouting)
250 # rx'ed seglist should be equal to SID-list in reversed order
251 self.assertEqual(rx_srh.addresses, self.sid_list[::-1])
252 # segleft should be equal to previous segleft value minus 1
253 self.assertEqual(rx_srh.segleft,
254 len(self.sid_list) - self.test_sid_index - 2)
255 # lastentry should be equal to the SID-list length minus 1
256 self.assertEqual(rx_srh.lastentry, len(self.sid_list) - 1)
258 # the whole rx'ed pkt beyond SRH should be equal to tx'ed pkt
259 # except for the hop-limit field
260 tx_ip = tx_pkt.getlayer(IPv6)
261 # -> update tx'ed hlim to the expected hlim
264 self.assertEqual(rx_srh.payload, tx_ip)
266 self.logger.debug("packet verification: SUCCESS")
268 def test_SRv6_End_AD_IPv4(self):
269 """ Test SRv6 End.AD behavior with IPv4 traffic.
271 self.src_addr = 'a0::'
272 self.sid_list = ['a1::', 'a2::a4', 'a3::']
273 self.test_sid_index = 1
275 # send traffic to one destination interface
276 # source and destination interfaces are IPv6 only
277 self.setup_interfaces(ipv6=[True, False], ipv4=[False, True])
279 # configure route to next segment
280 route = VppIpRoute(self, self.sid_list[self.test_sid_index + 1], 128,
281 [VppRoutePath(self.pg0.remote_ip6,
282 self.pg0.sw_if_index,
283 proto=DpoProto.DPO_PROTO_IP6)],
285 route.add_vpp_config()
287 # configure SRv6 localSID behavior
288 cli_str = "sr localsid address " + \
289 self.sid_list[self.test_sid_index] + \
290 " behavior end.ad" + \
291 " nh " + self.pg1.remote_ip4 + \
292 " oif " + self.pg1.name + \
293 " iif " + self.pg1.name
294 self.vapi.cli(cli_str)
297 self.logger.debug(self.vapi.cli("show sr localsid"))
299 # send one packet per packet size
300 count = len(self.pg_packet_sizes)
302 # prepare IPv4 in SRv6 headers
303 packet_header1 = self.create_packet_header_IPv6_SRH_IPv4(
304 srcaddr=self.src_addr,
305 sidlist=self.sid_list[::-1],
306 segleft=len(self.sid_list) - self.test_sid_index - 1)
308 # generate packets (pg0->pg1)
309 pkts1 = self.create_stream(self.pg0, self.pg1, packet_header1,
310 self.pg_packet_sizes, count)
312 # send packets and verify received packets
313 self.send_and_verify_pkts(self.pg0, pkts1, self.pg1,
314 self.compare_rx_tx_packet_End_AD_IPv4_out)
316 # log the localsid counters
317 self.logger.info(self.vapi.cli("show sr localsid"))
319 # prepare IPv6 header for returning packets
320 packet_header2 = self.create_packet_header_IPv4()
322 # generate returning packets (pg1->pg0)
323 pkts2 = self.create_stream(self.pg1, self.pg0, packet_header2,
324 self.pg_packet_sizes, count)
326 # send packets and verify received packets
327 self.send_and_verify_pkts(self.pg1, pkts2, self.pg0,
328 self.compare_rx_tx_packet_End_AD_IPv4_in)
330 # log the localsid counters
331 self.logger.info(self.vapi.cli("show sr localsid"))
333 # remove SRv6 localSIDs
334 cli_str = "sr localsid del address " + \
335 self.sid_list[self.test_sid_index]
336 self.vapi.cli(cli_str)
339 self.teardown_interfaces()
341 def compare_rx_tx_packet_End_AD_IPv4_out(self, tx_pkt, rx_pkt):
342 """ Compare input and output packet after passing End.AD with IPv4
344 :param tx_pkt: transmitted packet
345 :param rx_pkt: received packet
348 # get IPv4 header of rx'ed packet
349 rx_ip = rx_pkt.getlayer(IP)
351 tx_ip = tx_pkt.getlayer(IPv6)
352 tx_ip2 = tx_pkt.getlayer(IP)
354 # verify if rx'ed packet has no SRH
355 self.assertFalse(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting))
357 # the whole rx_ip pkt should be equal to tx_ip2
358 # except for the ttl field and ip checksum
359 # -> adjust tx'ed ttl to expected ttl
360 tx_ip2.ttl = tx_ip2.ttl - 1
361 # -> set tx'ed ip checksum to None and let scapy recompute
363 # read back the pkt (with str()) to force computing these fields
364 # probably other ways to accomplish this are possible
365 tx_ip2 = IP(str(tx_ip2))
367 self.assertEqual(rx_ip, tx_ip2)
369 self.logger.debug("packet verification: SUCCESS")
371 def compare_rx_tx_packet_End_AD_IPv4_in(self, tx_pkt, rx_pkt):
372 """ Compare input and output packet after passing End.AD
374 :param tx_pkt: transmitted packet
375 :param rx_pkt: received packet
378 # get first (outer) IPv6 header of rx'ed packet
379 rx_ip = rx_pkt.getlayer(IPv6)
380 # received ip.src should be equal to SR Policy source
381 self.assertEqual(rx_ip.src, self.src_addr)
382 # received ip.dst should be equal to expected sidlist next segment
383 self.assertEqual(rx_ip.dst, self.sid_list[self.test_sid_index + 1])
385 # rx'ed packet should have SRH
386 self.assertTrue(rx_pkt.haslayer(IPv6ExtHdrSegmentRouting))
389 rx_srh = rx_pkt.getlayer(IPv6ExtHdrSegmentRouting)
390 # rx'ed seglist should be equal to SID-list in reversed order
391 self.assertEqual(rx_srh.addresses, self.sid_list[::-1])
392 # segleft should be equal to previous segleft value minus 1
393 self.assertEqual(rx_srh.segleft,
394 len(self.sid_list) - self.test_sid_index - 2)
395 # lastentry should be equal to the SID-list length minus 1
396 self.assertEqual(rx_srh.lastentry, len(self.sid_list) - 1)
398 # the whole rx'ed pkt beyond SRH should be equal to tx'ed pkt
399 # except for the ttl field and ip checksum
400 tx_ip = tx_pkt.getlayer(IP)
401 # -> adjust tx'ed ttl to expected ttl
402 tx_ip.ttl = tx_ip.ttl - 1
403 # -> set tx'ed ip checksum to None and let scapy recompute
405 # -> read back the pkt (with str()) to force computing these fields
406 # probably other ways to accomplish this are possible
407 self.assertEqual(rx_srh.payload, IP(str(tx_ip)))
409 self.logger.debug("packet verification: SUCCESS")
411 def create_stream(self, src_if, dst_if, packet_header, packet_sizes,
413 """Create SRv6 input packet stream for defined interface.
415 :param VppInterface src_if: Interface to create packet stream for
416 :param VppInterface dst_if: destination interface of packet stream
417 :param packet_header: Layer3 scapy packet headers,
418 L2 is added when not provided,
419 Raw(payload) with packet_info is added
420 :param list packet_sizes: packet stream pckt sizes,sequentially applied
421 to packets in stream have
422 :param int count: number of packets in packet stream
423 :return: list of packets
425 self.logger.info("Creating packets")
427 for i in range(0, count - 1):
428 payload_info = self.create_packet_info(src_if, dst_if)
430 "Creating packet with index %d" % (payload_info.index))
431 payload = self.info_to_payload(payload_info)
432 # add L2 header if not yet provided in packet_header
433 if packet_header.getlayer(0).name == 'Ethernet':
434 p = packet_header / Raw(payload)
436 p = Ether(dst=src_if.local_mac, src=src_if.remote_mac) / \
437 packet_header / Raw(payload)
438 size = packet_sizes[i % len(packet_sizes)]
439 self.logger.debug("Packet size %d" % (size))
440 self.extend_packet(p, size)
441 # we need to store the packet with the automatic fields computed
442 # read back the dumped packet (with str())
443 # to force computing these fields
444 # probably other ways are possible
446 payload_info.data = p.copy()
447 self.logger.debug(ppp("Created packet:", p))
449 self.logger.info("Done creating packets")
452 def send_and_verify_pkts(self, input, pkts, output, compare_func):
453 """Send packets and verify received packets using compare_func
455 :param input: ingress interface of DUT
456 :param pkts: list of packets to transmit
457 :param output: egress interface of DUT
458 :param compare_func: function to compare in and out packets
460 # add traffic stream to input interface
461 input.add_stream(pkts)
463 # enable capture on all interfaces
464 self.pg_enable_capture(self.pg_interfaces)
467 self.logger.info("Starting traffic")
471 self.logger.info("Getting packet capture")
472 capture = output.get_capture()
474 # assert nothing was captured on input interface
475 # input.assert_nothing_captured()
477 # verify captured packets
478 self.verify_captured_pkts(output, capture, compare_func)
480 def create_packet_header_IPv6(self):
481 """Create packet header: IPv6 header, UDP header
483 :param dst: IPv6 destination address
485 IPv6 source address is 1234::1
486 IPv6 destination address is 4321::1
487 UDP source port and destination port are 1234
490 p = IPv6(src='1234::1', dst='4321::1') / UDP(sport=1234, dport=1234)
493 def create_packet_header_IPv6_SRH_IPv6(self, srcaddr, sidlist, segleft):
494 """Create packet header: IPv6 encapsulated in SRv6:
495 IPv6 header with SRH, IPv6 header, UDP header
497 :param int srcaddr: outer source address
498 :param list sidlist: segment list of outer IPv6 SRH
499 :param int segleft: segments-left field of outer IPv6 SRH
501 Outer IPv6 source address is set to srcaddr
502 Outer IPv6 destination address is set to sidlist[segleft]
503 Inner IPv6 source addresses is 1234::1
504 Inner IPv6 destination address is 4321::1
505 UDP source port and destination port are 1234
508 p = IPv6(src=srcaddr, dst=sidlist[segleft]) / \
509 IPv6ExtHdrSegmentRouting(addresses=sidlist,
510 segleft=segleft, nh=41) / \
511 IPv6(src='1234::1', dst='4321::1') / \
512 UDP(sport=1234, dport=1234)
515 def create_packet_header_IPv4(self):
516 """Create packet header: IPv4 header, UDP header
518 :param dst: IPv4 destination address
520 IPv4 source address is 123.1.1.1
521 IPv4 destination address is 124.1.1.1
522 UDP source port and destination port are 1234
525 p = IP(src='123.1.1.1', dst='124.1.1.1') / UDP(sport=1234, dport=1234)
528 def create_packet_header_IPv6_SRH_IPv4(self, srcaddr, sidlist, segleft):
529 """Create packet header: IPv4 encapsulated in SRv6:
530 IPv6 header with SRH, IPv4 header, UDP header
532 :param int srcaddr: outer source address
533 :param list sidlist: segment list of outer IPv6 SRH
534 :param int segleft: segments-left field of outer IPv6 SRH
536 Outer IPv6 source address is set to srcaddr
537 Outer IPv6 destination address is set to sidlist[segleft]
538 Inner IPv4 source address is 123.1.1.1
539 Inner IPv4 destination address is 124.1.1.1
540 UDP source port and destination port are 1234
543 p = IPv6(src=srcaddr, dst=sidlist[segleft]) / \
544 IPv6ExtHdrSegmentRouting(addresses=sidlist,
545 segleft=segleft, nh=4) / \
546 IP(src='123.1.1.1', dst='124.1.1.1') / \
547 UDP(sport=1234, dport=1234)
550 def get_payload_info(self, packet):
551 """ Extract the payload_info from the packet
553 # in most cases, payload_info is in packet[Raw]
554 # but packet[Raw] gives the complete payload
555 # (incl L2 header) for the T.Encaps L2 case
557 payload_info = self.payload_to_info(str(packet[Raw]))
560 # remote L2 header from packet[Raw]:
561 # take packet[Raw], convert it to an Ether layer
562 # and then extract Raw from it
563 payload_info = self.payload_to_info(
564 str(Ether(str(packet[Raw]))[Raw]))
568 def verify_captured_pkts(self, dst_if, capture, compare_func):
570 Verify captured packet stream for specified interface.
571 Compare ingress with egress packets using the specified compare fn
573 :param dst_if: egress interface of DUT
574 :param capture: captured packets
575 :param compare_func: function to compare in and out packet
577 self.logger.info("Verifying capture on interface %s using function %s"
578 % (dst_if.name, compare_func.func_name))
581 for i in self.pg_interfaces:
582 last_info[i.sw_if_index] = None
583 dst_sw_if_index = dst_if.sw_if_index
585 for packet in capture:
587 # extract payload_info from packet's payload
588 payload_info = self.get_payload_info(packet)
589 packet_index = payload_info.index
591 self.logger.debug("Verifying packet with index %d"
593 # packet should have arrived on the expected interface
594 self.assertEqual(payload_info.dst, dst_sw_if_index)
596 "Got packet on interface %s: src=%u (idx=%u)" %
597 (dst_if.name, payload_info.src, packet_index))
599 # search for payload_info with same src and dst if_index
600 # this will give us the transmitted packet
601 next_info = self.get_next_packet_info_for_interface2(
602 payload_info.src, dst_sw_if_index,
603 last_info[payload_info.src])
604 last_info[payload_info.src] = next_info
605 # next_info should not be None
606 self.assertTrue(next_info is not None)
607 # index of tx and rx packets should be equal
608 self.assertEqual(packet_index, next_info.index)
609 # data field of next_info contains the tx packet
610 txed_packet = next_info.data
612 self.logger.debug(ppp("Transmitted packet:",
613 txed_packet)) # ppp=Pretty Print Packet
615 self.logger.debug(ppp("Received packet:", packet))
617 # compare rcvd packet with expected packet using compare_func
618 compare_func(txed_packet, packet)
621 print packet.command()
622 self.logger.error(ppp("Unexpected or invalid packet:", packet))
625 # have all expected packets arrived?
626 for i in self.pg_interfaces:
627 remaining_packet = self.get_next_packet_info_for_interface2(
628 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
629 self.assertTrue(remaining_packet is None,
630 "Interface %s: Packet expected from interface %s "
631 "didn't arrive" % (dst_if.name, i.name))
634 if __name__ == '__main__':
635 unittest.main(testRunner=VppTestRunner)