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.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 #define DEBUG_SUBSYSTEM S_LNET
40 #include <linux/net.h>
41 #include <linux/file.h>
42 #include <linux/pagemap.h>
43 /* For sys_open & sys_close */
44 #include <linux/syscalls.h>
47 #include "../../include/linux/libcfs/libcfs.h"
48 #include "../../include/linux/lnet/lib-lnet.h"
51 kernel_sock_unlocked_ioctl(struct file *filp, int cmd, unsigned long arg)
53 mm_segment_t oldfs = get_fs();
57 err = filp->f_op->unlocked_ioctl(filp, cmd, arg);
64 lnet_sock_ioctl(int cmd, unsigned long arg)
66 struct file *sock_filp;
70 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
72 CERROR ("Can't create socket: %d\n", rc);
76 sock_filp = sock_alloc_file(sock, 0, NULL);
77 if (IS_ERR(sock_filp)) {
79 rc = PTR_ERR(sock_filp);
83 rc = kernel_sock_unlocked_ioctl(sock_filp, cmd, arg);
91 lnet_ipif_query (char *name, int *up, __u32 *ip, __u32 *mask)
98 nob = strnlen(name, IFNAMSIZ);
99 if (nob == IFNAMSIZ) {
100 CERROR("Interface name %s too long\n", name);
104 CLASSERT (sizeof(ifr.ifr_name) >= IFNAMSIZ);
106 strcpy(ifr.ifr_name, name);
107 rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
109 CERROR("Can't get flags for interface %s\n", name);
113 if ((ifr.ifr_flags & IFF_UP) == 0) {
114 CDEBUG(D_NET, "Interface %s down\n", name);
122 strcpy(ifr.ifr_name, name);
123 ifr.ifr_addr.sa_family = AF_INET;
124 rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
126 CERROR("Can't get IP address for interface %s\n", name);
130 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
133 strcpy(ifr.ifr_name, name);
134 ifr.ifr_addr.sa_family = AF_INET;
135 rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
137 CERROR("Can't get netmask for interface %s\n", name);
141 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
147 EXPORT_SYMBOL(lnet_ipif_query);
150 lnet_ipif_enumerate (char ***namesp)
152 /* Allocate and fill in 'names', returning # interfaces/error */
164 nalloc = 16; /* first guess at max interfaces */
167 if (nalloc * sizeof(*ifr) > PAGE_CACHE_SIZE) {
169 nalloc = PAGE_CACHE_SIZE/sizeof(*ifr);
170 CWARN("Too many interfaces: only enumerating first %d\n",
174 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
176 CERROR ("ENOMEM enumerating up to %d interfaces\n", nalloc);
181 ifc.ifc_buf = (char *)ifr;
182 ifc.ifc_len = nalloc * sizeof(*ifr);
184 rc = lnet_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
186 CERROR ("Error %d enumerating interfaces\n", rc);
192 nfound = ifc.ifc_len/sizeof(*ifr);
193 LASSERT (nfound <= nalloc);
195 if (nfound < nalloc || toobig)
198 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
205 LIBCFS_ALLOC(names, nfound * sizeof(*names));
211 for (i = 0; i < nfound; i++) {
213 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
214 if (nob == IFNAMSIZ) {
215 /* no space for terminating NULL */
216 CERROR("interface name %.*s too long (%d max)\n",
217 nob, ifr[i].ifr_name, IFNAMSIZ);
222 LIBCFS_ALLOC(names[i], IFNAMSIZ);
223 if (names[i] == NULL) {
228 memcpy(names[i], ifr[i].ifr_name, nob);
237 lnet_ipif_free_enumeration(names, nfound);
239 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
244 EXPORT_SYMBOL(lnet_ipif_enumerate);
247 lnet_ipif_free_enumeration (char **names, int n)
253 for (i = 0; i < n && names[i] != NULL; i++)
254 LIBCFS_FREE(names[i], IFNAMSIZ);
256 LIBCFS_FREE(names, n * sizeof(*names));
259 EXPORT_SYMBOL(lnet_ipif_free_enumeration);
262 lnet_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
265 long ticks = timeout * HZ;
270 /* Caller may pass a zero timeout if she thinks the socket buffer is
271 * empty enough to take the whole message immediately */
278 struct msghdr msg = {
279 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
283 /* Set send timeout to remaining time */
284 tv = (struct timeval) {
285 .tv_sec = ticks / HZ,
286 .tv_usec = ((ticks % HZ) * 1000000) / HZ
288 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
289 (char *)&tv, sizeof(tv));
291 CERROR("Can't set socket send timeout %ld.%06d: %d\n",
292 (long)tv.tv_sec, (int)tv.tv_usec, rc);
298 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
299 ticks -= jiffies - then;
308 CERROR ("Unexpected zero rc\n");
309 return -ECONNABORTED;
315 buffer = ((char *)buffer) + rc;
321 EXPORT_SYMBOL(lnet_sock_write);
324 lnet_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
327 long ticks = timeout * HZ;
339 struct msghdr msg = {
343 /* Set receive timeout to remaining time */
344 tv = (struct timeval) {
345 .tv_sec = ticks / HZ,
346 .tv_usec = ((ticks % HZ) * 1000000) / HZ
348 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
349 (char *)&tv, sizeof(tv));
351 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
352 (long)tv.tv_sec, (int)tv.tv_usec, rc);
357 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
358 ticks -= jiffies - then;
366 buffer = ((char *)buffer) + rc;
377 EXPORT_SYMBOL(lnet_sock_read);
380 lnet_sock_create (struct socket **sockp, int *fatal,
381 __u32 local_ip, int local_port)
383 struct sockaddr_in locaddr;
388 /* All errors are fatal except bind failure if the port is in use */
391 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
394 CERROR ("Can't create socket: %d\n", rc);
399 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
400 (char *)&option, sizeof (option));
402 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
406 if (local_ip != 0 || local_port != 0) {
407 memset(&locaddr, 0, sizeof(locaddr));
408 locaddr.sin_family = AF_INET;
409 locaddr.sin_port = htons(local_port);
410 locaddr.sin_addr.s_addr = (local_ip == 0) ?
411 INADDR_ANY : htonl(local_ip);
413 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
415 if (rc == -EADDRINUSE) {
416 CDEBUG(D_NET, "Port %d already in use\n", local_port);
421 CERROR("Error trying to bind to port %d: %d\n",
435 lnet_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
440 if (txbufsize != 0) {
442 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
443 (char *)&option, sizeof (option));
445 CERROR ("Can't set send buffer %d: %d\n",
451 if (rxbufsize != 0) {
453 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
454 (char *)&option, sizeof (option));
456 CERROR ("Can't set receive buffer %d: %d\n",
465 EXPORT_SYMBOL(lnet_sock_setbuf);
468 lnet_sock_getaddr (struct socket *sock, bool remote, __u32 *ip, int *port)
470 struct sockaddr_in sin;
471 int len = sizeof (sin);
475 rc = kernel_getpeername(sock, (struct sockaddr *)&sin, &len);
477 rc = kernel_getsockname(sock, (struct sockaddr *)&sin, &len);
479 CERROR ("Error %d getting sock %s IP/port\n",
480 rc, remote ? "peer" : "local");
485 *ip = ntohl (sin.sin_addr.s_addr);
488 *port = ntohs (sin.sin_port);
493 EXPORT_SYMBOL(lnet_sock_getaddr);
496 lnet_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
499 if (txbufsize != NULL) {
500 *txbufsize = sock->sk->sk_sndbuf;
503 if (rxbufsize != NULL) {
504 *rxbufsize = sock->sk->sk_rcvbuf;
510 EXPORT_SYMBOL(lnet_sock_getbuf);
513 lnet_sock_listen (struct socket **sockp,
514 __u32 local_ip, int local_port, int backlog)
519 rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
522 CERROR("Can't create socket: port %d already in use\n",
527 rc = kernel_listen(*sockp, backlog);
531 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
532 sock_release(*sockp);
536 EXPORT_SYMBOL(lnet_sock_listen);
539 lnet_sock_accept (struct socket **newsockp, struct socket *sock)
542 struct socket *newsock;
545 init_waitqueue_entry(&wait, current);
547 /* XXX this should add a ref to sock->ops->owner, if
548 * TCP could be a module */
549 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
551 CERROR("Can't allocate socket\n");
555 newsock->ops = sock->ops;
557 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
559 /* Nothing ready, so wait for activity */
560 set_current_state(TASK_INTERRUPTIBLE);
561 add_wait_queue(sk_sleep(sock->sk), &wait);
563 remove_wait_queue(sk_sleep(sock->sk), &wait);
564 set_current_state(TASK_RUNNING);
565 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
575 sock_release(newsock);
579 EXPORT_SYMBOL(lnet_sock_accept);
582 lnet_sock_connect (struct socket **sockp, int *fatal,
583 __u32 local_ip, int local_port,
584 __u32 peer_ip, int peer_port)
586 struct sockaddr_in srvaddr;
589 rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
593 memset (&srvaddr, 0, sizeof (srvaddr));
594 srvaddr.sin_family = AF_INET;
595 srvaddr.sin_port = htons(peer_port);
596 srvaddr.sin_addr.s_addr = htonl(peer_ip);
598 rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
603 /* EADDRNOTAVAIL probably means we're already connected to the same
604 * peer/port on the same local port on a differently typed
605 * connection. Let our caller retry with a different local
607 *fatal = !(rc == -EADDRNOTAVAIL);
609 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
610 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
611 &local_ip, local_port, &peer_ip, peer_port);
613 sock_release(*sockp);
617 EXPORT_SYMBOL(lnet_sock_connect);