Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | import binascii |
| 3 | import random |
| 4 | import socket |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 5 | import os |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 6 | import threading |
| 7 | import struct |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 8 | import copy |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 9 | from struct import unpack, unpack_from |
Paul Vinciguerra | 22ab6f7 | 2019-03-07 17:55:33 -0800 | [diff] [blame] | 10 | |
| 11 | try: |
| 12 | import unittest2 as unittest |
| 13 | except ImportError: |
| 14 | import unittest |
| 15 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 16 | from util import ppp, ppc |
| 17 | from re import compile |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 18 | import scapy.compat |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 19 | from scapy.packet import Raw |
| 20 | from scapy.layers.l2 import Ether |
| 21 | from scapy.layers.inet import IP, UDP, ICMP |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 22 | from scapy.layers.ipsec import ESP |
Paul Vinciguerra | 22ab6f7 | 2019-03-07 17:55:33 -0800 | [diff] [blame] | 23 | import scapy.layers.inet6 as inet6 |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 24 | from scapy.layers.inet6 import IPv6, ICMPv6DestUnreach |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 25 | from scapy.contrib.ospf import OSPF_Hdr, OSPFv3_Hello |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 26 | from framework import VppTestCase, VppTestRunner |
| 27 | |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 28 | from vpp_ip import DpoProto |
| 29 | from vpp_ip_route import VppIpRoute, VppRoutePath |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 30 | from vpp_papi import VppEnum |
| 31 | from vpp_ipsec_tun_interface import VppIpsecTunInterface |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 32 | |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 33 | NUM_PKTS = 67 |
| 34 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 35 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 36 | class serverSocketThread(threading.Thread): |
| 37 | """ Socket server thread""" |
| 38 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 39 | def __init__(self, threadID, sockName): |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 40 | threading.Thread.__init__(self) |
| 41 | self.threadID = threadID |
| 42 | self.sockName = sockName |
| 43 | self.sock = None |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 44 | self.rx_pkts = [] |
| 45 | |
| 46 | def rx_packets(self): |
| 47 | # Wait for some packets on socket |
| 48 | while True: |
| 49 | data = self.sock.recv(65536) |
| 50 | |
| 51 | # punt socket metadata |
| 52 | # packet_desc = data[0:8] |
| 53 | |
| 54 | # Ethernet |
| 55 | self.rx_pkts.append(Ether(data[8:])) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 56 | |
| 57 | def run(self): |
| 58 | self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) |
| 59 | try: |
| 60 | os.unlink(self.sockName) |
| 61 | except: |
| 62 | pass |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 63 | self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 65536) |
| 64 | self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 65536) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 65 | self.sock.bind(self.sockName) |
| 66 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 67 | self.rx_packets() |
| 68 | |
| 69 | def close(self): |
| 70 | self.sock.close() |
| 71 | return self.rx_pkts |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 72 | |
| 73 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 74 | class TestPuntSocket(VppTestCase): |
| 75 | """ Punt Socket """ |
| 76 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 77 | ports = [1111, 2222, 3333, 4444] |
| 78 | sock_servers = list() |
Andrew Yourtchenko | cb265c6 | 2019-07-25 10:03:51 +0000 | [diff] [blame] | 79 | # FIXME: nr_packets > 3 results in failure |
| 80 | # nr_packets = 3 makes the test unstable |
| 81 | nr_packets = 2 |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 82 | |
| 83 | @classmethod |
Paul Vinciguerra | 8d991d9 | 2019-01-25 14:05:48 -0800 | [diff] [blame] | 84 | def setUpClass(cls): |
| 85 | super(TestPuntSocket, cls).setUpClass() |
| 86 | |
| 87 | @classmethod |
| 88 | def tearDownClass(cls): |
| 89 | super(TestPuntSocket, cls).tearDownClass() |
| 90 | |
| 91 | @classmethod |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 92 | def setUpConstants(cls): |
Ole Troan | a45dc07 | 2018-12-21 16:04:22 +0100 | [diff] [blame] | 93 | cls.extra_vpp_punt_config = [ |
| 94 | "punt", "{", "socket", cls.tempdir+"/socket_punt", "}"] |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 95 | super(TestPuntSocket, cls).setUpConstants() |
| 96 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 97 | def setUp(self): |
| 98 | super(TestPuntSocket, self).setUp() |
| 99 | random.seed() |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 100 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 101 | self.create_pg_interfaces(range(2)) |
| 102 | for i in self.pg_interfaces: |
| 103 | i.admin_up() |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 104 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 105 | def tearDown(self): |
| 106 | del self.sock_servers[:] |
Paul Vinciguerra | 8d991d9 | 2019-01-25 14:05:48 -0800 | [diff] [blame] | 107 | super(TestPuntSocket, self).tearDown() |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 108 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 109 | def socket_client_create(self, sock_name, id=None): |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 110 | thread = serverSocketThread(id, sock_name) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 111 | self.sock_servers.append(thread) |
| 112 | thread.start() |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 113 | return thread |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 114 | |
| 115 | def socket_client_close(self): |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 116 | rx_pkts = [] |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 117 | for thread in self.sock_servers: |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 118 | rx_pkts += thread.close() |
| 119 | return rx_pkts |
| 120 | |
| 121 | def verify_port(self, pr, vpr): |
| 122 | self.assertEqual(vpr.punt.type, pr['type']) |
| 123 | self.assertEqual(vpr.punt.punt.l4.port, |
| 124 | pr['punt']['l4']['port']) |
| 125 | self.assertEqual(vpr.punt.punt.l4.protocol, |
| 126 | pr['punt']['l4']['protocol']) |
| 127 | self.assertEqual(vpr.punt.punt.l4.af, |
| 128 | pr['punt']['l4']['af']) |
| 129 | |
| 130 | def verify_exception(self, pr, vpr): |
| 131 | self.assertEqual(vpr.punt.type, pr['type']) |
| 132 | self.assertEqual(vpr.punt.punt.exception.id, |
| 133 | pr['punt']['exception']['id']) |
| 134 | |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 135 | def verify_ip_proto(self, pr, vpr): |
| 136 | self.assertEqual(vpr.punt.type, pr['type']) |
| 137 | self.assertEqual(vpr.punt.punt.ip_proto.af, |
| 138 | pr['punt']['ip_proto']['af']) |
| 139 | self.assertEqual(vpr.punt.punt.ip_proto.protocol, |
| 140 | pr['punt']['ip_proto']['protocol']) |
| 141 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 142 | def verify_udp_pkts(self, rxs, n_rx, port): |
| 143 | n_match = 0 |
| 144 | for rx in rxs: |
| 145 | self.assertTrue(rx.haslayer(UDP)) |
| 146 | if rx[UDP].dport == port: |
| 147 | n_match += 1 |
| 148 | self.assertEqual(n_match, n_rx) |
| 149 | |
| 150 | |
| 151 | def set_port(pr, port): |
| 152 | pr['punt']['l4']['port'] = port |
| 153 | return pr |
| 154 | |
| 155 | |
| 156 | def set_reason(pr, reason): |
| 157 | pr['punt']['exception']['id'] = reason |
| 158 | return pr |
| 159 | |
| 160 | |
| 161 | def mk_vpp_cfg4(): |
| 162 | pt_l4 = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_L4 |
| 163 | af_ip4 = VppEnum.vl_api_address_family_t.ADDRESS_IP4 |
| 164 | udp_proto = VppEnum.vl_api_ip_proto_t.IP_API_PROTO_UDP |
| 165 | punt_l4 = { |
| 166 | 'type': pt_l4, |
| 167 | 'punt': { |
| 168 | 'l4': { |
| 169 | 'af': af_ip4, |
| 170 | 'protocol': udp_proto |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return punt_l4 |
| 175 | |
| 176 | |
| 177 | def mk_vpp_cfg6(): |
| 178 | pt_l4 = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_L4 |
| 179 | af_ip6 = VppEnum.vl_api_address_family_t.ADDRESS_IP6 |
| 180 | udp_proto = VppEnum.vl_api_ip_proto_t.IP_API_PROTO_UDP |
| 181 | punt_l4 = { |
| 182 | 'type': pt_l4, |
| 183 | 'punt': { |
| 184 | 'l4': { |
| 185 | 'af': af_ip6, |
| 186 | 'protocol': udp_proto |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | return punt_l4 |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 191 | |
| 192 | |
| 193 | class TestIP4PuntSocket(TestPuntSocket): |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 194 | """ Punt Socket for IPv4 UDP """ |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 195 | |
Paul Vinciguerra | 8d991d9 | 2019-01-25 14:05:48 -0800 | [diff] [blame] | 196 | @classmethod |
| 197 | def setUpClass(cls): |
| 198 | super(TestIP4PuntSocket, cls).setUpClass() |
| 199 | |
| 200 | @classmethod |
| 201 | def tearDownClass(cls): |
| 202 | super(TestIP4PuntSocket, cls).tearDownClass() |
| 203 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 204 | def setUp(self): |
| 205 | super(TestIP4PuntSocket, self).setUp() |
| 206 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 207 | for i in self.pg_interfaces: |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 208 | i.config_ip4() |
| 209 | i.resolve_arp() |
| 210 | |
| 211 | def tearDown(self): |
| 212 | super(TestIP4PuntSocket, self).tearDown() |
| 213 | for i in self.pg_interfaces: |
| 214 | i.unconfig_ip4() |
| 215 | i.admin_down() |
| 216 | |
| 217 | def test_punt_socket_dump(self): |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 218 | """ Punt socket registration/deregistration""" |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 219 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 220 | pt_l4 = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_L4 |
| 221 | af_ip4 = VppEnum.vl_api_address_family_t.ADDRESS_IP4 |
| 222 | udp_proto = VppEnum.vl_api_ip_proto_t.IP_API_PROTO_UDP |
| 223 | |
| 224 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 225 | self.assertEqual(len(punts), 0) |
| 226 | |
| 227 | # |
| 228 | # configure a punt socket |
| 229 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 230 | punt_l4 = mk_vpp_cfg4() |
| 231 | |
| 232 | self.vapi.punt_socket_register(set_port(punt_l4, 1111), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 233 | "%s/socket_punt_1111" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 234 | self.vapi.punt_socket_register(set_port(punt_l4, 2222), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 235 | "%s/socket_punt_2222" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 236 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 237 | self.assertEqual(len(punts), 2) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 238 | self.verify_port(set_port(punt_l4, 1111), punts[0]) |
| 239 | self.verify_port(set_port(punt_l4, 2222), punts[1]) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 240 | |
| 241 | # |
| 242 | # deregister a punt socket |
| 243 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 244 | self.vapi.punt_socket_deregister(set_port(punt_l4, 1111)) |
| 245 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 246 | self.assertEqual(len(punts), 1) |
| 247 | |
| 248 | # |
| 249 | # configure a punt socket again |
| 250 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 251 | self.vapi.punt_socket_register(set_port(punt_l4, 1111), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 252 | "%s/socket_punt_1111" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 253 | self.vapi.punt_socket_register(set_port(punt_l4, 3333), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 254 | "%s/socket_punt_3333" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 255 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 256 | self.assertEqual(len(punts), 3) |
| 257 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 258 | self.logger.info(self.vapi.cli("sh punt sock reg")) |
| 259 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 260 | # |
| 261 | # deregister all punt socket |
| 262 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 263 | self.vapi.punt_socket_deregister(set_port(punt_l4, 1111)) |
| 264 | self.vapi.punt_socket_deregister(set_port(punt_l4, 2222)) |
| 265 | self.vapi.punt_socket_deregister(set_port(punt_l4, 3333)) |
| 266 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 267 | self.assertEqual(len(punts), 0) |
| 268 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 269 | def test_punt_socket_traffic_single_port_single_socket(self): |
| 270 | """ Punt socket traffic single port single socket""" |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 271 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 272 | port = self.ports[0] |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 273 | pt_l4 = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_L4 |
| 274 | punt_l4 = set_port(mk_vpp_cfg4(), port) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 275 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 276 | p = (Ether(src=self.pg0.remote_mac, |
| 277 | dst=self.pg0.local_mac) / |
| 278 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 279 | UDP(sport=9876, dport=port) / |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 280 | Raw(b'\xa5' * 100)) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 281 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 282 | pkts = p * self.nr_packets |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 283 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 284 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 285 | self.assertEqual(len(punts), 0) |
| 286 | |
| 287 | # |
| 288 | # expect ICMP - port unreachable for all packets |
| 289 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 290 | rx = self.send_and_expect(self.pg0, pkts, self.pg0) |
| 291 | |
| 292 | for p in rx: |
| 293 | self.assertEqual(int(p[IP].proto), 1) # ICMP |
| 294 | self.assertEqual(int(p[ICMP].code), 3) # unreachable |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 295 | |
| 296 | # |
| 297 | # configure a punt socket |
| 298 | # |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 299 | self.socket_client_create("%s/socket_%d" % (self.tempdir, port)) |
| 300 | self.vapi.punt_socket_register(punt_l4, "%s/socket_%d" % |
| 301 | (self.tempdir, port)) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 302 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 303 | self.assertEqual(len(punts), 1) |
| 304 | |
| 305 | # |
| 306 | # expect punt socket and no packets on pg0 |
| 307 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 308 | self.send_and_assert_no_replies(self.pg0, pkts) |
| 309 | rx = self.socket_client_close() |
| 310 | self.verify_udp_pkts(rx, len(pkts), port) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 311 | |
| 312 | # |
| 313 | # remove punt socket. expect ICMP - port unreachable for all packets |
| 314 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 315 | self.vapi.punt_socket_deregister(punt_l4) |
| 316 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 317 | self.assertEqual(len(punts), 0) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 318 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 319 | rx = self.send_and_expect(self.pg0, pkts, self.pg0) |
| 320 | for p in rx: |
| 321 | self.assertEqual(int(p[IP].proto), 1) # ICMP |
| 322 | self.assertEqual(int(p[ICMP].code), 3) # unreachable |
| 323 | |
| 324 | def test_punt_socket_traffic_multi_ports_multi_sockets(self): |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 325 | """ Punt socket traffic multi ports and multi sockets""" |
| 326 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 327 | punt_l4 = mk_vpp_cfg4() |
| 328 | |
| 329 | # configuration for each UDP port |
| 330 | cfgs = dict() |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 331 | |
| 332 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 333 | # create stream of packets for each port |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 334 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 335 | for port in self.ports: |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 336 | # choose port from port list |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 337 | cfgs[port] = {} |
| 338 | |
| 339 | pkt = (Ether(src=self.pg0.remote_mac, |
| 340 | dst=self.pg0.local_mac) / |
| 341 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 342 | UDP(sport=9876, dport=port) / |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 343 | Raw(b'\xa5' * 100)) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 344 | cfgs[port]['pkts'] = pkt * self.nr_packets |
| 345 | cfgs[port]['port'] = port |
| 346 | cfgs[port]['vpp'] = copy.deepcopy(set_port(punt_l4, port)) |
| 347 | |
| 348 | # configure punt sockets |
| 349 | cfgs[port]['sock'] = self.socket_client_create( |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 350 | "%s/socket_%d" % (self.tempdir, port)) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 351 | self.vapi.punt_socket_register( |
| 352 | cfgs[port]['vpp'], |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 353 | "%s/socket_%d" % (self.tempdir, port)) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 354 | |
| 355 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 356 | # send the packets that get punted |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 357 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 358 | for cfg in cfgs.values(): |
| 359 | self.send_and_assert_no_replies(self.pg0, cfg['pkts']) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 360 | |
| 361 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 362 | # test that we got the excepted packets on the expected socket |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 363 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 364 | for cfg in cfgs.values(): |
| 365 | rx = cfg['sock'].close() |
| 366 | self.verify_udp_pkts(rx, len(cfg['pkts']), cfg['port']) |
| 367 | self.vapi.punt_socket_deregister(cfg['vpp']) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 368 | |
| 369 | def test_punt_socket_traffic_multi_ports_single_socket(self): |
| 370 | """ Punt socket traffic multi ports and single socket""" |
| 371 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 372 | pt_l4 = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_L4 |
| 373 | punt_l4 = mk_vpp_cfg4() |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 374 | |
| 375 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 376 | # create stream of packets with each port |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 377 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 378 | pkts = [] |
| 379 | for port in self.ports: |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 380 | # choose port from port list |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 381 | pkt = (Ether(src=self.pg0.remote_mac, |
| 382 | dst=self.pg0.local_mac) / |
| 383 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 384 | UDP(sport=9876, dport=port) / |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 385 | Raw(b'\xa5' * 100)) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 386 | pkts += pkt * self.nr_packets |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 387 | |
| 388 | # |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 389 | # configure a punt socket |
| 390 | # |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 391 | self.socket_client_create("%s/socket_multi" % self.tempdir) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 392 | for p in self.ports: |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 393 | self.vapi.punt_socket_register(set_port(punt_l4, p), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 394 | "%s/socket_multi" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 395 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 396 | self.assertEqual(len(punts), len(self.ports)) |
| 397 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 398 | # |
| 399 | # expect punt socket and no packets on pg0 |
| 400 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 401 | self.send_and_assert_no_replies(self.pg0, pkts) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 402 | self.logger.info(self.vapi.cli("show trace")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 403 | rx = self.socket_client_close() |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 404 | |
| 405 | for p in self.ports: |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 406 | self.verify_udp_pkts(rx, self.nr_packets, p) |
| 407 | self.vapi.punt_socket_deregister(set_port(punt_l4, p)) |
| 408 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 409 | self.assertEqual(len(punts), 0) |
| 410 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 411 | |
| 412 | class TestIP6PuntSocket(TestPuntSocket): |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 413 | """ Punt Socket for IPv6 UDP """ |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 414 | |
Paul Vinciguerra | 8d991d9 | 2019-01-25 14:05:48 -0800 | [diff] [blame] | 415 | @classmethod |
| 416 | def setUpClass(cls): |
| 417 | super(TestIP6PuntSocket, cls).setUpClass() |
| 418 | |
| 419 | @classmethod |
| 420 | def tearDownClass(cls): |
| 421 | super(TestIP6PuntSocket, cls).tearDownClass() |
| 422 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 423 | def setUp(self): |
| 424 | super(TestIP6PuntSocket, self).setUp() |
| 425 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 426 | for i in self.pg_interfaces: |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 427 | i.config_ip6() |
| 428 | i.resolve_ndp() |
| 429 | |
| 430 | def tearDown(self): |
| 431 | super(TestIP6PuntSocket, self).tearDown() |
| 432 | for i in self.pg_interfaces: |
| 433 | i.unconfig_ip6() |
| 434 | i.admin_down() |
| 435 | |
| 436 | def test_punt_socket_dump(self): |
| 437 | """ Punt socket registration """ |
| 438 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 439 | pt_l4 = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_L4 |
| 440 | af_ip6 = VppEnum.vl_api_address_family_t.ADDRESS_IP6 |
| 441 | udp_proto = VppEnum.vl_api_ip_proto_t.IP_API_PROTO_UDP |
| 442 | # |
| 443 | # configure a punt socket |
| 444 | # |
| 445 | punt_l4 = { |
| 446 | 'type': pt_l4, |
| 447 | 'punt': { |
| 448 | 'l4': { |
| 449 | 'af': af_ip6, |
| 450 | 'protocol': udp_proto |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 456 | self.assertEqual(len(punts), 0) |
| 457 | |
| 458 | # |
| 459 | # configure a punt socket |
| 460 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 461 | self.vapi.punt_socket_register(set_port(punt_l4, 1111), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 462 | "%s/socket_1111" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 463 | self.vapi.punt_socket_register(set_port(punt_l4, 2222), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 464 | "%s/socket_2222" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 465 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 466 | self.assertEqual(len(punts), 2) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 467 | self.verify_port(set_port(punt_l4, 1111), punts[0]) |
| 468 | self.verify_port(set_port(punt_l4, 2222), punts[1]) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 469 | |
| 470 | # |
| 471 | # deregister a punt socket |
| 472 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 473 | self.vapi.punt_socket_deregister(set_port(punt_l4, 1111)) |
| 474 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 475 | self.assertEqual(len(punts), 1) |
| 476 | |
| 477 | # |
| 478 | # configure a punt socket again |
| 479 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 480 | self.vapi.punt_socket_register(set_port(punt_l4, 1111), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 481 | "%s/socket_1111" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 482 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 483 | self.assertEqual(len(punts), 2) |
| 484 | |
| 485 | # |
| 486 | # deregister all punt socket |
| 487 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 488 | self.vapi.punt_socket_deregister(set_port(punt_l4, 1111)) |
| 489 | self.vapi.punt_socket_deregister(set_port(punt_l4, 2222)) |
| 490 | self.vapi.punt_socket_deregister(set_port(punt_l4, 3333)) |
| 491 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 492 | self.assertEqual(len(punts), 0) |
| 493 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 494 | def test_punt_socket_traffic_single_port_single_socket(self): |
| 495 | """ Punt socket traffic single port single socket""" |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 496 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 497 | port = self.ports[0] |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 498 | pt_l4 = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_L4 |
| 499 | af_ip6 = VppEnum.vl_api_address_family_t.ADDRESS_IP6 |
| 500 | udp_proto = VppEnum.vl_api_ip_proto_t.IP_API_PROTO_UDP |
| 501 | punt_l4 = { |
| 502 | 'type': pt_l4, |
| 503 | 'punt': { |
| 504 | 'l4': { |
| 505 | 'af': af_ip6, |
| 506 | 'protocol': udp_proto, |
| 507 | 'port': port, |
| 508 | } |
| 509 | } |
| 510 | } |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 511 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 512 | p = (Ether(src=self.pg0.remote_mac, |
| 513 | dst=self.pg0.local_mac) / |
| 514 | IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) / |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 515 | inet6.UDP(sport=9876, dport=port) / |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 516 | Raw(b'\xa5' * 100)) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 517 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 518 | pkts = p * self.nr_packets |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 519 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 520 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 521 | self.assertEqual(len(punts), 0) |
| 522 | |
| 523 | # |
| 524 | # expect ICMPv6 - destination unreachable for all packets |
| 525 | # |
| 526 | self.vapi.cli("clear trace") |
| 527 | self.pg0.add_stream(pkts) |
| 528 | self.pg_enable_capture(self.pg_interfaces) |
| 529 | self.pg_start() |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 530 | # FIXME - when punt socket deregister is implemented |
| 531 | # rx = self.pg0.get_capture(self.nr_packets) |
| 532 | # for p in rx: |
| 533 | # self.assertEqual(int(p[IPv6].nh), 58) # ICMPv6 |
| 534 | # self.assertEqual(int(p[ICMPv6DestUnreach].code),4) # unreachable |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 535 | |
| 536 | # |
| 537 | # configure a punt socket |
| 538 | # |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 539 | self.socket_client_create("%s/socket_%d" % (self.tempdir, port)) |
| 540 | self.vapi.punt_socket_register(punt_l4, "%s/socket_%d" % |
| 541 | (self.tempdir, port)) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 542 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 543 | self.assertEqual(len(punts), 1) |
| 544 | |
| 545 | # |
| 546 | # expect punt socket and no packets on pg0 |
| 547 | # |
| 548 | self.vapi.cli("clear errors") |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 549 | self.vapi.cli("clear trace") |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 550 | self.pg0.add_stream(pkts) |
| 551 | self.pg_enable_capture(self.pg_interfaces) |
| 552 | self.pg_start() |
| 553 | self.pg0.get_capture(0) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 554 | self.logger.info(self.vapi.cli("show trace")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 555 | rx = self.socket_client_close() |
| 556 | self.verify_udp_pkts(rx, len(pkts), port) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 557 | |
| 558 | # |
| 559 | # remove punt socket. expect ICMP - dest. unreachable for all packets |
| 560 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 561 | self.vapi.punt_socket_deregister(punt_l4) |
| 562 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 563 | self.assertEqual(len(punts), 0) |
| 564 | self.pg0.add_stream(pkts) |
| 565 | self.pg_enable_capture(self.pg_interfaces) |
| 566 | self.pg_start() |
| 567 | # FIXME - when punt socket deregister is implemented |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 568 | # self.pg0.get_capture(nr_packets) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 569 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 570 | def test_punt_socket_traffic_multi_ports_multi_sockets(self): |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 571 | """ Punt socket traffic multi ports and multi sockets""" |
| 572 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 573 | punt_l4 = mk_vpp_cfg6() |
| 574 | |
| 575 | # configuration for each UDP port |
| 576 | cfgs = dict() |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 577 | |
| 578 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 579 | # create stream of packets for each port |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 580 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 581 | for port in self.ports: |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 582 | # choose port from port list |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 583 | cfgs[port] = {} |
| 584 | |
| 585 | pkt = (Ether(src=self.pg0.remote_mac, |
| 586 | dst=self.pg0.local_mac) / |
| 587 | IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) / |
| 588 | UDP(sport=9876, dport=port) / |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 589 | Raw(b'\xa5' * 100)) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 590 | cfgs[port]['pkts'] = pkt * self.nr_packets |
| 591 | cfgs[port]['port'] = port |
| 592 | cfgs[port]['vpp'] = copy.deepcopy(set_port(punt_l4, port)) |
| 593 | |
| 594 | # configure punt sockets |
| 595 | cfgs[port]['sock'] = self.socket_client_create( |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 596 | "%s/socket_%d" % (self.tempdir, port)) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 597 | self.vapi.punt_socket_register( |
| 598 | cfgs[port]['vpp'], |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 599 | "%s/socket_%d" % (self.tempdir, port)) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 600 | |
| 601 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 602 | # send the packets that get punted |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 603 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 604 | for cfg in cfgs.values(): |
| 605 | self.send_and_assert_no_replies(self.pg0, cfg['pkts']) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 606 | |
| 607 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 608 | # test that we got the excepted packets on the expected socket |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 609 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 610 | for cfg in cfgs.values(): |
| 611 | rx = cfg['sock'].close() |
| 612 | self.verify_udp_pkts(rx, len(cfg['pkts']), cfg['port']) |
| 613 | self.vapi.punt_socket_deregister(cfg['vpp']) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 614 | |
| 615 | def test_punt_socket_traffic_multi_ports_single_socket(self): |
| 616 | """ Punt socket traffic multi ports and single socket""" |
| 617 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 618 | pt_l4 = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_L4 |
| 619 | af_ip6 = VppEnum.vl_api_address_family_t.ADDRESS_IP6 |
| 620 | udp_proto = VppEnum.vl_api_ip_proto_t.IP_API_PROTO_UDP |
| 621 | punt_l4 = { |
| 622 | 'type': pt_l4, |
| 623 | 'punt': { |
| 624 | 'l4': { |
| 625 | 'af': af_ip6, |
| 626 | 'protocol': udp_proto, |
| 627 | } |
| 628 | } |
| 629 | } |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 630 | |
| 631 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 632 | # create stream of packets with each port |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 633 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 634 | pkts = [] |
| 635 | for port in self.ports: |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 636 | # choose port from port list |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 637 | pkt = (Ether(src=self.pg0.remote_mac, |
| 638 | dst=self.pg0.local_mac) / |
| 639 | IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) / |
| 640 | UDP(sport=9876, dport=port) / |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 641 | Raw(b'\xa5' * 100)) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 642 | pkts += pkt * self.nr_packets |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 643 | |
| 644 | # |
| 645 | # no punt socket |
| 646 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 647 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 648 | self.assertEqual(len(punts), 0) |
| 649 | |
| 650 | # |
| 651 | # configure a punt socket |
| 652 | # |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 653 | self.socket_client_create("%s/socket_multi" % self.tempdir) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 654 | for p in self.ports: |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 655 | self.vapi.punt_socket_register(set_port(punt_l4, p), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 656 | "%s/socket_multi" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 657 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 658 | self.assertEqual(len(punts), len(self.ports)) |
| 659 | |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 660 | # |
| 661 | # expect punt socket and no packets on pg0 |
| 662 | # |
| 663 | self.vapi.cli("clear errors") |
| 664 | self.vapi.cli("clear trace") |
| 665 | self.pg0.add_stream(pkts) |
| 666 | self.pg_enable_capture(self.pg_interfaces) |
| 667 | self.pg_start() |
Andrew Yourtchenko | cb265c6 | 2019-07-25 10:03:51 +0000 | [diff] [blame] | 668 | # give a chance to punt socket to collect all packets |
| 669 | self.sleep(1) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 670 | self.pg0.get_capture(0) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 671 | rx = self.socket_client_close() |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 672 | |
| 673 | for p in self.ports: |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 674 | self.verify_udp_pkts(rx, self.nr_packets, p) |
| 675 | self.vapi.punt_socket_deregister(set_port(punt_l4, p)) |
| 676 | punts = self.vapi.punt_socket_dump(type=pt_l4) |
Pavel Kotucek | 9edb83a | 2018-12-11 16:57:25 +0100 | [diff] [blame] | 677 | self.assertEqual(len(punts), 0) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 678 | |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 679 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 680 | class TestExceptionPuntSocket(TestPuntSocket): |
| 681 | """ Punt Socket for Exceptions """ |
| 682 | |
| 683 | @classmethod |
| 684 | def setUpClass(cls): |
| 685 | super(TestExceptionPuntSocket, cls).setUpClass() |
| 686 | |
| 687 | @classmethod |
| 688 | def tearDownClass(cls): |
| 689 | super(TestExceptionPuntSocket, cls).tearDownClass() |
| 690 | |
| 691 | def setUp(self): |
| 692 | super(TestExceptionPuntSocket, self).setUp() |
| 693 | |
| 694 | for i in self.pg_interfaces: |
| 695 | i.config_ip4() |
| 696 | i.resolve_arp() |
| 697 | |
| 698 | def tearDown(self): |
| 699 | super(TestExceptionPuntSocket, self).tearDown() |
| 700 | for i in self.pg_interfaces: |
| 701 | i.unconfig_ip4() |
| 702 | i.admin_down() |
| 703 | |
| 704 | def test_registration(self): |
| 705 | """ Punt socket registration/deregistration""" |
| 706 | |
| 707 | pt_ex = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_EXCEPTION |
| 708 | |
| 709 | punts = self.vapi.punt_socket_dump(type=pt_ex) |
| 710 | self.assertEqual(len(punts), 0) |
| 711 | |
| 712 | # |
| 713 | # configure a punt socket |
| 714 | # |
| 715 | punt_ex = { |
| 716 | 'type': pt_ex, |
| 717 | 'punt': { |
| 718 | 'exception': {} |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | self.vapi.punt_socket_register(set_reason(punt_ex, 1), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 723 | "%s/socket_punt_1" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 724 | self.vapi.punt_socket_register(set_reason(punt_ex, 2), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 725 | "%s/socket_punt_2" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 726 | punts = self.vapi.punt_socket_dump(type=pt_ex) |
| 727 | self.assertEqual(len(punts), 2) |
| 728 | self.verify_exception(set_reason(punt_ex, 1), punts[0]) |
| 729 | self.verify_exception(set_reason(punt_ex, 2), punts[1]) |
| 730 | |
| 731 | # |
| 732 | # deregister a punt socket |
| 733 | # |
| 734 | self.vapi.punt_socket_deregister(set_reason(punt_ex, 1)) |
| 735 | punts = self.vapi.punt_socket_dump(type=pt_ex) |
| 736 | self.assertEqual(len(punts), 1) |
| 737 | |
| 738 | # |
| 739 | # configure a punt socket again |
| 740 | # |
| 741 | self.vapi.punt_socket_register(set_reason(punt_ex, 1), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 742 | "%s/socket_punt_1" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 743 | self.vapi.punt_socket_register(set_reason(punt_ex, 3), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 744 | "%s/socket_punt_3" % self.tempdir) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 745 | punts = self.vapi.punt_socket_dump(type=pt_ex) |
| 746 | self.assertEqual(len(punts), 3) |
| 747 | |
| 748 | self.logger.info(self.vapi.cli("sh punt sock reg exception")) |
| 749 | |
| 750 | # |
| 751 | # deregister all punt socket |
| 752 | # |
| 753 | self.vapi.punt_socket_deregister(set_reason(punt_ex, 1)) |
| 754 | self.vapi.punt_socket_deregister(set_reason(punt_ex, 2)) |
| 755 | self.vapi.punt_socket_deregister(set_reason(punt_ex, 3)) |
| 756 | punts = self.vapi.punt_socket_dump(type=pt_ex) |
| 757 | self.assertEqual(len(punts), 0) |
| 758 | |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 759 | def verify_esp_pkts(self, rxs, n_sent, spi, has_udp): |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 760 | self.assertEqual(len(rxs), n_sent) |
| 761 | for rx in rxs: |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 762 | self.assertTrue(rx.haslayer(IP)) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 763 | self.assertTrue(rx.haslayer(ESP)) |
| 764 | self.assertEqual(rx[ESP].spi, spi) |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 765 | if has_udp: |
| 766 | self.assertTrue(rx.haslayer(UDP)) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 767 | |
| 768 | def test_traffic(self): |
| 769 | """ Punt socket traffic """ |
| 770 | |
| 771 | port = self.ports[0] |
| 772 | pt_ex = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_EXCEPTION |
| 773 | punt_ex = { |
| 774 | 'type': pt_ex, |
| 775 | 'punt': { |
| 776 | 'exception': {} |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | # |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 781 | # we need an IPSec tunnels for this to work otherwise ESP gets dropped |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 782 | # due to unknown IP proto |
| 783 | # |
| 784 | VppIpsecTunInterface(self, self.pg0, 1000, 1000, |
| 785 | (VppEnum.vl_api_ipsec_crypto_alg_t. |
| 786 | IPSEC_API_CRYPTO_ALG_AES_CBC_128), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 787 | b"0123456701234567", |
| 788 | b"0123456701234567", |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 789 | (VppEnum.vl_api_ipsec_integ_alg_t. |
| 790 | IPSEC_API_INTEG_ALG_SHA1_96), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 791 | b"0123456701234567", |
| 792 | b"0123456701234567").add_vpp_config() |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 793 | VppIpsecTunInterface(self, self.pg0, 1001, 1001, |
| 794 | (VppEnum.vl_api_ipsec_crypto_alg_t. |
| 795 | IPSEC_API_CRYPTO_ALG_AES_CBC_128), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 796 | b"0123456701234567", |
| 797 | b"0123456701234567", |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 798 | (VppEnum.vl_api_ipsec_integ_alg_t. |
| 799 | IPSEC_API_INTEG_ALG_SHA1_96), |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 800 | b"0123456701234567", |
| 801 | b"0123456701234567", |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 802 | udp_encap=True).add_vpp_config() |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 803 | |
| 804 | # |
| 805 | # we're dealing with IPSec tunnels punting for no-such-tunnel |
| 806 | # adn SPI=0 |
| 807 | # |
| 808 | cfgs = dict() |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 809 | cfgs['ipsec4-no-such-tunnel'] = {'spi': 99, 'udp': False} |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 810 | cfgs['ipsec4-spi-o-udp-0'] = {'spi': 0, 'udp': True} |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 811 | |
| 812 | # |
| 813 | # find the VPP ID for these punt exception reasin |
| 814 | # |
| 815 | rs = self.vapi.punt_reason_dump() |
| 816 | for key in cfgs: |
| 817 | for r in rs: |
| 818 | if r.reason.name == key: |
| 819 | cfgs[key]['id'] = r.reason.id |
| 820 | cfgs[key]['vpp'] = copy.deepcopy( |
| 821 | set_reason(punt_ex, |
| 822 | cfgs[key]['id'])) |
| 823 | break |
| 824 | |
| 825 | # |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 826 | # configure punt sockets |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 827 | # |
| 828 | for cfg in cfgs.values(): |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 829 | cfg['sock'] = self.socket_client_create("%s/socket_%d" % |
| 830 | (self.tempdir, cfg['id'])) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 831 | self.vapi.punt_socket_register( |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 832 | cfg['vpp'], "%s/socket_%d" % (self.tempdir, cfg['id'])) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 833 | |
| 834 | # |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 835 | # create packet streams for 'no-such-tunnel' exception |
| 836 | # |
| 837 | for cfg in cfgs.values(): |
| 838 | pkt = (Ether(src=self.pg0.remote_mac, |
| 839 | dst=self.pg0.local_mac) / |
| 840 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)) |
| 841 | if (cfg['udp']): |
| 842 | pkt = pkt / UDP(sport=666, dport=4500) |
| 843 | pkt = (pkt / ESP(spi=cfg['spi'], seq=3) / |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 844 | Raw(b'\xa5' * 100)) |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 845 | cfg['pkts'] = [pkt] |
| 846 | |
| 847 | # |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 848 | # send packets for each SPI we expect to be punted |
| 849 | # |
| 850 | for cfg in cfgs.values(): |
| 851 | self.send_and_assert_no_replies(self.pg0, cfg['pkts']) |
| 852 | |
| 853 | # |
| 854 | # verify the punted packets arrived on the associated socket |
| 855 | # |
| 856 | for cfg in cfgs.values(): |
| 857 | rx = cfg['sock'].close() |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 858 | self.verify_esp_pkts(rx, len(cfg['pkts']), |
| 859 | cfg['spi'], cfg['udp']) |
| 860 | |
| 861 | # |
| 862 | # socket deregister |
| 863 | # |
| 864 | for cfg in cfgs.values(): |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 865 | self.vapi.punt_socket_deregister(cfg['vpp']) |
| 866 | |
| 867 | |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 868 | class TestIpProtoPuntSocket(TestPuntSocket): |
| 869 | """ Punt Socket for IP packets """ |
| 870 | |
| 871 | @classmethod |
| 872 | def setUpClass(cls): |
| 873 | super(TestIpProtoPuntSocket, cls).setUpClass() |
| 874 | |
| 875 | @classmethod |
| 876 | def tearDownClass(cls): |
| 877 | super(TestIpProtoPuntSocket, cls).tearDownClass() |
| 878 | |
| 879 | def setUp(self): |
| 880 | super(TestIpProtoPuntSocket, self).setUp() |
| 881 | |
| 882 | for i in self.pg_interfaces: |
| 883 | i.config_ip4() |
| 884 | i.resolve_arp() |
| 885 | |
| 886 | def tearDown(self): |
| 887 | super(TestIpProtoPuntSocket, self).tearDown() |
| 888 | for i in self.pg_interfaces: |
| 889 | i.unconfig_ip4() |
| 890 | i.admin_down() |
| 891 | |
| 892 | def test_registration(self): |
| 893 | """ Punt socket registration/deregistration""" |
| 894 | |
| 895 | af_ip4 = VppEnum.vl_api_address_family_t.ADDRESS_IP4 |
| 896 | pt_ip = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_IP_PROTO |
| 897 | proto_ospf = VppEnum.vl_api_ip_proto_t.IP_API_PROTO_OSPF |
| 898 | proto_eigrp = VppEnum.vl_api_ip_proto_t.IP_API_PROTO_EIGRP |
| 899 | |
| 900 | punts = self.vapi.punt_socket_dump(type=pt_ip) |
| 901 | self.assertEqual(len(punts), 0) |
| 902 | |
| 903 | # |
| 904 | # configure a punt socket |
| 905 | # |
| 906 | punt_ospf = { |
| 907 | 'type': pt_ip, |
| 908 | 'punt': { |
| 909 | 'ip_proto': { |
| 910 | 'af': af_ip4, |
| 911 | 'protocol': proto_ospf |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | punt_eigrp = { |
| 916 | 'type': pt_ip, |
| 917 | 'punt': { |
| 918 | 'ip_proto': { |
| 919 | 'af': af_ip4, |
| 920 | 'protocol': proto_eigrp |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | self.vapi.punt_socket_register(punt_ospf, |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 926 | "%s/socket_punt_1" % self.tempdir) |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 927 | self.vapi.punt_socket_register(punt_eigrp, |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 928 | "%s/socket_punt_2" % self.tempdir) |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 929 | self.logger.info(self.vapi.cli("sh punt sock reg ip")) |
| 930 | punts = self.vapi.punt_socket_dump(type=pt_ip) |
| 931 | self.assertEqual(len(punts), 2) |
| 932 | self.verify_ip_proto(punt_ospf, punts[0]) |
| 933 | self.verify_ip_proto(punt_eigrp, punts[1]) |
| 934 | |
| 935 | # |
| 936 | # deregister a punt socket |
| 937 | # |
| 938 | self.vapi.punt_socket_deregister(punt_ospf) |
| 939 | punts = self.vapi.punt_socket_dump(type=pt_ip) |
| 940 | self.assertEqual(len(punts), 1) |
| 941 | |
| 942 | # |
| 943 | # configure a punt socket again |
| 944 | # |
| 945 | self.vapi.punt_socket_register(punt_ospf, |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 946 | "%s/socket_punt_3" % self.tempdir) |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 947 | punts = self.vapi.punt_socket_dump(type=pt_ip) |
| 948 | self.assertEqual(len(punts), 2) |
| 949 | |
| 950 | self.logger.info(self.vapi.cli("sh punt sock reg exception")) |
| 951 | |
| 952 | # |
| 953 | # deregister all punt socket |
| 954 | # |
| 955 | self.vapi.punt_socket_deregister(punt_eigrp) |
| 956 | self.vapi.punt_socket_deregister(punt_ospf) |
| 957 | punts = self.vapi.punt_socket_dump(type=pt_ip) |
| 958 | self.assertEqual(len(punts), 0) |
| 959 | |
| 960 | def verify_ospf_pkts(self, rxs, n_sent): |
| 961 | self.assertEqual(len(rxs), n_sent) |
| 962 | for rx in rxs: |
| 963 | self.assertTrue(rx.haslayer(OSPF_Hdr)) |
| 964 | |
| 965 | def test_traffic(self): |
| 966 | """ Punt socket traffic """ |
| 967 | |
| 968 | af_ip4 = VppEnum.vl_api_address_family_t.ADDRESS_IP4 |
| 969 | pt_ip = VppEnum.vl_api_punt_type_t.PUNT_API_TYPE_IP_PROTO |
| 970 | proto_ospf = VppEnum.vl_api_ip_proto_t.IP_API_PROTO_OSPF |
| 971 | |
| 972 | # |
| 973 | # configure a punt socket to capture OSPF packets |
| 974 | # |
| 975 | punt_ospf = { |
| 976 | 'type': pt_ip, |
| 977 | 'punt': { |
| 978 | 'ip_proto': { |
| 979 | 'af': af_ip4, |
| 980 | 'protocol': proto_ospf |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | # |
| 986 | # create packet streams and configure a punt sockets |
| 987 | # |
| 988 | pkt = (Ether(src=self.pg0.remote_mac, |
| 989 | dst=self.pg0.local_mac) / |
| 990 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 991 | OSPF_Hdr() / |
| 992 | OSPFv3_Hello()) |
| 993 | pkts = pkt * 7 |
| 994 | |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 995 | sock = self.socket_client_create("%s/socket_1" % self.tempdir) |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 996 | self.vapi.punt_socket_register( |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 997 | punt_ospf, "%s/socket_1" % self.tempdir) |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 998 | |
| 999 | # |
| 1000 | # send packets for each SPI we expect to be punted |
| 1001 | # |
| 1002 | self.send_and_assert_no_replies(self.pg0, pkts) |
| 1003 | |
| 1004 | # |
| 1005 | # verify the punted packets arrived on the associated socket |
| 1006 | # |
| 1007 | rx = sock.close() |
| 1008 | self.verify_ospf_pkts(rx, len(pkts)) |
| 1009 | self.vapi.punt_socket_deregister(punt_ospf) |
| 1010 | |
| 1011 | |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1012 | class TestPunt(VppTestCase): |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 1013 | """ Exception Punt Test Case """ |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1014 | |
| 1015 | @classmethod |
| 1016 | def setUpClass(cls): |
| 1017 | super(TestPunt, cls).setUpClass() |
| 1018 | |
| 1019 | @classmethod |
| 1020 | def tearDownClass(cls): |
| 1021 | super(TestPunt, cls).tearDownClass() |
| 1022 | |
| 1023 | def setUp(self): |
| 1024 | super(TestPunt, self).setUp() |
| 1025 | |
| 1026 | self.create_pg_interfaces(range(4)) |
| 1027 | |
| 1028 | for i in self.pg_interfaces: |
| 1029 | i.admin_up() |
| 1030 | i.config_ip4() |
| 1031 | i.resolve_arp() |
| 1032 | i.config_ip6() |
| 1033 | i.resolve_ndp() |
| 1034 | |
| 1035 | def tearDown(self): |
| 1036 | for i in self.pg_interfaces: |
| 1037 | i.unconfig_ip4() |
| 1038 | i.unconfig_ip6() |
| 1039 | i.ip6_disable() |
| 1040 | i.admin_down() |
| 1041 | super(TestPunt, self).tearDown() |
| 1042 | |
| 1043 | def test_punt(self): |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 1044 | """ Exception Path testing """ |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1045 | |
| 1046 | # |
Neale Ranns | 719beb7 | 2019-07-10 07:10:25 +0000 | [diff] [blame] | 1047 | # dump the punt registered reasons |
| 1048 | # search for a few we know should be there |
| 1049 | # |
| 1050 | rs = self.vapi.punt_reason_dump() |
| 1051 | |
| 1052 | reasons = ["ipsec6-no-such-tunnel", |
| 1053 | "ipsec4-no-such-tunnel", |
| 1054 | "ipsec4-spi-o-udp-0"] |
| 1055 | |
| 1056 | for reason in reasons: |
| 1057 | found = False |
| 1058 | for r in rs: |
| 1059 | if r.reason.name == reason: |
| 1060 | found = True |
| 1061 | break |
| 1062 | self.assertTrue(found) |
| 1063 | |
| 1064 | # |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1065 | # Using the test CLI we will hook in a exception path to |
| 1066 | # send ACL deny packets out of pg0 and pg1. |
| 1067 | # the ACL is src,dst = 1.1.1.1,1.1.1.2 |
| 1068 | # |
| 1069 | ip_1_1_1_2 = VppIpRoute(self, "1.1.1.2", 32, |
| 1070 | [VppRoutePath(self.pg3.remote_ip4, |
| 1071 | self.pg3.sw_if_index)]) |
| 1072 | ip_1_1_1_2.add_vpp_config() |
| 1073 | ip_1_2 = VppIpRoute(self, "1::2", 128, |
| 1074 | [VppRoutePath(self.pg3.remote_ip6, |
| 1075 | self.pg3.sw_if_index, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 1076 | proto=DpoProto.DPO_PROTO_IP6)]) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1077 | ip_1_2.add_vpp_config() |
| 1078 | |
| 1079 | p4 = (Ether(src=self.pg2.remote_mac, |
| 1080 | dst=self.pg2.local_mac) / |
| 1081 | IP(src="1.1.1.1", dst="1.1.1.2") / |
| 1082 | UDP(sport=1234, dport=1234) / |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 1083 | Raw(b'\xa5' * 100)) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1084 | p6 = (Ether(src=self.pg2.remote_mac, |
| 1085 | dst=self.pg2.local_mac) / |
| 1086 | IPv6(src="1::1", dst="1::2") / |
| 1087 | UDP(sport=1234, dport=1234) / |
Ole Troan | 9cd8f33 | 2019-10-18 15:57:56 +0200 | [diff] [blame] | 1088 | Raw(b'\xa5' * 100)) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1089 | self.send_and_expect(self.pg2, p4*1, self.pg3) |
| 1090 | self.send_and_expect(self.pg2, p6*1, self.pg3) |
| 1091 | |
| 1092 | # |
| 1093 | # apply the punting features |
| 1094 | # |
| 1095 | self.vapi.cli("test punt pg2") |
| 1096 | |
| 1097 | # |
Neale Ranns | 719beb7 | 2019-07-10 07:10:25 +0000 | [diff] [blame] | 1098 | # dump the punt reasons to learn the IDs assigned |
| 1099 | # |
| 1100 | rs = self.vapi.punt_reason_dump(reason={'name': "reason-v4"}) |
| 1101 | r4 = rs[0].reason.id |
| 1102 | rs = self.vapi.punt_reason_dump(reason={'name': "reason-v6"}) |
| 1103 | r6 = rs[0].reason.id |
| 1104 | |
| 1105 | # |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1106 | # pkts now dropped |
| 1107 | # |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 1108 | self.send_and_assert_no_replies(self.pg2, p4*NUM_PKTS) |
| 1109 | self.send_and_assert_no_replies(self.pg2, p6*NUM_PKTS) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1110 | |
| 1111 | # |
| 1112 | # Check state: |
| 1113 | # 1 - node error counters |
| 1114 | # 2 - per-reason counters |
| 1115 | # 2, 3 are the index of the assigned punt reason |
| 1116 | # |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 1117 | stats = self.statistics.get_err_counter( |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1118 | "/err/punt-dispatch/No registrations") |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 1119 | self.assertEqual(stats, 2*NUM_PKTS) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1120 | |
| 1121 | stats = self.statistics.get_counter("/net/punt") |
Neale Ranns | 719beb7 | 2019-07-10 07:10:25 +0000 | [diff] [blame] | 1122 | self.assertEqual(stats[0][r4]['packets'], NUM_PKTS) |
| 1123 | self.assertEqual(stats[0][r6]['packets'], NUM_PKTS) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1124 | |
| 1125 | # |
| 1126 | # use the test CLI to test a client that punts exception |
| 1127 | # packets out of pg0 |
| 1128 | # |
| 1129 | self.vapi.cli("test punt pg0 %s" % self.pg0.remote_ip4) |
| 1130 | self.vapi.cli("test punt pg0 %s" % self.pg0.remote_ip6) |
| 1131 | |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 1132 | rx4s = self.send_and_expect(self.pg2, p4*NUM_PKTS, self.pg0) |
| 1133 | rx6s = self.send_and_expect(self.pg2, p6*NUM_PKTS, self.pg0) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1134 | |
| 1135 | # |
| 1136 | # check the packets come out IP unmodified but destined to pg0 host |
| 1137 | # |
| 1138 | for rx in rx4s: |
| 1139 | self.assertEqual(rx[Ether].dst, self.pg0.remote_mac) |
| 1140 | self.assertEqual(rx[Ether].src, self.pg0.local_mac) |
| 1141 | self.assertEqual(p4[IP].dst, rx[IP].dst) |
| 1142 | self.assertEqual(p4[IP].ttl, rx[IP].ttl) |
| 1143 | for rx in rx6s: |
| 1144 | self.assertEqual(rx[Ether].dst, self.pg0.remote_mac) |
| 1145 | self.assertEqual(rx[Ether].src, self.pg0.local_mac) |
| 1146 | self.assertEqual(p6[IPv6].dst, rx[IPv6].dst) |
| 1147 | self.assertEqual(p6[IPv6].hlim, rx[IPv6].hlim) |
| 1148 | |
| 1149 | stats = self.statistics.get_counter("/net/punt") |
Neale Ranns | 719beb7 | 2019-07-10 07:10:25 +0000 | [diff] [blame] | 1150 | self.assertEqual(stats[0][r4]['packets'], 2*NUM_PKTS) |
| 1151 | self.assertEqual(stats[0][r6]['packets'], 2*NUM_PKTS) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1152 | |
| 1153 | # |
| 1154 | # add another registration for the same reason to send packets |
| 1155 | # out of pg1 |
| 1156 | # |
| 1157 | self.vapi.cli("test punt pg1 %s" % self.pg1.remote_ip4) |
| 1158 | self.vapi.cli("test punt pg1 %s" % self.pg1.remote_ip6) |
| 1159 | |
| 1160 | self.vapi.cli("clear trace") |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 1161 | self.pg2.add_stream(p4 * NUM_PKTS) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1162 | self.pg_enable_capture(self.pg_interfaces) |
| 1163 | self.pg_start() |
| 1164 | |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 1165 | rxd = self.pg0.get_capture(NUM_PKTS) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1166 | for rx in rxd: |
| 1167 | self.assertEqual(rx[Ether].dst, self.pg0.remote_mac) |
| 1168 | self.assertEqual(rx[Ether].src, self.pg0.local_mac) |
| 1169 | self.assertEqual(p4[IP].dst, rx[IP].dst) |
| 1170 | self.assertEqual(p4[IP].ttl, rx[IP].ttl) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 1171 | rxd = self.pg1.get_capture(NUM_PKTS) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1172 | for rx in rxd: |
| 1173 | self.assertEqual(rx[Ether].dst, self.pg1.remote_mac) |
| 1174 | self.assertEqual(rx[Ether].src, self.pg1.local_mac) |
| 1175 | self.assertEqual(p4[IP].dst, rx[IP].dst) |
| 1176 | self.assertEqual(p4[IP].ttl, rx[IP].ttl) |
| 1177 | |
| 1178 | self.vapi.cli("clear trace") |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 1179 | self.pg2.add_stream(p6 * NUM_PKTS) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1180 | self.pg_enable_capture(self.pg_interfaces) |
| 1181 | self.pg_start() |
| 1182 | |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 1183 | rxd = self.pg0.get_capture(NUM_PKTS) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1184 | for rx in rxd: |
| 1185 | self.assertEqual(rx[Ether].dst, self.pg0.remote_mac) |
| 1186 | self.assertEqual(rx[Ether].src, self.pg0.local_mac) |
| 1187 | self.assertEqual(p6[IPv6].dst, rx[IPv6].dst) |
| 1188 | self.assertEqual(p6[IPv6].hlim, rx[IPv6].hlim) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 1189 | rxd = self.pg1.get_capture(NUM_PKTS) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1190 | for rx in rxd: |
| 1191 | self.assertEqual(rx[Ether].dst, self.pg1.remote_mac) |
| 1192 | self.assertEqual(rx[Ether].src, self.pg1.local_mac) |
| 1193 | self.assertEqual(p6[IPv6].dst, rx[IPv6].dst) |
| 1194 | self.assertEqual(p6[IPv6].hlim, rx[IPv6].hlim) |
| 1195 | |
| 1196 | stats = self.statistics.get_counter("/net/punt") |
Neale Ranns | 719beb7 | 2019-07-10 07:10:25 +0000 | [diff] [blame] | 1197 | self.assertEqual(stats[0][r4]['packets'], 3*NUM_PKTS) |
| 1198 | self.assertEqual(stats[0][r6]['packets'], 3*NUM_PKTS) |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1199 | |
| 1200 | self.logger.info(self.vapi.cli("show vlib graph punt-dispatch")) |
| 1201 | self.logger.info(self.vapi.cli("show punt client")) |
| 1202 | self.logger.info(self.vapi.cli("show punt reason")) |
| 1203 | self.logger.info(self.vapi.cli("show punt stats")) |
| 1204 | self.logger.info(self.vapi.cli("show punt db")) |
| 1205 | |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 1206 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 1207 | if __name__ == '__main__': |
| 1208 | unittest.main(testRunner=VppTestRunner) |