1 from socket import socket, AF_UNIX, SOCK_DGRAM
2 from select import select
4 from struct import unpack, calcsize
6 from scapy.layers.l2 import Ether
8 client_uds_socket_name = "client-uds-socket"
9 vpp_uds_socket_name = "vpp-uds-socket"
11 VPP_PUNT_HEADER_FMT = '=Ii'
12 VPP_PUNT_HEADER_SIZE = calcsize(VPP_PUNT_HEADER_FMT)
21 class VppUDSPuntSocket(object):
22 def __init__(self, testcase, port, is_ip4=1, l4_protocol=0x11):
23 client_path = '%s/%s-%s-%s' % (testcase.tempdir,
24 client_uds_socket_name,
25 "4" if is_ip4 else "6", port)
26 testcase.vapi.punt_socket_register(
27 port, client_path, is_ip4=is_ip4, l4_protocol=l4_protocol)
28 self.testcase = testcase
29 self.uds = socket(AF_UNIX, SOCK_DGRAM)
30 self.uds.bind(client_path)
31 self.uds.connect(testcase.punt_socket_path)
33 def wait_for_packets(self, count, timeout=1):
36 deadline = now + timeout
37 while len(packets) < count and now < deadline:
38 r, w, e = select([self.uds], [], [self.uds], deadline - now)
40 x = self.uds.recv(1024 * 1024)
41 sw_if_index, punt_action = unpack(
42 VPP_PUNT_HEADER_FMT, x[:VPP_PUNT_HEADER_SIZE])
43 packets.append({'sw_if_index': sw_if_index,
44 'punt_action': punt_action,
45 'packet': x[VPP_PUNT_HEADER_SIZE:]})
48 raise Exception("select() indicates error on UDS socket")
51 if len(packets) != count:
52 raise Exception("Unexpected packet count received, got %s packets,"
53 " expected %s packets" % (len(packets), count))
54 self.testcase.logger.debug(
55 "Got %s packets via punt socket" % len(packets))
58 def assert_nothing_captured(self, timeout=.25):
61 deadline = now + timeout
63 r, w, e = select([self.uds], [], [self.uds], deadline - now)
65 x = self.uds.recv(1024 * 1024)
66 packets.append(Ether(x[VPP_PUNT_HEADER_SIZE:]))
68 raise Exception("select() indicates error on UDS socket")
72 self.testcase.logger.error(
73 ppc("Unexpected packets captured:", packets))
74 raise Exception("Unexpected packet count received, got %s packets,"
75 " expected no packets" % len(packets))