Skip to content

Commit a9bb791

Browse files
Hefty, Seanrolandd
authored andcommitted
RDMA/cma: Add an ID_REUSEADDR option
Lustre requires that clients bind to a privileged port number before connecting to a remote server. On larger clusters (typically more than about 1000 nodes), the number of privileged ports is exhausted, resulting in lustre being unusable. To handle this, we add support for reusable addresses to the rdma_cm. This mimics the behavior of the socket option SO_REUSEADDR. A user may set an rdma_cm_id to reuse an address before calling rdma_bind_addr() (explicitly or implicitly). If set, other rdma_cm_id's may be bound to the same address, provided that they all have reuse enabled, and there are no active listens. If rdma_listen() is called on an rdma_cm_id that has reuse enabled, it will only succeed if there are no other id's bound to that same address. The reuse option is exported to user space. The behavior of the kernel reuse implementation was verified against that given by sockets. This patch is derived from a path by Ira Weiny <weiny2@llnl.gov> Signed-off-by: Sean Hefty <sean.hefty@intel.com> Signed-off-by: Roland Dreier <roland@purestorage.com>
1 parent 43b752d commit a9bb791

4 files changed

Lines changed: 143 additions & 69 deletions

File tree

‎drivers/infiniband/core/cma.c‎

Lines changed: 123 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ struct rdma_id_private {
148148
u32 qp_num;
149149
u8 srq;
150150
u8 tos;
151+
u8 reuseaddr;
151152
};
152153

153154
struct cma_multicast {
@@ -1579,50 +1580,6 @@ static void cma_listen_on_all(struct rdma_id_private *id_priv)
15791580
mutex_unlock(&lock);
15801581
}
15811582

1582-
int rdma_listen(struct rdma_cm_id *id, int backlog)
1583-
{
1584-
struct rdma_id_private *id_priv;
1585-
int ret;
1586-
1587-
id_priv = container_of(id, struct rdma_id_private, id);
1588-
if (id_priv->state == CMA_IDLE) {
1589-
((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET;
1590-
ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr);
1591-
if (ret)
1592-
return ret;
1593-
}
1594-
1595-
if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1596-
return -EINVAL;
1597-
1598-
id_priv->backlog = backlog;
1599-
if (id->device) {
1600-
switch (rdma_node_get_transport(id->device->node_type)) {
1601-
case RDMA_TRANSPORT_IB:
1602-
ret = cma_ib_listen(id_priv);
1603-
if (ret)
1604-
goto err;
1605-
break;
1606-
case RDMA_TRANSPORT_IWARP:
1607-
ret = cma_iw_listen(id_priv, backlog);
1608-
if (ret)
1609-
goto err;
1610-
break;
1611-
default:
1612-
ret = -ENOSYS;
1613-
goto err;
1614-
}
1615-
} else
1616-
cma_listen_on_all(id_priv);
1617-
1618-
return 0;
1619-
err:
1620-
id_priv->backlog = 0;
1621-
cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1622-
return ret;
1623-
}
1624-
EXPORT_SYMBOL(rdma_listen);
1625-
16261583
void rdma_set_service_type(struct rdma_cm_id *id, int tos)
16271584
{
16281585
struct rdma_id_private *id_priv;
@@ -2105,6 +2062,25 @@ int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
21052062
}
21062063
EXPORT_SYMBOL(rdma_resolve_addr);
21072064

2065+
int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2066+
{
2067+
struct rdma_id_private *id_priv;
2068+
unsigned long flags;
2069+
int ret;
2070+
2071+
id_priv = container_of(id, struct rdma_id_private, id);
2072+
spin_lock_irqsave(&id_priv->lock, flags);
2073+
if (id_priv->state == CMA_IDLE) {
2074+
id_priv->reuseaddr = reuse;
2075+
ret = 0;
2076+
} else {
2077+
ret = -EINVAL;
2078+
}
2079+
spin_unlock_irqrestore(&id_priv->lock, flags);
2080+
return ret;
2081+
}
2082+
EXPORT_SYMBOL(rdma_set_reuseaddr);
2083+
21082084
static void cma_bind_port(struct rdma_bind_list *bind_list,
21092085
struct rdma_id_private *id_priv)
21102086
{
@@ -2180,43 +2156,73 @@ static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
21802156
return -EADDRNOTAVAIL;
21812157
}
21822158

2183-
static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2159+
/*
2160+
* Check that the requested port is available. This is called when trying to
2161+
* bind to a specific port, or when trying to listen on a bound port. In
2162+
* the latter case, the provided id_priv may already be on the bind_list, but
2163+
* we still need to check that it's okay to start listening.
2164+
*/
2165+
static int cma_check_port(struct rdma_bind_list *bind_list,
2166+
struct rdma_id_private *id_priv, uint8_t reuseaddr)
21842167
{
21852168
struct rdma_id_private *cur_id;
21862169
struct sockaddr *addr, *cur_addr;
2187-
struct rdma_bind_list *bind_list;
21882170
struct hlist_node *node;
2189-
unsigned short snum;
21902171

21912172
addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
2192-
snum = ntohs(cma_port(addr));
2193-
if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2194-
return -EACCES;
2195-
2196-
bind_list = idr_find(ps, snum);
2197-
if (!bind_list)
2198-
return cma_alloc_port(ps, id_priv, snum);
2199-
2200-
/*
2201-
* We don't support binding to any address if anyone is bound to
2202-
* a specific address on the same port.
2203-
*/
2204-
if (cma_any_addr(addr))
2173+
if (cma_any_addr(addr) && !reuseaddr)
22052174
return -EADDRNOTAVAIL;
22062175

22072176
hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
2208-
cur_addr = (struct sockaddr *) &cur_id->id.route.addr.src_addr;
2209-
if (cma_any_addr(cur_addr))
2210-
return -EADDRNOTAVAIL;
2177+
if (id_priv == cur_id)
2178+
continue;
22112179

2212-
if (!cma_addr_cmp(addr, cur_addr))
2213-
return -EADDRINUSE;
2214-
}
2180+
if ((cur_id->state == CMA_LISTEN) ||
2181+
!reuseaddr || !cur_id->reuseaddr) {
2182+
cur_addr = (struct sockaddr *) &cur_id->id.route.addr.src_addr;
2183+
if (cma_any_addr(cur_addr))
2184+
return -EADDRNOTAVAIL;
22152185

2216-
cma_bind_port(bind_list, id_priv);
2186+
if (!cma_addr_cmp(addr, cur_addr))
2187+
return -EADDRINUSE;
2188+
}
2189+
}
22172190
return 0;
22182191
}
22192192

2193+
static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2194+
{
2195+
struct rdma_bind_list *bind_list;
2196+
unsigned short snum;
2197+
int ret;
2198+
2199+
snum = ntohs(cma_port((struct sockaddr *) &id_priv->id.route.addr.src_addr));
2200+
if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2201+
return -EACCES;
2202+
2203+
bind_list = idr_find(ps, snum);
2204+
if (!bind_list) {
2205+
ret = cma_alloc_port(ps, id_priv, snum);
2206+
} else {
2207+
ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2208+
if (!ret)
2209+
cma_bind_port(bind_list, id_priv);
2210+
}
2211+
return ret;
2212+
}
2213+
2214+
static int cma_bind_listen(struct rdma_id_private *id_priv)
2215+
{
2216+
struct rdma_bind_list *bind_list = id_priv->bind_list;
2217+
int ret = 0;
2218+
2219+
mutex_lock(&lock);
2220+
if (bind_list->owners.first->next)
2221+
ret = cma_check_port(bind_list, id_priv, 0);
2222+
mutex_unlock(&lock);
2223+
return ret;
2224+
}
2225+
22202226
static int cma_get_port(struct rdma_id_private *id_priv)
22212227
{
22222228
struct idr *ps;
@@ -2268,6 +2274,56 @@ static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
22682274
return 0;
22692275
}
22702276

2277+
int rdma_listen(struct rdma_cm_id *id, int backlog)
2278+
{
2279+
struct rdma_id_private *id_priv;
2280+
int ret;
2281+
2282+
id_priv = container_of(id, struct rdma_id_private, id);
2283+
if (id_priv->state == CMA_IDLE) {
2284+
((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET;
2285+
ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr);
2286+
if (ret)
2287+
return ret;
2288+
}
2289+
2290+
if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
2291+
return -EINVAL;
2292+
2293+
if (id_priv->reuseaddr) {
2294+
ret = cma_bind_listen(id_priv);
2295+
if (ret)
2296+
goto err;
2297+
}
2298+
2299+
id_priv->backlog = backlog;
2300+
if (id->device) {
2301+
switch (rdma_node_get_transport(id->device->node_type)) {
2302+
case RDMA_TRANSPORT_IB:
2303+
ret = cma_ib_listen(id_priv);
2304+
if (ret)
2305+
goto err;
2306+
break;
2307+
case RDMA_TRANSPORT_IWARP:
2308+
ret = cma_iw_listen(id_priv, backlog);
2309+
if (ret)
2310+
goto err;
2311+
break;
2312+
default:
2313+
ret = -ENOSYS;
2314+
goto err;
2315+
}
2316+
} else
2317+
cma_listen_on_all(id_priv);
2318+
2319+
return 0;
2320+
err:
2321+
id_priv->backlog = 0;
2322+
cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
2323+
return ret;
2324+
}
2325+
EXPORT_SYMBOL(rdma_listen);
2326+
22712327
int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
22722328
{
22732329
struct rdma_id_private *id_priv;

‎drivers/infiniband/core/ucma.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,13 @@ static int ucma_set_option_id(struct ucma_context *ctx, int optname,
883883
}
884884
rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
885885
break;
886+
case RDMA_OPTION_ID_REUSEADDR:
887+
if (optlen != sizeof(int)) {
888+
ret = -EINVAL;
889+
break;
890+
}
891+
ret = rdma_set_reuseaddr(ctx->cm_id, *((int *) optval) ? 1 : 0);
892+
break;
886893
default:
887894
ret = -ENOSYS;
888895
}

‎include/rdma/rdma_cm.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,14 @@ void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr);
329329
*/
330330
void rdma_set_service_type(struct rdma_cm_id *id, int tos);
331331

332+
/**
333+
* rdma_set_reuseaddr - Allow the reuse of local addresses when binding
334+
* the rdma_cm_id.
335+
* @id: Communication identifier to configure.
336+
* @reuse: Value indicating if the bound address is reusable.
337+
*
338+
* Reuse must be set before an address is bound to the id.
339+
*/
340+
int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse);
341+
332342
#endif /* RDMA_CM_H */

‎include/rdma/rdma_user_cm.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ enum {
221221

222222
/* Option details */
223223
enum {
224-
RDMA_OPTION_ID_TOS = 0,
225-
RDMA_OPTION_IB_PATH = 1
224+
RDMA_OPTION_ID_TOS = 0,
225+
RDMA_OPTION_ID_REUSEADDR = 1,
226+
RDMA_OPTION_IB_PATH = 1
226227
};
227228

228229
struct rdma_ucm_set_option {

0 commit comments

Comments
 (0)