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 = cfs_time_seconds(timeout);
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 jiffies_to_timeval(jiffies_left, &tv);
91 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
92 (char *)&tv, sizeof(tv));
94 CERROR("Can't set socket send timeout "
96 (long)tv.tv_sec, (int)tv.tv_usec, rc);
102 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
103 jiffies_left -= jiffies - then;
112 CERROR("Unexpected zero rc\n");
113 return -ECONNABORTED;
116 if (jiffies_left <= 0)
119 buffer = ((char *)buffer) + rc;
124 EXPORT_SYMBOL(lnet_sock_write);
127 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
130 long jiffies_left = cfs_time_seconds(timeout);
135 LASSERT(jiffies_left > 0);
142 struct msghdr msg = {
146 /* Set receive timeout to remaining time */
147 jiffies_to_timeval(jiffies_left, &tv);
148 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
149 (char *)&tv, sizeof(tv));
151 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
152 (long)tv.tv_sec, (int)tv.tv_usec, rc);
157 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
158 jiffies_left -= jiffies - then;
166 buffer = ((char *)buffer) + rc;
172 if (jiffies_left <= 0)
176 EXPORT_SYMBOL(lnet_sock_read);
179 lnet_sock_create(struct socket **sockp,
180 __u32 local_ip, int local_port, struct net *ns)
182 struct sockaddr_in locaddr;
187 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
188 rc = sock_create_kern(ns, PF_INET, SOCK_STREAM, 0, &sock);
190 rc = sock_create_kern(PF_INET, SOCK_STREAM, 0, &sock);
194 CERROR("Can't create socket: %d\n", rc);
199 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
200 (char *)&option, sizeof(option));
202 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
206 if (local_ip != 0 || local_port != 0) {
207 memset(&locaddr, 0, sizeof(locaddr));
208 locaddr.sin_family = AF_INET;
209 locaddr.sin_port = htons(local_port);
210 locaddr.sin_addr.s_addr = (local_ip == 0) ?
211 INADDR_ANY : htonl(local_ip);
213 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
215 if (rc == -EADDRINUSE) {
216 CDEBUG(D_NET, "Port %d already in use\n", local_port);
220 CERROR("Error trying to bind to port %d: %d\n",
233 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
238 if (txbufsize != 0) {
240 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
241 (char *)&option, sizeof(option));
243 CERROR("Can't set send buffer %d: %d\n",
249 if (rxbufsize != 0) {
251 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
252 (char *)&option, sizeof(option));
254 CERROR("Can't set receive buffer %d: %d\n",
261 EXPORT_SYMBOL(lnet_sock_setbuf);
264 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
266 struct sockaddr_in sin;
268 #ifndef HAVE_KERN_SOCK_GETNAME_2ARGS
269 int len = sizeof(sin);
273 rc = lnet_kernel_getpeername(sock,
274 (struct sockaddr *)&sin, &len);
276 rc = lnet_kernel_getsockname(sock,
277 (struct sockaddr *)&sin, &len);
279 CERROR("Error %d getting sock %s IP/port\n",
280 rc, remote ? "peer" : "local");
285 *ip = ntohl(sin.sin_addr.s_addr);
288 *port = ntohs(sin.sin_port);
292 EXPORT_SYMBOL(lnet_sock_getaddr);
295 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
297 if (txbufsize != NULL)
298 *txbufsize = sock->sk->sk_sndbuf;
300 if (rxbufsize != NULL)
301 *rxbufsize = sock->sk->sk_rcvbuf;
305 EXPORT_SYMBOL(lnet_sock_getbuf);
308 lnet_sock_listen(struct socket **sockp,
309 __u32 local_ip, int local_port, int backlog, struct net *ns)
313 rc = lnet_sock_create(sockp, local_ip, local_port, ns);
315 if (rc == -EADDRINUSE)
316 CERROR("Can't create socket: port %d already in use\n",
321 rc = kernel_listen(*sockp, backlog);
325 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
326 sock_release(*sockp);
331 lnet_sock_connect(struct socket **sockp,
332 __u32 local_ip, int local_port,
333 __u32 peer_ip, int peer_port,
336 struct sockaddr_in srvaddr;
339 rc = lnet_sock_create(sockp, local_ip, local_port, ns);
343 memset(&srvaddr, 0, sizeof(srvaddr));
344 srvaddr.sin_family = AF_INET;
345 srvaddr.sin_port = htons(peer_port);
346 srvaddr.sin_addr.s_addr = htonl(peer_ip);
348 rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
353 /* EADDRNOTAVAIL probably means we're already connected to the same
354 * peer/port on the same local port on a differently typed
355 * connection. Let our caller retry with a different local
358 CDEBUG_LIMIT(rc == -EADDRNOTAVAIL ? D_NET : D_NETERROR,
359 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
360 &local_ip, local_port, &peer_ip, peer_port);
362 sock_release(*sockp);