xref: /spdk/lib/iscsi/portal_grp.c (revision a1246d4df98c50190ddaa9be43d18e00f2a83c12)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
3  *   Copyright (C) 2016 Intel Corporation.
4  *   All rights reserved.
5  */
6 
7 #include "spdk/stdinc.h"
8 
9 #include "spdk/sock.h"
10 #include "spdk/string.h"
11 
12 #include "spdk/log.h"
13 
14 #include "iscsi/iscsi.h"
15 #include "iscsi/conn.h"
16 #include "iscsi/portal_grp.h"
17 #include "iscsi/tgt_node.h"
18 
19 #define PORTNUMSTRLEN 32
20 #define ACCEPT_TIMEOUT_US 1000 /* 1ms */
21 
22 static int
iscsi_portal_accept(void * arg)23 iscsi_portal_accept(void *arg)
24 {
25 	struct spdk_iscsi_portal	*portal = arg;
26 	struct spdk_sock		*sock;
27 	int				rc;
28 	int				count = 0;
29 
30 	if (portal->sock == NULL) {
31 		return -1;
32 	}
33 
34 	while (1) {
35 		sock = spdk_sock_accept(portal->sock);
36 		if (sock != NULL) {
37 			rc = iscsi_conn_construct(portal, sock);
38 			if (rc < 0) {
39 				spdk_sock_close(&sock);
40 				SPDK_ERRLOG("spdk_iscsi_connection_construct() failed\n");
41 				break;
42 			}
43 			count++;
44 		} else {
45 			if (errno != EAGAIN && errno != EWOULDBLOCK) {
46 				SPDK_ERRLOG("accept error(%d): %s\n", errno, spdk_strerror(errno));
47 			}
48 			break;
49 		}
50 	}
51 
52 	return count;
53 }
54 
55 static struct spdk_iscsi_portal *
iscsi_portal_find_by_addr(const char * host,const char * port)56 iscsi_portal_find_by_addr(const char *host, const char *port)
57 {
58 	struct spdk_iscsi_portal *p;
59 
60 	TAILQ_FOREACH(p, &g_iscsi.portal_head, g_tailq) {
61 		if (!strcmp(p->host, host) && !strcmp(p->port, port)) {
62 			return p;
63 		}
64 	}
65 
66 	return NULL;
67 }
68 
69 /* Assumes caller allocated host and port strings on the heap */
70 struct spdk_iscsi_portal *
iscsi_portal_create(const char * host,const char * port)71 iscsi_portal_create(const char *host, const char *port)
72 {
73 	struct spdk_iscsi_portal *p = NULL, *tmp;
74 
75 	assert(host != NULL);
76 	assert(port != NULL);
77 
78 	if (strlen(host) > MAX_PORTAL_ADDR || strlen(port) > MAX_PORTAL_PORT) {
79 		return NULL;
80 	}
81 
82 	p = calloc(1, sizeof(*p));
83 	if (!p) {
84 		SPDK_ERRLOG("calloc() failed for portal\n");
85 		return NULL;
86 	}
87 
88 	/* check and overwrite abbreviation of wildcard */
89 	if (strcasecmp(host, "[*]") == 0) {
90 		SPDK_WARNLOG("Please use \"[::]\" as IPv6 wildcard\n");
91 		SPDK_WARNLOG("Convert \"[*]\" to \"[::]\" automatically\n");
92 		SPDK_WARNLOG("(Use of \"[*]\" will be deprecated in a future release)");
93 		snprintf(p->host, sizeof(p->host), "[::]");
94 	} else if (strcasecmp(host, "*") == 0) {
95 		SPDK_WARNLOG("Please use \"0.0.0.0\" as IPv4 wildcard\n");
96 		SPDK_WARNLOG("Convert \"*\" to \"0.0.0.0\" automatically\n");
97 		SPDK_WARNLOG("(Use of \"[*]\" will be deprecated in a future release)");
98 		snprintf(p->host, sizeof(p->host), "0.0.0.0");
99 	} else {
100 		memcpy(p->host, host, strlen(host));
101 	}
102 
103 	memcpy(p->port, port, strlen(port));
104 
105 	p->sock = NULL;
106 	p->group = NULL; /* set at a later time by caller */
107 	p->acceptor_poller = NULL;
108 
109 	pthread_mutex_lock(&g_iscsi.mutex);
110 	tmp = iscsi_portal_find_by_addr(host, port);
111 	if (tmp != NULL) {
112 		pthread_mutex_unlock(&g_iscsi.mutex);
113 		SPDK_ERRLOG("portal (%s, %s) already exists\n", host, port);
114 		goto error_out;
115 	}
116 
117 	TAILQ_INSERT_TAIL(&g_iscsi.portal_head, p, g_tailq);
118 	pthread_mutex_unlock(&g_iscsi.mutex);
119 
120 	return p;
121 
122 error_out:
123 	free(p);
124 
125 	return NULL;
126 }
127 
128 void
iscsi_portal_destroy(struct spdk_iscsi_portal * p)129 iscsi_portal_destroy(struct spdk_iscsi_portal *p)
130 {
131 	assert(p != NULL);
132 
133 	SPDK_DEBUGLOG(iscsi, "iscsi_portal_destroy\n");
134 
135 	pthread_mutex_lock(&g_iscsi.mutex);
136 	TAILQ_REMOVE(&g_iscsi.portal_head, p, g_tailq);
137 	pthread_mutex_unlock(&g_iscsi.mutex);
138 
139 	free(p);
140 
141 }
142 
143 static int
iscsi_portal_open(struct spdk_iscsi_portal * p)144 iscsi_portal_open(struct spdk_iscsi_portal *p)
145 {
146 	struct spdk_sock *sock;
147 	int port;
148 
149 	if (p->sock != NULL) {
150 		SPDK_ERRLOG("portal (%s, %s) is already opened\n",
151 			    p->host, p->port);
152 		return -1;
153 	}
154 
155 	port = (int)strtol(p->port, NULL, 0);
156 	if (port <= 0 || port > 65535) {
157 		SPDK_ERRLOG("invalid port %s\n", p->port);
158 		return -1;
159 	}
160 
161 	sock = spdk_sock_listen(p->host, port, NULL);
162 	if (sock == NULL) {
163 		SPDK_ERRLOG("listen error %.64s.%d\n", p->host, port);
164 		return -1;
165 	}
166 
167 	p->sock = sock;
168 
169 	/*
170 	 * When the portal is created by config file, incoming connection
171 	 * requests for the socket are pended to accept until reactors start.
172 	 * However the gap between listen() and accept() will be slight and
173 	 * the requests will be queued by the nonzero backlog of the socket
174 	 * or resend by TCP.
175 	 */
176 	p->acceptor_poller = SPDK_POLLER_REGISTER(iscsi_portal_accept, p, ACCEPT_TIMEOUT_US);
177 
178 	return 0;
179 }
180 
181 static void
iscsi_portal_close(struct spdk_iscsi_portal * p)182 iscsi_portal_close(struct spdk_iscsi_portal *p)
183 {
184 	if (p->sock) {
185 		SPDK_DEBUGLOG(iscsi, "close portal (%s, %s)\n",
186 			      p->host, p->port);
187 		spdk_poller_unregister(&p->acceptor_poller);
188 		spdk_sock_close(&p->sock);
189 	}
190 }
191 
192 static void
iscsi_portal_pause(struct spdk_iscsi_portal * p)193 iscsi_portal_pause(struct spdk_iscsi_portal *p)
194 {
195 	assert(p->acceptor_poller != NULL);
196 
197 	spdk_poller_pause(p->acceptor_poller);
198 }
199 
200 static void
iscsi_portal_resume(struct spdk_iscsi_portal * p)201 iscsi_portal_resume(struct spdk_iscsi_portal *p)
202 {
203 	assert(p->acceptor_poller != NULL);
204 
205 	spdk_poller_resume(p->acceptor_poller);
206 }
207 
208 int
iscsi_parse_redirect_addr(struct sockaddr_storage * sa,const char * host,const char * port)209 iscsi_parse_redirect_addr(struct sockaddr_storage *sa,
210 			  const char *host, const char *port)
211 {
212 	struct addrinfo hints, *res;
213 	int rc;
214 
215 	if (host == NULL || port == NULL) {
216 		return -EINVAL;
217 	}
218 
219 	memset(&hints, 0, sizeof(hints));
220 	hints.ai_family = PF_UNSPEC;
221 	hints.ai_socktype = SOCK_STREAM;
222 	hints.ai_flags = AI_NUMERICSERV;
223 	hints.ai_flags |= AI_NUMERICHOST;
224 	rc = getaddrinfo(host, port, &hints, &res);
225 	if (rc != 0) {
226 		SPDK_ERRLOG("getaddinrfo failed: %s (%d)\n", gai_strerror(rc), rc);
227 		return -(abs(rc));
228 	}
229 
230 	if (res->ai_addrlen > sizeof(*sa)) {
231 		SPDK_ERRLOG("getaddrinfo() ai_addrlen %zu too large\n",
232 			    (size_t)res->ai_addrlen);
233 		rc = -EINVAL;
234 	} else {
235 		memcpy(sa, res->ai_addr, res->ai_addrlen);
236 	}
237 
238 	freeaddrinfo(res);
239 	return rc;
240 }
241 
242 struct spdk_iscsi_portal_grp *
iscsi_portal_grp_create(int tag,bool is_private)243 iscsi_portal_grp_create(int tag, bool is_private)
244 {
245 	struct spdk_iscsi_portal_grp *pg = malloc(sizeof(*pg));
246 
247 	if (!pg) {
248 		SPDK_ERRLOG("malloc() failed for portal group\n");
249 		return NULL;
250 	}
251 
252 	pg->ref = 0;
253 	pg->tag = tag;
254 	pg->is_private = is_private;
255 
256 	pthread_mutex_lock(&g_iscsi.mutex);
257 	pg->disable_chap = g_iscsi.disable_chap;
258 	pg->require_chap = g_iscsi.require_chap;
259 	pg->mutual_chap = g_iscsi.mutual_chap;
260 	pg->chap_group = g_iscsi.chap_group;
261 	pthread_mutex_unlock(&g_iscsi.mutex);
262 
263 	TAILQ_INIT(&pg->head);
264 
265 	return pg;
266 }
267 
268 void
iscsi_portal_grp_destroy(struct spdk_iscsi_portal_grp * pg)269 iscsi_portal_grp_destroy(struct spdk_iscsi_portal_grp *pg)
270 {
271 	struct spdk_iscsi_portal	*p;
272 
273 	assert(pg != NULL);
274 
275 	SPDK_DEBUGLOG(iscsi, "iscsi_portal_grp_destroy\n");
276 	while (!TAILQ_EMPTY(&pg->head)) {
277 		p = TAILQ_FIRST(&pg->head);
278 		TAILQ_REMOVE(&pg->head, p, per_pg_tailq);
279 		iscsi_portal_destroy(p);
280 	}
281 	free(pg);
282 }
283 
284 int
iscsi_portal_grp_register(struct spdk_iscsi_portal_grp * pg)285 iscsi_portal_grp_register(struct spdk_iscsi_portal_grp *pg)
286 {
287 	int rc = -1;
288 	struct spdk_iscsi_portal_grp *tmp;
289 
290 	assert(pg != NULL);
291 
292 	pthread_mutex_lock(&g_iscsi.mutex);
293 	tmp = iscsi_portal_grp_find_by_tag(pg->tag);
294 	if (tmp == NULL) {
295 		TAILQ_INSERT_TAIL(&g_iscsi.pg_head, pg, tailq);
296 		rc = 0;
297 	}
298 	pthread_mutex_unlock(&g_iscsi.mutex);
299 	return rc;
300 }
301 
302 void
iscsi_portal_grp_add_portal(struct spdk_iscsi_portal_grp * pg,struct spdk_iscsi_portal * p)303 iscsi_portal_grp_add_portal(struct spdk_iscsi_portal_grp *pg,
304 			    struct spdk_iscsi_portal *p)
305 {
306 	assert(pg != NULL);
307 	assert(p != NULL);
308 
309 	p->group = pg;
310 	TAILQ_INSERT_TAIL(&pg->head, p, per_pg_tailq);
311 }
312 
313 struct spdk_iscsi_portal *
iscsi_portal_grp_find_portal_by_addr(struct spdk_iscsi_portal_grp * pg,const char * host,const char * port)314 iscsi_portal_grp_find_portal_by_addr(struct spdk_iscsi_portal_grp *pg,
315 				     const char *host, const char *port)
316 {
317 	struct spdk_iscsi_portal *p;
318 
319 	TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
320 		if (!strcmp(p->host, host) && !strcmp(p->port, port)) {
321 			return p;
322 		}
323 	}
324 
325 	return NULL;
326 }
327 
328 int
iscsi_portal_grp_set_chap_params(struct spdk_iscsi_portal_grp * pg,bool disable_chap,bool require_chap,bool mutual_chap,int32_t chap_group)329 iscsi_portal_grp_set_chap_params(struct spdk_iscsi_portal_grp *pg,
330 				 bool disable_chap, bool require_chap,
331 				 bool mutual_chap, int32_t chap_group)
332 {
333 	if (!iscsi_check_chap_params(disable_chap, require_chap,
334 				     mutual_chap, chap_group)) {
335 		return -EINVAL;
336 	}
337 
338 	pg->disable_chap = disable_chap;
339 	pg->require_chap = require_chap;
340 	pg->mutual_chap = mutual_chap;
341 	pg->chap_group = chap_group;
342 
343 	return 0;
344 }
345 
346 struct spdk_iscsi_portal_grp *
iscsi_portal_grp_find_by_tag(int tag)347 iscsi_portal_grp_find_by_tag(int tag)
348 {
349 	struct spdk_iscsi_portal_grp *pg;
350 
351 	TAILQ_FOREACH(pg, &g_iscsi.pg_head, tailq) {
352 		if (pg->tag == tag) {
353 			return pg;
354 		}
355 	}
356 
357 	return NULL;
358 }
359 
360 void
iscsi_portal_grps_destroy(void)361 iscsi_portal_grps_destroy(void)
362 {
363 	struct spdk_iscsi_portal_grp *pg;
364 
365 	SPDK_DEBUGLOG(iscsi, "iscsi_portal_grps_destroy\n");
366 	pthread_mutex_lock(&g_iscsi.mutex);
367 	while (!TAILQ_EMPTY(&g_iscsi.pg_head)) {
368 		pg = TAILQ_FIRST(&g_iscsi.pg_head);
369 		TAILQ_REMOVE(&g_iscsi.pg_head, pg, tailq);
370 		pthread_mutex_unlock(&g_iscsi.mutex);
371 		iscsi_portal_grp_destroy(pg);
372 		pthread_mutex_lock(&g_iscsi.mutex);
373 	}
374 	pthread_mutex_unlock(&g_iscsi.mutex);
375 }
376 
377 int
iscsi_portal_grp_open(struct spdk_iscsi_portal_grp * pg,bool pause)378 iscsi_portal_grp_open(struct spdk_iscsi_portal_grp *pg, bool pause)
379 {
380 	struct spdk_iscsi_portal *p;
381 	int rc;
382 
383 	TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
384 		rc = iscsi_portal_open(p);
385 		if (rc < 0) {
386 			return rc;
387 		}
388 
389 		if (pause) {
390 			iscsi_portal_pause(p);
391 		}
392 	}
393 	return 0;
394 }
395 
396 static void
iscsi_portal_grp_close(struct spdk_iscsi_portal_grp * pg)397 iscsi_portal_grp_close(struct spdk_iscsi_portal_grp *pg)
398 {
399 	struct spdk_iscsi_portal *p;
400 
401 	TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
402 		iscsi_portal_close(p);
403 	}
404 }
405 
406 void
iscsi_portal_grp_resume(struct spdk_iscsi_portal_grp * pg)407 iscsi_portal_grp_resume(struct spdk_iscsi_portal_grp *pg)
408 {
409 	struct spdk_iscsi_portal *p;
410 
411 	TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
412 		iscsi_portal_resume(p);
413 	}
414 }
415 
416 void
iscsi_portal_grp_close_all(void)417 iscsi_portal_grp_close_all(void)
418 {
419 	struct spdk_iscsi_portal_grp *pg;
420 
421 	SPDK_DEBUGLOG(iscsi, "iscsi_portal_grp_close_all\n");
422 	pthread_mutex_lock(&g_iscsi.mutex);
423 	TAILQ_FOREACH(pg, &g_iscsi.pg_head, tailq) {
424 		iscsi_portal_grp_close(pg);
425 	}
426 	pthread_mutex_unlock(&g_iscsi.mutex);
427 }
428 
429 struct spdk_iscsi_portal_grp *
iscsi_portal_grp_unregister(int tag)430 iscsi_portal_grp_unregister(int tag)
431 {
432 	struct spdk_iscsi_portal_grp *pg;
433 
434 	pthread_mutex_lock(&g_iscsi.mutex);
435 	TAILQ_FOREACH(pg, &g_iscsi.pg_head, tailq) {
436 		if (pg->tag == tag) {
437 			TAILQ_REMOVE(&g_iscsi.pg_head, pg, tailq);
438 			pthread_mutex_unlock(&g_iscsi.mutex);
439 			return pg;
440 		}
441 	}
442 	pthread_mutex_unlock(&g_iscsi.mutex);
443 	return NULL;
444 }
445 
446 void
iscsi_portal_grp_release(struct spdk_iscsi_portal_grp * pg)447 iscsi_portal_grp_release(struct spdk_iscsi_portal_grp *pg)
448 {
449 	iscsi_portal_grp_close(pg);
450 	iscsi_portal_grp_destroy(pg);
451 }
452 
453 static void
iscsi_portal_grp_info_json(struct spdk_iscsi_portal_grp * pg,struct spdk_json_write_ctx * w)454 iscsi_portal_grp_info_json(struct spdk_iscsi_portal_grp *pg,
455 			   struct spdk_json_write_ctx *w)
456 {
457 	struct spdk_iscsi_portal *portal;
458 
459 	spdk_json_write_object_begin(w);
460 
461 	spdk_json_write_named_int32(w, "tag", pg->tag);
462 
463 	spdk_json_write_named_array_begin(w, "portals");
464 	TAILQ_FOREACH(portal, &pg->head, per_pg_tailq) {
465 		spdk_json_write_object_begin(w);
466 
467 		spdk_json_write_named_string(w, "host", portal->host);
468 		spdk_json_write_named_string(w, "port", portal->port);
469 
470 		spdk_json_write_object_end(w);
471 	}
472 	spdk_json_write_array_end(w);
473 
474 	spdk_json_write_named_bool(w, "private", pg->is_private);
475 
476 	spdk_json_write_object_end(w);
477 }
478 
479 static void
iscsi_portal_grp_config_json(struct spdk_iscsi_portal_grp * pg,struct spdk_json_write_ctx * w)480 iscsi_portal_grp_config_json(struct spdk_iscsi_portal_grp *pg,
481 			     struct spdk_json_write_ctx *w)
482 {
483 	spdk_json_write_object_begin(w);
484 
485 	spdk_json_write_named_string(w, "method", "iscsi_create_portal_group");
486 
487 	spdk_json_write_name(w, "params");
488 	iscsi_portal_grp_info_json(pg, w);
489 
490 	spdk_json_write_object_end(w);
491 }
492 
493 void
iscsi_portal_grps_info_json(struct spdk_json_write_ctx * w)494 iscsi_portal_grps_info_json(struct spdk_json_write_ctx *w)
495 {
496 	struct spdk_iscsi_portal_grp *pg;
497 
498 	TAILQ_FOREACH(pg, &g_iscsi.pg_head, tailq) {
499 		iscsi_portal_grp_info_json(pg, w);
500 	}
501 }
502 
503 void
iscsi_portal_grps_config_json(struct spdk_json_write_ctx * w)504 iscsi_portal_grps_config_json(struct spdk_json_write_ctx *w)
505 {
506 	struct spdk_iscsi_portal_grp *pg;
507 
508 	TAILQ_FOREACH(pg, &g_iscsi.pg_head, tailq) {
509 		iscsi_portal_grp_config_json(pg, w);
510 	}
511 }
512