blob: 4a027bfdadbc23e16b73ee4b540b617d5df60fe7 [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>
27#include <vnet/pg/pg.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050028#include <vnet/udp/udp.h>
Pierre Pfister7fe51f32017-09-20 08:48:36 +020029#include <vnet/tcp/tcp.h>
Marco Varlese191a5942017-10-30 18:17:21 +010030#include <vnet/sctp/sctp.h>
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070031#include <vnet/ip/punt.h>
Ole Troanf7a55ad2017-05-16 14:59:29 +020032#include <vppinfra/sparse_vec.h>
33#include <vlib/unix/unix.h>
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070034
Ole Troanf7a55ad2017-05-16 14:59:29 +020035#include <stdio.h>
36#include <unistd.h>
37#include <sys/socket.h>
38#include <sys/un.h>
Marco Varlese22349832017-09-08 10:40:34 +020039#include <sys/uio.h>
Ole Troanf7a55ad2017-05-16 14:59:29 +020040#include <stdlib.h>
41#include <stdbool.h>
42
43#define foreach_punt_next \
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -080044 _ (PUNT4, "ip4-punt") \
45 _ (PUNT6, "ip6-punt")
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070046
47typedef enum
48{
49#define _(s,n) PUNT_NEXT_##s,
50 foreach_punt_next
51#undef _
52 PUNT_N_NEXT,
53} punt_next_t;
54
Ole Troanf7a55ad2017-05-16 14:59:29 +020055enum punt_socket_rx_next_e
56{
57 PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT,
58 PUNT_SOCKET_RX_NEXT_IP4_LOOKUP,
59 PUNT_SOCKET_RX_NEXT_IP6_LOOKUP,
60 PUNT_SOCKET_RX_N_NEXT
61};
62
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -080063#define punt_next_punt(is_ip4) (is_ip4 ? PUNT_NEXT_PUNT4 : PUNT_NEXT_PUNT6)
64
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070065vlib_node_registration_t udp4_punt_node;
66vlib_node_registration_t udp6_punt_node;
Ole Troanf7a55ad2017-05-16 14:59:29 +020067vlib_node_registration_t udp4_punt_socket_node;
68vlib_node_registration_t udp6_punt_socket_node;
69static vlib_node_registration_t punt_socket_rx_node;
70
71punt_main_t punt_main;
72
73char *
74vnet_punt_get_server_pathname (void)
75{
76 punt_main_t *pm = &punt_main;
77 return pm->sun_path;
78}
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070079
80/** @brief IPv4/IPv6 UDP punt node main loop.
81
82 This is the main loop inline function for IPv4/IPv6 UDP punt
83 transition node.
84
85 @param vm vlib_main_t corresponding to the current thread
86 @param node vlib_node_runtime_t
87 @param frame vlib_frame_t whose contents should be dispatched
88 @param is_ipv4 indicates if called for IPv4 or IPv6 node
89*/
90always_inline uword
91udp46_punt_inline (vlib_main_t * vm,
92 vlib_node_runtime_t * node,
93 vlib_frame_t * from_frame, int is_ip4)
94{
95 u32 n_left_from, *from, *to_next;
96 word advance;
97
98 from = vlib_frame_vector_args (from_frame);
99 n_left_from = from_frame->n_vectors;
100
101 /* udp[46]_lookup hands us the data payload, not the IP header */
102 if (is_ip4)
103 advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t));
104 else
105 advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t));
106
107 while (n_left_from > 0)
108 {
109 u32 n_left_to_next;
110
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800111 vlib_get_next_frame (vm, node, punt_next_punt (is_ip4), to_next,
112 n_left_to_next);
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700113
114 while (n_left_from > 0 && n_left_to_next > 0)
115 {
116 u32 bi0;
117 vlib_buffer_t *b0;
118
119 bi0 = from[0];
120 to_next[0] = bi0;
121 from += 1;
122 to_next += 1;
123 n_left_from -= 1;
124 n_left_to_next -= 1;
125
126 b0 = vlib_get_buffer (vm, bi0);
127 vlib_buffer_advance (b0, advance);
128 b0->error = node->errors[PUNT_ERROR_UDP_PORT];
129 }
130
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800131 vlib_put_next_frame (vm, node, punt_next_punt (is_ip4), n_left_to_next);
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700132 }
133
134 return from_frame->n_vectors;
135}
136
137static char *punt_error_strings[] = {
138#define punt_error(n,s) s,
139#include "punt_error.def"
140#undef punt_error
141};
142
143/** @brief IPv4 UDP punt node.
144 @node ip4-udp-punt
145
146 This is the IPv4 UDP punt transition node. It is registered as a next
147 node for the "ip4-udp-lookup" handling UDP port(s) requested for punt.
148 The buffer's current data pointer is adjusted to the original packet
149 IPv4 header. All buffers are dispatched to "error-punt".
150
151 @param vm vlib_main_t corresponding to the current thread
152 @param node vlib_node_runtime_t
153 @param frame vlib_frame_t whose contents should be dispatched
154
155 @par Graph mechanics: next index usage
156
157 @em Sets:
158 - <code>vnet_buffer(b)->current_data</code>
159 - <code>vnet_buffer(b)->current_len</code>
160
161 <em>Next Index:</em>
162 - Dispatches the packet to the "error-punt" node
163*/
164static uword
165udp4_punt (vlib_main_t * vm,
166 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
167{
168 return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ );
169}
170
171/** @brief IPv6 UDP punt node.
172 @node ip6-udp-punt
173
174 This is the IPv6 UDP punt transition node. It is registered as a next
175 node for the "ip6-udp-lookup" handling UDP port(s) requested for punt.
176 The buffer's current data pointer is adjusted to the original packet
177 IPv6 header. All buffers are dispatched to "error-punt".
178
179 @param vm vlib_main_t corresponding to the current thread
180 @param node vlib_node_runtime_t
181 @param frame vlib_frame_t whose contents should be dispatched
182
183 @par Graph mechanics: next index usage
184
185 @em Sets:
186 - <code>vnet_buffer(b)->current_data</code>
187 - <code>vnet_buffer(b)->current_len</code>
188
189 <em>Next Index:</em>
190 - Dispatches the packet to the "error-punt" node
191*/
192static uword
193udp6_punt (vlib_main_t * vm,
194 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
195{
196 return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ );
197}
198
199/* *INDENT-OFF* */
200VLIB_REGISTER_NODE (udp4_punt_node) = {
201 .function = udp4_punt,
202 .name = "ip4-udp-punt",
203 /* Takes a vector of packets. */
204 .vector_size = sizeof (u32),
205
206 .n_errors = PUNT_N_ERROR,
207 .error_strings = punt_error_strings,
208
209 .n_next_nodes = PUNT_N_NEXT,
210 .next_nodes = {
211#define _(s,n) [PUNT_NEXT_##s] = n,
212 foreach_punt_next
213#undef _
214 },
215};
216
Dave Barachd7cb1b52016-12-09 09:52:16 -0500217VLIB_NODE_FUNCTION_MULTIARCH (udp4_punt_node, udp4_punt);
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700218
219VLIB_REGISTER_NODE (udp6_punt_node) = {
220 .function = udp6_punt,
221 .name = "ip6-udp-punt",
222 /* Takes a vector of packets. */
223 .vector_size = sizeof (u32),
224
225 .n_errors = PUNT_N_ERROR,
226 .error_strings = punt_error_strings,
227
228 .n_next_nodes = PUNT_N_NEXT,
229 .next_nodes = {
230#define _(s,n) [PUNT_NEXT_##s] = n,
231 foreach_punt_next
232#undef _
233 },
234};
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700235
Dave Barachd7cb1b52016-12-09 09:52:16 -0500236VLIB_NODE_FUNCTION_MULTIARCH (udp6_punt_node, udp6_punt);;
237
Ole Troanf7a55ad2017-05-16 14:59:29 +0200238/* *INDENT-ON* */
239
Klement Sekera3c37ad52017-11-03 10:25:23 +0100240static punt_client_t *
241punt_client_get (bool is_ip4, u16 port)
Ole Troanf7a55ad2017-05-16 14:59:29 +0200242{
243 punt_main_t *pm = &punt_main;
Klement Sekera3c37ad52017-11-03 10:25:23 +0100244 punt_client_t *v =
245 is_ip4 ? pm->clients_by_dst_port4 : pm->clients_by_dst_port6;
Ole Troanf7a55ad2017-05-16 14:59:29 +0200246
247 u16 i = sparse_vec_index (v, port);
248 if (i == SPARSE_VEC_INVALID_INDEX)
249 return 0;
250
Klement Sekera3c37ad52017-11-03 10:25:23 +0100251 return &vec_elt (v, i);
252}
253
254static struct sockaddr_un *
255punt_socket_get (bool is_ip4, u16 port)
256{
257 punt_client_t *v = punt_client_get (is_ip4, port);
258 if (v)
259 return &v->caddr;
260
261 return NULL;
Ole Troanf7a55ad2017-05-16 14:59:29 +0200262}
263
264static void
265punt_socket_register (bool is_ip4, u8 protocol, u16 port,
266 char *client_pathname)
267{
268 punt_main_t *pm = &punt_main;
269 punt_client_t c, *n;
270 punt_client_t *v = is_ip4 ? pm->clients_by_dst_port4 :
271 pm->clients_by_dst_port6;
272
273 memset (&c, 0, sizeof (c));
274 memcpy (c.caddr.sun_path, client_pathname, sizeof (c.caddr.sun_path));
275 c.caddr.sun_family = AF_UNIX;
276 c.port = port;
277 n = sparse_vec_validate (v, port);
278 n[0] = c;
279}
280
281/* $$$$ Just leaves the mapping in place for now */
282static void
283punt_socket_unregister (bool is_ip4, u8 protocol, u16 port)
284{
285 return;
286}
287
Klement Sekera3c37ad52017-11-03 10:25:23 +0100288typedef struct
289{
290 punt_client_t client;
291 u8 is_midchain;
292} udp_punt_trace_t;
293
294u8 *
295format_udp_punt_trace (u8 * s, va_list * args)
296{
297 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
298 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
299 udp_punt_trace_t *t = va_arg (*args, udp_punt_trace_t *);
300 u32 indent = format_get_indent (s);
301 s = format (s, "to: %s", t->client.caddr.sun_path);
302 if (t->is_midchain)
303 {
304 s = format (s, "\n%U(buffer is part of chain)", format_white_space,
305 indent);
306 }
307 return s;
308}
309
Ole Troanf7a55ad2017-05-16 14:59:29 +0200310always_inline uword
311udp46_punt_socket_inline (vlib_main_t * vm,
312 vlib_node_runtime_t * node,
313 vlib_frame_t * frame, bool is_ip4)
314{
315 u32 *buffers = vlib_frame_args (frame);
316 uword n_packets = frame->n_vectors;
317 struct iovec *iovecs = 0;
318 punt_main_t *pm = &punt_main;
319 int i;
320
321 u32 node_index = is_ip4 ? udp4_punt_socket_node.index :
322 udp6_punt_socket_node.index;
323
324 for (i = 0; i < n_packets; i++)
325 {
326 struct iovec *iov;
327 vlib_buffer_t *b;
328 uword l;
329 punt_packetdesc_t packetdesc;
330
331 b = vlib_get_buffer (vm, buffers[i]);
332
333 /* Reverse UDP Punt advance */
334 udp_header_t *udp;
335 if (is_ip4)
336 {
337 vlib_buffer_advance (b, -(sizeof (ip4_header_t) +
338 sizeof (udp_header_t)));
339 ip4_header_t *ip = vlib_buffer_get_current (b);
340 udp = (udp_header_t *) (ip + 1);
341 }
342 else
343 {
344 vlib_buffer_advance (b, -(sizeof (ip6_header_t) +
345 sizeof (udp_header_t)));
346 ip6_header_t *ip = vlib_buffer_get_current (b);
347 udp = (udp_header_t *) (ip + 1);
348 }
349
350 u16 port = clib_net_to_host_u16 (udp->dst_port);
351
352 /*
353 * Find registerered client
354 * If no registered client, drop packet and count
355 */
356 struct sockaddr_un *caddr;
357 caddr = punt_socket_get (is_ip4, port);
358 if (!caddr)
359 {
360 vlib_node_increment_counter (vm, node_index,
361 PUNT_ERROR_SOCKET_TX_ERROR, 1);
362 goto error;
363 }
364
Klement Sekera3c37ad52017-11-03 10:25:23 +0100365 punt_client_t *c = NULL;
366 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
367 {
Klement Sekerab7163082017-11-08 04:13:49 +0100368 c = punt_client_get (is_ip4, port);
Klement Sekera3c37ad52017-11-03 10:25:23 +0100369 udp_punt_trace_t *t;
370 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
371 clib_memcpy (&t->client, c, sizeof (t->client));
372 }
373
Ole Troanf7a55ad2017-05-16 14:59:29 +0200374 /* Re-set iovecs if present. */
375 if (iovecs)
376 _vec_len (iovecs) = 0;
377
378 /* Add packet descriptor */
379 packetdesc.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
380 packetdesc.action = 0;
381 vec_add2 (iovecs, iov, 1);
382 iov->iov_base = &packetdesc;
383 iov->iov_len = sizeof (packetdesc);
384
385 /** VLIB buffer chain -> Unix iovec(s). */
386 vlib_buffer_advance (b, -(sizeof (ethernet_header_t)));
387 vec_add2 (iovecs, iov, 1);
388 iov->iov_base = b->data + b->current_data;
389 iov->iov_len = l = b->current_length;
390
391 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
392 {
393 do
394 {
395 b = vlib_get_buffer (vm, b->next_buffer);
Klement Sekera3c37ad52017-11-03 10:25:23 +0100396 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
397 {
Klement Sekerab7163082017-11-08 04:13:49 +0100398 if (PREDICT_FALSE (!c))
399 {
400 c = punt_client_get (is_ip4, port);
401 }
Klement Sekera3c37ad52017-11-03 10:25:23 +0100402 udp_punt_trace_t *t;
403 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
404 clib_memcpy (&t->client, c, sizeof (t->client));
405 t->is_midchain = 1;
406 }
Ole Troanf7a55ad2017-05-16 14:59:29 +0200407
408 vec_add2 (iovecs, iov, 1);
409
410 iov->iov_base = b->data + b->current_data;
411 iov->iov_len = b->current_length;
412 l += b->current_length;
413 }
414 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
415 }
416
417 struct msghdr msg = {
418 .msg_name = caddr,
419 .msg_namelen = sizeof (*caddr),
420 .msg_iov = iovecs,
421 .msg_iovlen = vec_len (iovecs),
422 };
423
Klement Sekera6bd0bc32017-11-08 12:18:21 +0100424 if (sendmsg (pm->socket_fd, &msg, 0) < (ssize_t) l)
Ole Troanf7a55ad2017-05-16 14:59:29 +0200425 vlib_node_increment_counter (vm, node_index,
426 PUNT_ERROR_SOCKET_TX_ERROR, 1);
427 }
428
429error:
Klement Sekera0dc11a12017-11-03 06:39:28 +0100430 vlib_buffer_free (vm, buffers, n_packets);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200431
432 return n_packets;
433}
434
435static uword
436udp4_punt_socket (vlib_main_t * vm,
437 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
438{
439 return udp46_punt_socket_inline (vm, node, from_frame, true /* is_ip4 */ );
440}
441
442static uword
443udp6_punt_socket (vlib_main_t * vm,
444 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
445{
446 return udp46_punt_socket_inline (vm, node, from_frame, false /* is_ip4 */ );
447}
448
449
450/* *INDENT-OFF* */
451VLIB_REGISTER_NODE (udp4_punt_socket_node) = {
452 .function = udp4_punt_socket,
453 .name = "ip4-udp-punt-socket",
Klement Sekera3c37ad52017-11-03 10:25:23 +0100454 .format_trace = format_udp_punt_trace,
Ole Troanf7a55ad2017-05-16 14:59:29 +0200455 .flags = VLIB_NODE_FLAG_IS_DROP,
456 /* Takes a vector of packets. */
457 .vector_size = sizeof (u32),
458 .n_errors = PUNT_N_ERROR,
459 .error_strings = punt_error_strings,
460};
461VLIB_REGISTER_NODE (udp6_punt_socket_node) = {
462 .function = udp6_punt_socket,
463 .name = "ip6-udp-punt-socket",
Klement Sekera3c37ad52017-11-03 10:25:23 +0100464 .format_trace = format_udp_punt_trace,
Ole Troanf7a55ad2017-05-16 14:59:29 +0200465 .flags = VLIB_NODE_FLAG_IS_DROP,
466 .vector_size = sizeof (u32),
467 .n_errors = PUNT_N_ERROR,
468 .error_strings = punt_error_strings,
469};
470/* *INDENT-ON* */
471
472typedef struct
473{
474 enum punt_action_e action;
475 u32 sw_if_index;
476} punt_trace_t;
477
478static u8 *
479format_punt_trace (u8 * s, va_list * va)
480{
481 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
482 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
483 vnet_main_t *vnm = vnet_get_main ();
484 punt_trace_t *t = va_arg (*va, punt_trace_t *);
485 s = format (s, "%U Action: %d", format_vnet_sw_if_index_name,
486 vnm, t->sw_if_index, t->action);
487 return s;
488}
489
490static uword
491punt_socket_rx_fd (vlib_main_t * vm, vlib_node_runtime_t * node, u32 fd)
492{
493 const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
494 u32 n_trace = vlib_get_trace_count (vm, node);
495 u32 next = node->cached_next_index;
496 u32 n_left_to_next, next_index;
497 u32 *to_next;
498 u32 error = PUNT_ERROR_NONE;
499 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
500
501 /* $$$$ Only dealing with one buffer at the time for now */
502
503 u32 bi;
504 vlib_buffer_t *b;
505 punt_packetdesc_t packetdesc;
506 ssize_t size;
507 struct iovec io[2];
508
509 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
510 {
511 error = PUNT_ERROR_NOBUFFER;
512 goto error;
513 }
514
515 b = vlib_get_buffer (vm, bi);
516 io[0].iov_base = &packetdesc;
517 io[0].iov_len = sizeof (packetdesc);
518 io[1].iov_base = b->data;
519 io[1].iov_len = buffer_size;
520
521 size = readv (fd, io, 2);
522 /* We need at least the packet descriptor plus a header */
523 if (size <= (int) (sizeof (packetdesc) + sizeof (ip4_header_t)))
524 {
525 vlib_buffer_free (vm, &bi, 1);
526 error = PUNT_ERROR_READV;
527 goto error;
528 }
529
530 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
531 b->current_length = size - sizeof (packetdesc);
532
533 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
534
535 switch (packetdesc.action)
536 {
537 case PUNT_L2:
538 vnet_buffer (b)->sw_if_index[VLIB_TX] = packetdesc.sw_if_index;
539 next_index = PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT;
540 break;
541
542 case PUNT_IP4_ROUTED:
543 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
544 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
545 next_index = PUNT_SOCKET_RX_NEXT_IP4_LOOKUP;
546 break;
547
548 case PUNT_IP6_ROUTED:
549 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
550 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
551 next_index = PUNT_SOCKET_RX_NEXT_IP6_LOOKUP;
552 break;
553
554 default:
555 error = PUNT_ERROR_ACTION;
556 vlib_buffer_free (vm, &bi, 1);
557 goto error;
558 }
559
560 if (PREDICT_FALSE (n_trace > 0))
561 {
562 punt_trace_t *t;
563 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
564 vlib_set_trace_count (vm, node, --n_trace);
565 t = vlib_add_trace (vm, node, b, sizeof (*t));
566 t->sw_if_index = packetdesc.sw_if_index;
567 t->action = packetdesc.action;
568 }
569
570 to_next[0] = bi;
571 to_next++;
572 n_left_to_next--;
573
574 vlib_validate_buffer_enqueue_x1 (vm, node, next, to_next, n_left_to_next,
575 bi, next_index);
576 vlib_put_next_frame (vm, node, next, n_left_to_next);
577 return 1;
578
579error:
580 vlib_node_increment_counter (vm, punt_socket_rx_node.index, error, 1);
581 return 0;
582}
583
584static uword
585punt_socket_rx (vlib_main_t * vm,
586 vlib_node_runtime_t * node, vlib_frame_t * frame)
587{
588 punt_main_t *pm = &punt_main;
589 u32 total_count = 0;
590 int i;
591
592 for (i = 0; i < vec_len (pm->ready_fds); i++)
593 {
594 total_count += punt_socket_rx_fd (vm, node, pm->ready_fds[i]);
595 vec_del1 (pm->ready_fds, i);
596 }
597 return total_count;
598}
599
600VLIB_REGISTER_NODE (punt_socket_rx_node, static) =
601{
602 .function = punt_socket_rx,.name = "punt-socket-rx",.type =
603 VLIB_NODE_TYPE_INPUT,.state = VLIB_NODE_STATE_INTERRUPT,.vector_size =
604 1,.n_errors = PUNT_N_ERROR,.error_strings =
605 punt_error_strings,.n_next_nodes = PUNT_SOCKET_RX_N_NEXT,.next_nodes =
606 {
607[PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT] = "interface-output",
608 [PUNT_SOCKET_RX_NEXT_IP4_LOOKUP] = "ip4-lookup",
609 [PUNT_SOCKET_RX_NEXT_IP6_LOOKUP] = "ip6-lookup",},.format_trace =
610 format_punt_trace,};
611
612static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200613punt_socket_read_ready (clib_file_t * uf)
Ole Troanf7a55ad2017-05-16 14:59:29 +0200614{
615 vlib_main_t *vm = vlib_get_main ();
616 punt_main_t *pm = &punt_main;
617
618 /** Schedule the rx node */
619 vlib_node_set_interrupt_pending (vm, punt_socket_rx_node.index);
620 vec_add1 (pm->ready_fds, uf->file_descriptor);
621
622 return 0;
623}
624
625clib_error_t *
626vnet_punt_socket_add (vlib_main_t * vm, u32 header_version,
627 bool is_ip4, u8 protocol, u16 port,
628 char *client_pathname)
629{
630 punt_main_t *pm = &punt_main;
631
632 if (!pm->is_configured)
633 return clib_error_return (0, "socket is not configured");
634
635 if (header_version != PUNT_PACKETDESC_VERSION)
636 return clib_error_return (0, "Invalid packet descriptor version");
637
638 /* For now we only support UDP punt */
639 if (protocol != IP_PROTOCOL_UDP)
640 return clib_error_return (0,
641 "only UDP protocol (%d) is supported, got %d",
642 IP_PROTOCOL_UDP, protocol);
643
644 if (port == (u16) ~ 0)
645 return clib_error_return (0, "UDP port number required");
646
647 /* Register client */
648 punt_socket_register (is_ip4, protocol, port, client_pathname);
649
650 u32 node_index = is_ip4 ? udp4_punt_socket_node.index :
651 udp6_punt_socket_node.index;
652
653 udp_register_dst_port (vm, port, node_index, is_ip4);
654
655 return 0;
656}
657
658clib_error_t *
659vnet_punt_socket_del (vlib_main_t * vm, bool is_ip4, u8 l4_protocol, u16 port)
660{
661 punt_main_t *pm = &punt_main;
662
663 if (!pm->is_configured)
664 return clib_error_return (0, "socket is not configured");
665
666 punt_socket_unregister (is_ip4, l4_protocol, port);
667 udp_unregister_dst_port (vm, port, is_ip4);
668
669 return 0;
670}
671
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700672/**
673 * @brief Request IP traffic punt to the local TCP/IP stack.
674 *
675 * @em Note
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200676 * - UDP and TCP are the only protocols supported in the current implementation
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700677 *
678 * @param vm vlib_main_t corresponding to the current thread
679 * @param ipv IP protcol version.
680 * 4 - IPv4, 6 - IPv6, ~0 for both IPv6 and IPv4
681 * @param protocol 8-bits L4 protocol value
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200682 * UDP is 17
683 * TCP is 1
684 * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700685 *
686 * @returns 0 on success, non-zero value otherwise
687 */
688clib_error_t *
689vnet_punt_add_del (vlib_main_t * vm, u8 ipv, u8 protocol, u16 port,
Ole Troanf7a55ad2017-05-16 14:59:29 +0200690 bool is_add)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700691{
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200692
Marco Varlese191a5942017-10-30 18:17:21 +0100693 /* For now we only support TCP, UDP and SCTP punt */
694 if (protocol != IP_PROTOCOL_UDP &&
695 protocol != IP_PROTOCOL_TCP && protocol != IP_PROTOCOL_SCTP)
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800696 return clib_error_return (0,
Marco Varlese191a5942017-10-30 18:17:21 +0100697 "only UDP (%d), TCP (%d) and SCTP (%d) protocols are supported, got %d",
698 IP_PROTOCOL_UDP, IP_PROTOCOL_TCP,
699 IP_PROTOCOL_SCTP, protocol);
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700700
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800701 if (ipv != (u8) ~ 0 && ipv != 4 && ipv != 6)
702 return clib_error_return (0, "IP version must be 4 or 6, got %d", ipv);
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700703
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800704 if (port == (u16) ~ 0)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700705 {
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200706 if ((ipv == 4) || (ipv == (u8) ~ 0))
707 {
708 if (protocol == IP_PROTOCOL_UDP)
709 udp_punt_unknown (vm, 1, is_add);
710 else if (protocol == IP_PROTOCOL_TCP)
711 tcp_punt_unknown (vm, 1, is_add);
Marco Varlese191a5942017-10-30 18:17:21 +0100712 else if (protocol == IP_PROTOCOL_SCTP)
713 sctp_punt_unknown (vm, 1, is_add);
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200714 }
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800715
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200716 if ((ipv == 6) || (ipv == (u8) ~ 0))
717 {
718 if (protocol == IP_PROTOCOL_UDP)
719 udp_punt_unknown (vm, 0, is_add);
720 else if (protocol == IP_PROTOCOL_TCP)
721 tcp_punt_unknown (vm, 0, is_add);
Marco Varlese191a5942017-10-30 18:17:21 +0100722 else if (protocol == IP_PROTOCOL_SCTP)
723 sctp_punt_unknown (vm, 0, is_add);
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200724 }
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800725
726 return 0;
727 }
728
729 else if (is_add)
730 {
Marco Varlese191a5942017-10-30 18:17:21 +0100731 if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP)
732 return clib_error_return (0,
733 "punt TCP/SCTP ports is not supported yet");
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200734
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800735 if (ipv == 4 || ipv == (u8) ~ 0)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700736 udp_register_dst_port (vm, port, udp4_punt_node.index, 1);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800737
738 if (ipv == 6 || ipv == (u8) ~ 0)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700739 udp_register_dst_port (vm, port, udp6_punt_node.index, 0);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800740
741 return 0;
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700742 }
743 else
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800744 return clib_error_return (0, "punt delete is not supported yet");
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700745}
746
747static clib_error_t *
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200748punt_cli (vlib_main_t * vm,
749 unformat_input_t * input, vlib_cli_command_t * cmd)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700750{
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200751 u32 port;
Ole Troanf7a55ad2017-05-16 14:59:29 +0200752 bool is_add = true;
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200753 u32 protocol = ~0;
Swarup Nayak1b708842017-12-13 13:27:23 +0530754 clib_error_t *error = NULL;
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700755
756 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
757 {
758 if (unformat (input, "del"))
Ole Troanf7a55ad2017-05-16 14:59:29 +0200759 is_add = false;
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200760 else if (unformat (input, "all"))
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800761 {
762 /* punt both IPv6 and IPv4 when used in CLI */
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200763 error = vnet_punt_add_del (vm, ~0, protocol, ~0, is_add);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800764 if (error)
Swarup Nayak1b708842017-12-13 13:27:23 +0530765 {
766 clib_error_report (error);
767 goto done;
768 }
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800769 }
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200770 else if (unformat (input, "%d", &port))
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700771 {
772 /* punt both IPv6 and IPv4 when used in CLI */
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200773 error = vnet_punt_add_del (vm, ~0, protocol, port, is_add);
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700774 if (error)
Swarup Nayak1b708842017-12-13 13:27:23 +0530775 {
776 clib_error_report (error);
777 goto done;
778 }
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700779 }
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200780 else if (unformat (input, "udp"))
781 protocol = IP_PROTOCOL_UDP;
782 else if (unformat (input, "tcp"))
783 protocol = IP_PROTOCOL_TCP;
Swarup Nayak1b708842017-12-13 13:27:23 +0530784 else
785 {
786 error = clib_error_return (0, "parse error: '%U'",
787 format_unformat_error, input);
788 goto done;
789 }
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700790 }
Swarup Nayak1b708842017-12-13 13:27:23 +0530791done:
792 return error;
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700793}
794
795/*?
796 * The set of '<em>set punt</em>' commands allows specific IP traffic to
797 * be punted to the host TCP/IP stack
798 *
799 * @em Note
800 * - UDP is the only protocol supported in the current implementation
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700801 * - All TCP traffic is currently punted to the host by default
802 *
803 * @cliexpar
804 * @parblock
805 * Example of how to request NTP traffic to be punted
806 * @cliexcmd{set punt udp 125}
807 *
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800808 * Example of how to request all 'unknown' UDP traffic to be punted
809 * @cliexcmd{set punt udp all}
810 *
811 * Example of how to stop all 'unknown' UDP traffic to be punted
812 * @cliexcmd{set punt udp del all}
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700813 * @endparblock
814?*/
815/* *INDENT-OFF* */
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200816VLIB_CLI_COMMAND (punt_command, static) = {
817 .path = "set punt",
818 .short_help = "set punt [udp|tcp] [del] <all | port-num1 [port-num2 ...]>",
819 .function = punt_cli,
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700820};
821/* *INDENT-ON* */
822
Ole Troanf7a55ad2017-05-16 14:59:29 +0200823clib_error_t *
824punt_init (vlib_main_t * vm)
825{
826 punt_main_t *pm = &punt_main;
827
828 pm->clients_by_dst_port6 = sparse_vec_new
829 (sizeof (pm->clients_by_dst_port6[0]),
830 BITS (((udp_header_t *) 0)->dst_port));
831 pm->clients_by_dst_port4 = sparse_vec_new
832 (sizeof (pm->clients_by_dst_port4[0]),
833 BITS (((udp_header_t *) 0)->dst_port));
834
835 pm->is_configured = false;
836 pm->interface_output_node = vlib_get_node_by_name (vm,
837 (u8 *)
838 "interface-output");
839 return 0;
840}
841
842VLIB_INIT_FUNCTION (punt_init);
843
844static clib_error_t *
845punt_config (vlib_main_t * vm, unformat_input_t * input)
846{
847 punt_main_t *pm = &punt_main;
848 char *socket_path = 0;
849
850 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
851 {
852 if (unformat (input, "socket %s", &socket_path))
853 strncpy (pm->sun_path, socket_path, 108 - 1);
854 else
855 return clib_error_return (0, "unknown input `%U'",
856 format_unformat_error, input);
857 }
858
859 if (socket_path == 0)
860 return 0;
861
862 /* UNIX domain socket */
863 struct sockaddr_un addr;
864 if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
865 {
866 return clib_error_return (0, "socket error");
867 }
868
869 memset (&addr, 0, sizeof (addr));
870 addr.sun_family = AF_UNIX;
871 if (*socket_path == '\0')
872 {
873 *addr.sun_path = '\0';
874 strncpy (addr.sun_path + 1, socket_path + 1,
875 sizeof (addr.sun_path) - 2);
876 }
877 else
878 {
879 strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
880 unlink (socket_path);
881 }
882
883 if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
884 {
885 return clib_error_return (0, "bind error");
886 }
887
888 /* Register socket */
Damjan Marion56dd5432017-09-08 19:52:02 +0200889 clib_file_main_t *fm = &file_main;
890 clib_file_t template = { 0 };
Ole Troanf7a55ad2017-05-16 14:59:29 +0200891 template.read_function = punt_socket_read_ready;
892 template.file_descriptor = pm->socket_fd;
Damjan Marion56dd5432017-09-08 19:52:02 +0200893 pm->clib_file_index = clib_file_add (fm, &template);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200894
895 pm->is_configured = true;
896
897 return 0;
898}
899
900VLIB_CONFIG_FUNCTION (punt_config, "punt");
901
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700902/*
903 * fd.io coding-style-patch-verification: ON
904 *
905 * Local Variables:
906 * eval: (c-set-style "gnu")
907 * End:
908 */