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