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