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, 2016, 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 kernel_sock_unlocked_ioctl(struct file *filp, int cmd, unsigned long arg)
49 mm_segment_t oldfs = get_fs();
53 err = filp->f_op->unlocked_ioctl(filp, cmd, arg);
60 lnet_sock_ioctl(int cmd, unsigned long arg)
62 struct file *sock_filp;
67 rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
69 CERROR("Can't create socket: %d\n", rc);
73 #if !defined(HAVE_SOCK_ALLOC_FILE) && !defined(HAVE_SOCK_ALLOC_FILE_3ARGS)
74 fd = sock_map_fd(sock, 0);
82 # ifdef HAVE_SOCK_ALLOC_FILE_3ARGS
83 sock_filp = sock_alloc_file(sock, 0, NULL);
85 sock_filp = sock_alloc_file(sock, 0);
88 if (IS_ERR(sock_filp)) {
89 rc = PTR_ERR(sock_filp);
94 rc = kernel_sock_unlocked_ioctl(sock_filp, cmd, arg);
104 lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask)
111 nob = strnlen(name, IFNAMSIZ);
112 if (nob == IFNAMSIZ) {
113 CERROR("Interface name %s too long\n", name);
117 CLASSERT(sizeof(ifr.ifr_name) >= IFNAMSIZ);
119 if (strlen(name) > sizeof(ifr.ifr_name)-1)
121 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
123 rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
125 CERROR("Can't get flags for interface %s\n", name);
129 if ((ifr.ifr_flags & IFF_UP) == 0) {
130 CDEBUG(D_NET, "Interface %s down\n", name);
137 if (strlen(name) > sizeof(ifr.ifr_name)-1)
139 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
141 ifr.ifr_addr.sa_family = AF_INET;
142 rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
145 CERROR("Can't get IP address for interface %s\n", name);
149 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
152 if (strlen(name) > sizeof(ifr.ifr_name)-1)
154 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
156 ifr.ifr_addr.sa_family = AF_INET;
157 rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
159 CERROR("Can't get netmask for interface %s\n", name);
163 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
168 EXPORT_SYMBOL(lnet_ipif_query);
171 lnet_ipif_free_enumeration(char **names, int n)
177 for (i = 0; i < n && names[i] != NULL; i++)
178 LIBCFS_FREE(names[i], IFNAMSIZ);
180 LIBCFS_FREE(names, n * sizeof(*names));
182 EXPORT_SYMBOL(lnet_ipif_free_enumeration);
185 lnet_ipif_enumerate(char ***namesp)
187 /* Allocate and fill in 'names', returning # interfaces/error */
198 nalloc = 16; /* first guess at max interfaces */
201 if (nalloc * sizeof(*ifr) > PAGE_SIZE) {
203 nalloc = PAGE_SIZE / sizeof(*ifr);
204 CWARN("Too many interfaces: only enumerating "
205 "first %d\n", nalloc);
208 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
210 CERROR("ENOMEM enumerating up to %d interfaces\n",
216 ifc.ifc_buf = (char *)ifr;
217 ifc.ifc_len = nalloc * sizeof(*ifr);
219 rc = lnet_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
221 CERROR("Error %d enumerating interfaces\n", rc);
227 nfound = ifc.ifc_len/sizeof(*ifr);
228 LASSERT(nfound <= nalloc);
230 if (nfound < nalloc || toobig)
233 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
240 LIBCFS_ALLOC(names, nfound * sizeof(*names));
246 for (i = 0; i < nfound; i++) {
247 nob = strnlen(ifr[i].ifr_name, IFNAMSIZ);
248 if (nob == IFNAMSIZ) {
249 /* no space for terminating NULL */
250 CERROR("interface name %.*s too long (%d max)\n",
251 nob, ifr[i].ifr_name, IFNAMSIZ);
256 LIBCFS_ALLOC(names[i], IFNAMSIZ);
257 if (names[i] == NULL) {
262 memcpy(names[i], ifr[i].ifr_name, nob);
271 lnet_ipif_free_enumeration(names, nfound);
273 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
277 EXPORT_SYMBOL(lnet_ipif_enumerate);
280 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
283 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
288 /* Caller may pass a zero timeout if she thinks the socket buffer is
289 * empty enough to take the whole message immediately */
296 struct msghdr msg = {
297 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
301 /* Set send timeout to remaining time */
302 tv = (struct timeval) {
303 .tv_sec = jiffies_left /
304 msecs_to_jiffies(MSEC_PER_SEC),
305 .tv_usec = ((jiffies_left %
306 msecs_to_jiffies(MSEC_PER_SEC)) *
308 msecs_to_jiffies(MSEC_PER_SEC)
311 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
312 (char *)&tv, sizeof(tv));
314 CERROR("Can't set socket send timeout "
316 (long)tv.tv_sec, (int)tv.tv_usec, rc);
322 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
323 jiffies_left -= jiffies - then;
332 CERROR("Unexpected zero rc\n");
333 return -ECONNABORTED;
336 if (jiffies_left <= 0)
339 buffer = ((char *)buffer) + rc;
344 EXPORT_SYMBOL(lnet_sock_write);
347 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
350 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
355 LASSERT(jiffies_left > 0);
362 struct msghdr msg = {
366 /* Set receive timeout to remaining time */
367 tv = (struct timeval) {
368 .tv_sec = jiffies_left / msecs_to_jiffies(MSEC_PER_SEC),
369 .tv_usec = ((jiffies_left %
370 msecs_to_jiffies(MSEC_PER_SEC)) *
372 msecs_to_jiffies(MSEC_PER_SEC)
374 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
375 (char *)&tv, sizeof(tv));
377 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
378 (long)tv.tv_sec, (int)tv.tv_usec, rc);
383 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
384 jiffies_left -= jiffies - then;
392 buffer = ((char *)buffer) + rc;
398 if (jiffies_left <= 0)
402 EXPORT_SYMBOL(lnet_sock_read);
405 lnet_sock_create(struct socket **sockp, int *fatal,
406 __u32 local_ip, int local_port)
408 struct sockaddr_in locaddr;
413 /* All errors are fatal except bind failure if the port is in use */
416 rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
419 CERROR("Can't create socket: %d\n", rc);
424 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
425 (char *)&option, sizeof(option));
427 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
431 if (local_ip != 0 || local_port != 0) {
432 memset(&locaddr, 0, sizeof(locaddr));
433 locaddr.sin_family = AF_INET;
434 locaddr.sin_port = htons(local_port);
435 locaddr.sin_addr.s_addr = (local_ip == 0) ?
436 INADDR_ANY : htonl(local_ip);
438 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
440 if (rc == -EADDRINUSE) {
441 CDEBUG(D_NET, "Port %d already in use\n", local_port);
446 CERROR("Error trying to bind to port %d: %d\n",
459 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
464 if (txbufsize != 0) {
466 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
467 (char *)&option, sizeof(option));
469 CERROR("Can't set send buffer %d: %d\n",
475 if (rxbufsize != 0) {
477 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
478 (char *)&option, sizeof(option));
480 CERROR("Can't set receive buffer %d: %d\n",
487 EXPORT_SYMBOL(lnet_sock_setbuf);
490 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
492 struct sockaddr_in sin;
493 int len = sizeof(sin);
497 rc = kernel_getpeername(sock, (struct sockaddr *)&sin, &len);
499 rc = kernel_getsockname(sock, (struct sockaddr *)&sin, &len);
501 CERROR("Error %d getting sock %s IP/port\n",
502 rc, remote ? "peer" : "local");
507 *ip = ntohl(sin.sin_addr.s_addr);
510 *port = ntohs(sin.sin_port);
514 EXPORT_SYMBOL(lnet_sock_getaddr);
517 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
519 if (txbufsize != NULL)
520 *txbufsize = sock->sk->sk_sndbuf;
522 if (rxbufsize != NULL)
523 *rxbufsize = sock->sk->sk_rcvbuf;
527 EXPORT_SYMBOL(lnet_sock_getbuf);
530 lnet_sock_listen(struct socket **sockp,
531 __u32 local_ip, int local_port, int backlog)
536 rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
539 CERROR("Can't create socket: port %d already in use\n",
544 rc = kernel_listen(*sockp, backlog);
548 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
549 sock_release(*sockp);
553 #ifndef HAVE_SK_SLEEP
554 static inline wait_queue_head_t *sk_sleep(struct sock *sk)
561 lnet_sock_accept(struct socket **newsockp, struct socket *sock)
564 struct socket *newsock;
567 /* XXX this should add a ref to sock->ops->owner, if
568 * TCP could be a module */
569 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
571 CERROR("Can't allocate socket\n");
575 newsock->ops = sock->ops;
577 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
579 /* Nothing ready, so wait for activity */
580 init_waitqueue_entry(&wait, current);
581 add_wait_queue(sk_sleep(sock->sk), &wait);
582 set_current_state(TASK_INTERRUPTIBLE);
584 remove_wait_queue(sk_sleep(sock->sk), &wait);
585 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
595 sock_release(newsock);
600 lnet_sock_connect(struct socket **sockp, int *fatal,
601 __u32 local_ip, int local_port,
602 __u32 peer_ip, int peer_port)
604 struct sockaddr_in srvaddr;
607 rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
611 memset(&srvaddr, 0, sizeof(srvaddr));
612 srvaddr.sin_family = AF_INET;
613 srvaddr.sin_port = htons(peer_port);
614 srvaddr.sin_addr.s_addr = htonl(peer_ip);
616 rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
621 /* EADDRNOTAVAIL probably means we're already connected to the same
622 * peer/port on the same local port on a differently typed
623 * connection. Let our caller retry with a different local
625 *fatal = !(rc == -EADDRNOTAVAIL);
627 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
628 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
629 &local_ip, local_port, &peer_ip, peer_port);
631 sock_release(*sockp);