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