Branch data 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) 2021, 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : : */
6 : :
7 : : #include "spdk/stdinc.h"
8 : :
9 : : #include "spdk/bdev.h"
10 : : #include "spdk/bit_array.h"
11 : : #include "spdk/thread.h"
12 : : #include "spdk/nvmf.h"
13 : : #include "spdk/endian.h"
14 : : #include "spdk/string.h"
15 : : #include "spdk/log.h"
16 : : #include "spdk_internal/usdt.h"
17 : :
18 : : #include "nvmf_internal.h"
19 : : #include "transport.h"
20 : :
21 : 845 : SPDK_LOG_REGISTER_COMPONENT(nvmf)
22 : :
23 : : #define SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS 1024
24 : :
25 : : static TAILQ_HEAD(, spdk_nvmf_tgt) g_nvmf_tgts = TAILQ_HEAD_INITIALIZER(g_nvmf_tgts);
26 : :
27 : : typedef void (*nvmf_qpair_disconnect_cpl)(void *ctx, int status);
28 : :
29 : : /* supplied to a single call to nvmf_qpair_disconnect */
30 : : struct nvmf_qpair_disconnect_ctx {
31 : : struct spdk_nvmf_qpair *qpair;
32 : : struct spdk_nvmf_ctrlr *ctrlr;
33 : : uint16_t qid;
34 : : };
35 : :
36 : : /*
37 : : * There are several times when we need to iterate through the list of all qpairs and selectively delete them.
38 : : * In order to do this sequentially without overlap, we must provide a context to recover the next qpair from
39 : : * to enable calling nvmf_qpair_disconnect on the next desired qpair.
40 : : */
41 : : struct nvmf_qpair_disconnect_many_ctx {
42 : : struct spdk_nvmf_subsystem *subsystem;
43 : : struct spdk_nvmf_poll_group *group;
44 : : spdk_nvmf_poll_group_mod_done cpl_fn;
45 : : void *cpl_ctx;
46 : : };
47 : :
48 : : static struct spdk_nvmf_referral *
49 : 39 : nvmf_tgt_find_referral(struct spdk_nvmf_tgt *tgt,
50 : : const struct spdk_nvme_transport_id *trid)
51 : : {
52 : : struct spdk_nvmf_referral *referral;
53 : :
54 [ + + ]: 60 : TAILQ_FOREACH(referral, &tgt->referrals, link) {
55 [ + + ]: 39 : if (spdk_nvme_transport_id_compare(&referral->trid, trid) == 0) {
56 : 18 : return referral;
57 : : }
58 : : }
59 : :
60 : 21 : return NULL;
61 : : }
62 : :
63 : : int
64 : 21 : spdk_nvmf_tgt_add_referral(struct spdk_nvmf_tgt *tgt,
65 : : const struct spdk_nvmf_referral_opts *uopts)
66 : : {
67 : : struct spdk_nvmf_referral *referral;
68 : 21 : struct spdk_nvmf_referral_opts opts = {};
69 : 21 : struct spdk_nvme_transport_id *trid = &opts.trid;
70 : :
71 [ - + - + ]: 21 : memcpy(&opts, uopts, spdk_min(uopts->size, sizeof(opts)));
72 [ + + ]: 21 : if (trid->subnqn[0] == '\0') {
73 [ - + ]: 15 : snprintf(trid->subnqn, sizeof(trid->subnqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
74 : : }
75 : :
76 [ - + ]: 21 : if (!nvmf_nqn_is_valid(trid->subnqn)) {
77 : 0 : SPDK_ERRLOG("Invalid subsystem NQN\n");
78 : 0 : return -EINVAL;
79 : : }
80 : :
81 : : /* If the entry already exists, just ignore it. */
82 [ - + ]: 21 : if (nvmf_tgt_find_referral(tgt, trid)) {
83 : 0 : return 0;
84 : : }
85 : :
86 : 21 : referral = calloc(1, sizeof(*referral));
87 [ - + ]: 21 : if (!referral) {
88 : 0 : SPDK_ERRLOG("Failed to allocate memory for a referral\n");
89 : 0 : return -ENOMEM;
90 : : }
91 : :
92 [ + + ]: 21 : referral->entry.subtype = nvmf_nqn_is_discovery(trid->subnqn) ?
93 : : SPDK_NVMF_SUBTYPE_DISCOVERY :
94 : : SPDK_NVMF_SUBTYPE_NVME;
95 [ - + ]: 42 : referral->entry.treq.secure_channel = opts.secure_channel ?
96 [ - + ]: 21 : SPDK_NVMF_TREQ_SECURE_CHANNEL_REQUIRED :
97 : : SPDK_NVMF_TREQ_SECURE_CHANNEL_NOT_REQUIRED;
98 : 21 : referral->entry.cntlid = 0xffff;
99 : 21 : referral->entry.trtype = trid->trtype;
100 : 21 : referral->entry.adrfam = trid->adrfam;
101 [ - + - + ]: 21 : memcpy(&referral->trid, trid, sizeof(struct spdk_nvme_transport_id));
102 : 21 : spdk_strcpy_pad(referral->entry.subnqn, trid->subnqn, sizeof(trid->subnqn), '\0');
103 : 21 : spdk_strcpy_pad(referral->entry.trsvcid, trid->trsvcid, sizeof(referral->entry.trsvcid), ' ');
104 : 21 : spdk_strcpy_pad(referral->entry.traddr, trid->traddr, sizeof(referral->entry.traddr), ' ');
105 : :
106 [ + + ]: 21 : TAILQ_INSERT_HEAD(&tgt->referrals, referral, link);
107 : 21 : nvmf_update_discovery_log(tgt, NULL);
108 : :
109 : 21 : return 0;
110 : : }
111 : :
112 : : int
113 : 18 : spdk_nvmf_tgt_remove_referral(struct spdk_nvmf_tgt *tgt,
114 : : const struct spdk_nvmf_referral_opts *uopts)
115 : : {
116 : : struct spdk_nvmf_referral *referral;
117 : 18 : struct spdk_nvmf_referral_opts opts = {};
118 : 18 : struct spdk_nvme_transport_id *trid = &opts.trid;
119 : :
120 [ - + - + ]: 18 : memcpy(&opts, uopts, spdk_min(uopts->size, sizeof(opts)));
121 [ + + ]: 18 : if (trid->subnqn[0] == '\0') {
122 [ - + ]: 12 : snprintf(trid->subnqn, sizeof(trid->subnqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
123 : : }
124 : :
125 : 18 : referral = nvmf_tgt_find_referral(tgt, &opts.trid);
126 [ - + ]: 18 : if (referral == NULL) {
127 : 0 : return -ENOENT;
128 : : }
129 : :
130 [ + + ]: 18 : TAILQ_REMOVE(&tgt->referrals, referral, link);
131 : 18 : nvmf_update_discovery_log(tgt, NULL);
132 : :
133 : 18 : free(referral);
134 : :
135 : 18 : return 0;
136 : : }
137 : :
138 : : void
139 : 30898 : nvmf_qpair_set_state(struct spdk_nvmf_qpair *qpair,
140 : : enum spdk_nvmf_qpair_state state)
141 : : {
142 [ - + ]: 30898 : assert(qpair != NULL);
143 [ - + ]: 30898 : assert(qpair->group->thread == spdk_get_thread());
144 : :
145 : 30898 : qpair->state = state;
146 : 30898 : }
147 : :
148 : : static int
149 : 2250254778 : nvmf_poll_group_poll(void *ctx)
150 : : {
151 : 2250254778 : struct spdk_nvmf_poll_group *group = ctx;
152 : : int rc;
153 : 2250254778 : int count = 0;
154 : : struct spdk_nvmf_transport_poll_group *tgroup;
155 : :
156 [ + + ]: 3323632270 : TAILQ_FOREACH(tgroup, &group->tgroups, link) {
157 : 1073377521 : rc = nvmf_transport_poll_group_poll(tgroup);
158 [ + + ]: 1073377521 : if (rc < 0) {
159 : 20 : return SPDK_POLLER_BUSY;
160 : : }
161 : 1073377501 : count += rc;
162 : : }
163 : :
164 : 2250254758 : return count > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
165 : : }
166 : :
167 : : /*
168 : : * Reset and clean up the poll group (I/O channel code will actually free the
169 : : * group).
170 : : */
171 : : static void
172 : 1290 : nvmf_tgt_cleanup_poll_group(struct spdk_nvmf_poll_group *group)
173 : : {
174 : : struct spdk_nvmf_transport_poll_group *tgroup, *tmp;
175 : : struct spdk_nvmf_subsystem_poll_group *sgroup;
176 : : uint32_t sid, nsid;
177 : :
178 [ + + ]: 1866 : TAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp) {
179 [ - + ]: 576 : TAILQ_REMOVE(&group->tgroups, tgroup, link);
180 : 576 : nvmf_transport_poll_group_destroy(tgroup);
181 : : }
182 : :
183 [ + + ]: 1307277 : for (sid = 0; sid < group->num_sgroups; sid++) {
184 : 1305987 : sgroup = &group->sgroups[sid];
185 : :
186 [ - + ]: 1305987 : assert(sgroup != NULL);
187 : :
188 [ + + ]: 1305990 : for (nsid = 0; nsid < sgroup->num_ns; nsid++) {
189 [ + - ]: 3 : if (sgroup->ns_info[nsid].channel) {
190 : 3 : spdk_put_io_channel(sgroup->ns_info[nsid].channel);
191 : 3 : sgroup->ns_info[nsid].channel = NULL;
192 : : }
193 : : }
194 : :
195 : 1305987 : free(sgroup->ns_info);
196 : : }
197 : :
198 : 1290 : free(group->sgroups);
199 : :
200 : 1290 : spdk_poller_unregister(&group->poller);
201 : :
202 [ + + ]: 1290 : if (group->destroy_cb_fn) {
203 : 1287 : group->destroy_cb_fn(group->destroy_cb_arg, 0);
204 : : }
205 : 1290 : }
206 : :
207 : : /*
208 : : * Callback to unregister a poll group from the target, and clean up its state.
209 : : */
210 : : static void
211 : 1290 : nvmf_tgt_destroy_poll_group(void *io_device, void *ctx_buf)
212 : : {
213 : 1290 : struct spdk_nvmf_tgt *tgt = io_device;
214 : 1290 : struct spdk_nvmf_poll_group *group = ctx_buf;
215 : :
216 : 288 : SPDK_DTRACE_PROBE1_TICKS(nvmf_destroy_poll_group, spdk_thread_get_id(group->thread));
217 : :
218 [ - + ]: 1290 : pthread_mutex_lock(&tgt->mutex);
219 [ + + ]: 1290 : TAILQ_REMOVE(&tgt->poll_groups, group, link);
220 : 1290 : tgt->num_poll_groups--;
221 [ - + ]: 1290 : pthread_mutex_unlock(&tgt->mutex);
222 : :
223 [ + - + - ]: 1290 : assert(!(tgt->state == NVMF_TGT_PAUSING || tgt->state == NVMF_TGT_RESUMING));
224 : 1290 : nvmf_tgt_cleanup_poll_group(group);
225 : 1290 : }
226 : :
227 : : static int
228 : 576 : nvmf_poll_group_add_transport(struct spdk_nvmf_poll_group *group,
229 : : struct spdk_nvmf_transport *transport)
230 : : {
231 : 576 : struct spdk_nvmf_transport_poll_group *tgroup = nvmf_get_transport_poll_group(group, transport);
232 : :
233 [ - + ]: 576 : if (tgroup != NULL) {
234 : : /* Transport already in the poll group */
235 : 0 : return 0;
236 : : }
237 : :
238 : 576 : tgroup = nvmf_transport_poll_group_create(transport, group);
239 [ - + ]: 576 : if (!tgroup) {
240 : 0 : SPDK_ERRLOG("Unable to create poll group for transport\n");
241 : 0 : return -1;
242 : : }
243 : 220 : SPDK_DTRACE_PROBE2_TICKS(nvmf_transport_poll_group_create, transport,
244 : : spdk_thread_get_id(group->thread));
245 : :
246 : 576 : tgroup->group = group;
247 : 576 : TAILQ_INSERT_TAIL(&group->tgroups, tgroup, link);
248 : :
249 : 576 : return 0;
250 : : }
251 : :
252 : : static void
253 : 4 : nvmf_tgt_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode)
254 : : {
255 : 4 : }
256 : :
257 : : static int
258 : 1290 : nvmf_tgt_create_poll_group(void *io_device, void *ctx_buf)
259 : : {
260 : 1290 : struct spdk_nvmf_tgt *tgt = io_device;
261 : 1290 : struct spdk_nvmf_poll_group *group = ctx_buf;
262 : : struct spdk_nvmf_transport *transport;
263 : : struct spdk_nvmf_subsystem *subsystem;
264 : 1290 : struct spdk_thread *thread = spdk_get_thread();
265 : : uint32_t i;
266 : : int rc;
267 : :
268 : 1290 : group->tgt = tgt;
269 : 1290 : TAILQ_INIT(&group->tgroups);
270 : 1290 : TAILQ_INIT(&group->qpairs);
271 : 1290 : group->thread = thread;
272 [ - + ]: 1290 : pthread_mutex_init(&group->mutex, NULL);
273 : :
274 : 1290 : group->poller = SPDK_POLLER_REGISTER(nvmf_poll_group_poll, group, 0);
275 : 1290 : spdk_poller_register_interrupt(group->poller, nvmf_tgt_poller_set_interrupt_mode, NULL);
276 : :
277 : 288 : SPDK_DTRACE_PROBE1_TICKS(nvmf_create_poll_group, spdk_thread_get_id(thread));
278 : :
279 [ + + ]: 1293 : TAILQ_FOREACH(transport, &tgt->transports, link) {
280 : 3 : rc = nvmf_poll_group_add_transport(group, transport);
281 [ - + ]: 3 : if (rc != 0) {
282 : 0 : nvmf_tgt_cleanup_poll_group(group);
283 : 0 : return rc;
284 : : }
285 : : }
286 : :
287 : 1290 : group->num_sgroups = tgt->max_subsystems;
288 : 1290 : group->sgroups = calloc(tgt->max_subsystems, sizeof(struct spdk_nvmf_subsystem_poll_group));
289 [ - + ]: 1290 : if (!group->sgroups) {
290 : 0 : nvmf_tgt_cleanup_poll_group(group);
291 : 0 : return -ENOMEM;
292 : : }
293 : :
294 [ + + ]: 1307277 : for (i = 0; i < tgt->max_subsystems; i++) {
295 : 1305987 : TAILQ_INIT(&group->sgroups[i].queued);
296 : : }
297 : :
298 [ + + ]: 1336 : for (subsystem = spdk_nvmf_subsystem_get_first(tgt);
299 [ + + ]: 2488 : subsystem != NULL;
300 : 1290 : subsystem = spdk_nvmf_subsystem_get_next(subsystem)) {
301 [ - + ]: 1290 : if (nvmf_poll_group_add_subsystem(group, subsystem, NULL, NULL) != 0) {
302 : 0 : nvmf_tgt_cleanup_poll_group(group);
303 : 0 : return -1;
304 : : }
305 : : }
306 : :
307 [ - + ]: 1290 : pthread_mutex_lock(&tgt->mutex);
308 : 1290 : tgt->num_poll_groups++;
309 : 1290 : TAILQ_INSERT_TAIL(&tgt->poll_groups, group, link);
310 [ - + ]: 1290 : pthread_mutex_unlock(&tgt->mutex);
311 : :
312 : 1290 : return 0;
313 : : }
314 : :
315 : : static void
316 : 1287 : _nvmf_tgt_disconnect_qpairs(void *ctx)
317 : : {
318 : : struct spdk_nvmf_qpair *qpair, *qpair_tmp;
319 : 1287 : struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
320 : 1287 : struct spdk_nvmf_poll_group *group = qpair_ctx->group;
321 : : struct spdk_io_channel *ch;
322 : : int rc;
323 : :
324 [ - + ]: 1287 : TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, qpair_tmp) {
325 : 0 : rc = spdk_nvmf_qpair_disconnect(qpair);
326 [ # # # # ]: 0 : if (rc && rc != -EINPROGRESS) {
327 : 0 : break;
328 : : }
329 : : }
330 : :
331 [ + - ]: 1287 : if (TAILQ_EMPTY(&group->qpairs)) {
332 : : /* When the refcount from the channels reaches 0, nvmf_tgt_destroy_poll_group will be called. */
333 : 1287 : ch = spdk_io_channel_from_ctx(group);
334 : 1287 : spdk_put_io_channel(ch);
335 : 1287 : free(qpair_ctx);
336 : 1287 : return;
337 : : }
338 : :
339 : : /* Some qpairs are in process of being disconnected. Send a message and try to remove them again */
340 : 0 : spdk_thread_send_msg(spdk_get_thread(), _nvmf_tgt_disconnect_qpairs, ctx);
341 : : }
342 : :
343 : : static void
344 : 1287 : nvmf_tgt_destroy_poll_group_qpairs(struct spdk_nvmf_poll_group *group)
345 : : {
346 : : struct nvmf_qpair_disconnect_many_ctx *ctx;
347 : :
348 : 288 : SPDK_DTRACE_PROBE1_TICKS(nvmf_destroy_poll_group_qpairs, spdk_thread_get_id(group->thread));
349 : :
350 : 1287 : ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx));
351 [ - + ]: 1287 : if (!ctx) {
352 : 0 : SPDK_ERRLOG("Failed to allocate memory for destroy poll group ctx\n");
353 : 0 : return;
354 : : }
355 : :
356 : 1287 : ctx->group = group;
357 : 1287 : _nvmf_tgt_disconnect_qpairs(ctx);
358 : : }
359 : :
360 : : struct spdk_nvmf_tgt *
361 : 774 : spdk_nvmf_tgt_create(struct spdk_nvmf_target_opts *_opts)
362 : : {
363 : : struct spdk_nvmf_tgt *tgt, *tmp_tgt;
364 : 774 : struct spdk_nvmf_target_opts opts = {
365 : : .max_subsystems = SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS,
366 : : .discovery_filter = SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY,
367 : : };
368 : :
369 [ - + ]: 774 : memcpy(&opts, _opts, _opts->size);
370 [ - + ]: 774 : if (strnlen(opts.name, NVMF_TGT_NAME_MAX_LENGTH) == NVMF_TGT_NAME_MAX_LENGTH) {
371 : 0 : SPDK_ERRLOG("Provided target name exceeds the max length of %u.\n", NVMF_TGT_NAME_MAX_LENGTH);
372 : 0 : return NULL;
373 : : }
374 : :
375 [ + + ]: 783 : TAILQ_FOREACH(tmp_tgt, &g_nvmf_tgts, link) {
376 [ - + - + ]: 9 : if (!strncmp(opts.name, tmp_tgt->name, NVMF_TGT_NAME_MAX_LENGTH)) {
377 : 0 : SPDK_ERRLOG("Provided target name must be unique.\n");
378 : 0 : return NULL;
379 : : }
380 : : }
381 : :
382 : 774 : tgt = calloc(1, sizeof(*tgt));
383 [ - + ]: 774 : if (!tgt) {
384 : 0 : return NULL;
385 : : }
386 : :
387 : 774 : snprintf(tgt->name, NVMF_TGT_NAME_MAX_LENGTH, "%s", opts.name);
388 : :
389 [ + + ]: 774 : if (!opts.max_subsystems) {
390 : 725 : tgt->max_subsystems = SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS;
391 : : } else {
392 : 49 : tgt->max_subsystems = opts.max_subsystems;
393 : : }
394 : :
395 : 774 : tgt->crdt[0] = opts.crdt[0];
396 : 774 : tgt->crdt[1] = opts.crdt[1];
397 : 774 : tgt->crdt[2] = opts.crdt[2];
398 : 774 : tgt->discovery_filter = opts.discovery_filter;
399 : 774 : tgt->discovery_genctr = 0;
400 : 774 : tgt->dhchap_digests = opts.dhchap_digests;
401 : 774 : tgt->dhchap_dhgroups = opts.dhchap_dhgroups;
402 : 774 : TAILQ_INIT(&tgt->transports);
403 : 774 : TAILQ_INIT(&tgt->poll_groups);
404 : 774 : TAILQ_INIT(&tgt->referrals);
405 : 774 : tgt->num_poll_groups = 0;
406 : :
407 : 774 : tgt->subsystem_ids = spdk_bit_array_create(tgt->max_subsystems);
408 [ - + ]: 774 : if (tgt->subsystem_ids == NULL) {
409 : 0 : free(tgt);
410 : 0 : return NULL;
411 : : }
412 : :
413 : 774 : RB_INIT(&tgt->subsystems);
414 : :
415 [ - + ]: 774 : pthread_mutex_init(&tgt->mutex, NULL);
416 : :
417 : 774 : spdk_io_device_register(tgt,
418 : : nvmf_tgt_create_poll_group,
419 : : nvmf_tgt_destroy_poll_group,
420 : : sizeof(struct spdk_nvmf_poll_group),
421 : 774 : tgt->name);
422 : :
423 : 774 : tgt->state = NVMF_TGT_RUNNING;
424 : :
425 [ + + ]: 774 : TAILQ_INSERT_HEAD(&g_nvmf_tgts, tgt, link);
426 : :
427 : 774 : return tgt;
428 : : }
429 : :
430 : : static void
431 : 1030 : _nvmf_tgt_destroy_next_transport(void *ctx)
432 : : {
433 : 1030 : struct spdk_nvmf_tgt *tgt = ctx;
434 : : struct spdk_nvmf_transport *transport;
435 : :
436 [ + + ]: 1030 : if (!TAILQ_EMPTY(&tgt->transports)) {
437 : 256 : transport = TAILQ_FIRST(&tgt->transports);
438 [ - + ]: 256 : TAILQ_REMOVE(&tgt->transports, transport, link);
439 : 256 : spdk_nvmf_transport_destroy(transport, _nvmf_tgt_destroy_next_transport, tgt);
440 : : } else {
441 : 774 : spdk_nvmf_tgt_destroy_done_fn *destroy_cb_fn = tgt->destroy_cb_fn;
442 : 774 : void *destroy_cb_arg = tgt->destroy_cb_arg;
443 : :
444 [ - + ]: 774 : pthread_mutex_destroy(&tgt->mutex);
445 : 774 : free(tgt);
446 : :
447 [ + - ]: 774 : if (destroy_cb_fn) {
448 : 774 : destroy_cb_fn(destroy_cb_arg, 0);
449 : : }
450 : : }
451 : 1030 : }
452 : :
453 : : static void
454 : 774 : nvmf_tgt_destroy_cb(void *io_device)
455 : : {
456 : 774 : struct spdk_nvmf_tgt *tgt = io_device;
457 : : struct spdk_nvmf_subsystem *subsystem, *subsystem_next;
458 : : int rc;
459 : : struct spdk_nvmf_referral *referral;
460 : :
461 [ + + ]: 777 : while ((referral = TAILQ_FIRST(&tgt->referrals))) {
462 [ - + ]: 3 : TAILQ_REMOVE(&tgt->referrals, referral, link);
463 : 3 : free(referral);
464 : : }
465 : :
466 : : /* We will be freeing subsystems in this loop, so we always need to get the next one
467 : : * ahead of time, since we can't call get_next() on a subsystem that's been freed.
468 : : */
469 [ - + ]: 1548 : for (subsystem = spdk_nvmf_subsystem_get_first(tgt),
470 : 774 : subsystem_next = spdk_nvmf_subsystem_get_next(subsystem);
471 [ + + ]: 747 : subsystem != NULL;
472 : 6 : subsystem = subsystem_next,
473 : 6 : subsystem_next = spdk_nvmf_subsystem_get_next(subsystem_next)) {
474 : 6 : nvmf_subsystem_remove_all_listeners(subsystem, true);
475 : :
476 : 6 : rc = spdk_nvmf_subsystem_destroy(subsystem, nvmf_tgt_destroy_cb, tgt);
477 [ - + ]: 6 : if (rc) {
478 [ # # ]: 0 : if (rc == -EINPROGRESS) {
479 : : /* If rc is -EINPROGRESS, nvmf_tgt_destroy_cb will be called again when subsystem #i
480 : : * is destroyed, nvmf_tgt_destroy_cb will continue to destroy other subsystems if any */
481 : 0 : return;
482 : : } else {
483 : 0 : SPDK_ERRLOG("Failed to destroy subsystem %s, rc %d\n", subsystem->subnqn, rc);
484 : : }
485 : : }
486 : : }
487 : 774 : spdk_bit_array_free(&tgt->subsystem_ids);
488 : 774 : _nvmf_tgt_destroy_next_transport(tgt);
489 : : }
490 : :
491 : : void
492 : 774 : spdk_nvmf_tgt_destroy(struct spdk_nvmf_tgt *tgt,
493 : : spdk_nvmf_tgt_destroy_done_fn cb_fn,
494 : : void *cb_arg)
495 : : {
496 [ + - + - ]: 774 : assert(!(tgt->state == NVMF_TGT_PAUSING || tgt->state == NVMF_TGT_RESUMING));
497 : :
498 : 774 : tgt->destroy_cb_fn = cb_fn;
499 : 774 : tgt->destroy_cb_arg = cb_arg;
500 : :
501 [ + + ]: 774 : TAILQ_REMOVE(&g_nvmf_tgts, tgt, link);
502 : :
503 : 774 : spdk_io_device_unregister(tgt, nvmf_tgt_destroy_cb);
504 : 774 : }
505 : :
506 : : const char *
507 : 21 : spdk_nvmf_tgt_get_name(struct spdk_nvmf_tgt *tgt)
508 : : {
509 : 21 : return tgt->name;
510 : : }
511 : :
512 : : struct spdk_nvmf_tgt *
513 : 10217 : spdk_nvmf_get_tgt(const char *name)
514 : : {
515 : : struct spdk_nvmf_tgt *tgt;
516 : 10217 : uint32_t num_targets = 0;
517 : :
518 [ + + ]: 20434 : TAILQ_FOREACH(tgt, &g_nvmf_tgts, link) {
519 [ + + ]: 10223 : if (name) {
520 [ - + - + : 24 : if (!strncmp(tgt->name, name, NVMF_TGT_NAME_MAX_LENGTH)) {
+ + ]
521 : 6 : return tgt;
522 : : }
523 : : }
524 : 10217 : num_targets++;
525 : : }
526 : :
527 : : /*
528 : : * special case. If there is only one target and
529 : : * no name was specified, return the only available
530 : : * target. If there is more than one target, name must
531 : : * be specified.
532 : : */
533 [ + + + - ]: 10211 : if (!name && num_targets == 1) {
534 : 10199 : return TAILQ_FIRST(&g_nvmf_tgts);
535 : : }
536 : :
537 : 12 : return NULL;
538 : : }
539 : :
540 : : struct spdk_nvmf_tgt *
541 : 9 : spdk_nvmf_get_first_tgt(void)
542 : : {
543 : 9 : return TAILQ_FIRST(&g_nvmf_tgts);
544 : : }
545 : :
546 : : struct spdk_nvmf_tgt *
547 : 15 : spdk_nvmf_get_next_tgt(struct spdk_nvmf_tgt *prev)
548 : : {
549 : 15 : return TAILQ_NEXT(prev, link);
550 : : }
551 : :
552 : : static void
553 : 23 : nvmf_write_nvme_subsystem_config(struct spdk_json_write_ctx *w,
554 : : struct spdk_nvmf_subsystem *subsystem)
555 : : {
556 : : struct spdk_nvmf_host *host;
557 : : struct spdk_nvmf_ns *ns;
558 : 1 : struct spdk_nvmf_ns_opts ns_opts;
559 : : uint32_t max_namespaces;
560 : : struct spdk_nvmf_transport *transport;
561 : :
562 [ - + ]: 23 : assert(spdk_nvmf_subsystem_get_type(subsystem) == SPDK_NVMF_SUBTYPE_NVME);
563 : :
564 : : /* { */
565 : 23 : spdk_json_write_object_begin(w);
566 : 23 : spdk_json_write_named_string(w, "method", "nvmf_create_subsystem");
567 : :
568 : : /* "params" : { */
569 : 23 : spdk_json_write_named_object_begin(w, "params");
570 : 23 : spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
571 : 23 : spdk_json_write_named_bool(w, "allow_any_host", spdk_nvmf_subsystem_get_allow_any_host(subsystem));
572 : 23 : spdk_json_write_named_string(w, "serial_number", spdk_nvmf_subsystem_get_sn(subsystem));
573 : 23 : spdk_json_write_named_string(w, "model_number", spdk_nvmf_subsystem_get_mn(subsystem));
574 : :
575 : 23 : max_namespaces = spdk_nvmf_subsystem_get_max_namespaces(subsystem);
576 [ + - ]: 23 : if (max_namespaces != 0) {
577 : 23 : spdk_json_write_named_uint32(w, "max_namespaces", max_namespaces);
578 : : }
579 : :
580 : 23 : spdk_json_write_named_uint32(w, "min_cntlid", spdk_nvmf_subsystem_get_min_cntlid(subsystem));
581 : 23 : spdk_json_write_named_uint32(w, "max_cntlid", spdk_nvmf_subsystem_get_max_cntlid(subsystem));
582 : 23 : spdk_json_write_named_bool(w, "ana_reporting", spdk_nvmf_subsystem_get_ana_reporting(subsystem));
583 : :
584 : : /* } "params" */
585 : 23 : spdk_json_write_object_end(w);
586 : :
587 : : /* } */
588 : 23 : spdk_json_write_object_end(w);
589 : :
590 [ + + ]: 29 : for (host = spdk_nvmf_subsystem_get_first_host(subsystem); host != NULL;
591 : 6 : host = spdk_nvmf_subsystem_get_next_host(subsystem, host)) {
592 : :
593 : 6 : spdk_json_write_object_begin(w);
594 : 6 : spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_host");
595 : :
596 : : /* "params" : { */
597 : 6 : spdk_json_write_named_object_begin(w, "params");
598 : :
599 : 6 : spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
600 : 6 : spdk_json_write_named_string(w, "host", spdk_nvmf_host_get_nqn(host));
601 [ - + ]: 6 : if (host->dhchap_key != NULL) {
602 : 0 : spdk_json_write_named_string(w, "dhchap_key",
603 : : spdk_key_get_name(host->dhchap_key));
604 : : }
605 [ - + ]: 6 : if (host->dhchap_ctrlr_key != NULL) {
606 : 0 : spdk_json_write_named_string(w, "dhchap_ctrlr_key",
607 : : spdk_key_get_name(host->dhchap_ctrlr_key));
608 : : }
609 [ + + ]: 12 : TAILQ_FOREACH(transport, &subsystem->tgt->transports, link) {
610 [ + - ]: 6 : if (transport->ops->subsystem_dump_host != NULL) {
611 : 6 : transport->ops->subsystem_dump_host(transport, subsystem, host->nqn, w);
612 : : }
613 : : }
614 : :
615 : : /* } "params" */
616 : 6 : spdk_json_write_object_end(w);
617 : :
618 : : /* } */
619 : 6 : spdk_json_write_object_end(w);
620 : : }
621 : :
622 [ + + ]: 62 : for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
623 : 39 : ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
624 : 39 : spdk_nvmf_ns_get_opts(ns, &ns_opts, sizeof(ns_opts));
625 : :
626 : 39 : spdk_json_write_object_begin(w);
627 : 39 : spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_ns");
628 : :
629 : : /* "params" : { */
630 : 39 : spdk_json_write_named_object_begin(w, "params");
631 : :
632 : 39 : spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
633 : :
634 : : /* "namespace" : { */
635 : 39 : spdk_json_write_named_object_begin(w, "namespace");
636 : :
637 : 39 : spdk_json_write_named_uint32(w, "nsid", spdk_nvmf_ns_get_id(ns));
638 : 39 : spdk_json_write_named_string(w, "bdev_name", spdk_bdev_get_name(spdk_nvmf_ns_get_bdev(ns)));
639 : :
640 [ - + ]: 39 : if (ns->ptpl_file != NULL) {
641 : 0 : spdk_json_write_named_string(w, "ptpl_file", ns->ptpl_file);
642 : : }
643 : :
644 [ + - ]: 39 : if (!spdk_mem_all_zero(ns_opts.nguid, sizeof(ns_opts.nguid))) {
645 : : SPDK_STATIC_ASSERT(sizeof(ns_opts.nguid) == sizeof(uint64_t) * 2, "size mismatch");
646 : 39 : spdk_json_write_named_string_fmt(w, "nguid", "%016"PRIX64"%016"PRIX64, from_be64(&ns_opts.nguid[0]),
647 : : from_be64(&ns_opts.nguid[8]));
648 : : }
649 : :
650 [ - + ]: 39 : if (!spdk_mem_all_zero(ns_opts.eui64, sizeof(ns_opts.eui64))) {
651 : : SPDK_STATIC_ASSERT(sizeof(ns_opts.eui64) == sizeof(uint64_t), "size mismatch");
652 : 0 : spdk_json_write_named_string_fmt(w, "eui64", "%016"PRIX64, from_be64(&ns_opts.eui64));
653 : : }
654 : :
655 [ + - ]: 39 : if (!spdk_uuid_is_null(&ns_opts.uuid)) {
656 : 39 : spdk_json_write_named_uuid(w, "uuid", &ns_opts.uuid);
657 : : }
658 : :
659 [ - + ]: 39 : if (spdk_nvmf_subsystem_get_ana_reporting(subsystem)) {
660 : 0 : spdk_json_write_named_uint32(w, "anagrpid", ns_opts.anagrpid);
661 : : }
662 : :
663 [ - + ]: 39 : spdk_json_write_named_bool(w, "no_auto_visible", !ns->always_visible);
664 : :
665 : : /* "namespace" */
666 : 39 : spdk_json_write_object_end(w);
667 : :
668 : : /* } "params" */
669 : 39 : spdk_json_write_object_end(w);
670 : :
671 : : /* } */
672 : 39 : spdk_json_write_object_end(w);
673 : :
674 [ - + ]: 39 : TAILQ_FOREACH(host, &ns->hosts, link) {
675 : 0 : spdk_json_write_object_begin(w);
676 : 0 : spdk_json_write_named_string(w, "method", "nvmf_ns_add_host");
677 : 0 : spdk_json_write_named_object_begin(w, "params");
678 : 0 : spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
679 : 0 : spdk_json_write_named_uint32(w, "nsid", spdk_nvmf_ns_get_id(ns));
680 : 0 : spdk_json_write_named_string(w, "host", spdk_nvmf_host_get_nqn(host));
681 : 0 : spdk_json_write_object_end(w);
682 : 0 : spdk_json_write_object_end(w);
683 : : }
684 : : }
685 : 23 : }
686 : :
687 : : static void
688 : 128 : nvmf_write_subsystem_config_json(struct spdk_json_write_ctx *w,
689 : : struct spdk_nvmf_subsystem *subsystem)
690 : : {
691 : : struct spdk_nvmf_subsystem_listener *listener;
692 : : struct spdk_nvmf_transport *transport;
693 : : const struct spdk_nvme_transport_id *trid;
694 : :
695 [ + + ]: 128 : if (spdk_nvmf_subsystem_get_type(subsystem) == SPDK_NVMF_SUBTYPE_NVME) {
696 : 23 : nvmf_write_nvme_subsystem_config(w, subsystem);
697 : : }
698 : :
699 [ + + ]: 151 : for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL;
700 : 23 : listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) {
701 : 23 : transport = listener->transport;
702 : 23 : trid = spdk_nvmf_subsystem_listener_get_trid(listener);
703 : :
704 : 23 : spdk_json_write_object_begin(w);
705 : 23 : spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_listener");
706 : :
707 : : /* "params" : { */
708 : 23 : spdk_json_write_named_object_begin(w, "params");
709 : :
710 : 23 : spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
711 : :
712 : 23 : spdk_json_write_named_object_begin(w, "listen_address");
713 : 23 : nvmf_transport_listen_dump_trid(trid, w);
714 : 23 : spdk_json_write_object_end(w);
715 [ - + ]: 23 : if (transport->ops->listen_dump_opts) {
716 : 0 : transport->ops->listen_dump_opts(transport, trid, w);
717 : : }
718 : :
719 [ - + ]: 23 : spdk_json_write_named_bool(w, "secure_channel", listener->opts.secure_channel);
720 : :
721 : : /* } "params" */
722 : 23 : spdk_json_write_object_end(w);
723 : :
724 : : /* } */
725 : 23 : spdk_json_write_object_end(w);
726 : : }
727 : 128 : }
728 : :
729 : : void
730 : 105 : spdk_nvmf_tgt_write_config_json(struct spdk_json_write_ctx *w, struct spdk_nvmf_tgt *tgt)
731 : : {
732 : : struct spdk_nvmf_subsystem *subsystem;
733 : : struct spdk_nvmf_transport *transport;
734 : : struct spdk_nvmf_referral *referral;
735 : :
736 : 105 : spdk_json_write_object_begin(w);
737 : 105 : spdk_json_write_named_string(w, "method", "nvmf_set_max_subsystems");
738 : :
739 : 105 : spdk_json_write_named_object_begin(w, "params");
740 : 105 : spdk_json_write_named_uint32(w, "max_subsystems", tgt->max_subsystems);
741 : 105 : spdk_json_write_object_end(w);
742 : :
743 : 105 : spdk_json_write_object_end(w);
744 : :
745 : 105 : spdk_json_write_object_begin(w);
746 : 105 : spdk_json_write_named_string(w, "method", "nvmf_set_crdt");
747 : 105 : spdk_json_write_named_object_begin(w, "params");
748 : 105 : spdk_json_write_named_uint32(w, "crdt1", tgt->crdt[0]);
749 : 105 : spdk_json_write_named_uint32(w, "crdt2", tgt->crdt[1]);
750 : 105 : spdk_json_write_named_uint32(w, "crdt3", tgt->crdt[2]);
751 : 105 : spdk_json_write_object_end(w);
752 : 105 : spdk_json_write_object_end(w);
753 : :
754 : : /* write transports */
755 [ + + ]: 151 : TAILQ_FOREACH(transport, &tgt->transports, link) {
756 : 46 : spdk_json_write_object_begin(w);
757 : 46 : spdk_json_write_named_string(w, "method", "nvmf_create_transport");
758 : 46 : nvmf_transport_dump_opts(transport, w, true);
759 : 46 : spdk_json_write_object_end(w);
760 : : }
761 : :
762 [ - + ]: 105 : TAILQ_FOREACH(referral, &tgt->referrals, link) {
763 : 0 : spdk_json_write_object_begin(w);
764 : 0 : spdk_json_write_named_string(w, "method", "nvmf_discovery_add_referral");
765 : :
766 : 0 : spdk_json_write_named_object_begin(w, "params");
767 : 0 : spdk_json_write_named_object_begin(w, "address");
768 : 0 : nvmf_transport_listen_dump_trid(&referral->trid, w);
769 : 0 : spdk_json_write_object_end(w);
770 : 0 : spdk_json_write_named_bool(w, "secure_channel",
771 : 0 : referral->entry.treq.secure_channel ==
772 : : SPDK_NVMF_TREQ_SECURE_CHANNEL_REQUIRED);
773 : 0 : spdk_json_write_named_string(w, "subnqn", referral->trid.subnqn);
774 : 0 : spdk_json_write_object_end(w);
775 : :
776 : 0 : spdk_json_write_object_end(w);
777 : : }
778 : :
779 : 105 : subsystem = spdk_nvmf_subsystem_get_first(tgt);
780 [ + + ]: 233 : while (subsystem) {
781 : 128 : nvmf_write_subsystem_config_json(w, subsystem);
782 : 128 : subsystem = spdk_nvmf_subsystem_get_next(subsystem);
783 : : }
784 : 105 : }
785 : :
786 : : static void
787 : 965 : nvmf_listen_opts_copy(struct spdk_nvmf_listen_opts *opts,
788 : : const struct spdk_nvmf_listen_opts *opts_src, size_t opts_size)
789 : : {
790 [ - + ]: 965 : assert(opts);
791 [ - + ]: 965 : assert(opts_src);
792 : :
793 : 965 : opts->opts_size = opts_size;
794 : :
795 : : #define SET_FIELD(field) \
796 : : if (offsetof(struct spdk_nvmf_listen_opts, field) + sizeof(opts->field) <= opts_size) { \
797 : : opts->field = opts_src->field; \
798 : : } \
799 : :
800 [ + - ]: 965 : SET_FIELD(transport_specific);
801 [ + - - + ]: 965 : SET_FIELD(secure_channel);
802 [ + - ]: 965 : SET_FIELD(ana_state);
803 : : #undef SET_FIELD
804 : :
805 : : /* Do not remove this statement, you should always update this statement when you adding a new field,
806 : : * and do not forget to add the SET_FIELD statement for your added field. */
807 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_nvmf_listen_opts) == 24, "Incorrect size");
808 : 965 : }
809 : :
810 : : void
811 : 484 : spdk_nvmf_listen_opts_init(struct spdk_nvmf_listen_opts *opts, size_t opts_size)
812 : : {
813 : 484 : struct spdk_nvmf_listen_opts opts_local = {};
814 : :
815 : : /* local version of opts should have defaults set here */
816 : 484 : opts_local.ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
817 : 484 : nvmf_listen_opts_copy(opts, &opts_local, opts_size);
818 : 484 : }
819 : :
820 : : int
821 : 481 : spdk_nvmf_tgt_listen_ext(struct spdk_nvmf_tgt *tgt, const struct spdk_nvme_transport_id *trid,
822 : : struct spdk_nvmf_listen_opts *opts)
823 : : {
824 : : struct spdk_nvmf_transport *transport;
825 : : int rc;
826 : 481 : struct spdk_nvmf_listen_opts opts_local = {};
827 : :
828 [ - + ]: 481 : if (!opts) {
829 : 0 : SPDK_ERRLOG("opts should not be NULL\n");
830 : 0 : return -EINVAL;
831 : : }
832 : :
833 [ - + ]: 481 : if (!opts->opts_size) {
834 : 0 : SPDK_ERRLOG("The opts_size in opts structure should not be zero\n");
835 : 0 : return -EINVAL;
836 : : }
837 : :
838 : 481 : transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring);
839 [ - + ]: 481 : if (!transport) {
840 : 0 : SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
841 : : trid->trstring);
842 : 0 : return -EINVAL;
843 : : }
844 : :
845 : 481 : nvmf_listen_opts_copy(&opts_local, opts, opts->opts_size);
846 : 481 : rc = spdk_nvmf_transport_listen(transport, trid, &opts_local);
847 [ - + ]: 481 : if (rc < 0) {
848 : 0 : SPDK_ERRLOG("Unable to listen on address '%s'\n", trid->traddr);
849 : : }
850 : :
851 : 481 : return rc;
852 : : }
853 : :
854 : : int
855 : 0 : spdk_nvmf_tgt_stop_listen(struct spdk_nvmf_tgt *tgt,
856 : : struct spdk_nvme_transport_id *trid)
857 : : {
858 : : struct spdk_nvmf_transport *transport;
859 : : int rc;
860 : :
861 : 0 : transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring);
862 [ # # ]: 0 : if (!transport) {
863 : 0 : SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
864 : : trid->trstring);
865 : 0 : return -EINVAL;
866 : : }
867 : :
868 : 0 : rc = spdk_nvmf_transport_stop_listen(transport, trid);
869 [ # # ]: 0 : if (rc < 0) {
870 : 0 : SPDK_ERRLOG("Failed to stop listening on address '%s'\n", trid->traddr);
871 : 0 : return rc;
872 : : }
873 : 0 : return 0;
874 : : }
875 : :
876 : : struct spdk_nvmf_tgt_add_transport_ctx {
877 : : struct spdk_nvmf_tgt *tgt;
878 : : struct spdk_nvmf_transport *transport;
879 : : spdk_nvmf_tgt_add_transport_done_fn cb_fn;
880 : : void *cb_arg;
881 : : int status;
882 : : };
883 : :
884 : : static void
885 : 0 : _nvmf_tgt_remove_transport_done(struct spdk_io_channel_iter *i, int status)
886 : : {
887 : 0 : struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
888 : :
889 : 0 : ctx->cb_fn(ctx->cb_arg, ctx->status);
890 : 0 : free(ctx);
891 : 0 : }
892 : :
893 : : static void
894 : 0 : _nvmf_tgt_remove_transport(struct spdk_io_channel_iter *i)
895 : : {
896 : 0 : struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
897 : 0 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
898 : 0 : struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
899 : : struct spdk_nvmf_transport_poll_group *tgroup, *tmp;
900 : :
901 [ # # ]: 0 : TAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp) {
902 [ # # ]: 0 : if (tgroup->transport == ctx->transport) {
903 [ # # ]: 0 : TAILQ_REMOVE(&group->tgroups, tgroup, link);
904 : 0 : nvmf_transport_poll_group_destroy(tgroup);
905 : : }
906 : : }
907 : :
908 : 0 : spdk_for_each_channel_continue(i, 0);
909 : 0 : }
910 : :
911 : : static void
912 : 256 : _nvmf_tgt_add_transport_done(struct spdk_io_channel_iter *i, int status)
913 : : {
914 : 256 : struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
915 : :
916 [ - + ]: 256 : if (status) {
917 : 0 : ctx->status = status;
918 : 0 : spdk_for_each_channel(ctx->tgt,
919 : : _nvmf_tgt_remove_transport,
920 : : ctx,
921 : : _nvmf_tgt_remove_transport_done);
922 : 0 : return;
923 : : }
924 : :
925 : 256 : ctx->transport->tgt = ctx->tgt;
926 : 256 : TAILQ_INSERT_TAIL(&ctx->tgt->transports, ctx->transport, link);
927 : 256 : ctx->cb_fn(ctx->cb_arg, status);
928 : 256 : free(ctx);
929 : : }
930 : :
931 : : static void
932 : 573 : _nvmf_tgt_add_transport(struct spdk_io_channel_iter *i)
933 : : {
934 : 573 : struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
935 : 573 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
936 : 573 : struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
937 : : int rc;
938 : :
939 : 573 : rc = nvmf_poll_group_add_transport(group, ctx->transport);
940 : 573 : spdk_for_each_channel_continue(i, rc);
941 : 573 : }
942 : :
943 : : void
944 : 256 : spdk_nvmf_tgt_add_transport(struct spdk_nvmf_tgt *tgt,
945 : : struct spdk_nvmf_transport *transport,
946 : : spdk_nvmf_tgt_add_transport_done_fn cb_fn,
947 : : void *cb_arg)
948 : : {
949 : : struct spdk_nvmf_tgt_add_transport_ctx *ctx;
950 : :
951 : 100 : SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_add_transport, transport, tgt->name);
952 : :
953 [ - + ]: 256 : if (spdk_nvmf_tgt_get_transport(tgt, transport->ops->name)) {
954 : 0 : cb_fn(cb_arg, -EEXIST);
955 : 0 : return; /* transport already created */
956 : : }
957 : :
958 : 256 : ctx = calloc(1, sizeof(*ctx));
959 [ - + ]: 256 : if (!ctx) {
960 : 0 : cb_fn(cb_arg, -ENOMEM);
961 : 0 : return;
962 : : }
963 : :
964 : 256 : ctx->tgt = tgt;
965 : 256 : ctx->transport = transport;
966 : 256 : ctx->cb_fn = cb_fn;
967 : 256 : ctx->cb_arg = cb_arg;
968 : :
969 : 256 : spdk_for_each_channel(tgt,
970 : : _nvmf_tgt_add_transport,
971 : : ctx,
972 : : _nvmf_tgt_add_transport_done);
973 : : }
974 : :
975 : : struct nvmf_tgt_pause_ctx {
976 : : struct spdk_nvmf_tgt *tgt;
977 : : spdk_nvmf_tgt_pause_polling_cb_fn cb_fn;
978 : : void *cb_arg;
979 : : };
980 : :
981 : : static void
982 : 0 : _nvmf_tgt_pause_polling_done(struct spdk_io_channel_iter *i, int status)
983 : : {
984 : 0 : struct nvmf_tgt_pause_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
985 : :
986 : 0 : ctx->tgt->state = NVMF_TGT_PAUSED;
987 : :
988 : 0 : ctx->cb_fn(ctx->cb_arg, status);
989 : 0 : free(ctx);
990 : 0 : }
991 : :
992 : : static void
993 : 0 : _nvmf_tgt_pause_polling(struct spdk_io_channel_iter *i)
994 : : {
995 : 0 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
996 : 0 : struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
997 : :
998 : 0 : spdk_poller_unregister(&group->poller);
999 : :
1000 : 0 : spdk_for_each_channel_continue(i, 0);
1001 : 0 : }
1002 : :
1003 : : int
1004 : 0 : spdk_nvmf_tgt_pause_polling(struct spdk_nvmf_tgt *tgt, spdk_nvmf_tgt_pause_polling_cb_fn cb_fn,
1005 : : void *cb_arg)
1006 : : {
1007 : : struct nvmf_tgt_pause_ctx *ctx;
1008 : :
1009 : 0 : SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_pause_polling, tgt, tgt->name);
1010 : :
1011 [ # # # ]: 0 : switch (tgt->state) {
1012 : 0 : case NVMF_TGT_PAUSING:
1013 : : case NVMF_TGT_RESUMING:
1014 : 0 : return -EBUSY;
1015 : 0 : case NVMF_TGT_RUNNING:
1016 : 0 : break;
1017 : 0 : default:
1018 : 0 : return -EINVAL;
1019 : : }
1020 : :
1021 : 0 : ctx = calloc(1, sizeof(*ctx));
1022 [ # # ]: 0 : if (!ctx) {
1023 : 0 : return -ENOMEM;
1024 : : }
1025 : :
1026 : :
1027 : 0 : tgt->state = NVMF_TGT_PAUSING;
1028 : :
1029 : 0 : ctx->tgt = tgt;
1030 : 0 : ctx->cb_fn = cb_fn;
1031 : 0 : ctx->cb_arg = cb_arg;
1032 : :
1033 : 0 : spdk_for_each_channel(tgt,
1034 : : _nvmf_tgt_pause_polling,
1035 : : ctx,
1036 : : _nvmf_tgt_pause_polling_done);
1037 : 0 : return 0;
1038 : : }
1039 : :
1040 : : static void
1041 : 0 : _nvmf_tgt_resume_polling_done(struct spdk_io_channel_iter *i, int status)
1042 : : {
1043 : 0 : struct nvmf_tgt_pause_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1044 : :
1045 : 0 : ctx->tgt->state = NVMF_TGT_RUNNING;
1046 : :
1047 : 0 : ctx->cb_fn(ctx->cb_arg, status);
1048 : 0 : free(ctx);
1049 : 0 : }
1050 : :
1051 : : static void
1052 : 0 : _nvmf_tgt_resume_polling(struct spdk_io_channel_iter *i)
1053 : : {
1054 : 0 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
1055 : 0 : struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
1056 : :
1057 [ # # ]: 0 : assert(group->poller == NULL);
1058 : 0 : group->poller = SPDK_POLLER_REGISTER(nvmf_poll_group_poll, group, 0);
1059 : :
1060 : 0 : spdk_for_each_channel_continue(i, 0);
1061 : 0 : }
1062 : :
1063 : : int
1064 : 0 : spdk_nvmf_tgt_resume_polling(struct spdk_nvmf_tgt *tgt, spdk_nvmf_tgt_resume_polling_cb_fn cb_fn,
1065 : : void *cb_arg)
1066 : : {
1067 : : struct nvmf_tgt_pause_ctx *ctx;
1068 : :
1069 : 0 : SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_resume_polling, tgt, tgt->name);
1070 : :
1071 [ # # # ]: 0 : switch (tgt->state) {
1072 : 0 : case NVMF_TGT_PAUSING:
1073 : : case NVMF_TGT_RESUMING:
1074 : 0 : return -EBUSY;
1075 : 0 : case NVMF_TGT_PAUSED:
1076 : 0 : break;
1077 : 0 : default:
1078 : 0 : return -EINVAL;
1079 : : }
1080 : :
1081 : 0 : ctx = calloc(1, sizeof(*ctx));
1082 [ # # ]: 0 : if (!ctx) {
1083 : 0 : return -ENOMEM;
1084 : : }
1085 : :
1086 : 0 : tgt->state = NVMF_TGT_RESUMING;
1087 : :
1088 : 0 : ctx->tgt = tgt;
1089 : 0 : ctx->cb_fn = cb_fn;
1090 : 0 : ctx->cb_arg = cb_arg;
1091 : :
1092 : 0 : spdk_for_each_channel(tgt,
1093 : : _nvmf_tgt_resume_polling,
1094 : : ctx,
1095 : : _nvmf_tgt_resume_polling_done);
1096 : 0 : return 0;
1097 : : }
1098 : :
1099 : : struct spdk_nvmf_subsystem *
1100 : 40785 : spdk_nvmf_tgt_find_subsystem(struct spdk_nvmf_tgt *tgt, const char *subnqn)
1101 : : {
1102 : 1035 : struct spdk_nvmf_subsystem subsystem;
1103 : :
1104 [ - + ]: 40785 : if (!subnqn) {
1105 : 0 : return NULL;
1106 : : }
1107 : :
1108 : : /* Ensure that subnqn is null terminated */
1109 [ - + - + ]: 40785 : if (!memchr(subnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1)) {
1110 : 0 : SPDK_ERRLOG("Connect SUBNQN is not null terminated\n");
1111 : 0 : return NULL;
1112 : : }
1113 : :
1114 : 40785 : snprintf(subsystem.subnqn, sizeof(subsystem.subnqn), "%s", subnqn);
1115 : 40785 : return RB_FIND(subsystem_tree, &tgt->subsystems, &subsystem);
1116 : : }
1117 : :
1118 : : struct spdk_nvmf_transport *
1119 : 1979 : spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, const char *transport_name)
1120 : : {
1121 : : struct spdk_nvmf_transport *transport;
1122 : :
1123 [ + + ]: 1979 : TAILQ_FOREACH(transport, &tgt->transports, link) {
1124 [ - + - + : 1448 : if (!strncasecmp(transport->ops->name, transport_name, SPDK_NVMF_TRSTRING_MAX_LEN)) {
+ - ]
1125 : 1448 : return transport;
1126 : : }
1127 : : }
1128 : 531 : return NULL;
1129 : : }
1130 : :
1131 : : struct nvmf_new_qpair_ctx {
1132 : : struct spdk_nvmf_qpair *qpair;
1133 : : struct spdk_nvmf_poll_group *group;
1134 : : };
1135 : :
1136 : : static void
1137 : 7640 : _nvmf_poll_group_add(void *_ctx)
1138 : : {
1139 : 7640 : struct nvmf_new_qpair_ctx *ctx = _ctx;
1140 : 7640 : struct spdk_nvmf_qpair *qpair = ctx->qpair;
1141 : 7640 : struct spdk_nvmf_poll_group *group = ctx->group;
1142 : :
1143 : 7640 : free(_ctx);
1144 : :
1145 [ - + ]: 7640 : if (spdk_nvmf_poll_group_add(group, qpair) != 0) {
1146 : 0 : SPDK_ERRLOG("Unable to add the qpair to a poll group.\n");
1147 : 0 : spdk_nvmf_qpair_disconnect(qpair);
1148 : : }
1149 : 7640 : }
1150 : :
1151 : : void
1152 : 7640 : spdk_nvmf_tgt_new_qpair(struct spdk_nvmf_tgt *tgt, struct spdk_nvmf_qpair *qpair)
1153 : : {
1154 : : struct spdk_nvmf_poll_group *group;
1155 : : struct nvmf_new_qpair_ctx *ctx;
1156 : :
1157 : 7640 : group = spdk_nvmf_get_optimal_poll_group(qpair);
1158 [ - + ]: 7640 : if (group == NULL) {
1159 [ # # ]: 0 : if (tgt->next_poll_group == NULL) {
1160 : 0 : tgt->next_poll_group = TAILQ_FIRST(&tgt->poll_groups);
1161 [ # # ]: 0 : if (tgt->next_poll_group == NULL) {
1162 : 0 : SPDK_ERRLOG("No poll groups exist.\n");
1163 : 0 : spdk_nvmf_qpair_disconnect(qpair);
1164 : 0 : return;
1165 : : }
1166 : : }
1167 : 0 : group = tgt->next_poll_group;
1168 : 0 : tgt->next_poll_group = TAILQ_NEXT(group, link);
1169 : : }
1170 : :
1171 : 7640 : ctx = calloc(1, sizeof(*ctx));
1172 [ - + ]: 7640 : if (!ctx) {
1173 : 0 : SPDK_ERRLOG("Unable to send message to poll group.\n");
1174 : 0 : spdk_nvmf_qpair_disconnect(qpair);
1175 : 0 : return;
1176 : : }
1177 : :
1178 : 7640 : ctx->qpair = qpair;
1179 : 7640 : ctx->group = group;
1180 : :
1181 [ - + ]: 7640 : pthread_mutex_lock(&group->mutex);
1182 : 7640 : group->current_unassociated_qpairs++;
1183 [ - + ]: 7640 : pthread_mutex_unlock(&group->mutex);
1184 : :
1185 : 7640 : spdk_thread_send_msg(group->thread, _nvmf_poll_group_add, ctx);
1186 : : }
1187 : :
1188 : : struct spdk_nvmf_poll_group *
1189 : 1287 : spdk_nvmf_poll_group_create(struct spdk_nvmf_tgt *tgt)
1190 : : {
1191 : : struct spdk_io_channel *ch;
1192 : :
1193 : 1287 : ch = spdk_get_io_channel(tgt);
1194 [ - + ]: 1287 : if (!ch) {
1195 : 0 : SPDK_ERRLOG("Unable to get I/O channel for target\n");
1196 : 0 : return NULL;
1197 : : }
1198 : :
1199 : 1287 : return spdk_io_channel_get_ctx(ch);
1200 : : }
1201 : :
1202 : : void
1203 : 1287 : spdk_nvmf_poll_group_destroy(struct spdk_nvmf_poll_group *group,
1204 : : spdk_nvmf_poll_group_destroy_done_fn cb_fn,
1205 : : void *cb_arg)
1206 : : {
1207 [ - + ]: 1287 : assert(group->destroy_cb_fn == NULL);
1208 : 1287 : group->destroy_cb_fn = cb_fn;
1209 : 1287 : group->destroy_cb_arg = cb_arg;
1210 : :
1211 : : /* This function will put the io_channel associated with this poll group */
1212 : 1287 : nvmf_tgt_destroy_poll_group_qpairs(group);
1213 : 1287 : }
1214 : :
1215 : : int
1216 : 7640 : spdk_nvmf_poll_group_add(struct spdk_nvmf_poll_group *group,
1217 : : struct spdk_nvmf_qpair *qpair)
1218 : : {
1219 : : int rc;
1220 : : struct spdk_nvmf_transport_poll_group *tgroup;
1221 : :
1222 : 7640 : TAILQ_INIT(&qpair->outstanding);
1223 : 7640 : qpair->group = group;
1224 : 7640 : qpair->ctrlr = NULL;
1225 : 7640 : qpair->disconnect_started = false;
1226 : :
1227 : 7640 : tgroup = nvmf_get_transport_poll_group(group, qpair->transport);
1228 [ - + ]: 7640 : if (tgroup == NULL) {
1229 : 0 : return -1;
1230 : : }
1231 : :
1232 : 7640 : rc = nvmf_transport_poll_group_add(tgroup, qpair);
1233 : :
1234 : : /* We add the qpair to the group only it is successfully added into the tgroup */
1235 [ + - ]: 7640 : if (rc == 0) {
1236 : 2333 : SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_add_qpair, qpair, spdk_thread_get_id(group->thread));
1237 : 7640 : TAILQ_INSERT_TAIL(&group->qpairs, qpair, link);
1238 : 7640 : nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_CONNECTING);
1239 : : }
1240 : :
1241 : 7640 : return rc;
1242 : : }
1243 : :
1244 : : static void
1245 : 1660 : _nvmf_ctrlr_destruct(void *ctx)
1246 : : {
1247 : 1660 : struct spdk_nvmf_ctrlr *ctrlr = ctx;
1248 : :
1249 : 1660 : nvmf_ctrlr_destruct(ctrlr);
1250 : 1660 : }
1251 : :
1252 : : static void
1253 : 7090 : _nvmf_ctrlr_free_from_qpair(void *ctx)
1254 : : {
1255 : 7090 : struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
1256 : 7090 : struct spdk_nvmf_ctrlr *ctrlr = qpair_ctx->ctrlr;
1257 : : uint32_t count;
1258 : :
1259 : 7090 : spdk_bit_array_clear(ctrlr->qpair_mask, qpair_ctx->qid);
1260 [ - + - + ]: 7090 : SPDK_DEBUGLOG(nvmf, "qpair_mask cleared, qid %u\n", qpair_ctx->qid);
1261 : 7090 : count = spdk_bit_array_count_set(ctrlr->qpair_mask);
1262 [ + + ]: 7090 : if (count == 0) {
1263 [ - + - + ]: 1660 : assert(!ctrlr->in_destruct);
1264 [ - + - + ]: 1660 : SPDK_DEBUGLOG(nvmf, "Last qpair %u, destroy ctrlr 0x%hx\n", qpair_ctx->qid, ctrlr->cntlid);
1265 : 1660 : ctrlr->in_destruct = true;
1266 : 1660 : spdk_thread_send_msg(ctrlr->subsys->thread, _nvmf_ctrlr_destruct, ctrlr);
1267 : : }
1268 : 7090 : free(qpair_ctx);
1269 : 7090 : }
1270 : :
1271 : : static void
1272 : 7640 : _nvmf_transport_qpair_fini_complete(void *cb_ctx)
1273 : : {
1274 : 7640 : struct nvmf_qpair_disconnect_ctx *qpair_ctx = cb_ctx;
1275 : : struct spdk_nvmf_ctrlr *ctrlr;
1276 : :
1277 : 7640 : ctrlr = qpair_ctx->ctrlr;
1278 [ - + - + ]: 7640 : SPDK_DEBUGLOG(nvmf, "Finish destroying qid %u\n", qpair_ctx->qid);
1279 : :
1280 [ + + ]: 7640 : if (ctrlr) {
1281 [ + + ]: 7090 : if (qpair_ctx->qid == 0) {
1282 : : /* Admin qpair is removed, so set the pointer to NULL.
1283 : : * This operation is safe since we are on ctrlr thread now, admin qpair's thread is the same
1284 : : * as controller's thread */
1285 [ - + ]: 1660 : assert(ctrlr->thread == spdk_get_thread());
1286 : 1660 : ctrlr->admin_qpair = NULL;
1287 : : }
1288 : : /* Free qpair id from controller's bit mask and destroy the controller if it is the last qpair */
1289 [ + - ]: 7090 : if (ctrlr->thread) {
1290 : 7090 : spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_free_from_qpair, qpair_ctx);
1291 : : } else {
1292 : 0 : _nvmf_ctrlr_free_from_qpair(qpair_ctx);
1293 : : }
1294 : : } else {
1295 : 550 : free(qpair_ctx);
1296 : : }
1297 : 7640 : }
1298 : :
1299 : : void
1300 : 7640 : spdk_nvmf_poll_group_remove(struct spdk_nvmf_qpair *qpair)
1301 : : {
1302 : : struct spdk_nvmf_transport_poll_group *tgroup;
1303 : : int rc;
1304 : :
1305 : 2333 : SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_remove_qpair, qpair,
1306 : : spdk_thread_get_id(qpair->group->thread));
1307 : 7640 : nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ERROR);
1308 : :
1309 : : /* Find the tgroup and remove the qpair from the tgroup */
1310 : 7640 : tgroup = nvmf_get_transport_poll_group(qpair->group, qpair->transport);
1311 [ + - ]: 7640 : if (tgroup != NULL) {
1312 : 7640 : rc = nvmf_transport_poll_group_remove(tgroup, qpair);
1313 [ - + - - ]: 7640 : if (rc && (rc != ENOTSUP)) {
1314 : 0 : SPDK_ERRLOG("Cannot remove qpair=%p from transport group=%p\n",
1315 : : qpair, tgroup);
1316 : : }
1317 : : }
1318 : :
1319 [ + + ]: 7640 : TAILQ_REMOVE(&qpair->group->qpairs, qpair, link);
1320 : 7640 : qpair->group = NULL;
1321 : 7640 : }
1322 : :
1323 : : static void
1324 : 570290 : _nvmf_qpair_sgroup_req_clean(struct spdk_nvmf_subsystem_poll_group *sgroup,
1325 : : const struct spdk_nvmf_qpair *qpair)
1326 : : {
1327 : : struct spdk_nvmf_request *req, *tmp;
1328 [ + + ]: 570291 : TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
1329 [ - + ]: 1 : if (req->qpair == qpair) {
1330 [ # # ]: 0 : TAILQ_REMOVE(&sgroup->queued, req, link);
1331 [ # # ]: 0 : if (nvmf_transport_req_free(req)) {
1332 : 0 : SPDK_ERRLOG("Transport request free error!\n");
1333 : : }
1334 : : }
1335 : : }
1336 : 570290 : }
1337 : :
1338 : : static void
1339 : 7640 : _nvmf_qpair_destroy(void *ctx, int status)
1340 : : {
1341 : 7640 : struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
1342 : 7640 : struct spdk_nvmf_qpair *qpair = qpair_ctx->qpair;
1343 : 7640 : struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
1344 : : struct spdk_nvmf_subsystem_poll_group *sgroup;
1345 : : uint32_t sid;
1346 : :
1347 [ - + ]: 7640 : assert(qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING);
1348 : 7640 : qpair_ctx->qid = qpair->qid;
1349 : :
1350 [ - + + + ]: 7640 : if (qpair->connect_received) {
1351 [ + + ]: 7616 : if (0 == qpair->qid) {
1352 [ - + ]: 1660 : assert(qpair->group->stat.current_admin_qpairs > 0);
1353 : 1660 : qpair->group->stat.current_admin_qpairs--;
1354 : : } else {
1355 [ - + ]: 5956 : assert(qpair->group->stat.current_io_qpairs > 0);
1356 : 5956 : qpair->group->stat.current_io_qpairs--;
1357 : : }
1358 : : } else {
1359 [ - + ]: 24 : pthread_mutex_lock(&qpair->group->mutex);
1360 : 24 : qpair->group->current_unassociated_qpairs--;
1361 [ - + ]: 24 : pthread_mutex_unlock(&qpair->group->mutex);
1362 : : }
1363 : :
1364 [ + + ]: 7640 : if (ctrlr) {
1365 : 7090 : sgroup = &qpair->group->sgroups[ctrlr->subsys->id];
1366 : 7090 : _nvmf_qpair_sgroup_req_clean(sgroup, qpair);
1367 : : } else {
1368 [ + + ]: 563750 : for (sid = 0; sid < qpair->group->num_sgroups; sid++) {
1369 : 563200 : sgroup = &qpair->group->sgroups[sid];
1370 [ - + ]: 563200 : assert(sgroup != NULL);
1371 : 563200 : _nvmf_qpair_sgroup_req_clean(sgroup, qpair);
1372 : : }
1373 : : }
1374 : :
1375 : 7640 : nvmf_qpair_auth_destroy(qpair);
1376 : 7640 : qpair_ctx->ctrlr = ctrlr;
1377 : 7640 : spdk_nvmf_poll_group_remove(qpair);
1378 : 7640 : nvmf_transport_qpair_fini(qpair, _nvmf_transport_qpair_fini_complete, qpair_ctx);
1379 : 7640 : }
1380 : :
1381 : : static void
1382 : 1415 : _nvmf_qpair_disconnect_msg(void *ctx)
1383 : : {
1384 : 1415 : struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
1385 : :
1386 : 1415 : spdk_nvmf_qpair_disconnect(qpair_ctx->qpair);
1387 : 1415 : free(ctx);
1388 : 1415 : }
1389 : :
1390 : : int
1391 : 947553 : spdk_nvmf_qpair_disconnect(struct spdk_nvmf_qpair *qpair)
1392 : : {
1393 : 947553 : struct spdk_nvmf_poll_group *group = qpair->group;
1394 : : struct nvmf_qpair_disconnect_ctx *qpair_ctx;
1395 : :
1396 [ + + ]: 947553 : if (__atomic_test_and_set(&qpair->disconnect_started, __ATOMIC_RELAXED)) {
1397 : 938498 : return -EINPROGRESS;
1398 : : }
1399 : :
1400 : : /* If we get a qpair in the uninitialized state, we can just destroy it immediately */
1401 [ - + ]: 9055 : if (qpair->state == SPDK_NVMF_QPAIR_UNINITIALIZED) {
1402 : 0 : nvmf_transport_qpair_fini(qpair, NULL, NULL);
1403 : 0 : return 0;
1404 : : }
1405 : :
1406 [ - + ]: 9055 : assert(group != NULL);
1407 [ + + ]: 9055 : if (spdk_get_thread() != group->thread) {
1408 : : /* clear the atomic so we can set it on the next call on the proper thread. */
1409 : 1415 : __atomic_clear(&qpair->disconnect_started, __ATOMIC_RELAXED);
1410 : 1415 : qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx));
1411 [ - + ]: 1415 : if (!qpair_ctx) {
1412 : 0 : SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n");
1413 : 0 : return -ENOMEM;
1414 : : }
1415 : 1415 : qpair_ctx->qpair = qpair;
1416 : 1415 : spdk_thread_send_msg(group->thread, _nvmf_qpair_disconnect_msg, qpair_ctx);
1417 : 1415 : return 0;
1418 : : }
1419 : :
1420 : 2333 : SPDK_DTRACE_PROBE2_TICKS(nvmf_qpair_disconnect, qpair, spdk_thread_get_id(group->thread));
1421 [ - + ]: 7640 : assert(spdk_nvmf_qpair_is_active(qpair));
1422 : 7640 : nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_DEACTIVATING);
1423 : :
1424 : 7640 : qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx));
1425 [ - + ]: 7640 : if (!qpair_ctx) {
1426 : 0 : SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n");
1427 : 0 : return -ENOMEM;
1428 : : }
1429 : :
1430 : 7640 : qpair_ctx->qpair = qpair;
1431 : :
1432 : : /* Check for outstanding I/O */
1433 [ + + ]: 7640 : if (!TAILQ_EMPTY(&qpair->outstanding)) {
1434 : 746 : SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_drain_qpair, qpair, spdk_thread_get_id(group->thread));
1435 : 1640 : qpair->state_cb = _nvmf_qpair_destroy;
1436 : 1640 : qpair->state_cb_arg = qpair_ctx;
1437 : 1640 : nvmf_qpair_abort_pending_zcopy_reqs(qpair);
1438 : 1640 : nvmf_qpair_free_aer(qpair);
1439 : 1640 : return 0;
1440 : : }
1441 : :
1442 : 6000 : _nvmf_qpair_destroy(qpair_ctx, 0);
1443 : :
1444 : 6000 : return 0;
1445 : : }
1446 : :
1447 : : int
1448 : 296 : spdk_nvmf_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair,
1449 : : struct spdk_nvme_transport_id *trid)
1450 : : {
1451 [ - + ]: 296 : memset(trid, 0, sizeof(*trid));
1452 : 296 : return nvmf_transport_qpair_get_peer_trid(qpair, trid);
1453 : : }
1454 : :
1455 : : int
1456 : 0 : spdk_nvmf_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair,
1457 : : struct spdk_nvme_transport_id *trid)
1458 : : {
1459 [ # # ]: 0 : memset(trid, 0, sizeof(*trid));
1460 : 0 : return nvmf_transport_qpair_get_local_trid(qpair, trid);
1461 : : }
1462 : :
1463 : : int
1464 : 9886 : spdk_nvmf_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair,
1465 : : struct spdk_nvme_transport_id *trid)
1466 : : {
1467 [ - + ]: 9886 : memset(trid, 0, sizeof(*trid));
1468 : 9886 : return nvmf_transport_qpair_get_listen_trid(qpair, trid);
1469 : : }
1470 : :
1471 : : static int
1472 : 23338 : poll_group_update_subsystem(struct spdk_nvmf_poll_group *group,
1473 : : struct spdk_nvmf_subsystem *subsystem)
1474 : : {
1475 : : struct spdk_nvmf_subsystem_poll_group *sgroup;
1476 : : uint32_t i, j;
1477 : : struct spdk_nvmf_ns *ns;
1478 : : struct spdk_nvmf_registrant *reg, *tmp;
1479 : : struct spdk_io_channel *ch;
1480 : : struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
1481 : : struct spdk_nvmf_ctrlr *ctrlr;
1482 : : bool ns_changed;
1483 : :
1484 : : /* Make sure our poll group has memory for this subsystem allocated */
1485 [ - + ]: 23338 : if (subsystem->id >= group->num_sgroups) {
1486 : 0 : return -ENOMEM;
1487 : : }
1488 : :
1489 : 23338 : sgroup = &group->sgroups[subsystem->id];
1490 : :
1491 : : /* Make sure the array of namespace information is the correct size */
1492 [ + + + + ]: 23338 : if (sgroup->num_ns == 0 && subsystem->max_nsid > 0) {
1493 : : /* First allocation */
1494 : 1015 : sgroup->ns_info = calloc(subsystem->max_nsid, sizeof(struct spdk_nvmf_subsystem_pg_ns_info));
1495 [ - + ]: 1015 : if (!sgroup->ns_info) {
1496 : 0 : return -ENOMEM;
1497 : : }
1498 : 1015 : sgroup->num_ns = subsystem->max_nsid;
1499 : : }
1500 : :
1501 : 23338 : ns_changed = false;
1502 : :
1503 : : /* Detect bdevs that were added or removed */
1504 [ + + ]: 326083 : for (i = 0; i < sgroup->num_ns; i++) {
1505 : 302745 : ns = subsystem->ns[i];
1506 : 302745 : ns_info = &sgroup->ns_info[i];
1507 : 302745 : ch = ns_info->channel;
1508 : :
1509 [ + + + + ]: 302745 : if (ns == NULL && ch == NULL) {
1510 : : /* Both NULL. Leave empty */
1511 [ + + + - ]: 25537 : } else if (ns == NULL && ch != NULL) {
1512 : : /* There was a channel here, but the namespace is gone. */
1513 : 7100 : ns_changed = true;
1514 : 7100 : spdk_put_io_channel(ch);
1515 : 7100 : ns_info->channel = NULL;
1516 [ + - + + ]: 18437 : } else if (ns != NULL && ch == NULL) {
1517 : : /* A namespace appeared but there is no channel yet */
1518 : 7935 : ns_changed = true;
1519 : 7935 : ch = spdk_bdev_get_io_channel(ns->desc);
1520 [ - + ]: 7935 : if (ch == NULL) {
1521 : 0 : SPDK_ERRLOG("Could not allocate I/O channel.\n");
1522 : 0 : return -ENOMEM;
1523 : : }
1524 : 7935 : ns_info->channel = ch;
1525 [ - + ]: 10502 : } else if (spdk_uuid_compare(&ns_info->uuid, spdk_bdev_get_uuid(ns->bdev)) != 0) {
1526 : : /* A namespace was here before, but was replaced by a new one. */
1527 : 0 : ns_changed = true;
1528 : 0 : spdk_put_io_channel(ns_info->channel);
1529 [ # # ]: 0 : memset(ns_info, 0, sizeof(*ns_info));
1530 : :
1531 : 0 : ch = spdk_bdev_get_io_channel(ns->desc);
1532 [ # # ]: 0 : if (ch == NULL) {
1533 : 0 : SPDK_ERRLOG("Could not allocate I/O channel.\n");
1534 : 0 : return -ENOMEM;
1535 : : }
1536 : 0 : ns_info->channel = ch;
1537 [ + + ]: 10502 : } else if (ns_info->num_blocks != spdk_bdev_get_num_blocks(ns->bdev)) {
1538 : : /* Namespace is still there but size has changed */
1539 [ - + - + ]: 345 : SPDK_DEBUGLOG(nvmf, "Namespace resized: subsystem_id %u,"
1540 : : " nsid %u, pg %p, old %" PRIu64 ", new %" PRIu64 "\n",
1541 : : subsystem->id,
1542 : : ns->nsid,
1543 : : group,
1544 : : ns_info->num_blocks,
1545 : : spdk_bdev_get_num_blocks(ns->bdev));
1546 : 345 : ns_changed = true;
1547 : : }
1548 : :
1549 [ + + ]: 302745 : if (ns == NULL) {
1550 [ - + ]: 284308 : memset(ns_info, 0, sizeof(*ns_info));
1551 : : } else {
1552 : 18437 : ns_info->uuid = *spdk_bdev_get_uuid(ns->bdev);
1553 : 18437 : ns_info->num_blocks = spdk_bdev_get_num_blocks(ns->bdev);
1554 : 18437 : ns_info->crkey = ns->crkey;
1555 : 18437 : ns_info->rtype = ns->rtype;
1556 [ - + ]: 18437 : if (ns->holder) {
1557 : 0 : ns_info->holder_id = ns->holder->hostid;
1558 : : }
1559 : :
1560 [ - + ]: 18437 : memset(&ns_info->reg_hostid, 0, SPDK_NVMF_MAX_NUM_REGISTRANTS * sizeof(struct spdk_uuid));
1561 : 18437 : j = 0;
1562 [ - + ]: 18437 : TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
1563 [ # # ]: 0 : if (j >= SPDK_NVMF_MAX_NUM_REGISTRANTS) {
1564 : 0 : SPDK_ERRLOG("Maximum %u registrants can support.\n", SPDK_NVMF_MAX_NUM_REGISTRANTS);
1565 : 0 : return -EINVAL;
1566 : : }
1567 : 0 : ns_info->reg_hostid[j++] = reg->hostid;
1568 : : }
1569 : : }
1570 : : }
1571 : :
1572 [ + + ]: 23338 : if (ns_changed) {
1573 [ + + ]: 26883 : TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
1574 [ + + ]: 11506 : if (ctrlr->thread != spdk_get_thread()) {
1575 : 7664 : continue;
1576 : : }
1577 : : /* It is possible that a ctrlr was added but the admin_qpair hasn't been
1578 : : * assigned yet.
1579 : : */
1580 [ + + ]: 3842 : if (!ctrlr->admin_qpair) {
1581 : 12 : continue;
1582 : : }
1583 [ + + ]: 3830 : if (ctrlr->admin_qpair->group == group) {
1584 : 3829 : nvmf_ctrlr_async_event_ns_notice(ctrlr);
1585 : 3829 : nvmf_ctrlr_async_event_ana_change_notice(ctrlr);
1586 : : }
1587 : : }
1588 : : }
1589 : :
1590 : 23338 : return 0;
1591 : : }
1592 : :
1593 : : int
1594 : 0 : nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group,
1595 : : struct spdk_nvmf_subsystem *subsystem)
1596 : : {
1597 : 0 : return poll_group_update_subsystem(group, subsystem);
1598 : : }
1599 : :
1600 : : int
1601 : 3589 : nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group,
1602 : : struct spdk_nvmf_subsystem *subsystem,
1603 : : spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
1604 : : {
1605 : 3589 : int rc = 0;
1606 : 3589 : struct spdk_nvmf_subsystem_poll_group *sgroup = &group->sgroups[subsystem->id];
1607 : : struct spdk_nvmf_request *req, *tmp;
1608 : : uint32_t i;
1609 : :
1610 [ - + ]: 3589 : if (!TAILQ_EMPTY(&sgroup->queued)) {
1611 : 0 : SPDK_ERRLOG("sgroup->queued not empty when adding subsystem\n");
1612 [ # # ]: 0 : TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
1613 [ # # ]: 0 : TAILQ_REMOVE(&sgroup->queued, req, link);
1614 [ # # ]: 0 : if (nvmf_transport_req_free(req)) {
1615 : 0 : SPDK_ERRLOG("Transport request free error!\n");
1616 : : }
1617 : : }
1618 : 0 : assert(false);
1619 : : }
1620 : :
1621 : 3589 : rc = poll_group_update_subsystem(group, subsystem);
1622 [ - + ]: 3589 : if (rc) {
1623 : 0 : nvmf_poll_group_remove_subsystem(group, subsystem, NULL, NULL);
1624 : 0 : goto fini;
1625 : : }
1626 : :
1627 : 3589 : sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
1628 : :
1629 [ + + ]: 33164 : for (i = 0; i < sgroup->num_ns; i++) {
1630 : 29575 : sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
1631 : : }
1632 : :
1633 : 3589 : fini:
1634 [ + + ]: 3589 : if (cb_fn) {
1635 : 2299 : cb_fn(cb_arg, rc);
1636 : : }
1637 : :
1638 : 876 : SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_add_subsystem, spdk_thread_get_id(group->thread),
1639 : : subsystem->subnqn);
1640 : :
1641 : 3589 : return rc;
1642 : : }
1643 : :
1644 : : static void
1645 : 2299 : _nvmf_poll_group_remove_subsystem_cb(void *ctx, int status)
1646 : : {
1647 : 2299 : struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
1648 : : struct spdk_nvmf_subsystem *subsystem;
1649 : : struct spdk_nvmf_poll_group *group;
1650 : : struct spdk_nvmf_subsystem_poll_group *sgroup;
1651 : 2299 : spdk_nvmf_poll_group_mod_done cpl_fn = NULL;
1652 : 2299 : void *cpl_ctx = NULL;
1653 : : uint32_t nsid;
1654 : :
1655 : 2299 : group = qpair_ctx->group;
1656 : 2299 : subsystem = qpair_ctx->subsystem;
1657 : 2299 : cpl_fn = qpair_ctx->cpl_fn;
1658 : 2299 : cpl_ctx = qpair_ctx->cpl_ctx;
1659 : 2299 : sgroup = &group->sgroups[subsystem->id];
1660 : :
1661 [ - + ]: 2299 : if (status) {
1662 : 0 : goto fini;
1663 : : }
1664 : :
1665 [ + + ]: 31871 : for (nsid = 0; nsid < sgroup->num_ns; nsid++) {
1666 [ + + ]: 29572 : if (sgroup->ns_info[nsid].channel) {
1667 : 832 : spdk_put_io_channel(sgroup->ns_info[nsid].channel);
1668 : 832 : sgroup->ns_info[nsid].channel = NULL;
1669 : : }
1670 : : }
1671 : :
1672 : 2299 : sgroup->num_ns = 0;
1673 : 2299 : free(sgroup->ns_info);
1674 : 2299 : sgroup->ns_info = NULL;
1675 : 2299 : fini:
1676 : 2299 : free(qpair_ctx);
1677 [ + - ]: 2299 : if (cpl_fn) {
1678 : 2299 : cpl_fn(cpl_ctx, status);
1679 : : }
1680 : 2299 : }
1681 : :
1682 : : static void nvmf_poll_group_remove_subsystem_msg(void *ctx);
1683 : :
1684 : : static void
1685 : 237048 : nvmf_poll_group_remove_subsystem_msg(void *ctx)
1686 : : {
1687 : : struct spdk_nvmf_qpair *qpair, *qpair_tmp;
1688 : : struct spdk_nvmf_subsystem *subsystem;
1689 : : struct spdk_nvmf_poll_group *group;
1690 : 237048 : struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
1691 : 237048 : bool qpairs_found = false;
1692 : 237048 : int rc = 0;
1693 : :
1694 : 237048 : group = qpair_ctx->group;
1695 : 237048 : subsystem = qpair_ctx->subsystem;
1696 : :
1697 [ + + ]: 1176180 : TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, qpair_tmp) {
1698 [ + - + + ]: 939132 : if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) {
1699 : 938576 : qpairs_found = true;
1700 : 938576 : rc = spdk_nvmf_qpair_disconnect(qpair);
1701 [ + + - + ]: 938576 : if (rc && rc != -EINPROGRESS) {
1702 : 0 : break;
1703 : : }
1704 : : }
1705 : : }
1706 : :
1707 [ + + ]: 237048 : if (!qpairs_found) {
1708 : 2299 : _nvmf_poll_group_remove_subsystem_cb(ctx, 0);
1709 : 2299 : return;
1710 : : }
1711 : :
1712 : : /* Some qpairs are in process of being disconnected. Send a message and try to remove them again */
1713 : 234749 : spdk_thread_send_msg(spdk_get_thread(), nvmf_poll_group_remove_subsystem_msg, ctx);
1714 : : }
1715 : :
1716 : : void
1717 : 2299 : nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group,
1718 : : struct spdk_nvmf_subsystem *subsystem,
1719 : : spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
1720 : : {
1721 : : struct spdk_nvmf_subsystem_poll_group *sgroup;
1722 : : struct nvmf_qpair_disconnect_many_ctx *ctx;
1723 : : uint32_t i;
1724 : :
1725 : 588 : SPDK_DTRACE_PROBE3_TICKS(nvmf_poll_group_remove_subsystem, group, spdk_thread_get_id(group->thread),
1726 : : subsystem->subnqn);
1727 : :
1728 : 2299 : ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx));
1729 [ - + ]: 2299 : if (!ctx) {
1730 : 0 : SPDK_ERRLOG("Unable to allocate memory for context to remove poll subsystem\n");
1731 [ # # ]: 0 : if (cb_fn) {
1732 : 0 : cb_fn(cb_arg, -1);
1733 : : }
1734 : 0 : return;
1735 : : }
1736 : :
1737 : 2299 : ctx->group = group;
1738 : 2299 : ctx->subsystem = subsystem;
1739 : 2299 : ctx->cpl_fn = cb_fn;
1740 : 2299 : ctx->cpl_ctx = cb_arg;
1741 : :
1742 : 2299 : sgroup = &group->sgroups[subsystem->id];
1743 : 2299 : sgroup->state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
1744 : :
1745 [ + + ]: 31871 : for (i = 0; i < sgroup->num_ns; i++) {
1746 : 29572 : sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
1747 : : }
1748 : :
1749 : 2299 : nvmf_poll_group_remove_subsystem_msg(ctx);
1750 : : }
1751 : :
1752 : : void
1753 : 19749 : nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group,
1754 : : struct spdk_nvmf_subsystem *subsystem,
1755 : : uint32_t nsid,
1756 : : spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
1757 : : {
1758 : : struct spdk_nvmf_subsystem_poll_group *sgroup;
1759 : 19749 : struct spdk_nvmf_subsystem_pg_ns_info *ns_info = NULL;
1760 : 19749 : int rc = 0;
1761 : : uint32_t i;
1762 : :
1763 [ - + ]: 19749 : if (subsystem->id >= group->num_sgroups) {
1764 : 0 : rc = -1;
1765 : 0 : goto fini;
1766 : : }
1767 : :
1768 : 19749 : sgroup = &group->sgroups[subsystem->id];
1769 [ - + ]: 19749 : if (sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSED) {
1770 : 0 : goto fini;
1771 : : }
1772 : 19749 : sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
1773 : :
1774 [ - + ]: 19749 : if (nsid == SPDK_NVME_GLOBAL_NS_TAG) {
1775 [ # # ]: 0 : for (i = 0; i < sgroup->num_ns; i++) {
1776 : 0 : ns_info = &sgroup->ns_info[i];
1777 : 0 : ns_info->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
1778 : : }
1779 : : } else {
1780 : : /* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */
1781 [ + + ]: 19749 : if (nsid - 1 < sgroup->num_ns) {
1782 : 9370 : ns_info = &sgroup->ns_info[nsid - 1];
1783 : 9370 : ns_info->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
1784 : : }
1785 : : }
1786 : :
1787 [ + + ]: 19749 : if (sgroup->mgmt_io_outstanding > 0) {
1788 [ - + ]: 4 : assert(sgroup->cb_fn == NULL);
1789 : 4 : sgroup->cb_fn = cb_fn;
1790 [ - + ]: 4 : assert(sgroup->cb_arg == NULL);
1791 : 4 : sgroup->cb_arg = cb_arg;
1792 : 4 : return;
1793 : : }
1794 : :
1795 [ - + ]: 19745 : if (nsid == SPDK_NVME_GLOBAL_NS_TAG) {
1796 [ # # ]: 0 : for (i = 0; i < sgroup->num_ns; i++) {
1797 : 0 : ns_info = &sgroup->ns_info[i];
1798 : :
1799 [ # # ]: 0 : if (ns_info->io_outstanding > 0) {
1800 [ # # ]: 0 : assert(sgroup->cb_fn == NULL);
1801 : 0 : sgroup->cb_fn = cb_fn;
1802 [ # # ]: 0 : assert(sgroup->cb_arg == NULL);
1803 : 0 : sgroup->cb_arg = cb_arg;
1804 : 0 : return;
1805 : : }
1806 : : }
1807 : : } else {
1808 [ + + + + ]: 19745 : if (ns_info != NULL && ns_info->io_outstanding > 0) {
1809 [ - + ]: 1237 : assert(sgroup->cb_fn == NULL);
1810 : 1237 : sgroup->cb_fn = cb_fn;
1811 [ - + ]: 1237 : assert(sgroup->cb_arg == NULL);
1812 : 1237 : sgroup->cb_arg = cb_arg;
1813 : 1237 : return;
1814 : : }
1815 : : }
1816 : :
1817 [ - + ]: 18508 : assert(sgroup->mgmt_io_outstanding == 0);
1818 : 18508 : sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED;
1819 : 18508 : fini:
1820 [ + - ]: 18508 : if (cb_fn) {
1821 : 18508 : cb_fn(cb_arg, rc);
1822 : : }
1823 : : }
1824 : :
1825 : : void
1826 : 19749 : nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group,
1827 : : struct spdk_nvmf_subsystem *subsystem,
1828 : : spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
1829 : : {
1830 : : struct spdk_nvmf_request *req, *tmp;
1831 : : struct spdk_nvmf_subsystem_poll_group *sgroup;
1832 : 19749 : int rc = 0;
1833 : : uint32_t i;
1834 : :
1835 [ - + ]: 19749 : if (subsystem->id >= group->num_sgroups) {
1836 : 0 : rc = -1;
1837 : 0 : goto fini;
1838 : : }
1839 : :
1840 : 19749 : sgroup = &group->sgroups[subsystem->id];
1841 : :
1842 [ - + ]: 19749 : if (sgroup->state == SPDK_NVMF_SUBSYSTEM_ACTIVE) {
1843 : 0 : goto fini;
1844 : : }
1845 : :
1846 : 19749 : rc = poll_group_update_subsystem(group, subsystem);
1847 [ - + ]: 19749 : if (rc) {
1848 : 0 : goto fini;
1849 : : }
1850 : :
1851 [ + + ]: 292919 : for (i = 0; i < sgroup->num_ns; i++) {
1852 : 273170 : sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
1853 : : }
1854 : :
1855 : 19749 : sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
1856 : :
1857 : : /* Release all queued requests */
1858 [ + + ]: 158968 : TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
1859 [ + + ]: 139219 : TAILQ_REMOVE(&sgroup->queued, req, link);
1860 [ + + ]: 139219 : if (spdk_nvmf_request_using_zcopy(req)) {
1861 : 134874 : spdk_nvmf_request_zcopy_start(req);
1862 : : } else {
1863 : 4345 : spdk_nvmf_request_exec(req);
1864 : : }
1865 : :
1866 : : }
1867 : 19749 : fini:
1868 [ + - ]: 19749 : if (cb_fn) {
1869 : 19749 : cb_fn(cb_arg, rc);
1870 : : }
1871 : 19749 : }
1872 : :
1873 : :
1874 : : struct spdk_nvmf_poll_group *
1875 : 7640 : spdk_nvmf_get_optimal_poll_group(struct spdk_nvmf_qpair *qpair)
1876 : : {
1877 : : struct spdk_nvmf_transport_poll_group *tgroup;
1878 : :
1879 : 7640 : tgroup = nvmf_transport_get_optimal_poll_group(qpair->transport, qpair);
1880 : :
1881 [ - + ]: 7640 : if (tgroup == NULL) {
1882 : 0 : return NULL;
1883 : : }
1884 : :
1885 : 7640 : return tgroup->group;
1886 : : }
1887 : :
1888 : : void
1889 : 44 : spdk_nvmf_poll_group_dump_stat(struct spdk_nvmf_poll_group *group, struct spdk_json_write_ctx *w)
1890 : : {
1891 : : struct spdk_nvmf_transport_poll_group *tgroup;
1892 : :
1893 : 44 : spdk_json_write_object_begin(w);
1894 : :
1895 : 44 : spdk_json_write_named_string(w, "name", spdk_thread_get_name(spdk_get_thread()));
1896 : 44 : spdk_json_write_named_uint32(w, "admin_qpairs", group->stat.admin_qpairs);
1897 : 44 : spdk_json_write_named_uint32(w, "io_qpairs", group->stat.io_qpairs);
1898 : 44 : spdk_json_write_named_uint32(w, "current_admin_qpairs", group->stat.current_admin_qpairs);
1899 : 44 : spdk_json_write_named_uint32(w, "current_io_qpairs", group->stat.current_io_qpairs);
1900 : 44 : spdk_json_write_named_uint64(w, "pending_bdev_io", group->stat.pending_bdev_io);
1901 : 44 : spdk_json_write_named_uint64(w, "completed_nvme_io", group->stat.completed_nvme_io);
1902 : :
1903 : 44 : spdk_json_write_named_array_begin(w, "transports");
1904 : :
1905 [ + + ]: 76 : TAILQ_FOREACH(tgroup, &group->tgroups, link) {
1906 : 32 : spdk_json_write_object_begin(w);
1907 : : /*
1908 : : * The trtype field intentionally contains a transport name as this is more informative.
1909 : : * The field has not been renamed for backward compatibility.
1910 : : */
1911 : 32 : spdk_json_write_named_string(w, "trtype", spdk_nvmf_get_transport_name(tgroup->transport));
1912 : :
1913 [ + + ]: 32 : if (tgroup->transport->ops->poll_group_dump_stat) {
1914 : 8 : tgroup->transport->ops->poll_group_dump_stat(tgroup, w);
1915 : : }
1916 : :
1917 : 32 : spdk_json_write_object_end(w);
1918 : : }
1919 : :
1920 : 44 : spdk_json_write_array_end(w);
1921 : 44 : spdk_json_write_object_end(w);
1922 : 44 : }
|