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