vppinfra: Improve code portability
[vpp.git] / src / plugins / lb / node.c
index ab192af..a37fe11 100644 (file)
@@ -174,26 +174,22 @@ lb_node_get_other_ports6 (ip6_header_t *ip60)
 }
 
 static_always_inline void
-lb_node_get_hash (lb_main_t *lbm, vlib_buffer_t *p, u8 is_input_v4,
-                  u32 *hash, u32 *vip_idx, u8 per_port_vip)
+lb_node_get_hash (lb_main_t *lbm, vlib_buffer_t *p, u8 is_input_v4, u32 *hash,
+                 u32 *vip_idx, u8 per_port_vip)
 {
   vip_port_key_t key;
   clib_bihash_kv_8_8_t kv, value;
+  ip4_header_t *ip40;
+  ip6_header_t *ip60;
+  lb_vip_t *vip0;
+  u64 ports;
 
   /* For vip case, retrieve vip index for ip lookup */
   *vip_idx = vnet_buffer (p)->ip.adj_index[VLIB_TX];
 
-  if (per_port_vip)
-    {
-      /* For per-port-vip case, ip lookup stores dummy index */
-      key.vip_prefix_index = *vip_idx;
-    }
-
+  /* Extract the L4 port number from the packet */
   if (is_input_v4)
     {
-      ip4_header_t *ip40;
-      u64 ports;
-
       ip40 = vlib_buffer_get_current (p);
       if (PREDICT_TRUE(
           ip40->protocol == IP_PROTOCOL_TCP
@@ -202,20 +198,10 @@ lb_node_get_hash (lb_main_t *lbm, vlib_buffer_t *p, u8 is_input_v4,
             | ((u64) ((udp_header_t *) (ip40 + 1))->dst_port);
       else
         ports = lb_node_get_other_ports4 (ip40);
-
-      *hash = lb_hash_hash (*((u64 *) &ip40->address_pair), ports, 0, 0, 0);
-
-      if (per_port_vip)
-        {
-          key.protocol = ip40->protocol;
-          key.port = (u16)(ports & 0xFFFF);
-        }
     }
   else
     {
-      ip6_header_t *ip60;
       ip60 = vlib_buffer_get_current (p);
-      u64 ports;
 
       if (PREDICT_TRUE(
           ip60->protocol == IP_PROTOCOL_TCP
@@ -224,33 +210,68 @@ lb_node_get_hash (lb_main_t *lbm, vlib_buffer_t *p, u8 is_input_v4,
             | ((u64) ((udp_header_t *) (ip60 + 1))->dst_port);
       else
         ports = lb_node_get_other_ports6 (ip60);
-
-      *hash = lb_hash_hash (ip60->src_address.as_u64[0],
-                           ip60->src_address.as_u64[1],
-                           ip60->dst_address.as_u64[0],
-                           ip60->dst_address.as_u64[1], ports);
-
-      if (per_port_vip)
-        {
-          key.protocol = ip60->protocol;
-          key.port = (u16)(ports & 0xFFFF);
-        }
     }
 
-  /* For per-port-vip case, retrieve vip index for vip_port_filter table */
   if (per_port_vip)
     {
+      /* For per-port-vip case, ip lookup stores placeholder index */
+      key.vip_prefix_index = *vip_idx;
+      key.port = (u16) (ports & 0xFFFF);
+      key.rsv = 0;
+      if (is_input_v4)
+       {
+         key.protocol = ip40->protocol;
+       }
+      else
+       {
+         key.protocol = ip60->protocol;
+       }
+
+      /* For per-port-vip case, retrieve vip index for vip_port_filter table */
       kv.key = key.as_u64;
-      if (clib_bihash_search_8_8(&lbm->vip_index_per_port, &kv, &value) < 0)
-        {
-          /* return default vip */
-          *vip_idx = 0;
-          return;
-        }
-      *vip_idx = value.value;
+      if (clib_bihash_search_8_8 (&lbm->vip_index_per_port, &kv, &value) < 0)
+       {
+         /* Set default vip */
+         *vip_idx = 0;
+       }
+      else
+       {
+         *vip_idx = value.value;
+       }
+    }
+
+  vip0 = pool_elt_at_index (lbm->vips, *vip_idx);
+
+  if (is_input_v4)
+    {
+      if (lb_vip_is_src_ip_sticky (vip0))
+       {
+         *hash = lb_hash_hash (*((u64 *) &ip40->address_pair), 0, 0, 0, 0);
+       }
+      else
+       {
+         *hash =
+           lb_hash_hash (*((u64 *) &ip40->address_pair), ports, 0, 0, 0);
+       }
+    }
+  else
+    {
+      if (lb_vip_is_src_ip_sticky (vip0))
+       {
+         *hash = lb_hash_hash (
+           ip60->src_address.as_u64[0], ip60->src_address.as_u64[1],
+           ip60->dst_address.as_u64[0], ip60->dst_address.as_u64[1], 0);
+       }
+      else
+       {
+         *hash = lb_hash_hash (
+           ip60->src_address.as_u64[0], ip60->src_address.as_u64[1],
+           ip60->dst_address.as_u64[0], ip60->dst_address.as_u64[1], ports);
+       }
     }
 }
 
+/* clang-format off */
 static_always_inline uword
 lb_node_fn (vlib_main_t * vm,
             vlib_node_runtime_t * node,
@@ -343,7 +364,7 @@ lb_node_fn (vlib_main_t * vm,
                        vip_index0, lb_time,
                        &available_index0, &asindex0);
 
-          if (PREDICT_TRUE(asindex0 != ~0))
+          if (PREDICT_TRUE(asindex0 != 0))
             {
               //Found an existing entry
               counter = LB_VIP_COUNTER_NEXT_PACKET;
@@ -435,7 +456,6 @@ lb_node_fn (vlib_main_t * vm,
           else if (encap_type == LB_ENCAP_TYPE_L3DSR) /* encap L3DSR*/
             {
               ip4_header_t *ip40;
-              tcp_header_t *th0;
               ip_csum_t csum;
               u32 old_dst, new_dst;
               u8 old_tos, new_tos;
@@ -459,9 +479,20 @@ lb_node_fn (vlib_main_t * vm,
               ip40->checksum = ip_csum_fold (csum);
 
               /* Recomputing L4 checksum after dst-IP modifying */
-              th0 = ip4_next_header (ip40);
-              th0->checksum = 0;
-              th0->checksum = ip4_tcp_udp_compute_checksum (vm, p0, ip40);
+              if (ip40->protocol == IP_PROTOCOL_TCP)
+                {
+                  tcp_header_t *th0;
+                  th0 = ip4_next_header (ip40);
+                  th0->checksum = 0;
+                  th0->checksum = ip4_tcp_udp_compute_checksum (vm, p0, ip40);
+                }
+              else if (ip40->protocol == IP_PROTOCOL_UDP)
+                {
+                  udp_header_t *uh0;
+                  uh0 = ip4_next_header (ip40);
+                  uh0->checksum = 0;
+                  uh0->checksum = ip4_tcp_udp_compute_checksum (vm, p0, ip40);
+                }
             }
           else if ((encap_type == LB_ENCAP_TYPE_NAT4)
               || (encap_type == LB_ENCAP_TYPE_NAT6))
@@ -555,6 +586,7 @@ lb_node_fn (vlib_main_t * vm,
 
   return frame->n_vectors;
 }
+/* clang-format on */
 
 u8 *
 format_nodeport_lb_trace (u8 * s, va_list * args)
@@ -1067,7 +1099,7 @@ VLIB_REGISTER_NODE (lb4_gre6_node) =
     .error_strings = lb_error_strings,
     .n_next_nodes = LB_N_NEXT,
     .next_nodes =
-        { [LB_NEXT_DROP] = "error-drop" }, 
+        { [LB_NEXT_DROP] = "error-drop" },
   };
 
 VLIB_REGISTER_NODE (lb4_gre4_node) =
@@ -1278,4 +1310,3 @@ VLIB_REGISTER_NODE (lb_nat6_in2out_node) =
           [LB_NAT6_IN2OUT_NEXT_LOOKUP] = "ip6-lookup",
       },
   };
-