Reorganize source tree to use single autotools instance
[vpp.git] / src / vnet / devices / dpdk / ipsec / crypto_node.c
1 /*
2  *------------------------------------------------------------------
3  * crypto_node.c - DPDK Cryptodev input node
4  *
5  * Copyright (c) 2016 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 copy 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 <vnet/devices/dpdk/ipsec/ipsec.h>
26
27 #define foreach_dpdk_crypto_input_next          \
28   _(DROP, "error-drop")                         \
29   _(ENCRYPT_POST, "dpdk-esp-encrypt-post")      \
30   _(DECRYPT_POST, "dpdk-esp-decrypt-post")
31
32 typedef enum
33 {
34 #define _(f,s) DPDK_CRYPTO_INPUT_NEXT_##f,
35   foreach_dpdk_crypto_input_next
36 #undef _
37     DPDK_CRYPTO_INPUT_N_NEXT,
38 } dpdk_crypto_input_next_t;
39
40 #define foreach_dpdk_crypto_input_error         \
41   _(DQ_COPS, "Crypto ops dequeued")             \
42   _(COP_FAILED, "Crypto op failed")
43
44 typedef enum
45 {
46 #define _(f,s) DPDK_CRYPTO_INPUT_ERROR_##f,
47   foreach_dpdk_crypto_input_error
48 #undef _
49     DPDK_CRYPTO_INPUT_N_ERROR,
50 } dpdk_crypto_input_error_t;
51
52 static char *dpdk_crypto_input_error_strings[] = {
53 #define _(n, s) s,
54   foreach_dpdk_crypto_input_error
55 #undef _
56 };
57
58 vlib_node_registration_t dpdk_crypto_input_node;
59
60 typedef struct
61 {
62   u32 cdev;
63   u32 qp;
64   u32 status;
65   u32 sa_idx;
66   u32 next_index;
67 } dpdk_crypto_input_trace_t;
68
69 static u8 *
70 format_dpdk_crypto_input_trace (u8 * s, va_list * args)
71 {
72   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74   dpdk_crypto_input_trace_t *t = va_arg (*args, dpdk_crypto_input_trace_t *);
75
76   s = format (s, "dpdk_crypto: cryptodev-id %u queue-pair %u next-index %d",
77               t->cdev, t->qp, t->next_index);
78
79   s = format (s, " status %u sa-idx %u\n", t->status, t->sa_idx);
80
81   return s;
82 }
83
84 static_always_inline u32
85 dpdk_crypto_dequeue (vlib_main_t * vm, vlib_node_runtime_t * node,
86                      crypto_qp_data_t * qpd)
87 {
88   u32 n_deq, *to_next = 0, next_index, n_cops, def_next_index;
89   struct rte_crypto_op **cops = qpd->cops;
90
91   if (qpd->inflights == 0)
92     return 0;
93
94   if (qpd->is_outbound)
95     def_next_index = DPDK_CRYPTO_INPUT_NEXT_ENCRYPT_POST;
96   else
97     def_next_index = DPDK_CRYPTO_INPUT_NEXT_DECRYPT_POST;
98
99   n_cops = rte_cryptodev_dequeue_burst (qpd->dev_id, qpd->qp_id,
100                                         cops, VLIB_FRAME_SIZE);
101   n_deq = n_cops;
102   next_index = def_next_index;
103
104   qpd->inflights -= n_cops;
105   ASSERT (qpd->inflights >= 0);
106
107   while (n_cops > 0)
108     {
109       u32 n_left_to_next;
110
111       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
112
113       while (n_cops > 0 && n_left_to_next > 0)
114         {
115           u32 bi0, next0;
116           vlib_buffer_t *b0 = 0;
117           struct rte_crypto_op *cop;
118           struct rte_crypto_sym_op *sym_cop;
119
120           cop = cops[0];
121           cops += 1;
122           n_cops -= 1;
123           n_left_to_next -= 1;
124
125           next0 = def_next_index;
126
127           if (PREDICT_FALSE (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
128             {
129               next0 = DPDK_CRYPTO_INPUT_NEXT_DROP;
130               vlib_node_increment_counter (vm, dpdk_crypto_input_node.index,
131                                            DPDK_CRYPTO_INPUT_ERROR_COP_FAILED,
132                                            1);
133             }
134           cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
135
136           sym_cop = (struct rte_crypto_sym_op *) (cop + 1);
137           b0 = vlib_buffer_from_rte_mbuf (sym_cop->m_src);
138           bi0 = vlib_get_buffer_index (vm, b0);
139
140           to_next[0] = bi0;
141           to_next += 1;
142
143           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
144             {
145               vlib_trace_next_frame (vm, node, next0);
146               dpdk_crypto_input_trace_t *tr =
147                 vlib_add_trace (vm, node, b0, sizeof (*tr));
148               tr->cdev = qpd->dev_id;
149               tr->qp = qpd->qp_id;
150               tr->status = cop->status;
151               tr->next_index = next0;
152               tr->sa_idx = vnet_buffer (b0)->ipsec.sad_index;
153             }
154
155           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
156                                            n_left_to_next, bi0, next0);
157         }
158       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
159     }
160
161   crypto_free_cop (qpd, qpd->cops, n_deq);
162
163   vlib_node_increment_counter (vm, dpdk_crypto_input_node.index,
164                                DPDK_CRYPTO_INPUT_ERROR_DQ_COPS, n_deq);
165   return n_deq;
166 }
167
168 static uword
169 dpdk_crypto_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
170                       vlib_frame_t * frame)
171 {
172   u32 cpu_index = os_get_cpu_number ();
173   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
174   crypto_worker_main_t *cwm = &dcm->workers_main[cpu_index];
175   crypto_qp_data_t *qpd;
176   u32 n_deq = 0;
177
178   /* *INDENT-OFF* */
179   vec_foreach (qpd, cwm->qp_data)
180       n_deq += dpdk_crypto_dequeue(vm, node, qpd);
181   /* *INDENT-ON* */
182
183   return n_deq;
184 }
185
186 VLIB_REGISTER_NODE (dpdk_crypto_input_node) =
187 {
188   .function = dpdk_crypto_input_fn,.name = "dpdk-crypto-input",.format_trace =
189     format_dpdk_crypto_input_trace,.type = VLIB_NODE_TYPE_INPUT,.state =
190     VLIB_NODE_STATE_DISABLED,.n_errors =
191     DPDK_CRYPTO_INPUT_N_ERROR,.error_strings =
192     dpdk_crypto_input_error_strings,.n_next_nodes =
193     DPDK_CRYPTO_INPUT_N_NEXT,.next_nodes =
194   {
195 #define _(s,n) [DPDK_CRYPTO_INPUT_NEXT_##s] = n,
196     foreach_dpdk_crypto_input_next
197 #undef _
198   }
199 ,};
200
201 #if DPDK_CRYPTO==1
202 VLIB_NODE_FUNCTION_MULTIARCH (dpdk_crypto_input_node, dpdk_crypto_input_fn)
203 #endif
204 /*
205  * fd.io coding-style-patch-verification: ON
206  *
207  * Local Variables:
208  * eval: (c-set-style "gnu")
209  * End:
210  */