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 /* this is just used for stats gathering :/ */
44 static DEFINE_SPINLOCK(rds_sock_lock);
45 static unsigned long rds_sock_count;
46 static LIST_HEAD(rds_sock_list);
47 DECLARE_WAIT_QUEUE_HEAD(rds_poll_waitq);
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.
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.
58 static int rds_release(struct socket *sock)
60 struct sock *sk = sock->sk;
66 rs = rds_sk_to_rs(sk);
69 /* Note - rds_clear_recv_queue grabs rs_recv_lock, so
70 * that ensures the recv path has completed messing
72 rds_clear_recv_queue(rs);
73 rds_cong_remove_socket(rs);
77 rds_send_drop_to(rs, NULL);
78 rds_rdma_drop_keys(rs);
79 rds_notify_queue_get(rs, NULL);
81 spin_lock_bh(&rds_sock_lock);
82 list_del_init(&rs->rs_item);
84 spin_unlock_bh(&rds_sock_lock);
86 rds_trans_put(rs->rs_transport);
95 * Careful not to race with rds_release -> sock_orphan which clears sk_sleep.
96 * _bh() isn't OK here, we're called from interrupt handlers. It's probably OK
97 * to wake the waitqueue after sk_sleep is clear as we hold a sock ref, but
98 * this seems more conservative.
99 * NB - normally, one would use sk_callback_lock for this, but we can
100 * get here from interrupts, whereas the network code grabs sk_callback_lock
101 * with _lock_bh only - so relying on sk_callback_lock introduces livelocks.
103 void rds_wake_sk_sleep(struct rds_sock *rs)
107 read_lock_irqsave(&rs->rs_recv_lock, flags);
108 __rds_wake_sk_sleep(rds_rs_to_sk(rs));
109 read_unlock_irqrestore(&rs->rs_recv_lock, flags);
112 static int rds_getname(struct socket *sock, struct sockaddr *uaddr,
113 int *uaddr_len, int peer)
115 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
116 struct rds_sock *rs = rds_sk_to_rs(sock->sk);
118 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
120 /* racey, don't care */
122 if (!rs->rs_conn_addr)
125 sin->sin_port = rs->rs_conn_port;
126 sin->sin_addr.s_addr = rs->rs_conn_addr;
128 sin->sin_port = rs->rs_bound_port;
129 sin->sin_addr.s_addr = rs->rs_bound_addr;
132 sin->sin_family = AF_INET;
134 *uaddr_len = sizeof(*sin);
139 * RDS' poll is without a doubt the least intuitive part of the interface,
140 * as EPOLLIN and EPOLLOUT do not behave entirely as you would expect from
141 * a network protocol.
143 * EPOLLIN is asserted if
144 * - there is data on the receive queue.
145 * - to signal that a previously congested destination may have become
147 * - A notification has been queued to the socket (this can be a congestion
148 * update, or a RDMA completion).
150 * EPOLLOUT is asserted if there is room on the send queue. This does not mean
151 * however, that the next sendmsg() call will succeed. If the application tries
152 * to send to a congested destination, the system call may still fail (and
155 static __poll_t rds_poll(struct file *file, struct socket *sock,
158 struct sock *sk = sock->sk;
159 struct rds_sock *rs = rds_sk_to_rs(sk);
163 poll_wait(file, sk_sleep(sk), wait);
165 if (rs->rs_seen_congestion)
166 poll_wait(file, &rds_poll_waitq, wait);
168 read_lock_irqsave(&rs->rs_recv_lock, flags);
169 if (!rs->rs_cong_monitor) {
170 /* When a congestion map was updated, we signal EPOLLIN for
171 * "historical" reasons. Applications can also poll for
173 if (rds_cong_updated_since(&rs->rs_cong_track))
174 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLWRBAND);
176 spin_lock(&rs->rs_lock);
177 if (rs->rs_cong_notify)
178 mask |= (EPOLLIN | EPOLLRDNORM);
179 spin_unlock(&rs->rs_lock);
181 if (!list_empty(&rs->rs_recv_queue) ||
182 !list_empty(&rs->rs_notify_queue))
183 mask |= (EPOLLIN | EPOLLRDNORM);
184 if (rs->rs_snd_bytes < rds_sk_sndbuf(rs))
185 mask |= (EPOLLOUT | EPOLLWRNORM);
186 read_unlock_irqrestore(&rs->rs_recv_lock, flags);
188 /* clear state any time we wake a seen-congested socket */
190 rs->rs_seen_congestion = 0;
195 static int rds_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
200 static int rds_cancel_sent_to(struct rds_sock *rs, char __user *optval,
203 struct sockaddr_in sin;
206 /* racing with another thread binding seems ok here */
207 if (rs->rs_bound_addr == 0) {
208 ret = -ENOTCONN; /* XXX not a great errno */
212 if (len < sizeof(struct sockaddr_in)) {
217 if (copy_from_user(&sin, optval, sizeof(sin))) {
222 rds_send_drop_to(rs, &sin);
227 static int rds_set_bool_option(unsigned char *optvar, char __user *optval,
232 if (optlen < sizeof(int))
234 if (get_user(value, (int __user *) optval))
240 static int rds_cong_monitor(struct rds_sock *rs, char __user *optval,
245 ret = rds_set_bool_option(&rs->rs_cong_monitor, optval, optlen);
247 if (rs->rs_cong_monitor) {
248 rds_cong_add_socket(rs);
250 rds_cong_remove_socket(rs);
251 rs->rs_cong_mask = 0;
252 rs->rs_cong_notify = 0;
258 static int rds_set_transport(struct rds_sock *rs, char __user *optval,
263 if (rs->rs_transport)
264 return -EOPNOTSUPP; /* previously attached to transport */
266 if (optlen != sizeof(int))
269 if (copy_from_user(&t_type, (int __user *)optval, sizeof(t_type)))
272 if (t_type < 0 || t_type >= RDS_TRANS_COUNT)
275 rs->rs_transport = rds_trans_get(t_type);
277 return rs->rs_transport ? 0 : -ENOPROTOOPT;
280 static int rds_enable_recvtstamp(struct sock *sk, char __user *optval,
285 if (optlen != sizeof(int))
288 if (get_user(val, (int __user *)optval))
291 valbool = val ? 1 : 0;
294 sock_set_flag(sk, SOCK_RCVTSTAMP);
296 sock_reset_flag(sk, SOCK_RCVTSTAMP);
301 static int rds_recv_track_latency(struct rds_sock *rs, char __user *optval,
304 struct rds_rx_trace_so trace;
307 if (optlen != sizeof(struct rds_rx_trace_so))
310 if (copy_from_user(&trace, optval, sizeof(trace)))
313 if (trace.rx_traces > RDS_MSG_RX_DGRAM_TRACE_MAX)
316 rs->rs_rx_traces = trace.rx_traces;
317 for (i = 0; i < rs->rs_rx_traces; i++) {
318 if (trace.rx_trace_pos[i] > RDS_MSG_RX_DGRAM_TRACE_MAX) {
319 rs->rs_rx_traces = 0;
322 rs->rs_rx_trace[i] = trace.rx_trace_pos[i];
328 static int rds_setsockopt(struct socket *sock, int level, int optname,
329 char __user *optval, unsigned int optlen)
331 struct rds_sock *rs = rds_sk_to_rs(sock->sk);
334 if (level != SOL_RDS) {
340 case RDS_CANCEL_SENT_TO:
341 ret = rds_cancel_sent_to(rs, optval, optlen);
344 ret = rds_get_mr(rs, optval, optlen);
346 case RDS_GET_MR_FOR_DEST:
347 ret = rds_get_mr_for_dest(rs, optval, optlen);
350 ret = rds_free_mr(rs, optval, optlen);
353 ret = rds_set_bool_option(&rs->rs_recverr, optval, optlen);
355 case RDS_CONG_MONITOR:
356 ret = rds_cong_monitor(rs, optval, optlen);
358 case SO_RDS_TRANSPORT:
360 ret = rds_set_transport(rs, optval, optlen);
361 release_sock(sock->sk);
365 ret = rds_enable_recvtstamp(sock->sk, optval, optlen);
366 release_sock(sock->sk);
368 case SO_RDS_MSG_RXPATH_LATENCY:
369 ret = rds_recv_track_latency(rs, optval, optlen);
378 static int rds_getsockopt(struct socket *sock, int level, int optname,
379 char __user *optval, int __user *optlen)
381 struct rds_sock *rs = rds_sk_to_rs(sock->sk);
382 int ret = -ENOPROTOOPT, len;
385 if (level != SOL_RDS)
388 if (get_user(len, optlen)) {
394 case RDS_INFO_FIRST ... RDS_INFO_LAST:
395 ret = rds_info_getsockopt(sock, optname, optval,
400 if (len < sizeof(int))
403 if (put_user(rs->rs_recverr, (int __user *) optval) ||
404 put_user(sizeof(int), optlen))
409 case SO_RDS_TRANSPORT:
410 if (len < sizeof(int)) {
414 trans = (rs->rs_transport ? rs->rs_transport->t_type :
415 RDS_TRANS_NONE); /* unbound */
416 if (put_user(trans, (int __user *)optval) ||
417 put_user(sizeof(int), optlen))
431 static int rds_connect(struct socket *sock, struct sockaddr *uaddr,
432 int addr_len, int flags)
434 struct sock *sk = sock->sk;
435 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
436 struct rds_sock *rs = rds_sk_to_rs(sk);
441 if (addr_len != sizeof(struct sockaddr_in)) {
446 if (sin->sin_family != AF_INET) {
451 if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) {
456 rs->rs_conn_addr = sin->sin_addr.s_addr;
457 rs->rs_conn_port = sin->sin_port;
464 static struct proto rds_proto = {
466 .owner = THIS_MODULE,
467 .obj_size = sizeof(struct rds_sock),
470 static const struct proto_ops rds_proto_ops = {
472 .owner = THIS_MODULE,
473 .release = rds_release,
475 .connect = rds_connect,
476 .socketpair = sock_no_socketpair,
477 .accept = sock_no_accept,
478 .getname = rds_getname,
481 .listen = sock_no_listen,
482 .shutdown = sock_no_shutdown,
483 .setsockopt = rds_setsockopt,
484 .getsockopt = rds_getsockopt,
485 .sendmsg = rds_sendmsg,
486 .recvmsg = rds_recvmsg,
487 .mmap = sock_no_mmap,
488 .sendpage = sock_no_sendpage,
491 static void rds_sock_destruct(struct sock *sk)
493 struct rds_sock *rs = rds_sk_to_rs(sk);
495 WARN_ON((&rs->rs_item != rs->rs_item.next ||
496 &rs->rs_item != rs->rs_item.prev));
499 static int __rds_create(struct socket *sock, struct sock *sk, int protocol)
503 sock_init_data(sock, sk);
504 sock->ops = &rds_proto_ops;
505 sk->sk_protocol = protocol;
506 sk->sk_destruct = rds_sock_destruct;
508 rs = rds_sk_to_rs(sk);
509 spin_lock_init(&rs->rs_lock);
510 rwlock_init(&rs->rs_recv_lock);
511 INIT_LIST_HEAD(&rs->rs_send_queue);
512 INIT_LIST_HEAD(&rs->rs_recv_queue);
513 INIT_LIST_HEAD(&rs->rs_notify_queue);
514 INIT_LIST_HEAD(&rs->rs_cong_list);
515 spin_lock_init(&rs->rs_rdma_lock);
516 rs->rs_rdma_keys = RB_ROOT;
517 rs->rs_rx_traces = 0;
519 spin_lock_bh(&rds_sock_lock);
520 list_add_tail(&rs->rs_item, &rds_sock_list);
522 spin_unlock_bh(&rds_sock_lock);
527 static int rds_create(struct net *net, struct socket *sock, int protocol,
532 if (sock->type != SOCK_SEQPACKET || protocol)
533 return -ESOCKTNOSUPPORT;
535 sk = sk_alloc(net, AF_RDS, GFP_ATOMIC, &rds_proto, kern);
539 return __rds_create(sock, sk, protocol);
542 void rds_sock_addref(struct rds_sock *rs)
544 sock_hold(rds_rs_to_sk(rs));
547 void rds_sock_put(struct rds_sock *rs)
549 sock_put(rds_rs_to_sk(rs));
552 static const struct net_proto_family rds_family_ops = {
554 .create = rds_create,
555 .owner = THIS_MODULE,
558 static void rds_sock_inc_info(struct socket *sock, unsigned int len,
559 struct rds_info_iterator *iter,
560 struct rds_info_lengths *lens)
563 struct rds_incoming *inc;
564 unsigned int total = 0;
566 len /= sizeof(struct rds_info_message);
568 spin_lock_bh(&rds_sock_lock);
570 list_for_each_entry(rs, &rds_sock_list, rs_item) {
571 read_lock(&rs->rs_recv_lock);
573 /* XXX too lazy to maintain counts.. */
574 list_for_each_entry(inc, &rs->rs_recv_queue, i_item) {
577 rds_inc_info_copy(inc, iter, inc->i_saddr,
578 rs->rs_bound_addr, 1);
581 read_unlock(&rs->rs_recv_lock);
584 spin_unlock_bh(&rds_sock_lock);
587 lens->each = sizeof(struct rds_info_message);
590 static void rds_sock_info(struct socket *sock, unsigned int len,
591 struct rds_info_iterator *iter,
592 struct rds_info_lengths *lens)
594 struct rds_info_socket sinfo;
597 len /= sizeof(struct rds_info_socket);
599 spin_lock_bh(&rds_sock_lock);
601 if (len < rds_sock_count)
604 list_for_each_entry(rs, &rds_sock_list, rs_item) {
605 sinfo.sndbuf = rds_sk_sndbuf(rs);
606 sinfo.rcvbuf = rds_sk_rcvbuf(rs);
607 sinfo.bound_addr = rs->rs_bound_addr;
608 sinfo.connected_addr = rs->rs_conn_addr;
609 sinfo.bound_port = rs->rs_bound_port;
610 sinfo.connected_port = rs->rs_conn_port;
611 sinfo.inum = sock_i_ino(rds_rs_to_sk(rs));
613 rds_info_copy(iter, &sinfo, sizeof(sinfo));
617 lens->nr = rds_sock_count;
618 lens->each = sizeof(struct rds_info_socket);
620 spin_unlock_bh(&rds_sock_lock);
623 static void rds_exit(void)
625 sock_unregister(rds_family_ops.family);
626 proto_unregister(&rds_proto);
633 rds_bind_lock_destroy();
634 rds_info_deregister_func(RDS_INFO_SOCKETS, rds_sock_info);
635 rds_info_deregister_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
637 module_exit(rds_exit);
641 static int rds_init(void)
645 net_get_random_once(&rds_gen_num, sizeof(rds_gen_num));
647 ret = rds_bind_lock_init();
651 ret = rds_conn_init();
655 ret = rds_threads_init();
658 ret = rds_sysctl_init();
661 ret = rds_stats_init();
664 ret = proto_register(&rds_proto, 1);
667 ret = sock_register(&rds_family_ops);
671 rds_info_register_func(RDS_INFO_SOCKETS, rds_sock_info);
672 rds_info_register_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
677 proto_unregister(&rds_proto);
689 rds_bind_lock_destroy();
693 module_init(rds_init);
695 #define DRV_VERSION "4.0"
696 #define DRV_RELDATE "Feb 12, 2009"
698 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
699 MODULE_DESCRIPTION("RDS: Reliable Datagram Sockets"
700 " v" DRV_VERSION " (" DRV_RELDATE ")");
701 MODULE_VERSION(DRV_VERSION);
702 MODULE_LICENSE("Dual BSD/GPL");
703 MODULE_ALIAS_NETPROTO(PF_RDS);