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