blob: 6fb8dd58776809e0db7fa696a0f2b5552be6be21 [file] [log] [blame]
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -07001/*
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 Troanf7a55ad2017-05-16 14:59:29 +020020 * Provides a set of VPP nodes together with the relevant APIs and CLI
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070021 * commands in order to adjust and dispatch packets from the VPP data plane
22 * to the local TCP/IP stack
23 */
Ole Troanf7a55ad2017-05-16 14:59:29 +020024
25#include <vnet/ip/ip.h>
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070026#include <vlib/vlib.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050027#include <vnet/udp/udp.h>
Pierre Pfister7fe51f32017-09-20 08:48:36 +020028#include <vnet/tcp/tcp.h>
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070029#include <vnet/ip/punt.h>
Ole Troanf7a55ad2017-05-16 14:59:29 +020030#include <vlib/unix/unix.h>
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070031
Ole Troanf7a55ad2017-05-16 14:59:29 +020032#include <stdio.h>
33#include <unistd.h>
34#include <sys/socket.h>
Marco Varlese22349832017-09-08 10:40:34 +020035#include <sys/uio.h>
Ole Troanf7a55ad2017-05-16 14:59:29 +020036#include <stdlib.h>
Ole Troanf7a55ad2017-05-16 14:59:29 +020037
Ole Troanf7a55ad2017-05-16 14:59:29 +020038punt_main_t punt_main;
39
40char *
41vnet_punt_get_server_pathname (void)
42{
43 punt_main_t *pm = &punt_main;
44 return pm->sun_path;
45}
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070046
Ole Troanf7a55ad2017-05-16 14:59:29 +020047static void
Neale Ranns50f0ac02019-05-15 02:13:37 -070048punt_client_l4_db_add (ip_address_family_t af, u16 port, u32 index)
Ole Troanf7a55ad2017-05-16 14:59:29 +020049{
50 punt_main_t *pm = &punt_main;
Ole Troanf7a55ad2017-05-16 14:59:29 +020051
Neale Ranns50f0ac02019-05-15 02:13:37 -070052 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 Troanf7a55ad2017-05-16 14:59:29 +020055}
56
Neale Ranns50f0ac02019-05-15 02:13:37 -070057static u32
58punt_client_l4_db_remove (ip_address_family_t af, u16 port)
Ole Troanf7a55ad2017-05-16 14:59:29 +020059{
Neale Ranns50f0ac02019-05-15 02:13:37 -070060 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
75static void
Neale Rannsb538dd82019-05-21 06:54:54 -070076punt_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
87static u32
88punt_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
105static void
Neale Ranns50f0ac02019-05-15 02:13:37 -0700106punt_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
115static u32
116punt_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 Troanf7a55ad2017-05-16 14:59:29 +0200129
130static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200131punt_socket_read_ready (clib_file_t * uf)
Ole Troanf7a55ad2017-05-16 14:59:29 +0200132{
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 Ranns50f0ac02019-05-15 02:13:37 -0700143static clib_error_t *
144punt_socket_register_l4 (vlib_main_t * vm,
145 ip_address_family_t af,
146 u8 protocol, u16 port, char *client_pathname)
Ole Troanf7a55ad2017-05-16 14:59:29 +0200147{
148 punt_main_t *pm = &punt_main;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700149 punt_client_t *c;
Ole Troanf7a55ad2017-05-16 14:59:29 +0200150
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 Ranns50f0ac02019-05-15 02:13:37 -0700160 c = punt_client_l4_get (af, port);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200161
Neale Ranns50f0ac02019-05-15 02:13:37 -0700162 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 Troanf7a55ad2017-05-16 14:59:29 +0200167
Neale Ranns50f0ac02019-05-15 02:13:37 -0700168 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
184static clib_error_t *
Neale Rannsb538dd82019-05-21 06:54:54 -0700185punt_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
214static clib_error_t *
Neale Ranns50f0ac02019-05-15 02:13:37 -0700215punt_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
241static clib_error_t *
242punt_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
257static clib_error_t *
Neale Rannsb538dd82019-05-21 06:54:54 -0700258punt_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
275static clib_error_t *
Neale Ranns50f0ac02019-05-15 02:13:37 -0700276punt_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 Troanf7a55ad2017-05-16 14:59:29 +0200286}
287
288clib_error_t *
Neale Ranns50f0ac02019-05-15 02:13:37 -0700289vnet_punt_socket_add (vlib_main_t * vm, u32 header_version,
290 const punt_reg_t * pr, char *client_pathname)
Ole Troanf7a55ad2017-05-16 14:59:29 +0200291{
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 Ranns50f0ac02019-05-15 02:13:37 -0700297 if (header_version != PUNT_PACKETDESC_VERSION)
298 return clib_error_return (0, "Invalid packet descriptor version");
299
Neale Rannsb538dd82019-05-21 06:54:54 -0700300 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 Ranns50f0ac02019-05-15 02:13:37 -0700306 /* 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 Rannsb538dd82019-05-21 06:54:54 -0700314 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 Ranns50f0ac02019-05-15 02:13:37 -0700319 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
328clib_error_t *
329vnet_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 Rannsb538dd82019-05-21 06:54:54 -0700342 case PUNT_TYPE_IP_PROTO:
343 return (punt_socket_unregister_ip_proto (pr->punt.ip_proto.af,
344 pr->punt.ip_proto.protocol));
Neale Ranns50f0ac02019-05-15 02:13:37 -0700345 case PUNT_TYPE_EXCEPTION:
346 return (punt_socket_unregister_exception (pr->punt.exception.reason));
347 }
Ole Troanf7a55ad2017-05-16 14:59:29 +0200348
349 return 0;
350}
351
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700352/**
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400353 * @brief Request IP L4 traffic punt to the local TCP/IP stack.
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700354 *
355 * @em Note
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400356 * - UDP is the only protocol supported in the current implementation
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700357 *
358 * @param vm vlib_main_t corresponding to the current thread
Neale Ranns50f0ac02019-05-15 02:13:37 -0700359 * @param af IP address family.
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700360 * @param protocol 8-bits L4 protocol value
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200361 * UDP is 17
362 * TCP is 1
363 * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700364 *
365 * @returns 0 on success, non-zero value otherwise
366 */
Neale Ranns50f0ac02019-05-15 02:13:37 -0700367static clib_error_t *
368punt_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)4a7e58b2016-10-05 22:31:23 -0700371{
Florin Coras3ffe6ca2019-06-26 16:27:13 -0700372 /* For now we only support TCP and UDP punt */
373 if (protocol != IP_PROTOCOL_UDP && protocol != IP_PROTOCOL_TCP)
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800374 return clib_error_return (0,
Florin Coras3ffe6ca2019-06-26 16:27:13 -0700375 "only UDP (%d) and TCP (%d) protocols are supported, got %d",
376 IP_PROTOCOL_UDP, IP_PROTOCOL_TCP, protocol);
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700377
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800378 if (port == (u16) ~ 0)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700379 {
Neale Ranns50f0ac02019-05-15 02:13:37 -0700380 if (protocol == IP_PROTOCOL_UDP)
381 udp_punt_unknown (vm, af == AF_IP4, is_add);
382 else if (protocol == IP_PROTOCOL_TCP)
383 tcp_punt_unknown (vm, af == AF_IP4, is_add);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800384
385 return 0;
386 }
387
388 else if (is_add)
389 {
Florin Coras3ffe6ca2019-06-26 16:27:13 -0700390 if (protocol == IP_PROTOCOL_TCP)
391 return clib_error_return (0, "punt TCP ports is not supported yet");
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200392
Neale Ranns50f0ac02019-05-15 02:13:37 -0700393 udp_register_dst_port (vm, port, udp4_punt_node.index, af == AF_IP4);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800394
395 return 0;
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700396 }
397 else
Pavel Kotucek41b923a2018-12-05 17:16:23 +0100398 {
Florin Coras3ffe6ca2019-06-26 16:27:13 -0700399 if (protocol == IP_PROTOCOL_TCP)
400 return clib_error_return (0, "punt TCP ports is not supported yet");
Pavel Kotucek41b923a2018-12-05 17:16:23 +0100401
Neale Ranns50f0ac02019-05-15 02:13:37 -0700402 udp_unregister_dst_port (vm, port, af == AF_IP4);
Pavel Kotucek41b923a2018-12-05 17:16:23 +0100403
404 return 0;
405 }
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700406}
Neale Ranns50f0ac02019-05-15 02:13:37 -0700407
Mohammed Hawari45723b82021-02-05 15:40:00 +0100408/**
409 * @brief Request exception traffic punt.
410 *
411 * @param reason Punting reason
412 *
413 * @returns 0 on success, non-zero value otherwise
414 */
415static clib_error_t *
416punt_exception_add_del (vlib_punt_reason_t reason, bool is_add)
417{
418 punt_main_t *pm = &punt_main;
419 int rv = 0;
420 vnet_punt_reason_flag_t flag = vlib_punt_reason_get_flags (reason);
421 const char *node_name =
422 vnet_punt_reason_flag_is_IP6_PACKET (flag) ? "ip6-punt" : "ip4-punt";
423 if (is_add)
424 rv = vlib_punt_register (pm->hdl, reason, node_name);
425 else
426 rv = vlib_punt_unregister (pm->hdl, reason, node_name);
427 if (!rv)
428 return 0;
429 else
430 return clib_error_return (0, is_add ? "Existing punting registration..." :
431 "Punting registration not found...");
432}
433
Neale Ranns50f0ac02019-05-15 02:13:37 -0700434clib_error_t *
435vnet_punt_add_del (vlib_main_t * vm, const punt_reg_t * pr, bool is_add)
436{
437 switch (pr->type)
438 {
439 case PUNT_TYPE_L4:
440 return (punt_l4_add_del (vm, pr->punt.l4.af, pr->punt.l4.protocol,
441 pr->punt.l4.port, is_add));
442 case PUNT_TYPE_EXCEPTION:
Mohammed Hawari45723b82021-02-05 15:40:00 +0100443 return punt_exception_add_del (pr->punt.exception.reason, is_add);
Neale Rannsb538dd82019-05-21 06:54:54 -0700444 case PUNT_TYPE_IP_PROTO:
445 break;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700446 }
447
448 return (clib_error_return (0, "Unsupported punt type: %d", pr->type));
449}
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700450
451static clib_error_t *
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200452punt_cli (vlib_main_t * vm,
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200453 unformat_input_t * input__, vlib_cli_command_t * cmd)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700454{
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200455 unformat_input_t line_input, *input = &line_input;
Swarup Nayak1b708842017-12-13 13:27:23 +0530456 clib_error_t *error = NULL;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700457 bool is_add = true;
Neale Ranns68577d22019-06-04 13:31:23 +0000458 /* *INDENT-OFF* */
Neale Ranns50f0ac02019-05-15 02:13:37 -0700459 punt_reg_t pr = {
460 .punt = {
Neale Ranns68577d22019-06-04 13:31:23 +0000461 .l4 = {
462 .af = AF_IP4,
463 .port = ~0,
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400464 .protocol = IP_PROTOCOL_UDP,
Neale Ranns68577d22019-06-04 13:31:23 +0000465 },
466 },
Neale Ranns50f0ac02019-05-15 02:13:37 -0700467 .type = PUNT_TYPE_L4,
468 };
Neale Ranns68577d22019-06-04 13:31:23 +0000469 u32 port;
470 /* *INDENT-ON* */
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700471
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200472 if (!unformat_user (input__, unformat_line_input, input))
473 return 0;
474
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700475 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
476 {
477 if (unformat (input, "del"))
Ole Troanf7a55ad2017-05-16 14:59:29 +0200478 is_add = false;
Mohammed Hawari45723b82021-02-05 15:40:00 +0100479 else if (unformat (input, "reason %U", unformat_punt_reason,
480 &pr.punt.exception.reason))
481 pr.type = PUNT_TYPE_EXCEPTION;
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400482 else if (unformat (input, "ipv4"))
483 pr.punt.l4.af = AF_IP4;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700484 else if (unformat (input, "ipv6"))
485 pr.punt.l4.af = AF_IP6;
486 else if (unformat (input, "ip6"))
487 pr.punt.l4.af = AF_IP6;
Neale Ranns68577d22019-06-04 13:31:23 +0000488 else if (unformat (input, "%d", &port))
489 pr.punt.l4.port = port;
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400490 else if (unformat (input, "all"))
491 pr.punt.l4.port = ~0;
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200492 else if (unformat (input, "udp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700493 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200494 else if (unformat (input, "tcp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700495 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
Swarup Nayak1b708842017-12-13 13:27:23 +0530496 else
497 {
498 error = clib_error_return (0, "parse error: '%U'",
499 format_unformat_error, input);
500 goto done;
501 }
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700502 }
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100503
504 /* punt both IPv6 and IPv4 when used in CLI */
Neale Ranns50f0ac02019-05-15 02:13:37 -0700505 error = vnet_punt_add_del (vm, &pr, is_add);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100506 if (error)
507 {
508 clib_error_report (error);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100509 }
510
Swarup Nayak1b708842017-12-13 13:27:23 +0530511done:
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200512 unformat_free (input);
Swarup Nayak1b708842017-12-13 13:27:23 +0530513 return error;
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700514}
515
516/*?
517 * The set of '<em>set punt</em>' commands allows specific IP traffic to
518 * be punted to the host TCP/IP stack
519 *
520 * @em Note
521 * - UDP is the only protocol supported in the current implementation
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700522 * - All TCP traffic is currently punted to the host by default
523 *
524 * @cliexpar
525 * @parblock
526 * Example of how to request NTP traffic to be punted
527 * @cliexcmd{set punt udp 125}
528 *
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800529 * Example of how to request all 'unknown' UDP traffic to be punted
530 * @cliexcmd{set punt udp all}
531 *
532 * Example of how to stop all 'unknown' UDP traffic to be punted
533 * @cliexcmd{set punt udp del all}
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700534 * @endparblock
535?*/
536/* *INDENT-OFF* */
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200537VLIB_CLI_COMMAND (punt_command, static) = {
538 .path = "set punt",
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400539 .short_help = "set punt [IPV4|ip6|ipv6] [UDP|tcp] [del] [ALL|<port-num>]",
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200540 .function = punt_cli,
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700541};
542/* *INDENT-ON* */
543
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100544static clib_error_t *
545punt_socket_register_cmd (vlib_main_t * vm,
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200546 unformat_input_t * input__,
547 vlib_cli_command_t * cmd)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100548{
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200549 unformat_input_t line_input, *input = &line_input;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100550 u8 *socket_name = 0;
551 clib_error_t *error = NULL;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700552 /* *INDENT-OFF* */
553 punt_reg_t pr = {
554 .punt = {
555 .l4 = {
556 .af = AF_IP4,
557 .port = ~0,
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400558 .protocol = IP_PROTOCOL_UDP,
Neale Ranns50f0ac02019-05-15 02:13:37 -0700559 },
560 },
561 .type = PUNT_TYPE_L4,
562 };
563 /* *INDENT-ON* */
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100564
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200565 if (!unformat_user (input__, unformat_line_input, input))
566 return 0;
567
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100568 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
569 {
570 if (unformat (input, "ipv4"))
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400571 pr.punt.l4.af = AF_IP4;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100572 else if (unformat (input, "ipv6"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700573 pr.punt.l4.af = AF_IP6;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100574 else if (unformat (input, "udp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700575 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100576 else if (unformat (input, "tcp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700577 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
578 else if (unformat (input, "%d", &pr.punt.l4.port))
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100579 ;
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400580 else if (unformat (input, "all"))
581 pr.punt.l4.port = ~0;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100582 else if (unformat (input, "socket %s", &socket_name))
583 ;
584 else
585 {
586 error = clib_error_return (0, "parse error: '%U'",
587 format_unformat_error, input);
588 goto done;
589 }
590 }
591
Neale Ranns4f3c1042019-06-11 01:39:08 -0700592 if (!socket_name)
593 error = clib_error_return (0, "socket name not specified");
594 else
595 error = vnet_punt_socket_add (vm, 1, &pr, (char *) socket_name);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700596
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100597done:
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200598 unformat_free (input);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100599 return error;
600}
601
602/*?
603 *
604 * @cliexpar
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400605 * @cliexcmd{punt socket register socket punt_l4_foo.sock}
606
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100607 ?*/
608/* *INDENT-OFF* */
609VLIB_CLI_COMMAND (punt_socket_register_command, static) =
610{
611 .path = "punt socket register",
612 .function = punt_socket_register_cmd,
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400613 .short_help = "punt socket register [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>] socket <socket>",
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100614 .is_mp_safe = 1,
615};
616/* *INDENT-ON* */
617
618static clib_error_t *
619punt_socket_deregister_cmd (vlib_main_t * vm,
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200620 unformat_input_t * input__,
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100621 vlib_cli_command_t * cmd)
622{
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200623 unformat_input_t line_input, *input = &line_input;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100624 clib_error_t *error = NULL;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700625 /* *INDENT-OFF* */
626 punt_reg_t pr = {
627 .punt = {
628 .l4 = {
629 .af = AF_IP4,
630 .port = ~0,
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400631 .protocol = IP_PROTOCOL_UDP,
Neale Ranns50f0ac02019-05-15 02:13:37 -0700632 },
633 },
634 .type = PUNT_TYPE_L4,
635 };
636 /* *INDENT-ON* */
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100637
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200638 if (!unformat_user (input__, unformat_line_input, input))
639 return 0;
640
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100641 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
642 {
643 if (unformat (input, "ipv4"))
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400644 pr.punt.l4.af = AF_IP4;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100645 else if (unformat (input, "ipv6"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700646 pr.punt.l4.af = AF_IP6;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100647 else if (unformat (input, "udp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700648 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100649 else if (unformat (input, "tcp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700650 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
651 else if (unformat (input, "%d", &pr.punt.l4.port))
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100652 ;
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400653 else if (unformat (input, "all"))
654 pr.punt.l4.port = ~0;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100655 else
656 {
657 error = clib_error_return (0, "parse error: '%U'",
658 format_unformat_error, input);
659 goto done;
660 }
661 }
662
Neale Ranns50f0ac02019-05-15 02:13:37 -0700663 error = vnet_punt_socket_del (vm, &pr);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100664done:
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200665 unformat_free (input);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100666 return error;
667}
668
669/*?
670 *
671 * @cliexpar
672 * @cliexcmd{punt socket register}
673 ?*/
674/* *INDENT-OFF* */
675VLIB_CLI_COMMAND (punt_socket_deregister_command, static) =
676{
677 .path = "punt socket deregister",
678 .function = punt_socket_deregister_cmd,
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400679 .short_help = "punt socket deregister [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>]",
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100680 .is_mp_safe = 1,
681};
682/* *INDENT-ON* */
683
Neale Ranns50f0ac02019-05-15 02:13:37 -0700684void
685punt_client_walk (punt_type_t pt, punt_client_walk_cb_t cb, void *ctx)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100686{
687 punt_main_t *pm = &punt_main;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100688
Neale Ranns50f0ac02019-05-15 02:13:37 -0700689 switch (pt)
690 {
691 case PUNT_TYPE_L4:
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100692 {
Neale Rannsb538dd82019-05-21 06:54:54 -0700693 u32 pci, key;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700694
695 /* *INDENT-OFF* */
Neale Rannsb538dd82019-05-21 06:54:54 -0700696 hash_foreach(key, pci, pm->db.clients_by_l4_port,
697 ({
698 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
699 }));
700 /* *INDENT-ON* */
701 break;
702 }
703 case PUNT_TYPE_IP_PROTO:
704 {
705 u32 pci, key;
706
707 /* *INDENT-OFF* */
708 hash_foreach(key, pci, pm->db.clients_by_ip_proto,
Neale Ranns50f0ac02019-05-15 02:13:37 -0700709 ({
710 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
711 }));
712 /* *INDENT-ON* */
713 break;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100714 }
Neale Ranns50f0ac02019-05-15 02:13:37 -0700715 case PUNT_TYPE_EXCEPTION:
716 {
717 u32 *pci;
718
719 vec_foreach (pci, pm->db.clients_by_exception)
720 {
721 if (~0 != *pci)
722 cb (pool_elt_at_index (pm->punt_client_pool, *pci), ctx);
723 }
724
725 break;
726 }
727 }
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100728}
729
Neale Ranns50f0ac02019-05-15 02:13:37 -0700730static u8 *
731format_punt_client (u8 * s, va_list * args)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100732{
Neale Ranns50f0ac02019-05-15 02:13:37 -0700733 punt_client_t *pc = va_arg (*args, punt_client_t *);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100734
Neale Ranns50f0ac02019-05-15 02:13:37 -0700735 s = format (s, " punt ");
736
737 switch (pc->reg.type)
738 {
739 case PUNT_TYPE_L4:
740 s = format (s, "%U %U port %d",
741 format_ip_address_family, pc->reg.punt.l4.af,
742 format_ip_protocol, pc->reg.punt.l4.protocol,
743 pc->reg.punt.l4.port);
744 break;
Neale Rannsb538dd82019-05-21 06:54:54 -0700745 case PUNT_TYPE_IP_PROTO:
746 s = format (s, "%U %U",
747 format_ip_address_family, pc->reg.punt.ip_proto.af,
748 format_ip_protocol, pc->reg.punt.ip_proto.protocol);
749 break;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700750 case PUNT_TYPE_EXCEPTION:
751 s = format (s, " %U", format_vlib_punt_reason,
752 pc->reg.punt.exception.reason);
753 break;
754 }
755
756 s = format (s, " to socket %s \n", pc->caddr.sun_path);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100757
758 return (s);
759}
760
Neale Ranns50f0ac02019-05-15 02:13:37 -0700761static walk_rc_t
762punt_client_show_one (const punt_client_t * pc, void *ctx)
763{
764 vlib_cli_output (ctx, "%U", format_punt_client, pc);
765
766 return (WALK_CONTINUE);
767}
768
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100769static clib_error_t *
770punt_socket_show_cmd (vlib_main_t * vm,
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200771 unformat_input_t * input__, vlib_cli_command_t * cmd)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100772{
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200773 unformat_input_t line_input, *input = &line_input;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100774 clib_error_t *error = NULL;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700775 punt_type_t pt;
776
777 pt = PUNT_TYPE_L4;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100778
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200779 if (!unformat_user (input__, unformat_line_input, input))
780 return 0;
781
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100782 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
783 {
Neale Ranns50f0ac02019-05-15 02:13:37 -0700784 if (unformat (input, "exception"))
785 pt = PUNT_TYPE_EXCEPTION;
786 else if (unformat (input, "l4"))
787 pt = PUNT_TYPE_L4;
Neale Rannsb538dd82019-05-21 06:54:54 -0700788 else if (unformat (input, "ip"))
789 pt = PUNT_TYPE_IP_PROTO;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100790 else
791 {
792 error = clib_error_return (0, "parse error: '%U'",
793 format_unformat_error, input);
794 goto done;
795 }
796 }
797
Neale Ranns50f0ac02019-05-15 02:13:37 -0700798 punt_client_walk (pt, punt_client_show_one, vm);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100799
800done:
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200801 unformat_free (input);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100802 return (error);
803}
804
805/*?
806 *
807 * @cliexpar
808 * @cliexcmd{show punt socket ipv4}
809 ?*/
810/* *INDENT-OFF* */
811VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) =
812{
813 .path = "show punt socket registrations",
814 .function = punt_socket_show_cmd,
Neale Ranns50f0ac02019-05-15 02:13:37 -0700815 .short_help = "show punt socket registrations [l4|exception]",
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100816 .is_mp_safe = 1,
817};
818/* *INDENT-ON* */
819
Ole Troanf7a55ad2017-05-16 14:59:29 +0200820clib_error_t *
Neale Ranns76b56492018-09-28 15:16:14 +0000821ip_punt_init (vlib_main_t * vm)
Ole Troanf7a55ad2017-05-16 14:59:29 +0200822{
Neale Ranns50f0ac02019-05-15 02:13:37 -0700823 clib_error_t *error = NULL;
Ole Troanf7a55ad2017-05-16 14:59:29 +0200824 punt_main_t *pm = &punt_main;
Neale Ranns39040a62019-07-10 01:47:15 -0700825 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ole Troanf7a55ad2017-05-16 14:59:29 +0200826
Ole Troanf7a55ad2017-05-16 14:59:29 +0200827 pm->is_configured = false;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700828 pm->interface_output_node =
829 vlib_get_node_by_name (vm, (u8 *) "interface-output");
830
831 if ((error = vlib_call_init_function (vm, punt_init)))
832 return error;
833
834 pm->hdl = vlib_punt_client_register ("ip-punt");
835
Neale Ranns39040a62019-07-10 01:47:15 -0700836 vec_validate_aligned (pm->thread_data, tm->n_vlib_mains,
837 CLIB_CACHE_LINE_BYTES);
838
Neale Ranns50f0ac02019-05-15 02:13:37 -0700839 return (error);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200840}
841
Mohammed Hawari45723b82021-02-05 15:40:00 +0100842u8 *
843format_vnet_punt_reason_flags (u8 *s, va_list *args)
844{
845 vnet_punt_reason_flag_t flag = va_arg (*args, int);
846#define _(pos, len, value, name, str) \
847 if (vnet_punt_reason_flag_is_##name (flag)) \
848 s = format (s, "%s ", str);
849
850 foreach_vnet_punt_reason_flag
851#undef _
852 return (s);
853}
854
Neale Ranns76b56492018-09-28 15:16:14 +0000855VLIB_INIT_FUNCTION (ip_punt_init);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200856
857static clib_error_t *
858punt_config (vlib_main_t * vm, unformat_input_t * input)
859{
860 punt_main_t *pm = &punt_main;
861 char *socket_path = 0;
862
863 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
864 {
865 if (unformat (input, "socket %s", &socket_path))
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100866 strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200867 else
868 return clib_error_return (0, "unknown input `%U'",
869 format_unformat_error, input);
870 }
871
872 if (socket_path == 0)
873 return 0;
874
875 /* UNIX domain socket */
876 struct sockaddr_un addr;
877 if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
878 {
879 return clib_error_return (0, "socket error");
880 }
881
Dave Barachb7b92992018-10-17 10:38:51 -0400882 clib_memset (&addr, 0, sizeof (addr));
Ole Troanf7a55ad2017-05-16 14:59:29 +0200883 addr.sun_family = AF_UNIX;
884 if (*socket_path == '\0')
885 {
886 *addr.sun_path = '\0';
887 strncpy (addr.sun_path + 1, socket_path + 1,
888 sizeof (addr.sun_path) - 2);
889 }
890 else
891 {
892 strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
893 unlink (socket_path);
894 }
895
896 if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
897 {
898 return clib_error_return (0, "bind error");
899 }
900
Neale Ranns50f0ac02019-05-15 02:13:37 -0700901 int n_bytes = 0x10000;
902
903 if (setsockopt
904 (pm->socket_fd, SOL_SOCKET, SO_SNDBUF, &n_bytes,
905 sizeof (n_bytes)) == -1)
906 {
907 return clib_error_return (0, "setsockopt error");
908 }
909
Ole Troanf7a55ad2017-05-16 14:59:29 +0200910 /* Register socket */
Damjan Marion56dd5432017-09-08 19:52:02 +0200911 clib_file_main_t *fm = &file_main;
912 clib_file_t template = { 0 };
Ole Troanf7a55ad2017-05-16 14:59:29 +0200913 template.read_function = punt_socket_read_ready;
914 template.file_descriptor = pm->socket_fd;
Paul Vinciguerra5481ad42020-01-28 14:47:17 -0500915 template.description = format (0, "punt socket %s", socket_path);
Damjan Marion56dd5432017-09-08 19:52:02 +0200916 pm->clib_file_index = clib_file_add (fm, &template);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200917
918 pm->is_configured = true;
919
920 return 0;
921}
922
923VLIB_CONFIG_FUNCTION (punt_config, "punt");
924
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700925/*
926 * fd.io coding-style-patch-verification: ON
927 *
928 * Local Variables:
929 * eval: (c-set-style "gnu")
930 * End:
931 */