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>
36 #include <linux/gfp.h>
38 #include <linux/poll.h>
43 char *rds_str_array(char **array, size_t elements, size_t index)
45 if ((index < elements) && array[index])
50 EXPORT_SYMBOL(rds_str_array);
52 /* this is just used for stats gathering :/ */
53 static DEFINE_SPINLOCK(rds_sock_lock);
54 static unsigned long rds_sock_count;
55 static LIST_HEAD(rds_sock_list);
56 DECLARE_WAIT_QUEUE_HEAD(rds_poll_waitq);
59 * This is called as the final descriptor referencing this socket is closed.
60 * We have to unbind the socket so that another socket can be bound to the
61 * address it was using.
63 * We have to be careful about racing with the incoming path. sock_orphan()
64 * sets SOCK_DEAD and we use that as an indicator to the rx path that new
65 * messages shouldn't be queued.
67 static int rds_release(struct socket *sock)
69 struct sock *sk = sock->sk;
75 rs = rds_sk_to_rs(sk);
78 /* Note - rds_clear_recv_queue grabs rs_recv_lock, so
79 * that ensures the recv path has completed messing
81 rds_clear_recv_queue(rs);
82 rds_cong_remove_socket(rs);
85 * the binding lookup hash uses rcu, we need to
86 * make sure we sychronize_rcu before we free our
92 rds_send_drop_to(rs, NULL);
93 rds_rdma_drop_keys(rs);
94 rds_notify_queue_get(rs, NULL);
96 spin_lock_bh(&rds_sock_lock);
97 list_del_init(&rs->rs_item);
99 spin_unlock_bh(&rds_sock_lock);
101 rds_trans_put(rs->rs_transport);
110 * Careful not to race with rds_release -> sock_orphan which clears sk_sleep.
111 * _bh() isn't OK here, we're called from interrupt handlers. It's probably OK
112 * to wake the waitqueue after sk_sleep is clear as we hold a sock ref, but
113 * this seems more conservative.
114 * NB - normally, one would use sk_callback_lock for this, but we can
115 * get here from interrupts, whereas the network code grabs sk_callback_lock
116 * with _lock_bh only - so relying on sk_callback_lock introduces livelocks.
118 void rds_wake_sk_sleep(struct rds_sock *rs)
122 read_lock_irqsave(&rs->rs_recv_lock, flags);
123 __rds_wake_sk_sleep(rds_rs_to_sk(rs));
124 read_unlock_irqrestore(&rs->rs_recv_lock, flags);
127 static int rds_getname(struct socket *sock, struct sockaddr *uaddr,
128 int *uaddr_len, int peer)
130 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
131 struct rds_sock *rs = rds_sk_to_rs(sock->sk);
133 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
135 /* racey, don't care */
137 if (!rs->rs_conn_addr)
140 sin->sin_port = rs->rs_conn_port;
141 sin->sin_addr.s_addr = rs->rs_conn_addr;
143 sin->sin_port = rs->rs_bound_port;
144 sin->sin_addr.s_addr = rs->rs_bound_addr;
147 sin->sin_family = AF_INET;
149 *uaddr_len = sizeof(*sin);
154 * RDS' poll is without a doubt the least intuitive part of the interface,
155 * as POLLIN and POLLOUT do not behave entirely as you would expect from
156 * a network protocol.
158 * POLLIN is asserted if
159 * - there is data on the receive queue.
160 * - to signal that a previously congested destination may have become
162 * - A notification has been queued to the socket (this can be a congestion
163 * update, or a RDMA completion).
165 * POLLOUT is asserted if there is room on the send queue. This does not mean
166 * however, that the next sendmsg() call will succeed. If the application tries
167 * to send to a congested destination, the system call may still fail (and
170 static unsigned int rds_poll(struct file *file, struct socket *sock,
173 struct sock *sk = sock->sk;
174 struct rds_sock *rs = rds_sk_to_rs(sk);
175 unsigned int mask = 0;
178 poll_wait(file, sk_sleep(sk), wait);
180 if (rs->rs_seen_congestion)
181 poll_wait(file, &rds_poll_waitq, wait);
183 read_lock_irqsave(&rs->rs_recv_lock, flags);
184 if (!rs->rs_cong_monitor) {
185 /* When a congestion map was updated, we signal POLLIN for
186 * "historical" reasons. Applications can also poll for
188 if (rds_cong_updated_since(&rs->rs_cong_track))
189 mask |= (POLLIN | POLLRDNORM | POLLWRBAND);
191 spin_lock(&rs->rs_lock);
192 if (rs->rs_cong_notify)
193 mask |= (POLLIN | POLLRDNORM);
194 spin_unlock(&rs->rs_lock);
196 if (!list_empty(&rs->rs_recv_queue) ||
197 !list_empty(&rs->rs_notify_queue))
198 mask |= (POLLIN | POLLRDNORM);
199 if (rs->rs_snd_bytes < rds_sk_sndbuf(rs))
200 mask |= (POLLOUT | POLLWRNORM);
201 read_unlock_irqrestore(&rs->rs_recv_lock, flags);
203 /* clear state any time we wake a seen-congested socket */
205 rs->rs_seen_congestion = 0;
210 static int rds_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
215 static int rds_cancel_sent_to(struct rds_sock *rs, char __user *optval,
218 struct sockaddr_in sin;
221 /* racing with another thread binding seems ok here */
222 if (rs->rs_bound_addr == 0) {
223 ret = -ENOTCONN; /* XXX not a great errno */
227 if (len < sizeof(struct sockaddr_in)) {
232 if (copy_from_user(&sin, optval, sizeof(sin))) {
237 rds_send_drop_to(rs, &sin);
242 static int rds_set_bool_option(unsigned char *optvar, char __user *optval,
247 if (optlen < sizeof(int))
249 if (get_user(value, (int __user *) optval))
255 static int rds_cong_monitor(struct rds_sock *rs, char __user *optval,
260 ret = rds_set_bool_option(&rs->rs_cong_monitor, optval, optlen);
262 if (rs->rs_cong_monitor) {
263 rds_cong_add_socket(rs);
265 rds_cong_remove_socket(rs);
266 rs->rs_cong_mask = 0;
267 rs->rs_cong_notify = 0;
273 static int rds_setsockopt(struct socket *sock, int level, int optname,
274 char __user *optval, unsigned int optlen)
276 struct rds_sock *rs = rds_sk_to_rs(sock->sk);
279 if (level != SOL_RDS) {
285 case RDS_CANCEL_SENT_TO:
286 ret = rds_cancel_sent_to(rs, optval, optlen);
289 ret = rds_get_mr(rs, optval, optlen);
291 case RDS_GET_MR_FOR_DEST:
292 ret = rds_get_mr_for_dest(rs, optval, optlen);
295 ret = rds_free_mr(rs, optval, optlen);
298 ret = rds_set_bool_option(&rs->rs_recverr, optval, optlen);
300 case RDS_CONG_MONITOR:
301 ret = rds_cong_monitor(rs, optval, optlen);
310 static int rds_getsockopt(struct socket *sock, int level, int optname,
311 char __user *optval, int __user *optlen)
313 struct rds_sock *rs = rds_sk_to_rs(sock->sk);
314 int ret = -ENOPROTOOPT, len;
316 if (level != SOL_RDS)
319 if (get_user(len, optlen)) {
325 case RDS_INFO_FIRST ... RDS_INFO_LAST:
326 ret = rds_info_getsockopt(sock, optname, optval,
331 if (len < sizeof(int))
334 if (put_user(rs->rs_recverr, (int __user *) optval) ||
335 put_user(sizeof(int), optlen))
349 static int rds_connect(struct socket *sock, struct sockaddr *uaddr,
350 int addr_len, int flags)
352 struct sock *sk = sock->sk;
353 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
354 struct rds_sock *rs = rds_sk_to_rs(sk);
359 if (addr_len != sizeof(struct sockaddr_in)) {
364 if (sin->sin_family != AF_INET) {
369 if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) {
374 rs->rs_conn_addr = sin->sin_addr.s_addr;
375 rs->rs_conn_port = sin->sin_port;
382 static struct proto rds_proto = {
384 .owner = THIS_MODULE,
385 .obj_size = sizeof(struct rds_sock),
388 static const struct proto_ops rds_proto_ops = {
390 .owner = THIS_MODULE,
391 .release = rds_release,
393 .connect = rds_connect,
394 .socketpair = sock_no_socketpair,
395 .accept = sock_no_accept,
396 .getname = rds_getname,
399 .listen = sock_no_listen,
400 .shutdown = sock_no_shutdown,
401 .setsockopt = rds_setsockopt,
402 .getsockopt = rds_getsockopt,
403 .sendmsg = rds_sendmsg,
404 .recvmsg = rds_recvmsg,
405 .mmap = sock_no_mmap,
406 .sendpage = sock_no_sendpage,
409 static int __rds_create(struct socket *sock, struct sock *sk, int protocol)
413 sock_init_data(sock, sk);
414 sock->ops = &rds_proto_ops;
415 sk->sk_protocol = protocol;
417 rs = rds_sk_to_rs(sk);
418 spin_lock_init(&rs->rs_lock);
419 rwlock_init(&rs->rs_recv_lock);
420 INIT_LIST_HEAD(&rs->rs_send_queue);
421 INIT_LIST_HEAD(&rs->rs_recv_queue);
422 INIT_LIST_HEAD(&rs->rs_notify_queue);
423 INIT_LIST_HEAD(&rs->rs_cong_list);
424 spin_lock_init(&rs->rs_rdma_lock);
425 rs->rs_rdma_keys = RB_ROOT;
427 spin_lock_bh(&rds_sock_lock);
428 list_add_tail(&rs->rs_item, &rds_sock_list);
430 spin_unlock_bh(&rds_sock_lock);
435 static int rds_create(struct net *net, struct socket *sock, int protocol,
440 if (sock->type != SOCK_SEQPACKET || protocol)
441 return -ESOCKTNOSUPPORT;
443 sk = sk_alloc(net, AF_RDS, GFP_ATOMIC, &rds_proto);
447 return __rds_create(sock, sk, protocol);
450 void rds_sock_addref(struct rds_sock *rs)
452 sock_hold(rds_rs_to_sk(rs));
455 void rds_sock_put(struct rds_sock *rs)
457 sock_put(rds_rs_to_sk(rs));
460 static const struct net_proto_family rds_family_ops = {
462 .create = rds_create,
463 .owner = THIS_MODULE,
466 static void rds_sock_inc_info(struct socket *sock, unsigned int len,
467 struct rds_info_iterator *iter,
468 struct rds_info_lengths *lens)
471 struct rds_incoming *inc;
472 unsigned int total = 0;
474 len /= sizeof(struct rds_info_message);
476 spin_lock_bh(&rds_sock_lock);
478 list_for_each_entry(rs, &rds_sock_list, rs_item) {
479 read_lock(&rs->rs_recv_lock);
481 /* XXX too lazy to maintain counts.. */
482 list_for_each_entry(inc, &rs->rs_recv_queue, i_item) {
485 rds_inc_info_copy(inc, iter, inc->i_saddr,
486 rs->rs_bound_addr, 1);
489 read_unlock(&rs->rs_recv_lock);
492 spin_unlock_bh(&rds_sock_lock);
495 lens->each = sizeof(struct rds_info_message);
498 static void rds_sock_info(struct socket *sock, unsigned int len,
499 struct rds_info_iterator *iter,
500 struct rds_info_lengths *lens)
502 struct rds_info_socket sinfo;
505 len /= sizeof(struct rds_info_socket);
507 spin_lock_bh(&rds_sock_lock);
509 if (len < rds_sock_count)
512 list_for_each_entry(rs, &rds_sock_list, rs_item) {
513 sinfo.sndbuf = rds_sk_sndbuf(rs);
514 sinfo.rcvbuf = rds_sk_rcvbuf(rs);
515 sinfo.bound_addr = rs->rs_bound_addr;
516 sinfo.connected_addr = rs->rs_conn_addr;
517 sinfo.bound_port = rs->rs_bound_port;
518 sinfo.connected_port = rs->rs_conn_port;
519 sinfo.inum = sock_i_ino(rds_rs_to_sk(rs));
521 rds_info_copy(iter, &sinfo, sizeof(sinfo));
525 lens->nr = rds_sock_count;
526 lens->each = sizeof(struct rds_info_socket);
528 spin_unlock_bh(&rds_sock_lock);
531 static void rds_exit(void)
533 sock_unregister(rds_family_ops.family);
534 proto_unregister(&rds_proto);
541 rds_info_deregister_func(RDS_INFO_SOCKETS, rds_sock_info);
542 rds_info_deregister_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
544 module_exit(rds_exit);
546 static int rds_init(void)
550 ret = rds_conn_init();
553 ret = rds_threads_init();
556 ret = rds_sysctl_init();
559 ret = rds_stats_init();
562 ret = proto_register(&rds_proto, 1);
565 ret = sock_register(&rds_family_ops);
569 rds_info_register_func(RDS_INFO_SOCKETS, rds_sock_info);
570 rds_info_register_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
575 proto_unregister(&rds_proto);
589 module_init(rds_init);
591 #define DRV_VERSION "4.0"
592 #define DRV_RELDATE "Feb 12, 2009"
594 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
595 MODULE_DESCRIPTION("RDS: Reliable Datagram Sockets"
596 " v" DRV_VERSION " (" DRV_RELDATE ")");
597 MODULE_VERSION(DRV_VERSION);
598 MODULE_LICENSE("Dual BSD/GPL");
599 MODULE_ALIAS_NETPROTO(PF_RDS);