Skip to content

Commit d17a024

Browse files
paravmellanoxdsahern
authored andcommitted
rdma: Add an option to set net namespace of rdma device
Enrich rdmatool with an option to set network namespace of RDMA device. After successful execution of it, rdma device will be accessible only in assigned network namespace. rdma tool command examples and output. First set netns mode to exclusive. $ rdma system set netns exclusive Now create network namespace and assign RDMA device to this network namespace. $ ip netns add foo $ rdma dev set mlx5_1 netns foo Reviewed-by: Leon Romanovsky <leonro@mellanox.com> Signed-off-by: Parav Pandit <parav@mellanox.com> Signed-off-by: David Ahern <dsahern@gmail.com>
1 parent e861272 commit d17a024

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

rdma/dev.c

+37
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
* Authors: Leon Romanovsky <leonro@mellanox.com>
55
*/
66

7+
#include <fcntl.h>
78
#include "rdma.h"
89

910
static int dev_help(struct rd *rd)
1011
{
1112
pr_out("Usage: %s dev show [DEV]\n", rd->filename);
1213
pr_out(" %s dev set [DEV] name DEVNAME\n", rd->filename);
14+
pr_out(" %s dev set [DEV] netns NSNAME\n", rd->filename);
1315
return 0;
1416
}
1517

@@ -272,11 +274,46 @@ static int dev_set_name(struct rd *rd)
272274
return rd_sendrecv_msg(rd, seq);
273275
}
274276

277+
static int dev_set_netns(struct rd *rd)
278+
{
279+
char *netns_path;
280+
uint32_t seq;
281+
int netns;
282+
int ret;
283+
284+
if (rd_no_arg(rd)) {
285+
pr_err("Please provide device name.\n");
286+
return -EINVAL;
287+
}
288+
289+
if (asprintf(&netns_path, "%s/%s", NETNS_RUN_DIR, rd_argv(rd)) < 0)
290+
return -ENOMEM;
291+
292+
netns = open(netns_path, O_RDONLY | O_CLOEXEC);
293+
if (netns < 0) {
294+
fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
295+
rd_argv(rd), strerror(errno));
296+
ret = -EINVAL;
297+
goto done;
298+
}
299+
300+
rd_prepare_msg(rd, RDMA_NLDEV_CMD_SET,
301+
&seq, (NLM_F_REQUEST | NLM_F_ACK));
302+
mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
303+
mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_NET_NS_FD, netns);
304+
ret = rd_sendrecv_msg(rd, seq);
305+
close(netns);
306+
done:
307+
free(netns_path);
308+
return ret;
309+
}
310+
275311
static int dev_one_set(struct rd *rd)
276312
{
277313
const struct rd_cmd cmds[] = {
278314
{ NULL, dev_help},
279315
{ "name", dev_set_name},
316+
{ "netns", dev_set_netns},
280317
{ 0 }
281318
};
282319

0 commit comments

Comments
 (0)