5fbaaf3316a91f8f9cba644c326eb8138e1cf66e
[vpp.git] / src / plugins / dpdk / ipsec / crypto_node.c
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,
36   foreach_dpdk_crypto_input_error
37 #undef _
38     DPDK_CRYPTO_INPUT_N_ERROR,
39 } dpdk_crypto_input_error_t;
40
41 static char *dpdk_crypto_input_error_strings[] = {
42 #define _(n, s) s,
43   foreach_dpdk_crypto_input_error
44 #undef _
45 };
46
47 extern vlib_node_registration_t dpdk_crypto_input_node;
48
49 typedef struct
50 {
51   u32 status;
52 } dpdk_crypto_input_trace_t;
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   char *str = 0;
67
68   switch (status)
69     {
70 #define _(x, z) case RTE_CRYPTO_OP_STATUS_##x: str = z; break;
71       foreach_cryptodev_status
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 *);
84   dpdk_crypto_input_trace_t *t = va_arg (*args, dpdk_crypto_input_trace_t *);
85
86   s = format (s, "status: %U", format_cryptodev_status, t->status);
87
88   return s;
89 }
90
91 static_always_inline u32
92 dpdk_crypto_dequeue (vlib_main_t * vm, vlib_node_runtime_t * node,
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 ();
97   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
98   u8 numa = rte_socket_id ();
99   crypto_worker_main_t *cwm =
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   {
106     ops = cwm->ops;
107     n_ops = rte_cryptodev_dequeue_burst (res->dev_id,
108                                          res->qp_id + outbound,
109                                          ops, VLIB_FRAME_SIZE);
110     res->inflights[outbound] -= n_ops;
111     ASSERT (res->inflights >= 0);
112
113     n_deq = n_ops;
114     total_n_deq += n_ops;
115
116     while (n_ops > 0)
117       {
118         u32 n_left_to_next;
119
120         vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
121
122         while (n_ops > 0 && n_left_to_next > 0)
123           {
124             u32 bi0, next0;
125             vlib_buffer_t *b0 = 0;
126             struct rte_crypto_op *op;
127
128             op = ops[0];
129             ops += 1;
130             n_ops -= 1;
131             n_left_to_next -= 1;
132
133             dpdk_op_priv_t *priv = crypto_op_get_priv (op);
134             next0 = priv->next;
135
136             if (PREDICT_FALSE (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
137               {
138                 next0 = DPDK_CRYPTO_INPUT_NEXT_DROP;
139                 vlib_node_increment_counter (vm,
140                                              dpdk_crypto_input_node.index,
141                                              DPDK_CRYPTO_INPUT_ERROR_STATUS,
142                                              1);
143               }
144
145             /* XXX store bi0 and next0 in op private? */
146
147             b0 = vlib_buffer_from_rte_mbuf (op->sym[0].m_src);
148             bi0 = vlib_get_buffer_index (vm, b0);
149
150             to_next[0] = bi0;
151             to_next += 1;
152
153             if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
154               {
155                 vlib_trace_next_frame (vm, node, next0);
156                 dpdk_crypto_input_trace_t *tr =
157                   vlib_add_trace (vm, node, b0, sizeof (*tr));
158                 tr->status = op->status;
159               }
160
161             op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
162
163             vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
164                                              n_left_to_next, bi0, next0);
165           }
166         vlib_put_next_frame (vm, node, next_index, n_left_to_next);
167       }
168
169     crypto_free_ops (numa, cwm->ops, n_deq);
170   }
171
172   vlib_node_increment_counter (vm, dpdk_crypto_input_node.index,
173                                DPDK_CRYPTO_INPUT_ERROR_DQ_COPS, total_n_deq);
174   return total_n_deq;
175 }
176
177 static_always_inline uword
178 dpdk_crypto_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
179                           vlib_frame_t * frame)
180 {
181   u32 thread_index = vlib_get_thread_index ();
182   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
183   crypto_worker_main_t *cwm = &dcm->workers_main[thread_index];
184   crypto_resource_t *res;
185   u32 n_deq = 0;
186   u16 *remove = NULL, *res_idx;
187   word i;
188
189   /* *INDENT-OFF* */
190   vec_foreach (res_idx, cwm->resource_idx)
191     {
192       res = vec_elt_at_index (dcm->resource, res_idx[0]);
193
194       if (res->inflights[0])
195         n_deq += dpdk_crypto_dequeue (vm, node, res, 0);
196
197       if (res->inflights[1])
198         n_deq += dpdk_crypto_dequeue (vm, node, res, 1);
199
200       if (unlikely(res->remove && !(res->inflights[0] || res->inflights[1])))
201         vec_add1 (remove, res_idx[0]);
202     }
203   /* *INDENT-ON* */
204
205   /* TODO removal on master thread? */
206   if (PREDICT_FALSE (remove != NULL))
207     {
208       /* *INDENT-OFF* */
209       vec_foreach (res_idx, remove)
210         {
211           i = vec_search (cwm->resource_idx, res_idx[0]);
212           vec_del1 (cwm->resource_idx, i);
213
214           res = vec_elt_at_index (dcm->resource, res_idx[0]);
215           res->thread_idx = (u16) ~0;
216           res->remove = 0;
217
218           i = vec_search (dcm->dev[res->dev_id].used_resources, res_idx[0]);
219           ASSERT (i != (u16) ~0);
220           vec_del1 (dcm->dev[res->dev_id].used_resources, i);
221           vec_add1 (dcm->dev[res->dev_id].free_resources, res_idx[0]);
222         }
223       /* *INDENT-ON* */
224
225       vec_free (remove);
226     }
227
228   return n_deq;
229 }
230
231 VLIB_NODE_FN (dpdk_crypto_input_node) (vlib_main_t * vm,
232                                        vlib_node_runtime_t * node,
233                                        vlib_frame_t * from_frame)
234 {
235   return dpdk_crypto_input_inline (vm, node, from_frame);
236 }
237
238 /* *INDENT-OFF* */
239 VLIB_REGISTER_NODE (dpdk_crypto_input_node) =
240 {
241   .name = "dpdk-crypto-input",
242   .format_trace = format_dpdk_crypto_input_trace,
243   .type = VLIB_NODE_TYPE_INPUT,
244   .state = VLIB_NODE_STATE_DISABLED,
245   .n_errors = DPDK_CRYPTO_INPUT_N_ERROR,
246   .error_strings = dpdk_crypto_input_error_strings,
247   .n_next_nodes = DPDK_CRYPTO_INPUT_N_NEXT,
248   .next_nodes =
249   {
250 #define _(s,n) [DPDK_CRYPTO_INPUT_NEXT_##s] = n,
251     foreach_dpdk_crypto_input_next
252 #undef _
253   },
254 };
255 /* *INDENT-ON* */
256
257 /*
258  * fd.io coding-style-patch-verification: ON
259  *
260  * Local Variables:
261  * eval: (c-set-style "gnu")
262  * End:
263  */