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