FD.io VPP  v17.10-9-gd594711
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/ip/punt.h>
31 #include <vppinfra/sparse_vec.h>
32 #include <vlib/unix/unix.h>
33 
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <sys/uio.h>
39 #include <stdlib.h>
40 #include <stdbool.h>
41 
42 #define foreach_punt_next \
43  _ (PUNT, "error-punt")
44 
45 typedef enum
46 {
47 #define _(s,n) PUNT_NEXT_##s,
49 #undef _
51 } punt_next_t;
52 
54 {
59 };
60 
66 
68 
69 char *
71 {
72  punt_main_t *pm = &punt_main;
73  return pm->sun_path;
74 }
75 
76 /** @brief IPv4/IPv6 UDP punt node main loop.
77 
78  This is the main loop inline function for IPv4/IPv6 UDP punt
79  transition node.
80 
81  @param vm vlib_main_t corresponding to the current thread
82  @param node vlib_node_runtime_t
83  @param frame vlib_frame_t whose contents should be dispatched
84  @param is_ipv4 indicates if called for IPv4 or IPv6 node
85 */
88  vlib_node_runtime_t * node,
89  vlib_frame_t * from_frame, int is_ip4)
90 {
91  u32 n_left_from, *from, *to_next;
92  word advance;
93 
94  from = vlib_frame_vector_args (from_frame);
95  n_left_from = from_frame->n_vectors;
96 
97  /* udp[46]_lookup hands us the data payload, not the IP header */
98  if (is_ip4)
99  advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t));
100  else
101  advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t));
102 
103  while (n_left_from > 0)
104  {
105  u32 n_left_to_next;
106 
107  vlib_get_next_frame (vm, node, PUNT_NEXT_PUNT, to_next, n_left_to_next);
108 
109  while (n_left_from > 0 && n_left_to_next > 0)
110  {
111  u32 bi0;
112  vlib_buffer_t *b0;
113 
114  bi0 = from[0];
115  to_next[0] = bi0;
116  from += 1;
117  to_next += 1;
118  n_left_from -= 1;
119  n_left_to_next -= 1;
120 
121  b0 = vlib_get_buffer (vm, bi0);
122  vlib_buffer_advance (b0, advance);
123  b0->error = node->errors[PUNT_ERROR_UDP_PORT];
124  }
125 
126  vlib_put_next_frame (vm, node, PUNT_NEXT_PUNT, n_left_to_next);
127  }
128 
129  return from_frame->n_vectors;
130 }
131 
132 static char *punt_error_strings[] = {
133 #define punt_error(n,s) s,
134 #include "punt_error.def"
135 #undef punt_error
136 };
137 
138 /** @brief IPv4 UDP punt node.
139  @node ip4-udp-punt
140 
141  This is the IPv4 UDP punt transition node. It is registered as a next
142  node for the "ip4-udp-lookup" handling UDP port(s) requested for punt.
143  The buffer's current data pointer is adjusted to the original packet
144  IPv4 header. All buffers are dispatched to "error-punt".
145 
146  @param vm vlib_main_t corresponding to the current thread
147  @param node vlib_node_runtime_t
148  @param frame vlib_frame_t whose contents should be dispatched
149 
150  @par Graph mechanics: next index usage
151 
152  @em Sets:
153  - <code>vnet_buffer(b)->current_data</code>
154  - <code>vnet_buffer(b)->current_len</code>
155 
156  <em>Next Index:</em>
157  - Dispatches the packet to the "error-punt" node
158 */
159 static uword
161  vlib_node_runtime_t * node, vlib_frame_t * from_frame)
162 {
163  return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ );
164 }
165 
166 /** @brief IPv6 UDP punt node.
167  @node ip6-udp-punt
168 
169  This is the IPv6 UDP punt transition node. It is registered as a next
170  node for the "ip6-udp-lookup" handling UDP port(s) requested for punt.
171  The buffer's current data pointer is adjusted to the original packet
172  IPv6 header. All buffers are dispatched to "error-punt".
173 
174  @param vm vlib_main_t corresponding to the current thread
175  @param node vlib_node_runtime_t
176  @param frame vlib_frame_t whose contents should be dispatched
177 
178  @par Graph mechanics: next index usage
179 
180  @em Sets:
181  - <code>vnet_buffer(b)->current_data</code>
182  - <code>vnet_buffer(b)->current_len</code>
183 
184  <em>Next Index:</em>
185  - Dispatches the packet to the "error-punt" node
186 */
187 static uword
189  vlib_node_runtime_t * node, vlib_frame_t * from_frame)
190 {
191  return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ );
192 }
193 
194 /* *INDENT-OFF* */
196  .function = udp4_punt,
197  .name = "ip4-udp-punt",
198  /* Takes a vector of packets. */
199  .vector_size = sizeof (u32),
200 
201  .n_errors = PUNT_N_ERROR,
202  .error_strings = punt_error_strings,
203 
204  .n_next_nodes = PUNT_N_NEXT,
205  .next_nodes = {
206 #define _(s,n) [PUNT_NEXT_##s] = n,
208 #undef _
209  },
210 };
211 
213 
215  .function = udp6_punt,
216  .name = "ip6-udp-punt",
217  /* Takes a vector of packets. */
218  .vector_size = sizeof (u32),
219 
220  .n_errors = PUNT_N_ERROR,
221  .error_strings = punt_error_strings,
222 
223  .n_next_nodes = PUNT_N_NEXT,
224  .next_nodes = {
225 #define _(s,n) [PUNT_NEXT_##s] = n,
227 #undef _
228  },
229 };
230 
232 
233 /* *INDENT-ON* */
234 
235 static struct sockaddr_un *
236 punt_socket_get (bool is_ip4, u16 port)
237 {
238  punt_main_t *pm = &punt_main;
239  punt_client_t *v = is_ip4 ? pm->clients_by_dst_port4 :
241 
242  u16 i = sparse_vec_index (v, port);
243  if (i == SPARSE_VEC_INVALID_INDEX)
244  return 0;
245 
246  return &vec_elt (v, i).caddr;
247 }
248 
249 static void
250 punt_socket_register (bool is_ip4, u8 protocol, u16 port,
251  char *client_pathname)
252 {
253  punt_main_t *pm = &punt_main;
254  punt_client_t c, *n;
255  punt_client_t *v = is_ip4 ? pm->clients_by_dst_port4 :
257 
258  memset (&c, 0, sizeof (c));
259  memcpy (c.caddr.sun_path, client_pathname, sizeof (c.caddr.sun_path));
260  c.caddr.sun_family = AF_UNIX;
261  c.port = port;
262  n = sparse_vec_validate (v, port);
263  n[0] = c;
264 }
265 
266 /* $$$$ Just leaves the mapping in place for now */
267 static void
268 punt_socket_unregister (bool is_ip4, u8 protocol, u16 port)
269 {
270  return;
271 }
272 
275  vlib_node_runtime_t * node,
276  vlib_frame_t * frame, bool is_ip4)
277 {
278  u32 *buffers = vlib_frame_args (frame);
279  uword n_packets = frame->n_vectors;
280  struct iovec *iovecs = 0;
281  punt_main_t *pm = &punt_main;
282  int i;
283 
284  u32 node_index = is_ip4 ? udp4_punt_socket_node.index :
285  udp6_punt_socket_node.index;
286 
287  for (i = 0; i < n_packets; i++)
288  {
289  struct iovec *iov;
290  vlib_buffer_t *b;
291  uword l;
292  punt_packetdesc_t packetdesc;
293 
294  b = vlib_get_buffer (vm, buffers[i]);
295 
296  /* Reverse UDP Punt advance */
297  udp_header_t *udp;
298  if (is_ip4)
299  {
300  vlib_buffer_advance (b, -(sizeof (ip4_header_t) +
301  sizeof (udp_header_t)));
303  udp = (udp_header_t *) (ip + 1);
304  }
305  else
306  {
307  vlib_buffer_advance (b, -(sizeof (ip6_header_t) +
308  sizeof (udp_header_t)));
310  udp = (udp_header_t *) (ip + 1);
311  }
312 
313  u16 port = clib_net_to_host_u16 (udp->dst_port);
314 
315  /*
316  * Find registerered client
317  * If no registered client, drop packet and count
318  */
319  struct sockaddr_un *caddr;
320  caddr = punt_socket_get (is_ip4, port);
321  if (!caddr)
322  {
323  vlib_node_increment_counter (vm, node_index,
324  PUNT_ERROR_SOCKET_TX_ERROR, 1);
325  goto error;
326  }
327 
328  /* Re-set iovecs if present. */
329  if (iovecs)
330  _vec_len (iovecs) = 0;
331 
332  /* Add packet descriptor */
333  packetdesc.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
334  packetdesc.action = 0;
335  vec_add2 (iovecs, iov, 1);
336  iov->iov_base = &packetdesc;
337  iov->iov_len = sizeof (packetdesc);
338 
339  /** VLIB buffer chain -> Unix iovec(s). */
340  vlib_buffer_advance (b, -(sizeof (ethernet_header_t)));
341  vec_add2 (iovecs, iov, 1);
342  iov->iov_base = b->data + b->current_data;
343  iov->iov_len = l = b->current_length;
344 
346  {
347  do
348  {
349  b = vlib_get_buffer (vm, b->next_buffer);
350 
351  vec_add2 (iovecs, iov, 1);
352 
353  iov->iov_base = b->data + b->current_data;
354  iov->iov_len = b->current_length;
355  l += b->current_length;
356  }
357  while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
358  }
359 
360  struct msghdr msg = {
361  .msg_name = caddr,
362  .msg_namelen = sizeof (*caddr),
363  .msg_iov = iovecs,
364  .msg_iovlen = vec_len (iovecs),
365  };
366 
367  if (sendmsg (pm->socket_fd, &msg, 0) < l)
368  vlib_node_increment_counter (vm, node_index,
369  PUNT_ERROR_SOCKET_TX_ERROR, 1);
370  }
371 
372 error:
373  vlib_buffer_free_no_next (vm, buffers, n_packets);
374 
375  return n_packets;
376 }
377 
378 static uword
380  vlib_node_runtime_t * node, vlib_frame_t * from_frame)
381 {
382  return udp46_punt_socket_inline (vm, node, from_frame, true /* is_ip4 */ );
383 }
384 
385 static uword
387  vlib_node_runtime_t * node, vlib_frame_t * from_frame)
388 {
389  return udp46_punt_socket_inline (vm, node, from_frame, false /* is_ip4 */ );
390 }
391 
392 
393 /* *INDENT-OFF* */
395  .function = udp4_punt_socket,
396  .name = "ip4-udp-punt-socket",
397  .flags = VLIB_NODE_FLAG_IS_DROP,
398  /* Takes a vector of packets. */
399  .vector_size = sizeof (u32),
400  .n_errors = PUNT_N_ERROR,
401  .error_strings = punt_error_strings,
402 };
404  .function = udp6_punt_socket,
405  .name = "ip6-udp-punt-socket",
406  .flags = VLIB_NODE_FLAG_IS_DROP,
407  .vector_size = sizeof (u32),
408  .n_errors = PUNT_N_ERROR,
409  .error_strings = punt_error_strings,
410 };
411 /* *INDENT-ON* */
412 
413 typedef struct
414 {
415  enum punt_action_e action;
417 } punt_trace_t;
418 
419 static u8 *
420 format_punt_trace (u8 * s, va_list * va)
421 {
422  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
423  CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
424  vnet_main_t *vnm = vnet_get_main ();
425  punt_trace_t *t = va_arg (*va, punt_trace_t *);
426  s = format (s, "%U Action: %d", format_vnet_sw_if_index_name,
427  vnm, t->sw_if_index, t->action);
428  return s;
429 }
430 
431 static uword
433 {
434  const uword buffer_size = VLIB_BUFFER_DATA_SIZE;
435  u32 n_trace = vlib_get_trace_count (vm, node);
436  u32 next = node->cached_next_index;
437  u32 n_left_to_next, next_index;
438  u32 *to_next;
439  u32 error = PUNT_ERROR_NONE;
440  vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
441 
442  /* $$$$ Only dealing with one buffer at the time for now */
443 
444  u32 bi;
445  vlib_buffer_t *b;
446  punt_packetdesc_t packetdesc;
447  ssize_t size;
448  struct iovec io[2];
449 
450  if (vlib_buffer_alloc (vm, &bi, 1) != 1)
451  {
452  error = PUNT_ERROR_NOBUFFER;
453  goto error;
454  }
455 
456  b = vlib_get_buffer (vm, bi);
457  io[0].iov_base = &packetdesc;
458  io[0].iov_len = sizeof (packetdesc);
459  io[1].iov_base = b->data;
460  io[1].iov_len = buffer_size;
461 
462  size = readv (fd, io, 2);
463  /* We need at least the packet descriptor plus a header */
464  if (size <= (int) (sizeof (packetdesc) + sizeof (ip4_header_t)))
465  {
466  vlib_buffer_free (vm, &bi, 1);
467  error = PUNT_ERROR_READV;
468  goto error;
469  }
470 
471  b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
472  b->current_length = size - sizeof (packetdesc);
473 
475 
476  switch (packetdesc.action)
477  {
478  case PUNT_L2:
479  vnet_buffer (b)->sw_if_index[VLIB_TX] = packetdesc.sw_if_index;
481  break;
482 
483  case PUNT_IP4_ROUTED:
484  vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
485  vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
486  next_index = PUNT_SOCKET_RX_NEXT_IP4_LOOKUP;
487  break;
488 
489  case PUNT_IP6_ROUTED:
490  vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
491  vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
492  next_index = PUNT_SOCKET_RX_NEXT_IP6_LOOKUP;
493  break;
494 
495  default:
496  error = PUNT_ERROR_ACTION;
497  vlib_buffer_free (vm, &bi, 1);
498  goto error;
499  }
500 
501  if (PREDICT_FALSE (n_trace > 0))
502  {
503  punt_trace_t *t;
504  vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
505  vlib_set_trace_count (vm, node, --n_trace);
506  t = vlib_add_trace (vm, node, b, sizeof (*t));
507  t->sw_if_index = packetdesc.sw_if_index;
508  t->action = packetdesc.action;
509  }
510 
511  to_next[0] = bi;
512  to_next++;
513  n_left_to_next--;
514 
515  vlib_validate_buffer_enqueue_x1 (vm, node, next, to_next, n_left_to_next,
516  bi, next_index);
517  vlib_put_next_frame (vm, node, next, n_left_to_next);
518  return 1;
519 
520 error:
521  vlib_node_increment_counter (vm, punt_socket_rx_node.index, error, 1);
522  return 0;
523 }
524 
525 static uword
527  vlib_node_runtime_t * node, vlib_frame_t * frame)
528 {
529  punt_main_t *pm = &punt_main;
530  u32 total_count = 0;
531  int i;
532 
533  for (i = 0; i < vec_len (pm->ready_fds); i++)
534  {
535  total_count += punt_socket_rx_fd (vm, node, pm->ready_fds[i]);
536  vec_del1 (pm->ready_fds, i);
537  }
538  return total_count;
539 }
540 
542 {
543  .function = punt_socket_rx,.name = "punt-socket-rx",.type =
544  VLIB_NODE_TYPE_INPUT,.state = VLIB_NODE_STATE_INTERRUPT,.vector_size =
545  1,.n_errors = PUNT_N_ERROR,.error_strings =
546  punt_error_strings,.n_next_nodes = PUNT_SOCKET_RX_N_NEXT,.next_nodes =
547  {
548 [PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT] = "interface-output",
549  [PUNT_SOCKET_RX_NEXT_IP4_LOOKUP] = "ip4-lookup",
550  [PUNT_SOCKET_RX_NEXT_IP6_LOOKUP] = "ip6-lookup",},.format_trace =
552 
553 static clib_error_t *
555 {
557  punt_main_t *pm = &punt_main;
558 
559  /** Schedule the rx node */
562 
563  return 0;
564 }
565 
566 clib_error_t *
568  bool is_ip4, u8 protocol, u16 port,
569  char *client_pathname)
570 {
571  punt_main_t *pm = &punt_main;
572 
573  if (!pm->is_configured)
574  return clib_error_return (0, "socket is not configured");
575 
576  if (header_version != PUNT_PACKETDESC_VERSION)
577  return clib_error_return (0, "Invalid packet descriptor version");
578 
579  /* For now we only support UDP punt */
580  if (protocol != IP_PROTOCOL_UDP)
581  return clib_error_return (0,
582  "only UDP protocol (%d) is supported, got %d",
583  IP_PROTOCOL_UDP, protocol);
584 
585  if (port == (u16) ~ 0)
586  return clib_error_return (0, "UDP port number required");
587 
588  /* Register client */
589  punt_socket_register (is_ip4, protocol, port, client_pathname);
590 
591  u32 node_index = is_ip4 ? udp4_punt_socket_node.index :
592  udp6_punt_socket_node.index;
593 
594  udp_register_dst_port (vm, port, node_index, is_ip4);
595 
596  return 0;
597 }
598 
599 clib_error_t *
600 vnet_punt_socket_del (vlib_main_t * vm, bool is_ip4, u8 l4_protocol, u16 port)
601 {
602  punt_main_t *pm = &punt_main;
603 
604  if (!pm->is_configured)
605  return clib_error_return (0, "socket is not configured");
606 
607  punt_socket_unregister (is_ip4, l4_protocol, port);
608  udp_unregister_dst_port (vm, port, is_ip4);
609 
610  return 0;
611 }
612 
613 /**
614  * @brief Request IP traffic punt to the local TCP/IP stack.
615  *
616  * @em Note
617  * - UDP and TCP are the only protocols supported in the current implementation
618  *
619  * @param vm vlib_main_t corresponding to the current thread
620  * @param ipv IP protcol version.
621  * 4 - IPv4, 6 - IPv6, ~0 for both IPv6 and IPv4
622  * @param protocol 8-bits L4 protocol value
623  * UDP is 17
624  * TCP is 1
625  * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only)
626  *
627  * @returns 0 on success, non-zero value otherwise
628  */
629 clib_error_t *
630 vnet_punt_add_del (vlib_main_t * vm, u8 ipv, u8 protocol, u16 port,
631  bool is_add)
632 {
633 
634  /* For now we only support UDP punt */
635  if (protocol != IP_PROTOCOL_UDP && protocol != IP_PROTOCOL_TCP)
636  return clib_error_return (0,
637  "only UDP (%d) and TCP (%d) protocols are supported, got %d",
638  IP_PROTOCOL_UDP, IP_PROTOCOL_TCP, protocol);
639 
640  if (ipv != (u8) ~ 0 && ipv != 4 && ipv != 6)
641  return clib_error_return (0, "IP version must be 4 or 6, got %d", ipv);
642 
643  if (port == (u16) ~ 0)
644  {
645  if ((ipv == 4) || (ipv == (u8) ~ 0))
646  {
647  if (protocol == IP_PROTOCOL_UDP)
648  udp_punt_unknown (vm, 1, is_add);
649  else if (protocol == IP_PROTOCOL_TCP)
650  tcp_punt_unknown (vm, 1, is_add);
651  }
652 
653  if ((ipv == 6) || (ipv == (u8) ~ 0))
654  {
655  if (protocol == IP_PROTOCOL_UDP)
656  udp_punt_unknown (vm, 0, is_add);
657  else if (protocol == IP_PROTOCOL_TCP)
658  tcp_punt_unknown (vm, 0, is_add);
659  }
660 
661  return 0;
662  }
663 
664  else if (is_add)
665  {
666  if (protocol == IP_PROTOCOL_TCP)
667  return clib_error_return (0, "punt TCP ports is not supported yet");
668 
669  if (ipv == 4 || ipv == (u8) ~ 0)
670  udp_register_dst_port (vm, port, udp4_punt_node.index, 1);
671 
672  if (ipv == 6 || ipv == (u8) ~ 0)
673  udp_register_dst_port (vm, port, udp6_punt_node.index, 0);
674 
675  return 0;
676  }
677  else
678  return clib_error_return (0, "punt delete is not supported yet");
679 }
680 
681 static clib_error_t *
683  unformat_input_t * input, vlib_cli_command_t * cmd)
684 {
685  u32 port;
686  bool is_add = true;
687  u32 protocol = ~0;
688  clib_error_t *error;
689 
691  {
692  if (unformat (input, "del"))
693  is_add = false;
694  else if (unformat (input, "all"))
695  {
696  /* punt both IPv6 and IPv4 when used in CLI */
697  error = vnet_punt_add_del (vm, ~0, protocol, ~0, is_add);
698  if (error)
699  clib_error_report (error);
700  }
701  else if (unformat (input, "%d", &port))
702  {
703  /* punt both IPv6 and IPv4 when used in CLI */
704  error = vnet_punt_add_del (vm, ~0, protocol, port, is_add);
705  if (error)
706  clib_error_report (error);
707  }
708  else if (unformat (input, "udp"))
709  protocol = IP_PROTOCOL_UDP;
710  else if (unformat (input, "tcp"))
711  protocol = IP_PROTOCOL_TCP;
712  }
713 
714  return 0;
715 }
716 
717 /*?
718  * The set of '<em>set punt</em>' commands allows specific IP traffic to
719  * be punted to the host TCP/IP stack
720  *
721  * @em Note
722  * - UDP is the only protocol supported in the current implementation
723  * - All TCP traffic is currently punted to the host by default
724  *
725  * @cliexpar
726  * @parblock
727  * Example of how to request NTP traffic to be punted
728  * @cliexcmd{set punt udp 125}
729  *
730  * Example of how to request all 'unknown' UDP traffic to be punted
731  * @cliexcmd{set punt udp all}
732  *
733  * Example of how to stop all 'unknown' UDP traffic to be punted
734  * @cliexcmd{set punt udp del all}
735  * @endparblock
736 ?*/
737 /* *INDENT-OFF* */
738 VLIB_CLI_COMMAND (punt_command, static) = {
739  .path = "set punt",
740  .short_help = "set punt [udp|tcp] [del] <all | port-num1 [port-num2 ...]>",
741  .function = punt_cli,
742 };
743 /* *INDENT-ON* */
744 
745 clib_error_t *
747 {
748  punt_main_t *pm = &punt_main;
749 
751  (sizeof (pm->clients_by_dst_port6[0]),
752  BITS (((udp_header_t *) 0)->dst_port));
754  (sizeof (pm->clients_by_dst_port4[0]),
755  BITS (((udp_header_t *) 0)->dst_port));
756 
757  pm->is_configured = false;
759  (u8 *)
760  "interface-output");
761  return 0;
762 }
763 
765 
766 static clib_error_t *
768 {
769  punt_main_t *pm = &punt_main;
770  char *socket_path = 0;
771 
773  {
774  if (unformat (input, "socket %s", &socket_path))
775  strncpy (pm->sun_path, socket_path, 108 - 1);
776  else
777  return clib_error_return (0, "unknown input `%U'",
778  format_unformat_error, input);
779  }
780 
781  if (socket_path == 0)
782  return 0;
783 
784  /* UNIX domain socket */
785  struct sockaddr_un addr;
786  if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
787  {
788  return clib_error_return (0, "socket error");
789  }
790 
791  memset (&addr, 0, sizeof (addr));
792  addr.sun_family = AF_UNIX;
793  if (*socket_path == '\0')
794  {
795  *addr.sun_path = '\0';
796  strncpy (addr.sun_path + 1, socket_path + 1,
797  sizeof (addr.sun_path) - 2);
798  }
799  else
800  {
801  strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
802  unlink (socket_path);
803  }
804 
805  if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
806  {
807  return clib_error_return (0, "bind error");
808  }
809 
810  /* Register socket */
812  clib_file_t template = { 0 };
814  template.file_descriptor = pm->socket_fd;
815  pm->clib_file_index = clib_file_add (fm, &template);
816 
817  pm->is_configured = true;
818 
819  return 0;
820 }
821 
823 
824 /*
825  * fd.io coding-style-patch-verification: ON
826  *
827  * Local Variables:
828  * eval: (c-set-style "gnu")
829  * End:
830  */
static uword punt_socket_rx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: punt.c:526
vlib_node_t * interface_output_node
Definition: punt.h:77
static clib_error_t * punt_socket_read_ready(clib_file_t *uf)
Definition: punt.c:554
static clib_error_t * punt_cli(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: punt.c:682
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
#define CLIB_UNUSED(x)
Definition: clib.h:79
static struct sockaddr_un * punt_socket_get(bool is_ip4, u16 port)
Definition: punt.c:236
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:143
punt_next_t
Definition: punt.c:45
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:317
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static uword udp6_punt_socket(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: punt.c:386
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:160
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:65
u32 * ready_fds
Definition: punt.h:78
u32 clib_file_index
Definition: punt.h:75
u32 file_descriptor
Definition: file.h:53
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:518
struct _vlib_node_registration vlib_node_registration_t
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:557
int socket_fd
Definition: punt.h:71
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:274
punt_action_e
Definition: punt.h:42
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:415
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:188
format_function_t format_vnet_sw_if_index_name
enum punt_action_e action
Definition: punt.h:57
clib_file_function_t * read_function
Definition: file.h:63
u16 port
Definition: punt.h:65
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:104
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:95
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:68
punt_main_t punt_main
Definition: punt.c:67
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
#define VLIB_NODE_FLAG_IS_DROP
Definition: node.h:254
#define always_inline
Definition: clib.h:84
#define sparse_vec_validate(v, i)
Definition: sparse_vec.h:214
vlib_node_registration_t udp4_punt_node
(constructor) VLIB_REGISTER_NODE (udp4_punt_node)
Definition: punt.c:61
#define clib_error_return(e, args...)
Definition: error.h:99
clib_file_main_t file_main
Definition: main.c:63
static u8 * format_punt_trace(u8 *s, va_list *va)
Definition: punt.c:420
punt_client_t * clients_by_dst_port6
Definition: punt.h:74
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:72
#define v
Definition: acl.c:323
struct _unformat_input_t unformat_input_t
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:193
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:119
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:801
vlib_node_registration_t udp6_punt_socket_node
(constructor) VLIB_REGISTER_NODE (udp6_punt_socket_node)
Definition: punt.c:64
#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:216
#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:113
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1158
void udp_punt_unknown(vlib_main_t *vm, u8 is_ip4, u8 is_add)
Definition: udp_local.c:549
static void vlib_buffer_free_no_next(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers, does not free the buffer chain for each buffer.
Definition: buffer_funcs.h:338
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
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:567
svmdb_client_t * c
u16 n_vectors
Definition: node.h:344
vlib_main_t * vm
Definition: buffer.c:283
u32 sw_if_index
Definition: punt.h:56
void udp_unregister_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u8 is_ip4)
Definition: udp_local.c:526
static void punt_socket_register(bool is_ip4, u8 protocol, u16 port, char *client_pathname)
Definition: punt.c:250
static uword udp4_punt_socket(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: punt.c:379
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:454
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:53
char * vnet_punt_get_server_pathname(void)
Definition: punt.c:70
#define VLIB_BUFFER_DATA_SIZE
Definition: buffer.h:51
enum punt_action_e action
Definition: punt.c:415
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
static uword sparse_vec_index(void *v, uword sparse_index)
Definition: sparse_vec.h:152
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:456
unsigned int u32
Definition: types.h:88
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
Definition: file.h:84
clib_error_t * vnet_punt_socket_del(vlib_main_t *vm, bool is_ip4, u8 l4_protocol, u16 port)
Definition: punt.c:600
VLIB_NODE_FUNCTION_MULTIARCH(udp4_punt_node, udp4_punt)
char sun_path[sizeof(struct sockaddr_un)]
Definition: punt.h:72
clib_error_t * punt_init(vlib_main_t *vm)
Definition: punt.c:746
u64 size
Definition: vhost-user.h:76
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:109
#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:73
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:206
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:87
static char * punt_error_strings[]
Definition: punt.c:132
static uword punt_socket_rx_fd(vlib_main_t *vm, vlib_node_runtime_t *node, u32 fd)
Definition: punt.c:432
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u64 uword
Definition: types.h:112
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:55
vlib_node_registration_t udp4_punt_socket_node
(constructor) VLIB_REGISTER_NODE (udp4_punt_socket_node)
Definition: punt.c:63
#define vec_elt(v, i)
Get vector value at index i.
void tcp_punt_unknown(vlib_main_t *vm, u8 is_ip4, u8 is_add)
Definition: tcp.c:1410
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
#define foreach_punt_next
Definition: punt.c:42
i64 word
Definition: types.h:111
bool is_configured
Definition: punt.h:76
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:517
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:268
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:630
#define vnet_buffer(b)
Definition: buffer.h:306
u32 sw_if_index
Definition: punt.c:416
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 data[0]
Packet data.
Definition: buffer.h:157
Definition: file.h:50
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:488
static clib_error_t * punt_config(vlib_main_t *vm, unformat_input_t *input)
Definition: punt.c:767
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:159
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:75
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:254
#define BITS(x)
Definition: clib.h:58
Definition: punt.h:44
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
vlib_node_registration_t udp6_punt_node
(constructor) VLIB_REGISTER_NODE (udp6_punt_node)
Definition: punt.c:62
#define PUNT_PACKETDESC_VERSION
Definition: punt.h:53
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
struct sockaddr_un caddr
Definition: punt.h:66
Definition: defs.h:46
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
#define SPARSE_VEC_INVALID_INDEX
Definition: sparse_vec.h:68