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 <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 static inline wait_queue_head_t *sk_sleep(struct sock *sk)
55 libcfs_sock_ioctl(int cmd, unsigned long arg)
57 mm_segment_t oldmm = get_fs();
61 struct file *sock_filp;
63 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
65 CERROR ("Can't create socket: %d\n", rc);
69 #if !defined(HAVE_SOCK_ALLOC_FILE) && !defined(HAVE_SOCK_ALLOC_FILE_3ARGS)
70 # ifdef HAVE_SOCK_MAP_FD_2ARG
71 fd = sock_map_fd(sock,0);
73 fd = sock_map_fd(sock);
82 # ifdef HAVE_SOCK_ALLOC_FILE_3ARGS
83 sock_filp = sock_alloc_file(sock, 0, NULL);
85 sock_filp = sock_alloc_file(sock, 0);
95 if (sock_filp->f_op->unlocked_ioctl)
96 rc = sock_filp->f_op->unlocked_ioctl(sock_filp, cmd, arg);
109 libcfs_ipif_query (char *name, int *up, __u32 *ip, __u32 *mask)
116 nob = strnlen(name, IFNAMSIZ);
117 if (nob == IFNAMSIZ) {
118 CERROR("Interface name %s too long\n", name);
122 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
124 strcpy(ifr.ifr_name, name);
125 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
128 CERROR("Can't get flags for interface %s\n", name);
132 if ((ifr.ifr_flags & IFF_UP) == 0) {
133 CDEBUG(D_NET, "Interface %s down\n", name);
141 strcpy(ifr.ifr_name, name);
142 ifr.ifr_addr.sa_family = AF_INET;
143 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
146 CERROR("Can't get IP address for interface %s\n", name);
150 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
153 strcpy(ifr.ifr_name, name);
154 ifr.ifr_addr.sa_family = AF_INET;
155 rc = libcfs_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
158 CERROR("Can't get netmask for interface %s\n", name);
162 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
168 EXPORT_SYMBOL(libcfs_ipif_query);
171 libcfs_ipif_enumerate (char ***namesp)
173 /* Allocate and fill in 'names', returning # interfaces/error */
185 nalloc = 16; /* first guess at max interfaces */
188 if (nalloc * sizeof(*ifr) > PAGE_CACHE_SIZE) {
190 nalloc = PAGE_CACHE_SIZE/sizeof(*ifr);
191 CWARN("Too many interfaces: only enumerating first %d\n",
195 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
197 CERROR ("ENOMEM enumerating up to %d interfaces\n", nalloc);
202 ifc.ifc_buf = (char *)ifr;
203 ifc.ifc_len = nalloc * sizeof(*ifr);
205 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
208 CERROR ("Error %d enumerating interfaces\n", rc);
214 nfound = ifc.ifc_len/sizeof(*ifr);
215 LASSERT (nfound <= nalloc);
217 if (nfound < nalloc || toobig)
220 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
227 LIBCFS_ALLOC(names, nfound * sizeof(*names));
232 /* NULL out all names[i] */
233 memset (names, 0, nfound * sizeof(*names));
235 for (i = 0; i < nfound; i++) {
237 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
238 if (nob == IFNAMSIZ) {
239 /* no space for terminating NULL */
240 CERROR("interface name %.*s too long (%d max)\n",
241 nob, ifr[i].ifr_name, IFNAMSIZ);
246 LIBCFS_ALLOC(names[i], IFNAMSIZ);
247 if (names[i] == NULL) {
252 memcpy(names[i], ifr[i].ifr_name, nob);
261 libcfs_ipif_free_enumeration(names, nfound);
263 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
268 EXPORT_SYMBOL(libcfs_ipif_enumerate);
271 libcfs_ipif_free_enumeration (char **names, int n)
277 for (i = 0; i < n && names[i] != NULL; i++)
278 LIBCFS_FREE(names[i], IFNAMSIZ);
280 LIBCFS_FREE(names, n * sizeof(*names));
283 EXPORT_SYMBOL(libcfs_ipif_free_enumeration);
286 libcfs_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
289 mm_segment_t oldmm = get_fs();
290 long ticks = timeout * HZ;
295 /* Caller may pass a zero timeout if she thinks the socket buffer is
296 * empty enough to take the whole message immediately */
303 struct msghdr msg = {
310 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
314 /* Set send timeout to remaining time */
315 tv = (struct timeval) {
316 .tv_sec = ticks / HZ,
317 .tv_usec = ((ticks % HZ) * 1000000) / HZ
320 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
321 (char *)&tv, sizeof(tv));
324 CERROR("Can't set socket send timeout "
326 (long)tv.tv_sec, (int)tv.tv_usec, rc);
333 rc = sock_sendmsg (sock, &msg, iov.iov_len);
334 ticks -= jiffies - then;
344 CERROR ("Unexpected zero rc\n");
345 return (-ECONNABORTED);
351 buffer = ((char *)buffer) + rc;
357 EXPORT_SYMBOL(libcfs_sock_write);
360 libcfs_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
363 mm_segment_t oldmm = get_fs();
364 long ticks = timeout * HZ;
376 struct msghdr msg = {
386 /* Set receive timeout to remaining time */
387 tv = (struct timeval) {
388 .tv_sec = ticks / HZ,
389 .tv_usec = ((ticks % HZ) * 1000000) / HZ
392 rc = sock_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
393 (char *)&tv, sizeof(tv));
396 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
397 (long)tv.tv_sec, (int)tv.tv_usec, rc);
403 rc = sock_recvmsg(sock, &msg, iov.iov_len, 0);
404 ticks -= jiffies - then;
413 buffer = ((char *)buffer) + rc;
424 EXPORT_SYMBOL(libcfs_sock_read);
427 libcfs_sock_create (struct socket **sockp, int *fatal,
428 __u32 local_ip, int local_port)
430 struct sockaddr_in locaddr;
434 mm_segment_t oldmm = get_fs();
436 /* All errors are fatal except bind failure if the port is in use */
439 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
442 CERROR ("Can't create socket: %d\n", rc);
448 rc = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
449 (char *)&option, sizeof (option));
452 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
456 if (local_ip != 0 || local_port != 0) {
457 memset(&locaddr, 0, sizeof(locaddr));
458 locaddr.sin_family = AF_INET;
459 locaddr.sin_port = htons(local_port);
460 locaddr.sin_addr.s_addr = (local_ip == 0) ?
461 INADDR_ANY : htonl(local_ip);
463 rc = sock->ops->bind(sock, (struct sockaddr *)&locaddr,
465 if (rc == -EADDRINUSE) {
466 CDEBUG(D_NET, "Port %d already in use\n", local_port);
471 CERROR("Error trying to bind to port %d: %d\n",
485 libcfs_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
487 mm_segment_t oldmm = get_fs();
491 if (txbufsize != 0) {
494 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
495 (char *)&option, sizeof (option));
498 CERROR ("Can't set send buffer %d: %d\n",
504 if (rxbufsize != 0) {
507 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
508 (char *)&option, sizeof (option));
511 CERROR ("Can't set receive buffer %d: %d\n",
520 EXPORT_SYMBOL(libcfs_sock_setbuf);
523 libcfs_sock_getaddr (struct socket *sock, int remote, __u32 *ip, int *port)
525 struct sockaddr_in sin;
526 int len = sizeof (sin);
529 rc = sock->ops->getname (sock, (struct sockaddr *)&sin, &len,
532 CERROR ("Error %d getting sock %s IP/port\n",
533 rc, remote ? "peer" : "local");
538 *ip = ntohl (sin.sin_addr.s_addr);
541 *port = ntohs (sin.sin_port);
546 EXPORT_SYMBOL(libcfs_sock_getaddr);
549 libcfs_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
552 if (txbufsize != NULL) {
553 *txbufsize = sock->sk->sk_sndbuf;
556 if (rxbufsize != NULL) {
557 *rxbufsize = sock->sk->sk_rcvbuf;
563 EXPORT_SYMBOL(libcfs_sock_getbuf);
566 libcfs_sock_listen (struct socket **sockp,
567 __u32 local_ip, int local_port, int backlog)
572 rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
575 CERROR("Can't create socket: port %d already in use\n",
580 rc = (*sockp)->ops->listen(*sockp, backlog);
584 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
585 sock_release(*sockp);
589 EXPORT_SYMBOL(libcfs_sock_listen);
592 libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
595 struct socket *newsock;
598 init_waitqueue_entry(&wait, current);
600 /* XXX this should add a ref to sock->ops->owner, if
601 * TCP could be a module */
602 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
604 CERROR("Can't allocate socket\n");
608 newsock->ops = sock->ops;
610 set_current_state(TASK_INTERRUPTIBLE);
611 add_wait_queue(sk_sleep(sock->sk), &wait);
613 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
615 /* Nothing ready, so wait for activity */
617 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
620 remove_wait_queue(sk_sleep(sock->sk), &wait);
621 set_current_state(TASK_RUNNING);
630 sock_release(newsock);
634 EXPORT_SYMBOL(libcfs_sock_accept);
637 libcfs_sock_abort_accept (struct socket *sock)
639 wake_up_all(sk_sleep(sock->sk));
642 EXPORT_SYMBOL(libcfs_sock_abort_accept);
645 libcfs_sock_connect (struct socket **sockp, int *fatal,
646 __u32 local_ip, int local_port,
647 __u32 peer_ip, int peer_port)
649 struct sockaddr_in srvaddr;
652 rc = libcfs_sock_create(sockp, fatal, local_ip, local_port);
656 memset (&srvaddr, 0, sizeof (srvaddr));
657 srvaddr.sin_family = AF_INET;
658 srvaddr.sin_port = htons(peer_port);
659 srvaddr.sin_addr.s_addr = htonl(peer_ip);
661 rc = (*sockp)->ops->connect(*sockp,
662 (struct sockaddr *)&srvaddr, sizeof(srvaddr),
667 /* EADDRNOTAVAIL probably means we're already connected to the same
668 * peer/port on the same local port on a differently typed
669 * connection. Let our caller retry with a different local
671 *fatal = !(rc == -EADDRNOTAVAIL);
673 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
674 "Error %d connecting %u.%u.%u.%u/%d -> %u.%u.%u.%u/%d\n", rc,
675 HIPQUAD(local_ip), local_port, HIPQUAD(peer_ip), peer_port);
677 sock_release(*sockp);
681 EXPORT_SYMBOL(libcfs_sock_connect);
684 libcfs_sock_release (struct socket *sock)
689 EXPORT_SYMBOL(libcfs_sock_release);