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, Whamcloud, Inc.
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 <libcfs/libcfs.h>
39 #include <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 #ifdef HAVE_SOCK_MAP_FD_2ARG
63 fd = sock_map_fd(sock,0);
65 fd = sock_map_fd(sock);
80 #ifdef HAVE_UNLOCKED_IOCTL
81 if (sock_filp->f_op->unlocked_ioctl)
82 rc = sock_filp->f_op->unlocked_ioctl(sock_filp, cmd, arg);
85 rc = sock_filp->f_op->ioctl(sock_filp->f_dentry->d_inode,
100 libcfs_ipif_query (char *name, int *up, __u32 *ip, __u32 *mask)
107 nob = strnlen(name, IFNAMSIZ);
108 if (nob == IFNAMSIZ) {
109 CERROR("Interface name %s too long\n", name);
113 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
115 strcpy(ifr.ifr_name, name);
116 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
119 CERROR("Can't get flags for interface %s\n", name);
123 if ((ifr.ifr_flags & IFF_UP) == 0) {
124 CDEBUG(D_NET, "Interface %s down\n", name);
132 strcpy(ifr.ifr_name, name);
133 ifr.ifr_addr.sa_family = AF_INET;
134 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
137 CERROR("Can't get IP address for interface %s\n", name);
141 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
144 strcpy(ifr.ifr_name, name);
145 ifr.ifr_addr.sa_family = AF_INET;
146 rc = libcfs_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
149 CERROR("Can't get netmask for interface %s\n", name);
153 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
159 EXPORT_SYMBOL(libcfs_ipif_query);
162 libcfs_ipif_enumerate (char ***namesp)
164 /* Allocate and fill in 'names', returning # interfaces/error */
176 nalloc = 16; /* first guess at max interfaces */
179 if (nalloc * sizeof(*ifr) > CFS_PAGE_SIZE) {
181 nalloc = CFS_PAGE_SIZE/sizeof(*ifr);
182 CWARN("Too many interfaces: only enumerating first %d\n",
186 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
188 CERROR ("ENOMEM enumerating up to %d interfaces\n", nalloc);
193 ifc.ifc_buf = (char *)ifr;
194 ifc.ifc_len = nalloc * sizeof(*ifr);
196 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
199 CERROR ("Error %d enumerating interfaces\n", rc);
205 nfound = ifc.ifc_len/sizeof(*ifr);
206 LASSERT (nfound <= nalloc);
208 if (nfound < nalloc || toobig)
211 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
218 LIBCFS_ALLOC(names, nfound * sizeof(*names));
223 /* NULL out all names[i] */
224 memset (names, 0, nfound * sizeof(*names));
226 for (i = 0; i < nfound; i++) {
228 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
229 if (nob == IFNAMSIZ) {
230 /* no space for terminating NULL */
231 CERROR("interface name %.*s too long (%d max)\n",
232 nob, ifr[i].ifr_name, IFNAMSIZ);
237 LIBCFS_ALLOC(names[i], IFNAMSIZ);
238 if (names[i] == NULL) {
243 memcpy(names[i], ifr[i].ifr_name, nob);
252 libcfs_ipif_free_enumeration(names, nfound);
254 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
259 EXPORT_SYMBOL(libcfs_ipif_enumerate);
262 libcfs_ipif_free_enumeration (char **names, int n)
268 for (i = 0; i < n && names[i] != NULL; i++)
269 LIBCFS_FREE(names[i], IFNAMSIZ);
271 LIBCFS_FREE(names, n * sizeof(*names));
274 EXPORT_SYMBOL(libcfs_ipif_free_enumeration);
277 libcfs_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
280 mm_segment_t oldmm = get_fs();
281 long ticks = timeout * HZ;
286 /* Caller may pass a zero timeout if she thinks the socket buffer is
287 * empty enough to take the whole message immediately */
294 struct msghdr msg = {
301 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
305 /* Set send timeout to remaining time */
306 tv = (struct timeval) {
307 .tv_sec = ticks / HZ,
308 .tv_usec = ((ticks % HZ) * 1000000) / HZ
311 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
312 (char *)&tv, sizeof(tv));
315 CERROR("Can't set socket send timeout "
317 (long)tv.tv_sec, (int)tv.tv_usec, rc);
324 rc = sock_sendmsg (sock, &msg, iov.iov_len);
325 ticks -= jiffies - then;
335 CERROR ("Unexpected zero rc\n");
336 return (-ECONNABORTED);
342 buffer = ((char *)buffer) + rc;
348 EXPORT_SYMBOL(libcfs_sock_write);
351 libcfs_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
354 mm_segment_t oldmm = get_fs();
355 long ticks = timeout * HZ;
367 struct msghdr msg = {
377 /* Set receive timeout to remaining time */
378 tv = (struct timeval) {
379 .tv_sec = ticks / HZ,
380 .tv_usec = ((ticks % HZ) * 1000000) / HZ
383 rc = sock_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
384 (char *)&tv, sizeof(tv));
387 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
388 (long)tv.tv_sec, (int)tv.tv_usec, rc);
394 rc = sock_recvmsg(sock, &msg, iov.iov_len, 0);
395 ticks -= jiffies - then;
404 buffer = ((char *)buffer) + rc;
415 EXPORT_SYMBOL(libcfs_sock_read);
418 libcfs_sock_create (struct socket **sockp, int *fatal,
419 __u32 local_ip, int local_port)
421 struct sockaddr_in locaddr;
425 mm_segment_t oldmm = get_fs();
427 /* All errors are fatal except bind failure if the port is in use */
430 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
433 CERROR ("Can't create socket: %d\n", rc);
439 rc = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
440 (char *)&option, sizeof (option));
443 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
447 if (local_ip != 0 || local_port != 0) {
448 memset(&locaddr, 0, sizeof(locaddr));
449 locaddr.sin_family = AF_INET;
450 locaddr.sin_port = htons(local_port);
451 locaddr.sin_addr.s_addr = (local_ip == 0) ?
452 INADDR_ANY : htonl(local_ip);
454 rc = sock->ops->bind(sock, (struct sockaddr *)&locaddr,
456 if (rc == -EADDRINUSE) {
457 CDEBUG(D_NET, "Port %d already in use\n", local_port);
462 CERROR("Error trying to bind to port %d: %d\n",
476 libcfs_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
478 mm_segment_t oldmm = get_fs();
482 if (txbufsize != 0) {
485 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
486 (char *)&option, sizeof (option));
489 CERROR ("Can't set send buffer %d: %d\n",
495 if (rxbufsize != 0) {
498 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
499 (char *)&option, sizeof (option));
502 CERROR ("Can't set receive buffer %d: %d\n",
511 EXPORT_SYMBOL(libcfs_sock_setbuf);
514 libcfs_sock_getaddr (struct socket *sock, int remote, __u32 *ip, int *port)
516 struct sockaddr_in sin;
517 int len = sizeof (sin);
520 rc = sock->ops->getname (sock, (struct sockaddr *)&sin, &len,
523 CERROR ("Error %d getting sock %s IP/port\n",
524 rc, remote ? "peer" : "local");
529 *ip = ntohl (sin.sin_addr.s_addr);
532 *port = ntohs (sin.sin_port);
537 EXPORT_SYMBOL(libcfs_sock_getaddr);
540 libcfs_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
543 if (txbufsize != NULL) {
544 *txbufsize = sock->sk->sk_sndbuf;
547 if (rxbufsize != NULL) {
548 *rxbufsize = sock->sk->sk_rcvbuf;
554 EXPORT_SYMBOL(libcfs_sock_getbuf);
557 libcfs_sock_listen (struct socket **sockp,
558 __u32 local_ip, int local_port, int backlog)
563 rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
566 CERROR("Can't create socket: port %d already in use\n",
571 rc = (*sockp)->ops->listen(*sockp, backlog);
575 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
576 sock_release(*sockp);
580 EXPORT_SYMBOL(libcfs_sock_listen);
582 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
583 int sock_create_lite(int family, int type, int protocol, struct socket **res)
599 libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
602 struct socket *newsock;
605 init_waitqueue_entry(&wait, current);
607 /* XXX this should add a ref to sock->ops->owner, if
608 * TCP could be a module */
609 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
611 CERROR("Can't allocate socket\n");
615 newsock->ops = sock->ops;
617 set_current_state(TASK_INTERRUPTIBLE);
618 add_wait_queue(sk_sleep(sock->sk), &wait);
620 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
622 /* Nothing ready, so wait for activity */
624 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
627 remove_wait_queue(sk_sleep(sock->sk), &wait);
628 set_current_state(TASK_RUNNING);
637 sock_release(newsock);
641 EXPORT_SYMBOL(libcfs_sock_accept);
644 libcfs_sock_abort_accept (struct socket *sock)
646 wake_up_all(sk_sleep(sock->sk));
649 EXPORT_SYMBOL(libcfs_sock_abort_accept);
652 libcfs_sock_connect (struct socket **sockp, int *fatal,
653 __u32 local_ip, int local_port,
654 __u32 peer_ip, int peer_port)
656 struct sockaddr_in srvaddr;
659 rc = libcfs_sock_create(sockp, fatal, local_ip, local_port);
663 memset (&srvaddr, 0, sizeof (srvaddr));
664 srvaddr.sin_family = AF_INET;
665 srvaddr.sin_port = htons(peer_port);
666 srvaddr.sin_addr.s_addr = htonl(peer_ip);
668 rc = (*sockp)->ops->connect(*sockp,
669 (struct sockaddr *)&srvaddr, sizeof(srvaddr),
674 /* EADDRNOTAVAIL probably means we're already connected to the same
675 * peer/port on the same local port on a differently typed
676 * connection. Let our caller retry with a different local
678 *fatal = !(rc == -EADDRNOTAVAIL);
680 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
681 "Error %d connecting %u.%u.%u.%u/%d -> %u.%u.%u.%u/%d\n", rc,
682 HIPQUAD(local_ip), local_port, HIPQUAD(peer_ip), peer_port);
684 sock_release(*sockp);
688 EXPORT_SYMBOL(libcfs_sock_connect);
691 libcfs_sock_release (struct socket *sock)
696 EXPORT_SYMBOL(libcfs_sock_release);