+ if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index)
+ {
+ if (current_sa_pkts)
+ vlib_increment_combined_counter (&ipsec_sa_counters, thread_index,
+ current_sa_index,
+ current_sa_pkts,
+ current_sa_bytes);
+ current_sa_bytes = current_sa_pkts = 0;
+
+ current_sa_index = vnet_buffer (b[0])->ipsec.sad_index;
+ sa0 = pool_elt_at_index (im->sad, current_sa_index);
+ cpd.icv_sz = sa0->integ_icv_size;
+ cpd.iv_sz = sa0->crypto_iv_size;
+ cpd.flags = sa0->flags;
+ cpd.sa_index = current_sa_index;
+ }
+
+ if (PREDICT_FALSE (~0 == sa0->decrypt_thread_index))
+ {
+ /* this is the first packet to use this SA, claim the SA
+ * for this thread. this could happen simultaneously on
+ * another thread */
+ clib_atomic_cmp_and_swap (&sa0->decrypt_thread_index, ~0,
+ ipsec_sa_assign_thread (thread_index));
+ }
+
+ if (PREDICT_TRUE (thread_index != sa0->decrypt_thread_index))
+ {
+ next[0] = ESP_DECRYPT_NEXT_HANDOFF;
+ goto next;
+ }
+
+ /* store packet data for next round for easier prefetch */
+ pd->sa_data = cpd.sa_data;
+ pd->current_data = b[0]->current_data;
+ pd->current_length = b[0]->current_length;
+ pd->hdr_sz = pd->current_data - vnet_buffer (b[0])->l3_hdr_offset;
+ payload = b[0]->data + pd->current_data;
+ pd->seq = clib_host_to_net_u32 (((esp_header_t *) payload)->seq);
+
+ /* we need 4 extra bytes for HMAC calculation when ESN are used */
+ if (ipsec_sa_is_set_USE_ESN (sa0) && pd->icv_sz &&
+ (pd->current_data + pd->current_length + 4 > buffer_data_size))
+ {
+ b[0]->error = node->errors[ESP_DECRYPT_ERROR_NO_TAIL_SPACE];
+ next[0] = ESP_DECRYPT_NEXT_DROP;
+ goto next;
+ }
+
+ /* anti-reply check */
+ if (ipsec_sa_anti_replay_check (sa0, pd->seq))
+ {
+ b[0]->error = node->errors[ESP_DECRYPT_ERROR_REPLAY];
+ next[0] = ESP_DECRYPT_NEXT_DROP;
+ goto next;
+ }
+
+ if (pd->current_length < cpd.icv_sz + esp_sz + cpd.iv_sz)
+ {
+ b[0]->error = node->errors[ESP_DECRYPT_ERROR_RUNT];
+ next[0] = ESP_DECRYPT_NEXT_DROP;
+ goto next;
+ }
+
+ len = pd->current_length - cpd.icv_sz;
+ current_sa_pkts += 1;
+ current_sa_bytes += pd->current_length;