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 <linux/libcfs/libcfs.h>
39 #include <linux/libcfs/libcfs.h>
43 #include <linux/file.h>
44 /* For sys_open & sys_close */
45 #include <linux/syscalls.h>
48 libcfs_sock_ioctl(int cmd, unsigned long arg)
50 mm_segment_t oldmm = get_fs();
54 struct file *sock_filp;
56 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
58 CERROR ("Can't create socket: %d\n", rc);
62 sock_filp = sock_alloc_file(sock, 0, NULL);
69 if (sock_filp->f_op->unlocked_ioctl)
70 rc = sock_filp->f_op->unlocked_ioctl(sock_filp, cmd, arg);
84 libcfs_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 strcpy(ifr.ifr_name, name);
100 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
103 CERROR("Can't get flags for interface %s\n", name);
107 if ((ifr.ifr_flags & IFF_UP) == 0) {
108 CDEBUG(D_NET, "Interface %s down\n", name);
116 strcpy(ifr.ifr_name, name);
117 ifr.ifr_addr.sa_family = AF_INET;
118 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
121 CERROR("Can't get IP address for interface %s\n", name);
125 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
128 strcpy(ifr.ifr_name, name);
129 ifr.ifr_addr.sa_family = AF_INET;
130 rc = libcfs_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
133 CERROR("Can't get netmask for interface %s\n", name);
137 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
143 EXPORT_SYMBOL(libcfs_ipif_query);
146 libcfs_ipif_enumerate (char ***namesp)
148 /* Allocate and fill in 'names', returning # interfaces/error */
160 nalloc = 16; /* first guess at max interfaces */
163 if (nalloc * sizeof(*ifr) > PAGE_CACHE_SIZE) {
165 nalloc = PAGE_CACHE_SIZE/sizeof(*ifr);
166 CWARN("Too many interfaces: only enumerating first %d\n",
170 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
172 CERROR ("ENOMEM enumerating up to %d interfaces\n", nalloc);
177 ifc.ifc_buf = (char *)ifr;
178 ifc.ifc_len = nalloc * sizeof(*ifr);
180 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
183 CERROR ("Error %d enumerating interfaces\n", rc);
189 nfound = ifc.ifc_len/sizeof(*ifr);
190 LASSERT (nfound <= nalloc);
192 if (nfound < nalloc || toobig)
195 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
202 LIBCFS_ALLOC(names, nfound * sizeof(*names));
207 /* NULL out all names[i] */
208 memset (names, 0, nfound * sizeof(*names));
210 for (i = 0; i < nfound; i++) {
212 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
213 if (nob == IFNAMSIZ) {
214 /* no space for terminating NULL */
215 CERROR("interface name %.*s too long (%d max)\n",
216 nob, ifr[i].ifr_name, IFNAMSIZ);
221 LIBCFS_ALLOC(names[i], IFNAMSIZ);
222 if (names[i] == NULL) {
227 memcpy(names[i], ifr[i].ifr_name, nob);
236 libcfs_ipif_free_enumeration(names, nfound);
238 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
243 EXPORT_SYMBOL(libcfs_ipif_enumerate);
246 libcfs_ipif_free_enumeration (char **names, int n)
252 for (i = 0; i < n && names[i] != NULL; i++)
253 LIBCFS_FREE(names[i], IFNAMSIZ);
255 LIBCFS_FREE(names, n * sizeof(*names));
258 EXPORT_SYMBOL(libcfs_ipif_free_enumeration);
261 libcfs_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
264 mm_segment_t oldmm = get_fs();
265 long ticks = timeout * HZ;
270 /* Caller may pass a zero timeout if she thinks the socket buffer is
271 * empty enough to take the whole message immediately */
278 struct msghdr msg = {
285 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
289 /* Set send timeout to remaining time */
290 tv = (struct timeval) {
291 .tv_sec = ticks / HZ,
292 .tv_usec = ((ticks % HZ) * 1000000) / HZ
295 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
296 (char *)&tv, sizeof(tv));
299 CERROR("Can't set socket send timeout "
301 (long)tv.tv_sec, (int)tv.tv_usec, rc);
308 rc = sock_sendmsg (sock, &msg, iov.iov_len);
309 ticks -= jiffies - then;
319 CERROR ("Unexpected zero rc\n");
320 return (-ECONNABORTED);
326 buffer = ((char *)buffer) + rc;
332 EXPORT_SYMBOL(libcfs_sock_write);
335 libcfs_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
338 mm_segment_t oldmm = get_fs();
339 long ticks = timeout * HZ;
351 struct msghdr msg = {
361 /* Set receive timeout to remaining time */
362 tv = (struct timeval) {
363 .tv_sec = ticks / HZ,
364 .tv_usec = ((ticks % HZ) * 1000000) / HZ
367 rc = sock_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
368 (char *)&tv, sizeof(tv));
371 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
372 (long)tv.tv_sec, (int)tv.tv_usec, rc);
378 rc = sock_recvmsg(sock, &msg, iov.iov_len, 0);
379 ticks -= jiffies - then;
388 buffer = ((char *)buffer) + rc;
399 EXPORT_SYMBOL(libcfs_sock_read);
402 libcfs_sock_create (struct socket **sockp, int *fatal,
403 __u32 local_ip, int local_port)
405 struct sockaddr_in locaddr;
409 mm_segment_t oldmm = get_fs();
411 /* All errors are fatal except bind failure if the port is in use */
414 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
417 CERROR ("Can't create socket: %d\n", rc);
423 rc = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
424 (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 = sock->ops->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",
460 libcfs_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
462 mm_segment_t oldmm = get_fs();
466 if (txbufsize != 0) {
469 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
470 (char *)&option, sizeof (option));
473 CERROR ("Can't set send buffer %d: %d\n",
479 if (rxbufsize != 0) {
482 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
483 (char *)&option, sizeof (option));
486 CERROR ("Can't set receive buffer %d: %d\n",
495 EXPORT_SYMBOL(libcfs_sock_setbuf);
498 libcfs_sock_getaddr (struct socket *sock, int remote, __u32 *ip, int *port)
500 struct sockaddr_in sin;
501 int len = sizeof (sin);
504 rc = sock->ops->getname (sock, (struct sockaddr *)&sin, &len,
507 CERROR ("Error %d getting sock %s IP/port\n",
508 rc, remote ? "peer" : "local");
513 *ip = ntohl (sin.sin_addr.s_addr);
516 *port = ntohs (sin.sin_port);
521 EXPORT_SYMBOL(libcfs_sock_getaddr);
524 libcfs_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
527 if (txbufsize != NULL) {
528 *txbufsize = sock->sk->sk_sndbuf;
531 if (rxbufsize != NULL) {
532 *rxbufsize = sock->sk->sk_rcvbuf;
538 EXPORT_SYMBOL(libcfs_sock_getbuf);
541 libcfs_sock_listen (struct socket **sockp,
542 __u32 local_ip, int local_port, int backlog)
547 rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
550 CERROR("Can't create socket: port %d already in use\n",
555 rc = (*sockp)->ops->listen(*sockp, backlog);
559 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
560 sock_release(*sockp);
564 EXPORT_SYMBOL(libcfs_sock_listen);
567 libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
570 struct socket *newsock;
573 init_waitqueue_entry(&wait, current);
575 /* XXX this should add a ref to sock->ops->owner, if
576 * TCP could be a module */
577 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
579 CERROR("Can't allocate socket\n");
583 newsock->ops = sock->ops;
585 set_current_state(TASK_INTERRUPTIBLE);
586 add_wait_queue(cfs_sk_sleep(sock->sk), &wait);
588 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
590 /* Nothing ready, so wait for activity */
592 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
595 remove_wait_queue(cfs_sk_sleep(sock->sk), &wait);
596 set_current_state(TASK_RUNNING);
605 sock_release(newsock);
609 EXPORT_SYMBOL(libcfs_sock_accept);
612 libcfs_sock_abort_accept (struct socket *sock)
614 wake_up_all(cfs_sk_sleep(sock->sk));
617 EXPORT_SYMBOL(libcfs_sock_abort_accept);
620 libcfs_sock_connect (struct socket **sockp, int *fatal,
621 __u32 local_ip, int local_port,
622 __u32 peer_ip, int peer_port)
624 struct sockaddr_in srvaddr;
627 rc = libcfs_sock_create(sockp, fatal, local_ip, local_port);
631 memset (&srvaddr, 0, sizeof (srvaddr));
632 srvaddr.sin_family = AF_INET;
633 srvaddr.sin_port = htons(peer_port);
634 srvaddr.sin_addr.s_addr = htonl(peer_ip);
636 rc = (*sockp)->ops->connect(*sockp,
637 (struct sockaddr *)&srvaddr, sizeof(srvaddr),
642 /* EADDRNOTAVAIL probably means we're already connected to the same
643 * peer/port on the same local port on a differently typed
644 * connection. Let our caller retry with a different local
646 *fatal = !(rc == -EADDRNOTAVAIL);
648 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
649 "Error %d connecting %u.%u.%u.%u/%d -> %u.%u.%u.%u/%d\n", rc,
650 HIPQUAD(local_ip), local_port, HIPQUAD(peer_ip), peer_port);
652 sock_release(*sockp);
656 EXPORT_SYMBOL(libcfs_sock_connect);
659 libcfs_sock_release (struct socket *sock)
664 EXPORT_SYMBOL(libcfs_sock_release);