FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
punt.c
Go to the documentation of this file.
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/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 <vppinfra/sparse_vec.h>
33 #include <vlib/unix/unix.h>
34 
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
39 #include <sys/uio.h>
40 #include <stdlib.h>
41 #include <stdbool.h>
42 
43 #define foreach_punt_next \
44  _ (PUNT4, "ip4-punt") \
45  _ (PUNT6, "ip6-punt")
46 
47 typedef enum
48 {
49 #define _(s,n) PUNT_NEXT_##s,
51 #undef _
53 } punt_next_t;
54 
56 {
61 };
62 
63 #define punt_next_punt(is_ip4) (is_ip4 ? PUNT_NEXT_PUNT4 : PUNT_NEXT_PUNT6)
64 
70 
72 
73 char *
75 {
76  punt_main_t *pm = &punt_main;
77  return pm->sun_path;
78 }
79 
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 */
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 
111  vlib_get_next_frame (vm, node, punt_next_punt (is_ip4), to_next,
112  n_left_to_next);
113 
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 
131  vlib_put_next_frame (vm, node, punt_next_punt (is_ip4), n_left_to_next);
132  }
133 
134  return from_frame->n_vectors;
135 }
136 
137 static 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 */
164 static uword
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 */
192 static uword
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* */
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,
213 #undef _
214  },
215 };
216 
218 
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,
232 #undef _
233  },
234 };
235 
237 
238 /* *INDENT-ON* */
239 
240 static punt_client_t *
241 punt_client_get (bool is_ip4, u16 port)
242 {
243  punt_main_t *pm = &punt_main;
244  punt_client_t *v =
245  is_ip4 ? pm->clients_by_dst_port4 : pm->clients_by_dst_port6;
246 
247  u16 i = sparse_vec_index (v, port);
248  if (i == SPARSE_VEC_INVALID_INDEX)
249  return 0;
250 
251  return &vec_elt (v, i);
252 }
253 
254 static struct sockaddr_un *
255 punt_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;
262 }
263 
264 static void
265 punt_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 :
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 */
282 static void
283 punt_socket_unregister (bool is_ip4, u8 protocol, u16 port)
284 {
285  return;
286 }
287 
288 typedef struct
289 {
293 
294 u8 *
295 format_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 
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)));
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)));
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 
365  punt_client_t *c = NULL;
366  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
367  {
368  c = punt_client_get (is_ip4, port);
369  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 
374  /* 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);
396  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
397  {
398  if (PREDICT_FALSE (!c))
399  {
400  c = punt_client_get (is_ip4, port);
401  }
402  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  }
407 
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 
424  if (sendmsg (pm->socket_fd, &msg, 0) < (ssize_t) l)
425  vlib_node_increment_counter (vm, node_index,
426  PUNT_ERROR_SOCKET_TX_ERROR, 1);
427  }
428 
429 error:
430  vlib_buffer_free (vm, buffers, n_packets);
431 
432  return n_packets;
433 }
434 
435 static uword
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 
442 static uword
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* */
452  .function = udp4_punt_socket,
453  .name = "ip4-udp-punt-socket",
454  .format_trace = format_udp_punt_trace,
455  .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 };
462  .function = udp6_punt_socket,
463  .name = "ip6-udp-punt-socket",
464  .format_trace = format_udp_punt_trace,
465  .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 
472 typedef struct
473 {
474  enum punt_action_e action;
476 } punt_trace_t;
477 
478 static u8 *
479 format_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 
490 static uword
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 
534 
535  switch (packetdesc.action)
536  {
537  case PUNT_L2:
538  vnet_buffer (b)->sw_if_index[VLIB_TX] = packetdesc.sw_if_index;
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 
579 error:
580  vlib_node_increment_counter (vm, punt_socket_rx_node.index, error, 1);
581  return 0;
582 }
583 
584 static uword
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 
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 =
611 
612 static clib_error_t *
614 {
616  punt_main_t *pm = &punt_main;
617 
618  /** Schedule the rx node */
621 
622  return 0;
623 }
624 
625 clib_error_t *
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 
658 clib_error_t *
659 vnet_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 
672 /**
673  * @brief Request IP traffic punt to the local TCP/IP stack.
674  *
675  * @em Note
676  * - UDP and TCP are the only protocols supported in the current implementation
677  *
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
682  * UDP is 17
683  * TCP is 1
684  * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only)
685  *
686  * @returns 0 on success, non-zero value otherwise
687  */
688 clib_error_t *
689 vnet_punt_add_del (vlib_main_t * vm, u8 ipv, u8 protocol, u16 port,
690  bool is_add)
691 {
692 
693  /* 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)
696  return clib_error_return (0,
697  "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);
700 
701  if (ipv != (u8) ~ 0 && ipv != 4 && ipv != 6)
702  return clib_error_return (0, "IP version must be 4 or 6, got %d", ipv);
703 
704  if (port == (u16) ~ 0)
705  {
706  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);
712  else if (protocol == IP_PROTOCOL_SCTP)
713  sctp_punt_unknown (vm, 1, is_add);
714  }
715 
716  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);
722  else if (protocol == IP_PROTOCOL_SCTP)
723  sctp_punt_unknown (vm, 0, is_add);
724  }
725 
726  return 0;
727  }
728 
729  else if (is_add)
730  {
731  if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP)
732  return clib_error_return (0,
733  "punt TCP/SCTP ports is not supported yet");
734 
735  if (ipv == 4 || ipv == (u8) ~ 0)
736  udp_register_dst_port (vm, port, udp4_punt_node.index, 1);
737 
738  if (ipv == 6 || ipv == (u8) ~ 0)
739  udp_register_dst_port (vm, port, udp6_punt_node.index, 0);
740 
741  return 0;
742  }
743  else
744  return clib_error_return (0, "punt delete is not supported yet");
745 }
746 
747 static clib_error_t *
749  unformat_input_t * input, vlib_cli_command_t * cmd)
750 {
751  u32 port;
752  bool is_add = true;
753  u32 protocol = ~0;
754  clib_error_t *error = NULL;
755 
757  {
758  if (unformat (input, "del"))
759  is_add = false;
760  else if (unformat (input, "all"))
761  {
762  /* punt both IPv6 and IPv4 when used in CLI */
763  error = vnet_punt_add_del (vm, ~0, protocol, ~0, is_add);
764  if (error)
765  {
766  clib_error_report (error);
767  goto done;
768  }
769  }
770  else if (unformat (input, "%d", &port))
771  {
772  /* punt both IPv6 and IPv4 when used in CLI */
773  error = vnet_punt_add_del (vm, ~0, protocol, port, is_add);
774  if (error)
775  {
776  clib_error_report (error);
777  goto done;
778  }
779  }
780  else if (unformat (input, "udp"))
781  protocol = IP_PROTOCOL_UDP;
782  else if (unformat (input, "tcp"))
783  protocol = IP_PROTOCOL_TCP;
784  else
785  {
786  error = clib_error_return (0, "parse error: '%U'",
787  format_unformat_error, input);
788  goto done;
789  }
790  }
791 done:
792  return error;
793 }
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
801  * - 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  *
808  * 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}
813  * @endparblock
814 ?*/
815 /* *INDENT-OFF* */
816 VLIB_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,
820 };
821 /* *INDENT-ON* */
822 
823 clib_error_t *
825 {
826  punt_main_t *pm = &punt_main;
827 
829  (sizeof (pm->clients_by_dst_port6[0]),
830  BITS (((udp_header_t *) 0)->dst_port));
832  (sizeof (pm->clients_by_dst_port4[0]),
833  BITS (((udp_header_t *) 0)->dst_port));
834 
835  pm->is_configured = false;
837  (u8 *)
838  "interface-output");
839  return 0;
840 }
841 
843 
844 static clib_error_t *
846 {
847  punt_main_t *pm = &punt_main;
848  char *socket_path = 0;
849 
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 */
890  clib_file_t template = { 0 };
892  template.file_descriptor = pm->socket_fd;
893  template.description = format (0, "%s", socket_path);
894  pm->clib_file_index = clib_file_add (fm, &template);
895 
896  pm->is_configured = true;
897 
898  return 0;
899 }
900 
902 
903 /*
904  * fd.io coding-style-patch-verification: ON
905  *
906  * Local Variables:
907  * eval: (c-set-style "gnu")
908  * End:
909  */
static uword punt_socket_rx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: punt.c:585
vlib_node_t * interface_output_node
Definition: punt.h:79
static clib_error_t * punt_socket_read_ready(clib_file_t *uf)
Definition: punt.c:613
static clib_error_t * punt_cli(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: punt.c:748
#define CLIB_UNUSED(x)
Definition: clib.h:81
static struct sockaddr_un * punt_socket_get(bool is_ip4, u16 port)
Definition: punt.c:255
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:156
punt_next_t
Definition: punt.c:47
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:547
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
static uword udp6_punt_socket(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: punt.c:443
static uword udp4_punt(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
IPv4 UDP punt node.
Definition: punt.c:165
static void vlib_node_set_interrupt_pending(vlib_main_t *vm, u32 node_index)
Definition: node_funcs.h:196
static vlib_node_registration_t punt_socket_rx_node
(constructor) VLIB_REGISTER_NODE (punt_socket_rx_node)
Definition: punt.c:69
#define NULL
Definition: clib.h:57
u32 * ready_fds
Definition: punt.h:80
u32 clib_file_index
Definition: punt.h:77
u32 file_descriptor
Definition: file.h:54
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:562
int i
int socket_fd
Definition: punt.h:73
static uword udp46_punt_socket_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, bool is_ip4)
Definition: punt.c:311
static u32 format_get_indent(u8 *s)
Definition: format.h:72
punt_action_e
Definition: punt.h:44
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:472
static uword udp6_punt(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
IPv6 UDP punt node.
Definition: punt.c:193
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
enum punt_action_e action
Definition: punt.h:59
clib_file_function_t * read_function
Definition: file.h:67
u16 port
Definition: punt.h:67
#define fm
static void vlib_trace_buffer(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, vlib_buffer_t *b, int follow_chain)
Definition: trace_funcs.h:114
memset(h->entries, 0, sizeof(h->entries[0])*entries)
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:109
punt_main_t punt_main
Definition: punt.c:71
i64 word
Definition: types.h:111
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
#define always_inline
Definition: clib.h:94
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
#define sparse_vec_validate(v, i)
Definition: sparse_vec.h:227
vlib_node_registration_t udp4_punt_node
(constructor) VLIB_REGISTER_NODE (udp4_punt_node)
Definition: punt.c:65
#define clib_error_return(e, args...)
Definition: error.h:99
clib_file_main_t file_main
Definition: main.c:63
unsigned int u32
Definition: types.h:88
static u8 * format_punt_trace(u8 *s, va_list *va)
Definition: punt.c:479
punt_client_t * clients_by_dst_port6
Definition: punt.h:76
#define punt_next_punt(is_ip4)
Definition: punt.c:63
void sctp_punt_unknown(vlib_main_t *vm, u8 is_ip4, u8 is_add)
Definition: sctp.c:94
uword size
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
#define v
Definition: acl.c:496
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:205
#define PREDICT_FALSE(x)
Definition: clib.h:107
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:172
u8 is_midchain
Definition: punt.c:291
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:806
vlib_node_registration_t udp6_punt_socket_node
(constructor) VLIB_REGISTER_NODE (udp6_punt_socket_node)
Definition: punt.c:68
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:364
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:138
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1176
void udp_punt_unknown(vlib_main_t *vm, u8 is_ip4, u8 is_add)
Definition: udp_local.c:553
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:155
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
clib_error_t * vnet_punt_socket_add(vlib_main_t *vm, u32 header_version, bool is_ip4, u8 protocol, u16 port, char *client_pathname)
Definition: punt.c:626
svmdb_client_t * c
u16 n_vectors
Definition: node.h:401
static punt_client_t * punt_client_get(bool is_ip4, u16 port)
Definition: punt.c:241
vlib_main_t * vm
Definition: buffer.c:294
u32 sw_if_index
Definition: punt.h:58
void udp_unregister_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u8 is_ip4)
Definition: udp_local.c:530
#define IP_PROTOCOL_SCTP
Definition: sctp.h:343
static void punt_socket_register(bool is_ip4, u8 protocol, u16 port, char *client_pathname)
Definition: punt.c:265
#define clib_memcpy(a, b, c)
Definition: string.h:75
static uword udp4_punt_socket(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: punt.c:436
Definitions for punt infrastructure.
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:455
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
punt_socket_rx_next_e
Definition: punt.c:55
char * vnet_punt_get_server_pathname(void)
Definition: punt.c:74
#define VLIB_BUFFER_DATA_SIZE
Definition: buffer.h:51
enum punt_action_e action
Definition: punt.c:474
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
static uword sparse_vec_index(void *v, uword sparse_index)
Definition: sparse_vec.h:157
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:513
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
Definition: file.h:96
clib_error_t * vnet_punt_socket_del(vlib_main_t *vm, bool is_ip4, u8 l4_protocol, u16 port)
Definition: punt.c:659
VLIB_NODE_FUNCTION_MULTIARCH(udp4_punt_node, udp4_punt)
char sun_path[sizeof(struct sockaddr_un)]
Definition: punt.h:74
clib_error_t * punt_init(vlib_main_t *vm)
Definition: punt.c:824
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:129
#define clib_error_report(e)
Definition: error.h:113
static void * vlib_frame_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:284
punt_client_t * clients_by_dst_port4
Definition: punt.h:75
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:218
static uword udp46_punt_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, int is_ip4)
IPv4/IPv6 UDP punt node main loop.
Definition: punt.c:91
static char * punt_error_strings[]
Definition: punt.c:137
static uword punt_socket_rx_fd(vlib_main_t *vm, vlib_node_runtime_t *node, u32 fd)
Definition: punt.c:491
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:57
vlib_node_registration_t udp4_punt_socket_node
(constructor) VLIB_REGISTER_NODE (udp4_punt_socket_node)
Definition: punt.c:67
#define vec_elt(v, i)
Get vector value at index i.
struct _vlib_node_registration vlib_node_registration_t
void tcp_punt_unknown(vlib_main_t *vm, u8 is_ip4, u8 is_add)
Definition: tcp.c:1362
Definition: defs.h:47
punt_client_t client
Definition: punt.c:290
#define foreach_punt_next
Definition: punt.c:43
u8 * format_udp_punt_trace(u8 *s, va_list *args)
Definition: punt.c:295
bool is_configured
Definition: punt.h:78
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:547
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
static void punt_socket_unregister(bool is_ip4, u8 protocol, u16 port)
Definition: punt.c:283
#define VLIB_NODE_FLAG_IS_DROP
Definition: node.h:305
clib_error_t * vnet_punt_add_del(vlib_main_t *vm, u8 ipv, u8 protocol, u16 port, bool is_add)
Request IP traffic punt to the local TCP/IP stack.
Definition: punt.c:689
#define vnet_buffer(b)
Definition: buffer.h:344
u32 sw_if_index
Definition: punt.c:475
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
u8 data[0]
Packet data.
Definition: buffer.h:175
Definition: file.h:51
void udp_register_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u32 node_index, u8 is_ip4)
Definition: udp_local.c:492
u16 dst_port
Definition: udp.api:42
static clib_error_t * punt_config(vlib_main_t *vm, unformat_input_t *input)
Definition: punt.c:845
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:172
static void * sparse_vec_new(uword elt_bytes, uword sparse_index_bits)
Definition: sparse_vec.h:71
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:116
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:503
#define BITS(x)
Definition: clib.h:60
Definition: punt.h:46
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:58
vlib_node_registration_t udp6_punt_node
(constructor) VLIB_REGISTER_NODE (udp6_punt_node)
Definition: punt.c:66
#define PUNT_PACKETDESC_VERSION
Definition: punt.h:55
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
struct sockaddr_un caddr
Definition: punt.h:68
Definition: defs.h:46
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
#define SPARSE_VEC_INVALID_INDEX
Definition: sparse_vec.h:68