Add support for multiple microarchitectures in single binary
[vpp.git] / vnet / vnet / ip / 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/ip/udp.h>
21 #include <vnet/ip/udp_packet.h>
22 #include <vppinfra/sparse_vec.h>
23
24 udp_main_t udp_main;
25
26 #define foreach_udp_input_next                  \
27   _ (PUNT, "error-punt")                        \
28   _ (DROP, "error-drop")
29
30 typedef enum {
31 #define _(s,n) UDP_INPUT_NEXT_##s,
32   foreach_udp_input_next
33 #undef _
34   UDP_INPUT_N_NEXT,
35 } udp_input_next_t;
36
37 typedef struct {
38   u16 src_port;
39   u16 dst_port;
40 } udp_rx_trace_t;
41
42 u8 * format_udp_rx_trace (u8 * s, va_list * args)
43 {
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 *);
47     
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));
51   return s;
52 }
53
54 typedef struct {
55   /* Sparse vector mapping udp dst_port in network byte order
56      to next index. */
57   u16 * next_by_dst_port;
58
59   u32 * sparse_index_by_next_index;
60 } udp_input_runtime_t;
61
62 vlib_node_registration_t udp4_input_node;
63 vlib_node_registration_t udp6_input_node;
64
65 always_inline uword
66 udp46_input_inline (vlib_main_t * vm,
67                     vlib_node_runtime_t * node,
68                     vlib_frame_t * from_frame,
69                     int is_ip4)
70 {
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;
75
76   from = vlib_frame_vector_args (from_frame);
77   n_left_from = from_frame->n_vectors;
78
79   next_index = node->cached_next_index;
80   i_next = vec_elt (rt->sparse_index_by_next_index, next_index);
81
82   while (n_left_from > 0)
83     {
84       u32 n_left_to_next;
85
86       vlib_get_next_frame (vm, node, next_index,
87                            to_next, n_left_to_next);
88
89       while (n_left_from >= 4 && n_left_to_next >= 2)
90         {
91           u32 bi0, bi1;
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;
97
98           /* Prefetch next iteration. */
99           {
100             vlib_buffer_t * p2, * p3;
101
102             p2 = vlib_get_buffer (vm, from[2]);
103             p3 = vlib_get_buffer (vm, from[3]);
104
105             vlib_prefetch_buffer_header (p2, LOAD);
106             vlib_prefetch_buffer_header (p3, LOAD);
107
108             CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
109             CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
110           }
111
112           bi0 = from[0];
113           bi1 = from[1];
114           to_next[0] = bi0;
115           to_next[1] = bi1;
116           from += 2;
117           to_next += 2;
118           n_left_to_next -= 2;
119           n_left_from -= 2;
120
121           b0 = vlib_get_buffer (vm, bi0);
122           b1 = vlib_get_buffer (vm, bi1);
123
124           /* ip4/6_local hands us the ip header, not the udp header */
125           if (is_ip4) 
126             {
127               advance0 = sizeof(ip4_header_t);
128               advance1 = sizeof(ip4_header_t);
129             }
130           else
131             {
132               advance0 = sizeof(ip6_header_t);
133               advance1 = sizeof(ip6_header_t);
134             }          
135
136           if (PREDICT_FALSE(b0->current_length < advance0 + sizeof (*h0)))
137             {
138               error0 = UDP_ERROR_LENGTH_ERROR;
139               next0 = UDP_INPUT_NEXT_DROP;
140             }
141           else
142             {
143               vlib_buffer_advance (b0, advance0);
144               h0 = vlib_buffer_get_current (b0);
145               error0 = next0 = 0;
146               if (PREDICT_FALSE(clib_net_to_host_u16(h0->length) >
147                                 vlib_buffer_length_in_chain(vm, b0)))
148                 {
149                   error0 = UDP_ERROR_LENGTH_ERROR;
150                   next0 = UDP_INPUT_NEXT_DROP;
151                 }
152             }
153
154           if (PREDICT_FALSE(b1->current_length < advance1 + sizeof (*h1)))
155             {
156               error1 = UDP_ERROR_LENGTH_ERROR;
157               next1 = UDP_INPUT_NEXT_DROP;
158             }
159           else
160             {
161               vlib_buffer_advance (b1, advance1);
162               h1 = vlib_buffer_get_current (b1);
163               error1 = next1 = 0;
164               if (PREDICT_FALSE(clib_net_to_host_u16(h1->length) >
165                             vlib_buffer_length_in_chain(vm, b1)))
166                 {
167                   error1 = UDP_ERROR_LENGTH_ERROR;
168                   next1 = UDP_INPUT_NEXT_DROP;
169                 }
170             }
171
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,
176                              &i0, &i1);
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;
179
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];
184           
185           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
186             {
187               udp_rx_trace_t *tr = vlib_add_trace (vm, node, 
188                                                    b0, sizeof (*tr));
189               if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
190                 {
191                   tr->src_port = h0->src_port;
192                   tr->dst_port = h0->dst_port;
193                 }
194             }
195           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) 
196             {
197               udp_rx_trace_t *tr = vlib_add_trace (vm, node, 
198                                                    b1, sizeof (*tr));
199               if (b1->error != node->errors[UDP_ERROR_LENGTH_ERROR])
200                 {
201                   tr->src_port = h1->src_port;
202                   tr->dst_port = h1->dst_port;
203                 }
204             }
205
206           vlib_buffer_advance (b0, sizeof (*h0));
207           vlib_buffer_advance (b1, sizeof (*h1));
208
209           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
210                                            to_next, n_left_to_next,
211                                            bi0, bi1, next0, next1);
212         }
213     
214       while (n_left_from > 0 && n_left_to_next > 0)
215         {
216           u32 bi0;
217           vlib_buffer_t * b0;
218           udp_header_t * h0 = 0;
219           u32 i0, next0;
220           u32 advance0;
221
222           bi0 = from[0];
223           to_next[0] = bi0;
224           from += 1;
225           to_next += 1;
226           n_left_from -= 1;
227           n_left_to_next -= 1;
228
229           b0 = vlib_get_buffer (vm, bi0);
230
231           /* ip4/6_local hands us the ip header, not the udp header */
232           if (is_ip4) 
233             advance0 = sizeof(ip4_header_t);
234           else
235             advance0 = sizeof(ip6_header_t);
236
237           if (PREDICT_FALSE(b0->current_length < advance0 + sizeof (*h0)))
238             {
239               b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
240               next0 = UDP_INPUT_NEXT_DROP;
241               goto trace_x1;
242             }
243
244           vlib_buffer_advance (b0, advance0);
245
246           h0 = vlib_buffer_get_current (b0);
247
248           if (PREDICT_TRUE(clib_net_to_host_u16(h0->length) <= 
249                            vlib_buffer_length_in_chain(vm, b0)))
250             {
251               i0 = sparse_vec_index (rt->next_by_dst_port, h0->dst_port);
252               next0 = vec_elt(rt->next_by_dst_port, i0);
253
254               b0->error = node->errors [next0 == SPARSE_VEC_INVALID_INDEX ? UDP_ERROR_NO_LISTENER : UDP_ERROR_NONE];
255             }
256           else
257             {
258               b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
259               next0 = UDP_INPUT_NEXT_DROP;
260             }
261
262         trace_x1:
263           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
264             {
265               udp_rx_trace_t *tr = vlib_add_trace (vm, node, 
266                                                    b0, sizeof (*tr));
267               if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
268                 {
269                   tr->src_port = h0->src_port;
270                   tr->dst_port = h0->dst_port;
271                 }
272             }
273           vlib_buffer_advance (b0, sizeof (*h0));
274           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
275                                            to_next, n_left_to_next,
276                                            bi0, next0);
277         }
278
279       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
280     }
281   return from_frame->n_vectors;
282 }
283
284 static char * udp_error_strings[] = {
285 #define udp_error(n,s) s,
286 #include "udp_error.def"
287 #undef udp_error
288 };
289
290 static uword
291 udp4_input (vlib_main_t * vm,
292             vlib_node_runtime_t * node,
293             vlib_frame_t * from_frame)
294 {
295   return udp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */);
296 }
297
298 static uword
299 udp6_input (vlib_main_t * vm,
300             vlib_node_runtime_t * node,
301             vlib_frame_t * from_frame)
302 {
303   return udp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */);
304 }
305
306
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),
312
313   .runtime_data_bytes = sizeof (udp_input_runtime_t),
314
315   .n_errors = UDP_N_ERROR,
316   .error_strings = udp_error_strings,
317
318   .n_next_nodes = UDP_INPUT_N_NEXT,
319   .next_nodes = {
320 #define _(s,n) [UDP_INPUT_NEXT_##s] = n,
321     foreach_udp_input_next
322 #undef _
323   },
324
325   .format_buffer = format_udp_header,
326   .format_trace = format_udp_rx_trace,
327   .unformat_buffer = unformat_udp_header,
328 };
329
330 VLIB_NODE_FUNCTION_MULTIARCH (udp4_input_node, udp4_input)
331
332 VLIB_REGISTER_NODE (udp6_input_node) = {
333   .function = udp6_input,
334   .name = "ip6-udp-lookup",
335   /* Takes a vector of packets. */
336   .vector_size = sizeof (u32),
337
338   .runtime_data_bytes = sizeof (udp_input_runtime_t),
339
340   .n_errors = UDP_N_ERROR,
341   .error_strings = udp_error_strings,
342
343   .n_next_nodes = UDP_INPUT_N_NEXT,
344   .next_nodes = {
345 #define _(s,n) [UDP_INPUT_NEXT_##s] = n,
346     foreach_udp_input_next
347 #undef _
348   },
349
350   .format_buffer = format_udp_header,
351   .format_trace = format_udp_rx_trace,
352   .unformat_buffer = unformat_udp_header,
353 };
354
355 VLIB_NODE_FUNCTION_MULTIARCH (udp6_input_node, udp6_input)
356
357 static void add_dst_port (udp_main_t * um,
358                           udp_dst_port_t dst_port,
359                           char * dst_port_name, u8 is_ip4)
360 {
361   udp_dst_port_info_t * pi;
362   u32 i;
363
364   vec_add2 (um->dst_port_infos[is_ip4], pi, 1);
365   i = pi - um->dst_port_infos[is_ip4];
366
367   pi->name = dst_port_name;
368   pi->dst_port = dst_port;
369   pi->next_index = pi->node_index = ~0;
370   
371   hash_set (um->dst_port_info_by_dst_port[is_ip4], dst_port, i);
372
373   if (pi->name)
374     hash_set_mem (um->dst_port_info_by_name[is_ip4], pi->name, i);
375 }
376
377 void
378 udp_register_dst_port (vlib_main_t * vm,
379                        udp_dst_port_t dst_port,
380                        u32 node_index, u8 is_ip4)
381 {
382   udp_main_t * um = &udp_main;
383   udp_dst_port_info_t * pi;
384   udp_input_runtime_t * rt;
385   u16 * n;
386   u32 i;
387
388   {
389     clib_error_t * error = vlib_call_init_function (vm, udp_local_init);
390     if (error)
391       clib_error_report (error);
392   }
393
394   pi = udp_get_dst_port_info (um, dst_port, is_ip4);
395   if (! pi) 
396     {
397       add_dst_port (um, dst_port, 0, is_ip4);
398       pi = udp_get_dst_port_info (um, dst_port, is_ip4);
399       ASSERT (pi);
400     }
401       
402   pi->node_index = node_index;
403   pi->next_index = vlib_node_add_next (vm, 
404                                        is_ip4 ? udp4_input_node.index
405                                        : udp6_input_node.index,
406                                        node_index);
407
408   /* Setup udp protocol -> next index sparse vector mapping. */
409   rt = vlib_node_get_runtime_data 
410     (vm, is_ip4 ? udp4_input_node.index: udp6_input_node.index);
411   n = sparse_vec_validate (rt->next_by_dst_port, 
412                            clib_host_to_net_u16 (dst_port));
413   n[0] = pi->next_index;
414
415   /* Rebuild next index -> sparse index inverse mapping when sparse vector
416      is updated. */
417   vec_validate (rt->sparse_index_by_next_index, pi->next_index);
418   for (i = 1; i < vec_len (rt->next_by_dst_port); i++)
419     rt->sparse_index_by_next_index[rt->next_by_dst_port[i]] = i;
420 }
421
422 /* Parse a UDP header. */
423 uword unformat_udp_header (unformat_input_t * input, va_list * args)
424 {
425   u8 ** result = va_arg (*args, u8 **);
426   udp_header_t * udp;
427   __attribute__((unused)) int old_length;
428   u16 src_port, dst_port;
429
430   /* Allocate space for IP header. */
431   {
432     void * p;
433
434     old_length = vec_len (*result);
435     vec_add2 (*result, p, sizeof (ip4_header_t));
436     udp = p;
437   }
438
439   memset (udp, 0, sizeof (udp[0]));
440   if (unformat (input, "src-port %d dst-port %d", 
441                 &src_port, &dst_port)) 
442     {
443       udp->src_port = clib_host_to_net_u16 (src_port);
444       udp->dst_port = clib_host_to_net_u16 (dst_port);
445       return 1;
446     }
447   return 0;
448 }
449
450 static void
451 udp_setup_node (vlib_main_t * vm, u32 node_index)
452 {
453   vlib_node_t * n = vlib_get_node (vm, node_index);
454   pg_node_t * pn = pg_get_node (node_index);
455
456   n->format_buffer = format_udp_header;
457   n->unformat_buffer = unformat_udp_header;
458   pn->unformat_edit = unformat_pg_udp_header;
459 }
460
461 clib_error_t * udp_local_init (vlib_main_t * vm)
462 {
463   udp_input_runtime_t * rt;
464   udp_main_t * um = &udp_main;
465   int i;
466
467   {
468     clib_error_t * error; 
469     error = vlib_call_init_function (vm, udp_init);
470     if (error)
471       clib_error_report (error);
472   }
473
474
475   for (i = 0; i < 2; i++)
476     {
477       um->dst_port_info_by_name[i] = hash_create_string (0, sizeof(uword));
478       um->dst_port_info_by_dst_port[i] = hash_create (0, sizeof(uword));
479     }
480
481   udp_setup_node (vm, udp4_input_node.index);
482   udp_setup_node (vm, udp6_input_node.index);
483
484   rt = vlib_node_get_runtime_data (vm, udp4_input_node.index);
485
486   rt->next_by_dst_port = sparse_vec_new
487     (/* elt bytes */ sizeof (rt->next_by_dst_port[0]),
488      /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
489
490   vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_DROP);
491   vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_PUNT);
492   rt->sparse_index_by_next_index[UDP_INPUT_NEXT_DROP]
493     = SPARSE_VEC_INVALID_INDEX;
494   rt->sparse_index_by_next_index[UDP_INPUT_NEXT_PUNT]
495     = SPARSE_VEC_INVALID_INDEX;
496
497 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 1 /* is_ip4 */);
498   foreach_udp4_dst_port
499 #undef _
500
501   rt = vlib_node_get_runtime_data (vm, udp6_input_node.index);
502
503   rt->next_by_dst_port = sparse_vec_new
504     (/* elt bytes */ sizeof (rt->next_by_dst_port[0]),
505      /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
506
507   vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_DROP);
508   vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_PUNT);
509   rt->sparse_index_by_next_index[UDP_INPUT_NEXT_DROP]
510     = SPARSE_VEC_INVALID_INDEX;
511   rt->sparse_index_by_next_index[UDP_INPUT_NEXT_PUNT]
512     = SPARSE_VEC_INVALID_INDEX;
513
514 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 0 /* is_ip4 */);
515   foreach_udp6_dst_port
516 #undef _
517
518   ip4_register_protocol (IP_PROTOCOL_UDP, udp4_input_node.index);
519   /* Note: ip6 differs from ip4, UDP is hotwired to ip6-udp-lookup */
520   return 0;
521 }
522
523 VLIB_INIT_FUNCTION (udp_local_init);