misc: remove GNU Indent directives
[vpp.git] / src / plugins / ioam / udp-ping / udp_ping_node.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/vnet.h>
17 #include <vlib/vlib.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20
21 #include <vnet/ip/ip.h>
22 #include <vnet/ip/ip6_hop_by_hop.h>
23 #include <ioam/encap/ip6_ioam_trace.h>
24 #include <ioam/encap/ip6_ioam_e2e.h>
25 #include <ioam/udp-ping/udp_ping_packet.h>
26 #include <ioam/udp-ping/udp_ping.h>
27 #include <ioam/udp-ping/udp_ping_util.h>
28 #include <vnet/srv6/sr_packet.h>
29
30 typedef enum
31 {
32   UDP_PING_NEXT_DROP,
33   UDP_PING_NEXT_PUNT,
34   UDP_PING_NEXT_UDP_LOOKUP,
35   UDP_PING_NEXT_ICMP,
36   UDP_PING_NEXT_IP6_LOOKUP,
37   UDP_PING_NEXT_IP6_DROP,
38   UDP_PING_N_NEXT,
39 } udp_ping_next_t;
40
41 #define foreach_udp_ping_error                  \
42 _(BADHBH, "Malformed hop-by-hop header")
43
44 typedef enum
45 {
46 #define _(sym,str) UDP_PING_ERROR_##sym,
47   foreach_udp_ping_error
48 #undef _
49     UDP_PING_N_ERROR,
50 } udp_ping_error_t;
51
52 static char *udp_ping_error_strings[] = {
53 #define _(sym,string) string,
54   foreach_udp_ping_error
55 #undef _
56 };
57
58 udp_ping_main_t udp_ping_main;
59
60 uword
61 udp_ping_process (vlib_main_t * vm,
62                   vlib_node_runtime_t * rt, vlib_frame_t * f);
63
64 extern int
65 ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t * b, ip6_header_t * ip,
66                                       ip6_hop_by_hop_option_t * opt);
67
68 typedef struct
69 {
70   ip6_address_t src;
71   ip6_address_t dst;
72   u16 src_port;
73   u16 dst_port;
74   u16 handle;
75   u16 next_index;
76   u8 msg_type;
77 } udp_ping_trace_t;
78
79 /* packet trace format function */
80 static u8 *
81 format_udp_ping_trace (u8 * s, va_list * args)
82 {
83   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
84   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
85   udp_ping_trace_t *t = va_arg (*args, udp_ping_trace_t *);
86
87   s = format (s, "udp-ping-local: src %U, dst %U, src_port %u, dst_port %u "
88               "handle %u, next_index %u, msg_type %u",
89               format_ip6_address, &t->src,
90               format_ip6_address, &t->dst,
91               t->src_port, t->dst_port,
92               t->handle, t->next_index, t->msg_type);
93   return s;
94 }
95
96 VLIB_REGISTER_NODE (udp_ping_node, static) =
97 {
98   .function = udp_ping_process,
99   .type = VLIB_NODE_TYPE_PROCESS,
100   .name = "udp-ping-process",
101 };
102
103 void
104 udp_ping_calculate_timer_interval (void)
105 {
106   int i;
107   ip46_udp_ping_flow *flow = NULL;
108   u16 min_interval = 0x1e9;
109
110   for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
111     {
112       if (pool_is_free_index (udp_ping_main.ip46_flow, i))
113         continue;
114
115       flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
116
117       if (min_interval > flow->udp_data.interval)
118         min_interval = flow->udp_data.interval;
119     }
120
121   if (udp_ping_main.timer_interval != min_interval)
122     {
123       udp_ping_main.timer_interval = min_interval;
124       vlib_process_signal_event (udp_ping_main.vlib_main,
125                                  udp_ping_node.index, EVENT_SIG_RECHECK, 0);
126     }
127 }
128
129 void
130 ip46_udp_ping_set_flow (ip46_address_t src, ip46_address_t dst,
131                         u16 start_src_port, u16 end_src_port,
132                         u16 start_dst_port, u16 end_dst_port,
133                         u16 interval, u8 fault_det, u8 is_disable)
134 {
135   u8 found = 0;
136   ip46_udp_ping_flow *flow = NULL;
137   int i;
138
139   for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
140     {
141       if (pool_is_free_index (udp_ping_main.ip46_flow, i))
142         continue;
143
144       flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
145       if ((0 == udp_ping_compare_flow (src, dst,
146                                        start_src_port, end_src_port,
147                                        start_dst_port, end_dst_port, flow)))
148         {
149           found = 1;
150           break;
151         }
152     }
153
154   if (found)
155     {
156       u16 cur_interval;
157       if (is_disable)
158         {
159           cur_interval = flow->udp_data.interval;
160           udp_ping_free_flow_data (flow);
161           pool_put_index (udp_ping_main.ip46_flow, i);
162           if (udp_ping_main.timer_interval == interval)
163             udp_ping_calculate_timer_interval ();
164           return;
165         }
166
167       cur_interval = flow->udp_data.interval;
168       flow->udp_data.interval = interval;
169       if (udp_ping_main.timer_interval > interval)
170         {
171           udp_ping_main.timer_interval = interval;
172           vlib_process_signal_event (udp_ping_main.vlib_main,
173                                      udp_ping_node.index,
174                                      EVENT_SIG_RECHECK, 0);
175         }
176       else if (udp_ping_main.timer_interval == cur_interval)
177         udp_ping_calculate_timer_interval ();
178
179       return;
180     }
181
182   /* Delete operation and item not found */
183   if (is_disable)
184     return;
185
186   /* Alloc new session */
187   pool_get_aligned (udp_ping_main.ip46_flow, flow, CLIB_CACHE_LINE_BYTES);
188   udp_ping_populate_flow (src, dst,
189                           start_src_port, end_src_port,
190                           start_dst_port, end_dst_port,
191                           interval, fault_det, flow);
192
193   udp_ping_create_rewrite (flow, (flow - udp_ping_main.ip46_flow));
194
195   if (udp_ping_main.timer_interval > interval)
196     {
197       udp_ping_main.timer_interval = interval;
198       vlib_process_signal_event (udp_ping_main.vlib_main,
199                                  udp_ping_node.index, EVENT_SIG_RECHECK, 0);
200     }
201   return;
202 }
203
204 uword
205 unformat_port_range (unformat_input_t * input, va_list * args)
206 {
207   u16 *start_port, *end_port;
208   uword c;
209   u8 colon_present = 0;
210
211   start_port = va_arg (*args, u16 *);
212   end_port = va_arg (*args, u16 *);
213
214   *start_port = *end_port = 0;
215   /* Get start port */
216   while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
217     {
218       switch (c)
219         {
220         case '0' ... '9':
221           *start_port = ((*start_port) * 10) + (c - '0');
222           break;
223
224         case ':':
225           colon_present = 1;
226           break;
227
228         default:
229           return 0;
230         }
231
232       if (colon_present)
233         break;
234     }
235
236   if (!colon_present)
237     return 0;
238
239   /* Get end port */
240   while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
241     {
242       switch (c)
243         {
244         case '0' ... '9':
245           *end_port = ((*end_port) * 10) + (c - '0');
246           break;
247
248         default:
249           return 1;
250         }
251     }
252
253   if (end_port < start_port)
254     return 0;
255
256   return 1;
257 }
258
259 static clib_error_t *
260 set_udp_ping_command_fn (vlib_main_t * vm,
261                          unformat_input_t * input, vlib_cli_command_t * cmd)
262 {
263   ip46_address_t dst, src;
264   u16 start_src_port, end_src_port;
265   u16 start_dst_port, end_dst_port;
266   u32 interval;
267   u8 is_disable = 0;
268   u8 fault_det = 0;
269
270   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
271     {
272       if (unformat
273           (input, "src %U", unformat_ip46_address, &src, IP46_TYPE_ANY))
274         ;
275       else if (unformat (input, "src-port-range %U",
276                          unformat_port_range, &start_src_port, &end_src_port))
277         ;
278       else
279         if (unformat
280             (input, "dst %U", unformat_ip46_address, &dst, IP46_TYPE_ANY))
281         ;
282       else if (unformat (input, "dst-port-range %U",
283                          unformat_port_range, &start_dst_port, &end_dst_port))
284         ;
285       else if (unformat (input, "interval %d", &interval))
286         ;
287       else if (unformat (input, "fault-detect"))
288         fault_det = 1;
289       else if (unformat (input, "disable"))
290         is_disable = 1;
291       else
292         break;
293     }
294
295   ip46_udp_ping_set_flow (src, dst, start_src_port, end_src_port,
296                           start_dst_port, end_dst_port, (u16) interval,
297                           fault_det, is_disable);
298
299   return 0;
300 }
301
302 VLIB_CLI_COMMAND (set_udp_ping_command, static) =
303 {
304   .path = "set udp-ping",
305   .short_help =
306       "set udp-ping src <local IPv6 address>  src-port-range <local port range> \
307       dst <remote IPv6 address> dst-port-range <destination port range> \
308       interval <time interval in sec for which ping packet will be sent> \
309       [disable]",
310   .function = set_udp_ping_command_fn,
311 };
312
313 static clib_error_t *
314 show_udp_ping_summary_cmd_fn (vlib_main_t * vm,
315                               unformat_input_t * input,
316                               vlib_cli_command_t * cmd)
317 {
318   u8 *s = 0;
319   int i, j;
320   ip46_udp_ping_flow *ip46_flow;
321   u16 src_port, dst_port;
322   udp_ping_flow_data *stats;
323
324   s = format (s, "UDP-Ping data:\n");
325
326   for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
327     {
328       if (pool_is_free_index (udp_ping_main.ip46_flow, i))
329         continue;
330
331       ip46_flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
332       s = format (s, "Src: %U, Dst: %U\n",
333                   format_ip46_address, &ip46_flow->src, IP46_TYPE_ANY,
334                   format_ip46_address, &ip46_flow->dst, IP46_TYPE_ANY);
335
336       s = format (s, "Start src port: %u, End src port: %u\n",
337                   ip46_flow->udp_data.start_src_port,
338                   ip46_flow->udp_data.end_src_port);
339       s = format (s, "Start dst port: %u, End dst port: %u\n",
340                   ip46_flow->udp_data.start_dst_port,
341                   ip46_flow->udp_data.end_dst_port);
342       s = format (s, "Interval: %u\n", ip46_flow->udp_data.interval);
343
344       j = 0;
345       for (src_port = ip46_flow->udp_data.start_src_port;
346            src_port <= ip46_flow->udp_data.end_src_port; src_port++)
347         {
348           for (dst_port = ip46_flow->udp_data.start_dst_port;
349                dst_port <= ip46_flow->udp_data.end_dst_port; dst_port++)
350             {
351               stats = ip46_flow->udp_data.stats + j;
352               s =
353                 format (s, "\nSrc Port - %u, Dst Port - %u, Flow CTX - %u\n",
354                         src_port, dst_port, stats->flow_ctx);
355               s =
356                 format (s, "Path State - %s\n",
357                         (stats->retry > MAX_PING_RETRIES) ? "Down" : "Up");
358               s = format (s, "Path Data:\n");
359               s = print_analyse_flow (s,
360                                       &ip46_flow->udp_data.
361                                       stats[j].analyse_data);
362               j++;
363             }
364         }
365       s = format (s, "\n\n");
366     }
367
368   vlib_cli_output (vm, "%v", s);
369   vec_free (s);
370   return 0;
371 }
372
373 VLIB_CLI_COMMAND (show_udp_ping_cmd, static) =
374 {
375   .path = "show udp-ping summary",
376   .short_help = "Summary of udp-ping",
377   .function = show_udp_ping_summary_cmd_fn,
378 };
379
380 /**
381  * @brief UDP-Ping Process node.
382  * @node udp-ping-process
383  *
384  * This is process node which wakes up when periodically to send
385  * out udp probe packets for all configured sessions.
386  *
387  * @param vm    vlib_main_t corresponding to the current thread.
388  * @param rt    vlib_node_runtime_t data for this node.
389  * @param f     vlib_frame_t whose contents should be dispatched.
390  *
391  */
392 uword
393 udp_ping_process (vlib_main_t * vm,
394                   vlib_node_runtime_t * rt, vlib_frame_t * f)
395 {
396   f64 now;
397   uword *event_data = 0;
398   int i;
399   ip46_udp_ping_flow *ip46_flow;
400
401   while (1)
402     {
403       vec_reset_length (event_data);
404       vlib_process_wait_for_event_or_clock (vm, udp_ping_main.timer_interval);
405       (void) vlib_process_get_events (vm, &event_data);
406       now = vlib_time_now (vm);
407
408       for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
409         {
410           if (pool_is_free_index (udp_ping_main.ip46_flow, i))
411             continue;
412
413           ip46_flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
414           if (ip46_flow->udp_data.next_send_time < now)
415             udp_ping_send_ip6_pak (udp_ping_main.vlib_main, ip46_flow);
416         }
417     }
418   return 0;
419 }
420
421 /**
422  * @brief HopByHop analyse function for udp-ping response.
423  *
424  * Walks through all hbh options present in udp-ping response
425  * and uses analyser library for the analysis.
426  *
427  */
428 void
429 udp_ping_analyse_hbh (vlib_buffer_t * b0,
430                       u32 flow_id,
431                       u16 src_port,
432                       u16 dst_port,
433                       ip6_hop_by_hop_option_t * opt0,
434                       ip6_hop_by_hop_option_t * limit0, u16 len)
435 {
436   u8 type0;
437   ip46_udp_ping_flow *ip46_flow;
438   u16 flow_index;
439   ioam_analyser_data_t *data;
440   ioam_e2e_option_t *e2e;
441   ioam_trace_option_t *trace;
442
443   /* If the packet doesnt match UDP session then return */
444   if (PREDICT_FALSE (pool_is_free_index (udp_ping_main.ip46_flow, flow_id)))
445     return;
446
447   ip46_flow = udp_ping_main.ip46_flow + flow_id;
448   /* Check port is within range */
449   if (PREDICT_FALSE ((src_port < ip46_flow->udp_data.start_src_port) ||
450                      (src_port > ip46_flow->udp_data.end_src_port) ||
451                      (dst_port < ip46_flow->udp_data.start_dst_port) ||
452                      (dst_port > ip46_flow->udp_data.end_dst_port)))
453     return;
454
455   flow_index = (src_port - ip46_flow->udp_data.start_src_port) *
456     (ip46_flow->udp_data.end_dst_port - ip46_flow->udp_data.start_dst_port +
457      1);
458   flow_index += (dst_port - ip46_flow->udp_data.start_dst_port);
459   data = &(ip46_flow->udp_data.stats[flow_index].analyse_data);
460
461   data->pkt_counter++;
462   data->bytes_counter += len;
463
464   vnet_buffer (b0)->l2_classify.opaque_index =
465     ip46_flow->udp_data.stats[flow_index].flow_ctx;
466
467   while (opt0 < limit0)
468     {
469       type0 = opt0->type;
470       switch (type0)
471         {
472         case HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST:
473           /* Add trace for here as it hasnt been done yet */
474           vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
475           trace = (ioam_trace_option_t *) opt0;
476           if (PREDICT_FALSE
477               (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK_REPLY))
478             {
479               ip6_ioam_analyse_hbh_trace_loopback (data, &trace->trace_hdr,
480                                                    (trace->hdr.length - 2));
481               return;
482             }
483           ip6_hbh_ioam_trace_data_list_handler (b0,
484                                                 vlib_buffer_get_current (b0),
485                                                 opt0);
486           (void) ip6_ioam_analyse_hbh_trace (data, &trace->trace_hdr, len,
487                                              (trace->hdr.length - 2));
488           break;
489         case HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE:
490           e2e = (ioam_e2e_option_t *) opt0;
491           (void) ip6_ioam_analyse_hbh_e2e (data, &e2e->e2e_hdr, len);
492           break;
493         case 0:         /* Pad1 */
494           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
495           continue;
496         case 1:         /* PadN */
497           break;
498         default:
499           break;
500         }
501       opt0 = (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
502                                           sizeof (ip6_hop_by_hop_option_t));
503     }
504   ip46_flow->udp_data.stats[flow_index].retry = 0;
505 }
506
507 /**
508  * @brief UDP-Ping request/response handler function.
509  *
510  * Checks udp-ping packet type - request/response and handles them.
511  * If not udp-ping packet then, strips off hbh options and enques
512  * packet to protocol registered node to enable next protocol processing.
513  *
514  */
515 void
516 udp_ping_local_analyse (vlib_node_runtime_t * node, vlib_buffer_t * b0,
517                         ip6_header_t * ip0, ip6_hop_by_hop_header_t * hbh0,
518                         u16 * next0)
519 {
520   ip6_main_t *im = &ip6_main;
521   ip_lookup_main_t *lm = &im->lookup_main;
522
523   *next0 = UDP_PING_NEXT_IP6_DROP;
524
525   /*
526    * Sanity check: hbh header length must be less than
527    * b0->current_length.
528    */
529   if (PREDICT_FALSE ((hbh0->length + 1) << 3) >= b0->current_length)
530     {
531       *next0 = UDP_PING_NEXT_DROP;
532       b0->error = node->errors[UDP_PING_ERROR_BADHBH];
533       return;
534     }
535
536   if (PREDICT_TRUE (hbh0->protocol == IP_PROTOCOL_UDP))
537     {
538       ip6_hop_by_hop_option_t *opt0;
539       ip6_hop_by_hop_option_t *limit0;
540       u16 p_len0;
541       udp_ping_t *udp0;
542
543       /* Check for udp ping packet */
544       udp0 = (udp_ping_t *) ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
545       opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
546       if ((udp0->ping_data.probe_marker1 ==
547            clib_host_to_net_u32 (UDP_PING_PROBE_MARKER1)) &&
548           (udp0->ping_data.probe_marker2 ==
549            clib_host_to_net_u32 (UDP_PING_PROBE_MARKER2)))
550         {
551           if (udp0->ping_data.msg_type == UDP_PING_PROBE)
552             {
553               udp_ping_create_reply_from_probe_ip6 (ip0, hbh0, udp0);
554               /* Skip e2e processing */
555               vnet_buffer (b0)->l2_classify.opaque_index = 0x7FFFFFFF;
556               *next0 = UDP_PING_NEXT_IP6_LOOKUP;
557               return;
558             }
559
560           /* Reply */
561           opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
562           limit0 = (ip6_hop_by_hop_option_t *)
563             ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
564           p_len0 = clib_net_to_host_u16 (ip0->payload_length);
565           udp_ping_analyse_hbh (b0,
566                                 clib_net_to_host_u16 (udp0->
567                                                       ping_data.sender_handle),
568                                 clib_net_to_host_u16 (udp0->udp.dst_port),
569                                 clib_net_to_host_u16 (udp0->udp.src_port),
570                                 opt0, limit0, p_len0);
571
572           /* UDP Ping packet, so return */
573           return;
574         }
575     }
576
577   /* If next header is SR, then destination may get overwritten to
578    * remote address. So pass it to SR processing as it may be local packet
579    * afterall
580    */
581   if (PREDICT_FALSE (hbh0->protocol == IPPROTO_IPV6_ROUTE))
582     goto end;
583
584   /* Other case remove hbh-ioam headers */
585   u64 *copy_dst0, *copy_src0;
586   u16 new_l0;
587
588   vlib_buffer_advance (b0, (hbh0->length + 1) << 3);
589
590   new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
591     ((hbh0->length + 1) << 3);
592
593   ip0->payload_length = clib_host_to_net_u16 (new_l0);
594
595   ip0->protocol = hbh0->protocol;
596
597   copy_src0 = (u64 *) ip0;
598   copy_dst0 = copy_src0 + (hbh0->length + 1);
599   copy_dst0[4] = copy_src0[4];
600   copy_dst0[3] = copy_src0[3];
601   copy_dst0[2] = copy_src0[2];
602   copy_dst0[1] = copy_src0[1];
603   copy_dst0[0] = copy_src0[0];
604
605 end:
606   *next0 = lm->local_next_by_ip_protocol[hbh0->protocol];
607   return;
608 }
609
610 /**
611  * @brief udp ping request/response packet receive node.
612  * @node udp-ping-local
613  *
614  * This function receives udp ping request/response packets and process them.
615  * For request packets, response is created and sent.
616  * For response packets, they are analysed and results stored.
617  *
618  * @param vm    vlib_main_t corresponding to the current thread.
619  * @param node  vlib_node_runtime_t data for this node.
620  * @param frame vlib_frame_t whose contents should be dispatched.
621  *
622  * @par Graph mechanics: buffer, next index usage
623  *
624  * <em>Uses:</em>
625  * - <code>udp_ping_local_analyse(node, p0, ip0, hbh0, &next0)</code>
626  *     - Checks packet type - request/respnse and process them.
627  *
628  * <em>Next Index:</em>
629  * - Dispatches the packet to ip6-lookup/ip6-drop depending on type of packet.
630  */
631 static uword
632 udp_ping_local_node_fn (vlib_main_t * vm,
633                         vlib_node_runtime_t * node, vlib_frame_t * frame)
634 {
635   udp_ping_next_t next_index;
636   u32 *from, *to_next, n_left_from, n_left_to_next;
637
638   from = vlib_frame_vector_args (frame);
639   n_left_from = frame->n_vectors;
640   next_index = node->cached_next_index;
641
642   while (n_left_from > 0)
643     {
644       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
645
646       while (n_left_from >= 4 && n_left_to_next >= 2)
647         {
648           vlib_buffer_t *p0, *p1;
649           ip6_header_t *ip0, *ip1;
650           ip6_hop_by_hop_header_t *hbh0, *hbh1;
651           u16 next0, next1;
652           u32 pi0, pi1;
653
654           /* Prefetch next iteration. */
655           {
656             vlib_buffer_t *p2, *p3;
657
658             p2 = vlib_get_buffer (vm, from[2]);
659             p3 = vlib_get_buffer (vm, from[3]);
660
661             vlib_prefetch_buffer_header (p2, LOAD);
662             vlib_prefetch_buffer_header (p3, LOAD);
663
664             /* Prefetch 3 cache lines as we need to look deep into packet */
665             CLIB_PREFETCH (p2->data, 3 * CLIB_CACHE_LINE_BYTES, STORE);
666             CLIB_PREFETCH (p3->data, 3 * CLIB_CACHE_LINE_BYTES, STORE);
667           }
668
669           pi0 = to_next[0] = from[0];
670           pi1 = to_next[1] = from[1];
671           from += 2;
672           n_left_from -= 2;
673           to_next += 2;
674           n_left_to_next -= 2;
675
676           p0 = vlib_get_buffer (vm, pi0);
677           p1 = vlib_get_buffer (vm, pi1);
678
679           ip0 = vlib_buffer_get_current (p0);
680           ip1 = vlib_buffer_get_current (p1);
681
682           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
683           hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
684
685           udp_ping_local_analyse (node, p0, ip0, hbh0, &next0);
686           udp_ping_local_analyse (node, p1, ip1, hbh1, &next1);
687
688           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
689             {
690               if (p0->flags & VLIB_BUFFER_IS_TRACED)
691                 {
692                   udp_ping_trace_t *t0 =
693                     vlib_add_trace (vm, node, p0, sizeof (*t0));
694                   udp_ping_t *udp0;
695
696                   /* Check for udp ping packet */
697                   udp0 =
698                     (udp_ping_t *) ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
699                   t0->src = ip0->src_address;
700                   t0->dst = ip0->dst_address;
701                   t0->src_port = clib_net_to_host_u16 (udp0->udp.src_port);
702                   t0->dst_port = clib_net_to_host_u16 (udp0->udp.dst_port);
703                   t0->handle =
704                     clib_net_to_host_u16 (udp0->ping_data.sender_handle);
705                   t0->msg_type = udp0->ping_data.msg_type;
706                   t0->next_index = next0;
707                 }
708               if (p1->flags & VLIB_BUFFER_IS_TRACED)
709                 {
710                   udp_ping_trace_t *t1 =
711                     vlib_add_trace (vm, node, p1, sizeof (*t1));
712                   udp_ping_t *udp1;
713
714                   /* Check for udp ping packet */
715                   udp1 =
716                     (udp_ping_t *) ((u8 *) hbh1 + ((hbh1->length + 1) << 3));
717                   t1->src = ip1->src_address;
718                   t1->dst = ip1->dst_address;
719                   t1->src_port = clib_net_to_host_u16 (udp1->udp.src_port);
720                   t1->dst_port = clib_net_to_host_u16 (udp1->udp.dst_port);
721                   t1->handle =
722                     clib_net_to_host_u16 (udp1->ping_data.sender_handle);
723                   t1->msg_type = udp1->ping_data.msg_type;
724                   t1->next_index = next1;
725                 }
726             }
727
728           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
729                                            to_next, n_left_to_next,
730                                            pi0, pi1, next0, next1);
731         }
732
733       while (n_left_from > 0 && n_left_to_next > 0)
734         {
735           vlib_buffer_t *p0;
736           ip6_header_t *ip0;
737           ip6_hop_by_hop_header_t *hbh0;
738           u16 next0;
739           u32 pi0;
740
741           pi0 = from[0];
742           to_next[0] = pi0;
743           from += 1;
744           to_next += 1;
745           n_left_from -= 1;
746           n_left_to_next -= 1;
747
748           p0 = vlib_get_buffer (vm, pi0);
749           ip0 = vlib_buffer_get_current (p0);
750           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
751
752           udp_ping_local_analyse (node, p0, ip0, hbh0, &next0);
753
754           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
755             {
756               if (p0->flags & VLIB_BUFFER_IS_TRACED)
757                 {
758                   udp_ping_trace_t *t0 =
759                     vlib_add_trace (vm, node, p0, sizeof (*t0));
760                   udp_ping_t *udp0;
761
762                   /* Check for udp ping packet */
763                   udp0 =
764                     (udp_ping_t *) ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
765                   t0->src = ip0->src_address;
766                   t0->dst = ip0->dst_address;
767                   t0->src_port = clib_net_to_host_u16 (udp0->udp.src_port);
768                   t0->dst_port = clib_net_to_host_u16 (udp0->udp.dst_port);
769                   t0->handle =
770                     clib_net_to_host_u16 (udp0->ping_data.sender_handle);
771                   t0->msg_type = udp0->ping_data.msg_type;
772                   t0->next_index = next0;
773                 }
774             }
775
776           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
777                                            to_next, n_left_to_next,
778                                            pi0, next0);
779         }
780
781       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
782     }
783
784   return frame->n_vectors;
785 }
786
787 /*
788  * Node for udp-ping-local
789  */
790 VLIB_REGISTER_NODE (udp_ping_local, static) =
791 {
792   .function = udp_ping_local_node_fn,
793   .name = "udp-ping-local",
794   .vector_size = sizeof (u32),
795   .format_trace = format_udp_ping_trace,
796   .type = VLIB_NODE_TYPE_INTERNAL,
797   .n_next_nodes = UDP_PING_N_NEXT,
798   .n_errors = UDP_PING_N_ERROR,
799   .error_strings = udp_ping_error_strings,
800   .next_nodes =
801     {
802       [UDP_PING_NEXT_DROP] = "error-drop",
803       [UDP_PING_NEXT_PUNT] = "error-punt",
804       [UDP_PING_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
805       [UDP_PING_NEXT_ICMP] = "ip6-icmp-input",
806       [UDP_PING_NEXT_IP6_LOOKUP] = "ip6-lookup",
807       [UDP_PING_NEXT_IP6_DROP] = "ip6-drop",
808     },
809 };
810
811 static clib_error_t *
812 udp_ping_init (vlib_main_t * vm)
813 {
814   udp_ping_main.vlib_main = vm;
815   udp_ping_main.vnet_main = vnet_get_main ();
816   udp_ping_main.timer_interval = 1e9;
817
818   ip6_local_hop_by_hop_register_protocol (IP_PROTOCOL_UDP,
819                                           udp_ping_local.index);
820   return 0;
821 }
822
823 VLIB_INIT_FUNCTION (udp_ping_init) =
824 {
825   .runs_after = VLIB_INITS("ip_main_init"),
826 };
827
828 /*
829  * fd.io coding-style-patch-verification: ON
830  *
831  * Local Variables:
832  * eval: (c-set-style "gnu")
833  * End:
834  */