1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * * Neither the name of Intel Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <rte_common.h> 34 #include <rte_cryptodev_pmd.h> 35 #include <rte_bus_vdev.h> 36 #include <rte_malloc.h> 37 38 #include "null_crypto_pmd_private.h" 39 40 static uint8_t cryptodev_driver_id; 41 42 /** verify and set session parameters */ 43 int 44 null_crypto_set_session_parameters( 45 struct null_crypto_session *sess __rte_unused, 46 const struct rte_crypto_sym_xform *xform) 47 { 48 if (xform == NULL) { 49 return -EINVAL; 50 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH && 51 xform->next == NULL) { 52 /* Authentication Only */ 53 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL) 54 return 0; 55 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH && 56 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) { 57 /* Authentication then Cipher */ 58 if (xform->auth.algo == RTE_CRYPTO_AUTH_NULL && 59 xform->next->cipher.algo == RTE_CRYPTO_CIPHER_NULL) 60 return 0; 61 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && 62 xform->next == NULL) { 63 /* Cipher Only */ 64 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL) 65 return 0; 66 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && 67 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) { 68 /* Cipher then Authentication */ 69 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_NULL && 70 xform->next->auth.algo == RTE_CRYPTO_AUTH_NULL) 71 return 0; 72 } 73 74 return -ENOTSUP; 75 } 76 77 /** Process crypto operation for mbuf */ 78 static int 79 process_op(const struct null_crypto_qp *qp, struct rte_crypto_op *op, 80 struct null_crypto_session *sess __rte_unused) 81 { 82 /* set status as successful by default */ 83 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS; 84 85 /* Free session if a session-less crypto op. */ 86 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) { 87 memset(op->sym->session, 0, 88 sizeof(struct null_crypto_session)); 89 rte_cryptodev_sym_session_free(op->sym->session); 90 op->sym->session = NULL; 91 } 92 93 /* 94 * if crypto session and operation are valid just enqueue the packet 95 * in the processed ring 96 */ 97 return rte_ring_enqueue(qp->processed_pkts, (void *)op); 98 } 99 100 static struct null_crypto_session * 101 get_session(struct null_crypto_qp *qp, struct rte_crypto_op *op) 102 { 103 struct null_crypto_session *sess = NULL; 104 struct rte_crypto_sym_op *sym_op = op->sym; 105 106 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) { 107 if (likely(sym_op->session != NULL)) 108 sess = (struct null_crypto_session *) 109 get_session_private_data( 110 sym_op->session, cryptodev_driver_id); 111 } else { 112 void *_sess = NULL; 113 void *_sess_private_data = NULL; 114 115 if (rte_mempool_get(qp->sess_mp, (void **)&_sess)) 116 return NULL; 117 118 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data)) 119 return NULL; 120 121 sess = (struct null_crypto_session *)_sess_private_data; 122 123 if (unlikely(null_crypto_set_session_parameters(sess, 124 sym_op->xform) != 0)) { 125 rte_mempool_put(qp->sess_mp, _sess); 126 rte_mempool_put(qp->sess_mp, _sess_private_data); 127 sess = NULL; 128 } 129 sym_op->session = (struct rte_cryptodev_sym_session *)_sess; 130 set_session_private_data(sym_op->session, cryptodev_driver_id, 131 _sess_private_data); 132 } 133 134 return sess; 135 } 136 137 /** Enqueue burst */ 138 static uint16_t 139 null_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops, 140 uint16_t nb_ops) 141 { 142 struct null_crypto_session *sess; 143 struct null_crypto_qp *qp = queue_pair; 144 145 int i, retval; 146 147 for (i = 0; i < nb_ops; i++) { 148 sess = get_session(qp, ops[i]); 149 if (unlikely(sess == NULL)) 150 goto enqueue_err; 151 152 retval = process_op(qp, ops[i], sess); 153 if (unlikely(retval < 0)) 154 goto enqueue_err; 155 } 156 157 qp->qp_stats.enqueued_count += i; 158 return i; 159 160 enqueue_err: 161 if (ops[i]) 162 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS; 163 164 qp->qp_stats.enqueue_err_count++; 165 return i; 166 } 167 168 /** Dequeue burst */ 169 static uint16_t 170 null_crypto_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops, 171 uint16_t nb_ops) 172 { 173 struct null_crypto_qp *qp = queue_pair; 174 175 unsigned nb_dequeued; 176 177 nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts, 178 (void **)ops, nb_ops, NULL); 179 qp->qp_stats.dequeued_count += nb_dequeued; 180 181 return nb_dequeued; 182 } 183 184 /** Create crypto device */ 185 static int 186 cryptodev_null_create(const char *name, 187 struct rte_vdev_device *vdev, 188 struct rte_cryptodev_pmd_init_params *init_params) 189 { 190 struct rte_cryptodev *dev; 191 struct null_crypto_private *internals; 192 193 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params); 194 if (dev == NULL) { 195 NULL_CRYPTO_LOG_ERR("failed to create cryptodev vdev"); 196 return -EFAULT; 197 } 198 199 dev->driver_id = cryptodev_driver_id; 200 dev->dev_ops = null_crypto_pmd_ops; 201 202 /* register rx/tx burst functions for data path */ 203 dev->dequeue_burst = null_crypto_pmd_dequeue_burst; 204 dev->enqueue_burst = null_crypto_pmd_enqueue_burst; 205 206 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO | 207 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING | 208 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER; 209 210 internals = dev->data->dev_private; 211 212 internals->max_nb_qpairs = init_params->max_nb_queue_pairs; 213 internals->max_nb_sessions = init_params->max_nb_sessions; 214 215 return 0; 216 } 217 218 /** Initialise null crypto device */ 219 static int 220 cryptodev_null_probe(struct rte_vdev_device *dev) 221 { 222 struct rte_cryptodev_pmd_init_params init_params = { 223 "", 224 sizeof(struct null_crypto_private), 225 rte_socket_id(), 226 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS, 227 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS 228 }; 229 const char *name, *args; 230 int retval; 231 232 name = rte_vdev_device_name(dev); 233 if (name == NULL) 234 return -EINVAL; 235 236 args = rte_vdev_device_args(dev); 237 238 retval = rte_cryptodev_pmd_parse_input_args(&init_params, args); 239 if (retval) { 240 RTE_LOG(ERR, PMD, 241 "Failed to parse initialisation arguments[%s]\n", args); 242 return -EINVAL; 243 } 244 245 return cryptodev_null_create(name, dev, &init_params); 246 } 247 248 static int 249 cryptodev_null_remove_dev(struct rte_vdev_device *vdev) 250 { 251 struct rte_cryptodev *cryptodev; 252 const char *name; 253 254 name = rte_vdev_device_name(vdev); 255 if (name == NULL) 256 return -EINVAL; 257 258 cryptodev = rte_cryptodev_pmd_get_named_dev(name); 259 if (cryptodev == NULL) 260 return -ENODEV; 261 262 return rte_cryptodev_pmd_destroy(cryptodev); 263 } 264 265 static struct rte_vdev_driver cryptodev_null_pmd_drv = { 266 .probe = cryptodev_null_probe, 267 .remove = cryptodev_null_remove_dev, 268 }; 269 270 static struct cryptodev_driver null_crypto_drv; 271 272 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd_drv); 273 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_NULL_PMD, cryptodev_null_pmd); 274 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_NULL_PMD, 275 "max_nb_queue_pairs=<int> " 276 "max_nb_sessions=<int> " 277 "socket_id=<int>"); 278 RTE_PMD_REGISTER_CRYPTO_DRIVER(null_crypto_drv, cryptodev_null_pmd_drv, 279 cryptodev_driver_id); 280