There are 10+ disks and 40+ CPUs in typical storage server. rather than single-process-multi-threads model (one tgtd with multi LUNs), we usually prefer to use multi-processes model (each tgtd with a backend). and we can get better performence and full isolation for backends. each tgtd need to listen different TCP port. This patch adds support for listening on random TCP port, and use ISCSI_LISTEN_PORT instead of 3260. Test cases: # tgtd -f --iscsi portal=127.0.0.1 # tgtadm --lld iscsi --mode portal --op show Portal: 127.0.0.1:3260,1 # tgtd -f --iscsi portal=127.0.0.1:3261 # tgtadm --lld iscsi --mode portal --op show Portal: 127.0.0.1:3261,1 # tgtd -f --iscsi portal=127.0.0.1:0 # tgtadm --lld iscsi --mode portal --op show Portal: 127.0.0.1:36753,1 # tgtd -f --iscsi portal=[fe80::da9e:f3ff:fe8e:d40e%eth0] # tgtadm --lld iscsi --mode portal --op show Portal: [fe80::da9e:f3ff:fe8e:d40e]:3260,1 # tgtd -f --iscsi portal=[fe80::da9e:f3ff:fe8e:d40e%eth0]:3261 # tgtadm --lld iscsi --mode portal --op show Portal: [fe80::da9e:f3ff:fe8e:d40e]:3261,1 # tgtd -f --iscsi portal=[fe80::da9e:f3ff:fe8e:d40e%eth0]:0 # tgtadm --lld iscsi --mode portal --op show Portal: [fe80::da9e:f3ff:fe8e:d40e]:18795,1 Signed-off-by: zhenwei pi <pizhenwei@xxxxxxxxxxxxx> Signed-off-by: honghao wang <wanghonghao@xxxxxxxxxxxxx> --- usr/iscsi/iscsi_tcp.c | 19 +++++++++++++++++-- usr/iscsi/iscsid.c | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/usr/iscsi/iscsi_tcp.c b/usr/iscsi/iscsi_tcp.c index 0a076b0..2bb8356 100644 --- a/usr/iscsi/iscsi_tcp.c +++ b/usr/iscsi/iscsi_tcp.c @@ -298,7 +298,11 @@ int iscsi_tcp_init_portal(char *addr, int port, int tpgt) char addrstr[64]; void *addrptr = NULL; - port = port ? port : ISCSI_LISTEN_PORT; + if (port < 0 || port > 65535) { + errno = EINVAL; + eprintf("port out of range, %m\n"); + return -errno; + } memset(servname, 0, sizeof(servname)); snprintf(servname, sizeof(servname), "%d", port); @@ -355,6 +359,13 @@ int iscsi_tcp_init_portal(char *addr, int port, int tpgt) continue; } + ret = getsockname(fd, res->ai_addr, &res->ai_addrlen); + if (ret) { + close(fd); + eprintf("unable to get socket address, %m\n"); + continue; + } + set_non_blocking(fd); ret = tgt_event_add(fd, EPOLLIN, accept_connection, NULL); if (ret) @@ -369,10 +380,14 @@ int iscsi_tcp_init_portal(char *addr, int port, int tpgt) case AF_INET: addrptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr; + port = ntohs(((struct sockaddr_in *) + res->ai_addr)->sin_port); break; case AF_INET6: addrptr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr; + port = ntohs(((struct sockaddr_in6 *) + res->ai_addr)->sin6_port); break; } portal->addr = strdup(inet_ntop(res->ai_family, addrptr, @@ -431,7 +446,7 @@ static int iscsi_tcp_init(void) for ipv4 and ipv6 */ if (list_empty(&iscsi_portals_list)) { - iscsi_add_portal(NULL, 3260, 1); + iscsi_add_portal(NULL, ISCSI_LISTEN_PORT, 1); } INIT_LIST_HEAD(&iscsi_tcp_conn_list); diff --git a/usr/iscsi/iscsid.c b/usr/iscsi/iscsid.c index 99da059..a54a46e 100644 --- a/usr/iscsi/iscsid.c +++ b/usr/iscsi/iscsid.c @@ -2469,7 +2469,7 @@ int iscsi_param_parse_portals(char *p, int do_add, while (*p) { if (!strncmp(p, "portal", 6)) { char *addr, *q; - int len = 0, port = 0; + int len = 0, port = ISCSI_LISTEN_PORT; addr = p + 7; -- 2.11.0