xref: /spdk/lib/iscsi/tgt_node.c (revision b45788036fd43c6d9ca73f047d53f8592fe44909)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "spdk/stdinc.h"
36 
37 #include "spdk/sock.h"
38 #include "spdk/scsi.h"
39 
40 #include "spdk/log.h"
41 
42 #include "iscsi/iscsi.h"
43 #include "iscsi/conn.h"
44 #include "iscsi/tgt_node.h"
45 #include "iscsi/portal_grp.h"
46 #include "iscsi/init_grp.h"
47 #include "iscsi/task.h"
48 
49 #define MAX_TMPBUF 4096
50 #define MAX_MASKBUF 128
51 
52 static bool
53 iscsi_ipv6_netmask_allow_addr(const char *netmask, const char *addr)
54 {
55 	struct in6_addr in6_mask;
56 	struct in6_addr in6_addr;
57 	char mask[MAX_MASKBUF];
58 	const char *p;
59 	size_t n;
60 	int bits, bmask;
61 	int i;
62 
63 	if (netmask[0] != '[') {
64 		return false;
65 	}
66 	p = strchr(netmask, ']');
67 	if (p == NULL) {
68 		return false;
69 	}
70 	n = p - (netmask + 1);
71 	if (n + 1 > sizeof mask) {
72 		return false;
73 	}
74 
75 	memcpy(mask, netmask + 1, n);
76 	mask[n] = '\0';
77 	p++;
78 
79 	if (p[0] == '/') {
80 		bits = (int) strtol(p + 1, NULL, 10);
81 		if (bits <= 0 || bits > 128) {
82 			return false;
83 		}
84 	} else {
85 		bits = 128;
86 	}
87 
88 #if 0
89 	SPDK_DEBUGLOG(iscsi, "input %s\n", addr);
90 	SPDK_DEBUGLOG(iscsi, "mask  %s / %d\n", mask, bits);
91 #endif
92 
93 	/* presentation to network order binary */
94 	if (inet_pton(AF_INET6, mask, &in6_mask) <= 0
95 	    || inet_pton(AF_INET6, addr, &in6_addr) <= 0) {
96 		return false;
97 	}
98 
99 	/* check 128bits */
100 	for (i = 0; i < (bits / 8); i++) {
101 		if (in6_mask.s6_addr[i] != in6_addr.s6_addr[i]) {
102 			return false;
103 		}
104 	}
105 	if (bits % 8) {
106 		bmask = (0xffU << (8 - (bits % 8))) & 0xffU;
107 		if ((in6_mask.s6_addr[i] & bmask) != (in6_addr.s6_addr[i] & bmask)) {
108 			return false;
109 		}
110 	}
111 
112 	/* match */
113 	return true;
114 }
115 
116 static bool
117 iscsi_ipv4_netmask_allow_addr(const char *netmask, const char *addr)
118 {
119 	struct in_addr in4_mask;
120 	struct in_addr in4_addr;
121 	char mask[MAX_MASKBUF];
122 	const char *p;
123 	uint32_t bmask;
124 	size_t n;
125 	int bits;
126 
127 	p = strchr(netmask, '/');
128 	if (p == NULL) {
129 		p = netmask + strlen(netmask);
130 	}
131 	n = p - netmask;
132 	if (n + 1 > sizeof mask) {
133 		return false;
134 	}
135 
136 	memcpy(mask, netmask, n);
137 	mask[n] = '\0';
138 
139 	if (p[0] == '/') {
140 		bits = (int) strtol(p + 1, NULL, 10);
141 		if (bits <= 0 || bits > 32) {
142 			return false;
143 		}
144 	} else {
145 		bits = 32;
146 	}
147 
148 	/* presentation to network order binary */
149 	if (inet_pton(AF_INET, mask, &in4_mask) <= 0
150 	    || inet_pton(AF_INET, addr, &in4_addr) <= 0) {
151 		return false;
152 	}
153 
154 	/* check 32bits */
155 	bmask = (0xffffffffU << (32 - bits)) & 0xffffffffU;
156 	if ((ntohl(in4_mask.s_addr) & bmask) != (ntohl(in4_addr.s_addr) & bmask)) {
157 		return false;
158 	}
159 
160 	/* match */
161 	return true;
162 }
163 
164 static bool
165 iscsi_netmask_allow_addr(const char *netmask, const char *addr)
166 {
167 	if (netmask == NULL || addr == NULL) {
168 		return false;
169 	}
170 	if (strcasecmp(netmask, "ANY") == 0) {
171 		return true;
172 	}
173 	if (netmask[0] == '[') {
174 		/* IPv6 */
175 		if (iscsi_ipv6_netmask_allow_addr(netmask, addr)) {
176 			return true;
177 		}
178 	} else {
179 		/* IPv4 */
180 		if (iscsi_ipv4_netmask_allow_addr(netmask, addr)) {
181 			return true;
182 		}
183 	}
184 	return false;
185 }
186 
187 static bool
188 iscsi_init_grp_allow_addr(struct spdk_iscsi_init_grp *igp,
189 			  const char *addr)
190 {
191 	struct spdk_iscsi_initiator_netmask *imask;
192 
193 	TAILQ_FOREACH(imask, &igp->netmask_head, tailq) {
194 		SPDK_DEBUGLOG(iscsi, "netmask=%s, addr=%s\n",
195 			      imask->mask, addr);
196 		if (iscsi_netmask_allow_addr(imask->mask, addr)) {
197 			return true;
198 		}
199 	}
200 	return false;
201 }
202 
203 static int
204 iscsi_init_grp_allow_iscsi_name(struct spdk_iscsi_init_grp *igp,
205 				const char *iqn, bool *result)
206 {
207 	struct spdk_iscsi_initiator_name *iname;
208 
209 	TAILQ_FOREACH(iname, &igp->initiator_head, tailq) {
210 		/* denied if iqn is matched */
211 		if ((iname->name[0] == '!')
212 		    && (strcasecmp(&iname->name[1], "ANY") == 0
213 			|| strcasecmp(&iname->name[1], iqn) == 0)) {
214 			*result = false;
215 			return 0;
216 		}
217 		/* allowed if iqn is matched */
218 		if (strcasecmp(iname->name, "ANY") == 0
219 		    || strcasecmp(iname->name, iqn) == 0) {
220 			*result = true;
221 			return 0;
222 		}
223 	}
224 	return -1;
225 }
226 
227 static struct spdk_iscsi_pg_map *
228 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
229 			   struct spdk_iscsi_portal_grp *pg);
230 
231 bool
232 iscsi_tgt_node_access(struct spdk_iscsi_conn *conn,
233 		      struct spdk_iscsi_tgt_node *target, const char *iqn, const char *addr)
234 {
235 	struct spdk_iscsi_portal_grp *pg;
236 	struct spdk_iscsi_pg_map *pg_map;
237 	struct spdk_iscsi_ig_map *ig_map;
238 	int rc;
239 	bool allowed = false;
240 
241 	if (conn == NULL || target == NULL || iqn == NULL || addr == NULL) {
242 		return false;
243 	}
244 	pg = conn->portal->group;
245 
246 	SPDK_DEBUGLOG(iscsi, "pg=%d, iqn=%s, addr=%s\n",
247 		      pg->tag, iqn, addr);
248 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
249 	if (pg_map == NULL) {
250 		return false;
251 	}
252 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
253 		rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &allowed);
254 		if (rc == 0) {
255 			if (allowed == false) {
256 				goto denied;
257 			} else {
258 				if (iscsi_init_grp_allow_addr(ig_map->ig, addr)) {
259 					return true;
260 				}
261 			}
262 		} else {
263 			/* netmask is denied in this initiator group */
264 		}
265 	}
266 
267 denied:
268 	SPDK_DEBUGLOG(iscsi, "access denied from %s (%s) to %s (%s:%s,%d)\n",
269 		      iqn, addr, target->name, conn->portal_host,
270 		      conn->portal_port, conn->pg_tag);
271 	return false;
272 }
273 
274 static bool
275 iscsi_tgt_node_allow_iscsi_name(struct spdk_iscsi_tgt_node *target, const char *iqn)
276 {
277 	struct spdk_iscsi_pg_map *pg_map;
278 	struct spdk_iscsi_ig_map *ig_map;
279 	int rc;
280 	bool result = false;
281 
282 	if (target == NULL || iqn == NULL) {
283 		return false;
284 	}
285 
286 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
287 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
288 			rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &result);
289 			if (rc == 0) {
290 				return result;
291 			}
292 		}
293 	}
294 
295 	return false;
296 }
297 
298 static int
299 iscsi_send_tgt_portals(struct spdk_iscsi_conn *conn,
300 		       struct spdk_iscsi_tgt_node *target,
301 		       uint8_t *data, int alloc_len, int total)
302 {
303 	char buf[MAX_TMPBUF];
304 	struct spdk_iscsi_portal_grp *pg;
305 	struct spdk_iscsi_pg_map *pg_map;
306 	struct spdk_iscsi_portal *p;
307 	char *host;
308 	int len;
309 
310 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
311 		pg = pg_map->pg;
312 
313 		if (pg->is_private) {
314 			/* Skip the private portal group. Portals in the private portal group
315 			 * will be returned only by temporary login redirection responses.
316 			 */
317 			continue;
318 		}
319 
320 		TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
321 			if (alloc_len - total < 1) {
322 				/* TODO: long text responses support */
323 				SPDK_ERRLOG("SPDK doesn't support long text responses now, "
324 					    "you can use larger MaxRecvDataSegmentLength"
325 					    "value in initiator\n");
326 				return alloc_len;
327 			}
328 			host = p->host;
329 			/* wildcard? */
330 			if (strcasecmp(host, "[::]") == 0 || strcasecmp(host, "0.0.0.0") == 0) {
331 				if (spdk_sock_is_ipv6(conn->sock)) {
332 					snprintf(buf, sizeof buf, "[%s]", conn->target_addr);
333 					host = buf;
334 				} else if (spdk_sock_is_ipv4(conn->sock)) {
335 					snprintf(buf, sizeof buf, "%s", conn->target_addr);
336 					host = buf;
337 				} else {
338 					/* skip portal for the family */
339 					continue;
340 				}
341 			}
342 			SPDK_DEBUGLOG(iscsi, "TargetAddress=%s:%s,%d\n",
343 				      host, p->port, pg->tag);
344 			len = snprintf((char *)data + total, alloc_len - total,
345 				       "TargetAddress=%s:%s,%d", host, p->port, pg->tag);
346 			total += len + 1;
347 		}
348 	}
349 
350 	return total;
351 }
352 
353 int
354 iscsi_send_tgts(struct spdk_iscsi_conn *conn, const char *iiqn,
355 		const char *tiqn, uint8_t *data, int alloc_len, int data_len)
356 {
357 	struct spdk_iscsi_tgt_node *target;
358 	int total;
359 	int len;
360 	int rc;
361 
362 	if (conn == NULL) {
363 		return 0;
364 	}
365 
366 	total = data_len;
367 	if (alloc_len < 1) {
368 		return 0;
369 	}
370 	if (total >= alloc_len) {
371 		total = alloc_len;
372 		data[total - 1] = '\0';
373 		return total;
374 	}
375 
376 	pthread_mutex_lock(&g_iscsi.mutex);
377 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
378 		if (strcasecmp(tiqn, "ALL") != 0
379 		    && strcasecmp(tiqn, target->name) != 0) {
380 			continue;
381 		}
382 		rc = iscsi_tgt_node_allow_iscsi_name(target, iiqn);
383 		if (rc == 0) {
384 			continue;
385 		}
386 
387 		len = snprintf((char *)data + total, alloc_len - total, "TargetName=%s",
388 			       target->name);
389 		total += len + 1;
390 
391 		total = iscsi_send_tgt_portals(conn, target, data, alloc_len, total);
392 		if (alloc_len - total < 1) {
393 			break;
394 		}
395 	}
396 	pthread_mutex_unlock(&g_iscsi.mutex);
397 
398 	return total;
399 }
400 
401 struct spdk_iscsi_tgt_node *
402 iscsi_find_tgt_node(const char *target_name)
403 {
404 	struct spdk_iscsi_tgt_node *target;
405 
406 	if (target_name == NULL) {
407 		return NULL;
408 	}
409 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
410 		if (strcasecmp(target_name, target->name) == 0) {
411 			return target;
412 		}
413 	}
414 	return NULL;
415 }
416 
417 static int
418 iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target)
419 {
420 	pthread_mutex_lock(&g_iscsi.mutex);
421 
422 	if (iscsi_find_tgt_node(target->name) != NULL) {
423 		pthread_mutex_unlock(&g_iscsi.mutex);
424 		return -EEXIST;
425 	}
426 
427 	TAILQ_INSERT_TAIL(&g_iscsi.target_head, target, tailq);
428 
429 	pthread_mutex_unlock(&g_iscsi.mutex);
430 	return 0;
431 }
432 
433 static int
434 iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target)
435 {
436 	struct spdk_iscsi_tgt_node *t;
437 
438 	TAILQ_FOREACH(t, &g_iscsi.target_head, tailq) {
439 		if (t == target) {
440 			TAILQ_REMOVE(&g_iscsi.target_head, t, tailq);
441 			return 0;
442 		}
443 	}
444 
445 	return -1;
446 }
447 
448 static struct spdk_iscsi_ig_map *
449 iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map,
450 			 struct spdk_iscsi_init_grp *ig)
451 {
452 	struct spdk_iscsi_ig_map *ig_map;
453 
454 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
455 		if (ig_map->ig == ig) {
456 			return ig_map;
457 		}
458 	}
459 
460 	return NULL;
461 }
462 
463 static struct spdk_iscsi_ig_map *
464 iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map,
465 			struct spdk_iscsi_init_grp *ig)
466 {
467 	struct spdk_iscsi_ig_map *ig_map;
468 
469 	if (iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) {
470 		return NULL;
471 	}
472 
473 	ig_map = malloc(sizeof(*ig_map));
474 	if (ig_map == NULL) {
475 		return NULL;
476 	}
477 
478 	ig_map->ig = ig;
479 	ig->ref++;
480 	pg_map->num_ig_maps++;
481 	TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq);
482 
483 	return ig_map;
484 }
485 
486 static void
487 _iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
488 			    struct spdk_iscsi_ig_map *ig_map)
489 {
490 	TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq);
491 	pg_map->num_ig_maps--;
492 	ig_map->ig->ref--;
493 	free(ig_map);
494 }
495 
496 static int
497 iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
498 			   struct spdk_iscsi_init_grp *ig)
499 {
500 	struct spdk_iscsi_ig_map *ig_map;
501 
502 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
503 	if (ig_map == NULL) {
504 		return -ENOENT;
505 	}
506 
507 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
508 	return 0;
509 }
510 
511 static void
512 iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map)
513 {
514 	struct spdk_iscsi_ig_map *ig_map, *tmp;
515 
516 	TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) {
517 		_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
518 	}
519 }
520 
521 static struct spdk_iscsi_pg_map *
522 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
523 			   struct spdk_iscsi_portal_grp *pg)
524 {
525 	struct spdk_iscsi_pg_map *pg_map;
526 
527 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
528 		if (pg_map->pg == pg) {
529 			return pg_map;
530 		}
531 	}
532 
533 	return NULL;
534 }
535 
536 static struct spdk_iscsi_pg_map *
537 iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target,
538 			  struct spdk_iscsi_portal_grp *pg)
539 {
540 	struct spdk_iscsi_pg_map *pg_map;
541 	char port_name[MAX_TMPBUF];
542 	int rc;
543 
544 	if (iscsi_tgt_node_find_pg_map(target, pg) != NULL) {
545 		return NULL;
546 	}
547 
548 	if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) {
549 		SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n",
550 			    SPDK_SCSI_DEV_MAX_PORTS);
551 		return NULL;
552 	}
553 
554 	pg_map = calloc(1, sizeof(*pg_map));
555 	if (pg_map == NULL) {
556 		return NULL;
557 	}
558 
559 	snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x",
560 		 spdk_scsi_dev_get_name(target->dev), pg->tag);
561 	rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name);
562 	if (rc != 0) {
563 		free(pg_map);
564 		return NULL;
565 	}
566 
567 	TAILQ_INIT(&pg_map->ig_map_head);
568 	pg_map->num_ig_maps = 0;
569 	pg->ref++;
570 	pg_map->pg = pg;
571 	target->num_pg_maps++;
572 	TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq);
573 
574 	return pg_map;
575 }
576 
577 static void
578 _iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
579 			      struct spdk_iscsi_pg_map *pg_map)
580 {
581 	TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq);
582 	target->num_pg_maps--;
583 	pg_map->pg->ref--;
584 
585 	spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag);
586 
587 	free(pg_map);
588 }
589 
590 static int
591 iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
592 			     struct spdk_iscsi_portal_grp *pg)
593 {
594 	struct spdk_iscsi_pg_map *pg_map;
595 
596 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
597 	if (pg_map == NULL) {
598 		return -ENOENT;
599 	}
600 
601 	if (pg_map->num_ig_maps > 0) {
602 		SPDK_DEBUGLOG(iscsi, "delete %d ig_maps forcefully\n",
603 			      pg_map->num_ig_maps);
604 	}
605 
606 	iscsi_pg_map_delete_all_ig_maps(pg_map);
607 	_iscsi_tgt_node_delete_pg_map(target, pg_map);
608 	return 0;
609 }
610 
611 static void
612 iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target,
613 			      struct spdk_iscsi_init_grp *ig)
614 {
615 	struct spdk_iscsi_pg_map *pg_map, *tmp;
616 
617 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
618 		iscsi_pg_map_delete_ig_map(pg_map, ig);
619 		if (pg_map->num_ig_maps == 0) {
620 			_iscsi_tgt_node_delete_pg_map(target, pg_map);
621 		}
622 	}
623 }
624 
625 static void
626 iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target)
627 {
628 	struct spdk_iscsi_pg_map *pg_map, *tmp;
629 
630 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
631 		iscsi_pg_map_delete_all_ig_maps(pg_map);
632 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
633 	}
634 }
635 
636 static void
637 _iscsi_tgt_node_destruct(void *cb_arg, int rc)
638 {
639 	struct spdk_iscsi_tgt_node *target = cb_arg;
640 	iscsi_tgt_node_destruct_cb destruct_cb_fn = target->destruct_cb_fn;
641 	void *destruct_cb_arg = target->destruct_cb_arg;
642 
643 	if (rc != 0) {
644 		if (destruct_cb_fn) {
645 			destruct_cb_fn(destruct_cb_arg, rc);
646 		}
647 		return;
648 	}
649 
650 	pthread_mutex_lock(&g_iscsi.mutex);
651 	iscsi_tgt_node_delete_all_pg_maps(target);
652 	pthread_mutex_unlock(&g_iscsi.mutex);
653 
654 	pthread_mutex_destroy(&target->mutex);
655 	free(target);
656 
657 	if (destruct_cb_fn) {
658 		destruct_cb_fn(destruct_cb_arg, 0);
659 	}
660 }
661 
662 static int
663 iscsi_tgt_node_check_active_conns(void *arg)
664 {
665 	struct spdk_iscsi_tgt_node *target = arg;
666 
667 	if (iscsi_get_active_conns(target) != 0) {
668 		return SPDK_POLLER_BUSY;
669 	}
670 
671 	spdk_poller_unregister(&target->destruct_poller);
672 
673 	spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
674 
675 	return SPDK_POLLER_BUSY;
676 }
677 
678 static void
679 iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target,
680 			iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
681 {
682 	if (target == NULL) {
683 		if (cb_fn) {
684 			cb_fn(cb_arg, -ENOENT);
685 		}
686 		return;
687 	}
688 
689 	if (target->destructed) {
690 		SPDK_ERRLOG("Destructing %s is already started\n", target->name);
691 		if (cb_fn) {
692 			cb_fn(cb_arg, -EBUSY);
693 		}
694 		return;
695 	}
696 
697 	target->destructed = true;
698 	target->destruct_cb_fn = cb_fn;
699 	target->destruct_cb_arg = cb_arg;
700 
701 	iscsi_conns_request_logout(target, -1);
702 
703 	if (iscsi_get_active_conns(target) != 0) {
704 		target->destruct_poller = SPDK_POLLER_REGISTER(iscsi_tgt_node_check_active_conns,
705 					  target, 10);
706 	} else {
707 		spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
708 	}
709 
710 }
711 
712 static int
713 iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target,
714 				int pg_tag, int ig_tag)
715 {
716 	struct spdk_iscsi_portal_grp	*pg;
717 	struct spdk_iscsi_init_grp	*ig;
718 	struct spdk_iscsi_pg_map	*pg_map;
719 	struct spdk_iscsi_ig_map	*ig_map;
720 
721 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
722 	if (pg == NULL) {
723 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
724 		return -ENOENT;
725 	}
726 	ig = iscsi_init_grp_find_by_tag(ig_tag);
727 	if (ig == NULL) {
728 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
729 		return -ENOENT;
730 	}
731 
732 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
733 	if (pg_map == NULL) {
734 		SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag);
735 		return -ENOENT;
736 	}
737 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
738 	if (ig_map == NULL) {
739 		SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag);
740 		return -ENOENT;
741 	}
742 
743 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
744 	if (pg_map->num_ig_maps == 0) {
745 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
746 	}
747 
748 	return 0;
749 }
750 
751 static int
752 iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target,
753 			     int pg_tag, int ig_tag)
754 {
755 	struct spdk_iscsi_portal_grp	*pg;
756 	struct spdk_iscsi_pg_map	*pg_map;
757 	struct spdk_iscsi_init_grp	*ig;
758 	struct spdk_iscsi_ig_map	*ig_map;
759 	bool				new_pg_map = false;
760 
761 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
762 	if (pg == NULL) {
763 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
764 		return -ENOENT;
765 	}
766 	ig = iscsi_init_grp_find_by_tag(ig_tag);
767 	if (ig == NULL) {
768 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
769 		return -ENOENT;
770 	}
771 
772 	/* get existing pg_map or create new pg_map and add it to target */
773 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
774 	if (pg_map == NULL) {
775 		pg_map = iscsi_tgt_node_add_pg_map(target, pg);
776 		if (pg_map == NULL) {
777 			goto failed;
778 		}
779 		new_pg_map = true;
780 	}
781 
782 	/* create new ig_map and add it to pg_map */
783 	ig_map = iscsi_pg_map_add_ig_map(pg_map, ig);
784 	if (ig_map == NULL) {
785 		goto failed;
786 	}
787 
788 	return 0;
789 
790 failed:
791 	if (new_pg_map) {
792 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
793 	}
794 
795 	return -1;
796 }
797 
798 int
799 iscsi_target_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
800 				 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
801 {
802 	uint16_t i;
803 	int rc;
804 
805 	pthread_mutex_lock(&g_iscsi.mutex);
806 	for (i = 0; i < num_maps; i++) {
807 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i],
808 						  ig_tag_list[i]);
809 		if (rc != 0) {
810 			SPDK_ERRLOG("could not add map to target\n");
811 			goto invalid;
812 		}
813 	}
814 	pthread_mutex_unlock(&g_iscsi.mutex);
815 	return 0;
816 
817 invalid:
818 	for (; i > 0; --i) {
819 		iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1],
820 						ig_tag_list[i - 1]);
821 	}
822 	pthread_mutex_unlock(&g_iscsi.mutex);
823 	return -1;
824 }
825 
826 int
827 iscsi_target_node_remove_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
828 				    int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
829 {
830 	uint16_t i;
831 	int rc;
832 
833 	pthread_mutex_lock(&g_iscsi.mutex);
834 	for (i = 0; i < num_maps; i++) {
835 		rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i],
836 						     ig_tag_list[i]);
837 		if (rc != 0) {
838 			SPDK_ERRLOG("could not delete map from target\n");
839 			goto invalid;
840 		}
841 	}
842 	pthread_mutex_unlock(&g_iscsi.mutex);
843 	return 0;
844 
845 invalid:
846 	for (; i > 0; --i) {
847 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1],
848 						  ig_tag_list[i - 1]);
849 		if (rc != 0) {
850 			iscsi_tgt_node_delete_all_pg_maps(target);
851 			break;
852 		}
853 	}
854 	pthread_mutex_unlock(&g_iscsi.mutex);
855 	return -1;
856 }
857 
858 int
859 iscsi_tgt_node_redirect(struct spdk_iscsi_tgt_node *target, int pg_tag,
860 			const char *host, const char *port)
861 {
862 	struct spdk_iscsi_portal_grp *pg;
863 	struct spdk_iscsi_pg_map *pg_map;
864 	struct sockaddr_storage sa;
865 
866 	if (target == NULL) {
867 		return -EINVAL;
868 	}
869 
870 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
871 	if (pg == NULL) {
872 		SPDK_ERRLOG("Portal group %d is not found.\n", pg_tag);
873 		return -EINVAL;
874 	}
875 
876 	if (pg->is_private) {
877 		SPDK_ERRLOG("Portal group %d is not public portal group.\n", pg_tag);
878 		return -EINVAL;
879 	}
880 
881 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
882 	if (pg_map == NULL) {
883 		SPDK_ERRLOG("Portal group %d is not mapped.\n", pg_tag);
884 		return -EINVAL;
885 	}
886 
887 	if (host == NULL && port == NULL) {
888 		/* Clear redirect setting. */
889 		memset(pg_map->redirect_host, 0, MAX_PORTAL_ADDR + 1);
890 		memset(pg_map->redirect_port, 0, MAX_PORTAL_PORT + 1);
891 	} else {
892 		if (iscsi_parse_redirect_addr(&sa, host, port) != 0) {
893 			SPDK_ERRLOG("IP address-port pair is not valid.\n");
894 			return -EINVAL;
895 		}
896 
897 		if (iscsi_portal_grp_find_portal_by_addr(pg, port, host) != NULL) {
898 			SPDK_ERRLOG("IP address-port pair must be chosen from a "
899 				    "different private portal group\n");
900 			return -EINVAL;
901 		}
902 
903 		snprintf(pg_map->redirect_host, MAX_PORTAL_ADDR + 1, "%s", host);
904 		snprintf(pg_map->redirect_port, MAX_PORTAL_PORT + 1, "%s", port);
905 	}
906 
907 	return 0;
908 }
909 
910 bool
911 iscsi_tgt_node_is_redirected(struct spdk_iscsi_conn *conn,
912 			     struct spdk_iscsi_tgt_node *target,
913 			     char *buf, int buf_len)
914 {
915 	struct spdk_iscsi_pg_map *pg_map;
916 
917 	if (conn == NULL || target == NULL || buf == NULL || buf_len == 0) {
918 		return false;
919 	}
920 
921 	pg_map = iscsi_tgt_node_find_pg_map(target, conn->portal->group);
922 	if (pg_map == NULL) {
923 		return false;
924 	}
925 
926 	if (pg_map->redirect_host[0] == '\0' || pg_map->redirect_port[0] == '\0') {
927 		return false;
928 	}
929 
930 	snprintf(buf, buf_len, "%s:%s", pg_map->redirect_host, pg_map->redirect_port);
931 
932 	return true;
933 }
934 
935 static int
936 check_iscsi_name(const char *name)
937 {
938 	const unsigned char *up = (const unsigned char *) name;
939 	size_t n;
940 
941 	/* valid iSCSI name no larger than 223 bytes */
942 	if (strlen(name) > MAX_TARGET_NAME) {
943 		return -1;
944 	}
945 
946 	/* valid iSCSI name? */
947 	for (n = 0; up[n] != 0; n++) {
948 		if (up[n] > 0x00U && up[n] <= 0x2cU) {
949 			return -1;
950 		}
951 		if (up[n] == 0x2fU) {
952 			return -1;
953 		}
954 		if (up[n] >= 0x3bU && up[n] <= 0x40U) {
955 			return -1;
956 		}
957 		if (up[n] >= 0x5bU && up[n] <= 0x60U) {
958 			return -1;
959 		}
960 		if (up[n] >= 0x7bU && up[n] <= 0x7fU) {
961 			return -1;
962 		}
963 		if (isspace(up[n])) {
964 			return -1;
965 		}
966 	}
967 	/* valid format? */
968 	if (strncasecmp(name, "iqn.", 4) == 0) {
969 		/* iqn.YYYY-MM.reversed.domain.name */
970 		if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
971 		    || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
972 		    || !isdigit(up[10]) || up[11] != '.') {
973 			SPDK_ERRLOG("invalid iqn format. "
974 				    "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
975 			return -1;
976 		}
977 	} else if (strncasecmp(name, "eui.", 4) == 0) {
978 		/* EUI-64 -> 16bytes */
979 		/* XXX */
980 	} else if (strncasecmp(name, "naa.", 4) == 0) {
981 		/* 64bit -> 16bytes, 128bit -> 32bytes */
982 		/* XXX */
983 	}
984 	/* OK */
985 	return 0;
986 }
987 
988 bool
989 iscsi_check_chap_params(bool disable, bool require, bool mutual, int group)
990 {
991 	if (group < 0) {
992 		SPDK_ERRLOG("Invalid auth group ID (%d)\n", group);
993 		return false;
994 	}
995 	if ((!disable && !require && !mutual) ||	/* Auto */
996 	    (disable && !require && !mutual) ||	/* None */
997 	    (!disable && require && !mutual) ||	/* CHAP */
998 	    (!disable && require && mutual)) {	/* CHAP Mutual */
999 		return true;
1000 	}
1001 	SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n",
1002 		    disable, require, mutual);
1003 	return false;
1004 }
1005 
1006 struct spdk_iscsi_tgt_node *iscsi_tgt_node_construct(int target_index,
1007 		const char *name, const char *alias,
1008 		int *pg_tag_list, int *ig_tag_list, uint16_t num_maps,
1009 		const char *bdev_name_list[], int *lun_id_list, int num_luns,
1010 		int queue_depth,
1011 		bool disable_chap, bool require_chap, bool mutual_chap, int chap_group,
1012 		bool header_digest, bool data_digest)
1013 {
1014 	char				fullname[MAX_TMPBUF];
1015 	struct spdk_iscsi_tgt_node	*target;
1016 	int				rc;
1017 
1018 	if (!iscsi_check_chap_params(disable_chap, require_chap,
1019 				     mutual_chap, chap_group)) {
1020 		return NULL;
1021 	}
1022 
1023 	if (num_maps == 0) {
1024 		SPDK_ERRLOG("num_maps = 0\n");
1025 		return NULL;
1026 	}
1027 
1028 	if (name == NULL) {
1029 		SPDK_ERRLOG("TargetName not found\n");
1030 		return NULL;
1031 	}
1032 
1033 	if (strncasecmp(name, "iqn.", 4) != 0
1034 	    && strncasecmp(name, "eui.", 4) != 0
1035 	    && strncasecmp(name, "naa.", 4) != 0) {
1036 		snprintf(fullname, sizeof(fullname), "%s:%s", g_iscsi.nodebase, name);
1037 	} else {
1038 		snprintf(fullname, sizeof(fullname), "%s", name);
1039 	}
1040 
1041 	if (check_iscsi_name(fullname) != 0) {
1042 		SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n",
1043 			    name);
1044 		return NULL;
1045 	}
1046 
1047 	target = calloc(1, sizeof(*target));
1048 	if (!target) {
1049 		SPDK_ERRLOG("could not allocate target\n");
1050 		return NULL;
1051 	}
1052 
1053 	rc = pthread_mutex_init(&target->mutex, NULL);
1054 	if (rc != 0) {
1055 		SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num);
1056 		iscsi_tgt_node_destruct(target, NULL, NULL);
1057 		return NULL;
1058 	}
1059 
1060 	target->num = target_index;
1061 
1062 	memcpy(target->name, fullname, strlen(fullname));
1063 
1064 	if (alias != NULL) {
1065 		if (strlen(alias) > MAX_TARGET_NAME) {
1066 			iscsi_tgt_node_destruct(target, NULL, NULL);
1067 			return NULL;
1068 		}
1069 		memcpy(target->alias, alias, strlen(alias));
1070 	}
1071 
1072 	target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns,
1073 					      SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL);
1074 	if (!target->dev) {
1075 		SPDK_ERRLOG("Could not construct SCSI device\n");
1076 		iscsi_tgt_node_destruct(target, NULL, NULL);
1077 		return NULL;
1078 	}
1079 
1080 	TAILQ_INIT(&target->pg_map_head);
1081 	rc = iscsi_target_node_add_pg_ig_maps(target, pg_tag_list,
1082 					      ig_tag_list, num_maps);
1083 	if (rc != 0) {
1084 		SPDK_ERRLOG("could not add map to target\n");
1085 		iscsi_tgt_node_destruct(target, NULL, NULL);
1086 		return NULL;
1087 	}
1088 
1089 	target->disable_chap = disable_chap;
1090 	target->require_chap = require_chap;
1091 	target->mutual_chap = mutual_chap;
1092 	target->chap_group = chap_group;
1093 	target->header_digest = header_digest;
1094 	target->data_digest = data_digest;
1095 
1096 	if (queue_depth > 0 && ((uint32_t)queue_depth <= g_iscsi.MaxQueueDepth)) {
1097 		target->queue_depth = queue_depth;
1098 	} else {
1099 		SPDK_DEBUGLOG(iscsi, "QueueDepth %d is invalid and %d is used instead.\n",
1100 			      queue_depth, g_iscsi.MaxQueueDepth);
1101 		target->queue_depth = g_iscsi.MaxQueueDepth;
1102 	}
1103 
1104 	rc = iscsi_tgt_node_register(target);
1105 	if (rc != 0) {
1106 		SPDK_ERRLOG("register target is failed\n");
1107 		iscsi_tgt_node_destruct(target, NULL, NULL);
1108 		return NULL;
1109 	}
1110 
1111 	return target;
1112 }
1113 
1114 void
1115 iscsi_shutdown_tgt_nodes(void)
1116 {
1117 	struct spdk_iscsi_tgt_node *target;
1118 
1119 	pthread_mutex_lock(&g_iscsi.mutex);
1120 	while (!TAILQ_EMPTY(&g_iscsi.target_head)) {
1121 		target = TAILQ_FIRST(&g_iscsi.target_head);
1122 		TAILQ_REMOVE(&g_iscsi.target_head, target, tailq);
1123 
1124 		pthread_mutex_unlock(&g_iscsi.mutex);
1125 
1126 		iscsi_tgt_node_destruct(target, NULL, NULL);
1127 
1128 		pthread_mutex_lock(&g_iscsi.mutex);
1129 	}
1130 	pthread_mutex_unlock(&g_iscsi.mutex);
1131 }
1132 
1133 void
1134 iscsi_shutdown_tgt_node_by_name(const char *target_name,
1135 				iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
1136 {
1137 	struct spdk_iscsi_tgt_node *target;
1138 
1139 	pthread_mutex_lock(&g_iscsi.mutex);
1140 	target = iscsi_find_tgt_node(target_name);
1141 	if (target != NULL) {
1142 		iscsi_tgt_node_unregister(target);
1143 		pthread_mutex_unlock(&g_iscsi.mutex);
1144 
1145 		iscsi_tgt_node_destruct(target, cb_fn, cb_arg);
1146 
1147 		return;
1148 	}
1149 	pthread_mutex_unlock(&g_iscsi.mutex);
1150 
1151 	if (cb_fn) {
1152 		cb_fn(cb_arg, -ENOENT);
1153 	}
1154 }
1155 
1156 bool
1157 iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target)
1158 {
1159 	return target->destructed;
1160 }
1161 
1162 int
1163 iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1164 			    struct spdk_iscsi_tgt_node *target)
1165 {
1166 	int i;
1167 	struct spdk_iscsi_task *task;
1168 
1169 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1170 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1171 
1172 		if (!lun) {
1173 			continue;
1174 		}
1175 
1176 		/* we create a fake management task per LUN to cleanup */
1177 		task = iscsi_task_get(conn, NULL, iscsi_task_mgmt_cpl);
1178 		if (!task) {
1179 			SPDK_ERRLOG("Unable to acquire task\n");
1180 			return -1;
1181 		}
1182 
1183 		task->scsi.target_port = conn->target_port;
1184 		task->scsi.initiator_port = conn->initiator_port;
1185 		task->scsi.lun = lun;
1186 
1187 		iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
1188 	}
1189 
1190 	return 0;
1191 }
1192 
1193 void iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1194 			       struct spdk_iscsi_init_grp *initiator_group)
1195 {
1196 	struct spdk_iscsi_tgt_node *target;
1197 
1198 	pthread_mutex_lock(&g_iscsi.mutex);
1199 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1200 		if (portal_group) {
1201 			iscsi_tgt_node_delete_pg_map(target, portal_group);
1202 		}
1203 		if (initiator_group) {
1204 			iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1205 		}
1206 	}
1207 	pthread_mutex_unlock(&g_iscsi.mutex);
1208 }
1209 
1210 int
1211 iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target,
1212 		       const char *bdev_name, int lun_id)
1213 {
1214 	struct spdk_scsi_dev *dev;
1215 	int rc;
1216 
1217 	if (target->num_active_conns > 0) {
1218 		SPDK_ERRLOG("Target has active connections (count=%d)\n",
1219 			    target->num_active_conns);
1220 		return -1;
1221 	}
1222 
1223 	if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
1224 		SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id);
1225 		return -1;
1226 	}
1227 
1228 	dev = target->dev;
1229 	if (dev == NULL) {
1230 		SPDK_ERRLOG("SCSI device is not found\n");
1231 		return -1;
1232 	}
1233 
1234 	rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL);
1235 	if (rc != 0) {
1236 		SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n");
1237 		return -1;
1238 	}
1239 
1240 	return 0;
1241 }
1242 
1243 int
1244 iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target,
1245 			       bool disable_chap, bool require_chap,
1246 			       bool mutual_chap, int32_t chap_group)
1247 {
1248 	if (!iscsi_check_chap_params(disable_chap, require_chap,
1249 				     mutual_chap, chap_group)) {
1250 		return -EINVAL;
1251 	}
1252 
1253 	pthread_mutex_lock(&target->mutex);
1254 	target->disable_chap = disable_chap;
1255 	target->require_chap = require_chap;
1256 	target->mutual_chap = mutual_chap;
1257 	target->chap_group = chap_group;
1258 	pthread_mutex_unlock(&target->mutex);
1259 
1260 	return 0;
1261 }
1262 
1263 static void
1264 iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target,
1265 			 struct spdk_json_write_ctx *w)
1266 {
1267 	struct spdk_iscsi_pg_map *pg_map;
1268 	struct spdk_iscsi_ig_map *ig_map;
1269 	int i;
1270 
1271 	spdk_json_write_object_begin(w);
1272 
1273 	spdk_json_write_named_string(w, "name", target->name);
1274 
1275 	if (target->alias[0] != '\0') {
1276 		spdk_json_write_named_string(w, "alias_name", target->alias);
1277 	}
1278 
1279 	spdk_json_write_named_array_begin(w, "pg_ig_maps");
1280 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1281 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1282 			spdk_json_write_object_begin(w);
1283 			spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag);
1284 			spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag);
1285 			spdk_json_write_object_end(w);
1286 		}
1287 	}
1288 	spdk_json_write_array_end(w);
1289 
1290 	spdk_json_write_named_array_begin(w, "luns");
1291 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1292 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1293 
1294 		if (lun) {
1295 			spdk_json_write_object_begin(w);
1296 			spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1297 			spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun));
1298 			spdk_json_write_object_end(w);
1299 		}
1300 	}
1301 	spdk_json_write_array_end(w);
1302 
1303 	spdk_json_write_named_int32(w, "queue_depth", target->queue_depth);
1304 
1305 	spdk_json_write_named_bool(w, "disable_chap", target->disable_chap);
1306 	spdk_json_write_named_bool(w, "require_chap", target->require_chap);
1307 	spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap);
1308 	spdk_json_write_named_int32(w, "chap_group", target->chap_group);
1309 
1310 	spdk_json_write_named_bool(w, "header_digest", target->header_digest);
1311 	spdk_json_write_named_bool(w, "data_digest", target->data_digest);
1312 
1313 	spdk_json_write_object_end(w);
1314 }
1315 
1316 static void
1317 iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target,
1318 			   struct spdk_json_write_ctx *w)
1319 {
1320 	spdk_json_write_object_begin(w);
1321 
1322 	spdk_json_write_named_string(w, "method", "iscsi_create_target_node");
1323 
1324 	spdk_json_write_name(w, "params");
1325 	iscsi_tgt_node_info_json(target, w);
1326 
1327 	spdk_json_write_object_end(w);
1328 }
1329 
1330 void
1331 iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w)
1332 {
1333 	struct spdk_iscsi_tgt_node *target;
1334 
1335 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1336 		iscsi_tgt_node_info_json(target, w);
1337 	}
1338 }
1339 
1340 void
1341 iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w)
1342 {
1343 	struct spdk_iscsi_tgt_node *target;
1344 
1345 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1346 		iscsi_tgt_node_config_json(target, w);
1347 	}
1348 }
1349