aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/arping/arping.c
blob: 11fb0704dd32970eab933718409ea9e13fd66625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * Copyright (c) 2017-2019 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.
 */

#ifndef include_dpdk_buffer_h
#define include_dpdk_buffer_h

#define rte_mbuf_from_vlib_buffer(x) (((struct rte_mbuf *)x) - 1)
#define vlib_buffer_from_rte_mbuf(x) ((vlib_buffer_t *)(x+1))

extern struct rte_mempool **dpdk_mempool_by_buffer_pool_index;
extern struct rte_mempool **dpdk_no_cache_mempool_by_buffer_pool_index;

clib_error_t *dpdk_buffer_pools_create (vlib_main_t * vm);

#endif /* include_dpdk_buffer_h */

/** @endcond */
/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
'>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
/*
 * Copyright (c) 2021 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 <stddef.h>

#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>
#include <vnet/ip-neighbor/ip4_neighbor.h>
#include <vnet/ip-neighbor/ip6_neighbor.h>
#include <arping/arping.h>

arping_main_t arping_main;

#define foreach_arping_error _ (NONE, "no error")

typedef enum
{
#define _(f, s) ARPING_ERROR_##f,
  foreach_arping_error
#undef _
    ARPING_N_ERROR,
} arping__error_t;

static char *arping_error_strings[] = {
#define _(n, s) s,
  foreach_arping_error
#undef _
};

#define foreach_arping                                                        \
  _ (DROP, "error-drop")                                                      \
  _ (IO, "interface-output")

typedef enum
{
#define _(sym, str) ARPING_NEXT_##sym,
  foreach_arping
#undef _
    ARPING_N_NEXT,
} arping_next_t;

typedef struct arping_trace_t_
{
  u32 sw_if_index;
  u16 arp_opcode;
  ethernet_arp_ip4_over_ethernet_address_t reply;
} arping_trace_t;

typedef enum
{
#define _(sym, str) ARPING6_NEXT_##sym,
  foreach_arping
#undef _
    ARPING6_N_NEXT,
} arping6_next_t;

typedef CLIB_PACKED (struct {
  mac_address_t mac;
  ip6_address_t ip6;
}) ethernet_arp_ip6_over_ethernet_address_t;

typedef struct arping6_trace_t_
{
  u32 sw_if_index;
  u8 type;
  ethernet_arp_ip6_over_ethernet_address_t reply;
} arping6_trace_t;

/* packet trace format function */
static u8 *
format_arping_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 *);
  arping_trace_t *t = va_arg (*args, arping_trace_t *);

  s = format (s, "sw-if-index: %u, opcode: %U, from %U (%U)", t->sw_if_index,
	      format_ethernet_arp_opcode, t->arp_opcode, format_mac_address,
	      &t->reply.mac, format_ip4_address, &t->reply.ip4);

  return s;
}

static u8 *
format_arping6_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 *);
  arping6_trace_t *t = va_arg (*args, arping6_trace_t *);

  s = format (s, "sw-if-index: %u, type: %u, from %U (%U)", t->sw_if_index,
	      t->type, format_mac_address, &t->reply.mac, format_ip6_address,
	      &t->reply.ip6);

  return s;
}

VLIB_NODE_FN (arping_input_node)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  u32 n_left_from, *from, *to_next, n_left_to_next;
  arping_next_t next_index;
  arping_main_t *am = &arping_main;

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

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

      while (n_left_from >= 2 && n_left_to_next >= 2)
	{
	  u32 next0, next1, bi0, bi1;
	  vlib_buffer_t *b0, *b1;
	  ethernet_arp_header_t *arp0, *arp1;
	  u32 sw_if_index0, sw_if_index1;
	  arping_intf_t *aif0, *aif1;

	  bi0 = to_next[0] = from[0];
	  bi1 = to_next[1] = from[1];

	  from += 2;
	  n_left_from -= 2;
	  to_next += 2;
	  n_left_to_next -= 2;

	  next0 = next1 = ARPING_NEXT_DROP;

	  b0 = vlib_get_buffer (vm, bi0);
	  b1 = vlib_get_buffer (vm, bi1);

	  arp0 = vlib_buffer_get_current (b0);
	  arp1 = vlib_buffer_get_current (b1);

	  vnet_feature_next (&next0, b0);
	  vnet_feature_next (&next1, b1);

	  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
	  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];

	  if (PREDICT_TRUE (arp0->opcode ==
			    clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply)))
	    {
	      aif0 = am->interfaces[sw_if_index0];
	      if (PREDICT_TRUE (aif0->address.ip.ip4.as_u32 ==
				arp0->ip4_over_ethernet[0].ip4.as_u32))
		{
		  aif0->recv.from4.ip4.as_u32 =
		    arp0->ip4_over_ethernet[0].ip4.as_u32;
		  clib_memcpy_fast (&aif0->recv.from4.mac,
				    &arp0->ip4_over_ethernet[0].mac, 6);
		  aif0->reply_count++;
		}
	    }
	  if (PREDICT_TRUE (arp1->opcode ==
			    clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply)))
	    {
	      aif1 = am->interfaces[sw_if_index1];
	      if (PREDICT_TRUE (aif1->address.ip.ip4.as_u32 ==
				arp1->ip4_over_ethernet[0].ip4.as_u32))
		{
		  aif1->recv.from4.ip4.as_u32 =
		    arp1->ip4_over_ethernet[0].ip4.as_u32;
		  clib_memcpy_fast (&aif1->recv.from4.mac,
				    &arp1->ip4_over_ethernet[0].mac, 6);
		  aif1->reply_count++;
		}
	    }

	  if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
	    {
	      arping_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
	      t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
	      t->arp_opcode = clib_host_to_net_u16 (arp0->opcode);
	      t->reply.ip4.as_u32 = arp0->ip4_over_ethernet[0].ip4.as_u32;
	      clib_memcpy_fast (&t->reply.mac, &arp0->ip4_over_ethernet[0].mac,
				6);
	    }
	  if (PREDICT_FALSE ((b1->flags & VLIB_BUFFER_IS_TRACED)))
	    {
	      arping_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
	      t->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
	      t->arp_opcode = clib_host_to_net_u16 (arp1->opcode);
	      t->reply.ip4.as_u32 = arp1->ip4_over_ethernet[0].ip4.as_u32;
	      clib_memcpy_fast (&t->reply.mac, &arp1->ip4_over_ethernet[0].mac,
				6);
	    }

	  vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
					   n_left_to_next, bi0, bi1, next0,
					   next1);
	}

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 next0, bi0;
	  vlib_buffer_t *b0;
	  ethernet_arp_header_t *arp0;
	  arping_intf_t *aif0;
	  u32 sw_if_index0;

	  bi0 = to_next[0] = from[0];

	  from += 1;
	  n_left_from -= 1;
	  to_next += 1;
	  n_left_to_next -= 1;
	  next0 = ARPING_NEXT_DROP;

	  b0 = vlib_get_buffer (vm, bi0);
	  arp0 = vlib_buffer_get_current (b0);

	  vnet_feature_next (&next0, b0);

	  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];

	  if (PREDICT_TRUE (arp0->opcode ==
			    clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply)))
	    {
	      aif0 = am->interfaces[sw_if_index0];
	      if (PREDICT_TRUE (aif0->address.ip.ip4.as_u32 ==
				arp0->ip4_over_ethernet[0].ip4.as_u32))
		{
		  aif0->recv.from4.ip4.as_u32 =
		    arp0->ip4_over_ethernet[0].ip4.as_u32;
		  clib_memcpy_fast (&aif0->recv.from4.mac,
				    &arp0->ip4_over_ethernet[0].mac, 6);
		  aif0->reply_count++;
		}
	    }

	  if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
	    {
	      arping_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
	      t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
	      t->arp_opcode = clib_host_to_net_u16 (arp0->opcode);
	      t->reply.ip4.as_u32 = arp0->ip4_over_ethernet[0].ip4.as_u32;
	      clib_memcpy_fast (&t->reply.mac, &arp0->ip4_over_ethernet[0].mac,
				6);
	    }

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

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

  return frame->n_vectors;
}

VLIB_REGISTER_NODE (arping_input_node) =
{
  .name = "arping-input",.vector_size = sizeof (u32),.format_trace =
    format_arping_trace,.type = VLIB_NODE_TYPE_INTERNAL,.n_errors =
    ARPING_N_ERROR,.error_strings = arping_error_strings,.n_next_nodes =
    ARPING_N_NEXT,.next_nodes =
  {
  [ARPING_NEXT_DROP] = "error-drop",[ARPING_NEXT_IO] = "interface-output",}
,};

VNET_FEATURE_INIT (arping_feat_node, static) = {
  .arc_name = "arp",
  .node_name = "arping-input",
  .runs_before = VNET_FEATURES ("arp-reply"),
};

VLIB_NODE_FN (arping6_input_node)
(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
{
  u32 n_left_from, *from, *to_next, n_left_to_next;
  arping_next_t next_index;
  arping_main_t *am = &arping_main;

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

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

      while (n_left_from >= 2 && n_left_to_next >= 2)
	{
	  u32 next0, next1, bi0, bi1;
	  vlib_buffer_t *b0, *b1;
	  ip6_header_t *ip60, *ip61;
	  u32 sw_if_index0, sw_if_index1;
	  arping_intf_t *aif0, *aif1;
	  icmp6_neighbor_solicitation_or_advertisement_header_t *sol_adv0,
	    *sol_adv1;
	  icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
	    *lladdr0,
	    *lladdr1;

	  bi0 = to_next[0] = from[0];
	  bi1 = to_next[1] = from[1];

	  from += 2;
	  n_left_from -= 2;
	  to_next += 2;
	  n_left_to_next -= 2;

	  next0 = next1 = ARPING6_NEXT_DROP;

	  b0 = vlib_get_buffer (vm, bi0);
	  b1 = vlib_get_buffer (vm, bi1);

	  ip60 = vlib_buffer_get_current (b0);
	  ip61 = vlib_buffer_get_current (b1);

	  vnet_feature_next (&next0, b0);
	  vnet_feature_next (&next1, b1);

	  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
	  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];

	  sol_adv0 = ip6_next_header (ip60);
	  lladdr0 =
	    (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
	       *) (sol_adv0 + 1);

	  if (PREDICT_TRUE (sol_adv0->icmp.type ==
			    ICMP6_neighbor_advertisement))
	    {
	      aif0 = am->interfaces[sw_if_index0];
	      if (PREDICT_TRUE (clib_memcmp (&aif0->address.ip.ip6,
					     &sol_adv0->target_address,
					     sizeof (aif0->address.ip.ip6)) ==
				0))
		{
		  clib_memcpy_fast (&aif0->recv.from6.ip6,
				    &sol_adv0->target_address,
				    sizeof (aif0->recv.from6.ip6));
		  clib_memcpy_fast (&aif0->recv.from6.mac,
				    lladdr0->ethernet_address, 6);
		  aif0->reply_count++;
		}
	    }

	  sol_adv1 = ip6_next_header (ip61);
	  lladdr1 =
	    (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
	       *) (sol_adv1 + 1);

	  if (PREDICT_TRUE (sol_adv1->icmp.type ==
			    ICMP6_neighbor_advertisement))
	    {
	      aif1 = am->interfaces[sw_if_index1];
	      if (PREDICT_TRUE (clib_memcmp (&aif1->address.ip.ip6,
					     &sol_adv1->target_address,
					     sizeof (aif1->address.ip.ip6)) ==
				0))
		{
		  clib_memcpy_fast (&aif1->recv.from6.ip6,
				    &sol_adv1->target_address,
				    sizeof (aif1->recv.from6.ip6));
		  clib_memcpy_fast (&aif1->recv.from6.mac,
				    lladdr1->ethernet_address, 6);
		  aif1->reply_count++;
		}
	    }

	  if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
	    {
	      arping6_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
	      t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
	      t->type = sol_adv0->icmp.type;
	      clib_memcpy_fast (&t->reply.ip6, &sol_adv0->target_address,
				sizeof (t->reply.ip6));
	      clib_memcpy_fast (&t->reply.mac, lladdr0->ethernet_address, 6);
	    }
	  if (PREDICT_FALSE ((b1->flags & VLIB_BUFFER_IS_TRACED)))
	    {
	      arping6_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
	      t->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
	      t->type = sol_adv1->icmp.type;
	      clib_memcpy_fast (&t->reply.ip6, &sol_adv1->target_address,
				sizeof (t->reply.ip6));
	      clib_memcpy_fast (&t->reply.mac, lladdr1->ethernet_address, 6);
	    }

	  vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
					   n_left_to_next, bi0, bi1, next0,
					   next1);
	}

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 next0, bi0;
	  vlib_buffer_t *b0;
	  arping_intf_t *aif0;
	  u32 sw_if_index0;
	  ip6_header_t *ip60;
	  icmp6_neighbor_solicitation_or_advertisement_header_t *sol_adv0;
	  icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
	    *lladdr0;

	  bi0 = to_next[0] = from[0];

	  from += 1;
	  n_left_from -= 1;
	  to_next += 1;
	  n_left_to_next -= 1;
	  next0 = ARPING_NEXT_DROP;

	  b0 = vlib_get_buffer (vm, bi0);
	  ip60 = vlib_buffer_get_current (b0);

	  vnet_feature_next (&next0, b0);

	  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];

	  sol_adv0 = ip6_next_header (ip60);
	  lladdr0 =
	    (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
	       *) (sol_adv0 + 1);
	  if (PREDICT_TRUE (sol_adv0->icmp.type ==
			    ICMP6_neighbor_advertisement))
	    {
	      aif0 = am->interfaces[sw_if_index0];
	      if (PREDICT_TRUE (clib_memcmp (&aif0->address.ip.ip6,
					     &sol_adv0->target_address,
					     sizeof (aif0->address.ip.ip6)) ==
				0))
		{
		  clib_memcpy_fast (&aif0->recv.from6.ip6,
				    &sol_adv0->target_address,
				    sizeof (aif0->recv.from6.ip6));
		  clib_memcpy_fast (&aif0->recv.from6.mac,
				    lladdr0->ethernet_address, 6);
		  aif0->reply_count++;
		}
	    }

	  if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
	    {
	      arping6_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
	      t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
	      t->type = sol_adv0->icmp.type;
	      clib_memcpy_fast (&t->reply.ip6, &sol_adv0->target_address,
				sizeof (t->reply.ip6));
	      clib_memcpy_fast (&t->reply.mac, lladdr0->ethernet_address, 6);
	    }

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

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

  return frame->n_vectors;
}

VLIB_REGISTER_NODE (arping6_input_node) =
{
  .name = "arping6-input",.vector_size = sizeof (u32),.format_trace =
    format_arping6_trace,.type = VLIB_NODE_TYPE_INTERNAL,.n_errors =
    ARPING_N_ERROR,.error_strings = arping_error_strings,.n_next_nodes =
    ARPING_N_NEXT,.next_nodes =
  {
  [ARPING6_NEXT_DROP] = "error-drop",[ARPING6_NEXT_IO] = "interface-output",}
,};

VNET_FEATURE_INIT (arping6_feat_node, static) = {
  .arc_name = "ip6-local",
  .node_name = "arping6-input",
  .runs_before = VNET_FEATURES ("ip6-local-end-of-arc"),
};

static clib_error_t *
arping_neighbor_advertisement (vlib_main_t *vm, arping_args_t *args)
{
  vnet_main_t *vnm = vnet_get_main ();
  u32 send_count = 0;

  while (args->repeat > 0)
    {
      send_count++;
      if (args->address.version == AF_IP4)
	{
	  if (args->silence == 0)
	    vlib_cli_output (vm, "Sending %u GARP to %U", send_count,
			     format_ip4_address, &args->address.ip.ip4);
	  ip4_neighbor_advertise (vm, vnm, args->sw_if_index,
				  vlib_get_thread_index (),
				  &args->address.ip.ip4);
	}
      else
	{
	  if (args->silence == 0)
	    vlib_cli_output (vm, "Sending %u Neighbor Advertisement to %U",
			     send_count, format_ip6_address,
			     &args->address.ip.ip6);
	  ip6_neighbor_advertise (vm, vnm, args->sw_if_index,
				  vlib_get_thread_index (),
				  &args->address.ip.ip6);
	}
      args->repeat--;
      if ((args->interval > 0.0) && (args->repeat > 0))
	vlib_process_suspend (vm, args->interval);
    }

  return 0;
}

static void
arping_vnet_feature_enable_disable (vlib_main_t *vm, const char *arc_name,
				    const char *node_name, u32 sw_if_index,
				    int enable_disable, void *feature_config,
				    u32 n_feature_config_bytes)
{
  vlib_worker_thread_barrier_sync (vm);
  vnet_feature_enable_disable (arc_name, node_name, sw_if_index,
			       enable_disable, feature_config,
			       n_feature_config_bytes);
  vlib_worker_thread_barrier_release (vm);
}

static void
arping_vec_validate (vlib_main_t *vm, u32 sw_if_index)
{
  arping_main_t *am = &arping_main;

  if (sw_if_index >= vec_len (am->interfaces))
    {
      vlib_worker_thread_barrier_sync (vm);
      vec_validate (am->interfaces, sw_if_index);
      vlib_worker_thread_barrier_release (vm);
    }
}

static clib_error_t *
arping_neighbor_probe_dst (vlib_main_t *vm, arping_args_t *args)
{
  arping_main_t *am = &arping_main;
  u32 send_count = 0;
  clib_error_t *error;
  arping_intf_t aif;

  /* Disallow multiple sends on the same interface for now. Who needs it? */
  if ((vec_len (am->interfaces) > args->sw_if_index) &&
      (am->interfaces[args->sw_if_index] != 0))
    {
      error = clib_error_return (
	0, "arping command is in progress for the same interface. "
	   "Please try again later.");
      args->rv = VNET_API_ERROR_INVALID_VALUE;
      return error;
    }

  arping_vec_validate (vm, args->sw_if_index);
  clib_memset (&aif, 0, sizeof (aif));
  aif.interval = args->interval;
  aif.repeat = args->repeat;
  aif.reply_count = 0;
  am->interfaces[args->sw_if_index] = &aif;

  clib_memcpy (&aif.address, &args->address, sizeof (aif.address));
  if (args->address.version == AF_IP4)
    arping_vnet_feature_enable_disable (vm, "arp", "arping-input",
					args->sw_if_index, 1, 0, 0);
  else
    arping_vnet_feature_enable_disable (vm, "ip6-local", "arping6-input",
					args->sw_if_index, 1, 0, 0);

  while (args->repeat > 0)
    {
      send_count++;
      if (args->address.version == AF_IP4)
	{
	  if (args->silence == 0)
	    vlib_cli_output (vm, "Sending %u ARP Request to %U", send_count,
			     format_ip4_address, &args->address.ip.ip4);
	  ip4_neighbor_probe_dst (args->sw_if_index, vlib_get_thread_index (),
				  &args->address.ip.ip4);
	}
      else
	{
	  if (args->silence == 0)
	    vlib_cli_output (vm, "Sending %u Neighbor Solicitation  to %U",
			     send_count, format_ip6_address,
			     &args->address.ip.ip6);
	  ip6_neighbor_probe_dst (args->sw_if_index, vlib_get_thread_index (),
				  &args->address.ip.ip6);
	}
      args->repeat--;
      if ((args->interval > 0.0) && (args->repeat > 0))
	vlib_process_suspend (vm, args->interval);
    }

  /* wait for a second on the reply */
  u32 wait_count = 0;
  while ((aif.reply_count < send_count) && (wait_count < 10))
    {
      vlib_process_suspend (vm, 0.1);
      wait_count++;
    }

  if (args->address.version == AF_IP4)
    {
      clib_memcpy (&args->recv.from4, &aif.recv.from4,
		   sizeof (args->recv.from4));
      arping_vnet_feature_enable_disable (vm, "arp", "arping-input",
					  args->sw_if_index, 0, 0, 0);
    }
  else
    {
      clib_memcpy (&args->recv.from6, &aif.recv.from6,
		   sizeof (args->recv.from6));
      arping_vnet_feature_enable_disable (vm, "ip6-local", "arping6-input",
					  args->sw_if_index, 0, 0, 0);
    }
  args->reply_count = aif.reply_count;

  am->interfaces[args->sw_if_index] = 0;

  return 0;
}

void
arping_run_command (vlib_main_t *vm, arping_args_t *args)
{
  if (args->is_garp)
    args->error = arping_neighbor_advertisement (vm, args);
  else
    args->error = arping_neighbor_probe_dst (vm, args);
}

static clib_error_t *
arping_ip_address (vlib_main_t *vm, unformat_input_t *input,
		   vlib_cli_command_t *cmd)
{
  clib_error_t *error = 0;
  vnet_main_t *vnm = vnet_get_main ();
  arping_args_t args = { 0 };
  f64 interval = ARPING_DEFAULT_INTERVAL;

  args.repeat = ARPING_DEFAULT_REPEAT;
  args.interval = ARPING_DEFAULT_INTERVAL;
  args.sw_if_index = ~0;
  args.silence = 0;

  if (unformat (input, "gratuitous"))
    args.is_garp = 1;

  if (unformat (input, "%U", unformat_ip4_address, &args.address.ip.ip4))
    args.address.version = AF_IP4;
  else if (unformat (input, "%U", unformat_ip6_address, &args.address.ip.ip6))
    args.address.version = AF_IP6;
  else
    {
      error = clib_error_return (
	0,
	"expecting IP4/IP6 address `%U'. Usage: arping [gratuitous] <addr> "
	"<intf> [repeat <count>] [interval <secs>]",
	format_unformat_error, input);
      goto done;
    }

  if (!unformat_user (input, unformat_vnet_sw_interface, vnm,
		      &args.sw_if_index))
    {
      error = clib_error_return (0, "unknown interface `%U'",
				 format_unformat_error, input);
      goto done;
    }

  /* parse the rest of the parameters  in a cycle */
  while (!unformat_eof (input, NULL))
    {
      if (unformat (input, "interval"))
	{
	  if (!unformat (input, "%f", &interval))
	    {
	      error = clib_error_return (
		0, "expecting interval (floating point number) got `%U'",
		format_unformat_error, input);
	      goto done;
	    }
	  args.interval = interval;
	}
      else if (unformat (input, "repeat"))
	{
	  if (!unformat (input, "%u", &args.repeat))
	    {
	      error =
		clib_error_return (0, "expecting repeat count but got `%U'",
				   format_unformat_error, input);
	      goto done;
	    }
	}
      else
	{
	  error = clib_error_return (0, "unknown input `%U'",
				     format_unformat_error, input);
	  goto done;
	}
    }

  arping_run_command (vm, &args);

  if (args.reply_count)
    {
      if (args.address.version == AF_IP4)
	vlib_cli_output (vm, "Received %u ARP Replies from %U (%U)",
			 args.reply_count, format_mac_address,
			 &args.recv.from4.mac, format_ip4_address,
			 &args.recv.from4.ip4);
      else
	vlib_cli_output (
	  vm, "Received %u ICMP6 neighbor advertisements from %U (%U)",
	  args.reply_count, format_mac_address, &args.recv.from6.mac,
	  format_ip6_address, &args.recv.from6.ip6);
    }
  else if (args.is_garp == 0)
    vlib_cli_output (vm, "Received 0 Reply");

  error = args.error;
done:
  return error;
}
// clang-format off
/*?
 * This command sends an ARP REQUEST or gratuitous ARP to network hosts. The
 * address can be an IPv4 or IPv6 address.
 *
 * @cliexpar
 * @parblock
 * Example of how to send an IPv4 ARP REQUEST
 * @cliexstart{arping 100.1.1.10 VirtualEthernet0/0/0 repeat 3 interval 1}
 * Sending 1 ARP Request to 100.1.1.10
 * Sending 2 ARP Request to 100.1.1.10
 * Sending 3 ARP Request to 100.1.1.10
 * Received 3 ARP Replies from 52:53:00:00:04:01 (100.1.1.10)
 * @cliexend
 *
 * Example of how to send an IPv6 Neighbor Solicitation
 * @cliexstart{arping 2001:192::2 VirtualEthernet0/0/0 repeat 3 interval 1}
 * Sending 1 Neighbor Solicitation to 2001:192::2
 * Sending 2 Neighbor Solicitation to 2001:192::2
 * Sending 3 Neighbor Solicitation to 2001:192::2
 * Received 3 ICMP6 neighbor advertisements from 52:53:00:00:04:01 (2001:192::2)
 * @cliexend
 *
 * Example of how to send an IPv4 gratuitous ARP
 * @cliexstart{arping gratuitous 100.1.1.100 VirtualEthernet0/0/0 repeat 2}
 * Sending 1 GARP to 100.1.1.100
 * Sending 2 GARP to 100.1.1.100
 * @cliexend
 * @endparblock
 *
?*/
// clang-format on
VLIB_CLI_COMMAND (arping_command, static) = {
  .path = "arping",
  .function = arping_ip_address,
  .short_help = "arping [gratuitous] {addr} {interface}"
		" [interval {sec}] [repeat {cnt}]",
  .is_mp_safe = 1,
};

static clib_error_t *
arping_cli_init (vlib_main_t *vm)
{
  /* initialize binary API */
  arping_plugin_api_hookup (vm);

  return 0;
}

VLIB_INIT_FUNCTION (arping_cli_init);

VLIB_PLUGIN_REGISTER () = {
  .version = VPP_BUILD_VER,
  .description = "Arping (arping)",
};

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