4 from util import ip4n_range, ip4_range
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
9 from scapy.layers.l2 import Ether
10 from scapy.layers.inet import IP, UDP
11 from scapy.layers.geneve import GENEVE
12 from scapy.utils import atol
15 class TestGeneve(BridgeDomain, VppTestCase):
16 """ GENEVE Test Case """
18 def __init__(self, *args):
19 BridgeDomain.__init__(self)
20 VppTestCase.__init__(self, *args)
22 def encapsulate(self, pkt, vni):
25 Encapsulate the original payload frame by adding GENEVE header with its
26 UDP, IP and Ethernet fields
28 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
29 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
30 UDP(sport=self.dport, dport=self.dport, chksum=0) /
34 def ip_range(self, start, end):
35 """ range of remote ip's """
36 return ip4_range(self.pg0.remote_ip4, start, end)
38 def encap_mcast(self, pkt, src_ip, src_mac, vni):
40 Encapsulate the original payload frame by adding GENEVE header with its
41 UDP, IP and Ethernet fields
43 return (Ether(src=src_mac, dst=self.mcast_mac) /
44 IP(src=src_ip, dst=self.mcast_ip4) /
45 UDP(sport=self.dport, dport=self.dport, chksum=0) /
49 def decapsulate(self, pkt):
51 Decapsulate the original payload frame by removing GENEVE header
53 # check if is set I flag
54 # self.assertEqual(pkt[GENEVE].flags, int('0x8', 16))
55 return pkt[GENEVE].payload
57 # Method for checking GENEVE encapsulation.
59 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
60 # TODO: add error messages
61 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
63 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
66 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
68 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
69 # Verify GENEVE tunnel source IP is VPP_IP and destination IP is MY_IP.
70 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
73 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
75 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
76 # Verify UDP destination port is GENEVE 4789, source UDP port could be
78 self.assertEqual(pkt[UDP].dport, type(self).dport)
79 # TODO: checksum check
81 self.assertEqual(pkt[GENEVE].vni, vni)
84 def create_geneve_flood_test_bd(cls, vni, n_ucast_tunnels):
85 # Create 10 ucast geneve tunnels under bd
87 ip_range_end = ip_range_start + n_ucast_tunnels
88 next_hop_address = cls.pg0.remote_ip4n
89 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
91 # add host route so dest_ip4n will not be resolved
92 cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
93 r = cls.vapi.geneve_add_del_tunnel(
94 local_addr=cls.pg0.local_ip4n,
95 remote_addr=dest_ip4n,
97 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
100 def add_del_shared_mcast_dst_load(cls, is_add):
102 add or del tunnels sharing the same mcast dst
103 to test geneve ref_count mechanism
105 n_shared_dst_tunnels = 10
107 vni_end = vni_start + n_shared_dst_tunnels
108 for vni in range(vni_start, vni_end):
109 r = cls.vapi.geneve_add_del_tunnel(
110 local_addr=cls.pg0.local_ip4n,
111 remote_addr=cls.mcast_ip4n,
115 if r.sw_if_index == 0xffffffff:
116 raise "bad sw_if_index"
119 def add_shared_mcast_dst_load(cls):
120 cls.add_del_shared_mcast_dst_load(is_add=1)
123 def del_shared_mcast_dst_load(cls):
124 cls.add_del_shared_mcast_dst_load(is_add=0)
127 def add_del_mcast_tunnels_load(cls, is_add):
129 add or del tunnels to test geneve stability
131 n_distinct_dst_tunnels = 10
133 ip_range_end = ip_range_start + n_distinct_dst_tunnels
134 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
136 vni = bytearray(dest_ip4n)[3]
137 cls.vapi.geneve_add_del_tunnel(
138 local_addr=cls.pg0.local_ip4n,
139 remote_addr=dest_ip4n,
145 def add_mcast_tunnels_load(cls):
146 cls.add_del_mcast_tunnels_load(is_add=1)
149 def del_mcast_tunnels_load(cls):
150 cls.add_del_mcast_tunnels_load(is_add=0)
152 # Class method to start the GENEVE test case.
153 # Overrides setUpClass method in VppTestCase class.
154 # Python try..except statement is used to ensure that the tear down of
155 # the class will be executed even if exception is raised.
156 # @param cls The class pointer.
159 super(TestGeneve, cls).setUpClass()
164 # Create 2 pg interfaces.
165 cls.create_pg_interfaces(range(4))
166 for pg in cls.pg_interfaces:
169 # Configure IPv4 addresses on VPP pg0.
172 # Resolve MAC address for VPP's IP address on pg0.
173 cls.pg0.resolve_arp()
175 # Our Multicast address
176 cls.mcast_ip4 = '239.1.1.1'
177 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
178 iplong = atol(cls.mcast_ip4)
179 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
180 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
182 # Create GENEVE VTEP on VPP pg0, and put geneve_tunnel0 and pg1
184 cls.single_tunnel_bd = 1
185 r = cls.vapi.geneve_add_del_tunnel(
186 local_addr=cls.pg0.local_ip4n,
187 remote_addr=cls.pg0.remote_ip4n,
188 vni=cls.single_tunnel_bd)
189 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
190 bd_id=cls.single_tunnel_bd)
191 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
192 bd_id=cls.single_tunnel_bd)
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,
199 r = cls.vapi.geneve_add_del_tunnel(
200 local_addr=cls.pg0.local_ip4n,
201 remote_addr=cls.mcast_ip4n,
203 vni=cls.mcast_flood_bd)
204 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
205 bd_id=cls.mcast_flood_bd)
206 cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
207 bd_id=cls.mcast_flood_bd)
209 # Add and delete mcast tunnels to check stability
210 cls.add_shared_mcast_dst_load()
211 cls.add_mcast_tunnels_load()
212 cls.del_shared_mcast_dst_load()
213 cls.del_mcast_tunnels_load()
215 # Setup vni 3 to test unicast flooding
216 cls.ucast_flood_bd = 3
217 cls.create_geneve_flood_test_bd(cls.ucast_flood_bd,
219 cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
220 bd_id=cls.ucast_flood_bd)
222 super(TestGeneve, cls).tearDownClass()
225 # Method to define VPP actions before tear down of the test case.
226 # Overrides tearDown method in VppTestCase class.
227 # @param self The object pointer.
229 super(TestGeneve, self).tearDown()
230 if not self.vpp_dead:
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"))
237 if __name__ == '__main__':
238 unittest.main(testRunner=VppTestRunner)