xref: /spdk/lib/iscsi/tgt_node.c (revision ae7b5890ef728af40bd233a5011b924c482603bf)
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->portal->group->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_start_exit(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_tgt_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_tgt_node_delete_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_tgt_node_add_pg_ig_maps(target, pg_tag_list, ig_tag_list, num_maps);
997 	if (rc != 0) {
998 		SPDK_ERRLOG("could not add map to target\n");
999 		iscsi_tgt_node_destruct(target, NULL, NULL);
1000 		return NULL;
1001 	}
1002 
1003 	target->disable_chap = disable_chap;
1004 	target->require_chap = require_chap;
1005 	target->mutual_chap = mutual_chap;
1006 	target->chap_group = chap_group;
1007 	target->header_digest = header_digest;
1008 	target->data_digest = data_digest;
1009 
1010 	if (queue_depth > 0 && ((uint32_t)queue_depth <= g_spdk_iscsi.MaxQueueDepth)) {
1011 		target->queue_depth = queue_depth;
1012 	} else {
1013 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "QueueDepth %d is invalid and %d is used instead.\n",
1014 			      queue_depth, g_spdk_iscsi.MaxQueueDepth);
1015 		target->queue_depth = g_spdk_iscsi.MaxQueueDepth;
1016 	}
1017 
1018 	rc = iscsi_tgt_node_register(target);
1019 	if (rc != 0) {
1020 		SPDK_ERRLOG("register target is failed\n");
1021 		iscsi_tgt_node_destruct(target, NULL, NULL);
1022 		return NULL;
1023 	}
1024 
1025 	return target;
1026 }
1027 
1028 static int
1029 iscsi_parse_tgt_node(struct spdk_conf_section *sp)
1030 {
1031 	char buf[MAX_TMPBUF];
1032 	struct spdk_iscsi_tgt_node *target;
1033 	int pg_tag_list[MAX_TARGET_MAP], ig_tag_list[MAX_TARGET_MAP];
1034 	int num_target_maps;
1035 	const char *alias, *pg_tag, *ig_tag;
1036 	const char *ag_tag;
1037 	const char *val, *name;
1038 	int target_num, chap_group, pg_tag_i, ig_tag_i;
1039 	bool header_digest, data_digest;
1040 	bool disable_chap, require_chap, mutual_chap;
1041 	int i;
1042 	int lun_id_list[SPDK_SCSI_DEV_MAX_LUN];
1043 	const char *bdev_name_list[SPDK_SCSI_DEV_MAX_LUN];
1044 	int num_luns, queue_depth;
1045 
1046 	target_num = spdk_conf_section_get_num(sp);
1047 
1048 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "add unit %d\n", target_num);
1049 
1050 	data_digest = false;
1051 	header_digest = false;
1052 
1053 	name = spdk_conf_section_get_val(sp, "TargetName");
1054 
1055 	if (name == NULL) {
1056 		SPDK_ERRLOG("tgt_node%d: TargetName not found\n", target_num);
1057 		return -1;
1058 	}
1059 
1060 	alias = spdk_conf_section_get_val(sp, "TargetAlias");
1061 
1062 	/* Setup initiator and portal group mapping */
1063 	val = spdk_conf_section_get_val(sp, "Mapping");
1064 	if (val == NULL) {
1065 		/* no map */
1066 		SPDK_ERRLOG("tgt_node%d: no Mapping\n", target_num);
1067 		return -1;
1068 	}
1069 
1070 	for (i = 0; i < MAX_TARGET_MAP; i++) {
1071 		val = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1072 		if (val == NULL) {
1073 			break;
1074 		}
1075 		pg_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1076 		ig_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 1);
1077 		if (pg_tag == NULL || ig_tag == NULL) {
1078 			SPDK_ERRLOG("tgt_node%d: mapping error\n", target_num);
1079 			return -1;
1080 		}
1081 		if (strncasecmp(pg_tag, "PortalGroup",
1082 				strlen("PortalGroup")) != 0
1083 		    || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) {
1084 			SPDK_ERRLOG("tgt_node%d: mapping portal error\n", target_num);
1085 			return -1;
1086 		}
1087 		if (strncasecmp(ig_tag, "InitiatorGroup",
1088 				strlen("InitiatorGroup")) != 0
1089 		    || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) {
1090 			SPDK_ERRLOG("tgt_node%d: mapping initiator error\n", target_num);
1091 			return -1;
1092 		}
1093 		if (pg_tag_i < 1 || ig_tag_i < 1) {
1094 			SPDK_ERRLOG("tgt_node%d: invalid group tag\n", target_num);
1095 			return -1;
1096 		}
1097 		pg_tag_list[i] = pg_tag_i;
1098 		ig_tag_list[i] = ig_tag_i;
1099 	}
1100 
1101 	num_target_maps = i;
1102 
1103 	/* Setup AuthMethod */
1104 	val = spdk_conf_section_get_val(sp, "AuthMethod");
1105 	disable_chap = false;
1106 	require_chap = false;
1107 	mutual_chap = false;
1108 	if (val != NULL) {
1109 		for (i = 0; ; i++) {
1110 			val = spdk_conf_section_get_nmval(sp, "AuthMethod", 0, i);
1111 			if (val == NULL) {
1112 				break;
1113 			}
1114 			if (strcasecmp(val, "CHAP") == 0) {
1115 				require_chap = true;
1116 			} else if (strcasecmp(val, "Mutual") == 0) {
1117 				mutual_chap = true;
1118 			} else if (strcasecmp(val, "Auto") == 0) {
1119 				disable_chap = false;
1120 				require_chap = false;
1121 				mutual_chap = false;
1122 			} else if (strcasecmp(val, "None") == 0) {
1123 				disable_chap = true;
1124 				require_chap = false;
1125 				mutual_chap = false;
1126 			} else {
1127 				SPDK_ERRLOG("tgt_node%d: unknown auth\n", target_num);
1128 				return -1;
1129 			}
1130 		}
1131 		if (mutual_chap && !require_chap) {
1132 			SPDK_ERRLOG("tgt_node%d: Mutual but not CHAP\n", target_num);
1133 			return -1;
1134 		}
1135 	}
1136 	if (disable_chap) {
1137 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod None\n");
1138 	} else if (!require_chap) {
1139 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod Auto\n");
1140 	} else {
1141 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod CHAP %s\n",
1142 			      mutual_chap ? "Mutual" : "");
1143 	}
1144 
1145 	val = spdk_conf_section_get_val(sp, "AuthGroup");
1146 	if (val == NULL) {
1147 		chap_group = 0;
1148 	} else {
1149 		ag_tag = val;
1150 		if (strcasecmp(ag_tag, "None") == 0) {
1151 			chap_group = 0;
1152 		} else {
1153 			if (strncasecmp(ag_tag, "AuthGroup",
1154 					strlen("AuthGroup")) != 0
1155 			    || sscanf(ag_tag, "%*[^0-9]%d", &chap_group) != 1) {
1156 				SPDK_ERRLOG("tgt_node%d: auth group error\n", target_num);
1157 				return -1;
1158 			}
1159 			if (chap_group == 0) {
1160 				SPDK_ERRLOG("tgt_node%d: invalid auth group 0\n", target_num);
1161 				return -1;
1162 			}
1163 		}
1164 	}
1165 	if (chap_group == 0) {
1166 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup None\n");
1167 	} else {
1168 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup AuthGroup%d\n", chap_group);
1169 	}
1170 
1171 	val = spdk_conf_section_get_val(sp, "UseDigest");
1172 	if (val != NULL) {
1173 		for (i = 0; ; i++) {
1174 			val = spdk_conf_section_get_nmval(sp, "UseDigest", 0, i);
1175 			if (val == NULL) {
1176 				break;
1177 			}
1178 			if (strcasecmp(val, "Header") == 0) {
1179 				header_digest = true;
1180 			} else if (strcasecmp(val, "Data") == 0) {
1181 				data_digest = true;
1182 			} else if (strcasecmp(val, "Auto") == 0) {
1183 				header_digest = false;
1184 				data_digest = false;
1185 			} else {
1186 				SPDK_ERRLOG("tgt_node%d: unknown digest\n", target_num);
1187 				return -1;
1188 			}
1189 		}
1190 	}
1191 	if (!header_digest && !data_digest) {
1192 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest Auto\n");
1193 	} else {
1194 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest %s %s\n",
1195 			      header_digest ? "Header" : "",
1196 			      data_digest ? "Data" : "");
1197 	}
1198 
1199 	val = spdk_conf_section_get_val(sp, "QueueDepth");
1200 	if (val == NULL) {
1201 		queue_depth = g_spdk_iscsi.MaxQueueDepth;
1202 	} else {
1203 		queue_depth = (int) strtol(val, NULL, 10);
1204 	}
1205 
1206 	num_luns = 0;
1207 
1208 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1209 		snprintf(buf, sizeof(buf), "LUN%d", i);
1210 		val = spdk_conf_section_get_val(sp, buf);
1211 		if (val == NULL) {
1212 			continue;
1213 		}
1214 
1215 		bdev_name_list[num_luns] = val;
1216 		lun_id_list[num_luns] = i;
1217 		num_luns++;
1218 	}
1219 
1220 	if (num_luns == 0) {
1221 		SPDK_ERRLOG("tgt_node%d: No LUN specified for target %s.\n", target_num, name);
1222 		return -1;
1223 	}
1224 
1225 	target = spdk_iscsi_tgt_node_construct(target_num, name, alias,
1226 					       pg_tag_list, ig_tag_list, num_target_maps,
1227 					       bdev_name_list, lun_id_list, num_luns, queue_depth,
1228 					       disable_chap, require_chap, mutual_chap, chap_group,
1229 					       header_digest, data_digest);
1230 
1231 	if (target == NULL) {
1232 		SPDK_ERRLOG("tgt_node%d: add_iscsi_target_node error\n", target_num);
1233 		return -1;
1234 	}
1235 
1236 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1237 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1238 
1239 		if (lun) {
1240 			SPDK_INFOLOG(SPDK_LOG_ISCSI, "device %d: LUN%d %s\n",
1241 				     spdk_scsi_dev_get_id(target->dev),
1242 				     spdk_scsi_lun_get_id(lun),
1243 				     spdk_scsi_lun_get_bdev_name(lun));
1244 		}
1245 	}
1246 
1247 	return 0;
1248 }
1249 
1250 int spdk_iscsi_parse_tgt_nodes(void)
1251 {
1252 	struct spdk_conf_section *sp;
1253 	int rc;
1254 
1255 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_parse_tgt_nodes\n");
1256 
1257 	sp = spdk_conf_first_section(NULL);
1258 	while (sp != NULL) {
1259 		if (spdk_conf_section_match_prefix(sp, "TargetNode")) {
1260 			int tag = spdk_conf_section_get_num(sp);
1261 
1262 			if (tag > SPDK_TN_TAG_MAX) {
1263 				SPDK_ERRLOG("tag %d is invalid\n", tag);
1264 				return -1;
1265 			}
1266 			rc = iscsi_parse_tgt_node(sp);
1267 			if (rc < 0) {
1268 				SPDK_ERRLOG("spdk_iscsi_parse_tgt_node() failed\n");
1269 				return -1;
1270 			}
1271 		}
1272 		sp = spdk_conf_next_section(sp);
1273 	}
1274 	return 0;
1275 }
1276 
1277 void
1278 spdk_iscsi_shutdown_tgt_nodes(void)
1279 {
1280 	struct spdk_iscsi_tgt_node *target;
1281 
1282 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1283 	while (!TAILQ_EMPTY(&g_spdk_iscsi.target_head)) {
1284 		target = TAILQ_FIRST(&g_spdk_iscsi.target_head);
1285 		TAILQ_REMOVE(&g_spdk_iscsi.target_head, target, tailq);
1286 
1287 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1288 
1289 		iscsi_tgt_node_destruct(target, NULL, NULL);
1290 
1291 		pthread_mutex_lock(&g_spdk_iscsi.mutex);
1292 	}
1293 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1294 }
1295 
1296 void
1297 spdk_iscsi_shutdown_tgt_node_by_name(const char *target_name,
1298 				     iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
1299 {
1300 	struct spdk_iscsi_tgt_node *target;
1301 
1302 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1303 	target = spdk_iscsi_find_tgt_node(target_name);
1304 	if (target != NULL) {
1305 		iscsi_tgt_node_unregister(target);
1306 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1307 
1308 		iscsi_tgt_node_destruct(target, cb_fn, cb_arg);
1309 
1310 		return;
1311 	}
1312 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1313 
1314 	if (cb_fn) {
1315 		cb_fn(cb_arg, -ENOENT);
1316 	}
1317 }
1318 
1319 bool
1320 spdk_iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target)
1321 {
1322 	return target->destructed;
1323 }
1324 
1325 int
1326 spdk_iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1327 				 struct spdk_iscsi_tgt_node *target)
1328 {
1329 	int i;
1330 	struct spdk_iscsi_task *task;
1331 
1332 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1333 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1334 
1335 		if (!lun) {
1336 			continue;
1337 		}
1338 
1339 		/* we create a fake management task per LUN to cleanup */
1340 		task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_mgmt_cpl);
1341 		if (!task) {
1342 			SPDK_ERRLOG("Unable to acquire task\n");
1343 			return -1;
1344 		}
1345 
1346 		task->scsi.target_port = conn->target_port;
1347 		task->scsi.initiator_port = conn->initiator_port;
1348 		task->scsi.lun = lun;
1349 
1350 		spdk_iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
1351 	}
1352 
1353 	return 0;
1354 }
1355 
1356 void spdk_iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1357 				    struct spdk_iscsi_init_grp *initiator_group)
1358 {
1359 	struct spdk_iscsi_tgt_node *target;
1360 
1361 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1362 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1363 		if (portal_group) {
1364 			iscsi_tgt_node_delete_pg_map(target, portal_group);
1365 		}
1366 		if (initiator_group) {
1367 			iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1368 		}
1369 	}
1370 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1371 }
1372 
1373 int
1374 spdk_iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target,
1375 			    const char *bdev_name, int lun_id)
1376 {
1377 	struct spdk_scsi_dev *dev;
1378 	int rc;
1379 
1380 	if (target->num_active_conns > 0) {
1381 		SPDK_ERRLOG("Target has active connections (count=%d)\n",
1382 			    target->num_active_conns);
1383 		return -1;
1384 	}
1385 
1386 	if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
1387 		SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id);
1388 		return -1;
1389 	}
1390 
1391 	dev = target->dev;
1392 	if (dev == NULL) {
1393 		SPDK_ERRLOG("SCSI device is not found\n");
1394 		return -1;
1395 	}
1396 
1397 	rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL);
1398 	if (rc != 0) {
1399 		SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n");
1400 		return -1;
1401 	}
1402 
1403 	return 0;
1404 }
1405 
1406 int
1407 spdk_iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target,
1408 				    bool disable_chap, bool require_chap,
1409 				    bool mutual_chap, int32_t chap_group)
1410 {
1411 	if (!spdk_iscsi_check_chap_params(disable_chap, require_chap,
1412 					  mutual_chap, chap_group)) {
1413 		return -EINVAL;
1414 	}
1415 
1416 	pthread_mutex_lock(&target->mutex);
1417 	target->disable_chap = disable_chap;
1418 	target->require_chap = require_chap;
1419 	target->mutual_chap = mutual_chap;
1420 	target->chap_group = chap_group;
1421 	pthread_mutex_unlock(&target->mutex);
1422 
1423 	return 0;
1424 }
1425 
1426 static const char *target_nodes_section = \
1427 		"\n"
1428 		"# Users should change the TargetNode section(s) below to match the\n"
1429 		"#  desired iSCSI target node configuration.\n"
1430 		"# TargetName, Mapping, LUN0 are minimum required\n";
1431 
1432 #define TARGET_NODE_TMPL \
1433 "[TargetNode%d]\n" \
1434 "  Comment \"Target%d\"\n" \
1435 "  TargetName %s\n" \
1436 "  TargetAlias \"%s\"\n"
1437 
1438 #define TARGET_NODE_PGIG_MAPPING_TMPL \
1439 "  Mapping PortalGroup%d InitiatorGroup%d\n"
1440 
1441 #define TARGET_NODE_AUTH_TMPL \
1442 "  AuthMethod %s\n" \
1443 "  AuthGroup %s\n" \
1444 "  UseDigest %s\n"
1445 
1446 #define TARGET_NODE_QD_TMPL \
1447 "  QueueDepth %d\n\n"
1448 
1449 #define TARGET_NODE_LUN_TMPL \
1450 "  LUN%d %s\n"
1451 
1452 void
1453 spdk_iscsi_tgt_nodes_config_text(FILE *fp)
1454 {
1455 	int l = 0;
1456 	struct spdk_scsi_dev *dev = NULL;
1457 	struct spdk_iscsi_tgt_node *target = NULL;
1458 	struct spdk_iscsi_pg_map *pg_map;
1459 	struct spdk_iscsi_ig_map *ig_map;
1460 
1461 	/* Create target nodes section */
1462 	fprintf(fp, "%s", target_nodes_section);
1463 
1464 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1465 		int idx;
1466 		const char *authmethod = "None";
1467 		char authgroup[32] = "None";
1468 		const char *usedigest = "Auto";
1469 
1470 		dev = target->dev;
1471 		if (NULL == dev) { continue; }
1472 
1473 		idx = target->num;
1474 		fprintf(fp, TARGET_NODE_TMPL, idx, idx, target->name, spdk_scsi_dev_get_name(dev));
1475 
1476 		TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1477 			TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1478 				fprintf(fp, TARGET_NODE_PGIG_MAPPING_TMPL,
1479 					pg_map->pg->tag,
1480 					ig_map->ig->tag);
1481 			}
1482 		}
1483 
1484 		if (target->disable_chap) {
1485 			authmethod = "None";
1486 		} else if (!target->require_chap) {
1487 			authmethod = "Auto";
1488 		} else if (target->mutual_chap) {
1489 			authmethod = "CHAP Mutual";
1490 		} else {
1491 			authmethod = "CHAP";
1492 		}
1493 
1494 		if (target->chap_group > 0) {
1495 			snprintf(authgroup, sizeof(authgroup), "AuthGroup%d", target->chap_group);
1496 		}
1497 
1498 		if (target->header_digest) {
1499 			usedigest = "Header";
1500 		} else if (target->data_digest) {
1501 			usedigest = "Data";
1502 		}
1503 
1504 		fprintf(fp, TARGET_NODE_AUTH_TMPL,
1505 			authmethod, authgroup, usedigest);
1506 
1507 		for (l = 0; l < SPDK_SCSI_DEV_MAX_LUN; l++) {
1508 			struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(dev, l);
1509 
1510 			if (!lun) {
1511 				continue;
1512 			}
1513 
1514 			fprintf(fp, TARGET_NODE_LUN_TMPL,
1515 				spdk_scsi_lun_get_id(lun),
1516 				spdk_scsi_lun_get_bdev_name(lun));
1517 		}
1518 
1519 		fprintf(fp, TARGET_NODE_QD_TMPL,
1520 			target->queue_depth);
1521 	}
1522 }
1523 
1524 static void
1525 iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target,
1526 			 struct spdk_json_write_ctx *w)
1527 {
1528 	struct spdk_iscsi_pg_map *pg_map;
1529 	struct spdk_iscsi_ig_map *ig_map;
1530 	int i;
1531 
1532 	spdk_json_write_object_begin(w);
1533 
1534 	spdk_json_write_named_string(w, "name", target->name);
1535 
1536 	if (target->alias[0] != '\0') {
1537 		spdk_json_write_named_string(w, "alias_name", target->alias);
1538 	}
1539 
1540 	spdk_json_write_named_array_begin(w, "pg_ig_maps");
1541 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1542 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1543 			spdk_json_write_object_begin(w);
1544 			spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag);
1545 			spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag);
1546 			spdk_json_write_object_end(w);
1547 		}
1548 	}
1549 	spdk_json_write_array_end(w);
1550 
1551 	spdk_json_write_named_array_begin(w, "luns");
1552 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1553 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1554 
1555 		if (lun) {
1556 			spdk_json_write_object_begin(w);
1557 			spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1558 			spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun));
1559 			spdk_json_write_object_end(w);
1560 		}
1561 	}
1562 	spdk_json_write_array_end(w);
1563 
1564 	spdk_json_write_named_int32(w, "queue_depth", target->queue_depth);
1565 
1566 	spdk_json_write_named_bool(w, "disable_chap", target->disable_chap);
1567 	spdk_json_write_named_bool(w, "require_chap", target->require_chap);
1568 	spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap);
1569 	spdk_json_write_named_int32(w, "chap_group", target->chap_group);
1570 
1571 	spdk_json_write_named_bool(w, "header_digest", target->header_digest);
1572 	spdk_json_write_named_bool(w, "data_digest", target->data_digest);
1573 
1574 	spdk_json_write_object_end(w);
1575 }
1576 
1577 static void
1578 iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target,
1579 			   struct spdk_json_write_ctx *w)
1580 {
1581 	spdk_json_write_object_begin(w);
1582 
1583 	spdk_json_write_named_string(w, "method", "construct_target_node");
1584 
1585 	spdk_json_write_name(w, "params");
1586 	iscsi_tgt_node_info_json(target, w);
1587 
1588 	spdk_json_write_object_end(w);
1589 }
1590 
1591 void
1592 spdk_iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w)
1593 {
1594 	struct spdk_iscsi_tgt_node *target;
1595 
1596 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1597 		iscsi_tgt_node_info_json(target, w);
1598 	}
1599 }
1600 
1601 void
1602 spdk_iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w)
1603 {
1604 	struct spdk_iscsi_tgt_node *target;
1605 
1606 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1607 		iscsi_tgt_node_config_json(target, w);
1608 	}
1609 }
1610