nat: pick outside addr based on local addr
[vpp.git] / src / plugins / nat / nat44-ed / nat44_ed_in2out.c
index 784dea0..59355e4 100644 (file)
@@ -165,91 +165,112 @@ snat_not_translate_fast (snat_main_t *sm, vlib_node_runtime_t *node,
 }
 
 static int
-nat_ed_alloc_addr_and_port (snat_main_t *sm, u32 rx_fib_index, u32 nat_proto,
-                           u32 thread_index, ip4_address_t r_addr, u16 r_port,
-                           u8 proto, u16 port_per_thread,
-                           u32 snat_thread_index, snat_session_t *s,
-                           ip4_address_t *outside_addr, u16 *outside_port)
+nat_ed_alloc_addr_and_port_with_snat_address (
+  snat_main_t *sm, u32 nat_proto, u32 thread_index, snat_address_t *a,
+  u16 port_per_thread, u32 snat_thread_index, snat_session_t *s,
+  ip4_address_t *outside_addr, u16 *outside_port)
 {
-  int i;
-  snat_address_t *a, *ga = 0;
-
   const u16 port_thread_offset = (port_per_thread * snat_thread_index) + 1024;
 
-  for (i = 0; i < vec_len (sm->addresses); i++)
+  s->o2i.match.daddr = a->addr;
+  /* first try port suggested by caller */
+  u16 port = clib_net_to_host_u16 (*outside_port);
+  u16 port_offset = port - port_thread_offset;
+  if (port <= port_thread_offset ||
+      port > port_thread_offset + port_per_thread)
     {
-      a = sm->addresses + i;
-      switch (nat_proto)
+      /* need to pick a different port, suggested port doesn't fit in
+       * this thread's port range */
+      port_offset = snat_random_port (0, port_per_thread - 1);
+      port = port_thread_offset + port_offset;
+    }
+  u16 attempts = ED_PORT_ALLOC_ATTEMPTS;
+  do
+    {
+      if (NAT_PROTOCOL_ICMP == nat_proto)
        {
-#define _(N, j, n, unused)                                                    \
+         s->o2i.match.sport = clib_host_to_net_u16 (port);
+       }
+      s->o2i.match.dport = clib_host_to_net_u16 (port);
+      if (0 == nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 2))
+       {
+#define _(N, i, n, s)                                                         \
   case NAT_PROTOCOL_##N:                                                      \
-    if (a->fib_index == rx_fib_index)                                         \
-      {                                                                       \
-       s->o2i.match.daddr = a->addr;                                         \
-       /* first try port suggested by caller */                              \
-       u16 port = clib_net_to_host_u16 (*outside_port);                      \
-       u16 port_offset = port - port_thread_offset;                          \
-       if (port <= port_thread_offset ||                                     \
-           port > port_thread_offset + port_per_thread)                      \
-         {                                                                   \
-           /* need to pick a different port, suggested port doesn't fit in   \
-            * this thread's port range */                                    \
-           port_offset = snat_random_port (0, port_per_thread - 1);          \
-           port = port_thread_offset + port_offset;                          \
-         }                                                                   \
-       u16 attempts = ED_PORT_ALLOC_ATTEMPTS;                                \
-       do                                                                    \
-         {                                                                   \
-           if (NAT_PROTOCOL_ICMP == nat_proto)                               \
-             {                                                               \
-               s->o2i.match.sport = clib_host_to_net_u16 (port);             \
-             }                                                               \
-           s->o2i.match.dport = clib_host_to_net_u16 (port);                 \
-           if (0 ==                                                          \
-               nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 2))    \
-             {                                                               \
-               ++a->busy_##n##_port_refcounts[port];                         \
-               a->busy_##n##_ports_per_thread[thread_index]++;               \
-               a->busy_##n##_ports++;                                        \
-               *outside_addr = a->addr;                                      \
-               *outside_port = clib_host_to_net_u16 (port);                  \
-               return 0;                                                     \
-             }                                                               \
-           port_offset = snat_random_port (0, port_per_thread - 1);          \
-           port = port_thread_offset + port_offset;                          \
-           --attempts;                                                       \
-         }                                                                   \
-       while (attempts > 0);                                                 \
-      }                                                                       \
-    else if (a->fib_index == ~0)                                              \
-      {                                                                       \
-       ga = a;                                                               \
-      }                                                                       \
+    ++a->busy_##n##_port_refcounts[port];                                     \
+    a->busy_##n##_ports_per_thread[thread_index]++;                           \
+    a->busy_##n##_ports++;                                                    \
     break;
-
-         foreach_nat_protocol;
-       default:
-         nat_elog_info (sm, "unknown protocol");
-         return 1;
+         switch (nat_proto)
+           {
+             foreach_nat_protocol;
+           default:
+             nat_elog_info (sm, "unknown protocol");
+             return 1;
+           }
+#undef _
+         *outside_addr = a->addr;
+         *outside_port = clib_host_to_net_u16 (port);
+         return 0;
        }
+      port_offset = snat_random_port (0, port_per_thread - 1);
+      port = port_thread_offset + port_offset;
+      --attempts;
     }
+  while (attempts > 0);
+  return 1;
+}
+
+static int
+nat_ed_alloc_addr_and_port (snat_main_t *sm, u32 rx_fib_index, u32 nat_proto,
+                           u32 thread_index, ip4_address_t s_addr,
+                           u16 port_per_thread, u32 snat_thread_index,
+                           snat_session_t *s, ip4_address_t *outside_addr,
+                           u16 *outside_port)
+{
+  int i;
+  snat_address_t *a, *ga = 0;
 
-  if (ga)
+  if (vec_len (sm->addresses) > 0)
     {
-      /* fake fib_index to reuse macro */
-      rx_fib_index = ~0;
-      a = ga;
-      switch (nat_proto)
+      int s_addr_offset = s_addr.as_u32 % vec_len (sm->addresses);
+
+      for (i = s_addr_offset; i < vec_len (sm->addresses); ++i)
        {
-         foreach_nat_protocol;
-       default:
-         nat_elog_info (sm, "unknown protocol");
-         return 1;
+         a = sm->addresses + i;
+         if (a->fib_index == rx_fib_index)
+           {
+             return nat_ed_alloc_addr_and_port_with_snat_address (
+               sm, nat_proto, thread_index, a, port_per_thread,
+               snat_thread_index, s, outside_addr, outside_port);
+           }
+         else if (a->fib_index == ~0)
+           {
+             ga = a;
+           }
        }
-    }
 
-#undef _
+      for (i = 0; i < s_addr_offset; ++i)
+       {
+         a = sm->addresses + i;
+         if (a->fib_index == rx_fib_index)
+           {
+             return nat_ed_alloc_addr_and_port_with_snat_address (
+               sm, nat_proto, thread_index, a, port_per_thread,
+               snat_thread_index, s, outside_addr, outside_port);
+           }
+         else if (a->fib_index == ~0)
+           {
+             ga = a;
+           }
+       }
 
+      if (ga)
+       {
+         return nat_ed_alloc_addr_and_port_with_snat_address (
+           sm, nat_proto, thread_index, a, port_per_thread, snat_thread_index,
+           s, outside_addr, outside_port);
+       }
+    }
   /* Totally out of translations to use... */
   nat_ipfix_logging_addresses_exhausted (thread_index, 0);
   return 1;
@@ -423,7 +444,7 @@ slow_path_ed (snat_main_t *sm, vlib_buffer_t *b, ip4_address_t l_addr,
       nat_6t_flow_txfib_rewrite_set (&s->o2i, rx_fib_index);
 
       if (nat_ed_alloc_addr_and_port (
-           sm, rx_fib_index, nat_proto, thread_index, daddr, dport, proto,
+           sm, rx_fib_index, nat_proto, thread_index, l_addr,
            sm->port_per_thread, tsm->snat_thread_index, s, &outside_addr,
            &outside_port))
        {
@@ -918,10 +939,11 @@ nat44_ed_in2out_slowpath_unknown_proto (snat_main_t *sm, vlib_buffer_t *b,
 }
 
 static inline uword
-nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
-                                         vlib_node_runtime_t * node,
-                                         vlib_frame_t * frame,
-                                         int is_output_feature)
+nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t *vm,
+                                         vlib_node_runtime_t *node,
+                                         vlib_frame_t *frame,
+                                         int is_output_feature,
+                                         int is_multi_worker)
 {
   u32 n_left_from, *from;
   snat_main_t *sm = &snat_main;
@@ -948,9 +970,7 @@ nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
       clib_bihash_kv_16_8_t kv0, value0;
       nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
       nat_6t_flow_t *f = 0;
-      ip4_address_t lookup_saddr, lookup_daddr;
-      u16 lookup_sport, lookup_dport;
-      u8 lookup_protocol;
+      nat_6t_t lookup;
       int lookup_skipped = 0;
 
       b0 = *b;
@@ -981,6 +1001,7 @@ nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
       rx_fib_index0 =
        fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
+      lookup.fib_index = rx_fib_index0;
 
       if (PREDICT_FALSE (ip0->ttl == 1))
        {
@@ -1016,8 +1037,8 @@ nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
              goto trace0;
            }
          int err = nat_get_icmp_session_lookup_values (
-           b0, ip0, &lookup_saddr, &lookup_sport, &lookup_daddr,
-           &lookup_dport, &lookup_protocol);
+           b0, ip0, &lookup.saddr, &lookup.sport, &lookup.daddr,
+           &lookup.dport, &lookup.proto);
          if (err != 0)
            {
              b0->error = node->errors[err];
@@ -1027,30 +1048,27 @@ nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
        }
       else
        {
-         lookup_protocol = ip0->protocol;
-         lookup_saddr = ip0->src_address;
-         lookup_daddr = ip0->dst_address;
-         lookup_sport = vnet_buffer (b0)->ip.reass.l4_src_port;
-         lookup_dport = vnet_buffer (b0)->ip.reass.l4_dst_port;
+         lookup.proto = ip0->protocol;
+         lookup.saddr.as_u32 = ip0->src_address.as_u32;
+         lookup.daddr.as_u32 = ip0->dst_address.as_u32;
+         lookup.sport = vnet_buffer (b0)->ip.reass.l4_src_port;
+         lookup.dport = vnet_buffer (b0)->ip.reass.l4_dst_port;
        }
 
       /* there might be a stashed index in vnet_buffer2 from handoff or
        * classify node, see if it can be used */
-      if (!pool_is_free_index (tsm->sessions,
+      if (is_multi_worker &&
+         !pool_is_free_index (tsm->sessions,
                               vnet_buffer2 (b0)->nat.cached_session_index))
        {
          s0 = pool_elt_at_index (tsm->sessions,
                                  vnet_buffer2 (b0)->nat.cached_session_index);
          if (PREDICT_TRUE (
-               nat_6t_flow_match (&s0->i2o, b0, lookup_saddr, lookup_sport,
-                                  lookup_daddr, lookup_dport, lookup_protocol,
-                                  rx_fib_index0)
+               nat_6t_t_eq (&s0->i2o.match, &lookup)
                // for some hairpinning cases there are two "i2i" flows instead
                // of i2o and o2i as both hosts are on inside
                || (s0->flags & SNAT_SESSION_FLAG_HAIRPINNING &&
-                   nat_6t_flow_match (
-                     &s0->o2i, b0, lookup_saddr, lookup_sport, lookup_daddr,
-                     lookup_dport, lookup_protocol, rx_fib_index0))))
+                   nat_6t_t_eq (&s0->o2i.match, &lookup))))
            {
              /* yes, this is the droid we're looking for */
              lookup_skipped = 1;
@@ -1059,8 +1077,8 @@ nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
          s0 = NULL;
        }
 
-      init_ed_k (&kv0, ip0->src_address, lookup_sport, ip0->dst_address,
-                lookup_dport, rx_fib_index0, lookup_protocol);
+      init_ed_k (&kv0, lookup.saddr, lookup.sport, lookup.daddr, lookup.dport,
+                lookup.fib_index, lookup.proto);
 
       // lookup flow
       if (clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
@@ -1117,16 +1135,12 @@ nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
 
       b0->flags |= VNET_BUFFER_F_IS_NATED;
 
-      if (nat_6t_flow_match (&s0->i2o, b0, lookup_saddr, lookup_sport,
-                            lookup_daddr, lookup_dport, lookup_protocol,
-                            rx_fib_index0))
+      if (nat_6t_t_eq (&s0->i2o.match, &lookup))
        {
          f = &s0->i2o;
        }
       else if (s0->flags & SNAT_SESSION_FLAG_HAIRPINNING &&
-              nat_6t_flow_match (&s0->o2i, b0, lookup_saddr, lookup_sport,
-                                 lookup_daddr, lookup_dport, lookup_protocol,
-                                 rx_fib_index0))
+              nat_6t_t_eq (&s0->o2i.match, &lookup))
        {
          f = &s0->o2i;
        }
@@ -1459,7 +1473,14 @@ VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
                                     vlib_node_runtime_t * node,
                                     vlib_frame_t * frame)
 {
-  return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0);
+  if (snat_main.num_workers > 1)
+    {
+      return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0, 1);
+    }
+  else
+    {
+      return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0, 0);
+    }
 }
 
 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
@@ -1477,7 +1498,14 @@ VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
                                            vlib_node_runtime_t * node,
                                            vlib_frame_t * frame)
 {
-  return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1);
+  if (snat_main.num_workers > 1)
+    {
+      return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1, 1);
+    }
+  else
+    {
+      return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1, 0);
+    }
 }
 
 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {