FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
crypto_node.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * crypto_node.c - DPDK Cryptodev input node
4  *
5  * Copyright (c) 2017 Intel and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a opy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <vlib/vlib.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/ipsec/ipsec.h>
24 
25 #include <dpdk/buffer.h>
26 #include <dpdk/device/dpdk.h>
27 #include <dpdk/device/dpdk_priv.h>
28 #include <dpdk/ipsec/ipsec.h>
29 
30 #define foreach_dpdk_crypto_input_error \
31  _(DQ_COPS, "Crypto ops dequeued") \
32  _(AUTH_FAILED, "Crypto verification failed") \
33  _(STATUS, "Crypto operation failed")
34 
35 typedef enum
36 {
37 #define _(f,s) DPDK_CRYPTO_INPUT_ERROR_##f,
39 #undef _
42 
44 #define _(n, s) s,
46 #undef _
47 };
48 
50 
51 typedef struct
52 {
53  /* dev id of this cryptodev */
57 
58 static u8 *
59 format_dpdk_crypto_input_trace (u8 * s, va_list * args)
60 {
61  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
62  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
64 
65  s = format (s, "cryptodev-id %d next-index %d", t->dev_id, t->next_index);
66 
67  return s;
68 }
69 
72  struct rte_crypto_op *op0, u16 * next)
73 {
74  if (PREDICT_FALSE (op0->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
75  {
76  next[0] = DPDK_CRYPTO_INPUT_NEXT_DROP;
78  node->node_index,
79  DPDK_CRYPTO_INPUT_ERROR_STATUS, 1);
80  /* if auth failed */
81  if (op0->status == RTE_CRYPTO_OP_STATUS_AUTH_FAILED)
83  node->node_index,
84  DPDK_CRYPTO_INPUT_ERROR_AUTH_FAILED, 1);
85  }
86 }
87 
88 always_inline void
90  u8 dev_id, u32 * bis, u16 * nexts, u32 n_deq)
91 {
92  u32 n_left, n_trace;
93 
94  if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
95  {
96  n_left = n_deq;
97 
98  while (n_trace && n_left)
99  {
100  vlib_buffer_t *b0;
101  u16 next;
102  u32 bi;
103 
104  bi = bis[0];
105  next = nexts[0];
106 
107  b0 = vlib_get_buffer (vm, bi);
108 
109  vlib_trace_buffer (vm, node, next, b0, /* follow_chain */ 0);
110 
112  vlib_add_trace (vm, node, b0, sizeof (*tr));
113  tr->dev_id = dev_id;
114  tr->next_index = next;
115 
116  n_trace--;
117  n_left--;
118  nexts++;
119  bis++;
120  }
121  vlib_set_trace_count (vm, node, n_trace);
122  }
123 }
124 
128 {
129  u8 numa = rte_socket_id ();
130  u32 n_ops, total_n_deq, n_deq[2];
131  u32 bis[VLIB_FRAME_SIZE], *bi;
132  u16 nexts[VLIB_FRAME_SIZE], *next;
133  struct rte_crypto_op **ops;
134 
135  n_deq[0] = 0;
136  n_deq[1] = 0;
137  bi = bis;
138  next = nexts;
139  ops = cwm->ops;
140 
141  n_ops = total_n_deq = rte_cryptodev_dequeue_burst (res->dev_id,
142  res->qp_id,
143  ops, VLIB_FRAME_SIZE);
144  /* no op dequeued, do not proceed */
145  if (n_ops == 0)
146  return 0;
147 
148  while (n_ops >= 4)
149  {
150  struct rte_crypto_op *op0, *op1, *op2, *op3;
151 
152  /* Prefetch next iteration. */
153  if (n_ops >= 8)
154  {
155  CLIB_PREFETCH (ops[4], CLIB_CACHE_LINE_BYTES, LOAD);
156  CLIB_PREFETCH (ops[5], CLIB_CACHE_LINE_BYTES, LOAD);
157  CLIB_PREFETCH (ops[6], CLIB_CACHE_LINE_BYTES, LOAD);
158  CLIB_PREFETCH (ops[7], CLIB_CACHE_LINE_BYTES, LOAD);
159 
161  CLIB_CACHE_LINE_BYTES, LOAD);
163  CLIB_CACHE_LINE_BYTES, LOAD);
165  CLIB_CACHE_LINE_BYTES, LOAD);
167  CLIB_CACHE_LINE_BYTES, LOAD);
168  }
169 
170  op0 = ops[0];
171  op1 = ops[1];
172  op2 = ops[2];
173  op3 = ops[3];
174 
175  next[0] = crypto_op_get_priv (op0)->next;
176  next[1] = crypto_op_get_priv (op1)->next;
177  next[2] = crypto_op_get_priv (op2)->next;
178  next[3] = crypto_op_get_priv (op3)->next;
179 
180  bi[0] = crypto_op_get_priv (op0)->bi;
181  bi[1] = crypto_op_get_priv (op1)->bi;
182  bi[2] = crypto_op_get_priv (op2)->bi;
183  bi[3] = crypto_op_get_priv (op3)->bi;
184 
185  n_deq[crypto_op_get_priv (op0)->encrypt] += 1;
186  n_deq[crypto_op_get_priv (op1)->encrypt] += 1;
187  n_deq[crypto_op_get_priv (op2)->encrypt] += 1;
188  n_deq[crypto_op_get_priv (op3)->encrypt] += 1;
189 
190  dpdk_crypto_input_check_op (vm, node, op0, next + 0);
191  dpdk_crypto_input_check_op (vm, node, op1, next + 1);
192  dpdk_crypto_input_check_op (vm, node, op2, next + 2);
193  dpdk_crypto_input_check_op (vm, node, op3, next + 3);
194 
195  op0->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
196  op1->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
197  op2->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
198  op3->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
199 
200  /* next */
201  next += 4;
202  n_ops -= 4;
203  ops += 4;
204  bi += 4;
205  }
206  while (n_ops > 0)
207  {
208  struct rte_crypto_op *op0;
209 
210  op0 = ops[0];
211 
212  next[0] = crypto_op_get_priv (op0)->next;
213  bi[0] = crypto_op_get_priv (op0)->bi;
214 
215  n_deq[crypto_op_get_priv (op0)->encrypt] += 1;
216 
217  dpdk_crypto_input_check_op (vm, node, op0, next + 0);
218 
219  op0->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
220 
221  /* next */
222  next += 1;
223  n_ops -= 1;
224  ops += 1;
225  bi += 1;
226  }
227 
229  DPDK_CRYPTO_INPUT_ERROR_DQ_COPS, total_n_deq);
230 
231  res->inflights[0] -= n_deq[0];
232  res->inflights[1] -= n_deq[1];
233 
234  vlib_buffer_enqueue_to_next (vm, node, bis, nexts, total_n_deq);
235 
236  dpdk_crypto_input_trace (vm, node, res->dev_id, bis, nexts, total_n_deq);
237 
238  crypto_free_ops (numa, cwm->ops, total_n_deq);
239 
240  return total_n_deq;
241 }
242 
246 {
249  crypto_resource_t *res;
250  u32 n_deq = 0;
251  u16 *remove = NULL, *res_idx;
252  word i;
253 
254  /* *INDENT-OFF* */
255  vec_foreach (res_idx, cwm->resource_idx)
256  {
257  res = vec_elt_at_index (dcm->resource, res_idx[0]);
258  u32 inflights = res->inflights[0] + res->inflights[1];
259 
260  if (inflights)
261  n_deq += dpdk_crypto_dequeue (vm, cwm, node, res);
262 
263  inflights = res->inflights[0] + res->inflights[1];
264  if (PREDICT_FALSE (res->remove && !(inflights)))
265  vec_add1 (remove, res_idx[0]);
266  }
267  /* *INDENT-ON* */
268 
269  /* TODO removal on master thread? */
270  if (PREDICT_FALSE (remove != NULL))
271  {
272  /* *INDENT-OFF* */
273  vec_foreach (res_idx, remove)
274  {
275  i = vec_search (cwm->resource_idx, res_idx[0]);
276  vec_del1 (cwm->resource_idx, i);
277 
278  res = vec_elt_at_index (dcm->resource, res_idx[0]);
279  res->thread_idx = (u16) ~0;
280  res->remove = 0;
281 
282  i = vec_search (dcm->dev[res->dev_id].used_resources, res_idx[0]);
283  ASSERT (i != (u16) ~0);
284  vec_del1 (dcm->dev[res->dev_id].used_resources, i);
285  vec_add1 (dcm->dev[res->dev_id].free_resources, res_idx[0]);
286  }
287  /* *INDENT-ON* */
288 
289  vec_free (remove);
290  }
291 
292  return n_deq;
293 }
294 
297  vlib_frame_t * from_frame)
298 {
299  return dpdk_crypto_input_inline (vm, node, from_frame);
300 }
301 
302 /* *INDENT-OFF* */
304 {
305  .name = "dpdk-crypto-input",
307  .format_trace = format_dpdk_crypto_input_trace,
308  .type = VLIB_NODE_TYPE_INPUT,
309  .state = VLIB_NODE_STATE_DISABLED,
310  .n_errors = DPDK_CRYPTO_INPUT_N_ERROR,
311  .error_strings = dpdk_crypto_input_error_strings,
312  .n_next_nodes = DPDK_CRYPTO_INPUT_N_NEXT,
313  .next_nodes =
314  {
315 #define _(s,n) [DPDK_CRYPTO_INPUT_NEXT_##s] = n,
317 #undef _
318  },
319 };
320 /* *INDENT-ON* */
321 
322 /*
323  * fd.io coding-style-patch-verification: ON
324  *
325  * Local Variables:
326  * eval: (c-set-style "gnu")
327  * End:
328  */
dpdk_crypto_input_error_t
Definition: crypto_node.c:35
#define CLIB_UNUSED(x)
Definition: clib.h:82
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:187
#define foreach_dpdk_crypto_input_next
Definition: ipsec.h:36
static_always_inline u32 dpdk_crypto_dequeue(vlib_main_t *vm, crypto_worker_main_t *cwm, vlib_node_runtime_t *node, crypto_resource_t *res)
Definition: crypto_node.c:126
#define NULL
Definition: clib.h:58
#define VLIB_NODE_FLAG_TRACE_SUPPORTED
Definition: node.h:306
u32 thread_index
Definition: main.h:218
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
int i
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:202
vlib_node_registration_t dpdk_crypto_input_node
(constructor) VLIB_REGISTER_NODE (dpdk_crypto_input_node)
Definition: crypto_node.c:303
unsigned char u8
Definition: types.h:56
static void vlib_trace_buffer(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, vlib_buffer_t *b, int follow_chain)
Definition: trace_funcs.h:130
u16 * resource_idx
Definition: ipsec.h:75
static_always_inline void crypto_free_ops(u8 numa, struct rte_crypto_op **ops, u32 n)
Definition: ipsec.h:300
#define static_always_inline
Definition: clib.h:99
i64 word
Definition: types.h:111
dpdk_crypto_main_t dpdk_crypto_main
Definition: ipsec.c:25
static_always_inline uword dpdk_crypto_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: crypto_node.c:244
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u16 * free_resources
Definition: ipsec.h:98
unsigned int u32
Definition: types.h:88
#define vec_search(v, E)
Search a vector for the index of the entry that matches.
Definition: vec.h:943
#define VLIB_FRAME_SIZE
Definition: node.h:378
u32 next
Definition: ipsec.h:64
#define foreach_dpdk_crypto_input_error
Definition: crypto_node.c:30
u16 * used_resources
Definition: ipsec.h:99
unsigned short u16
Definition: types.h:57
#define PREDICT_FALSE(x)
Definition: clib.h:111
#define always_inline
Definition: ipsec.h:28
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:806
u32 node_index
Node index.
Definition: node.h:496
vlib_main_t * vm
Definition: in2out_ed.c:1810
u8 encrypt
Definition: ipsec.h:66
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
static_always_inline void dpdk_crypto_input_check_op(vlib_main_t *vm, vlib_node_runtime_t *node, struct rte_crypto_op *op0, u16 *next)
Definition: crypto_node.c:71
static_always_inline void vlib_buffer_enqueue_to_next(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts, uword count)
Definition: buffer_node.h:332
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
static char * dpdk_crypto_input_error_strings[]
Definition: crypto_node.c:43
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1810
#define ASSERT(truth)
u16 inflights[2]
Definition: ipsec.h:124
crypto_worker_main_t * workers_main
Definition: ipsec.h:163
static void dpdk_crypto_input_trace(vlib_main_t *vm, vlib_node_runtime_t *node, u8 dev_id, u32 *bis, u16 *nexts, u32 n_deq)
Definition: crypto_node.c:89
crypto_resource_t * resource
Definition: ipsec.h:165
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
struct _vlib_node_registration vlib_node_registration_t
static u8 * format_dpdk_crypto_input_trace(u8 *s, va_list *args)
Definition: crypto_node.c:59
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
struct rte_crypto_op ** ops
Definition: ipsec.h:76
crypto_dev_t * dev
Definition: ipsec.h:164
#define vec_foreach(var, vec)
Vector iterator.
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1811
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:203
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
static_always_inline dpdk_op_priv_t * crypto_op_get_priv(struct rte_crypto_op *op)
Definition: ipsec.h:208