fib: fib api updates
[vpp.git] / test / test_geneve.py
1 #!/usr/bin/env python
2
3 import socket
4 from util import ip4n_range, ip4_range
5 import unittest
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
8
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
13 from vpp_ip_route import VppIpRoute, VppRoutePath
14 from vpp_ip import INVALID_INDEX
15
16
17 class 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
36     def ip_range(self, start, end):
37         """ range of remote ip's """
38         return ip4_range(self.pg0.remote_ip4, start, end)
39
40     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
90         next_hop_address = cls.pg0.remote_ip4
91         for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
92                                   ip_range_end):
93             # add host route so dest_ip4n will not be resolved
94             rip = VppIpRoute(cls, dest_ip4, 32,
95                              [VppRoutePath(next_hop_address,
96                                            INVALID_INDEX)],
97                              register=False)
98             rip.add_vpp_config()
99             dest_ip4n = socket.inet_pton(socket.AF_INET, dest_ip4)
100             r = cls.vapi.geneve_add_del_tunnel(
101                 local_address=cls.pg0.local_ip4n, remote_address=dest_ip4n,
102                 vni=vni)
103             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
104                                                 bd_id=vni)
105
106     @classmethod
107     def add_del_shared_mcast_dst_load(cls, is_add):
108         """
109         add or del tunnels sharing the same mcast dst
110         to test geneve ref_count mechanism
111         """
112         n_shared_dst_tunnels = 10
113         vni_start = 10000
114         vni_end = vni_start + n_shared_dst_tunnels
115         for vni in range(vni_start, vni_end):
116             r = cls.vapi.geneve_add_del_tunnel(
117                 local_address=cls.pg0.local_ip4n,
118                 remote_address=cls.mcast_ip4n, mcast_sw_if_index=1,
119                 is_add=is_add, vni=vni)
120             if r.sw_if_index == 0xffffffff:
121                 raise ValueError("bad sw_if_index: ~0")
122
123     @classmethod
124     def add_shared_mcast_dst_load(cls):
125         cls.add_del_shared_mcast_dst_load(is_add=1)
126
127     @classmethod
128     def del_shared_mcast_dst_load(cls):
129         cls.add_del_shared_mcast_dst_load(is_add=0)
130
131     @classmethod
132     def add_del_mcast_tunnels_load(cls, is_add):
133         """
134         add or del tunnels to test geneve stability
135         """
136         n_distinct_dst_tunnels = 10
137         ip_range_start = 10
138         ip_range_end = ip_range_start + n_distinct_dst_tunnels
139         for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
140                                     ip_range_end):
141             vni = bytearray(dest_ip4n)[3]
142             cls.vapi.geneve_add_del_tunnel(local_address=cls.pg0.local_ip4n,
143                                            remote_address=dest_ip4n,
144                                            mcast_sw_if_index=1, is_add=is_add,
145                                            vni=vni)
146
147     @classmethod
148     def add_mcast_tunnels_load(cls):
149         cls.add_del_mcast_tunnels_load(is_add=1)
150
151     @classmethod
152     def del_mcast_tunnels_load(cls):
153         cls.add_del_mcast_tunnels_load(is_add=0)
154
155     # Class method to start the GENEVE test case.
156     #  Overrides setUpClass method in VppTestCase class.
157     #  Python try..except statement is used to ensure that the tear down of
158     #  the class will be executed even if exception is raised.
159     #  @param cls The class pointer.
160     @classmethod
161     def setUpClass(cls):
162         super(TestGeneve, cls).setUpClass()
163
164         try:
165             cls.dport = 6081
166
167             # Create 2 pg interfaces.
168             cls.create_pg_interfaces(range(4))
169             for pg in cls.pg_interfaces:
170                 pg.admin_up()
171
172             # Configure IPv4 addresses on VPP pg0.
173             cls.pg0.config_ip4()
174
175             # Resolve MAC address for VPP's IP address on pg0.
176             cls.pg0.resolve_arp()
177
178             # Our Multicast address
179             cls.mcast_ip4 = '239.1.1.1'
180             cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
181             iplong = atol(cls.mcast_ip4)
182             cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
183                 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
184
185             # Create GENEVE VTEP on VPP pg0, and put geneve_tunnel0 and pg1
186             #  into BD.
187             cls.single_tunnel_bd = 1
188             r = cls.vapi.geneve_add_del_tunnel(
189                 local_address=cls.pg0.local_ip4n,
190                 remote_address=cls.pg0.remote_ip4n, vni=cls.single_tunnel_bd)
191             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
192                                                 bd_id=cls.single_tunnel_bd)
193             cls.vapi.sw_interface_set_l2_bridge(
194                 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
195
196             # Setup vni 2 to test multicast flooding
197             cls.n_ucast_tunnels = 10
198             cls.mcast_flood_bd = 2
199             cls.create_geneve_flood_test_bd(cls.mcast_flood_bd,
200                                             cls.n_ucast_tunnels)
201             r = cls.vapi.geneve_add_del_tunnel(
202                 local_address=cls.pg0.local_ip4n,
203                 remote_address=cls.mcast_ip4n, mcast_sw_if_index=1,
204                 vni=cls.mcast_flood_bd)
205             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
206                                                 bd_id=cls.mcast_flood_bd)
207             cls.vapi.sw_interface_set_l2_bridge(
208                 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
209
210             # Add and delete mcast tunnels to check stability
211             cls.add_shared_mcast_dst_load()
212             cls.add_mcast_tunnels_load()
213             cls.del_shared_mcast_dst_load()
214             cls.del_mcast_tunnels_load()
215
216             # Setup vni 3 to test unicast flooding
217             cls.ucast_flood_bd = 3
218             cls.create_geneve_flood_test_bd(cls.ucast_flood_bd,
219                                             cls.n_ucast_tunnels)
220             cls.vapi.sw_interface_set_l2_bridge(
221                 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
222         except Exception:
223             super(TestGeneve, cls).tearDownClass()
224             raise
225
226     # Method to define VPP actions before tear down of the test case.
227     #  Overrides tearDown method in VppTestCase class.
228     #  @param self The object pointer.
229     def tearDown(self):
230         super(TestGeneve, self).tearDown()
231
232     def show_commands_at_teardown(self):
233         self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
234         self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
235         self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
236         self.logger.info(self.vapi.cli("show geneve tunnel"))
237
238
239 if __name__ == '__main__':
240     unittest.main(testRunner=VppTestRunner)