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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2015, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 #define DEBUG_SUBSYSTEM S_LNET
36 #include <linux/net.h>
37 #include <linux/file.h>
38 #include <linux/pagemap.h>
39 /* For sys_open & sys_close */
40 #include <linux/syscalls.h>
43 #include <libcfs/libcfs.h>
44 #include <lnet/lib-lnet.h>
47 lnet_sock_ioctl(int cmd, unsigned long arg)
52 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
53 rc = sock_create_kern(&init_net, PF_INET, SOCK_STREAM, 0, &sock);
55 rc = sock_create_kern(PF_INET, SOCK_STREAM, 0, &sock);
58 CERROR("Can't create socket: %d\n", rc);
62 if (cmd == SIOCGIFFLAGS) {
63 /* This cmd is used only to get IFF_UP flag */
64 struct ifreq *ifr = (struct ifreq *) arg;
65 struct net_device *dev;
67 dev = dev_get_by_name(sock_net(sock->sk), ifr->ifr_name);
69 ifr->ifr_flags = dev->flags;
76 rc = kernel_sock_ioctl(sock, cmd, arg);
84 lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask)
91 nob = strnlen(name, IFNAMSIZ);
92 if (nob == IFNAMSIZ) {
93 CERROR("Interface name %s too long\n", name);
97 CLASSERT(sizeof(ifr.ifr_name) >= IFNAMSIZ);
99 if (strlen(name) > sizeof(ifr.ifr_name)-1)
101 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
103 rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
105 CERROR("Can't get flags for interface %s\n", name);
109 if ((ifr.ifr_flags & IFF_UP) == 0) {
110 CDEBUG(D_NET, "Interface %s down\n", name);
117 if (strlen(name) > sizeof(ifr.ifr_name)-1)
119 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
121 ifr.ifr_addr.sa_family = AF_INET;
122 rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
125 CERROR("Can't get IP address for interface %s\n", name);
129 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
132 if (strlen(name) > sizeof(ifr.ifr_name)-1)
134 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
136 ifr.ifr_addr.sa_family = AF_INET;
137 rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
139 CERROR("Can't get netmask for interface %s\n", name);
143 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
148 EXPORT_SYMBOL(lnet_ipif_query);
151 lnet_ipif_free_enumeration(char **names, int n)
157 for (i = 0; i < n && names[i] != NULL; i++)
158 LIBCFS_FREE(names[i], IFNAMSIZ);
160 LIBCFS_FREE(names, n * sizeof(*names));
162 EXPORT_SYMBOL(lnet_ipif_free_enumeration);
165 lnet_ipif_enumerate(char ***namesp)
167 /* Allocate and fill in 'names', returning # interfaces/error */
168 struct net_device *dev;
178 nalloc = 16; /* first guess at max interfaces */
182 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
183 rc = sock_create_kern(&init_net, PF_INET, SOCK_STREAM, 0, &sock);
185 rc = sock_create_kern(PF_INET, SOCK_STREAM, 0, &sock);
188 CERROR("Can't create socket: %d\n", rc);
192 for_each_netdev(sock_net(sock->sk), dev)
196 goto out_release_sock;
198 LIBCFS_ALLOC(names, nfound * sizeof(*names));
201 goto out_release_sock;
205 for_each_netdev(sock_net(sock->sk), dev) {
206 nob = strnlen(dev->name, IFNAMSIZ);
207 if (nob == IFNAMSIZ) {
208 /* no space for terminating NULL */
209 CERROR("interface name %.*s too long (%d max)\n",
210 nob, dev->name, IFNAMSIZ);
215 LIBCFS_ALLOC(names[i], IFNAMSIZ);
221 memcpy(names[i], dev->name, nob);
231 lnet_ipif_free_enumeration(names, nfound);
236 EXPORT_SYMBOL(lnet_ipif_enumerate);
239 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
242 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
247 /* Caller may pass a zero timeout if she thinks the socket buffer is
248 * empty enough to take the whole message immediately */
255 struct msghdr msg = {
256 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
260 /* Set send timeout to remaining time */
261 tv = (struct timeval) {
262 .tv_sec = jiffies_left /
263 msecs_to_jiffies(MSEC_PER_SEC),
264 .tv_usec = ((jiffies_left %
265 msecs_to_jiffies(MSEC_PER_SEC)) *
267 msecs_to_jiffies(MSEC_PER_SEC)
270 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
271 (char *)&tv, sizeof(tv));
273 CERROR("Can't set socket send timeout "
275 (long)tv.tv_sec, (int)tv.tv_usec, rc);
281 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
282 jiffies_left -= jiffies - then;
291 CERROR("Unexpected zero rc\n");
292 return -ECONNABORTED;
295 if (jiffies_left <= 0)
298 buffer = ((char *)buffer) + rc;
303 EXPORT_SYMBOL(lnet_sock_write);
306 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
309 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
314 LASSERT(jiffies_left > 0);
321 struct msghdr msg = {
325 /* Set receive timeout to remaining time */
326 tv = (struct timeval) {
327 .tv_sec = jiffies_left / msecs_to_jiffies(MSEC_PER_SEC),
328 .tv_usec = ((jiffies_left %
329 msecs_to_jiffies(MSEC_PER_SEC)) *
331 msecs_to_jiffies(MSEC_PER_SEC)
333 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
334 (char *)&tv, sizeof(tv));
336 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
337 (long)tv.tv_sec, (int)tv.tv_usec, rc);
342 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
343 jiffies_left -= jiffies - then;
351 buffer = ((char *)buffer) + rc;
357 if (jiffies_left <= 0)
361 EXPORT_SYMBOL(lnet_sock_read);
364 lnet_sock_create(struct socket **sockp, int *fatal,
365 __u32 local_ip, int local_port)
367 struct sockaddr_in locaddr;
372 /* All errors are fatal except bind failure if the port is in use */
375 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
376 rc = sock_create_kern(&init_net, PF_INET, SOCK_STREAM, 0, &sock);
378 rc = sock_create_kern(PF_INET, SOCK_STREAM, 0, &sock);
382 CERROR("Can't create socket: %d\n", rc);
387 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
388 (char *)&option, sizeof(option));
390 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
394 if (local_ip != 0 || local_port != 0) {
395 memset(&locaddr, 0, sizeof(locaddr));
396 locaddr.sin_family = AF_INET;
397 locaddr.sin_port = htons(local_port);
398 locaddr.sin_addr.s_addr = (local_ip == 0) ?
399 INADDR_ANY : htonl(local_ip);
401 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
403 if (rc == -EADDRINUSE) {
404 CDEBUG(D_NET, "Port %d already in use\n", local_port);
409 CERROR("Error trying to bind to port %d: %d\n",
422 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
427 if (txbufsize != 0) {
429 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
430 (char *)&option, sizeof(option));
432 CERROR("Can't set send buffer %d: %d\n",
438 if (rxbufsize != 0) {
440 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
441 (char *)&option, sizeof(option));
443 CERROR("Can't set receive buffer %d: %d\n",
450 EXPORT_SYMBOL(lnet_sock_setbuf);
453 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
455 struct sockaddr_in sin;
456 int len = sizeof(sin);
460 rc = kernel_getpeername(sock, (struct sockaddr *)&sin, &len);
462 rc = kernel_getsockname(sock, (struct sockaddr *)&sin, &len);
464 CERROR("Error %d getting sock %s IP/port\n",
465 rc, remote ? "peer" : "local");
470 *ip = ntohl(sin.sin_addr.s_addr);
473 *port = ntohs(sin.sin_port);
477 EXPORT_SYMBOL(lnet_sock_getaddr);
480 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
482 if (txbufsize != NULL)
483 *txbufsize = sock->sk->sk_sndbuf;
485 if (rxbufsize != NULL)
486 *rxbufsize = sock->sk->sk_rcvbuf;
490 EXPORT_SYMBOL(lnet_sock_getbuf);
493 lnet_sock_listen(struct socket **sockp,
494 __u32 local_ip, int local_port, int backlog)
499 rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
502 CERROR("Can't create socket: port %d already in use\n",
507 rc = kernel_listen(*sockp, backlog);
511 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
512 sock_release(*sockp);
516 #ifndef HAVE_SK_SLEEP
517 static inline wait_queue_head_t *sk_sleep(struct sock *sk)
524 lnet_sock_accept(struct socket **newsockp, struct socket *sock)
526 wait_queue_entry_t wait;
527 struct socket *newsock;
530 /* XXX this should add a ref to sock->ops->owner, if
531 * TCP could be a module */
532 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
534 CERROR("Can't allocate socket\n");
538 newsock->ops = sock->ops;
540 #ifdef HAVE_KERN_SOCK_ACCEPT_FLAG_ARG
541 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
543 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
546 /* Nothing ready, so wait for activity */
547 init_waitqueue_entry(&wait, current);
548 add_wait_queue(sk_sleep(sock->sk), &wait);
549 set_current_state(TASK_INTERRUPTIBLE);
551 remove_wait_queue(sk_sleep(sock->sk), &wait);
552 #ifdef HAVE_KERN_SOCK_ACCEPT_FLAG_ARG
553 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
555 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
566 sock_release(newsock);
571 lnet_sock_connect(struct socket **sockp, int *fatal,
572 __u32 local_ip, int local_port,
573 __u32 peer_ip, int peer_port)
575 struct sockaddr_in srvaddr;
578 rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
582 memset(&srvaddr, 0, sizeof(srvaddr));
583 srvaddr.sin_family = AF_INET;
584 srvaddr.sin_port = htons(peer_port);
585 srvaddr.sin_addr.s_addr = htonl(peer_ip);
587 rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
592 /* EADDRNOTAVAIL probably means we're already connected to the same
593 * peer/port on the same local port on a differently typed
594 * connection. Let our caller retry with a different local
596 *fatal = !(rc == -EADDRNOTAVAIL);
598 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
599 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
600 &local_ip, local_port, &peer_ip, peer_port);
602 sock_release(*sockp);