summaryrefslogtreecommitdiffstats
path: root/src/vnet/llc
AgeCommit message (Expand)AuthorFilesLines
2018-10-23c11 safe string handling supportDave Barach1-1/+1
2017-10-04[aarch64] Fixes CLI crashes on dpaa2 platform.Christophe Fontaine1-1/+1
2017-03-27Mcast rewrite no memcpyNeale Ranns1-3/+0
2016-12-28Reorganize source tree to use single autotools instanceDamjan Marion4-0/+879
/a> 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/vnet.h>
#include <vlib/vlib.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vlibsocket/api.h>
#include <vnet/ip/ip.h>
#include <vnet/ip/ip6_hop_by_hop.h>
#include <ioam/encap/ip6_ioam_trace.h>
#include <ioam/encap/ip6_ioam_e2e.h>
#include <ioam/udp-ping/udp_ping_packet.h>
#include <ioam/udp-ping/udp_ping.h>
#include <ioam/udp-ping/udp_ping_util.h>
#include <vnet/sr/sr_packet.h>

typedef enum
{
  UDP_PING_NEXT_DROP,
  UDP_PING_NEXT_PUNT,
  UDP_PING_NEXT_UDP_LOOKUP,
  UDP_PING_NEXT_ICMP,
  UDP_PING_NEXT_IP6_LOOKUP,
  UDP_PING_NEXT_IP6_DROP,
  UDP_PING_N_NEXT,
} udp_ping_next_t;

udp_ping_main_t udp_ping_main;

uword
udp_ping_process (vlib_main_t * vm,
		  vlib_node_runtime_t * rt, vlib_frame_t * f);

extern int
ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t * b, ip6_header_t * ip,
				      ip6_hop_by_hop_option_t * opt);

typedef struct
{
  ip6_address_t src;
  ip6_address_t dst;
  u16 src_port;
  u16 dst_port;
  u16 handle;
  u16 next_index;
  u8 msg_type;
} udp_ping_trace_t;

/* packet trace format function */
static u8 *
format_udp_ping_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  udp_ping_trace_t *t = va_arg (*args, udp_ping_trace_t *);

  s = format (s, "udp-ping-local: src %U, dst %U, src_port %u, dst_port %u "
	      "handle %u, next_index %u, msg_type %u",
	      format_ip6_address, &t->src,
	      format_ip6_address, &t->dst,
	      t->src_port, t->dst_port,
	      t->handle, t->next_index, t->msg_type);
  return s;
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (udp_ping_node, static) =
{
  .function = udp_ping_process,
  .type = VLIB_NODE_TYPE_PROCESS,
  .name = "udp-ping-process",
};
/* *INDENT-ON* */

void
udp_ping_calculate_timer_interval (void)
{
  int i;
  ip46_udp_ping_flow *flow = NULL;
  u16 min_interval = 0x1e9;

  for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
    {
      if (pool_is_free_index (udp_ping_main.ip46_flow, i))
	continue;

      flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);

      if (min_interval > flow->udp_data.interval)
	min_interval = flow->udp_data.interval;
    }

  if (udp_ping_main.timer_interval != min_interval)
    {
      udp_ping_main.timer_interval = min_interval;
      vlib_process_signal_event (udp_ping_main.vlib_main,
				 udp_ping_node.index, EVENT_SIG_RECHECK, 0);
    }
}

void
ip46_udp_ping_set_flow (ip46_address_t src, ip46_address_t dst,
			u16 start_src_port, u16 end_src_port,
			u16 start_dst_port, u16 end_dst_port,
			u16 interval, u8 fault_det, u8 is_disable)
{
  u8 found = 0;
  ip46_udp_ping_flow *flow = NULL;
  int i;

  for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
    {
      if (pool_is_free_index (udp_ping_main.ip46_flow, i))
	continue;

      flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
      if ((0 == udp_ping_compare_flow (src, dst,
				       start_src_port, end_src_port,
				       start_dst_port, end_dst_port, flow)))
	{
	  found = 1;
	  break;
	}
    }

  if (found)
    {
      u16 cur_interval;
      if (is_disable)
	{
	  cur_interval = flow->udp_data.interval;
	  udp_ping_free_flow_data (flow);
	  pool_put_index (udp_ping_main.ip46_flow, i);
	  if (udp_ping_main.timer_interval == interval)
	    udp_ping_calculate_timer_interval ();
	  return;
	}

      cur_interval = flow->udp_data.interval;
      flow->udp_data.interval = interval;
      if (udp_ping_main.timer_interval > interval)
	{
	  udp_ping_main.timer_interval = interval;
	  vlib_process_signal_event (udp_ping_main.vlib_main,
				     udp_ping_node.index,
				     EVENT_SIG_RECHECK, 0);
	}
      else if (udp_ping_main.timer_interval == cur_interval)
	udp_ping_calculate_timer_interval ();

      return;
    }

  /* Delete operation and item not found */
  if (is_disable)
    return;

  /* Alloc new session */
  pool_get_aligned (udp_ping_main.ip46_flow, flow, CLIB_CACHE_LINE_BYTES);
  udp_ping_populate_flow (src, dst,
			  start_src_port, end_src_port,
			  start_dst_port, end_dst_port,
			  interval, fault_det, flow);

  udp_ping_create_rewrite (flow, (flow - udp_ping_main.ip46_flow));

  if (udp_ping_main.timer_interval > interval)
    {
      udp_ping_main.timer_interval = interval;
      vlib_process_signal_event (udp_ping_main.vlib_main,
				 udp_ping_node.index, EVENT_SIG_RECHECK, 0);
    }
  return;
}

uword
unformat_port_range (unformat_input_t * input, va_list * args)
{
  u16 *start_port, *end_port;
  uword c;
  u8 colon_present = 0;

  start_port = va_arg (*args, u16 *);
  end_port = va_arg (*args, u16 *);

  *start_port = *end_port = 0;
  /* Get start port */
  while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
    {
      switch (c)
	{
	case '0' ... '9':
	  *start_port = ((*start_port) * 10) + (c - '0');
	  break;

	case ':':
	  colon_present = 1;
	  break;

	default:
	  return 0;
	}

      if (colon_present)
	break;
    }

  if (!colon_present)
    return 0;

  /* Get end port */
  while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
    {
      switch (c)
	{
	case '0' ... '9':
	  *end_port = ((*end_port) * 10) + (c - '0');
	  break;

	default:
	  return 1;
	}
    }

  if (end_port < start_port)
    return 0;

  return 1;
}

static clib_error_t *
set_udp_ping_command_fn (vlib_main_t * vm,
			 unformat_input_t * input, vlib_cli_command_t * cmd)
{
  ip46_address_t dst, src;
  u16 start_src_port, end_src_port;
  u16 start_dst_port, end_dst_port;
  u32 interval;
  u8 is_disable = 0;
  u8 fault_det = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat
	  (input, "src %U", unformat_ip46_address, &src, IP46_TYPE_ANY))
	;
      else if (unformat (input, "src-port-range %U",
			 unformat_port_range, &start_src_port, &end_src_port))
	;
      else
	if (unformat
	    (input, "dst %U", unformat_ip46_address, &dst, IP46_TYPE_ANY))
	;
      else if (unformat (input, "dst-port-range %U",
			 unformat_port_range, &start_dst_port, &end_dst_port))
	;
      else if (unformat (input, "interval %d", &interval))
	;
      else if (unformat (input, "fault-detect"))
	fault_det = 1;
      else if (unformat (input, "disable"))
	is_disable = 1;
      else
	break;
    }

  ip46_udp_ping_set_flow (src, dst, start_src_port, end_src_port,
			  start_dst_port, end_dst_port, (u16) interval,
			  fault_det, is_disable);

  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (set_udp_ping_command, static) =
{
  .path = "set udp-ping",
  .short_help =
      "set udp-ping src <local IPv6 address>  src-port-range <local port range> \
      dst <remote IPv6 address> dst-port-range <destination port range> \
      interval <time interval in sec for which ping packet will be sent> \
      [disable]",
  .function = set_udp_ping_command_fn,
};
/* *INDENT-ON* */

static clib_error_t *
show_udp_ping_summary_cmd_fn (vlib_main_t * vm,
			      unformat_input_t * input,
			      vlib_cli_command_t * cmd)
{
  u8 *s = 0;
  int i, j;
  ip46_udp_ping_flow *ip46_flow;
  u16 src_port, dst_port;
  udp_ping_flow_data *stats;

  s = format (s, "UDP-Ping data:\n");

  for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
    {
      if (pool_is_free_index (udp_ping_main.ip46_flow, i))
	continue;

      ip46_flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
      s = format (s, "Src: %U, Dst: %U\n",
		  format_ip46_address, &ip46_flow->src, IP46_TYPE_ANY,
		  format_ip46_address, &ip46_flow->dst, IP46_TYPE_ANY);

      s = format (s, "Start src port: %u, End src port: %u\n",
		  ip46_flow->udp_data.start_src_port,
		  ip46_flow->udp_data.end_src_port);
      s = format (s, "Start dst port: %u, End dst port: %u\n",
		  ip46_flow->udp_data.start_dst_port,
		  ip46_flow->udp_data.end_dst_port);
      s = format (s, "Interval: %u\n", ip46_flow->udp_data.interval);

      j = 0;
      for (src_port = ip46_flow->udp_data.start_src_port;
	   src_port <= ip46_flow->udp_data.end_src_port; src_port++)
	{
	  for (dst_port = ip46_flow->udp_data.start_dst_port;
	       dst_port <= ip46_flow->udp_data.end_dst_port; dst_port++)
	    {
	      stats = ip46_flow->udp_data.stats + j;
	      s =
		format (s, "\nSrc Port - %u, Dst Port - %u, Flow CTX - %u\n",
			src_port, dst_port, stats->flow_ctx);
	      s =
		format (s, "Path State - %s\n",
			(stats->retry > MAX_PING_RETRIES) ? "Down" : "Up");
	      s = format (s, "Path Data:\n");
	      s = print_analyse_flow (s,
				      &ip46_flow->udp_data.
				      stats[j].analyse_data);
	      j++;
	    }
	}
      s = format (s, "\n\n");
    }

  vlib_cli_output (vm, "%v", s);
  vec_free (s);
  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (show_udp_ping_cmd, static) =
{
  .path = "show udp-ping summary",
  .short_help = "Summary of udp-ping",
  .function = show_udp_ping_summary_cmd_fn,
};
/* *INDENT-ON* */

/**
 * @brief UDP-Ping Process node.
 * @node udp-ping-process
 *
 * This is process node which wakes up when periodically to send
 * out udp probe packets for all configured sessions.
 *
 * @param vm    vlib_main_t corresponding to the current thread.
 * @param node  vlib_node_runtime_t data for this node.
 * @param frame vlib_frame_t whose contents should be dispatched.
 *
 */
uword
udp_ping_process (vlib_main_t * vm,
		  vlib_node_runtime_t * rt, vlib_frame_t * f)
{
  f64 now;
  uword *event_data = 0;
  int i;
  ip46_udp_ping_flow *ip46_flow;

  while (1)
    {
      vec_reset_length (event_data);
      vlib_process_wait_for_event_or_clock (vm, udp_ping_main.timer_interval);
      (void) vlib_process_get_events (vm, &event_data);
      now = vlib_time_now (vm);

      for (i = 0; i < vec_len (udp_ping_main.ip46_flow); i++)
	{
	  if (pool_is_free_index (udp_ping_main.ip46_flow, i))
	    continue;

	  ip46_flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
	  if (ip46_flow->udp_data.next_send_time < now)
	    udp_ping_send_ip6_pak (udp_ping_main.vlib_main, ip46_flow);
	}
    }
  return 0;
}

/**
 * @brief HopByHop analyse function for udp-ping response.
 *
 * Walks through all hbh options present in udp-ping response
 * and uses analyser library for the analysis.
 *
 */
void
udp_ping_analyse_hbh (vlib_buffer_t * b0,
		      u32 flow_id,
		      u16 src_port,
		      u16 dst_port,
		      ip6_hop_by_hop_option_t * opt0,
		      ip6_hop_by_hop_option_t * limit0, u16 len)
{
  u8 type0;
  ip46_udp_ping_flow *ip46_flow;
  u16 flow_index;
  ioam_analyser_data_t *data;
  ioam_e2e_option_t *e2e;
  ioam_trace_option_t *trace;

  /* If the packet doesnt match UDP session then return */
  if (PREDICT_FALSE (pool_is_free_index (udp_ping_main.ip46_flow, flow_id)))
    return;

  ip46_flow = udp_ping_main.ip46_flow + flow_id;
  /* Check port is within range */
  if (PREDICT_FALSE ((src_port < ip46_flow->udp_data.start_src_port) ||
		     (src_port > ip46_flow->udp_data.end_src_port) ||
		     (dst_port < ip46_flow->udp_data.start_dst_port) ||
		     (dst_port > ip46_flow->udp_data.end_dst_port)))
    return;

  flow_index = (src_port - ip46_flow->udp_data.start_src_port) *
    (ip46_flow->udp_data.end_dst_port - ip46_flow->udp_data.start_dst_port +
     1);
  flow_index += (dst_port - ip46_flow->udp_data.start_dst_port);
  data = &(ip46_flow->udp_data.stats[flow_index].analyse_data);

  data->pkt_counter++;
  data->bytes_counter += len;

  vnet_buffer (b0)->l2_classify.opaque_index =
    ip46_flow->udp_data.stats[flow_index].flow_ctx;

  while (opt0 < limit0)
    {
      type0 = opt0->type;
      switch (type0)
	{
	case HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST:
	  /* Add trace for here as it hasnt been done yet */
	  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
	  trace = (ioam_trace_option_t *) opt0;
	  if (PREDICT_FALSE
	      (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK_REPLY))
	    {
	      ip6_ioam_analyse_hbh_trace_loopback (data, &trace->trace_hdr,
						   (trace->hdr.length - 2));
	      return;
	    }
	  ip6_hbh_ioam_trace_data_list_handler (b0,
						vlib_buffer_get_current (b0),
						opt0);
	  (void) ip6_ioam_analyse_hbh_trace (data, &trace->trace_hdr, len,
					     (trace->hdr.length - 2));
	  break;
	case HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE:
	  e2e = (ioam_e2e_option_t *) opt0;
	  (void) ip6_ioam_analyse_hbh_e2e (data, &e2e->e2e_hdr, len);
	  break;
	case 0:		/* Pad1 */
	  opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
	  continue;
	case 1:		/* PadN */
	  break;
	default:
	  break;
	}
      opt0 = (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
					  sizeof (ip6_hop_by_hop_option_t));
    }
  ip46_flow->udp_data.stats[flow_index].retry = 0;
}

/**
 * @brief UDP-Ping request/response handler function.
 *
 * Checks udp-ping packet type - request/response and handles them.
 * If not udp-ping packet then, strips off hbh options and enques
 * packet to protocol registered node to enable next protocol processing.
 *
 */
void
udp_ping_local_analyse (vlib_buffer_t * b0,
			ip6_header_t * ip0,
			ip6_hop_by_hop_header_t * hbh0, u16 * next0)
{
  ip6_main_t *im = &ip6_main;
  ip_lookup_main_t *lm = &im->lookup_main;

  *next0 = UDP_PING_NEXT_IP6_DROP;

  if (PREDICT_TRUE (hbh0->protocol == IP_PROTOCOL_UDP))
    {
      ip6_hop_by_hop_option_t *opt0;
      ip6_hop_by_hop_option_t *limit0;
      u16 p_len0;
      udp_ping_t *udp0;

      /* Check for udp ping packet */
      udp0 = (udp_ping_t *) ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
      opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
      if ((udp0->ping_data.probe_marker1 ==
	   clib_host_to_net_u32 (UDP_PING_PROBE_MARKER1)) &&
	  (udp0->ping_data.probe_marker2 ==
	   clib_host_to_net_u32 (UDP_PING_PROBE_MARKER2)))
	{
	  if (udp0->ping_data.msg_type == UDP_PING_PROBE)
	    {
	      udp_ping_create_reply_from_probe_ip6 (ip0, hbh0, udp0);
	      /* Skip e2e processing */
	      vnet_buffer (b0)->l2_classify.opaque_index = 0x7FFFFFFF;
	      *next0 = UDP_PING_NEXT_IP6_LOOKUP;
	      return;
	    }

	  /* Reply */
	  opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
	  limit0 = (ip6_hop_by_hop_option_t *)
	    ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
	  p_len0 = clib_net_to_host_u16 (ip0->payload_length);
	  udp_ping_analyse_hbh (b0,
				clib_net_to_host_u16 (udp0->
						      ping_data.sender_handle),
				clib_net_to_host_u16 (udp0->udp.dst_port),
				clib_net_to_host_u16 (udp0->udp.src_port),
				opt0, limit0, p_len0);

	  /* UDP Ping packet, so return */
	  return;
	}
    }

  /* If next header is SR, then destination may get overwritten to
   * remote address. So pass it to SR processing as it may be local packet
   * afterall
   */
  if (PREDICT_FALSE (hbh0->protocol == IPPROTO_IPV6_ROUTE))
    goto end;

  /* Other case remove hbh-ioam headers */
  u64 *copy_dst0, *copy_src0;
  u16 new_l0;

  vlib_buffer_advance (b0, (hbh0->length + 1) << 3);

  new_l0 = clib_net_to_host_u16 (ip0->payload_length) -
    ((hbh0->length + 1) << 3);

  ip0->payload_length = clib_host_to_net_u16 (new_l0);

  ip0->protocol = hbh0->protocol;

  copy_src0 = (u64 *) ip0;
  copy_dst0 = copy_src0 + (hbh0->length + 1);
  copy_dst0[4] = copy_src0[4];
  copy_dst0[3] = copy_src0[3];
  copy_dst0[2] = copy_src0[2];
  copy_dst0[1] = copy_src0[1];
  copy_dst0[0] = copy_src0[0];

end:
  *next0 = lm->local_next_by_ip_protocol[hbh0->protocol];
  return;
}

/**
 * @brief udp ping request/response packet receive node.
 * @node udp-ping-local
 *
 * This function receives udp ping request/response packets and process them.
 * For request packets, response is created and sent.
 * For response packets, they are analysed and results stored.
 *
 * @param vm    vlib_main_t corresponding to the current thread.
 * @param node  vlib_node_runtime_t data for this node.
 * @param frame vlib_frame_t whose contents should be dispatched.
 *
 * @par Graph mechanics: buffer, next index usage
 *
 * <em>Uses:</em>
 * - <code>udp_ping_local_analyse(p0, ip0, hbh0, &next0)</code>
 *     - Checks packet type - request/respnse and process them.
 *
 * <em>Next Index:</em>
 * - Dispatches the packet to ip6-lookup/ip6-drop depending on type of packet.
 */
static uword
udp_ping_local_node_fn (vlib_main_t * vm,
			vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  udp_ping_next_t next_index;
  u32 *from, *to_next, n_left_from, n_left_to_next;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from >= 4 && n_left_to_next >= 2)
	{
	  vlib_buffer_t *p0, *p1;
	  ip6_header_t *ip0, *ip1;
	  ip6_hop_by_hop_header_t *hbh0, *hbh1;
	  u16 next0, next1;
	  u32 pi0, pi1;

	  /* Prefetch next iteration. */
	  {
	    vlib_buffer_t *p2, *p3;

	    p2 = vlib_get_buffer (vm, from[2]);
	    p3 = vlib_get_buffer (vm, from[3]);

	    vlib_prefetch_buffer_header (p2, LOAD);
	    vlib_prefetch_buffer_header (p3, LOAD);

	    /* Prefetch 3 cache lines as we need to look deep into packet */
	    CLIB_PREFETCH (p2->data, 3 * CLIB_CACHE_LINE_BYTES, STORE);
	    CLIB_PREFETCH (p3->data, 3 * CLIB_CACHE_LINE_BYTES, STORE);
	  }

	  pi0 = to_next[0] = from[0];
	  pi1 = to_next[1] = from[1];
	  from += 2;
	  n_left_from -= 2;
	  to_next += 2;
	  n_left_to_next -= 2;

	  p0 = vlib_get_buffer (vm, pi0);
	  p1 = vlib_get_buffer (vm, pi1);

	  ip0 = vlib_buffer_get_current (p0);
	  ip1 = vlib_buffer_get_current (p1);

	  hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
	  hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);

	  udp_ping_local_analyse (p0, ip0, hbh0, &next0);
	  udp_ping_local_analyse (p1, ip1, hbh1, &next1);

	  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
	    {
	      if (p0->flags & VLIB_BUFFER_IS_TRACED)
		{
		  udp_ping_trace_t *t0 =
		    vlib_add_trace (vm, node, p0, sizeof (*t0));
		  udp_ping_t *udp0;

		  /* Check for udp ping packet */
		  udp0 =
		    (udp_ping_t *) ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
		  t0->src = ip0->src_address;
		  t0->dst = ip0->dst_address;
		  t0->src_port = clib_net_to_host_u16 (udp0->udp.src_port);
		  t0->dst_port = clib_net_to_host_u16 (udp0->udp.dst_port);
		  t0->handle =
		    clib_net_to_host_u16 (udp0->ping_data.sender_handle);
		  t0->msg_type = udp0->ping_data.msg_type;
		  t0->next_index = next0;
		}
	      if (p1->flags & VLIB_BUFFER_IS_TRACED)
		{
		  udp_ping_trace_t *t1 =
		    vlib_add_trace (vm, node, p1, sizeof (*t1));
		  udp_ping_t *udp1;

		  /* Check for udp ping packet */
		  udp1 =
		    (udp_ping_t *) ((u8 *) hbh1 + ((hbh1->length + 1) << 3));
		  t1->src = ip1->src_address;
		  t1->dst = ip1->dst_address;
		  t1->src_port = clib_net_to_host_u16 (udp1->udp.src_port);
		  t1->dst_port = clib_net_to_host_u16 (udp1->udp.dst_port);
		  t1->handle =
		    clib_net_to_host_u16 (udp1->ping_data.sender_handle);
		  t1->msg_type = udp1->ping_data.msg_type;
		  t1->next_index = next1;
		}
	    }

	  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
					   to_next, n_left_to_next,
					   pi0, pi1, next0, next1);
	}

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  vlib_buffer_t *p0;
	  ip6_header_t *ip0;
	  ip6_hop_by_hop_header_t *hbh0;
	  u16 next0;
	  u32 pi0;

	  pi0 = from[0];
	  to_next[0] = pi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  p0 = vlib_get_buffer (vm, pi0);
	  ip0 = vlib_buffer_get_current (p0);
	  hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);

	  udp_ping_local_analyse (p0, ip0, hbh0, &next0);

	  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
	    {
	      if (p0->flags & VLIB_BUFFER_IS_TRACED)
		{
		  udp_ping_trace_t *t0 =
		    vlib_add_trace (vm, node, p0, sizeof (*t0));
		  udp_ping_t *udp0;

		  /* Check for udp ping packet */
		  udp0 =
		    (udp_ping_t *) ((u8 *) hbh0 + ((hbh0->length + 1) << 3));
		  t0->src = ip0->src_address;
		  t0->dst = ip0->dst_address;
		  t0->src_port = clib_net_to_host_u16 (udp0->udp.src_port);
		  t0->dst_port = clib_net_to_host_u16 (udp0->udp.dst_port);
		  t0->handle =
		    clib_net_to_host_u16 (udp0->ping_data.sender_handle);
		  t0->msg_type = udp0->ping_data.msg_type;
		  t0->next_index = next0;
		}
	    }

	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
					   to_next, n_left_to_next,
					   pi0, next0);
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  return frame->n_vectors;
}

/* *INDENT-OFF* */
/*
 * Node for udp-ping-local
 */
VLIB_REGISTER_NODE (udp_ping_local, static) =
{
  .function = udp_ping_local_node_fn,
  .name = "udp-ping-local",
  .vector_size = sizeof (u32),
  .format_trace = format_udp_ping_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,
  .n_next_nodes = UDP_PING_N_NEXT,
  .next_nodes =
    {
      [UDP_PING_NEXT_DROP] = "error-drop",
      [UDP_PING_NEXT_PUNT] = "error-punt",
      [UDP_PING_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
      [UDP_PING_NEXT_ICMP] = "ip6-icmp-input",
      [UDP_PING_NEXT_IP6_LOOKUP] = "ip6-lookup",
      [UDP_PING_NEXT_IP6_DROP] = "ip6-drop",
    },
};
/* *INDENT-ON* */

static clib_error_t *
udp_ping_init (vlib_main_t * vm)
{
  clib_error_t *error = 0;

  udp_ping_main.vlib_main = vm;
  udp_ping_main.vnet_main = vnet_get_main ();
  udp_ping_main.timer_interval = 1e9;

  if ((error = vlib_call_init_function (vm, ip_main_init)))
    return (error);

  ip6_register_protocol (IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS,
			 udp_ping_local.index);
  return 0;
}

VLIB_INIT_FUNCTION (udp_ping_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */