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.
22 #if !defined(__KERNEL__) || !defined(REDSTORM)
24 #include <libcfs/libcfs.h>
25 #include <libcfs/kp30.h>
27 #include <sys/socket.h>
28 #ifdef HAVE_NETINET_IN_H
29 #include <netinet/in.h>
31 #include <netinet/tcp.h>
32 #include <sys/ioctl.h>
38 #include <arpa/inet.h>
40 #if defined(__sun__) || defined(__sun)
41 #include <sys/sockio.h>
44 #include <sys/syscall.h>
48 * Functions to get network interfaces info
52 libcfs_sock_ioctl(int cmd, unsigned long arg)
56 fd = socket(AF_INET, SOCK_STREAM, 0);
60 CERROR("socket() failed: errno==%d\n", errno);
64 rc = ioctl(fd, cmd, arg);
71 libcfs_ipif_query (char *name, int *up, __u32 *ip)
79 if (nob >= IFNAMSIZ) {
80 CERROR("Interface name %s too long\n", name);
84 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
86 strcpy(ifr.ifr_name, name);
87 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
90 CERROR("Can't get flags for interface %s\n", name);
94 if ((ifr.ifr_flags & IFF_UP) == 0) {
95 CDEBUG(D_NET, "Interface %s down\n", name);
103 strcpy(ifr.ifr_name, name);
104 ifr.ifr_addr.sa_family = AF_INET;
105 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
108 CERROR("Can't get IP address for interface %s\n", name);
112 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
119 libcfs_ipif_free_enumeration (char **names, int n)
125 for (i = 0; i < n && names[i] != NULL; i++)
126 LIBCFS_FREE(names[i], IFNAMSIZ);
128 LIBCFS_FREE(names, n * sizeof(*names));
132 libcfs_ipif_enumerate (char ***namesp)
134 /* Allocate and fill in 'names', returning # interfaces/error */
145 nalloc = 16; /* first guess at max interfaces */
147 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
149 CERROR ("ENOMEM enumerating up to %d interfaces\n",
155 ifc.ifc_buf = (char *)ifr;
156 ifc.ifc_len = nalloc * sizeof(*ifr);
158 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
161 CERROR ("Error %d enumerating interfaces\n", rc);
167 nfound = ifc.ifc_len/sizeof(*ifr);
168 LASSERT (nfound <= nalloc);
173 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
180 LIBCFS_ALLOC(names, nfound * sizeof(*names));
185 /* NULL out all names[i] */
186 memset (names, 0, nfound * sizeof(*names));
188 for (i = 0; i < nfound; i++) {
190 nob = strlen (ifr[i].ifr_name);
191 if (nob >= IFNAMSIZ) {
192 /* no space for terminating NULL */
193 CERROR("interface name %.*s too long (%d max)\n",
194 nob, ifr[i].ifr_name, IFNAMSIZ);
199 LIBCFS_ALLOC(names[i], IFNAMSIZ);
200 if (names[i] == NULL) {
205 memcpy(names[i], ifr[i].ifr_name, nob);
214 libcfs_ipif_free_enumeration(names, nfound);
216 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
222 * Network functions used by user-land lnet acceptor
226 libcfs_sock_listen (int *sockp, __u32 local_ip, int local_port, int backlog)
230 struct sockaddr_in locaddr;
232 *sockp = socket(AF_INET, SOCK_STREAM, 0);
235 CERROR("socket() failed: errno==%d\n", errno);
240 if ( setsockopt(*sockp, SOL_SOCKET, SO_REUSEADDR,
241 (char *)&option, sizeof (option)) ) {
243 CERROR("setsockopt(SO_REUSEADDR) failed: errno==%d\n", errno);
247 if (local_ip != 0 || local_port != 0) {
248 memset(&locaddr, 0, sizeof(locaddr));
249 locaddr.sin_family = AF_INET;
250 locaddr.sin_port = htons(local_port);
251 locaddr.sin_addr.s_addr = (local_ip == 0) ?
252 INADDR_ANY : htonl(local_ip);
254 if ( bind(*sockp, (struct sockaddr *)&locaddr, sizeof(locaddr)) ) {
256 if ( errno == -EADDRINUSE )
257 CDEBUG(D_NET, "Port %d already in use\n",
260 CERROR("bind() to port %d failed: errno==%d\n",
266 if ( listen(*sockp, backlog) ) {
268 CERROR("listen() with backlog==%d failed: errno==%d\n",
281 libcfs_sock_accept (int *newsockp, int sock, __u32 *peer_ip, int *peer_port)
283 struct sockaddr_in accaddr;
284 socklen_t accaddr_len = sizeof(struct sockaddr_in);
286 *newsockp = accept(sock, (struct sockaddr *)&accaddr, &accaddr_len);
288 if ( *newsockp < 0 ) {
289 CERROR("accept() failed: errno==%d\n", errno);
293 *peer_ip = ntohl(accaddr.sin_addr.s_addr);
294 *peer_port = ntohs(accaddr.sin_port);
300 libcfs_sock_read (int sock, void *buffer, int nob, int timeout)
304 cfs_time_t start_time = cfs_time_current();
310 /* poll(2) measures timeout in msec */
313 while (nob != 0 && timeout > 0) {
314 cfs_time_t current_time;
316 rc = poll(&pfd, 1, timeout);
321 if ((pfd.revents & POLLIN) == 0)
324 rc = read(sock, buffer, nob);
330 buffer = ((char *)buffer) + rc;
333 current_time = cfs_time_current();
334 timeout -= cfs_duration_sec(cfs_time_sub(cfs_time_current(),
344 /* Just try to connect to localhost to wake up entity that are
345 * sleeping in accept() */
347 libcfs_sock_abort_accept(__u16 port)
350 struct sockaddr_in locaddr;
352 memset(&locaddr, 0, sizeof(locaddr));
353 locaddr.sin_family = AF_INET;
354 locaddr.sin_port = htons(port);
355 locaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
357 fd = socket(AF_INET, SOCK_STREAM, 0);
359 CERROR("socket() failed: errno==%d\n", errno);
363 rc = connect(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
365 if ( errno != ECONNREFUSED )
366 CERROR("connect() failed: errno==%d\n", errno);
368 CDEBUG(D_NET, "Nobody to wake up at %d\n", port);
375 * Network functions of common use
379 libcfs_getpeername(int sock_fd, __u32 *ipaddr_p, __u16 *port_p)
382 struct sockaddr_in peer_addr;
383 socklen_t peer_addr_len = sizeof(peer_addr);
385 rc = getpeername(sock_fd, (struct sockaddr *)&peer_addr, &peer_addr_len);
389 if (ipaddr_p != NULL)
390 *ipaddr_p = ntohl(peer_addr.sin_addr.s_addr);
392 *port_p = ntohs(peer_addr.sin_port);
398 libcfs_socketpair(int *fdp)
402 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fdp);
405 CERROR ("Cannot create socket pair\n");
409 for (i = 0; i < 2; i++) {
410 rc = libcfs_fcntl_nonblock(fdp[i]);
422 libcfs_fcntl_nonblock(int fd)
426 flags = fcntl(fd, F_GETFL, 0);
429 CERROR ("Cannot get socket flags\n");
433 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
436 CERROR ("Cannot set socket flags\n");
444 libcfs_sock_set_nagle(int fd, int nagle)
447 int option = nagle ? 0 : 1;
449 #if defined(__sun__) || defined(__sun)
450 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &option, sizeof(option));
452 rc = setsockopt(fd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
457 CERROR ("Cannot set NODELAY socket option\n");
465 libcfs_sock_set_bufsiz(int fd, int bufsiz)
469 LASSERT (bufsiz != 0);
472 rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &option, sizeof(option));
475 CERROR ("Cannot set SNDBUF socket option\n");
480 rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &option, sizeof(option));
483 CERROR ("Cannot set RCVBUF socket option\n");
491 libcfs_sock_create(int *fdp)
495 fd = socket(AF_INET, SOCK_STREAM, 0);
498 CERROR ("Cannot create socket\n");
503 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
504 &option, sizeof(option));
507 CERROR ("Cannot set SO_REUSEADDR for socket\n");
517 libcfs_sock_bind_to_port(int fd, __u16 port)
520 struct sockaddr_in locaddr;
522 memset(&locaddr, 0, sizeof(locaddr));
523 locaddr.sin_family = AF_INET;
524 locaddr.sin_addr.s_addr = INADDR_ANY;
525 locaddr.sin_port = htons(port);
527 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
530 CERROR ("Cannot bind to port %d\n", port);
538 libcfs_sock_connect(int fd, __u32 ip, __u16 port)
541 struct sockaddr_in addr;
543 memset(&addr, 0, sizeof(addr));
544 addr.sin_family = AF_INET;
545 addr.sin_addr.s_addr = htonl(ip);
546 addr.sin_port = htons(port);
548 rc = connect(fd, (struct sockaddr *)&addr,
549 sizeof(struct sockaddr_in));
551 if(rc != 0 && errno != EINPROGRESS) {
553 if (rc != -EADDRINUSE && rc != -EADDRNOTAVAIL)
554 CERROR ("Cannot connect to %u.%u.%u.%u:%d (err=%d)\n",
555 HIPQUAD(ip), port, errno);
562 /* NB: EPIPE and ECONNRESET are considered as non-fatal
564 * 1) it still makes sense to continue reading &&
565 * 2) anyway, poll() will set up POLLHUP|POLLERR flags */
566 int libcfs_sock_writev(int fd, const struct iovec *vector, int count)
570 rc = syscall(SYS_writev, fd, vector, count);
572 if (rc == 0) /* write nothing */
576 if (errno == EAGAIN || /* write nothing */
577 errno == EPIPE || /* non-fatal error */
578 errno == ECONNRESET) /* non-fatal error */
587 int libcfs_sock_readv(int fd, const struct iovec *vector, int count)
591 rc = syscall(SYS_readv, fd, vector, count);
593 if (rc == 0) /* EOF */
597 if (errno == EAGAIN) /* read nothing */
606 #endif /* !__KERNEL__ || !defined(REDSTORM) */