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>
42 #include <linux/inetdevice.h>
44 #include <libcfs/linux/linux-time.h>
45 #include <libcfs/libcfs.h>
46 #include <lnet/lib-lnet.h>
49 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
52 long jiffies_left = cfs_time_seconds(timeout);
56 /* Caller may pass a zero timeout if she thinks the socket buffer is
57 * empty enough to take the whole message immediately */
65 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
69 struct sock *sk = sock->sk;
71 /* Set send timeout to remaining time */
73 sk->sk_sndtimeo = jiffies_left;
78 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
79 jiffies_left -= jiffies - then;
88 CERROR("Unexpected zero rc\n");
92 if (jiffies_left <= 0)
95 buffer = ((char *)buffer) + rc;
100 EXPORT_SYMBOL(lnet_sock_write);
103 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
106 long jiffies_left = cfs_time_seconds(timeout);
110 LASSERT(jiffies_left > 0);
117 struct msghdr msg = {
120 struct sock *sk = sock->sk;
122 /* Set receive timeout to remaining time */
124 sk->sk_rcvtimeo = jiffies_left;
128 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
129 jiffies_left -= jiffies - then;
137 buffer = ((char *)buffer) + rc;
143 if (jiffies_left <= 0)
147 EXPORT_SYMBOL(lnet_sock_read);
149 int choose_ipv4_src(__u32 *ret, int interface, __u32 dst_ipaddr, struct net *ns)
151 struct net_device *dev;
152 struct in_device *in_dev;
154 DECLARE_CONST_IN_IFADDR(ifa);
157 dev = dev_get_by_index_rcu(ns, interface);
159 if (!dev || !(dev->flags & IFF_UP))
161 in_dev = __in_dev_get_rcu(dev);
165 in_dev_for_each_ifa_rcu(ifa, in_dev) {
167 ((dst_ipaddr ^ ntohl(ifa->ifa_local))
168 & ntohl(ifa->ifa_mask)) == 0) {
169 /* This address at least as good as what we
172 *ret = ntohl(ifa->ifa_local);
181 EXPORT_SYMBOL(choose_ipv4_src);
183 static struct socket *
184 lnet_sock_create(int interface, struct sockaddr *remaddr,
185 int local_port, struct net *ns)
191 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
192 rc = sock_create_kern(ns, PF_INET, SOCK_STREAM, 0, &sock);
194 rc = sock_create_kern(PF_INET, SOCK_STREAM, 0, &sock);
197 CERROR("Can't create socket: %d\n", rc);
202 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
203 (char *)&option, sizeof(option));
205 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
209 if (interface >= 0 || local_port != 0) {
210 struct sockaddr_in locaddr = {};
212 locaddr.sin_family = AF_INET;
213 locaddr.sin_addr.s_addr = INADDR_ANY;
214 if (interface >= 0) {
215 struct sockaddr_in *sin = (void *)remaddr;
218 rc = choose_ipv4_src(&ip,
220 ntohl(sin->sin_addr.s_addr),
224 locaddr.sin_addr.s_addr = htonl(ip);
227 locaddr.sin_port = htons(local_port);
229 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
231 if (rc == -EADDRINUSE) {
232 CDEBUG(D_NET, "Port %d already in use\n", local_port);
236 CERROR("Error trying to bind to port %d: %d\n",
249 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
254 if (txbufsize != 0) {
256 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
257 (char *)&option, sizeof(option));
259 CERROR("Can't set send buffer %d: %d\n",
265 if (rxbufsize != 0) {
267 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
268 (char *)&option, sizeof(option));
270 CERROR("Can't set receive buffer %d: %d\n",
277 EXPORT_SYMBOL(lnet_sock_setbuf);
280 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
282 struct sockaddr_in sin;
284 #ifndef HAVE_KERN_SOCK_GETNAME_2ARGS
285 int len = sizeof(sin);
289 rc = lnet_kernel_getpeername(sock,
290 (struct sockaddr *)&sin, &len);
292 rc = lnet_kernel_getsockname(sock,
293 (struct sockaddr *)&sin, &len);
295 CERROR("Error %d getting sock %s IP/port\n",
296 rc, remote ? "peer" : "local");
301 *ip = ntohl(sin.sin_addr.s_addr);
304 *port = ntohs(sin.sin_port);
308 EXPORT_SYMBOL(lnet_sock_getaddr);
311 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
313 if (txbufsize != NULL)
314 *txbufsize = sock->sk->sk_sndbuf;
316 if (rxbufsize != NULL)
317 *rxbufsize = sock->sk->sk_rcvbuf;
321 EXPORT_SYMBOL(lnet_sock_getbuf);
324 lnet_sock_listen(int local_port, int backlog, struct net *ns)
329 sock = lnet_sock_create(-1, NULL, local_port, ns);
332 if (rc == -EADDRINUSE)
333 CERROR("Can't create socket: port %d already in use\n",
338 rc = kernel_listen(sock, backlog);
342 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
348 lnet_sock_connect(int interface, int local_port,
349 struct sockaddr *peeraddr,
355 sock = lnet_sock_create(interface, peeraddr, local_port, ns);
359 rc = kernel_connect(sock, peeraddr, sizeof(struct sockaddr_in), 0);
363 /* EADDRNOTAVAIL probably means we're already connected to the same
364 * peer/port on the same local port on a differently typed
365 * connection. Let our caller retry with a different local
368 CDEBUG_LIMIT(rc == -EADDRNOTAVAIL ? D_NET : D_NETERROR,
369 "Error %d connecting %d -> %pISp\n", rc,
370 local_port, peeraddr);