xref: /spdk/lib/iscsi/tgt_node.c (revision c4d9daeb7bf491bc0eb6e8d417b75d44773cb009)
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 	free(target->name);
642 	free(target->alias);
643 
644 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
645 	iscsi_tgt_node_delete_all_pg_maps(target);
646 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
647 
648 	pthread_mutex_destroy(&target->mutex);
649 	free(target);
650 
651 	if (destruct_cb_fn) {
652 		destruct_cb_fn(destruct_cb_arg, 0);
653 	}
654 }
655 
656 static int
657 iscsi_tgt_node_check_active_conns(void *arg)
658 {
659 	struct spdk_iscsi_tgt_node *target = arg;
660 
661 	if (spdk_iscsi_get_active_conns(target) != 0) {
662 		return 1;
663 	}
664 
665 	spdk_poller_unregister(&target->destruct_poller);
666 
667 	spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
668 
669 	return 1;
670 }
671 
672 static void
673 iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target,
674 			iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
675 {
676 	if (target == NULL) {
677 		if (cb_fn) {
678 			cb_fn(cb_arg, -ENOENT);
679 		}
680 		return;
681 	}
682 
683 	if (target->destructed) {
684 		SPDK_ERRLOG("Destructing %s is already started\n", target->name);
685 		if (cb_fn) {
686 			cb_fn(cb_arg, -EBUSY);
687 		}
688 		return;
689 	}
690 
691 	target->destructed = true;
692 	target->destruct_cb_fn = cb_fn;
693 	target->destruct_cb_arg = cb_arg;
694 
695 	spdk_iscsi_conns_start_exit(target);
696 
697 	if (spdk_iscsi_get_active_conns(target) != 0) {
698 		target->destruct_poller = spdk_poller_register(iscsi_tgt_node_check_active_conns,
699 					  target, 10);
700 	} else {
701 		spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
702 	}
703 
704 }
705 
706 static int
707 iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target,
708 				int pg_tag, int ig_tag)
709 {
710 	struct spdk_iscsi_portal_grp	*pg;
711 	struct spdk_iscsi_init_grp	*ig;
712 	struct spdk_iscsi_pg_map	*pg_map;
713 	struct spdk_iscsi_ig_map	*ig_map;
714 
715 	pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag);
716 	if (pg == NULL) {
717 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
718 		return -ENOENT;
719 	}
720 	ig = spdk_iscsi_init_grp_find_by_tag(ig_tag);
721 	if (ig == NULL) {
722 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
723 		return -ENOENT;
724 	}
725 
726 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
727 	if (pg_map == NULL) {
728 		SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag);
729 		return -ENOENT;
730 	}
731 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
732 	if (ig_map == NULL) {
733 		SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag);
734 		return -ENOENT;
735 	}
736 
737 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
738 	if (pg_map->num_ig_maps == 0) {
739 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
740 	}
741 
742 	return 0;
743 }
744 
745 static int
746 iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target,
747 			     int pg_tag, int ig_tag)
748 {
749 	struct spdk_iscsi_portal_grp	*pg;
750 	struct spdk_iscsi_pg_map	*pg_map;
751 	struct spdk_iscsi_init_grp	*ig;
752 	struct spdk_iscsi_ig_map	*ig_map;
753 	bool				new_pg_map = false;
754 
755 	pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag);
756 	if (pg == NULL) {
757 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
758 		return -ENOENT;
759 	}
760 	ig = spdk_iscsi_init_grp_find_by_tag(ig_tag);
761 	if (ig == NULL) {
762 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
763 		return -ENOENT;
764 	}
765 
766 	/* get existing pg_map or create new pg_map and add it to target */
767 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
768 	if (pg_map == NULL) {
769 		pg_map = iscsi_tgt_node_add_pg_map(target, pg);
770 		if (pg_map == NULL) {
771 			goto failed;
772 		}
773 		new_pg_map = true;
774 	}
775 
776 	/* create new ig_map and add it to pg_map */
777 	ig_map = iscsi_pg_map_add_ig_map(pg_map, ig);
778 	if (ig_map == NULL) {
779 		goto failed;
780 	}
781 
782 	return 0;
783 
784 failed:
785 	if (new_pg_map) {
786 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
787 	}
788 
789 	return -1;
790 }
791 
792 int
793 spdk_iscsi_tgt_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
794 				   int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
795 {
796 	uint16_t i;
797 	int rc;
798 
799 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
800 	for (i = 0; i < num_maps; i++) {
801 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i],
802 						  ig_tag_list[i]);
803 		if (rc != 0) {
804 			SPDK_ERRLOG("could not add map to target\n");
805 			goto invalid;
806 		}
807 	}
808 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
809 	return 0;
810 
811 invalid:
812 	for (; i > 0; --i) {
813 		iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1],
814 						ig_tag_list[i - 1]);
815 	}
816 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
817 	return -1;
818 }
819 
820 int
821 spdk_iscsi_tgt_node_delete_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
822 				      int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
823 {
824 	uint16_t i;
825 	int rc;
826 
827 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
828 	for (i = 0; i < num_maps; i++) {
829 		rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i],
830 						     ig_tag_list[i]);
831 		if (rc != 0) {
832 			SPDK_ERRLOG("could not delete map from target\n");
833 			goto invalid;
834 		}
835 	}
836 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
837 	return 0;
838 
839 invalid:
840 	for (; i > 0; --i) {
841 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1],
842 						  ig_tag_list[i - 1]);
843 		if (rc != 0) {
844 			iscsi_tgt_node_delete_all_pg_maps(target);
845 			break;
846 		}
847 	}
848 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
849 	return -1;
850 }
851 
852 static int
853 check_iscsi_name(const char *name)
854 {
855 	const unsigned char *up = (const unsigned char *) name;
856 	size_t n;
857 
858 	/* valid iSCSI name no larger than 223 bytes */
859 	if (strlen(name) > MAX_TARGET_NAME) {
860 		return -1;
861 	}
862 
863 	/* valid iSCSI name? */
864 	for (n = 0; up[n] != 0; n++) {
865 		if (up[n] > 0x00U && up[n] <= 0x2cU) {
866 			return -1;
867 		}
868 		if (up[n] == 0x2fU) {
869 			return -1;
870 		}
871 		if (up[n] >= 0x3bU && up[n] <= 0x40U) {
872 			return -1;
873 		}
874 		if (up[n] >= 0x5bU && up[n] <= 0x60U) {
875 			return -1;
876 		}
877 		if (up[n] >= 0x7bU && up[n] <= 0x7fU) {
878 			return -1;
879 		}
880 		if (isspace(up[n])) {
881 			return -1;
882 		}
883 	}
884 	/* valid format? */
885 	if (strncasecmp(name, "iqn.", 4) == 0) {
886 		/* iqn.YYYY-MM.reversed.domain.name */
887 		if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
888 		    || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
889 		    || !isdigit(up[10]) || up[11] != '.') {
890 			SPDK_ERRLOG("invalid iqn format. "
891 				    "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
892 			return -1;
893 		}
894 	} else if (strncasecmp(name, "eui.", 4) == 0) {
895 		/* EUI-64 -> 16bytes */
896 		/* XXX */
897 	} else if (strncasecmp(name, "naa.", 4) == 0) {
898 		/* 64bit -> 16bytes, 128bit -> 32bytes */
899 		/* XXX */
900 	}
901 	/* OK */
902 	return 0;
903 }
904 
905 bool
906 spdk_iscsi_check_chap_params(bool disable, bool require, bool mutual, int group)
907 {
908 	if (group < 0) {
909 		SPDK_ERRLOG("Invalid auth group ID (%d)\n", group);
910 		return false;
911 	}
912 	if ((!disable && !require && !mutual) ||	/* Auto */
913 	    (disable && !require && !mutual) ||	/* None */
914 	    (!disable && require && !mutual) ||	/* CHAP */
915 	    (!disable && require && mutual)) {	/* CHAP Mutual */
916 		return true;
917 	}
918 	SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n",
919 		    disable, require, mutual);
920 	return false;
921 }
922 
923 _spdk_iscsi_tgt_node *
924 spdk_iscsi_tgt_node_construct(int target_index,
925 			      const char *name, const char *alias,
926 			      int *pg_tag_list, int *ig_tag_list, uint16_t num_maps,
927 			      const char *bdev_name_list[], int *lun_id_list, int num_luns,
928 			      int queue_depth,
929 			      bool disable_chap, bool require_chap, bool mutual_chap, int chap_group,
930 			      bool header_digest, bool data_digest)
931 {
932 	char				fullname[MAX_TMPBUF];
933 	struct spdk_iscsi_tgt_node	*target;
934 	int				rc;
935 
936 	if (!spdk_iscsi_check_chap_params(disable_chap, require_chap,
937 					  mutual_chap, chap_group)) {
938 		return NULL;
939 	}
940 
941 	if (num_maps == 0) {
942 		SPDK_ERRLOG("num_maps = 0\n");
943 		return NULL;
944 	}
945 
946 	if (name == NULL) {
947 		SPDK_ERRLOG("TargetName not found\n");
948 		return NULL;
949 	}
950 
951 	if (strncasecmp(name, "iqn.", 4) != 0
952 	    && strncasecmp(name, "eui.", 4) != 0
953 	    && strncasecmp(name, "naa.", 4) != 0) {
954 		snprintf(fullname, sizeof(fullname), "%s:%s", g_spdk_iscsi.nodebase, name);
955 	} else {
956 		snprintf(fullname, sizeof(fullname), "%s", name);
957 	}
958 
959 	if (check_iscsi_name(fullname) != 0) {
960 		SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n",
961 			    name);
962 		return NULL;
963 	}
964 
965 	target = malloc(sizeof(*target));
966 	if (!target) {
967 		SPDK_ERRLOG("could not allocate target\n");
968 		return NULL;
969 	}
970 
971 	memset(target, 0, sizeof(*target));
972 
973 	rc = pthread_mutex_init(&target->mutex, NULL);
974 	if (rc != 0) {
975 		SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num);
976 		iscsi_tgt_node_destruct(target, NULL, NULL);
977 		return NULL;
978 	}
979 
980 	target->num = target_index;
981 
982 	target->name = strdup(fullname);
983 	if (!target->name) {
984 		SPDK_ERRLOG("Could not allocate TargetName\n");
985 		iscsi_tgt_node_destruct(target, NULL, NULL);
986 		return NULL;
987 	}
988 
989 	if (alias == NULL) {
990 		target->alias = NULL;
991 	} else {
992 		target->alias = strdup(alias);
993 		if (!target->alias) {
994 			SPDK_ERRLOG("Could not allocate TargetAlias\n");
995 			iscsi_tgt_node_destruct(target, NULL, NULL);
996 			return NULL;
997 		}
998 	}
999 
1000 	target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns,
1001 					      SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL);
1002 	if (!target->dev) {
1003 		SPDK_ERRLOG("Could not construct SCSI device\n");
1004 		iscsi_tgt_node_destruct(target, NULL, NULL);
1005 		return NULL;
1006 	}
1007 
1008 	TAILQ_INIT(&target->pg_map_head);
1009 	rc = spdk_iscsi_tgt_node_add_pg_ig_maps(target, pg_tag_list, ig_tag_list, num_maps);
1010 	if (rc != 0) {
1011 		SPDK_ERRLOG("could not add map to target\n");
1012 		iscsi_tgt_node_destruct(target, NULL, NULL);
1013 		return NULL;
1014 	}
1015 
1016 	target->disable_chap = disable_chap;
1017 	target->require_chap = require_chap;
1018 	target->mutual_chap = mutual_chap;
1019 	target->chap_group = chap_group;
1020 	target->header_digest = header_digest;
1021 	target->data_digest = data_digest;
1022 
1023 	if (queue_depth > 0 && ((uint32_t)queue_depth <= g_spdk_iscsi.MaxQueueDepth)) {
1024 		target->queue_depth = queue_depth;
1025 	} else {
1026 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "QueueDepth %d is invalid and %d is used instead.\n",
1027 			      queue_depth, g_spdk_iscsi.MaxQueueDepth);
1028 		target->queue_depth = g_spdk_iscsi.MaxQueueDepth;
1029 	}
1030 
1031 	rc = iscsi_tgt_node_register(target);
1032 	if (rc != 0) {
1033 		SPDK_ERRLOG("register target is failed\n");
1034 		iscsi_tgt_node_destruct(target, NULL, NULL);
1035 		return NULL;
1036 	}
1037 
1038 	return target;
1039 }
1040 
1041 static int
1042 iscsi_parse_tgt_node(struct spdk_conf_section *sp)
1043 {
1044 	char buf[MAX_TMPBUF];
1045 	struct spdk_iscsi_tgt_node *target;
1046 	int pg_tag_list[MAX_TARGET_MAP], ig_tag_list[MAX_TARGET_MAP];
1047 	int num_target_maps;
1048 	const char *alias, *pg_tag, *ig_tag;
1049 	const char *ag_tag;
1050 	const char *val, *name;
1051 	int target_num, chap_group, pg_tag_i, ig_tag_i;
1052 	bool header_digest, data_digest;
1053 	bool disable_chap, require_chap, mutual_chap;
1054 	int i;
1055 	int lun_id_list[SPDK_SCSI_DEV_MAX_LUN];
1056 	const char *bdev_name_list[SPDK_SCSI_DEV_MAX_LUN];
1057 	int num_luns, queue_depth;
1058 
1059 	target_num = spdk_conf_section_get_num(sp);
1060 
1061 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "add unit %d\n", target_num);
1062 
1063 	data_digest = false;
1064 	header_digest = false;
1065 
1066 	name = spdk_conf_section_get_val(sp, "TargetName");
1067 
1068 	if (name == NULL) {
1069 		SPDK_ERRLOG("tgt_node%d: TargetName not found\n", target_num);
1070 		return -1;
1071 	}
1072 
1073 	alias = spdk_conf_section_get_val(sp, "TargetAlias");
1074 
1075 	/* Setup initiator and portal group mapping */
1076 	val = spdk_conf_section_get_val(sp, "Mapping");
1077 	if (val == NULL) {
1078 		/* no map */
1079 		SPDK_ERRLOG("tgt_node%d: no Mapping\n", target_num);
1080 		return -1;
1081 	}
1082 
1083 	for (i = 0; i < MAX_TARGET_MAP; i++) {
1084 		val = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1085 		if (val == NULL) {
1086 			break;
1087 		}
1088 		pg_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1089 		ig_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 1);
1090 		if (pg_tag == NULL || ig_tag == NULL) {
1091 			SPDK_ERRLOG("tgt_node%d: mapping error\n", target_num);
1092 			return -1;
1093 		}
1094 		if (strncasecmp(pg_tag, "PortalGroup",
1095 				strlen("PortalGroup")) != 0
1096 		    || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) {
1097 			SPDK_ERRLOG("tgt_node%d: mapping portal error\n", target_num);
1098 			return -1;
1099 		}
1100 		if (strncasecmp(ig_tag, "InitiatorGroup",
1101 				strlen("InitiatorGroup")) != 0
1102 		    || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) {
1103 			SPDK_ERRLOG("tgt_node%d: mapping initiator error\n", target_num);
1104 			return -1;
1105 		}
1106 		if (pg_tag_i < 1 || ig_tag_i < 1) {
1107 			SPDK_ERRLOG("tgt_node%d: invalid group tag\n", target_num);
1108 			return -1;
1109 		}
1110 		pg_tag_list[i] = pg_tag_i;
1111 		ig_tag_list[i] = ig_tag_i;
1112 	}
1113 
1114 	num_target_maps = i;
1115 
1116 	/* Setup AuthMethod */
1117 	val = spdk_conf_section_get_val(sp, "AuthMethod");
1118 	disable_chap = false;
1119 	require_chap = false;
1120 	mutual_chap = false;
1121 	if (val != NULL) {
1122 		for (i = 0; ; i++) {
1123 			val = spdk_conf_section_get_nmval(sp, "AuthMethod", 0, i);
1124 			if (val == NULL) {
1125 				break;
1126 			}
1127 			if (strcasecmp(val, "CHAP") == 0) {
1128 				require_chap = true;
1129 			} else if (strcasecmp(val, "Mutual") == 0) {
1130 				mutual_chap = true;
1131 			} else if (strcasecmp(val, "Auto") == 0) {
1132 				disable_chap = false;
1133 				require_chap = false;
1134 				mutual_chap = false;
1135 			} else if (strcasecmp(val, "None") == 0) {
1136 				disable_chap = true;
1137 				require_chap = false;
1138 				mutual_chap = false;
1139 			} else {
1140 				SPDK_ERRLOG("tgt_node%d: unknown auth\n", target_num);
1141 				return -1;
1142 			}
1143 		}
1144 		if (mutual_chap && !require_chap) {
1145 			SPDK_ERRLOG("tgt_node%d: Mutual but not CHAP\n", target_num);
1146 			return -1;
1147 		}
1148 	}
1149 	if (disable_chap) {
1150 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod None\n");
1151 	} else if (!require_chap) {
1152 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod Auto\n");
1153 	} else {
1154 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod CHAP %s\n",
1155 			      mutual_chap ? "Mutual" : "");
1156 	}
1157 
1158 	val = spdk_conf_section_get_val(sp, "AuthGroup");
1159 	if (val == NULL) {
1160 		chap_group = 0;
1161 	} else {
1162 		ag_tag = val;
1163 		if (strcasecmp(ag_tag, "None") == 0) {
1164 			chap_group = 0;
1165 		} else {
1166 			if (strncasecmp(ag_tag, "AuthGroup",
1167 					strlen("AuthGroup")) != 0
1168 			    || sscanf(ag_tag, "%*[^0-9]%d", &chap_group) != 1) {
1169 				SPDK_ERRLOG("tgt_node%d: auth group error\n", target_num);
1170 				return -1;
1171 			}
1172 			if (chap_group == 0) {
1173 				SPDK_ERRLOG("tgt_node%d: invalid auth group 0\n", target_num);
1174 				return -1;
1175 			}
1176 		}
1177 	}
1178 	if (chap_group == 0) {
1179 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup None\n");
1180 	} else {
1181 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup AuthGroup%d\n", chap_group);
1182 	}
1183 
1184 	val = spdk_conf_section_get_val(sp, "UseDigest");
1185 	if (val != NULL) {
1186 		for (i = 0; ; i++) {
1187 			val = spdk_conf_section_get_nmval(sp, "UseDigest", 0, i);
1188 			if (val == NULL) {
1189 				break;
1190 			}
1191 			if (strcasecmp(val, "Header") == 0) {
1192 				header_digest = true;
1193 			} else if (strcasecmp(val, "Data") == 0) {
1194 				data_digest = true;
1195 			} else if (strcasecmp(val, "Auto") == 0) {
1196 				header_digest = false;
1197 				data_digest = false;
1198 			} else {
1199 				SPDK_ERRLOG("tgt_node%d: unknown digest\n", target_num);
1200 				return -1;
1201 			}
1202 		}
1203 	}
1204 	if (!header_digest && !data_digest) {
1205 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest Auto\n");
1206 	} else {
1207 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest %s %s\n",
1208 			      header_digest ? "Header" : "",
1209 			      data_digest ? "Data" : "");
1210 	}
1211 
1212 	val = spdk_conf_section_get_val(sp, "QueueDepth");
1213 	if (val == NULL) {
1214 		queue_depth = g_spdk_iscsi.MaxQueueDepth;
1215 	} else {
1216 		queue_depth = (int) strtol(val, NULL, 10);
1217 	}
1218 
1219 	num_luns = 0;
1220 
1221 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1222 		snprintf(buf, sizeof(buf), "LUN%d", i);
1223 		val = spdk_conf_section_get_val(sp, buf);
1224 		if (val == NULL) {
1225 			continue;
1226 		}
1227 
1228 		bdev_name_list[num_luns] = val;
1229 		lun_id_list[num_luns] = i;
1230 		num_luns++;
1231 	}
1232 
1233 	if (num_luns == 0) {
1234 		SPDK_ERRLOG("tgt_node%d: No LUN specified for target %s.\n", target_num, name);
1235 		return -1;
1236 	}
1237 
1238 	target = spdk_iscsi_tgt_node_construct(target_num, name, alias,
1239 					       pg_tag_list, ig_tag_list, num_target_maps,
1240 					       bdev_name_list, lun_id_list, num_luns, queue_depth,
1241 					       disable_chap, require_chap, mutual_chap, chap_group,
1242 					       header_digest, data_digest);
1243 
1244 	if (target == NULL) {
1245 		SPDK_ERRLOG("tgt_node%d: add_iscsi_target_node error\n", target_num);
1246 		return -1;
1247 	}
1248 
1249 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1250 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1251 
1252 		if (lun) {
1253 			SPDK_INFOLOG(SPDK_LOG_ISCSI, "device %d: LUN%d %s\n",
1254 				     spdk_scsi_dev_get_id(target->dev),
1255 				     spdk_scsi_lun_get_id(lun),
1256 				     spdk_scsi_lun_get_bdev_name(lun));
1257 		}
1258 	}
1259 
1260 	return 0;
1261 }
1262 
1263 int spdk_iscsi_parse_tgt_nodes(void)
1264 {
1265 	struct spdk_conf_section *sp;
1266 	int rc;
1267 
1268 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_parse_tgt_nodes\n");
1269 
1270 	sp = spdk_conf_first_section(NULL);
1271 	while (sp != NULL) {
1272 		if (spdk_conf_section_match_prefix(sp, "TargetNode")) {
1273 			int tag = spdk_conf_section_get_num(sp);
1274 
1275 			if (tag > SPDK_TN_TAG_MAX) {
1276 				SPDK_ERRLOG("tag %d is invalid\n", tag);
1277 				return -1;
1278 			}
1279 			rc = iscsi_parse_tgt_node(sp);
1280 			if (rc < 0) {
1281 				SPDK_ERRLOG("spdk_iscsi_parse_tgt_node() failed\n");
1282 				return -1;
1283 			}
1284 		}
1285 		sp = spdk_conf_next_section(sp);
1286 	}
1287 	return 0;
1288 }
1289 
1290 void
1291 spdk_iscsi_shutdown_tgt_nodes(void)
1292 {
1293 	struct spdk_iscsi_tgt_node *target;
1294 
1295 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1296 	while (!TAILQ_EMPTY(&g_spdk_iscsi.target_head)) {
1297 		target = TAILQ_FIRST(&g_spdk_iscsi.target_head);
1298 		TAILQ_REMOVE(&g_spdk_iscsi.target_head, target, tailq);
1299 
1300 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1301 
1302 		iscsi_tgt_node_destruct(target, NULL, NULL);
1303 
1304 		pthread_mutex_lock(&g_spdk_iscsi.mutex);
1305 	}
1306 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1307 }
1308 
1309 void
1310 spdk_iscsi_shutdown_tgt_node_by_name(const char *target_name,
1311 				     iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
1312 {
1313 	struct spdk_iscsi_tgt_node *target;
1314 
1315 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1316 	target = spdk_iscsi_find_tgt_node(target_name);
1317 	if (target != NULL) {
1318 		iscsi_tgt_node_unregister(target);
1319 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1320 
1321 		iscsi_tgt_node_destruct(target, cb_fn, cb_arg);
1322 
1323 		return;
1324 	}
1325 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1326 
1327 	if (cb_fn) {
1328 		cb_fn(cb_arg, -ENOENT);
1329 	}
1330 }
1331 
1332 bool
1333 spdk_iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target)
1334 {
1335 	return target->destructed;
1336 }
1337 
1338 int
1339 spdk_iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1340 				 struct spdk_iscsi_tgt_node *target)
1341 {
1342 	int i;
1343 	struct spdk_iscsi_task *task;
1344 
1345 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1346 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1347 
1348 		if (!lun) {
1349 			continue;
1350 		}
1351 
1352 		/* we create a fake management task per LUN to cleanup */
1353 		task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_mgmt_cpl);
1354 		if (!task) {
1355 			SPDK_ERRLOG("Unable to acquire task\n");
1356 			return -1;
1357 		}
1358 
1359 		task->scsi.target_port = conn->target_port;
1360 		task->scsi.initiator_port = conn->initiator_port;
1361 		task->scsi.lun = lun;
1362 
1363 		spdk_iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
1364 	}
1365 
1366 	return 0;
1367 }
1368 
1369 void spdk_iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1370 				    struct spdk_iscsi_init_grp *initiator_group)
1371 {
1372 	struct spdk_iscsi_tgt_node *target;
1373 
1374 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1375 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1376 		if (portal_group) {
1377 			iscsi_tgt_node_delete_pg_map(target, portal_group);
1378 		}
1379 		if (initiator_group) {
1380 			iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1381 		}
1382 	}
1383 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1384 }
1385 
1386 int
1387 spdk_iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target,
1388 			    const char *bdev_name, int lun_id)
1389 {
1390 	struct spdk_scsi_dev *dev;
1391 	int rc;
1392 
1393 	if (target->num_active_conns > 0) {
1394 		SPDK_ERRLOG("Target has active connections (count=%d)\n",
1395 			    target->num_active_conns);
1396 		return -1;
1397 	}
1398 
1399 	if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
1400 		SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id);
1401 		return -1;
1402 	}
1403 
1404 	dev = target->dev;
1405 	if (dev == NULL) {
1406 		SPDK_ERRLOG("SCSI device is not found\n");
1407 		return -1;
1408 	}
1409 
1410 	rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL);
1411 	if (rc != 0) {
1412 		SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n");
1413 		return -1;
1414 	}
1415 
1416 	return 0;
1417 }
1418 
1419 int
1420 spdk_iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target,
1421 				    bool disable_chap, bool require_chap,
1422 				    bool mutual_chap, int32_t chap_group)
1423 {
1424 	if (!spdk_iscsi_check_chap_params(disable_chap, require_chap,
1425 					  mutual_chap, chap_group)) {
1426 		return -EINVAL;
1427 	}
1428 
1429 	pthread_mutex_lock(&target->mutex);
1430 	target->disable_chap = disable_chap;
1431 	target->require_chap = require_chap;
1432 	target->mutual_chap = mutual_chap;
1433 	target->chap_group = chap_group;
1434 	pthread_mutex_unlock(&target->mutex);
1435 
1436 	return 0;
1437 }
1438 
1439 static const char *target_nodes_section = \
1440 		"\n"
1441 		"# Users should change the TargetNode section(s) below to match the\n"
1442 		"#  desired iSCSI target node configuration.\n"
1443 		"# TargetName, Mapping, LUN0 are minimum required\n";
1444 
1445 #define TARGET_NODE_TMPL \
1446 "[TargetNode%d]\n" \
1447 "  Comment \"Target%d\"\n" \
1448 "  TargetName %s\n" \
1449 "  TargetAlias \"%s\"\n"
1450 
1451 #define TARGET_NODE_PGIG_MAPPING_TMPL \
1452 "  Mapping PortalGroup%d InitiatorGroup%d\n"
1453 
1454 #define TARGET_NODE_AUTH_TMPL \
1455 "  AuthMethod %s\n" \
1456 "  AuthGroup %s\n" \
1457 "  UseDigest %s\n"
1458 
1459 #define TARGET_NODE_QD_TMPL \
1460 "  QueueDepth %d\n\n"
1461 
1462 #define TARGET_NODE_LUN_TMPL \
1463 "  LUN%d %s\n"
1464 
1465 void
1466 spdk_iscsi_tgt_nodes_config_text(FILE *fp)
1467 {
1468 	int l = 0;
1469 	struct spdk_scsi_dev *dev = NULL;
1470 	struct spdk_iscsi_tgt_node *target = NULL;
1471 	struct spdk_iscsi_pg_map *pg_map;
1472 	struct spdk_iscsi_ig_map *ig_map;
1473 
1474 	/* Create target nodes section */
1475 	fprintf(fp, "%s", target_nodes_section);
1476 
1477 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1478 		int idx;
1479 		const char *authmethod = "None";
1480 		char authgroup[32] = "None";
1481 		const char *usedigest = "Auto";
1482 
1483 		dev = target->dev;
1484 		if (NULL == dev) { continue; }
1485 
1486 		idx = target->num;
1487 		fprintf(fp, TARGET_NODE_TMPL, idx, idx, target->name, spdk_scsi_dev_get_name(dev));
1488 
1489 		TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1490 			TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1491 				fprintf(fp, TARGET_NODE_PGIG_MAPPING_TMPL,
1492 					pg_map->pg->tag,
1493 					ig_map->ig->tag);
1494 			}
1495 		}
1496 
1497 		if (target->disable_chap) {
1498 			authmethod = "None";
1499 		} else if (!target->require_chap) {
1500 			authmethod = "Auto";
1501 		} else if (target->mutual_chap) {
1502 			authmethod = "CHAP Mutual";
1503 		} else {
1504 			authmethod = "CHAP";
1505 		}
1506 
1507 		if (target->chap_group > 0) {
1508 			snprintf(authgroup, sizeof(authgroup), "AuthGroup%d", target->chap_group);
1509 		}
1510 
1511 		if (target->header_digest) {
1512 			usedigest = "Header";
1513 		} else if (target->data_digest) {
1514 			usedigest = "Data";
1515 		}
1516 
1517 		fprintf(fp, TARGET_NODE_AUTH_TMPL,
1518 			authmethod, authgroup, usedigest);
1519 
1520 		for (l = 0; l < SPDK_SCSI_DEV_MAX_LUN; l++) {
1521 			struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(dev, l);
1522 
1523 			if (!lun) {
1524 				continue;
1525 			}
1526 
1527 			fprintf(fp, TARGET_NODE_LUN_TMPL,
1528 				spdk_scsi_lun_get_id(lun),
1529 				spdk_scsi_lun_get_bdev_name(lun));
1530 		}
1531 
1532 		fprintf(fp, TARGET_NODE_QD_TMPL,
1533 			target->queue_depth);
1534 	}
1535 }
1536 
1537 static void
1538 iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target,
1539 			 struct spdk_json_write_ctx *w)
1540 {
1541 	struct spdk_iscsi_pg_map *pg_map;
1542 	struct spdk_iscsi_ig_map *ig_map;
1543 	int i;
1544 
1545 	spdk_json_write_object_begin(w);
1546 
1547 	spdk_json_write_named_string(w, "name", target->name);
1548 
1549 	if (target->alias) {
1550 		spdk_json_write_named_string(w, "alias_name", target->alias);
1551 	}
1552 
1553 	spdk_json_write_named_array_begin(w, "pg_ig_maps");
1554 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1555 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1556 			spdk_json_write_object_begin(w);
1557 			spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag);
1558 			spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag);
1559 			spdk_json_write_object_end(w);
1560 		}
1561 	}
1562 	spdk_json_write_array_end(w);
1563 
1564 	spdk_json_write_named_array_begin(w, "luns");
1565 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1566 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1567 
1568 		if (lun) {
1569 			spdk_json_write_object_begin(w);
1570 			spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1571 			spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun));
1572 			spdk_json_write_object_end(w);
1573 		}
1574 	}
1575 	spdk_json_write_array_end(w);
1576 
1577 	spdk_json_write_named_int32(w, "queue_depth", target->queue_depth);
1578 
1579 	spdk_json_write_named_bool(w, "disable_chap", target->disable_chap);
1580 	spdk_json_write_named_bool(w, "require_chap", target->require_chap);
1581 	spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap);
1582 	spdk_json_write_named_int32(w, "chap_group", target->chap_group);
1583 
1584 	spdk_json_write_named_bool(w, "header_digest", target->header_digest);
1585 	spdk_json_write_named_bool(w, "data_digest", target->data_digest);
1586 
1587 	spdk_json_write_object_end(w);
1588 }
1589 
1590 static void
1591 iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target,
1592 			   struct spdk_json_write_ctx *w)
1593 {
1594 	spdk_json_write_object_begin(w);
1595 
1596 	spdk_json_write_named_string(w, "method", "construct_target_node");
1597 
1598 	spdk_json_write_name(w, "params");
1599 	iscsi_tgt_node_info_json(target, w);
1600 
1601 	spdk_json_write_object_end(w);
1602 }
1603 
1604 void
1605 spdk_iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w)
1606 {
1607 	struct spdk_iscsi_tgt_node *target;
1608 
1609 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1610 		iscsi_tgt_node_info_json(target, w);
1611 	}
1612 }
1613 
1614 void
1615 spdk_iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w)
1616 {
1617 	struct spdk_iscsi_tgt_node *target;
1618 
1619 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1620 		iscsi_tgt_node_config_json(target, w);
1621 	}
1622 }
1623