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 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
50 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
55 /* Caller may pass a zero timeout if she thinks the socket buffer is
56 * empty enough to take the whole message immediately */
64 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
68 /* Set send timeout to remaining time */
69 tv = (struct timeval) {
70 .tv_sec = jiffies_left /
71 msecs_to_jiffies(MSEC_PER_SEC),
72 .tv_usec = ((jiffies_left %
73 msecs_to_jiffies(MSEC_PER_SEC)) *
75 msecs_to_jiffies(MSEC_PER_SEC)
78 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
79 (char *)&tv, sizeof(tv));
81 CERROR("Can't set socket send timeout "
83 (long)tv.tv_sec, (int)tv.tv_usec, rc);
89 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
90 jiffies_left -= jiffies - then;
99 CERROR("Unexpected zero rc\n");
100 return -ECONNABORTED;
103 if (jiffies_left <= 0)
106 buffer = ((char *)buffer) + rc;
111 EXPORT_SYMBOL(lnet_sock_write);
114 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
117 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
122 LASSERT(jiffies_left > 0);
129 struct msghdr msg = {
133 /* Set receive timeout to remaining time */
134 tv = (struct timeval) {
135 .tv_sec = jiffies_left / msecs_to_jiffies(MSEC_PER_SEC),
136 .tv_usec = ((jiffies_left %
137 msecs_to_jiffies(MSEC_PER_SEC)) *
139 msecs_to_jiffies(MSEC_PER_SEC)
141 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
142 (char *)&tv, sizeof(tv));
144 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
145 (long)tv.tv_sec, (int)tv.tv_usec, rc);
150 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
151 jiffies_left -= jiffies - then;
159 buffer = ((char *)buffer) + rc;
165 if (jiffies_left <= 0)
169 EXPORT_SYMBOL(lnet_sock_read);
172 lnet_sock_create(struct socket **sockp, int *fatal,
173 __u32 local_ip, int local_port)
175 struct sockaddr_in locaddr;
180 /* All errors are fatal except bind failure if the port is in use */
183 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
184 rc = sock_create_kern(&init_net, PF_INET, SOCK_STREAM, 0, &sock);
186 rc = sock_create_kern(PF_INET, SOCK_STREAM, 0, &sock);
190 CERROR("Can't create socket: %d\n", rc);
195 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
196 (char *)&option, sizeof(option));
198 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
202 if (local_ip != 0 || local_port != 0) {
203 memset(&locaddr, 0, sizeof(locaddr));
204 locaddr.sin_family = AF_INET;
205 locaddr.sin_port = htons(local_port);
206 locaddr.sin_addr.s_addr = (local_ip == 0) ?
207 INADDR_ANY : htonl(local_ip);
209 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
211 if (rc == -EADDRINUSE) {
212 CDEBUG(D_NET, "Port %d already in use\n", local_port);
217 CERROR("Error trying to bind to port %d: %d\n",
230 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
235 if (txbufsize != 0) {
237 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
238 (char *)&option, sizeof(option));
240 CERROR("Can't set send buffer %d: %d\n",
246 if (rxbufsize != 0) {
248 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
249 (char *)&option, sizeof(option));
251 CERROR("Can't set receive buffer %d: %d\n",
258 EXPORT_SYMBOL(lnet_sock_setbuf);
261 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
263 struct sockaddr_in sin;
265 #ifndef HAVE_KERN_SOCK_GETNAME_2ARGS
266 int len = sizeof(sin);
270 rc = lnet_kernel_getpeername(sock,
271 (struct sockaddr *)&sin, &len);
273 rc = lnet_kernel_getsockname(sock,
274 (struct sockaddr *)&sin, &len);
276 CERROR("Error %d getting sock %s IP/port\n",
277 rc, remote ? "peer" : "local");
282 *ip = ntohl(sin.sin_addr.s_addr);
285 *port = ntohs(sin.sin_port);
289 EXPORT_SYMBOL(lnet_sock_getaddr);
292 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
294 if (txbufsize != NULL)
295 *txbufsize = sock->sk->sk_sndbuf;
297 if (rxbufsize != NULL)
298 *rxbufsize = sock->sk->sk_rcvbuf;
302 EXPORT_SYMBOL(lnet_sock_getbuf);
305 lnet_sock_listen(struct socket **sockp,
306 __u32 local_ip, int local_port, int backlog)
311 rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
314 CERROR("Can't create socket: port %d already in use\n",
319 rc = kernel_listen(*sockp, backlog);
323 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
324 sock_release(*sockp);
328 #ifndef HAVE_SK_SLEEP
329 static inline wait_queue_head_t *sk_sleep(struct sock *sk)
336 lnet_sock_accept(struct socket **newsockp, struct socket *sock)
338 wait_queue_entry_t wait;
339 struct socket *newsock;
342 /* XXX this should add a ref to sock->ops->owner, if
343 * TCP could be a module */
344 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
346 CERROR("Can't allocate socket\n");
350 newsock->ops = sock->ops;
352 #ifdef HAVE_KERN_SOCK_ACCEPT_FLAG_ARG
353 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
355 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
358 /* Nothing ready, so wait for activity */
359 init_waitqueue_entry(&wait, current);
360 add_wait_queue(sk_sleep(sock->sk), &wait);
361 set_current_state(TASK_INTERRUPTIBLE);
363 remove_wait_queue(sk_sleep(sock->sk), &wait);
364 #ifdef HAVE_KERN_SOCK_ACCEPT_FLAG_ARG
365 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
367 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
378 sock_release(newsock);
383 lnet_sock_connect(struct socket **sockp, int *fatal,
384 __u32 local_ip, int local_port,
385 __u32 peer_ip, int peer_port)
387 struct sockaddr_in srvaddr;
390 rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
394 memset(&srvaddr, 0, sizeof(srvaddr));
395 srvaddr.sin_family = AF_INET;
396 srvaddr.sin_port = htons(peer_port);
397 srvaddr.sin_addr.s_addr = htonl(peer_ip);
399 rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
404 /* EADDRNOTAVAIL probably means we're already connected to the same
405 * peer/port on the same local port on a differently typed
406 * connection. Let our caller retry with a different local
408 *fatal = !(rc == -EADDRNOTAVAIL);
410 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
411 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
412 &local_ip, local_port, &peer_ip, peer_port);
414 sock_release(*sockp);