udp: refactor udp code
[vpp.git] / src / vnet / udp / udp_local.c
1 /*
2  * node.c: udp packet processing
3  *
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:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/udp/udp.h>
21 #include <vnet/udp/udp_packet.h>
22 #include <vppinfra/sparse_vec.h>
23
24 udp_main_t udp_main;
25
26 #define foreach_udp_local_next                  \
27   _ (PUNT, "error-punt")                        \
28   _ (DROP, "error-drop")                        \
29   _ (ICMP4_ERROR, "ip4-icmp-error")             \
30   _ (ICMP6_ERROR, "ip6-icmp-error")
31
32 typedef enum
33 {
34 #define _(s,n) UDP_LOCAL_NEXT_##s,
35   foreach_udp_local_next
36 #undef _
37     UDP_LOCAL_N_NEXT,
38 } udp_local_next_t;
39
40 typedef struct
41 {
42   u16 src_port;
43   u16 dst_port;
44   u8 bound;
45 } udp_local_rx_trace_t;
46
47 u8 *
48 format_udp_rx_trace (u8 * s, va_list * args)
49 {
50   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
51   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
52   udp_local_rx_trace_t *t = va_arg (*args, udp_local_rx_trace_t *);
53
54   s = format (s, "UDP: src-port %d dst-port %d%s",
55               clib_net_to_host_u16 (t->src_port),
56               clib_net_to_host_u16 (t->dst_port),
57               t->bound ? "" : " (no listener)");
58   return s;
59 }
60
61 vlib_node_registration_t udp4_local_node;
62 vlib_node_registration_t udp6_local_node;
63
64 always_inline uword
65 udp46_local_inline (vlib_main_t * vm,
66                     vlib_node_runtime_t * node,
67                     vlib_frame_t * from_frame, int is_ip4)
68 {
69   udp_main_t *um = &udp_main;
70   __attribute__ ((unused)) u32 n_left_from, next_index, *from, *to_next;
71   word n_no_listener = 0;
72   u8 punt_unknown = is_ip4 ? um->punt_unknown4 : um->punt_unknown6;
73
74   from = vlib_frame_vector_args (from_frame);
75   n_left_from = from_frame->n_vectors;
76
77   next_index = node->cached_next_index;
78
79   while (n_left_from > 0)
80     {
81       u32 n_left_to_next;
82
83       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
84
85       while (n_left_from >= 4 && n_left_to_next >= 2)
86         {
87           u32 bi0, bi1;
88           vlib_buffer_t *b0, *b1;
89           udp_header_t *h0 = 0, *h1 = 0;
90           u32 i0, i1, dst_port0, dst_port1;
91           u32 advance0, advance1;
92           u32 error0, next0, error1, next1;
93
94           /* Prefetch next iteration. */
95           {
96             vlib_buffer_t *p2, *p3;
97
98             p2 = vlib_get_buffer (vm, from[2]);
99             p3 = vlib_get_buffer (vm, from[3]);
100
101             vlib_prefetch_buffer_header (p2, LOAD);
102             vlib_prefetch_buffer_header (p3, LOAD);
103
104             CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
105             CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
106           }
107
108           bi0 = from[0];
109           bi1 = from[1];
110           to_next[0] = bi0;
111           to_next[1] = bi1;
112           from += 2;
113           to_next += 2;
114           n_left_to_next -= 2;
115           n_left_from -= 2;
116
117           b0 = vlib_get_buffer (vm, bi0);
118           b1 = vlib_get_buffer (vm, bi1);
119
120           /* ip4/6_local hands us the ip header, not the udp header */
121           if (is_ip4)
122             {
123               advance0 = sizeof (ip4_header_t);
124               advance1 = sizeof (ip4_header_t);
125             }
126           else
127             {
128               advance0 = sizeof (ip6_header_t);
129               advance1 = sizeof (ip6_header_t);
130             }
131
132           if (PREDICT_FALSE (b0->current_length < advance0 + sizeof (*h0)))
133             {
134               error0 = UDP_ERROR_LENGTH_ERROR;
135               next0 = UDP_LOCAL_NEXT_DROP;
136             }
137           else
138             {
139               vlib_buffer_advance (b0, advance0);
140               h0 = vlib_buffer_get_current (b0);
141               error0 = next0 = 0;
142               if (PREDICT_FALSE (clib_net_to_host_u16 (h0->length) >
143                                  vlib_buffer_length_in_chain (vm, b0)))
144                 {
145                   error0 = UDP_ERROR_LENGTH_ERROR;
146                   next0 = UDP_LOCAL_NEXT_DROP;
147                 }
148             }
149
150           if (PREDICT_FALSE (b1->current_length < advance1 + sizeof (*h1)))
151             {
152               error1 = UDP_ERROR_LENGTH_ERROR;
153               next1 = UDP_LOCAL_NEXT_DROP;
154             }
155           else
156             {
157               vlib_buffer_advance (b1, advance1);
158               h1 = vlib_buffer_get_current (b1);
159               error1 = next1 = 0;
160               if (PREDICT_FALSE (clib_net_to_host_u16 (h1->length) >
161                                  vlib_buffer_length_in_chain (vm, b1)))
162                 {
163                   error1 = UDP_ERROR_LENGTH_ERROR;
164                   next1 = UDP_LOCAL_NEXT_DROP;
165                 }
166             }
167
168           /* Index sparse array with network byte order. */
169           dst_port0 = (error0 == 0) ? h0->dst_port : 0;
170           dst_port1 = (error1 == 0) ? h1->dst_port : 0;
171           sparse_vec_index2 (is_ip4 ? um->next_by_dst_port4 :
172                              um->next_by_dst_port6,
173                              dst_port0, dst_port1, &i0, &i1);
174           next0 = (error0 == 0) ?
175             vec_elt (is_ip4 ? um->next_by_dst_port4 : um->next_by_dst_port6,
176                      i0) : next0;
177           next1 = (error1 == 0) ?
178             vec_elt (is_ip4 ? um->next_by_dst_port4 : um->next_by_dst_port6,
179                      i1) : next1;
180
181           if (PREDICT_FALSE (i0 == SPARSE_VEC_INVALID_INDEX))
182             {
183               // move the pointer back so icmp-error can find the
184               // ip packet header
185               vlib_buffer_advance (b0, -(word) advance0);
186
187               if (PREDICT_FALSE (punt_unknown))
188                 {
189                   b0->error = node->errors[UDP_ERROR_PUNT];
190                   next0 = UDP_LOCAL_NEXT_PUNT;
191                 }
192               else if (is_ip4)
193                 {
194                   icmp4_error_set_vnet_buffer (b0,
195                                                ICMP4_destination_unreachable,
196                                                ICMP4_destination_unreachable_port_unreachable,
197                                                0);
198                   next0 = UDP_LOCAL_NEXT_ICMP4_ERROR;
199                   n_no_listener++;
200                 }
201               else
202                 {
203                   icmp6_error_set_vnet_buffer (b0,
204                                                ICMP6_destination_unreachable,
205                                                ICMP6_destination_unreachable_port_unreachable,
206                                                0);
207                   next0 = UDP_LOCAL_NEXT_ICMP6_ERROR;
208                   n_no_listener++;
209                 }
210             }
211           else
212             {
213               b0->error = node->errors[UDP_ERROR_NONE];
214               // advance to the payload
215               vlib_buffer_advance (b0, sizeof (*h0));
216             }
217
218           if (PREDICT_FALSE (i1 == SPARSE_VEC_INVALID_INDEX))
219             {
220               // move the pointer back so icmp-error can find the
221               // ip packet header
222               vlib_buffer_advance (b1, -(word) advance1);
223
224               if (PREDICT_FALSE (punt_unknown))
225                 {
226                   b1->error = node->errors[UDP_ERROR_PUNT];
227                   next1 = UDP_LOCAL_NEXT_PUNT;
228                 }
229               else if (is_ip4)
230                 {
231                   icmp4_error_set_vnet_buffer (b1,
232                                                ICMP4_destination_unreachable,
233                                                ICMP4_destination_unreachable_port_unreachable,
234                                                0);
235                   next1 = UDP_LOCAL_NEXT_ICMP4_ERROR;
236                   n_no_listener++;
237                 }
238               else
239                 {
240                   icmp6_error_set_vnet_buffer (b1,
241                                                ICMP6_destination_unreachable,
242                                                ICMP6_destination_unreachable_port_unreachable,
243                                                0);
244                   next1 = UDP_LOCAL_NEXT_ICMP6_ERROR;
245                   n_no_listener++;
246                 }
247             }
248           else
249             {
250               b1->error = node->errors[UDP_ERROR_NONE];
251               // advance to the payload
252               vlib_buffer_advance (b1, sizeof (*h1));
253             }
254
255           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
256             {
257               udp_local_rx_trace_t *tr = vlib_add_trace (vm, node,
258                                                          b0, sizeof (*tr));
259               if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
260                 {
261                   tr->src_port = h0 ? h0->src_port : 0;
262                   tr->dst_port = h0 ? h0->dst_port : 0;
263                   tr->bound = (next0 != UDP_LOCAL_NEXT_ICMP4_ERROR &&
264                                next0 != UDP_LOCAL_NEXT_ICMP6_ERROR);
265                 }
266             }
267           if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
268             {
269               udp_local_rx_trace_t *tr = vlib_add_trace (vm, node,
270                                                          b1, sizeof (*tr));
271               if (b1->error != node->errors[UDP_ERROR_LENGTH_ERROR])
272                 {
273                   tr->src_port = h1 ? h1->src_port : 0;
274                   tr->dst_port = h1 ? h1->dst_port : 0;
275                   tr->bound = (next1 != UDP_LOCAL_NEXT_ICMP4_ERROR &&
276                                next1 != UDP_LOCAL_NEXT_ICMP6_ERROR);
277                 }
278             }
279
280           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
281                                            to_next, n_left_to_next,
282                                            bi0, bi1, next0, next1);
283         }
284
285       while (n_left_from > 0 && n_left_to_next > 0)
286         {
287           u32 bi0;
288           vlib_buffer_t *b0;
289           udp_header_t *h0 = 0;
290           u32 i0, next0;
291           u32 advance0;
292
293           bi0 = from[0];
294           to_next[0] = bi0;
295           from += 1;
296           to_next += 1;
297           n_left_from -= 1;
298           n_left_to_next -= 1;
299
300           b0 = vlib_get_buffer (vm, bi0);
301
302           /* ip4/6_local hands us the ip header, not the udp header */
303           if (is_ip4)
304             advance0 = sizeof (ip4_header_t);
305           else
306             advance0 = sizeof (ip6_header_t);
307
308           if (PREDICT_FALSE (b0->current_length < advance0 + sizeof (*h0)))
309             {
310               b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
311               next0 = UDP_LOCAL_NEXT_DROP;
312               goto trace_x1;
313             }
314
315           vlib_buffer_advance (b0, advance0);
316
317           h0 = vlib_buffer_get_current (b0);
318
319           if (PREDICT_TRUE (clib_net_to_host_u16 (h0->length) <=
320                             vlib_buffer_length_in_chain (vm, b0)))
321             {
322               i0 = sparse_vec_index (is_ip4 ? um->next_by_dst_port4 :
323                                      um->next_by_dst_port6, h0->dst_port);
324               next0 = vec_elt (is_ip4 ? um->next_by_dst_port4 :
325                                um->next_by_dst_port6, i0);
326
327               if (PREDICT_FALSE (i0 == SPARSE_VEC_INVALID_INDEX))
328                 {
329                   // move the pointer back so icmp-error can find the
330                   // ip packet header
331                   vlib_buffer_advance (b0, -(word) advance0);
332
333                   if (PREDICT_FALSE (punt_unknown))
334                     {
335                       b0->error = node->errors[UDP_ERROR_PUNT];
336                       next0 = UDP_LOCAL_NEXT_PUNT;
337                     }
338                   else if (is_ip4)
339                     {
340                       icmp4_error_set_vnet_buffer (b0,
341                                                    ICMP4_destination_unreachable,
342                                                    ICMP4_destination_unreachable_port_unreachable,
343                                                    0);
344                       next0 = UDP_LOCAL_NEXT_ICMP4_ERROR;
345                       n_no_listener++;
346                     }
347                   else
348                     {
349                       icmp6_error_set_vnet_buffer (b0,
350                                                    ICMP6_destination_unreachable,
351                                                    ICMP6_destination_unreachable_port_unreachable,
352                                                    0);
353                       next0 = UDP_LOCAL_NEXT_ICMP6_ERROR;
354                       n_no_listener++;
355                     }
356                 }
357               else
358                 {
359                   b0->error = node->errors[UDP_ERROR_NONE];
360                   // advance to the payload
361                   vlib_buffer_advance (b0, sizeof (*h0));
362                 }
363             }
364           else
365             {
366               b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
367               next0 = UDP_LOCAL_NEXT_DROP;
368             }
369
370         trace_x1:
371           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
372             {
373               udp_local_rx_trace_t *tr = vlib_add_trace (vm, node,
374                                                          b0, sizeof (*tr));
375               if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
376                 {
377                   tr->src_port = h0->src_port;
378                   tr->dst_port = h0->dst_port;
379                   tr->bound = (next0 != UDP_LOCAL_NEXT_ICMP4_ERROR &&
380                                next0 != UDP_LOCAL_NEXT_ICMP6_ERROR);
381                 }
382             }
383
384           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
385                                            to_next, n_left_to_next,
386                                            bi0, next0);
387         }
388
389       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
390     }
391   vlib_error_count (vm, node->node_index, UDP_ERROR_NO_LISTENER,
392                     n_no_listener);
393   return from_frame->n_vectors;
394 }
395
396 static char *udp_error_strings[] = {
397 #define udp_error(n,s) s,
398 #include "udp_error.def"
399 #undef udp_error
400 };
401
402 static uword
403 udp4_local (vlib_main_t * vm,
404             vlib_node_runtime_t * node, vlib_frame_t * from_frame)
405 {
406   return udp46_local_inline (vm, node, from_frame, 1 /* is_ip4 */ );
407 }
408
409 static uword
410 udp6_local (vlib_main_t * vm,
411             vlib_node_runtime_t * node, vlib_frame_t * from_frame)
412 {
413   return udp46_local_inline (vm, node, from_frame, 0 /* is_ip4 */ );
414 }
415
416 /* *INDENT-OFF* */
417 VLIB_REGISTER_NODE (udp4_local_node) = {
418   .function = udp4_local,
419   .name = "ip4-udp-lookup",
420   /* Takes a vector of packets. */
421   .vector_size = sizeof (u32),
422
423   .n_errors = UDP_N_ERROR,
424   .error_strings = udp_error_strings,
425
426   .n_next_nodes = UDP_LOCAL_N_NEXT,
427   .next_nodes = {
428 #define _(s,n) [UDP_LOCAL_NEXT_##s] = n,
429     foreach_udp_local_next
430 #undef _
431   },
432
433   .format_buffer = format_udp_header,
434   .format_trace = format_udp_rx_trace,
435   .unformat_buffer = unformat_udp_header,
436 };
437 /* *INDENT-ON* */
438
439 VLIB_NODE_FUNCTION_MULTIARCH (udp4_local_node, udp4_local);
440
441 /* *INDENT-OFF* */
442 VLIB_REGISTER_NODE (udp6_local_node) = {
443   .function = udp6_local,
444   .name = "ip6-udp-lookup",
445   /* Takes a vector of packets. */
446   .vector_size = sizeof (u32),
447
448   .n_errors = UDP_N_ERROR,
449   .error_strings = udp_error_strings,
450
451   .n_next_nodes = UDP_LOCAL_N_NEXT,
452   .next_nodes = {
453 #define _(s,n) [UDP_LOCAL_NEXT_##s] = n,
454     foreach_udp_local_next
455 #undef _
456   },
457
458   .format_buffer = format_udp_header,
459   .format_trace = format_udp_rx_trace,
460   .unformat_buffer = unformat_udp_header,
461 };
462 /* *INDENT-ON* */
463
464 VLIB_NODE_FUNCTION_MULTIARCH (udp6_local_node, udp6_local);
465
466 static void
467 add_dst_port (udp_main_t * um,
468               udp_dst_port_t dst_port, char *dst_port_name, u8 is_ip4)
469 {
470   udp_dst_port_info_t *pi;
471   u32 i;
472
473   vec_add2 (um->dst_port_infos[is_ip4], pi, 1);
474   i = pi - um->dst_port_infos[is_ip4];
475
476   pi->name = dst_port_name;
477   pi->dst_port = dst_port;
478   pi->next_index = pi->node_index = ~0;
479
480   hash_set (um->dst_port_info_by_dst_port[is_ip4], dst_port, i);
481
482   if (pi->name)
483     hash_set_mem (um->dst_port_info_by_name[is_ip4], pi->name, i);
484 }
485
486 void
487 udp_register_dst_port (vlib_main_t * vm,
488                        udp_dst_port_t dst_port, u32 node_index, u8 is_ip4)
489 {
490   udp_main_t *um = &udp_main;
491   udp_dst_port_info_t *pi;
492   u16 *n;
493
494   {
495     clib_error_t *error = vlib_call_init_function (vm, udp_local_init);
496     if (error)
497       clib_error_report (error);
498   }
499
500   pi = udp_get_dst_port_info (um, dst_port, is_ip4);
501   if (!pi)
502     {
503       add_dst_port (um, dst_port, 0, is_ip4);
504       pi = udp_get_dst_port_info (um, dst_port, is_ip4);
505       ASSERT (pi);
506     }
507
508   pi->node_index = node_index;
509   pi->next_index = vlib_node_add_next (vm,
510                                        is_ip4 ? udp4_local_node.index
511                                        : udp6_local_node.index, node_index);
512
513   /* Setup udp protocol -> next index sparse vector mapping. */
514   if (is_ip4)
515     n = sparse_vec_validate (um->next_by_dst_port4,
516                              clib_host_to_net_u16 (dst_port));
517   else
518     n = sparse_vec_validate (um->next_by_dst_port6,
519                              clib_host_to_net_u16 (dst_port));
520
521   n[0] = pi->next_index;
522 }
523
524 void
525 udp_unregister_dst_port (vlib_main_t * vm, udp_dst_port_t dst_port, u8 is_ip4)
526 {
527   udp_main_t *um = &udp_main;
528   udp_dst_port_info_t *pi;
529   u16 *n;
530
531   pi = udp_get_dst_port_info (um, dst_port, is_ip4);
532   /* Not registered? Fagedaboudit */
533   if (!pi)
534     return;
535
536   /* Kill the mapping. Don't bother killing the pi, it may be back. */
537   if (is_ip4)
538     n = sparse_vec_validate (um->next_by_dst_port4,
539                              clib_host_to_net_u16 (dst_port));
540   else
541     n = sparse_vec_validate (um->next_by_dst_port6,
542                              clib_host_to_net_u16 (dst_port));
543
544   n[0] = SPARSE_VEC_INVALID_INDEX;
545 }
546
547 void
548 udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add)
549 {
550   udp_main_t *um = &udp_main;
551   {
552     clib_error_t *error = vlib_call_init_function (vm, udp_local_init);
553     if (error)
554       clib_error_report (error);
555   }
556
557   if (is_ip4)
558     um->punt_unknown4 = is_add;
559   else
560     um->punt_unknown6 = is_add;
561 }
562
563 /* Parse a UDP header. */
564 uword
565 unformat_udp_header (unformat_input_t * input, va_list * args)
566 {
567   u8 **result = va_arg (*args, u8 **);
568   udp_header_t *udp;
569   __attribute__ ((unused)) int old_length;
570   u16 src_port, dst_port;
571
572   /* Allocate space for IP header. */
573   {
574     void *p;
575
576     old_length = vec_len (*result);
577     vec_add2 (*result, p, sizeof (ip4_header_t));
578     udp = p;
579   }
580
581   memset (udp, 0, sizeof (udp[0]));
582   if (unformat (input, "src-port %d dst-port %d", &src_port, &dst_port))
583     {
584       udp->src_port = clib_host_to_net_u16 (src_port);
585       udp->dst_port = clib_host_to_net_u16 (dst_port);
586       return 1;
587     }
588   return 0;
589 }
590
591 static void
592 udp_setup_node (vlib_main_t * vm, u32 node_index)
593 {
594   vlib_node_t *n = vlib_get_node (vm, node_index);
595   pg_node_t *pn = pg_get_node (node_index);
596
597   n->format_buffer = format_udp_header;
598   n->unformat_buffer = unformat_udp_header;
599   pn->unformat_edit = unformat_pg_udp_header;
600 }
601
602 clib_error_t *
603 udp_local_init (vlib_main_t * vm)
604 {
605   udp_main_t *um = &udp_main;
606   int i;
607
608   {
609     clib_error_t *error;
610     error = vlib_call_init_function (vm, udp_init);
611     if (error)
612       clib_error_report (error);
613   }
614
615
616   for (i = 0; i < 2; i++)
617     {
618       um->dst_port_info_by_name[i] = hash_create_string (0, sizeof (uword));
619       um->dst_port_info_by_dst_port[i] = hash_create (0, sizeof (uword));
620     }
621
622   udp_setup_node (vm, udp4_local_node.index);
623   udp_setup_node (vm, udp6_local_node.index);
624
625   um->punt_unknown4 = 0;
626   um->punt_unknown6 = 0;
627
628   um->next_by_dst_port4 = sparse_vec_new
629     ( /* elt bytes */ sizeof (um->next_by_dst_port4[0]),
630      /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
631
632   um->next_by_dst_port6 = sparse_vec_new
633     ( /* elt bytes */ sizeof (um->next_by_dst_port6[0]),
634      /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
635
636 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 1 /* is_ip4 */);
637   foreach_udp4_dst_port
638 #undef _
639 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 0 /* is_ip4 */);
640     foreach_udp6_dst_port
641 #undef _
642     ip4_register_protocol (IP_PROTOCOL_UDP, udp4_local_node.index);
643   /* Note: ip6 differs from ip4, UDP is hotwired to ip6-udp-lookup */
644   return 0;
645 }
646
647 VLIB_INIT_FUNCTION (udp_local_init);
648
649 /*
650  * fd.io coding-style-patch-verification: ON
651  *
652  * Local Variables:
653  * eval: (c-set-style "gnu")
654  * End:
655  */