FD.io VPP  v18.04-17-g3a0d853
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  _(STATUS, "Crypto operation failed")
32 
33 typedef enum
34 {
35 #define _(f,s) DPDK_CRYPTO_INPUT_ERROR_##f,
37 #undef _
40 
42 #define _(n, s) s,
44 #undef _
45 };
46 
48 
49 typedef struct
50 {
53 
54 #define foreach_cryptodev_status \
55  _(SUCCESS, "success") \
56  _(NOT_PROCESSED, "not processed") \
57  _(AUTH_FAILED, "auth failed") \
58  _(INVALID_SESSION, "invalid session") \
59  _(INVALID_ARGS, "invalid arguments") \
60  _(ERROR, "error")
61 
62 static u8 *
63 format_cryptodev_status (u8 * s, va_list * args)
64 {
65  u32 status = va_arg (*args, u32);
66  i8 *str = 0;
67 
68  switch (status)
69  {
70 #define _(x, z) case RTE_CRYPTO_OP_STATUS_##x: str = z; break;
72 #undef _
73  }
74  s = format (s, "%s", str);
75 
76  return s;
77 }
78 
79 static u8 *
80 format_dpdk_crypto_input_trace (u8 * s, va_list * args)
81 {
82  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
83  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
85 
86  s = format (s, "status: %U", format_cryptodev_status, t->status);
87 
88  return s;
89 }
90 
93  crypto_resource_t * res, u8 outbound)
94 {
95  u32 n_deq, total_n_deq = 0, *to_next = 0, n_ops, next_index;
96  u32 thread_idx = vlib_get_thread_index ();
98  u8 numa = rte_socket_id ();
100  vec_elt_at_index (dcm->workers_main, thread_idx);
101  struct rte_crypto_op **ops;
102 
103  next_index = node->cached_next_index;
104 
105  do
106  {
107  ops = cwm->ops;
108  n_ops = rte_cryptodev_dequeue_burst (res->dev_id,
109  res->qp_id + outbound,
110  ops, VLIB_FRAME_SIZE);
111  res->inflights[outbound] -= n_ops;
112  ASSERT (res->inflights >= 0);
113 
114  n_deq = n_ops;
115  total_n_deq += n_ops;
116 
117  while (n_ops > 0)
118  {
119  u32 n_left_to_next;
120 
121  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
122 
123  while (n_ops > 0 && n_left_to_next > 0)
124  {
125  u32 bi0, next0;
126  vlib_buffer_t *b0 = 0;
127  struct rte_crypto_op *op;
128 
129  op = ops[0];
130  ops += 1;
131  n_ops -= 1;
132  n_left_to_next -= 1;
133 
134  dpdk_op_priv_t *priv = crypto_op_get_priv (op);
135  next0 = priv->next;
136 
137  if (PREDICT_FALSE (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
138  {
139  next0 = DPDK_CRYPTO_INPUT_NEXT_DROP;
142  DPDK_CRYPTO_INPUT_ERROR_STATUS,
143  1);
144  }
145 
146  /* XXX store bi0 and next0 in op private? */
147 
148  b0 = vlib_buffer_from_rte_mbuf (op->sym[0].m_src);
149  bi0 = vlib_get_buffer_index (vm, b0);
150 
151  to_next[0] = bi0;
152  to_next += 1;
153 
154  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
155  {
156  vlib_trace_next_frame (vm, node, next0);
158  vlib_add_trace (vm, node, b0, sizeof (*tr));
159  tr->status = op->status;
160  }
161 
162  op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
163 
164  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
165  n_left_to_next, bi0, next0);
166  }
167  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
168  }
169 
170  crypto_free_ops (numa, cwm->ops, n_deq);
171  }
172  while (n_deq == VLIB_FRAME_SIZE && res->inflights[outbound]);
173 
175  DPDK_CRYPTO_INPUT_ERROR_DQ_COPS, total_n_deq);
176  return total_n_deq;
177 }
178 
179 static uword
181  vlib_frame_t * frame)
182 {
183  u32 thread_index = vlib_get_thread_index ();
185  crypto_worker_main_t *cwm = &dcm->workers_main[thread_index];
186  crypto_resource_t *res;
187  u32 n_deq = 0;
188  u8 outbound;
189  u16 *remove = NULL, *res_idx;
190  word i;
191 
192  /* *INDENT-OFF* */
193  vec_foreach (res_idx, cwm->resource_idx)
194  {
195  res = vec_elt_at_index (dcm->resource, res_idx[0]);
196 
197  outbound = 0;
198  if (res->inflights[outbound])
199  n_deq += dpdk_crypto_dequeue (vm, node, res, outbound);
200 
201  outbound = 1;
202  if (res->inflights[outbound])
203  n_deq += dpdk_crypto_dequeue (vm, node, res, outbound);
204 
205  if (unlikely(res->remove && !(res->inflights[0] || res->inflights[1])))
206  vec_add1 (remove, res_idx[0]);
207  }
208  /* *INDENT-ON* */
209 
210  /* TODO removal on master thread? */
211  if (PREDICT_FALSE (remove != NULL))
212  {
213  /* *INDENT-OFF* */
214  vec_foreach (res_idx, remove)
215  {
216  i = vec_search (cwm->resource_idx, res_idx[0]);
217  vec_del1 (cwm->resource_idx, i);
218 
219  res = vec_elt_at_index (dcm->resource, res_idx[0]);
220  res->thread_idx = (u16) ~0;
221  res->remove = 0;
222 
223  i = vec_search (dcm->dev[res->dev_id].used_resources, res_idx[0]);
224  ASSERT (i != (u16) ~0);
225  vec_del1 (dcm->dev[res->dev_id].used_resources, i);
226  vec_add1 (dcm->dev[res->dev_id].free_resources, res_idx[0]);
227  }
228  /* *INDENT-ON* */
229 
230  vec_free (remove);
231  }
232 
233  return n_deq;
234 }
235 
236 /* *INDENT-OFF* */
238 {
239  .function = dpdk_crypto_input_fn,
240  .name = "dpdk-crypto-input",
241  .format_trace = format_dpdk_crypto_input_trace,
242  .type = VLIB_NODE_TYPE_INPUT,
243  .state = VLIB_NODE_STATE_DISABLED,
244  .n_errors = DPDK_CRYPTO_INPUT_N_ERROR,
245  .error_strings = dpdk_crypto_input_error_strings,
246  .n_next_nodes = DPDK_CRYPTO_INPUT_N_NEXT,
247  .next_nodes =
248  {
249 #define _(s,n) [DPDK_CRYPTO_INPUT_NEXT_##s] = n,
251 #undef _
252  },
253 };
254 /* *INDENT-ON* */
255 
257 /*
258  * fd.io coding-style-patch-verification: ON
259  *
260  * Local Variables:
261  * eval: (c-set-style "gnu")
262  * End:
263  */
dpdk_crypto_input_error_t
Definition: crypto_node.c:33
#define vlib_buffer_from_rte_mbuf(x)
Definition: dpdk_priv.h:17
#define CLIB_UNUSED(x)
Definition: clib.h:79
#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:92
#define NULL
Definition: clib.h:55
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:520
int i
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
vlib_node_registration_t dpdk_crypto_input_node
(constructor) VLIB_REGISTER_NODE (dpdk_crypto_input_node)
Definition: crypto_node.c:47
u16 * resource_idx
Definition: ipsec.h:67
static_always_inline void crypto_free_ops(u8 numa, struct rte_crypto_op **ops, u32 n)
Definition: ipsec.h:274
#define static_always_inline
Definition: clib.h:93
dpdk_crypto_main_t dpdk_crypto_main
Definition: ipsec.c:24
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
char i8
Definition: types.h:45
static void vlib_trace_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index)
Definition: trace_funcs.h:92
u16 * free_resources
Definition: ipsec.h:88
#define vec_search(v, E)
Search a vector for the index of the entry that matches.
Definition: vec.h:939
#define VLIB_FRAME_SIZE
Definition: node.h:328
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:74
u32 next
Definition: ipsec.h:59
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:158
#define foreach_dpdk_crypto_input_error
Definition: crypto_node.c:29
u16 * used_resources
Definition: ipsec.h:89
#define PREDICT_FALSE(x)
Definition: clib.h:105
static uword dpdk_crypto_input_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: crypto_node.c:180
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:803
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:364
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1166
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
static char * dpdk_crypto_input_error_strings[]
Definition: crypto_node.c:41
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:454
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:456
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
#define foreach_cryptodev_status
Definition: crypto_node.c:54
u16 inflights[2]
Definition: ipsec.h:114
crypto_worker_main_t * workers_main
Definition: ipsec.h:145
crypto_resource_t * resource
Definition: ipsec.h:148
u64 uword
Definition: types.h:112
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
unsigned short u16
Definition: types.h:57
i64 word
Definition: types.h:111
static u8 * format_dpdk_crypto_input_trace(u8 *s, va_list *args)
Definition: crypto_node.c:80
unsigned char u8
Definition: types.h:56
struct rte_crypto_op ** ops
Definition: ipsec.h:68
crypto_dev_t * dev
Definition: ipsec.h:147
static u8 * format_cryptodev_status(u8 *s, va_list *args)
Definition: crypto_node.c:63
#define vec_foreach(var, vec)
Vector iterator.
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:111
static_always_inline dpdk_op_priv_t * crypto_op_get_priv(struct rte_crypto_op *op)
Definition: ipsec.h:191