ip: Router ID included in flow hash
[vpp.git] / src / plugins / cnat / cnat_node_vip.c
index d041606..ffe5899 100644 (file)
 #include <vlibmemory/api.h>
 #include <cnat/cnat_node.h>
 #include <cnat/cnat_translation.h>
+#include <cnat/cnat_inline.h>
+#include <cnat/cnat_src_policy.h>
 
 #include <vnet/dpo/load_balance.h>
 #include <vnet/dpo/load_balance_map.h>
 
+#include <vnet/ip/ip4_inlines.h>
+#include <vnet/ip/ip6_inlines.h>
+
 typedef struct cnat_translation_trace_t_
 {
   cnat_session_t session;
@@ -44,30 +49,27 @@ format_cnat_translation_trace (u8 * s, va_list * args)
 {
   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
-  cnat_translation_trace_t *t =
-    va_arg (*args, cnat_translation_trace_t *);
+  cnat_translation_trace_t *t = va_arg (*args, cnat_translation_trace_t *);
 
   if (t->found_session)
     s = format (s, "found: %U", format_cnat_session, &t->session, 1);
   else if (t->created_session)
     s = format (s, "created: %U\n  tr: %U",
                format_cnat_session, &t->session, 1,
-               format_cnat_translation,
-               &t->tr, 0);
+               format_cnat_translation, &t->tr, 0);
   else if (t->has_tr)
-    s = format (s, "tr pass: %U", format_cnat_translation,
-       &t->tr, 0);
+    s = format (s, "tr pass: %U", format_cnat_translation, &t->tr, 0);
   else
     s = format (s, "not found");
   return s;
 }
 
 /* CNat sub for NAT behind a fib entry (VIP or interposed real IP) */
-always_inline uword
-cnat_vip_inline (vlib_main_t * vm,
-                  vlib_node_runtime_t * node,
-                  vlib_buffer_t * b,
-                  cnat_node_ctx_t * ctx, int rv, cnat_session_t * session)
+static uword
+cnat_vip_node_fn (vlib_main_t * vm,
+                 vlib_node_runtime_t * node,
+                 vlib_buffer_t * b,
+                 cnat_node_ctx_t * ctx, int rv, cnat_session_t * session)
 {
   vlib_combined_counter_main_t *cntm = &cnat_translation_counters;
   const cnat_translation_t *ct = NULL;
@@ -79,6 +81,7 @@ cnat_vip_inline (vlib_main_t * vm,
   u16 next0;
   index_t cti;
   int created_session = 0;
+  cnat_src_policy_main_t *cspm = &cnat_src_policy_main;
   if (AF_IP4 == ctx->af)
     {
       ip4 = vlib_buffer_get_current (b);
@@ -103,18 +106,15 @@ cnat_vip_inline (vlib_main_t * vm,
       goto trace;
     }
 
-  ct = cnat_find_translation (cc->parent_cci,
-                               clib_host_to_net_u16 (udp0->dst_port),
-                               iproto);
-
   if (!rv)
     {
       /* session table hit */
       cnat_timestamp_update (session->value.cs_ts_index, ctx->now);
 
-      if (NULL != ct)
+      if (INDEX_INVALID != session->value.cs_lbi)
        {
          /* Translate & follow the translation given LB */
+         ct = cnat_translation_get (session->value.ct_index);
          next0 = ct->ct_lb.dpoi_next_node;
          vnet_buffer (b)->ip.adj_index[VLIB_TX] = session->value.cs_lbi;
        }
@@ -133,6 +133,9 @@ cnat_vip_inline (vlib_main_t * vm,
     }
   else
     {
+      ct =
+       cnat_find_translation (cc->parent_cci,
+                              clib_host_to_net_u16 (udp0->dst_port), iproto);
       if (NULL == ct)
        {
          /* Dont translate & Follow the fib programming */
@@ -161,7 +164,7 @@ cnat_vip_inline (vlib_main_t * vm,
       hash_c0 = (AF_IP4 == ctx->af ?
                 ip4_compute_flow_hash (ip4, lb0->lb_hash_config) :
                 ip6_compute_flow_hash (ip6, lb0->lb_hash_config));
-      bucket0 = hash_c0 & lb0->lb_n_buckets_minus_1;
+      bucket0 = hash_c0 % lb0->lb_n_buckets;
       dpo0 = load_balance_get_fwd_bucket (lb0, bucket0);
 
       /* add the session */
@@ -190,29 +193,20 @@ cnat_vip_inline (vlib_main_t * vm,
       session->value.cs_port[VLIB_RX] =
        clib_host_to_net_u16 (trk0->ct_ep[VLIB_RX].ce_port);
 
-      session->value.flags = 0;
-      if (!session->value.cs_port[VLIB_RX])
-       {
-         u16 sport;
-         sport = udp0->src_port;
-         /* Allocate a port only if asked and if we actually sNATed */
-         if ((ct->flags & CNAT_TRANSLATION_FLAG_ALLOCATE_PORT)
-              && (rsession_flags & CNAT_SESSION_FLAG_HAS_SNAT)) {
-           sport = 0; /* force allocation */
-           session->value.flags |= CNAT_SESSION_FLAG_ALLOC_PORT;
-           rv = cnat_allocate_port (&sport, iproto);
-           if (rv)
-             {
-               vlib_node_increment_counter (vm, cnat_vip_ip4_node.index,
-                                           CNAT_ERROR_EXHAUSTED_PORTS, 1);
-               next0 = CNAT_TRANSLATION_NEXT_DROP;
-               goto trace;
-             }
-           }
+      session->value.ct_index = ct - cnat_translation_pool;
+      session->value.cs_lbi = dpo0->dpoi_index;
 
-         session->value.cs_port[VLIB_RX] = sport;
+      rv = cspm->vip_policy (vm, b, session, &rsession_flags, ct, ctx);
+      if (CNAT_SOURCE_ERROR_USE_DEFAULT == rv)
+       rv = cspm->default_policy (vm, b, session, &rsession_flags, ct, ctx);
+      if (rv)
+       {
+         if (CNAT_SOURCE_ERROR_EXHAUSTED_PORTS == rv)
+           vlib_node_increment_counter (vm, cnat_vip_ip4_node.index,
+                                        CNAT_ERROR_EXHAUSTED_PORTS, 1);
+         next0 = CNAT_TRANSLATION_NEXT_DROP;
+         goto trace;
        }
-      session->value.cs_lbi = dpo0->dpoi_index;
 
       /* refcnt session in current client */
       cnat_client_cnt_session (cc);
@@ -232,7 +226,7 @@ cnat_vip_inline (vlib_main_t * vm,
     {
       cti = ct - cnat_translation_pool;
       vlib_increment_combined_counter (cntm, ctx->thread_index, cti, 1,
-                                 vlib_buffer_length_in_chain (vm, b));
+                                      vlib_buffer_length_in_chain (vm, b));
     }
 
 trace:
@@ -254,25 +248,25 @@ trace:
 }
 
 VLIB_NODE_FN (cnat_vip_ip4_node) (vlib_main_t * vm,
-                                   vlib_node_runtime_t * node,
-                                   vlib_frame_t * frame)
+                                 vlib_node_runtime_t * node,
+                                 vlib_frame_t * frame)
 {
   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
-    return cnat_node_inline (vm, node, frame, cnat_vip_inline, AF_IP4,
-                              1 /* do_trace */ );
-  return cnat_node_inline (vm, node, frame, cnat_vip_inline, AF_IP4,
-                            0 /* do_trace */ );
+    return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP4,
+                            1 /* do_trace */ );
+  return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP4,
+                          0 /* do_trace */ );
 }
 
 VLIB_NODE_FN (cnat_vip_ip6_node) (vlib_main_t * vm,
-                                   vlib_node_runtime_t * node,
-                                   vlib_frame_t * frame)
+                                 vlib_node_runtime_t * node,
+                                 vlib_frame_t * frame)
 {
   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
-    return cnat_node_inline (vm, node, frame, cnat_vip_inline, AF_IP6,
-                              1 /* do_trace */ );
-  return cnat_node_inline (vm, node, frame, cnat_vip_inline, AF_IP6,
-                            0 /* do_trace */ );
+    return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP6,
+                            1 /* do_trace */ );
+  return cnat_node_inline (vm, node, frame, cnat_vip_node_fn, AF_IP6,
+                          0 /* do_trace */ );
 }
 
 /* *INDENT-OFF* */
@@ -306,3 +300,10 @@ VLIB_REGISTER_NODE (cnat_vip_ip6_node) =
 };
 /* *INDENT-ON* */
 
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */