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