2 *------------------------------------------------------------------
3 * crypto_node.c - DPDK Cryptodev input node
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vlib/vlib.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/ipsec/ipsec.h>
25 #include <dpdk/device/dpdk.h>
26 #include <dpdk/device/dpdk_priv.h>
27 #include <dpdk/ipsec/ipsec.h>
29 #define foreach_dpdk_crypto_input_next \
30 _(DROP, "error-drop") \
31 _(ENCRYPT_POST, "dpdk-esp-encrypt-post") \
32 _(DECRYPT_POST, "dpdk-esp-decrypt-post")
36 #define _(f,s) DPDK_CRYPTO_INPUT_NEXT_##f,
37 foreach_dpdk_crypto_input_next
39 DPDK_CRYPTO_INPUT_N_NEXT,
40 } dpdk_crypto_input_next_t;
42 #define foreach_dpdk_crypto_input_error \
43 _(DQ_COPS, "Crypto ops dequeued") \
44 _(COP_FAILED, "Crypto op failed")
48 #define _(f,s) DPDK_CRYPTO_INPUT_ERROR_##f,
49 foreach_dpdk_crypto_input_error
51 DPDK_CRYPTO_INPUT_N_ERROR,
52 } dpdk_crypto_input_error_t;
54 static char *dpdk_crypto_input_error_strings[] = {
56 foreach_dpdk_crypto_input_error
60 vlib_node_registration_t dpdk_crypto_input_node;
69 } dpdk_crypto_input_trace_t;
72 format_dpdk_crypto_input_trace (u8 * s, va_list * args)
74 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
75 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
76 dpdk_crypto_input_trace_t *t = va_arg (*args, dpdk_crypto_input_trace_t *);
78 s = format (s, "dpdk_crypto: cryptodev-id %u queue-pair %u next-index %d",
79 t->cdev, t->qp, t->next_index);
81 s = format (s, " status %u sa-idx %u\n", t->status, t->sa_idx);
86 static_always_inline u32
87 dpdk_crypto_dequeue (vlib_main_t * vm, vlib_node_runtime_t * node,
88 crypto_qp_data_t * qpd)
90 u32 n_deq, *to_next = 0, next_index, n_cops, def_next_index;
91 struct rte_crypto_op **cops = qpd->cops;
93 if (qpd->inflights == 0)
97 def_next_index = DPDK_CRYPTO_INPUT_NEXT_ENCRYPT_POST;
99 def_next_index = DPDK_CRYPTO_INPUT_NEXT_DECRYPT_POST;
101 n_cops = rte_cryptodev_dequeue_burst (qpd->dev_id, qpd->qp_id,
102 cops, VLIB_FRAME_SIZE);
104 next_index = def_next_index;
106 qpd->inflights -= n_cops;
107 ASSERT (qpd->inflights >= 0);
113 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
115 while (n_cops > 0 && n_left_to_next > 0)
118 vlib_buffer_t *b0 = 0;
119 struct rte_crypto_op *cop;
120 struct rte_crypto_sym_op *sym_cop;
127 next0 = def_next_index;
129 if (PREDICT_FALSE (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
131 next0 = DPDK_CRYPTO_INPUT_NEXT_DROP;
132 vlib_node_increment_counter (vm, dpdk_crypto_input_node.index,
133 DPDK_CRYPTO_INPUT_ERROR_COP_FAILED,
136 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
138 sym_cop = (struct rte_crypto_sym_op *) (cop + 1);
139 b0 = vlib_buffer_from_rte_mbuf (sym_cop->m_src);
140 bi0 = vlib_get_buffer_index (vm, b0);
145 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
147 vlib_trace_next_frame (vm, node, next0);
148 dpdk_crypto_input_trace_t *tr =
149 vlib_add_trace (vm, node, b0, sizeof (*tr));
150 tr->cdev = qpd->dev_id;
152 tr->status = cop->status;
153 tr->next_index = next0;
154 tr->sa_idx = vnet_buffer (b0)->ipsec.sad_index;
157 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
158 n_left_to_next, bi0, next0);
160 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
163 crypto_free_cop (qpd, qpd->cops, n_deq);
165 vlib_node_increment_counter (vm, dpdk_crypto_input_node.index,
166 DPDK_CRYPTO_INPUT_ERROR_DQ_COPS, n_deq);
171 dpdk_crypto_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
172 vlib_frame_t * frame)
174 u32 thread_index = vlib_get_thread_index ();
175 dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
176 crypto_worker_main_t *cwm = &dcm->workers_main[thread_index];
177 crypto_qp_data_t *qpd;
181 vec_foreach (qpd, cwm->qp_data)
182 n_deq += dpdk_crypto_dequeue(vm, node, qpd);
189 VLIB_REGISTER_NODE (dpdk_crypto_input_node) =
191 .function = dpdk_crypto_input_fn,
192 .name = "dpdk-crypto-input",
193 .format_trace = format_dpdk_crypto_input_trace,
194 .type = VLIB_NODE_TYPE_INPUT,
195 .state = VLIB_NODE_STATE_DISABLED,
196 .n_errors = DPDK_CRYPTO_INPUT_N_ERROR,
197 .error_strings = dpdk_crypto_input_error_strings,
198 .n_next_nodes = DPDK_CRYPTO_INPUT_N_NEXT,
201 #define _(s,n) [DPDK_CRYPTO_INPUT_NEXT_##s] = n,
202 foreach_dpdk_crypto_input_next
208 VLIB_NODE_FUNCTION_MULTIARCH (dpdk_crypto_input_node, dpdk_crypto_input_fn)
210 * fd.io coding-style-patch-verification: ON
213 * eval: (c-set-style "gnu")