2 * node.c: udp packet processing
4 * Copyright (c) 2013 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/ip/udp.h>
21 #include <vnet/ip/udp_packet.h>
22 #include <vppinfra/sparse_vec.h>
26 #define foreach_udp_input_next \
27 _ (PUNT, "error-punt") \
28 _ (DROP, "error-drop")
31 #define _(s,n) UDP_INPUT_NEXT_##s,
32 foreach_udp_input_next
42 u8 * format_udp_rx_trace (u8 * s, va_list * args)
44 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
45 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
46 udp_rx_trace_t * t = va_arg (*args, udp_rx_trace_t *);
48 s = format (s, "UDP: src-port %d dst-port %d",
49 clib_net_to_host_u16(t->src_port),
50 clib_net_to_host_u16(t->dst_port));
55 /* Sparse vector mapping udp dst_port in network byte order
57 u16 * next_by_dst_port;
59 u32 * sparse_index_by_next_index;
60 } udp_input_runtime_t;
62 vlib_node_registration_t udp4_input_node;
63 vlib_node_registration_t udp6_input_node;
66 udp46_input_inline (vlib_main_t * vm,
67 vlib_node_runtime_t * node,
68 vlib_frame_t * from_frame,
71 udp_input_runtime_t * rt = is_ip4 ?
72 (void *) vlib_node_get_runtime_data (vm, udp4_input_node.index)
73 : (void *) vlib_node_get_runtime_data (vm, udp6_input_node.index);
74 __attribute__((unused)) u32 n_left_from, next_index, i_next, * from, * to_next;
76 from = vlib_frame_vector_args (from_frame);
77 n_left_from = from_frame->n_vectors;
79 next_index = node->cached_next_index;
80 i_next = vec_elt (rt->sparse_index_by_next_index, next_index);
82 while (n_left_from > 0)
86 vlib_get_next_frame (vm, node, next_index,
87 to_next, n_left_to_next);
89 while (n_left_from >= 4 && n_left_to_next >= 2)
92 vlib_buffer_t * b0, * b1;
93 udp_header_t * h0 = 0, * h1 = 0;
94 u32 i0, i1, dst_port0, dst_port1;
95 u32 advance0, advance1;
96 u32 error0, next0, error1, next1;
98 /* Prefetch next iteration. */
100 vlib_buffer_t * p2, * p3;
102 p2 = vlib_get_buffer (vm, from[2]);
103 p3 = vlib_get_buffer (vm, from[3]);
105 vlib_prefetch_buffer_header (p2, LOAD);
106 vlib_prefetch_buffer_header (p3, LOAD);
108 CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
109 CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
121 b0 = vlib_get_buffer (vm, bi0);
122 b1 = vlib_get_buffer (vm, bi1);
124 /* ip4/6_local hands us the ip header, not the udp header */
127 advance0 = sizeof(ip4_header_t);
128 advance1 = sizeof(ip4_header_t);
132 advance0 = sizeof(ip6_header_t);
133 advance1 = sizeof(ip6_header_t);
136 if (PREDICT_FALSE(b0->current_length < advance0 + sizeof (*h0)))
138 error0 = UDP_ERROR_LENGTH_ERROR;
139 next0 = UDP_INPUT_NEXT_DROP;
143 vlib_buffer_advance (b0, advance0);
144 h0 = vlib_buffer_get_current (b0);
146 if (PREDICT_FALSE(clib_net_to_host_u16(h0->length) >
147 vlib_buffer_length_in_chain(vm, b0)))
149 error0 = UDP_ERROR_LENGTH_ERROR;
150 next0 = UDP_INPUT_NEXT_DROP;
154 if (PREDICT_FALSE(b1->current_length < advance1 + sizeof (*h1)))
156 error1 = UDP_ERROR_LENGTH_ERROR;
157 next1 = UDP_INPUT_NEXT_DROP;
161 vlib_buffer_advance (b1, advance1);
162 h1 = vlib_buffer_get_current (b1);
164 if (PREDICT_FALSE(clib_net_to_host_u16(h1->length) >
165 vlib_buffer_length_in_chain(vm, b1)))
167 error1 = UDP_ERROR_LENGTH_ERROR;
168 next1 = UDP_INPUT_NEXT_DROP;
172 /* Index sparse array with network byte order. */
173 dst_port0 = (error0 == 0) ? h0->dst_port : 0;
174 dst_port1 = (error1 == 0) ? h1->dst_port : 0;
175 sparse_vec_index2 (rt->next_by_dst_port, dst_port0, dst_port1,
177 next0 = (error0 == 0) ? vec_elt(rt->next_by_dst_port, i0) : next0;
178 next1 = (error1 == 0) ? vec_elt(rt->next_by_dst_port, i1) : next1;
180 b0->error = node->errors[next0 == SPARSE_VEC_INVALID_INDEX ?
181 UDP_ERROR_NO_LISTENER : error0];
182 b1->error = node->errors[next1 == SPARSE_VEC_INVALID_INDEX ?
183 UDP_ERROR_NO_LISTENER : error1];
185 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
187 udp_rx_trace_t *tr = vlib_add_trace (vm, node,
189 if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
191 tr->src_port = h0->src_port;
192 tr->dst_port = h0->dst_port;
195 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
197 udp_rx_trace_t *tr = vlib_add_trace (vm, node,
199 if (b1->error != node->errors[UDP_ERROR_LENGTH_ERROR])
201 tr->src_port = h1->src_port;
202 tr->dst_port = h1->dst_port;
206 vlib_buffer_advance (b0, sizeof (*h0));
207 vlib_buffer_advance (b1, sizeof (*h1));
209 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
210 to_next, n_left_to_next,
211 bi0, bi1, next0, next1);
214 while (n_left_from > 0 && n_left_to_next > 0)
218 udp_header_t * h0 = 0;
229 b0 = vlib_get_buffer (vm, bi0);
231 /* ip4/6_local hands us the ip header, not the udp header */
233 advance0 = sizeof(ip4_header_t);
235 advance0 = sizeof(ip6_header_t);
237 if (PREDICT_FALSE(b0->current_length < advance0 + sizeof (*h0)))
239 b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
240 next0 = UDP_INPUT_NEXT_DROP;
244 vlib_buffer_advance (b0, advance0);
246 h0 = vlib_buffer_get_current (b0);
248 if (PREDICT_TRUE(clib_net_to_host_u16(h0->length) <=
249 vlib_buffer_length_in_chain(vm, b0)))
251 i0 = sparse_vec_index (rt->next_by_dst_port, h0->dst_port);
252 next0 = vec_elt(rt->next_by_dst_port, i0);
254 b0->error = node->errors [next0 == SPARSE_VEC_INVALID_INDEX ? UDP_ERROR_NO_LISTENER : UDP_ERROR_NONE];
258 b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
259 next0 = UDP_INPUT_NEXT_DROP;
263 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
265 udp_rx_trace_t *tr = vlib_add_trace (vm, node,
267 if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
269 tr->src_port = h0->src_port;
270 tr->dst_port = h0->dst_port;
273 vlib_buffer_advance (b0, sizeof (*h0));
274 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
275 to_next, n_left_to_next,
279 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
281 return from_frame->n_vectors;
284 static char * udp_error_strings[] = {
285 #define udp_error(n,s) s,
286 #include "udp_error.def"
291 udp4_input (vlib_main_t * vm,
292 vlib_node_runtime_t * node,
293 vlib_frame_t * from_frame)
295 return udp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */);
299 udp6_input (vlib_main_t * vm,
300 vlib_node_runtime_t * node,
301 vlib_frame_t * from_frame)
303 return udp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */);
307 VLIB_REGISTER_NODE (udp4_input_node) = {
308 .function = udp4_input,
309 .name = "ip4-udp-lookup",
310 /* Takes a vector of packets. */
311 .vector_size = sizeof (u32),
313 .runtime_data_bytes = sizeof (udp_input_runtime_t),
315 .n_errors = UDP_N_ERROR,
316 .error_strings = udp_error_strings,
318 .n_next_nodes = UDP_INPUT_N_NEXT,
320 #define _(s,n) [UDP_INPUT_NEXT_##s] = n,
321 foreach_udp_input_next
325 .format_buffer = format_udp_header,
326 .format_trace = format_udp_rx_trace,
327 .unformat_buffer = unformat_udp_header,
330 VLIB_REGISTER_NODE (udp6_input_node) = {
331 .function = udp6_input,
332 .name = "ip6-udp-lookup",
333 /* Takes a vector of packets. */
334 .vector_size = sizeof (u32),
336 .runtime_data_bytes = sizeof (udp_input_runtime_t),
338 .n_errors = UDP_N_ERROR,
339 .error_strings = udp_error_strings,
341 .n_next_nodes = UDP_INPUT_N_NEXT,
343 #define _(s,n) [UDP_INPUT_NEXT_##s] = n,
344 foreach_udp_input_next
348 .format_buffer = format_udp_header,
349 .format_trace = format_udp_rx_trace,
350 .unformat_buffer = unformat_udp_header,
353 static void add_dst_port (udp_main_t * um,
354 udp_dst_port_t dst_port,
355 char * dst_port_name, u8 is_ip4)
357 udp_dst_port_info_t * pi;
360 vec_add2 (um->dst_port_infos[is_ip4], pi, 1);
361 i = pi - um->dst_port_infos[is_ip4];
363 pi->name = dst_port_name;
364 pi->dst_port = dst_port;
365 pi->next_index = pi->node_index = ~0;
367 hash_set (um->dst_port_info_by_dst_port[is_ip4], dst_port, i);
370 hash_set_mem (um->dst_port_info_by_name[is_ip4], pi->name, i);
374 udp_register_dst_port (vlib_main_t * vm,
375 udp_dst_port_t dst_port,
376 u32 node_index, u8 is_ip4)
378 udp_main_t * um = &udp_main;
379 udp_dst_port_info_t * pi;
380 udp_input_runtime_t * rt;
385 clib_error_t * error = vlib_call_init_function (vm, udp_local_init);
387 clib_error_report (error);
390 pi = udp_get_dst_port_info (um, dst_port, is_ip4);
393 add_dst_port (um, dst_port, 0, is_ip4);
394 pi = udp_get_dst_port_info (um, dst_port, is_ip4);
398 pi->node_index = node_index;
399 pi->next_index = vlib_node_add_next (vm,
400 is_ip4 ? udp4_input_node.index
401 : udp6_input_node.index,
404 /* Setup udp protocol -> next index sparse vector mapping. */
405 rt = vlib_node_get_runtime_data
406 (vm, is_ip4 ? udp4_input_node.index: udp6_input_node.index);
407 n = sparse_vec_validate (rt->next_by_dst_port,
408 clib_host_to_net_u16 (dst_port));
409 n[0] = pi->next_index;
411 /* Rebuild next index -> sparse index inverse mapping when sparse vector
413 vec_validate (rt->sparse_index_by_next_index, pi->next_index);
414 for (i = 1; i < vec_len (rt->next_by_dst_port); i++)
415 rt->sparse_index_by_next_index[rt->next_by_dst_port[i]] = i;
418 /* Parse a UDP header. */
419 uword unformat_udp_header (unformat_input_t * input, va_list * args)
421 u8 ** result = va_arg (*args, u8 **);
423 __attribute__((unused)) int old_length;
424 u16 src_port, dst_port;
426 /* Allocate space for IP header. */
430 old_length = vec_len (*result);
431 vec_add2 (*result, p, sizeof (ip4_header_t));
435 memset (udp, 0, sizeof (udp[0]));
436 if (unformat (input, "src-port %d dst-port %d",
437 &src_port, &dst_port))
439 udp->src_port = clib_host_to_net_u16 (src_port);
440 udp->dst_port = clib_host_to_net_u16 (dst_port);
447 udp_setup_node (vlib_main_t * vm, u32 node_index)
449 vlib_node_t * n = vlib_get_node (vm, node_index);
450 pg_node_t * pn = pg_get_node (node_index);
452 n->format_buffer = format_udp_header;
453 n->unformat_buffer = unformat_udp_header;
454 pn->unformat_edit = unformat_pg_udp_header;
457 clib_error_t * udp_local_init (vlib_main_t * vm)
459 udp_input_runtime_t * rt;
460 udp_main_t * um = &udp_main;
464 clib_error_t * error;
465 error = vlib_call_init_function (vm, udp_init);
467 clib_error_report (error);
471 for (i = 0; i < 2; i++)
473 um->dst_port_info_by_name[i] = hash_create_string (0, sizeof(uword));
474 um->dst_port_info_by_dst_port[i] = hash_create (0, sizeof(uword));
477 udp_setup_node (vm, udp4_input_node.index);
478 udp_setup_node (vm, udp6_input_node.index);
480 rt = vlib_node_get_runtime_data (vm, udp4_input_node.index);
482 rt->next_by_dst_port = sparse_vec_new
483 (/* elt bytes */ sizeof (rt->next_by_dst_port[0]),
484 /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
486 vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_DROP);
487 vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_PUNT);
488 rt->sparse_index_by_next_index[UDP_INPUT_NEXT_DROP]
489 = SPARSE_VEC_INVALID_INDEX;
490 rt->sparse_index_by_next_index[UDP_INPUT_NEXT_PUNT]
491 = SPARSE_VEC_INVALID_INDEX;
493 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 1 /* is_ip4 */);
494 foreach_udp4_dst_port
497 rt = vlib_node_get_runtime_data (vm, udp6_input_node.index);
499 rt->next_by_dst_port = sparse_vec_new
500 (/* elt bytes */ sizeof (rt->next_by_dst_port[0]),
501 /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
503 vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_DROP);
504 vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_PUNT);
505 rt->sparse_index_by_next_index[UDP_INPUT_NEXT_DROP]
506 = SPARSE_VEC_INVALID_INDEX;
507 rt->sparse_index_by_next_index[UDP_INPUT_NEXT_PUNT]
508 = SPARSE_VEC_INVALID_INDEX;
510 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 0 /* is_ip4 */);
511 foreach_udp6_dst_port
514 ip4_register_protocol (IP_PROTOCOL_UDP, udp4_input_node.index);
515 /* Note: ip6 differs from ip4, UDP is hotwired to ip6-udp-lookup */
519 VLIB_INIT_FUNCTION (udp_local_init);