blob: d5d621f052edc65a5bc451649a3b986944b4bf0d [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Marco Varleseb598f1d2017-09-19 14:25:28 +02002
3import socket
Jakub Grajciar2d3282e2019-10-01 12:04:56 +02004from util import ip4_range, ip4_range
Marco Varleseb598f1d2017-09-19 14:25:28 +02005import unittest
6from framework import VppTestCase, VppTestRunner
7from template_bd import BridgeDomain
8
9from scapy.layers.l2 import Ether
10from scapy.layers.inet import IP, UDP
11from scapy.layers.geneve import GENEVE
12from scapy.utils import atol
Neale Ranns097fa662018-05-01 05:17:55 -070013from vpp_ip_route import VppIpRoute, VppRoutePath
14from vpp_ip import INVALID_INDEX
Marco Varleseb598f1d2017-09-19 14:25:28 +020015
16
17class TestGeneve(BridgeDomain, VppTestCase):
18 """ GENEVE Test Case """
19
20 def __init__(self, *args):
21 BridgeDomain.__init__(self)
22 VppTestCase.__init__(self, *args)
23
24 def encapsulate(self, pkt, vni):
25
26 """
27 Encapsulate the original payload frame by adding GENEVE header with its
28 UDP, IP and Ethernet fields
29 """
30 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
31 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
32 UDP(sport=self.dport, dport=self.dport, chksum=0) /
33 GENEVE(vni=vni) /
34 pkt)
35
Eyal Baricef1e2a2018-06-18 13:01:59 +030036 def ip_range(self, start, end):
37 """ range of remote ip's """
38 return ip4_range(self.pg0.remote_ip4, start, end)
39
Marco Varleseb598f1d2017-09-19 14:25:28 +020040 def encap_mcast(self, pkt, src_ip, src_mac, vni):
41 """
42 Encapsulate the original payload frame by adding GENEVE header with its
43 UDP, IP and Ethernet fields
44 """
45 return (Ether(src=src_mac, dst=self.mcast_mac) /
46 IP(src=src_ip, dst=self.mcast_ip4) /
47 UDP(sport=self.dport, dport=self.dport, chksum=0) /
48 GENEVE(vni=vni) /
49 pkt)
50
51 def decapsulate(self, pkt):
52 """
53 Decapsulate the original payload frame by removing GENEVE header
54 """
55 # check if is set I flag
56 # self.assertEqual(pkt[GENEVE].flags, int('0x8', 16))
57 return pkt[GENEVE].payload
58
59 # Method for checking GENEVE encapsulation.
60 #
61 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
62 # TODO: add error messages
63 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
64 # by VPP using ARP.
65 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
66 if not local_only:
67 if not mcast_pkt:
68 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
69 else:
70 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
71 # Verify GENEVE tunnel source IP is VPP_IP and destination IP is MY_IP.
72 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
73 if not local_only:
74 if not mcast_pkt:
75 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
76 else:
77 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
78 # Verify UDP destination port is GENEVE 4789, source UDP port could be
79 # arbitrary.
80 self.assertEqual(pkt[UDP].dport, type(self).dport)
81 # TODO: checksum check
82 # Verify VNI
83 self.assertEqual(pkt[GENEVE].vni, vni)
84
85 @classmethod
86 def create_geneve_flood_test_bd(cls, vni, n_ucast_tunnels):
87 # Create 10 ucast geneve tunnels under bd
88 ip_range_start = 10
89 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070090 next_hop_address = cls.pg0.remote_ip4
91 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
92 ip_range_end):
Jakub Grajciar2d3282e2019-10-01 12:04:56 +020093 # add host route so dest_ip4 will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -070094 rip = VppIpRoute(cls, dest_ip4, 32,
95 [VppRoutePath(next_hop_address,
96 INVALID_INDEX)],
97 register=False)
98 rip.add_vpp_config()
Marco Varleseb598f1d2017-09-19 14:25:28 +020099 r = cls.vapi.geneve_add_del_tunnel(
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200100 local_address=cls.pg0.local_ip4, remote_address=dest_ip4,
Marco Varleseb598f1d2017-09-19 14:25:28 +0200101 vni=vni)
Ole Troana5b2eec2019-03-11 19:23:25 +0100102 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
103 bd_id=vni)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200104
105 @classmethod
106 def add_del_shared_mcast_dst_load(cls, is_add):
107 """
108 add or del tunnels sharing the same mcast dst
109 to test geneve ref_count mechanism
110 """
Gabriel Ganne97cabc92018-02-08 11:22:33 +0100111 n_shared_dst_tunnels = 10
Marco Varleseb598f1d2017-09-19 14:25:28 +0200112 vni_start = 10000
113 vni_end = vni_start + n_shared_dst_tunnels
114 for vni in range(vni_start, vni_end):
115 r = cls.vapi.geneve_add_del_tunnel(
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200116 local_address=cls.pg0.local_ip4,
117 remote_address=cls.mcast_ip4, mcast_sw_if_index=1,
Ole Troana5b2eec2019-03-11 19:23:25 +0100118 is_add=is_add, vni=vni)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200119 if r.sw_if_index == 0xffffffff:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700120 raise ValueError("bad sw_if_index: ~0")
Marco Varleseb598f1d2017-09-19 14:25:28 +0200121
122 @classmethod
123 def add_shared_mcast_dst_load(cls):
124 cls.add_del_shared_mcast_dst_load(is_add=1)
125
126 @classmethod
127 def del_shared_mcast_dst_load(cls):
128 cls.add_del_shared_mcast_dst_load(is_add=0)
129
130 @classmethod
131 def add_del_mcast_tunnels_load(cls, is_add):
132 """
133 add or del tunnels to test geneve stability
134 """
Gabriel Ganne97cabc92018-02-08 11:22:33 +0100135 n_distinct_dst_tunnels = 10
Marco Varleseb598f1d2017-09-19 14:25:28 +0200136 ip_range_start = 10
137 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200138 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
139 ip_range_end):
Ole Troan67694732019-10-21 21:03:39 +0200140 vni = int(dest_ip4.split('.')[3])
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200141 cls.vapi.geneve_add_del_tunnel(local_address=cls.pg0.local_ip4,
142 remote_address=dest_ip4,
Ole Troana5b2eec2019-03-11 19:23:25 +0100143 mcast_sw_if_index=1, is_add=is_add,
144 vni=vni)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200145
146 @classmethod
147 def add_mcast_tunnels_load(cls):
148 cls.add_del_mcast_tunnels_load(is_add=1)
149
150 @classmethod
151 def del_mcast_tunnels_load(cls):
152 cls.add_del_mcast_tunnels_load(is_add=0)
153
154 # Class method to start the GENEVE test case.
155 # Overrides setUpClass method in VppTestCase class.
156 # Python try..except statement is used to ensure that the tear down of
157 # the class will be executed even if exception is raised.
158 # @param cls The class pointer.
159 @classmethod
160 def setUpClass(cls):
161 super(TestGeneve, cls).setUpClass()
162
163 try:
164 cls.dport = 6081
165
166 # Create 2 pg interfaces.
167 cls.create_pg_interfaces(range(4))
168 for pg in cls.pg_interfaces:
169 pg.admin_up()
170
171 # Configure IPv4 addresses on VPP pg0.
172 cls.pg0.config_ip4()
173
174 # Resolve MAC address for VPP's IP address on pg0.
175 cls.pg0.resolve_arp()
176
177 # Our Multicast address
178 cls.mcast_ip4 = '239.1.1.1'
Marco Varleseb598f1d2017-09-19 14:25:28 +0200179 iplong = atol(cls.mcast_ip4)
180 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
181 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
182
183 # Create GENEVE VTEP on VPP pg0, and put geneve_tunnel0 and pg1
184 # into BD.
185 cls.single_tunnel_bd = 1
186 r = cls.vapi.geneve_add_del_tunnel(
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200187 local_address=cls.pg0.local_ip4,
188 remote_address=cls.pg0.remote_ip4, vni=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100189 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Marco Varleseb598f1d2017-09-19 14:25:28 +0200190 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100191 cls.vapi.sw_interface_set_l2_bridge(
192 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200193
194 # Setup vni 2 to test multicast flooding
195 cls.n_ucast_tunnels = 10
196 cls.mcast_flood_bd = 2
197 cls.create_geneve_flood_test_bd(cls.mcast_flood_bd,
198 cls.n_ucast_tunnels)
199 r = cls.vapi.geneve_add_del_tunnel(
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200200 local_address=cls.pg0.local_ip4,
201 remote_address=cls.mcast_ip4, mcast_sw_if_index=1,
Marco Varleseb598f1d2017-09-19 14:25:28 +0200202 vni=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100203 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Marco Varleseb598f1d2017-09-19 14:25:28 +0200204 bd_id=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100205 cls.vapi.sw_interface_set_l2_bridge(
206 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200207
208 # Add and delete mcast tunnels to check stability
209 cls.add_shared_mcast_dst_load()
210 cls.add_mcast_tunnels_load()
211 cls.del_shared_mcast_dst_load()
212 cls.del_mcast_tunnels_load()
213
214 # Setup vni 3 to test unicast flooding
215 cls.ucast_flood_bd = 3
216 cls.create_geneve_flood_test_bd(cls.ucast_flood_bd,
217 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100218 cls.vapi.sw_interface_set_l2_bridge(
219 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200220 except Exception:
221 super(TestGeneve, cls).tearDownClass()
222 raise
223
224 # Method to define VPP actions before tear down of the test case.
225 # Overrides tearDown method in VppTestCase class.
226 # @param self The object pointer.
227 def tearDown(self):
228 super(TestGeneve, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700229
230 def show_commands_at_teardown(self):
231 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
232 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
233 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
234 self.logger.info(self.vapi.cli("show geneve tunnel"))
Marco Varleseb598f1d2017-09-19 14:25:28 +0200235
236
237if __name__ == '__main__':
238 unittest.main(testRunner=VppTestRunner)