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 if (strlen(name) > sizeof(ifr.ifr_name)-1)
126 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
128 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
131 CERROR("Can't get flags for interface %s\n", name);
135 if ((ifr.ifr_flags & IFF_UP) == 0) {
136 CDEBUG(D_NET, "Interface %s down\n", name);
144 if (strlen(name) > sizeof(ifr.ifr_name)-1)
146 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
148 ifr.ifr_addr.sa_family = AF_INET;
149 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
152 CERROR("Can't get IP address for interface %s\n", name);
156 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
159 if (strlen(name) > sizeof(ifr.ifr_name)-1)
161 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
163 ifr.ifr_addr.sa_family = AF_INET;
164 rc = libcfs_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
167 CERROR("Can't get netmask for interface %s\n", name);
171 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
177 EXPORT_SYMBOL(libcfs_ipif_query);
180 libcfs_ipif_enumerate (char ***namesp)
182 /* Allocate and fill in 'names', returning # interfaces/error */
194 nalloc = 16; /* first guess at max interfaces */
197 if (nalloc * sizeof(*ifr) > PAGE_CACHE_SIZE) {
199 nalloc = PAGE_CACHE_SIZE/sizeof(*ifr);
200 CWARN("Too many interfaces: only enumerating first %d\n",
204 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
206 CERROR ("ENOMEM enumerating up to %d interfaces\n", nalloc);
211 ifc.ifc_buf = (char *)ifr;
212 ifc.ifc_len = nalloc * sizeof(*ifr);
214 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
217 CERROR ("Error %d enumerating interfaces\n", rc);
223 nfound = ifc.ifc_len/sizeof(*ifr);
224 LASSERT (nfound <= nalloc);
226 if (nfound < nalloc || toobig)
229 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
236 LIBCFS_ALLOC(names, nfound * sizeof(*names));
241 /* NULL out all names[i] */
242 memset (names, 0, nfound * sizeof(*names));
244 for (i = 0; i < nfound; i++) {
246 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
247 if (nob == IFNAMSIZ) {
248 /* no space for terminating NULL */
249 CERROR("interface name %.*s too long (%d max)\n",
250 nob, ifr[i].ifr_name, IFNAMSIZ);
255 LIBCFS_ALLOC(names[i], IFNAMSIZ);
256 if (names[i] == NULL) {
261 memcpy(names[i], ifr[i].ifr_name, nob);
270 libcfs_ipif_free_enumeration(names, nfound);
272 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
277 EXPORT_SYMBOL(libcfs_ipif_enumerate);
280 libcfs_ipif_free_enumeration (char **names, int n)
286 for (i = 0; i < n && names[i] != NULL; i++)
287 LIBCFS_FREE(names[i], IFNAMSIZ);
289 LIBCFS_FREE(names, n * sizeof(*names));
292 EXPORT_SYMBOL(libcfs_ipif_free_enumeration);
295 libcfs_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
298 mm_segment_t oldmm = get_fs();
299 long ticks = timeout * HZ;
304 /* Caller may pass a zero timeout if she thinks the socket buffer is
305 * empty enough to take the whole message immediately */
312 struct msghdr msg = {
319 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
323 /* Set send timeout to remaining time */
324 tv = (struct timeval) {
325 .tv_sec = ticks / HZ,
326 .tv_usec = ((ticks % HZ) * 1000000) / HZ
329 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
330 (char *)&tv, sizeof(tv));
333 CERROR("Can't set socket send timeout "
335 (long)tv.tv_sec, (int)tv.tv_usec, rc);
342 rc = sock_sendmsg (sock, &msg, iov.iov_len);
343 ticks -= jiffies - then;
353 CERROR ("Unexpected zero rc\n");
354 return (-ECONNABORTED);
360 buffer = ((char *)buffer) + rc;
366 EXPORT_SYMBOL(libcfs_sock_write);
369 libcfs_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
372 mm_segment_t oldmm = get_fs();
373 long ticks = timeout * HZ;
385 struct msghdr msg = {
395 /* Set receive timeout to remaining time */
396 tv = (struct timeval) {
397 .tv_sec = ticks / HZ,
398 .tv_usec = ((ticks % HZ) * 1000000) / HZ
401 rc = sock_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
402 (char *)&tv, sizeof(tv));
405 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
406 (long)tv.tv_sec, (int)tv.tv_usec, rc);
412 rc = sock_recvmsg(sock, &msg, iov.iov_len, 0);
413 ticks -= jiffies - then;
422 buffer = ((char *)buffer) + rc;
433 EXPORT_SYMBOL(libcfs_sock_read);
436 libcfs_sock_create (struct socket **sockp, int *fatal,
437 __u32 local_ip, int local_port)
439 struct sockaddr_in locaddr;
443 mm_segment_t oldmm = get_fs();
445 /* All errors are fatal except bind failure if the port is in use */
448 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
451 CERROR ("Can't create socket: %d\n", rc);
457 rc = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
458 (char *)&option, sizeof (option));
461 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
465 if (local_ip != 0 || local_port != 0) {
466 memset(&locaddr, 0, sizeof(locaddr));
467 locaddr.sin_family = AF_INET;
468 locaddr.sin_port = htons(local_port);
469 locaddr.sin_addr.s_addr = (local_ip == 0) ?
470 INADDR_ANY : htonl(local_ip);
472 rc = sock->ops->bind(sock, (struct sockaddr *)&locaddr,
474 if (rc == -EADDRINUSE) {
475 CDEBUG(D_NET, "Port %d already in use\n", local_port);
480 CERROR("Error trying to bind to port %d: %d\n",
494 libcfs_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
496 mm_segment_t oldmm = get_fs();
500 if (txbufsize != 0) {
503 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
504 (char *)&option, sizeof (option));
507 CERROR ("Can't set send buffer %d: %d\n",
513 if (rxbufsize != 0) {
516 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
517 (char *)&option, sizeof (option));
520 CERROR ("Can't set receive buffer %d: %d\n",
529 EXPORT_SYMBOL(libcfs_sock_setbuf);
532 libcfs_sock_getaddr (struct socket *sock, int remote, __u32 *ip, int *port)
534 struct sockaddr_in sin;
535 int len = sizeof (sin);
538 rc = sock->ops->getname (sock, (struct sockaddr *)&sin, &len,
541 CERROR ("Error %d getting sock %s IP/port\n",
542 rc, remote ? "peer" : "local");
547 *ip = ntohl (sin.sin_addr.s_addr);
550 *port = ntohs (sin.sin_port);
555 EXPORT_SYMBOL(libcfs_sock_getaddr);
558 libcfs_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
561 if (txbufsize != NULL) {
562 *txbufsize = sock->sk->sk_sndbuf;
565 if (rxbufsize != NULL) {
566 *rxbufsize = sock->sk->sk_rcvbuf;
572 EXPORT_SYMBOL(libcfs_sock_getbuf);
575 libcfs_sock_listen (struct socket **sockp,
576 __u32 local_ip, int local_port, int backlog)
581 rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
584 CERROR("Can't create socket: port %d already in use\n",
589 rc = (*sockp)->ops->listen(*sockp, backlog);
593 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
594 sock_release(*sockp);
598 EXPORT_SYMBOL(libcfs_sock_listen);
601 libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
604 struct socket *newsock;
607 init_waitqueue_entry(&wait, current);
609 /* XXX this should add a ref to sock->ops->owner, if
610 * TCP could be a module */
611 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
613 CERROR("Can't allocate socket\n");
617 newsock->ops = sock->ops;
619 set_current_state(TASK_INTERRUPTIBLE);
620 add_wait_queue(sk_sleep(sock->sk), &wait);
622 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
624 /* Nothing ready, so wait for activity */
626 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
629 remove_wait_queue(sk_sleep(sock->sk), &wait);
630 set_current_state(TASK_RUNNING);
639 sock_release(newsock);
643 EXPORT_SYMBOL(libcfs_sock_accept);
646 libcfs_sock_abort_accept (struct socket *sock)
648 wake_up_all(sk_sleep(sock->sk));
651 EXPORT_SYMBOL(libcfs_sock_abort_accept);
654 libcfs_sock_connect (struct socket **sockp, int *fatal,
655 __u32 local_ip, int local_port,
656 __u32 peer_ip, int peer_port)
658 struct sockaddr_in srvaddr;
661 rc = libcfs_sock_create(sockp, fatal, local_ip, local_port);
665 memset (&srvaddr, 0, sizeof (srvaddr));
666 srvaddr.sin_family = AF_INET;
667 srvaddr.sin_port = htons(peer_port);
668 srvaddr.sin_addr.s_addr = htonl(peer_ip);
670 rc = (*sockp)->ops->connect(*sockp,
671 (struct sockaddr *)&srvaddr, sizeof(srvaddr),
676 /* EADDRNOTAVAIL probably means we're already connected to the same
677 * peer/port on the same local port on a differently typed
678 * connection. Let our caller retry with a different local
680 *fatal = !(rc == -EADDRNOTAVAIL);
682 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
683 "Error %d connecting %u.%u.%u.%u/%d -> %u.%u.%u.%u/%d\n", rc,
684 HIPQUAD(local_ip), local_port, HIPQUAD(peer_ip), peer_port);
686 sock_release(*sockp);
690 EXPORT_SYMBOL(libcfs_sock_connect);
693 libcfs_sock_release (struct socket *sock)
698 EXPORT_SYMBOL(libcfs_sock_release);