Initial commit of vpp code.
[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             }
147
148           if (PREDICT_FALSE(b1->current_length < advance1 + sizeof (h1)))
149             {
150               error1 = UDP_ERROR_LENGTH_ERROR;
151               next1 = UDP_INPUT_NEXT_DROP;
152             }
153           else
154             {
155               vlib_buffer_advance (b1, advance1);
156               h1 = vlib_buffer_get_current (b1);
157               error1 = next1 = 0;
158             }
159
160
161           /* Index sparse array with network byte order. */
162           dst_port0 = (error0 == 0) ? h0->dst_port : 0;
163           dst_port1 = (error1 == 0) ? h1->dst_port : 0;
164           sparse_vec_index2 (rt->next_by_dst_port, dst_port0, dst_port1,
165                              &i0, &i1);
166           next0 = (error0 == 0) ? vec_elt(rt->next_by_dst_port, i0) : next0;
167           next1 = (error1 == 0) ? vec_elt(rt->next_by_dst_port, i1) : next1;
168
169           if (PREDICT_TRUE (error0 == 0))
170               b0->error = node->errors[next0 == SPARSE_VEC_INVALID_INDEX ? UDP_ERROR_NO_LISTENER : UDP_ERROR_NONE];
171           if (PREDICT_TRUE (error1 == 0))
172               b1->error = node->errors[next1 == SPARSE_VEC_INVALID_INDEX ? UDP_ERROR_NO_LISTENER : UDP_ERROR_NONE];
173           
174           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
175             {
176               udp_rx_trace_t *tr = vlib_add_trace (vm, node, 
177                                                    b0, sizeof (*tr));
178               if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
179                 {
180                   tr->src_port = h0->src_port;
181                   tr->dst_port = h0->dst_port;
182                 }
183             }
184           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) 
185             {
186               udp_rx_trace_t *tr = vlib_add_trace (vm, node, 
187                                                    b1, sizeof (*tr));
188               if (b1->error != node->errors[UDP_ERROR_LENGTH_ERROR])
189                 {
190                   tr->src_port = h1->src_port;
191                   tr->dst_port = h1->dst_port;
192                 }
193             }
194
195           vlib_buffer_advance (b0, sizeof (*h0));
196           vlib_buffer_advance (b1, sizeof (*h1));
197
198           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
199                                            to_next, n_left_to_next,
200                                            bi0, bi1, next0, next1);
201         }
202     
203       while (n_left_from > 0 && n_left_to_next > 0)
204         {
205           u32 bi0;
206           vlib_buffer_t * b0;
207           udp_header_t * h0 = 0;
208           u32 i0, next0;
209           u32 advance0;
210
211           bi0 = from[0];
212           to_next[0] = bi0;
213           from += 1;
214           to_next += 1;
215           n_left_from -= 1;
216           n_left_to_next -= 1;
217
218           b0 = vlib_get_buffer (vm, bi0);
219
220           /* ip4/6_local hands us the ip header, not the udp header */
221           if (is_ip4) 
222             advance0 = sizeof(ip4_header_t);
223           else
224             advance0 = sizeof(ip6_header_t);
225
226           if (PREDICT_FALSE(b0->current_length < advance0 + sizeof (h0)))
227             {
228               b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
229               next0 = UDP_INPUT_NEXT_DROP;
230               goto trace_x1;
231             }
232
233           vlib_buffer_advance (b0, advance0);
234
235           h0 = vlib_buffer_get_current (b0);
236
237           if (PREDICT_TRUE 
238               (clib_net_to_host_u16(h0->length) <= b0->current_length))
239             {
240               i0 = sparse_vec_index (rt->next_by_dst_port, h0->dst_port);
241               next0 = vec_elt(rt->next_by_dst_port, i0);
242
243               b0->error = node->errors [next0 == SPARSE_VEC_INVALID_INDEX ? UDP_ERROR_NO_LISTENER : UDP_ERROR_NONE];
244             }
245           else
246             {
247               b0->error = node->errors[UDP_ERROR_LENGTH_ERROR];
248               next0 = UDP_INPUT_NEXT_DROP;
249             }
250
251         trace_x1:
252           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
253             {
254               udp_rx_trace_t *tr = vlib_add_trace (vm, node, 
255                                                    b0, sizeof (*tr));
256               if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
257                 {
258                   tr->src_port = h0->src_port;
259                   tr->dst_port = h0->dst_port;
260                 }
261             }
262           vlib_buffer_advance (b0, sizeof (*h0));
263           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
264                                            to_next, n_left_to_next,
265                                            bi0, next0);
266         }
267
268       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
269     }
270   return from_frame->n_vectors;
271 }
272
273 static char * udp_error_strings[] = {
274 #define udp_error(n,s) s,
275 #include "udp_error.def"
276 #undef udp_error
277 };
278
279 static uword
280 udp4_input (vlib_main_t * vm,
281             vlib_node_runtime_t * node,
282             vlib_frame_t * from_frame)
283 {
284   return udp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */);
285 }
286
287 static uword
288 udp6_input (vlib_main_t * vm,
289             vlib_node_runtime_t * node,
290             vlib_frame_t * from_frame)
291 {
292   return udp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */);
293 }
294
295
296 VLIB_REGISTER_NODE (udp4_input_node) = {
297   .function = udp4_input,
298   .name = "ip4-udp-lookup",
299   /* Takes a vector of packets. */
300   .vector_size = sizeof (u32),
301
302   .runtime_data_bytes = sizeof (udp_input_runtime_t),
303
304   .n_errors = UDP_N_ERROR,
305   .error_strings = udp_error_strings,
306
307   .n_next_nodes = UDP_INPUT_N_NEXT,
308   .next_nodes = {
309 #define _(s,n) [UDP_INPUT_NEXT_##s] = n,
310     foreach_udp_input_next
311 #undef _
312   },
313
314   .format_buffer = format_udp_header,
315   .format_trace = format_udp_rx_trace,
316   .unformat_buffer = unformat_udp_header,
317 };
318
319 VLIB_REGISTER_NODE (udp6_input_node) = {
320   .function = udp6_input,
321   .name = "ip6-udp-lookup",
322   /* Takes a vector of packets. */
323   .vector_size = sizeof (u32),
324
325   .runtime_data_bytes = sizeof (udp_input_runtime_t),
326
327   .n_errors = UDP_N_ERROR,
328   .error_strings = udp_error_strings,
329
330   .n_next_nodes = UDP_INPUT_N_NEXT,
331   .next_nodes = {
332 #define _(s,n) [UDP_INPUT_NEXT_##s] = n,
333     foreach_udp_input_next
334 #undef _
335   },
336
337   .format_buffer = format_udp_header,
338   .format_trace = format_udp_rx_trace,
339   .unformat_buffer = unformat_udp_header,
340 };
341
342 static void add_dst_port (udp_main_t * um,
343                           udp_dst_port_t dst_port,
344                           char * dst_port_name, u8 is_ip4)
345 {
346   udp_dst_port_info_t * pi;
347   u32 i;
348
349   vec_add2 (um->dst_port_infos[is_ip4], pi, 1);
350   i = pi - um->dst_port_infos[is_ip4];
351
352   pi->name = dst_port_name;
353   pi->dst_port = dst_port;
354   pi->next_index = pi->node_index = ~0;
355   
356   hash_set (um->dst_port_info_by_dst_port[is_ip4], dst_port, i);
357
358   if (pi->name)
359     hash_set_mem (um->dst_port_info_by_name[is_ip4], pi->name, i);
360 }
361
362 void
363 udp_register_dst_port (vlib_main_t * vm,
364                        udp_dst_port_t dst_port,
365                        u32 node_index, u8 is_ip4)
366 {
367   udp_main_t * um = &udp_main;
368   udp_dst_port_info_t * pi;
369   udp_input_runtime_t * rt;
370   u16 * n;
371   u32 i;
372
373   {
374     clib_error_t * error = vlib_call_init_function (vm, udp_local_init);
375     if (error)
376       clib_error_report (error);
377   }
378
379   pi = udp_get_dst_port_info (um, dst_port, is_ip4);
380   if (! pi) 
381     {
382       add_dst_port (um, dst_port, 0, is_ip4);
383       pi = udp_get_dst_port_info (um, dst_port, is_ip4);
384       ASSERT (pi);
385     }
386       
387   pi->node_index = node_index;
388   pi->next_index = vlib_node_add_next (vm, 
389                                        is_ip4 ? udp4_input_node.index
390                                        : udp6_input_node.index,
391                                        node_index);
392
393   /* Setup udp protocol -> next index sparse vector mapping. */
394   rt = vlib_node_get_runtime_data 
395     (vm, is_ip4 ? udp4_input_node.index: udp6_input_node.index);
396   n = sparse_vec_validate (rt->next_by_dst_port, 
397                            clib_host_to_net_u16 (dst_port));
398   n[0] = pi->next_index;
399
400   /* Rebuild next index -> sparse index inverse mapping when sparse vector
401      is updated. */
402   vec_validate (rt->sparse_index_by_next_index, pi->next_index);
403   for (i = 1; i < vec_len (rt->next_by_dst_port); i++)
404     rt->sparse_index_by_next_index[rt->next_by_dst_port[i]] = i;
405 }
406
407 /* Parse a UDP header. */
408 uword unformat_udp_header (unformat_input_t * input, va_list * args)
409 {
410   u8 ** result = va_arg (*args, u8 **);
411   udp_header_t * udp;
412   __attribute__((unused)) int old_length;
413   u16 src_port, dst_port;
414
415   /* Allocate space for IP header. */
416   {
417     void * p;
418
419     old_length = vec_len (*result);
420     vec_add2 (*result, p, sizeof (ip4_header_t));
421     udp = p;
422   }
423
424   memset (udp, 0, sizeof (udp[0]));
425   if (unformat (input, "src-port %d dst-port %d", 
426                 &src_port, &dst_port)) 
427     {
428       udp->src_port = clib_host_to_net_u16 (src_port);
429       udp->dst_port = clib_host_to_net_u16 (dst_port);
430       return 1;
431     }
432   return 0;
433 }
434
435 static void
436 udp_setup_node (vlib_main_t * vm, u32 node_index)
437 {
438   vlib_node_t * n = vlib_get_node (vm, node_index);
439   pg_node_t * pn = pg_get_node (node_index);
440
441   n->format_buffer = format_udp_header;
442   n->unformat_buffer = unformat_udp_header;
443   pn->unformat_edit = unformat_pg_udp_header;
444 }
445
446 clib_error_t * udp_local_init (vlib_main_t * vm)
447 {
448   udp_input_runtime_t * rt;
449   udp_main_t * um = &udp_main;
450   int i;
451
452   {
453     clib_error_t * error; 
454     error = vlib_call_init_function (vm, udp_init);
455     if (error)
456       clib_error_report (error);
457   }
458
459
460   for (i = 0; i < 2; i++)
461     {
462       um->dst_port_info_by_name[i] = hash_create_string (0, sizeof(uword));
463       um->dst_port_info_by_dst_port[i] = hash_create (0, sizeof(uword));
464     }
465
466   udp_setup_node (vm, udp4_input_node.index);
467   udp_setup_node (vm, udp6_input_node.index);
468
469   rt = vlib_node_get_runtime_data (vm, udp4_input_node.index);
470
471   rt->next_by_dst_port = sparse_vec_new
472     (/* elt bytes */ sizeof (rt->next_by_dst_port[0]),
473      /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
474
475   vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_DROP);
476   vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_PUNT);
477   rt->sparse_index_by_next_index[UDP_INPUT_NEXT_DROP]
478     = SPARSE_VEC_INVALID_INDEX;
479   rt->sparse_index_by_next_index[UDP_INPUT_NEXT_PUNT]
480     = SPARSE_VEC_INVALID_INDEX;
481
482 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 1 /* is_ip4 */);
483   foreach_udp4_dst_port
484 #undef _
485
486   rt = vlib_node_get_runtime_data (vm, udp6_input_node.index);
487
488   rt->next_by_dst_port = sparse_vec_new
489     (/* elt bytes */ sizeof (rt->next_by_dst_port[0]),
490      /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
491
492   vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_DROP);
493   vec_validate (rt->sparse_index_by_next_index, UDP_INPUT_NEXT_PUNT);
494   rt->sparse_index_by_next_index[UDP_INPUT_NEXT_DROP]
495     = SPARSE_VEC_INVALID_INDEX;
496   rt->sparse_index_by_next_index[UDP_INPUT_NEXT_PUNT]
497     = SPARSE_VEC_INVALID_INDEX;
498
499 #define _(n,s) add_dst_port (um, UDP_DST_PORT_##s, #s, 0 /* is_ip4 */);
500   foreach_udp6_dst_port
501 #undef _
502
503   ip4_register_protocol (IP_PROTOCOL_UDP, udp4_input_node.index);
504   /* Note: ip6 differs from ip4, UDP is hotwired to ip6-udp-lookup */
505   return 0;
506 }
507
508 VLIB_INIT_FUNCTION (udp_local_init);