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") \
29 _ (ICMP4_ERROR, "ip4-icmp-error") \
30 _ (ICMP6_ERROR, "ip6-icmp-error")
33 #define _(s,n) UDP_INPUT_NEXT_##s,
34 foreach_udp_input_next
45 u8 * format_udp_rx_trace (u8 * s, va_list * args)
47 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49 udp_rx_trace_t * t = va_arg (*args, udp_rx_trace_t *);
51 s = format (s, "UDP: src-port %d dst-port %d%s",
52 clib_net_to_host_u16(t->src_port),
53 clib_net_to_host_u16(t->dst_port),
54 t->bound ? "" : " (no listener)");
59 /* Sparse vector mapping udp dst_port in network byte order
61 u16 * next_by_dst_port;
63 } udp_input_runtime_t;
65 vlib_node_registration_t udp4_input_node;
66 vlib_node_registration_t udp6_input_node;
69 udp46_input_inline (vlib_main_t * vm,
70 vlib_node_runtime_t * node,
71 vlib_frame_t * from_frame,
74 udp_input_runtime_t * rt = is_ip4 ?
75 (void *) vlib_node_get_runtime_data (vm, udp4_input_node.index)
76 : (void *) vlib_node_get_runtime_data (vm, udp6_input_node.index);
77 __attribute__((unused)) u32 n_left_from, next_index, * from, * to_next;
78 word n_no_listener = 0;
79 u8 punt_unknown = rt->punt_unknown;
81 from = vlib_frame_vector_args (from_frame);
82 n_left_from = from_frame->n_vectors;
84 next_index = node->cached_next_index;
86 while (n_left_from > 0)
90 vlib_get_next_frame (vm, node, next_index,
91 to_next, n_left_to_next);
93 while (n_left_from >= 4 && n_left_to_next >= 2)
96 vlib_buffer_t * b0, * b1;
97 udp_header_t * h0 = 0, * h1 = 0;
98 u32 i0, i1, dst_port0, dst_port1;
99 u32 advance0, advance1;
100 u32 error0, next0, error1, next1;
102 /* Prefetch next iteration. */
104 vlib_buffer_t * p2, * p3;
106 p2 = vlib_get_buffer (vm, from[2]);
107 p3 = vlib_get_buffer (vm, from[3]);
109 vlib_prefetch_buffer_header (p2, LOAD);
110 vlib_prefetch_buffer_header (p3, LOAD);
112 CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
113 CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
125 b0 = vlib_get_buffer (vm, bi0);
126 b1 = vlib_get_buffer (vm, bi1);
128 /* ip4/6_local hands us the ip header, not the udp header */
131 advance0 = sizeof(ip4_header_t);
132 advance1 = sizeof(ip4_header_t);
136 advance0 = sizeof(ip6_header_t);
137 advance1 = sizeof(ip6_header_t);
140 if (PREDICT_FALSE(b0->current_length < advance0 + sizeof (*h0)))
142 error0 = UDP_ERROR_LENGTH_ERROR;
143 next0 = UDP_INPUT_NEXT_DROP;
147 vlib_buffer_advance (b0, advance0);
148 h0 = vlib_buffer_get_current (b0);
150 if (PREDICT_FALSE(clib_net_to_host_u16(h0->length) >
151 vlib_buffer_length_in_chain(vm, b0)))
153 error0 = UDP_ERROR_LENGTH_ERROR;
154 next0 = UDP_INPUT_NEXT_DROP;
158 if (PREDICT_FALSE(b1->current_length < advance1 + sizeof (*h1)))
160 error1 = UDP_ERROR_LENGTH_ERROR;
161 next1 = UDP_INPUT_NEXT_DROP;
165 vlib_buffer_advance (b1, advance1);
166 h1 = vlib_buffer_get_current (b1);
168 if (PREDICT_FALSE(clib_net_to_host_u16(h1->length) >
169 vlib_buffer_length_in_chain(vm, b1)))
171 error1 = UDP_ERROR_LENGTH_ERROR;
172 next1 = UDP_INPUT_NEXT_DROP;
176 /* Index sparse array with network byte order. */
177 dst_port0 = (error0 == 0) ? h0->dst_port : 0;
178 dst_port1 = (error1 == 0) ? h1->dst_port : 0;
179 sparse_vec_index2 (rt->next_by_dst_port, dst_port0, dst_port1,
181 next0 = (error0 == 0) ? vec_elt(rt->next_by_dst_port, i0) : next0;
182 next1 = (error1 == 0) ? vec_elt(rt->next_by_dst_port, i1) : next1;
184 if (PREDICT_FALSE(i0 == SPARSE_VEC_INVALID_INDEX))
186 // move the pointer back so icmp-error can find the
188 vlib_buffer_advance (b0, - (word)advance0);
190 if (PREDICT_FALSE(punt_unknown))
192 b0->error = node->errors[UDP_ERROR_PUNT];
193 next0 = UDP_INPUT_NEXT_PUNT;
197 icmp4_error_set_vnet_buffer(b0, ICMP4_destination_unreachable,
198 ICMP4_destination_unreachable_port_unreachable, 0);
199 next0 = UDP_INPUT_NEXT_ICMP4_ERROR;
204 icmp6_error_set_vnet_buffer(b0, ICMP6_destination_unreachable,
205 ICMP6_destination_unreachable_port_unreachable, 0);
206 next0 = UDP_INPUT_NEXT_ICMP6_ERROR;
212 b0->error = node->errors[UDP_ERROR_NONE];
213 // advance to the payload
214 vlib_buffer_advance (b0, sizeof (*h0));
217 if (PREDICT_FALSE(i1 == SPARSE_VEC_INVALID_INDEX))
219 // move the pointer back so icmp-error can find the
221 vlib_buffer_advance (b1, - (word)advance1);
223 if (PREDICT_FALSE(punt_unknown))
225 b1->error = node->errors[UDP_ERROR_PUNT];
226 next1 = UDP_INPUT_NEXT_PUNT;
230 icmp4_error_set_vnet_buffer(b1, ICMP4_destination_unreachable,
231 ICMP4_destination_unreachable_port_unreachable, 0);
232 next1 = UDP_INPUT_NEXT_ICMP4_ERROR;
237 icmp6_error_set_vnet_buffer(b1, ICMP6_destination_unreachable,
238 ICMP6_destination_unreachable_port_unreachable, 0);
239 next1 = UDP_INPUT_NEXT_ICMP6_ERROR;
245 b1->error = node->errors[UDP_ERROR_NONE];
246 // advance to the payload
247 vlib_buffer_advance (b1, sizeof (*h1));
250 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
252 udp_rx_trace_t *tr = vlib_add_trace (vm, node,
254 if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
256 tr->src_port = h0 ? h0->src_port : 0;
257 tr->dst_port = h0 ? h0->dst_port : 0;
258 tr->bound = (next0 != UDP_INPUT_NEXT_ICMP4_ERROR &&
259 next0 != UDP_INPUT_NEXT_ICMP6_ERROR);
262 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
264 udp_rx_trace_t *tr = vlib_add_trace (vm, node,
266 if (b1->error != node->errors[UDP_ERROR_LENGTH_ERROR])
268 tr->src_port = h1 ? h1->src_port : 0;
269 tr->dst_port = h1 ? h1->dst_port : 0;
270 tr->bound = (next1 != UDP_INPUT_NEXT_ICMP4_ERROR &&
271 next1 != UDP_INPUT_NEXT_ICMP6_ERROR);
275 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
276 to_next, n_left_to_next,
277 bi0, bi1, next0, next1);
280 while (n_left_from > 0 && n_left_to_next > 0)
284 udp_header_t * h0 = 0;
295 b0 = vlib_get_buffer (vm, bi0);
297 /* ip4/6_local hands us the ip header, not the udp header */
299 advance0 = sizeof(ip4_header_t);
301 advance0 = sizeof(ip6_header_t);
303 if (PREDICT_FALSE(b0->current_length < advance0 + sizeof (*h0)))
305 b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
306 next0 = UDP_INPUT_NEXT_DROP;
310 vlib_buffer_advance (b0, advance0);
312 h0 = vlib_buffer_get_current (b0);
314 if (PREDICT_TRUE(clib_net_to_host_u16(h0->length) <=
315 vlib_buffer_length_in_chain(vm, b0)))
317 i0 = sparse_vec_index (rt->next_by_dst_port, h0->dst_port);
318 next0 = vec_elt(rt->next_by_dst_port, i0);
320 if (PREDICT_FALSE(i0 == SPARSE_VEC_INVALID_INDEX))
322 // move the pointer back so icmp-error can find the
324 vlib_buffer_advance (b0, - (word)advance0);
326 if (PREDICT_FALSE(punt_unknown))
328 b0->error = node->errors[UDP_ERROR_PUNT];
329 next0 = UDP_INPUT_NEXT_PUNT;
333 icmp4_error_set_vnet_buffer(b0, ICMP4_destination_unreachable,
334 ICMP4_destination_unreachable_port_unreachable, 0);
335 next0 = UDP_INPUT_NEXT_ICMP4_ERROR;
340 icmp6_error_set_vnet_buffer(b0, ICMP6_destination_unreachable,
341 ICMP6_destination_unreachable_port_unreachable, 0);
342 next0 = UDP_INPUT_NEXT_ICMP6_ERROR;
348 b0->error = node->errors[UDP_ERROR_NONE];
349 // advance to the payload
350 vlib_buffer_advance (b0, sizeof (*h0));
355 b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
356 next0 = UDP_INPUT_NEXT_DROP;
360 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
362 udp_rx_trace_t *tr = vlib_add_trace (vm, node,
364 if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
366 tr->src_port = h0->src_port;
367 tr->dst_port = h0->dst_port;
368 tr->bound = (next0 != UDP_INPUT_NEXT_ICMP4_ERROR &&
369 next0 != UDP_INPUT_NEXT_ICMP6_ERROR);
373 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
374 to_next, n_left_to_next,
378 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
380 vlib_error_count(vm, node->node_index, UDP_ERROR_NO_LISTENER, n_no_listener);
381 return from_frame->n_vectors;
384 static char * udp_error_strings[] = {
385 #define udp_error(n,s) s,
386 #include "udp_error.def"
391 udp4_input (vlib_main_t * vm,
392 vlib_node_runtime_t * node,
393 vlib_frame_t * from_frame)
395 return udp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */);
399 udp6_input (vlib_main_t * vm,
400 vlib_node_runtime_t * node,
401 vlib_frame_t * from_frame)
403 return udp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */);
407 VLIB_REGISTER_NODE (udp4_input_node) = {
408 .function = udp4_input,
409 .name = "ip4-udp-lookup",
410 /* Takes a vector of packets. */
411 .vector_size = sizeof (u32),
413 .runtime_data_bytes = sizeof (udp_input_runtime_t),
415 .n_errors = UDP_N_ERROR,
416 .error_strings = udp_error_strings,
418 .n_next_nodes = UDP_INPUT_N_NEXT,
420 #define _(s,n) [UDP_INPUT_NEXT_##s] = n,
421 foreach_udp_input_next
425 .format_buffer = format_udp_header,
426 .format_trace = format_udp_rx_trace,
427 .unformat_buffer = unformat_udp_header,
430 VLIB_NODE_FUNCTION_MULTIARCH (udp4_input_node, udp4_input)
432 VLIB_REGISTER_NODE (udp6_input_node) = {
433 .function = udp6_input,
434 .name = "ip6-udp-lookup",
435 /* Takes a vector of packets. */
436 .vector_size = sizeof (u32),
438 .runtime_data_bytes = sizeof (udp_input_runtime_t),
440 .n_errors = UDP_N_ERROR,
441 .error_strings = udp_error_strings,
443 .n_next_nodes = UDP_INPUT_N_NEXT,
445 #define _(s,n) [UDP_INPUT_NEXT_##s] = n,
446 foreach_udp_input_next
450 .format_buffer = format_udp_header,
451 .format_trace = format_udp_rx_trace,
452 .unformat_buffer = unformat_udp_header,
455 VLIB_NODE_FUNCTION_MULTIARCH (udp6_input_node, udp6_input)
457 static void add_dst_port (udp_main_t * um,
458 udp_dst_port_t dst_port,
459 char * dst_port_name, u8 is_ip4)
461 udp_dst_port_info_t * pi;
464 vec_add2 (um->dst_port_infos[is_ip4], pi, 1);
465 i = pi - um->dst_port_infos[is_ip4];
467 pi->name = dst_port_name;
468 pi->dst_port = dst_port;
469 pi->next_index = pi->node_index = ~0;
471 hash_set (um->dst_port_info_by_dst_port[is_ip4], dst_port, i);
474 hash_set_mem (um->dst_port_info_by_name[is_ip4], pi->name, i);
478 udp_register_dst_port (vlib_main_t * vm,
479 udp_dst_port_t dst_port,
480 u32 node_index, u8 is_ip4)
482 udp_main_t * um = &udp_main;
483 udp_dst_port_info_t * pi;
484 udp_input_runtime_t * rt;
488 clib_error_t * error = vlib_call_init_function (vm, udp_local_init);
490 clib_error_report (error);
493 pi = udp_get_dst_port_info (um, dst_port, is_ip4);
496 add_dst_port (um, dst_port, 0, is_ip4);
497 pi = udp_get_dst_port_info (um, dst_port, is_ip4);
501 pi->node_index = node_index;
502 pi->next_index = vlib_node_add_next (vm,
503 is_ip4 ? udp4_input_node.index
504 : udp6_input_node.index,
507 /* Setup udp protocol -> next index sparse vector mapping. */
508 rt = vlib_node_get_runtime_data
509 (vm, is_ip4 ? udp4_input_node.index: udp6_input_node.index);
510 n = sparse_vec_validate (rt->next_by_dst_port,
511 clib_host_to_net_u16 (dst_port));
512 n[0] = pi->next_index;
516 udp_punt_unknown(vlib_main_t * vm, u8 is_ip4, u8 is_add)
518 udp_input_runtime_t * rt;
521 clib_error_t * error = vlib_call_init_function (vm, udp_local_init);
523 clib_error_report (error);
526 rt = vlib_node_get_runtime_data
527 (vm, is_ip4 ? udp4_input_node.index: udp6_input_node.index);
529 rt->punt_unknown = is_add;
532 /* Parse a UDP header. */
533 uword unformat_udp_header (unformat_input_t * input, va_list * args)
535 u8 ** result = va_arg (*args, u8 **);
537 __attribute__((unused)) int old_length;
538 u16 src_port, dst_port;
540 /* Allocate space for IP header. */
544 old_length = vec_len (*result);
545 vec_add2 (*result, p, sizeof (ip4_header_t));
549 memset (udp, 0, sizeof (udp[0]));
550 if (unformat (input, "src-port %d dst-port %d",
551 &src_port, &dst_port))
553 udp->src_port = clib_host_to_net_u16 (src_port);
554 udp->dst_port = clib_host_to_net_u16 (dst_port);
561 udp_setup_node (vlib_main_t * vm, u32 node_index)
563 vlib_node_t * n = vlib_get_node (vm, node_index);
564 pg_node_t * pn = pg_get_node (node_index);
566 n->format_buffer = format_udp_header;
567 n->unformat_buffer = unformat_udp_header;
568 pn->unformat_edit = unformat_pg_udp_header;
571 clib_error_t * udp_local_init (vlib_main_t * vm)
573 udp_input_runtime_t * rt;
574 udp_main_t * um = &udp_main;
578 clib_error_t * error;
579 error = vlib_call_init_function (vm, udp_init);
581 clib_error_report (error);
585 for (i = 0; i < 2; i++)
587 um->dst_port_info_by_name[i] = hash_create_string (0, sizeof(uword));
588 um->dst_port_info_by_dst_port[i] = hash_create (0, sizeof(uword));
591 udp_setup_node (vm, udp4_input_node.index);
592 udp_setup_node (vm, udp6_input_node.index);
594 rt = vlib_node_get_runtime_data (vm, udp4_input_node.index);
596 rt->next_by_dst_port = sparse_vec_new
597 (/* elt bytes */ sizeof (rt->next_by_dst_port[0]),
598 /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
600 rt->punt_unknown = 0;
602 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 1 /* is_ip4 */);
603 foreach_udp4_dst_port
606 rt = vlib_node_get_runtime_data (vm, udp6_input_node.index);
608 rt->next_by_dst_port = sparse_vec_new
609 (/* elt bytes */ sizeof (rt->next_by_dst_port[0]),
610 /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
612 rt->punt_unknown = 0;
614 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 0 /* is_ip4 */);
615 foreach_udp6_dst_port
618 ip4_register_protocol (IP_PROTOCOL_UDP, udp4_input_node.index);
619 /* Note: ip6 differs from ip4, UDP is hotwired to ip6-udp-lookup */
623 VLIB_INIT_FUNCTION (udp_local_init);