FD.io VPP  v19.01.3-6-g70449b9b9
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/device/dpdk.h>
26 #include <dpdk/device/dpdk_priv.h>
27 #include <dpdk/ipsec/ipsec.h>
28 
29 #define foreach_dpdk_crypto_input_error \
30  _(DQ_COPS, "Crypto ops dequeued") \
31  _(AUTH_FAILED, "Crypto verification failed") \
32  _(STATUS, "Crypto operation failed")
33 
34 typedef enum
35 {
36 #define _(f,s) DPDK_CRYPTO_INPUT_ERROR_##f,
38 #undef _
41 
43 #define _(n, s) s,
45 #undef _
46 };
47 
49 
50 typedef struct
51 {
54 
55 #define foreach_cryptodev_status \
56  _(SUCCESS, "success") \
57  _(NOT_PROCESSED, "not processed") \
58  _(AUTH_FAILED, "auth failed") \
59  _(INVALID_SESSION, "invalid session") \
60  _(INVALID_ARGS, "invalid arguments") \
61  _(ERROR, "error")
62 
63 static u8 *
64 format_cryptodev_status (u8 * s, va_list * args)
65 {
66  u32 status = va_arg (*args, u32);
67  char *str = 0;
68 
69  switch (status)
70  {
71 #define _(x, z) case RTE_CRYPTO_OP_STATUS_##x: str = z; break;
73 #undef _
74  }
75  s = format (s, "%s", str);
76 
77  return s;
78 }
79 
80 static u8 *
81 format_dpdk_crypto_input_trace (u8 * s, va_list * args)
82 {
83  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
84  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
86 
87  s = format (s, "status: %U", format_cryptodev_status, t->status);
88 
89  return s;
90 }
91 
94  struct rte_crypto_op *op0, u16 * next)
95 {
96  if (PREDICT_FALSE (op0->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
97  {
98  next[0] = DPDK_CRYPTO_INPUT_NEXT_DROP;
100  node->node_index,
101  DPDK_CRYPTO_INPUT_ERROR_STATUS, 1);
102  /* if auth failed */
103  if (op0->status == RTE_CRYPTO_OP_STATUS_AUTH_FAILED)
105  node->node_index,
106  DPDK_CRYPTO_INPUT_ERROR_AUTH_FAILED, 1);
107  }
108 }
109 
110 always_inline void
112  struct rte_crypto_op **ops, u32 n_deq)
113 {
114  u32 n_left, n_trace;
115  if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
116  {
117  n_left = n_deq;
118 
119  while (n_trace && n_left)
120  {
121  vlib_buffer_t *b0;
122  struct rte_crypto_op *op0;
123  u16 next;
124 
125  op0 = ops[0];
126 
127  next = crypto_op_get_priv (op0)->next;
128 
129  b0 = vlib_buffer_from_rte_mbuf (op0->sym[0].m_src);
130 
131  vlib_trace_buffer (vm, node, next, b0, /* follow_chain */ 0);
132 
134  vlib_add_trace (vm, node, b0, sizeof (*tr));
135  tr->status = op0->status;
136 
137  n_trace--;
138  n_left--;
139  ops++;
140  }
141  vlib_set_trace_count (vm, node, n_trace);
142  }
143 }
144 
147  crypto_resource_t * res, u8 outbound)
148 {
149  u32 thread_idx = vlib_get_thread_index ();
150  u8 numa = rte_socket_id ();
151 
153  crypto_worker_main_t *cwm =
154  vec_elt_at_index (dcm->workers_main, thread_idx);
155 
156  u32 n_ops, n_deq;
157  u32 bis[VLIB_FRAME_SIZE], *bi;
158  u16 nexts[VLIB_FRAME_SIZE], *next;
159  struct rte_crypto_op **ops;
160 
161  bi = bis;
162  next = nexts;
163  ops = cwm->ops;
164 
165  n_ops = n_deq = rte_cryptodev_dequeue_burst (res->dev_id,
166  res->qp_id + outbound,
167  ops, VLIB_FRAME_SIZE);
168 
169  /* no op dequeued, do not proceed */
170  if (n_deq == 0)
171  return 0;
172 
173  res->inflights[outbound] -= n_ops;
174 
175  dpdk_crypto_input_trace (vm, node, ops, n_deq);
176 
177  while (n_ops >= 4)
178  {
179  struct rte_crypto_op *op0, *op1, *op2, *op3;
180  vlib_buffer_t *b0, *b1, *b2, *b3;
181 
182  /* Prefetch next iteration. */
183  if (n_ops >= 8)
184  {
185  CLIB_PREFETCH (ops[4], CLIB_CACHE_LINE_BYTES, LOAD);
186  CLIB_PREFETCH (ops[5], CLIB_CACHE_LINE_BYTES, LOAD);
187  CLIB_PREFETCH (ops[6], CLIB_CACHE_LINE_BYTES, LOAD);
188  CLIB_PREFETCH (ops[7], CLIB_CACHE_LINE_BYTES, LOAD);
189 
191  CLIB_CACHE_LINE_BYTES, LOAD);
193  CLIB_CACHE_LINE_BYTES, LOAD);
195  CLIB_CACHE_LINE_BYTES, LOAD);
197  CLIB_CACHE_LINE_BYTES, LOAD);
198  }
199 
200  op0 = ops[0];
201  op1 = ops[1];
202  op2 = ops[2];
203  op3 = ops[3];
204 
205  next[0] = crypto_op_get_priv (op0)->next;
206  next[1] = crypto_op_get_priv (op1)->next;
207  next[2] = crypto_op_get_priv (op2)->next;
208  next[3] = crypto_op_get_priv (op3)->next;
209 
210  dpdk_crypto_input_check_op (vm, node, op0, next + 0);
211  dpdk_crypto_input_check_op (vm, node, op1, next + 1);
212  dpdk_crypto_input_check_op (vm, node, op2, next + 2);
213  dpdk_crypto_input_check_op (vm, node, op3, next + 3);
214 
215  b0 = vlib_buffer_from_rte_mbuf (op0->sym[0].m_src);
216  b1 = vlib_buffer_from_rte_mbuf (op1->sym[0].m_src);
217  b2 = vlib_buffer_from_rte_mbuf (op2->sym[0].m_src);
218  b3 = vlib_buffer_from_rte_mbuf (op3->sym[0].m_src);
219 
220  bi[0] = vlib_get_buffer_index (vm, b0);
221  bi[1] = vlib_get_buffer_index (vm, b1);
222  bi[2] = vlib_get_buffer_index (vm, b2);
223  bi[3] = vlib_get_buffer_index (vm, b3);
224 
225  op0->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
226  op1->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
227  op2->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
228  op3->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
229 
230  /* next */
231  next += 4;
232  n_ops -= 4;
233  ops += 4;
234  bi += 4;
235  }
236  while (n_ops > 0)
237  {
238  struct rte_crypto_op *op0;
239  vlib_buffer_t *b0;
240 
241  op0 = ops[0];
242 
243  next[0] = crypto_op_get_priv (op0)->next;
244 
245  dpdk_crypto_input_check_op (vm, node, op0, next + 0);
246 
247  /* XXX store bi0 and next0 in op0 private? */
248  b0 = vlib_buffer_from_rte_mbuf (op0->sym[0].m_src);
249  bi[0] = vlib_get_buffer_index (vm, b0);
250 
251  op0->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
252 
253  /* next */
254  next += 1;
255  n_ops -= 1;
256  ops += 1;
257  bi += 1;
258  }
259 
261  DPDK_CRYPTO_INPUT_ERROR_DQ_COPS, n_deq);
262 
263  vlib_buffer_enqueue_to_next (vm, node, bis, nexts, n_deq);
264 
265  crypto_free_ops (numa, cwm->ops, n_deq);
266 
267  return n_deq;
268 }
269 
272  vlib_frame_t * frame)
273 {
274  u32 thread_index = vlib_get_thread_index ();
276  crypto_worker_main_t *cwm = &dcm->workers_main[thread_index];
277  crypto_resource_t *res;
278  u32 n_deq = 0;
279  u16 *remove = NULL, *res_idx;
280  word i;
281 
282  /* *INDENT-OFF* */
283  vec_foreach (res_idx, cwm->resource_idx)
284  {
285  res = vec_elt_at_index (dcm->resource, res_idx[0]);
286 
287  if (res->inflights[0])
288  n_deq += dpdk_crypto_dequeue (vm, node, res, 0);
289 
290  if (res->inflights[1])
291  n_deq += dpdk_crypto_dequeue (vm, node, res, 1);
292 
293  if (PREDICT_FALSE (res->remove && !(res->inflights[0] || res->inflights[1])))
294  vec_add1 (remove, res_idx[0]);
295  }
296  /* *INDENT-ON* */
297 
298  /* TODO removal on master thread? */
299  if (PREDICT_FALSE (remove != NULL))
300  {
301  /* *INDENT-OFF* */
302  vec_foreach (res_idx, remove)
303  {
304  i = vec_search (cwm->resource_idx, res_idx[0]);
305  vec_del1 (cwm->resource_idx, i);
306 
307  res = vec_elt_at_index (dcm->resource, res_idx[0]);
308  res->thread_idx = (u16) ~0;
309  res->remove = 0;
310 
311  i = vec_search (dcm->dev[res->dev_id].used_resources, res_idx[0]);
312  ASSERT (i != (u16) ~0);
313  vec_del1 (dcm->dev[res->dev_id].used_resources, i);
314  vec_add1 (dcm->dev[res->dev_id].free_resources, res_idx[0]);
315  }
316  /* *INDENT-ON* */
317 
318  vec_free (remove);
319  }
320 
321  return n_deq;
322 }
323 
325  vlib_node_runtime_t * node,
326  vlib_frame_t * from_frame)
327 {
328  return dpdk_crypto_input_inline (vm, node, from_frame);
329 }
330 
331 /* *INDENT-OFF* */
333 {
334  .name = "dpdk-crypto-input",
335  .format_trace = format_dpdk_crypto_input_trace,
336  .type = VLIB_NODE_TYPE_INPUT,
337  .state = VLIB_NODE_STATE_DISABLED,
338  .n_errors = DPDK_CRYPTO_INPUT_N_ERROR,
339  .error_strings = dpdk_crypto_input_error_strings,
340  .n_next_nodes = DPDK_CRYPTO_INPUT_N_NEXT,
341  .next_nodes =
342  {
343 #define _(s,n) [DPDK_CRYPTO_INPUT_NEXT_##s] = n,
345 #undef _
346  },
347 };
348 /* *INDENT-ON* */
349 
350 /*
351  * fd.io coding-style-patch-verification: ON
352  *
353  * Local Variables:
354  * eval: (c-set-style "gnu")
355  * End:
356  */
dpdk_crypto_input_error_t
Definition: crypto_node.c:34
#define vlib_buffer_from_rte_mbuf(x)
Definition: dpdk_priv.h:17
#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:156
#define foreach_dpdk_crypto_input_next
Definition: ipsec.h:33
static_always_inline u32 dpdk_crypto_dequeue(vlib_main_t *vm, vlib_node_runtime_t *node, crypto_resource_t *res, u8 outbound)
Definition: crypto_node.c:146
#define NULL
Definition: clib.h:58
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:525
int i
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define VLIB_NODE_FN(node)
Definition: node.h:201
vlib_node_registration_t dpdk_crypto_input_node
(constructor) VLIB_REGISTER_NODE (dpdk_crypto_input_node)
Definition: crypto_node.c:332
unsigned char u8
Definition: types.h:56
static void dpdk_crypto_input_trace(vlib_main_t *vm, vlib_node_runtime_t *node, struct rte_crypto_op **ops, u32 n_deq)
Definition: crypto_node.c:111
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:114
u16 * resource_idx
Definition: ipsec.h:68
static_always_inline void crypto_free_ops(u8 numa, struct rte_crypto_op **ops, u32 n)
Definition: ipsec.h:292
#define static_always_inline
Definition: clib.h:99
i64 word
Definition: types.h:111
dpdk_crypto_main_t dpdk_crypto_main
Definition: ipsec.c:24
#define always_inline
Definition: clib.h:98
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:271
#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:91
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:944
#define VLIB_FRAME_SIZE
Definition: node.h:401
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:158
u32 next
Definition: ipsec.h:60
#define foreach_dpdk_crypto_input_error
Definition: crypto_node.c:29
u16 * used_resources
Definition: ipsec.h:92
unsigned short u16
Definition: types.h:57
#define PREDICT_FALSE(x)
Definition: clib.h:111
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:808
u32 node_index
Node index.
Definition: node.h:518
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
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:212
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:79
vlib_main_t * vm
Definition: buffer.c:301
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:93
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:341
static char * dpdk_crypto_input_error_strings[]
Definition: crypto_node.c:42
#define ASSERT(truth)
#define foreach_cryptodev_status
Definition: crypto_node.c:55
u16 inflights[2]
Definition: ipsec.h:117
crypto_worker_main_t * workers_main
Definition: ipsec.h:155
crypto_resource_t * resource
Definition: ipsec.h:157
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:57
struct _vlib_node_registration vlib_node_registration_t
static u8 * format_dpdk_crypto_input_trace(u8 *s, va_list *args)
Definition: crypto_node.c:81
u64 uword
Definition: types.h:112
struct rte_crypto_op ** ops
Definition: ipsec.h:69
crypto_dev_t * dev
Definition: ipsec.h:156
static u8 * format_cryptodev_status(u8 *s, va_list *args)
Definition: crypto_node.c:64
#define vec_foreach(var, vec)
Vector iterator.
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:172
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
static_always_inline dpdk_op_priv_t * crypto_op_get_priv(struct rte_crypto_op *op)
Definition: ipsec.h:200