2 * Copyright (c) 2006 Oracle. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/module.h>
34 #include <linux/errno.h>
35 #include <linux/kernel.h>
37 #include <linux/poll.h>
38 #include <linux/version.h>
43 #include "rdma_transport.h"
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);
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.
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.
60 static int rds_release(struct socket *sock)
62 struct sock *sk = sock->sk;
69 rs = rds_sk_to_rs(sk);
72 /* Note - rds_clear_recv_queue grabs rs_recv_lock, so
73 * that ensures the recv path has completed messing
75 rds_clear_recv_queue(rs);
76 rds_cong_remove_socket(rs);
78 rds_send_drop_to(rs, NULL);
79 rds_rdma_drop_keys(rs);
80 rds_notify_queue_get(rs, NULL);
82 spin_lock_irqsave(&rds_sock_lock, flags);
83 list_del_init(&rs->rs_item);
85 spin_unlock_irqrestore(&rds_sock_lock, flags);
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.
102 void rds_wake_sk_sleep(struct rds_sock *rs)
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);
111 static int rds_getname(struct socket *sock, struct sockaddr *uaddr,
112 int *uaddr_len, int peer)
114 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
115 struct rds_sock *rs = rds_sk_to_rs(sock->sk);
117 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
119 /* racey, don't care */
121 if (!rs->rs_conn_addr)
124 sin->sin_port = rs->rs_conn_port;
125 sin->sin_addr.s_addr = rs->rs_conn_addr;
127 sin->sin_port = rs->rs_bound_port;
128 sin->sin_addr.s_addr = rs->rs_bound_addr;
131 sin->sin_family = AF_INET;
133 *uaddr_len = sizeof(*sin);
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.
142 * POLLIN is asserted if
143 * - there is data on the receive queue.
144 * - to signal that a previously congested destination may have become
146 * - A notification has been queued to the socket (this can be a congestion
147 * update, or a RDMA completion).
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
154 static unsigned int rds_poll(struct file *file, struct socket *sock,
157 struct sock *sk = sock->sk;
158 struct rds_sock *rs = rds_sk_to_rs(sk);
159 unsigned int mask = 0;
162 poll_wait(file, sk->sk_sleep, wait);
164 poll_wait(file, &rds_poll_waitq, wait);
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
171 if (rds_cong_updated_since(&rs->rs_cong_track))
172 mask |= (POLLIN | POLLRDNORM | POLLWRBAND);
174 spin_lock(&rs->rs_lock);
175 if (rs->rs_cong_notify)
176 mask |= (POLLIN | POLLRDNORM);
177 spin_unlock(&rs->rs_lock);
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);
189 static int rds_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
194 static int rds_cancel_sent_to(struct rds_sock *rs, char __user *optval,
197 struct sockaddr_in sin;
200 /* racing with another thread binding seems ok here */
201 if (rs->rs_bound_addr == 0) {
202 ret = -ENOTCONN; /* XXX not a great errno */
206 if (len < sizeof(struct sockaddr_in)) {
211 if (copy_from_user(&sin, optval, sizeof(sin))) {
216 rds_send_drop_to(rs, &sin);
221 static int rds_set_bool_option(unsigned char *optvar, char __user *optval,
226 if (optlen < sizeof(int))
228 if (get_user(value, (int __user *) optval))
234 static int rds_cong_monitor(struct rds_sock *rs, char __user *optval,
239 ret = rds_set_bool_option(&rs->rs_cong_monitor, optval, optlen);
241 if (rs->rs_cong_monitor) {
242 rds_cong_add_socket(rs);
244 rds_cong_remove_socket(rs);
245 rs->rs_cong_mask = 0;
246 rs->rs_cong_notify = 0;
252 static int rds_setsockopt(struct socket *sock, int level, int optname,
253 char __user *optval, int optlen)
255 struct rds_sock *rs = rds_sk_to_rs(sock->sk);
258 if (level != SOL_RDS) {
264 case RDS_CANCEL_SENT_TO:
265 ret = rds_cancel_sent_to(rs, optval, optlen);
268 ret = rds_get_mr(rs, optval, optlen);
271 ret = rds_free_mr(rs, optval, optlen);
274 ret = rds_set_bool_option(&rs->rs_recverr, optval, optlen);
276 case RDS_CONG_MONITOR:
277 ret = rds_cong_monitor(rs, optval, optlen);
286 static int rds_getsockopt(struct socket *sock, int level, int optname,
287 char __user *optval, int __user *optlen)
289 struct rds_sock *rs = rds_sk_to_rs(sock->sk);
290 int ret = -ENOPROTOOPT, len;
292 if (level != SOL_RDS)
295 if (get_user(len, optlen)) {
301 case RDS_INFO_FIRST ... RDS_INFO_LAST:
302 ret = rds_info_getsockopt(sock, optname, optval,
307 if (len < sizeof(int))
310 if (put_user(rs->rs_recverr, (int __user *) optval)
311 || put_user(sizeof(int), optlen))
325 static int rds_connect(struct socket *sock, struct sockaddr *uaddr,
326 int addr_len, int flags)
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);
335 if (addr_len != sizeof(struct sockaddr_in)) {
340 if (sin->sin_family != AF_INET) {
345 if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) {
350 rs->rs_conn_addr = sin->sin_addr.s_addr;
351 rs->rs_conn_port = sin->sin_port;
358 static struct proto rds_proto = {
360 .owner = THIS_MODULE,
361 .obj_size = sizeof(struct rds_sock),
364 static struct proto_ops rds_proto_ops = {
366 .owner = THIS_MODULE,
367 .release = rds_release,
369 .connect = rds_connect,
370 .socketpair = sock_no_socketpair,
371 .accept = sock_no_accept,
372 .getname = rds_getname,
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,
385 static int __rds_create(struct socket *sock, struct sock *sk, int protocol)
390 sock_init_data(sock, sk);
391 sock->ops = &rds_proto_ops;
392 sk->sk_protocol = protocol;
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;
404 spin_lock_irqsave(&rds_sock_lock, flags);
405 list_add_tail(&rs->rs_item, &rds_sock_list);
407 spin_unlock_irqrestore(&rds_sock_lock, flags);
412 static int rds_create(struct net *net, struct socket *sock, int protocol)
416 if (sock->type != SOCK_SEQPACKET || protocol)
417 return -ESOCKTNOSUPPORT;
419 sk = sk_alloc(net, AF_RDS, GFP_ATOMIC, &rds_proto);
423 return __rds_create(sock, sk, protocol);
426 void rds_sock_addref(struct rds_sock *rs)
428 sock_hold(rds_rs_to_sk(rs));
431 void rds_sock_put(struct rds_sock *rs)
433 sock_put(rds_rs_to_sk(rs));
436 static struct net_proto_family rds_family_ops = {
438 .create = rds_create,
439 .owner = THIS_MODULE,
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)
448 struct rds_incoming *inc;
450 unsigned int total = 0;
452 len /= sizeof(struct rds_info_message);
454 spin_lock_irqsave(&rds_sock_lock, flags);
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);
460 /* XXX too lazy to maintain counts.. */
461 list_for_each_entry(inc, &rs->rs_recv_queue, i_item) {
464 rds_inc_info_copy(inc, iter, inc->i_saddr,
465 rs->rs_bound_addr, 1);
468 read_unlock(&rs->rs_recv_lock);
471 spin_unlock_irqrestore(&rds_sock_lock, flags);
474 lens->each = sizeof(struct rds_info_message);
477 static void rds_sock_info(struct socket *sock, unsigned int len,
478 struct rds_info_iterator *iter,
479 struct rds_info_lengths *lens)
481 struct rds_info_socket sinfo;
485 len /= sizeof(struct rds_info_socket);
487 spin_lock_irqsave(&rds_sock_lock, flags);
489 if (len < rds_sock_count)
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));
501 rds_info_copy(iter, &sinfo, sizeof(sinfo));
505 lens->nr = rds_sock_count;
506 lens->each = sizeof(struct rds_info_socket);
508 spin_unlock_irqrestore(&rds_sock_lock, flags);
511 static void __exit rds_exit(void)
514 sock_unregister(rds_family_ops.family);
515 proto_unregister(&rds_proto);
522 rds_info_deregister_func(RDS_INFO_SOCKETS, rds_sock_info);
523 rds_info_deregister_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
525 module_exit(rds_exit);
527 static int __init rds_init(void)
531 ret = rds_conn_init();
534 ret = rds_threads_init();
537 ret = rds_sysctl_init();
540 ret = rds_stats_init();
543 ret = proto_register(&rds_proto, 1);
546 ret = sock_register(&rds_family_ops);
550 rds_info_register_func(RDS_INFO_SOCKETS, rds_sock_info);
551 rds_info_register_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
553 /* ib/iwarp transports currently compiled-in */
554 ret = rds_rdma_init();
560 sock_unregister(rds_family_ops.family);
562 proto_unregister(&rds_proto);
576 module_init(rds_init);
578 #define DRV_VERSION "4.0"
579 #define DRV_RELDATE "Feb 12, 2009"
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);