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, 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
38 #include "../../include/linux/libcfs/libcfs.h"
39 #include "../../include/linux/lnet/lib-lnet.h"
43 #include <linux/file.h>
44 /* For sys_open & sys_close */
45 #include <linux/syscalls.h>
48 lnet_sock_ioctl(int cmd, unsigned long arg)
50 mm_segment_t oldmm = get_fs();
53 struct file *sock_filp;
55 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
57 CERROR ("Can't create socket: %d\n", rc);
61 sock_filp = sock_alloc_file(sock, 0, NULL);
62 if (IS_ERR(sock_filp)) {
64 rc = PTR_ERR(sock_filp);
69 if (sock_filp->f_op->unlocked_ioctl)
70 rc = sock_filp->f_op->unlocked_ioctl(sock_filp, cmd, arg);
79 lnet_ipif_query (char *name, int *up, __u32 *ip, __u32 *mask)
86 nob = strnlen(name, IFNAMSIZ);
87 if (nob == IFNAMSIZ) {
88 CERROR("Interface name %s too long\n", name);
92 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
94 strcpy(ifr.ifr_name, name);
95 rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
97 CERROR("Can't get flags for interface %s\n", name);
101 if ((ifr.ifr_flags & IFF_UP) == 0) {
102 CDEBUG(D_NET, "Interface %s down\n", name);
110 strcpy(ifr.ifr_name, name);
111 ifr.ifr_addr.sa_family = AF_INET;
112 rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
114 CERROR("Can't get IP address for interface %s\n", name);
118 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
121 strcpy(ifr.ifr_name, name);
122 ifr.ifr_addr.sa_family = AF_INET;
123 rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
125 CERROR("Can't get netmask for interface %s\n", name);
129 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
135 EXPORT_SYMBOL(lnet_ipif_query);
138 lnet_ipif_enumerate (char ***namesp)
140 /* Allocate and fill in 'names', returning # interfaces/error */
152 nalloc = 16; /* first guess at max interfaces */
155 if (nalloc * sizeof(*ifr) > PAGE_CACHE_SIZE) {
157 nalloc = PAGE_CACHE_SIZE/sizeof(*ifr);
158 CWARN("Too many interfaces: only enumerating first %d\n",
162 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
164 CERROR ("ENOMEM enumerating up to %d interfaces\n", nalloc);
169 ifc.ifc_buf = (char *)ifr;
170 ifc.ifc_len = nalloc * sizeof(*ifr);
172 rc = lnet_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
174 CERROR ("Error %d enumerating interfaces\n", rc);
180 nfound = ifc.ifc_len/sizeof(*ifr);
181 LASSERT (nfound <= nalloc);
183 if (nfound < nalloc || toobig)
186 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
193 LIBCFS_ALLOC(names, nfound * sizeof(*names));
199 for (i = 0; i < nfound; i++) {
201 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
202 if (nob == IFNAMSIZ) {
203 /* no space for terminating NULL */
204 CERROR("interface name %.*s too long (%d max)\n",
205 nob, ifr[i].ifr_name, IFNAMSIZ);
210 LIBCFS_ALLOC(names[i], IFNAMSIZ);
211 if (names[i] == NULL) {
216 memcpy(names[i], ifr[i].ifr_name, nob);
225 lnet_ipif_free_enumeration(names, nfound);
227 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
232 EXPORT_SYMBOL(lnet_ipif_enumerate);
235 lnet_ipif_free_enumeration (char **names, int n)
241 for (i = 0; i < n && names[i] != NULL; i++)
242 LIBCFS_FREE(names[i], IFNAMSIZ);
244 LIBCFS_FREE(names, n * sizeof(*names));
247 EXPORT_SYMBOL(lnet_ipif_free_enumeration);
250 lnet_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
253 long ticks = timeout * HZ;
258 /* Caller may pass a zero timeout if she thinks the socket buffer is
259 * empty enough to take the whole message immediately */
266 struct msghdr msg = {
267 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
271 /* Set send timeout to remaining time */
272 tv = (struct timeval) {
273 .tv_sec = ticks / HZ,
274 .tv_usec = ((ticks % HZ) * 1000000) / HZ
276 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
277 (char *)&tv, sizeof(tv));
279 CERROR("Can't set socket send timeout %ld.%06d: %d\n",
280 (long)tv.tv_sec, (int)tv.tv_usec, rc);
286 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
287 ticks -= jiffies - then;
296 CERROR ("Unexpected zero rc\n");
297 return -ECONNABORTED;
303 buffer = ((char *)buffer) + rc;
309 EXPORT_SYMBOL(lnet_sock_write);
312 lnet_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
315 long ticks = timeout * HZ;
327 struct msghdr msg = {
331 /* Set receive timeout to remaining time */
332 tv = (struct timeval) {
333 .tv_sec = ticks / HZ,
334 .tv_usec = ((ticks % HZ) * 1000000) / HZ
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 ticks -= jiffies - then;
354 buffer = ((char *)buffer) + rc;
365 EXPORT_SYMBOL(lnet_sock_read);
368 lnet_sock_create (struct socket **sockp, int *fatal,
369 __u32 local_ip, int local_port)
371 struct sockaddr_in locaddr;
376 /* All errors are fatal except bind failure if the port is in use */
379 rc = sock_create (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 = sock->ops->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",
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",
453 EXPORT_SYMBOL(lnet_sock_setbuf);
456 lnet_sock_getaddr (struct socket *sock, bool remote, __u32 *ip, int *port)
458 struct sockaddr_in sin;
459 int len = sizeof (sin);
462 rc = sock->ops->getname (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);
479 EXPORT_SYMBOL(lnet_sock_getaddr);
482 lnet_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
485 if (txbufsize != NULL) {
486 *txbufsize = sock->sk->sk_sndbuf;
489 if (rxbufsize != NULL) {
490 *rxbufsize = sock->sk->sk_rcvbuf;
496 EXPORT_SYMBOL(lnet_sock_getbuf);
499 lnet_sock_listen (struct socket **sockp,
500 __u32 local_ip, int local_port, int backlog)
505 rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
508 CERROR("Can't create socket: port %d already in use\n",
513 rc = (*sockp)->ops->listen(*sockp, backlog);
517 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
518 sock_release(*sockp);
522 EXPORT_SYMBOL(lnet_sock_listen);
525 lnet_sock_accept (struct socket **newsockp, struct socket *sock)
528 struct socket *newsock;
531 init_waitqueue_entry(&wait, current);
533 /* XXX this should add a ref to sock->ops->owner, if
534 * TCP could be a module */
535 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
537 CERROR("Can't allocate socket\n");
541 newsock->ops = sock->ops;
543 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
545 /* Nothing ready, so wait for activity */
546 set_current_state(TASK_INTERRUPTIBLE);
547 add_wait_queue(sk_sleep(sock->sk), &wait);
549 remove_wait_queue(sk_sleep(sock->sk), &wait);
550 set_current_state(TASK_RUNNING);
551 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
561 sock_release(newsock);
565 EXPORT_SYMBOL(lnet_sock_accept);
568 lnet_sock_connect (struct socket **sockp, int *fatal,
569 __u32 local_ip, int local_port,
570 __u32 peer_ip, int peer_port)
572 struct sockaddr_in srvaddr;
575 rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
579 memset (&srvaddr, 0, sizeof (srvaddr));
580 srvaddr.sin_family = AF_INET;
581 srvaddr.sin_port = htons(peer_port);
582 srvaddr.sin_addr.s_addr = htonl(peer_ip);
584 rc = (*sockp)->ops->connect(*sockp,
585 (struct sockaddr *)&srvaddr, sizeof(srvaddr),
590 /* EADDRNOTAVAIL probably means we're already connected to the same
591 * peer/port on the same local port on a differently typed
592 * connection. Let our caller retry with a different local
594 *fatal = !(rc == -EADDRNOTAVAIL);
596 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
597 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
598 &local_ip, local_port, &peer_ip, peer_port);
600 sock_release(*sockp);
604 EXPORT_SYMBOL(lnet_sock_connect);