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 <net/addrconf.h>
39 #include <linux/file.h>
40 #include <linux/pagemap.h>
41 /* For sys_open & sys_close */
42 #include <linux/syscalls.h>
44 #include <linux/inetdevice.h>
46 #include <libcfs/linux/linux-time.h>
47 #include <libcfs/linux/linux-net.h>
48 #include <libcfs/libcfs.h>
49 #include <lnet/lib-lnet.h>
52 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
55 long jiffies_left = cfs_time_seconds(timeout);
59 /* Caller may pass a zero timeout if she thinks the socket buffer is
60 * empty enough to take the whole message immediately */
68 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
72 struct sock *sk = sock->sk;
74 /* Set send timeout to remaining time */
76 sk->sk_sndtimeo = jiffies_left;
81 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
82 jiffies_left -= jiffies - then;
91 CERROR("Unexpected zero rc\n");
95 if (jiffies_left <= 0)
98 buffer = ((char *)buffer) + rc;
103 EXPORT_SYMBOL(lnet_sock_write);
106 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
109 long jiffies_left = cfs_time_seconds(timeout);
113 LASSERT(jiffies_left > 0);
120 struct msghdr msg = {
123 struct sock *sk = sock->sk;
125 /* Set receive timeout to remaining time */
127 sk->sk_rcvtimeo = jiffies_left;
131 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
132 jiffies_left -= jiffies - then;
140 buffer = ((char *)buffer) + rc;
146 if (jiffies_left <= 0)
150 EXPORT_SYMBOL(lnet_sock_read);
152 int choose_ipv4_src(__u32 *ret, int interface, __u32 dst_ipaddr, struct net *ns)
154 struct net_device *dev;
155 struct in_device *in_dev;
157 DECLARE_CONST_IN_IFADDR(ifa);
160 dev = dev_get_by_index_rcu(ns, interface);
162 if (!dev || !(dev->flags & IFF_UP))
164 in_dev = __in_dev_get_rcu(dev);
168 in_dev_for_each_ifa_rcu(ifa, in_dev) {
170 ((dst_ipaddr ^ ntohl(ifa->ifa_local))
171 & ntohl(ifa->ifa_mask)) == 0) {
172 /* This address at least as good as what we
175 *ret = ntohl(ifa->ifa_local);
184 EXPORT_SYMBOL(choose_ipv4_src);
186 static struct socket *
187 lnet_sock_create(int interface, struct sockaddr *remaddr,
188 int local_port, struct net *ns)
196 family = remaddr->sa_family;
197 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
198 rc = sock_create_kern(ns, family, SOCK_STREAM, 0, &sock);
200 rc = sock_create_kern(family, SOCK_STREAM, 0, &sock);
203 CERROR("Can't create socket: %d\n", rc);
207 sock->sk->sk_reuseport = 1;
209 if (interface >= 0 || local_port != 0) {
210 struct sockaddr_storage locaddr = {};
211 struct sockaddr_in *sin = (void *)&locaddr;
212 struct sockaddr_in6 *sin6 = (void *)&locaddr;
216 sin->sin_family = AF_INET;
217 sin->sin_addr.s_addr = INADDR_ANY;
218 if (interface >= 0 && remaddr) {
219 struct sockaddr_in *rem = (void *)remaddr;
222 rc = choose_ipv4_src(&ip,
224 ntohl(rem->sin_addr.s_addr),
228 sin->sin_addr.s_addr = htonl(ip);
230 sin->sin_port = htons(local_port);
233 sin6->sin6_family = AF_INET6;
234 sin6->sin6_addr = in6addr_any;
235 if (interface >= 0 && remaddr) {
236 struct sockaddr_in6 *rem = (void *)remaddr;
238 ipv6_dev_get_saddr(ns,
244 sin6->sin6_port = htons(local_port);
247 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
249 if (rc == -EADDRINUSE) {
250 CDEBUG(D_NET, "Port %d already in use\n", local_port);
254 CERROR("Error trying to bind to port %d: %d\n",
267 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
269 struct sock *sk = sock->sk;
271 if (txbufsize != 0) {
272 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
273 sk->sk_sndbuf = txbufsize;
274 sk->sk_write_space(sk);
277 if (rxbufsize != 0) {
278 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
279 sk->sk_sndbuf = rxbufsize;
282 EXPORT_SYMBOL(lnet_sock_setbuf);
285 lnet_sock_getaddr(struct socket *sock, bool remote,
286 struct sockaddr_storage *peer)
289 #ifndef HAVE_KERN_SOCK_GETNAME_2ARGS
290 int len = sizeof(*peer);
294 rc = lnet_kernel_getpeername(sock,
295 (struct sockaddr *)peer, &len);
297 rc = lnet_kernel_getsockname(sock,
298 (struct sockaddr *)peer, &len);
300 CERROR("Error %d getting sock %s IP/port\n",
301 rc, remote ? "peer" : "local");
304 if (peer->ss_family == AF_INET6) {
305 struct sockaddr_in6 *in6 = (void *)peer;
306 struct sockaddr_in *in = (void *)peer;
307 short port = in6->sin6_port;
309 if (ipv6_addr_v4mapped(&in6->sin6_addr)) {
310 /* Pretend it is a v4 socket */
311 memset(in, 0, sizeof(*in));
312 in->sin_family = AF_INET;
314 memcpy(&in->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
319 EXPORT_SYMBOL(lnet_sock_getaddr);
321 void lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
323 if (txbufsize != NULL)
324 *txbufsize = sock->sk->sk_sndbuf;
326 if (rxbufsize != NULL)
327 *rxbufsize = sock->sk->sk_rcvbuf;
329 EXPORT_SYMBOL(lnet_sock_getbuf);
332 lnet_sock_listen(int local_port, int backlog, struct net *ns)
338 sock = lnet_sock_create(-1, NULL, local_port, ns);
341 if (rc == -EADDRINUSE)
342 CERROR("Can't create socket: port %d already in use\n",
347 /* Make sure we get both IPv4 and IPv6 connections.
348 * This is the default, but it can be overridden so
351 kernel_setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
352 (char *) &val, sizeof(val));
354 rc = kernel_listen(sock, backlog);
358 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
364 lnet_sock_connect(int interface, int local_port,
365 struct sockaddr *peeraddr,
371 sock = lnet_sock_create(interface, peeraddr, local_port, ns);
375 rc = kernel_connect(sock, peeraddr, sizeof(struct sockaddr_in6), 0);
379 /* EADDRNOTAVAIL probably means we're already connected to the same
380 * peer/port on the same local port on a differently typed
381 * connection. Let our caller retry with a different local
384 CDEBUG_LIMIT(rc == -EADDRNOTAVAIL ? D_NET : D_NETERROR,
385 "Error %d connecting %d -> %pISp\n", rc,
386 local_port, peeraddr);