4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
35 #if !defined(__KERNEL__) || !defined(REDSTORM)
37 #include <libcfs/libcfs.h>
39 #include <sys/socket.h>
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <sys/ioctl.h>
50 #include <arpa/inet.h>
53 #if defined(__sun__) || defined(__sun)
54 #include <sys/sockio.h>
56 #include <sys/syscall.h>
59 * Functions to get network interfaces info
63 libcfs_sock_ioctl(int cmd, unsigned long arg)
67 fd = socket(AF_INET, SOCK_STREAM, 0);
71 CERROR("socket() failed: errno==%d\n", errno);
75 rc = ioctl(fd, cmd, arg);
82 libcfs_ipif_query (char *name, int *up, __u32 *ip)
90 if (nob >= IFNAMSIZ) {
91 CERROR("Interface name %s too long\n", name);
95 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
97 strcpy(ifr.ifr_name, name);
98 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
101 CERROR("Can't get flags for interface %s\n", name);
105 if ((ifr.ifr_flags & IFF_UP) == 0) {
106 CDEBUG(D_NET, "Interface %s down\n", name);
114 strcpy(ifr.ifr_name, name);
115 ifr.ifr_addr.sa_family = AF_INET;
116 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
119 CERROR("Can't get IP address for interface %s\n", name);
123 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
130 libcfs_ipif_free_enumeration (char **names, int n)
136 for (i = 0; i < n && names[i] != NULL; i++)
137 LIBCFS_FREE(names[i], IFNAMSIZ);
139 LIBCFS_FREE(names, n * sizeof(*names));
143 libcfs_ipif_enumerate (char ***namesp)
145 /* Allocate and fill in 'names', returning # interfaces/error */
156 nalloc = 16; /* first guess at max interfaces */
158 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
160 CERROR ("ENOMEM enumerating up to %d interfaces\n",
166 ifc.ifc_buf = (char *)ifr;
167 ifc.ifc_len = nalloc * sizeof(*ifr);
169 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
172 CERROR ("Error %d enumerating interfaces\n", rc);
178 nfound = ifc.ifc_len/sizeof(*ifr);
179 LASSERT (nfound <= nalloc);
184 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
191 LIBCFS_ALLOC(names, nfound * sizeof(*names));
196 /* NULL out all names[i] */
197 memset (names, 0, nfound * sizeof(*names));
199 for (i = 0; i < nfound; i++) {
201 nob = strlen (ifr[i].ifr_name);
202 if (nob >= IFNAMSIZ) {
203 /* no space for terminating NULL */
204 CERROR("interface name %.*s too long (%d max)\n",
205 nob, ifr[i].ifr_name, IFNAMSIZ);
210 LIBCFS_ALLOC(names[i], IFNAMSIZ);
211 if (names[i] == NULL) {
216 memcpy(names[i], ifr[i].ifr_name, nob);
225 libcfs_ipif_free_enumeration(names, nfound);
227 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
233 * Network functions used by user-land lnet acceptor
237 libcfs_sock_listen (cfs_socket_t **sockp,
238 __u32 local_ip, int local_port, int backlog)
243 rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
247 if ( listen((*sockp)->s_fd, backlog) ) {
249 CERROR("listen() with backlog==%d failed: errno==%d\n",
257 libcfs_sock_release(*sockp);
262 libcfs_sock_release (cfs_socket_t *sock)
265 LIBCFS_FREE(sock, sizeof(cfs_socket_t));
269 libcfs_sock_accept (cfs_socket_t **newsockp, cfs_socket_t *sock)
271 struct sockaddr_in accaddr;
272 socklen_t accaddr_len = sizeof(struct sockaddr_in);
274 LIBCFS_ALLOC(*newsockp, sizeof(cfs_socket_t));
275 if (*newsockp == NULL) {
276 CERROR ("Can't alloc memory for cfs_socket_t\n");
280 (*newsockp)->s_fd = accept(sock->s_fd,
281 (struct sockaddr *)&accaddr, &accaddr_len);
283 if ( (*newsockp)->s_fd < 0 ) {
285 CERROR("accept() failed: errno==%d\n", -rc);
286 LIBCFS_FREE(*newsockp, sizeof(cfs_socket_t));
294 libcfs_sock_read (cfs_socket_t *sock, void *buffer, int nob, int timeout)
298 cfs_time_t start_time = cfs_time_current();
304 /* poll(2) measures timeout in msec */
307 while (nob != 0 && timeout > 0) {
308 cfs_time_t current_time;
310 rc = poll(&pfd, 1, timeout);
315 if ((pfd.revents & POLLIN) == 0)
318 rc = read(sock->s_fd, buffer, nob);
324 buffer = ((char *)buffer) + rc;
327 current_time = cfs_time_current();
329 cfs_duration_sec(cfs_time_sub(current_time,
331 start_time = current_time;
341 libcfs_sock_write (cfs_socket_t *sock, void *buffer, int nob, int timeout)
345 cfs_time_t start_time = cfs_time_current();
348 pfd.events = POLLOUT;
351 /* poll(2) measures timeout in msec */
354 while (nob != 0 && timeout > 0) {
355 cfs_time_t current_time;
357 rc = poll(&pfd, 1, timeout);
362 if ((pfd.revents & POLLOUT) == 0)
365 rc = write(sock->s_fd, buffer, nob);
371 buffer = ((char *)buffer) + rc;
374 current_time = cfs_time_current();
376 cfs_duration_sec(cfs_time_sub(current_time,
378 start_time = current_time;
387 /* Just try to connect to localhost to wake up entity that are
388 * sleeping in accept() */
390 libcfs_sock_abort_accept(cfs_socket_t *sock)
393 struct sockaddr_in remaddr;
394 struct sockaddr_in locaddr;
395 socklen_t alen = sizeof(struct sockaddr_in);
397 rc = getsockname(sock->s_fd, (struct sockaddr *)&remaddr, &alen);
399 CERROR("getsockname() failed: errno==%d\n", errno);
403 memset(&locaddr, 0, sizeof(locaddr));
404 locaddr.sin_family = AF_INET;
405 locaddr.sin_port = remaddr.sin_port;
406 locaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
408 fd = socket(AF_INET, SOCK_STREAM, 0);
410 CERROR("socket() failed: errno==%d\n", errno);
414 rc = connect(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
416 if ( errno != ECONNREFUSED )
417 CERROR("connect() failed: errno==%d\n", errno);
419 CDEBUG(D_NET, "Nobody to wake up at %d\n",
420 ntohs(remaddr.sin_port));
427 libcfs_sock_getaddr(cfs_socket_t *sock, int remote, __u32 *ip, int *port)
430 struct sockaddr_in peer_addr;
431 socklen_t peer_addr_len = sizeof(peer_addr);
433 LASSERT(remote == 1);
435 rc = getpeername(sock->s_fd,
436 (struct sockaddr *)&peer_addr, &peer_addr_len);
441 *ip = ntohl(peer_addr.sin_addr.s_addr);
443 *port = ntohs(peer_addr.sin_port);
449 * Network functions of common use
453 libcfs_socketpair(cfs_socket_t **sockp)
457 LIBCFS_ALLOC(sockp[0], sizeof(cfs_socket_t));
458 if (sockp[0] == NULL) {
459 CERROR ("Can't alloc memory for cfs_socket_t (1)\n");
463 LIBCFS_ALLOC(sockp[1], sizeof(cfs_socket_t));
464 if (sockp[1] == NULL) {
465 CERROR ("Can't alloc memory for cfs_socket_t (2)\n");
466 LIBCFS_FREE(sockp[0], sizeof(cfs_socket_t));
470 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fdp);
473 CERROR ("Cannot create socket pair\n");
474 LIBCFS_FREE(sockp[0], sizeof(cfs_socket_t));
475 LIBCFS_FREE(sockp[1], sizeof(cfs_socket_t));
479 sockp[0]->s_fd = fdp[0];
480 sockp[1]->s_fd = fdp[1];
482 for (i = 0; i < 2; i++) {
483 rc = libcfs_fcntl_nonblock(sockp[i]);
485 libcfs_sock_release(sockp[0]);
486 libcfs_sock_release(sockp[1]);
495 libcfs_fcntl_nonblock(cfs_socket_t *sock)
499 flags = fcntl(sock->s_fd, F_GETFL, 0);
502 CERROR ("Cannot get socket flags\n");
506 rc = fcntl(sock->s_fd, F_SETFL, flags | O_NONBLOCK);
509 CERROR ("Cannot set socket flags\n");
517 libcfs_sock_set_nagle(cfs_socket_t *sock, int nagle)
520 int option = nagle ? 0 : 1;
522 rc = setsockopt(sock->s_fd,
523 IPPROTO_TCP, TCP_NODELAY, &option, sizeof(option));
526 CERROR ("Cannot set NODELAY socket option\n");
534 libcfs_sock_set_bufsiz(cfs_socket_t *sock, int bufsiz)
538 LASSERT (bufsiz != 0);
541 rc = setsockopt(sock->s_fd,
542 SOL_SOCKET, SO_SNDBUF, &option, sizeof(option));
545 CERROR ("Cannot set SNDBUF socket option\n");
550 rc = setsockopt(sock->s_fd,
551 SOL_SOCKET, SO_RCVBUF, &option, sizeof(option));
554 CERROR ("Cannot set RCVBUF socket option\n");
562 libcfs_sock_bind(cfs_socket_t *sock, __u32 ip, __u16 port)
565 struct sockaddr_in locaddr;
567 if (ip == 0 && port == 0)
570 memset(&locaddr, 0, sizeof(locaddr));
571 locaddr.sin_family = AF_INET;
572 locaddr.sin_addr.s_addr = (ip == 0) ? INADDR_ANY : htonl(ip);
573 locaddr.sin_port = htons(port);
575 rc = bind(sock->s_fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
578 CERROR("Cannot bind to %d.%d.%d.%d %d: %d\n",
579 HIPQUAD(ip), port, rc);
587 libcfs_sock_create(cfs_socket_t **sockp, int *fatal,
588 __u32 local_ip, int local_port)
594 LIBCFS_ALLOC(*sockp, sizeof(cfs_socket_t));
595 if (*sockp == NULL) {
596 CERROR("Can't alloc memory for cfs_socket_t\n");
600 fd = socket(AF_INET, SOCK_STREAM, 0);
603 CERROR("Cannot create socket: %d\n", rc);
604 LIBCFS_FREE(*sockp, sizeof(cfs_socket_t));
611 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
612 &option, sizeof(option));
615 CERROR("Cannot set SO_REUSEADDR for socket: %d\n", rc);
616 libcfs_sock_release(*sockp);
620 rc = libcfs_sock_bind(*sockp, local_ip, local_port);
623 libcfs_sock_release(*sockp);
630 libcfs_sock_connect(cfs_socket_t *sock, __u32 ip, __u16 port)
633 struct sockaddr_in addr;
635 memset(&addr, 0, sizeof(addr));
636 addr.sin_family = AF_INET;
637 addr.sin_addr.s_addr = htonl(ip);
638 addr.sin_port = htons(port);
640 rc = connect(sock->s_fd, (struct sockaddr *)&addr,
641 sizeof(struct sockaddr_in));
643 if(rc != 0 && errno != EINPROGRESS) {
645 if (rc != -EADDRINUSE && rc != -EADDRNOTAVAIL)
646 CERROR ("Cannot connect to %u.%u.%u.%u:%d (err=%d)\n",
647 HIPQUAD(ip), port, errno);
654 /* NB: EPIPE and ECONNRESET are considered as non-fatal
656 * 1) it still makes sense to continue reading &&
657 * 2) anyway, poll() will set up POLLHUP|POLLERR flags */
659 libcfs_sock_writev(cfs_socket_t *sock, const struct iovec *vector, int count)
663 rc = syscall(SYS_writev, sock->s_fd, vector, count);
665 if (rc == 0) /* write nothing */
669 if (errno == EAGAIN || /* write nothing */
670 errno == EPIPE || /* non-fatal error */
671 errno == ECONNRESET) /* non-fatal error */
681 libcfs_sock_readv(cfs_socket_t *sock, const struct iovec *vector, int count)
685 rc = syscall(SYS_readv, sock->s_fd, vector, count);
687 if (rc == 0) /* EOF */
691 if (errno == EAGAIN) /* read nothing */
700 #endif /* !__KERNEL__ || !defined(REDSTORM) */