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);
100 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
102 strcpy(ifr.ifr_name, name);
103 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
106 CERROR("Can't get flags for interface %s\n", name);
110 if ((ifr.ifr_flags & IFF_UP) == 0) {
111 CDEBUG(D_NET, "Interface %s down\n", name);
119 strcpy(ifr.ifr_name, name);
120 ifr.ifr_addr.sa_family = AF_INET;
121 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
124 CERROR("Can't get IP address for interface %s\n", name);
128 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
131 strcpy(ifr.ifr_name, name);
132 ifr.ifr_addr.sa_family = AF_INET;
133 rc = libcfs_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
136 CERROR("Can't get netmask for interface %s\n", name);
140 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
146 EXPORT_SYMBOL(libcfs_ipif_query);
149 libcfs_ipif_enumerate (char ***namesp)
151 /* Allocate and fill in 'names', returning # interfaces/error */
163 nalloc = 16; /* first guess at max interfaces */
166 if (nalloc * sizeof(*ifr) > CFS_PAGE_SIZE) {
168 nalloc = CFS_PAGE_SIZE/sizeof(*ifr);
169 CWARN("Too many interfaces: only enumerating first %d\n",
173 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
175 CERROR ("ENOMEM enumerating up to %d interfaces\n", nalloc);
180 ifc.ifc_buf = (char *)ifr;
181 ifc.ifc_len = nalloc * sizeof(*ifr);
183 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
186 CERROR ("Error %d enumerating interfaces\n", rc);
192 nfound = ifc.ifc_len/sizeof(*ifr);
193 LASSERT (nfound <= nalloc);
195 if (nfound < nalloc || toobig)
198 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
205 LIBCFS_ALLOC(names, nfound * sizeof(*names));
210 /* NULL out all names[i] */
211 memset (names, 0, nfound * sizeof(*names));
213 for (i = 0; i < nfound; i++) {
215 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
216 if (nob == IFNAMSIZ) {
217 /* no space for terminating NULL */
218 CERROR("interface name %.*s too long (%d max)\n",
219 nob, ifr[i].ifr_name, IFNAMSIZ);
224 LIBCFS_ALLOC(names[i], IFNAMSIZ);
225 if (names[i] == NULL) {
230 memcpy(names[i], ifr[i].ifr_name, nob);
239 libcfs_ipif_free_enumeration(names, nfound);
241 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
246 EXPORT_SYMBOL(libcfs_ipif_enumerate);
249 libcfs_ipif_free_enumeration (char **names, int n)
255 for (i = 0; i < n && names[i] != NULL; i++)
256 LIBCFS_FREE(names[i], IFNAMSIZ);
258 LIBCFS_FREE(names, n * sizeof(*names));
261 EXPORT_SYMBOL(libcfs_ipif_free_enumeration);
264 libcfs_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
267 mm_segment_t oldmm = get_fs();
268 long ticks = timeout * HZ;
273 /* Caller may pass a zero timeout if she thinks the socket buffer is
274 * empty enough to take the whole message immediately */
281 struct msghdr msg = {
288 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
292 /* Set send timeout to remaining time */
293 tv = (struct timeval) {
294 .tv_sec = ticks / HZ,
295 .tv_usec = ((ticks % HZ) * 1000000) / HZ
298 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
299 (char *)&tv, sizeof(tv));
302 CERROR("Can't set socket send timeout "
304 (long)tv.tv_sec, (int)tv.tv_usec, rc);
311 rc = sock_sendmsg (sock, &msg, iov.iov_len);
312 ticks -= jiffies - then;
322 CERROR ("Unexpected zero rc\n");
323 return (-ECONNABORTED);
329 buffer = ((char *)buffer) + rc;
335 EXPORT_SYMBOL(libcfs_sock_write);
338 libcfs_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
341 mm_segment_t oldmm = get_fs();
342 long ticks = timeout * HZ;
354 struct msghdr msg = {
364 /* Set receive timeout to remaining time */
365 tv = (struct timeval) {
366 .tv_sec = ticks / HZ,
367 .tv_usec = ((ticks % HZ) * 1000000) / HZ
370 rc = sock_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
371 (char *)&tv, sizeof(tv));
374 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
375 (long)tv.tv_sec, (int)tv.tv_usec, rc);
381 rc = sock_recvmsg(sock, &msg, iov.iov_len, 0);
382 ticks -= jiffies - then;
391 buffer = ((char *)buffer) + rc;
402 EXPORT_SYMBOL(libcfs_sock_read);
405 libcfs_sock_create (struct socket **sockp, int *fatal,
406 __u32 local_ip, int local_port)
408 struct sockaddr_in locaddr;
412 mm_segment_t oldmm = get_fs();
414 /* All errors are fatal except bind failure if the port is in use */
417 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
420 CERROR ("Can't create socket: %d\n", rc);
426 rc = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
427 (char *)&option, sizeof (option));
430 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
434 if (local_ip != 0 || local_port != 0) {
435 memset(&locaddr, 0, sizeof(locaddr));
436 locaddr.sin_family = AF_INET;
437 locaddr.sin_port = htons(local_port);
438 locaddr.sin_addr.s_addr = (local_ip == 0) ?
439 INADDR_ANY : htonl(local_ip);
441 rc = sock->ops->bind(sock, (struct sockaddr *)&locaddr,
443 if (rc == -EADDRINUSE) {
444 CDEBUG(D_NET, "Port %d already in use\n", local_port);
449 CERROR("Error trying to bind to port %d: %d\n",
463 libcfs_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
465 mm_segment_t oldmm = get_fs();
469 if (txbufsize != 0) {
472 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
473 (char *)&option, sizeof (option));
476 CERROR ("Can't set send buffer %d: %d\n",
482 if (rxbufsize != 0) {
485 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
486 (char *)&option, sizeof (option));
489 CERROR ("Can't set receive buffer %d: %d\n",
498 EXPORT_SYMBOL(libcfs_sock_setbuf);
501 libcfs_sock_getaddr (struct socket *sock, int remote, __u32 *ip, int *port)
503 struct sockaddr_in sin;
504 int len = sizeof (sin);
507 rc = sock->ops->getname (sock, (struct sockaddr *)&sin, &len,
510 CERROR ("Error %d getting sock %s IP/port\n",
511 rc, remote ? "peer" : "local");
516 *ip = ntohl (sin.sin_addr.s_addr);
519 *port = ntohs (sin.sin_port);
524 EXPORT_SYMBOL(libcfs_sock_getaddr);
527 libcfs_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
530 if (txbufsize != NULL) {
531 *txbufsize = sock->sk->sk_sndbuf;
534 if (rxbufsize != NULL) {
535 *rxbufsize = sock->sk->sk_rcvbuf;
541 EXPORT_SYMBOL(libcfs_sock_getbuf);
544 libcfs_sock_listen (struct socket **sockp,
545 __u32 local_ip, int local_port, int backlog)
550 rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
553 CERROR("Can't create socket: port %d already in use\n",
558 rc = (*sockp)->ops->listen(*sockp, backlog);
562 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
563 sock_release(*sockp);
567 EXPORT_SYMBOL(libcfs_sock_listen);
569 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
570 int sock_create_lite(int family, int type, int protocol, struct socket **res)
586 libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
589 struct socket *newsock;
592 init_waitqueue_entry(&wait, current);
594 /* XXX this should add a ref to sock->ops->owner, if
595 * TCP could be a module */
596 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
598 CERROR("Can't allocate socket\n");
602 newsock->ops = sock->ops;
604 set_current_state(TASK_INTERRUPTIBLE);
605 add_wait_queue(sock->sk->sk_sleep, &wait);
607 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
609 /* Nothing ready, so wait for activity */
611 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
614 remove_wait_queue(sock->sk->sk_sleep, &wait);
615 set_current_state(TASK_RUNNING);
624 sock_release(newsock);
628 EXPORT_SYMBOL(libcfs_sock_accept);
631 libcfs_sock_abort_accept (struct socket *sock)
633 wake_up_all(sock->sk->sk_sleep);
636 EXPORT_SYMBOL(libcfs_sock_abort_accept);
639 libcfs_sock_connect (struct socket **sockp, int *fatal,
640 __u32 local_ip, int local_port,
641 __u32 peer_ip, int peer_port)
643 struct sockaddr_in srvaddr;
646 rc = libcfs_sock_create(sockp, fatal, local_ip, local_port);
650 memset (&srvaddr, 0, sizeof (srvaddr));
651 srvaddr.sin_family = AF_INET;
652 srvaddr.sin_port = htons(peer_port);
653 srvaddr.sin_addr.s_addr = htonl(peer_ip);
655 rc = (*sockp)->ops->connect(*sockp,
656 (struct sockaddr *)&srvaddr, sizeof(srvaddr),
661 /* EADDRNOTAVAIL probably means we're already connected to the same
662 * peer/port on the same local port on a differently typed
663 * connection. Let our caller retry with a different local
665 *fatal = !(rc == -EADDRNOTAVAIL);
667 CDEBUG(*fatal ? D_NETERROR : D_NET,
668 "Error %d connecting %u.%u.%u.%u/%d -> %u.%u.%u.%u/%d\n", rc,
669 HIPQUAD(local_ip), local_port, HIPQUAD(peer_ip), peer_port);
671 sock_release(*sockp);
675 EXPORT_SYMBOL(libcfs_sock_connect);
678 libcfs_sock_release (struct socket *sock)
683 EXPORT_SYMBOL(libcfs_sock_release);