net: Implement register_net_sysctl.
[linux-2.6/cjktty.git] / net / rds / af_rds.c
blob424ff622ab5f8e77dc69c7b54b98bdaafa350329
1 /*
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
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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
30 * SOFTWARE.
33 #include <linux/module.h>
34 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/gfp.h>
37 #include <linux/in.h>
38 #include <linux/poll.h>
39 #include <net/sock.h>
41 #include "rds.h"
43 char *rds_str_array(char **array, size_t elements, size_t index)
45 if ((index < elements) && array[index])
46 return array[index];
47 else
48 return "unknown";
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;
70 struct rds_sock *rs;
72 if (!sk)
73 goto out;
75 rs = rds_sk_to_rs(sk);
77 sock_orphan(sk);
78 /* Note - rds_clear_recv_queue grabs rs_recv_lock, so
79 * that ensures the recv path has completed messing
80 * with the socket. */
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
87 * entry
89 rds_remove_bound(rs);
90 synchronize_rcu();
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);
98 rds_sock_count--;
99 spin_unlock_bh(&rds_sock_lock);
101 rds_trans_put(rs->rs_transport);
103 sock->sk = NULL;
104 sock_put(sk);
105 out:
106 return 0;
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)
120 unsigned long flags;
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 */
136 if (peer) {
137 if (!rs->rs_conn_addr)
138 return -ENOTCONN;
140 sin->sin_port = rs->rs_conn_port;
141 sin->sin_addr.s_addr = rs->rs_conn_addr;
142 } else {
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);
150 return 0;
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
161 * uncongested
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
168 * return ENOBUFS).
170 static unsigned int rds_poll(struct file *file, struct socket *sock,
171 poll_table *wait)
173 struct sock *sk = sock->sk;
174 struct rds_sock *rs = rds_sk_to_rs(sk);
175 unsigned int mask = 0;
176 unsigned long flags;
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
187 * WRBAND instead. */
188 if (rds_cong_updated_since(&rs->rs_cong_track))
189 mask |= (POLLIN | POLLRDNORM | POLLWRBAND);
190 } else {
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 */
204 if (mask)
205 rs->rs_seen_congestion = 0;
207 return mask;
210 static int rds_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
212 return -ENOIOCTLCMD;
215 static int rds_cancel_sent_to(struct rds_sock *rs, char __user *optval,
216 int len)
218 struct sockaddr_in sin;
219 int ret = 0;
221 /* racing with another thread binding seems ok here */
222 if (rs->rs_bound_addr == 0) {
223 ret = -ENOTCONN; /* XXX not a great errno */
224 goto out;
227 if (len < sizeof(struct sockaddr_in)) {
228 ret = -EINVAL;
229 goto out;
232 if (copy_from_user(&sin, optval, sizeof(sin))) {
233 ret = -EFAULT;
234 goto out;
237 rds_send_drop_to(rs, &sin);
238 out:
239 return ret;
242 static int rds_set_bool_option(unsigned char *optvar, char __user *optval,
243 int optlen)
245 int value;
247 if (optlen < sizeof(int))
248 return -EINVAL;
249 if (get_user(value, (int __user *) optval))
250 return -EFAULT;
251 *optvar = !!value;
252 return 0;
255 static int rds_cong_monitor(struct rds_sock *rs, char __user *optval,
256 int optlen)
258 int ret;
260 ret = rds_set_bool_option(&rs->rs_cong_monitor, optval, optlen);
261 if (ret == 0) {
262 if (rs->rs_cong_monitor) {
263 rds_cong_add_socket(rs);
264 } else {
265 rds_cong_remove_socket(rs);
266 rs->rs_cong_mask = 0;
267 rs->rs_cong_notify = 0;
270 return ret;
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);
277 int ret;
279 if (level != SOL_RDS) {
280 ret = -ENOPROTOOPT;
281 goto out;
284 switch (optname) {
285 case RDS_CANCEL_SENT_TO:
286 ret = rds_cancel_sent_to(rs, optval, optlen);
287 break;
288 case RDS_GET_MR:
289 ret = rds_get_mr(rs, optval, optlen);
290 break;
291 case RDS_GET_MR_FOR_DEST:
292 ret = rds_get_mr_for_dest(rs, optval, optlen);
293 break;
294 case RDS_FREE_MR:
295 ret = rds_free_mr(rs, optval, optlen);
296 break;
297 case RDS_RECVERR:
298 ret = rds_set_bool_option(&rs->rs_recverr, optval, optlen);
299 break;
300 case RDS_CONG_MONITOR:
301 ret = rds_cong_monitor(rs, optval, optlen);
302 break;
303 default:
304 ret = -ENOPROTOOPT;
306 out:
307 return ret;
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)
317 goto out;
319 if (get_user(len, optlen)) {
320 ret = -EFAULT;
321 goto out;
324 switch (optname) {
325 case RDS_INFO_FIRST ... RDS_INFO_LAST:
326 ret = rds_info_getsockopt(sock, optname, optval,
327 optlen);
328 break;
330 case RDS_RECVERR:
331 if (len < sizeof(int))
332 ret = -EINVAL;
333 else
334 if (put_user(rs->rs_recverr, (int __user *) optval) ||
335 put_user(sizeof(int), optlen))
336 ret = -EFAULT;
337 else
338 ret = 0;
339 break;
340 default:
341 break;
344 out:
345 return ret;
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);
355 int ret = 0;
357 lock_sock(sk);
359 if (addr_len != sizeof(struct sockaddr_in)) {
360 ret = -EINVAL;
361 goto out;
364 if (sin->sin_family != AF_INET) {
365 ret = -EAFNOSUPPORT;
366 goto out;
369 if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) {
370 ret = -EDESTADDRREQ;
371 goto out;
374 rs->rs_conn_addr = sin->sin_addr.s_addr;
375 rs->rs_conn_port = sin->sin_port;
377 out:
378 release_sock(sk);
379 return ret;
382 static struct proto rds_proto = {
383 .name = "RDS",
384 .owner = THIS_MODULE,
385 .obj_size = sizeof(struct rds_sock),
388 static const struct proto_ops rds_proto_ops = {
389 .family = AF_RDS,
390 .owner = THIS_MODULE,
391 .release = rds_release,
392 .bind = rds_bind,
393 .connect = rds_connect,
394 .socketpair = sock_no_socketpair,
395 .accept = sock_no_accept,
396 .getname = rds_getname,
397 .poll = rds_poll,
398 .ioctl = rds_ioctl,
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)
411 struct rds_sock *rs;
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);
429 rds_sock_count++;
430 spin_unlock_bh(&rds_sock_lock);
432 return 0;
435 static int rds_create(struct net *net, struct socket *sock, int protocol,
436 int kern)
438 struct sock *sk;
440 if (sock->type != SOCK_SEQPACKET || protocol)
441 return -ESOCKTNOSUPPORT;
443 sk = sk_alloc(net, AF_RDS, GFP_ATOMIC, &rds_proto);
444 if (!sk)
445 return -ENOMEM;
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 = {
461 .family = AF_RDS,
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)
470 struct rds_sock *rs;
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) {
483 total++;
484 if (total <= len)
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);
494 lens->nr = total;
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;
503 struct rds_sock *rs;
505 len /= sizeof(struct rds_info_socket);
507 spin_lock_bh(&rds_sock_lock);
509 if (len < rds_sock_count)
510 goto out;
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));
524 out:
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);
535 rds_conn_exit();
536 rds_cong_exit();
537 rds_sysctl_exit();
538 rds_threads_exit();
539 rds_stats_exit();
540 rds_page_exit();
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)
548 int ret;
550 ret = rds_conn_init();
551 if (ret)
552 goto out;
553 ret = rds_threads_init();
554 if (ret)
555 goto out_conn;
556 ret = rds_sysctl_init();
557 if (ret)
558 goto out_threads;
559 ret = rds_stats_init();
560 if (ret)
561 goto out_sysctl;
562 ret = proto_register(&rds_proto, 1);
563 if (ret)
564 goto out_stats;
565 ret = sock_register(&rds_family_ops);
566 if (ret)
567 goto out_proto;
569 rds_info_register_func(RDS_INFO_SOCKETS, rds_sock_info);
570 rds_info_register_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
572 goto out;
574 out_proto:
575 proto_unregister(&rds_proto);
576 out_stats:
577 rds_stats_exit();
578 out_sysctl:
579 rds_sysctl_exit();
580 out_threads:
581 rds_threads_exit();
582 out_conn:
583 rds_conn_exit();
584 rds_cong_exit();
585 rds_page_exit();
586 out:
587 return ret;
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);