NAT44: fix next_src_nat (VPP-1384)
[vpp.git] / src / plugins / nat / out2in.c
index 80465b0..d308308 100755 (executable)
@@ -188,6 +188,7 @@ create_session_for_static_mapping (snat_main_t *sm,
   s = nat_session_alloc_or_recycle (sm, u, thread_index);
   if (!s)
     {
+      nat44_delete_user_with_no_session (sm, u, thread_index);
       nat_log_warn ("create NAT session failed");
       return 0;
     }
@@ -626,7 +627,7 @@ nat_out2in_sm_unknown_proto (snat_main_t *sm,
   m_key.addr = ip->dst_address;
   m_key.port = 0;
   m_key.protocol = 0;
-  m_key.fib_index = rx_fib_index;
+  m_key.fib_index = 0;
   kv.key = m_key.as_u64;
   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
     return 1;
@@ -653,7 +654,7 @@ snat_out2in_node_fn (vlib_main_t * vm,
   u32 pkts_processed = 0;
   snat_main_t * sm = &snat_main;
   f64 now = vlib_time_now (vm);
-  u32 thread_index = vlib_get_thread_index ();
+  u32 thread_index = vm->thread_index;
 
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
@@ -786,8 +787,7 @@ snat_out2in_node_fn (vlib_main_t * vm,
                      && (udp0->dst_port ==
                          clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
                    {
-                     vnet_feature_next
-                       (vnet_buffer (b0)->sw_if_index[VLIB_RX], &next0, b0);
+                     vnet_feature_next (&next0, b0);
                      goto trace0;
                    }
 
@@ -795,8 +795,8 @@ snat_out2in_node_fn (vlib_main_t * vm,
                     {
                       b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
                       next0 = SNAT_OUT2IN_NEXT_DROP;
-                      goto trace0;
                     }
+                  goto trace0;
                 }
 
               /* Create session initiated by host from external network */
@@ -938,8 +938,7 @@ snat_out2in_node_fn (vlib_main_t * vm,
                      && (udp1->dst_port ==
                          clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
                    {
-                     vnet_feature_next
-                       (vnet_buffer (b1)->sw_if_index[VLIB_RX], &next1, b1);
+                     vnet_feature_next (&next1, b1);
                      goto trace1;
                    }
 
@@ -947,8 +946,8 @@ snat_out2in_node_fn (vlib_main_t * vm,
                     {
                       b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
                       next1 = SNAT_OUT2IN_NEXT_DROP;
-                      goto trace1;
                     }
+                  goto trace1;
                 }
 
               /* Create session initiated by host from external network */
@@ -1126,8 +1125,7 @@ snat_out2in_node_fn (vlib_main_t * vm,
                      && (udp0->dst_port ==
                          clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
                    {
-                     vnet_feature_next
-                       (vnet_buffer (b0)->sw_if_index[VLIB_RX], &next0, b0);
+                     vnet_feature_next (&next0, b0);
                      goto trace00;
                    }
 
@@ -1135,8 +1133,8 @@ snat_out2in_node_fn (vlib_main_t * vm,
                     {
                       b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
                       next0 = SNAT_OUT2IN_NEXT_DROP;
-                      goto trace00;
                     }
+                  goto trace00;
                 }
 
               /* Create session initiated by host from external network */
@@ -1256,7 +1254,7 @@ nat44_out2in_reass_node_fn (vlib_main_t * vm,
   u32 pkts_processed = 0;
   snat_main_t *sm = &snat_main;
   f64 now = vlib_time_now (vm);
-  u32 thread_index = vlib_get_thread_index ();
+  u32 thread_index = vm->thread_index;
   snat_main_per_thread_data_t *per_thread_data =
     &sm->per_thread_data[thread_index];
   u32 *fragments_to_drop = 0;
@@ -1352,9 +1350,7 @@ nat44_out2in_reass_node_fn (vlib_main_t * vm,
                           && (udp0->dst_port
                               == clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
                        {
-                          vnet_feature_next
-                            (vnet_buffer (b0)->sw_if_index[VLIB_RX],
-                             &next0, b0);
+                          vnet_feature_next (&next0, b0);
                           goto trace0;
                         }
 
@@ -1604,6 +1600,7 @@ create_session_for_static_mapping_ed (snat_main_t * sm,
   s = nat_session_alloc_or_recycle (sm, u, thread_index);
   if (!s)
     {
+      nat44_delete_user_with_no_session (sm, u, thread_index);
       nat_log_warn ("create NAT session failed");
       return 0;
     }
@@ -1715,13 +1712,13 @@ icmp_get_ed_key(ip4_header_t *ip0, nat_ed_ses_key_t *p_key0)
 
 static int
 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
-              u16 dst_port, u32 thread_index)
+              u16 dst_port, u32 thread_index, u32 rx_fib_index)
 {
   clib_bihash_kv_16_8_t kv, value;
   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
 
   make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
-              sm->inside_fib_index, src_port, dst_port);
+              rx_fib_index, src_port, dst_port);
   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
     return 1;
 
@@ -1785,6 +1782,7 @@ create_bypass_for_fwd(snat_main_t * sm, ip4_header_t * ip, u32 rx_fib_index,
       s = nat_session_alloc_or_recycle (sm, u, thread_index);
       if (!s)
         {
+          nat44_delete_user_with_no_session (sm, u, thread_index);
           nat_log_warn ("create NAT session failed");
           return;
         }
@@ -1872,7 +1870,8 @@ icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
           else
             {
               dont_translate = 1;
-              if (next_src_nat(sm, ip, key.proto, key.l_port, key.r_port, thread_index))
+              if (next_src_nat(sm, ip, key.proto, key.l_port, key.r_port,
+                               thread_index, rx_fib_index))
                 {
                   next = NAT44_ED_OUT2IN_NEXT_IN2OUT;
                   goto out;
@@ -1962,7 +1961,7 @@ nat44_ed_out2in_unknown_proto (snat_main_t *sm,
           return 0;
         }
 
-      make_sm_kv (&kv, &ip->dst_address, 0, rx_fib_index, 0);
+      make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
       if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
         {
           b->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
@@ -1985,6 +1984,7 @@ nat44_ed_out2in_unknown_proto (snat_main_t *sm,
       s = nat_session_alloc_or_recycle (sm, u, thread_index);
       if (!s)
         {
+          nat44_delete_user_with_no_session (sm, u, thread_index);
           nat_log_warn ("create NAT session failed");
           return 0;
         }
@@ -2038,7 +2038,7 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
   nat44_ed_out2in_next_t next_index;
   snat_main_t *sm = &snat_main;
   f64 now = vlib_time_now (vm);
-  u32 thread_index = vlib_get_thread_index ();
+  u32 thread_index = vm->thread_index;
   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
 
   stats_node_index = is_slow_path ? nat44_ed_out2in_slowpath_node.index :
@@ -2182,8 +2182,7 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
                           && (udp0->dst_port ==
                           clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
                         {
-                          vnet_feature_next
-                            (vnet_buffer (b0)->sw_if_index[VLIB_RX], &next0, b0);
+                          vnet_feature_next (&next0, b0);
                           goto trace00;
                         }
 
@@ -2196,7 +2195,7 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
                         {
                           if (next_src_nat(sm, ip0, ip0->protocol,
                                            udp0->src_port, udp0->dst_port,
-                                           thread_index))
+                                           thread_index, rx_fib_index0))
                             {
                               next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
                               goto trace00;
@@ -2386,8 +2385,7 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
                           && (udp1->dst_port ==
                           clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
                         {
-                          vnet_feature_next
-                            (vnet_buffer (b1)->sw_if_index[VLIB_RX], &next1, b1);
+                          vnet_feature_next (&next1, b1);
                           goto trace01;
                         }
 
@@ -2400,7 +2398,7 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
                         {
                           if (next_src_nat(sm, ip1, ip1->protocol,
                                            udp1->src_port, udp1->dst_port,
-                                           thread_index))
+                                           thread_index, rx_fib_index1))
                             {
                               next1 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
                               goto trace01;
@@ -2622,8 +2620,7 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
                           && (udp0->dst_port ==
                           clib_host_to_net_u16(UDP_DST_PORT_dhcp_to_client))))
                         {
-                          vnet_feature_next
-                            (vnet_buffer (b0)->sw_if_index[VLIB_RX], &next0, b0);
+                          vnet_feature_next (&next0, b0);
                           goto trace0;
                         }
 
@@ -2636,7 +2633,7 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
                         {
                           if (next_src_nat(sm, ip0, ip0->protocol,
                                            udp0->src_port, udp0->dst_port,
-                                           thread_index))
+                                           thread_index, rx_fib_index0))
                             {
                               next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
                               goto trace0;
@@ -2837,7 +2834,7 @@ snat_det_out2in_node_fn (vlib_main_t * vm,
   snat_out2in_next_t next_index;
   u32 pkts_processed = 0;
   snat_main_t * sm = &snat_main;
-  u32 thread_index = vlib_get_thread_index ();
+  u32 thread_index = vm->thread_index;
 
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
@@ -3473,7 +3470,7 @@ snat_out2in_worker_handoff_fn (vlib_main_t * vm,
   u32 n_left_to_next_worker = 0, *to_next_worker = 0;
   u32 next_worker_index = 0;
   u32 current_worker_index = ~0;
-  u32 thread_index = vlib_get_thread_index ();
+  u32 thread_index = vm->thread_index;
   vlib_frame_t *d = 0;
 
   ASSERT (vec_len (sm->workers));
@@ -3700,7 +3697,7 @@ snat_out2in_fast_node_fn (vlib_main_t * vm,
           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
          rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
 
-         vnet_feature_next (sw_if_index0, &next0, b0);
+         vnet_feature_next (&next0, b0);
 
           if (PREDICT_FALSE(ip0->ttl == 1))
             {