blob: 7959b981837542836184418f5fe3aab78ac12629 [file] [log] [blame]
Pavel Kotuceke88865d2018-11-28 07:42:11 +01001#!/usr/bin/env python
2import binascii
3import random
4import socket
5import unittest
6import os
7import scapy.layers.inet6 as inet6
Pavel Kotucek9edb83a2018-12-11 16:57:25 +01008import threading
9import struct
Pavel Kotuceke88865d2018-11-28 07:42:11 +010010
Pavel Kotucek9edb83a2018-12-11 16:57:25 +010011from struct import unpack, unpack_from
Pavel Kotuceke88865d2018-11-28 07:42:11 +010012from util import ppp, ppc
13from re import compile
14from scapy.packet import Raw
15from scapy.layers.l2 import Ether
16from scapy.layers.inet import IP, UDP, ICMP
17from scapy.layers.inet6 import IPv6, ICMPv6DestUnreach
18from framework import VppTestCase, VppTestRunner
19
20
Pavel Kotucek9edb83a2018-12-11 16:57:25 +010021# Format MAC Address
22def get_mac_addr(bytes_addr):
23 return ':'.join('%02x' % ord(b) for b in bytes_addr)
24
25
26# Format IP Address
27def ipv4(bytes_addr):
28 return '.'.join('%d' % ord(b) for b in bytes_addr)
29
30
31# Unpack Ethernet Frame
32def ethernet_frame(data):
33 dest_mac, src_mac, proto = struct.unpack('! 6s 6s H', data[:14])
34 return dest_mac, src_mac, socket.htons(proto), data[14:]
35
36
37# Unpack IPv4 Packets
38def ipv4_packet(data):
39 proto, src, target = struct.unpack('! 8x 1x B 2x 4s 4s', data[:20])
40 return proto, src, target, data[20:]
41
42
43# Unpack IPv6 Packets
44def ipv6_packet(data):
45 nh, src, target = struct.unpack('! 6x B 1x 16s 16s', data[:40])
46 return nh, src, target, data[40:]
47
48
49# Unpacks any UDP Packet
50def udp_seg(data):
51 src_port, dest_port, size = struct.unpack('! H H 2x H', data[:8])
52 return src_port, dest_port, size, data[8:]
53
54
55# Unpacks any TCP Packet
56def tcp_seg(data):
57 src_port, dest_port, seq, flag = struct.unpack('! H H L 4x H', data[:14])
58 return src_port, dest_port, seq, data[((flag >> 12) * 4):]
59
60
61def receivePackets(sock, counters):
62 # Wait for some packets on socket
63 while True:
64 data = sock.recv(65536)
65
66 # punt socket metadata
67 # packet_desc = data[0:8]
68
69 # Ethernet
70 _, _, eth_proto, data = ethernet_frame(data[8:])
71 # Ipv4
72 if eth_proto == 8:
73 proto, _, _, data = ipv4_packet(data)
74 # TCP
75 if proto == 6:
76 _, dst_port, _, data = udp_seg(data)
77 # UDP
78 elif proto == 17:
79 _, dst_port, _, data = udp_seg(data)
80 counters[dst_port] = 0
81 # Ipv6
82 elif eth_proto == 0xdd86:
83 nh, _, _, data = ipv6_packet(data)
84 # TCP
85 if nh == 6:
86 _, dst_port, _, data = udp_seg(data)
87 # UDP
88 elif nh == 17:
89 _, dst_port, _, data = udp_seg(data)
90 counters[dst_port] = 0
91
92
93class serverSocketThread(threading.Thread):
94 """ Socket server thread"""
95
96 def __init__(self, threadID, sockName, counters):
97 threading.Thread.__init__(self)
98 self.threadID = threadID
99 self.sockName = sockName
100 self.sock = None
101 self.counters = counters
102
103 def run(self):
104 self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
105 try:
106 os.unlink(self.sockName)
107 except:
108 pass
109 self.sock.bind(self.sockName)
110
111 receivePackets(self.sock, self.counters)
112
113
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100114class TestPuntSocket(VppTestCase):
115 """ Punt Socket """
116
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100117 ports = [1111, 2222, 3333, 4444]
118 sock_servers = list()
119 portsCheck = dict()
120 nr_packets = 256
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100121
122 @classmethod
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800123 def setUpClass(cls):
124 super(TestPuntSocket, cls).setUpClass()
125
126 @classmethod
127 def tearDownClass(cls):
128 super(TestPuntSocket, cls).tearDownClass()
129
130 @classmethod
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100131 def setUpConstants(cls):
Ole Troana45dc072018-12-21 16:04:22 +0100132 cls.extra_vpp_punt_config = [
133 "punt", "{", "socket", cls.tempdir+"/socket_punt", "}"]
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100134 super(TestPuntSocket, cls).setUpConstants()
135
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100136 def setUp(self):
137 super(TestPuntSocket, self).setUp()
138 random.seed()
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100139
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100140 self.create_pg_interfaces(range(2))
141 for i in self.pg_interfaces:
142 i.admin_up()
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100143
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100144 def tearDown(self):
145 del self.sock_servers[:]
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800146 super(TestPuntSocket, self).tearDown()
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100147
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100148 def socket_client_create(self, sock_name, id=None):
149 thread = serverSocketThread(id, sock_name, self.portsCheck)
150 self.sock_servers.append(thread)
151 thread.start()
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100152
153 def socket_client_close(self):
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100154 for thread in self.sock_servers:
155 thread.sock.close()
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100156
157
158class TestIP4PuntSocket(TestPuntSocket):
159 """ Punt Socket for IPv4 """
160
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800161 @classmethod
162 def setUpClass(cls):
163 super(TestIP4PuntSocket, cls).setUpClass()
164
165 @classmethod
166 def tearDownClass(cls):
167 super(TestIP4PuntSocket, cls).tearDownClass()
168
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100169 def setUp(self):
170 super(TestIP4PuntSocket, self).setUp()
171
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100172 for i in self.pg_interfaces:
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100173 i.config_ip4()
174 i.resolve_arp()
175
176 def tearDown(self):
177 super(TestIP4PuntSocket, self).tearDown()
178 for i in self.pg_interfaces:
179 i.unconfig_ip4()
180 i.admin_down()
181
182 def test_punt_socket_dump(self):
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100183 """ Punt socket registration/deregistration"""
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100184
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100185 punts = self.vapi.punt_socket_dump(is_ip6=0)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100186 self.assertEqual(len(punts), 0)
187
188 #
189 # configure a punt socket
190 #
191 self.vapi.punt_socket_register(1111, self.tempdir+"/socket_punt_1111")
192 self.vapi.punt_socket_register(2222, self.tempdir+"/socket_punt_2222")
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100193 punts = self.vapi.punt_socket_dump(is_ip6=0)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100194 self.assertEqual(len(punts), 2)
195 self.assertEqual(punts[0].punt.l4_port, 1111)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100196 self.assertEqual(punts[1].punt.l4_port, 2222)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100197
198 #
199 # deregister a punt socket
200 #
201 self.vapi.punt_socket_deregister(1111)
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100202 punts = self.vapi.punt_socket_dump(is_ip6=0)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100203 self.assertEqual(len(punts), 1)
204
205 #
206 # configure a punt socket again
207 #
208 self.vapi.punt_socket_register(1111, self.tempdir+"/socket_punt_1111")
209 self.vapi.punt_socket_register(3333, self.tempdir+"/socket_punt_3333")
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100210 punts = self.vapi.punt_socket_dump(is_ip6=0)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100211 self.assertEqual(len(punts), 3)
212
213 #
214 # deregister all punt socket
215 #
216 self.vapi.punt_socket_deregister(1111)
217 self.vapi.punt_socket_deregister(2222)
218 self.vapi.punt_socket_deregister(3333)
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100219 punts = self.vapi.punt_socket_dump(is_ip6=0)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100220 self.assertEqual(len(punts), 0)
221
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100222 def test_punt_socket_traffic_single_port_single_socket(self):
223 """ Punt socket traffic single port single socket"""
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100224
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100225 port = self.ports[0]
226
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100227 p = (Ether(src=self.pg0.remote_mac,
228 dst=self.pg0.local_mac) /
229 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100230 UDP(sport=9876, dport=port) /
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100231 Raw('\xa5' * 100))
232
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100233 pkts = p * self.nr_packets
234 self.portsCheck[port] = self.nr_packets
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100235
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100236 punts = self.vapi.punt_socket_dump(is_ip6=0)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100237 self.assertEqual(len(punts), 0)
238
239 #
240 # expect ICMP - port unreachable for all packets
241 #
242 self.vapi.cli("clear trace")
243 self.pg0.add_stream(pkts)
244 self.pg_enable_capture(self.pg_interfaces)
245 self.pg_start()
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100246 # FIXME - when punt socket deregister is implemented
247 # rx = self.pg0.get_capture(self.nr_packets)
248 # for p in rx:
249 # self.assertEqual(int(p[IP].proto), 1) # ICMP
250 # self.assertEqual(int(p[ICMP].code), 3) # unreachable
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100251
252 #
253 # configure a punt socket
254 #
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100255 self.socket_client_create(self.tempdir+"/socket_" + str(port))
256 self.vapi.punt_socket_register(port, self.tempdir+"/socket_" +
257 str(port))
258 punts = self.vapi.punt_socket_dump(is_ip6=0)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100259 self.assertEqual(len(punts), 1)
260
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100261 self.logger.debug("Sending %s packets to port %d",
262 str(self.portsCheck[port]), port)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100263 #
264 # expect punt socket and no packets on pg0
265 #
266 self.vapi.cli("clear errors")
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100267 self.vapi.cli("clear trace")
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100268 self.pg0.add_stream(pkts)
269 self.pg_enable_capture(self.pg_interfaces)
270 self.pg_start()
271 self.pg0.get_capture(0)
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100272 self.logger.info(self.vapi.cli("show trace"))
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100273 self.socket_client_close()
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100274 self.assertEqual(self.portsCheck[port], 0)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100275
276 #
277 # remove punt socket. expect ICMP - port unreachable for all packets
278 #
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100279 self.vapi.punt_socket_deregister(port)
280 punts = self.vapi.punt_socket_dump(is_ip6=0)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100281 self.assertEqual(len(punts), 0)
282 self.pg0.add_stream(pkts)
283 self.pg_enable_capture(self.pg_interfaces)
284 self.pg_start()
285 # FIXME - when punt socket deregister is implemented
286 # self.pg0.get_capture(nr_packets)
287
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100288 def test_punt_socket_traffic_multi_port_multi_sockets(self):
289 """ Punt socket traffic multi ports and multi sockets"""
290
291 for p in self.ports:
292 self.portsCheck[p] = 0
293
294 #
295 # create stream with random pakets count per given ports
296 #
297 pkts = list()
298 for _ in range(0, self.nr_packets):
299 # choose port from port list
300 p = random.choice(self.ports)
301 pkts.append((
302 Ether(src=self.pg0.remote_mac,
303 dst=self.pg0.local_mac) /
304 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
305 UDP(sport=9876, dport=p) /
306 Raw('\xa5' * 100)))
307 self.portsCheck[p] += 1
308 #
309 # no punt socket
310 #
311 punts = self.vapi.punt_socket_dump(is_ip6=0)
312 self.assertEqual(len(punts), 0)
313
314 #
315 # configure a punt socket
316 #
317 for p in self.ports:
318 self.socket_client_create(self.tempdir+"/socket_" + str(p))
319 self.vapi.punt_socket_register(p, self.tempdir+"/socket_" + str(p))
320 punts = self.vapi.punt_socket_dump(is_ip6=0)
321 self.assertEqual(len(punts), len(self.ports))
322
323 for p in self.ports:
324 self.logger.debug("Sending %s packets to port %d",
325 str(self.portsCheck[p]), p)
326
327 #
328 # expect punt socket and no packets on pg0
329 #
330 self.vapi.cli("clear errors")
331 self.vapi.cli("clear trace")
332 self.pg0.add_stream(pkts)
333 self.pg_enable_capture(self.pg_interfaces)
334 self.pg_start()
335 self.pg0.get_capture(0)
336 self.logger.info(self.vapi.cli("show trace"))
337 self.socket_client_close()
338
339 for p in self.ports:
340 self.assertEqual(self.portsCheck[p], 0)
341 self.vapi.punt_socket_deregister(p)
342 punts = self.vapi.punt_socket_dump(is_ip6=0)
343 self.assertEqual(len(punts), 0)
344
345 def test_punt_socket_traffic_multi_ports_single_socket(self):
346 """ Punt socket traffic multi ports and single socket"""
347
348 for p in self.ports:
349 self.portsCheck[p] = 0
350
351 #
352 # create stream with random pakets count per given ports
353 #
354 pkts = list()
355 for _ in range(0, self.nr_packets):
356 # choose port from port list
357 p = random.choice(self.ports)
358 pkts.append((
359 Ether(src=self.pg0.remote_mac,
360 dst=self.pg0.local_mac) /
361 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
362 UDP(sport=9876, dport=p) /
363 Raw('\xa5' * 100)))
364 self.portsCheck[p] += 1
365
366 #
367 # no punt socket
368 #
369 punts = self.vapi.punt_socket_dump(is_ip6=0)
370 self.assertEqual(len(punts), 0)
371
372 # configure a punt socket
373 #
374 self.socket_client_create(self.tempdir+"/socket_multi")
375 for p in self.ports:
376 self.vapi.punt_socket_register(p, self.tempdir+"/socket_multi")
377 punts = self.vapi.punt_socket_dump(is_ip6=0)
378 self.assertEqual(len(punts), len(self.ports))
379
380 for p in self.ports:
381 self.logger.debug("Sending %s packets to port %d",
382 str(self.portsCheck[p]), p)
383 #
384 # expect punt socket and no packets on pg0
385 #
386 self.vapi.cli("clear errors")
387 self.vapi.cli("clear trace")
388 self.pg0.add_stream(pkts)
389 self.pg_enable_capture(self.pg_interfaces)
390 self.pg_start()
391 self.pg0.get_capture(0)
392 self.logger.info(self.vapi.cli("show trace"))
393 self.socket_client_close()
394
395 for p in self.ports:
396 self.assertEqual(self.portsCheck[p], 0)
397 self.vapi.punt_socket_deregister(p)
398 punts = self.vapi.punt_socket_dump(is_ip6=0)
399 self.assertEqual(len(punts), 0)
400
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100401
402class TestIP6PuntSocket(TestPuntSocket):
403 """ Punt Socket for IPv6"""
404
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800405 @classmethod
406 def setUpClass(cls):
407 super(TestIP6PuntSocket, cls).setUpClass()
408
409 @classmethod
410 def tearDownClass(cls):
411 super(TestIP6PuntSocket, cls).tearDownClass()
412
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100413 def setUp(self):
414 super(TestIP6PuntSocket, self).setUp()
415
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100416 for i in self.pg_interfaces:
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100417 i.config_ip6()
418 i.resolve_ndp()
419
420 def tearDown(self):
421 super(TestIP6PuntSocket, self).tearDown()
422 for i in self.pg_interfaces:
423 i.unconfig_ip6()
424 i.admin_down()
425
426 def test_punt_socket_dump(self):
427 """ Punt socket registration """
428
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100429 punts = self.vapi.punt_socket_dump(is_ip6=1)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100430 self.assertEqual(len(punts), 0)
431
432 #
433 # configure a punt socket
434 #
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100435 self.vapi.punt_socket_register(1111, self.tempdir+"/socket_1111",
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100436 is_ip4=0)
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100437 self.vapi.punt_socket_register(2222, self.tempdir+"/socket_2222",
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100438 is_ip4=0)
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100439 punts = self.vapi.punt_socket_dump(is_ip6=1)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100440 self.assertEqual(len(punts), 2)
441 self.assertEqual(punts[0].punt.l4_port, 1111)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100442 self.assertEqual(punts[1].punt.l4_port, 2222)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100443
444 #
445 # deregister a punt socket
446 #
447 self.vapi.punt_socket_deregister(1111, is_ip4=0)
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100448 punts = self.vapi.punt_socket_dump(is_ip6=1)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100449 self.assertEqual(len(punts), 1)
450
451 #
452 # configure a punt socket again
453 #
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100454 self.vapi.punt_socket_register(1111, self.tempdir+"/socket_1111",
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100455 is_ip4=0)
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100456 punts = self.vapi.punt_socket_dump(is_ip6=1)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100457 self.assertEqual(len(punts), 2)
458
459 #
460 # deregister all punt socket
461 #
462 self.vapi.punt_socket_deregister(1111, is_ip4=0)
463 self.vapi.punt_socket_deregister(2222, is_ip4=0)
464 self.vapi.punt_socket_deregister(3333, is_ip4=0)
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100465 punts = self.vapi.punt_socket_dump(is_ip6=1)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100466 self.assertEqual(len(punts), 0)
467
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100468 def test_punt_socket_traffic_single_port_single_socket(self):
469 """ Punt socket traffic single port single socket"""
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100470
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100471 port = self.ports[0]
472
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100473 p = (Ether(src=self.pg0.remote_mac,
474 dst=self.pg0.local_mac) /
475 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100476 inet6.UDP(sport=9876, dport=port) /
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100477 Raw('\xa5' * 100))
478
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100479 pkts = p * self.nr_packets
480 self.portsCheck[port] = self.nr_packets
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100481
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100482 punts = self.vapi.punt_socket_dump(is_ip6=1)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100483 self.assertEqual(len(punts), 0)
484
485 #
486 # expect ICMPv6 - destination unreachable for all packets
487 #
488 self.vapi.cli("clear trace")
489 self.pg0.add_stream(pkts)
490 self.pg_enable_capture(self.pg_interfaces)
491 self.pg_start()
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100492 # FIXME - when punt socket deregister is implemented
493 # rx = self.pg0.get_capture(self.nr_packets)
494 # for p in rx:
495 # self.assertEqual(int(p[IPv6].nh), 58) # ICMPv6
496 # self.assertEqual(int(p[ICMPv6DestUnreach].code),4) # unreachable
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100497
498 #
499 # configure a punt socket
500 #
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100501 self.socket_client_create(self.tempdir+"/socket_" + str(port))
502 self.vapi.punt_socket_register(port, self.tempdir+"/socket_" +
503 str(port), is_ip4=0)
504 punts = self.vapi.punt_socket_dump(is_ip6=1)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100505 self.assertEqual(len(punts), 1)
506
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100507 self.logger.debug("Sending %s packets to port %d",
508 str(self.portsCheck[port]), port)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100509 #
510 # expect punt socket and no packets on pg0
511 #
512 self.vapi.cli("clear errors")
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100513 self.vapi.cli("clear trace")
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100514 self.pg0.add_stream(pkts)
515 self.pg_enable_capture(self.pg_interfaces)
516 self.pg_start()
517 self.pg0.get_capture(0)
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100518 self.logger.info(self.vapi.cli("show trace"))
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100519 self.socket_client_close()
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100520 self.assertEqual(self.portsCheck[port], 0)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100521
522 #
523 # remove punt socket. expect ICMP - dest. unreachable for all packets
524 #
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100525 self.vapi.punt_socket_deregister(port, is_ip4=0)
526 punts = self.vapi.punt_socket_dump(is_ip6=1)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100527 self.assertEqual(len(punts), 0)
528 self.pg0.add_stream(pkts)
529 self.pg_enable_capture(self.pg_interfaces)
530 self.pg_start()
531 # FIXME - when punt socket deregister is implemented
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100532 # self.pg0.get_capture(nr_packets)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100533
Pavel Kotucek9edb83a2018-12-11 16:57:25 +0100534 def test_punt_socket_traffic_multi_port_multi_sockets(self):
535 """ Punt socket traffic multi ports and multi sockets"""
536
537 for p in self.ports:
538 self.portsCheck[p] = 0
539
540 #
541 # create stream with random pakets count per given ports
542 #
543 pkts = list()
544 for _ in range(0, self.nr_packets):
545 # choose port from port list
546 p = random.choice(self.ports)
547 pkts.append((
548 Ether(src=self.pg0.remote_mac,
549 dst=self.pg0.local_mac) /
550 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
551 inet6.UDP(sport=9876, dport=p) /
552 Raw('\xa5' * 100)))
553 self.portsCheck[p] += 1
554 #
555 # no punt socket
556 #
557 punts = self.vapi.punt_socket_dump(is_ip6=1)
558 self.assertEqual(len(punts), 0)
559
560 #
561 # configure a punt socket
562 #
563 for p in self.ports:
564 self.socket_client_create(self.tempdir+"/socket_" + str(p))
565 self.vapi.punt_socket_register(p, self.tempdir+"/socket_" + str(p),
566 is_ip4=0)
567 punts = self.vapi.punt_socket_dump(is_ip6=1)
568 self.assertEqual(len(punts), len(self.ports))
569
570 for p in self.ports:
571 self.logger.debug("Sending %s packets to port %d",
572 str(self.portsCheck[p]), p)
573
574 #
575 # expect punt socket and no packets on pg0
576 #
577 self.vapi.cli("clear errors")
578 self.vapi.cli("clear trace")
579 self.pg0.add_stream(pkts)
580 self.pg_enable_capture(self.pg_interfaces)
581 self.pg_start()
582 self.pg0.get_capture(0)
583 self.logger.info(self.vapi.cli("show trace"))
584 self.socket_client_close()
585
586 for p in self.ports:
587 self.assertEqual(self.portsCheck[p], 0)
588 self.vapi.punt_socket_deregister(p, is_ip4=0)
589 punts = self.vapi.punt_socket_dump(is_ip6=1)
590 self.assertEqual(len(punts), 0)
591
592 def test_punt_socket_traffic_multi_ports_single_socket(self):
593 """ Punt socket traffic multi ports and single socket"""
594
595 for p in self.ports:
596 self.portsCheck[p] = 0
597
598 #
599 # create stream with random pakets count per given ports
600 #
601 pkts = list()
602 for _ in range(0, self.nr_packets):
603 # choose port from port list
604 p = random.choice(self.ports)
605 pkts.append((
606 Ether(src=self.pg0.remote_mac,
607 dst=self.pg0.local_mac) /
608 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
609 inet6.UDP(sport=9876, dport=p) /
610 Raw('\xa5' * 100)))
611 self.portsCheck[p] += 1
612
613 #
614 # no punt socket
615 #
616 punts = self.vapi.punt_socket_dump(is_ip6=1)
617 self.assertEqual(len(punts), 0)
618
619 #
620 # configure a punt socket
621 #
622 self.socket_client_create(self.tempdir+"/socket_multi")
623 for p in self.ports:
624 self.vapi.punt_socket_register(p, self.tempdir+"/socket_multi",
625 is_ip4=0)
626 punts = self.vapi.punt_socket_dump(is_ip6=1)
627 self.assertEqual(len(punts), len(self.ports))
628
629 for p in self.ports:
630 self.logger.debug("Send %s packets to port %d",
631 str(self.portsCheck[p]), p)
632 #
633 # expect punt socket and no packets on pg0
634 #
635 self.vapi.cli("clear errors")
636 self.vapi.cli("clear trace")
637 self.pg0.add_stream(pkts)
638 self.pg_enable_capture(self.pg_interfaces)
639 self.pg_start()
640 self.pg0.get_capture(0)
641 self.logger.info(self.vapi.cli("show trace"))
642 self.socket_client_close()
643
644 for p in self.ports:
645 self.assertEqual(self.portsCheck[p], 0)
646 self.vapi.punt_socket_deregister(p, is_ip4=0)
647 punts = self.vapi.punt_socket_dump(is_ip6=1)
648 self.assertEqual(len(punts), 0)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100649
650if __name__ == '__main__':
651 unittest.main(testRunner=VppTestRunner)