a670bc40864d207043d0697580cf2ed09c371d13
[vpp.git] / test / test_geneve.py
1 #!/usr/bin/env python
2
3 import socket
4 from util import ip4n_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
14
15 class TestGeneve(BridgeDomain, VppTestCase):
16     """ GENEVE Test Case """
17
18     def __init__(self, *args):
19         BridgeDomain.__init__(self)
20         VppTestCase.__init__(self, *args)
21
22     def encapsulate(self, pkt, vni):
23
24         """
25         Encapsulate the original payload frame by adding GENEVE header with its
26         UDP, IP and Ethernet fields
27         """
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) /
31                 GENEVE(vni=vni) /
32                 pkt)
33
34     def encap_mcast(self, pkt, src_ip, src_mac, vni):
35         """
36         Encapsulate the original payload frame by adding GENEVE header with its
37         UDP, IP and Ethernet fields
38         """
39         return (Ether(src=src_mac, dst=self.mcast_mac) /
40                 IP(src=src_ip, dst=self.mcast_ip4) /
41                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
42                 GENEVE(vni=vni) /
43                 pkt)
44
45     def decapsulate(self, pkt):
46         """
47         Decapsulate the original payload frame by removing GENEVE header
48         """
49         # check if is set I flag
50         # self.assertEqual(pkt[GENEVE].flags, int('0x8', 16))
51         return pkt[GENEVE].payload
52
53     # Method for checking GENEVE encapsulation.
54     #
55     def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
56         # TODO: add error messages
57         # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
58         #  by VPP using ARP.
59         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
60         if not local_only:
61             if not mcast_pkt:
62                 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
63             else:
64                 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
65         # Verify GENEVE tunnel source IP is VPP_IP and destination IP is MY_IP.
66         self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
67         if not local_only:
68             if not mcast_pkt:
69                 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
70             else:
71                 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
72         # Verify UDP destination port is GENEVE 4789, source UDP port could be
73         #  arbitrary.
74         self.assertEqual(pkt[UDP].dport, type(self).dport)
75         # TODO: checksum check
76         # Verify VNI
77         self.assertEqual(pkt[GENEVE].vni, vni)
78
79     @classmethod
80     def create_geneve_flood_test_bd(cls, vni, n_ucast_tunnels):
81         # Create 10 ucast geneve tunnels under bd
82         ip_range_start = 10
83         ip_range_end = ip_range_start + n_ucast_tunnels
84         next_hop_address = cls.pg0.remote_ip4n
85         for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
86                                     ip_range_end):
87             # add host route so dest_ip4n will not be resolved
88             cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
89             r = cls.vapi.geneve_add_del_tunnel(
90                 local_addr=cls.pg0.local_ip4n,
91                 remote_addr=dest_ip4n,
92                 vni=vni)
93             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
94
95     @classmethod
96     def add_del_shared_mcast_dst_load(cls, is_add):
97         """
98         add or del tunnels sharing the same mcast dst
99         to test geneve ref_count mechanism
100         """
101         n_shared_dst_tunnels = 10
102         vni_start = 10000
103         vni_end = vni_start + n_shared_dst_tunnels
104         for vni in range(vni_start, vni_end):
105             r = cls.vapi.geneve_add_del_tunnel(
106                 local_addr=cls.pg0.local_ip4n,
107                 remote_addr=cls.mcast_ip4n,
108                 mcast_sw_if_index=1,
109                 vni=vni,
110                 is_add=is_add)
111             if r.sw_if_index == 0xffffffff:
112                 raise "bad sw_if_index"
113
114     @classmethod
115     def add_shared_mcast_dst_load(cls):
116         cls.add_del_shared_mcast_dst_load(is_add=1)
117
118     @classmethod
119     def del_shared_mcast_dst_load(cls):
120         cls.add_del_shared_mcast_dst_load(is_add=0)
121
122     @classmethod
123     def add_del_mcast_tunnels_load(cls, is_add):
124         """
125         add or del tunnels to test geneve stability
126         """
127         n_distinct_dst_tunnels = 10
128         ip_range_start = 10
129         ip_range_end = ip_range_start + n_distinct_dst_tunnels
130         for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
131                                     ip_range_end):
132             vni = bytearray(dest_ip4n)[3]
133             cls.vapi.geneve_add_del_tunnel(
134                 local_addr=cls.pg0.local_ip4n,
135                 remote_addr=dest_ip4n,
136                 mcast_sw_if_index=1,
137                 vni=vni,
138                 is_add=is_add)
139
140     @classmethod
141     def add_mcast_tunnels_load(cls):
142         cls.add_del_mcast_tunnels_load(is_add=1)
143
144     @classmethod
145     def del_mcast_tunnels_load(cls):
146         cls.add_del_mcast_tunnels_load(is_add=0)
147
148     # Class method to start the GENEVE test case.
149     #  Overrides setUpClass method in VppTestCase class.
150     #  Python try..except statement is used to ensure that the tear down of
151     #  the class will be executed even if exception is raised.
152     #  @param cls The class pointer.
153     @classmethod
154     def setUpClass(cls):
155         super(TestGeneve, cls).setUpClass()
156
157         try:
158             cls.dport = 6081
159
160             # Create 2 pg interfaces.
161             cls.create_pg_interfaces(range(4))
162             for pg in cls.pg_interfaces:
163                 pg.admin_up()
164
165             # Configure IPv4 addresses on VPP pg0.
166             cls.pg0.config_ip4()
167
168             # Resolve MAC address for VPP's IP address on pg0.
169             cls.pg0.resolve_arp()
170
171             # Our Multicast address
172             cls.mcast_ip4 = '239.1.1.1'
173             cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
174             iplong = atol(cls.mcast_ip4)
175             cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
176                 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
177
178             # Create GENEVE VTEP on VPP pg0, and put geneve_tunnel0 and pg1
179             #  into BD.
180             cls.single_tunnel_bd = 1
181             r = cls.vapi.geneve_add_del_tunnel(
182                 local_addr=cls.pg0.local_ip4n,
183                 remote_addr=cls.pg0.remote_ip4n,
184                 vni=cls.single_tunnel_bd)
185             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
186                                                 bd_id=cls.single_tunnel_bd)
187             cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
188                                                 bd_id=cls.single_tunnel_bd)
189
190             # Setup vni 2 to test multicast flooding
191             cls.n_ucast_tunnels = 10
192             cls.mcast_flood_bd = 2
193             cls.create_geneve_flood_test_bd(cls.mcast_flood_bd,
194                                             cls.n_ucast_tunnels)
195             r = cls.vapi.geneve_add_del_tunnel(
196                 local_addr=cls.pg0.local_ip4n,
197                 remote_addr=cls.mcast_ip4n,
198                 mcast_sw_if_index=1,
199                 vni=cls.mcast_flood_bd)
200             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
201                                                 bd_id=cls.mcast_flood_bd)
202             cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
203                                                 bd_id=cls.mcast_flood_bd)
204
205             # Add and delete mcast tunnels to check stability
206             cls.add_shared_mcast_dst_load()
207             cls.add_mcast_tunnels_load()
208             cls.del_shared_mcast_dst_load()
209             cls.del_mcast_tunnels_load()
210
211             # Setup vni 3 to test unicast flooding
212             cls.ucast_flood_bd = 3
213             cls.create_geneve_flood_test_bd(cls.ucast_flood_bd,
214                                             cls.n_ucast_tunnels)
215             cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
216                                                 bd_id=cls.ucast_flood_bd)
217         except Exception:
218             super(TestGeneve, cls).tearDownClass()
219             raise
220
221     # Method to define VPP actions before tear down of the test case.
222     #  Overrides tearDown method in VppTestCase class.
223     #  @param self The object pointer.
224     def tearDown(self):
225         super(TestGeneve, self).tearDown()
226         if not self.vpp_dead:
227             self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
228             self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
229             self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
230             self.logger.info(self.vapi.cli("show geneve tunnel"))
231
232
233 if __name__ == '__main__':
234     unittest.main(testRunner=VppTestRunner)