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