blob: 22b6d2f02e3a4a7a824959b686a1205bd59224d2 [file] [log] [blame]
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001#!/usr/bin/env python
2
3import unittest
4import socket
5
6from framework import VppTestCase, VppTestRunner, running_extended_tests
7from vpp_igmp import *
8
9from scapy.packet import Raw
10from scapy.layers.l2 import Ether
11from scapy.layers.inet import IP
12from scapy.contrib.igmpv3 import *
13from scapy.contrib.igmp import *
14
15
Jakub Grajciar7b867a82017-12-08 16:28:42 +010016class TestIgmp(VppTestCase):
17 """ IGMP Test Case """
18
19 def setUp(self):
20 super(TestIgmp, self).setUp()
21
22 self.create_pg_interfaces(range(2))
23 self.sg_list = []
24 self.config_list = []
25
26 self.ip_addr = []
27 for pg in self.pg_interfaces:
28 pg.admin_up()
29 pg.config_ip4()
30 pg.resolve_arp()
31
32 def tearDown(self):
33 for pg in self.pg_interfaces:
34 self.vapi.igmp_clear_interface(pg.sw_if_index)
35 pg.unconfig_ip4()
36 pg.admin_down()
37 super(TestIgmp, self).tearDown()
38
39 def send(self, ti, pkts):
40 ti.add_stream(pkts)
41 self.pg_enable_capture(self.pg_interfaces)
42 self.pg_start()
43
Jakub Grajciar7b867a82017-12-08 16:28:42 +010044 def test_igmp_parse_report(self):
45 """ IGMP parse Membership Report """
46
47 #
48 # VPP acts as a router
49 #
50 self.vapi.want_igmp_events(1)
51
52 # hos sends join IGMP 'join'
53 p_join = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Neale Rannsc667ffd2018-06-27 18:59:03 -070054 IP(src=self.pg0.remote_ip4, dst='224.0.0.22',
55 tos=0xc0, ttl=1,
56 options=IPOption(copy_flag=1, optclass=0,
57 option="router_alert",
58 length=2, value=0)) /
Jakub Grajciar7b867a82017-12-08 16:28:42 +010059 IGMPv3() /
60 IGMPv3mr(numgrp=1) /
61 IGMPv3gr(rtype=3, maddr="224.1.1.1", srcaddrs=["10.1.1.1"]))
62
63 self.send(self.pg0, p_join)
64
65 # search for the corresponding state created in VPP
66 dump = self.vapi.igmp_dump()
67 self.assertEqual(len(dump), 1)
68 self.assertEqual(dump[0].sw_if_index, self.pg0.sw_if_index)
69 self.assertEqual(dump[0].gaddr,
70 socket.inet_pton(socket.AF_INET,
71 "224.1.1.1"))
72 self.assertEqual(dump[0].saddr,
73 socket.inet_pton(socket.AF_INET,
74 "10.1.1.1"))
75
76 # VPP sends a notification that a new group has been joined
77 ev = self.vapi.wait_for_event(2, "igmp_event")
78
79 self.assertEqual(ev.saddr,
80 socket.inet_pton(socket.AF_INET,
81 "10.1.1.1"))
82 self.assertEqual(ev.gaddr,
83 socket.inet_pton(socket.AF_INET,
84 "224.1.1.1"))
85 self.assertEqual(ev.is_join, 1)
86
87 # host sends IGMP leave
88 p_leave = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
89 IP(src=self.pg0.remote_ip4, dst='224.0.0.22', tos=0xc0) /
90 IGMPv3() /
91 IGMPv3mr(numgrp=1) /
92 IGMPv3gr(rtype=4, maddr="224.1.1.1", srcaddrs=["10.1.1.1"]))
93
94 self.send(self.pg0, p_leave)
95
96 # VPP sends a notification that a new group has been left
97 ev = self.vapi.wait_for_event(2, "igmp_event")
98
99 self.assertEqual(ev.saddr,
100 socket.inet_pton(socket.AF_INET,
101 "10.1.1.1"))
102 self.assertEqual(ev.gaddr,
103 socket.inet_pton(socket.AF_INET,
104 "224.1.1.1"))
105 self.assertEqual(ev.is_join, 0)
106
107 # state gone
108 dump = self.vapi.igmp_dump()
109 self.assertFalse(dump)
110
111 # resend the join
112 self.send(self.pg0, p_join)
113 dump = self.vapi.igmp_dump()
114 self.assertEqual(len(dump), 1)
115 self.assertEqual(dump[0].sw_if_index, self.pg0.sw_if_index)
116 self.assertEqual(dump[0].gaddr,
117 socket.inet_pton(socket.AF_INET,
118 "224.1.1.1"))
119 self.assertEqual(dump[0].saddr,
120 socket.inet_pton(socket.AF_INET,
121 "10.1.1.1"))
122
123 # IGMP block
124 p_block = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
125 IP(src=self.pg0.remote_ip4, dst='224.0.0.22', tos=0xc0) /
126 IGMPv3() /
127 IGMPv3mr(numgrp=1) /
128 IGMPv3gr(rtype=6, maddr="224.1.1.1", srcaddrs=["10.1.1.1"]))
129
130 self.send(self.pg0, p_block)
131
132 dump = self.vapi.igmp_dump()
133 self.assertFalse(dump)
134
135 def verify_general_query(self, p):
136 ip = p[IP]
137 self.assertEqual(ip.dst, "224.0.0.1")
138 self.assertEqual(ip.proto, 2)
139 igmp = p[IGMPv3]
140 self.assertEqual(igmp.type, 0x11)
141 self.assertEqual(igmp.gaddr, "0.0.0.0")
142
Jakub Grajciar7b867a82017-12-08 16:28:42 +0100143 def test_igmp_send_query(self):
144 """ IGMP send General Query """
145
146 #
147 # VPP acts as a router.
148 # Send a membership report so VPP builds state
149 #
150 p_mr = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
151 IP(src=self.pg0.remote_ip4, dst='224.0.0.22') /
152 IGMPv3() /
153 IGMPv3mr(numgrp=1) /
154 IGMPv3gr(rtype=3, maddr="224.1.1.1", srcaddrs=["10.1.1.1"]))
155
156 self.send(self.pg0, p_mr)
157 self.logger.info(self.vapi.cli("sh igmp config"))
158
159 #
160 # wait for VPP to send out the General Query
161 #
162 capture = self.pg0.get_capture(1, timeout=61)
163
164 self.verify_general_query(capture[0])
165
166 #
167 # the state will expire in 10 more seconds
168 #
169 self.sleep(10)
170 self.assertFalse(self.vapi.igmp_dump())
171
Neale Ranns5237c772018-03-21 16:34:28 -0700172 @unittest.skipUnless(running_extended_tests(), "part of extended tests")
Jakub Grajciar7b867a82017-12-08 16:28:42 +0100173 def test_igmp_src_exp(self):
174 """ IGMP per source timer """
175
176 #
177 # VPP Acts as a router
178 #
179
180 # Host join for (10.1.1.1,224.1.1.1)
181 p_mr1 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
182 IP(src=self.pg0.remote_ip4, dst='224.0.0.22') /
183 IGMPv3() /
184 IGMPv3mr(numgrp=1) /
185 IGMPv3gr(rtype=3, maddr="224.1.1.1", srcaddrs=["10.1.1.1"]))
186
187 self.send(self.pg0, p_mr1)
188
189 # VPP (router) sends General Query
190 capture = self.pg0.get_capture(1, timeout=61)
191
192 self.verify_general_query(capture[0])
193
194 # host join for same G and another S: (10.1.1.2,224.1.1.1)
195 # therefore leaving (10.1.1.1,224.1.1.1)
196 p_mr2 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
197 IP(src=self.pg0.remote_ip4, dst='224.0.0.22') /
198 IGMPv3() /
199 IGMPv3mr(numgrp=1) /
200 IGMPv3gr(rtype=2, maddr="224.1.1.1", srcaddrs=["10.1.1.2"]))
201
202 self.send(self.pg0, p_mr2)
203
204 # wait for VPP to send general query
205 capture = self.pg0.get_capture(1, timeout=61)
206 self.verify_general_query(capture[0])
207
208 # host leaves (10.1.1.2,224.1.1.1)
209 p_l = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
210 IP(src=self.pg0.remote_ip4, dst='224.0.0.22') /
211 IGMPv3() /
212 IGMPv3mr(numgrp=1) /
213 IGMPv3gr(rtype=2, maddr="224.1.1.1", srcaddrs=["10.1.1.2"]))
214
215 self.send(self.pg0, p_l)
216
217 # FIXME BUG
218 p_l = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
219 IP(src=self.pg0.remote_ip4, dst='224.0.0.22') /
220 IGMPv3() /
221 IGMPv3mr(numgrp=1) /
222 IGMPv3gr(rtype=2, maddr="224.1.1.1", srcaddrs=["10.1.1.1"]))
223 self.send(self.pg0, p_l)
224
225 #
226 # host has left all groups, no state left.
227 #
228 self.sleep(10)
229 self.logger.error(self.vapi.cli("sh igmp config"))
230 self.assertFalse(self.vapi.igmp_dump())
231
Jakub Grajciar7b867a82017-12-08 16:28:42 +0100232 def test_igmp_query_resp(self):
233 """ IGMP General Query response """
234
235 #
236 # VPP acting as a host.
237 # Add a listener in VPP for (10.1.1.1,244.1.1.1)
238 #
239 self.config_list.append(
240 VppIgmpConfig(
241 self, self.pg0.sw_if_index, IgmpSG(
242 socket.inet_pton(
243 socket.AF_INET, "10.1.1.1"), socket.inet_pton(
244 socket.AF_INET, "224.1.1.1"))))
245 self.config_list[0].add_vpp_config()
246
247 # verify state exists
248 self.assertTrue(self.vapi.igmp_dump(self.pg0.sw_if_index))
249
250 #
251 # Send a general query (from a router)
252 #
253 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
254 IP(src=self.pg0.remote_ip4, dst='224.0.0.1', tos=0xc0) /
255 IGMPv3(type=0x11, mrcode=100) /
256 IGMPv3mq(gaddr="0.0.0.0"))
257
258 self.send(self.pg0, p)
259
260 #
261 # expect VPP to respond with a membership report for the
262 # (10.1.1.1, 224.1.1.1) state
263 #
264 capture = self.pg0.get_capture(1, timeout=10)
265
266 self.assertEqual(capture[0][IGMPv3].type, 0x22)
267 self.assertEqual(capture[0][IGMPv3mr].numgrp, 1)
268 self.assertEqual(capture[0][IGMPv3gr].rtype, 1)
269 self.assertEqual(capture[0][IGMPv3gr].numsrc, 1)
270 self.assertEqual(capture[0][IGMPv3gr].maddr, "224.1.1.1")
271 self.assertEqual(len(capture[0][IGMPv3gr].srcaddrs), 1)
272 self.assertEqual(capture[0][IGMPv3gr].srcaddrs[0], "10.1.1.1")
273
Jakub Grajciar7b867a82017-12-08 16:28:42 +0100274 def test_igmp_listen(self):
275 """ IGMP listen (S,G)s """
276
277 #
278 # VPP acts as a host
279 # Add IGMP group state to multiple interfaces and validate its
280 # presence
281 #
282 for pg in self.pg_interfaces:
283 self.sg_list.append(IgmpSG(socket.inet_pton(
284 socket.AF_INET, "10.1.1.%d" % pg._sw_if_index),
285 socket.inet_pton(socket.AF_INET, "224.1.1.1")))
286
287 for pg in self.pg_interfaces:
288 self.config_list.append(
289 VppIgmpConfig(
290 self,
291 pg._sw_if_index,
292 self.sg_list))
293 self.config_list[-1].add_vpp_config()
294
295 for config in self.config_list:
296 dump = self.vapi.igmp_dump(config.sw_if_index)
297 self.assertTrue(dump)
298 self.assertEqual(len(dump), len(config.sg_list))
299 for idx, e in enumerate(dump):
300 self.assertEqual(e.sw_if_index, config.sw_if_index)
301 self.assertEqual(e.saddr, config.sg_list[idx].saddr)
302 self.assertEqual(e.gaddr, config.sg_list[idx].gaddr)
303
304 for config in self.config_list:
305 config.remove_vpp_config()
306
307 dump = self.vapi.igmp_dump()
308 self.assertFalse(dump)
309
310
311if __name__ == '__main__':
312 unittest.main(testRunner=VppTestRunner)