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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Seagate, Inc.
32 #define DEBUG_SUBSYSTEM S_LNET
36 #include <linux/net.h>
37 #include <linux/file.h>
38 #include <linux/pagemap.h>
39 /* For sys_open & sys_close */
40 #include <linux/syscalls.h>
43 #include "../../include/linux/libcfs/libcfs.h"
44 #include "../../include/linux/lnet/lib-lnet.h"
47 kernel_sock_unlocked_ioctl(struct file
*filp
, int cmd
, unsigned long arg
)
49 mm_segment_t oldfs
= get_fs();
53 err
= filp
->f_op
->unlocked_ioctl(filp
, cmd
, arg
);
60 lnet_sock_ioctl(int cmd
, unsigned long arg
)
62 struct file
*sock_filp
;
66 rc
= sock_create(PF_INET
, SOCK_STREAM
, 0, &sock
);
68 CERROR("Can't create socket: %d\n", rc
);
72 sock_filp
= sock_alloc_file(sock
, 0, NULL
);
73 if (IS_ERR(sock_filp
)) {
75 rc
= PTR_ERR(sock_filp
);
79 rc
= kernel_sock_unlocked_ioctl(sock_filp
, cmd
, arg
);
87 lnet_ipif_query(char *name
, int *up
, __u32
*ip
, __u32
*mask
)
94 nob
= strnlen(name
, IFNAMSIZ
);
95 if (nob
== IFNAMSIZ
) {
96 CERROR("Interface name %s too long\n", name
);
100 CLASSERT(sizeof(ifr
.ifr_name
) >= IFNAMSIZ
);
102 if (strlen(name
) > sizeof(ifr
.ifr_name
) - 1)
104 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
106 rc
= lnet_sock_ioctl(SIOCGIFFLAGS
, (unsigned long)&ifr
);
108 CERROR("Can't get flags for interface %s\n", name
);
112 if (!(ifr
.ifr_flags
& IFF_UP
)) {
113 CDEBUG(D_NET
, "Interface %s down\n", name
);
120 if (strlen(name
) > sizeof(ifr
.ifr_name
) - 1)
122 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
124 ifr
.ifr_addr
.sa_family
= AF_INET
;
125 rc
= lnet_sock_ioctl(SIOCGIFADDR
, (unsigned long)&ifr
);
127 CERROR("Can't get IP address for interface %s\n", name
);
131 val
= ((struct sockaddr_in
*)&ifr
.ifr_addr
)->sin_addr
.s_addr
;
134 if (strlen(name
) > sizeof(ifr
.ifr_name
) - 1)
136 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
138 ifr
.ifr_addr
.sa_family
= AF_INET
;
139 rc
= lnet_sock_ioctl(SIOCGIFNETMASK
, (unsigned long)&ifr
);
141 CERROR("Can't get netmask for interface %s\n", name
);
145 val
= ((struct sockaddr_in
*)&ifr
.ifr_netmask
)->sin_addr
.s_addr
;
150 EXPORT_SYMBOL(lnet_ipif_query
);
153 lnet_ipif_enumerate(char ***namesp
)
155 /* Allocate and fill in 'names', returning # interfaces/error */
166 nalloc
= 16; /* first guess at max interfaces */
169 if (nalloc
* sizeof(*ifr
) > PAGE_SIZE
) {
171 nalloc
= PAGE_SIZE
/ sizeof(*ifr
);
172 CWARN("Too many interfaces: only enumerating first %d\n",
176 LIBCFS_ALLOC(ifr
, nalloc
* sizeof(*ifr
));
178 CERROR("ENOMEM enumerating up to %d interfaces\n",
184 ifc
.ifc_buf
= (char *)ifr
;
185 ifc
.ifc_len
= nalloc
* sizeof(*ifr
);
187 rc
= lnet_sock_ioctl(SIOCGIFCONF
, (unsigned long)&ifc
);
189 CERROR("Error %d enumerating interfaces\n", rc
);
195 nfound
= ifc
.ifc_len
/ sizeof(*ifr
);
196 LASSERT(nfound
<= nalloc
);
198 if (nfound
< nalloc
|| toobig
)
201 LIBCFS_FREE(ifr
, nalloc
* sizeof(*ifr
));
208 LIBCFS_ALLOC(names
, nfound
* sizeof(*names
));
214 for (i
= 0; i
< nfound
; i
++) {
215 nob
= strnlen(ifr
[i
].ifr_name
, IFNAMSIZ
);
216 if (nob
== IFNAMSIZ
) {
217 /* no space for terminating NULL */
218 CERROR("interface name %.*s too long (%d max)\n",
219 nob
, ifr
[i
].ifr_name
, IFNAMSIZ
);
224 LIBCFS_ALLOC(names
[i
], IFNAMSIZ
);
230 memcpy(names
[i
], ifr
[i
].ifr_name
, nob
);
239 lnet_ipif_free_enumeration(names
, nfound
);
241 LIBCFS_FREE(ifr
, nalloc
* sizeof(*ifr
));
245 EXPORT_SYMBOL(lnet_ipif_enumerate
);
248 lnet_ipif_free_enumeration(char **names
, int n
)
254 for (i
= 0; i
< n
&& names
[i
]; i
++)
255 LIBCFS_FREE(names
[i
], IFNAMSIZ
);
257 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 jiffies_left
= timeout
* msecs_to_jiffies(MSEC_PER_SEC
);
271 * Caller may pass a zero timeout if she thinks the socket buffer is
272 * empty enough to take the whole message immediately
279 struct msghdr msg
= {
280 .msg_flags
= !timeout
? MSG_DONTWAIT
: 0
284 /* Set send timeout to remaining time */
285 jiffies_to_timeval(jiffies_left
, &tv
);
286 rc
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_SNDTIMEO
,
287 (char *)&tv
, sizeof(tv
));
289 CERROR("Can't set socket send timeout %ld.%06d: %d\n",
290 (long)tv
.tv_sec
, (int)tv
.tv_usec
, rc
);
296 rc
= kernel_sendmsg(sock
, &msg
, &iov
, 1, nob
);
297 jiffies_left
-= jiffies
- then
;
306 CERROR("Unexpected zero rc\n");
307 return -ECONNABORTED
;
310 if (jiffies_left
<= 0)
313 buffer
= ((char *)buffer
) + rc
;
318 EXPORT_SYMBOL(lnet_sock_write
);
321 lnet_sock_read(struct socket
*sock
, void *buffer
, int nob
, int timeout
)
324 long jiffies_left
= timeout
* msecs_to_jiffies(MSEC_PER_SEC
);
329 LASSERT(jiffies_left
> 0);
336 struct msghdr msg
= {
340 /* Set receive timeout to remaining time */
341 jiffies_to_timeval(jiffies_left
, &tv
);
342 rc
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
,
343 (char *)&tv
, sizeof(tv
));
345 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
346 (long)tv
.tv_sec
, (int)tv
.tv_usec
, rc
);
351 rc
= kernel_recvmsg(sock
, &msg
, &iov
, 1, nob
, 0);
352 jiffies_left
-= jiffies
- then
;
360 buffer
= ((char *)buffer
) + rc
;
366 if (jiffies_left
<= 0)
370 EXPORT_SYMBOL(lnet_sock_read
);
373 lnet_sock_create(struct socket
**sockp
, int *fatal
, __u32 local_ip
,
376 struct sockaddr_in locaddr
;
381 /* All errors are fatal except bind failure if the port is in use */
384 rc
= sock_create(PF_INET
, SOCK_STREAM
, 0, &sock
);
387 CERROR("Can't create socket: %d\n", rc
);
392 rc
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
,
393 (char *)&option
, sizeof(option
));
395 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc
);
399 if (local_ip
|| local_port
) {
400 memset(&locaddr
, 0, sizeof(locaddr
));
401 locaddr
.sin_family
= AF_INET
;
402 locaddr
.sin_port
= htons(local_port
);
403 locaddr
.sin_addr
.s_addr
= !local_ip
?
404 INADDR_ANY
: htonl(local_ip
);
406 rc
= kernel_bind(sock
, (struct sockaddr
*)&locaddr
,
408 if (rc
== -EADDRINUSE
) {
409 CDEBUG(D_NET
, "Port %d already in use\n", local_port
);
414 CERROR("Error trying to bind to port %d: %d\n",
427 lnet_sock_setbuf(struct socket
*sock
, int txbufsize
, int rxbufsize
)
434 rc
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
,
435 (char *)&option
, sizeof(option
));
437 CERROR("Can't set send buffer %d: %d\n",
445 rc
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
,
446 (char *)&option
, sizeof(option
));
448 CERROR("Can't set receive buffer %d: %d\n",
455 EXPORT_SYMBOL(lnet_sock_setbuf
);
458 lnet_sock_getaddr(struct socket
*sock
, bool remote
, __u32
*ip
, int *port
)
460 struct sockaddr_in sin
;
461 int len
= sizeof(sin
);
465 rc
= kernel_getpeername(sock
, (struct sockaddr
*)&sin
, &len
);
467 rc
= kernel_getsockname(sock
, (struct sockaddr
*)&sin
, &len
);
469 CERROR("Error %d getting sock %s IP/port\n",
470 rc
, remote
? "peer" : "local");
475 *ip
= ntohl(sin
.sin_addr
.s_addr
);
478 *port
= ntohs(sin
.sin_port
);
482 EXPORT_SYMBOL(lnet_sock_getaddr
);
485 lnet_sock_getbuf(struct socket
*sock
, int *txbufsize
, int *rxbufsize
)
488 *txbufsize
= sock
->sk
->sk_sndbuf
;
491 *rxbufsize
= sock
->sk
->sk_rcvbuf
;
495 EXPORT_SYMBOL(lnet_sock_getbuf
);
498 lnet_sock_listen(struct socket
**sockp
, __u32 local_ip
, int local_port
,
504 rc
= lnet_sock_create(sockp
, &fatal
, local_ip
, local_port
);
507 CERROR("Can't create socket: port %d already in use\n",
512 rc
= kernel_listen(*sockp
, backlog
);
516 CERROR("Can't set listen backlog %d: %d\n", backlog
, rc
);
517 sock_release(*sockp
);
522 lnet_sock_accept(struct socket
**newsockp
, struct socket
*sock
)
525 struct socket
*newsock
;
529 * XXX this should add a ref to sock->ops->owner, if
530 * TCP could be a module
532 rc
= sock_create_lite(PF_PACKET
, sock
->type
, IPPROTO_TCP
, &newsock
);
534 CERROR("Can't allocate socket\n");
538 newsock
->ops
= sock
->ops
;
540 rc
= sock
->ops
->accept(sock
, newsock
, O_NONBLOCK
);
542 /* Nothing ready, so wait for activity */
543 init_waitqueue_entry(&wait
, current
);
544 add_wait_queue(sk_sleep(sock
->sk
), &wait
);
545 set_current_state(TASK_INTERRUPTIBLE
);
547 remove_wait_queue(sk_sleep(sock
->sk
), &wait
);
548 rc
= sock
->ops
->accept(sock
, newsock
, O_NONBLOCK
);
558 sock_release(newsock
);
563 lnet_sock_connect(struct socket
**sockp
, int *fatal
, __u32 local_ip
,
564 int local_port
, __u32 peer_ip
, int peer_port
)
566 struct sockaddr_in srvaddr
;
569 rc
= lnet_sock_create(sockp
, fatal
, local_ip
, local_port
);
573 memset(&srvaddr
, 0, sizeof(srvaddr
));
574 srvaddr
.sin_family
= AF_INET
;
575 srvaddr
.sin_port
= htons(peer_port
);
576 srvaddr
.sin_addr
.s_addr
= htonl(peer_ip
);
578 rc
= kernel_connect(*sockp
, (struct sockaddr
*)&srvaddr
,
584 * EADDRNOTAVAIL probably means we're already connected to the same
585 * peer/port on the same local port on a differently typed
586 * connection. Let our caller retry with a different local
589 *fatal
= !(rc
== -EADDRNOTAVAIL
);
591 CDEBUG_LIMIT(*fatal
? D_NETERROR
: D_NET
,
592 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc
,
593 &local_ip
, local_port
, &peer_ip
, peer_port
);
595 sock_release(*sockp
);