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) 2015, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, 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 <libcfs/libcfs.h>
44 #include <lnet/lib-lnet.h>
47 * kernel 5.1: commit 7f1bc6e95d7840d4305595b3e4025cddda88cee5
49 * SO_TIMESTAMP, SO_TIMESTAMPNS and SO_TIMESTAMPING options, the
50 * way they are currently defined, are not y2038 safe.
51 * Subsequent patches in the series add new y2038 safe versions
52 * of these options which provide 64 bit timestamps on all
53 * architectures uniformly.
54 * Hence, rename existing options with OLD tag suffixes.
56 * NOTE: When updating to timespec64 change change these to '_NEW'.
60 #define SO_SNDTIMEO SO_SNDTIMEO_OLD
64 #define SO_RCVTIMEO SO_RCVTIMEO_OLD
68 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
71 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
76 /* Caller may pass a zero timeout if she thinks the socket buffer is
77 * empty enough to take the whole message immediately */
85 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
89 /* Set send timeout to remaining time */
90 tv = (struct timeval) {
91 .tv_sec = jiffies_left /
92 msecs_to_jiffies(MSEC_PER_SEC),
93 .tv_usec = ((jiffies_left %
94 msecs_to_jiffies(MSEC_PER_SEC)) *
96 msecs_to_jiffies(MSEC_PER_SEC)
99 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
100 (char *)&tv, sizeof(tv));
102 CERROR("Can't set socket send timeout "
104 (long)tv.tv_sec, (int)tv.tv_usec, rc);
110 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
111 jiffies_left -= jiffies - then;
120 CERROR("Unexpected zero rc\n");
121 return -ECONNABORTED;
124 if (jiffies_left <= 0)
127 buffer = ((char *)buffer) + rc;
132 EXPORT_SYMBOL(lnet_sock_write);
135 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
138 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
143 LASSERT(jiffies_left > 0);
150 struct msghdr msg = {
154 /* Set receive timeout to remaining time */
155 tv = (struct timeval) {
156 .tv_sec = jiffies_left / msecs_to_jiffies(MSEC_PER_SEC),
157 .tv_usec = ((jiffies_left %
158 msecs_to_jiffies(MSEC_PER_SEC)) *
160 msecs_to_jiffies(MSEC_PER_SEC)
162 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
163 (char *)&tv, sizeof(tv));
165 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
166 (long)tv.tv_sec, (int)tv.tv_usec, rc);
171 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
172 jiffies_left -= jiffies - then;
180 buffer = ((char *)buffer) + rc;
186 if (jiffies_left <= 0)
190 EXPORT_SYMBOL(lnet_sock_read);
193 lnet_sock_create(struct socket **sockp, int *fatal,
194 __u32 local_ip, int local_port)
196 struct sockaddr_in locaddr;
201 /* All errors are fatal except bind failure if the port is in use */
204 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
205 rc = sock_create_kern(&init_net, PF_INET, SOCK_STREAM, 0, &sock);
207 rc = sock_create_kern(PF_INET, SOCK_STREAM, 0, &sock);
211 CERROR("Can't create socket: %d\n", rc);
216 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
217 (char *)&option, sizeof(option));
219 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
223 if (local_ip != 0 || local_port != 0) {
224 memset(&locaddr, 0, sizeof(locaddr));
225 locaddr.sin_family = AF_INET;
226 locaddr.sin_port = htons(local_port);
227 locaddr.sin_addr.s_addr = (local_ip == 0) ?
228 INADDR_ANY : htonl(local_ip);
230 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
232 if (rc == -EADDRINUSE) {
233 CDEBUG(D_NET, "Port %d already in use\n", local_port);
238 CERROR("Error trying to bind to port %d: %d\n",
251 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
256 if (txbufsize != 0) {
258 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
259 (char *)&option, sizeof(option));
261 CERROR("Can't set send buffer %d: %d\n",
267 if (rxbufsize != 0) {
269 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
270 (char *)&option, sizeof(option));
272 CERROR("Can't set receive buffer %d: %d\n",
279 EXPORT_SYMBOL(lnet_sock_setbuf);
282 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
284 struct sockaddr_in sin;
286 #ifndef HAVE_KERN_SOCK_GETNAME_2ARGS
287 int len = sizeof(sin);
291 rc = lnet_kernel_getpeername(sock,
292 (struct sockaddr *)&sin, &len);
294 rc = lnet_kernel_getsockname(sock,
295 (struct sockaddr *)&sin, &len);
297 CERROR("Error %d getting sock %s IP/port\n",
298 rc, remote ? "peer" : "local");
303 *ip = ntohl(sin.sin_addr.s_addr);
306 *port = ntohs(sin.sin_port);
310 EXPORT_SYMBOL(lnet_sock_getaddr);
313 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
315 if (txbufsize != NULL)
316 *txbufsize = sock->sk->sk_sndbuf;
318 if (rxbufsize != NULL)
319 *rxbufsize = sock->sk->sk_rcvbuf;
323 EXPORT_SYMBOL(lnet_sock_getbuf);
326 lnet_sock_listen(struct socket **sockp,
327 __u32 local_ip, int local_port, int backlog)
332 rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
335 CERROR("Can't create socket: port %d already in use\n",
340 rc = kernel_listen(*sockp, backlog);
344 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
345 sock_release(*sockp);
349 #ifndef HAVE_SK_SLEEP
350 static inline wait_queue_head_t *sk_sleep(struct sock *sk)
357 lnet_sock_accept(struct socket **newsockp, struct socket *sock)
359 wait_queue_entry_t wait;
360 struct socket *newsock;
363 /* XXX this should add a ref to sock->ops->owner, if
364 * TCP could be a module */
365 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
367 CERROR("Can't allocate socket\n");
371 newsock->ops = sock->ops;
373 #ifdef HAVE_KERN_SOCK_ACCEPT_FLAG_ARG
374 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
376 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
379 /* Nothing ready, so wait for activity */
380 init_waitqueue_entry(&wait, current);
381 add_wait_queue(sk_sleep(sock->sk), &wait);
382 set_current_state(TASK_INTERRUPTIBLE);
384 remove_wait_queue(sk_sleep(sock->sk), &wait);
385 #ifdef HAVE_KERN_SOCK_ACCEPT_FLAG_ARG
386 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
388 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
399 sock_release(newsock);
404 lnet_sock_connect(struct socket **sockp, int *fatal,
405 __u32 local_ip, int local_port,
406 __u32 peer_ip, int peer_port)
408 struct sockaddr_in srvaddr;
411 rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
415 memset(&srvaddr, 0, sizeof(srvaddr));
416 srvaddr.sin_family = AF_INET;
417 srvaddr.sin_port = htons(peer_port);
418 srvaddr.sin_addr.s_addr = htonl(peer_ip);
420 rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
425 /* EADDRNOTAVAIL probably means we're already connected to the same
426 * peer/port on the same local port on a differently typed
427 * connection. Let our caller retry with a different local
429 *fatal = !(rc == -EADDRNOTAVAIL);
431 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
432 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
433 &local_ip, local_port, &peer_ip, peer_port);
435 sock_release(*sockp);