ip6-nd: refactor neighbour advertisement code

Type: refactor

Refactor neighbour advertisement code into inline function
to be used solely in feature nodes.

Change-Id: I1e84c54f9807b4e3d90c37526c78a7afcb0ba087
Signed-off-by: Mohsin Kazmi <sykazmi@cisco.com>
diff --git a/src/vnet/ip6-nd/ip6_nd.c b/src/vnet/ip6-nd/ip6_nd.c
index 311cbf7..772c811 100644
--- a/src/vnet/ip6-nd/ip6_nd.c
+++ b/src/vnet/ip6-nd/ip6_nd.c
@@ -16,6 +16,7 @@
  */
 
 #include <vnet/ip6-nd/ip6_nd.h>
+#include <vnet/ip6-nd/ip6_nd_inline.h>
 
 #include <vnet/ip-neighbor/ip_neighbor.h>
 #include <vnet/ip-neighbor/ip_neighbor_dp.h>
@@ -48,21 +49,12 @@
 static ip6_link_delegate_id_t ip6_nd_delegate_id;
 static ip6_nd_t *ip6_nd_pool;
 
-
-typedef enum
-{
-  ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
-  ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
-  ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
-} icmp6_neighbor_solicitation_or_advertisement_next_t;
-
 static_always_inline uword
 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
 					      vlib_node_runtime_t * node,
 					      vlib_frame_t * frame,
 					      uword is_solicitation)
 {
-  vnet_main_t *vnm = vnet_get_main ();
   ip6_main_t *im = &ip6_main;
   uword n_packets = frame->n_vectors;
   u32 *from, *to_next;
@@ -70,7 +62,6 @@
   icmp6_neighbor_discovery_option_type_t option_type;
   vlib_node_runtime_t *error_node =
     vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
-  int bogus_length;
 
   from = vlib_frame_vector_args (frame);
   n_left_from = n_packets;
@@ -251,57 +242,8 @@
 
 	  if (is_solicitation && error0 == ICMP6_ERROR_NONE)
 	    {
-	      vnet_sw_interface_t *sw_if0;
-	      ethernet_interface_t *eth_if0;
-	      ethernet_header_t *eth0;
-
-	      /* dst address is either source address or the all-nodes mcast addr */
-	      if (!ip6_sadd_unspecified)
-		ip0->dst_address = ip0->src_address;
-	      else
-		ip6_set_reserved_multicast_address (&ip0->dst_address,
-						    IP6_MULTICAST_SCOPE_link_local,
-						    IP6_MULTICAST_GROUP_ID_all_hosts);
-
-	      ip0->src_address = h0->target_address;
-	      ip0->hop_limit = 255;
-	      h0->icmp.type = ICMP6_neighbor_advertisement;
-
-	      sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
-	      ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
-	      eth_if0 =
-		ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
-	      if (eth_if0 && o0)
-		{
-		  clib_memcpy (o0->ethernet_address, &eth_if0->address, 6);
-		  o0->header.type =
-		    ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
-		}
-
-	      h0->advertisement_flags = clib_host_to_net_u32
-		(ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
-		 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
-
-	      h0->icmp.checksum = 0;
-	      h0->icmp.checksum =
-		ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
-						   &bogus_length);
-	      ASSERT (bogus_length == 0);
-
-	      /* Reuse current MAC header, copy SMAC to DMAC and
-	       * interface MAC to SMAC */
-	      vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
-	      eth0 = vlib_buffer_get_current (p0);
-	      clib_memcpy (eth0->dst_address, eth0->src_address, 6);
-	      if (eth_if0)
-		clib_memcpy (eth0->src_address, &eth_if0->address, 6);
-
-	      /* Setup input and output sw_if_index for packet */
-	      ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
-	      vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
-	      vnet_buffer (p0)->sw_if_index[VLIB_RX] =
-		vnet_main.local_interface_sw_if_index;
-
+	      icmp6_send_neighbor_advertisement (vm, p0, ip0, h0, o0,
+						 sw_if_index0);
 	      n_advertisements_sent++;
 	    }
 
diff --git a/src/vnet/ip6-nd/ip6_nd_inline.h b/src/vnet/ip6-nd/ip6_nd_inline.h
new file mode 100644
index 0000000..ad0c3a3
--- /dev/null
+++ b/src/vnet/ip6-nd/ip6_nd_inline.h
@@ -0,0 +1,101 @@
+/*
+ *
+ * ip6_nd_inline.h: ip6 neighbor discovery inline
+ *
+ * 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.
+ */
+
+#ifndef __IP6_ND_INLINE_H__
+#define __IP6_ND_INLINE_H__
+
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/ip/icmp46_packet.h>
+#include <vnet/ip/ip6.h>
+
+typedef enum
+{
+  ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
+  ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
+  ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
+} icmp6_neighbor_solicitation_or_advertisement_next_t;
+
+static_always_inline void
+icmp6_send_neighbor_advertisement (
+  vlib_main_t *vm, vlib_buffer_t *b, ip6_header_t *ip6_h,
+  icmp6_neighbor_solicitation_or_advertisement_header_t *icmp6_nsa,
+  icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
+    *icmp6_nd_ell_addr,
+  u32 sw_if_index0)
+{
+  vnet_main_t *vnm = vnet_get_main ();
+  vnet_sw_interface_t *sw_if;
+  ethernet_interface_t *eth_if;
+  ethernet_header_t *eth;
+  int bogus_length;
+
+  /* dst address is either source address or the all-nodes mcast addr */
+  if (!ip6_address_is_unspecified (&ip6_h->src_address))
+    ip6_h->dst_address = ip6_h->src_address;
+  else
+    ip6_set_reserved_multicast_address (&ip6_h->dst_address,
+					IP6_MULTICAST_SCOPE_link_local,
+					IP6_MULTICAST_GROUP_ID_all_hosts);
+
+  ip6_h->src_address = icmp6_nsa->target_address;
+  ip6_h->hop_limit = 255;
+  icmp6_nsa->icmp.type = ICMP6_neighbor_advertisement;
+
+  sw_if = vnet_get_sup_sw_interface (vnm, sw_if_index0);
+  ASSERT (sw_if->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
+  eth_if = ethernet_get_interface (&ethernet_main, sw_if->hw_if_index);
+  if (eth_if && icmp6_nd_ell_addr)
+    {
+      clib_memcpy (icmp6_nd_ell_addr->ethernet_address, &eth_if->address, 6);
+      icmp6_nd_ell_addr->header.type =
+	ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
+    }
+
+  icmp6_nsa->advertisement_flags =
+    clib_host_to_net_u32 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
+			  ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
+
+  icmp6_nsa->icmp.checksum = 0;
+  icmp6_nsa->icmp.checksum =
+    ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6_h, &bogus_length);
+  ASSERT (bogus_length == 0);
+
+  /* Reuse current MAC header, copy SMAC to DMAC and
+   * interface MAC to SMAC */
+  vlib_buffer_advance (b, -ethernet_buffer_header_size (b));
+  eth = vlib_buffer_get_current (b);
+  clib_memcpy (eth->dst_address, eth->src_address, 6);
+  if (eth_if)
+    clib_memcpy (eth->src_address, &eth_if->address, 6);
+
+  /* Setup input and output sw_if_index for packet */
+  ASSERT (vnet_buffer (b)->sw_if_index[VLIB_RX] == sw_if_index0);
+  vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index0;
+  vnet_buffer (b)->sw_if_index[VLIB_RX] =
+    vnet_main.local_interface_sw_if_index;
+}
+
+#endif /* included_ip6_nd_inline_h */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */