Skip to content

Commit 565edd1

Browse files
Guy Shapirodledford
authored andcommitted
IB/addr: Pass network namespace as a parameter
Add network namespace support to the ib_addr module. For that, all the address resolution and matching should be done using the appropriate namespace instead of init_net. This is achieved by: 1. Adding an explicit network namespace argument to exported function that require a namespace. 2. Saving the namespace in the rdma_addr_client structure. 3. Using it when calling networking functions. In order to preserve the behavior of calling modules, &init_net is passed as the parameter in calls from other modules. This is modified as namespace support is added on more levels. Signed-off-by: Haggai Eran <[email protected]> Signed-off-by: Yotam Kenneth <[email protected]> Signed-off-by: Shachar Raindel <[email protected]> Signed-off-by: Guy Shapiro <[email protected]> Signed-off-by: Doug Ledford <[email protected]>
1 parent 630c318 commit 565edd1

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

drivers/infiniband/core/addr.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ int rdma_translate_ip(struct sockaddr *addr, struct rdma_dev_addr *dev_addr,
128128
int ret = -EADDRNOTAVAIL;
129129

130130
if (dev_addr->bound_dev_if) {
131-
dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
131+
dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
132132
if (!dev)
133133
return -ENODEV;
134134
ret = rdma_copy_addr(dev_addr, dev, NULL);
@@ -138,7 +138,7 @@ int rdma_translate_ip(struct sockaddr *addr, struct rdma_dev_addr *dev_addr,
138138

139139
switch (addr->sa_family) {
140140
case AF_INET:
141-
dev = ip_dev_find(&init_net,
141+
dev = ip_dev_find(dev_addr->net,
142142
((struct sockaddr_in *) addr)->sin_addr.s_addr);
143143

144144
if (!dev)
@@ -149,12 +149,11 @@ int rdma_translate_ip(struct sockaddr *addr, struct rdma_dev_addr *dev_addr,
149149
*vlan_id = rdma_vlan_dev_vlan_id(dev);
150150
dev_put(dev);
151151
break;
152-
153152
#if IS_ENABLED(CONFIG_IPV6)
154153
case AF_INET6:
155154
rcu_read_lock();
156-
for_each_netdev_rcu(&init_net, dev) {
157-
if (ipv6_chk_addr(&init_net,
155+
for_each_netdev_rcu(dev_addr->net, dev) {
156+
if (ipv6_chk_addr(dev_addr->net,
158157
&((struct sockaddr_in6 *) addr)->sin6_addr,
159158
dev, 1)) {
160159
ret = rdma_copy_addr(dev_addr, dev, NULL);
@@ -236,7 +235,7 @@ static int addr4_resolve(struct sockaddr_in *src_in,
236235
fl4.daddr = dst_ip;
237236
fl4.saddr = src_ip;
238237
fl4.flowi4_oif = addr->bound_dev_if;
239-
rt = ip_route_output_key(&init_net, &fl4);
238+
rt = ip_route_output_key(addr->net, &fl4);
240239
if (IS_ERR(rt)) {
241240
ret = PTR_ERR(rt);
242241
goto out;
@@ -278,12 +277,12 @@ static int addr6_resolve(struct sockaddr_in6 *src_in,
278277
fl6.saddr = src_in->sin6_addr;
279278
fl6.flowi6_oif = addr->bound_dev_if;
280279

281-
dst = ip6_route_output(&init_net, NULL, &fl6);
280+
dst = ip6_route_output(addr->net, NULL, &fl6);
282281
if ((ret = dst->error))
283282
goto put;
284283

285284
if (ipv6_addr_any(&fl6.saddr)) {
286-
ret = ipv6_dev_get_saddr(&init_net, ip6_dst_idev(dst)->dev,
285+
ret = ipv6_dev_get_saddr(addr->net, ip6_dst_idev(dst)->dev,
287286
&fl6.daddr, 0, &fl6.saddr);
288287
if (ret)
289288
goto put;
@@ -477,6 +476,7 @@ int rdma_addr_find_dmac_by_grh(const union ib_gid *sgid, const union ib_gid *dgi
477476

478477
memset(&dev_addr, 0, sizeof(dev_addr));
479478
dev_addr.bound_dev_if = if_index;
479+
dev_addr.net = &init_net;
480480

481481
ctx.addr = &dev_addr;
482482
init_completion(&ctx.comp);
@@ -511,6 +511,7 @@ int rdma_addr_find_smac_by_sgid(union ib_gid *sgid, u8 *smac, u16 *vlan_id)
511511
rdma_gid2ip(&gid_addr._sockaddr, sgid);
512512

513513
memset(&dev_addr, 0, sizeof(dev_addr));
514+
dev_addr.net = &init_net;
514515
ret = rdma_translate_ip(&gid_addr._sockaddr, &dev_addr, vlan_id);
515516
if (ret)
516517
return ret;

drivers/infiniband/core/cma.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
610610
INIT_LIST_HEAD(&id_priv->listen_list);
611611
INIT_LIST_HEAD(&id_priv->mc_list);
612612
get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
613+
id_priv->id.route.addr.dev_addr.net = &init_net;
613614

614615
return &id_priv->id;
615616
}

include/rdma/ib_addr.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <rdma/ib_verbs.h>
4848
#include <rdma/ib_pack.h>
4949
#include <net/ipv6.h>
50+
#include <net/net_namespace.h>
5051

5152
struct rdma_addr_client {
5253
atomic_t refcount;
@@ -64,18 +65,31 @@ void rdma_addr_register_client(struct rdma_addr_client *client);
6465
*/
6566
void rdma_addr_unregister_client(struct rdma_addr_client *client);
6667

68+
/**
69+
* struct rdma_dev_addr - Contains resolved RDMA hardware addresses
70+
* @src_dev_addr: Source MAC address.
71+
* @dst_dev_addr: Destination MAC address.
72+
* @broadcast: Broadcast address of the device.
73+
* @dev_type: The interface hardware type of the device.
74+
* @bound_dev_if: An optional device interface index.
75+
* @transport: The transport type used.
76+
* @net: Network namespace containing the bound_dev_if net_dev.
77+
*/
6778
struct rdma_dev_addr {
6879
unsigned char src_dev_addr[MAX_ADDR_LEN];
6980
unsigned char dst_dev_addr[MAX_ADDR_LEN];
7081
unsigned char broadcast[MAX_ADDR_LEN];
7182
unsigned short dev_type;
7283
int bound_dev_if;
7384
enum rdma_transport_type transport;
85+
struct net *net;
7486
};
7587

7688
/**
7789
* rdma_translate_ip - Translate a local IP address to an RDMA hardware
7890
* address.
91+
*
92+
* The dev_addr->net field must be initialized.
7993
*/
8094
int rdma_translate_ip(struct sockaddr *addr, struct rdma_dev_addr *dev_addr,
8195
u16 *vlan_id);
@@ -90,7 +104,7 @@ int rdma_translate_ip(struct sockaddr *addr, struct rdma_dev_addr *dev_addr,
90104
* @dst_addr: The destination address to resolve.
91105
* @addr: A reference to a data location that will receive the resolved
92106
* addresses. The data location must remain valid until the callback has
93-
* been invoked.
107+
* been invoked. The net field of the addr struct must be valid.
94108
* @timeout_ms: Amount of time to wait for the address resolution to complete.
95109
* @callback: Call invoked once address resolution has completed, timed out,
96110
* or been canceled. A status of 0 indicates success.

0 commit comments

Comments
 (0)