Whamcloud - gitweb
LU-9210 statahead: missing barrier before wake_up
[fs/lustre-release.git] / lnet / lnet / lib-socket.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2015, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 #define DEBUG_SUBSYSTEM S_LNET
33
34 #include <linux/if.h>
35 #include <linux/in.h>
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>
41 #include <net/sock.h>
42
43 #include <libcfs/libcfs.h>
44 #include <lnet/lib-lnet.h>
45
46 static int
47 kernel_sock_unlocked_ioctl(struct file *filp, int cmd, unsigned long arg)
48 {
49         mm_segment_t oldfs = get_fs();
50         int err;
51
52         set_fs(KERNEL_DS);
53         err = filp->f_op->unlocked_ioctl(filp, cmd, arg);
54         set_fs(oldfs);
55
56         return err;
57 }
58
59 static int
60 lnet_sock_ioctl(int cmd, unsigned long arg)
61 {
62         struct file    *sock_filp;
63         struct socket  *sock;
64         int             fd = -1;
65         int             rc;
66
67 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
68         rc = sock_create_kern(&init_net, PF_INET, SOCK_STREAM, 0, &sock);
69 #else
70         rc = sock_create_kern(PF_INET, SOCK_STREAM, 0, &sock);
71 #endif
72         if (rc != 0) {
73                 CERROR("Can't create socket: %d\n", rc);
74                 return rc;
75         }
76
77 #if !defined(HAVE_SOCK_ALLOC_FILE) && !defined(HAVE_SOCK_ALLOC_FILE_3ARGS)
78         fd = sock_map_fd(sock, 0);
79         if (fd < 0) {
80                 rc = fd;
81                 sock_release(sock);
82                 goto out;
83         }
84         sock_filp = fget(fd);
85 #else
86 # ifdef HAVE_SOCK_ALLOC_FILE_3ARGS
87         sock_filp = sock_alloc_file(sock, 0, NULL);
88 # else
89         sock_filp = sock_alloc_file(sock, 0);
90 # endif
91 #endif
92         if (IS_ERR(sock_filp)) {
93                 rc = PTR_ERR(sock_filp);
94                 sock_release(sock);
95                 goto out;
96         }
97
98         rc = kernel_sock_unlocked_ioctl(sock_filp, cmd, arg);
99
100         fput(sock_filp);
101 out:
102         if (fd >= 0)
103                 sys_close(fd);
104         return rc;
105 }
106
107 int
108 lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask)
109 {
110         struct ifreq    ifr;
111         int             nob;
112         int             rc;
113         __u32           val;
114
115         nob = strnlen(name, IFNAMSIZ);
116         if (nob == IFNAMSIZ) {
117                 CERROR("Interface name %s too long\n", name);
118                 return -EINVAL;
119         }
120
121         CLASSERT(sizeof(ifr.ifr_name) >= IFNAMSIZ);
122
123         if (strlen(name) > sizeof(ifr.ifr_name)-1)
124                 return -E2BIG;
125         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
126
127         rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
128         if (rc != 0) {
129                 CERROR("Can't get flags for interface %s\n", name);
130                 return rc;
131         }
132
133         if ((ifr.ifr_flags & IFF_UP) == 0) {
134                 CDEBUG(D_NET, "Interface %s down\n", name);
135                 *up = 0;
136                 *ip = *mask = 0;
137                 return 0;
138         }
139         *up = 1;
140
141         if (strlen(name) > sizeof(ifr.ifr_name)-1)
142                 return -E2BIG;
143         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
144
145         ifr.ifr_addr.sa_family = AF_INET;
146         rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
147
148         if (rc != 0) {
149                 CERROR("Can't get IP address for interface %s\n", name);
150                 return rc;
151         }
152
153         val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
154         *ip = ntohl(val);
155
156         if (strlen(name) > sizeof(ifr.ifr_name)-1)
157                 return -E2BIG;
158         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
159
160         ifr.ifr_addr.sa_family = AF_INET;
161         rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
162         if (rc != 0) {
163                 CERROR("Can't get netmask for interface %s\n", name);
164                 return rc;
165         }
166
167         val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
168         *mask = ntohl(val);
169
170         return 0;
171 }
172 EXPORT_SYMBOL(lnet_ipif_query);
173
174 void
175 lnet_ipif_free_enumeration(char **names, int n)
176 {
177         int     i;
178
179         LASSERT(n > 0);
180
181         for (i = 0; i < n && names[i] != NULL; i++)
182                 LIBCFS_FREE(names[i], IFNAMSIZ);
183
184         LIBCFS_FREE(names, n * sizeof(*names));
185 }
186 EXPORT_SYMBOL(lnet_ipif_free_enumeration);
187
188 int
189 lnet_ipif_enumerate(char ***namesp)
190 {
191         /* Allocate and fill in 'names', returning # interfaces/error */
192         char          **names;
193         int             toobig;
194         int             nalloc;
195         int             nfound;
196         struct ifreq   *ifr;
197         struct ifconf   ifc;
198         int             rc;
199         int             nob;
200         int             i;
201
202         nalloc = 16;    /* first guess at max interfaces */
203         toobig = 0;
204         for (;;) {
205                 if (nalloc * sizeof(*ifr) > PAGE_SIZE) {
206                         toobig = 1;
207                         nalloc = PAGE_SIZE / sizeof(*ifr);
208                         CWARN("Too many interfaces: only enumerating "
209                               "first %d\n", nalloc);
210                 }
211
212                 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
213                 if (ifr == NULL) {
214                         CERROR("ENOMEM enumerating up to %d interfaces\n",
215                                nalloc);
216                         rc = -ENOMEM;
217                         goto out0;
218                 }
219
220                 ifc.ifc_buf = (char *)ifr;
221                 ifc.ifc_len = nalloc * sizeof(*ifr);
222
223                 rc = lnet_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
224                 if (rc < 0) {
225                         CERROR("Error %d enumerating interfaces\n", rc);
226                         goto out1;
227                 }
228
229                 LASSERT(rc == 0);
230
231                 nfound = ifc.ifc_len/sizeof(*ifr);
232                 LASSERT(nfound <= nalloc);
233
234                 if (nfound < nalloc || toobig)
235                         break;
236
237                 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
238                 nalloc *= 2;
239         }
240
241         if (nfound == 0)
242                 goto out1;
243
244         LIBCFS_ALLOC(names, nfound * sizeof(*names));
245         if (names == NULL) {
246                 rc = -ENOMEM;
247                 goto out1;
248         }
249
250         for (i = 0; i < nfound; i++) {
251                 nob = strnlen(ifr[i].ifr_name, IFNAMSIZ);
252                 if (nob == IFNAMSIZ) {
253                         /* no space for terminating NULL */
254                         CERROR("interface name %.*s too long (%d max)\n",
255                                nob, ifr[i].ifr_name, IFNAMSIZ);
256                         rc = -ENAMETOOLONG;
257                         goto out2;
258                 }
259
260                 LIBCFS_ALLOC(names[i], IFNAMSIZ);
261                 if (names[i] == NULL) {
262                         rc = -ENOMEM;
263                         goto out2;
264                 }
265
266                 memcpy(names[i], ifr[i].ifr_name, nob);
267                 names[i][nob] = 0;
268         }
269
270         *namesp = names;
271         rc = nfound;
272
273  out2:
274         if (rc < 0)
275                 lnet_ipif_free_enumeration(names, nfound);
276  out1:
277         LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
278  out0:
279         return rc;
280 }
281 EXPORT_SYMBOL(lnet_ipif_enumerate);
282
283 int
284 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
285 {
286         int             rc;
287         long            jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
288         unsigned long   then;
289         struct timeval  tv;
290
291         LASSERT(nob > 0);
292         /* Caller may pass a zero timeout if she thinks the socket buffer is
293          * empty enough to take the whole message immediately */
294
295         for (;;) {
296                 struct kvec  iov = {
297                         .iov_base = buffer,
298                         .iov_len  = nob
299                 };
300                 struct msghdr msg = {
301                         .msg_flags      = (timeout == 0) ? MSG_DONTWAIT : 0
302                 };
303
304                 if (timeout != 0) {
305                         /* Set send timeout to remaining time */
306                         tv = (struct timeval) {
307                                 .tv_sec = jiffies_left /
308                                           msecs_to_jiffies(MSEC_PER_SEC),
309                                 .tv_usec = ((jiffies_left %
310                                              msecs_to_jiffies(MSEC_PER_SEC)) *
311                                              USEC_PER_SEC) /
312                                              msecs_to_jiffies(MSEC_PER_SEC)
313                         };
314
315                         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
316                                                (char *)&tv, sizeof(tv));
317                         if (rc != 0) {
318                                 CERROR("Can't set socket send timeout "
319                                        "%ld.%06d: %d\n",
320                                        (long)tv.tv_sec, (int)tv.tv_usec, rc);
321                                 return rc;
322                         }
323                 }
324
325                 then = jiffies;
326                 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
327                 jiffies_left -= jiffies - then;
328
329                 if (rc == nob)
330                         return 0;
331
332                 if (rc < 0)
333                         return rc;
334
335                 if (rc == 0) {
336                         CERROR("Unexpected zero rc\n");
337                         return -ECONNABORTED;
338                 }
339
340                 if (jiffies_left <= 0)
341                         return -EAGAIN;
342
343                 buffer = ((char *)buffer) + rc;
344                 nob -= rc;
345         }
346         return 0;
347 }
348 EXPORT_SYMBOL(lnet_sock_write);
349
350 int
351 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
352 {
353         int             rc;
354         long            jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
355         unsigned long   then;
356         struct timeval  tv;
357
358         LASSERT(nob > 0);
359         LASSERT(jiffies_left > 0);
360
361         for (;;) {
362                 struct kvec  iov = {
363                         .iov_base = buffer,
364                         .iov_len  = nob
365                 };
366                 struct msghdr msg = {
367                         .msg_flags      = 0
368                 };
369
370                 /* Set receive timeout to remaining time */
371                 tv = (struct timeval) {
372                         .tv_sec = jiffies_left / msecs_to_jiffies(MSEC_PER_SEC),
373                         .tv_usec = ((jiffies_left %
374                                         msecs_to_jiffies(MSEC_PER_SEC)) *
375                                         USEC_PER_SEC) /
376                                         msecs_to_jiffies(MSEC_PER_SEC)
377                 };
378                 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
379                                        (char *)&tv, sizeof(tv));
380                 if (rc != 0) {
381                         CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
382                                (long)tv.tv_sec, (int)tv.tv_usec, rc);
383                         return rc;
384                 }
385
386                 then = jiffies;
387                 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
388                 jiffies_left -= jiffies - then;
389
390                 if (rc < 0)
391                         return rc;
392
393                 if (rc == 0)
394                         return -ECONNRESET;
395
396                 buffer = ((char *)buffer) + rc;
397                 nob -= rc;
398
399                 if (nob == 0)
400                         return 0;
401
402                 if (jiffies_left <= 0)
403                         return -ETIMEDOUT;
404         }
405 }
406 EXPORT_SYMBOL(lnet_sock_read);
407
408 static int
409 lnet_sock_create(struct socket **sockp, int *fatal,
410                  __u32 local_ip, int local_port)
411 {
412         struct sockaddr_in  locaddr;
413         struct socket      *sock;
414         int                 rc;
415         int                 option;
416
417         /* All errors are fatal except bind failure if the port is in use */
418         *fatal = 1;
419
420 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
421         rc = sock_create_kern(&init_net, PF_INET, SOCK_STREAM, 0, &sock);
422 #else
423         rc = sock_create_kern(PF_INET, SOCK_STREAM, 0, &sock);
424 #endif
425         *sockp = sock;
426         if (rc != 0) {
427                 CERROR("Can't create socket: %d\n", rc);
428                 return rc;
429         }
430
431         option = 1;
432         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
433                                (char *)&option, sizeof(option));
434         if (rc != 0) {
435                 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
436                 goto failed;
437         }
438
439         if (local_ip != 0 || local_port != 0) {
440                 memset(&locaddr, 0, sizeof(locaddr));
441                 locaddr.sin_family = AF_INET;
442                 locaddr.sin_port = htons(local_port);
443                 locaddr.sin_addr.s_addr = (local_ip == 0) ?
444                                           INADDR_ANY : htonl(local_ip);
445
446                 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
447                                  sizeof(locaddr));
448                 if (rc == -EADDRINUSE) {
449                         CDEBUG(D_NET, "Port %d already in use\n", local_port);
450                         *fatal = 0;
451                         goto failed;
452                 }
453                 if (rc != 0) {
454                         CERROR("Error trying to bind to port %d: %d\n",
455                                local_port, rc);
456                         goto failed;
457                 }
458         }
459         return 0;
460
461 failed:
462         sock_release(sock);
463         return rc;
464 }
465
466 int
467 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
468 {
469         int                 option;
470         int                 rc;
471
472         if (txbufsize != 0) {
473                 option = txbufsize;
474                 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
475                                        (char *)&option, sizeof(option));
476                 if (rc != 0) {
477                         CERROR("Can't set send buffer %d: %d\n",
478                                 option, rc);
479                         return rc;
480                 }
481         }
482
483         if (rxbufsize != 0) {
484                 option = rxbufsize;
485                 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
486                                        (char *)&option, sizeof(option));
487                 if (rc != 0) {
488                         CERROR("Can't set receive buffer %d: %d\n",
489                                 option, rc);
490                         return rc;
491                 }
492         }
493         return 0;
494 }
495 EXPORT_SYMBOL(lnet_sock_setbuf);
496
497 int
498 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
499 {
500         struct sockaddr_in sin;
501         int                len = sizeof(sin);
502         int                rc;
503
504         if (remote)
505                 rc = kernel_getpeername(sock, (struct sockaddr *)&sin, &len);
506         else
507                 rc = kernel_getsockname(sock, (struct sockaddr *)&sin, &len);
508         if (rc != 0) {
509                 CERROR("Error %d getting sock %s IP/port\n",
510                         rc, remote ? "peer" : "local");
511                 return rc;
512         }
513
514         if (ip != NULL)
515                 *ip = ntohl(sin.sin_addr.s_addr);
516
517         if (port != NULL)
518                 *port = ntohs(sin.sin_port);
519
520         return 0;
521 }
522 EXPORT_SYMBOL(lnet_sock_getaddr);
523
524 int
525 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
526 {
527         if (txbufsize != NULL)
528                 *txbufsize = sock->sk->sk_sndbuf;
529
530         if (rxbufsize != NULL)
531                 *rxbufsize = sock->sk->sk_rcvbuf;
532
533         return 0;
534 }
535 EXPORT_SYMBOL(lnet_sock_getbuf);
536
537 int
538 lnet_sock_listen(struct socket **sockp,
539                    __u32 local_ip, int local_port, int backlog)
540 {
541         int      fatal;
542         int      rc;
543
544         rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
545         if (rc != 0) {
546                 if (!fatal)
547                         CERROR("Can't create socket: port %d already in use\n",
548                                local_port);
549                 return rc;
550         }
551
552         rc = kernel_listen(*sockp, backlog);
553         if (rc == 0)
554                 return 0;
555
556         CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
557         sock_release(*sockp);
558         return rc;
559 }
560
561 #ifndef HAVE_SK_SLEEP
562 static inline wait_queue_head_t *sk_sleep(struct sock *sk)
563 {
564         return sk->sk_sleep;
565 }
566 #endif
567
568 int
569 lnet_sock_accept(struct socket **newsockp, struct socket *sock)
570 {
571         wait_queue_t   wait;
572         struct socket *newsock;
573         int            rc;
574
575         /* XXX this should add a ref to sock->ops->owner, if
576          * TCP could be a module */
577         rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
578         if (rc) {
579                 CERROR("Can't allocate socket\n");
580                 return rc;
581         }
582
583         newsock->ops = sock->ops;
584
585         rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
586         if (rc == -EAGAIN) {
587                 /* Nothing ready, so wait for activity */
588                 init_waitqueue_entry(&wait, current);
589                 add_wait_queue(sk_sleep(sock->sk), &wait);
590                 set_current_state(TASK_INTERRUPTIBLE);
591                 schedule();
592                 remove_wait_queue(sk_sleep(sock->sk), &wait);
593                 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
594         }
595
596         if (rc != 0)
597                 goto failed;
598
599         *newsockp = newsock;
600         return 0;
601
602 failed:
603         sock_release(newsock);
604         return rc;
605 }
606
607 int
608 lnet_sock_connect(struct socket **sockp, int *fatal,
609                   __u32 local_ip, int local_port,
610                   __u32 peer_ip, int peer_port)
611 {
612         struct sockaddr_in  srvaddr;
613         int                 rc;
614
615         rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
616         if (rc != 0)
617                 return rc;
618
619         memset(&srvaddr, 0, sizeof(srvaddr));
620         srvaddr.sin_family = AF_INET;
621         srvaddr.sin_port = htons(peer_port);
622         srvaddr.sin_addr.s_addr = htonl(peer_ip);
623
624         rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
625                             sizeof(srvaddr), 0);
626         if (rc == 0)
627                 return 0;
628
629         /* EADDRNOTAVAIL probably means we're already connected to the same
630          * peer/port on the same local port on a differently typed
631          * connection.  Let our caller retry with a different local
632          * port... */
633         *fatal = !(rc == -EADDRNOTAVAIL);
634
635         CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
636                "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
637                &local_ip, local_port, &peer_ip, peer_port);
638
639         sock_release(*sockp);
640         return rc;
641 }