NAT44: client-IP based session affinity for load-balancing (VPP-1297)
[vpp.git] / src / plugins / nat / in2out.c
index 9c8b2c6..0fe3633 100755 (executable)
@@ -258,7 +258,7 @@ snat_not_translate (snat_main_t * sm, vlib_node_runtime_t *node,
                               &value0))
     {
       /* or is static mappings */
-      if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+      if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0, 0))
         return 0;
     }
   else
@@ -387,7 +387,7 @@ static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
   key1.protocol = key0->protocol;
 
   /* First try to match static mapping by local address and port */
-  if (snat_static_mapping_match (sm, *key0, &key1, 0, 0, 0, 0))
+  if (snat_static_mapping_match (sm, *key0, &key1, 0, 0, 0, 0, 0))
     {
       /* Try to create dynamic translation */
       if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
@@ -674,7 +674,7 @@ u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
     }
   key0.fib_index = rx_fib_index0;
 
-  if (snat_static_mapping_match(sm, key0, &sm0, 0, &is_addr_only, 0, 0))
+  if (snat_static_mapping_match(sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0))
     {
       if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
           IP_PROTOCOL_ICMP, rx_fib_index0)))
@@ -875,7 +875,7 @@ snat_hairpinning (snat_main_t *sm,
   kv0.key = key0.as_u64;
 
   /* Check if destination is static mappings */
-  if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+  if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0, 0))
     {
       new_dst_addr0 = sm0.addr.as_u32;
       new_dst_port0 = sm0.port;
@@ -1006,7 +1006,7 @@ snat_icmp_hairpinning (snat_main_t *sm,
       if (rv)
         {
           /* or static mappings */
-          if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+          if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0, 0))
             {
               new_dst_addr0 = sm0.addr.as_u32;
               vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
@@ -2031,7 +2031,7 @@ nat44_reass_hairpinning (snat_main_t *sm,
   udp0 = ip4_next_header (ip0);
 
   /* Check if destination is static mappings */
-  if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+  if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0, 0))
     {
       new_dst_addr0 = sm0.addr.as_u32;
       new_dst_port0 = sm0.port;
@@ -2535,7 +2535,7 @@ slow_path_ed (snat_main_t *sm,
   snat_session_t *s;
   snat_user_t *u;
   snat_session_key_t key0, key1;
-  u8 lb = 0, is_sm = 0;
+  lb_nat_type_t lb = 0, is_sm = 0;
   u32 address_index = ~0;
   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
   nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
@@ -2565,7 +2565,7 @@ slow_path_ed (snat_main_t *sm,
   key0.fib_index = rx_fib_index;
   key1.fib_index = sm->outside_fib_index;
   /* First try to match static mapping by local address and port */
-  if (snat_static_mapping_match (sm, key0, &key1, 0, 0, 0, &lb))
+  if (snat_static_mapping_match (sm, key0, &key1, 0, 0, 0, &lb, 0))
     {
       /* Try to create dynamic translation */
       if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index,
@@ -2691,7 +2691,7 @@ nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t *node,
       key0.protocol = proto;
       key0.fib_index = sm->outside_fib_index;
       /* or is static mappings */
-      if (!snat_static_mapping_match(sm, key0, &key1, 1, 0, 0, 0))
+      if (!snat_static_mapping_match(sm, key0, &key1, 1, 0, 0, 0, 0))
         return 0;
     }
   else
@@ -2764,22 +2764,24 @@ nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
 static_always_inline int
 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
                                        u8 proto, u16 src_port, u16 dst_port,
-                                       u32 thread_index, u32 sw_if_index)
+                                       u32 thread_index, u32 rx_sw_if_index,
+                                       u32 tx_sw_if_index)
 {
   clib_bihash_kv_16_8_t kv, value;
   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
   snat_interface_t *i;
   snat_session_t *s;
-  u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
+  u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
+  u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
 
   /* src NAT check */
-  make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, fib_index,
+  make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, tx_fib_index,
               src_port, dst_port);
   if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
     return 1;
 
   /* dst NAT check */
-  make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, fib_index,
+  make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, rx_fib_index,
               dst_port, src_port);
   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
   {
@@ -2790,7 +2792,7 @@ nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
     /* hairpinning */
     pool_foreach (i, sm->output_feature_interfaces,
     ({
-      if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
+      if ((nat_interface_is_inside(i)) && (rx_sw_if_index == i->sw_if_index))
         return 0;
     }));
     return 1;
@@ -2838,7 +2840,8 @@ icmp_match_in2out_ed(snat_main_t *sm, vlib_node_runtime_t *node,
       if (vnet_buffer(b)->sw_if_index[VLIB_TX] != ~0)
         {
           if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(sm, ip,
-              key.proto, key.l_port, key.r_port, thread_index, sw_if_index)))
+              key.proto, key.l_port, key.r_port, thread_index, sw_if_index,
+              vnet_buffer(b)->sw_if_index[VLIB_TX])))
             {
               dont_translate = 1;
               goto out;
@@ -3285,7 +3288,8 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                     {
                       if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(
                           sm, ip0, ip0->protocol, udp0->src_port,
-                          udp0->dst_port, thread_index, sw_if_index0)))
+                          udp0->dst_port, thread_index, sw_if_index0,
+                          vnet_buffer(b0)->sw_if_index[VLIB_TX])))
                         goto trace00;
                     }
                   else
@@ -3468,7 +3472,8 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                     {
                       if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(
                           sm, ip1, ip1->protocol, udp1->src_port,
-                          udp1->dst_port, thread_index, sw_if_index1)))
+                          udp1->dst_port, thread_index, sw_if_index1,
+                          vnet_buffer(b1)->sw_if_index[VLIB_TX])))
                         goto trace01;
                     }
                   else
@@ -3680,7 +3685,8 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
                     {
                       if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(
                           sm, ip0, ip0->protocol, udp0->src_port,
-                          udp0->dst_port, thread_index, sw_if_index0)))
+                          udp0->dst_port, thread_index, sw_if_index0,
+                          vnet_buffer(b0)->sw_if_index[VLIB_TX])))
                         goto trace0;
                     }
                   else
@@ -5315,7 +5321,7 @@ snat_in2out_fast_static_map_fn (vlib_main_t * vm,
           key0.port = udp0->src_port;
           key0.fib_index = rx_fib_index0;
 
-          if (snat_static_mapping_match(sm, key0, &sm0, 0, 0, 0, 0))
+          if (snat_static_mapping_match(sm, key0, &sm0, 0, 0, 0, 0, 0))
             {
               b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
               next0= SNAT_IN2OUT_NEXT_DROP;