diff options
author | Andy Grover <andy.grover@oracle.com> | 2009-02-24 10:30:18 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2009-02-27 02:39:23 -0500 |
commit | 639b321b4d8f4e412bfbb2a4a19bfebc1e68ace4 (patch) | |
tree | 2de81f536b54c7890295cbc507e6c2a3e57bc984 /net/rds | |
parent | b775a750c3afacbfac884537d466d34d50b1023b (diff) |
RDS: Socket interface
Implement the RDS (Reliable Datagram Sockets) interface.
Signed-off-by: Andy Grover <andy.grover@oracle.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/rds')
-rw-r--r-- | net/rds/af_rds.c | 586 | ||||
-rw-r--r-- | net/rds/bind.c | 199 |
2 files changed, 785 insertions, 0 deletions
diff --git a/net/rds/af_rds.c b/net/rds/af_rds.c new file mode 100644 index 000000000000..20cf16fc572f --- /dev/null +++ b/net/rds/af_rds.c | |||
@@ -0,0 +1,586 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2006 Oracle. 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 | #include <linux/module.h> | ||
34 | #include <linux/errno.h> | ||
35 | #include <linux/kernel.h> | ||
36 | #include <linux/in.h> | ||
37 | #include <linux/poll.h> | ||
38 | #include <linux/version.h> | ||
39 | #include <net/sock.h> | ||
40 | |||
41 | #include "rds.h" | ||
42 | #include "rdma.h" | ||
43 | #include "rdma_transport.h" | ||
44 | |||
45 | /* this is just used for stats gathering :/ */ | ||
46 | static DEFINE_SPINLOCK(rds_sock_lock); | ||
47 | static unsigned long rds_sock_count; | ||
48 | static LIST_HEAD(rds_sock_list); | ||
49 | DECLARE_WAIT_QUEUE_HEAD(rds_poll_waitq); | ||
50 | |||
51 | /* | ||
52 | * This is called as the final descriptor referencing this socket is closed. | ||
53 | * We have to unbind the socket so that another socket can be bound to the | ||
54 | * address it was using. | ||
55 | * | ||
56 | * We have to be careful about racing with the incoming path. sock_orphan() | ||
57 | * sets SOCK_DEAD and we use that as an indicator to the rx path that new | ||
58 | * messages shouldn't be queued. | ||
59 | */ | ||
60 | static int rds_release(struct socket *sock) | ||
61 | { | ||
62 | struct sock *sk = sock->sk; | ||
63 | struct rds_sock *rs; | ||
64 | unsigned long flags; | ||
65 | |||
66 | if (sk == NULL) | ||
67 | goto out; | ||
68 | |||
69 | rs = rds_sk_to_rs(sk); | ||
70 | |||
71 | sock_orphan(sk); | ||
72 | /* Note - rds_clear_recv_queue grabs rs_recv_lock, so | ||
73 | * that ensures the recv path has completed messing | ||
74 | * with the socket. */ | ||
75 | rds_clear_recv_queue(rs); | ||
76 | rds_cong_remove_socket(rs); | ||
77 | rds_remove_bound(rs); | ||
78 | rds_send_drop_to(rs, NULL); | ||
79 | rds_rdma_drop_keys(rs); | ||
80 | rds_notify_queue_get(rs, NULL); | ||
81 | |||
82 | spin_lock_irqsave(&rds_sock_lock, flags); | ||
83 | list_del_init(&rs->rs_item); | ||
84 | rds_sock_count--; | ||
85 | spin_unlock_irqrestore(&rds_sock_lock, flags); | ||
86 | |||
87 | sock->sk = NULL; | ||
88 | sock_put(sk); | ||
89 | out: | ||
90 | return 0; | ||
91 | } | ||
92 | |||
93 | /* | ||
94 | * Careful not to race with rds_release -> sock_orphan which clears sk_sleep. | ||
95 | * _bh() isn't OK here, we're called from interrupt handlers. It's probably OK | ||
96 | * to wake the waitqueue after sk_sleep is clear as we hold a sock ref, but | ||
97 | * this seems more conservative. | ||
98 | * NB - normally, one would use sk_callback_lock for this, but we can | ||
99 | * get here from interrupts, whereas the network code grabs sk_callback_lock | ||
100 | * with _lock_bh only - so relying on sk_callback_lock introduces livelocks. | ||
101 | */ | ||
102 | void rds_wake_sk_sleep(struct rds_sock *rs) | ||
103 | { | ||
104 | unsigned long flags; | ||
105 | |||
106 | read_lock_irqsave(&rs->rs_recv_lock, flags); | ||
107 | __rds_wake_sk_sleep(rds_rs_to_sk(rs)); | ||
108 | read_unlock_irqrestore(&rs->rs_recv_lock, flags); | ||
109 | } | ||
110 | |||
111 | static int rds_getname(struct socket *sock, struct sockaddr *uaddr, | ||
112 | int *uaddr_len, int peer) | ||
113 | { | ||
114 | struct sockaddr_in *sin = (struct sockaddr_in *)uaddr; | ||
115 | struct rds_sock *rs = rds_sk_to_rs(sock->sk); | ||
116 | |||
117 | memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); | ||
118 | |||
119 | /* racey, don't care */ | ||
120 | if (peer) { | ||
121 | if (!rs->rs_conn_addr) | ||
122 | return -ENOTCONN; | ||
123 | |||
124 | sin->sin_port = rs->rs_conn_port; | ||
125 | sin->sin_addr.s_addr = rs->rs_conn_addr; | ||
126 | } else { | ||
127 | sin->sin_port = rs->rs_bound_port; | ||
128 | sin->sin_addr.s_addr = rs->rs_bound_addr; | ||
129 | } | ||
130 | |||
131 | sin->sin_family = AF_INET; | ||
132 | |||
133 | *uaddr_len = sizeof(*sin); | ||
134 | return 0; | ||
135 | } | ||
136 | |||
137 | /* | ||
138 | * RDS' poll is without a doubt the least intuitive part of the interface, | ||
139 | * as POLLIN and POLLOUT do not behave entirely as you would expect from | ||
140 | * a network protocol. | ||
141 | * | ||
142 | * POLLIN is asserted if | ||
143 | * - there is data on the receive queue. | ||
144 | * - to signal that a previously congested destination may have become | ||
145 | * uncongested | ||
146 | * - A notification has been queued to the socket (this can be a congestion | ||
147 | * update, or a RDMA completion). | ||
148 | * | ||
149 | * POLLOUT is asserted if there is room on the send queue. This does not mean | ||
150 | * however, that the next sendmsg() call will succeed. If the application tries | ||
151 | * to send to a congested destination, the system call may still fail (and | ||
152 | * return ENOBUFS). | ||
153 | */ | ||
154 | static unsigned int rds_poll(struct file *file, struct socket *sock, | ||
155 | poll_table *wait) | ||
156 | { | ||
157 | struct sock *sk = sock->sk; | ||
158 | struct rds_sock *rs = rds_sk_to_rs(sk); | ||
159 | unsigned int mask = 0; | ||
160 | unsigned long flags; | ||
161 | |||
162 | poll_wait(file, sk->sk_sleep, wait); | ||
163 | |||
164 | poll_wait(file, &rds_poll_waitq, wait); | ||
165 | |||
166 | read_lock_irqsave(&rs->rs_recv_lock, flags); | ||
167 | if (!rs->rs_cong_monitor) { | ||
168 | /* When a congestion map was updated, we signal POLLIN for | ||
169 | * "historical" reasons. Applications can also poll for | ||
170 | * WRBAND instead. */ | ||
171 | if (rds_cong_updated_since(&rs->rs_cong_track)) | ||
172 | mask |= (POLLIN | POLLRDNORM | POLLWRBAND); | ||
173 | } else { | ||
174 | spin_lock(&rs->rs_lock); | ||
175 | if (rs->rs_cong_notify) | ||
176 | mask |= (POLLIN | POLLRDNORM); | ||
177 | spin_unlock(&rs->rs_lock); | ||
178 | } | ||
179 | if (!list_empty(&rs->rs_recv_queue) | ||
180 | || !list_empty(&rs->rs_notify_queue)) | ||
181 | mask |= (POLLIN | POLLRDNORM); | ||
182 | if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) | ||
183 | mask |= (POLLOUT | POLLWRNORM); | ||
184 | read_unlock_irqrestore(&rs->rs_recv_lock, flags); | ||
185 | |||
186 | return mask; | ||
187 | } | ||
188 | |||
189 | static int rds_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) | ||
190 | { | ||
191 | return -ENOIOCTLCMD; | ||
192 | } | ||
193 | |||
194 | static int rds_cancel_sent_to(struct rds_sock *rs, char __user *optval, | ||
195 | int len) | ||
196 | { | ||
197 | struct sockaddr_in sin; | ||
198 | int ret = 0; | ||
199 | |||
200 | /* racing with another thread binding seems ok here */ | ||
201 | if (rs->rs_bound_addr == 0) { | ||
202 | ret = -ENOTCONN; /* XXX not a great errno */ | ||
203 | goto out; | ||
204 | } | ||
205 | |||
206 | if (len < sizeof(struct sockaddr_in)) { | ||
207 | ret = -EINVAL; | ||
208 | goto out; | ||
209 | } | ||
210 | |||
211 | if (copy_from_user(&sin, optval, sizeof(sin))) { | ||
212 | ret = -EFAULT; | ||
213 | goto out; | ||
214 | } | ||
215 | |||
216 | rds_send_drop_to(rs, &sin); | ||
217 | out: | ||
218 | return ret; | ||
219 | } | ||
220 | |||
221 | static int rds_set_bool_option(unsigned char *optvar, char __user *optval, | ||
222 | int optlen) | ||
223 | { | ||
224 | int value; | ||
225 | |||
226 | if (optlen < sizeof(int)) | ||
227 | return -EINVAL; | ||
228 | if (get_user(value, (int __user *) optval)) | ||
229 | return -EFAULT; | ||
230 | *optvar = !!value; | ||
231 | return 0; | ||
232 | } | ||
233 | |||
234 | static int rds_cong_monitor(struct rds_sock *rs, char __user *optval, | ||
235 | int optlen) | ||
236 | { | ||
237 | int ret; | ||
238 | |||
239 | ret = rds_set_bool_option(&rs->rs_cong_monitor, optval, optlen); | ||
240 | if (ret == 0) { | ||
241 | if (rs->rs_cong_monitor) { | ||
242 | rds_cong_add_socket(rs); | ||
243 | } else { | ||
244 | rds_cong_remove_socket(rs); | ||
245 | rs->rs_cong_mask = 0; | ||
246 | rs->rs_cong_notify = 0; | ||
247 | } | ||
248 | } | ||
249 | return ret; | ||
250 | } | ||
251 | |||
252 | static int rds_setsockopt(struct socket *sock, int level, int optname, | ||
253 | char __user *optval, int optlen) | ||
254 | { | ||
255 | struct rds_sock *rs = rds_sk_to_rs(sock->sk); | ||
256 | int ret; | ||
257 | |||
258 | if (level != SOL_RDS) { | ||
259 | ret = -ENOPROTOOPT; | ||
260 | goto out; | ||
261 | } | ||
262 | |||
263 | switch (optname) { | ||
264 | case RDS_CANCEL_SENT_TO: | ||
265 | ret = rds_cancel_sent_to(rs, optval, optlen); | ||
266 | break; | ||
267 | case RDS_GET_MR: | ||
268 | ret = rds_get_mr(rs, optval, optlen); | ||
269 | break; | ||
270 | case RDS_FREE_MR: | ||
271 | ret = rds_free_mr(rs, optval, optlen); | ||
272 | break; | ||
273 | case RDS_RECVERR: | ||
274 | ret = rds_set_bool_option(&rs->rs_recverr, optval, optlen); | ||
275 | break; | ||
276 | case RDS_CONG_MONITOR: | ||
277 | ret = rds_cong_monitor(rs, optval, optlen); | ||
278 | break; | ||
279 | default: | ||
280 | ret = -ENOPROTOOPT; | ||
281 | } | ||
282 | out: | ||
283 | return ret; | ||
284 | } | ||
285 | |||
286 | static int rds_getsockopt(struct socket *sock, int level, int optname, | ||
287 | char __user *optval, int __user *optlen) | ||
288 | { | ||
289 | struct rds_sock *rs = rds_sk_to_rs(sock->sk); | ||
290 | int ret = -ENOPROTOOPT, len; | ||
291 | |||
292 | if (level != SOL_RDS) | ||
293 | goto out; | ||
294 | |||
295 | if (get_user(len, optlen)) { | ||
296 | ret = -EFAULT; | ||
297 | goto out; | ||
298 | } | ||
299 | |||
300 | switch (optname) { | ||
301 | case RDS_INFO_FIRST ... RDS_INFO_LAST: | ||
302 | ret = rds_info_getsockopt(sock, optname, optval, | ||
303 | optlen); | ||
304 | break; | ||
305 | |||
306 | case RDS_RECVERR: | ||
307 | if (len < sizeof(int)) | ||
308 | ret = -EINVAL; | ||
309 | else | ||
310 | if (put_user(rs->rs_recverr, (int __user *) optval) | ||
311 | || put_user(sizeof(int), optlen)) | ||
312 | ret = -EFAULT; | ||
313 | else | ||
314 | ret = 0; | ||
315 | break; | ||
316 | default: | ||
317 | break; | ||
318 | } | ||
319 | |||
320 | out: | ||
321 | return ret; | ||
322 | |||
323 | } | ||
324 | |||
325 | static int rds_connect(struct socket *sock, struct sockaddr *uaddr, | ||
326 | int addr_len, int flags) | ||
327 | { | ||
328 | struct sock *sk = sock->sk; | ||
329 | struct sockaddr_in *sin = (struct sockaddr_in *)uaddr; | ||
330 | struct rds_sock *rs = rds_sk_to_rs(sk); | ||
331 | int ret = 0; | ||
332 | |||
333 | lock_sock(sk); | ||
334 | |||
335 | if (addr_len != sizeof(struct sockaddr_in)) { | ||
336 | ret = -EINVAL; | ||
337 | goto out; | ||
338 | } | ||
339 | |||
340 | if (sin->sin_family != AF_INET) { | ||
341 | ret = -EAFNOSUPPORT; | ||
342 | goto out; | ||
343 | } | ||
344 | |||
345 | if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) { | ||
346 | ret = -EDESTADDRREQ; | ||
347 | goto out; | ||
348 | } | ||
349 | |||
350 | rs->rs_conn_addr = sin->sin_addr.s_addr; | ||
351 | rs->rs_conn_port = sin->sin_port; | ||
352 | |||
353 | out: | ||
354 | release_sock(sk); | ||
355 | return ret; | ||
356 | } | ||
357 | |||
358 | static struct proto rds_proto = { | ||
359 | .name = "RDS", | ||
360 | .owner = THIS_MODULE, | ||
361 | .obj_size = sizeof(struct rds_sock), | ||
362 | }; | ||
363 | |||
364 | static struct proto_ops rds_proto_ops = { | ||
365 | .family = AF_RDS, | ||
366 | .owner = THIS_MODULE, | ||
367 | .release = rds_release, | ||
368 | .bind = rds_bind, | ||
369 | .connect = rds_connect, | ||
370 | .socketpair = sock_no_socketpair, | ||
371 | .accept = sock_no_accept, | ||
372 | .getname = rds_getname, | ||
373 | .poll = rds_poll, | ||
374 | .ioctl = rds_ioctl, | ||
375 | .listen = sock_no_listen, | ||
376 | .shutdown = sock_no_shutdown, | ||
377 | .setsockopt = rds_setsockopt, | ||
378 | .getsockopt = rds_getsockopt, | ||
379 | .sendmsg = rds_sendmsg, | ||
380 | .recvmsg = rds_recvmsg, | ||
381 | .mmap = sock_no_mmap, | ||
382 | .sendpage = sock_no_sendpage, | ||
383 | }; | ||
384 | |||
385 | static int __rds_create(struct socket *sock, struct sock *sk, int protocol) | ||
386 | { | ||
387 | unsigned long flags; | ||
388 | struct rds_sock *rs; | ||
389 | |||
390 | sock_init_data(sock, sk); | ||
391 | sock->ops = &rds_proto_ops; | ||
392 | sk->sk_protocol = protocol; | ||
393 | |||
394 | rs = rds_sk_to_rs(sk); | ||
395 | spin_lock_init(&rs->rs_lock); | ||
396 | rwlock_init(&rs->rs_recv_lock); | ||
397 | INIT_LIST_HEAD(&rs->rs_send_queue); | ||
398 | INIT_LIST_HEAD(&rs->rs_recv_queue); | ||
399 | INIT_LIST_HEAD(&rs->rs_notify_queue); | ||
400 | INIT_LIST_HEAD(&rs->rs_cong_list); | ||
401 | spin_lock_init(&rs->rs_rdma_lock); | ||
402 | rs->rs_rdma_keys = RB_ROOT; | ||
403 | |||
404 | spin_lock_irqsave(&rds_sock_lock, flags); | ||
405 | list_add_tail(&rs->rs_item, &rds_sock_list); | ||
406 | rds_sock_count++; | ||
407 | spin_unlock_irqrestore(&rds_sock_lock, flags); | ||
408 | |||
409 | return 0; | ||
410 | } | ||
411 | |||
412 | static int rds_create(struct net *net, struct socket *sock, int protocol) | ||
413 | { | ||
414 | struct sock *sk; | ||
415 | |||
416 | if (sock->type != SOCK_SEQPACKET || protocol) | ||
417 | return -ESOCKTNOSUPPORT; | ||
418 | |||
419 | sk = sk_alloc(net, AF_RDS, GFP_ATOMIC, &rds_proto); | ||
420 | if (!sk) | ||
421 | return -ENOMEM; | ||
422 | |||
423 | return __rds_create(sock, sk, protocol); | ||
424 | } | ||
425 | |||
426 | void rds_sock_addref(struct rds_sock *rs) | ||
427 | { | ||
428 | sock_hold(rds_rs_to_sk(rs)); | ||
429 | } | ||
430 | |||
431 | void rds_sock_put(struct rds_sock *rs) | ||
432 | { | ||
433 | sock_put(rds_rs_to_sk(rs)); | ||
434 | } | ||
435 | |||
436 | static struct net_proto_family rds_family_ops = { | ||
437 | .family = AF_RDS, | ||
438 | .create = rds_create, | ||
439 | .owner = THIS_MODULE, | ||
440 | }; | ||
441 | |||
442 | static void rds_sock_inc_info(struct socket *sock, unsigned int len, | ||
443 | struct rds_info_iterator *iter, | ||
444 | struct rds_info_lengths *lens) | ||
445 | { | ||
446 | struct rds_sock *rs; | ||
447 | struct sock *sk; | ||
448 | struct rds_incoming *inc; | ||
449 | unsigned long flags; | ||
450 | unsigned int total = 0; | ||
451 | |||
452 | len /= sizeof(struct rds_info_message); | ||
453 | |||
454 | spin_lock_irqsave(&rds_sock_lock, flags); | ||
455 | |||
456 | list_for_each_entry(rs, &rds_sock_list, rs_item) { | ||
457 | sk = rds_rs_to_sk(rs); | ||
458 | read_lock(&rs->rs_recv_lock); | ||
459 | |||
460 | /* XXX too lazy to maintain counts.. */ | ||
461 | list_for_each_entry(inc, &rs->rs_recv_queue, i_item) { | ||
462 | total++; | ||
463 | if (total <= len) | ||
464 | rds_inc_info_copy(inc, iter, inc->i_saddr, | ||
465 | rs->rs_bound_addr, 1); | ||
466 | } | ||
467 | |||
468 | read_unlock(&rs->rs_recv_lock); | ||
469 | } | ||
470 | |||
471 | spin_unlock_irqrestore(&rds_sock_lock, flags); | ||
472 | |||
473 | lens->nr = total; | ||
474 | lens->each = sizeof(struct rds_info_message); | ||
475 | } | ||
476 | |||
477 | static void rds_sock_info(struct socket *sock, unsigned int len, | ||
478 | struct rds_info_iterator *iter, | ||
479 | struct rds_info_lengths *lens) | ||
480 | { | ||
481 | struct rds_info_socket sinfo; | ||
482 | struct rds_sock *rs; | ||
483 | unsigned long flags; | ||
484 | |||
485 | len /= sizeof(struct rds_info_socket); | ||
486 | |||
487 | spin_lock_irqsave(&rds_sock_lock, flags); | ||
488 | |||
489 | if (len < rds_sock_count) | ||
490 | goto out; | ||
491 | |||
492 | list_for_each_entry(rs, &rds_sock_list, rs_item) { | ||
493 | sinfo.sndbuf = rds_sk_sndbuf(rs); | ||
494 | sinfo.rcvbuf = rds_sk_rcvbuf(rs); | ||
495 | sinfo.bound_addr = rs->rs_bound_addr; | ||
496 | sinfo.connected_addr = rs->rs_conn_addr; | ||
497 | sinfo.bound_port = rs->rs_bound_port; | ||
498 | sinfo.connected_port = rs->rs_conn_port; | ||
499 | sinfo.inum = sock_i_ino(rds_rs_to_sk(rs)); | ||
500 | |||
501 | rds_info_copy(iter, &sinfo, sizeof(sinfo)); | ||
502 | } | ||
503 | |||
504 | out: | ||
505 | lens->nr = rds_sock_count; | ||
506 | lens->each = sizeof(struct rds_info_socket); | ||
507 | |||
508 | spin_unlock_irqrestore(&rds_sock_lock, flags); | ||
509 | } | ||
510 | |||
511 | static void __exit rds_exit(void) | ||
512 | { | ||
513 | rds_rdma_exit(); | ||
514 | sock_unregister(rds_family_ops.family); | ||
515 | proto_unregister(&rds_proto); | ||
516 | rds_conn_exit(); | ||
517 | rds_cong_exit(); | ||
518 | rds_sysctl_exit(); | ||
519 | rds_threads_exit(); | ||
520 | rds_stats_exit(); | ||
521 | rds_page_exit(); | ||
522 | rds_info_deregister_func(RDS_INFO_SOCKETS, rds_sock_info); | ||
523 | rds_info_deregister_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info); | ||
524 | } | ||
525 | module_exit(rds_exit); | ||
526 | |||
527 | static int __init rds_init(void) | ||
528 | { | ||
529 | int ret; | ||
530 | |||
531 | ret = rds_conn_init(); | ||
532 | if (ret) | ||
533 | goto out; | ||
534 | ret = rds_threads_init(); | ||
535 | if (ret) | ||
536 | goto out_conn; | ||
537 | ret = rds_sysctl_init(); | ||
538 | if (ret) | ||
539 | goto out_threads; | ||
540 | ret = rds_stats_init(); | ||
541 | if (ret) | ||
542 | goto out_sysctl; | ||
543 | ret = proto_register(&rds_proto, 1); | ||
544 | if (ret) | ||
545 | goto out_stats; | ||
546 | ret = sock_register(&rds_family_ops); | ||
547 | if (ret) | ||
548 | goto out_proto; | ||
549 | |||
550 | rds_info_register_func(RDS_INFO_SOCKETS, rds_sock_info); | ||
551 | rds_info_register_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info); | ||
552 | |||
553 | /* ib/iwarp transports currently compiled-in */ | ||
554 | ret = rds_rdma_init(); | ||
555 | if (ret) | ||
556 | goto out_sock; | ||
557 | goto out; | ||
558 | |||
559 | out_sock: | ||
560 | sock_unregister(rds_family_ops.family); | ||
561 | out_proto: | ||
562 | proto_unregister(&rds_proto); | ||
563 | out_stats: | ||
564 | rds_stats_exit(); | ||
565 | out_sysctl: | ||
566 | rds_sysctl_exit(); | ||
567 | out_threads: | ||
568 | rds_threads_exit(); | ||
569 | out_conn: | ||
570 | rds_conn_exit(); | ||
571 | rds_cong_exit(); | ||
572 | rds_page_exit(); | ||
573 | out: | ||
574 | return ret; | ||
575 | } | ||
576 | module_init(rds_init); | ||
577 | |||
578 | #define DRV_VERSION "4.0" | ||
579 | #define DRV_RELDATE "Feb 12, 2009" | ||
580 | |||
581 | MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>"); | ||
582 | MODULE_DESCRIPTION("RDS: Reliable Datagram Sockets" | ||
583 | " v" DRV_VERSION " (" DRV_RELDATE ")"); | ||
584 | MODULE_VERSION(DRV_VERSION); | ||
585 | MODULE_LICENSE("Dual BSD/GPL"); | ||
586 | MODULE_ALIAS_NETPROTO(PF_RDS); | ||
diff --git a/net/rds/bind.c b/net/rds/bind.c new file mode 100644 index 000000000000..c17cc39160ce --- /dev/null +++ b/net/rds/bind.c | |||
@@ -0,0 +1,199 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2006 Oracle. 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 | #include <linux/kernel.h> | ||
34 | #include <net/sock.h> | ||
35 | #include <linux/in.h> | ||
36 | #include <linux/if_arp.h> | ||
37 | #include "rds.h" | ||
38 | |||
39 | /* | ||
40 | * XXX this probably still needs more work.. no INADDR_ANY, and rbtrees aren't | ||
41 | * particularly zippy. | ||
42 | * | ||
43 | * This is now called for every incoming frame so we arguably care much more | ||
44 | * about it than we used to. | ||
45 | */ | ||
46 | static DEFINE_SPINLOCK(rds_bind_lock); | ||
47 | static struct rb_root rds_bind_tree = RB_ROOT; | ||
48 | |||
49 | static struct rds_sock *rds_bind_tree_walk(__be32 addr, __be16 port, | ||
50 | struct rds_sock *insert) | ||
51 | { | ||
52 | struct rb_node **p = &rds_bind_tree.rb_node; | ||
53 | struct rb_node *parent = NULL; | ||
54 | struct rds_sock *rs; | ||
55 | u64 cmp; | ||
56 | u64 needle = ((u64)be32_to_cpu(addr) << 32) | be16_to_cpu(port); | ||
57 | |||
58 | while (*p) { | ||
59 | parent = *p; | ||
60 | rs = rb_entry(parent, struct rds_sock, rs_bound_node); | ||
61 | |||
62 | cmp = ((u64)be32_to_cpu(rs->rs_bound_addr) << 32) | | ||
63 | be16_to_cpu(rs->rs_bound_port); | ||
64 | |||
65 | if (needle < cmp) | ||
66 | p = &(*p)->rb_left; | ||
67 | else if (needle > cmp) | ||
68 | p = &(*p)->rb_right; | ||
69 | else | ||
70 | return rs; | ||
71 | } | ||
72 | |||
73 | if (insert) { | ||
74 | rb_link_node(&insert->rs_bound_node, parent, p); | ||
75 | rb_insert_color(&insert->rs_bound_node, &rds_bind_tree); | ||
76 | } | ||
77 | return NULL; | ||
78 | } | ||
79 | |||
80 | /* | ||
81 | * Return the rds_sock bound at the given local address. | ||
82 | * | ||
83 | * The rx path can race with rds_release. We notice if rds_release() has | ||
84 | * marked this socket and don't return a rs ref to the rx path. | ||
85 | */ | ||
86 | struct rds_sock *rds_find_bound(__be32 addr, __be16 port) | ||
87 | { | ||
88 | struct rds_sock *rs; | ||
89 | unsigned long flags; | ||
90 | |||
91 | spin_lock_irqsave(&rds_bind_lock, flags); | ||
92 | rs = rds_bind_tree_walk(addr, port, NULL); | ||
93 | if (rs && !sock_flag(rds_rs_to_sk(rs), SOCK_DEAD)) | ||
94 | rds_sock_addref(rs); | ||
95 | else | ||
96 | rs = NULL; | ||
97 | spin_unlock_irqrestore(&rds_bind_lock, flags); | ||
98 | |||
99 | rdsdebug("returning rs %p for %pI4:%u\n", rs, &addr, | ||
100 | ntohs(port)); | ||
101 | return rs; | ||
102 | } | ||
103 | |||
104 | /* returns -ve errno or +ve port */ | ||
105 | static int rds_add_bound(struct rds_sock *rs, __be32 addr, __be16 *port) | ||
106 | { | ||
107 | unsigned long flags; | ||
108 | int ret = -EADDRINUSE; | ||
109 | u16 rover, last; | ||
110 | |||
111 | if (*port != 0) { | ||
112 | rover = be16_to_cpu(*port); | ||
113 | last = rover; | ||
114 | } else { | ||
115 | rover = max_t(u16, net_random(), 2); | ||
116 | last = rover - 1; | ||
117 | } | ||
118 | |||
119 | spin_lock_irqsave(&rds_bind_lock, flags); | ||
120 | |||
121 | do { | ||
122 | if (rover == 0) | ||
123 | rover++; | ||
124 | if (rds_bind_tree_walk(addr, cpu_to_be16(rover), rs) == NULL) { | ||
125 | *port = cpu_to_be16(rover); | ||
126 | ret = 0; | ||
127 | break; | ||
128 | } | ||
129 | } while (rover++ != last); | ||
130 | |||
131 | if (ret == 0) { | ||
132 | rs->rs_bound_addr = addr; | ||
133 | rs->rs_bound_port = *port; | ||
134 | rds_sock_addref(rs); | ||
135 | |||
136 | rdsdebug("rs %p binding to %pI4:%d\n", | ||
137 | rs, &addr, (int)ntohs(*port)); | ||
138 | } | ||
139 | |||
140 | spin_unlock_irqrestore(&rds_bind_lock, flags); | ||
141 | |||
142 | return ret; | ||
143 | } | ||
144 | |||
145 | void rds_remove_bound(struct rds_sock *rs) | ||
146 | { | ||
147 | unsigned long flags; | ||
148 | |||
149 | spin_lock_irqsave(&rds_bind_lock, flags); | ||
150 | |||
151 | if (rs->rs_bound_addr) { | ||
152 | rdsdebug("rs %p unbinding from %pI4:%d\n", | ||
153 | rs, &rs->rs_bound_addr, | ||
154 | ntohs(rs->rs_bound_port)); | ||
155 | |||
156 | rb_erase(&rs->rs_bound_node, &rds_bind_tree); | ||
157 | rds_sock_put(rs); | ||
158 | rs->rs_bound_addr = 0; | ||
159 | } | ||
160 | |||
161 | spin_unlock_irqrestore(&rds_bind_lock, flags); | ||
162 | } | ||
163 | |||
164 | int rds_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) | ||
165 | { | ||
166 | struct sock *sk = sock->sk; | ||
167 | struct sockaddr_in *sin = (struct sockaddr_in *)uaddr; | ||
168 | struct rds_sock *rs = rds_sk_to_rs(sk); | ||
169 | struct rds_transport *trans; | ||
170 | int ret = 0; | ||
171 | |||
172 | lock_sock(sk); | ||
173 | |||
174 | if (addr_len != sizeof(struct sockaddr_in) || | ||
175 | sin->sin_family != AF_INET || | ||
176 | rs->rs_bound_addr || | ||
177 | sin->sin_addr.s_addr == htonl(INADDR_ANY)) { | ||
178 | ret = -EINVAL; | ||
179 | goto out; | ||
180 | } | ||
181 | |||
182 | ret = rds_add_bound(rs, sin->sin_addr.s_addr, &sin->sin_port); | ||
183 | if (ret) | ||
184 | goto out; | ||
185 | |||
186 | trans = rds_trans_get_preferred(sin->sin_addr.s_addr); | ||
187 | if (trans == NULL) { | ||
188 | ret = -EADDRNOTAVAIL; | ||
189 | rds_remove_bound(rs); | ||
190 | goto out; | ||
191 | } | ||
192 | |||
193 | rs->rs_transport = trans; | ||
194 | ret = 0; | ||
195 | |||
196 | out: | ||
197 | release_sock(sk); | ||
198 | return ret; | ||
199 | } | ||