1 // SPDX-License-Identifier: GPL-2.0
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License version 2 for more details (a copy is included
15 * in the LICENSE file that accompanied this code).
17 * You should have received a copy of the GNU General Public License
18 * version 2 along with this program; If not, see
19 * http://www.gnu.org/licenses/gpl-2.0.html
24 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Use is subject to license terms.
27 * Copyright (c) 2012, 2015, Intel Corporation.
30 * This file is part of Lustre, http://www.lustre.org/
31 * Lustre is a trademark of Seagate, Inc.
33 #define DEBUG_SUBSYSTEM S_LNET
37 #include <linux/net.h>
38 #include <linux/file.h>
39 #include <linux/pagemap.h>
40 /* For sys_open & sys_close */
41 #include <linux/syscalls.h>
44 #include <linux/libcfs/libcfs.h>
45 #include <linux/lnet/lib-lnet.h>
48 kernel_sock_unlocked_ioctl(struct file *filp, int cmd, unsigned long arg)
50 mm_segment_t oldfs = get_fs();
54 err = filp->f_op->unlocked_ioctl(filp, cmd, arg);
61 lnet_sock_ioctl(int cmd, unsigned long arg)
63 struct file *sock_filp;
67 rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
69 CERROR("Can't create socket: %d\n", rc);
73 sock_filp = sock_alloc_file(sock, 0, NULL);
74 if (IS_ERR(sock_filp)) {
76 rc = PTR_ERR(sock_filp);
80 rc = kernel_sock_unlocked_ioctl(sock_filp, cmd, arg);
88 lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask)
95 nob = strnlen(name, IFNAMSIZ);
96 if (nob == IFNAMSIZ) {
97 CERROR("Interface name %s too long\n", name);
101 BUILD_BUG_ON(sizeof(ifr.ifr_name) < IFNAMSIZ);
103 if (strlen(name) > sizeof(ifr.ifr_name) - 1)
105 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
107 rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
109 CERROR("Can't get flags for interface %s\n", name);
113 if (!(ifr.ifr_flags & IFF_UP)) {
114 CDEBUG(D_NET, "Interface %s down\n", name);
121 if (strlen(name) > sizeof(ifr.ifr_name) - 1)
123 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
125 ifr.ifr_addr.sa_family = AF_INET;
126 rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
128 CERROR("Can't get IP address for interface %s\n", name);
132 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
135 if (strlen(name) > sizeof(ifr.ifr_name) - 1)
137 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
139 ifr.ifr_addr.sa_family = AF_INET;
140 rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
142 CERROR("Can't get netmask for interface %s\n", name);
146 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
151 EXPORT_SYMBOL(lnet_ipif_query);
154 lnet_ipif_enumerate(char ***namesp)
156 /* Allocate and fill in 'names', returning # interfaces/error */
167 nalloc = 16; /* first guess at max interfaces */
170 if (nalloc * sizeof(*ifr) > PAGE_SIZE) {
172 nalloc = PAGE_SIZE / sizeof(*ifr);
173 CWARN("Too many interfaces: only enumerating first %d\n",
177 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
179 CERROR("ENOMEM enumerating up to %d interfaces\n",
185 ifc.ifc_buf = (char *)ifr;
186 ifc.ifc_len = nalloc * sizeof(*ifr);
188 rc = lnet_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
190 CERROR("Error %d enumerating interfaces\n", rc);
196 nfound = ifc.ifc_len / sizeof(*ifr);
197 LASSERT(nfound <= nalloc);
199 if (nfound < nalloc || toobig)
202 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
209 LIBCFS_ALLOC(names, nfound * sizeof(*names));
215 for (i = 0; i < nfound; i++) {
216 nob = strnlen(ifr[i].ifr_name, IFNAMSIZ);
217 if (nob == IFNAMSIZ) {
218 /* no space for terminating NULL */
219 CERROR("interface name %.*s too long (%d max)\n",
220 nob, ifr[i].ifr_name, IFNAMSIZ);
225 LIBCFS_ALLOC(names[i], IFNAMSIZ);
231 memcpy(names[i], ifr[i].ifr_name, nob);
240 lnet_ipif_free_enumeration(names, nfound);
242 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
246 EXPORT_SYMBOL(lnet_ipif_enumerate);
249 lnet_ipif_free_enumeration(char **names, int n)
255 for (i = 0; i < n && names[i]; i++)
256 LIBCFS_FREE(names[i], IFNAMSIZ);
258 LIBCFS_FREE(names, n * sizeof(*names));
260 EXPORT_SYMBOL(lnet_ipif_free_enumeration);
263 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
266 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
269 struct kvec iov = { .iov_base = buffer, .iov_len = nob };
270 struct msghdr msg = {NULL,};
274 * Caller may pass a zero timeout if she thinks the socket buffer is
275 * empty enough to take the whole message immediately
277 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iov, 1, nob);
279 msg.msg_flags = !timeout ? MSG_DONTWAIT : 0;
281 /* Set send timeout to remaining time */
282 jiffies_to_timeval(jiffies_left, &tv);
283 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
284 (char *)&tv, sizeof(tv));
286 CERROR("Can't set socket send timeout %ld.%06d: %d\n",
287 (long)tv.tv_sec, (int)tv.tv_usec, rc);
293 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
294 jiffies_left -= jiffies - then;
300 CERROR("Unexpected zero rc\n");
301 return -ECONNABORTED;
304 if (!msg_data_left(&msg))
307 if (jiffies_left <= 0)
312 EXPORT_SYMBOL(lnet_sock_write);
315 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
318 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
323 LASSERT(jiffies_left > 0);
330 struct msghdr msg = {
334 /* Set receive timeout to remaining time */
335 jiffies_to_timeval(jiffies_left, &tv);
336 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
337 (char *)&tv, sizeof(tv));
339 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
340 (long)tv.tv_sec, (int)tv.tv_usec, rc);
345 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
346 jiffies_left -= jiffies - then;
354 buffer = ((char *)buffer) + rc;
360 if (jiffies_left <= 0)
364 EXPORT_SYMBOL(lnet_sock_read);
367 lnet_sock_create(struct socket **sockp, int *fatal, __u32 local_ip,
370 struct sockaddr_in locaddr;
375 /* All errors are fatal except bind failure if the port is in use */
378 rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
381 CERROR("Can't create socket: %d\n", rc);
386 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
387 (char *)&option, sizeof(option));
389 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
393 if (local_ip || local_port) {
394 memset(&locaddr, 0, sizeof(locaddr));
395 locaddr.sin_family = AF_INET;
396 locaddr.sin_port = htons(local_port);
398 locaddr.sin_addr.s_addr = htonl(INADDR_ANY);
400 locaddr.sin_addr.s_addr = 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)
430 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
431 (char *)&option, sizeof(option));
433 CERROR("Can't set send buffer %d: %d\n",
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)
484 *txbufsize = sock->sk->sk_sndbuf;
487 *rxbufsize = sock->sk->sk_rcvbuf;
491 EXPORT_SYMBOL(lnet_sock_getbuf);
494 lnet_sock_listen(struct socket **sockp, __u32 local_ip, int local_port,
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);
518 lnet_sock_accept(struct socket **newsockp, struct socket *sock)
520 wait_queue_entry_t wait;
521 struct socket *newsock;
525 * XXX this should add a ref to sock->ops->owner, if
526 * TCP could be a module
528 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
530 CERROR("Can't allocate socket\n");
534 newsock->ops = sock->ops;
536 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
538 /* Nothing ready, so wait for activity */
539 init_waitqueue_entry(&wait, current);
540 add_wait_queue(sk_sleep(sock->sk), &wait);
541 set_current_state(TASK_INTERRUPTIBLE);
543 remove_wait_queue(sk_sleep(sock->sk), &wait);
544 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
554 sock_release(newsock);
559 lnet_sock_connect(struct socket **sockp, int *fatal, __u32 local_ip,
560 int local_port, __u32 peer_ip, int peer_port)
562 struct sockaddr_in srvaddr;
565 rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
569 memset(&srvaddr, 0, sizeof(srvaddr));
570 srvaddr.sin_family = AF_INET;
571 srvaddr.sin_port = htons(peer_port);
572 srvaddr.sin_addr.s_addr = htonl(peer_ip);
574 rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
580 * EADDRNOTAVAIL probably means we're already connected to the same
581 * peer/port on the same local port on a differently typed
582 * connection. Let our caller retry with a different local
585 *fatal = !(rc == -EADDRNOTAVAIL);
587 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
588 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
589 &local_ip, local_port, &peer_ip, peer_port);
591 sock_release(*sockp);