Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * @file |
| 18 | * @brief Local TCP/IP stack punt infrastructure. |
| 19 | * |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 20 | * Provides a set of VPP nodes together with the relevant APIs and CLI |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 21 | * commands in order to adjust and dispatch packets from the VPP data plane |
| 22 | * to the local TCP/IP stack |
| 23 | */ |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 24 | |
| 25 | #include <vnet/ip/ip.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 26 | #include <vlib/vlib.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | #include <vnet/udp/udp.h> |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 28 | #include <vnet/tcp/tcp.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 29 | #include <vnet/ip/punt.h> |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 30 | #include <vlib/unix/unix.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 31 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <unistd.h> |
| 34 | #include <sys/socket.h> |
Marco Varlese | 2234983 | 2017-09-08 10:40:34 +0200 | [diff] [blame] | 35 | #include <sys/uio.h> |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 36 | #include <stdlib.h> |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 37 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 38 | punt_main_t punt_main; |
| 39 | |
| 40 | char * |
| 41 | vnet_punt_get_server_pathname (void) |
| 42 | { |
| 43 | punt_main_t *pm = &punt_main; |
| 44 | return pm->sun_path; |
| 45 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 46 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 47 | static void |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 48 | punt_client_l4_db_add (ip_address_family_t af, u16 port, u32 index) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 49 | { |
| 50 | punt_main_t *pm = &punt_main; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 51 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 52 | pm->db.clients_by_l4_port = hash_set (pm->db.clients_by_l4_port, |
| 53 | punt_client_l4_mk_key (af, port), |
| 54 | index); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 57 | static u32 |
| 58 | punt_client_l4_db_remove (ip_address_family_t af, u16 port) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 59 | { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 60 | punt_main_t *pm = &punt_main; |
| 61 | u32 key, index = ~0; |
| 62 | uword *p; |
| 63 | |
| 64 | key = punt_client_l4_mk_key (af, port); |
| 65 | p = hash_get (pm->db.clients_by_l4_port, key); |
| 66 | |
| 67 | if (p) |
| 68 | index = p[0]; |
| 69 | |
| 70 | hash_unset (pm->db.clients_by_l4_port, key); |
| 71 | |
| 72 | return (index); |
| 73 | } |
| 74 | |
| 75 | static void |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 76 | punt_client_ip_proto_db_add (ip_address_family_t af, |
| 77 | ip_protocol_t proto, u32 index) |
| 78 | { |
| 79 | punt_main_t *pm = &punt_main; |
| 80 | |
| 81 | pm->db.clients_by_ip_proto = hash_set (pm->db.clients_by_ip_proto, |
| 82 | punt_client_ip_proto_mk_key (af, |
| 83 | proto), |
| 84 | index); |
| 85 | } |
| 86 | |
| 87 | static u32 |
| 88 | punt_client_ip_proto_db_remove (ip_address_family_t af, ip_protocol_t proto) |
| 89 | { |
| 90 | punt_main_t *pm = &punt_main; |
| 91 | u32 key, index = ~0; |
| 92 | uword *p; |
| 93 | |
| 94 | key = punt_client_ip_proto_mk_key (af, proto); |
| 95 | p = hash_get (pm->db.clients_by_ip_proto, key); |
| 96 | |
| 97 | if (p) |
| 98 | index = p[0]; |
| 99 | |
| 100 | hash_unset (pm->db.clients_by_ip_proto, key); |
| 101 | |
| 102 | return (index); |
| 103 | } |
| 104 | |
| 105 | static void |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 106 | punt_client_exception_db_add (vlib_punt_reason_t reason, u32 pci) |
| 107 | { |
| 108 | punt_main_t *pm = &punt_main; |
| 109 | |
| 110 | vec_validate_init_empty (pm->db.clients_by_exception, reason, ~0); |
| 111 | |
| 112 | pm->db.clients_by_exception[reason] = pci; |
| 113 | } |
| 114 | |
| 115 | static u32 |
| 116 | punt_client_exception_db_remove (vlib_punt_reason_t reason) |
| 117 | { |
| 118 | punt_main_t *pm = &punt_main; |
| 119 | u32 pci = ~0; |
| 120 | |
| 121 | if (punt_client_exception_get (reason)) |
| 122 | { |
| 123 | pci = pm->db.clients_by_exception[reason]; |
| 124 | pm->db.clients_by_exception[reason] = ~0; |
| 125 | } |
| 126 | |
| 127 | return pci; |
| 128 | } |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 129 | |
| 130 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 131 | punt_socket_read_ready (clib_file_t * uf) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 132 | { |
| 133 | vlib_main_t *vm = vlib_get_main (); |
| 134 | punt_main_t *pm = &punt_main; |
| 135 | |
| 136 | /** Schedule the rx node */ |
| 137 | vlib_node_set_interrupt_pending (vm, punt_socket_rx_node.index); |
| 138 | vec_add1 (pm->ready_fds, uf->file_descriptor); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 143 | static clib_error_t * |
| 144 | punt_socket_register_l4 (vlib_main_t * vm, |
| 145 | ip_address_family_t af, |
| 146 | u8 protocol, u16 port, char *client_pathname) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 147 | { |
| 148 | punt_main_t *pm = &punt_main; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 149 | punt_client_t *c; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 150 | |
| 151 | /* For now we only support UDP punt */ |
| 152 | if (protocol != IP_PROTOCOL_UDP) |
| 153 | return clib_error_return (0, |
| 154 | "only UDP protocol (%d) is supported, got %d", |
| 155 | IP_PROTOCOL_UDP, protocol); |
| 156 | |
| 157 | if (port == (u16) ~ 0) |
| 158 | return clib_error_return (0, "UDP port number required"); |
| 159 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 160 | c = punt_client_l4_get (af, port); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 161 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 162 | if (NULL == c) |
| 163 | { |
| 164 | pool_get_zero (pm->punt_client_pool, c); |
| 165 | punt_client_l4_db_add (af, port, c - pm->punt_client_pool); |
| 166 | } |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 167 | |
BenoƮt Ganne | edb0d45 | 2021-10-12 10:14:30 +0200 | [diff] [blame] | 168 | snprintf (c->caddr.sun_path, sizeof (c->caddr.sun_path), "%s", |
| 169 | client_pathname); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 170 | c->caddr.sun_family = AF_UNIX; |
| 171 | c->reg.type = PUNT_TYPE_L4; |
| 172 | c->reg.punt.l4.port = port; |
| 173 | c->reg.punt.l4.protocol = protocol; |
| 174 | c->reg.punt.l4.af = af; |
| 175 | |
| 176 | u32 node_index = (af == AF_IP4 ? |
| 177 | udp4_punt_socket_node.index : |
| 178 | udp6_punt_socket_node.index); |
| 179 | |
| 180 | udp_register_dst_port (vm, port, node_index, af == AF_IP4); |
| 181 | |
| 182 | return (NULL); |
| 183 | } |
| 184 | |
| 185 | static clib_error_t * |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 186 | punt_socket_register_ip_proto (vlib_main_t * vm, |
| 187 | ip_address_family_t af, |
| 188 | ip_protocol_t proto, char *client_pathname) |
| 189 | { |
| 190 | punt_main_t *pm = &punt_main; |
| 191 | punt_client_t *c; |
| 192 | |
| 193 | c = punt_client_ip_proto_get (af, proto); |
| 194 | |
| 195 | if (NULL == c) |
| 196 | { |
| 197 | pool_get_zero (pm->punt_client_pool, c); |
| 198 | punt_client_ip_proto_db_add (af, proto, c - pm->punt_client_pool); |
| 199 | } |
| 200 | |
BenoƮt Ganne | edb0d45 | 2021-10-12 10:14:30 +0200 | [diff] [blame] | 201 | snprintf (c->caddr.sun_path, sizeof (c->caddr.sun_path), "%s", |
| 202 | client_pathname); |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 203 | c->caddr.sun_family = AF_UNIX; |
| 204 | c->reg.type = PUNT_TYPE_IP_PROTO; |
| 205 | c->reg.punt.ip_proto.protocol = proto; |
| 206 | c->reg.punt.ip_proto.af = af; |
| 207 | |
| 208 | if (af == AF_IP4) |
| 209 | ip4_register_protocol (proto, ip4_proto_punt_socket_node.index); |
| 210 | else |
| 211 | ip6_register_protocol (proto, ip6_proto_punt_socket_node.index); |
| 212 | |
| 213 | return (NULL); |
| 214 | } |
| 215 | |
| 216 | static clib_error_t * |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 217 | punt_socket_register_exception (vlib_main_t * vm, |
| 218 | vlib_punt_reason_t reason, |
| 219 | char *client_pathname) |
| 220 | { |
| 221 | punt_main_t *pm = &punt_main; |
| 222 | punt_client_t *pc; |
| 223 | |
| 224 | pc = punt_client_exception_get (reason); |
| 225 | |
| 226 | if (NULL == pc) |
| 227 | { |
| 228 | pool_get_zero (pm->punt_client_pool, pc); |
| 229 | punt_client_exception_db_add (reason, pc - pm->punt_client_pool); |
| 230 | } |
| 231 | |
BenoƮt Ganne | edb0d45 | 2021-10-12 10:14:30 +0200 | [diff] [blame] | 232 | snprintf (pc->caddr.sun_path, sizeof (pc->caddr.sun_path), "%s", |
| 233 | client_pathname); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 234 | pc->caddr.sun_family = AF_UNIX; |
| 235 | pc->reg.type = PUNT_TYPE_EXCEPTION; |
| 236 | pc->reg.punt.exception.reason = reason; |
| 237 | |
| 238 | vlib_punt_register (pm->hdl, |
| 239 | pc->reg.punt.exception.reason, "exception-punt-socket"); |
| 240 | |
| 241 | return (NULL); |
| 242 | } |
| 243 | |
| 244 | static clib_error_t * |
| 245 | punt_socket_unregister_l4 (ip_address_family_t af, |
| 246 | ip_protocol_t protocol, u16 port) |
| 247 | { |
| 248 | u32 pci; |
| 249 | |
| 250 | udp_unregister_dst_port (vlib_get_main (), port, af == AF_IP4); |
| 251 | |
| 252 | pci = punt_client_l4_db_remove (af, port); |
| 253 | |
| 254 | if (~0 != pci) |
| 255 | pool_put_index (punt_main.punt_client_pool, pci); |
| 256 | |
| 257 | return (NULL); |
| 258 | } |
| 259 | |
| 260 | static clib_error_t * |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 261 | punt_socket_unregister_ip_proto (ip_address_family_t af, ip_protocol_t proto) |
| 262 | { |
| 263 | u32 pci; |
| 264 | |
| 265 | if (af == AF_IP4) |
| 266 | ip4_unregister_protocol (proto); |
| 267 | else |
| 268 | ip6_unregister_protocol (proto); |
| 269 | |
| 270 | pci = punt_client_ip_proto_db_remove (af, proto); |
| 271 | |
| 272 | if (~0 != pci) |
| 273 | pool_put_index (punt_main.punt_client_pool, pci); |
| 274 | |
| 275 | return (NULL); |
| 276 | } |
| 277 | |
| 278 | static clib_error_t * |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 279 | punt_socket_unregister_exception (vlib_punt_reason_t reason) |
| 280 | { |
| 281 | u32 pci; |
| 282 | |
| 283 | pci = punt_client_exception_db_remove (reason); |
| 284 | |
| 285 | if (~0 != pci) |
| 286 | pool_put_index (punt_main.punt_client_pool, pci); |
| 287 | |
| 288 | return (NULL); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | clib_error_t * |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 292 | vnet_punt_socket_add (vlib_main_t * vm, u32 header_version, |
| 293 | const punt_reg_t * pr, char *client_pathname) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 294 | { |
| 295 | punt_main_t *pm = &punt_main; |
| 296 | |
| 297 | if (!pm->is_configured) |
| 298 | return clib_error_return (0, "socket is not configured"); |
| 299 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 300 | if (header_version != PUNT_PACKETDESC_VERSION) |
| 301 | return clib_error_return (0, "Invalid packet descriptor version"); |
| 302 | |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 303 | if (strncmp (client_pathname, vnet_punt_get_server_pathname (), |
| 304 | UNIX_PATH_MAX) == 0) |
| 305 | return clib_error_return (0, |
| 306 | "Punt socket: Invalid client path: %s", |
| 307 | client_pathname); |
| 308 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 309 | /* Register client */ |
| 310 | switch (pr->type) |
| 311 | { |
| 312 | case PUNT_TYPE_L4: |
| 313 | return (punt_socket_register_l4 (vm, |
| 314 | pr->punt.l4.af, |
| 315 | pr->punt.l4.protocol, |
| 316 | pr->punt.l4.port, client_pathname)); |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 317 | case PUNT_TYPE_IP_PROTO: |
| 318 | return (punt_socket_register_ip_proto (vm, |
| 319 | pr->punt.ip_proto.af, |
| 320 | pr->punt.ip_proto.protocol, |
| 321 | client_pathname)); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 322 | case PUNT_TYPE_EXCEPTION: |
| 323 | return (punt_socket_register_exception (vm, |
| 324 | pr->punt.exception.reason, |
| 325 | client_pathname)); |
| 326 | } |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | clib_error_t * |
| 332 | vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr) |
| 333 | { |
| 334 | punt_main_t *pm = &punt_main; |
| 335 | |
| 336 | if (!pm->is_configured) |
| 337 | return clib_error_return (0, "socket is not configured"); |
| 338 | |
| 339 | switch (pr->type) |
| 340 | { |
| 341 | case PUNT_TYPE_L4: |
| 342 | return (punt_socket_unregister_l4 (pr->punt.l4.af, |
| 343 | pr->punt.l4.protocol, |
| 344 | pr->punt.l4.port)); |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 345 | case PUNT_TYPE_IP_PROTO: |
| 346 | return (punt_socket_unregister_ip_proto (pr->punt.ip_proto.af, |
| 347 | pr->punt.ip_proto.protocol)); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 348 | case PUNT_TYPE_EXCEPTION: |
| 349 | return (punt_socket_unregister_exception (pr->punt.exception.reason)); |
| 350 | } |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 355 | /** |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 356 | * @brief Request IP L4 traffic punt to the local TCP/IP stack. |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 357 | * |
| 358 | * @em Note |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 359 | * - UDP is the only protocol supported in the current implementation |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 360 | * |
| 361 | * @param vm vlib_main_t corresponding to the current thread |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 362 | * @param af IP address family. |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 363 | * @param protocol 8-bits L4 protocol value |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 364 | * UDP is 17 |
| 365 | * TCP is 1 |
| 366 | * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 367 | * |
| 368 | * @returns 0 on success, non-zero value otherwise |
| 369 | */ |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 370 | static clib_error_t * |
| 371 | punt_l4_add_del (vlib_main_t * vm, |
| 372 | ip_address_family_t af, |
| 373 | ip_protocol_t protocol, u16 port, bool is_add) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 374 | { |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 375 | int is_ip4 = af == AF_IP4; |
| 376 | |
Florin Coras | 3ffe6ca | 2019-06-26 16:27:13 -0700 | [diff] [blame] | 377 | /* For now we only support TCP and UDP punt */ |
| 378 | if (protocol != IP_PROTOCOL_UDP && protocol != IP_PROTOCOL_TCP) |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 379 | return clib_error_return (0, |
Florin Coras | 3ffe6ca | 2019-06-26 16:27:13 -0700 | [diff] [blame] | 380 | "only UDP (%d) and TCP (%d) protocols are supported, got %d", |
| 381 | IP_PROTOCOL_UDP, IP_PROTOCOL_TCP, protocol); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 382 | |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 383 | if (port == (u16) ~ 0) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 384 | { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 385 | if (protocol == IP_PROTOCOL_UDP) |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 386 | udp_punt_unknown (vm, is_ip4, is_add); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 387 | else if (protocol == IP_PROTOCOL_TCP) |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 388 | tcp_punt_unknown (vm, is_ip4, is_add); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | else if (is_add) |
| 394 | { |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 395 | const vlib_node_registration_t *punt_node = |
| 396 | is_ip4 ? &udp4_punt_node : &udp6_punt_node; |
| 397 | |
Florin Coras | 3ffe6ca | 2019-06-26 16:27:13 -0700 | [diff] [blame] | 398 | if (protocol == IP_PROTOCOL_TCP) |
| 399 | return clib_error_return (0, "punt TCP ports is not supported yet"); |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 400 | |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 401 | udp_register_dst_port (vm, port, punt_node->index, is_ip4); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 402 | |
| 403 | return 0; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 404 | } |
| 405 | else |
Pavel Kotucek | 41b923a | 2018-12-05 17:16:23 +0100 | [diff] [blame] | 406 | { |
Florin Coras | 3ffe6ca | 2019-06-26 16:27:13 -0700 | [diff] [blame] | 407 | if (protocol == IP_PROTOCOL_TCP) |
| 408 | return clib_error_return (0, "punt TCP ports is not supported yet"); |
Pavel Kotucek | 41b923a | 2018-12-05 17:16:23 +0100 | [diff] [blame] | 409 | |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 410 | udp_unregister_dst_port (vm, port, is_ip4); |
Pavel Kotucek | 41b923a | 2018-12-05 17:16:23 +0100 | [diff] [blame] | 411 | |
| 412 | return 0; |
| 413 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 414 | } |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 415 | |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 416 | /** |
| 417 | * @brief Request exception traffic punt. |
| 418 | * |
| 419 | * @param reason Punting reason |
| 420 | * |
| 421 | * @returns 0 on success, non-zero value otherwise |
| 422 | */ |
| 423 | static clib_error_t * |
| 424 | punt_exception_add_del (vlib_punt_reason_t reason, bool is_add) |
| 425 | { |
| 426 | punt_main_t *pm = &punt_main; |
| 427 | int rv = 0; |
| 428 | vnet_punt_reason_flag_t flag = vlib_punt_reason_get_flags (reason); |
| 429 | const char *node_name = |
| 430 | vnet_punt_reason_flag_is_IP6_PACKET (flag) ? "ip6-punt" : "ip4-punt"; |
| 431 | if (is_add) |
| 432 | rv = vlib_punt_register (pm->hdl, reason, node_name); |
| 433 | else |
| 434 | rv = vlib_punt_unregister (pm->hdl, reason, node_name); |
| 435 | if (!rv) |
| 436 | return 0; |
| 437 | else |
| 438 | return clib_error_return (0, is_add ? "Existing punting registration..." : |
| 439 | "Punting registration not found..."); |
| 440 | } |
| 441 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 442 | clib_error_t * |
| 443 | vnet_punt_add_del (vlib_main_t * vm, const punt_reg_t * pr, bool is_add) |
| 444 | { |
| 445 | switch (pr->type) |
| 446 | { |
| 447 | case PUNT_TYPE_L4: |
| 448 | return (punt_l4_add_del (vm, pr->punt.l4.af, pr->punt.l4.protocol, |
| 449 | pr->punt.l4.port, is_add)); |
| 450 | case PUNT_TYPE_EXCEPTION: |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 451 | return punt_exception_add_del (pr->punt.exception.reason, is_add); |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 452 | case PUNT_TYPE_IP_PROTO: |
| 453 | break; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | return (clib_error_return (0, "Unsupported punt type: %d", pr->type)); |
| 457 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 458 | |
| 459 | static clib_error_t * |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 460 | punt_cli (vlib_main_t * vm, |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 461 | unformat_input_t * input__, vlib_cli_command_t * cmd) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 462 | { |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 463 | unformat_input_t line_input, *input = &line_input; |
Swarup Nayak | 1b70884 | 2017-12-13 13:27:23 +0530 | [diff] [blame] | 464 | clib_error_t *error = NULL; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 465 | bool is_add = true; |
Neale Ranns | 68577d2 | 2019-06-04 13:31:23 +0000 | [diff] [blame] | 466 | /* *INDENT-OFF* */ |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 467 | punt_reg_t pr = { |
| 468 | .punt = { |
Neale Ranns | 68577d2 | 2019-06-04 13:31:23 +0000 | [diff] [blame] | 469 | .l4 = { |
| 470 | .af = AF_IP4, |
| 471 | .port = ~0, |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 472 | .protocol = IP_PROTOCOL_UDP, |
Neale Ranns | 68577d2 | 2019-06-04 13:31:23 +0000 | [diff] [blame] | 473 | }, |
| 474 | }, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 475 | .type = PUNT_TYPE_L4, |
| 476 | }; |
Neale Ranns | 68577d2 | 2019-06-04 13:31:23 +0000 | [diff] [blame] | 477 | u32 port; |
| 478 | /* *INDENT-ON* */ |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 479 | |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 480 | if (!unformat_user (input__, unformat_line_input, input)) |
| 481 | return 0; |
| 482 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 483 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 484 | { |
| 485 | if (unformat (input, "del")) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 486 | is_add = false; |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 487 | else if (unformat (input, "reason %U", unformat_punt_reason, |
| 488 | &pr.punt.exception.reason)) |
| 489 | pr.type = PUNT_TYPE_EXCEPTION; |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 490 | else if (unformat (input, "ipv4")) |
| 491 | pr.punt.l4.af = AF_IP4; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 492 | else if (unformat (input, "ipv6")) |
| 493 | pr.punt.l4.af = AF_IP6; |
| 494 | else if (unformat (input, "ip6")) |
| 495 | pr.punt.l4.af = AF_IP6; |
Neale Ranns | 68577d2 | 2019-06-04 13:31:23 +0000 | [diff] [blame] | 496 | else if (unformat (input, "%d", &port)) |
| 497 | pr.punt.l4.port = port; |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 498 | else if (unformat (input, "all")) |
| 499 | pr.punt.l4.port = ~0; |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 500 | else if (unformat (input, "udp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 501 | pr.punt.l4.protocol = IP_PROTOCOL_UDP; |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 502 | else if (unformat (input, "tcp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 503 | pr.punt.l4.protocol = IP_PROTOCOL_TCP; |
Swarup Nayak | 1b70884 | 2017-12-13 13:27:23 +0530 | [diff] [blame] | 504 | else |
| 505 | { |
| 506 | error = clib_error_return (0, "parse error: '%U'", |
| 507 | format_unformat_error, input); |
| 508 | goto done; |
| 509 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 510 | } |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 511 | |
| 512 | /* punt both IPv6 and IPv4 when used in CLI */ |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 513 | error = vnet_punt_add_del (vm, &pr, is_add); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 514 | if (error) |
| 515 | { |
| 516 | clib_error_report (error); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Swarup Nayak | 1b70884 | 2017-12-13 13:27:23 +0530 | [diff] [blame] | 519 | done: |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 520 | unformat_free (input); |
Swarup Nayak | 1b70884 | 2017-12-13 13:27:23 +0530 | [diff] [blame] | 521 | return error; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /*? |
| 525 | * The set of '<em>set punt</em>' commands allows specific IP traffic to |
| 526 | * be punted to the host TCP/IP stack |
| 527 | * |
| 528 | * @em Note |
| 529 | * - UDP is the only protocol supported in the current implementation |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 530 | * - All TCP traffic is currently punted to the host by default |
| 531 | * |
| 532 | * @cliexpar |
| 533 | * @parblock |
| 534 | * Example of how to request NTP traffic to be punted |
| 535 | * @cliexcmd{set punt udp 125} |
| 536 | * |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 537 | * Example of how to request all 'unknown' UDP traffic to be punted |
| 538 | * @cliexcmd{set punt udp all} |
| 539 | * |
| 540 | * Example of how to stop all 'unknown' UDP traffic to be punted |
| 541 | * @cliexcmd{set punt udp del all} |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 542 | * @endparblock |
| 543 | ?*/ |
| 544 | /* *INDENT-OFF* */ |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 545 | VLIB_CLI_COMMAND (punt_command, static) = { |
| 546 | .path = "set punt", |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 547 | .short_help = "set punt [IPV4|ip6|ipv6] [UDP|tcp] [del] [ALL|<port-num>]", |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 548 | .function = punt_cli, |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 549 | }; |
| 550 | /* *INDENT-ON* */ |
| 551 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 552 | static clib_error_t * |
| 553 | punt_socket_register_cmd (vlib_main_t * vm, |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 554 | unformat_input_t * input__, |
| 555 | vlib_cli_command_t * cmd) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 556 | { |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 557 | unformat_input_t line_input, *input = &line_input; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 558 | u8 *socket_name = 0; |
| 559 | clib_error_t *error = NULL; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 560 | /* *INDENT-OFF* */ |
| 561 | punt_reg_t pr = { |
| 562 | .punt = { |
| 563 | .l4 = { |
| 564 | .af = AF_IP4, |
| 565 | .port = ~0, |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 566 | .protocol = IP_PROTOCOL_UDP, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 567 | }, |
| 568 | }, |
| 569 | .type = PUNT_TYPE_L4, |
| 570 | }; |
| 571 | /* *INDENT-ON* */ |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 572 | |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 573 | if (!unformat_user (input__, unformat_line_input, input)) |
| 574 | return 0; |
| 575 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 576 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 577 | { |
| 578 | if (unformat (input, "ipv4")) |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 579 | pr.punt.l4.af = AF_IP4; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 580 | else if (unformat (input, "ipv6")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 581 | pr.punt.l4.af = AF_IP6; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 582 | else if (unformat (input, "udp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 583 | pr.punt.l4.protocol = IP_PROTOCOL_UDP; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 584 | else if (unformat (input, "tcp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 585 | pr.punt.l4.protocol = IP_PROTOCOL_TCP; |
| 586 | else if (unformat (input, "%d", &pr.punt.l4.port)) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 587 | ; |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 588 | else if (unformat (input, "all")) |
| 589 | pr.punt.l4.port = ~0; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 590 | else if (unformat (input, "socket %s", &socket_name)) |
| 591 | ; |
Arthur de Kerhor | a80ff13 | 2021-04-12 08:16:56 -0700 | [diff] [blame] | 592 | else if (unformat (input, "reason %U", unformat_punt_reason, |
| 593 | &pr.punt.exception.reason)) |
| 594 | pr.type = PUNT_TYPE_EXCEPTION; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 595 | else |
| 596 | { |
| 597 | error = clib_error_return (0, "parse error: '%U'", |
| 598 | format_unformat_error, input); |
| 599 | goto done; |
| 600 | } |
| 601 | } |
| 602 | |
Neale Ranns | 4f3c104 | 2019-06-11 01:39:08 -0700 | [diff] [blame] | 603 | if (!socket_name) |
| 604 | error = clib_error_return (0, "socket name not specified"); |
| 605 | else |
| 606 | error = vnet_punt_socket_add (vm, 1, &pr, (char *) socket_name); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 607 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 608 | done: |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 609 | unformat_free (input); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 610 | return error; |
| 611 | } |
| 612 | |
| 613 | /*? |
| 614 | * |
| 615 | * @cliexpar |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 616 | * @cliexcmd{punt socket register socket punt_l4_foo.sock} |
| 617 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 618 | ?*/ |
| 619 | /* *INDENT-OFF* */ |
| 620 | VLIB_CLI_COMMAND (punt_socket_register_command, static) = |
| 621 | { |
| 622 | .path = "punt socket register", |
| 623 | .function = punt_socket_register_cmd, |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 624 | .short_help = "punt socket register [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>] socket <socket>", |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 625 | .is_mp_safe = 1, |
| 626 | }; |
| 627 | /* *INDENT-ON* */ |
| 628 | |
| 629 | static clib_error_t * |
| 630 | punt_socket_deregister_cmd (vlib_main_t * vm, |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 631 | unformat_input_t * input__, |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 632 | vlib_cli_command_t * cmd) |
| 633 | { |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 634 | unformat_input_t line_input, *input = &line_input; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 635 | clib_error_t *error = NULL; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 636 | /* *INDENT-OFF* */ |
| 637 | punt_reg_t pr = { |
| 638 | .punt = { |
| 639 | .l4 = { |
| 640 | .af = AF_IP4, |
| 641 | .port = ~0, |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 642 | .protocol = IP_PROTOCOL_UDP, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 643 | }, |
| 644 | }, |
| 645 | .type = PUNT_TYPE_L4, |
| 646 | }; |
| 647 | /* *INDENT-ON* */ |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 648 | |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 649 | if (!unformat_user (input__, unformat_line_input, input)) |
| 650 | return 0; |
| 651 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 652 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 653 | { |
| 654 | if (unformat (input, "ipv4")) |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 655 | pr.punt.l4.af = AF_IP4; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 656 | else if (unformat (input, "ipv6")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 657 | pr.punt.l4.af = AF_IP6; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 658 | else if (unformat (input, "udp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 659 | pr.punt.l4.protocol = IP_PROTOCOL_UDP; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 660 | else if (unformat (input, "tcp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 661 | pr.punt.l4.protocol = IP_PROTOCOL_TCP; |
| 662 | else if (unformat (input, "%d", &pr.punt.l4.port)) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 663 | ; |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 664 | else if (unformat (input, "all")) |
| 665 | pr.punt.l4.port = ~0; |
Arthur de Kerhor | a80ff13 | 2021-04-12 08:16:56 -0700 | [diff] [blame] | 666 | else if (unformat (input, "reason %U", unformat_punt_reason, |
| 667 | &pr.punt.exception.reason)) |
| 668 | pr.type = PUNT_TYPE_EXCEPTION; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 669 | else |
| 670 | { |
| 671 | error = clib_error_return (0, "parse error: '%U'", |
| 672 | format_unformat_error, input); |
| 673 | goto done; |
| 674 | } |
| 675 | } |
| 676 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 677 | error = vnet_punt_socket_del (vm, &pr); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 678 | done: |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 679 | unformat_free (input); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 680 | return error; |
| 681 | } |
| 682 | |
| 683 | /*? |
| 684 | * |
| 685 | * @cliexpar |
| 686 | * @cliexcmd{punt socket register} |
| 687 | ?*/ |
| 688 | /* *INDENT-OFF* */ |
| 689 | VLIB_CLI_COMMAND (punt_socket_deregister_command, static) = |
| 690 | { |
| 691 | .path = "punt socket deregister", |
| 692 | .function = punt_socket_deregister_cmd, |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 693 | .short_help = "punt socket deregister [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>]", |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 694 | .is_mp_safe = 1, |
| 695 | }; |
| 696 | /* *INDENT-ON* */ |
| 697 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 698 | void |
| 699 | punt_client_walk (punt_type_t pt, punt_client_walk_cb_t cb, void *ctx) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 700 | { |
| 701 | punt_main_t *pm = &punt_main; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 702 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 703 | switch (pt) |
| 704 | { |
| 705 | case PUNT_TYPE_L4: |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 706 | { |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 707 | u32 pci, key; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 708 | |
| 709 | /* *INDENT-OFF* */ |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 710 | hash_foreach(key, pci, pm->db.clients_by_l4_port, |
| 711 | ({ |
| 712 | cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx); |
| 713 | })); |
| 714 | /* *INDENT-ON* */ |
| 715 | break; |
| 716 | } |
| 717 | case PUNT_TYPE_IP_PROTO: |
| 718 | { |
| 719 | u32 pci, key; |
| 720 | |
| 721 | /* *INDENT-OFF* */ |
| 722 | hash_foreach(key, pci, pm->db.clients_by_ip_proto, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 723 | ({ |
| 724 | cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx); |
| 725 | })); |
| 726 | /* *INDENT-ON* */ |
| 727 | break; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 728 | } |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 729 | case PUNT_TYPE_EXCEPTION: |
| 730 | { |
| 731 | u32 *pci; |
| 732 | |
| 733 | vec_foreach (pci, pm->db.clients_by_exception) |
| 734 | { |
| 735 | if (~0 != *pci) |
| 736 | cb (pool_elt_at_index (pm->punt_client_pool, *pci), ctx); |
| 737 | } |
| 738 | |
| 739 | break; |
| 740 | } |
| 741 | } |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 744 | static u8 * |
| 745 | format_punt_client (u8 * s, va_list * args) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 746 | { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 747 | punt_client_t *pc = va_arg (*args, punt_client_t *); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 748 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 749 | s = format (s, " punt "); |
| 750 | |
| 751 | switch (pc->reg.type) |
| 752 | { |
| 753 | case PUNT_TYPE_L4: |
| 754 | s = format (s, "%U %U port %d", |
| 755 | format_ip_address_family, pc->reg.punt.l4.af, |
| 756 | format_ip_protocol, pc->reg.punt.l4.protocol, |
| 757 | pc->reg.punt.l4.port); |
| 758 | break; |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 759 | case PUNT_TYPE_IP_PROTO: |
| 760 | s = format (s, "%U %U", |
| 761 | format_ip_address_family, pc->reg.punt.ip_proto.af, |
| 762 | format_ip_protocol, pc->reg.punt.ip_proto.protocol); |
| 763 | break; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 764 | case PUNT_TYPE_EXCEPTION: |
| 765 | s = format (s, " %U", format_vlib_punt_reason, |
| 766 | pc->reg.punt.exception.reason); |
| 767 | break; |
| 768 | } |
| 769 | |
| 770 | s = format (s, " to socket %s \n", pc->caddr.sun_path); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 771 | |
| 772 | return (s); |
| 773 | } |
| 774 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 775 | static walk_rc_t |
| 776 | punt_client_show_one (const punt_client_t * pc, void *ctx) |
| 777 | { |
| 778 | vlib_cli_output (ctx, "%U", format_punt_client, pc); |
| 779 | |
| 780 | return (WALK_CONTINUE); |
| 781 | } |
| 782 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 783 | static clib_error_t * |
| 784 | punt_socket_show_cmd (vlib_main_t * vm, |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 785 | unformat_input_t * input__, vlib_cli_command_t * cmd) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 786 | { |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 787 | unformat_input_t line_input, *input = &line_input; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 788 | clib_error_t *error = NULL; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 789 | punt_type_t pt; |
| 790 | |
| 791 | pt = PUNT_TYPE_L4; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 792 | |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 793 | if (!unformat_user (input__, unformat_line_input, input)) |
| 794 | return 0; |
| 795 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 796 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 797 | { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 798 | if (unformat (input, "exception")) |
| 799 | pt = PUNT_TYPE_EXCEPTION; |
| 800 | else if (unformat (input, "l4")) |
| 801 | pt = PUNT_TYPE_L4; |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 802 | else if (unformat (input, "ip")) |
| 803 | pt = PUNT_TYPE_IP_PROTO; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 804 | else |
| 805 | { |
| 806 | error = clib_error_return (0, "parse error: '%U'", |
| 807 | format_unformat_error, input); |
| 808 | goto done; |
| 809 | } |
| 810 | } |
| 811 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 812 | punt_client_walk (pt, punt_client_show_one, vm); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 813 | |
| 814 | done: |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 815 | unformat_free (input); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 816 | return (error); |
| 817 | } |
| 818 | |
| 819 | /*? |
| 820 | * |
| 821 | * @cliexpar |
| 822 | * @cliexcmd{show punt socket ipv4} |
| 823 | ?*/ |
| 824 | /* *INDENT-OFF* */ |
| 825 | VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) = |
| 826 | { |
| 827 | .path = "show punt socket registrations", |
| 828 | .function = punt_socket_show_cmd, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 829 | .short_help = "show punt socket registrations [l4|exception]", |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 830 | .is_mp_safe = 1, |
| 831 | }; |
| 832 | /* *INDENT-ON* */ |
| 833 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 834 | clib_error_t * |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 835 | ip_punt_init (vlib_main_t * vm) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 836 | { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 837 | clib_error_t *error = NULL; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 838 | punt_main_t *pm = &punt_main; |
Neale Ranns | 39040a6 | 2019-07-10 01:47:15 -0700 | [diff] [blame] | 839 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 840 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 841 | pm->is_configured = false; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 842 | pm->interface_output_node = |
| 843 | vlib_get_node_by_name (vm, (u8 *) "interface-output"); |
| 844 | |
| 845 | if ((error = vlib_call_init_function (vm, punt_init))) |
| 846 | return error; |
| 847 | |
| 848 | pm->hdl = vlib_punt_client_register ("ip-punt"); |
| 849 | |
Neale Ranns | 39040a6 | 2019-07-10 01:47:15 -0700 | [diff] [blame] | 850 | vec_validate_aligned (pm->thread_data, tm->n_vlib_mains, |
| 851 | CLIB_CACHE_LINE_BYTES); |
| 852 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 853 | return (error); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 854 | } |
| 855 | |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 856 | u8 * |
| 857 | format_vnet_punt_reason_flags (u8 *s, va_list *args) |
| 858 | { |
| 859 | vnet_punt_reason_flag_t flag = va_arg (*args, int); |
| 860 | #define _(pos, len, value, name, str) \ |
| 861 | if (vnet_punt_reason_flag_is_##name (flag)) \ |
| 862 | s = format (s, "%s ", str); |
| 863 | |
| 864 | foreach_vnet_punt_reason_flag |
| 865 | #undef _ |
| 866 | return (s); |
| 867 | } |
| 868 | |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 869 | VLIB_INIT_FUNCTION (ip_punt_init); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 870 | |
| 871 | static clib_error_t * |
| 872 | punt_config (vlib_main_t * vm, unformat_input_t * input) |
| 873 | { |
| 874 | punt_main_t *pm = &punt_main; |
| 875 | char *socket_path = 0; |
| 876 | |
| 877 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 878 | { |
| 879 | if (unformat (input, "socket %s", &socket_path)) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 880 | strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 881 | else |
| 882 | return clib_error_return (0, "unknown input `%U'", |
| 883 | format_unformat_error, input); |
| 884 | } |
| 885 | |
| 886 | if (socket_path == 0) |
| 887 | return 0; |
| 888 | |
| 889 | /* UNIX domain socket */ |
| 890 | struct sockaddr_un addr; |
| 891 | if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1) |
| 892 | { |
| 893 | return clib_error_return (0, "socket error"); |
| 894 | } |
| 895 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 896 | clib_memset (&addr, 0, sizeof (addr)); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 897 | addr.sun_family = AF_UNIX; |
| 898 | if (*socket_path == '\0') |
| 899 | { |
| 900 | *addr.sun_path = '\0'; |
| 901 | strncpy (addr.sun_path + 1, socket_path + 1, |
| 902 | sizeof (addr.sun_path) - 2); |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1); |
| 907 | unlink (socket_path); |
| 908 | } |
| 909 | |
| 910 | if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1) |
| 911 | { |
| 912 | return clib_error_return (0, "bind error"); |
| 913 | } |
| 914 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 915 | int n_bytes = 0x10000; |
| 916 | |
| 917 | if (setsockopt |
| 918 | (pm->socket_fd, SOL_SOCKET, SO_SNDBUF, &n_bytes, |
| 919 | sizeof (n_bytes)) == -1) |
| 920 | { |
| 921 | return clib_error_return (0, "setsockopt error"); |
| 922 | } |
| 923 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 924 | /* Register socket */ |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 925 | clib_file_main_t *fm = &file_main; |
| 926 | clib_file_t template = { 0 }; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 927 | template.read_function = punt_socket_read_ready; |
| 928 | template.file_descriptor = pm->socket_fd; |
Paul Vinciguerra | 5481ad4 | 2020-01-28 14:47:17 -0500 | [diff] [blame] | 929 | template.description = format (0, "punt socket %s", socket_path); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 930 | pm->clib_file_index = clib_file_add (fm, &template); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 931 | |
| 932 | pm->is_configured = true; |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | VLIB_CONFIG_FUNCTION (punt_config, "punt"); |
| 938 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 939 | /* |
| 940 | * fd.io coding-style-patch-verification: ON |
| 941 | * |
| 942 | * Local Variables: |
| 943 | * eval: (c-set-style "gnu") |
| 944 | * End: |
| 945 | */ |