IPSEC: move SA counters into the stats segment
[vpp.git] / src / vnet / ipsec / ah_decrypt.c
index 34ea000..629e7f0 100644 (file)
@@ -60,6 +60,7 @@ static char *ah_decrypt_error_strings[] = {
 typedef struct
 {
   ipsec_integ_alg_t integ_alg;
+  u32 seq_num;
 } ah_decrypt_trace_t;
 
 /* packet trace format function */
@@ -70,7 +71,8 @@ format_ah_decrypt_trace (u8 * s, va_list * args)
   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
   ah_decrypt_trace_t *t = va_arg (*args, ah_decrypt_trace_t *);
 
-  s = format (s, "ah: integrity %U", format_ipsec_integ_alg, t->integ_alg);
+  s = format (s, "ah: integrity %U seq-num %d",
+             format_ipsec_integ_alg, t->integ_alg, t->seq_num);
   return s;
 }
 
@@ -79,7 +81,7 @@ 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);
@@ -87,6 +89,7 @@ ah_decrypt_inline (vlib_main_t * vm,
   int icv_size = 0;
 
   next_index = node->cached_next_index;
+  thread_index = vm->thread_index;
 
   while (n_left_from > 0)
     {
@@ -129,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;
@@ -143,8 +149,8 @@ ah_decrypt_inline (vlib_main_t * vm,
            }
 
          seq = clib_host_to_net_u32 (ah0->seq_no);
+
          /* anti-replay check */
-         //TODO UT remaining
          if (sa0->use_anti_replay)
            {
              int rv = 0;
@@ -156,23 +162,16 @@ ah_decrypt_inline (vlib_main_t * vm,
 
              if (PREDICT_FALSE (rv))
                {
-                 clib_warning ("anti-replay SPI %u seq %u", sa0->spi, seq);
-                 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);
-                 to_next[0] = i_bi0;
-                 to_next += 1;
+                 vlib_node_increment_counter (vm, node->node_index,
+                                              AH_DECRYPT_ERROR_REPLAY, 1);
                  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;
          if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
@@ -207,28 +206,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 (sa0->integ_alg, sa0->integ_key.data,
+                        sa0->integ_key.len, (u8 *) ih4, i_b0->current_length,
+                        sig, sa0->use_esn, sa0->seq_hi);
 
              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);
-                 to_next[0] = i_bi0;
-                 to_next += 1;
+                 vlib_node_increment_counter (vm, node->node_index,
+                                              AH_DECRYPT_ERROR_INTEG_ERROR,
+                                              1);
                  goto trace;
                }
 
-             //TODO UT remaining
              if (PREDICT_TRUE (sa0->use_anti_replay))
                {
                  if (PREDICT_TRUE (sa0->use_esn))
@@ -252,17 +241,9 @@ ah_decrypt_inline (vlib_main_t * vm,
                next0 = AH_DECRYPT_NEXT_IP6_INPUT;
              else
                {
-                 clib_warning ("next header: 0x%x", ah0->nexthdr);
-                 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);
+                 vlib_node_increment_counter (vm, node->node_index,
+                                              AH_DECRYPT_ERROR_DECRYPTION_FAILED,
+                                              1);
                  goto trace;
                }
            }
@@ -318,20 +299,15 @@ ah_decrypt_inline (vlib_main_t * vm,
              ah_decrypt_trace_t *tr =
                vlib_add_trace (vm, node, i_b0, sizeof (*tr));
              tr->integ_alg = sa0->integ_alg;
+             tr->seq_num = seq;
            }
          vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
                                           n_left_to_next, i_bi0, next0);
        }
       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;
 }