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();
53 struct file *sock_filp;
55 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
57 CERROR ("Can't create socket: %d\n", rc);
61 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);
79 libcfs_ipif_query (char *name, int *up, __u32 *ip, __u32 *mask)
86 nob = strnlen(name, IFNAMSIZ);
87 if (nob == IFNAMSIZ) {
88 CERROR("Interface name %s too long\n", name);
92 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
94 strcpy(ifr.ifr_name, name);
95 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
98 CERROR("Can't get flags for interface %s\n", name);
102 if ((ifr.ifr_flags & IFF_UP) == 0) {
103 CDEBUG(D_NET, "Interface %s down\n", name);
111 strcpy(ifr.ifr_name, name);
112 ifr.ifr_addr.sa_family = AF_INET;
113 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
116 CERROR("Can't get IP address for interface %s\n", name);
120 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
123 strcpy(ifr.ifr_name, name);
124 ifr.ifr_addr.sa_family = AF_INET;
125 rc = libcfs_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
128 CERROR("Can't get netmask for interface %s\n", name);
132 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
138 EXPORT_SYMBOL(libcfs_ipif_query);
141 libcfs_ipif_enumerate (char ***namesp)
143 /* Allocate and fill in 'names', returning # interfaces/error */
155 nalloc = 16; /* first guess at max interfaces */
158 if (nalloc * sizeof(*ifr) > PAGE_CACHE_SIZE) {
160 nalloc = PAGE_CACHE_SIZE/sizeof(*ifr);
161 CWARN("Too many interfaces: only enumerating first %d\n",
165 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
167 CERROR ("ENOMEM enumerating up to %d interfaces\n", nalloc);
172 ifc.ifc_buf = (char *)ifr;
173 ifc.ifc_len = nalloc * sizeof(*ifr);
175 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
178 CERROR ("Error %d enumerating interfaces\n", rc);
184 nfound = ifc.ifc_len/sizeof(*ifr);
185 LASSERT (nfound <= nalloc);
187 if (nfound < nalloc || toobig)
190 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
197 LIBCFS_ALLOC(names, nfound * sizeof(*names));
202 /* NULL out all names[i] */
203 memset (names, 0, nfound * sizeof(*names));
205 for (i = 0; i < nfound; i++) {
207 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
208 if (nob == IFNAMSIZ) {
209 /* no space for terminating NULL */
210 CERROR("interface name %.*s too long (%d max)\n",
211 nob, ifr[i].ifr_name, IFNAMSIZ);
216 LIBCFS_ALLOC(names[i], IFNAMSIZ);
217 if (names[i] == NULL) {
222 memcpy(names[i], ifr[i].ifr_name, nob);
231 libcfs_ipif_free_enumeration(names, nfound);
233 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
238 EXPORT_SYMBOL(libcfs_ipif_enumerate);
241 libcfs_ipif_free_enumeration (char **names, int n)
247 for (i = 0; i < n && names[i] != NULL; i++)
248 LIBCFS_FREE(names[i], IFNAMSIZ);
250 LIBCFS_FREE(names, n * sizeof(*names));
253 EXPORT_SYMBOL(libcfs_ipif_free_enumeration);
256 libcfs_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
259 mm_segment_t oldmm = get_fs();
260 long ticks = timeout * HZ;
265 /* Caller may pass a zero timeout if she thinks the socket buffer is
266 * empty enough to take the whole message immediately */
273 struct msghdr msg = {
280 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
284 /* Set send timeout to remaining time */
285 tv = (struct timeval) {
286 .tv_sec = ticks / HZ,
287 .tv_usec = ((ticks % HZ) * 1000000) / HZ
290 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
291 (char *)&tv, sizeof(tv));
294 CERROR("Can't set socket send timeout "
296 (long)tv.tv_sec, (int)tv.tv_usec, rc);
303 rc = sock_sendmsg (sock, &msg, iov.iov_len);
304 ticks -= jiffies - then;
314 CERROR ("Unexpected zero rc\n");
315 return (-ECONNABORTED);
321 buffer = ((char *)buffer) + rc;
327 EXPORT_SYMBOL(libcfs_sock_write);
330 libcfs_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
333 mm_segment_t oldmm = get_fs();
334 long ticks = timeout * HZ;
346 struct msghdr msg = {
356 /* Set receive timeout to remaining time */
357 tv = (struct timeval) {
358 .tv_sec = ticks / HZ,
359 .tv_usec = ((ticks % HZ) * 1000000) / HZ
362 rc = sock_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
363 (char *)&tv, sizeof(tv));
366 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
367 (long)tv.tv_sec, (int)tv.tv_usec, rc);
373 rc = sock_recvmsg(sock, &msg, iov.iov_len, 0);
374 ticks -= jiffies - then;
383 buffer = ((char *)buffer) + rc;
394 EXPORT_SYMBOL(libcfs_sock_read);
397 libcfs_sock_create (struct socket **sockp, int *fatal,
398 __u32 local_ip, int local_port)
400 struct sockaddr_in locaddr;
404 mm_segment_t oldmm = get_fs();
406 /* All errors are fatal except bind failure if the port is in use */
409 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
412 CERROR ("Can't create socket: %d\n", rc);
418 rc = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
419 (char *)&option, sizeof (option));
422 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
426 if (local_ip != 0 || local_port != 0) {
427 memset(&locaddr, 0, sizeof(locaddr));
428 locaddr.sin_family = AF_INET;
429 locaddr.sin_port = htons(local_port);
430 locaddr.sin_addr.s_addr = (local_ip == 0) ?
431 INADDR_ANY : htonl(local_ip);
433 rc = sock->ops->bind(sock, (struct sockaddr *)&locaddr,
435 if (rc == -EADDRINUSE) {
436 CDEBUG(D_NET, "Port %d already in use\n", local_port);
441 CERROR("Error trying to bind to port %d: %d\n",
455 libcfs_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
457 mm_segment_t oldmm = get_fs();
461 if (txbufsize != 0) {
464 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
465 (char *)&option, sizeof (option));
468 CERROR ("Can't set send buffer %d: %d\n",
474 if (rxbufsize != 0) {
477 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
478 (char *)&option, sizeof (option));
481 CERROR ("Can't set receive buffer %d: %d\n",
490 EXPORT_SYMBOL(libcfs_sock_setbuf);
493 libcfs_sock_getaddr (struct socket *sock, int remote, __u32 *ip, int *port)
495 struct sockaddr_in sin;
496 int len = sizeof (sin);
499 rc = sock->ops->getname (sock, (struct sockaddr *)&sin, &len,
502 CERROR ("Error %d getting sock %s IP/port\n",
503 rc, remote ? "peer" : "local");
508 *ip = ntohl (sin.sin_addr.s_addr);
511 *port = ntohs (sin.sin_port);
516 EXPORT_SYMBOL(libcfs_sock_getaddr);
519 libcfs_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
522 if (txbufsize != NULL) {
523 *txbufsize = sock->sk->sk_sndbuf;
526 if (rxbufsize != NULL) {
527 *rxbufsize = sock->sk->sk_rcvbuf;
533 EXPORT_SYMBOL(libcfs_sock_getbuf);
536 libcfs_sock_listen (struct socket **sockp,
537 __u32 local_ip, int local_port, int backlog)
542 rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
545 CERROR("Can't create socket: port %d already in use\n",
550 rc = (*sockp)->ops->listen(*sockp, backlog);
554 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
555 sock_release(*sockp);
559 EXPORT_SYMBOL(libcfs_sock_listen);
562 libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
565 struct socket *newsock;
568 init_waitqueue_entry(&wait, current);
570 /* XXX this should add a ref to sock->ops->owner, if
571 * TCP could be a module */
572 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
574 CERROR("Can't allocate socket\n");
578 newsock->ops = sock->ops;
580 set_current_state(TASK_INTERRUPTIBLE);
581 add_wait_queue(cfs_sk_sleep(sock->sk), &wait);
583 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
585 /* Nothing ready, so wait for activity */
587 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
590 remove_wait_queue(cfs_sk_sleep(sock->sk), &wait);
591 set_current_state(TASK_RUNNING);
600 sock_release(newsock);
604 EXPORT_SYMBOL(libcfs_sock_accept);
607 libcfs_sock_abort_accept (struct socket *sock)
609 wake_up_all(cfs_sk_sleep(sock->sk));
612 EXPORT_SYMBOL(libcfs_sock_abort_accept);
615 libcfs_sock_connect (struct socket **sockp, int *fatal,
616 __u32 local_ip, int local_port,
617 __u32 peer_ip, int peer_port)
619 struct sockaddr_in srvaddr;
622 rc = libcfs_sock_create(sockp, fatal, local_ip, local_port);
626 memset (&srvaddr, 0, sizeof (srvaddr));
627 srvaddr.sin_family = AF_INET;
628 srvaddr.sin_port = htons(peer_port);
629 srvaddr.sin_addr.s_addr = htonl(peer_ip);
631 rc = (*sockp)->ops->connect(*sockp,
632 (struct sockaddr *)&srvaddr, sizeof(srvaddr),
637 /* EADDRNOTAVAIL probably means we're already connected to the same
638 * peer/port on the same local port on a differently typed
639 * connection. Let our caller retry with a different local
641 *fatal = !(rc == -EADDRNOTAVAIL);
643 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
644 "Error %d connecting %u.%u.%u.%u/%d -> %u.%u.%u.%u/%d\n", rc,
645 HIPQUAD(local_ip), local_port, HIPQUAD(peer_ip), peer_port);
647 sock_release(*sockp);
651 EXPORT_SYMBOL(libcfs_sock_connect);
654 libcfs_sock_release (struct socket *sock)
659 EXPORT_SYMBOL(libcfs_sock_release);