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