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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
34 UDP_PING_NEXT_UDP_LOOKUP,
36 UDP_PING_NEXT_IP6_LOOKUP,
37 UDP_PING_NEXT_IP6_DROP,
41 udp_ping_main_t udp_ping_main;
44 udp_ping_process (vlib_main_t * vm,
45 vlib_node_runtime_t * rt, vlib_frame_t * f);
48 ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t * b, ip6_header_t * ip,
49 ip6_hop_by_hop_option_t * opt);
62 /* packet trace format function */
64 format_udp_ping_trace (u8 * s, va_list * args)
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 *);
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);
80 VLIB_REGISTER_NODE (udp_ping_node, static) =
82 .function = udp_ping_process,
83 .type = VLIB_NODE_TYPE_PROCESS,
84 .name = "udp-ping-process",
89 udp_ping_calculate_timer_interval (void)
92 ip46_udp_ping_flow *flow = NULL;
93 u16 min_interval = 0x1e9;
95 for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
97 if (pool_is_free_index (udp_ping_main.ip46_flow, i))
100 flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
102 if (min_interval > flow->udp_data.interval)
103 min_interval = flow->udp_data.interval;
106 if (udp_ping_main.timer_interval != min_interval)
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);
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)
121 ip46_udp_ping_flow *flow = NULL;
124 for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
126 if (pool_is_free_index (udp_ping_main.ip46_flow, i))
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)))
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 ();
152 cur_interval = flow->udp_data.interval;
153 flow->udp_data.interval = interval;
154 if (udp_ping_main.timer_interval > interval)
156 udp_ping_main.timer_interval = interval;
157 vlib_process_signal_event (udp_ping_main.vlib_main,
159 EVENT_SIG_RECHECK, 0);
161 else if (udp_ping_main.timer_interval == cur_interval)
162 udp_ping_calculate_timer_interval ();
167 /* Delete operation and item not found */
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);
178 udp_ping_create_rewrite (flow, (flow - udp_ping_main.ip46_flow));
180 if (udp_ping_main.timer_interval > interval)
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);
190 unformat_port_range (unformat_input_t * input, va_list * args)
192 u16 *start_port, *end_port;
194 u8 colon_present = 0;
196 start_port = va_arg (*args, u16 *);
197 end_port = va_arg (*args, u16 *);
199 *start_port = *end_port = 0;
201 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
206 *start_port = ((*start_port) * 10) + (c - '0');
225 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
230 *end_port = ((*end_port) * 10) + (c - '0');
238 if (end_port < start_port)
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)
248 ip46_address_t dst, src;
249 u16 start_src_port, end_src_port;
250 u16 start_dst_port, end_dst_port;
255 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
258 (input, "src %U", unformat_ip46_address, &src, IP46_TYPE_ANY))
260 else if (unformat (input, "src-port-range %U",
261 unformat_port_range, &start_src_port, &end_src_port))
265 (input, "dst %U", unformat_ip46_address, &dst, IP46_TYPE_ANY))
267 else if (unformat (input, "dst-port-range %U",
268 unformat_port_range, &start_dst_port, &end_dst_port))
270 else if (unformat (input, "interval %d", &interval))
272 else if (unformat (input, "fault-detect"))
274 else if (unformat (input, "disable"))
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);
288 VLIB_CLI_COMMAND (set_udp_ping_command, static) =
290 .path = "set udp-ping",
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> \
296 .function = set_udp_ping_command_fn,
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)
307 ip46_udp_ping_flow *ip46_flow;
308 u16 src_port, dst_port;
309 udp_ping_flow_data *stats;
311 s = format (s, "UDP-Ping data:\n");
313 for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
315 if (pool_is_free_index (udp_ping_main.ip46_flow, i))
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);
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);
332 for (src_port = ip46_flow->udp_data.start_src_port;
333 src_port <= ip46_flow->udp_data.end_src_port; src_port++)
335 for (dst_port = ip46_flow->udp_data.start_dst_port;
336 dst_port <= ip46_flow->udp_data.end_dst_port; dst_port++)
338 stats = ip46_flow->udp_data.stats + j;
340 format (s, "\nSrc Port - %u, Dst Port - %u, Flow CTX - %u\n",
341 src_port, dst_port, stats->flow_ctx);
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);
352 s = format (s, "\n\n");
355 vlib_cli_output (vm, "%v", s);
361 VLIB_CLI_COMMAND (show_udp_ping_cmd, static) =
363 .path = "show udp-ping summary",
364 .short_help = "Summary of udp-ping",
365 .function = show_udp_ping_summary_cmd_fn,
370 * @brief UDP-Ping Process node.
371 * @node udp-ping-process
373 * This is process node which wakes up when periodically to send
374 * out udp probe packets for all configured sessions.
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.
382 udp_ping_process (vlib_main_t * vm,
383 vlib_node_runtime_t * rt, vlib_frame_t * f)
386 uword *event_data = 0;
388 ip46_udp_ping_flow *ip46_flow;
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);
397 for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
399 if (pool_is_free_index (udp_ping_main.ip46_flow, i))
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);
411 * @brief HopByHop analyse function for udp-ping response.
413 * Walks through all hbh options present in udp-ping response
414 * and uses analyser library for the analysis.
418 udp_ping_analyse_hbh (vlib_buffer_t * b0,
422 ip6_hop_by_hop_option_t * opt0,
423 ip6_hop_by_hop_option_t * limit0, u16 len)
426 ip46_udp_ping_flow *ip46_flow;
428 ioam_analyser_data_t *data;
429 ioam_e2e_option_t *e2e;
430 ioam_trace_option_t *trace;
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 +
436 flow_index += (dst_port - ip46_flow->udp_data.start_dst_port);
437 data = &(ip46_flow->udp_data.stats[flow_index].analyse_data);
440 data->bytes_counter += len;
442 vnet_buffer (b0)->l2_classify.opaque_index =
443 ip46_flow->udp_data.stats[flow_index].flow_ctx;
445 while (opt0 < limit0)
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;
455 (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK_REPLY))
457 ip6_ioam_analyse_hbh_trace_loopback (data, &trace->trace_hdr,
458 (trace->hdr.length - 2));
461 ip6_hbh_ioam_trace_data_list_handler (b0,
462 vlib_buffer_get_current (b0),
464 (void) ip6_ioam_analyse_hbh_trace (data, &trace->trace_hdr, len,
465 (trace->hdr.length - 2));
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);
472 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
479 opt0 = (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
480 sizeof (ip6_hop_by_hop_option_t));
482 ip46_flow->udp_data.stats[flow_index].retry = 0;
486 * @brief UDP-Ping request/response handler function.
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.
494 udp_ping_local_analyse (vlib_buffer_t * b0,
496 ip6_hop_by_hop_header_t * hbh0, u16 * next0)
498 ip6_main_t *im = &ip6_main;
499 ip_lookup_main_t *lm = &im->lookup_main;
501 *next0 = UDP_PING_NEXT_IP6_DROP;
503 if (PREDICT_TRUE (hbh0->protocol == IP_PROTOCOL_UDP))
505 ip6_hop_by_hop_option_t *opt0;
506 ip6_hop_by_hop_option_t *limit0;
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)))
518 if (udp0->ping_data.msg_type == UDP_PING_PROBE)
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;
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);
539 /* UDP Ping packet, so return */
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
548 if (PREDICT_FALSE (hbh0->protocol == IPPROTO_IPV6_ROUTE))
551 /* Other case remove hbh-ioam headers */
552 u64 *copy_dst0, *copy_src0;
555 vlib_buffer_advance (b0, (hbh0->length + 1) << 3);
557 new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
558 ((hbh0->length + 1) << 3);
560 ip0->payload_length = clib_host_to_net_u16 (new_l0);
562 ip0->protocol = hbh0->protocol;
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];
573 *next0 = lm->local_next_by_ip_protocol[hbh0->protocol];
578 * @brief udp ping request/response packet receive node.
579 * @node udp-ping-local
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.
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.
589 * @par Graph mechanics: buffer, next index usage
592 * - <code>udp_ping_local_analyse(p0, ip0, hbh0, &next0)</code>
593 * - Checks packet type - request/respnse and process them.
595 * <em>Next Index:</em>
596 * - Dispatches the packet to ip6-lookup/ip6-drop depending on type of packet.
599 udp_ping_local_node_fn (vlib_main_t * vm,
600 vlib_node_runtime_t * node, vlib_frame_t * frame)
602 udp_ping_next_t next_index;
603 u32 *from, *to_next, n_left_from, n_left_to_next;
605 from = vlib_frame_vector_args (frame);
606 n_left_from = frame->n_vectors;
607 next_index = node->cached_next_index;
609 while (n_left_from > 0)
611 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
613 while (n_left_from >= 4 && n_left_to_next >= 2)
615 vlib_buffer_t *p0, *p1;
616 ip6_header_t *ip0, *ip1;
617 ip6_hop_by_hop_header_t *hbh0, *hbh1;
621 /* Prefetch next iteration. */
623 vlib_buffer_t *p2, *p3;
625 p2 = vlib_get_buffer (vm, from[2]);
626 p3 = vlib_get_buffer (vm, from[3]);
628 vlib_prefetch_buffer_header (p2, LOAD);
629 vlib_prefetch_buffer_header (p3, LOAD);
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);
636 pi0 = to_next[0] = from[0];
637 pi1 = to_next[1] = from[1];
643 p0 = vlib_get_buffer (vm, pi0);
644 p1 = vlib_get_buffer (vm, pi1);
646 ip0 = vlib_buffer_get_current (p0);
647 ip1 = vlib_buffer_get_current (p1);
649 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
650 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
652 udp_ping_local_analyse (p0, ip0, hbh0, &next0);
653 udp_ping_local_analyse (p1, ip1, hbh1, &next1);
655 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
657 if (p0->flags & VLIB_BUFFER_IS_TRACED)
659 udp_ping_trace_t *t0 =
660 vlib_add_trace (vm, node, p0, sizeof (*t0));
663 /* Check for udp ping packet */
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);
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;
675 if (p1->flags & VLIB_BUFFER_IS_TRACED)
677 udp_ping_trace_t *t1 =
678 vlib_add_trace (vm, node, p1, sizeof (*t1));
681 /* Check for udp ping packet */
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);
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;
695 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
696 to_next, n_left_to_next,
697 pi0, pi1, next0, next1);
700 while (n_left_from > 0 && n_left_to_next > 0)
704 ip6_hop_by_hop_header_t *hbh0;
715 p0 = vlib_get_buffer (vm, pi0);
716 ip0 = vlib_buffer_get_current (p0);
717 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
719 udp_ping_local_analyse (p0, ip0, hbh0, &next0);
721 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
723 if (p0->flags & VLIB_BUFFER_IS_TRACED)
725 udp_ping_trace_t *t0 =
726 vlib_add_trace (vm, node, p0, sizeof (*t0));
729 /* Check for udp ping packet */
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);
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;
743 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
744 to_next, n_left_to_next,
748 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
751 return frame->n_vectors;
756 * Node for udp-ping-local
758 VLIB_REGISTER_NODE (udp_ping_local, static) =
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,
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",
778 static clib_error_t *
779 udp_ping_init (vlib_main_t * vm)
781 clib_error_t *error = 0;
783 udp_ping_main.vlib_main = vm;
784 udp_ping_main.vnet_main = vnet_get_main ();
785 udp_ping_main.timer_interval = 1e9;
787 if ((error = vlib_call_init_function (vm, ip_main_init)))
790 ip6_register_protocol (IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
791 udp_ping_local.index);
795 VLIB_INIT_FUNCTION (udp_ping_init);
798 * fd.io coding-style-patch-verification: ON
801 * eval: (c-set-style "gnu")