Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2016 Intel Corporation. All rights reserved.
3 : * Copyright (c) 2018-2019, 2021 Mellanox Technologies LTD. All rights reserved.
4 : * Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : */
6 :
7 : #include "spdk/stdinc.h"
8 :
9 : #include "nvmf_internal.h"
10 : #include "transport.h"
11 :
12 : #include "spdk/config.h"
13 : #include "spdk/log.h"
14 : #include "spdk/nvmf.h"
15 : #include "spdk/nvmf_transport.h"
16 : #include "spdk/queue.h"
17 : #include "spdk/util.h"
18 : #include "spdk_internal/usdt.h"
19 :
20 : #define NVMF_TRANSPORT_DEFAULT_ASSOCIATION_TIMEOUT_IN_MS 120000
21 :
22 : struct nvmf_transport_ops_list_element {
23 : struct spdk_nvmf_transport_ops ops;
24 : TAILQ_ENTRY(nvmf_transport_ops_list_element) link;
25 : };
26 :
27 : TAILQ_HEAD(nvmf_transport_ops_list, nvmf_transport_ops_list_element)
28 : g_spdk_nvmf_transport_ops = TAILQ_HEAD_INITIALIZER(g_spdk_nvmf_transport_ops);
29 :
30 : static inline const struct spdk_nvmf_transport_ops *
31 22 : nvmf_get_transport_ops(const char *transport_name)
32 : {
33 : struct nvmf_transport_ops_list_element *ops;
34 40 : TAILQ_FOREACH(ops, &g_spdk_nvmf_transport_ops, link) {
35 31 : if (strcasecmp(transport_name, ops->ops.name) == 0) {
36 13 : return &ops->ops;
37 : }
38 : }
39 9 : return NULL;
40 : }
41 :
42 : void
43 7 : spdk_nvmf_transport_register(const struct spdk_nvmf_transport_ops *ops)
44 : {
45 : struct nvmf_transport_ops_list_element *new_ops;
46 :
47 7 : if (nvmf_get_transport_ops(ops->name) != NULL) {
48 0 : SPDK_ERRLOG("Double registering nvmf transport type %s.\n", ops->name);
49 0 : assert(false);
50 : return;
51 : }
52 :
53 7 : new_ops = calloc(1, sizeof(*new_ops));
54 7 : if (new_ops == NULL) {
55 0 : SPDK_ERRLOG("Unable to allocate memory to register new transport type %s.\n", ops->name);
56 0 : assert(false);
57 : return;
58 : }
59 :
60 7 : new_ops->ops = *ops;
61 :
62 7 : TAILQ_INSERT_TAIL(&g_spdk_nvmf_transport_ops, new_ops, link);
63 : }
64 :
65 : const struct spdk_nvmf_transport_opts *
66 0 : spdk_nvmf_get_transport_opts(struct spdk_nvmf_transport *transport)
67 : {
68 0 : return &transport->opts;
69 : }
70 :
71 : void
72 0 : nvmf_transport_dump_opts(struct spdk_nvmf_transport *transport, struct spdk_json_write_ctx *w,
73 : bool named)
74 : {
75 0 : const struct spdk_nvmf_transport_opts *opts = spdk_nvmf_get_transport_opts(transport);
76 :
77 0 : named ? spdk_json_write_named_object_begin(w, "params") : spdk_json_write_object_begin(w);
78 :
79 0 : spdk_json_write_named_string(w, "trtype", spdk_nvmf_get_transport_name(transport));
80 0 : spdk_json_write_named_uint32(w, "max_queue_depth", opts->max_queue_depth);
81 0 : spdk_json_write_named_uint32(w, "max_io_qpairs_per_ctrlr", opts->max_qpairs_per_ctrlr - 1);
82 0 : spdk_json_write_named_uint32(w, "in_capsule_data_size", opts->in_capsule_data_size);
83 0 : spdk_json_write_named_uint32(w, "max_io_size", opts->max_io_size);
84 0 : spdk_json_write_named_uint32(w, "io_unit_size", opts->io_unit_size);
85 0 : spdk_json_write_named_uint32(w, "max_aq_depth", opts->max_aq_depth);
86 0 : spdk_json_write_named_uint32(w, "num_shared_buffers", opts->num_shared_buffers);
87 0 : spdk_json_write_named_uint32(w, "buf_cache_size", opts->buf_cache_size);
88 0 : spdk_json_write_named_bool(w, "dif_insert_or_strip", opts->dif_insert_or_strip);
89 0 : spdk_json_write_named_bool(w, "zcopy", opts->zcopy);
90 :
91 0 : if (transport->ops->dump_opts) {
92 0 : transport->ops->dump_opts(transport, w);
93 : }
94 :
95 0 : spdk_json_write_named_uint32(w, "abort_timeout_sec", opts->abort_timeout_sec);
96 0 : spdk_json_write_named_uint32(w, "ack_timeout", opts->ack_timeout);
97 0 : spdk_json_write_named_uint32(w, "data_wr_pool_size", opts->data_wr_pool_size);
98 0 : spdk_json_write_object_end(w);
99 0 : }
100 :
101 : void
102 0 : nvmf_transport_listen_dump_trid(const struct spdk_nvme_transport_id *trid,
103 : struct spdk_json_write_ctx *w)
104 : {
105 0 : const char *adrfam = spdk_nvme_transport_id_adrfam_str(trid->adrfam);
106 :
107 0 : spdk_json_write_named_string(w, "trtype", trid->trstring);
108 0 : spdk_json_write_named_string(w, "adrfam", adrfam ? adrfam : "unknown");
109 0 : spdk_json_write_named_string(w, "traddr", trid->traddr);
110 0 : spdk_json_write_named_string(w, "trsvcid", trid->trsvcid);
111 0 : }
112 :
113 : spdk_nvme_transport_type_t
114 0 : spdk_nvmf_get_transport_type(struct spdk_nvmf_transport *transport)
115 : {
116 0 : return transport->ops->type;
117 : }
118 :
119 : const char *
120 0 : spdk_nvmf_get_transport_name(struct spdk_nvmf_transport *transport)
121 : {
122 0 : return transport->ops->name;
123 : }
124 :
125 : static void
126 8 : nvmf_transport_opts_copy(struct spdk_nvmf_transport_opts *opts,
127 : struct spdk_nvmf_transport_opts *opts_src,
128 : size_t opts_size)
129 : {
130 8 : assert(opts);
131 8 : assert(opts_src);
132 :
133 8 : opts->opts_size = opts_size;
134 :
135 : #define SET_FIELD(field) \
136 : if (offsetof(struct spdk_nvmf_transport_opts, field) + sizeof(opts->field) <= opts_size) { \
137 : opts->field = opts_src->field; \
138 : } \
139 :
140 8 : SET_FIELD(max_queue_depth);
141 8 : SET_FIELD(max_qpairs_per_ctrlr);
142 8 : SET_FIELD(in_capsule_data_size);
143 8 : SET_FIELD(max_io_size);
144 8 : SET_FIELD(io_unit_size);
145 8 : SET_FIELD(max_aq_depth);
146 8 : SET_FIELD(buf_cache_size);
147 8 : SET_FIELD(num_shared_buffers);
148 8 : SET_FIELD(dif_insert_or_strip);
149 8 : SET_FIELD(abort_timeout_sec);
150 8 : SET_FIELD(association_timeout);
151 8 : SET_FIELD(transport_specific);
152 8 : SET_FIELD(acceptor_poll_rate);
153 8 : SET_FIELD(zcopy);
154 8 : SET_FIELD(ack_timeout);
155 8 : SET_FIELD(data_wr_pool_size);
156 :
157 : /* Do not remove this statement, you should always update this statement when you adding a new field,
158 : * and do not forget to add the SET_FIELD statement for your added field. */
159 : SPDK_STATIC_ASSERT(sizeof(struct spdk_nvmf_transport_opts) == 72, "Incorrect size");
160 :
161 : #undef SET_FIELD
162 : #undef FILED_CHECK
163 8 : }
164 :
165 : struct nvmf_transport_create_ctx {
166 : const struct spdk_nvmf_transport_ops *ops;
167 : struct spdk_nvmf_transport_opts opts;
168 : void *cb_arg;
169 : spdk_nvmf_transport_create_done_cb cb_fn;
170 : };
171 :
172 : static bool
173 34 : nvmf_transport_use_iobuf(struct spdk_nvmf_transport *transport)
174 : {
175 34 : return transport->opts.num_shared_buffers || transport->opts.buf_cache_size;
176 : }
177 :
178 : static void
179 4 : nvmf_transport_create_async_done(void *cb_arg, struct spdk_nvmf_transport *transport)
180 : {
181 4 : struct nvmf_transport_create_ctx *ctx = cb_arg;
182 : int chars_written;
183 :
184 4 : if (!transport) {
185 0 : SPDK_ERRLOG("Failed to create transport.\n");
186 0 : goto err;
187 : }
188 :
189 4 : pthread_mutex_init(&transport->mutex, NULL);
190 4 : TAILQ_INIT(&transport->listeners);
191 4 : transport->ops = ctx->ops;
192 4 : transport->opts = ctx->opts;
193 4 : chars_written = snprintf(transport->iobuf_name, MAX_MEMPOOL_NAME_LENGTH, "%s_%s", "nvmf",
194 4 : transport->ops->name);
195 4 : if (chars_written < 0) {
196 0 : SPDK_ERRLOG("Unable to generate transport data buffer pool name.\n");
197 0 : goto err;
198 : }
199 :
200 4 : if (nvmf_transport_use_iobuf(transport)) {
201 3 : spdk_iobuf_register_module(transport->iobuf_name);
202 : }
203 :
204 4 : ctx->cb_fn(ctx->cb_arg, transport);
205 4 : free(ctx);
206 4 : return;
207 :
208 0 : err:
209 0 : if (transport) {
210 0 : transport->ops->destroy(transport, NULL, NULL);
211 : }
212 :
213 0 : ctx->cb_fn(ctx->cb_arg, NULL);
214 0 : free(ctx);
215 : }
216 :
217 : static void
218 1 : _nvmf_transport_create_done(void *ctx)
219 : {
220 1 : struct nvmf_transport_create_ctx *_ctx = (struct nvmf_transport_create_ctx *)ctx;
221 :
222 1 : nvmf_transport_create_async_done(_ctx, _ctx->ops->create(&_ctx->opts));
223 1 : }
224 :
225 : static int
226 8 : nvmf_transport_create(const char *transport_name, struct spdk_nvmf_transport_opts *opts,
227 : spdk_nvmf_transport_create_done_cb cb_fn, void *cb_arg, bool sync)
228 : {
229 : struct nvmf_transport_create_ctx *ctx;
230 8 : struct spdk_iobuf_opts opts_iobuf = {};
231 : int rc;
232 : uint64_t count;
233 :
234 8 : ctx = calloc(1, sizeof(*ctx));
235 8 : if (!ctx) {
236 0 : return -ENOMEM;
237 : }
238 :
239 8 : if (!opts) {
240 0 : SPDK_ERRLOG("opts should not be NULL\n");
241 0 : goto err;
242 : }
243 :
244 8 : if (!opts->opts_size) {
245 0 : SPDK_ERRLOG("The opts_size in opts structure should not be zero\n");
246 0 : goto err;
247 : }
248 :
249 8 : ctx->ops = nvmf_get_transport_ops(transport_name);
250 8 : if (!ctx->ops) {
251 1 : SPDK_ERRLOG("Transport type '%s' unavailable.\n", transport_name);
252 1 : goto err;
253 : }
254 :
255 7 : nvmf_transport_opts_copy(&ctx->opts, opts, opts->opts_size);
256 7 : if (ctx->opts.max_io_size != 0 && (!spdk_u32_is_pow2(ctx->opts.max_io_size) ||
257 6 : ctx->opts.max_io_size < 8192)) {
258 1 : SPDK_ERRLOG("max_io_size %u must be a power of 2 and be greater than or equal 8KB\n",
259 : ctx->opts.max_io_size);
260 1 : goto err;
261 : }
262 :
263 6 : if (ctx->opts.max_aq_depth < SPDK_NVMF_MIN_ADMIN_MAX_SQ_SIZE) {
264 1 : SPDK_ERRLOG("max_aq_depth %u is less than minimum defined by NVMf spec, use min value\n",
265 : ctx->opts.max_aq_depth);
266 1 : ctx->opts.max_aq_depth = SPDK_NVMF_MIN_ADMIN_MAX_SQ_SIZE;
267 : }
268 :
269 6 : spdk_iobuf_get_opts(&opts_iobuf, sizeof(opts_iobuf));
270 6 : if (ctx->opts.io_unit_size == 0) {
271 1 : SPDK_ERRLOG("io_unit_size cannot be 0\n");
272 1 : goto err;
273 : }
274 5 : if (ctx->opts.io_unit_size > opts_iobuf.large_bufsize) {
275 1 : SPDK_ERRLOG("io_unit_size %u is larger than iobuf pool large buffer size %d\n",
276 : ctx->opts.io_unit_size, opts_iobuf.large_bufsize);
277 1 : goto err;
278 : }
279 :
280 4 : if (ctx->opts.io_unit_size <= opts_iobuf.small_bufsize) {
281 : /* We'll be using the small buffer pool only */
282 1 : count = opts_iobuf.small_pool_count;
283 : } else {
284 3 : count = spdk_min(opts_iobuf.small_pool_count, opts_iobuf.large_pool_count);
285 : }
286 :
287 4 : if (ctx->opts.num_shared_buffers > count) {
288 0 : SPDK_WARNLOG("The num_shared_buffers value (%u) is larger than the available iobuf"
289 : " pool size (%lu). Please increase the iobuf pool sizes.\n",
290 : ctx->opts.num_shared_buffers, count);
291 : }
292 :
293 4 : ctx->cb_fn = cb_fn;
294 4 : ctx->cb_arg = cb_arg;
295 :
296 : /* Prioritize sync create operation. */
297 4 : if (ctx->ops->create) {
298 1 : if (sync) {
299 1 : _nvmf_transport_create_done(ctx);
300 1 : return 0;
301 : }
302 :
303 0 : rc = spdk_thread_send_msg(spdk_get_thread(), _nvmf_transport_create_done, ctx);
304 0 : if (rc) {
305 0 : goto err;
306 : }
307 :
308 0 : return 0;
309 : }
310 :
311 3 : assert(ctx->ops->create_async);
312 3 : rc = ctx->ops->create_async(&ctx->opts, nvmf_transport_create_async_done, ctx);
313 3 : if (rc) {
314 0 : SPDK_ERRLOG("Unable to create new transport of type %s\n", transport_name);
315 0 : goto err;
316 : }
317 :
318 3 : return 0;
319 4 : err:
320 4 : free(ctx);
321 4 : return -1;
322 : }
323 :
324 : int
325 7 : spdk_nvmf_transport_create_async(const char *transport_name, struct spdk_nvmf_transport_opts *opts,
326 : spdk_nvmf_transport_create_done_cb cb_fn, void *cb_arg)
327 : {
328 7 : return nvmf_transport_create(transport_name, opts, cb_fn, cb_arg, false);
329 : }
330 :
331 : static void
332 1 : nvmf_transport_create_sync_done(void *cb_arg, struct spdk_nvmf_transport *transport)
333 : {
334 1 : struct spdk_nvmf_transport **_transport = cb_arg;
335 :
336 1 : *_transport = transport;
337 1 : }
338 :
339 : struct spdk_nvmf_transport *
340 1 : spdk_nvmf_transport_create(const char *transport_name, struct spdk_nvmf_transport_opts *opts)
341 : {
342 1 : struct spdk_nvmf_transport *transport = NULL;
343 :
344 : /* Current implementation supports synchronous version of create operation only. */
345 1 : assert(nvmf_get_transport_ops(transport_name) && nvmf_get_transport_ops(transport_name)->create);
346 :
347 1 : nvmf_transport_create(transport_name, opts, nvmf_transport_create_sync_done, &transport, true);
348 1 : return transport;
349 : }
350 :
351 : struct spdk_nvmf_transport *
352 13 : spdk_nvmf_transport_get_first(struct spdk_nvmf_tgt *tgt)
353 : {
354 13 : return TAILQ_FIRST(&tgt->transports);
355 : }
356 :
357 : struct spdk_nvmf_transport *
358 1 : spdk_nvmf_transport_get_next(struct spdk_nvmf_transport *transport)
359 : {
360 1 : return TAILQ_NEXT(transport, link);
361 : }
362 :
363 : int
364 3 : spdk_nvmf_transport_destroy(struct spdk_nvmf_transport *transport,
365 : spdk_nvmf_transport_destroy_done_cb cb_fn, void *cb_arg)
366 : {
367 : struct spdk_nvmf_listener *listener, *listener_tmp;
368 :
369 3 : TAILQ_FOREACH_SAFE(listener, &transport->listeners, link, listener_tmp) {
370 0 : TAILQ_REMOVE(&transport->listeners, listener, link);
371 0 : transport->ops->stop_listen(transport, &listener->trid);
372 0 : free(listener);
373 : }
374 :
375 3 : if (nvmf_transport_use_iobuf(transport)) {
376 3 : spdk_iobuf_unregister_module(transport->iobuf_name);
377 : }
378 :
379 3 : pthread_mutex_destroy(&transport->mutex);
380 3 : return transport->ops->destroy(transport, cb_fn, cb_arg);
381 : }
382 :
383 : struct spdk_nvmf_listener *
384 11 : nvmf_transport_find_listener(struct spdk_nvmf_transport *transport,
385 : const struct spdk_nvme_transport_id *trid)
386 : {
387 : struct spdk_nvmf_listener *listener;
388 :
389 11 : TAILQ_FOREACH(listener, &transport->listeners, link) {
390 6 : if (spdk_nvme_transport_id_compare(&listener->trid, trid) == 0) {
391 6 : return listener;
392 : }
393 : }
394 :
395 5 : return NULL;
396 : }
397 :
398 : int
399 3 : spdk_nvmf_transport_listen(struct spdk_nvmf_transport *transport,
400 : const struct spdk_nvme_transport_id *trid, struct spdk_nvmf_listen_opts *opts)
401 : {
402 : struct spdk_nvmf_listener *listener;
403 : int rc;
404 :
405 3 : listener = nvmf_transport_find_listener(transport, trid);
406 3 : if (!listener) {
407 2 : listener = calloc(1, sizeof(*listener));
408 2 : if (!listener) {
409 0 : return -ENOMEM;
410 : }
411 :
412 2 : listener->ref = 1;
413 2 : listener->trid = *trid;
414 2 : listener->sock_impl = opts->sock_impl;
415 2 : TAILQ_INSERT_TAIL(&transport->listeners, listener, link);
416 2 : pthread_mutex_lock(&transport->mutex);
417 2 : rc = transport->ops->listen(transport, &listener->trid, opts);
418 2 : pthread_mutex_unlock(&transport->mutex);
419 2 : if (rc != 0) {
420 1 : TAILQ_REMOVE(&transport->listeners, listener, link);
421 1 : free(listener);
422 : }
423 2 : return rc;
424 : }
425 :
426 1 : if (opts->sock_impl && strncmp(opts->sock_impl, listener->sock_impl, strlen(listener->sock_impl))) {
427 0 : SPDK_ERRLOG("opts->sock_impl: '%s' doesn't match listener->sock_impl: '%s'\n", opts->sock_impl,
428 : listener->sock_impl);
429 0 : return -EINVAL;
430 : }
431 :
432 1 : ++listener->ref;
433 :
434 1 : return 0;
435 : }
436 :
437 : int
438 3 : spdk_nvmf_transport_stop_listen(struct spdk_nvmf_transport *transport,
439 : const struct spdk_nvme_transport_id *trid)
440 : {
441 : struct spdk_nvmf_listener *listener;
442 :
443 3 : listener = nvmf_transport_find_listener(transport, trid);
444 3 : if (!listener) {
445 1 : return -ENOENT;
446 : }
447 :
448 2 : if (--listener->ref == 0) {
449 1 : TAILQ_REMOVE(&transport->listeners, listener, link);
450 1 : pthread_mutex_lock(&transport->mutex);
451 1 : transport->ops->stop_listen(transport, trid);
452 1 : pthread_mutex_unlock(&transport->mutex);
453 1 : free(listener);
454 : }
455 :
456 2 : return 0;
457 : }
458 :
459 : struct nvmf_stop_listen_ctx {
460 : struct spdk_nvmf_transport *transport;
461 : struct spdk_nvme_transport_id trid;
462 : struct spdk_nvmf_subsystem *subsystem;
463 : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn;
464 : void *cb_arg;
465 : };
466 :
467 : static void
468 0 : nvmf_stop_listen_fini(struct spdk_io_channel_iter *i, int status)
469 : {
470 : struct nvmf_stop_listen_ctx *ctx;
471 : struct spdk_nvmf_transport *transport;
472 0 : int rc = status;
473 :
474 0 : ctx = spdk_io_channel_iter_get_ctx(i);
475 0 : transport = ctx->transport;
476 0 : assert(transport != NULL);
477 :
478 0 : rc = spdk_nvmf_transport_stop_listen(transport, &ctx->trid);
479 0 : if (rc) {
480 0 : SPDK_ERRLOG("Failed to stop listening on address '%s'\n", ctx->trid.traddr);
481 : }
482 :
483 0 : if (ctx->cb_fn) {
484 0 : ctx->cb_fn(ctx->cb_arg, rc);
485 : }
486 0 : free(ctx);
487 0 : }
488 :
489 : static void nvmf_stop_listen_disconnect_qpairs(struct spdk_io_channel_iter *i);
490 :
491 : static void
492 0 : nvmf_stop_listen_disconnect_qpairs_msg(void *ctx)
493 : {
494 0 : nvmf_stop_listen_disconnect_qpairs((struct spdk_io_channel_iter *)ctx);
495 0 : }
496 :
497 : static void
498 0 : nvmf_stop_listen_disconnect_qpairs(struct spdk_io_channel_iter *i)
499 : {
500 : struct nvmf_stop_listen_ctx *ctx;
501 : struct spdk_nvmf_poll_group *group;
502 : struct spdk_io_channel *ch;
503 : struct spdk_nvmf_qpair *qpair, *tmp_qpair;
504 0 : struct spdk_nvme_transport_id tmp_trid;
505 0 : bool qpair_found = false;
506 :
507 0 : ctx = spdk_io_channel_iter_get_ctx(i);
508 0 : ch = spdk_io_channel_iter_get_channel(i);
509 0 : group = spdk_io_channel_get_ctx(ch);
510 :
511 0 : TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, tmp_qpair) {
512 0 : if (spdk_nvmf_qpair_get_listen_trid(qpair, &tmp_trid)) {
513 0 : continue;
514 : }
515 :
516 : /* Skip qpairs that don't match the listen trid and subsystem pointer. If
517 : * the ctx->subsystem is NULL, it means disconnect all qpairs that match
518 : * the listen trid. */
519 0 : if (!spdk_nvme_transport_id_compare(&ctx->trid, &tmp_trid)) {
520 0 : if (ctx->subsystem == NULL ||
521 0 : (qpair->ctrlr != NULL && ctx->subsystem == qpair->ctrlr->subsys)) {
522 0 : spdk_nvmf_qpair_disconnect(qpair);
523 0 : qpair_found = true;
524 : }
525 : }
526 : }
527 0 : if (qpair_found) {
528 0 : spdk_thread_send_msg(spdk_get_thread(), nvmf_stop_listen_disconnect_qpairs_msg, i);
529 0 : return;
530 : }
531 :
532 0 : spdk_for_each_channel_continue(i, 0);
533 : }
534 :
535 : int
536 0 : spdk_nvmf_transport_stop_listen_async(struct spdk_nvmf_transport *transport,
537 : const struct spdk_nvme_transport_id *trid,
538 : struct spdk_nvmf_subsystem *subsystem,
539 : spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn,
540 : void *cb_arg)
541 : {
542 : struct nvmf_stop_listen_ctx *ctx;
543 :
544 0 : if (trid->subnqn[0] != '\0') {
545 0 : SPDK_ERRLOG("subnqn should be empty, use subsystem pointer instead\n");
546 0 : return -EINVAL;
547 : }
548 :
549 0 : ctx = calloc(1, sizeof(struct nvmf_stop_listen_ctx));
550 0 : if (ctx == NULL) {
551 0 : return -ENOMEM;
552 : }
553 :
554 0 : ctx->trid = *trid;
555 0 : ctx->subsystem = subsystem;
556 0 : ctx->transport = transport;
557 0 : ctx->cb_fn = cb_fn;
558 0 : ctx->cb_arg = cb_arg;
559 :
560 0 : spdk_for_each_channel(transport->tgt, nvmf_stop_listen_disconnect_qpairs, ctx,
561 : nvmf_stop_listen_fini);
562 :
563 0 : return 0;
564 : }
565 :
566 : void
567 0 : nvmf_transport_listener_discover(struct spdk_nvmf_transport *transport,
568 : struct spdk_nvme_transport_id *trid,
569 : struct spdk_nvmf_discovery_log_page_entry *entry)
570 : {
571 0 : transport->ops->listener_discover(transport, trid, entry);
572 0 : }
573 :
574 : struct spdk_nvmf_transport_poll_group *
575 2 : nvmf_transport_poll_group_create(struct spdk_nvmf_transport *transport,
576 : struct spdk_nvmf_poll_group *group)
577 : {
578 : struct spdk_nvmf_transport_poll_group *tgroup;
579 2 : struct spdk_iobuf_opts opts_iobuf = {};
580 : uint32_t buf_cache_size, small_cache_size, large_cache_size;
581 : int rc;
582 :
583 2 : pthread_mutex_lock(&transport->mutex);
584 2 : tgroup = transport->ops->poll_group_create(transport, group);
585 2 : pthread_mutex_unlock(&transport->mutex);
586 2 : if (!tgroup) {
587 0 : return NULL;
588 : }
589 2 : tgroup->transport = transport;
590 :
591 2 : STAILQ_INIT(&tgroup->pending_buf_queue);
592 :
593 2 : if (!nvmf_transport_use_iobuf(transport)) {
594 : /* We aren't going to allocate any shared buffers or cache, so just return now. */
595 0 : return tgroup;
596 : }
597 :
598 2 : buf_cache_size = transport->opts.buf_cache_size;
599 :
600 : /* buf_cache_size of UINT32_MAX means the value should be calculated dynamically
601 : * based on the number of buffers in the shared pool and the number of poll groups
602 : * that are sharing them. We allocate 75% of the pool for the cache, and then
603 : * divide that by number of poll groups to determine the buf_cache_size for this
604 : * poll group.
605 : */
606 2 : if (buf_cache_size == UINT32_MAX) {
607 0 : uint32_t num_shared_buffers = transport->opts.num_shared_buffers;
608 :
609 : /* Theoretically the nvmf library can dynamically add poll groups to
610 : * the target, after transports have already been created. We aren't
611 : * going to try to really handle this case efficiently, just do enough
612 : * here to ensure we don't divide-by-zero.
613 : */
614 0 : uint16_t num_poll_groups = group->tgt->num_poll_groups ? : spdk_env_get_core_count();
615 :
616 0 : buf_cache_size = (num_shared_buffers * 3 / 4) / num_poll_groups;
617 : }
618 :
619 2 : spdk_iobuf_get_opts(&opts_iobuf, sizeof(opts_iobuf));
620 2 : small_cache_size = buf_cache_size;
621 2 : if (transport->opts.io_unit_size <= opts_iobuf.small_bufsize) {
622 2 : large_cache_size = 0;
623 : } else {
624 0 : large_cache_size = buf_cache_size;
625 : }
626 :
627 2 : tgroup->buf_cache = calloc(1, sizeof(*tgroup->buf_cache));
628 2 : if (!tgroup->buf_cache) {
629 0 : SPDK_ERRLOG("Unable to allocate an iobuf channel in the poll group.\n");
630 0 : goto err;
631 : }
632 :
633 2 : rc = spdk_iobuf_channel_init(tgroup->buf_cache, transport->iobuf_name, small_cache_size,
634 : large_cache_size);
635 2 : if (rc != 0) {
636 0 : SPDK_ERRLOG("Unable to reserve the full number of buffers for the pg buffer cache.\n");
637 0 : rc = spdk_iobuf_channel_init(tgroup->buf_cache, transport->iobuf_name, 0, 0);
638 0 : if (rc != 0) {
639 0 : SPDK_ERRLOG("Unable to create an iobuf channel in the poll group.\n");
640 0 : goto err;
641 : }
642 : }
643 :
644 2 : return tgroup;
645 0 : err:
646 0 : transport->ops->poll_group_destroy(tgroup);
647 0 : return NULL;
648 : }
649 :
650 : struct spdk_nvmf_transport_poll_group *
651 0 : nvmf_transport_get_optimal_poll_group(struct spdk_nvmf_transport *transport,
652 : struct spdk_nvmf_qpair *qpair)
653 : {
654 : struct spdk_nvmf_transport_poll_group *tgroup;
655 :
656 0 : if (transport->ops->get_optimal_poll_group) {
657 0 : pthread_mutex_lock(&transport->mutex);
658 0 : tgroup = transport->ops->get_optimal_poll_group(qpair);
659 0 : pthread_mutex_unlock(&transport->mutex);
660 :
661 0 : return tgroup;
662 : } else {
663 0 : return NULL;
664 : }
665 : }
666 :
667 : void
668 2 : nvmf_transport_poll_group_destroy(struct spdk_nvmf_transport_poll_group *group)
669 : {
670 : struct spdk_nvmf_transport *transport;
671 2 : struct spdk_iobuf_channel *ch = NULL;
672 :
673 2 : transport = group->transport;
674 :
675 2 : if (!STAILQ_EMPTY(&group->pending_buf_queue)) {
676 0 : SPDK_ERRLOG("Pending I/O list wasn't empty on poll group destruction\n");
677 : }
678 :
679 2 : if (nvmf_transport_use_iobuf(transport)) {
680 : /* The call to poll_group_destroy both frees the group memory, but also
681 : * releases any remaining buffers. Cache channel pointer so we can still
682 : * release the resources after the group has been freed. */
683 2 : ch = group->buf_cache;
684 : }
685 :
686 2 : pthread_mutex_lock(&transport->mutex);
687 2 : transport->ops->poll_group_destroy(group);
688 2 : pthread_mutex_unlock(&transport->mutex);
689 :
690 2 : if (nvmf_transport_use_iobuf(transport)) {
691 2 : spdk_iobuf_channel_fini(ch);
692 2 : free(ch);
693 : }
694 2 : }
695 :
696 : int
697 0 : nvmf_transport_poll_group_add(struct spdk_nvmf_transport_poll_group *group,
698 : struct spdk_nvmf_qpair *qpair)
699 : {
700 0 : if (qpair->transport) {
701 0 : assert(qpair->transport == group->transport);
702 0 : if (qpair->transport != group->transport) {
703 0 : return -1;
704 : }
705 : } else {
706 0 : qpair->transport = group->transport;
707 : }
708 :
709 : SPDK_DTRACE_PROBE3(nvmf_transport_poll_group_add, qpair, qpair->qid,
710 : spdk_thread_get_id(group->group->thread));
711 :
712 0 : return group->transport->ops->poll_group_add(group, qpair);
713 : }
714 :
715 : int
716 0 : nvmf_transport_poll_group_remove(struct spdk_nvmf_transport_poll_group *group,
717 : struct spdk_nvmf_qpair *qpair)
718 : {
719 0 : int rc = ENOTSUP;
720 :
721 : SPDK_DTRACE_PROBE3(nvmf_transport_poll_group_remove, qpair, qpair->qid,
722 : spdk_thread_get_id(group->group->thread));
723 :
724 0 : assert(qpair->transport == group->transport);
725 0 : if (group->transport->ops->poll_group_remove) {
726 0 : rc = group->transport->ops->poll_group_remove(group, qpair);
727 : }
728 :
729 0 : return rc;
730 : }
731 :
732 : int
733 0 : nvmf_transport_poll_group_poll(struct spdk_nvmf_transport_poll_group *group)
734 : {
735 0 : return group->transport->ops->poll_group_poll(group);
736 : }
737 :
738 : int
739 0 : nvmf_transport_req_free(struct spdk_nvmf_request *req)
740 : {
741 0 : return req->qpair->transport->ops->req_free(req);
742 : }
743 :
744 : int
745 0 : nvmf_transport_req_complete(struct spdk_nvmf_request *req)
746 : {
747 0 : return req->qpair->transport->ops->req_complete(req);
748 : }
749 :
750 : void
751 0 : nvmf_transport_qpair_fini(struct spdk_nvmf_qpair *qpair,
752 : spdk_nvmf_transport_qpair_fini_cb cb_fn,
753 : void *cb_arg)
754 : {
755 : SPDK_DTRACE_PROBE1(nvmf_transport_qpair_fini, qpair);
756 :
757 0 : qpair->transport->ops->qpair_fini(qpair, cb_fn, cb_arg);
758 0 : }
759 :
760 : int
761 0 : nvmf_transport_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair,
762 : struct spdk_nvme_transport_id *trid)
763 : {
764 0 : return qpair->transport->ops->qpair_get_peer_trid(qpair, trid);
765 : }
766 :
767 : int
768 0 : nvmf_transport_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair,
769 : struct spdk_nvme_transport_id *trid)
770 : {
771 0 : return qpair->transport->ops->qpair_get_local_trid(qpair, trid);
772 : }
773 :
774 : int
775 0 : nvmf_transport_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair,
776 : struct spdk_nvme_transport_id *trid)
777 : {
778 0 : return qpair->transport->ops->qpair_get_listen_trid(qpair, trid);
779 : }
780 :
781 : void
782 0 : nvmf_transport_qpair_abort_request(struct spdk_nvmf_qpair *qpair,
783 : struct spdk_nvmf_request *req)
784 : {
785 0 : if (qpair->transport->ops->qpair_abort_request) {
786 0 : qpair->transport->ops->qpair_abort_request(qpair, req);
787 : }
788 0 : }
789 :
790 : bool
791 4 : spdk_nvmf_transport_opts_init(const char *transport_name,
792 : struct spdk_nvmf_transport_opts *opts, size_t opts_size)
793 : {
794 : const struct spdk_nvmf_transport_ops *ops;
795 4 : struct spdk_nvmf_transport_opts opts_local = {};
796 :
797 4 : ops = nvmf_get_transport_ops(transport_name);
798 4 : if (!ops) {
799 1 : SPDK_ERRLOG("Transport type %s unavailable.\n", transport_name);
800 1 : return false;
801 : }
802 :
803 3 : if (!opts) {
804 1 : SPDK_ERRLOG("opts should not be NULL\n");
805 1 : return false;
806 : }
807 :
808 2 : if (!opts_size) {
809 1 : SPDK_ERRLOG("opts_size inside opts should not be zero value\n");
810 1 : return false;
811 : }
812 :
813 1 : opts_local.association_timeout = NVMF_TRANSPORT_DEFAULT_ASSOCIATION_TIMEOUT_IN_MS;
814 1 : opts_local.acceptor_poll_rate = SPDK_NVMF_DEFAULT_ACCEPT_POLL_RATE_US;
815 1 : opts_local.disable_command_passthru = false;
816 1 : ops->opts_init(&opts_local);
817 :
818 1 : nvmf_transport_opts_copy(opts, &opts_local, opts_size);
819 :
820 1 : return true;
821 : }
822 :
823 : void
824 6 : spdk_nvmf_request_free_buffers(struct spdk_nvmf_request *req,
825 : struct spdk_nvmf_transport_poll_group *group,
826 : struct spdk_nvmf_transport *transport)
827 : {
828 : uint32_t i;
829 :
830 11 : for (i = 0; i < req->iovcnt; i++) {
831 5 : spdk_iobuf_put(group->buf_cache, req->iov[i].iov_base, req->iov[i].iov_len);
832 5 : req->iov[i].iov_base = NULL;
833 5 : req->iov[i].iov_len = 0;
834 : }
835 6 : req->iovcnt = 0;
836 6 : req->data_from_pool = false;
837 6 : }
838 :
839 : static int
840 74 : nvmf_request_set_buffer(struct spdk_nvmf_request *req, void *buf, uint32_t length,
841 : uint32_t io_unit_size)
842 : {
843 74 : req->iov[req->iovcnt].iov_base = buf;
844 74 : req->iov[req->iovcnt].iov_len = spdk_min(length, io_unit_size);
845 74 : length -= req->iov[req->iovcnt].iov_len;
846 74 : req->iovcnt++;
847 :
848 74 : return length;
849 : }
850 :
851 : static int
852 6 : nvmf_request_set_stripped_buffer(struct spdk_nvmf_request *req, void *buf, uint32_t length,
853 : uint32_t io_unit_size)
854 : {
855 6 : struct spdk_nvmf_stripped_data *data = req->stripped_data;
856 :
857 6 : data->iov[data->iovcnt].iov_base = buf;
858 6 : data->iov[data->iovcnt].iov_len = spdk_min(length, io_unit_size);
859 6 : length -= data->iov[data->iovcnt].iov_len;
860 6 : data->iovcnt++;
861 :
862 6 : return length;
863 : }
864 :
865 : static void nvmf_request_iobuf_get_cb(struct spdk_iobuf_entry *entry, void *buf);
866 :
867 : static int
868 25 : nvmf_request_get_buffers(struct spdk_nvmf_request *req,
869 : struct spdk_nvmf_transport_poll_group *group,
870 : struct spdk_nvmf_transport *transport,
871 : uint32_t length, uint32_t io_unit_size,
872 : bool stripped_buffers)
873 : {
874 25 : struct spdk_iobuf_entry *entry = NULL;
875 : uint32_t num_buffers;
876 25 : uint32_t i = 0;
877 : void *buffer;
878 :
879 : /* If the number of buffers is too large, then we know the I/O is larger than allowed.
880 : * Fail it.
881 : */
882 25 : num_buffers = SPDK_CEIL_DIV(length, io_unit_size);
883 25 : if (spdk_unlikely(num_buffers > NVMF_REQ_MAX_BUFFERS)) {
884 0 : return -EINVAL;
885 : }
886 :
887 : /* Use iobuf queuing only if transport supports it */
888 25 : if (transport->ops->req_get_buffers_done != NULL) {
889 0 : entry = &req->iobuf.entry;
890 : }
891 :
892 105 : while (i < num_buffers) {
893 81 : buffer = spdk_iobuf_get(group->buf_cache, spdk_min(io_unit_size, length), entry,
894 : nvmf_request_iobuf_get_cb);
895 81 : if (spdk_unlikely(buffer == NULL)) {
896 1 : req->iobuf.remaining_length = length;
897 1 : return -ENOMEM;
898 : }
899 80 : if (stripped_buffers) {
900 6 : length = nvmf_request_set_stripped_buffer(req, buffer, length, io_unit_size);
901 : } else {
902 74 : length = nvmf_request_set_buffer(req, buffer, length, io_unit_size);
903 : }
904 80 : i++;
905 : }
906 :
907 24 : assert(length == 0);
908 24 : req->data_from_pool = true;
909 :
910 24 : return 0;
911 : }
912 :
913 : static void
914 0 : nvmf_request_iobuf_get_cb(struct spdk_iobuf_entry *entry, void *buf)
915 : {
916 0 : struct spdk_nvmf_request *req = SPDK_CONTAINEROF(entry, struct spdk_nvmf_request, iobuf.entry);
917 0 : struct spdk_nvmf_transport *transport = req->qpair->transport;
918 0 : struct spdk_nvmf_poll_group *group = req->qpair->group;
919 0 : struct spdk_nvmf_transport_poll_group *tgroup = nvmf_get_transport_poll_group(group, transport);
920 0 : uint32_t length = req->iobuf.remaining_length;
921 0 : uint32_t io_unit_size = transport->opts.io_unit_size;
922 : int rc;
923 :
924 0 : assert(tgroup != NULL);
925 :
926 0 : length = nvmf_request_set_buffer(req, buf, length, io_unit_size);
927 0 : rc = nvmf_request_get_buffers(req, tgroup, transport, length, io_unit_size, false);
928 0 : if (rc == 0) {
929 0 : transport->ops->req_get_buffers_done(req);
930 : }
931 0 : }
932 :
933 : int
934 21 : spdk_nvmf_request_get_buffers(struct spdk_nvmf_request *req,
935 : struct spdk_nvmf_transport_poll_group *group,
936 : struct spdk_nvmf_transport *transport,
937 : uint32_t length)
938 : {
939 : int rc;
940 :
941 21 : assert(nvmf_transport_use_iobuf(transport));
942 :
943 21 : req->iovcnt = 0;
944 21 : rc = nvmf_request_get_buffers(req, group, transport, length, transport->opts.io_unit_size, false);
945 21 : if (spdk_unlikely(rc == -ENOMEM && transport->ops->req_get_buffers_done == NULL)) {
946 1 : spdk_nvmf_request_free_buffers(req, group, transport);
947 : }
948 :
949 21 : return rc;
950 : }
951 :
952 : static int
953 0 : nvmf_request_get_buffers_abort_cb(struct spdk_iobuf_channel *ch, struct spdk_iobuf_entry *entry,
954 : void *cb_ctx)
955 : {
956 0 : struct spdk_nvmf_request *req, *req_to_abort = cb_ctx;
957 :
958 0 : req = SPDK_CONTAINEROF(entry, struct spdk_nvmf_request, iobuf.entry);
959 0 : if (req != req_to_abort) {
960 0 : return 0;
961 : }
962 :
963 0 : spdk_iobuf_entry_abort(ch, entry, spdk_min(req->iobuf.remaining_length,
964 : req->qpair->transport->opts.io_unit_size));
965 0 : return 1;
966 : }
967 :
968 : bool
969 0 : nvmf_request_get_buffers_abort(struct spdk_nvmf_request *req)
970 : {
971 0 : struct spdk_nvmf_transport_poll_group *tgroup = nvmf_get_transport_poll_group(req->qpair->group,
972 0 : req->qpair->transport);
973 : int rc;
974 :
975 0 : assert(tgroup != NULL);
976 :
977 0 : rc = spdk_iobuf_for_each_entry(tgroup->buf_cache, nvmf_request_get_buffers_abort_cb, req);
978 0 : return rc == 1;
979 : }
980 :
981 : void
982 0 : nvmf_request_free_stripped_buffers(struct spdk_nvmf_request *req,
983 : struct spdk_nvmf_transport_poll_group *group,
984 : struct spdk_nvmf_transport *transport)
985 : {
986 0 : struct spdk_nvmf_stripped_data *data = req->stripped_data;
987 : uint32_t i;
988 :
989 0 : for (i = 0; i < data->iovcnt; i++) {
990 0 : spdk_iobuf_put(group->buf_cache, data->iov[i].iov_base, data->iov[i].iov_len);
991 : }
992 0 : free(data);
993 0 : req->stripped_data = NULL;
994 0 : }
995 :
996 : int
997 8 : nvmf_request_get_stripped_buffers(struct spdk_nvmf_request *req,
998 : struct spdk_nvmf_transport_poll_group *group,
999 : struct spdk_nvmf_transport *transport,
1000 : uint32_t length)
1001 : {
1002 8 : uint32_t block_size = req->dif.dif_ctx.block_size;
1003 8 : uint32_t data_block_size = block_size - req->dif.dif_ctx.md_size;
1004 8 : uint32_t io_unit_size = transport->opts.io_unit_size / block_size * data_block_size;
1005 : struct spdk_nvmf_stripped_data *data;
1006 : uint32_t i;
1007 : int rc;
1008 :
1009 : /* We don't support iobuf queueing with stripped buffers yet */
1010 8 : assert(transport->ops->req_get_buffers_done == NULL);
1011 :
1012 : /* Data blocks must be block aligned */
1013 14 : for (i = 0; i < req->iovcnt; i++) {
1014 10 : if (req->iov[i].iov_len % block_size) {
1015 4 : return -EINVAL;
1016 : }
1017 : }
1018 :
1019 4 : data = calloc(1, sizeof(*data));
1020 4 : if (data == NULL) {
1021 0 : SPDK_ERRLOG("Unable to allocate memory for stripped_data.\n");
1022 0 : return -ENOMEM;
1023 : }
1024 4 : req->stripped_data = data;
1025 4 : req->stripped_data->iovcnt = 0;
1026 :
1027 4 : rc = nvmf_request_get_buffers(req, group, transport, length, io_unit_size, true);
1028 4 : if (rc == -ENOMEM) {
1029 0 : nvmf_request_free_stripped_buffers(req, group, transport);
1030 0 : return rc;
1031 : }
1032 4 : return rc;
1033 : }
|