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 #include <sys/syscall.h>
56 * Functions to get network interfaces info
60 libcfs_sock_ioctl(int cmd, unsigned long arg)
64 fd = socket(AF_INET, SOCK_STREAM, 0);
68 CERROR("socket() failed: errno==%d\n", errno);
72 rc = ioctl(fd, cmd, arg);
79 libcfs_ipif_query (char *name, int *up, __u32 *ip)
87 if (nob >= IFNAMSIZ) {
88 CERROR("Interface name %s too long\n", name);
92 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
94 strcpy(ifr.ifr_name, name);
95 rc = libcfs_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
98 CERROR("Can't get flags for interface %s\n", name);
102 if ((ifr.ifr_flags & IFF_UP) == 0) {
103 CDEBUG(D_NET, "Interface %s down\n", name);
111 strcpy(ifr.ifr_name, name);
112 ifr.ifr_addr.sa_family = AF_INET;
113 rc = libcfs_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
116 CERROR("Can't get IP address for interface %s\n", name);
120 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
127 libcfs_ipif_free_enumeration (char **names, int n)
133 for (i = 0; i < n && names[i] != NULL; i++)
134 LIBCFS_FREE(names[i], IFNAMSIZ);
136 LIBCFS_FREE(names, n * sizeof(*names));
140 libcfs_ipif_enumerate (char ***namesp)
142 /* Allocate and fill in 'names', returning # interfaces/error */
153 nalloc = 16; /* first guess at max interfaces */
155 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
157 CERROR ("ENOMEM enumerating up to %d interfaces\n",
163 ifc.ifc_buf = (char *)ifr;
164 ifc.ifc_len = nalloc * sizeof(*ifr);
166 rc = libcfs_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
169 CERROR ("Error %d enumerating interfaces\n", rc);
175 nfound = ifc.ifc_len/sizeof(*ifr);
176 LASSERT (nfound <= nalloc);
181 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
188 LIBCFS_ALLOC(names, nfound * sizeof(*names));
193 /* NULL out all names[i] */
194 memset (names, 0, nfound * sizeof(*names));
196 for (i = 0; i < nfound; i++) {
198 nob = strlen (ifr[i].ifr_name);
199 if (nob >= IFNAMSIZ) {
200 /* no space for terminating NULL */
201 CERROR("interface name %.*s too long (%d max)\n",
202 nob, ifr[i].ifr_name, IFNAMSIZ);
207 LIBCFS_ALLOC(names[i], IFNAMSIZ);
208 if (names[i] == NULL) {
213 memcpy(names[i], ifr[i].ifr_name, nob);
222 libcfs_ipif_free_enumeration(names, nfound);
224 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
230 * Network functions used by user-land lnet acceptor
234 libcfs_sock_listen (cfs_socket_t **sockp,
235 __u32 local_ip, int local_port, int backlog)
240 rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
244 if ( listen((*sockp)->s_fd, backlog) ) {
246 CERROR("listen() with backlog==%d failed: errno==%d\n",
254 libcfs_sock_release(*sockp);
259 libcfs_sock_release (cfs_socket_t *sock)
262 LIBCFS_FREE(sock, sizeof(cfs_socket_t));
266 libcfs_sock_accept (cfs_socket_t **newsockp, cfs_socket_t *sock)
268 struct sockaddr_in accaddr;
269 socklen_t accaddr_len = sizeof(struct sockaddr_in);
271 LIBCFS_ALLOC(*newsockp, sizeof(cfs_socket_t));
272 if (*newsockp == NULL) {
273 CERROR ("Can't alloc memory for cfs_socket_t\n");
277 (*newsockp)->s_fd = accept(sock->s_fd,
278 (struct sockaddr *)&accaddr, &accaddr_len);
280 if ( (*newsockp)->s_fd < 0 ) {
282 CERROR("accept() failed: errno==%d\n", -rc);
283 LIBCFS_FREE(*newsockp, sizeof(cfs_socket_t));
291 libcfs_sock_read (cfs_socket_t *sock, void *buffer, int nob, int timeout)
295 cfs_time_t start_time = cfs_time_current();
301 /* poll(2) measures timeout in msec */
304 while (nob != 0 && timeout > 0) {
305 cfs_time_t current_time;
307 rc = poll(&pfd, 1, timeout);
312 if ((pfd.revents & POLLIN) == 0)
315 rc = read(sock->s_fd, buffer, nob);
321 buffer = ((char *)buffer) + rc;
324 current_time = cfs_time_current();
326 cfs_duration_sec(cfs_time_sub(current_time,
328 start_time = current_time;
338 libcfs_sock_write (cfs_socket_t *sock, void *buffer, int nob, int timeout)
342 cfs_time_t start_time = cfs_time_current();
345 pfd.events = POLLOUT;
348 /* poll(2) measures timeout in msec */
351 while (nob != 0 && timeout > 0) {
352 cfs_time_t current_time;
354 rc = poll(&pfd, 1, timeout);
359 if ((pfd.revents & POLLOUT) == 0)
362 rc = write(sock->s_fd, buffer, nob);
368 buffer = ((char *)buffer) + rc;
371 current_time = cfs_time_current();
373 cfs_duration_sec(cfs_time_sub(current_time,
375 start_time = current_time;
384 /* Just try to connect to localhost to wake up entity that are
385 * sleeping in accept() */
387 libcfs_sock_abort_accept(cfs_socket_t *sock)
390 struct sockaddr_in remaddr;
391 struct sockaddr_in locaddr;
392 socklen_t alen = sizeof(struct sockaddr_in);
394 rc = getsockname(sock->s_fd, (struct sockaddr *)&remaddr, &alen);
396 CERROR("getsockname() failed: errno==%d\n", errno);
400 memset(&locaddr, 0, sizeof(locaddr));
401 locaddr.sin_family = AF_INET;
402 locaddr.sin_port = remaddr.sin_port;
403 locaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
405 fd = socket(AF_INET, SOCK_STREAM, 0);
407 CERROR("socket() failed: errno==%d\n", errno);
411 rc = connect(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
413 if ( errno != ECONNREFUSED )
414 CERROR("connect() failed: errno==%d\n", errno);
416 CDEBUG(D_NET, "Nobody to wake up at %d\n",
417 ntohs(remaddr.sin_port));
424 libcfs_sock_getaddr(cfs_socket_t *sock, int remote, __u32 *ip, int *port)
427 struct sockaddr_in peer_addr;
428 socklen_t peer_addr_len = sizeof(peer_addr);
430 LASSERT(remote == 1);
432 rc = getpeername(sock->s_fd,
433 (struct sockaddr *)&peer_addr, &peer_addr_len);
438 *ip = ntohl(peer_addr.sin_addr.s_addr);
440 *port = ntohs(peer_addr.sin_port);
446 * Network functions of common use
450 libcfs_socketpair(cfs_socket_t **sockp)
454 LIBCFS_ALLOC(sockp[0], sizeof(cfs_socket_t));
455 if (sockp[0] == NULL) {
456 CERROR ("Can't alloc memory for cfs_socket_t (1)\n");
460 LIBCFS_ALLOC(sockp[1], sizeof(cfs_socket_t));
461 if (sockp[1] == NULL) {
462 CERROR ("Can't alloc memory for cfs_socket_t (2)\n");
463 LIBCFS_FREE(sockp[0], sizeof(cfs_socket_t));
467 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fdp);
470 CERROR ("Cannot create socket pair\n");
471 LIBCFS_FREE(sockp[0], sizeof(cfs_socket_t));
472 LIBCFS_FREE(sockp[1], sizeof(cfs_socket_t));
476 sockp[0]->s_fd = fdp[0];
477 sockp[1]->s_fd = fdp[1];
479 for (i = 0; i < 2; i++) {
480 rc = libcfs_fcntl_nonblock(sockp[i]);
482 libcfs_sock_release(sockp[0]);
483 libcfs_sock_release(sockp[1]);
492 libcfs_fcntl_nonblock(cfs_socket_t *sock)
496 flags = fcntl(sock->s_fd, F_GETFL, 0);
499 CERROR ("Cannot get socket flags\n");
503 rc = fcntl(sock->s_fd, F_SETFL, flags | O_NONBLOCK);
506 CERROR ("Cannot set socket flags\n");
514 libcfs_sock_set_nagle(cfs_socket_t *sock, int nagle)
517 int option = nagle ? 0 : 1;
519 rc = setsockopt(sock->s_fd,
520 IPPROTO_TCP, TCP_NODELAY, &option, sizeof(option));
523 CERROR ("Cannot set NODELAY socket option\n");
531 libcfs_sock_set_bufsiz(cfs_socket_t *sock, int bufsiz)
535 LASSERT (bufsiz != 0);
538 rc = setsockopt(sock->s_fd,
539 SOL_SOCKET, SO_SNDBUF, &option, sizeof(option));
542 CERROR ("Cannot set SNDBUF socket option\n");
547 rc = setsockopt(sock->s_fd,
548 SOL_SOCKET, SO_RCVBUF, &option, sizeof(option));
551 CERROR ("Cannot set RCVBUF socket option\n");
559 libcfs_sock_bind(cfs_socket_t *sock, __u32 ip, __u16 port)
562 struct sockaddr_in locaddr;
564 if (ip == 0 && port == 0)
567 memset(&locaddr, 0, sizeof(locaddr));
568 locaddr.sin_family = AF_INET;
569 locaddr.sin_addr.s_addr = (ip == 0) ? INADDR_ANY : htonl(ip);
570 locaddr.sin_port = htons(port);
572 rc = bind(sock->s_fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
575 CERROR("Cannot bind to %d.%d.%d.%d %d: %d\n",
576 HIPQUAD(ip), port, rc);
584 libcfs_sock_create(cfs_socket_t **sockp, int *fatal,
585 __u32 local_ip, int local_port)
591 LIBCFS_ALLOC(*sockp, sizeof(cfs_socket_t));
592 if (*sockp == NULL) {
593 CERROR("Can't alloc memory for cfs_socket_t\n");
597 fd = socket(AF_INET, SOCK_STREAM, 0);
600 CERROR("Cannot create socket: %d\n", rc);
601 LIBCFS_FREE(*sockp, sizeof(cfs_socket_t));
608 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
609 &option, sizeof(option));
612 CERROR("Cannot set SO_REUSEADDR for socket: %d\n", rc);
613 libcfs_sock_release(*sockp);
617 rc = libcfs_sock_bind(*sockp, local_ip, local_port);
620 libcfs_sock_release(*sockp);
627 libcfs_sock_connect(cfs_socket_t *sock, __u32 ip, __u16 port)
630 struct sockaddr_in addr;
632 memset(&addr, 0, sizeof(addr));
633 addr.sin_family = AF_INET;
634 addr.sin_addr.s_addr = htonl(ip);
635 addr.sin_port = htons(port);
637 rc = connect(sock->s_fd, (struct sockaddr *)&addr,
638 sizeof(struct sockaddr_in));
640 if(rc != 0 && errno != EINPROGRESS) {
642 if (rc != -EADDRINUSE && rc != -EADDRNOTAVAIL)
643 CERROR ("Cannot connect to %u.%u.%u.%u:%d (err=%d)\n",
644 HIPQUAD(ip), port, errno);
651 /* NB: EPIPE and ECONNRESET are considered as non-fatal
653 * 1) it still makes sense to continue reading &&
654 * 2) anyway, poll() will set up POLLHUP|POLLERR flags */
656 libcfs_sock_writev(cfs_socket_t *sock, const struct iovec *vector, int count)
660 rc = syscall(SYS_writev, sock->s_fd, vector, count);
662 if (rc == 0) /* write nothing */
666 if (errno == EAGAIN || /* write nothing */
667 errno == EPIPE || /* non-fatal error */
668 errno == ECONNRESET) /* non-fatal error */
678 libcfs_sock_readv(cfs_socket_t *sock, const struct iovec *vector, int count)
682 rc = syscall(SYS_readv, sock->s_fd, vector, count);
684 if (rc == 0) /* EOF */
688 if (errno == EAGAIN) /* read nothing */
697 #endif /* !__KERNEL__ || !defined(REDSTORM) */