xref: /spdk/lib/iscsi/tgt_node.c (revision 712a3f69d32632bf6c862f00200f7f437d3f7529)
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/conf.h"
38 #include "spdk/sock.h"
39 #include "spdk/scsi.h"
40 
41 #include "spdk_internal/log.h"
42 
43 #include "iscsi/iscsi.h"
44 #include "iscsi/conn.h"
45 #include "iscsi/tgt_node.h"
46 #include "iscsi/portal_grp.h"
47 #include "iscsi/init_grp.h"
48 #include "iscsi/task.h"
49 
50 #define MAX_TMPBUF 1024
51 #define MAX_MASKBUF 128
52 
53 static bool
54 iscsi_ipv6_netmask_allow_addr(const char *netmask, const char *addr)
55 {
56 	struct in6_addr in6_mask;
57 	struct in6_addr in6_addr;
58 	char mask[MAX_MASKBUF];
59 	const char *p;
60 	size_t n;
61 	int bits, bmask;
62 	int i;
63 
64 	if (netmask[0] != '[') {
65 		return false;
66 	}
67 	p = strchr(netmask, ']');
68 	if (p == NULL) {
69 		return false;
70 	}
71 	n = p - (netmask + 1);
72 	if (n + 1 > sizeof mask) {
73 		return false;
74 	}
75 
76 	memcpy(mask, netmask + 1, n);
77 	mask[n] = '\0';
78 	p++;
79 
80 	if (p[0] == '/') {
81 		bits = (int) strtol(p + 1, NULL, 10);
82 		if (bits <= 0 || bits > 128) {
83 			return false;
84 		}
85 	} else {
86 		bits = 128;
87 	}
88 
89 #if 0
90 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "input %s\n", addr);
91 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "mask  %s / %d\n", mask, bits);
92 #endif
93 
94 	/* presentation to network order binary */
95 	if (inet_pton(AF_INET6, mask, &in6_mask) <= 0
96 	    || inet_pton(AF_INET6, addr, &in6_addr) <= 0) {
97 		return false;
98 	}
99 
100 	/* check 128bits */
101 	for (i = 0; i < (bits / 8); i++) {
102 		if (in6_mask.s6_addr[i] != in6_addr.s6_addr[i]) {
103 			return false;
104 		}
105 	}
106 	if (bits % 8) {
107 		bmask = (0xffU << (8 - (bits % 8))) & 0xffU;
108 		if ((in6_mask.s6_addr[i] & bmask) != (in6_addr.s6_addr[i] & bmask)) {
109 			return false;
110 		}
111 	}
112 
113 	/* match */
114 	return true;
115 }
116 
117 static bool
118 iscsi_ipv4_netmask_allow_addr(const char *netmask, const char *addr)
119 {
120 	struct in_addr in4_mask;
121 	struct in_addr in4_addr;
122 	char mask[MAX_MASKBUF];
123 	const char *p;
124 	uint32_t bmask;
125 	size_t n;
126 	int bits;
127 
128 	p = strchr(netmask, '/');
129 	if (p == NULL) {
130 		p = netmask + strlen(netmask);
131 	}
132 	n = p - netmask;
133 	if (n + 1 > sizeof mask) {
134 		return false;
135 	}
136 
137 	memcpy(mask, netmask, n);
138 	mask[n] = '\0';
139 
140 	if (p[0] == '/') {
141 		bits = (int) strtol(p + 1, NULL, 10);
142 		if (bits <= 0 || bits > 32) {
143 			return false;
144 		}
145 	} else {
146 		bits = 32;
147 	}
148 
149 	/* presentation to network order binary */
150 	if (inet_pton(AF_INET, mask, &in4_mask) <= 0
151 	    || inet_pton(AF_INET, addr, &in4_addr) <= 0) {
152 		return false;
153 	}
154 
155 	/* check 32bits */
156 	bmask = (0xffffffffU << (32 - bits)) & 0xffffffffU;
157 	if ((ntohl(in4_mask.s_addr) & bmask) != (ntohl(in4_addr.s_addr) & bmask)) {
158 		return false;
159 	}
160 
161 	/* match */
162 	return true;
163 }
164 
165 static bool
166 iscsi_netmask_allow_addr(const char *netmask, const char *addr)
167 {
168 	if (netmask == NULL || addr == NULL) {
169 		return false;
170 	}
171 	if (strcasecmp(netmask, "ANY") == 0) {
172 		return true;
173 	}
174 	if (netmask[0] == '[') {
175 		/* IPv6 */
176 		if (iscsi_ipv6_netmask_allow_addr(netmask, addr)) {
177 			return true;
178 		}
179 	} else {
180 		/* IPv4 */
181 		if (iscsi_ipv4_netmask_allow_addr(netmask, addr)) {
182 			return true;
183 		}
184 	}
185 	return false;
186 }
187 
188 static bool
189 iscsi_init_grp_allow_addr(struct spdk_iscsi_init_grp *igp,
190 			  const char *addr)
191 {
192 	struct spdk_iscsi_initiator_netmask *imask;
193 
194 	TAILQ_FOREACH(imask, &igp->netmask_head, tailq) {
195 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "netmask=%s, addr=%s\n",
196 			      imask->mask, addr);
197 		if (iscsi_netmask_allow_addr(imask->mask, addr)) {
198 			return true;
199 		}
200 	}
201 	return false;
202 }
203 
204 static int
205 iscsi_init_grp_allow_iscsi_name(struct spdk_iscsi_init_grp *igp,
206 				const char *iqn, bool *result)
207 {
208 	struct spdk_iscsi_initiator_name *iname;
209 
210 	TAILQ_FOREACH(iname, &igp->initiator_head, tailq) {
211 		/* denied if iqn is matched */
212 		if ((iname->name[0] == '!')
213 		    && (strcasecmp(&iname->name[1], "ANY") == 0
214 			|| strcasecmp(&iname->name[1], iqn) == 0)) {
215 			*result = false;
216 			return 0;
217 		}
218 		/* allowed if iqn is matched */
219 		if (strcasecmp(iname->name, "ANY") == 0
220 		    || strcasecmp(iname->name, iqn) == 0) {
221 			*result = true;
222 			return 0;
223 		}
224 	}
225 	return -1;
226 }
227 
228 static struct spdk_iscsi_pg_map *
229 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
230 			   struct spdk_iscsi_portal_grp *pg);
231 
232 bool
233 spdk_iscsi_tgt_node_access(struct spdk_iscsi_conn *conn,
234 			   struct spdk_iscsi_tgt_node *target, const char *iqn, const char *addr)
235 {
236 	struct spdk_iscsi_portal_grp *pg;
237 	struct spdk_iscsi_pg_map *pg_map;
238 	struct spdk_iscsi_ig_map *ig_map;
239 	int rc;
240 	bool allowed = false;
241 
242 	if (conn == NULL || target == NULL || iqn == NULL || addr == NULL) {
243 		return false;
244 	}
245 	pg = conn->portal->group;
246 
247 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "pg=%d, iqn=%s, addr=%s\n",
248 		      pg->tag, iqn, addr);
249 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
250 	if (pg_map == NULL) {
251 		return false;
252 	}
253 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
254 		rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &allowed);
255 		if (rc == 0) {
256 			if (allowed == false) {
257 				goto denied;
258 			} else {
259 				if (iscsi_init_grp_allow_addr(ig_map->ig, addr)) {
260 					return true;
261 				}
262 			}
263 		} else {
264 			/* netmask is denied in this initiator group */
265 		}
266 	}
267 
268 denied:
269 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "access denied from %s (%s) to %s (%s:%s,%d)\n",
270 		      iqn, addr, target->name, conn->portal_host,
271 		      conn->portal_port, conn->pg_tag);
272 	return false;
273 }
274 
275 static bool
276 iscsi_tgt_node_allow_iscsi_name(struct spdk_iscsi_tgt_node *target, const char *iqn)
277 {
278 	struct spdk_iscsi_pg_map *pg_map;
279 	struct spdk_iscsi_ig_map *ig_map;
280 	int rc;
281 	bool result = false;
282 
283 	if (target == NULL || iqn == NULL) {
284 		return false;
285 	}
286 
287 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
288 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
289 			rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &result);
290 			if (rc == 0) {
291 				return result;
292 			}
293 		}
294 	}
295 
296 	return false;
297 }
298 
299 int
300 spdk_iscsi_send_tgts(struct spdk_iscsi_conn *conn, const char *iiqn,
301 		     const char *iaddr, const char *tiqn, uint8_t *data, int alloc_len,
302 		     int data_len)
303 {
304 	char buf[MAX_TMPBUF];
305 	struct spdk_iscsi_portal_grp	*pg;
306 	struct spdk_iscsi_pg_map	*pg_map;
307 	struct spdk_iscsi_portal	*p;
308 	struct spdk_iscsi_tgt_node	*target;
309 	char *host;
310 	int total;
311 	int len;
312 	int rc;
313 
314 	if (conn == NULL) {
315 		return 0;
316 	}
317 
318 	total = data_len;
319 	if (alloc_len < 1) {
320 		return 0;
321 	}
322 	if (total > alloc_len) {
323 		total = alloc_len;
324 		data[total - 1] = '\0';
325 		return total;
326 	}
327 
328 	if (alloc_len - total < 1) {
329 		SPDK_ERRLOG("data space small %d\n", alloc_len);
330 		return total;
331 	}
332 
333 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
334 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
335 		if (strcasecmp(tiqn, "ALL") != 0
336 		    && strcasecmp(tiqn, target->name) != 0) {
337 			continue;
338 		}
339 		rc = iscsi_tgt_node_allow_iscsi_name(target, iiqn);
340 		if (rc == 0) {
341 			continue;
342 		}
343 
344 		/* DO SENDTARGETS */
345 		len = snprintf((char *) data + total, alloc_len - total,
346 			       "TargetName=%s", target->name);
347 		total += len + 1;
348 
349 		/* write to data */
350 		TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
351 			pg = pg_map->pg;
352 			TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
353 				if (alloc_len - total < 1) {
354 					pthread_mutex_unlock(&g_spdk_iscsi.mutex);
355 					SPDK_ERRLOG("data space small %d\n", alloc_len);
356 					return total;
357 				}
358 				host = p->host;
359 				/* wildcard? */
360 				if (strcasecmp(host, "[::]") == 0
361 				    || strcasecmp(host, "0.0.0.0") == 0) {
362 					if (spdk_sock_is_ipv6(conn->sock)) {
363 						snprintf(buf, sizeof buf, "[%s]",
364 							 conn->target_addr);
365 						host = buf;
366 					} else if (spdk_sock_is_ipv4(conn->sock)) {
367 						snprintf(buf, sizeof buf, "%s",
368 							 conn->target_addr);
369 						host = buf;
370 					} else {
371 						/* skip portal for the family */
372 						continue;
373 					}
374 				}
375 				SPDK_DEBUGLOG(SPDK_LOG_ISCSI,
376 					      "TargetAddress=%s:%s,%d\n",
377 					      host, p->port, pg->tag);
378 				len = snprintf((char *) data + total,
379 					       alloc_len - total,
380 					       "TargetAddress=%s:%s,%d",
381 					       host, p->port, pg->tag);
382 				total += len + 1;
383 			}
384 		}
385 	}
386 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
387 
388 	return total;
389 }
390 
391 struct spdk_iscsi_tgt_node *
392 spdk_iscsi_find_tgt_node(const char *target_name)
393 {
394 	struct spdk_iscsi_tgt_node *target;
395 
396 	if (target_name == NULL) {
397 		return NULL;
398 	}
399 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
400 		if (strcasecmp(target_name, target->name) == 0) {
401 			return target;
402 		}
403 	}
404 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "can't find target %s\n", target_name);
405 	return NULL;
406 }
407 
408 static int
409 iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target)
410 {
411 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
412 
413 	if (spdk_iscsi_find_tgt_node(target->name) != NULL) {
414 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
415 		return -EEXIST;
416 	}
417 
418 	TAILQ_INSERT_TAIL(&g_spdk_iscsi.target_head, target, tailq);
419 
420 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
421 	return 0;
422 }
423 
424 static int
425 iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target)
426 {
427 	struct spdk_iscsi_tgt_node *t;
428 
429 	TAILQ_FOREACH(t, &g_spdk_iscsi.target_head, tailq) {
430 		if (t == target) {
431 			TAILQ_REMOVE(&g_spdk_iscsi.target_head, t, tailq);
432 			return 0;
433 		}
434 	}
435 
436 	return -1;
437 }
438 
439 static struct spdk_iscsi_ig_map *
440 iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map,
441 			 struct spdk_iscsi_init_grp *ig)
442 {
443 	struct spdk_iscsi_ig_map *ig_map;
444 
445 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
446 		if (ig_map->ig == ig) {
447 			return ig_map;
448 		}
449 	}
450 
451 	return NULL;
452 }
453 
454 static struct spdk_iscsi_ig_map *
455 iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map,
456 			struct spdk_iscsi_init_grp *ig)
457 {
458 	struct spdk_iscsi_ig_map *ig_map;
459 
460 	if (iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) {
461 		return NULL;
462 	}
463 
464 	ig_map = malloc(sizeof(*ig_map));
465 	if (ig_map == NULL) {
466 		return NULL;
467 	}
468 
469 	ig_map->ig = ig;
470 	ig->ref++;
471 	pg_map->num_ig_maps++;
472 	TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq);
473 
474 	return ig_map;
475 }
476 
477 static void
478 _iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
479 			    struct spdk_iscsi_ig_map *ig_map)
480 {
481 	TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq);
482 	pg_map->num_ig_maps--;
483 	ig_map->ig->ref--;
484 	free(ig_map);
485 }
486 
487 static int
488 iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
489 			   struct spdk_iscsi_init_grp *ig)
490 {
491 	struct spdk_iscsi_ig_map *ig_map;
492 
493 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
494 	if (ig_map == NULL) {
495 		return -ENOENT;
496 	}
497 
498 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
499 	return 0;
500 }
501 
502 static void
503 iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map)
504 {
505 	struct spdk_iscsi_ig_map *ig_map, *tmp;
506 
507 	TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) {
508 		_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
509 	}
510 }
511 
512 static struct spdk_iscsi_pg_map *
513 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
514 			   struct spdk_iscsi_portal_grp *pg)
515 {
516 	struct spdk_iscsi_pg_map *pg_map;
517 
518 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
519 		if (pg_map->pg == pg) {
520 			return pg_map;
521 		}
522 	}
523 
524 	return NULL;
525 }
526 
527 static struct spdk_iscsi_pg_map *
528 iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target,
529 			  struct spdk_iscsi_portal_grp *pg)
530 {
531 	struct spdk_iscsi_pg_map *pg_map;
532 	char port_name[MAX_TMPBUF];
533 	int rc;
534 
535 	if (iscsi_tgt_node_find_pg_map(target, pg) != NULL) {
536 		return NULL;
537 	}
538 
539 	if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) {
540 		SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n",
541 			    SPDK_SCSI_DEV_MAX_PORTS);
542 		return NULL;
543 	}
544 
545 	pg_map = malloc(sizeof(*pg_map));
546 	if (pg_map == NULL) {
547 		return NULL;
548 	}
549 
550 	snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x",
551 		 spdk_scsi_dev_get_name(target->dev), pg->tag);
552 	rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name);
553 	if (rc != 0) {
554 		free(pg_map);
555 		return NULL;
556 	}
557 
558 	TAILQ_INIT(&pg_map->ig_map_head);
559 	pg_map->num_ig_maps = 0;
560 	pg->ref++;
561 	pg_map->pg = pg;
562 	target->num_pg_maps++;
563 	TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq);
564 
565 	return pg_map;
566 }
567 
568 static void
569 _iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
570 			      struct spdk_iscsi_pg_map *pg_map)
571 {
572 	TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq);
573 	target->num_pg_maps--;
574 	pg_map->pg->ref--;
575 
576 	spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag);
577 
578 	free(pg_map);
579 }
580 
581 static int
582 iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
583 			     struct spdk_iscsi_portal_grp *pg)
584 {
585 	struct spdk_iscsi_pg_map *pg_map;
586 
587 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
588 	if (pg_map == NULL) {
589 		return -ENOENT;
590 	}
591 
592 	if (pg_map->num_ig_maps > 0) {
593 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "delete %d ig_maps forcefully\n",
594 			      pg_map->num_ig_maps);
595 	}
596 
597 	iscsi_pg_map_delete_all_ig_maps(pg_map);
598 	_iscsi_tgt_node_delete_pg_map(target, pg_map);
599 	return 0;
600 }
601 
602 static void
603 iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target,
604 			      struct spdk_iscsi_init_grp *ig)
605 {
606 	struct spdk_iscsi_pg_map *pg_map, *tmp;
607 
608 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
609 		iscsi_pg_map_delete_ig_map(pg_map, ig);
610 		if (pg_map->num_ig_maps == 0) {
611 			_iscsi_tgt_node_delete_pg_map(target, pg_map);
612 		}
613 	}
614 }
615 
616 static void
617 iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target)
618 {
619 	struct spdk_iscsi_pg_map *pg_map, *tmp;
620 
621 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
622 		iscsi_pg_map_delete_all_ig_maps(pg_map);
623 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
624 	}
625 }
626 
627 static void
628 _iscsi_tgt_node_destruct(void *cb_arg, int rc)
629 {
630 	struct spdk_iscsi_tgt_node *target = cb_arg;
631 	iscsi_tgt_node_destruct_cb destruct_cb_fn = target->destruct_cb_fn;
632 	void *destruct_cb_arg = target->destruct_cb_arg;
633 
634 	if (rc != 0) {
635 		if (destruct_cb_fn) {
636 			destruct_cb_fn(destruct_cb_arg, rc);
637 		}
638 		return;
639 	}
640 
641 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
642 	iscsi_tgt_node_delete_all_pg_maps(target);
643 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
644 
645 	pthread_mutex_destroy(&target->mutex);
646 	free(target);
647 
648 	if (destruct_cb_fn) {
649 		destruct_cb_fn(destruct_cb_arg, 0);
650 	}
651 }
652 
653 static int
654 iscsi_tgt_node_check_active_conns(void *arg)
655 {
656 	struct spdk_iscsi_tgt_node *target = arg;
657 
658 	if (spdk_iscsi_get_active_conns(target) != 0) {
659 		return 1;
660 	}
661 
662 	spdk_poller_unregister(&target->destruct_poller);
663 
664 	spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
665 
666 	return 1;
667 }
668 
669 static void
670 iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target,
671 			iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
672 {
673 	if (target == NULL) {
674 		if (cb_fn) {
675 			cb_fn(cb_arg, -ENOENT);
676 		}
677 		return;
678 	}
679 
680 	if (target->destructed) {
681 		SPDK_ERRLOG("Destructing %s is already started\n", target->name);
682 		if (cb_fn) {
683 			cb_fn(cb_arg, -EBUSY);
684 		}
685 		return;
686 	}
687 
688 	target->destructed = true;
689 	target->destruct_cb_fn = cb_fn;
690 	target->destruct_cb_arg = cb_arg;
691 
692 	spdk_iscsi_conns_request_logout(target);
693 
694 	if (spdk_iscsi_get_active_conns(target) != 0) {
695 		target->destruct_poller = spdk_poller_register(iscsi_tgt_node_check_active_conns,
696 					  target, 10);
697 	} else {
698 		spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
699 	}
700 
701 }
702 
703 static int
704 iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target,
705 				int pg_tag, int ig_tag)
706 {
707 	struct spdk_iscsi_portal_grp	*pg;
708 	struct spdk_iscsi_init_grp	*ig;
709 	struct spdk_iscsi_pg_map	*pg_map;
710 	struct spdk_iscsi_ig_map	*ig_map;
711 
712 	pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag);
713 	if (pg == NULL) {
714 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
715 		return -ENOENT;
716 	}
717 	ig = spdk_iscsi_init_grp_find_by_tag(ig_tag);
718 	if (ig == NULL) {
719 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
720 		return -ENOENT;
721 	}
722 
723 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
724 	if (pg_map == NULL) {
725 		SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag);
726 		return -ENOENT;
727 	}
728 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
729 	if (ig_map == NULL) {
730 		SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag);
731 		return -ENOENT;
732 	}
733 
734 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
735 	if (pg_map->num_ig_maps == 0) {
736 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
737 	}
738 
739 	return 0;
740 }
741 
742 static int
743 iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target,
744 			     int pg_tag, int ig_tag)
745 {
746 	struct spdk_iscsi_portal_grp	*pg;
747 	struct spdk_iscsi_pg_map	*pg_map;
748 	struct spdk_iscsi_init_grp	*ig;
749 	struct spdk_iscsi_ig_map	*ig_map;
750 	bool				new_pg_map = false;
751 
752 	pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag);
753 	if (pg == NULL) {
754 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
755 		return -ENOENT;
756 	}
757 	ig = spdk_iscsi_init_grp_find_by_tag(ig_tag);
758 	if (ig == NULL) {
759 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
760 		return -ENOENT;
761 	}
762 
763 	/* get existing pg_map or create new pg_map and add it to target */
764 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
765 	if (pg_map == NULL) {
766 		pg_map = iscsi_tgt_node_add_pg_map(target, pg);
767 		if (pg_map == NULL) {
768 			goto failed;
769 		}
770 		new_pg_map = true;
771 	}
772 
773 	/* create new ig_map and add it to pg_map */
774 	ig_map = iscsi_pg_map_add_ig_map(pg_map, ig);
775 	if (ig_map == NULL) {
776 		goto failed;
777 	}
778 
779 	return 0;
780 
781 failed:
782 	if (new_pg_map) {
783 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
784 	}
785 
786 	return -1;
787 }
788 
789 int
790 spdk_iscsi_target_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
791 				      int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
792 {
793 	uint16_t i;
794 	int rc;
795 
796 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
797 	for (i = 0; i < num_maps; i++) {
798 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i],
799 						  ig_tag_list[i]);
800 		if (rc != 0) {
801 			SPDK_ERRLOG("could not add map to target\n");
802 			goto invalid;
803 		}
804 	}
805 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
806 	return 0;
807 
808 invalid:
809 	for (; i > 0; --i) {
810 		iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1],
811 						ig_tag_list[i - 1]);
812 	}
813 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
814 	return -1;
815 }
816 
817 int
818 spdk_iscsi_target_node_remove_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
819 		int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
820 {
821 	uint16_t i;
822 	int rc;
823 
824 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
825 	for (i = 0; i < num_maps; i++) {
826 		rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i],
827 						     ig_tag_list[i]);
828 		if (rc != 0) {
829 			SPDK_ERRLOG("could not delete map from target\n");
830 			goto invalid;
831 		}
832 	}
833 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
834 	return 0;
835 
836 invalid:
837 	for (; i > 0; --i) {
838 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1],
839 						  ig_tag_list[i - 1]);
840 		if (rc != 0) {
841 			iscsi_tgt_node_delete_all_pg_maps(target);
842 			break;
843 		}
844 	}
845 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
846 	return -1;
847 }
848 
849 static int
850 check_iscsi_name(const char *name)
851 {
852 	const unsigned char *up = (const unsigned char *) name;
853 	size_t n;
854 
855 	/* valid iSCSI name no larger than 223 bytes */
856 	if (strlen(name) > MAX_TARGET_NAME) {
857 		return -1;
858 	}
859 
860 	/* valid iSCSI name? */
861 	for (n = 0; up[n] != 0; n++) {
862 		if (up[n] > 0x00U && up[n] <= 0x2cU) {
863 			return -1;
864 		}
865 		if (up[n] == 0x2fU) {
866 			return -1;
867 		}
868 		if (up[n] >= 0x3bU && up[n] <= 0x40U) {
869 			return -1;
870 		}
871 		if (up[n] >= 0x5bU && up[n] <= 0x60U) {
872 			return -1;
873 		}
874 		if (up[n] >= 0x7bU && up[n] <= 0x7fU) {
875 			return -1;
876 		}
877 		if (isspace(up[n])) {
878 			return -1;
879 		}
880 	}
881 	/* valid format? */
882 	if (strncasecmp(name, "iqn.", 4) == 0) {
883 		/* iqn.YYYY-MM.reversed.domain.name */
884 		if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
885 		    || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
886 		    || !isdigit(up[10]) || up[11] != '.') {
887 			SPDK_ERRLOG("invalid iqn format. "
888 				    "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
889 			return -1;
890 		}
891 	} else if (strncasecmp(name, "eui.", 4) == 0) {
892 		/* EUI-64 -> 16bytes */
893 		/* XXX */
894 	} else if (strncasecmp(name, "naa.", 4) == 0) {
895 		/* 64bit -> 16bytes, 128bit -> 32bytes */
896 		/* XXX */
897 	}
898 	/* OK */
899 	return 0;
900 }
901 
902 bool
903 spdk_iscsi_check_chap_params(bool disable, bool require, bool mutual, int group)
904 {
905 	if (group < 0) {
906 		SPDK_ERRLOG("Invalid auth group ID (%d)\n", group);
907 		return false;
908 	}
909 	if ((!disable && !require && !mutual) ||	/* Auto */
910 	    (disable && !require && !mutual) ||	/* None */
911 	    (!disable && require && !mutual) ||	/* CHAP */
912 	    (!disable && require && mutual)) {	/* CHAP Mutual */
913 		return true;
914 	}
915 	SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n",
916 		    disable, require, mutual);
917 	return false;
918 }
919 
920 _spdk_iscsi_tgt_node *
921 spdk_iscsi_tgt_node_construct(int target_index,
922 			      const char *name, const char *alias,
923 			      int *pg_tag_list, int *ig_tag_list, uint16_t num_maps,
924 			      const char *bdev_name_list[], int *lun_id_list, int num_luns,
925 			      int queue_depth,
926 			      bool disable_chap, bool require_chap, bool mutual_chap, int chap_group,
927 			      bool header_digest, bool data_digest)
928 {
929 	char				fullname[MAX_TMPBUF];
930 	struct spdk_iscsi_tgt_node	*target;
931 	int				rc;
932 
933 	if (!spdk_iscsi_check_chap_params(disable_chap, require_chap,
934 					  mutual_chap, chap_group)) {
935 		return NULL;
936 	}
937 
938 	if (num_maps == 0) {
939 		SPDK_ERRLOG("num_maps = 0\n");
940 		return NULL;
941 	}
942 
943 	if (name == NULL) {
944 		SPDK_ERRLOG("TargetName not found\n");
945 		return NULL;
946 	}
947 
948 	if (strncasecmp(name, "iqn.", 4) != 0
949 	    && strncasecmp(name, "eui.", 4) != 0
950 	    && strncasecmp(name, "naa.", 4) != 0) {
951 		snprintf(fullname, sizeof(fullname), "%s:%s", g_spdk_iscsi.nodebase, name);
952 	} else {
953 		snprintf(fullname, sizeof(fullname), "%s", name);
954 	}
955 
956 	if (check_iscsi_name(fullname) != 0) {
957 		SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n",
958 			    name);
959 		return NULL;
960 	}
961 
962 	target = calloc(1, sizeof(*target));
963 	if (!target) {
964 		SPDK_ERRLOG("could not allocate target\n");
965 		return NULL;
966 	}
967 
968 	rc = pthread_mutex_init(&target->mutex, NULL);
969 	if (rc != 0) {
970 		SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num);
971 		iscsi_tgt_node_destruct(target, NULL, NULL);
972 		return NULL;
973 	}
974 
975 	target->num = target_index;
976 
977 	memcpy(target->name, fullname, strlen(fullname));
978 
979 	if (alias != NULL) {
980 		if (strlen(alias) > MAX_TARGET_NAME) {
981 			iscsi_tgt_node_destruct(target, NULL, NULL);
982 			return NULL;
983 		}
984 		memcpy(target->alias, alias, strlen(alias));
985 	}
986 
987 	target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns,
988 					      SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL);
989 	if (!target->dev) {
990 		SPDK_ERRLOG("Could not construct SCSI device\n");
991 		iscsi_tgt_node_destruct(target, NULL, NULL);
992 		return NULL;
993 	}
994 
995 	TAILQ_INIT(&target->pg_map_head);
996 	rc = spdk_iscsi_target_node_add_pg_ig_maps(target, pg_tag_list,
997 			ig_tag_list, num_maps);
998 	if (rc != 0) {
999 		SPDK_ERRLOG("could not add map to target\n");
1000 		iscsi_tgt_node_destruct(target, NULL, NULL);
1001 		return NULL;
1002 	}
1003 
1004 	target->disable_chap = disable_chap;
1005 	target->require_chap = require_chap;
1006 	target->mutual_chap = mutual_chap;
1007 	target->chap_group = chap_group;
1008 	target->header_digest = header_digest;
1009 	target->data_digest = data_digest;
1010 
1011 	if (queue_depth > 0 && ((uint32_t)queue_depth <= g_spdk_iscsi.MaxQueueDepth)) {
1012 		target->queue_depth = queue_depth;
1013 	} else {
1014 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "QueueDepth %d is invalid and %d is used instead.\n",
1015 			      queue_depth, g_spdk_iscsi.MaxQueueDepth);
1016 		target->queue_depth = g_spdk_iscsi.MaxQueueDepth;
1017 	}
1018 
1019 	rc = iscsi_tgt_node_register(target);
1020 	if (rc != 0) {
1021 		SPDK_ERRLOG("register target is failed\n");
1022 		iscsi_tgt_node_destruct(target, NULL, NULL);
1023 		return NULL;
1024 	}
1025 
1026 	return target;
1027 }
1028 
1029 static int
1030 iscsi_parse_tgt_node(struct spdk_conf_section *sp)
1031 {
1032 	char buf[MAX_TMPBUF];
1033 	struct spdk_iscsi_tgt_node *target;
1034 	int pg_tag_list[MAX_TARGET_MAP], ig_tag_list[MAX_TARGET_MAP];
1035 	int num_target_maps;
1036 	const char *alias, *pg_tag, *ig_tag;
1037 	const char *ag_tag;
1038 	const char *val, *name;
1039 	int target_num, chap_group, pg_tag_i, ig_tag_i;
1040 	bool header_digest, data_digest;
1041 	bool disable_chap, require_chap, mutual_chap;
1042 	int i;
1043 	int lun_id_list[SPDK_SCSI_DEV_MAX_LUN];
1044 	const char *bdev_name_list[SPDK_SCSI_DEV_MAX_LUN];
1045 	int num_luns, queue_depth;
1046 
1047 	target_num = spdk_conf_section_get_num(sp);
1048 
1049 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "add unit %d\n", target_num);
1050 
1051 	data_digest = false;
1052 	header_digest = false;
1053 
1054 	name = spdk_conf_section_get_val(sp, "TargetName");
1055 
1056 	if (name == NULL) {
1057 		SPDK_ERRLOG("tgt_node%d: TargetName not found\n", target_num);
1058 		return -1;
1059 	}
1060 
1061 	alias = spdk_conf_section_get_val(sp, "TargetAlias");
1062 
1063 	/* Setup initiator and portal group mapping */
1064 	val = spdk_conf_section_get_val(sp, "Mapping");
1065 	if (val == NULL) {
1066 		/* no map */
1067 		SPDK_ERRLOG("tgt_node%d: no Mapping\n", target_num);
1068 		return -1;
1069 	}
1070 
1071 	for (i = 0; i < MAX_TARGET_MAP; i++) {
1072 		val = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1073 		if (val == NULL) {
1074 			break;
1075 		}
1076 		pg_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1077 		ig_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 1);
1078 		if (pg_tag == NULL || ig_tag == NULL) {
1079 			SPDK_ERRLOG("tgt_node%d: mapping error\n", target_num);
1080 			return -1;
1081 		}
1082 		if (strncasecmp(pg_tag, "PortalGroup",
1083 				strlen("PortalGroup")) != 0
1084 		    || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) {
1085 			SPDK_ERRLOG("tgt_node%d: mapping portal error\n", target_num);
1086 			return -1;
1087 		}
1088 		if (strncasecmp(ig_tag, "InitiatorGroup",
1089 				strlen("InitiatorGroup")) != 0
1090 		    || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) {
1091 			SPDK_ERRLOG("tgt_node%d: mapping initiator error\n", target_num);
1092 			return -1;
1093 		}
1094 		if (pg_tag_i < 1 || ig_tag_i < 1) {
1095 			SPDK_ERRLOG("tgt_node%d: invalid group tag\n", target_num);
1096 			return -1;
1097 		}
1098 		pg_tag_list[i] = pg_tag_i;
1099 		ig_tag_list[i] = ig_tag_i;
1100 	}
1101 
1102 	num_target_maps = i;
1103 
1104 	/* Setup AuthMethod */
1105 	val = spdk_conf_section_get_val(sp, "AuthMethod");
1106 	disable_chap = false;
1107 	require_chap = false;
1108 	mutual_chap = false;
1109 	if (val != NULL) {
1110 		for (i = 0; ; i++) {
1111 			val = spdk_conf_section_get_nmval(sp, "AuthMethod", 0, i);
1112 			if (val == NULL) {
1113 				break;
1114 			}
1115 			if (strcasecmp(val, "CHAP") == 0) {
1116 				require_chap = true;
1117 			} else if (strcasecmp(val, "Mutual") == 0) {
1118 				mutual_chap = true;
1119 			} else if (strcasecmp(val, "Auto") == 0) {
1120 				disable_chap = false;
1121 				require_chap = false;
1122 				mutual_chap = false;
1123 			} else if (strcasecmp(val, "None") == 0) {
1124 				disable_chap = true;
1125 				require_chap = false;
1126 				mutual_chap = false;
1127 			} else {
1128 				SPDK_ERRLOG("tgt_node%d: unknown auth\n", target_num);
1129 				return -1;
1130 			}
1131 		}
1132 		if (mutual_chap && !require_chap) {
1133 			SPDK_ERRLOG("tgt_node%d: Mutual but not CHAP\n", target_num);
1134 			return -1;
1135 		}
1136 	}
1137 	if (disable_chap) {
1138 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod None\n");
1139 	} else if (!require_chap) {
1140 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod Auto\n");
1141 	} else {
1142 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod CHAP %s\n",
1143 			      mutual_chap ? "Mutual" : "");
1144 	}
1145 
1146 	val = spdk_conf_section_get_val(sp, "AuthGroup");
1147 	if (val == NULL) {
1148 		chap_group = 0;
1149 	} else {
1150 		ag_tag = val;
1151 		if (strcasecmp(ag_tag, "None") == 0) {
1152 			chap_group = 0;
1153 		} else {
1154 			if (strncasecmp(ag_tag, "AuthGroup",
1155 					strlen("AuthGroup")) != 0
1156 			    || sscanf(ag_tag, "%*[^0-9]%d", &chap_group) != 1) {
1157 				SPDK_ERRLOG("tgt_node%d: auth group error\n", target_num);
1158 				return -1;
1159 			}
1160 			if (chap_group == 0) {
1161 				SPDK_ERRLOG("tgt_node%d: invalid auth group 0\n", target_num);
1162 				return -1;
1163 			}
1164 		}
1165 	}
1166 	if (chap_group == 0) {
1167 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup None\n");
1168 	} else {
1169 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup AuthGroup%d\n", chap_group);
1170 	}
1171 
1172 	val = spdk_conf_section_get_val(sp, "UseDigest");
1173 	if (val != NULL) {
1174 		for (i = 0; ; i++) {
1175 			val = spdk_conf_section_get_nmval(sp, "UseDigest", 0, i);
1176 			if (val == NULL) {
1177 				break;
1178 			}
1179 			if (strcasecmp(val, "Header") == 0) {
1180 				header_digest = true;
1181 			} else if (strcasecmp(val, "Data") == 0) {
1182 				data_digest = true;
1183 			} else if (strcasecmp(val, "Auto") == 0) {
1184 				header_digest = false;
1185 				data_digest = false;
1186 			} else {
1187 				SPDK_ERRLOG("tgt_node%d: unknown digest\n", target_num);
1188 				return -1;
1189 			}
1190 		}
1191 	}
1192 	if (!header_digest && !data_digest) {
1193 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest Auto\n");
1194 	} else {
1195 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest %s %s\n",
1196 			      header_digest ? "Header" : "",
1197 			      data_digest ? "Data" : "");
1198 	}
1199 
1200 	val = spdk_conf_section_get_val(sp, "QueueDepth");
1201 	if (val == NULL) {
1202 		queue_depth = g_spdk_iscsi.MaxQueueDepth;
1203 	} else {
1204 		queue_depth = (int) strtol(val, NULL, 10);
1205 	}
1206 
1207 	num_luns = 0;
1208 
1209 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1210 		snprintf(buf, sizeof(buf), "LUN%d", i);
1211 		val = spdk_conf_section_get_val(sp, buf);
1212 		if (val == NULL) {
1213 			continue;
1214 		}
1215 
1216 		bdev_name_list[num_luns] = val;
1217 		lun_id_list[num_luns] = i;
1218 		num_luns++;
1219 	}
1220 
1221 	if (num_luns == 0) {
1222 		SPDK_ERRLOG("tgt_node%d: No LUN specified for target %s.\n", target_num, name);
1223 		return -1;
1224 	}
1225 
1226 	target = spdk_iscsi_tgt_node_construct(target_num, name, alias,
1227 					       pg_tag_list, ig_tag_list, num_target_maps,
1228 					       bdev_name_list, lun_id_list, num_luns, queue_depth,
1229 					       disable_chap, require_chap, mutual_chap, chap_group,
1230 					       header_digest, data_digest);
1231 
1232 	if (target == NULL) {
1233 		SPDK_ERRLOG("tgt_node%d: add_iscsi_target_node error\n", target_num);
1234 		return -1;
1235 	}
1236 
1237 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1238 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1239 
1240 		if (lun) {
1241 			SPDK_INFOLOG(SPDK_LOG_ISCSI, "device %d: LUN%d %s\n",
1242 				     spdk_scsi_dev_get_id(target->dev),
1243 				     spdk_scsi_lun_get_id(lun),
1244 				     spdk_scsi_lun_get_bdev_name(lun));
1245 		}
1246 	}
1247 
1248 	return 0;
1249 }
1250 
1251 int spdk_iscsi_parse_tgt_nodes(void)
1252 {
1253 	struct spdk_conf_section *sp;
1254 	int rc;
1255 
1256 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_parse_tgt_nodes\n");
1257 
1258 	sp = spdk_conf_first_section(NULL);
1259 	while (sp != NULL) {
1260 		if (spdk_conf_section_match_prefix(sp, "TargetNode")) {
1261 			int tag = spdk_conf_section_get_num(sp);
1262 
1263 			if (tag > SPDK_TN_TAG_MAX) {
1264 				SPDK_ERRLOG("tag %d is invalid\n", tag);
1265 				return -1;
1266 			}
1267 			rc = iscsi_parse_tgt_node(sp);
1268 			if (rc < 0) {
1269 				SPDK_ERRLOG("spdk_iscsi_parse_tgt_node() failed\n");
1270 				return -1;
1271 			}
1272 		}
1273 		sp = spdk_conf_next_section(sp);
1274 	}
1275 	return 0;
1276 }
1277 
1278 void
1279 spdk_iscsi_shutdown_tgt_nodes(void)
1280 {
1281 	struct spdk_iscsi_tgt_node *target;
1282 
1283 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1284 	while (!TAILQ_EMPTY(&g_spdk_iscsi.target_head)) {
1285 		target = TAILQ_FIRST(&g_spdk_iscsi.target_head);
1286 		TAILQ_REMOVE(&g_spdk_iscsi.target_head, target, tailq);
1287 
1288 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1289 
1290 		iscsi_tgt_node_destruct(target, NULL, NULL);
1291 
1292 		pthread_mutex_lock(&g_spdk_iscsi.mutex);
1293 	}
1294 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1295 }
1296 
1297 void
1298 spdk_iscsi_shutdown_tgt_node_by_name(const char *target_name,
1299 				     iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
1300 {
1301 	struct spdk_iscsi_tgt_node *target;
1302 
1303 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1304 	target = spdk_iscsi_find_tgt_node(target_name);
1305 	if (target != NULL) {
1306 		iscsi_tgt_node_unregister(target);
1307 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1308 
1309 		iscsi_tgt_node_destruct(target, cb_fn, cb_arg);
1310 
1311 		return;
1312 	}
1313 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1314 
1315 	if (cb_fn) {
1316 		cb_fn(cb_arg, -ENOENT);
1317 	}
1318 }
1319 
1320 bool
1321 spdk_iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target)
1322 {
1323 	return target->destructed;
1324 }
1325 
1326 int
1327 spdk_iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1328 				 struct spdk_iscsi_tgt_node *target)
1329 {
1330 	int i;
1331 	struct spdk_iscsi_task *task;
1332 
1333 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1334 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1335 
1336 		if (!lun) {
1337 			continue;
1338 		}
1339 
1340 		/* we create a fake management task per LUN to cleanup */
1341 		task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_mgmt_cpl);
1342 		if (!task) {
1343 			SPDK_ERRLOG("Unable to acquire task\n");
1344 			return -1;
1345 		}
1346 
1347 		task->scsi.target_port = conn->target_port;
1348 		task->scsi.initiator_port = conn->initiator_port;
1349 		task->scsi.lun = lun;
1350 
1351 		spdk_iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
1352 	}
1353 
1354 	return 0;
1355 }
1356 
1357 void spdk_iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1358 				    struct spdk_iscsi_init_grp *initiator_group)
1359 {
1360 	struct spdk_iscsi_tgt_node *target;
1361 
1362 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1363 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1364 		if (portal_group) {
1365 			iscsi_tgt_node_delete_pg_map(target, portal_group);
1366 		}
1367 		if (initiator_group) {
1368 			iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1369 		}
1370 	}
1371 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1372 }
1373 
1374 int
1375 spdk_iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target,
1376 			    const char *bdev_name, int lun_id)
1377 {
1378 	struct spdk_scsi_dev *dev;
1379 	int rc;
1380 
1381 	if (target->num_active_conns > 0) {
1382 		SPDK_ERRLOG("Target has active connections (count=%d)\n",
1383 			    target->num_active_conns);
1384 		return -1;
1385 	}
1386 
1387 	if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
1388 		SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id);
1389 		return -1;
1390 	}
1391 
1392 	dev = target->dev;
1393 	if (dev == NULL) {
1394 		SPDK_ERRLOG("SCSI device is not found\n");
1395 		return -1;
1396 	}
1397 
1398 	rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL);
1399 	if (rc != 0) {
1400 		SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n");
1401 		return -1;
1402 	}
1403 
1404 	return 0;
1405 }
1406 
1407 int
1408 spdk_iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target,
1409 				    bool disable_chap, bool require_chap,
1410 				    bool mutual_chap, int32_t chap_group)
1411 {
1412 	if (!spdk_iscsi_check_chap_params(disable_chap, require_chap,
1413 					  mutual_chap, chap_group)) {
1414 		return -EINVAL;
1415 	}
1416 
1417 	pthread_mutex_lock(&target->mutex);
1418 	target->disable_chap = disable_chap;
1419 	target->require_chap = require_chap;
1420 	target->mutual_chap = mutual_chap;
1421 	target->chap_group = chap_group;
1422 	pthread_mutex_unlock(&target->mutex);
1423 
1424 	return 0;
1425 }
1426 
1427 static const char *target_nodes_section = \
1428 		"\n"
1429 		"# Users should change the TargetNode section(s) below to match the\n"
1430 		"#  desired iSCSI target node configuration.\n"
1431 		"# TargetName, Mapping, LUN0 are minimum required\n";
1432 
1433 #define TARGET_NODE_TMPL \
1434 "[TargetNode%d]\n" \
1435 "  Comment \"Target%d\"\n" \
1436 "  TargetName %s\n" \
1437 "  TargetAlias \"%s\"\n"
1438 
1439 #define TARGET_NODE_PGIG_MAPPING_TMPL \
1440 "  Mapping PortalGroup%d InitiatorGroup%d\n"
1441 
1442 #define TARGET_NODE_AUTH_TMPL \
1443 "  AuthMethod %s\n" \
1444 "  AuthGroup %s\n" \
1445 "  UseDigest %s\n"
1446 
1447 #define TARGET_NODE_QD_TMPL \
1448 "  QueueDepth %d\n\n"
1449 
1450 #define TARGET_NODE_LUN_TMPL \
1451 "  LUN%d %s\n"
1452 
1453 void
1454 spdk_iscsi_tgt_nodes_config_text(FILE *fp)
1455 {
1456 	int l = 0;
1457 	struct spdk_scsi_dev *dev = NULL;
1458 	struct spdk_iscsi_tgt_node *target = NULL;
1459 	struct spdk_iscsi_pg_map *pg_map;
1460 	struct spdk_iscsi_ig_map *ig_map;
1461 
1462 	/* Create target nodes section */
1463 	fprintf(fp, "%s", target_nodes_section);
1464 
1465 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1466 		int idx;
1467 		const char *authmethod = "None";
1468 		char authgroup[32] = "None";
1469 		const char *usedigest = "Auto";
1470 
1471 		dev = target->dev;
1472 		if (NULL == dev) { continue; }
1473 
1474 		idx = target->num;
1475 		fprintf(fp, TARGET_NODE_TMPL, idx, idx, target->name, spdk_scsi_dev_get_name(dev));
1476 
1477 		TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1478 			TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1479 				fprintf(fp, TARGET_NODE_PGIG_MAPPING_TMPL,
1480 					pg_map->pg->tag,
1481 					ig_map->ig->tag);
1482 			}
1483 		}
1484 
1485 		if (target->disable_chap) {
1486 			authmethod = "None";
1487 		} else if (!target->require_chap) {
1488 			authmethod = "Auto";
1489 		} else if (target->mutual_chap) {
1490 			authmethod = "CHAP Mutual";
1491 		} else {
1492 			authmethod = "CHAP";
1493 		}
1494 
1495 		if (target->chap_group > 0) {
1496 			snprintf(authgroup, sizeof(authgroup), "AuthGroup%d", target->chap_group);
1497 		}
1498 
1499 		if (target->header_digest) {
1500 			usedigest = "Header";
1501 		} else if (target->data_digest) {
1502 			usedigest = "Data";
1503 		}
1504 
1505 		fprintf(fp, TARGET_NODE_AUTH_TMPL,
1506 			authmethod, authgroup, usedigest);
1507 
1508 		for (l = 0; l < SPDK_SCSI_DEV_MAX_LUN; l++) {
1509 			struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(dev, l);
1510 
1511 			if (!lun) {
1512 				continue;
1513 			}
1514 
1515 			fprintf(fp, TARGET_NODE_LUN_TMPL,
1516 				spdk_scsi_lun_get_id(lun),
1517 				spdk_scsi_lun_get_bdev_name(lun));
1518 		}
1519 
1520 		fprintf(fp, TARGET_NODE_QD_TMPL,
1521 			target->queue_depth);
1522 	}
1523 }
1524 
1525 static void
1526 iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target,
1527 			 struct spdk_json_write_ctx *w)
1528 {
1529 	struct spdk_iscsi_pg_map *pg_map;
1530 	struct spdk_iscsi_ig_map *ig_map;
1531 	int i;
1532 
1533 	spdk_json_write_object_begin(w);
1534 
1535 	spdk_json_write_named_string(w, "name", target->name);
1536 
1537 	if (target->alias[0] != '\0') {
1538 		spdk_json_write_named_string(w, "alias_name", target->alias);
1539 	}
1540 
1541 	spdk_json_write_named_array_begin(w, "pg_ig_maps");
1542 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1543 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1544 			spdk_json_write_object_begin(w);
1545 			spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag);
1546 			spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag);
1547 			spdk_json_write_object_end(w);
1548 		}
1549 	}
1550 	spdk_json_write_array_end(w);
1551 
1552 	spdk_json_write_named_array_begin(w, "luns");
1553 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1554 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1555 
1556 		if (lun) {
1557 			spdk_json_write_object_begin(w);
1558 			spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1559 			spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun));
1560 			spdk_json_write_object_end(w);
1561 		}
1562 	}
1563 	spdk_json_write_array_end(w);
1564 
1565 	spdk_json_write_named_int32(w, "queue_depth", target->queue_depth);
1566 
1567 	spdk_json_write_named_bool(w, "disable_chap", target->disable_chap);
1568 	spdk_json_write_named_bool(w, "require_chap", target->require_chap);
1569 	spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap);
1570 	spdk_json_write_named_int32(w, "chap_group", target->chap_group);
1571 
1572 	spdk_json_write_named_bool(w, "header_digest", target->header_digest);
1573 	spdk_json_write_named_bool(w, "data_digest", target->data_digest);
1574 
1575 	spdk_json_write_object_end(w);
1576 }
1577 
1578 static void
1579 iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target,
1580 			   struct spdk_json_write_ctx *w)
1581 {
1582 	spdk_json_write_object_begin(w);
1583 
1584 	spdk_json_write_named_string(w, "method", "iscsi_create_target_node");
1585 
1586 	spdk_json_write_name(w, "params");
1587 	iscsi_tgt_node_info_json(target, w);
1588 
1589 	spdk_json_write_object_end(w);
1590 }
1591 
1592 void
1593 spdk_iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w)
1594 {
1595 	struct spdk_iscsi_tgt_node *target;
1596 
1597 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1598 		iscsi_tgt_node_info_json(target, w);
1599 	}
1600 }
1601 
1602 void
1603 spdk_iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w)
1604 {
1605 	struct spdk_iscsi_tgt_node *target;
1606 
1607 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1608 		iscsi_tgt_node_config_json(target, w);
1609 	}
1610 }
1611