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