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 <string.h> 34 35 #include <rte_common.h> 36 #include <rte_malloc.h> 37 #include <rte_cryptodev_pmd.h> 38 39 #include "null_crypto_pmd_private.h" 40 41 static const struct rte_cryptodev_capabilities null_crypto_pmd_capabilities[] = { 42 { /* NULL (AUTH) */ 43 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 44 {.sym = { 45 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 46 {.auth = { 47 .algo = RTE_CRYPTO_AUTH_NULL, 48 .block_size = 1, 49 .key_size = { 50 .min = 0, 51 .max = 0, 52 .increment = 0 53 }, 54 .digest_size = { 55 .min = 0, 56 .max = 0, 57 .increment = 0 58 }, 59 .aad_size = { 0 }, 60 .iv_size = { 0 } 61 }, }, 62 }, }, 63 }, 64 { /* NULL (CIPHER) */ 65 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 66 {.sym = { 67 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 68 {.cipher = { 69 .algo = RTE_CRYPTO_CIPHER_NULL, 70 .block_size = 1, 71 .key_size = { 72 .min = 0, 73 .max = 0, 74 .increment = 0 75 }, 76 .iv_size = { 0 } 77 }, }, 78 }, } 79 }, 80 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST() 81 }; 82 83 /** Configure device */ 84 static int 85 null_crypto_pmd_config(__rte_unused struct rte_cryptodev *dev, 86 __rte_unused struct rte_cryptodev_config *config) 87 { 88 return 0; 89 } 90 91 /** Start device */ 92 static int 93 null_crypto_pmd_start(__rte_unused struct rte_cryptodev *dev) 94 { 95 return 0; 96 } 97 98 /** Stop device */ 99 static void 100 null_crypto_pmd_stop(__rte_unused struct rte_cryptodev *dev) 101 { 102 } 103 104 /** Close device */ 105 static int 106 null_crypto_pmd_close(__rte_unused struct rte_cryptodev *dev) 107 { 108 return 0; 109 } 110 111 /** Get device statistics */ 112 static void 113 null_crypto_pmd_stats_get(struct rte_cryptodev *dev, 114 struct rte_cryptodev_stats *stats) 115 { 116 int qp_id; 117 118 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) { 119 struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id]; 120 121 stats->enqueued_count += qp->qp_stats.enqueued_count; 122 stats->dequeued_count += qp->qp_stats.dequeued_count; 123 124 stats->enqueue_err_count += qp->qp_stats.enqueue_err_count; 125 stats->dequeue_err_count += qp->qp_stats.dequeue_err_count; 126 } 127 } 128 129 /** Reset device statistics */ 130 static void 131 null_crypto_pmd_stats_reset(struct rte_cryptodev *dev) 132 { 133 int qp_id; 134 135 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) { 136 struct null_crypto_qp *qp = dev->data->queue_pairs[qp_id]; 137 138 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats)); 139 } 140 } 141 142 143 /** Get device info */ 144 static void 145 null_crypto_pmd_info_get(struct rte_cryptodev *dev, 146 struct rte_cryptodev_info *dev_info) 147 { 148 struct null_crypto_private *internals = dev->data->dev_private; 149 150 if (dev_info != NULL) { 151 dev_info->driver_id = dev->driver_id; 152 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs; 153 dev_info->sym.max_nb_sessions = internals->max_nb_sessions; 154 dev_info->feature_flags = dev->feature_flags; 155 dev_info->capabilities = null_crypto_pmd_capabilities; 156 } 157 } 158 159 /** Release queue pair */ 160 static int 161 null_crypto_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id) 162 { 163 if (dev->data->queue_pairs[qp_id] != NULL) { 164 rte_free(dev->data->queue_pairs[qp_id]); 165 dev->data->queue_pairs[qp_id] = NULL; 166 } 167 return 0; 168 } 169 170 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */ 171 static int 172 null_crypto_pmd_qp_set_unique_name(struct rte_cryptodev *dev, 173 struct null_crypto_qp *qp) 174 { 175 unsigned n = snprintf(qp->name, sizeof(qp->name), 176 "null_crypto_pmd_%u_qp_%u", 177 dev->data->dev_id, qp->id); 178 179 if (n > sizeof(qp->name)) 180 return -1; 181 182 return 0; 183 } 184 185 /** Create a ring to place process packets on */ 186 static struct rte_ring * 187 null_crypto_pmd_qp_create_processed_pkts_ring(struct null_crypto_qp *qp, 188 unsigned ring_size, int socket_id) 189 { 190 struct rte_ring *r; 191 192 r = rte_ring_lookup(qp->name); 193 if (r) { 194 if (rte_ring_get_size(r) >= ring_size) { 195 NULL_CRYPTO_LOG_INFO( 196 "Reusing existing ring %s for processed packets", 197 qp->name); 198 return r; 199 } 200 201 NULL_CRYPTO_LOG_INFO( 202 "Unable to reuse existing ring %s for processed packets", 203 qp->name); 204 return NULL; 205 } 206 207 return rte_ring_create(qp->name, ring_size, socket_id, 208 RING_F_SP_ENQ | RING_F_SC_DEQ); 209 } 210 211 /** Setup a queue pair */ 212 static int 213 null_crypto_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id, 214 const struct rte_cryptodev_qp_conf *qp_conf, 215 int socket_id, struct rte_mempool *session_pool) 216 { 217 struct null_crypto_private *internals = dev->data->dev_private; 218 struct null_crypto_qp *qp; 219 int retval; 220 221 if (qp_id >= internals->max_nb_qpairs) { 222 NULL_CRYPTO_LOG_ERR("Invalid qp_id %u, greater than maximum " 223 "number of queue pairs supported (%u).", 224 qp_id, internals->max_nb_qpairs); 225 return (-EINVAL); 226 } 227 228 /* Free memory prior to re-allocation if needed. */ 229 if (dev->data->queue_pairs[qp_id] != NULL) 230 null_crypto_pmd_qp_release(dev, qp_id); 231 232 /* Allocate the queue pair data structure. */ 233 qp = rte_zmalloc_socket("Null Crypto PMD Queue Pair", sizeof(*qp), 234 RTE_CACHE_LINE_SIZE, socket_id); 235 if (qp == NULL) { 236 NULL_CRYPTO_LOG_ERR("Failed to allocate queue pair memory"); 237 return (-ENOMEM); 238 } 239 240 qp->id = qp_id; 241 dev->data->queue_pairs[qp_id] = qp; 242 243 retval = null_crypto_pmd_qp_set_unique_name(dev, qp); 244 if (retval) { 245 NULL_CRYPTO_LOG_ERR("Failed to create unique name for null " 246 "crypto device"); 247 goto qp_setup_cleanup; 248 } 249 250 qp->processed_pkts = null_crypto_pmd_qp_create_processed_pkts_ring(qp, 251 qp_conf->nb_descriptors, socket_id); 252 if (qp->processed_pkts == NULL) { 253 NULL_CRYPTO_LOG_ERR("Failed to create unique name for null " 254 "crypto device"); 255 goto qp_setup_cleanup; 256 } 257 258 qp->sess_mp = session_pool; 259 260 memset(&qp->qp_stats, 0, sizeof(qp->qp_stats)); 261 262 return 0; 263 264 qp_setup_cleanup: 265 if (qp) 266 rte_free(qp); 267 268 return -1; 269 } 270 271 /** Start queue pair */ 272 static int 273 null_crypto_pmd_qp_start(__rte_unused struct rte_cryptodev *dev, 274 __rte_unused uint16_t queue_pair_id) 275 { 276 return -ENOTSUP; 277 } 278 279 /** Stop queue pair */ 280 static int 281 null_crypto_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev, 282 __rte_unused uint16_t queue_pair_id) 283 { 284 return -ENOTSUP; 285 } 286 287 /** Return the number of allocated queue pairs */ 288 static uint32_t 289 null_crypto_pmd_qp_count(struct rte_cryptodev *dev) 290 { 291 return dev->data->nb_queue_pairs; 292 } 293 294 /** Returns the size of the NULL crypto session structure */ 295 static unsigned 296 null_crypto_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused) 297 { 298 return sizeof(struct null_crypto_session); 299 } 300 301 /** Configure a null crypto session from a crypto xform chain */ 302 static int 303 null_crypto_pmd_session_configure(struct rte_cryptodev *dev __rte_unused, 304 struct rte_crypto_sym_xform *xform, 305 struct rte_cryptodev_sym_session *sess, 306 struct rte_mempool *mp) 307 { 308 void *sess_private_data; 309 310 if (unlikely(sess == NULL)) { 311 NULL_CRYPTO_LOG_ERR("invalid session struct"); 312 return -1; 313 } 314 315 if (rte_mempool_get(mp, &sess_private_data)) { 316 CDEV_LOG_ERR( 317 "Couldn't get object from session mempool"); 318 return -1; 319 } 320 321 if (null_crypto_set_session_parameters(sess_private_data, xform) != 0) { 322 NULL_CRYPTO_LOG_ERR("failed configure session parameters"); 323 324 /* Return session to mempool */ 325 rte_mempool_put(mp, sess_private_data); 326 return -1; 327 } 328 329 set_session_private_data(sess, dev->driver_id, 330 sess_private_data); 331 332 return 0; 333 } 334 335 /** Clear the memory of session so it doesn't leave key material behind */ 336 static void 337 null_crypto_pmd_session_clear(struct rte_cryptodev *dev, 338 struct rte_cryptodev_sym_session *sess) 339 { 340 uint8_t index = dev->driver_id; 341 void *sess_priv = get_session_private_data(sess, index); 342 343 /* Zero out the whole structure */ 344 if (sess_priv) { 345 memset(sess_priv, 0, sizeof(struct null_crypto_session)); 346 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv); 347 set_session_private_data(sess, index, NULL); 348 rte_mempool_put(sess_mp, sess_priv); 349 } 350 } 351 352 struct rte_cryptodev_ops pmd_ops = { 353 .dev_configure = null_crypto_pmd_config, 354 .dev_start = null_crypto_pmd_start, 355 .dev_stop = null_crypto_pmd_stop, 356 .dev_close = null_crypto_pmd_close, 357 358 .stats_get = null_crypto_pmd_stats_get, 359 .stats_reset = null_crypto_pmd_stats_reset, 360 361 .dev_infos_get = null_crypto_pmd_info_get, 362 363 .queue_pair_setup = null_crypto_pmd_qp_setup, 364 .queue_pair_release = null_crypto_pmd_qp_release, 365 .queue_pair_start = null_crypto_pmd_qp_start, 366 .queue_pair_stop = null_crypto_pmd_qp_stop, 367 .queue_pair_count = null_crypto_pmd_qp_count, 368 369 .session_get_size = null_crypto_pmd_session_get_size, 370 .session_configure = null_crypto_pmd_session_configure, 371 .session_clear = null_crypto_pmd_session_clear 372 }; 373 374 struct rte_cryptodev_ops *null_crypto_pmd_ops = &pmd_ops; 375