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