1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2005 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define DEBUG_SUBSYSTEM S_LNET
23 #include <libcfs/kp30.h>
24 #include <libcfs/libcfs.h>
28 #include <linux/file.h>
29 /* For sys_open & sys_close */
30 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
31 #include <linux/syscalls.h>
37 libcfs_sock_ioctl(int cmd, unsigned long arg)
39 mm_segment_t oldmm = get_fs();
43 struct file *sock_filp;
45 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
47 CERROR ("Can't create socket: %d\n", rc);
51 fd = sock_map_fd(sock);
65 #ifdef HAVE_UNLOCKED_IOCTL
66 if (sock_filp->f_op->unlocked_ioctl)
67 rc = sock_filp->f_op->unlocked_ioctl(sock_filp, cmd, arg);
72 rc =sock_filp->f_op->ioctl(sock_filp->f_dentry->d_inode,
87 libcfs_ipif_query (char *name, int *up, __u32 *ip, __u32 *mask)
94 nob = strnlen(name, IFNAMSIZ);
95 if (nob == IFNAMSIZ) {
96 CERROR("Interface name %s too long\n", name);
101 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
103 strcpy(ifr.ifr_name, name);
104 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
107 CERROR("Can't get flags for interface %s\n", name);
111 if ((ifr.ifr_flags & IFF_UP) == 0) {
112 CDEBUG(D_NET, "Interface %s down\n", name);
120 strcpy(ifr.ifr_name, name);
121 ifr.ifr_addr.sa_family = AF_INET;
122 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
125 CERROR("Can't get IP address for interface %s\n", name);
129 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
132 strcpy(ifr.ifr_name, name);
133 ifr.ifr_addr.sa_family = AF_INET;
134 rc = libcfs_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
137 CERROR("Can't get netmask for interface %s\n", name);
141 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
148 EXPORT_SYMBOL(libcfs_ipif_query);
151 libcfs_ipif_enumerate (char ***namesp)
153 /* Allocate and fill in 'names', returning # interfaces/error */
165 nalloc = 16; /* first guess at max interfaces */
168 if (nalloc * sizeof(*ifr) > CFS_PAGE_SIZE) {
170 nalloc = CFS_PAGE_SIZE/sizeof(*ifr);
171 CWARN("Too many interfaces: only enumerating first %d\n",
175 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
177 CERROR ("ENOMEM enumerating up to %d interfaces\n", nalloc);
182 ifc.ifc_buf = (char *)ifr;
183 ifc.ifc_len = nalloc * sizeof(*ifr);
185 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
188 CERROR ("Error %d enumerating interfaces\n", rc);
194 nfound = ifc.ifc_len/sizeof(*ifr);
195 LASSERT (nfound <= nalloc);
197 if (nfound < nalloc || toobig)
200 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
207 LIBCFS_ALLOC(names, nfound * sizeof(*names));
212 /* NULL out all names[i] */
213 memset (names, 0, nfound * sizeof(*names));
215 for (i = 0; i < nfound; i++) {
217 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
218 if (nob == IFNAMSIZ) {
219 /* no space for terminating NULL */
220 CERROR("interface name %.*s too long (%d max)\n",
221 nob, ifr[i].ifr_name, IFNAMSIZ);
226 LIBCFS_ALLOC(names[i], IFNAMSIZ);
227 if (names[i] == NULL) {
232 memcpy(names[i], ifr[i].ifr_name, nob);
241 libcfs_ipif_free_enumeration(names, nfound);
243 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
248 EXPORT_SYMBOL(libcfs_ipif_enumerate);
251 libcfs_ipif_free_enumeration (char **names, int n)
257 for (i = 0; i < n && names[i] != NULL; i++)
258 LIBCFS_FREE(names[i], IFNAMSIZ);
260 LIBCFS_FREE(names, n * sizeof(*names));
263 EXPORT_SYMBOL(libcfs_ipif_free_enumeration);
266 libcfs_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
269 mm_segment_t oldmm = get_fs();
270 long ticks = timeout * HZ;
275 /* Caller may pass a zero timeout if she thinks the socket buffer is
276 * empty enough to take the whole message immediately */
283 struct msghdr msg = {
290 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
294 /* Set send timeout to remaining time */
295 tv = (struct timeval) {
296 .tv_sec = ticks / HZ,
297 .tv_usec = ((ticks % HZ) * 1000000) / HZ
300 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
301 (char *)&tv, sizeof(tv));
304 CERROR("Can't set socket send timeout "
306 (long)tv.tv_sec, (int)tv.tv_usec, rc);
313 rc = sock_sendmsg (sock, &msg, iov.iov_len);
314 ticks -= jiffies - then;
324 CERROR ("Unexpected zero rc\n");
325 return (-ECONNABORTED);
331 buffer = ((char *)buffer) + rc;
337 EXPORT_SYMBOL(libcfs_sock_write);
340 libcfs_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
343 mm_segment_t oldmm = get_fs();
344 long ticks = timeout * HZ;
356 struct msghdr msg = {
366 /* Set receive timeout to remaining time */
367 tv = (struct timeval) {
368 .tv_sec = ticks / HZ,
369 .tv_usec = ((ticks % HZ) * 1000000) / HZ
372 rc = sock_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
373 (char *)&tv, sizeof(tv));
376 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
377 (long)tv.tv_sec, (int)tv.tv_usec, rc);
383 rc = sock_recvmsg(sock, &msg, iov.iov_len, 0);
384 ticks -= jiffies - then;
393 buffer = ((char *)buffer) + rc;
404 EXPORT_SYMBOL(libcfs_sock_read);
407 libcfs_sock_create (struct socket **sockp, int *fatal,
408 __u32 local_ip, int local_port)
410 struct sockaddr_in locaddr;
414 mm_segment_t oldmm = get_fs();
416 /* All errors are fatal except bind failure if the port is in use */
419 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
422 CERROR ("Can't create socket: %d\n", rc);
428 rc = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
429 (char *)&option, sizeof (option));
432 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
436 if (local_ip != 0 || local_port != 0) {
437 memset(&locaddr, 0, sizeof(locaddr));
438 locaddr.sin_family = AF_INET;
439 locaddr.sin_port = htons(local_port);
440 locaddr.sin_addr.s_addr = (local_ip == 0) ?
441 INADDR_ANY : htonl(local_ip);
443 rc = sock->ops->bind(sock, (struct sockaddr *)&locaddr,
445 if (rc == -EADDRINUSE) {
446 CDEBUG(D_NET, "Port %d already in use\n", local_port);
451 CERROR("Error trying to bind to port %d: %d\n",
465 libcfs_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
467 mm_segment_t oldmm = get_fs();
471 if (txbufsize != 0) {
474 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
475 (char *)&option, sizeof (option));
478 CERROR ("Can't set send buffer %d: %d\n",
484 if (rxbufsize != 0) {
487 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
488 (char *)&option, sizeof (option));
491 CERROR ("Can't set receive buffer %d: %d\n",
500 EXPORT_SYMBOL(libcfs_sock_setbuf);
503 libcfs_sock_getaddr (struct socket *sock, int remote, __u32 *ip, int *port)
505 struct sockaddr_in sin;
506 int len = sizeof (sin);
509 rc = sock->ops->getname (sock, (struct sockaddr *)&sin, &len,
512 CERROR ("Error %d getting sock %s IP/port\n",
513 rc, remote ? "peer" : "local");
518 *ip = ntohl (sin.sin_addr.s_addr);
521 *port = ntohs (sin.sin_port);
526 EXPORT_SYMBOL(libcfs_sock_getaddr);
529 libcfs_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
532 if (txbufsize != NULL) {
533 *txbufsize = sock->sk->sk_sndbuf;
536 if (rxbufsize != NULL) {
537 *rxbufsize = sock->sk->sk_rcvbuf;
543 EXPORT_SYMBOL(libcfs_sock_getbuf);
546 libcfs_sock_listen (struct socket **sockp,
547 __u32 local_ip, int local_port, int backlog)
552 rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
555 CERROR("Can't create socket: port %d already in use\n",
560 rc = (*sockp)->ops->listen(*sockp, backlog);
564 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
565 sock_release(*sockp);
569 EXPORT_SYMBOL(libcfs_sock_listen);
571 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
572 int sock_create_lite(int family, int type, int protocol, struct socket **res)
590 libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
593 struct socket *newsock;
596 init_waitqueue_entry(&wait, current);
598 /* XXX this should add a ref to sock->ops->owner, if
599 * TCP could be a module */
600 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
602 CERROR("Can't allocate socket\n");
606 newsock->ops = sock->ops;
608 set_current_state(TASK_INTERRUPTIBLE);
609 add_wait_queue(sock->sk->sk_sleep, &wait);
611 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
613 /* Nothing ready, so wait for activity */
615 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
618 remove_wait_queue(sock->sk->sk_sleep, &wait);
619 set_current_state(TASK_RUNNING);
628 sock_release(newsock);
632 EXPORT_SYMBOL(libcfs_sock_accept);
635 libcfs_sock_abort_accept (struct socket *sock)
637 wake_up_all(sock->sk->sk_sleep);
640 EXPORT_SYMBOL(libcfs_sock_abort_accept);
643 libcfs_sock_connect (struct socket **sockp, int *fatal,
644 __u32 local_ip, int local_port,
645 __u32 peer_ip, int peer_port)
647 struct sockaddr_in srvaddr;
650 rc = libcfs_sock_create(sockp, fatal, local_ip, local_port);
654 memset (&srvaddr, 0, sizeof (srvaddr));
655 srvaddr.sin_family = AF_INET;
656 srvaddr.sin_port = htons(peer_port);
657 srvaddr.sin_addr.s_addr = htonl(peer_ip);
659 rc = (*sockp)->ops->connect(*sockp,
660 (struct sockaddr *)&srvaddr, sizeof(srvaddr),
665 /* EADDRNOTAVAIL probably means we're already connected to the same
666 * peer/port on the same local port on a differently typed
667 * connection. Let our caller retry with a different local
669 *fatal = !(rc == -EADDRNOTAVAIL);
671 CDEBUG(*fatal ? D_NETERROR : D_NET,
672 "Error %d connecting %u.%u.%u.%u/%d -> %u.%u.%u.%u/%d\n", rc,
673 HIPQUAD(local_ip), local_port, HIPQUAD(peer_ip), peer_port);
675 sock_release(*sockp);
679 EXPORT_SYMBOL(libcfs_sock_connect);
682 libcfs_sock_release (struct socket *sock)
687 EXPORT_SYMBOL(libcfs_sock_release);