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