2 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the names of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * Alternatively, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL") version 2 as published by the Free
18 * Software Foundation.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <linux/module.h>
34 #include <net/netlink.h>
35 #include <rdma/rdma_netlink.h>
37 #include "core_priv.h"
39 static const struct nla_policy nldev_policy
[RDMA_NLDEV_ATTR_MAX
] = {
40 [RDMA_NLDEV_ATTR_DEV_INDEX
] = { .type
= NLA_U32
},
41 [RDMA_NLDEV_ATTR_DEV_NAME
] = { .type
= NLA_NUL_STRING
,
42 .len
= IB_DEVICE_NAME_MAX
- 1},
43 [RDMA_NLDEV_ATTR_PORT_INDEX
] = { .type
= NLA_U32
},
44 [RDMA_NLDEV_ATTR_FW_VERSION
] = { .type
= NLA_NUL_STRING
,
45 .len
= IB_FW_VERSION_NAME_MAX
- 1},
46 [RDMA_NLDEV_ATTR_NODE_GUID
] = { .type
= NLA_U64
},
47 [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID
] = { .type
= NLA_U64
},
48 [RDMA_NLDEV_ATTR_SUBNET_PREFIX
] = { .type
= NLA_U64
},
49 [RDMA_NLDEV_ATTR_LID
] = { .type
= NLA_U32
},
50 [RDMA_NLDEV_ATTR_SM_LID
] = { .type
= NLA_U32
},
51 [RDMA_NLDEV_ATTR_LMC
] = { .type
= NLA_U8
},
52 [RDMA_NLDEV_ATTR_PORT_STATE
] = { .type
= NLA_U8
},
53 [RDMA_NLDEV_ATTR_PORT_PHYS_STATE
] = { .type
= NLA_U8
},
54 [RDMA_NLDEV_ATTR_DEV_NODE_TYPE
] = { .type
= NLA_U8
},
57 static int fill_dev_info(struct sk_buff
*msg
, struct ib_device
*device
)
59 char fw
[IB_FW_VERSION_NAME_MAX
];
61 if (nla_put_u32(msg
, RDMA_NLDEV_ATTR_DEV_INDEX
, device
->index
))
63 if (nla_put_string(msg
, RDMA_NLDEV_ATTR_DEV_NAME
, device
->name
))
65 if (nla_put_u32(msg
, RDMA_NLDEV_ATTR_PORT_INDEX
, rdma_end_port(device
)))
68 BUILD_BUG_ON(sizeof(device
->attrs
.device_cap_flags
) != sizeof(u64
));
69 if (nla_put_u64_64bit(msg
, RDMA_NLDEV_ATTR_CAP_FLAGS
,
70 device
->attrs
.device_cap_flags
, 0))
73 ib_get_device_fw_str(device
, fw
);
74 /* Device without FW has strlen(fw) */
75 if (strlen(fw
) && nla_put_string(msg
, RDMA_NLDEV_ATTR_FW_VERSION
, fw
))
78 if (nla_put_u64_64bit(msg
, RDMA_NLDEV_ATTR_NODE_GUID
,
79 be64_to_cpu(device
->node_guid
), 0))
81 if (nla_put_u64_64bit(msg
, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID
,
82 be64_to_cpu(device
->attrs
.sys_image_guid
), 0))
84 if (nla_put_u8(msg
, RDMA_NLDEV_ATTR_DEV_NODE_TYPE
, device
->node_type
))
89 static int fill_port_info(struct sk_buff
*msg
,
90 struct ib_device
*device
, u32 port
)
92 struct ib_port_attr attr
;
95 if (nla_put_u32(msg
, RDMA_NLDEV_ATTR_DEV_INDEX
, device
->index
))
97 if (nla_put_string(msg
, RDMA_NLDEV_ATTR_DEV_NAME
, device
->name
))
99 if (nla_put_u32(msg
, RDMA_NLDEV_ATTR_PORT_INDEX
, port
))
102 ret
= ib_query_port(device
, port
, &attr
);
106 BUILD_BUG_ON(sizeof(attr
.port_cap_flags
) > sizeof(u64
));
107 if (nla_put_u64_64bit(msg
, RDMA_NLDEV_ATTR_CAP_FLAGS
,
108 (u64
)attr
.port_cap_flags
, 0))
110 if (rdma_protocol_ib(device
, port
) &&
111 nla_put_u64_64bit(msg
, RDMA_NLDEV_ATTR_SUBNET_PREFIX
,
112 attr
.subnet_prefix
, 0))
114 if (rdma_protocol_ib(device
, port
)) {
115 if (nla_put_u32(msg
, RDMA_NLDEV_ATTR_LID
, attr
.lid
))
117 if (nla_put_u32(msg
, RDMA_NLDEV_ATTR_SM_LID
, attr
.sm_lid
))
119 if (nla_put_u8(msg
, RDMA_NLDEV_ATTR_LMC
, attr
.lmc
))
122 if (nla_put_u8(msg
, RDMA_NLDEV_ATTR_PORT_STATE
, attr
.state
))
124 if (nla_put_u8(msg
, RDMA_NLDEV_ATTR_PORT_PHYS_STATE
, attr
.phys_state
))
129 static int nldev_get_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
130 struct netlink_ext_ack
*extack
)
132 struct nlattr
*tb
[RDMA_NLDEV_ATTR_MAX
];
133 struct ib_device
*device
;
138 err
= nlmsg_parse(nlh
, 0, tb
, RDMA_NLDEV_ATTR_MAX
- 1,
139 nldev_policy
, extack
);
140 if (err
|| !tb
[RDMA_NLDEV_ATTR_DEV_INDEX
])
143 index
= nla_get_u32(tb
[RDMA_NLDEV_ATTR_DEV_INDEX
]);
145 device
= ib_device_get_by_index(index
);
149 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
155 nlh
= nlmsg_put(msg
, NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
,
156 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV
, RDMA_NLDEV_CMD_GET
),
159 err
= fill_dev_info(msg
, device
);
165 put_device(&device
->dev
);
166 return rdma_nl_unicast(msg
, NETLINK_CB(skb
).portid
);
171 put_device(&device
->dev
);
175 static int _nldev_get_dumpit(struct ib_device
*device
,
177 struct netlink_callback
*cb
,
180 int start
= cb
->args
[0];
181 struct nlmsghdr
*nlh
;
186 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
187 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV
, RDMA_NLDEV_CMD_GET
),
190 if (fill_dev_info(skb
, device
)) {
191 nlmsg_cancel(skb
, nlh
);
199 out
: cb
->args
[0] = idx
;
203 static int nldev_get_dumpit(struct sk_buff
*skb
, struct netlink_callback
*cb
)
206 * There is no need to take lock, because
207 * we are relying on ib_core's lists_rwsem
209 return ib_enum_all_devs(_nldev_get_dumpit
, skb
, cb
);
212 static int nldev_port_get_doit(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
213 struct netlink_ext_ack
*extack
)
215 struct nlattr
*tb
[RDMA_NLDEV_ATTR_MAX
];
216 struct ib_device
*device
;
222 err
= nlmsg_parse(nlh
, 0, tb
, RDMA_NLDEV_ATTR_MAX
- 1,
223 nldev_policy
, extack
);
225 !tb
[RDMA_NLDEV_ATTR_DEV_INDEX
] ||
226 !tb
[RDMA_NLDEV_ATTR_PORT_INDEX
])
229 index
= nla_get_u32(tb
[RDMA_NLDEV_ATTR_DEV_INDEX
]);
230 device
= ib_device_get_by_index(index
);
234 port
= nla_get_u32(tb
[RDMA_NLDEV_ATTR_PORT_INDEX
]);
235 if (!rdma_is_port_valid(device
, port
)) {
240 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
246 nlh
= nlmsg_put(msg
, NETLINK_CB(skb
).portid
, nlh
->nlmsg_seq
,
247 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV
, RDMA_NLDEV_CMD_GET
),
250 err
= fill_port_info(msg
, device
, port
);
255 put_device(&device
->dev
);
257 return rdma_nl_unicast(msg
, NETLINK_CB(skb
).portid
);
262 put_device(&device
->dev
);
266 static int nldev_port_get_dumpit(struct sk_buff
*skb
,
267 struct netlink_callback
*cb
)
269 struct nlattr
*tb
[RDMA_NLDEV_ATTR_MAX
];
270 struct ib_device
*device
;
271 int start
= cb
->args
[0];
272 struct nlmsghdr
*nlh
;
278 err
= nlmsg_parse(cb
->nlh
, 0, tb
, RDMA_NLDEV_ATTR_MAX
- 1,
280 if (err
|| !tb
[RDMA_NLDEV_ATTR_DEV_INDEX
])
283 ifindex
= nla_get_u32(tb
[RDMA_NLDEV_ATTR_DEV_INDEX
]);
284 device
= ib_device_get_by_index(ifindex
);
288 for (p
= rdma_start_port(device
); p
<= rdma_end_port(device
); ++p
) {
290 * The dumpit function returns all information from specific
291 * index. This specific index is taken from the netlink
292 * messages request sent by user and it is available
295 * Usually, the user doesn't fill this field and it causes
296 * to return everything.
304 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
,
306 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV
,
307 RDMA_NLDEV_CMD_PORT_GET
),
310 if (fill_port_info(skb
, device
, p
)) {
311 nlmsg_cancel(skb
, nlh
);
319 put_device(&device
->dev
);
324 static const struct rdma_nl_cbs nldev_cb_table
[RDMA_NLDEV_NUM_OPS
] = {
325 [RDMA_NLDEV_CMD_GET
] = {
326 .doit
= nldev_get_doit
,
327 .dump
= nldev_get_dumpit
,
329 [RDMA_NLDEV_CMD_PORT_GET
] = {
330 .doit
= nldev_port_get_doit
,
331 .dump
= nldev_port_get_dumpit
,
335 void __init
nldev_init(void)
337 rdma_nl_register(RDMA_NL_NLDEV
, nldev_cb_table
);
340 void __exit
nldev_exit(void)
342 rdma_nl_unregister(RDMA_NL_NLDEV
);
345 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV
, 5);