Branch data Line data Source code
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/scsi.h"
11 : :
12 : : #include "spdk/log.h"
13 : :
14 : : #include "iscsi/iscsi.h"
15 : : #include "iscsi/conn.h"
16 : : #include "iscsi/tgt_node.h"
17 : : #include "iscsi/portal_grp.h"
18 : : #include "iscsi/init_grp.h"
19 : : #include "iscsi/task.h"
20 : : #include "spdk/histogram_data.h"
21 : :
22 : : #define MAX_TMPBUF 4096
23 : : #define MAX_MASKBUF 128
24 : :
25 : :
26 : : #define MAX_TMP_NAME_BUF (11 /* TargetName= */ + MAX_TARGET_NAME + 1 /* null */)
27 : : #define MAX_TMP_ADDR_BUF (14 /* TargetAddress= */ + MAX_PORTAL_ADDR + 1 /* : */ + \
28 : : MAX_PORTAL_PORT + 1 /* , */ + 10 /* max length of int in Decimal */ + 1 /* null */)
29 : :
30 : : static bool
31 : 45 : iscsi_ipv6_netmask_allow_addr(const char *netmask, const char *addr)
32 : : {
33 : 36 : struct in6_addr in6_mask;
34 : 36 : struct in6_addr in6_addr;
35 : 36 : char mask[MAX_MASKBUF];
36 : : const char *p;
37 : : size_t n;
38 : : int bits, bmask;
39 : : int i;
40 : :
41 [ - + ]: 45 : if (netmask[0] != '[') {
42 : 0 : return false;
43 : : }
44 [ - + ]: 45 : p = strchr(netmask, ']');
45 [ - + ]: 45 : if (p == NULL) {
46 : 0 : return false;
47 : : }
48 : 45 : n = p - (netmask + 1);
49 [ - + ]: 45 : if (n + 1 > sizeof mask) {
50 : 0 : return false;
51 : : }
52 : :
53 [ - + ]: 45 : memcpy(mask, netmask + 1, n);
54 : 45 : mask[n] = '\0';
55 : 45 : p++;
56 : :
57 [ + - ]: 45 : if (p[0] == '/') {
58 [ - + ]: 45 : bits = (int) strtol(p + 1, NULL, 10);
59 [ + + + + ]: 45 : if (bits <= 0 || bits > 128) {
60 : 15 : return false;
61 : : }
62 : : } else {
63 : 0 : bits = 128;
64 : : }
65 : :
66 : : #if 0
67 : : SPDK_DEBUGLOG(iscsi, "input %s\n", addr);
68 : : SPDK_DEBUGLOG(iscsi, "mask %s / %d\n", mask, bits);
69 : : #endif
70 : :
71 : : /* presentation to network order binary */
72 [ + - ]: 30 : if (inet_pton(AF_INET6, mask, &in6_mask) <= 0
73 [ - + ]: 30 : || inet_pton(AF_INET6, addr, &in6_addr) <= 0) {
74 : 0 : return false;
75 : : }
76 : :
77 : : /* check 128bits */
78 [ + + ]: 305 : for (i = 0; i < (bits / 8); i++) {
79 [ + + ]: 290 : if (in6_mask.s6_addr[i] != in6_addr.s6_addr[i]) {
80 : 15 : return false;
81 : : }
82 : : }
83 [ - + ]: 15 : if (bits % 8) {
84 [ # # ]: 0 : bmask = (0xffU << (8 - (bits % 8))) & 0xffU;
85 [ # # ]: 0 : if ((in6_mask.s6_addr[i] & bmask) != (in6_addr.s6_addr[i] & bmask)) {
86 : 0 : return false;
87 : : }
88 : : }
89 : :
90 : : /* match */
91 : 15 : return true;
92 : : }
93 : :
94 : : static bool
95 : 619 : iscsi_ipv4_netmask_allow_addr(const char *netmask, const char *addr)
96 : : {
97 : 76 : struct in_addr in4_mask;
98 : 76 : struct in_addr in4_addr;
99 : 76 : char mask[MAX_MASKBUF];
100 : : const char *p;
101 : : uint32_t bmask;
102 : : size_t n;
103 : : int bits;
104 : :
105 [ - + ]: 619 : p = strchr(netmask, '/');
106 [ + + ]: 619 : if (p == NULL) {
107 [ - + ]: 55 : p = netmask + strlen(netmask);
108 : : }
109 : 619 : n = p - netmask;
110 [ - + ]: 619 : if (n + 1 > sizeof mask) {
111 : 0 : return false;
112 : : }
113 : :
114 [ - + ]: 619 : memcpy(mask, netmask, n);
115 : 619 : mask[n] = '\0';
116 : :
117 [ + + ]: 619 : if (p[0] == '/') {
118 [ - + ]: 564 : bits = (int) strtol(p + 1, NULL, 10);
119 [ + + + + ]: 564 : if (bits <= 0 || bits > 32) {
120 : 15 : return false;
121 : : }
122 : : } else {
123 : 55 : bits = 32;
124 : : }
125 : :
126 : : /* presentation to network order binary */
127 [ + - ]: 604 : if (inet_pton(AF_INET, mask, &in4_mask) <= 0
128 [ - + ]: 604 : || inet_pton(AF_INET, addr, &in4_addr) <= 0) {
129 : 0 : return false;
130 : : }
131 : :
132 : : /* check 32bits */
133 [ - + ]: 604 : bmask = (0xffffffffU << (32 - bits)) & 0xffffffffU;
134 [ + + ]: 604 : if ((ntohl(in4_mask.s_addr) & bmask) != (ntohl(in4_addr.s_addr) & bmask)) {
135 : 45 : return false;
136 : : }
137 : :
138 : : /* match */
139 : 559 : return true;
140 : : }
141 : :
142 : : static bool
143 : 604 : iscsi_netmask_allow_addr(const char *netmask, const char *addr)
144 : : {
145 [ + - - + ]: 604 : if (netmask == NULL || addr == NULL) {
146 : 0 : return false;
147 : : }
148 [ + + + + ]: 604 : if (strcasecmp(netmask, "ANY") == 0) {
149 : 10 : return true;
150 : : }
151 [ + + ]: 594 : if (netmask[0] == '[') {
152 : : /* IPv6 */
153 [ + + ]: 10 : if (iscsi_ipv6_netmask_allow_addr(netmask, addr)) {
154 : 5 : return true;
155 : : }
156 : : } else {
157 : : /* IPv4 */
158 [ + + ]: 584 : if (iscsi_ipv4_netmask_allow_addr(netmask, addr)) {
159 : 549 : return true;
160 : : }
161 : : }
162 : 40 : return false;
163 : : }
164 : :
165 : : static bool
166 : 579 : iscsi_init_grp_allow_addr(struct spdk_iscsi_init_grp *igp,
167 : : const char *addr)
168 : : {
169 : : struct spdk_iscsi_initiator_netmask *imask;
170 : :
171 [ + + ]: 609 : TAILQ_FOREACH(imask, &igp->netmask_head, tailq) {
172 [ - + - + ]: 574 : SPDK_DEBUGLOG(iscsi, "netmask=%s, addr=%s\n",
173 : : imask->mask, addr);
174 [ + + ]: 574 : if (iscsi_netmask_allow_addr(imask->mask, addr)) {
175 : 544 : return true;
176 : : }
177 : : }
178 : 35 : return false;
179 : : }
180 : :
181 : : static int
182 : 774 : iscsi_init_grp_allow_iscsi_name(struct spdk_iscsi_init_grp *igp,
183 : : const char *iqn, bool *result)
184 : : {
185 : : struct spdk_iscsi_initiator_name *iname;
186 : :
187 [ + + ]: 814 : TAILQ_FOREACH(iname, &igp->initiator_head, tailq) {
188 : : /* denied if iqn is matched */
189 [ + + ]: 774 : if ((iname->name[0] == '!')
190 [ + + + - ]: 15 : && (strcasecmp(&iname->name[1], "ANY") == 0
191 [ + + - + : 15 : || strcasecmp(&iname->name[1], iqn) == 0)) {
+ - ]
192 : 15 : *result = false;
193 : 15 : return 0;
194 : : }
195 : : /* allowed if iqn is matched */
196 [ + + + + ]: 759 : if (strcasecmp(iname->name, "ANY") == 0
197 [ + + - + : 100 : || strcasecmp(iname->name, iqn) == 0) {
+ + ]
198 : 719 : *result = true;
199 : 719 : return 0;
200 : : }
201 : : }
202 : 40 : return -1;
203 : : }
204 : :
205 : : static struct spdk_iscsi_pg_map *iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
206 : : struct spdk_iscsi_portal_grp *pg);
207 : :
208 : : bool
209 : 584 : iscsi_tgt_node_access(struct spdk_iscsi_conn *conn,
210 : : struct spdk_iscsi_tgt_node *target, const char *iqn, const char *addr)
211 : : {
212 : : struct spdk_iscsi_portal_grp *pg;
213 : : struct spdk_iscsi_pg_map *pg_map;
214 : : struct spdk_iscsi_ig_map *ig_map;
215 : : int rc;
216 : 584 : bool allowed = false;
217 : :
218 [ + - + - : 584 : if (conn == NULL || target == NULL || iqn == NULL || addr == NULL) {
+ - - + ]
219 : 0 : return false;
220 : : }
221 : 584 : pg = conn->portal->group;
222 : :
223 [ - + - + ]: 584 : SPDK_DEBUGLOG(iscsi, "pg=%d, iqn=%s, addr=%s\n",
224 : : pg->tag, iqn, addr);
225 : 584 : pg_map = iscsi_tgt_node_find_pg_map(target, pg);
226 [ - + ]: 584 : if (pg_map == NULL) {
227 : 0 : return false;
228 : : }
229 [ + + ]: 649 : TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
230 : 624 : rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &allowed);
231 [ + + ]: 624 : if (rc == 0) {
232 [ + + + + ]: 594 : if (allowed == false) {
233 : 15 : goto denied;
234 : : } else {
235 [ + + ]: 579 : if (iscsi_init_grp_allow_addr(ig_map->ig, addr)) {
236 : 544 : return true;
237 : : }
238 : : }
239 : : } else {
240 : : /* netmask is denied in this initiator group */
241 : : }
242 : : }
243 : :
244 : 37 : denied:
245 [ - + - + ]: 40 : SPDK_DEBUGLOG(iscsi, "access denied from %s (%s) to %s (%s:%s,%d)\n",
246 : : iqn, addr, target->name, conn->portal_host,
247 : : conn->portal_port, conn->pg_tag);
248 : 40 : return false;
249 : : }
250 : :
251 : : static bool
252 : 145 : iscsi_tgt_node_allow_iscsi_name(struct spdk_iscsi_tgt_node *target, const char *iqn)
253 : : {
254 : : struct spdk_iscsi_pg_map *pg_map;
255 : : struct spdk_iscsi_ig_map *ig_map;
256 : : int rc;
257 : 145 : bool result = false;
258 : :
259 [ + - - + ]: 145 : if (target == NULL || iqn == NULL) {
260 : 0 : return false;
261 : : }
262 : :
263 [ + + ]: 155 : TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
264 [ + + ]: 160 : TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
265 : 150 : rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &result);
266 [ + + ]: 150 : if (rc == 0) {
267 [ - + ]: 140 : return result;
268 : : }
269 : : }
270 : : }
271 : :
272 : 5 : return false;
273 : : }
274 : :
275 : : static bool
276 : 270 : iscsi_copy_str(char *data, int *total, int alloc_len,
277 : : int *previous_completed_len, int expected_size, char *src)
278 : : {
279 : 270 : int len = 0;
280 : :
281 [ - + ]: 270 : assert(*previous_completed_len >= 0);
282 : :
283 [ - + ]: 270 : if (alloc_len - *total < 1) {
284 : 0 : return true;
285 : : }
286 : :
287 [ + - ]: 270 : if (*previous_completed_len < expected_size) {
288 : 270 : len = spdk_min(alloc_len - *total, expected_size - *previous_completed_len);
289 [ - + - + ]: 270 : memcpy((char *)data + *total, src + *previous_completed_len, len);
290 : 270 : *total += len;
291 : 270 : *previous_completed_len = 0;
292 : : } else {
293 : 0 : *previous_completed_len -= expected_size;
294 : : }
295 : :
296 : 270 : return false;
297 : : }
298 : :
299 : : static int
300 : 135 : iscsi_send_tgt_portals(struct spdk_iscsi_conn *conn,
301 : : struct spdk_iscsi_tgt_node *target,
302 : : uint8_t *data, int alloc_len, int total,
303 : : int *previous_completed_len, bool *no_buf_space)
304 : : {
305 : 0 : char buf[MAX_TARGET_ADDR + 2];
306 : : struct spdk_iscsi_portal_grp *pg;
307 : : struct spdk_iscsi_pg_map *pg_map;
308 : : struct spdk_iscsi_portal *p;
309 : : char *host;
310 : 0 : char tmp_buf[MAX_TMP_ADDR_BUF];
311 : : int len;
312 : :
313 [ + + ]: 270 : TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
314 : 135 : pg = pg_map->pg;
315 : :
316 [ - + - + ]: 135 : if (pg->is_private) {
317 : : /* Skip the private portal group. Portals in the private portal group
318 : : * will be returned only by temporary login redirection responses.
319 : : */
320 : 0 : continue;
321 : : }
322 : :
323 [ + + ]: 270 : TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
324 : 135 : host = p->host;
325 : : /* wildcard? */
326 [ - + + - : 135 : if (strcasecmp(host, "[::]") == 0 || strcasecmp(host, "0.0.0.0") == 0) {
- + - + ]
327 [ # # ]: 0 : if (spdk_sock_is_ipv6(conn->sock)) {
328 : 0 : snprintf(buf, sizeof buf, "[%s]", conn->target_addr);
329 : 0 : host = buf;
330 [ # # ]: 0 : } else if (spdk_sock_is_ipv4(conn->sock)) {
331 : 0 : snprintf(buf, sizeof buf, "%s", conn->target_addr);
332 : 0 : host = buf;
333 : : } else {
334 : : /* skip portal for the family */
335 : 0 : continue;
336 : : }
337 : : }
338 [ - + - + ]: 135 : SPDK_DEBUGLOG(iscsi, "TargetAddress=%s:%s,%d\n",
339 : : host, p->port, pg->tag);
340 : :
341 : 135 : memset(tmp_buf, 0, sizeof(tmp_buf));
342 : : /* Calculate the whole string size */
343 : 135 : len = snprintf(NULL, 0, "TargetAddress=%s:%s,%d", host, p->port, pg->tag);
344 [ - + ]: 135 : assert(len < MAX_TMPBUF);
345 : :
346 : : /* string contents are not fully copied */
347 [ + - ]: 135 : if (*previous_completed_len < len) {
348 : : /* Copy the string into the temporary buffer */
349 : 135 : snprintf(tmp_buf, len + 1, "TargetAddress=%s:%s,%d", host, p->port, pg->tag);
350 : : }
351 : :
352 : 135 : *no_buf_space = iscsi_copy_str(data, &total, alloc_len, previous_completed_len,
353 : : len + 1, tmp_buf);
354 [ - + - + ]: 135 : if (*no_buf_space) {
355 : 0 : break;
356 : : }
357 : : }
358 : : }
359 : :
360 : 135 : return total;
361 : : }
362 : :
363 : : int
364 : 29 : iscsi_send_tgts(struct spdk_iscsi_conn *conn, const char *iiqn,
365 : : const char *tiqn, uint8_t *data, int alloc_len, int data_len)
366 : : {
367 : : struct spdk_iscsi_tgt_node *target;
368 : 0 : int total;
369 : : int len;
370 : : int rc;
371 : 29 : int previous_completed_size = 0;
372 : 29 : bool no_buf_space = false;
373 : 0 : char tmp_buf[MAX_TMP_NAME_BUF];
374 : :
375 [ - + ]: 29 : if (conn == NULL) {
376 : 0 : return 0;
377 : : }
378 : 29 : previous_completed_size = conn->send_tgt_completed_size;
379 : :
380 : 29 : total = data_len;
381 [ - + ]: 29 : if (alloc_len < 1) {
382 : 0 : return 0;
383 : : }
384 [ - + ]: 29 : if (total >= alloc_len) {
385 : 0 : total = alloc_len;
386 : 0 : data[total - 1] = '\0';
387 : 0 : return total;
388 : : }
389 : :
390 [ - + ]: 29 : pthread_mutex_lock(&g_iscsi.mutex);
391 [ + + ]: 164 : TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
392 [ - + - + ]: 135 : if (strcasecmp(tiqn, "ALL") != 0
393 [ # # # # : 0 : && strcasecmp(tiqn, target->name) != 0) {
# # ]
394 : 0 : continue;
395 : : }
396 : 135 : rc = iscsi_tgt_node_allow_iscsi_name(target, iiqn);
397 [ - + ]: 135 : if (rc == 0) {
398 : 0 : continue;
399 : : }
400 : :
401 : 135 : memset(tmp_buf, 0, sizeof(tmp_buf));
402 : : /* Calculate the whole string size */
403 : 135 : len = snprintf(NULL, 0, "TargetName=%s", target->name);
404 [ - + ]: 135 : assert(len < MAX_TMPBUF);
405 : :
406 : : /* String contents are not copied */
407 [ + - ]: 135 : if (previous_completed_size < len) {
408 : : /* Copy the string into the temporary buffer */
409 : 135 : snprintf(tmp_buf, len + 1, "TargetName=%s", target->name);
410 : : }
411 : :
412 : 135 : no_buf_space = iscsi_copy_str(data, &total, alloc_len, &previous_completed_size,
413 : : len + 1, tmp_buf);
414 [ - + - + ]: 135 : if (no_buf_space) {
415 : 0 : break;
416 : : }
417 : :
418 : 135 : total = iscsi_send_tgt_portals(conn, target, data, alloc_len, total,
419 : : &previous_completed_size, &no_buf_space);
420 [ - + - + ]: 135 : if (no_buf_space) {
421 : 0 : break;
422 : : }
423 : : }
424 [ - + ]: 29 : pthread_mutex_unlock(&g_iscsi.mutex);
425 : :
426 : : /* Only set it when it is not successfully completed */
427 [ - + - + ]: 29 : if (no_buf_space) {
428 : 0 : conn->send_tgt_completed_size += total;
429 : : } else {
430 : 29 : conn->send_tgt_completed_size = 0;
431 : : }
432 : :
433 : 29 : return total;
434 : : }
435 : :
436 : : struct spdk_iscsi_tgt_node *
437 : 756 : iscsi_find_tgt_node(const char *target_name)
438 : : {
439 : : struct spdk_iscsi_tgt_node *target;
440 : :
441 [ - + ]: 756 : if (target_name == NULL) {
442 : 0 : return NULL;
443 : : }
444 [ + + ]: 3162 : TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
445 [ - + - + : 3004 : if (strcasecmp(target_name, target->name) == 0) {
+ + ]
446 : 598 : return target;
447 : : }
448 : : }
449 : 158 : return NULL;
450 : : }
451 : :
452 : : static int
453 : 158 : iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target)
454 : : {
455 [ - + ]: 158 : pthread_mutex_lock(&g_iscsi.mutex);
456 : :
457 [ - + ]: 158 : if (iscsi_find_tgt_node(target->name) != NULL) {
458 [ # # ]: 0 : pthread_mutex_unlock(&g_iscsi.mutex);
459 : 0 : return -EEXIST;
460 : : }
461 : :
462 : 158 : TAILQ_INSERT_TAIL(&g_iscsi.target_head, target, tailq);
463 : :
464 [ - + ]: 158 : pthread_mutex_unlock(&g_iscsi.mutex);
465 : 158 : return 0;
466 : : }
467 : :
468 : : static int
469 : 54 : iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target)
470 : : {
471 : : struct spdk_iscsi_tgt_node *t;
472 : :
473 [ + - ]: 56 : TAILQ_FOREACH(t, &g_iscsi.target_head, tailq) {
474 [ + + ]: 56 : if (t == target) {
475 [ + + ]: 54 : TAILQ_REMOVE(&g_iscsi.target_head, t, tailq);
476 : 54 : return 0;
477 : : }
478 : : }
479 : :
480 : 0 : return -1;
481 : : }
482 : :
483 : : static struct spdk_iscsi_ig_map *
484 : 230 : iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map,
485 : : struct spdk_iscsi_init_grp *ig)
486 : : {
487 : : struct spdk_iscsi_ig_map *ig_map;
488 : :
489 [ + + ]: 239 : TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
490 [ + + ]: 47 : if (ig_map->ig == ig) {
491 : 38 : return ig_map;
492 : : }
493 : : }
494 : :
495 : 192 : return NULL;
496 : : }
497 : :
498 : : static struct spdk_iscsi_ig_map *
499 : 192 : iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map,
500 : : struct spdk_iscsi_init_grp *ig)
501 : : {
502 : : struct spdk_iscsi_ig_map *ig_map;
503 : :
504 [ - + ]: 192 : if (iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) {
505 : 0 : return NULL;
506 : : }
507 : :
508 : 192 : ig_map = malloc(sizeof(*ig_map));
509 [ - + ]: 192 : if (ig_map == NULL) {
510 : 0 : return NULL;
511 : : }
512 : :
513 : 192 : ig_map->ig = ig;
514 : 192 : ig->ref++;
515 : 192 : pg_map->num_ig_maps++;
516 : 192 : TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq);
517 : :
518 : 192 : return ig_map;
519 : : }
520 : :
521 : : static void
522 : 192 : _iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
523 : : struct spdk_iscsi_ig_map *ig_map)
524 : : {
525 [ + + ]: 192 : TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq);
526 : 192 : pg_map->num_ig_maps--;
527 : 192 : ig_map->ig->ref--;
528 : 192 : free(ig_map);
529 : 192 : }
530 : :
531 : : static int
532 : 34 : iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
533 : : struct spdk_iscsi_init_grp *ig)
534 : : {
535 : : struct spdk_iscsi_ig_map *ig_map;
536 : :
537 : 34 : ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
538 [ - + ]: 34 : if (ig_map == NULL) {
539 : 0 : return -ENOENT;
540 : : }
541 : :
542 : 34 : _iscsi_pg_map_delete_ig_map(pg_map, ig_map);
543 : 34 : return 0;
544 : : }
545 : :
546 : : static void
547 : 179 : iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map)
548 : : {
549 : : struct spdk_iscsi_ig_map *ig_map, *tmp;
550 : :
551 [ + + ]: 333 : TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) {
552 : 154 : _iscsi_pg_map_delete_ig_map(pg_map, ig_map);
553 : : }
554 : 179 : }
555 : :
556 : : static struct spdk_iscsi_pg_map *
557 : 1498 : iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
558 : : struct spdk_iscsi_portal_grp *pg)
559 : : {
560 : : struct spdk_iscsi_pg_map *pg_map;
561 : :
562 [ + + ]: 1509 : TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
563 [ + + ]: 1160 : if (pg_map->pg == pg) {
564 : 1149 : return pg_map;
565 : : }
566 : : }
567 : :
568 : 349 : return NULL;
569 : : }
570 : :
571 : : static struct spdk_iscsi_pg_map *
572 : 185 : iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target,
573 : : struct spdk_iscsi_portal_grp *pg)
574 : : {
575 : : struct spdk_iscsi_pg_map *pg_map;
576 : 20 : char port_name[MAX_TMPBUF];
577 : : int rc;
578 : :
579 [ - + ]: 185 : if (iscsi_tgt_node_find_pg_map(target, pg) != NULL) {
580 : 0 : return NULL;
581 : : }
582 : :
583 [ - + ]: 185 : if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) {
584 : 0 : SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n",
585 : : SPDK_SCSI_DEV_MAX_PORTS);
586 : 0 : return NULL;
587 : : }
588 : :
589 : 185 : pg_map = calloc(1, sizeof(*pg_map));
590 [ - + ]: 185 : if (pg_map == NULL) {
591 : 0 : return NULL;
592 : : }
593 : :
594 [ - + ]: 370 : snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x",
595 : 185 : spdk_scsi_dev_get_name(target->dev), pg->tag);
596 : 185 : rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name);
597 [ - + ]: 185 : if (rc != 0) {
598 : 0 : free(pg_map);
599 : 0 : return NULL;
600 : : }
601 : :
602 : 185 : TAILQ_INIT(&pg_map->ig_map_head);
603 : 185 : pg_map->num_ig_maps = 0;
604 : 185 : pg->ref++;
605 : 185 : pg_map->pg = pg;
606 : 185 : target->num_pg_maps++;
607 : 185 : TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq);
608 : :
609 : 185 : return pg_map;
610 : : }
611 : :
612 : : static void
613 : 185 : _iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
614 : : struct spdk_iscsi_pg_map *pg_map)
615 : : {
616 [ + + ]: 185 : TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq);
617 : 185 : target->num_pg_maps--;
618 : 185 : pg_map->pg->ref--;
619 : :
620 : 185 : spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag);
621 : :
622 : 185 : free(pg_map);
623 : 185 : }
624 : :
625 : : static int
626 : 33 : iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
627 : : struct spdk_iscsi_portal_grp *pg)
628 : : {
629 : : struct spdk_iscsi_pg_map *pg_map;
630 : :
631 : 33 : pg_map = iscsi_tgt_node_find_pg_map(target, pg);
632 [ + + ]: 33 : if (pg_map == NULL) {
633 : 4 : return -ENOENT;
634 : : }
635 : :
636 [ + + ]: 29 : if (pg_map->num_ig_maps > 0) {
637 [ - + - + ]: 4 : SPDK_DEBUGLOG(iscsi, "delete %d ig_maps forcefully\n",
638 : : pg_map->num_ig_maps);
639 : : }
640 : :
641 : 29 : iscsi_pg_map_delete_all_ig_maps(pg_map);
642 : 29 : _iscsi_tgt_node_delete_pg_map(target, pg_map);
643 : 29 : return 0;
644 : : }
645 : :
646 : : static void
647 : 8 : iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target,
648 : : struct spdk_iscsi_init_grp *ig)
649 : : {
650 : : struct spdk_iscsi_pg_map *pg_map, *tmp;
651 : :
652 [ + + ]: 12 : TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
653 : 4 : iscsi_pg_map_delete_ig_map(pg_map, ig);
654 [ + - ]: 4 : if (pg_map->num_ig_maps == 0) {
655 : 4 : _iscsi_tgt_node_delete_pg_map(target, pg_map);
656 : : }
657 : : }
658 : 8 : }
659 : :
660 : : static void
661 : 158 : iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target)
662 : : {
663 : : struct spdk_iscsi_pg_map *pg_map, *tmp;
664 : :
665 [ + + ]: 308 : TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
666 : 150 : iscsi_pg_map_delete_all_ig_maps(pg_map);
667 : 150 : _iscsi_tgt_node_delete_pg_map(target, pg_map);
668 : : }
669 : 158 : }
670 : :
671 : : static void
672 : 158 : _iscsi_tgt_node_destruct(void *cb_arg, int rc)
673 : : {
674 : 158 : struct spdk_iscsi_tgt_node *target = cb_arg;
675 : 158 : iscsi_tgt_node_destruct_cb destruct_cb_fn = target->destruct_cb_fn;
676 : 158 : void *destruct_cb_arg = target->destruct_cb_arg;
677 : :
678 [ - + ]: 158 : if (rc != 0) {
679 [ # # ]: 0 : if (destruct_cb_fn) {
680 : 0 : destruct_cb_fn(destruct_cb_arg, rc);
681 : : }
682 : 0 : return;
683 : : }
684 : :
685 [ - + ]: 158 : pthread_mutex_lock(&g_iscsi.mutex);
686 : 158 : iscsi_tgt_node_delete_all_pg_maps(target);
687 [ - + ]: 158 : pthread_mutex_unlock(&g_iscsi.mutex);
688 : :
689 [ - + ]: 158 : pthread_mutex_destroy(&target->mutex);
690 : :
691 : 158 : spdk_histogram_data_free(target->histogram);
692 : :
693 : 158 : free(target);
694 : :
695 [ + + ]: 158 : if (destruct_cb_fn) {
696 : 54 : destruct_cb_fn(destruct_cb_arg, 0);
697 : : }
698 : : }
699 : :
700 : : static int
701 : 0 : iscsi_tgt_node_check_active_conns(void *arg)
702 : : {
703 : 0 : struct spdk_iscsi_tgt_node *target = arg;
704 : :
705 [ # # ]: 0 : if (iscsi_get_active_conns(target) != 0) {
706 : 0 : return SPDK_POLLER_BUSY;
707 : : }
708 : :
709 : 0 : spdk_poller_unregister(&target->destruct_poller);
710 : :
711 : 0 : spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
712 : :
713 : 0 : return SPDK_POLLER_BUSY;
714 : : }
715 : :
716 : : static void
717 : 158 : iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target,
718 : : iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
719 : : {
720 [ - + ]: 158 : if (target == NULL) {
721 [ # # ]: 0 : if (cb_fn) {
722 : 0 : cb_fn(cb_arg, -ENOENT);
723 : : }
724 : 0 : return;
725 : : }
726 : :
727 [ - + - + ]: 158 : if (target->destructed) {
728 : 0 : SPDK_ERRLOG("Destructing %s is already started\n", target->name);
729 [ # # ]: 0 : if (cb_fn) {
730 : 0 : cb_fn(cb_arg, -EBUSY);
731 : : }
732 : 0 : return;
733 : : }
734 : :
735 : 158 : target->destructed = true;
736 : 158 : target->destruct_cb_fn = cb_fn;
737 : 158 : target->destruct_cb_arg = cb_arg;
738 : :
739 : 158 : iscsi_conns_request_logout(target, -1);
740 : :
741 [ - + ]: 158 : if (iscsi_get_active_conns(target) != 0) {
742 : 0 : target->destruct_poller = SPDK_POLLER_REGISTER(iscsi_tgt_node_check_active_conns,
743 : : target, 10);
744 : : } else {
745 : 158 : spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
746 : : }
747 : :
748 : : }
749 : :
750 : : static int
751 : 4 : iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target,
752 : : int pg_tag, int ig_tag)
753 : : {
754 : : struct spdk_iscsi_portal_grp *pg;
755 : : struct spdk_iscsi_init_grp *ig;
756 : : struct spdk_iscsi_pg_map *pg_map;
757 : : struct spdk_iscsi_ig_map *ig_map;
758 : :
759 : 4 : pg = iscsi_portal_grp_find_by_tag(pg_tag);
760 [ - + ]: 4 : if (pg == NULL) {
761 : 0 : SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
762 : 0 : return -ENOENT;
763 : : }
764 : 4 : ig = iscsi_init_grp_find_by_tag(ig_tag);
765 [ - + ]: 4 : if (ig == NULL) {
766 : 0 : SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
767 : 0 : return -ENOENT;
768 : : }
769 : :
770 : 4 : pg_map = iscsi_tgt_node_find_pg_map(target, pg);
771 [ - + ]: 4 : if (pg_map == NULL) {
772 : 0 : SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag);
773 : 0 : return -ENOENT;
774 : : }
775 : 4 : ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
776 [ - + ]: 4 : if (ig_map == NULL) {
777 : 0 : SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag);
778 : 0 : return -ENOENT;
779 : : }
780 : :
781 : 4 : _iscsi_pg_map_delete_ig_map(pg_map, ig_map);
782 [ + + ]: 4 : if (pg_map->num_ig_maps == 0) {
783 : 2 : _iscsi_tgt_node_delete_pg_map(target, pg_map);
784 : : }
785 : :
786 : 4 : return 0;
787 : : }
788 : :
789 : : static int
790 : 162 : iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target,
791 : : int pg_tag, int ig_tag)
792 : : {
793 : : struct spdk_iscsi_portal_grp *pg;
794 : : struct spdk_iscsi_pg_map *pg_map;
795 : : struct spdk_iscsi_init_grp *ig;
796 : : struct spdk_iscsi_ig_map *ig_map;
797 : 162 : bool new_pg_map = false;
798 : :
799 : 162 : pg = iscsi_portal_grp_find_by_tag(pg_tag);
800 [ - + ]: 162 : if (pg == NULL) {
801 : 0 : SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
802 : 0 : return -ENOENT;
803 : : }
804 : 162 : ig = iscsi_init_grp_find_by_tag(ig_tag);
805 [ - + ]: 162 : if (ig == NULL) {
806 : 0 : SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
807 : 0 : return -ENOENT;
808 : : }
809 : :
810 : : /* get existing pg_map or create new pg_map and add it to target */
811 : 162 : pg_map = iscsi_tgt_node_find_pg_map(target, pg);
812 [ + + ]: 162 : if (pg_map == NULL) {
813 : 160 : pg_map = iscsi_tgt_node_add_pg_map(target, pg);
814 [ - + ]: 160 : if (pg_map == NULL) {
815 : 0 : goto failed;
816 : : }
817 : 160 : new_pg_map = true;
818 : : }
819 : :
820 : : /* create new ig_map and add it to pg_map */
821 : 162 : ig_map = iscsi_pg_map_add_ig_map(pg_map, ig);
822 [ - + ]: 162 : if (ig_map == NULL) {
823 : 0 : goto failed;
824 : : }
825 : :
826 : 162 : return 0;
827 : :
828 : 0 : failed:
829 [ # # ]: 0 : if (new_pg_map) {
830 : 0 : _iscsi_tgt_node_delete_pg_map(target, pg_map);
831 : : }
832 : :
833 : 0 : return -1;
834 : : }
835 : :
836 : : int
837 : 160 : iscsi_target_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
838 : : int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
839 : : {
840 : : uint16_t i;
841 : : int rc;
842 : :
843 [ - + ]: 160 : pthread_mutex_lock(&g_iscsi.mutex);
844 [ + + ]: 322 : for (i = 0; i < num_maps; i++) {
845 : 162 : rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i],
846 : 162 : ig_tag_list[i]);
847 [ - + ]: 162 : if (rc != 0) {
848 : 0 : SPDK_ERRLOG("could not add map to target\n");
849 : 0 : goto invalid;
850 : : }
851 : : }
852 [ - + ]: 160 : pthread_mutex_unlock(&g_iscsi.mutex);
853 : 160 : return 0;
854 : :
855 : 0 : invalid:
856 [ # # ]: 0 : for (; i > 0; --i) {
857 : 0 : iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1],
858 : 0 : ig_tag_list[i - 1]);
859 : : }
860 [ # # ]: 0 : pthread_mutex_unlock(&g_iscsi.mutex);
861 : 0 : return -1;
862 : : }
863 : :
864 : : int
865 : 2 : iscsi_target_node_remove_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
866 : : int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
867 : : {
868 : : uint16_t i;
869 : : int rc;
870 : :
871 [ - + ]: 2 : pthread_mutex_lock(&g_iscsi.mutex);
872 [ + + ]: 6 : for (i = 0; i < num_maps; i++) {
873 : 4 : rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i],
874 : 4 : ig_tag_list[i]);
875 [ - + ]: 4 : if (rc != 0) {
876 : 0 : SPDK_ERRLOG("could not delete map from target\n");
877 : 0 : goto invalid;
878 : : }
879 : : }
880 [ - + ]: 2 : pthread_mutex_unlock(&g_iscsi.mutex);
881 : 2 : return 0;
882 : :
883 : 0 : invalid:
884 [ # # ]: 0 : for (; i > 0; --i) {
885 : 0 : rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1],
886 : 0 : ig_tag_list[i - 1]);
887 [ # # ]: 0 : if (rc != 0) {
888 : 0 : iscsi_tgt_node_delete_all_pg_maps(target);
889 : 0 : break;
890 : : }
891 : : }
892 [ # # ]: 0 : pthread_mutex_unlock(&g_iscsi.mutex);
893 : 0 : return -1;
894 : : }
895 : :
896 : : int
897 : 4 : iscsi_tgt_node_redirect(struct spdk_iscsi_tgt_node *target, int pg_tag,
898 : : const char *host, const char *port)
899 : : {
900 : : struct spdk_iscsi_portal_grp *pg;
901 : : struct spdk_iscsi_pg_map *pg_map;
902 : 0 : struct sockaddr_storage sa;
903 : :
904 [ - + ]: 4 : if (target == NULL) {
905 : 0 : return -EINVAL;
906 : : }
907 : :
908 : 4 : pg = iscsi_portal_grp_find_by_tag(pg_tag);
909 [ - + ]: 4 : if (pg == NULL) {
910 : 0 : SPDK_ERRLOG("Portal group %d is not found.\n", pg_tag);
911 : 0 : return -EINVAL;
912 : : }
913 : :
914 [ - + - + ]: 4 : if (pg->is_private) {
915 : 0 : SPDK_ERRLOG("Portal group %d is not public portal group.\n", pg_tag);
916 : 0 : return -EINVAL;
917 : : }
918 : :
919 : 4 : pg_map = iscsi_tgt_node_find_pg_map(target, pg);
920 [ - + ]: 4 : if (pg_map == NULL) {
921 : 0 : SPDK_ERRLOG("Portal group %d is not mapped.\n", pg_tag);
922 : 0 : return -EINVAL;
923 : : }
924 : :
925 [ + + + - ]: 4 : if (host == NULL && port == NULL) {
926 : : /* Clear redirect setting. */
927 [ - + ]: 2 : memset(pg_map->redirect_host, 0, MAX_PORTAL_ADDR + 1);
928 [ - + ]: 2 : memset(pg_map->redirect_port, 0, MAX_PORTAL_PORT + 1);
929 : : } else {
930 [ - + ]: 2 : if (iscsi_parse_redirect_addr(&sa, host, port) != 0) {
931 : 0 : SPDK_ERRLOG("IP address-port pair is not valid.\n");
932 : 0 : return -EINVAL;
933 : : }
934 : :
935 [ - + ]: 2 : if (iscsi_portal_grp_find_portal_by_addr(pg, port, host) != NULL) {
936 : 0 : SPDK_ERRLOG("IP address-port pair must be chosen from a "
937 : : "different private portal group\n");
938 : 0 : return -EINVAL;
939 : : }
940 : :
941 [ - + ]: 2 : snprintf(pg_map->redirect_host, MAX_PORTAL_ADDR + 1, "%s", host);
942 [ - + ]: 2 : snprintf(pg_map->redirect_port, MAX_PORTAL_PORT + 1, "%s", port);
943 : : }
944 : :
945 : 4 : return 0;
946 : : }
947 : :
948 : : bool
949 : 526 : iscsi_tgt_node_is_redirected(struct spdk_iscsi_conn *conn,
950 : : struct spdk_iscsi_tgt_node *target,
951 : : char *buf, int buf_len)
952 : : {
953 : : struct spdk_iscsi_pg_map *pg_map;
954 : :
955 [ + - + - : 526 : if (conn == NULL || target == NULL || buf == NULL || buf_len == 0) {
+ - - + ]
956 : 0 : return false;
957 : : }
958 : :
959 : 526 : pg_map = iscsi_tgt_node_find_pg_map(target, conn->portal->group);
960 [ - + ]: 526 : if (pg_map == NULL) {
961 : 0 : return false;
962 : : }
963 : :
964 [ + + - + ]: 526 : if (pg_map->redirect_host[0] == '\0' || pg_map->redirect_port[0] == '\0') {
965 : 524 : return false;
966 : : }
967 : :
968 [ - + ]: 2 : snprintf(buf, buf_len, "%s:%s", pg_map->redirect_host, pg_map->redirect_port);
969 : :
970 : 2 : return true;
971 : : }
972 : :
973 : : static int
974 : 158 : check_iscsi_name(const char *name)
975 : : {
976 : 158 : const unsigned char *up = (const unsigned char *) name;
977 : : size_t n;
978 : :
979 : : /* valid iSCSI name no larger than 223 bytes */
980 [ - + - + ]: 158 : if (strlen(name) > MAX_TARGET_NAME) {
981 : 0 : return -1;
982 : : }
983 : :
984 : : /* valid iSCSI name? */
985 [ + + ]: 4510 : for (n = 0; up[n] != 0; n++) {
986 [ + - - + ]: 4352 : if (up[n] > 0x00U && up[n] <= 0x2cU) {
987 : 0 : return -1;
988 : : }
989 [ - + ]: 4352 : if (up[n] == 0x2fU) {
990 : 0 : return -1;
991 : : }
992 [ + + - + ]: 4352 : if (up[n] >= 0x3bU && up[n] <= 0x40U) {
993 : 0 : return -1;
994 : : }
995 [ + + - + ]: 4352 : if (up[n] >= 0x5bU && up[n] <= 0x60U) {
996 : 0 : return -1;
997 : : }
998 [ - + - - ]: 4352 : if (up[n] >= 0x7bU && up[n] <= 0x7fU) {
999 : 0 : return -1;
1000 : : }
1001 [ - + ]: 4352 : if (isspace(up[n])) {
1002 : 0 : return -1;
1003 : : }
1004 : : }
1005 : : /* valid format? */
1006 [ - + + - ]: 158 : if (strncasecmp(name, "iqn.", 4) == 0) {
1007 : : /* iqn.YYYY-MM.reversed.domain.name */
1008 [ + - + - : 158 : if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
+ - ]
1009 [ + - + - : 158 : || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
+ - ]
1010 [ + - - + ]: 158 : || !isdigit(up[10]) || up[11] != '.') {
1011 : 0 : SPDK_ERRLOG("invalid iqn format. "
1012 : : "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
1013 : 0 : return -1;
1014 : : }
1015 [ # # # # ]: 0 : } else if (strncasecmp(name, "eui.", 4) == 0) {
1016 : : /* EUI-64 -> 16bytes */
1017 : : /* XXX */
1018 [ # # ]: 0 : } else if (strncasecmp(name, "naa.", 4) == 0) {
1019 : : /* 64bit -> 16bytes, 128bit -> 32bytes */
1020 : : /* XXX */
1021 : : }
1022 : : /* OK */
1023 : 158 : return 0;
1024 : : }
1025 : :
1026 : : bool
1027 : 809 : iscsi_check_chap_params(bool disable, bool require, bool mutual, int group)
1028 : : {
1029 [ + + ]: 809 : if (group < 0) {
1030 : 5 : SPDK_ERRLOG("Invalid auth group ID (%d)\n", group);
1031 : 5 : return false;
1032 : : }
1033 [ + + + + : 804 : if ((!disable && !require && !mutual) || /* Auto */
+ + + + ]
1034 [ + + + + ]: 176 : (disable && !require && !mutual) || /* None */
1035 [ + + + + : 30 : (!disable && require && !mutual) || /* CHAP */
+ + ]
1036 [ + + + + : 25 : (!disable && require && mutual)) { /* CHAP Mutual */
+ - ]
1037 : 784 : return true;
1038 : : }
1039 : 20 : SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n",
1040 : : disable, require, mutual);
1041 : 20 : return false;
1042 : : }
1043 : :
1044 : 158 : struct spdk_iscsi_tgt_node *iscsi_tgt_node_construct(int target_index,
1045 : : const char *name, const char *alias,
1046 : : int *pg_tag_list, int *ig_tag_list, uint16_t num_maps,
1047 : : const char *bdev_name_list[], int *lun_id_list, int num_luns,
1048 : : int queue_depth,
1049 : : bool disable_chap, bool require_chap, bool mutual_chap, int chap_group,
1050 : : bool header_digest, bool data_digest)
1051 : : {
1052 : 0 : char fullname[MAX_TMPBUF];
1053 : : struct spdk_iscsi_tgt_node *target;
1054 : : int rc;
1055 : :
1056 [ - + ]: 158 : if (!iscsi_check_chap_params(disable_chap, require_chap,
1057 : : mutual_chap, chap_group)) {
1058 : 0 : return NULL;
1059 : : }
1060 : :
1061 [ - + ]: 158 : if (num_maps == 0) {
1062 : 0 : SPDK_ERRLOG("num_maps = 0\n");
1063 : 0 : return NULL;
1064 : : }
1065 : :
1066 [ - + ]: 158 : if (name == NULL) {
1067 : 0 : SPDK_ERRLOG("TargetName not found\n");
1068 : 0 : return NULL;
1069 : : }
1070 : :
1071 [ - + + + ]: 158 : if (strncasecmp(name, "iqn.", 4) != 0
1072 [ - + + - ]: 156 : && strncasecmp(name, "eui.", 4) != 0
1073 [ - + + - ]: 156 : && strncasecmp(name, "naa.", 4) != 0) {
1074 : 156 : snprintf(fullname, sizeof(fullname), "%s:%s", g_iscsi.nodebase, name);
1075 : : } else {
1076 : 2 : snprintf(fullname, sizeof(fullname), "%s", name);
1077 : : }
1078 : :
1079 [ - + ]: 158 : if (check_iscsi_name(fullname) != 0) {
1080 : 0 : SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n",
1081 : : name);
1082 : 0 : return NULL;
1083 : : }
1084 : :
1085 : 158 : target = calloc(1, sizeof(*target));
1086 [ - + ]: 158 : if (!target) {
1087 : 0 : SPDK_ERRLOG("could not allocate target\n");
1088 : 0 : return NULL;
1089 : : }
1090 : :
1091 [ - + ]: 158 : rc = pthread_mutex_init(&target->mutex, NULL);
1092 [ - + ]: 158 : if (rc != 0) {
1093 : 0 : SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num);
1094 : 0 : iscsi_tgt_node_destruct(target, NULL, NULL);
1095 : 0 : return NULL;
1096 : : }
1097 : :
1098 : 158 : target->num = target_index;
1099 : :
1100 [ - + ]: 158 : memcpy(target->name, fullname, strlen(fullname));
1101 : :
1102 [ + - ]: 158 : if (alias != NULL) {
1103 [ - + - + ]: 158 : if (strlen(alias) > MAX_TARGET_NAME) {
1104 : 0 : iscsi_tgt_node_destruct(target, NULL, NULL);
1105 : 0 : return NULL;
1106 : : }
1107 [ - + - + : 158 : memcpy(target->alias, alias, strlen(alias));
- + ]
1108 : : }
1109 : :
1110 : 158 : target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns,
1111 : : SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL);
1112 [ - + ]: 158 : if (!target->dev) {
1113 : 0 : SPDK_ERRLOG("Could not construct SCSI device\n");
1114 : 0 : iscsi_tgt_node_destruct(target, NULL, NULL);
1115 : 0 : return NULL;
1116 : : }
1117 : :
1118 : 158 : TAILQ_INIT(&target->pg_map_head);
1119 : 158 : rc = iscsi_target_node_add_pg_ig_maps(target, pg_tag_list,
1120 : : ig_tag_list, num_maps);
1121 [ - + ]: 158 : if (rc != 0) {
1122 : 0 : SPDK_ERRLOG("could not add map to target\n");
1123 : 0 : iscsi_tgt_node_destruct(target, NULL, NULL);
1124 : 0 : return NULL;
1125 : : }
1126 : :
1127 : 158 : target->disable_chap = disable_chap;
1128 : 158 : target->require_chap = require_chap;
1129 : 158 : target->mutual_chap = mutual_chap;
1130 : 158 : target->chap_group = chap_group;
1131 : 158 : target->header_digest = header_digest;
1132 : 158 : target->data_digest = data_digest;
1133 : :
1134 [ + - + + ]: 158 : if (queue_depth > 0 && ((uint32_t)queue_depth <= g_iscsi.MaxQueueDepth)) {
1135 : 39 : target->queue_depth = queue_depth;
1136 : : } else {
1137 [ - + - + ]: 119 : SPDK_DEBUGLOG(iscsi, "QueueDepth %d is invalid and %d is used instead.\n",
1138 : : queue_depth, g_iscsi.MaxQueueDepth);
1139 : 119 : target->queue_depth = g_iscsi.MaxQueueDepth;
1140 : : }
1141 : :
1142 : 158 : rc = iscsi_tgt_node_register(target);
1143 [ - + ]: 158 : if (rc != 0) {
1144 : 0 : SPDK_ERRLOG("register target is failed\n");
1145 : 0 : iscsi_tgt_node_destruct(target, NULL, NULL);
1146 : 0 : return NULL;
1147 : : }
1148 : :
1149 : 158 : return target;
1150 : : }
1151 : :
1152 : : void
1153 : 595 : iscsi_shutdown_tgt_nodes(void)
1154 : : {
1155 : : struct spdk_iscsi_tgt_node *target;
1156 : :
1157 [ - + ]: 595 : pthread_mutex_lock(&g_iscsi.mutex);
1158 [ + + ]: 699 : while (!TAILQ_EMPTY(&g_iscsi.target_head)) {
1159 : 104 : target = TAILQ_FIRST(&g_iscsi.target_head);
1160 [ + + ]: 104 : TAILQ_REMOVE(&g_iscsi.target_head, target, tailq);
1161 : :
1162 [ - + ]: 104 : pthread_mutex_unlock(&g_iscsi.mutex);
1163 : :
1164 : 104 : iscsi_tgt_node_destruct(target, NULL, NULL);
1165 : :
1166 [ - + ]: 104 : pthread_mutex_lock(&g_iscsi.mutex);
1167 : : }
1168 [ - + ]: 595 : pthread_mutex_unlock(&g_iscsi.mutex);
1169 : 595 : }
1170 : :
1171 : : void
1172 : 54 : iscsi_shutdown_tgt_node_by_name(const char *target_name,
1173 : : iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
1174 : : {
1175 : : struct spdk_iscsi_tgt_node *target;
1176 : :
1177 [ - + ]: 54 : pthread_mutex_lock(&g_iscsi.mutex);
1178 : 54 : target = iscsi_find_tgt_node(target_name);
1179 [ + - ]: 54 : if (target != NULL) {
1180 : 54 : iscsi_tgt_node_unregister(target);
1181 [ - + ]: 54 : pthread_mutex_unlock(&g_iscsi.mutex);
1182 : :
1183 : 54 : iscsi_tgt_node_destruct(target, cb_fn, cb_arg);
1184 : :
1185 : 54 : return;
1186 : : }
1187 [ # # ]: 0 : pthread_mutex_unlock(&g_iscsi.mutex);
1188 : :
1189 [ # # ]: 0 : if (cb_fn) {
1190 : 0 : cb_fn(cb_arg, -ENOENT);
1191 : : }
1192 : : }
1193 : :
1194 : : bool
1195 : 526 : iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target)
1196 : : {
1197 [ - + ]: 526 : return target->destructed;
1198 : : }
1199 : :
1200 : : int
1201 : 0 : iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1202 : : struct spdk_iscsi_tgt_node *target)
1203 : : {
1204 : : struct spdk_scsi_lun *lun;
1205 : : struct spdk_iscsi_task *task;
1206 : :
1207 [ # # ]: 0 : for (lun = spdk_scsi_dev_get_first_lun(target->dev); lun != NULL;
1208 : 0 : lun = spdk_scsi_dev_get_next_lun(lun)) {
1209 : : /* we create a fake management task per LUN to cleanup */
1210 : 0 : task = iscsi_task_get(conn, NULL, iscsi_task_mgmt_cpl);
1211 [ # # ]: 0 : if (!task) {
1212 : 0 : SPDK_ERRLOG("Unable to acquire task\n");
1213 : 0 : return -1;
1214 : : }
1215 : :
1216 : 0 : task->scsi.target_port = conn->target_port;
1217 : 0 : task->scsi.initiator_port = conn->initiator_port;
1218 : 0 : task->scsi.lun = lun;
1219 : :
1220 : 0 : iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_TARGET_RESET);
1221 : : }
1222 : :
1223 : 0 : return 0;
1224 : : }
1225 : :
1226 : : void
1227 : 50 : iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1228 : : struct spdk_iscsi_init_grp *initiator_group)
1229 : : {
1230 : : struct spdk_iscsi_tgt_node *target;
1231 : :
1232 [ - + ]: 50 : pthread_mutex_lock(&g_iscsi.mutex);
1233 [ + + ]: 66 : TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1234 [ + + ]: 16 : if (portal_group) {
1235 : 8 : iscsi_tgt_node_delete_pg_map(target, portal_group);
1236 : : }
1237 [ + + ]: 16 : if (initiator_group) {
1238 : 8 : iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1239 : : }
1240 : : }
1241 [ - + ]: 50 : pthread_mutex_unlock(&g_iscsi.mutex);
1242 : 50 : }
1243 : :
1244 : : int
1245 : 34 : iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target,
1246 : : const char *bdev_name, int lun_id)
1247 : : {
1248 : : struct spdk_scsi_dev *dev;
1249 : : int rc;
1250 : :
1251 [ + + ]: 34 : if (target->num_active_conns > 0) {
1252 : 5 : SPDK_ERRLOG("Target has active connections (count=%d)\n",
1253 : : target->num_active_conns);
1254 : 5 : return -1;
1255 : : }
1256 : :
1257 [ + + ]: 29 : if (lun_id < -1) {
1258 : 5 : SPDK_ERRLOG("Specified LUN ID (%d) is negative\n", lun_id);
1259 : 5 : return -1;
1260 : : }
1261 : :
1262 : 24 : dev = target->dev;
1263 [ + + ]: 24 : if (dev == NULL) {
1264 : 10 : SPDK_ERRLOG("SCSI device is not found\n");
1265 : 10 : return -1;
1266 : : }
1267 : :
1268 : 14 : rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL);
1269 [ + + ]: 14 : if (rc != 0) {
1270 : 5 : SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n");
1271 : 5 : return -1;
1272 : : }
1273 : :
1274 : 9 : return 0;
1275 : : }
1276 : :
1277 : : int
1278 : 2 : iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target,
1279 : : bool disable_chap, bool require_chap,
1280 : : bool mutual_chap, int32_t chap_group)
1281 : : {
1282 [ - + ]: 2 : if (!iscsi_check_chap_params(disable_chap, require_chap,
1283 : : mutual_chap, chap_group)) {
1284 : 0 : return -EINVAL;
1285 : : }
1286 : :
1287 [ - + ]: 2 : pthread_mutex_lock(&target->mutex);
1288 : 2 : target->disable_chap = disable_chap;
1289 : 2 : target->require_chap = require_chap;
1290 : 2 : target->mutual_chap = mutual_chap;
1291 : 2 : target->chap_group = chap_group;
1292 [ - + ]: 2 : pthread_mutex_unlock(&target->mutex);
1293 : :
1294 : 2 : return 0;
1295 : : }
1296 : :
1297 : : static void
1298 : 92 : iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target,
1299 : : struct spdk_json_write_ctx *w)
1300 : : {
1301 : : struct spdk_iscsi_pg_map *pg_map;
1302 : : struct spdk_iscsi_ig_map *ig_map;
1303 : : struct spdk_scsi_lun *lun;
1304 : :
1305 : 92 : spdk_json_write_object_begin(w);
1306 : :
1307 : 92 : spdk_json_write_named_string(w, "name", target->name);
1308 : :
1309 [ + - ]: 92 : if (target->alias[0] != '\0') {
1310 : 92 : spdk_json_write_named_string(w, "alias_name", target->alias);
1311 : : }
1312 : :
1313 : 92 : spdk_json_write_named_array_begin(w, "pg_ig_maps");
1314 [ + + ]: 212 : TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1315 [ + + ]: 268 : TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1316 : 148 : spdk_json_write_object_begin(w);
1317 : 148 : spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag);
1318 : 148 : spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag);
1319 : 148 : spdk_json_write_object_end(w);
1320 : : }
1321 : : }
1322 : 92 : spdk_json_write_array_end(w);
1323 : :
1324 : 92 : spdk_json_write_named_array_begin(w, "luns");
1325 [ + + ]: 256 : for (lun = spdk_scsi_dev_get_first_lun(target->dev); lun != NULL;
1326 : 164 : lun = spdk_scsi_dev_get_next_lun(lun)) {
1327 : 164 : spdk_json_write_object_begin(w);
1328 : 164 : spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1329 : 164 : spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun));
1330 : 164 : spdk_json_write_object_end(w);
1331 : : }
1332 : 92 : spdk_json_write_array_end(w);
1333 : :
1334 : 92 : spdk_json_write_named_int32(w, "queue_depth", target->queue_depth);
1335 : :
1336 [ - + ]: 92 : spdk_json_write_named_bool(w, "disable_chap", target->disable_chap);
1337 [ - + ]: 92 : spdk_json_write_named_bool(w, "require_chap", target->require_chap);
1338 [ - + ]: 92 : spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap);
1339 : 92 : spdk_json_write_named_int32(w, "chap_group", target->chap_group);
1340 : :
1341 [ - + ]: 92 : spdk_json_write_named_bool(w, "header_digest", target->header_digest);
1342 [ - + ]: 92 : spdk_json_write_named_bool(w, "data_digest", target->data_digest);
1343 : :
1344 : 92 : spdk_json_write_object_end(w);
1345 : 92 : }
1346 : :
1347 : : static void
1348 : 8 : iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target,
1349 : : struct spdk_json_write_ctx *w)
1350 : : {
1351 : 8 : spdk_json_write_object_begin(w);
1352 : :
1353 : 8 : spdk_json_write_named_string(w, "method", "iscsi_create_target_node");
1354 : :
1355 : 8 : spdk_json_write_name(w, "params");
1356 : 8 : iscsi_tgt_node_info_json(target, w);
1357 : :
1358 : 8 : spdk_json_write_object_end(w);
1359 : 8 : }
1360 : :
1361 : : void
1362 : 142 : iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w)
1363 : : {
1364 : : struct spdk_iscsi_tgt_node *target;
1365 : :
1366 [ + + ]: 226 : TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1367 : 84 : iscsi_tgt_node_info_json(target, w);
1368 : : }
1369 : 142 : }
1370 : :
1371 : : void
1372 : 94 : iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w)
1373 : : {
1374 : : struct spdk_iscsi_tgt_node *target;
1375 : :
1376 [ + + ]: 102 : TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1377 : 8 : iscsi_tgt_node_config_json(target, w);
1378 : : }
1379 : 94 : }
1380 : :
1381 : : int
1382 : 0 : iscsi_tgt_node_enable_histogram(struct spdk_iscsi_tgt_node *target, bool enable)
1383 : : {
1384 [ # # ]: 0 : if (enable) {
1385 [ # # ]: 0 : if (!target->histogram) {
1386 : 0 : target->histogram = spdk_histogram_data_alloc();
1387 [ # # ]: 0 : if (target->histogram == NULL) {
1388 : 0 : SPDK_ERRLOG("could not allocate histogram\n");
1389 : 0 : return -ENOMEM;
1390 : : }
1391 : : }
1392 : : } else {
1393 [ # # ]: 0 : if (target->histogram) {
1394 : 0 : spdk_histogram_data_free(target->histogram);
1395 : 0 : target->histogram = NULL;
1396 : : }
1397 : : }
1398 : :
1399 : 0 : return 0;
1400 : : }
|