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>
42 #include <linux/file.h>
43 /* For sys_open & sys_close */
44 #include <linux/syscalls.h>
47 libcfs_sock_ioctl(int cmd, unsigned long arg)
49 mm_segment_t oldmm = get_fs();
52 struct file *sock_filp;
54 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
56 CERROR ("Can't create socket: %d\n", rc);
60 sock_filp = sock_alloc_file(sock, 0, NULL);
61 if (IS_ERR(sock_filp)) {
63 rc = PTR_ERR(sock_filp);
68 if (sock_filp->f_op->unlocked_ioctl)
69 rc = sock_filp->f_op->unlocked_ioctl(sock_filp, cmd, arg);
78 libcfs_ipif_query (char *name, int *up, __u32 *ip, __u32 *mask)
85 nob = strnlen(name, IFNAMSIZ);
86 if (nob == IFNAMSIZ) {
87 CERROR("Interface name %s too long\n", name);
91 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
93 strcpy(ifr.ifr_name, name);
94 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
97 CERROR("Can't get flags for interface %s\n", name);
101 if ((ifr.ifr_flags & IFF_UP) == 0) {
102 CDEBUG(D_NET, "Interface %s down\n", name);
110 strcpy(ifr.ifr_name, name);
111 ifr.ifr_addr.sa_family = AF_INET;
112 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
115 CERROR("Can't get IP address for interface %s\n", name);
119 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
122 strcpy(ifr.ifr_name, name);
123 ifr.ifr_addr.sa_family = AF_INET;
124 rc = libcfs_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
127 CERROR("Can't get netmask for interface %s\n", name);
131 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
137 EXPORT_SYMBOL(libcfs_ipif_query);
140 libcfs_ipif_enumerate (char ***namesp)
142 /* Allocate and fill in 'names', returning # interfaces/error */
154 nalloc = 16; /* first guess at max interfaces */
157 if (nalloc * sizeof(*ifr) > PAGE_CACHE_SIZE) {
159 nalloc = PAGE_CACHE_SIZE/sizeof(*ifr);
160 CWARN("Too many interfaces: only enumerating first %d\n",
164 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
166 CERROR ("ENOMEM enumerating up to %d interfaces\n", nalloc);
171 ifc.ifc_buf = (char *)ifr;
172 ifc.ifc_len = nalloc * sizeof(*ifr);
174 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
177 CERROR ("Error %d enumerating interfaces\n", rc);
183 nfound = ifc.ifc_len/sizeof(*ifr);
184 LASSERT (nfound <= nalloc);
186 if (nfound < nalloc || toobig)
189 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
196 LIBCFS_ALLOC(names, nfound * sizeof(*names));
201 /* NULL out all names[i] */
202 memset (names, 0, nfound * sizeof(*names));
204 for (i = 0; i < nfound; i++) {
206 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
207 if (nob == IFNAMSIZ) {
208 /* no space for terminating NULL */
209 CERROR("interface name %.*s too long (%d max)\n",
210 nob, ifr[i].ifr_name, IFNAMSIZ);
215 LIBCFS_ALLOC(names[i], IFNAMSIZ);
216 if (names[i] == NULL) {
221 memcpy(names[i], ifr[i].ifr_name, nob);
230 libcfs_ipif_free_enumeration(names, nfound);
232 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
237 EXPORT_SYMBOL(libcfs_ipif_enumerate);
240 libcfs_ipif_free_enumeration (char **names, int n)
246 for (i = 0; i < n && names[i] != NULL; i++)
247 LIBCFS_FREE(names[i], IFNAMSIZ);
249 LIBCFS_FREE(names, n * sizeof(*names));
252 EXPORT_SYMBOL(libcfs_ipif_free_enumeration);
255 libcfs_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
258 mm_segment_t oldmm = get_fs();
259 long ticks = timeout * HZ;
264 /* Caller may pass a zero timeout if she thinks the socket buffer is
265 * empty enough to take the whole message immediately */
272 struct msghdr msg = {
273 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
277 /* Set send timeout to remaining time */
278 tv = (struct timeval) {
279 .tv_sec = ticks / HZ,
280 .tv_usec = ((ticks % HZ) * 1000000) / HZ
283 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
284 (char *)&tv, sizeof(tv));
287 CERROR("Can't set socket send timeout "
289 (long)tv.tv_sec, (int)tv.tv_usec, rc);
295 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
296 ticks -= jiffies - then;
305 CERROR ("Unexpected zero rc\n");
306 return (-ECONNABORTED);
312 buffer = ((char *)buffer) + rc;
318 EXPORT_SYMBOL(libcfs_sock_write);
321 libcfs_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
324 mm_segment_t oldmm = get_fs();
325 long ticks = timeout * HZ;
337 struct msghdr msg = {
341 /* Set receive timeout to remaining time */
342 tv = (struct timeval) {
343 .tv_sec = ticks / HZ,
344 .tv_usec = ((ticks % HZ) * 1000000) / HZ
347 rc = sock_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
348 (char *)&tv, sizeof(tv));
351 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
352 (long)tv.tv_sec, (int)tv.tv_usec, rc);
357 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
358 ticks -= jiffies - then;
366 buffer = ((char *)buffer) + rc;
377 EXPORT_SYMBOL(libcfs_sock_read);
380 libcfs_sock_create (struct socket **sockp, int *fatal,
381 __u32 local_ip, int local_port)
383 struct sockaddr_in locaddr;
387 mm_segment_t oldmm = get_fs();
389 /* All errors are fatal except bind failure if the port is in use */
392 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
395 CERROR ("Can't create socket: %d\n", rc);
401 rc = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
402 (char *)&option, sizeof (option));
405 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
409 if (local_ip != 0 || local_port != 0) {
410 memset(&locaddr, 0, sizeof(locaddr));
411 locaddr.sin_family = AF_INET;
412 locaddr.sin_port = htons(local_port);
413 locaddr.sin_addr.s_addr = (local_ip == 0) ?
414 INADDR_ANY : htonl(local_ip);
416 rc = sock->ops->bind(sock, (struct sockaddr *)&locaddr,
418 if (rc == -EADDRINUSE) {
419 CDEBUG(D_NET, "Port %d already in use\n", local_port);
424 CERROR("Error trying to bind to port %d: %d\n",
438 libcfs_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
440 mm_segment_t oldmm = get_fs();
444 if (txbufsize != 0) {
447 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
448 (char *)&option, sizeof (option));
451 CERROR ("Can't set send buffer %d: %d\n",
457 if (rxbufsize != 0) {
460 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
461 (char *)&option, sizeof (option));
464 CERROR ("Can't set receive buffer %d: %d\n",
473 EXPORT_SYMBOL(libcfs_sock_setbuf);
476 libcfs_sock_getaddr (struct socket *sock, int remote, __u32 *ip, int *port)
478 struct sockaddr_in sin;
479 int len = sizeof (sin);
482 rc = sock->ops->getname (sock, (struct sockaddr *)&sin, &len,
485 CERROR ("Error %d getting sock %s IP/port\n",
486 rc, remote ? "peer" : "local");
491 *ip = ntohl (sin.sin_addr.s_addr);
494 *port = ntohs (sin.sin_port);
499 EXPORT_SYMBOL(libcfs_sock_getaddr);
502 libcfs_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
505 if (txbufsize != NULL) {
506 *txbufsize = sock->sk->sk_sndbuf;
509 if (rxbufsize != NULL) {
510 *rxbufsize = sock->sk->sk_rcvbuf;
516 EXPORT_SYMBOL(libcfs_sock_getbuf);
519 libcfs_sock_listen (struct socket **sockp,
520 __u32 local_ip, int local_port, int backlog)
525 rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
528 CERROR("Can't create socket: port %d already in use\n",
533 rc = (*sockp)->ops->listen(*sockp, backlog);
537 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
538 sock_release(*sockp);
542 EXPORT_SYMBOL(libcfs_sock_listen);
545 libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
548 struct socket *newsock;
551 init_waitqueue_entry(&wait, current);
553 /* XXX this should add a ref to sock->ops->owner, if
554 * TCP could be a module */
555 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
557 CERROR("Can't allocate socket\n");
561 newsock->ops = sock->ops;
563 set_current_state(TASK_INTERRUPTIBLE);
564 add_wait_queue(cfs_sk_sleep(sock->sk), &wait);
566 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
568 /* Nothing ready, so wait for activity */
570 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
573 remove_wait_queue(cfs_sk_sleep(sock->sk), &wait);
574 set_current_state(TASK_RUNNING);
583 sock_release(newsock);
587 EXPORT_SYMBOL(libcfs_sock_accept);
590 libcfs_sock_abort_accept (struct socket *sock)
592 wake_up_all(cfs_sk_sleep(sock->sk));
595 EXPORT_SYMBOL(libcfs_sock_abort_accept);
598 libcfs_sock_connect (struct socket **sockp, int *fatal,
599 __u32 local_ip, int local_port,
600 __u32 peer_ip, int peer_port)
602 struct sockaddr_in srvaddr;
605 rc = libcfs_sock_create(sockp, fatal, local_ip, local_port);
609 memset (&srvaddr, 0, sizeof (srvaddr));
610 srvaddr.sin_family = AF_INET;
611 srvaddr.sin_port = htons(peer_port);
612 srvaddr.sin_addr.s_addr = htonl(peer_ip);
614 rc = (*sockp)->ops->connect(*sockp,
615 (struct sockaddr *)&srvaddr, sizeof(srvaddr),
620 /* EADDRNOTAVAIL probably means we're already connected to the same
621 * peer/port on the same local port on a differently typed
622 * connection. Let our caller retry with a different local
624 *fatal = !(rc == -EADDRNOTAVAIL);
626 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
627 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
628 &local_ip, local_port, &peer_ip, peer_port);
630 sock_release(*sockp);
634 EXPORT_SYMBOL(libcfs_sock_connect);
637 libcfs_sock_release (struct socket *sock)
642 EXPORT_SYMBOL(libcfs_sock_release);