blob: a896698c7b40628fccffa9162caec0d4437d84ac [file] [log] [blame]
Filip Tehlar770e89e2017-01-31 10:39:16 +01001#!/usr/bin/env python
2import unittest
3
4from scapy.packet import Raw
5from scapy.layers.inet import IP, UDP, Ether
6from py_lispnetworking.lisp import LISP_GPE_Header
7
8from util import ppp, ForeignAddressFactory
9from framework import VppTestCase, VppTestRunner
10from lisp import *
11
12
13class Driver(object):
14
15 config_order = ['locator-sets',
16 'locators',
17 'local-mappings',
18 'remote-mappings',
19 'adjacencies']
20
21 """ Basic class for data driven testing """
22 def __init__(self, test, test_cases):
23 self._test_cases = test_cases
24 self._test = test
25
26 @property
27 def test_cases(self):
28 return self._test_cases
29
30 @property
31 def test(self):
32 return self._test
33
34 def create_packet(self, src_if, dst_if, deid, payload=''):
35 """
36 Create IPv4 packet
37
38 param: src_if
39 param: dst_if
40 """
41 packet = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
42 IP(src=src_if.remote_ip4, dst=deid) /
43 Raw(payload))
44 return packet
45
46 @abstractmethod
47 def run(self):
48 """ testing procedure """
49 pass
50
51
52class SimpleDriver(Driver):
53 """ Implements simple test procedure """
54 def __init__(self, test, test_cases):
55 super(SimpleDriver, self).__init__(test, test_cases)
56
57 def verify_capture(self, src_loc, dst_loc, capture):
58 """
59 Verify captured packet
60
61 :param src_loc: source locator address
62 :param dst_loc: destination locator address
63 :param capture: list of captured packets
64 """
65 self.test.assertEqual(len(capture), 1, "Unexpected number of "
66 "packets! Expected 1 but {} received"
67 .format(len(capture)))
68 packet = capture[0]
69 try:
70 ip_hdr = packet[IP]
71 # assert the values match
72 self.test.assertEqual(ip_hdr.src, src_loc, "IP source address")
73 self.test.assertEqual(ip_hdr.dst, dst_loc,
74 "IP destination address")
75 gpe_hdr = packet[LISP_GPE_Header]
76 self.test.assertEqual(gpe_hdr.next_proto, 1,
77 "next_proto is not ipv4!")
78 ih = gpe_hdr[IP]
79 self.test.assertEqual(ih.src, self.test.pg0.remote_ip4,
80 "unexpected source EID!")
81 self.test.assertEqual(ih.dst, self.test.deid_ip4,
82 "unexpected dest EID!")
83 except:
84 self.test.logger.error(ppp("Unexpected or invalid packet:",
85 packet))
86 raise
87
88 def configure_tc(self, tc):
89 for config_item in self.config_order:
90 for vpp_object in tc[config_item]:
91 vpp_object.add_vpp_config()
92
93 def run(self, dest):
94 """ Send traffic for each test case and verify that it
95 is encapsulated """
96 for tc in enumerate(self.test_cases):
97 self.test.logger.info('Running {}'.format(tc[1]['name']))
98 self.configure_tc(tc[1])
99
100 print self.test.vapi.cli("sh lisp loc")
101 print self.test.vapi.cli("sh lisp eid")
102 print self.test.vapi.cli("sh lisp adj vni 0")
103 print self.test.vapi.cli("sh lisp gpe entry")
104
105 packet = self.create_packet(self.test.pg0, self.test.pg1, dest,
106 'data')
107 self.test.pg0.add_stream(packet)
108 self.test.pg0.enable_capture()
109 self.test.pg1.enable_capture()
110 self.test.pg_start()
111 capture = self.test.pg1.get_capture(1)
112 self.verify_capture(self.test.pg1.local_ip4,
113 self.test.pg1.remote_ip4, capture)
114 self.test.pg0.assert_nothing_captured()
115
116
117class TestLisp(VppTestCase):
118 """ Basic LISP test """
119
120 @classmethod
121 def setUpClass(cls):
122 super(TestLisp, cls).setUpClass()
123 cls.faf = ForeignAddressFactory()
124 cls.create_pg_interfaces(range(2)) # create pg0 and pg1
125 for i in cls.pg_interfaces:
126 i.admin_up() # put the interface upsrc_if
127 i.config_ip4() # configure IPv4 address on the interface
128 i.resolve_arp() # resolve ARP, so that we know VPP MAC
129
130 def setUp(self):
131 super(TestLisp, self).setUp()
132 self.vapi.lisp_enable_disable(is_enabled=1)
133
134 def test_lisp_basic_encap(self):
135 """Test case for basic encapsulation"""
136
137 self.deid_ip4_net = self.faf.net
138 self.deid_ip4 = self.faf.get_ip4()
139 self.seid_ip4 = '{}/{}'.format(self.pg0.local_ip4, 32)
140 self.rloc_ip4 = self.pg1.remote_ip4n
141
142 test_cases = [
143 {
144 'name': 'basic ip4 over ip4',
145 'locator-sets': [VppLispLocatorSet(self, 'ls-4o4')],
146 'locators': [
147 VppLispLocator(self, self.pg1.sw_if_index, 'ls-4o4')
148 ],
149 'local-mappings': [
150 VppLocalMapping(self, self.seid_ip4, 'ls-4o4')
151 ],
152 'remote-mappings': [
153 VppRemoteMapping(self, self.deid_ip4_net,
154 [{
155 "is_ip4": 1,
156 "priority": 1,
157 "weight": 1,
158 "addr": self.rloc_ip4
159 }])
160 ],
161 'adjacencies': [
162 VppLispAdjacency(self, self.seid_ip4, self.deid_ip4_net)
163 ]
164 }
165 ]
166 self.test_driver = SimpleDriver(self, test_cases)
167 self.test_driver.run(self.deid_ip4)
168
169
170if __name__ == '__main__':
171 unittest.main(testRunner=VppTestRunner)