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.
24 #include <sys/socket.h>
25 #include <netinet/tcp.h>
26 #include <sys/ioctl.h>
32 #include <arpa/inet.h>
34 #if defined(__sun__) || defined(__sun)
35 #include <sys/sockio.h>
38 #include <sys/syscall.h>
41 #include <libcfs/libcfs.h>
42 #include <libcfs/kp30.h>
45 * Functions to get network interfaces info
49 libcfs_sock_ioctl(int cmd, unsigned long arg)
53 fd = socket(AF_INET, SOCK_STREAM, 0);
57 CERROR("socket() failed: errno==%d\n", errno);
61 rc = ioctl(fd, cmd, arg);
68 libcfs_ipif_query (char *name, int *up, __u32 *ip)
76 if (nob >= IFNAMSIZ) {
77 CERROR("Interface name %s too long\n", name);
81 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
83 strcpy(ifr.ifr_name, name);
84 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
87 CERROR("Can't get flags for interface %s\n", name);
91 if ((ifr.ifr_flags & IFF_UP) == 0) {
92 CDEBUG(D_NET, "Interface %s down\n", name);
100 strcpy(ifr.ifr_name, name);
101 ifr.ifr_addr.sa_family = AF_INET;
102 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
105 CERROR("Can't get IP address for interface %s\n", name);
109 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
116 libcfs_ipif_free_enumeration (char **names, int n)
122 for (i = 0; i < n && names[i] != NULL; i++)
123 LIBCFS_FREE(names[i], IFNAMSIZ);
125 LIBCFS_FREE(names, n * sizeof(*names));
129 libcfs_ipif_enumerate (char ***namesp)
131 /* Allocate and fill in 'names', returning # interfaces/error */
142 nalloc = 16; /* first guess at max interfaces */
144 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
146 CERROR ("ENOMEM enumerating up to %d interfaces\n",
152 ifc.ifc_buf = (char *)ifr;
153 ifc.ifc_len = nalloc * sizeof(*ifr);
155 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
158 CERROR ("Error %d enumerating interfaces\n", rc);
164 nfound = ifc.ifc_len/sizeof(*ifr);
165 LASSERT (nfound <= nalloc);
170 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
177 LIBCFS_ALLOC(names, nfound * sizeof(*names));
182 /* NULL out all names[i] */
183 memset (names, 0, nfound * sizeof(*names));
185 for (i = 0; i < nfound; i++) {
187 nob = strlen (ifr[i].ifr_name);
188 if (nob >= IFNAMSIZ) {
189 /* no space for terminating NULL */
190 CERROR("interface name %.*s too long (%d max)\n",
191 nob, ifr[i].ifr_name, IFNAMSIZ);
196 LIBCFS_ALLOC(names[i], IFNAMSIZ);
197 if (names[i] == NULL) {
202 memcpy(names[i], ifr[i].ifr_name, nob);
211 libcfs_ipif_free_enumeration(names, nfound);
213 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
219 * Network functions used by user-land lnet acceptor
223 libcfs_sock_listen (int *sockp, __u32 local_ip, int local_port, int backlog)
227 struct sockaddr_in locaddr;
229 *sockp = socket(AF_INET, SOCK_STREAM, 0);
232 CERROR("socket() failed: errno==%d\n", errno);
237 if ( setsockopt(*sockp, SOL_SOCKET, SO_REUSEADDR,
238 (char *)&option, sizeof (option)) ) {
240 CERROR("setsockopt(SO_REUSEADDR) failed: errno==%d\n", errno);
244 if (local_ip != 0 || local_port != 0) {
245 memset(&locaddr, 0, sizeof(locaddr));
246 locaddr.sin_family = AF_INET;
247 locaddr.sin_port = htons(local_port);
248 locaddr.sin_addr.s_addr = (local_ip == 0) ?
249 INADDR_ANY : htonl(local_ip);
251 if ( bind(*sockp, (struct sockaddr *)&locaddr, sizeof(locaddr)) ) {
253 if ( errno == -EADDRINUSE )
254 CDEBUG(D_NET, "Port %d already in use\n",
257 CERROR("bind() to port %d failed: errno==%d\n",
263 if ( listen(*sockp, backlog) ) {
265 CERROR("listen() with backlog==%d failed: errno==%d\n",
278 libcfs_sock_accept (int *newsockp, int sock, __u32 *peer_ip, int *peer_port)
280 struct sockaddr_in accaddr;
281 socklen_t accaddr_len = sizeof(struct sockaddr_in);
283 *newsockp = accept(sock, (struct sockaddr *)&accaddr, &accaddr_len);
285 if ( *newsockp < 0 ) {
286 CERROR("accept() failed: errno==%d\n", errno);
290 *peer_ip = ntohl(accaddr.sin_addr.s_addr);
291 *peer_port = ntohs(accaddr.sin_port);
297 libcfs_sock_read (int sock, void *buffer, int nob, int timeout)
301 cfs_time_t start_time = cfs_time_current();
307 /* poll(2) measures timeout in msec */
310 while (nob != 0 && timeout > 0) {
311 cfs_time_t current_time;
313 rc = poll(&pfd, 1, timeout);
318 if ((pfd.revents & POLLIN) == 0)
321 rc = read(sock, buffer, nob);
327 buffer = ((char *)buffer) + rc;
330 current_time = cfs_time_current();
331 timeout -= cfs_duration_sec(cfs_time_sub(cfs_time_current(),
341 /* Just try to connect to localhost to wake up entity that are
342 * sleeping in accept() */
344 libcfs_sock_abort_accept(__u16 port)
347 struct sockaddr_in locaddr;
349 memset(&locaddr, 0, sizeof(locaddr));
350 locaddr.sin_family = AF_INET;
351 locaddr.sin_port = htons(port);
352 locaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
354 fd = socket(AF_INET, SOCK_STREAM, 0);
356 CERROR("socket() failed: errno==%d\n", errno);
360 rc = connect(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
362 if ( errno != ECONNREFUSED )
363 CERROR("connect() failed: errno==%d\n", errno);
365 CDEBUG(D_NET, "Nobody to wake up at %d\n", port);
372 * Network functions of common use
376 libcfs_getpeername(int sock_fd, __u32 *ipaddr_p, __u16 *port_p)
379 struct sockaddr_in peer_addr;
380 socklen_t peer_addr_len = sizeof(peer_addr);
382 rc = getpeername(sock_fd, (struct sockaddr *)&peer_addr, &peer_addr_len);
386 if (ipaddr_p != NULL)
387 *ipaddr_p = ntohl(peer_addr.sin_addr.s_addr);
389 *port_p = ntohs(peer_addr.sin_port);
395 libcfs_socketpair(int *fdp)
399 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fdp);
402 CERROR ("Cannot create socket pair\n");
406 for (i = 0; i < 2; i++) {
407 rc = libcfs_fcntl_nonblock(fdp[i]);
419 libcfs_fcntl_nonblock(int fd)
423 flags = fcntl(fd, F_GETFL, 0);
426 CERROR ("Cannot get socket flags\n");
430 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
433 CERROR ("Cannot set socket flags\n");
441 libcfs_sock_set_nagle(int fd, int nagle)
444 int option = nagle ? 0 : 1;
446 #if defined(__sun__) || defined(__sun)
447 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &option, sizeof(option));
449 rc = setsockopt(fd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
454 CERROR ("Cannot set NODELAY socket option\n");
462 libcfs_sock_set_bufsiz(int fd, int bufsiz)
466 LASSERT (bufsiz != 0);
469 rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &option, sizeof(option));
472 CERROR ("Cannot set SNDBUF socket option\n");
477 rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &option, sizeof(option));
480 CERROR ("Cannot set RCVBUF socket option\n");
488 libcfs_sock_create(int *fdp)
492 fd = socket(AF_INET, SOCK_STREAM, 0);
495 CERROR ("Cannot create socket\n");
500 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
501 &option, sizeof(option));
504 CERROR ("Cannot set SO_REUSEADDR for socket\n");
514 libcfs_sock_bind_to_port(int fd, __u16 port)
517 struct sockaddr_in locaddr;
519 memset(&locaddr, 0, sizeof(locaddr));
520 locaddr.sin_family = AF_INET;
521 locaddr.sin_addr.s_addr = INADDR_ANY;
522 locaddr.sin_port = htons(port);
524 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
527 CERROR ("Cannot bind to port %d\n", port);
535 libcfs_sock_connect(int fd, __u32 ip, __u16 port)
538 struct sockaddr_in addr;
540 memset(&addr, 0, sizeof(addr));
541 addr.sin_family = AF_INET;
542 addr.sin_addr.s_addr = htonl(ip);
543 addr.sin_port = htons(port);
545 rc = connect(fd, (struct sockaddr *)&addr,
546 sizeof(struct sockaddr_in));
548 if(rc != 0 && errno != EINPROGRESS) {
550 if (rc != -EADDRINUSE && rc != -EADDRNOTAVAIL)
551 CERROR ("Cannot connect to %u.%u.%u.%u:%d (err=%d)\n",
552 HIPQUAD(ip), port, errno);
559 /* NB: EPIPE and ECONNRESET are considered as non-fatal
561 * 1) it still makes sense to continue reading &&
562 * 2) anyway, poll() will set up POLLHUP|POLLERR flags */
563 int libcfs_sock_writev(int fd, const struct iovec *vector, int count)
567 rc = syscall(SYS_writev, fd, vector, count);
569 if (rc == 0) /* write nothing */
573 if (errno == EAGAIN || /* write nothing */
574 errno == EPIPE || /* non-fatal error */
575 errno == ECONNRESET) /* non-fatal error */
584 int libcfs_sock_readv(int fd, const struct iovec *vector, int count)
588 rc = syscall(SYS_readv, fd, vector, count);
590 if (rc == 0) /* EOF */
594 if (errno == EAGAIN) /* read nothing */
603 #endif /* !__KERNEL__ */