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