4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 #define DEBUG_SUBSYSTEM S_LNET
40 #include <linux/net.h>
41 #include <linux/file.h>
42 #include <linux/pagemap.h>
43 /* For sys_open & sys_close */
44 #include <linux/syscalls.h>
47 #include <libcfs/libcfs.h>
48 #include <lnet/lib-lnet.h>
51 kernel_sock_unlocked_ioctl(struct file *filp, int cmd, unsigned long arg)
53 mm_segment_t oldfs = get_fs();
57 err = filp->f_op->unlocked_ioctl(filp, cmd, arg);
64 lnet_sock_ioctl(int cmd, unsigned long arg)
66 struct file *sock_filp;
71 rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
73 CERROR("Can't create socket: %d\n", rc);
77 #if !defined(HAVE_SOCK_ALLOC_FILE) && !defined(HAVE_SOCK_ALLOC_FILE_3ARGS)
78 fd = sock_map_fd(sock, 0);
86 # ifdef HAVE_SOCK_ALLOC_FILE_3ARGS
87 sock_filp = sock_alloc_file(sock, 0, NULL);
89 sock_filp = sock_alloc_file(sock, 0);
92 if (IS_ERR(sock_filp)) {
93 rc = PTR_ERR(sock_filp);
98 rc = kernel_sock_unlocked_ioctl(sock_filp, cmd, arg);
108 lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask)
115 nob = strnlen(name, IFNAMSIZ);
116 if (nob == IFNAMSIZ) {
117 CERROR("Interface name %s too long\n", name);
121 CLASSERT(sizeof(ifr.ifr_name) >= IFNAMSIZ);
123 if (strlen(name) > sizeof(ifr.ifr_name)-1)
125 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
127 rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
129 CERROR("Can't get flags for interface %s\n", name);
133 if ((ifr.ifr_flags & IFF_UP) == 0) {
134 CDEBUG(D_NET, "Interface %s down\n", name);
141 if (strlen(name) > sizeof(ifr.ifr_name)-1)
143 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
145 ifr.ifr_addr.sa_family = AF_INET;
146 rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
149 CERROR("Can't get IP address for interface %s\n", name);
153 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
156 if (strlen(name) > sizeof(ifr.ifr_name)-1)
158 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
160 ifr.ifr_addr.sa_family = AF_INET;
161 rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
163 CERROR("Can't get netmask for interface %s\n", name);
167 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
172 EXPORT_SYMBOL(lnet_ipif_query);
175 lnet_ipif_free_enumeration(char **names, int n)
181 for (i = 0; i < n && names[i] != NULL; i++)
182 LIBCFS_FREE(names[i], IFNAMSIZ);
184 LIBCFS_FREE(names, n * sizeof(*names));
186 EXPORT_SYMBOL(lnet_ipif_free_enumeration);
189 lnet_ipif_enumerate(char ***namesp)
191 /* Allocate and fill in 'names', returning # interfaces/error */
202 nalloc = 16; /* first guess at max interfaces */
205 if (nalloc * sizeof(*ifr) > PAGE_CACHE_SIZE) {
207 nalloc = PAGE_CACHE_SIZE/sizeof(*ifr);
208 CWARN("Too many interfaces: only enumerating "
209 "first %d\n", nalloc);
212 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
214 CERROR("ENOMEM enumerating up to %d interfaces\n",
220 ifc.ifc_buf = (char *)ifr;
221 ifc.ifc_len = nalloc * sizeof(*ifr);
223 rc = lnet_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
225 CERROR("Error %d enumerating interfaces\n", rc);
231 nfound = ifc.ifc_len/sizeof(*ifr);
232 LASSERT(nfound <= nalloc);
234 if (nfound < nalloc || toobig)
237 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
244 LIBCFS_ALLOC(names, nfound * sizeof(*names));
250 for (i = 0; i < nfound; i++) {
251 nob = strnlen(ifr[i].ifr_name, IFNAMSIZ);
252 if (nob == IFNAMSIZ) {
253 /* no space for terminating NULL */
254 CERROR("interface name %.*s too long (%d max)\n",
255 nob, ifr[i].ifr_name, IFNAMSIZ);
260 LIBCFS_ALLOC(names[i], IFNAMSIZ);
261 if (names[i] == NULL) {
266 memcpy(names[i], ifr[i].ifr_name, nob);
275 lnet_ipif_free_enumeration(names, nfound);
277 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
281 EXPORT_SYMBOL(lnet_ipif_enumerate);
284 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
287 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
292 /* Caller may pass a zero timeout if she thinks the socket buffer is
293 * empty enough to take the whole message immediately */
300 struct msghdr msg = {
301 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
305 /* Set send timeout to remaining time */
306 tv = (struct timeval) {
307 .tv_sec = jiffies_left /
308 msecs_to_jiffies(MSEC_PER_SEC),
309 .tv_usec = ((jiffies_left %
310 msecs_to_jiffies(MSEC_PER_SEC)) *
312 msecs_to_jiffies(MSEC_PER_SEC)
315 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
316 (char *)&tv, sizeof(tv));
318 CERROR("Can't set socket send timeout "
320 (long)tv.tv_sec, (int)tv.tv_usec, rc);
326 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
327 jiffies_left -= jiffies - then;
336 CERROR("Unexpected zero rc\n");
337 return -ECONNABORTED;
340 if (jiffies_left <= 0)
343 buffer = ((char *)buffer) + rc;
348 EXPORT_SYMBOL(lnet_sock_write);
351 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
354 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
359 LASSERT(jiffies_left > 0);
366 struct msghdr msg = {
370 /* Set receive timeout to remaining time */
371 tv = (struct timeval) {
372 .tv_sec = jiffies_left / msecs_to_jiffies(MSEC_PER_SEC),
373 .tv_usec = ((jiffies_left %
374 msecs_to_jiffies(MSEC_PER_SEC)) *
376 msecs_to_jiffies(MSEC_PER_SEC)
378 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
379 (char *)&tv, sizeof(tv));
381 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
382 (long)tv.tv_sec, (int)tv.tv_usec, rc);
387 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
388 jiffies_left -= jiffies - then;
396 buffer = ((char *)buffer) + rc;
402 if (jiffies_left <= 0)
406 EXPORT_SYMBOL(lnet_sock_read);
409 lnet_sock_create(struct socket **sockp, int *fatal,
410 __u32 local_ip, int local_port)
412 struct sockaddr_in locaddr;
417 /* All errors are fatal except bind failure if the port is in use */
420 rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
423 CERROR("Can't create socket: %d\n", rc);
428 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
429 (char *)&option, sizeof(option));
431 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
435 if (local_ip != 0 || local_port != 0) {
436 memset(&locaddr, 0, sizeof(locaddr));
437 locaddr.sin_family = AF_INET;
438 locaddr.sin_port = htons(local_port);
439 locaddr.sin_addr.s_addr = (local_ip == 0) ?
440 INADDR_ANY : htonl(local_ip);
442 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
444 if (rc == -EADDRINUSE) {
445 CDEBUG(D_NET, "Port %d already in use\n", local_port);
450 CERROR("Error trying to bind to port %d: %d\n",
463 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
468 if (txbufsize != 0) {
470 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
471 (char *)&option, sizeof(option));
473 CERROR("Can't set send buffer %d: %d\n",
479 if (rxbufsize != 0) {
481 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
482 (char *)&option, sizeof(option));
484 CERROR("Can't set receive buffer %d: %d\n",
491 EXPORT_SYMBOL(lnet_sock_setbuf);
494 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
496 struct sockaddr_in sin;
497 int len = sizeof(sin);
501 rc = kernel_getpeername(sock, (struct sockaddr *)&sin, &len);
503 rc = kernel_getsockname(sock, (struct sockaddr *)&sin, &len);
505 CERROR("Error %d getting sock %s IP/port\n",
506 rc, remote ? "peer" : "local");
511 *ip = ntohl(sin.sin_addr.s_addr);
514 *port = ntohs(sin.sin_port);
518 EXPORT_SYMBOL(lnet_sock_getaddr);
521 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
523 if (txbufsize != NULL)
524 *txbufsize = sock->sk->sk_sndbuf;
526 if (rxbufsize != NULL)
527 *rxbufsize = sock->sk->sk_rcvbuf;
531 EXPORT_SYMBOL(lnet_sock_getbuf);
534 lnet_sock_listen(struct socket **sockp,
535 __u32 local_ip, int local_port, int backlog)
540 rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
543 CERROR("Can't create socket: port %d already in use\n",
548 rc = kernel_listen(*sockp, backlog);
552 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
553 sock_release(*sockp);
557 #ifndef HAVE_SK_SLEEP
558 static inline wait_queue_head_t *sk_sleep(struct sock *sk)
565 lnet_sock_accept(struct socket **newsockp, struct socket *sock)
568 struct socket *newsock;
571 /* XXX this should add a ref to sock->ops->owner, if
572 * TCP could be a module */
573 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
575 CERROR("Can't allocate socket\n");
579 newsock->ops = sock->ops;
581 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
583 /* Nothing ready, so wait for activity */
584 init_waitqueue_entry(&wait, current);
585 add_wait_queue(sk_sleep(sock->sk), &wait);
586 set_current_state(TASK_INTERRUPTIBLE);
588 remove_wait_queue(sk_sleep(sock->sk), &wait);
589 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
599 sock_release(newsock);
604 lnet_sock_connect(struct socket **sockp, int *fatal,
605 __u32 local_ip, int local_port,
606 __u32 peer_ip, int peer_port)
608 struct sockaddr_in srvaddr;
611 rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
615 memset(&srvaddr, 0, sizeof(srvaddr));
616 srvaddr.sin_family = AF_INET;
617 srvaddr.sin_port = htons(peer_port);
618 srvaddr.sin_addr.s_addr = htonl(peer_ip);
620 rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
625 /* EADDRNOTAVAIL probably means we're already connected to the same
626 * peer/port on the same local port on a differently typed
627 * connection. Let our caller retry with a different local
629 *fatal = !(rc == -EADDRNOTAVAIL);
631 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
632 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
633 &local_ip, local_port, &peer_ip, peer_port);
635 sock_release(*sockp);