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 fd = sock_map_fd(sock, 0);
78 # ifdef HAVE_SOCK_ALLOC_FILE_3ARGS
79 sock_filp = sock_alloc_file(sock, 0, NULL);
81 sock_filp = sock_alloc_file(sock, 0);
91 if (sock_filp->f_op->unlocked_ioctl)
92 rc = sock_filp->f_op->unlocked_ioctl(sock_filp, cmd, arg);
105 libcfs_ipif_query (char *name, int *up, __u32 *ip, __u32 *mask)
112 nob = strnlen(name, IFNAMSIZ);
113 if (nob == IFNAMSIZ) {
114 CERROR("Interface name %s too long\n", name);
118 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
120 if (strlen(name) > sizeof(ifr.ifr_name)-1)
122 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
124 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
127 CERROR("Can't get flags for interface %s\n", name);
131 if ((ifr.ifr_flags & IFF_UP) == 0) {
132 CDEBUG(D_NET, "Interface %s down\n", name);
140 if (strlen(name) > sizeof(ifr.ifr_name)-1)
142 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
144 ifr.ifr_addr.sa_family = AF_INET;
145 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
148 CERROR("Can't get IP address for interface %s\n", name);
152 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
155 if (strlen(name) > sizeof(ifr.ifr_name)-1)
157 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
159 ifr.ifr_addr.sa_family = AF_INET;
160 rc = libcfs_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
163 CERROR("Can't get netmask for interface %s\n", name);
167 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
173 EXPORT_SYMBOL(libcfs_ipif_query);
176 libcfs_ipif_enumerate (char ***namesp)
178 /* Allocate and fill in 'names', returning # interfaces/error */
190 nalloc = 16; /* first guess at max interfaces */
193 if (nalloc * sizeof(*ifr) > PAGE_CACHE_SIZE) {
195 nalloc = PAGE_CACHE_SIZE/sizeof(*ifr);
196 CWARN("Too many interfaces: only enumerating first %d\n",
200 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
202 CERROR ("ENOMEM enumerating up to %d interfaces\n", nalloc);
207 ifc.ifc_buf = (char *)ifr;
208 ifc.ifc_len = nalloc * sizeof(*ifr);
210 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
213 CERROR ("Error %d enumerating interfaces\n", rc);
219 nfound = ifc.ifc_len/sizeof(*ifr);
220 LASSERT (nfound <= nalloc);
222 if (nfound < nalloc || toobig)
225 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
232 LIBCFS_ALLOC(names, nfound * sizeof(*names));
237 /* NULL out all names[i] */
238 memset (names, 0, nfound * sizeof(*names));
240 for (i = 0; i < nfound; i++) {
242 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
243 if (nob == IFNAMSIZ) {
244 /* no space for terminating NULL */
245 CERROR("interface name %.*s too long (%d max)\n",
246 nob, ifr[i].ifr_name, IFNAMSIZ);
251 LIBCFS_ALLOC(names[i], IFNAMSIZ);
252 if (names[i] == NULL) {
257 memcpy(names[i], ifr[i].ifr_name, nob);
266 libcfs_ipif_free_enumeration(names, nfound);
268 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
273 EXPORT_SYMBOL(libcfs_ipif_enumerate);
276 libcfs_ipif_free_enumeration (char **names, int n)
282 for (i = 0; i < n && names[i] != NULL; i++)
283 LIBCFS_FREE(names[i], IFNAMSIZ);
285 LIBCFS_FREE(names, n * sizeof(*names));
288 EXPORT_SYMBOL(libcfs_ipif_free_enumeration);
291 libcfs_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
294 mm_segment_t oldmm = get_fs();
295 long ticks = timeout * HZ;
300 /* Caller may pass a zero timeout if she thinks the socket buffer is
301 * empty enough to take the whole message immediately */
308 struct msghdr msg = {
309 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
313 /* Set send timeout to remaining time */
314 tv = (struct timeval) {
315 .tv_sec = ticks / HZ,
316 .tv_usec = ((ticks % HZ) * 1000000) / HZ
319 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
320 (char *)&tv, sizeof(tv));
323 CERROR("Can't set socket send timeout "
325 (long)tv.tv_sec, (int)tv.tv_usec, rc);
331 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
332 ticks -= jiffies - then;
341 CERROR ("Unexpected zero rc\n");
342 return (-ECONNABORTED);
348 buffer = ((char *)buffer) + rc;
354 EXPORT_SYMBOL(libcfs_sock_write);
357 libcfs_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
360 mm_segment_t oldmm = get_fs();
361 long ticks = timeout * HZ;
373 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);
393 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
394 ticks -= jiffies - then;
402 buffer = ((char *)buffer) + rc;
413 EXPORT_SYMBOL(libcfs_sock_read);
416 libcfs_sock_create (struct socket **sockp, int *fatal,
417 __u32 local_ip, int local_port)
419 struct sockaddr_in locaddr;
423 mm_segment_t oldmm = get_fs();
425 /* All errors are fatal except bind failure if the port is in use */
428 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
431 CERROR ("Can't create socket: %d\n", rc);
437 rc = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
438 (char *)&option, sizeof (option));
441 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
445 if (local_ip != 0 || local_port != 0) {
446 memset(&locaddr, 0, sizeof(locaddr));
447 locaddr.sin_family = AF_INET;
448 locaddr.sin_port = htons(local_port);
449 locaddr.sin_addr.s_addr = (local_ip == 0) ?
450 INADDR_ANY : htonl(local_ip);
452 rc = sock->ops->bind(sock, (struct sockaddr *)&locaddr,
454 if (rc == -EADDRINUSE) {
455 CDEBUG(D_NET, "Port %d already in use\n", local_port);
460 CERROR("Error trying to bind to port %d: %d\n",
474 libcfs_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
476 mm_segment_t oldmm = get_fs();
480 if (txbufsize != 0) {
483 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
484 (char *)&option, sizeof (option));
487 CERROR ("Can't set send buffer %d: %d\n",
493 if (rxbufsize != 0) {
496 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
497 (char *)&option, sizeof (option));
500 CERROR ("Can't set receive buffer %d: %d\n",
509 EXPORT_SYMBOL(libcfs_sock_setbuf);
512 libcfs_sock_getaddr (struct socket *sock, int remote, __u32 *ip, int *port)
514 struct sockaddr_in sin;
515 int len = sizeof (sin);
518 rc = sock->ops->getname (sock, (struct sockaddr *)&sin, &len,
521 CERROR ("Error %d getting sock %s IP/port\n",
522 rc, remote ? "peer" : "local");
527 *ip = ntohl (sin.sin_addr.s_addr);
530 *port = ntohs (sin.sin_port);
535 EXPORT_SYMBOL(libcfs_sock_getaddr);
538 libcfs_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
541 if (txbufsize != NULL) {
542 *txbufsize = sock->sk->sk_sndbuf;
545 if (rxbufsize != NULL) {
546 *rxbufsize = sock->sk->sk_rcvbuf;
552 EXPORT_SYMBOL(libcfs_sock_getbuf);
555 libcfs_sock_listen (struct socket **sockp,
556 __u32 local_ip, int local_port, int backlog)
561 rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
564 CERROR("Can't create socket: port %d already in use\n",
569 rc = (*sockp)->ops->listen(*sockp, backlog);
573 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
574 sock_release(*sockp);
578 EXPORT_SYMBOL(libcfs_sock_listen);
581 libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
584 struct socket *newsock;
587 init_waitqueue_entry(&wait, current);
589 /* XXX this should add a ref to sock->ops->owner, if
590 * TCP could be a module */
591 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
593 CERROR("Can't allocate socket\n");
597 newsock->ops = sock->ops;
599 set_current_state(TASK_INTERRUPTIBLE);
600 add_wait_queue(sk_sleep(sock->sk), &wait);
602 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
604 /* Nothing ready, so wait for activity */
606 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
609 remove_wait_queue(sk_sleep(sock->sk), &wait);
610 set_current_state(TASK_RUNNING);
619 sock_release(newsock);
623 EXPORT_SYMBOL(libcfs_sock_accept);
626 libcfs_sock_abort_accept (struct socket *sock)
628 wake_up_all(sk_sleep(sock->sk));
631 EXPORT_SYMBOL(libcfs_sock_abort_accept);
634 libcfs_sock_connect (struct socket **sockp, int *fatal,
635 __u32 local_ip, int local_port,
636 __u32 peer_ip, int peer_port)
638 struct sockaddr_in srvaddr;
641 rc = libcfs_sock_create(sockp, fatal, local_ip, local_port);
645 memset (&srvaddr, 0, sizeof (srvaddr));
646 srvaddr.sin_family = AF_INET;
647 srvaddr.sin_port = htons(peer_port);
648 srvaddr.sin_addr.s_addr = htonl(peer_ip);
650 rc = (*sockp)->ops->connect(*sockp,
651 (struct sockaddr *)&srvaddr, sizeof(srvaddr),
656 /* EADDRNOTAVAIL probably means we're already connected to the same
657 * peer/port on the same local port on a differently typed
658 * connection. Let our caller retry with a different local
660 *fatal = !(rc == -EADDRNOTAVAIL);
662 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
663 "Error %d connecting %u.%u.%u.%u/%d -> %u.%u.%u.%u/%d\n", rc,
664 HIPQUAD(local_ip), local_port, HIPQUAD(peer_ip), peer_port);
666 sock_release(*sockp);
670 EXPORT_SYMBOL(libcfs_sock_connect);
673 libcfs_sock_release (struct socket *sock)
678 EXPORT_SYMBOL(libcfs_sock_release);