ipsec: compress ipsec_sa_t so data used by dataplane code fits in cacheline
[vpp.git] / src / vnet / ipsec / ah_decrypt.c
index a2fc07f..87e1de1 100644 (file)
@@ -22,6 +22,7 @@
 #include <vnet/ipsec/ipsec.h>
 #include <vnet/ipsec/esp.h>
 #include <vnet/ipsec/ah.h>
+#include <vnet/ipsec/ipsec_io.h>
 
 #define foreach_ah_decrypt_next \
   _ (DROP, "error-drop")        \
@@ -81,14 +82,14 @@ ah_decrypt_inline (vlib_main_t * vm,
                   vlib_node_runtime_t * node, vlib_frame_t * from_frame,
                   int is_ip6)
 {
-  u32 n_left_from, *from, next_index, *to_next;
+  u32 n_left_from, *from, next_index, *to_next, thread_index;
   ipsec_main_t *im = &ipsec_main;
-  ipsec_proto_main_t *em = &ipsec_proto_main;
   from = vlib_frame_vector_args (from_frame);
   n_left_from = from_frame->n_vectors;
-  int icv_size = 0;
+  int icv_size;
 
   next_index = node->cached_next_index;
+  thread_index = vm->thread_index;
 
   while (n_left_from > 0)
     {
@@ -131,6 +132,9 @@ ah_decrypt_inline (vlib_main_t * vm,
          sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
          sa0 = pool_elt_at_index (im->sad, sa_index0);
 
+         vlib_prefetch_combined_counter (&ipsec_sa_counters,
+                                         thread_index, sa_index0);
+
          if (is_ip6)
            {
              ip6_ext_header_t *prev = NULL;
@@ -147,39 +151,31 @@ ah_decrypt_inline (vlib_main_t * vm,
          seq = clib_host_to_net_u32 (ah0->seq_no);
 
          /* anti-replay check */
-         if (sa0->use_anti_replay)
+         if (ipsec_sa_is_set_USE_ANTI_REPLAY (sa0))
            {
              int rv = 0;
 
-             if (PREDICT_TRUE (sa0->use_esn))
+             if (PREDICT_TRUE (ipsec_sa_is_set_USE_EXTENDED_SEQ_NUM (sa0)))
                rv = esp_replay_check_esn (sa0, seq);
              else
                rv = esp_replay_check (sa0, seq);
 
              if (PREDICT_FALSE (rv))
                {
-                 if (is_ip6)
-                   vlib_node_increment_counter (vm,
-                                                ah6_decrypt_node.index,
-                                                AH_DECRYPT_ERROR_REPLAY, 1);
-                 else
-                   vlib_node_increment_counter (vm,
-                                                ah4_decrypt_node.index,
-                                                AH_DECRYPT_ERROR_REPLAY, 1);
+                 i_b0->error = node->errors[AH_DECRYPT_ERROR_REPLAY];
                  goto trace;
                }
            }
 
+         vlib_increment_combined_counter
+           (&ipsec_sa_counters, thread_index, sa_index0,
+            1, i_b0->current_length);
 
-         sa0->total_data_size += i_b0->current_length;
-         icv_size =
-           em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
+         icv_size = sa0->integ_trunc_size;
          if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
            {
              u8 sig[64];
-             u8 digest[64];
-             clib_memset (sig, 0, sizeof (sig));
-             clib_memset (digest, 0, sizeof (digest));
+             u8 digest[icv_size];
              u8 *icv = ah0->auth_data;
              memcpy (digest, icv, icv_size);
              clib_memset (icv, 0, icv_size);
@@ -206,28 +202,18 @@ ah_decrypt_inline (vlib_main_t * vm,
                  icv_padding_len =
                    ah_calc_icv_padding_len (icv_size, 0 /* is_ipv6 */ );
                }
-             hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len,
-                        (u8 *) ih4, i_b0->current_length, sig, sa0->use_esn,
-                        sa0->seq_hi);
+             hmac_calc (vm, sa0, (u8 *) ih4, i_b0->current_length, sig);
 
              if (PREDICT_FALSE (memcmp (digest, sig, icv_size)))
                {
-                 if (is_ip6)
-                   vlib_node_increment_counter (vm,
-                                                ah6_decrypt_node.index,
-                                                AH_DECRYPT_ERROR_INTEG_ERROR,
-                                                1);
-                 else
-                   vlib_node_increment_counter (vm,
-                                                ah4_decrypt_node.index,
-                                                AH_DECRYPT_ERROR_INTEG_ERROR,
-                                                1);
+                 i_b0->error = node->errors[AH_DECRYPT_ERROR_INTEG_ERROR];
                  goto trace;
                }
 
-             if (PREDICT_TRUE (sa0->use_anti_replay))
+             if (PREDICT_TRUE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa0)))
                {
-                 if (PREDICT_TRUE (sa0->use_esn))
+                 if (PREDICT_TRUE
+                     (ipsec_sa_is_set_USE_EXTENDED_SEQ_NUM (sa0)))
                    esp_replay_advance_esn (sa0, seq);
                  else
                    esp_replay_advance (sa0, seq);
@@ -240,7 +226,7 @@ ah_decrypt_inline (vlib_main_t * vm,
                               icv_padding_len);
          i_b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
 
-         if (PREDICT_TRUE (sa0->is_tunnel))
+         if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0)))
            {                   /* tunnel mode */
              if (PREDICT_TRUE (ah0->nexthdr == IP_PROTOCOL_IP_IN_IP))
                next0 = AH_DECRYPT_NEXT_IP4_INPUT;
@@ -248,16 +234,8 @@ ah_decrypt_inline (vlib_main_t * vm,
                next0 = AH_DECRYPT_NEXT_IP6_INPUT;
              else
                {
-                 if (is_ip6)
-                   vlib_node_increment_counter (vm,
-                                                ah6_decrypt_node.index,
-                                                AH_DECRYPT_ERROR_DECRYPTION_FAILED,
-                                                1);
-                 else
-                   vlib_node_increment_counter (vm,
-                                                ah4_decrypt_node.index,
-                                                AH_DECRYPT_ERROR_DECRYPTION_FAILED,
-                                                1);
+                 i_b0->error =
+                   node->errors[AH_DECRYPT_ERROR_DECRYPTION_FAILED];
                  goto trace;
                }
            }
@@ -320,14 +298,8 @@ ah_decrypt_inline (vlib_main_t * vm,
        }
       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
     }
-  if (is_ip6)
-    vlib_node_increment_counter (vm, ah6_decrypt_node.index,
-                                AH_DECRYPT_ERROR_RX_PKTS,
-                                from_frame->n_vectors);
-  else
-    vlib_node_increment_counter (vm, ah4_decrypt_node.index,
-                                AH_DECRYPT_ERROR_RX_PKTS,
-                                from_frame->n_vectors);
+  vlib_node_increment_counter (vm, node->node_index, AH_DECRYPT_ERROR_RX_PKTS,
+                              from_frame->n_vectors);
 
   return from_frame->n_vectors;
 }