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 CERROR("netdev %s\n", dev->name);
208 if (nob == IFNAMSIZ) {
209 /* no space for terminating NULL */
210 CERROR("interface name %.*s too long (%d max)\n",
211 nob, dev->name, IFNAMSIZ);
216 LIBCFS_ALLOC(names[i], IFNAMSIZ);
222 memcpy(names[i], dev->name, nob);
232 lnet_ipif_free_enumeration(names, nfound);
237 EXPORT_SYMBOL(lnet_ipif_enumerate);
240 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
243 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
248 /* Caller may pass a zero timeout if she thinks the socket buffer is
249 * empty enough to take the whole message immediately */
256 struct msghdr msg = {
257 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
261 /* Set send timeout to remaining time */
262 tv = (struct timeval) {
263 .tv_sec = jiffies_left /
264 msecs_to_jiffies(MSEC_PER_SEC),
265 .tv_usec = ((jiffies_left %
266 msecs_to_jiffies(MSEC_PER_SEC)) *
268 msecs_to_jiffies(MSEC_PER_SEC)
271 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
272 (char *)&tv, sizeof(tv));
274 CERROR("Can't set socket send timeout "
276 (long)tv.tv_sec, (int)tv.tv_usec, rc);
282 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
283 jiffies_left -= jiffies - then;
292 CERROR("Unexpected zero rc\n");
293 return -ECONNABORTED;
296 if (jiffies_left <= 0)
299 buffer = ((char *)buffer) + rc;
304 EXPORT_SYMBOL(lnet_sock_write);
307 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
310 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
315 LASSERT(jiffies_left > 0);
322 struct msghdr msg = {
326 /* Set receive timeout to remaining time */
327 tv = (struct timeval) {
328 .tv_sec = jiffies_left / msecs_to_jiffies(MSEC_PER_SEC),
329 .tv_usec = ((jiffies_left %
330 msecs_to_jiffies(MSEC_PER_SEC)) *
332 msecs_to_jiffies(MSEC_PER_SEC)
334 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
335 (char *)&tv, sizeof(tv));
337 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
338 (long)tv.tv_sec, (int)tv.tv_usec, rc);
343 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
344 jiffies_left -= jiffies - then;
352 buffer = ((char *)buffer) + rc;
358 if (jiffies_left <= 0)
362 EXPORT_SYMBOL(lnet_sock_read);
365 lnet_sock_create(struct socket **sockp, int *fatal,
366 __u32 local_ip, int local_port)
368 struct sockaddr_in locaddr;
373 /* All errors are fatal except bind failure if the port is in use */
376 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
377 rc = sock_create_kern(&init_net, PF_INET, SOCK_STREAM, 0, &sock);
379 rc = sock_create_kern(PF_INET, SOCK_STREAM, 0, &sock);
383 CERROR("Can't create socket: %d\n", rc);
388 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
389 (char *)&option, sizeof(option));
391 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
395 if (local_ip != 0 || local_port != 0) {
396 memset(&locaddr, 0, sizeof(locaddr));
397 locaddr.sin_family = AF_INET;
398 locaddr.sin_port = htons(local_port);
399 locaddr.sin_addr.s_addr = (local_ip == 0) ?
400 INADDR_ANY : htonl(local_ip);
402 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
404 if (rc == -EADDRINUSE) {
405 CDEBUG(D_NET, "Port %d already in use\n", local_port);
410 CERROR("Error trying to bind to port %d: %d\n",
423 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
428 if (txbufsize != 0) {
430 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
431 (char *)&option, sizeof(option));
433 CERROR("Can't set send buffer %d: %d\n",
439 if (rxbufsize != 0) {
441 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
442 (char *)&option, sizeof(option));
444 CERROR("Can't set receive buffer %d: %d\n",
451 EXPORT_SYMBOL(lnet_sock_setbuf);
454 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
456 struct sockaddr_in sin;
457 int len = sizeof(sin);
461 rc = kernel_getpeername(sock, (struct sockaddr *)&sin, &len);
463 rc = kernel_getsockname(sock, (struct sockaddr *)&sin, &len);
465 CERROR("Error %d getting sock %s IP/port\n",
466 rc, remote ? "peer" : "local");
471 *ip = ntohl(sin.sin_addr.s_addr);
474 *port = ntohs(sin.sin_port);
478 EXPORT_SYMBOL(lnet_sock_getaddr);
481 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
483 if (txbufsize != NULL)
484 *txbufsize = sock->sk->sk_sndbuf;
486 if (rxbufsize != NULL)
487 *rxbufsize = sock->sk->sk_rcvbuf;
491 EXPORT_SYMBOL(lnet_sock_getbuf);
494 lnet_sock_listen(struct socket **sockp,
495 __u32 local_ip, int local_port, int backlog)
500 rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
503 CERROR("Can't create socket: port %d already in use\n",
508 rc = kernel_listen(*sockp, backlog);
512 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
513 sock_release(*sockp);
517 #ifndef HAVE_SK_SLEEP
518 static inline wait_queue_head_t *sk_sleep(struct sock *sk)
525 lnet_sock_accept(struct socket **newsockp, struct socket *sock)
527 wait_queue_entry_t wait;
528 struct socket *newsock;
531 /* XXX this should add a ref to sock->ops->owner, if
532 * TCP could be a module */
533 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
535 CERROR("Can't allocate socket\n");
539 newsock->ops = sock->ops;
541 #ifdef HAVE_KERN_SOCK_ACCEPT_FLAG_ARG
542 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
544 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
547 /* Nothing ready, so wait for activity */
548 init_waitqueue_entry(&wait, current);
549 add_wait_queue(sk_sleep(sock->sk), &wait);
550 set_current_state(TASK_INTERRUPTIBLE);
552 remove_wait_queue(sk_sleep(sock->sk), &wait);
553 #ifdef HAVE_KERN_SOCK_ACCEPT_FLAG_ARG
554 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
556 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
567 sock_release(newsock);
572 lnet_sock_connect(struct socket **sockp, int *fatal,
573 __u32 local_ip, int local_port,
574 __u32 peer_ip, int peer_port)
576 struct sockaddr_in srvaddr;
579 rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
583 memset(&srvaddr, 0, sizeof(srvaddr));
584 srvaddr.sin_family = AF_INET;
585 srvaddr.sin_port = htons(peer_port);
586 srvaddr.sin_addr.s_addr = htonl(peer_ip);
588 rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
593 /* EADDRNOTAVAIL probably means we're already connected to the same
594 * peer/port on the same local port on a differently typed
595 * connection. Let our caller retry with a different local
597 *fatal = !(rc == -EADDRNOTAVAIL);
599 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
600 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
601 &local_ip, local_port, &peer_ip, peer_port);
603 sock_release(*sockp);