FD.io VPP  v17.01-9-ge7dcee4
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 togather 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 #include <vlib/vlib.h>
25 #include <vnet/pg/pg.h>
26 #include <vnet/ip/udp.h>
27 #include <vnet/ip/punt.h>
28 
29 #define foreach_punt_next \
30  _ (PUNT, "error-punt")
31 
32 typedef enum
33 {
34 #define _(s,n) PUNT_NEXT_##s,
36 #undef _
38 } punt_next_t;
39 
42 
43 /** @brief IPv4/IPv6 UDP punt node main loop.
44 
45  This is the main loop inline function for IPv4/IPv6 UDP punt
46  transition node.
47 
48  @param vm vlib_main_t corresponding to the current thread
49  @param node vlib_node_runtime_t
50  @param frame vlib_frame_t whose contents should be dispatched
51  @param is_ipv4 indicates if called for IPv4 or IPv6 node
52 */
55  vlib_node_runtime_t * node,
56  vlib_frame_t * from_frame, int is_ip4)
57 {
58  u32 n_left_from, *from, *to_next;
59  word advance;
60 
61  from = vlib_frame_vector_args (from_frame);
62  n_left_from = from_frame->n_vectors;
63 
64  /* udp[46]_lookup hands us the data payload, not the IP header */
65  if (is_ip4)
66  advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t));
67  else
68  advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t));
69 
70  while (n_left_from > 0)
71  {
72  u32 n_left_to_next;
73 
74  vlib_get_next_frame (vm, node, PUNT_NEXT_PUNT, to_next, n_left_to_next);
75 
76  while (n_left_from > 0 && n_left_to_next > 0)
77  {
78  u32 bi0;
79  vlib_buffer_t *b0;
80 
81  bi0 = from[0];
82  to_next[0] = bi0;
83  from += 1;
84  to_next += 1;
85  n_left_from -= 1;
86  n_left_to_next -= 1;
87 
88  b0 = vlib_get_buffer (vm, bi0);
89  vlib_buffer_advance (b0, advance);
90  b0->error = node->errors[PUNT_ERROR_UDP_PORT];
91  }
92 
93  vlib_put_next_frame (vm, node, PUNT_NEXT_PUNT, n_left_to_next);
94  }
95 
96  return from_frame->n_vectors;
97 }
98 
99 static char *punt_error_strings[] = {
100 #define punt_error(n,s) s,
101 #include "punt_error.def"
102 #undef punt_error
103 };
104 
105 /** @brief IPv4 UDP punt node.
106  @node ip4-udp-punt
107 
108  This is the IPv4 UDP punt transition node. It is registered as a next
109  node for the "ip4-udp-lookup" handling UDP port(s) requested for punt.
110  The buffer's current data pointer is adjusted to the original packet
111  IPv4 header. All buffers are dispatched to "error-punt".
112 
113  @param vm vlib_main_t corresponding to the current thread
114  @param node vlib_node_runtime_t
115  @param frame vlib_frame_t whose contents should be dispatched
116 
117  @par Graph mechanics: next index usage
118 
119  @em Sets:
120  - <code>vnet_buffer(b)->current_data</code>
121  - <code>vnet_buffer(b)->current_len</code>
122 
123  <em>Next Index:</em>
124  - Dispatches the packet to the "error-punt" node
125 */
126 static uword
128  vlib_node_runtime_t * node, vlib_frame_t * from_frame)
129 {
130  return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ );
131 }
132 
133 /** @brief IPv6 UDP punt node.
134  @node ip6-udp-punt
135 
136  This is the IPv6 UDP punt transition node. It is registered as a next
137  node for the "ip6-udp-lookup" handling UDP port(s) requested for punt.
138  The buffer's current data pointer is adjusted to the original packet
139  IPv6 header. All buffers are dispatched to "error-punt".
140 
141  @param vm vlib_main_t corresponding to the current thread
142  @param node vlib_node_runtime_t
143  @param frame vlib_frame_t whose contents should be dispatched
144 
145  @par Graph mechanics: next index usage
146 
147  @em Sets:
148  - <code>vnet_buffer(b)->current_data</code>
149  - <code>vnet_buffer(b)->current_len</code>
150 
151  <em>Next Index:</em>
152  - Dispatches the packet to the "error-punt" node
153 */
154 static uword
156  vlib_node_runtime_t * node, vlib_frame_t * from_frame)
157 {
158  return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ );
159 }
160 
161 /* *INDENT-OFF* */
163  .function = udp4_punt,
164  .name = "ip4-udp-punt",
165  /* Takes a vector of packets. */
166  .vector_size = sizeof (u32),
167 
168  .n_errors = PUNT_N_ERROR,
169  .error_strings = punt_error_strings,
170 
171  .n_next_nodes = PUNT_N_NEXT,
172  .next_nodes = {
173 #define _(s,n) [PUNT_NEXT_##s] = n,
175 #undef _
176  },
177 };
178 
180 
182  .function = udp6_punt,
183  .name = "ip6-udp-punt",
184  /* Takes a vector of packets. */
185  .vector_size = sizeof (u32),
186 
187  .n_errors = PUNT_N_ERROR,
188  .error_strings = punt_error_strings,
189 
190  .n_next_nodes = PUNT_N_NEXT,
191  .next_nodes = {
192 #define _(s,n) [PUNT_NEXT_##s] = n,
194 #undef _
195  },
196 };
197 /* *INDENT-ON* */
198 
200 
201 /**
202  * @brief Request IP traffic punt to the local TCP/IP stack.
203  *
204  * @em Note
205  * - UDP is the only protocol supported in the current implementation
206  * - When requesting UDP punt port number(s) must be specified
207  * - All TCP traffic is currently punted to the host by default
208  *
209  * @param vm vlib_main_t corresponding to the current thread
210  * @param ipv IP protcol version.
211  * 4 - IPv4, 6 - IPv6, ~0 for both IPv6 and IPv4
212  * @param protocol 8-bits L4 protocol value
213  * Only value of 17 (UDP) is currently supported
214  * @param port 16-bits L4 (TCP/IP) port number when applicable
215  *
216  * @returns 0 on success, non-zero value otherwise
217  */
218 clib_error_t *
219 vnet_punt_add_del (vlib_main_t * vm, u8 ipv, u8 protocol, u16 port,
220  int is_add)
221 {
222  /* For now we only support UDP punt */
223  if (protocol != IP_PROTOCOL_UDP)
224  return clib_error_return (0,
225  "only UDP protocol (%d) is supported, got %d",
226  IP_PROTOCOL_UDP, protocol);
227 
228  if (ipv != (u8) ~ 0 && ipv != 4 && ipv != 6)
229  return clib_error_return (0, "IP version must be 4 or 6, got %d", ipv);
230 
231  if (port == (u16) ~ 0)
232  {
233  if (ipv == 4 || ipv == (u8) ~ 0)
234  udp_punt_unknown (vm, 1, is_add);
235 
236  if (ipv == 6 || ipv == (u8) ~ 0)
237  udp_punt_unknown (vm, 0, is_add);
238 
239  return 0;
240  }
241 
242  else if (is_add)
243  {
244  if (ipv == 4 || ipv == (u8) ~ 0)
245  udp_register_dst_port (vm, port, udp4_punt_node.index, 1);
246 
247  if (ipv == 6 || ipv == (u8) ~ 0)
248  udp_register_dst_port (vm, port, udp6_punt_node.index, 0);
249 
250  return 0;
251  }
252 
253  else
254  return clib_error_return (0, "punt delete is not supported yet");
255 }
256 
257 static clib_error_t *
259  unformat_input_t * input, vlib_cli_command_t * cmd)
260 {
261  u32 udp_port;
262  int is_add = 1;
263  clib_error_t *error;
264 
266  {
267  if (unformat (input, "del"))
268  is_add = 0;
269  if (unformat (input, "all"))
270  {
271  /* punt both IPv6 and IPv4 when used in CLI */
272  error = vnet_punt_add_del (vm, ~0, IP_PROTOCOL_UDP, ~0, is_add);
273  if (error)
274  clib_error_report (error);
275  }
276  else if (unformat (input, "%d", &udp_port))
277  {
278  /* punt both IPv6 and IPv4 when used in CLI */
279  error = vnet_punt_add_del (vm, ~0, IP_PROTOCOL_UDP,
280  udp_port, is_add);
281  if (error)
282  clib_error_report (error);
283  }
284  }
285 
286  return 0;
287 }
288 
289 /*?
290  * The set of '<em>set punt</em>' commands allows specific IP traffic to
291  * be punted to the host TCP/IP stack
292  *
293  * @em Note
294  * - UDP is the only protocol supported in the current implementation
295  * - All TCP traffic is currently punted to the host by default
296  *
297  * @cliexpar
298  * @parblock
299  * Example of how to request NTP traffic to be punted
300  * @cliexcmd{set punt udp 125}
301  *
302  * Example of how to request all 'unknown' UDP traffic to be punted
303  * @cliexcmd{set punt udp all}
304  *
305  * Example of how to stop all 'unknown' UDP traffic to be punted
306  * @cliexcmd{set punt udp del all}
307  * @endparblock
308 ?*/
309 /* *INDENT-OFF* */
310 VLIB_CLI_COMMAND (punt_udp_command, static) = {
311  .path = "set punt udp",
312  .short_help = "set punt udp [del] <all | port-num1 [port-num2 ...]>",
313  .function = udp_punt_cli,
314 };
315 /* *INDENT-ON* */
316 
317 /*
318  * fd.io coding-style-patch-verification: ON
319  *
320  * Local Variables:
321  * eval: (c-set-style "gnu")
322  * End:
323  */
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:459
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
punt_next_t
Definition: punt.c:32
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:127
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
struct _vlib_node_registration vlib_node_registration_t
#define clib_error_report(e)
Definition: error.h:125
vlib_error_t * errors
Definition: node.h:419
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:155
#define always_inline
Definition: clib.h:84
vlib_node_registration_t udp4_punt_node
(constructor) VLIB_REGISTER_NODE (udp4_punt_node)
Definition: punt.c:40
clib_error_t * vnet_punt_add_del(vlib_main_t *vm, u8 ipv, u8 protocol, u16 port, int is_add)
Request IP traffic punt to the local TCP/IP stack.
Definition: punt.c:219
#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:350
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:121
void udp_punt_unknown(vlib_main_t *vm, u8 is_ip4, u8 is_add)
Definition: udp_local.c:531
u16 n_vectors
Definition: node.h:344
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:207
Definitions for punt infrastructure.
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
unsigned int u32
Definition: types.h:88
VLIB_NODE_FUNCTION_MULTIARCH(udp4_punt_node, udp4_punt)
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:54
static char * punt_error_strings[]
Definition: punt.c:99
u64 uword
Definition: types.h:112
static clib_error_t * udp_punt_cli(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: punt.c:258
unsigned short u16
Definition: types.h:57
#define foreach_punt_next
Definition: punt.c:29
i64 word
Definition: types.h:111
unsigned char u8
Definition: types.h:56
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:253
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
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:495
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t
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:41