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