blob: 32a0884d86b86f811c17ab6942a2117c9a91460e [file] [log] [blame]
Neale Ranns50f0ac02019-05-15 02:13:37 -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 *
20 * Provides a set of VPP nodes together with the relevant APIs and CLI
21 * commands in order to adjust and dispatch packets from the VPP data plane
22 * to the local TCP/IP stack
23 */
24
25#include <vnet/ip/ip.h>
26#include <vlib/vlib.h>
27#include <vnet/pg/pg.h>
28#include <vnet/udp/udp.h>
29#include <vnet/tcp/tcp.h>
Neale Ranns50f0ac02019-05-15 02:13:37 -070030#include <vnet/ip/punt.h>
31#include <vlib/unix/unix.h>
32
33#include <stdio.h>
34#include <unistd.h>
35#include <sys/socket.h>
36#include <sys/uio.h>
37#include <stdlib.h>
38
39typedef enum
40{
41#define punt_error(n,s) PUNT_ERROR_##n,
42#include <vnet/ip/punt_error.def>
43#undef punt_error
44 PUNT_N_ERROR,
45} punt_error_t;
46
47#define foreach_punt_next \
48 _ (PUNT4, "ip4-punt") \
49 _ (PUNT6, "ip6-punt")
50
51typedef enum
52{
53#define _(s,n) PUNT_NEXT_##s,
54 foreach_punt_next
55#undef _
56 PUNT_N_NEXT,
57} punt_next_t;
58
59enum punt_socket_rx_next_e
60{
61 PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT,
62 PUNT_SOCKET_RX_NEXT_IP4_LOOKUP,
63 PUNT_SOCKET_RX_NEXT_IP6_LOOKUP,
64 PUNT_SOCKET_RX_N_NEXT
65};
66
67#define punt_next_punt(is_ip4) (is_ip4 ? PUNT_NEXT_PUNT4 : PUNT_NEXT_PUNT6)
68
69/** @brief IPv4/IPv6 UDP punt node main loop.
70
71 This is the main loop inline function for IPv4/IPv6 UDP punt
72 transition node.
73
74 @param vm vlib_main_t corresponding to the current thread
75 @param node vlib_node_runtime_t
76 @param frame vlib_frame_t whose contents should be dispatched
77 @param is_ipv4 indicates if called for IPv4 or IPv6 node
78*/
79always_inline uword
80udp46_punt_inline (vlib_main_t * vm,
81 vlib_node_runtime_t * node,
82 vlib_frame_t * from_frame, int is_ip4)
83{
84 u32 n_left_from, *from, *to_next;
85 word advance;
86
87 from = vlib_frame_vector_args (from_frame);
88 n_left_from = from_frame->n_vectors;
89
90 /* udp[46]_lookup hands us the data payload, not the IP header */
91 if (is_ip4)
92 advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t));
93 else
94 advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t));
95
96 while (n_left_from > 0)
97 {
98 u32 n_left_to_next;
99
100 vlib_get_next_frame (vm, node, punt_next_punt (is_ip4), to_next,
101 n_left_to_next);
102
103 while (n_left_from > 0 && n_left_to_next > 0)
104 {
105 u32 bi0;
106 vlib_buffer_t *b0;
107
108 bi0 = from[0];
109 to_next[0] = bi0;
110 from += 1;
111 to_next += 1;
112 n_left_from -= 1;
113 n_left_to_next -= 1;
114
115 b0 = vlib_get_buffer (vm, bi0);
116 vlib_buffer_advance (b0, advance);
117 b0->error = node->errors[PUNT_ERROR_UDP_PORT];
118 }
119
120 vlib_put_next_frame (vm, node, punt_next_punt (is_ip4), n_left_to_next);
121 }
122
123 return from_frame->n_vectors;
124}
125
126static char *punt_error_strings[] = {
127#define punt_error(n,s) s,
128#include "punt_error.def"
129#undef punt_error
130};
131
132/** @brief IPv4 UDP punt node.
133 @node ip4-udp-punt
134
135 This is the IPv4 UDP punt transition node. It is registered as a next
136 node for the "ip4-udp-lookup" handling UDP port(s) requested for punt.
137 The buffer's current data pointer is adjusted to the original packet
138 IPv4 header. All buffers are dispatched to "error-punt".
139
140 @param vm vlib_main_t corresponding to the current thread
141 @param node vlib_node_runtime_t
142 @param frame vlib_frame_t whose contents should be dispatched
143
144 @par Graph mechanics: next index usage
145
146 @em Sets:
147 - <code>vnet_buffer(b)->current_data</code>
148 - <code>vnet_buffer(b)->current_len</code>
149
150 <em>Next Index:</em>
151 - Dispatches the packet to the "error-punt" node
152*/
153VLIB_NODE_FN (udp4_punt_node) (vlib_main_t * vm,
154 vlib_node_runtime_t * node,
155 vlib_frame_t * from_frame)
156{
157 return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ );
158}
159
160/** @brief IPv6 UDP punt node.
161 @node ip6-udp-punt
162
163 This is the IPv6 UDP punt transition node. It is registered as a next
164 node for the "ip6-udp-lookup" handling UDP port(s) requested for punt.
165 The buffer's current data pointer is adjusted to the original packet
166 IPv6 header. All buffers are dispatched to "error-punt".
167
168 @param vm vlib_main_t corresponding to the current thread
169 @param node vlib_node_runtime_t
170 @param frame vlib_frame_t whose contents should be dispatched
171
172 @par Graph mechanics: next index usage
173
174 @em Sets:
175 - <code>vnet_buffer(b)->current_data</code>
176 - <code>vnet_buffer(b)->current_len</code>
177
178 <em>Next Index:</em>
179 - Dispatches the packet to the "error-punt" node
180*/
181VLIB_NODE_FN (udp6_punt_node) (vlib_main_t * vm,
182 vlib_node_runtime_t * node,
183 vlib_frame_t * from_frame)
184{
185 return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ );
186}
187
188/* *INDENT-OFF* */
189VLIB_REGISTER_NODE (udp4_punt_node) = {
190 .name = "ip4-udp-punt",
191 /* Takes a vector of packets. */
192 .vector_size = sizeof (u32),
193
194 .n_errors = PUNT_N_ERROR,
195 .error_strings = punt_error_strings,
196
197 .n_next_nodes = PUNT_N_NEXT,
198 .next_nodes = {
199#define _(s,n) [PUNT_NEXT_##s] = n,
200 foreach_punt_next
201#undef _
202 },
203};
204
205VLIB_REGISTER_NODE (udp6_punt_node) = {
206 .name = "ip6-udp-punt",
207 /* Takes a vector of packets. */
208 .vector_size = sizeof (u32),
209
210 .n_errors = PUNT_N_ERROR,
211 .error_strings = punt_error_strings,
212
213 .n_next_nodes = PUNT_N_NEXT,
214 .next_nodes = {
215#define _(s,n) [PUNT_NEXT_##s] = n,
216 foreach_punt_next
217#undef _
218 },
219};
220/* *INDENT-ON* */
221
222typedef struct
223{
224 punt_client_t client;
225 u8 is_midchain;
226} udp_punt_trace_t;
227
228static u8 *
229format_udp_punt_trace (u8 * s, va_list * args)
230{
231 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
232 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
233 udp_punt_trace_t *t = va_arg (*args, udp_punt_trace_t *);
234 u32 indent = format_get_indent (s);
235 s = format (s, "to: %s", t->client.caddr.sun_path);
236 if (t->is_midchain)
237 {
238 s = format (s, "\n%U(buffer is part of chain)", format_white_space,
239 indent);
240 }
241 return s;
242}
243
244always_inline uword
245punt_socket_inline (vlib_main_t * vm,
246 vlib_node_runtime_t * node,
247 vlib_frame_t * frame,
248 punt_type_t pt, ip_address_family_t af)
249{
250 u32 *buffers = vlib_frame_vector_args (frame);
251 uword n_packets = frame->n_vectors;
252 struct iovec *iovecs = 0;
253 punt_main_t *pm = &punt_main;
254 int i;
255
256 u32 node_index = AF_IP4 == af ? udp4_punt_socket_node.index :
257 udp6_punt_socket_node.index;
258
259 for (i = 0; i < n_packets; i++)
260 {
261 struct iovec *iov;
262 vlib_buffer_t *b;
263 uword l;
264 punt_packetdesc_t packetdesc;
265 punt_client_t *c;
266
267 b = vlib_get_buffer (vm, buffers[i]);
268
269 if (PUNT_TYPE_L4 == pt)
270 {
271 /* Reverse UDP Punt advance */
272 udp_header_t *udp;
273 if (AF_IP4 == af)
274 {
275 vlib_buffer_advance (b, -(sizeof (ip4_header_t) +
276 sizeof (udp_header_t)));
277 ip4_header_t *ip = vlib_buffer_get_current (b);
278 udp = (udp_header_t *) (ip + 1);
279 }
280 else
281 {
282 vlib_buffer_advance (b, -(sizeof (ip6_header_t) +
283 sizeof (udp_header_t)));
284 ip6_header_t *ip = vlib_buffer_get_current (b);
285 udp = (udp_header_t *) (ip + 1);
286 }
287
Neale Ranns50f0ac02019-05-15 02:13:37 -0700288 /*
289 * Find registerered client
290 * If no registered client, drop packet and count
291 */
Neale Rannsb538dd82019-05-21 06:54:54 -0700292 c = punt_client_l4_get (af, clib_net_to_host_u16 (udp->dst_port));
293 }
294 else if (PUNT_TYPE_IP_PROTO == pt)
295 {
296 /* Reverse UDP Punt advance */
297 ip_protocol_t proto;
298
299 if (AF_IP4 == af)
300 {
301 ip4_header_t *ip = vlib_buffer_get_current (b);
302 proto = ip->protocol;
303 }
304 else
305 {
306 ip6_header_t *ip = vlib_buffer_get_current (b);
307 proto = ip->protocol;
308 }
309
310 c = punt_client_ip_proto_get (af, proto);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700311 }
312 else if (PUNT_TYPE_EXCEPTION == pt)
313 {
314 c = punt_client_exception_get (b->punt_reason);
315 }
316 else
317 c = NULL;
318
319 if (PREDICT_FALSE (NULL == c))
320 {
321 vlib_node_increment_counter (vm, node_index,
322 PUNT_ERROR_SOCKET_TX_ERROR, 1);
323 goto error;
324 }
325
326 struct sockaddr_un *caddr = &c->caddr;
327
328 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
329 {
330 udp_punt_trace_t *t;
331 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
332 clib_memcpy_fast (&t->client, c, sizeof (t->client));
333 }
334
335 /* Re-set iovecs if present. */
336 if (iovecs)
337 _vec_len (iovecs) = 0;
338
339 /* Add packet descriptor */
340 packetdesc.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
341 packetdesc.action = 0;
342 vec_add2 (iovecs, iov, 1);
343 iov->iov_base = &packetdesc;
344 iov->iov_len = sizeof (packetdesc);
345
346 /** VLIB buffer chain -> Unix iovec(s). */
347 vlib_buffer_advance (b, -(sizeof (ethernet_header_t)));
348 vec_add2 (iovecs, iov, 1);
349 iov->iov_base = b->data + b->current_data;
350 iov->iov_len = l = b->current_length;
351
352 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
353 {
354 do
355 {
356 b = vlib_get_buffer (vm, b->next_buffer);
357 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
358 {
359 udp_punt_trace_t *t;
360 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
361 clib_memcpy_fast (&t->client, c, sizeof (t->client));
362 t->is_midchain = 1;
363 }
364
365 vec_add2 (iovecs, iov, 1);
366
367 iov->iov_base = b->data + b->current_data;
368 iov->iov_len = b->current_length;
369 l += b->current_length;
370 }
371 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
372 }
373
374 struct msghdr msg = {
375 .msg_name = caddr,
376 .msg_namelen = sizeof (*caddr),
377 .msg_iov = iovecs,
378 .msg_iovlen = vec_len (iovecs),
379 };
380
381 if (sendmsg (pm->socket_fd, &msg, 0) < (ssize_t) l)
382 vlib_node_increment_counter (vm, node_index,
383 PUNT_ERROR_SOCKET_TX_ERROR, 1);
384 else
385 vlib_node_increment_counter (vm, node_index, PUNT_ERROR_SOCKET_TX, 1);
386 }
387
388error:
389 vlib_buffer_free (vm, buffers, n_packets);
390
391 return n_packets;
392}
393
394static uword
395udp4_punt_socket (vlib_main_t * vm,
396 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
397{
398 return punt_socket_inline (vm, node, from_frame, PUNT_TYPE_L4, AF_IP4);
399}
400
401static uword
402udp6_punt_socket (vlib_main_t * vm,
403 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
404{
405 return punt_socket_inline (vm, node, from_frame, PUNT_TYPE_L4, AF_IP6);
406}
407
408static uword
Neale Rannsb538dd82019-05-21 06:54:54 -0700409ip4_proto_punt_socket (vlib_main_t * vm,
410 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
411{
412 return punt_socket_inline (vm, node, from_frame,
413 PUNT_TYPE_IP_PROTO, AF_IP4);
414}
415
416static uword
417ip6_proto_punt_socket (vlib_main_t * vm,
418 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
419{
420 return punt_socket_inline (vm, node, from_frame,
421 PUNT_TYPE_IP_PROTO, AF_IP6);
422}
423
424static uword
Neale Ranns50f0ac02019-05-15 02:13:37 -0700425exception_punt_socket (vlib_main_t * vm,
426 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
427{
428 return punt_socket_inline (vm, node, from_frame,
429 PUNT_TYPE_EXCEPTION, AF_IP4);
430}
431
432
433/* *INDENT-OFF* */
434VLIB_REGISTER_NODE (udp4_punt_socket_node) = {
435 .function = udp4_punt_socket,
436 .name = "ip4-udp-punt-socket",
437 .format_trace = format_udp_punt_trace,
438 .flags = VLIB_NODE_FLAG_IS_DROP,
439 /* Takes a vector of packets. */
440 .vector_size = sizeof (u32),
441 .n_errors = PUNT_N_ERROR,
442 .error_strings = punt_error_strings,
443};
444VLIB_REGISTER_NODE (udp6_punt_socket_node) = {
445 .function = udp6_punt_socket,
446 .name = "ip6-udp-punt-socket",
447 .format_trace = format_udp_punt_trace,
448 .flags = VLIB_NODE_FLAG_IS_DROP,
449 .vector_size = sizeof (u32),
450 .n_errors = PUNT_N_ERROR,
451 .error_strings = punt_error_strings,
452};
Neale Rannsb538dd82019-05-21 06:54:54 -0700453VLIB_REGISTER_NODE (ip4_proto_punt_socket_node) = {
454 .function = ip4_proto_punt_socket,
455 .name = "ip4-proto-punt-socket",
456 .format_trace = format_udp_punt_trace,
457 .flags = VLIB_NODE_FLAG_IS_DROP,
458 /* Takes a vector of packets. */
459 .vector_size = sizeof (u32),
460 .n_errors = PUNT_N_ERROR,
461 .error_strings = punt_error_strings,
462};
463VLIB_REGISTER_NODE (ip6_proto_punt_socket_node) = {
464 .function = ip6_proto_punt_socket,
465 .name = "ip6-proto-punt-socket",
466 .format_trace = format_udp_punt_trace,
467 .flags = VLIB_NODE_FLAG_IS_DROP,
468 .vector_size = sizeof (u32),
469 .n_errors = PUNT_N_ERROR,
470 .error_strings = punt_error_strings,
471};
Neale Ranns50f0ac02019-05-15 02:13:37 -0700472VLIB_REGISTER_NODE (exception_punt_socket_node) = {
473 .function = exception_punt_socket,
474 .name = "exception-punt-socket",
475 .format_trace = format_udp_punt_trace,
476 .flags = VLIB_NODE_FLAG_IS_DROP,
477 .vector_size = sizeof (u32),
478 .n_errors = PUNT_N_ERROR,
479 .error_strings = punt_error_strings,
480};
481/* *INDENT-ON* */
482
483typedef struct
484{
485 enum punt_action_e action;
486 u32 sw_if_index;
487} punt_trace_t;
488
489static u8 *
490format_punt_trace (u8 * s, va_list * va)
491{
492 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
493 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
494 vnet_main_t *vnm = vnet_get_main ();
495 punt_trace_t *t = va_arg (*va, punt_trace_t *);
496 s = format (s, "%U Action: %d", format_vnet_sw_if_index_name,
497 vnm, t->sw_if_index, t->action);
498 return s;
499}
500
501static uword
502punt_socket_rx_fd (vlib_main_t * vm, vlib_node_runtime_t * node, u32 fd)
503{
504 const uword buffer_size = vlib_buffer_get_default_data_size (vm);
505 u32 n_trace = vlib_get_trace_count (vm, node);
506 u32 next = node->cached_next_index;
507 u32 n_left_to_next, next_index;
508 u32 *to_next;
509 u32 error = PUNT_ERROR_NONE;
510 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
511
512 /* $$$$ Only dealing with one buffer at the time for now */
513
514 u32 bi;
515 vlib_buffer_t *b;
516 punt_packetdesc_t packetdesc;
517 ssize_t size;
518 struct iovec io[2];
519
520 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
521 {
522 error = PUNT_ERROR_NOBUFFER;
523 goto error;
524 }
525
526 b = vlib_get_buffer (vm, bi);
527 io[0].iov_base = &packetdesc;
528 io[0].iov_len = sizeof (packetdesc);
529 io[1].iov_base = b->data;
530 io[1].iov_len = buffer_size;
531
532 size = readv (fd, io, 2);
533 /* We need at least the packet descriptor plus a header */
534 if (size <= (int) (sizeof (packetdesc) + sizeof (ip4_header_t)))
535 {
536 vlib_buffer_free (vm, &bi, 1);
537 error = PUNT_ERROR_READV;
538 goto error;
539 }
540
541 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
542 b->current_length = size - sizeof (packetdesc);
543
544 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
545
546 switch (packetdesc.action)
547 {
548 case PUNT_L2:
549 vnet_buffer (b)->sw_if_index[VLIB_TX] = packetdesc.sw_if_index;
550 next_index = PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT;
551 break;
552
553 case PUNT_IP4_ROUTED:
554 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
555 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
556 next_index = PUNT_SOCKET_RX_NEXT_IP4_LOOKUP;
557 break;
558
559 case PUNT_IP6_ROUTED:
560 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
561 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
562 next_index = PUNT_SOCKET_RX_NEXT_IP6_LOOKUP;
563 break;
564
565 default:
566 error = PUNT_ERROR_ACTION;
567 vlib_buffer_free (vm, &bi, 1);
568 goto error;
569 }
570
571 if (PREDICT_FALSE (n_trace > 0))
572 {
573 punt_trace_t *t;
574 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
575 vlib_set_trace_count (vm, node, --n_trace);
576 t = vlib_add_trace (vm, node, b, sizeof (*t));
577 t->sw_if_index = packetdesc.sw_if_index;
578 t->action = packetdesc.action;
579 }
580
581 to_next[0] = bi;
582 to_next++;
583 n_left_to_next--;
584
585 vlib_validate_buffer_enqueue_x1 (vm, node, next, to_next, n_left_to_next,
586 bi, next_index);
587 vlib_put_next_frame (vm, node, next, n_left_to_next);
588 return 1;
589
590error:
591 vlib_node_increment_counter (vm, punt_socket_rx_node.index, error, 1);
592 return 0;
593}
594
595static uword
596punt_socket_rx (vlib_main_t * vm,
597 vlib_node_runtime_t * node, vlib_frame_t * frame)
598{
599 punt_main_t *pm = &punt_main;
600 u32 total_count = 0;
601 int i;
602
603 for (i = 0; i < vec_len (pm->ready_fds); i++)
604 {
605 total_count += punt_socket_rx_fd (vm, node, pm->ready_fds[i]);
606 vec_del1 (pm->ready_fds, i);
607 }
608 return total_count;
609}
610
611/* *INDENT-OFF* */
612VLIB_REGISTER_NODE (punt_socket_rx_node) =
613{
614 .function = punt_socket_rx,
615 .name = "punt-socket-rx",
616 .type = VLIB_NODE_TYPE_INPUT,
617 .state = VLIB_NODE_STATE_INTERRUPT,
618 .vector_size = 1,
619 .n_errors = PUNT_N_ERROR,
620 .error_strings = punt_error_strings,
621 .n_next_nodes = PUNT_SOCKET_RX_N_NEXT,
622 .next_nodes = {
623 [PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT] = "interface-output",
624 [PUNT_SOCKET_RX_NEXT_IP4_LOOKUP] = "ip4-lookup",
625 [PUNT_SOCKET_RX_NEXT_IP6_LOOKUP] = "ip6-lookup",
626 },
627 .format_trace = format_punt_trace,
628};
629/* *INDENT-ON* */
630
631/*
632 * fd.io coding-style-patch-verification: ON
633 *
634 * Local Variables:
635 * eval: (c-set-style "gnu")
636 * End:
637 */