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>
26 #include <sys/socket.h>
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
30 #include <netinet/tcp.h>
31 #include <sys/ioctl.h>
37 #include <arpa/inet.h>
39 #if defined(__sun__) || defined(__sun)
40 #include <sys/sockio.h>
43 #include <sys/syscall.h>
47 * Functions to get network interfaces info
51 libcfs_sock_ioctl(int cmd, unsigned long arg)
55 fd = socket(AF_INET, SOCK_STREAM, 0);
59 CERROR("socket() failed: errno==%d\n", errno);
63 rc = ioctl(fd, cmd, arg);
70 libcfs_ipif_query (char *name, int *up, __u32 *ip)
78 if (nob >= IFNAMSIZ) {
79 CERROR("Interface name %s too long\n", name);
83 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
85 strcpy(ifr.ifr_name, name);
86 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
89 CERROR("Can't get flags for interface %s\n", name);
93 if ((ifr.ifr_flags & IFF_UP) == 0) {
94 CDEBUG(D_NET, "Interface %s down\n", name);
102 strcpy(ifr.ifr_name, name);
103 ifr.ifr_addr.sa_family = AF_INET;
104 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
107 CERROR("Can't get IP address for interface %s\n", name);
111 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
118 libcfs_ipif_free_enumeration (char **names, int n)
124 for (i = 0; i < n && names[i] != NULL; i++)
125 LIBCFS_FREE(names[i], IFNAMSIZ);
127 LIBCFS_FREE(names, n * sizeof(*names));
131 libcfs_ipif_enumerate (char ***namesp)
133 /* Allocate and fill in 'names', returning # interfaces/error */
144 nalloc = 16; /* first guess at max interfaces */
146 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
148 CERROR ("ENOMEM enumerating up to %d interfaces\n",
154 ifc.ifc_buf = (char *)ifr;
155 ifc.ifc_len = nalloc * sizeof(*ifr);
157 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
160 CERROR ("Error %d enumerating interfaces\n", rc);
166 nfound = ifc.ifc_len/sizeof(*ifr);
167 LASSERT (nfound <= nalloc);
172 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
179 LIBCFS_ALLOC(names, nfound * sizeof(*names));
184 /* NULL out all names[i] */
185 memset (names, 0, nfound * sizeof(*names));
187 for (i = 0; i < nfound; i++) {
189 nob = strlen (ifr[i].ifr_name);
190 if (nob >= IFNAMSIZ) {
191 /* no space for terminating NULL */
192 CERROR("interface name %.*s too long (%d max)\n",
193 nob, ifr[i].ifr_name, IFNAMSIZ);
198 LIBCFS_ALLOC(names[i], IFNAMSIZ);
199 if (names[i] == NULL) {
204 memcpy(names[i], ifr[i].ifr_name, nob);
213 libcfs_ipif_free_enumeration(names, nfound);
215 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
221 * Network functions used by user-land lnet acceptor
225 libcfs_sock_listen (int *sockp, __u32 local_ip, int local_port, int backlog)
229 struct sockaddr_in locaddr;
231 *sockp = socket(AF_INET, SOCK_STREAM, 0);
234 CERROR("socket() failed: errno==%d\n", errno);
239 if ( setsockopt(*sockp, SOL_SOCKET, SO_REUSEADDR,
240 (char *)&option, sizeof (option)) ) {
242 CERROR("setsockopt(SO_REUSEADDR) failed: errno==%d\n", errno);
246 if (local_ip != 0 || local_port != 0) {
247 memset(&locaddr, 0, sizeof(locaddr));
248 locaddr.sin_family = AF_INET;
249 locaddr.sin_port = htons(local_port);
250 locaddr.sin_addr.s_addr = (local_ip == 0) ?
251 INADDR_ANY : htonl(local_ip);
253 if ( bind(*sockp, (struct sockaddr *)&locaddr, sizeof(locaddr)) ) {
255 if ( errno == -EADDRINUSE )
256 CDEBUG(D_NET, "Port %d already in use\n",
259 CERROR("bind() to port %d failed: errno==%d\n",
265 if ( listen(*sockp, backlog) ) {
267 CERROR("listen() with backlog==%d failed: errno==%d\n",
280 libcfs_sock_accept (int *newsockp, int sock, __u32 *peer_ip, int *peer_port)
282 struct sockaddr_in accaddr;
283 socklen_t accaddr_len = sizeof(struct sockaddr_in);
285 *newsockp = accept(sock, (struct sockaddr *)&accaddr, &accaddr_len);
287 if ( *newsockp < 0 ) {
288 CERROR("accept() failed: errno==%d\n", errno);
292 *peer_ip = ntohl(accaddr.sin_addr.s_addr);
293 *peer_port = ntohs(accaddr.sin_port);
299 libcfs_sock_read (int sock, void *buffer, int nob, int timeout)
303 cfs_time_t start_time = cfs_time_current();
309 /* poll(2) measures timeout in msec */
312 while (nob != 0 && timeout > 0) {
313 cfs_time_t current_time;
315 rc = poll(&pfd, 1, timeout);
320 if ((pfd.revents & POLLIN) == 0)
323 rc = read(sock, buffer, nob);
329 buffer = ((char *)buffer) + rc;
332 current_time = cfs_time_current();
333 timeout -= cfs_duration_sec(cfs_time_sub(cfs_time_current(),
343 /* Just try to connect to localhost to wake up entity that are
344 * sleeping in accept() */
346 libcfs_sock_abort_accept(__u16 port)
349 struct sockaddr_in locaddr;
351 memset(&locaddr, 0, sizeof(locaddr));
352 locaddr.sin_family = AF_INET;
353 locaddr.sin_port = htons(port);
354 locaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
356 fd = socket(AF_INET, SOCK_STREAM, 0);
358 CERROR("socket() failed: errno==%d\n", errno);
362 rc = connect(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
364 if ( errno != ECONNREFUSED )
365 CERROR("connect() failed: errno==%d\n", errno);
367 CDEBUG(D_NET, "Nobody to wake up at %d\n", port);
374 * Network functions of common use
378 libcfs_getpeername(int sock_fd, __u32 *ipaddr_p, __u16 *port_p)
381 struct sockaddr_in peer_addr;
382 socklen_t peer_addr_len = sizeof(peer_addr);
384 rc = getpeername(sock_fd, (struct sockaddr *)&peer_addr, &peer_addr_len);
388 if (ipaddr_p != NULL)
389 *ipaddr_p = ntohl(peer_addr.sin_addr.s_addr);
391 *port_p = ntohs(peer_addr.sin_port);
397 libcfs_socketpair(int *fdp)
401 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fdp);
404 CERROR ("Cannot create socket pair\n");
408 for (i = 0; i < 2; i++) {
409 rc = libcfs_fcntl_nonblock(fdp[i]);
421 libcfs_fcntl_nonblock(int fd)
425 flags = fcntl(fd, F_GETFL, 0);
428 CERROR ("Cannot get socket flags\n");
432 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
435 CERROR ("Cannot set socket flags\n");
443 libcfs_sock_set_nagle(int fd, int nagle)
446 int option = nagle ? 0 : 1;
448 #if defined(__sun__) || defined(__sun)
449 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &option, sizeof(option));
451 rc = setsockopt(fd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
456 CERROR ("Cannot set NODELAY socket option\n");
464 libcfs_sock_set_bufsiz(int fd, int bufsiz)
468 LASSERT (bufsiz != 0);
471 rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &option, sizeof(option));
474 CERROR ("Cannot set SNDBUF socket option\n");
479 rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &option, sizeof(option));
482 CERROR ("Cannot set RCVBUF socket option\n");
490 libcfs_sock_create(int *fdp)
494 fd = socket(AF_INET, SOCK_STREAM, 0);
497 CERROR ("Cannot create socket\n");
502 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
503 &option, sizeof(option));
506 CERROR ("Cannot set SO_REUSEADDR for socket\n");
516 libcfs_sock_bind_to_port(int fd, __u16 port)
519 struct sockaddr_in locaddr;
521 memset(&locaddr, 0, sizeof(locaddr));
522 locaddr.sin_family = AF_INET;
523 locaddr.sin_addr.s_addr = INADDR_ANY;
524 locaddr.sin_port = htons(port);
526 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
529 CERROR ("Cannot bind to port %d\n", port);
537 libcfs_sock_connect(int fd, __u32 ip, __u16 port)
540 struct sockaddr_in addr;
542 memset(&addr, 0, sizeof(addr));
543 addr.sin_family = AF_INET;
544 addr.sin_addr.s_addr = htonl(ip);
545 addr.sin_port = htons(port);
547 rc = connect(fd, (struct sockaddr *)&addr,
548 sizeof(struct sockaddr_in));
550 if(rc != 0 && errno != EINPROGRESS) {
552 if (rc != -EADDRINUSE && rc != -EADDRNOTAVAIL)
553 CERROR ("Cannot connect to %u.%u.%u.%u:%d (err=%d)\n",
554 HIPQUAD(ip), port, errno);
561 /* NB: EPIPE and ECONNRESET are considered as non-fatal
563 * 1) it still makes sense to continue reading &&
564 * 2) anyway, poll() will set up POLLHUP|POLLERR flags */
565 int libcfs_sock_writev(int fd, const struct iovec *vector, int count)
569 rc = syscall(SYS_writev, fd, vector, count);
571 if (rc == 0) /* write nothing */
575 if (errno == EAGAIN || /* write nothing */
576 errno == EPIPE || /* non-fatal error */
577 errno == ECONNRESET) /* non-fatal error */
586 int libcfs_sock_readv(int fd, const struct iovec *vector, int count)
590 rc = syscall(SYS_readv, fd, vector, count);
592 if (rc == 0) /* EOF */
596 if (errno == EAGAIN) /* read nothing */
605 #endif /* !__KERNEL__ || !defined(REDSTORM) */