xref: /openbmc/linux/net/rds/af_rds.c (revision 3805e6a1)
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>
36 #include <linux/gfp.h>
37 #include <linux/in.h>
38 #include <linux/poll.h>
39 #include <net/sock.h>
40 
41 #include "rds.h"
42 
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);
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  */
58 static int rds_release(struct socket *sock)
59 {
60 	struct sock *sk = sock->sk;
61 	struct rds_sock *rs;
62 
63 	if (!sk)
64 		goto out;
65 
66 	rs = rds_sk_to_rs(sk);
67 
68 	sock_orphan(sk);
69 	/* Note - rds_clear_recv_queue grabs rs_recv_lock, so
70 	 * that ensures the recv path has completed messing
71 	 * with the socket. */
72 	rds_clear_recv_queue(rs);
73 	rds_cong_remove_socket(rs);
74 
75 	rds_remove_bound(rs);
76 
77 	rds_send_drop_to(rs, NULL);
78 	rds_rdma_drop_keys(rs);
79 	rds_notify_queue_get(rs, NULL);
80 
81 	spin_lock_bh(&rds_sock_lock);
82 	list_del_init(&rs->rs_item);
83 	rds_sock_count--;
84 	spin_unlock_bh(&rds_sock_lock);
85 
86 	rds_trans_put(rs->rs_transport);
87 
88 	sock->sk = NULL;
89 	sock_put(sk);
90 out:
91 	return 0;
92 }
93 
94 /*
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.
102  */
103 void rds_wake_sk_sleep(struct rds_sock *rs)
104 {
105 	unsigned long flags;
106 
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);
110 }
111 
112 static int rds_getname(struct socket *sock, struct sockaddr *uaddr,
113 		       int *uaddr_len, int peer)
114 {
115 	struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
116 	struct rds_sock *rs = rds_sk_to_rs(sock->sk);
117 
118 	memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
119 
120 	/* racey, don't care */
121 	if (peer) {
122 		if (!rs->rs_conn_addr)
123 			return -ENOTCONN;
124 
125 		sin->sin_port = rs->rs_conn_port;
126 		sin->sin_addr.s_addr = rs->rs_conn_addr;
127 	} else {
128 		sin->sin_port = rs->rs_bound_port;
129 		sin->sin_addr.s_addr = rs->rs_bound_addr;
130 	}
131 
132 	sin->sin_family = AF_INET;
133 
134 	*uaddr_len = sizeof(*sin);
135 	return 0;
136 }
137 
138 /*
139  * RDS' poll is without a doubt the least intuitive part of the interface,
140  * as POLLIN and POLLOUT do not behave entirely as you would expect from
141  * a network protocol.
142  *
143  * POLLIN is asserted if
144  *  -	there is data on the receive queue.
145  *  -	to signal that a previously congested destination may have become
146  *	uncongested
147  *  -	A notification has been queued to the socket (this can be a congestion
148  *	update, or a RDMA completion).
149  *
150  * POLLOUT 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
153  * return ENOBUFS).
154  */
155 static unsigned int rds_poll(struct file *file, struct socket *sock,
156 			     poll_table *wait)
157 {
158 	struct sock *sk = sock->sk;
159 	struct rds_sock *rs = rds_sk_to_rs(sk);
160 	unsigned int mask = 0;
161 	unsigned long flags;
162 
163 	poll_wait(file, sk_sleep(sk), wait);
164 
165 	if (rs->rs_seen_congestion)
166 		poll_wait(file, &rds_poll_waitq, wait);
167 
168 	read_lock_irqsave(&rs->rs_recv_lock, flags);
169 	if (!rs->rs_cong_monitor) {
170 		/* When a congestion map was updated, we signal POLLIN for
171 		 * "historical" reasons. Applications can also poll for
172 		 * WRBAND instead. */
173 		if (rds_cong_updated_since(&rs->rs_cong_track))
174 			mask |= (POLLIN | POLLRDNORM | POLLWRBAND);
175 	} else {
176 		spin_lock(&rs->rs_lock);
177 		if (rs->rs_cong_notify)
178 			mask |= (POLLIN | POLLRDNORM);
179 		spin_unlock(&rs->rs_lock);
180 	}
181 	if (!list_empty(&rs->rs_recv_queue) ||
182 	    !list_empty(&rs->rs_notify_queue))
183 		mask |= (POLLIN | POLLRDNORM);
184 	if (rs->rs_snd_bytes < rds_sk_sndbuf(rs))
185 		mask |= (POLLOUT | POLLWRNORM);
186 	read_unlock_irqrestore(&rs->rs_recv_lock, flags);
187 
188 	/* clear state any time we wake a seen-congested socket */
189 	if (mask)
190 		rs->rs_seen_congestion = 0;
191 
192 	return mask;
193 }
194 
195 static int rds_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
196 {
197 	return -ENOIOCTLCMD;
198 }
199 
200 static int rds_cancel_sent_to(struct rds_sock *rs, char __user *optval,
201 			      int len)
202 {
203 	struct sockaddr_in sin;
204 	int ret = 0;
205 
206 	/* racing with another thread binding seems ok here */
207 	if (rs->rs_bound_addr == 0) {
208 		ret = -ENOTCONN; /* XXX not a great errno */
209 		goto out;
210 	}
211 
212 	if (len < sizeof(struct sockaddr_in)) {
213 		ret = -EINVAL;
214 		goto out;
215 	}
216 
217 	if (copy_from_user(&sin, optval, sizeof(sin))) {
218 		ret = -EFAULT;
219 		goto out;
220 	}
221 
222 	rds_send_drop_to(rs, &sin);
223 out:
224 	return ret;
225 }
226 
227 static int rds_set_bool_option(unsigned char *optvar, char __user *optval,
228 			       int optlen)
229 {
230 	int value;
231 
232 	if (optlen < sizeof(int))
233 		return -EINVAL;
234 	if (get_user(value, (int __user *) optval))
235 		return -EFAULT;
236 	*optvar = !!value;
237 	return 0;
238 }
239 
240 static int rds_cong_monitor(struct rds_sock *rs, char __user *optval,
241 			    int optlen)
242 {
243 	int ret;
244 
245 	ret = rds_set_bool_option(&rs->rs_cong_monitor, optval, optlen);
246 	if (ret == 0) {
247 		if (rs->rs_cong_monitor) {
248 			rds_cong_add_socket(rs);
249 		} else {
250 			rds_cong_remove_socket(rs);
251 			rs->rs_cong_mask = 0;
252 			rs->rs_cong_notify = 0;
253 		}
254 	}
255 	return ret;
256 }
257 
258 static int rds_set_transport(struct rds_sock *rs, char __user *optval,
259 			     int optlen)
260 {
261 	int t_type;
262 
263 	if (rs->rs_transport)
264 		return -EOPNOTSUPP; /* previously attached to transport */
265 
266 	if (optlen != sizeof(int))
267 		return -EINVAL;
268 
269 	if (copy_from_user(&t_type, (int __user *)optval, sizeof(t_type)))
270 		return -EFAULT;
271 
272 	if (t_type < 0 || t_type >= RDS_TRANS_COUNT)
273 		return -EINVAL;
274 
275 	rs->rs_transport = rds_trans_get(t_type);
276 
277 	return rs->rs_transport ? 0 : -ENOPROTOOPT;
278 }
279 
280 static int rds_enable_recvtstamp(struct sock *sk, char __user *optval,
281 				 int optlen)
282 {
283 	int val, valbool;
284 
285 	if (optlen != sizeof(int))
286 		return -EFAULT;
287 
288 	if (get_user(val, (int __user *)optval))
289 		return -EFAULT;
290 
291 	valbool = val ? 1 : 0;
292 
293 	if (valbool)
294 		sock_set_flag(sk, SOCK_RCVTSTAMP);
295 	else
296 		sock_reset_flag(sk, SOCK_RCVTSTAMP);
297 
298 	return 0;
299 }
300 
301 static int rds_setsockopt(struct socket *sock, int level, int optname,
302 			  char __user *optval, unsigned int optlen)
303 {
304 	struct rds_sock *rs = rds_sk_to_rs(sock->sk);
305 	int ret;
306 
307 	if (level != SOL_RDS) {
308 		ret = -ENOPROTOOPT;
309 		goto out;
310 	}
311 
312 	switch (optname) {
313 	case RDS_CANCEL_SENT_TO:
314 		ret = rds_cancel_sent_to(rs, optval, optlen);
315 		break;
316 	case RDS_GET_MR:
317 		ret = rds_get_mr(rs, optval, optlen);
318 		break;
319 	case RDS_GET_MR_FOR_DEST:
320 		ret = rds_get_mr_for_dest(rs, optval, optlen);
321 		break;
322 	case RDS_FREE_MR:
323 		ret = rds_free_mr(rs, optval, optlen);
324 		break;
325 	case RDS_RECVERR:
326 		ret = rds_set_bool_option(&rs->rs_recverr, optval, optlen);
327 		break;
328 	case RDS_CONG_MONITOR:
329 		ret = rds_cong_monitor(rs, optval, optlen);
330 		break;
331 	case SO_RDS_TRANSPORT:
332 		lock_sock(sock->sk);
333 		ret = rds_set_transport(rs, optval, optlen);
334 		release_sock(sock->sk);
335 		break;
336 	case SO_TIMESTAMP:
337 		lock_sock(sock->sk);
338 		ret = rds_enable_recvtstamp(sock->sk, optval, optlen);
339 		release_sock(sock->sk);
340 		break;
341 	default:
342 		ret = -ENOPROTOOPT;
343 	}
344 out:
345 	return ret;
346 }
347 
348 static int rds_getsockopt(struct socket *sock, int level, int optname,
349 			  char __user *optval, int __user *optlen)
350 {
351 	struct rds_sock *rs = rds_sk_to_rs(sock->sk);
352 	int ret = -ENOPROTOOPT, len;
353 	int trans;
354 
355 	if (level != SOL_RDS)
356 		goto out;
357 
358 	if (get_user(len, optlen)) {
359 		ret = -EFAULT;
360 		goto out;
361 	}
362 
363 	switch (optname) {
364 	case RDS_INFO_FIRST ... RDS_INFO_LAST:
365 		ret = rds_info_getsockopt(sock, optname, optval,
366 					  optlen);
367 		break;
368 
369 	case RDS_RECVERR:
370 		if (len < sizeof(int))
371 			ret = -EINVAL;
372 		else
373 		if (put_user(rs->rs_recverr, (int __user *) optval) ||
374 		    put_user(sizeof(int), optlen))
375 			ret = -EFAULT;
376 		else
377 			ret = 0;
378 		break;
379 	case SO_RDS_TRANSPORT:
380 		if (len < sizeof(int)) {
381 			ret = -EINVAL;
382 			break;
383 		}
384 		trans = (rs->rs_transport ? rs->rs_transport->t_type :
385 			 RDS_TRANS_NONE); /* unbound */
386 		if (put_user(trans, (int __user *)optval) ||
387 		    put_user(sizeof(int), optlen))
388 			ret = -EFAULT;
389 		else
390 			ret = 0;
391 		break;
392 	default:
393 		break;
394 	}
395 
396 out:
397 	return ret;
398 
399 }
400 
401 static int rds_connect(struct socket *sock, struct sockaddr *uaddr,
402 		       int addr_len, int flags)
403 {
404 	struct sock *sk = sock->sk;
405 	struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
406 	struct rds_sock *rs = rds_sk_to_rs(sk);
407 	int ret = 0;
408 
409 	lock_sock(sk);
410 
411 	if (addr_len != sizeof(struct sockaddr_in)) {
412 		ret = -EINVAL;
413 		goto out;
414 	}
415 
416 	if (sin->sin_family != AF_INET) {
417 		ret = -EAFNOSUPPORT;
418 		goto out;
419 	}
420 
421 	if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) {
422 		ret = -EDESTADDRREQ;
423 		goto out;
424 	}
425 
426 	rs->rs_conn_addr = sin->sin_addr.s_addr;
427 	rs->rs_conn_port = sin->sin_port;
428 
429 out:
430 	release_sock(sk);
431 	return ret;
432 }
433 
434 static struct proto rds_proto = {
435 	.name	  = "RDS",
436 	.owner	  = THIS_MODULE,
437 	.obj_size = sizeof(struct rds_sock),
438 };
439 
440 static const struct proto_ops rds_proto_ops = {
441 	.family =	AF_RDS,
442 	.owner =	THIS_MODULE,
443 	.release =	rds_release,
444 	.bind =		rds_bind,
445 	.connect =	rds_connect,
446 	.socketpair =	sock_no_socketpair,
447 	.accept =	sock_no_accept,
448 	.getname =	rds_getname,
449 	.poll =		rds_poll,
450 	.ioctl =	rds_ioctl,
451 	.listen =	sock_no_listen,
452 	.shutdown =	sock_no_shutdown,
453 	.setsockopt =	rds_setsockopt,
454 	.getsockopt =	rds_getsockopt,
455 	.sendmsg =	rds_sendmsg,
456 	.recvmsg =	rds_recvmsg,
457 	.mmap =		sock_no_mmap,
458 	.sendpage =	sock_no_sendpage,
459 };
460 
461 static void rds_sock_destruct(struct sock *sk)
462 {
463 	struct rds_sock *rs = rds_sk_to_rs(sk);
464 
465 	WARN_ON((&rs->rs_item != rs->rs_item.next ||
466 		 &rs->rs_item != rs->rs_item.prev));
467 }
468 
469 static int __rds_create(struct socket *sock, struct sock *sk, int protocol)
470 {
471 	struct rds_sock *rs;
472 
473 	sock_init_data(sock, sk);
474 	sock->ops		= &rds_proto_ops;
475 	sk->sk_protocol		= protocol;
476 	sk->sk_destruct		= rds_sock_destruct;
477 
478 	rs = rds_sk_to_rs(sk);
479 	spin_lock_init(&rs->rs_lock);
480 	rwlock_init(&rs->rs_recv_lock);
481 	INIT_LIST_HEAD(&rs->rs_send_queue);
482 	INIT_LIST_HEAD(&rs->rs_recv_queue);
483 	INIT_LIST_HEAD(&rs->rs_notify_queue);
484 	INIT_LIST_HEAD(&rs->rs_cong_list);
485 	spin_lock_init(&rs->rs_rdma_lock);
486 	rs->rs_rdma_keys = RB_ROOT;
487 
488 	spin_lock_bh(&rds_sock_lock);
489 	list_add_tail(&rs->rs_item, &rds_sock_list);
490 	rds_sock_count++;
491 	spin_unlock_bh(&rds_sock_lock);
492 
493 	return 0;
494 }
495 
496 static int rds_create(struct net *net, struct socket *sock, int protocol,
497 		      int kern)
498 {
499 	struct sock *sk;
500 
501 	if (sock->type != SOCK_SEQPACKET || protocol)
502 		return -ESOCKTNOSUPPORT;
503 
504 	sk = sk_alloc(net, AF_RDS, GFP_ATOMIC, &rds_proto, kern);
505 	if (!sk)
506 		return -ENOMEM;
507 
508 	return __rds_create(sock, sk, protocol);
509 }
510 
511 void rds_sock_addref(struct rds_sock *rs)
512 {
513 	sock_hold(rds_rs_to_sk(rs));
514 }
515 
516 void rds_sock_put(struct rds_sock *rs)
517 {
518 	sock_put(rds_rs_to_sk(rs));
519 }
520 
521 static const struct net_proto_family rds_family_ops = {
522 	.family =	AF_RDS,
523 	.create =	rds_create,
524 	.owner	=	THIS_MODULE,
525 };
526 
527 static void rds_sock_inc_info(struct socket *sock, unsigned int len,
528 			      struct rds_info_iterator *iter,
529 			      struct rds_info_lengths *lens)
530 {
531 	struct rds_sock *rs;
532 	struct rds_incoming *inc;
533 	unsigned int total = 0;
534 
535 	len /= sizeof(struct rds_info_message);
536 
537 	spin_lock_bh(&rds_sock_lock);
538 
539 	list_for_each_entry(rs, &rds_sock_list, rs_item) {
540 		read_lock(&rs->rs_recv_lock);
541 
542 		/* XXX too lazy to maintain counts.. */
543 		list_for_each_entry(inc, &rs->rs_recv_queue, i_item) {
544 			total++;
545 			if (total <= len)
546 				rds_inc_info_copy(inc, iter, inc->i_saddr,
547 						  rs->rs_bound_addr, 1);
548 		}
549 
550 		read_unlock(&rs->rs_recv_lock);
551 	}
552 
553 	spin_unlock_bh(&rds_sock_lock);
554 
555 	lens->nr = total;
556 	lens->each = sizeof(struct rds_info_message);
557 }
558 
559 static void rds_sock_info(struct socket *sock, unsigned int len,
560 			  struct rds_info_iterator *iter,
561 			  struct rds_info_lengths *lens)
562 {
563 	struct rds_info_socket sinfo;
564 	struct rds_sock *rs;
565 
566 	len /= sizeof(struct rds_info_socket);
567 
568 	spin_lock_bh(&rds_sock_lock);
569 
570 	if (len < rds_sock_count)
571 		goto out;
572 
573 	list_for_each_entry(rs, &rds_sock_list, rs_item) {
574 		sinfo.sndbuf = rds_sk_sndbuf(rs);
575 		sinfo.rcvbuf = rds_sk_rcvbuf(rs);
576 		sinfo.bound_addr = rs->rs_bound_addr;
577 		sinfo.connected_addr = rs->rs_conn_addr;
578 		sinfo.bound_port = rs->rs_bound_port;
579 		sinfo.connected_port = rs->rs_conn_port;
580 		sinfo.inum = sock_i_ino(rds_rs_to_sk(rs));
581 
582 		rds_info_copy(iter, &sinfo, sizeof(sinfo));
583 	}
584 
585 out:
586 	lens->nr = rds_sock_count;
587 	lens->each = sizeof(struct rds_info_socket);
588 
589 	spin_unlock_bh(&rds_sock_lock);
590 }
591 
592 static void rds_exit(void)
593 {
594 	sock_unregister(rds_family_ops.family);
595 	proto_unregister(&rds_proto);
596 	rds_conn_exit();
597 	rds_cong_exit();
598 	rds_sysctl_exit();
599 	rds_threads_exit();
600 	rds_stats_exit();
601 	rds_page_exit();
602 	rds_bind_lock_destroy();
603 	rds_info_deregister_func(RDS_INFO_SOCKETS, rds_sock_info);
604 	rds_info_deregister_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
605 }
606 module_exit(rds_exit);
607 
608 static int rds_init(void)
609 {
610 	int ret;
611 
612 	ret = rds_bind_lock_init();
613 	if (ret)
614 		goto out;
615 
616 	ret = rds_conn_init();
617 	if (ret)
618 		goto out_bind;
619 
620 	ret = rds_threads_init();
621 	if (ret)
622 		goto out_conn;
623 	ret = rds_sysctl_init();
624 	if (ret)
625 		goto out_threads;
626 	ret = rds_stats_init();
627 	if (ret)
628 		goto out_sysctl;
629 	ret = proto_register(&rds_proto, 1);
630 	if (ret)
631 		goto out_stats;
632 	ret = sock_register(&rds_family_ops);
633 	if (ret)
634 		goto out_proto;
635 
636 	rds_info_register_func(RDS_INFO_SOCKETS, rds_sock_info);
637 	rds_info_register_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
638 
639 	goto out;
640 
641 out_proto:
642 	proto_unregister(&rds_proto);
643 out_stats:
644 	rds_stats_exit();
645 out_sysctl:
646 	rds_sysctl_exit();
647 out_threads:
648 	rds_threads_exit();
649 out_conn:
650 	rds_conn_exit();
651 	rds_cong_exit();
652 	rds_page_exit();
653 out_bind:
654 	rds_bind_lock_destroy();
655 out:
656 	return ret;
657 }
658 module_init(rds_init);
659 
660 #define DRV_VERSION     "4.0"
661 #define DRV_RELDATE     "Feb 12, 2009"
662 
663 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
664 MODULE_DESCRIPTION("RDS: Reliable Datagram Sockets"
665 		   " v" DRV_VERSION " (" DRV_RELDATE ")");
666 MODULE_VERSION(DRV_VERSION);
667 MODULE_LICENSE("Dual BSD/GPL");
668 MODULE_ALIAS_NETPROTO(PF_RDS);
669