]>
Commit | Line | Data |
---|---|---|
b2cbae2c RD |
1 | /* |
2 | * Copyright (c) 2010 Voltaire Inc. All rights reserved. | |
3 | * | |
4 | * This software is available to you under a choice of one of two | |
5 | * licenses. You may choose to be licensed under the terms of the GNU | |
6 | * General Public License (GPL) Version 2, available from the file | |
7 | * COPYING in the main directory of this source tree, or the | |
8 | * OpenIB.org BSD license below: | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or | |
11 | * without modification, are permitted provided that the following | |
12 | * conditions are met: | |
13 | * | |
14 | * - Redistributions of source code must retain the above | |
15 | * copyright notice, this list of conditions and the following | |
16 | * disclaimer. | |
17 | * | |
18 | * - Redistributions in binary form must reproduce the above | |
19 | * copyright notice, this list of conditions and the following | |
20 | * disclaimer in the documentation and/or other materials | |
21 | * provided with the distribution. | |
22 | * | |
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
30 | * SOFTWARE. | |
31 | */ | |
32 | ||
33 | #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__ | |
34 | ||
b108d976 | 35 | #include <linux/export.h> |
b2cbae2c RD |
36 | #include <net/netlink.h> |
37 | #include <net/net_namespace.h> | |
38 | #include <net/sock.h> | |
39 | #include <rdma/rdma_netlink.h> | |
233c1955 | 40 | #include "core_priv.h" |
b2cbae2c | 41 | |
c9901724 | 42 | #include "core_priv.h" |
b2cbae2c | 43 | |
c9901724 | 44 | static DEFINE_MUTEX(rdma_nl_mutex); |
b2cbae2c | 45 | static struct sock *nls; |
c9901724 LR |
46 | static struct { |
47 | const struct ibnl_client_cbs *cb_table; | |
48 | } rdma_nl_types[RDMA_NL_NUM_CLIENTS]; | |
b2cbae2c | 49 | |
bc10ed7d KW |
50 | int ibnl_chk_listeners(unsigned int group) |
51 | { | |
52 | if (netlink_has_listeners(nls, group) == 0) | |
53 | return -1; | |
54 | return 0; | |
55 | } | |
bc10ed7d | 56 | |
c9901724 | 57 | static bool is_nl_msg_valid(unsigned int type, unsigned int op) |
b2cbae2c | 58 | { |
c9901724 LR |
59 | static const unsigned int max_num_ops[RDMA_NL_NUM_CLIENTS - 1] = { |
60 | RDMA_NL_RDMA_CM_NUM_OPS, | |
61 | RDMA_NL_IWPM_NUM_OPS, | |
62 | 0, | |
63 | RDMA_NL_LS_NUM_OPS, | |
64 | 0 }; | |
b2cbae2c | 65 | |
c9901724 LR |
66 | /* |
67 | * This BUILD_BUG_ON is intended to catch addition of new | |
68 | * RDMA netlink protocol without updating the array above. | |
69 | */ | |
70 | BUILD_BUG_ON(RDMA_NL_NUM_CLIENTS != 6); | |
b2cbae2c | 71 | |
c9901724 LR |
72 | if (type > RDMA_NL_NUM_CLIENTS - 1) |
73 | return false; | |
b2cbae2c | 74 | |
c9901724 LR |
75 | return (op < max_num_ops[type - 1]) ? true : false; |
76 | } | |
b2cbae2c | 77 | |
c9901724 LR |
78 | static bool is_nl_valid(unsigned int type, unsigned int op) |
79 | { | |
80 | if (!is_nl_msg_valid(type, op) || | |
81 | !rdma_nl_types[type].cb_table || | |
82 | !rdma_nl_types[type].cb_table[op].dump) | |
83 | return false; | |
84 | return true; | |
85 | } | |
b2cbae2c | 86 | |
c9901724 LR |
87 | void rdma_nl_register(unsigned int index, |
88 | const struct ibnl_client_cbs cb_table[]) | |
89 | { | |
90 | mutex_lock(&rdma_nl_mutex); | |
91 | if (!is_nl_msg_valid(index, 0)) { | |
92 | /* | |
93 | * All clients are not interesting in success/failure of | |
94 | * this call. They want to see the print to error log and | |
95 | * continue their initialization. Print warning for them, | |
96 | * because it is programmer's error to be here. | |
97 | */ | |
98 | mutex_unlock(&rdma_nl_mutex); | |
99 | WARN(true, | |
100 | "The not-valid %u index was supplied to RDMA netlink\n", | |
101 | index); | |
102 | return; | |
103 | } | |
b2cbae2c | 104 | |
c9901724 LR |
105 | if (rdma_nl_types[index].cb_table) { |
106 | mutex_unlock(&rdma_nl_mutex); | |
107 | WARN(true, | |
108 | "The %u index is already registered in RDMA netlink\n", | |
109 | index); | |
110 | return; | |
111 | } | |
b2cbae2c | 112 | |
c9901724 LR |
113 | rdma_nl_types[index].cb_table = cb_table; |
114 | mutex_unlock(&rdma_nl_mutex); | |
b2cbae2c | 115 | } |
c9901724 | 116 | EXPORT_SYMBOL(rdma_nl_register); |
b2cbae2c | 117 | |
c9901724 | 118 | void rdma_nl_unregister(unsigned int index) |
b2cbae2c | 119 | { |
c9901724 LR |
120 | mutex_lock(&rdma_nl_mutex); |
121 | rdma_nl_types[index].cb_table = NULL; | |
122 | mutex_unlock(&rdma_nl_mutex); | |
b2cbae2c | 123 | } |
c9901724 | 124 | EXPORT_SYMBOL(rdma_nl_unregister); |
b2cbae2c RD |
125 | |
126 | void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq, | |
30dc5e63 | 127 | int len, int client, int op, int flags) |
b2cbae2c RD |
128 | { |
129 | unsigned char *prev_tail; | |
130 | ||
131 | prev_tail = skb_tail_pointer(skb); | |
e0527334 | 132 | *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op), |
30dc5e63 | 133 | len, flags); |
e0527334 DM |
134 | if (!*nlh) |
135 | goto out_nlmsg_trim; | |
b2cbae2c | 136 | (*nlh)->nlmsg_len = skb_tail_pointer(skb) - prev_tail; |
e0527334 | 137 | return nlmsg_data(*nlh); |
b2cbae2c | 138 | |
e0527334 | 139 | out_nlmsg_trim: |
b2cbae2c RD |
140 | nlmsg_trim(skb, prev_tail); |
141 | return NULL; | |
142 | } | |
143 | EXPORT_SYMBOL(ibnl_put_msg); | |
144 | ||
145 | int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh, | |
146 | int len, void *data, int type) | |
147 | { | |
148 | unsigned char *prev_tail; | |
149 | ||
150 | prev_tail = skb_tail_pointer(skb); | |
4e24ffa4 DM |
151 | if (nla_put(skb, type, len, data)) |
152 | goto nla_put_failure; | |
b2cbae2c RD |
153 | nlh->nlmsg_len += skb_tail_pointer(skb) - prev_tail; |
154 | return 0; | |
155 | ||
156 | nla_put_failure: | |
157 | nlmsg_trim(skb, prev_tail - nlh->nlmsg_len); | |
158 | return -EMSGSIZE; | |
159 | } | |
160 | EXPORT_SYMBOL(ibnl_put_attr); | |
161 | ||
2d4bc933 JB |
162 | static int ibnl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
163 | struct netlink_ext_ack *extack) | |
b2cbae2c | 164 | { |
b2cbae2c | 165 | int type = nlh->nlmsg_type; |
c9901724 | 166 | unsigned int index = RDMA_NL_GET_CLIENT(type); |
1ae5ccc7 | 167 | unsigned int op = RDMA_NL_GET_OP(type); |
c9901724 LR |
168 | struct netlink_callback cb = {}; |
169 | struct netlink_dump_control c = {}; | |
b2cbae2c | 170 | |
c9901724 LR |
171 | if (!is_nl_valid(index, op)) |
172 | return -EINVAL; | |
173 | ||
174 | /* | |
175 | * For response or local service set_timeout request, | |
176 | * there is no need to use netlink_dump_start. | |
177 | */ | |
178 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST) || | |
179 | (index == RDMA_NL_LS && op == RDMA_NL_LS_OP_SET_TIMEOUT)) { | |
180 | cb.skb = skb; | |
181 | cb.nlh = nlh; | |
182 | cb.dump = rdma_nl_types[index].cb_table[op].dump; | |
c9901724 | 183 | return cb.dump(skb, &cb); |
b2cbae2c RD |
184 | } |
185 | ||
c9901724 | 186 | c.dump = rdma_nl_types[index].cb_table[op].dump; |
c9901724 | 187 | return netlink_dump_start(nls, skb, nlh, &c); |
b2cbae2c RD |
188 | } |
189 | ||
bc10ed7d KW |
190 | static void ibnl_rcv_reply_skb(struct sk_buff *skb) |
191 | { | |
192 | struct nlmsghdr *nlh; | |
193 | int msglen; | |
194 | ||
195 | /* | |
196 | * Process responses until there is no more message or the first | |
197 | * request. Generally speaking, it is not recommended to mix responses | |
198 | * with requests. | |
199 | */ | |
200 | while (skb->len >= nlmsg_total_size(0)) { | |
201 | nlh = nlmsg_hdr(skb); | |
202 | ||
203 | if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len) | |
204 | return; | |
205 | ||
206 | /* Handle response only */ | |
207 | if (nlh->nlmsg_flags & NLM_F_REQUEST) | |
208 | return; | |
209 | ||
2d4bc933 | 210 | ibnl_rcv_msg(skb, nlh, NULL); |
bc10ed7d KW |
211 | |
212 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); | |
213 | if (msglen > skb->len) | |
214 | msglen = skb->len; | |
215 | skb_pull(skb, msglen); | |
216 | } | |
217 | } | |
218 | ||
b2cbae2c RD |
219 | static void ibnl_rcv(struct sk_buff *skb) |
220 | { | |
c9901724 | 221 | mutex_lock(&rdma_nl_mutex); |
bc10ed7d | 222 | ibnl_rcv_reply_skb(skb); |
b2cbae2c | 223 | netlink_rcv_skb(skb, &ibnl_rcv_msg); |
c9901724 | 224 | mutex_unlock(&rdma_nl_mutex); |
b2cbae2c RD |
225 | } |
226 | ||
30dc5e63 TN |
227 | int ibnl_unicast(struct sk_buff *skb, struct nlmsghdr *nlh, |
228 | __u32 pid) | |
229 | { | |
cea05ead MI |
230 | int err; |
231 | ||
9047811b | 232 | err = netlink_unicast(nls, skb, pid, MSG_DONTWAIT); |
cea05ead | 233 | return (err < 0) ? err : 0; |
30dc5e63 TN |
234 | } |
235 | EXPORT_SYMBOL(ibnl_unicast); | |
236 | ||
9047811b IM |
237 | int ibnl_unicast_wait(struct sk_buff *skb, struct nlmsghdr *nlh, |
238 | __u32 pid) | |
239 | { | |
240 | int err; | |
241 | ||
242 | err = netlink_unicast(nls, skb, pid, 0); | |
243 | return (err < 0) ? err : 0; | |
244 | } | |
245 | EXPORT_SYMBOL(ibnl_unicast_wait); | |
246 | ||
30dc5e63 TN |
247 | int ibnl_multicast(struct sk_buff *skb, struct nlmsghdr *nlh, |
248 | unsigned int group, gfp_t flags) | |
249 | { | |
250 | return nlmsg_multicast(nls, skb, 0, group, flags); | |
251 | } | |
252 | EXPORT_SYMBOL(ibnl_multicast); | |
253 | ||
c9901724 | 254 | int __init rdma_nl_init(void) |
b2cbae2c | 255 | { |
a31f2d17 PNA |
256 | struct netlink_kernel_cfg cfg = { |
257 | .input = ibnl_rcv, | |
258 | }; | |
259 | ||
9f00d977 | 260 | nls = netlink_kernel_create(&init_net, NETLINK_RDMA, &cfg); |
c9901724 | 261 | if (!nls) |
b2cbae2c | 262 | return -ENOMEM; |
b2cbae2c | 263 | |
cea05ead | 264 | nls->sk_sndtimeo = 10 * HZ; |
b2cbae2c RD |
265 | return 0; |
266 | } | |
267 | ||
c9901724 | 268 | void rdma_nl_exit(void) |
b2cbae2c | 269 | { |
c9901724 | 270 | int idx; |
b2cbae2c | 271 | |
c9901724 LR |
272 | for (idx = 0; idx < RDMA_NL_NUM_CLIENTS; idx++) |
273 | rdma_nl_unregister(idx); | |
b2cbae2c RD |
274 | |
275 | netlink_kernel_release(nls); | |
276 | } |