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