xref: /spdk/lib/iscsi/tgt_node.c (revision f0dbfd081894917e46aa520f6d1e5118150bdf97)
1488570ebSJim Harris /*   SPDX-License-Identifier: BSD-3-Clause
2d29384bfSBen Walker  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
3a6dbe372Spaul luse  *   Copyright (C) 2016 Intel Corporation.
4d29384bfSBen Walker  *   All rights reserved.
5d29384bfSBen Walker  */
6d29384bfSBen Walker 
7b961d9ccSBen Walker #include "spdk/stdinc.h"
8d29384bfSBen Walker 
967c9ea6aSJim Harris #include "spdk/sock.h"
10d777bfd1SShuhei Matsumoto #include "spdk/scsi.h"
11d27b24c9SDaniel Verkamp 
124e8e97c8STomasz Zawadzki #include "spdk/log.h"
13d27b24c9SDaniel Verkamp 
14d29384bfSBen Walker #include "iscsi/iscsi.h"
15d29384bfSBen Walker #include "iscsi/conn.h"
16d29384bfSBen Walker #include "iscsi/tgt_node.h"
17d29384bfSBen Walker #include "iscsi/portal_grp.h"
18d29384bfSBen Walker #include "iscsi/init_grp.h"
19d29384bfSBen Walker #include "iscsi/task.h"
2090ba272cSChangqi Lu #include "spdk/histogram_data.h"
21d29384bfSBen Walker 
22eb15a29fSyidong0635 #define MAX_TMPBUF 4096
23d29384bfSBen Walker #define MAX_MASKBUF 128
24d29384bfSBen Walker 
25abb6823dSZiye Yang 
26abb6823dSZiye Yang #define MAX_TMP_NAME_BUF (11 /* TargetName= */ + MAX_TARGET_NAME + 1 /* null */)
27abb6823dSZiye Yang #define MAX_TMP_ADDR_BUF (14 /* TargetAddress= */ + MAX_PORTAL_ADDR + 1 /* : */ + \
28abb6823dSZiye Yang 			  MAX_PORTAL_PORT + 1 /* , */ + 10 /* max length of int in Decimal */ + 1 /* null */)
29abb6823dSZiye Yang 
30f9bf9cddSShuhei Matsumoto static bool
iscsi_ipv6_netmask_allow_addr(const char * netmask,const char * addr)31893e02a5SShuhei Matsumoto iscsi_ipv6_netmask_allow_addr(const char *netmask, const char *addr)
32d29384bfSBen Walker {
33d29384bfSBen Walker 	struct in6_addr in6_mask;
34d29384bfSBen Walker 	struct in6_addr in6_addr;
35d29384bfSBen Walker 	char mask[MAX_MASKBUF];
36d29384bfSBen Walker 	const char *p;
37d29384bfSBen Walker 	size_t n;
38d29384bfSBen Walker 	int bits, bmask;
39d29384bfSBen Walker 	int i;
40d29384bfSBen Walker 
4159970a89SDaniel Verkamp 	if (netmask[0] != '[') {
42f9bf9cddSShuhei Matsumoto 		return false;
4359970a89SDaniel Verkamp 	}
44d29384bfSBen Walker 	p = strchr(netmask, ']');
4559970a89SDaniel Verkamp 	if (p == NULL) {
46f9bf9cddSShuhei Matsumoto 		return false;
4759970a89SDaniel Verkamp 	}
48d29384bfSBen Walker 	n = p - (netmask + 1);
4959970a89SDaniel Verkamp 	if (n + 1 > sizeof mask) {
50f9bf9cddSShuhei Matsumoto 		return false;
5159970a89SDaniel Verkamp 	}
52d29384bfSBen Walker 
53d29384bfSBen Walker 	memcpy(mask, netmask + 1, n);
54d29384bfSBen Walker 	mask[n] = '\0';
55d29384bfSBen Walker 	p++;
56d29384bfSBen Walker 
57d29384bfSBen Walker 	if (p[0] == '/') {
58d29384bfSBen Walker 		bits = (int) strtol(p + 1, NULL, 10);
5961c14937SDaniel Verkamp 		if (bits <= 0 || bits > 128) {
60f9bf9cddSShuhei Matsumoto 			return false;
6159970a89SDaniel Verkamp 		}
62d29384bfSBen Walker 	} else {
63d29384bfSBen Walker 		bits = 128;
64d29384bfSBen Walker 	}
65d29384bfSBen Walker 
66d29384bfSBen Walker #if 0
672172c432STomasz Zawadzki 	SPDK_DEBUGLOG(iscsi, "input %s\n", addr);
682172c432STomasz Zawadzki 	SPDK_DEBUGLOG(iscsi, "mask  %s / %d\n", mask, bits);
69d29384bfSBen Walker #endif
70d29384bfSBen Walker 
71d29384bfSBen Walker 	/* presentation to network order binary */
72d29384bfSBen Walker 	if (inet_pton(AF_INET6, mask, &in6_mask) <= 0
73d29384bfSBen Walker 	    || inet_pton(AF_INET6, addr, &in6_addr) <= 0) {
74f9bf9cddSShuhei Matsumoto 		return false;
75d29384bfSBen Walker 	}
76d29384bfSBen Walker 
77d29384bfSBen Walker 	/* check 128bits */
78d29384bfSBen Walker 	for (i = 0; i < (bits / 8); i++) {
7959970a89SDaniel Verkamp 		if (in6_mask.s6_addr[i] != in6_addr.s6_addr[i]) {
80f9bf9cddSShuhei Matsumoto 			return false;
81d29384bfSBen Walker 		}
8259970a89SDaniel Verkamp 	}
83d29384bfSBen Walker 	if (bits % 8) {
84d29384bfSBen Walker 		bmask = (0xffU << (8 - (bits % 8))) & 0xffU;
8559970a89SDaniel Verkamp 		if ((in6_mask.s6_addr[i] & bmask) != (in6_addr.s6_addr[i] & bmask)) {
86f9bf9cddSShuhei Matsumoto 			return false;
87d29384bfSBen Walker 		}
8859970a89SDaniel Verkamp 	}
89d29384bfSBen Walker 
90d29384bfSBen Walker 	/* match */
91f9bf9cddSShuhei Matsumoto 	return true;
92d29384bfSBen Walker }
93d29384bfSBen Walker 
94f9bf9cddSShuhei Matsumoto static bool
iscsi_ipv4_netmask_allow_addr(const char * netmask,const char * addr)95893e02a5SShuhei Matsumoto iscsi_ipv4_netmask_allow_addr(const char *netmask, const char *addr)
96d29384bfSBen Walker {
97d29384bfSBen Walker 	struct in_addr in4_mask;
98d29384bfSBen Walker 	struct in_addr in4_addr;
99d29384bfSBen Walker 	char mask[MAX_MASKBUF];
100d29384bfSBen Walker 	const char *p;
101d29384bfSBen Walker 	uint32_t bmask;
102d29384bfSBen Walker 	size_t n;
103d29384bfSBen Walker 	int bits;
104d29384bfSBen Walker 
105d29384bfSBen Walker 	p = strchr(netmask, '/');
106d29384bfSBen Walker 	if (p == NULL) {
107d29384bfSBen Walker 		p = netmask + strlen(netmask);
108d29384bfSBen Walker 	}
109d29384bfSBen Walker 	n = p - netmask;
11059970a89SDaniel Verkamp 	if (n + 1 > sizeof mask) {
111f9bf9cddSShuhei Matsumoto 		return false;
11259970a89SDaniel Verkamp 	}
113d29384bfSBen Walker 
114d29384bfSBen Walker 	memcpy(mask, netmask, n);
115d29384bfSBen Walker 	mask[n] = '\0';
116d29384bfSBen Walker 
117d29384bfSBen Walker 	if (p[0] == '/') {
118d29384bfSBen Walker 		bits = (int) strtol(p + 1, NULL, 10);
11961c14937SDaniel Verkamp 		if (bits <= 0 || bits > 32) {
120f9bf9cddSShuhei Matsumoto 			return false;
12159970a89SDaniel Verkamp 		}
122d29384bfSBen Walker 	} else {
123d29384bfSBen Walker 		bits = 32;
124d29384bfSBen Walker 	}
125d29384bfSBen Walker 
126d29384bfSBen Walker 	/* presentation to network order binary */
127d29384bfSBen Walker 	if (inet_pton(AF_INET, mask, &in4_mask) <= 0
128d29384bfSBen Walker 	    || inet_pton(AF_INET, addr, &in4_addr) <= 0) {
129f9bf9cddSShuhei Matsumoto 		return false;
130d29384bfSBen Walker 	}
131d29384bfSBen Walker 
132d29384bfSBen Walker 	/* check 32bits */
13361c14937SDaniel Verkamp 	bmask = (0xffffffffU << (32 - bits)) & 0xffffffffU;
13459970a89SDaniel Verkamp 	if ((ntohl(in4_mask.s_addr) & bmask) != (ntohl(in4_addr.s_addr) & bmask)) {
135f9bf9cddSShuhei Matsumoto 		return false;
13659970a89SDaniel Verkamp 	}
137d29384bfSBen Walker 
138d29384bfSBen Walker 	/* match */
139f9bf9cddSShuhei Matsumoto 	return true;
140d29384bfSBen Walker }
141d29384bfSBen Walker 
142f9bf9cddSShuhei Matsumoto static bool
iscsi_netmask_allow_addr(const char * netmask,const char * addr)143893e02a5SShuhei Matsumoto iscsi_netmask_allow_addr(const char *netmask, const char *addr)
144d29384bfSBen Walker {
14559970a89SDaniel Verkamp 	if (netmask == NULL || addr == NULL) {
146f9bf9cddSShuhei Matsumoto 		return false;
14759970a89SDaniel Verkamp 	}
148f53462b4SShuhei Matsumoto 	if (strcasecmp(netmask, "ANY") == 0) {
149f9bf9cddSShuhei Matsumoto 		return true;
15059970a89SDaniel Verkamp 	}
151d29384bfSBen Walker 	if (netmask[0] == '[') {
152d29384bfSBen Walker 		/* IPv6 */
153893e02a5SShuhei Matsumoto 		if (iscsi_ipv6_netmask_allow_addr(netmask, addr)) {
154f9bf9cddSShuhei Matsumoto 			return true;
15559970a89SDaniel Verkamp 		}
156d29384bfSBen Walker 	} else {
157d29384bfSBen Walker 		/* IPv4 */
158893e02a5SShuhei Matsumoto 		if (iscsi_ipv4_netmask_allow_addr(netmask, addr)) {
159f9bf9cddSShuhei Matsumoto 			return true;
160d29384bfSBen Walker 		}
16159970a89SDaniel Verkamp 	}
162f9bf9cddSShuhei Matsumoto 	return false;
163d29384bfSBen Walker }
164d29384bfSBen Walker 
165c2f38258SShuhei Matsumoto static bool
iscsi_init_grp_allow_addr(struct spdk_iscsi_init_grp * igp,const char * addr)166893e02a5SShuhei Matsumoto iscsi_init_grp_allow_addr(struct spdk_iscsi_init_grp *igp,
167c2f38258SShuhei Matsumoto 			  const char *addr)
168c2f38258SShuhei Matsumoto {
169c2f38258SShuhei Matsumoto 	struct spdk_iscsi_initiator_netmask *imask;
170c2f38258SShuhei Matsumoto 
171c2f38258SShuhei Matsumoto 	TAILQ_FOREACH(imask, &igp->netmask_head, tailq) {
1722172c432STomasz Zawadzki 		SPDK_DEBUGLOG(iscsi, "netmask=%s, addr=%s\n",
173c2f38258SShuhei Matsumoto 			      imask->mask, addr);
174893e02a5SShuhei Matsumoto 		if (iscsi_netmask_allow_addr(imask->mask, addr)) {
175c2f38258SShuhei Matsumoto 			return true;
176c2f38258SShuhei Matsumoto 		}
177c2f38258SShuhei Matsumoto 	}
178c2f38258SShuhei Matsumoto 	return false;
179c2f38258SShuhei Matsumoto }
180c2f38258SShuhei Matsumoto 
181c2f38258SShuhei Matsumoto static int
iscsi_init_grp_allow_iscsi_name(struct spdk_iscsi_init_grp * igp,const char * iqn,bool * result)182893e02a5SShuhei Matsumoto iscsi_init_grp_allow_iscsi_name(struct spdk_iscsi_init_grp *igp,
183c2f38258SShuhei Matsumoto 				const char *iqn, bool *result)
184c2f38258SShuhei Matsumoto {
185c2f38258SShuhei Matsumoto 	struct spdk_iscsi_initiator_name *iname;
186c2f38258SShuhei Matsumoto 
187c2f38258SShuhei Matsumoto 	TAILQ_FOREACH(iname, &igp->initiator_head, tailq) {
188c2f38258SShuhei Matsumoto 		/* denied if iqn is matched */
189c2f38258SShuhei Matsumoto 		if ((iname->name[0] == '!')
190c2f38258SShuhei Matsumoto 		    && (strcasecmp(&iname->name[1], "ANY") == 0
191c2f38258SShuhei Matsumoto 			|| strcasecmp(&iname->name[1], iqn) == 0)) {
192c2f38258SShuhei Matsumoto 			*result = false;
193c2f38258SShuhei Matsumoto 			return 0;
194c2f38258SShuhei Matsumoto 		}
195c2f38258SShuhei Matsumoto 		/* allowed if iqn is matched */
196c2f38258SShuhei Matsumoto 		if (strcasecmp(iname->name, "ANY") == 0
197c2f38258SShuhei Matsumoto 		    || strcasecmp(iname->name, iqn) == 0) {
198c2f38258SShuhei Matsumoto 			*result = true;
199c2f38258SShuhei Matsumoto 			return 0;
200c2f38258SShuhei Matsumoto 		}
201c2f38258SShuhei Matsumoto 	}
202c2f38258SShuhei Matsumoto 	return -1;
203c2f38258SShuhei Matsumoto }
204c2f38258SShuhei Matsumoto 
2058dd1cd21SBen Walker static struct spdk_iscsi_pg_map *iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
206c5e5047dSShuhei Matsumoto 		struct spdk_iscsi_portal_grp *pg);
207c5e5047dSShuhei Matsumoto 
208f9bf9cddSShuhei Matsumoto bool
iscsi_tgt_node_access(struct spdk_iscsi_conn * conn,struct spdk_iscsi_tgt_node * target,const char * iqn,const char * addr)2096a313725SShuhei Matsumoto iscsi_tgt_node_access(struct spdk_iscsi_conn *conn,
210d29384bfSBen Walker 		      struct spdk_iscsi_tgt_node *target, const char *iqn, const char *addr)
211d29384bfSBen Walker {
212d29384bfSBen Walker 	struct spdk_iscsi_portal_grp *pg;
213c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_pg_map *pg_map;
214c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_ig_map *ig_map;
215c2f38258SShuhei Matsumoto 	int rc;
216c2f38258SShuhei Matsumoto 	bool allowed = false;
217d29384bfSBen Walker 
21859970a89SDaniel Verkamp 	if (conn == NULL || target == NULL || iqn == NULL || addr == NULL) {
219f9bf9cddSShuhei Matsumoto 		return false;
22059970a89SDaniel Verkamp 	}
221d29384bfSBen Walker 	pg = conn->portal->group;
222d29384bfSBen Walker 
2232172c432STomasz Zawadzki 	SPDK_DEBUGLOG(iscsi, "pg=%d, iqn=%s, addr=%s\n",
224d29384bfSBen Walker 		      pg->tag, iqn, addr);
225893e02a5SShuhei Matsumoto 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
226c5e5047dSShuhei Matsumoto 	if (pg_map == NULL) {
227c5e5047dSShuhei Matsumoto 		return false;
228c5e5047dSShuhei Matsumoto 	}
229c5e5047dSShuhei Matsumoto 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
230893e02a5SShuhei Matsumoto 		rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &allowed);
231c2f38258SShuhei Matsumoto 		if (rc == 0) {
232c2f38258SShuhei Matsumoto 			if (allowed == false) {
2334df53290SShuhei Matsumoto 				goto denied;
234c2f38258SShuhei Matsumoto 			} else {
235893e02a5SShuhei Matsumoto 				if (iscsi_init_grp_allow_addr(ig_map->ig, addr)) {
236f9bf9cddSShuhei Matsumoto 					return true;
237d29384bfSBen Walker 				}
238d29384bfSBen Walker 			}
239c2f38258SShuhei Matsumoto 		} else {
2404df53290SShuhei Matsumoto 			/* netmask is denied in this initiator group */
241d29384bfSBen Walker 		}
242d29384bfSBen Walker 	}
243d29384bfSBen Walker 
2444df53290SShuhei Matsumoto denied:
2452172c432STomasz Zawadzki 	SPDK_DEBUGLOG(iscsi, "access denied from %s (%s) to %s (%s:%s,%d)\n",
2467877ddc8SShuhei Matsumoto 		      iqn, addr, target->name, conn->portal_host,
2477877ddc8SShuhei Matsumoto 		      conn->portal_port, conn->pg_tag);
248f9bf9cddSShuhei Matsumoto 	return false;
249d29384bfSBen Walker }
250d29384bfSBen Walker 
251f9bf9cddSShuhei Matsumoto static bool
iscsi_tgt_node_allow_iscsi_name(struct spdk_iscsi_tgt_node * target,const char * iqn)252893e02a5SShuhei Matsumoto iscsi_tgt_node_allow_iscsi_name(struct spdk_iscsi_tgt_node *target, const char *iqn)
253d29384bfSBen Walker {
254c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_pg_map *pg_map;
255c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_ig_map *ig_map;
256c2f38258SShuhei Matsumoto 	int rc;
257c2f38258SShuhei Matsumoto 	bool result = false;
258d29384bfSBen Walker 
25959970a89SDaniel Verkamp 	if (target == NULL || iqn == NULL) {
260f9bf9cddSShuhei Matsumoto 		return false;
26159970a89SDaniel Verkamp 	}
26292a69886SShuhei Matsumoto 
263c5e5047dSShuhei Matsumoto 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
264c5e5047dSShuhei Matsumoto 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
265893e02a5SShuhei Matsumoto 			rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &result);
266c2f38258SShuhei Matsumoto 			if (rc == 0) {
267c2f38258SShuhei Matsumoto 				return result;
268c2f38258SShuhei Matsumoto 			}
269c5e5047dSShuhei Matsumoto 		}
270d29384bfSBen Walker 	}
271d29384bfSBen Walker 
272f9bf9cddSShuhei Matsumoto 	return false;
273d29384bfSBen Walker }
274d29384bfSBen Walker 
275abb6823dSZiye Yang static bool
iscsi_copy_str(char * data,int * total,int alloc_len,int * previous_completed_len,int expected_size,char * src)276abb6823dSZiye Yang iscsi_copy_str(char *data, int *total, int alloc_len,
277abb6823dSZiye Yang 	       int *previous_completed_len, int expected_size, char *src)
278abb6823dSZiye Yang {
279abb6823dSZiye Yang 	int len = 0;
280abb6823dSZiye Yang 
281abb6823dSZiye Yang 	assert(*previous_completed_len >= 0);
282abb6823dSZiye Yang 
283abb6823dSZiye Yang 	if (alloc_len - *total < 1) {
284abb6823dSZiye Yang 		return true;
285abb6823dSZiye Yang 	}
286abb6823dSZiye Yang 
287abb6823dSZiye Yang 	if (*previous_completed_len < expected_size) {
288abb6823dSZiye Yang 		len = spdk_min(alloc_len - *total, expected_size - *previous_completed_len);
289abb6823dSZiye Yang 		memcpy((char *)data + *total, src + *previous_completed_len, len);
290abb6823dSZiye Yang 		*total += len;
291abb6823dSZiye Yang 		*previous_completed_len = 0;
292abb6823dSZiye Yang 	} else {
293abb6823dSZiye Yang 		*previous_completed_len -= expected_size;
294abb6823dSZiye Yang 	}
295abb6823dSZiye Yang 
296abb6823dSZiye Yang 	return false;
297abb6823dSZiye Yang }
298abb6823dSZiye Yang 
299f5ee7d9bSShuhei Matsumoto static int
iscsi_send_tgt_portals(struct spdk_iscsi_conn * conn,struct spdk_iscsi_tgt_node * target,uint8_t * data,int alloc_len,int total,int * previous_completed_len,bool * no_buf_space)300f5ee7d9bSShuhei Matsumoto iscsi_send_tgt_portals(struct spdk_iscsi_conn *conn,
301f5ee7d9bSShuhei Matsumoto 		       struct spdk_iscsi_tgt_node *target,
302abb6823dSZiye Yang 		       uint8_t *data, int alloc_len, int total,
303abb6823dSZiye Yang 		       int *previous_completed_len, bool *no_buf_space)
304d29384bfSBen Walker {
30557833a5dSZiye Yang 	char buf[MAX_TARGET_ADDR + 2];
306d29384bfSBen Walker 	struct spdk_iscsi_portal_grp *pg;
307c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_pg_map *pg_map;
308d29384bfSBen Walker 	struct spdk_iscsi_portal *p;
309d29384bfSBen Walker 	char *host;
310abb6823dSZiye Yang 	char tmp_buf[MAX_TMP_ADDR_BUF];
311f5ee7d9bSShuhei Matsumoto 	int len;
312f5ee7d9bSShuhei Matsumoto 
313f5ee7d9bSShuhei Matsumoto 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
314f5ee7d9bSShuhei Matsumoto 		pg = pg_map->pg;
315651f6d6aSShuhei Matsumoto 
316651f6d6aSShuhei Matsumoto 		if (pg->is_private) {
317651f6d6aSShuhei Matsumoto 			/* Skip the private portal group. Portals in the private portal group
318651f6d6aSShuhei Matsumoto 			 * will be returned only by temporary login redirection responses.
319651f6d6aSShuhei Matsumoto 			 */
320651f6d6aSShuhei Matsumoto 			continue;
321651f6d6aSShuhei Matsumoto 		}
322651f6d6aSShuhei Matsumoto 
323f5ee7d9bSShuhei Matsumoto 		TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
324f5ee7d9bSShuhei Matsumoto 			host = p->host;
325f5ee7d9bSShuhei Matsumoto 			/* wildcard? */
3260f22282fSShuhei Matsumoto 			if (strcasecmp(host, "[::]") == 0 || strcasecmp(host, "0.0.0.0") == 0) {
327f5ee7d9bSShuhei Matsumoto 				if (spdk_sock_is_ipv6(conn->sock)) {
328f5ee7d9bSShuhei Matsumoto 					snprintf(buf, sizeof buf, "[%s]", conn->target_addr);
329f5ee7d9bSShuhei Matsumoto 					host = buf;
330f5ee7d9bSShuhei Matsumoto 				} else if (spdk_sock_is_ipv4(conn->sock)) {
331f5ee7d9bSShuhei Matsumoto 					snprintf(buf, sizeof buf, "%s", conn->target_addr);
332f5ee7d9bSShuhei Matsumoto 					host = buf;
333f5ee7d9bSShuhei Matsumoto 				} else {
334f5ee7d9bSShuhei Matsumoto 					/* skip portal for the family */
335f5ee7d9bSShuhei Matsumoto 					continue;
336f5ee7d9bSShuhei Matsumoto 				}
337f5ee7d9bSShuhei Matsumoto 			}
3382172c432STomasz Zawadzki 			SPDK_DEBUGLOG(iscsi, "TargetAddress=%s:%s,%d\n",
339f5ee7d9bSShuhei Matsumoto 				      host, p->port, pg->tag);
340abb6823dSZiye Yang 
341abb6823dSZiye Yang 			memset(tmp_buf, 0, sizeof(tmp_buf));
342cc6920a4SJosh Soref 			/* Calculate the whole string size */
343abb6823dSZiye Yang 			len = snprintf(NULL, 0, "TargetAddress=%s:%s,%d", host, p->port, pg->tag);
344abb6823dSZiye Yang 			assert(len < MAX_TMPBUF);
345abb6823dSZiye Yang 
346abb6823dSZiye Yang 			/* string contents are not fully copied */
347abb6823dSZiye Yang 			if (*previous_completed_len < len) {
348abb6823dSZiye Yang 				/* Copy the string into the temporary buffer */
349abb6823dSZiye Yang 				snprintf(tmp_buf, len + 1, "TargetAddress=%s:%s,%d", host, p->port, pg->tag);
350abb6823dSZiye Yang 			}
351abb6823dSZiye Yang 
352abb6823dSZiye Yang 			*no_buf_space = iscsi_copy_str(data, &total, alloc_len, previous_completed_len,
353abb6823dSZiye Yang 						       len + 1, tmp_buf);
354abb6823dSZiye Yang 			if (*no_buf_space) {
355abb6823dSZiye Yang 				break;
356abb6823dSZiye Yang 			}
357f5ee7d9bSShuhei Matsumoto 		}
358f5ee7d9bSShuhei Matsumoto 	}
359f5ee7d9bSShuhei Matsumoto 
360f5ee7d9bSShuhei Matsumoto 	return total;
361f5ee7d9bSShuhei Matsumoto }
362f5ee7d9bSShuhei Matsumoto 
363f5ee7d9bSShuhei Matsumoto int
iscsi_send_tgts(struct spdk_iscsi_conn * conn,const char * iiqn,const char * tiqn,uint8_t * data,int alloc_len,int data_len)364f5ee7d9bSShuhei Matsumoto iscsi_send_tgts(struct spdk_iscsi_conn *conn, const char *iiqn,
365f5ee7d9bSShuhei Matsumoto 		const char *tiqn, uint8_t *data, int alloc_len, int data_len)
366f5ee7d9bSShuhei Matsumoto {
367f5ee7d9bSShuhei Matsumoto 	struct spdk_iscsi_tgt_node *target;
368d29384bfSBen Walker 	int total;
369d29384bfSBen Walker 	int len;
370d29384bfSBen Walker 	int rc;
3711665a80fSGangCao 	int previous_completed_size = 0;
372abb6823dSZiye Yang 	bool no_buf_space = false;
373abb6823dSZiye Yang 	char tmp_buf[MAX_TMP_NAME_BUF];
374d29384bfSBen Walker 
37559970a89SDaniel Verkamp 	if (conn == NULL) {
376d29384bfSBen Walker 		return 0;
37759970a89SDaniel Verkamp 	}
3781665a80fSGangCao 	previous_completed_size = conn->send_tgt_completed_size;
379d29384bfSBen Walker 
380d29384bfSBen Walker 	total = data_len;
381d29384bfSBen Walker 	if (alloc_len < 1) {
382d29384bfSBen Walker 		return 0;
383d29384bfSBen Walker 	}
3842d1fc3caSChangpeng Liu 	if (total >= alloc_len) {
385d29384bfSBen Walker 		total = alloc_len;
386d29384bfSBen Walker 		data[total - 1] = '\0';
387d29384bfSBen Walker 		return total;
388d29384bfSBen Walker 	}
389d29384bfSBen Walker 
390be1489b9SShuhei Matsumoto 	pthread_mutex_lock(&g_iscsi.mutex);
391be1489b9SShuhei Matsumoto 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
392d29384bfSBen Walker 		if (strcasecmp(tiqn, "ALL") != 0
393d29384bfSBen Walker 		    && strcasecmp(tiqn, target->name) != 0) {
394d29384bfSBen Walker 			continue;
395d29384bfSBen Walker 		}
396893e02a5SShuhei Matsumoto 		rc = iscsi_tgt_node_allow_iscsi_name(target, iiqn);
397d29384bfSBen Walker 		if (rc == 0) {
398d29384bfSBen Walker 			continue;
399d29384bfSBen Walker 		}
400d29384bfSBen Walker 
401abb6823dSZiye Yang 		memset(tmp_buf, 0, sizeof(tmp_buf));
402abb6823dSZiye Yang 		/* Calculate the whole string size */
403abb6823dSZiye Yang 		len = snprintf(NULL, 0, "TargetName=%s", target->name);
404abb6823dSZiye Yang 		assert(len < MAX_TMPBUF);
405d29384bfSBen Walker 
406cc6920a4SJosh Soref 		/* String contents are not copied */
407abb6823dSZiye Yang 		if (previous_completed_size < len) {
408abb6823dSZiye Yang 			/* Copy the string into the temporary buffer */
409abb6823dSZiye Yang 			snprintf(tmp_buf, len + 1, "TargetName=%s", target->name);
410abb6823dSZiye Yang 		}
411abb6823dSZiye Yang 
412abb6823dSZiye Yang 		no_buf_space = iscsi_copy_str(data, &total, alloc_len, &previous_completed_size,
413abb6823dSZiye Yang 					      len + 1, tmp_buf);
414abb6823dSZiye Yang 		if (no_buf_space) {
415abb6823dSZiye Yang 			break;
416abb6823dSZiye Yang 		}
417abb6823dSZiye Yang 
418abb6823dSZiye Yang 		total = iscsi_send_tgt_portals(conn, target, data, alloc_len, total,
419abb6823dSZiye Yang 					       &previous_completed_size, &no_buf_space);
420abb6823dSZiye Yang 		if (no_buf_space) {
421f5ee7d9bSShuhei Matsumoto 			break;
422d29384bfSBen Walker 		}
423d29384bfSBen Walker 	}
424be1489b9SShuhei Matsumoto 	pthread_mutex_unlock(&g_iscsi.mutex);
425d29384bfSBen Walker 
426cc6920a4SJosh Soref 	/* Only set it when it is not successfully completed */
427abb6823dSZiye Yang 	if (no_buf_space) {
428abb6823dSZiye Yang 		conn->send_tgt_completed_size += total;
429abb6823dSZiye Yang 	} else {
430abb6823dSZiye Yang 		conn->send_tgt_completed_size = 0;
431abb6823dSZiye Yang 	}
432abb6823dSZiye Yang 
433d29384bfSBen Walker 	return total;
434d29384bfSBen Walker }
435d29384bfSBen Walker 
436d29384bfSBen Walker struct spdk_iscsi_tgt_node *
iscsi_find_tgt_node(const char * target_name)4376a313725SShuhei Matsumoto iscsi_find_tgt_node(const char *target_name)
438d29384bfSBen Walker {
439d29384bfSBen Walker 	struct spdk_iscsi_tgt_node *target;
440d29384bfSBen Walker 
44159970a89SDaniel Verkamp 	if (target_name == NULL) {
442d29384bfSBen Walker 		return NULL;
44359970a89SDaniel Verkamp 	}
444be1489b9SShuhei Matsumoto 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
445d29384bfSBen Walker 		if (strcasecmp(target_name, target->name) == 0) {
446d29384bfSBen Walker 			return target;
447d29384bfSBen Walker 		}
448d29384bfSBen Walker 	}
449d29384bfSBen Walker 	return NULL;
450d29384bfSBen Walker }
451d29384bfSBen Walker 
452d29384bfSBen Walker static int
iscsi_tgt_node_register(struct spdk_iscsi_tgt_node * target)453893e02a5SShuhei Matsumoto iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target)
454f4e892feSShuhei Matsumoto {
455be1489b9SShuhei Matsumoto 	pthread_mutex_lock(&g_iscsi.mutex);
456f4e892feSShuhei Matsumoto 
4576a313725SShuhei Matsumoto 	if (iscsi_find_tgt_node(target->name) != NULL) {
458be1489b9SShuhei Matsumoto 		pthread_mutex_unlock(&g_iscsi.mutex);
459f4e892feSShuhei Matsumoto 		return -EEXIST;
460f4e892feSShuhei Matsumoto 	}
461f4e892feSShuhei Matsumoto 
462be1489b9SShuhei Matsumoto 	TAILQ_INSERT_TAIL(&g_iscsi.target_head, target, tailq);
463f4e892feSShuhei Matsumoto 
464be1489b9SShuhei Matsumoto 	pthread_mutex_unlock(&g_iscsi.mutex);
465f4e892feSShuhei Matsumoto 	return 0;
466f4e892feSShuhei Matsumoto }
467f4e892feSShuhei Matsumoto 
468f4e892feSShuhei Matsumoto static int
iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node * target)469893e02a5SShuhei Matsumoto iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target)
470f4e892feSShuhei Matsumoto {
471f4e892feSShuhei Matsumoto 	struct spdk_iscsi_tgt_node *t;
472f4e892feSShuhei Matsumoto 
473be1489b9SShuhei Matsumoto 	TAILQ_FOREACH(t, &g_iscsi.target_head, tailq) {
474f4e892feSShuhei Matsumoto 		if (t == target) {
475be1489b9SShuhei Matsumoto 			TAILQ_REMOVE(&g_iscsi.target_head, t, tailq);
476f4e892feSShuhei Matsumoto 			return 0;
477f4e892feSShuhei Matsumoto 		}
478f4e892feSShuhei Matsumoto 	}
479f4e892feSShuhei Matsumoto 
480f4e892feSShuhei Matsumoto 	return -1;
481f4e892feSShuhei Matsumoto }
482f4e892feSShuhei Matsumoto 
483c5e5047dSShuhei Matsumoto static struct spdk_iscsi_ig_map *
iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map * pg_map,struct spdk_iscsi_init_grp * ig)484893e02a5SShuhei Matsumoto iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map,
485c5e5047dSShuhei Matsumoto 			 struct spdk_iscsi_init_grp *ig)
486c5e5047dSShuhei Matsumoto {
487c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_ig_map *ig_map;
488c5e5047dSShuhei Matsumoto 
489c5e5047dSShuhei Matsumoto 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
490c5e5047dSShuhei Matsumoto 		if (ig_map->ig == ig) {
491c5e5047dSShuhei Matsumoto 			return ig_map;
492c5e5047dSShuhei Matsumoto 		}
493c5e5047dSShuhei Matsumoto 	}
494c5e5047dSShuhei Matsumoto 
495c5e5047dSShuhei Matsumoto 	return NULL;
496c5e5047dSShuhei Matsumoto }
497c5e5047dSShuhei Matsumoto 
498c5e5047dSShuhei Matsumoto static struct spdk_iscsi_ig_map *
iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map * pg_map,struct spdk_iscsi_init_grp * ig)499893e02a5SShuhei Matsumoto iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map,
500c5e5047dSShuhei Matsumoto 			struct spdk_iscsi_init_grp *ig)
501c5e5047dSShuhei Matsumoto {
502c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_ig_map *ig_map;
503c5e5047dSShuhei Matsumoto 
504893e02a5SShuhei Matsumoto 	if (iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) {
505c5e5047dSShuhei Matsumoto 		return NULL;
506c5e5047dSShuhei Matsumoto 	}
507c5e5047dSShuhei Matsumoto 
508c5e5047dSShuhei Matsumoto 	ig_map = malloc(sizeof(*ig_map));
509c5e5047dSShuhei Matsumoto 	if (ig_map == NULL) {
510c5e5047dSShuhei Matsumoto 		return NULL;
511c5e5047dSShuhei Matsumoto 	}
512c5e5047dSShuhei Matsumoto 
513c5e5047dSShuhei Matsumoto 	ig_map->ig = ig;
514c5e5047dSShuhei Matsumoto 	ig->ref++;
515c5e5047dSShuhei Matsumoto 	pg_map->num_ig_maps++;
516c5e5047dSShuhei Matsumoto 	TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq);
517c5e5047dSShuhei Matsumoto 
518c5e5047dSShuhei Matsumoto 	return ig_map;
519c5e5047dSShuhei Matsumoto }
520c5e5047dSShuhei Matsumoto 
521c5e5047dSShuhei Matsumoto static void
_iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map * pg_map,struct spdk_iscsi_ig_map * ig_map)522893e02a5SShuhei Matsumoto _iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
523c5e5047dSShuhei Matsumoto 			    struct spdk_iscsi_ig_map *ig_map)
524c5e5047dSShuhei Matsumoto {
525c5e5047dSShuhei Matsumoto 	TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq);
526c5e5047dSShuhei Matsumoto 	pg_map->num_ig_maps--;
527c5e5047dSShuhei Matsumoto 	ig_map->ig->ref--;
528c5e5047dSShuhei Matsumoto 	free(ig_map);
529c5e5047dSShuhei Matsumoto }
530c5e5047dSShuhei Matsumoto 
531c5e5047dSShuhei Matsumoto static int
iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map * pg_map,struct spdk_iscsi_init_grp * ig)532893e02a5SShuhei Matsumoto iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
533c5e5047dSShuhei Matsumoto 			   struct spdk_iscsi_init_grp *ig)
534c5e5047dSShuhei Matsumoto {
535c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_ig_map *ig_map;
536c5e5047dSShuhei Matsumoto 
537893e02a5SShuhei Matsumoto 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
538c5e5047dSShuhei Matsumoto 	if (ig_map == NULL) {
539c5e5047dSShuhei Matsumoto 		return -ENOENT;
540c5e5047dSShuhei Matsumoto 	}
541c5e5047dSShuhei Matsumoto 
542893e02a5SShuhei Matsumoto 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
543c5e5047dSShuhei Matsumoto 	return 0;
544c5e5047dSShuhei Matsumoto }
545c5e5047dSShuhei Matsumoto 
546c5e5047dSShuhei Matsumoto static void
iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map * pg_map)547893e02a5SShuhei Matsumoto iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map)
548c5e5047dSShuhei Matsumoto {
549c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_ig_map *ig_map, *tmp;
550c5e5047dSShuhei Matsumoto 
551c5e5047dSShuhei Matsumoto 	TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) {
552893e02a5SShuhei Matsumoto 		_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
553c5e5047dSShuhei Matsumoto 	}
554c5e5047dSShuhei Matsumoto }
555c5e5047dSShuhei Matsumoto 
556c5e5047dSShuhei Matsumoto static struct spdk_iscsi_pg_map *
iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node * target,struct spdk_iscsi_portal_grp * pg)557893e02a5SShuhei Matsumoto iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
558c5e5047dSShuhei Matsumoto 			   struct spdk_iscsi_portal_grp *pg)
559c5e5047dSShuhei Matsumoto {
560c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_pg_map *pg_map;
561c5e5047dSShuhei Matsumoto 
562c5e5047dSShuhei Matsumoto 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
563c5e5047dSShuhei Matsumoto 		if (pg_map->pg == pg) {
564c5e5047dSShuhei Matsumoto 			return pg_map;
565c5e5047dSShuhei Matsumoto 		}
566c5e5047dSShuhei Matsumoto 	}
567c5e5047dSShuhei Matsumoto 
568c5e5047dSShuhei Matsumoto 	return NULL;
569c5e5047dSShuhei Matsumoto }
570c5e5047dSShuhei Matsumoto 
571c5e5047dSShuhei Matsumoto static struct spdk_iscsi_pg_map *
iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node * target,struct spdk_iscsi_portal_grp * pg)572893e02a5SShuhei Matsumoto iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target,
573c5e5047dSShuhei Matsumoto 			  struct spdk_iscsi_portal_grp *pg)
574c5e5047dSShuhei Matsumoto {
575c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_pg_map *pg_map;
57650ec7389SShuhei Matsumoto 	char port_name[MAX_TMPBUF];
57750ec7389SShuhei Matsumoto 	int rc;
578c5e5047dSShuhei Matsumoto 
579893e02a5SShuhei Matsumoto 	if (iscsi_tgt_node_find_pg_map(target, pg) != NULL) {
580c5e5047dSShuhei Matsumoto 		return NULL;
581c5e5047dSShuhei Matsumoto 	}
582c5e5047dSShuhei Matsumoto 
58350ec7389SShuhei Matsumoto 	if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) {
58450ec7389SShuhei Matsumoto 		SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n",
58550ec7389SShuhei Matsumoto 			    SPDK_SCSI_DEV_MAX_PORTS);
58650ec7389SShuhei Matsumoto 		return NULL;
58750ec7389SShuhei Matsumoto 	}
58850ec7389SShuhei Matsumoto 
5892c8309e0SShuhei Matsumoto 	pg_map = calloc(1, sizeof(*pg_map));
590c5e5047dSShuhei Matsumoto 	if (pg_map == NULL) {
591c5e5047dSShuhei Matsumoto 		return NULL;
592c5e5047dSShuhei Matsumoto 	}
593c5e5047dSShuhei Matsumoto 
59450ec7389SShuhei Matsumoto 	snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x",
59550ec7389SShuhei Matsumoto 		 spdk_scsi_dev_get_name(target->dev), pg->tag);
59650ec7389SShuhei Matsumoto 	rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name);
59750ec7389SShuhei Matsumoto 	if (rc != 0) {
59850ec7389SShuhei Matsumoto 		free(pg_map);
59950ec7389SShuhei Matsumoto 		return NULL;
60050ec7389SShuhei Matsumoto 	}
60150ec7389SShuhei Matsumoto 
602c5e5047dSShuhei Matsumoto 	TAILQ_INIT(&pg_map->ig_map_head);
603c5e5047dSShuhei Matsumoto 	pg_map->num_ig_maps = 0;
604c5e5047dSShuhei Matsumoto 	pg->ref++;
605c5e5047dSShuhei Matsumoto 	pg_map->pg = pg;
606c5e5047dSShuhei Matsumoto 	target->num_pg_maps++;
607c5e5047dSShuhei Matsumoto 	TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq);
608c5e5047dSShuhei Matsumoto 
609c5e5047dSShuhei Matsumoto 	return pg_map;
610c5e5047dSShuhei Matsumoto }
611c5e5047dSShuhei Matsumoto 
612c5e5047dSShuhei Matsumoto static void
_iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node * target,struct spdk_iscsi_pg_map * pg_map)613893e02a5SShuhei Matsumoto _iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
614c5e5047dSShuhei Matsumoto 			      struct spdk_iscsi_pg_map *pg_map)
615c5e5047dSShuhei Matsumoto {
616c5e5047dSShuhei Matsumoto 	TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq);
617c5e5047dSShuhei Matsumoto 	target->num_pg_maps--;
618c5e5047dSShuhei Matsumoto 	pg_map->pg->ref--;
61950ec7389SShuhei Matsumoto 
62050ec7389SShuhei Matsumoto 	spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag);
62150ec7389SShuhei Matsumoto 
622c5e5047dSShuhei Matsumoto 	free(pg_map);
623c5e5047dSShuhei Matsumoto }
624c5e5047dSShuhei Matsumoto 
625c5e5047dSShuhei Matsumoto static int
iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node * target,struct spdk_iscsi_portal_grp * pg)626893e02a5SShuhei Matsumoto iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
627c5e5047dSShuhei Matsumoto 			     struct spdk_iscsi_portal_grp *pg)
628c5e5047dSShuhei Matsumoto {
629c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_pg_map *pg_map;
630c5e5047dSShuhei Matsumoto 
631893e02a5SShuhei Matsumoto 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
632c5e5047dSShuhei Matsumoto 	if (pg_map == NULL) {
633c5e5047dSShuhei Matsumoto 		return -ENOENT;
634c5e5047dSShuhei Matsumoto 	}
635c5e5047dSShuhei Matsumoto 
636c5e5047dSShuhei Matsumoto 	if (pg_map->num_ig_maps > 0) {
6372172c432STomasz Zawadzki 		SPDK_DEBUGLOG(iscsi, "delete %d ig_maps forcefully\n",
63811774a35SShuhei Matsumoto 			      pg_map->num_ig_maps);
639c5e5047dSShuhei Matsumoto 	}
640c5e5047dSShuhei Matsumoto 
641893e02a5SShuhei Matsumoto 	iscsi_pg_map_delete_all_ig_maps(pg_map);
642893e02a5SShuhei Matsumoto 	_iscsi_tgt_node_delete_pg_map(target, pg_map);
643c5e5047dSShuhei Matsumoto 	return 0;
644c5e5047dSShuhei Matsumoto }
645c5e5047dSShuhei Matsumoto 
646c5e5047dSShuhei Matsumoto static void
iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node * target,struct spdk_iscsi_init_grp * ig)647893e02a5SShuhei Matsumoto iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target,
648c5e5047dSShuhei Matsumoto 			      struct spdk_iscsi_init_grp *ig)
649c5e5047dSShuhei Matsumoto {
650c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_pg_map *pg_map, *tmp;
651c5e5047dSShuhei Matsumoto 
652c5e5047dSShuhei Matsumoto 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
653893e02a5SShuhei Matsumoto 		iscsi_pg_map_delete_ig_map(pg_map, ig);
654c5e5047dSShuhei Matsumoto 		if (pg_map->num_ig_maps == 0) {
655893e02a5SShuhei Matsumoto 			_iscsi_tgt_node_delete_pg_map(target, pg_map);
656c5e5047dSShuhei Matsumoto 		}
657c5e5047dSShuhei Matsumoto 	}
658c5e5047dSShuhei Matsumoto }
659c5e5047dSShuhei Matsumoto 
660c5e5047dSShuhei Matsumoto static void
iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node * target)661893e02a5SShuhei Matsumoto iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target)
662c5e5047dSShuhei Matsumoto {
663c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_pg_map *pg_map, *tmp;
664c5e5047dSShuhei Matsumoto 
665c5e5047dSShuhei Matsumoto 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
666893e02a5SShuhei Matsumoto 		iscsi_pg_map_delete_all_ig_maps(pg_map);
667893e02a5SShuhei Matsumoto 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
668c5e5047dSShuhei Matsumoto 	}
669c5e5047dSShuhei Matsumoto }
670c5e5047dSShuhei Matsumoto 
671c5e5047dSShuhei Matsumoto static void
_iscsi_tgt_node_destruct(void * cb_arg,int rc)672d039746cSShuhei Matsumoto _iscsi_tgt_node_destruct(void *cb_arg, int rc)
673a5478cefSShuhei Matsumoto {
674d039746cSShuhei Matsumoto 	struct spdk_iscsi_tgt_node *target = cb_arg;
675d039746cSShuhei Matsumoto 	iscsi_tgt_node_destruct_cb destruct_cb_fn = target->destruct_cb_fn;
676d039746cSShuhei Matsumoto 	void *destruct_cb_arg = target->destruct_cb_arg;
677d039746cSShuhei Matsumoto 
678d039746cSShuhei Matsumoto 	if (rc != 0) {
679d039746cSShuhei Matsumoto 		if (destruct_cb_fn) {
680d039746cSShuhei Matsumoto 			destruct_cb_fn(destruct_cb_arg, rc);
681d039746cSShuhei Matsumoto 		}
682d039746cSShuhei Matsumoto 		return;
683d039746cSShuhei Matsumoto 	}
684d039746cSShuhei Matsumoto 
685be1489b9SShuhei Matsumoto 	pthread_mutex_lock(&g_iscsi.mutex);
686a5478cefSShuhei Matsumoto 	iscsi_tgt_node_delete_all_pg_maps(target);
687be1489b9SShuhei Matsumoto 	pthread_mutex_unlock(&g_iscsi.mutex);
688a5478cefSShuhei Matsumoto 
689a5478cefSShuhei Matsumoto 	pthread_mutex_destroy(&target->mutex);
69090ba272cSChangqi Lu 
69190ba272cSChangqi Lu 	spdk_histogram_data_free(target->histogram);
69290ba272cSChangqi Lu 
693a5478cefSShuhei Matsumoto 	free(target);
694d039746cSShuhei Matsumoto 
695d039746cSShuhei Matsumoto 	if (destruct_cb_fn) {
696d039746cSShuhei Matsumoto 		destruct_cb_fn(destruct_cb_arg, 0);
697d039746cSShuhei Matsumoto 	}
698a5478cefSShuhei Matsumoto }
699a5478cefSShuhei Matsumoto 
700a5478cefSShuhei Matsumoto static int
iscsi_tgt_node_check_active_conns(void * arg)701a5478cefSShuhei Matsumoto iscsi_tgt_node_check_active_conns(void *arg)
702a5478cefSShuhei Matsumoto {
703a5478cefSShuhei Matsumoto 	struct spdk_iscsi_tgt_node *target = arg;
704a5478cefSShuhei Matsumoto 
7053570494dSShuhei Matsumoto 	if (iscsi_get_active_conns(target) != 0) {
706eb05cbd6SMaciej Szwed 		return SPDK_POLLER_BUSY;
707a5478cefSShuhei Matsumoto 	}
708a5478cefSShuhei Matsumoto 
709a5478cefSShuhei Matsumoto 	spdk_poller_unregister(&target->destruct_poller);
710a5478cefSShuhei Matsumoto 
71156d8b785SShuhei Matsumoto 	spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
712a5478cefSShuhei Matsumoto 
713eb05cbd6SMaciej Szwed 	return SPDK_POLLER_BUSY;
714a5478cefSShuhei Matsumoto }
715a5478cefSShuhei Matsumoto 
716a5478cefSShuhei Matsumoto static void
iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node * target,iscsi_tgt_node_destruct_cb cb_fn,void * cb_arg)717d039746cSShuhei Matsumoto iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target,
718d039746cSShuhei Matsumoto 			iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
719c5e5047dSShuhei Matsumoto {
720c5e5047dSShuhei Matsumoto 	if (target == NULL) {
721d039746cSShuhei Matsumoto 		if (cb_fn) {
722d039746cSShuhei Matsumoto 			cb_fn(cb_arg, -ENOENT);
723d039746cSShuhei Matsumoto 		}
724c5e5047dSShuhei Matsumoto 		return;
725c5e5047dSShuhei Matsumoto 	}
726c5e5047dSShuhei Matsumoto 
7278cc7b0bcSShuhei Matsumoto 	if (target->destructed) {
7288cc7b0bcSShuhei Matsumoto 		SPDK_ERRLOG("Destructing %s is already started\n", target->name);
729d039746cSShuhei Matsumoto 		if (cb_fn) {
730d039746cSShuhei Matsumoto 			cb_fn(cb_arg, -EBUSY);
731d039746cSShuhei Matsumoto 		}
7328cc7b0bcSShuhei Matsumoto 		return;
7338cc7b0bcSShuhei Matsumoto 	}
7348cc7b0bcSShuhei Matsumoto 
7358cc7b0bcSShuhei Matsumoto 	target->destructed = true;
736d039746cSShuhei Matsumoto 	target->destruct_cb_fn = cb_fn;
737d039746cSShuhei Matsumoto 	target->destruct_cb_arg = cb_arg;
7388cc7b0bcSShuhei Matsumoto 
739c15a6180SShuhei Matsumoto 	iscsi_conns_request_logout(target, -1);
740a5478cefSShuhei Matsumoto 
7413570494dSShuhei Matsumoto 	if (iscsi_get_active_conns(target) != 0) {
742ab0bc5c2SShuhei Matsumoto 		target->destruct_poller = SPDK_POLLER_REGISTER(iscsi_tgt_node_check_active_conns,
743a5478cefSShuhei Matsumoto 					  target, 10);
744a5478cefSShuhei Matsumoto 	} else {
74556d8b785SShuhei Matsumoto 		spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
746a5478cefSShuhei Matsumoto 	}
747907ef255SShuhei Matsumoto 
748c5e5047dSShuhei Matsumoto }
749c5e5047dSShuhei Matsumoto 
750c5e5047dSShuhei Matsumoto static int
iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node * target,int pg_tag,int ig_tag)751893e02a5SShuhei Matsumoto iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target,
75235cd6005SShuhei Matsumoto 				int pg_tag, int ig_tag)
75335cd6005SShuhei Matsumoto {
75435cd6005SShuhei Matsumoto 	struct spdk_iscsi_portal_grp	*pg;
75535cd6005SShuhei Matsumoto 	struct spdk_iscsi_init_grp	*ig;
75635cd6005SShuhei Matsumoto 	struct spdk_iscsi_pg_map	*pg_map;
75735cd6005SShuhei Matsumoto 	struct spdk_iscsi_ig_map	*ig_map;
75835cd6005SShuhei Matsumoto 
759a587a107SShuhei Matsumoto 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
76035cd6005SShuhei Matsumoto 	if (pg == NULL) {
76135cd6005SShuhei Matsumoto 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
76235cd6005SShuhei Matsumoto 		return -ENOENT;
76335cd6005SShuhei Matsumoto 	}
764df53885bSShuhei Matsumoto 	ig = iscsi_init_grp_find_by_tag(ig_tag);
76535cd6005SShuhei Matsumoto 	if (ig == NULL) {
76635cd6005SShuhei Matsumoto 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
76735cd6005SShuhei Matsumoto 		return -ENOENT;
76835cd6005SShuhei Matsumoto 	}
76935cd6005SShuhei Matsumoto 
770893e02a5SShuhei Matsumoto 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
77135cd6005SShuhei Matsumoto 	if (pg_map == NULL) {
77235cd6005SShuhei Matsumoto 		SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag);
77335cd6005SShuhei Matsumoto 		return -ENOENT;
77435cd6005SShuhei Matsumoto 	}
775893e02a5SShuhei Matsumoto 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
77635cd6005SShuhei Matsumoto 	if (ig_map == NULL) {
77735cd6005SShuhei Matsumoto 		SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag);
77835cd6005SShuhei Matsumoto 		return -ENOENT;
77935cd6005SShuhei Matsumoto 	}
78035cd6005SShuhei Matsumoto 
781893e02a5SShuhei Matsumoto 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
78235cd6005SShuhei Matsumoto 	if (pg_map->num_ig_maps == 0) {
783893e02a5SShuhei Matsumoto 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
78435cd6005SShuhei Matsumoto 	}
78535cd6005SShuhei Matsumoto 
78635cd6005SShuhei Matsumoto 	return 0;
78735cd6005SShuhei Matsumoto }
78835cd6005SShuhei Matsumoto 
78935cd6005SShuhei Matsumoto static int
iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node * target,int pg_tag,int ig_tag)790893e02a5SShuhei Matsumoto iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target,
791c5e5047dSShuhei Matsumoto 			     int pg_tag, int ig_tag)
792c5e5047dSShuhei Matsumoto {
793c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_portal_grp	*pg;
794c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_pg_map	*pg_map;
795c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_init_grp	*ig;
796c5e5047dSShuhei Matsumoto 	struct spdk_iscsi_ig_map	*ig_map;
797c5e5047dSShuhei Matsumoto 	bool				new_pg_map = false;
798c5e5047dSShuhei Matsumoto 
799a587a107SShuhei Matsumoto 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
800c5e5047dSShuhei Matsumoto 	if (pg == NULL) {
801c5e5047dSShuhei Matsumoto 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
802c5e5047dSShuhei Matsumoto 		return -ENOENT;
803c5e5047dSShuhei Matsumoto 	}
804df53885bSShuhei Matsumoto 	ig = iscsi_init_grp_find_by_tag(ig_tag);
805c5e5047dSShuhei Matsumoto 	if (ig == NULL) {
806c5e5047dSShuhei Matsumoto 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
807c5e5047dSShuhei Matsumoto 		return -ENOENT;
808c5e5047dSShuhei Matsumoto 	}
809c5e5047dSShuhei Matsumoto 
810c5e5047dSShuhei Matsumoto 	/* get existing pg_map or create new pg_map and add it to target */
811893e02a5SShuhei Matsumoto 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
812c5e5047dSShuhei Matsumoto 	if (pg_map == NULL) {
813893e02a5SShuhei Matsumoto 		pg_map = iscsi_tgt_node_add_pg_map(target, pg);
814c5e5047dSShuhei Matsumoto 		if (pg_map == NULL) {
815c5e5047dSShuhei Matsumoto 			goto failed;
816c5e5047dSShuhei Matsumoto 		}
817c5e5047dSShuhei Matsumoto 		new_pg_map = true;
818c5e5047dSShuhei Matsumoto 	}
819c5e5047dSShuhei Matsumoto 
820c5e5047dSShuhei Matsumoto 	/* create new ig_map and add it to pg_map */
821893e02a5SShuhei Matsumoto 	ig_map = iscsi_pg_map_add_ig_map(pg_map, ig);
822c5e5047dSShuhei Matsumoto 	if (ig_map == NULL) {
823c5e5047dSShuhei Matsumoto 		goto failed;
824c5e5047dSShuhei Matsumoto 	}
825c5e5047dSShuhei Matsumoto 
826c5e5047dSShuhei Matsumoto 	return 0;
827c5e5047dSShuhei Matsumoto 
828c5e5047dSShuhei Matsumoto failed:
829c5e5047dSShuhei Matsumoto 	if (new_pg_map) {
830893e02a5SShuhei Matsumoto 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
831c5e5047dSShuhei Matsumoto 	}
832c5e5047dSShuhei Matsumoto 
83335cd6005SShuhei Matsumoto 	return -1;
83435cd6005SShuhei Matsumoto }
83535cd6005SShuhei Matsumoto 
83635cd6005SShuhei Matsumoto int
iscsi_target_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node * target,int * pg_tag_list,int * ig_tag_list,uint16_t num_maps)8376a313725SShuhei Matsumoto iscsi_target_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
83835cd6005SShuhei Matsumoto 				 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
83935cd6005SShuhei Matsumoto {
84035cd6005SShuhei Matsumoto 	uint16_t i;
84135cd6005SShuhei Matsumoto 	int rc;
84235cd6005SShuhei Matsumoto 
843be1489b9SShuhei Matsumoto 	pthread_mutex_lock(&g_iscsi.mutex);
84435cd6005SShuhei Matsumoto 	for (i = 0; i < num_maps; i++) {
845893e02a5SShuhei Matsumoto 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i],
84635cd6005SShuhei Matsumoto 						  ig_tag_list[i]);
84735cd6005SShuhei Matsumoto 		if (rc != 0) {
84835cd6005SShuhei Matsumoto 			SPDK_ERRLOG("could not add map to target\n");
84935cd6005SShuhei Matsumoto 			goto invalid;
85035cd6005SShuhei Matsumoto 		}
85135cd6005SShuhei Matsumoto 	}
852be1489b9SShuhei Matsumoto 	pthread_mutex_unlock(&g_iscsi.mutex);
85335cd6005SShuhei Matsumoto 	return 0;
85435cd6005SShuhei Matsumoto 
85535cd6005SShuhei Matsumoto invalid:
85635cd6005SShuhei Matsumoto 	for (; i > 0; --i) {
857893e02a5SShuhei Matsumoto 		iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1],
85835cd6005SShuhei Matsumoto 						ig_tag_list[i - 1]);
85935cd6005SShuhei Matsumoto 	}
860be1489b9SShuhei Matsumoto 	pthread_mutex_unlock(&g_iscsi.mutex);
86135cd6005SShuhei Matsumoto 	return -1;
86235cd6005SShuhei Matsumoto }
86335cd6005SShuhei Matsumoto 
86435cd6005SShuhei Matsumoto int
iscsi_target_node_remove_pg_ig_maps(struct spdk_iscsi_tgt_node * target,int * pg_tag_list,int * ig_tag_list,uint16_t num_maps)8656a313725SShuhei Matsumoto iscsi_target_node_remove_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
86635cd6005SShuhei Matsumoto 				    int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
86735cd6005SShuhei Matsumoto {
86835cd6005SShuhei Matsumoto 	uint16_t i;
86935cd6005SShuhei Matsumoto 	int rc;
87035cd6005SShuhei Matsumoto 
871be1489b9SShuhei Matsumoto 	pthread_mutex_lock(&g_iscsi.mutex);
87235cd6005SShuhei Matsumoto 	for (i = 0; i < num_maps; i++) {
873893e02a5SShuhei Matsumoto 		rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i],
87435cd6005SShuhei Matsumoto 						     ig_tag_list[i]);
87535cd6005SShuhei Matsumoto 		if (rc != 0) {
87635cd6005SShuhei Matsumoto 			SPDK_ERRLOG("could not delete map from target\n");
87735cd6005SShuhei Matsumoto 			goto invalid;
87835cd6005SShuhei Matsumoto 		}
87935cd6005SShuhei Matsumoto 	}
880be1489b9SShuhei Matsumoto 	pthread_mutex_unlock(&g_iscsi.mutex);
88135cd6005SShuhei Matsumoto 	return 0;
88235cd6005SShuhei Matsumoto 
88335cd6005SShuhei Matsumoto invalid:
88435cd6005SShuhei Matsumoto 	for (; i > 0; --i) {
885893e02a5SShuhei Matsumoto 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1],
88635cd6005SShuhei Matsumoto 						  ig_tag_list[i - 1]);
88735cd6005SShuhei Matsumoto 		if (rc != 0) {
888893e02a5SShuhei Matsumoto 			iscsi_tgt_node_delete_all_pg_maps(target);
88935cd6005SShuhei Matsumoto 			break;
89035cd6005SShuhei Matsumoto 		}
89135cd6005SShuhei Matsumoto 	}
892be1489b9SShuhei Matsumoto 	pthread_mutex_unlock(&g_iscsi.mutex);
893c5e5047dSShuhei Matsumoto 	return -1;
894c5e5047dSShuhei Matsumoto }
895c5e5047dSShuhei Matsumoto 
8962c8309e0SShuhei Matsumoto int
iscsi_tgt_node_redirect(struct spdk_iscsi_tgt_node * target,int pg_tag,const char * host,const char * port)8972c8309e0SShuhei Matsumoto iscsi_tgt_node_redirect(struct spdk_iscsi_tgt_node *target, int pg_tag,
8982c8309e0SShuhei Matsumoto 			const char *host, const char *port)
8992c8309e0SShuhei Matsumoto {
9002c8309e0SShuhei Matsumoto 	struct spdk_iscsi_portal_grp *pg;
9012c8309e0SShuhei Matsumoto 	struct spdk_iscsi_pg_map *pg_map;
9022c8309e0SShuhei Matsumoto 	struct sockaddr_storage sa;
9032c8309e0SShuhei Matsumoto 
9042c8309e0SShuhei Matsumoto 	if (target == NULL) {
9052c8309e0SShuhei Matsumoto 		return -EINVAL;
9062c8309e0SShuhei Matsumoto 	}
9072c8309e0SShuhei Matsumoto 
9082c8309e0SShuhei Matsumoto 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
9092c8309e0SShuhei Matsumoto 	if (pg == NULL) {
9102c8309e0SShuhei Matsumoto 		SPDK_ERRLOG("Portal group %d is not found.\n", pg_tag);
9112c8309e0SShuhei Matsumoto 		return -EINVAL;
9122c8309e0SShuhei Matsumoto 	}
9132c8309e0SShuhei Matsumoto 
9142c8309e0SShuhei Matsumoto 	if (pg->is_private) {
9152c8309e0SShuhei Matsumoto 		SPDK_ERRLOG("Portal group %d is not public portal group.\n", pg_tag);
9162c8309e0SShuhei Matsumoto 		return -EINVAL;
9172c8309e0SShuhei Matsumoto 	}
9182c8309e0SShuhei Matsumoto 
9192c8309e0SShuhei Matsumoto 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
9202c8309e0SShuhei Matsumoto 	if (pg_map == NULL) {
9212c8309e0SShuhei Matsumoto 		SPDK_ERRLOG("Portal group %d is not mapped.\n", pg_tag);
9222c8309e0SShuhei Matsumoto 		return -EINVAL;
9232c8309e0SShuhei Matsumoto 	}
9242c8309e0SShuhei Matsumoto 
9252c8309e0SShuhei Matsumoto 	if (host == NULL && port == NULL) {
9262c8309e0SShuhei Matsumoto 		/* Clear redirect setting. */
9272c8309e0SShuhei Matsumoto 		memset(pg_map->redirect_host, 0, MAX_PORTAL_ADDR + 1);
9282c8309e0SShuhei Matsumoto 		memset(pg_map->redirect_port, 0, MAX_PORTAL_PORT + 1);
9292c8309e0SShuhei Matsumoto 	} else {
9302c8309e0SShuhei Matsumoto 		if (iscsi_parse_redirect_addr(&sa, host, port) != 0) {
9312c8309e0SShuhei Matsumoto 			SPDK_ERRLOG("IP address-port pair is not valid.\n");
9322c8309e0SShuhei Matsumoto 			return -EINVAL;
9332c8309e0SShuhei Matsumoto 		}
9342c8309e0SShuhei Matsumoto 
9352c8309e0SShuhei Matsumoto 		if (iscsi_portal_grp_find_portal_by_addr(pg, port, host) != NULL) {
9362c8309e0SShuhei Matsumoto 			SPDK_ERRLOG("IP address-port pair must be chosen from a "
9372c8309e0SShuhei Matsumoto 				    "different private portal group\n");
9382c8309e0SShuhei Matsumoto 			return -EINVAL;
9392c8309e0SShuhei Matsumoto 		}
9402c8309e0SShuhei Matsumoto 
9412c8309e0SShuhei Matsumoto 		snprintf(pg_map->redirect_host, MAX_PORTAL_ADDR + 1, "%s", host);
9422c8309e0SShuhei Matsumoto 		snprintf(pg_map->redirect_port, MAX_PORTAL_PORT + 1, "%s", port);
9432c8309e0SShuhei Matsumoto 	}
9442c8309e0SShuhei Matsumoto 
9452c8309e0SShuhei Matsumoto 	return 0;
9462c8309e0SShuhei Matsumoto }
9472c8309e0SShuhei Matsumoto 
948420b2353SShuhei Matsumoto bool
iscsi_tgt_node_is_redirected(struct spdk_iscsi_conn * conn,struct spdk_iscsi_tgt_node * target,char * buf,int buf_len)949420b2353SShuhei Matsumoto iscsi_tgt_node_is_redirected(struct spdk_iscsi_conn *conn,
950420b2353SShuhei Matsumoto 			     struct spdk_iscsi_tgt_node *target,
951420b2353SShuhei Matsumoto 			     char *buf, int buf_len)
952420b2353SShuhei Matsumoto {
953420b2353SShuhei Matsumoto 	struct spdk_iscsi_pg_map *pg_map;
954420b2353SShuhei Matsumoto 
955420b2353SShuhei Matsumoto 	if (conn == NULL || target == NULL || buf == NULL || buf_len == 0) {
956420b2353SShuhei Matsumoto 		return false;
957420b2353SShuhei Matsumoto 	}
958420b2353SShuhei Matsumoto 
959420b2353SShuhei Matsumoto 	pg_map = iscsi_tgt_node_find_pg_map(target, conn->portal->group);
960420b2353SShuhei Matsumoto 	if (pg_map == NULL) {
961420b2353SShuhei Matsumoto 		return false;
962420b2353SShuhei Matsumoto 	}
963420b2353SShuhei Matsumoto 
964420b2353SShuhei Matsumoto 	if (pg_map->redirect_host[0] == '\0' || pg_map->redirect_port[0] == '\0') {
965420b2353SShuhei Matsumoto 		return false;
966420b2353SShuhei Matsumoto 	}
967420b2353SShuhei Matsumoto 
968420b2353SShuhei Matsumoto 	snprintf(buf, buf_len, "%s:%s", pg_map->redirect_host, pg_map->redirect_port);
969420b2353SShuhei Matsumoto 
970420b2353SShuhei Matsumoto 	return true;
971420b2353SShuhei Matsumoto }
972420b2353SShuhei Matsumoto 
973f4e892feSShuhei Matsumoto static int
check_iscsi_name(const char * name)974893e02a5SShuhei Matsumoto check_iscsi_name(const char *name)
975d29384bfSBen Walker {
976d29384bfSBen Walker 	const unsigned char *up = (const unsigned char *) name;
977d29384bfSBen Walker 	size_t n;
978d29384bfSBen Walker 
979e8b65232SChangpeng Liu 	/* valid iSCSI name no larger than 223 bytes */
980e8b65232SChangpeng Liu 	if (strlen(name) > MAX_TARGET_NAME) {
981e8b65232SChangpeng Liu 		return -1;
982e8b65232SChangpeng Liu 	}
983e8b65232SChangpeng Liu 
984d29384bfSBen Walker 	/* valid iSCSI name? */
985d29384bfSBen Walker 	for (n = 0; up[n] != 0; n++) {
98659970a89SDaniel Verkamp 		if (up[n] > 0x00U && up[n] <= 0x2cU) {
987d29384bfSBen Walker 			return -1;
98859970a89SDaniel Verkamp 		}
98959970a89SDaniel Verkamp 		if (up[n] == 0x2fU) {
990d29384bfSBen Walker 			return -1;
99159970a89SDaniel Verkamp 		}
99259970a89SDaniel Verkamp 		if (up[n] >= 0x3bU && up[n] <= 0x40U) {
993d29384bfSBen Walker 			return -1;
99459970a89SDaniel Verkamp 		}
99559970a89SDaniel Verkamp 		if (up[n] >= 0x5bU && up[n] <= 0x60U) {
996d29384bfSBen Walker 			return -1;
99759970a89SDaniel Verkamp 		}
99859970a89SDaniel Verkamp 		if (up[n] >= 0x7bU && up[n] <= 0x7fU) {
999d29384bfSBen Walker 			return -1;
100059970a89SDaniel Verkamp 		}
100159970a89SDaniel Verkamp 		if (isspace(up[n])) {
1002d29384bfSBen Walker 			return -1;
1003d29384bfSBen Walker 		}
100459970a89SDaniel Verkamp 	}
1005d29384bfSBen Walker 	/* valid format? */
1006d29384bfSBen Walker 	if (strncasecmp(name, "iqn.", 4) == 0) {
1007d29384bfSBen Walker 		/* iqn.YYYY-MM.reversed.domain.name */
1008d29384bfSBen Walker 		if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
1009d29384bfSBen Walker 		    || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
1010d29384bfSBen Walker 		    || !isdigit(up[10]) || up[11] != '.') {
1011d29384bfSBen Walker 			SPDK_ERRLOG("invalid iqn format. "
1012d29384bfSBen Walker 				    "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
1013d29384bfSBen Walker 			return -1;
1014d29384bfSBen Walker 		}
1015d29384bfSBen Walker 	} else if (strncasecmp(name, "eui.", 4) == 0) {
1016d29384bfSBen Walker 		/* EUI-64 -> 16bytes */
1017d29384bfSBen Walker 		/* XXX */
1018d29384bfSBen Walker 	} else if (strncasecmp(name, "naa.", 4) == 0) {
1019d29384bfSBen Walker 		/* 64bit -> 16bytes, 128bit -> 32bytes */
1020d29384bfSBen Walker 		/* XXX */
1021d29384bfSBen Walker 	}
1022d29384bfSBen Walker 	/* OK */
1023d29384bfSBen Walker 	return 0;
1024d29384bfSBen Walker }
1025d29384bfSBen Walker 
10264492fd6cSShuhei Matsumoto bool
iscsi_check_chap_params(bool disable,bool require,bool mutual,int group)10276a313725SShuhei Matsumoto iscsi_check_chap_params(bool disable, bool require, bool mutual, int group)
1028de70d712SShuhei Matsumoto {
1029de70d712SShuhei Matsumoto 	if (group < 0) {
1030de70d712SShuhei Matsumoto 		SPDK_ERRLOG("Invalid auth group ID (%d)\n", group);
1031de70d712SShuhei Matsumoto 		return false;
1032de70d712SShuhei Matsumoto 	}
1033afe51a15SShuhei Matsumoto 	if ((!disable && !require && !mutual) ||	/* Auto */
1034afe51a15SShuhei Matsumoto 	    (disable && !require && !mutual) ||	/* None */
1035afe51a15SShuhei Matsumoto 	    (!disable && require && !mutual) ||	/* CHAP */
1036afe51a15SShuhei Matsumoto 	    (!disable && require && mutual)) {	/* CHAP Mutual */
1037de70d712SShuhei Matsumoto 		return true;
1038de70d712SShuhei Matsumoto 	}
1039de70d712SShuhei Matsumoto 	SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n",
1040afe51a15SShuhei Matsumoto 		    disable, require, mutual);
1041de70d712SShuhei Matsumoto 	return false;
1042de70d712SShuhei Matsumoto }
1043de70d712SShuhei Matsumoto 
iscsi_tgt_node_construct(int target_index,const char * name,const char * alias,int * pg_tag_list,int * ig_tag_list,uint16_t num_maps,const char * bdev_name_list[],int * lun_id_list,int num_luns,int queue_depth,bool disable_chap,bool require_chap,bool mutual_chap,int chap_group,bool header_digest,bool data_digest)10446a313725SShuhei Matsumoto struct spdk_iscsi_tgt_node *iscsi_tgt_node_construct(int target_index,
1045d29384bfSBen Walker 		const char *name, const char *alias,
1046d29384bfSBen Walker 		int *pg_tag_list, int *ig_tag_list, uint16_t num_maps,
1047583e9699SShuhei Matsumoto 		const char *bdev_name_list[], int *lun_id_list, int num_luns,
1048d29384bfSBen Walker 		int queue_depth,
1049afe51a15SShuhei Matsumoto 		bool disable_chap, bool require_chap, bool mutual_chap, int chap_group,
1050b94ba1eeSShuhei Matsumoto 		bool header_digest, bool data_digest)
1051d29384bfSBen Walker {
105250ec7389SShuhei Matsumoto 	char				fullname[MAX_TMPBUF];
1053d29384bfSBen Walker 	struct spdk_iscsi_tgt_node	*target;
105435cd6005SShuhei Matsumoto 	int				rc;
1055d29384bfSBen Walker 
10566a313725SShuhei Matsumoto 	if (!iscsi_check_chap_params(disable_chap, require_chap,
1057afe51a15SShuhei Matsumoto 				     mutual_chap, chap_group)) {
1058d29384bfSBen Walker 		return NULL;
1059d29384bfSBen Walker 	}
1060d29384bfSBen Walker 
1061c5e5047dSShuhei Matsumoto 	if (num_maps == 0) {
1062c5e5047dSShuhei Matsumoto 		SPDK_ERRLOG("num_maps = 0\n");
1063d29384bfSBen Walker 		return NULL;
1064d29384bfSBen Walker 	}
1065d29384bfSBen Walker 
1066d29384bfSBen Walker 	if (name == NULL) {
1067d29384bfSBen Walker 		SPDK_ERRLOG("TargetName not found\n");
1068d29384bfSBen Walker 		return NULL;
1069d29384bfSBen Walker 	}
1070d29384bfSBen Walker 
1071d29384bfSBen Walker 	if (strncasecmp(name, "iqn.", 4) != 0
1072d29384bfSBen Walker 	    && strncasecmp(name, "eui.", 4) != 0
1073d29384bfSBen Walker 	    && strncasecmp(name, "naa.", 4) != 0) {
1074be1489b9SShuhei Matsumoto 		snprintf(fullname, sizeof(fullname), "%s:%s", g_iscsi.nodebase, name);
107559970a89SDaniel Verkamp 	} else {
1076d29384bfSBen Walker 		snprintf(fullname, sizeof(fullname), "%s", name);
107759970a89SDaniel Verkamp 	}
1078d29384bfSBen Walker 
1079893e02a5SShuhei Matsumoto 	if (check_iscsi_name(fullname) != 0) {
1080d29384bfSBen Walker 		SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n",
1081d29384bfSBen Walker 			    name);
1082d29384bfSBen Walker 		return NULL;
1083d29384bfSBen Walker 	}
1084d29384bfSBen Walker 
1085d1961b5eSShuhei Matsumoto 	target = calloc(1, sizeof(*target));
1086d29384bfSBen Walker 	if (!target) {
1087d29384bfSBen Walker 		SPDK_ERRLOG("could not allocate target\n");
1088d29384bfSBen Walker 		return NULL;
1089d29384bfSBen Walker 	}
1090d29384bfSBen Walker 
1091d29384bfSBen Walker 	rc = pthread_mutex_init(&target->mutex, NULL);
1092d29384bfSBen Walker 	if (rc != 0) {
1093d29384bfSBen Walker 		SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num);
1094d039746cSShuhei Matsumoto 		iscsi_tgt_node_destruct(target, NULL, NULL);
1095d29384bfSBen Walker 		return NULL;
1096d29384bfSBen Walker 	}
1097d29384bfSBen Walker 
1098d29384bfSBen Walker 	target->num = target_index;
1099d29384bfSBen Walker 
1100d1961b5eSShuhei Matsumoto 	memcpy(target->name, fullname, strlen(fullname));
1101d29384bfSBen Walker 
1102d1961b5eSShuhei Matsumoto 	if (alias != NULL) {
1103d1961b5eSShuhei Matsumoto 		if (strlen(alias) > MAX_TARGET_NAME) {
1104d039746cSShuhei Matsumoto 			iscsi_tgt_node_destruct(target, NULL, NULL);
1105d29384bfSBen Walker 			return NULL;
1106d29384bfSBen Walker 		}
1107d1961b5eSShuhei Matsumoto 		memcpy(target->alias, alias, strlen(alias));
1108d29384bfSBen Walker 	}
1109d29384bfSBen Walker 
1110583e9699SShuhei Matsumoto 	target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns,
111172343bcfSDariusz Stojaczyk 					      SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL);
1112d29384bfSBen Walker 	if (!target->dev) {
1113d29384bfSBen Walker 		SPDK_ERRLOG("Could not construct SCSI device\n");
1114d039746cSShuhei Matsumoto 		iscsi_tgt_node_destruct(target, NULL, NULL);
1115d29384bfSBen Walker 		return NULL;
1116d29384bfSBen Walker 	}
1117d29384bfSBen Walker 
1118c5e5047dSShuhei Matsumoto 	TAILQ_INIT(&target->pg_map_head);
11196a313725SShuhei Matsumoto 	rc = iscsi_target_node_add_pg_ig_maps(target, pg_tag_list,
1120a1c9546fSPawel Kaminski 					      ig_tag_list, num_maps);
1121c5e5047dSShuhei Matsumoto 	if (rc != 0) {
1122d29384bfSBen Walker 		SPDK_ERRLOG("could not add map to target\n");
1123d039746cSShuhei Matsumoto 		iscsi_tgt_node_destruct(target, NULL, NULL);
1124d29384bfSBen Walker 		return NULL;
1125d29384bfSBen Walker 	}
1126d29384bfSBen Walker 
1127afe51a15SShuhei Matsumoto 	target->disable_chap = disable_chap;
1128afe51a15SShuhei Matsumoto 	target->require_chap = require_chap;
1129afe51a15SShuhei Matsumoto 	target->mutual_chap = mutual_chap;
1130afe51a15SShuhei Matsumoto 	target->chap_group = chap_group;
1131d29384bfSBen Walker 	target->header_digest = header_digest;
1132d29384bfSBen Walker 	target->data_digest = data_digest;
1133d29384bfSBen Walker 
1134be1489b9SShuhei Matsumoto 	if (queue_depth > 0 && ((uint32_t)queue_depth <= g_iscsi.MaxQueueDepth)) {
1135d29384bfSBen Walker 		target->queue_depth = queue_depth;
1136e6fea425SShuhei Matsumoto 	} else {
11372172c432STomasz Zawadzki 		SPDK_DEBUGLOG(iscsi, "QueueDepth %d is invalid and %d is used instead.\n",
1138be1489b9SShuhei Matsumoto 			      queue_depth, g_iscsi.MaxQueueDepth);
1139be1489b9SShuhei Matsumoto 		target->queue_depth = g_iscsi.MaxQueueDepth;
1140e6fea425SShuhei Matsumoto 	}
1141d29384bfSBen Walker 
1142893e02a5SShuhei Matsumoto 	rc = iscsi_tgt_node_register(target);
1143f4e892feSShuhei Matsumoto 	if (rc != 0) {
1144f4e892feSShuhei Matsumoto 		SPDK_ERRLOG("register target is failed\n");
1145d039746cSShuhei Matsumoto 		iscsi_tgt_node_destruct(target, NULL, NULL);
1146f4e892feSShuhei Matsumoto 		return NULL;
1147f4e892feSShuhei Matsumoto 	}
1148d29384bfSBen Walker 
1149d29384bfSBen Walker 	return target;
1150d29384bfSBen Walker }
1151d29384bfSBen Walker 
1152f4e892feSShuhei Matsumoto void
iscsi_shutdown_tgt_nodes(void)11536a313725SShuhei Matsumoto iscsi_shutdown_tgt_nodes(void)
1154d29384bfSBen Walker {
1155da98b92bSShuhei Matsumoto 	struct spdk_iscsi_tgt_node *target;
1156d29384bfSBen Walker 
1157be1489b9SShuhei Matsumoto 	pthread_mutex_lock(&g_iscsi.mutex);
1158be1489b9SShuhei Matsumoto 	while (!TAILQ_EMPTY(&g_iscsi.target_head)) {
1159be1489b9SShuhei Matsumoto 		target = TAILQ_FIRST(&g_iscsi.target_head);
1160be1489b9SShuhei Matsumoto 		TAILQ_REMOVE(&g_iscsi.target_head, target, tailq);
1161da98b92bSShuhei Matsumoto 
1162be1489b9SShuhei Matsumoto 		pthread_mutex_unlock(&g_iscsi.mutex);
1163da98b92bSShuhei Matsumoto 
1164d039746cSShuhei Matsumoto 		iscsi_tgt_node_destruct(target, NULL, NULL);
1165da98b92bSShuhei Matsumoto 
1166be1489b9SShuhei Matsumoto 		pthread_mutex_lock(&g_iscsi.mutex);
1167d29384bfSBen Walker 	}
1168be1489b9SShuhei Matsumoto 	pthread_mutex_unlock(&g_iscsi.mutex);
1169d29384bfSBen Walker }
1170d29384bfSBen Walker 
1171787b5efeSShuhei Matsumoto void
iscsi_shutdown_tgt_node_by_name(const char * target_name,iscsi_tgt_node_destruct_cb cb_fn,void * cb_arg)11726a313725SShuhei Matsumoto iscsi_shutdown_tgt_node_by_name(const char *target_name,
1173787b5efeSShuhei Matsumoto 				iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
1174d29384bfSBen Walker {
1175d29384bfSBen Walker 	struct spdk_iscsi_tgt_node *target;
1176d29384bfSBen Walker 
1177be1489b9SShuhei Matsumoto 	pthread_mutex_lock(&g_iscsi.mutex);
11786a313725SShuhei Matsumoto 	target = iscsi_find_tgt_node(target_name);
1179f4e892feSShuhei Matsumoto 	if (target != NULL) {
1180893e02a5SShuhei Matsumoto 		iscsi_tgt_node_unregister(target);
1181be1489b9SShuhei Matsumoto 		pthread_mutex_unlock(&g_iscsi.mutex);
1182f4e892feSShuhei Matsumoto 
1183d039746cSShuhei Matsumoto 		iscsi_tgt_node_destruct(target, cb_fn, cb_arg);
1184da98b92bSShuhei Matsumoto 
1185787b5efeSShuhei Matsumoto 		return;
1186d29384bfSBen Walker 	}
1187be1489b9SShuhei Matsumoto 	pthread_mutex_unlock(&g_iscsi.mutex);
1188d29384bfSBen Walker 
1189787b5efeSShuhei Matsumoto 	if (cb_fn) {
1190787b5efeSShuhei Matsumoto 		cb_fn(cb_arg, -ENOENT);
1191787b5efeSShuhei Matsumoto 	}
1192d29384bfSBen Walker }
1193d29384bfSBen Walker 
11948cc7b0bcSShuhei Matsumoto bool
iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node * target)11956a313725SShuhei Matsumoto iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target)
11968cc7b0bcSShuhei Matsumoto {
11978cc7b0bcSShuhei Matsumoto 	return target->destructed;
11988cc7b0bcSShuhei Matsumoto }
11998cc7b0bcSShuhei Matsumoto 
1200d29384bfSBen Walker int
iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn * conn,struct spdk_iscsi_tgt_node * target)12016a313725SShuhei Matsumoto iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1202d29384bfSBen Walker 			    struct spdk_iscsi_tgt_node *target)
1203d29384bfSBen Walker {
1204594f46d7SShuhei Matsumoto 	struct spdk_scsi_lun *lun;
1205d29384bfSBen Walker 	struct spdk_iscsi_task *task;
1206d29384bfSBen Walker 
1207594f46d7SShuhei Matsumoto 	for (lun = spdk_scsi_dev_get_first_lun(target->dev); lun != NULL;
1208594f46d7SShuhei Matsumoto 	     lun = spdk_scsi_dev_get_next_lun(lun)) {
1209d29384bfSBen Walker 		/* we create a fake management task per LUN to cleanup */
12103570494dSShuhei Matsumoto 		task = iscsi_task_get(conn, NULL, iscsi_task_mgmt_cpl);
1211d29384bfSBen Walker 		if (!task) {
1212d29384bfSBen Walker 			SPDK_ERRLOG("Unable to acquire task\n");
1213d29384bfSBen Walker 			return -1;
1214d29384bfSBen Walker 		}
1215d29384bfSBen Walker 
1216d29384bfSBen Walker 		task->scsi.target_port = conn->target_port;
1217d29384bfSBen Walker 		task->scsi.initiator_port = conn->initiator_port;
1218a3738d90SDaniel Verkamp 		task->scsi.lun = lun;
1219d29384bfSBen Walker 
12202c01ded3Szhenwei pi 		iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_TARGET_RESET);
1221d29384bfSBen Walker 	}
1222d29384bfSBen Walker 
1223d29384bfSBen Walker 	return 0;
1224d29384bfSBen Walker }
1225d29384bfSBen Walker 
12268dd1cd21SBen Walker void
iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp * portal_group,struct spdk_iscsi_init_grp * initiator_group)12278dd1cd21SBen Walker iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1228d29384bfSBen Walker 			  struct spdk_iscsi_init_grp *initiator_group)
1229d29384bfSBen Walker {
1230d29384bfSBen Walker 	struct spdk_iscsi_tgt_node *target;
1231d29384bfSBen Walker 
1232be1489b9SShuhei Matsumoto 	pthread_mutex_lock(&g_iscsi.mutex);
1233be1489b9SShuhei Matsumoto 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1234d29384bfSBen Walker 		if (portal_group) {
1235893e02a5SShuhei Matsumoto 			iscsi_tgt_node_delete_pg_map(target, portal_group);
1236d29384bfSBen Walker 		}
1237d29384bfSBen Walker 		if (initiator_group) {
1238893e02a5SShuhei Matsumoto 			iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1239d29384bfSBen Walker 		}
1240d29384bfSBen Walker 	}
1241be1489b9SShuhei Matsumoto 	pthread_mutex_unlock(&g_iscsi.mutex);
1242d29384bfSBen Walker }
12433b3c6002SShuhei Matsumoto 
12443b3c6002SShuhei Matsumoto int
iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node * target,const char * bdev_name,int lun_id)12456a313725SShuhei Matsumoto iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target,
12463b3c6002SShuhei Matsumoto 		       const char *bdev_name, int lun_id)
12473b3c6002SShuhei Matsumoto {
12483b3c6002SShuhei Matsumoto 	struct spdk_scsi_dev *dev;
12493b3c6002SShuhei Matsumoto 	int rc;
12503b3c6002SShuhei Matsumoto 
12513b3c6002SShuhei Matsumoto 	if (target->num_active_conns > 0) {
12523b3c6002SShuhei Matsumoto 		SPDK_ERRLOG("Target has active connections (count=%d)\n",
12533b3c6002SShuhei Matsumoto 			    target->num_active_conns);
12543b3c6002SShuhei Matsumoto 		return -1;
12553b3c6002SShuhei Matsumoto 	}
12563b3c6002SShuhei Matsumoto 
12578215c0acSShuhei Matsumoto 	if (lun_id < -1) {
12588215c0acSShuhei Matsumoto 		SPDK_ERRLOG("Specified LUN ID (%d) is negative\n", lun_id);
12593b3c6002SShuhei Matsumoto 		return -1;
12603b3c6002SShuhei Matsumoto 	}
12613b3c6002SShuhei Matsumoto 
12623b3c6002SShuhei Matsumoto 	dev = target->dev;
12633b3c6002SShuhei Matsumoto 	if (dev == NULL) {
12643b3c6002SShuhei Matsumoto 		SPDK_ERRLOG("SCSI device is not found\n");
12653b3c6002SShuhei Matsumoto 		return -1;
12663b3c6002SShuhei Matsumoto 	}
12673b3c6002SShuhei Matsumoto 
12683b3c6002SShuhei Matsumoto 	rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL);
12693b3c6002SShuhei Matsumoto 	if (rc != 0) {
12703b3c6002SShuhei Matsumoto 		SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n");
12713b3c6002SShuhei Matsumoto 		return -1;
12723b3c6002SShuhei Matsumoto 	}
12733b3c6002SShuhei Matsumoto 
12743b3c6002SShuhei Matsumoto 	return 0;
12753b3c6002SShuhei Matsumoto }
12769114e3a2SShuhei Matsumoto 
1277844735c9SShuhei Matsumoto int
iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node * target,bool disable_chap,bool require_chap,bool mutual_chap,int32_t chap_group)12786a313725SShuhei Matsumoto iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target,
1279844735c9SShuhei Matsumoto 			       bool disable_chap, bool require_chap,
1280844735c9SShuhei Matsumoto 			       bool mutual_chap, int32_t chap_group)
1281844735c9SShuhei Matsumoto {
12826a313725SShuhei Matsumoto 	if (!iscsi_check_chap_params(disable_chap, require_chap,
1283844735c9SShuhei Matsumoto 				     mutual_chap, chap_group)) {
1284844735c9SShuhei Matsumoto 		return -EINVAL;
1285844735c9SShuhei Matsumoto 	}
1286844735c9SShuhei Matsumoto 
1287844735c9SShuhei Matsumoto 	pthread_mutex_lock(&target->mutex);
1288844735c9SShuhei Matsumoto 	target->disable_chap = disable_chap;
1289844735c9SShuhei Matsumoto 	target->require_chap = require_chap;
1290844735c9SShuhei Matsumoto 	target->mutual_chap = mutual_chap;
1291844735c9SShuhei Matsumoto 	target->chap_group = chap_group;
1292844735c9SShuhei Matsumoto 	pthread_mutex_unlock(&target->mutex);
1293844735c9SShuhei Matsumoto 
1294844735c9SShuhei Matsumoto 	return 0;
1295844735c9SShuhei Matsumoto }
1296844735c9SShuhei Matsumoto 
12979114e3a2SShuhei Matsumoto static void
iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node * target,struct spdk_json_write_ctx * w)1298893e02a5SShuhei Matsumoto iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target,
12999114e3a2SShuhei Matsumoto 			 struct spdk_json_write_ctx *w)
13009114e3a2SShuhei Matsumoto {
13019114e3a2SShuhei Matsumoto 	struct spdk_iscsi_pg_map *pg_map;
13029114e3a2SShuhei Matsumoto 	struct spdk_iscsi_ig_map *ig_map;
1303594f46d7SShuhei Matsumoto 	struct spdk_scsi_lun *lun;
13049114e3a2SShuhei Matsumoto 
13059114e3a2SShuhei Matsumoto 	spdk_json_write_object_begin(w);
13069114e3a2SShuhei Matsumoto 
1307abfed229SShuhei Matsumoto 	spdk_json_write_named_string(w, "name", target->name);
13089114e3a2SShuhei Matsumoto 
1309d1961b5eSShuhei Matsumoto 	if (target->alias[0] != '\0') {
1310abfed229SShuhei Matsumoto 		spdk_json_write_named_string(w, "alias_name", target->alias);
13119114e3a2SShuhei Matsumoto 	}
13129114e3a2SShuhei Matsumoto 
1313abfed229SShuhei Matsumoto 	spdk_json_write_named_array_begin(w, "pg_ig_maps");
13149114e3a2SShuhei Matsumoto 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
13159114e3a2SShuhei Matsumoto 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
13169114e3a2SShuhei Matsumoto 			spdk_json_write_object_begin(w);
1317abfed229SShuhei Matsumoto 			spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag);
1318abfed229SShuhei Matsumoto 			spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag);
13199114e3a2SShuhei Matsumoto 			spdk_json_write_object_end(w);
13209114e3a2SShuhei Matsumoto 		}
13219114e3a2SShuhei Matsumoto 	}
13229114e3a2SShuhei Matsumoto 	spdk_json_write_array_end(w);
13239114e3a2SShuhei Matsumoto 
1324abfed229SShuhei Matsumoto 	spdk_json_write_named_array_begin(w, "luns");
1325594f46d7SShuhei Matsumoto 	for (lun = spdk_scsi_dev_get_first_lun(target->dev); lun != NULL;
1326594f46d7SShuhei Matsumoto 	     lun = spdk_scsi_dev_get_next_lun(lun)) {
13279114e3a2SShuhei Matsumoto 		spdk_json_write_object_begin(w);
1328abfed229SShuhei Matsumoto 		spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1329718440bcSShuhei Matsumoto 		spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun));
13309114e3a2SShuhei Matsumoto 		spdk_json_write_object_end(w);
13319114e3a2SShuhei Matsumoto 	}
13329114e3a2SShuhei Matsumoto 	spdk_json_write_array_end(w);
13339114e3a2SShuhei Matsumoto 
1334abfed229SShuhei Matsumoto 	spdk_json_write_named_int32(w, "queue_depth", target->queue_depth);
13359114e3a2SShuhei Matsumoto 
1336abfed229SShuhei Matsumoto 	spdk_json_write_named_bool(w, "disable_chap", target->disable_chap);
1337abfed229SShuhei Matsumoto 	spdk_json_write_named_bool(w, "require_chap", target->require_chap);
1338abfed229SShuhei Matsumoto 	spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap);
1339abfed229SShuhei Matsumoto 	spdk_json_write_named_int32(w, "chap_group", target->chap_group);
13409114e3a2SShuhei Matsumoto 
1341abfed229SShuhei Matsumoto 	spdk_json_write_named_bool(w, "header_digest", target->header_digest);
1342abfed229SShuhei Matsumoto 	spdk_json_write_named_bool(w, "data_digest", target->data_digest);
13439114e3a2SShuhei Matsumoto 
13449114e3a2SShuhei Matsumoto 	spdk_json_write_object_end(w);
13459114e3a2SShuhei Matsumoto }
13469114e3a2SShuhei Matsumoto 
13479114e3a2SShuhei Matsumoto static void
iscsi_tgt_node_histogram_config_json(struct spdk_iscsi_tgt_node * target,struct spdk_json_write_ctx * w)1348*f0dbfd08SChangqi Lu iscsi_tgt_node_histogram_config_json(struct spdk_iscsi_tgt_node *target,
1349*f0dbfd08SChangqi Lu 				     struct spdk_json_write_ctx *w)
1350*f0dbfd08SChangqi Lu {
1351*f0dbfd08SChangqi Lu 	if (!target->histogram) {
1352*f0dbfd08SChangqi Lu 		return;
1353*f0dbfd08SChangqi Lu 	}
1354*f0dbfd08SChangqi Lu 
1355*f0dbfd08SChangqi Lu 	spdk_json_write_object_begin(w);
1356*f0dbfd08SChangqi Lu 	spdk_json_write_named_string(w, "method", "iscsi_enable_histogram");
1357*f0dbfd08SChangqi Lu 
1358*f0dbfd08SChangqi Lu 	spdk_json_write_named_object_begin(w, "params");
1359*f0dbfd08SChangqi Lu 	spdk_json_write_named_string(w, "name", target->name);
1360*f0dbfd08SChangqi Lu 
1361*f0dbfd08SChangqi Lu 	spdk_json_write_named_bool(w, "enable", true);
1362*f0dbfd08SChangqi Lu 	spdk_json_write_object_end(w);
1363*f0dbfd08SChangqi Lu 
1364*f0dbfd08SChangqi Lu 	spdk_json_write_object_end(w);
1365*f0dbfd08SChangqi Lu }
1366*f0dbfd08SChangqi Lu 
1367*f0dbfd08SChangqi Lu static void
iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node * target,struct spdk_json_write_ctx * w)1368893e02a5SShuhei Matsumoto iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target,
13699114e3a2SShuhei Matsumoto 			   struct spdk_json_write_ctx *w)
13709114e3a2SShuhei Matsumoto {
13719114e3a2SShuhei Matsumoto 	spdk_json_write_object_begin(w);
13729114e3a2SShuhei Matsumoto 
1373758b2f94SPawel Kaminski 	spdk_json_write_named_string(w, "method", "iscsi_create_target_node");
13749114e3a2SShuhei Matsumoto 
13759114e3a2SShuhei Matsumoto 	spdk_json_write_name(w, "params");
1376893e02a5SShuhei Matsumoto 	iscsi_tgt_node_info_json(target, w);
13779114e3a2SShuhei Matsumoto 
13789114e3a2SShuhei Matsumoto 	spdk_json_write_object_end(w);
1379*f0dbfd08SChangqi Lu 
1380*f0dbfd08SChangqi Lu 	iscsi_tgt_node_histogram_config_json(target, w);
13819114e3a2SShuhei Matsumoto }
13829114e3a2SShuhei Matsumoto 
13839114e3a2SShuhei Matsumoto void
iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx * w)13846a313725SShuhei Matsumoto iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w)
13859114e3a2SShuhei Matsumoto {
13869114e3a2SShuhei Matsumoto 	struct spdk_iscsi_tgt_node *target;
13879114e3a2SShuhei Matsumoto 
1388be1489b9SShuhei Matsumoto 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1389893e02a5SShuhei Matsumoto 		iscsi_tgt_node_info_json(target, w);
13909114e3a2SShuhei Matsumoto 	}
13919114e3a2SShuhei Matsumoto }
13929114e3a2SShuhei Matsumoto 
13939114e3a2SShuhei Matsumoto void
iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx * w)13946a313725SShuhei Matsumoto iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w)
13959114e3a2SShuhei Matsumoto {
13969114e3a2SShuhei Matsumoto 	struct spdk_iscsi_tgt_node *target;
13979114e3a2SShuhei Matsumoto 
1398be1489b9SShuhei Matsumoto 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1399893e02a5SShuhei Matsumoto 		iscsi_tgt_node_config_json(target, w);
14009114e3a2SShuhei Matsumoto 	}
14019114e3a2SShuhei Matsumoto }
140290ba272cSChangqi Lu 
140390ba272cSChangqi Lu int
iscsi_tgt_node_enable_histogram(struct spdk_iscsi_tgt_node * target,bool enable)140490ba272cSChangqi Lu iscsi_tgt_node_enable_histogram(struct spdk_iscsi_tgt_node *target, bool enable)
140590ba272cSChangqi Lu {
140690ba272cSChangqi Lu 	if (enable) {
140790ba272cSChangqi Lu 		if (!target->histogram) {
140890ba272cSChangqi Lu 			target->histogram = spdk_histogram_data_alloc();
140990ba272cSChangqi Lu 			if (target->histogram == NULL) {
141090ba272cSChangqi Lu 				SPDK_ERRLOG("could not allocate histogram\n");
141190ba272cSChangqi Lu 				return -ENOMEM;
141290ba272cSChangqi Lu 			}
141390ba272cSChangqi Lu 		}
141490ba272cSChangqi Lu 	} else {
141590ba272cSChangqi Lu 		if (target->histogram) {
141690ba272cSChangqi Lu 			spdk_histogram_data_free(target->histogram);
141790ba272cSChangqi Lu 			target->histogram = NULL;
141890ba272cSChangqi Lu 		}
141990ba272cSChangqi Lu 	}
142090ba272cSChangqi Lu 
142190ba272cSChangqi Lu 	return 0;
142290ba272cSChangqi Lu }
1423