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