Whamcloud - gitweb
dd609c0d1a2a223a8f95506b92a62323e82335cd
[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         /* NULL out all names[i] */
238         memset (names, 0, nfound * sizeof(*names));
239
240         for (i = 0; i < nfound; i++) {
241
242                 nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
243                 if (nob == IFNAMSIZ) {
244                         /* no space for terminating NULL */
245                         CERROR("interface name %.*s too long (%d max)\n",
246                                nob, ifr[i].ifr_name, IFNAMSIZ);
247                         rc = -ENAMETOOLONG;
248                         goto out2;
249                 }
250
251                 LIBCFS_ALLOC(names[i], IFNAMSIZ);
252                 if (names[i] == NULL) {
253                         rc = -ENOMEM;
254                         goto out2;
255                 }
256
257                 memcpy(names[i], ifr[i].ifr_name, nob);
258                 names[i][nob] = 0;
259         }
260
261         *namesp = names;
262         rc = nfound;
263
264  out2:
265         if (rc < 0)
266                 libcfs_ipif_free_enumeration(names, nfound);
267  out1:
268         LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
269  out0:
270         return rc;
271 }
272
273 EXPORT_SYMBOL(libcfs_ipif_enumerate);
274
275 void
276 libcfs_ipif_free_enumeration (char **names, int n)
277 {
278         int      i;
279
280         LASSERT (n > 0);
281
282         for (i = 0; i < n && names[i] != NULL; i++)
283                 LIBCFS_FREE(names[i], IFNAMSIZ);
284
285         LIBCFS_FREE(names, n * sizeof(*names));
286 }
287
288 EXPORT_SYMBOL(libcfs_ipif_free_enumeration);
289
290 int
291 libcfs_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
292 {
293         int            rc;
294         mm_segment_t   oldmm = get_fs();
295         long           ticks = timeout * HZ;
296         unsigned long  then;
297         struct timeval tv;
298
299         LASSERT (nob > 0);
300         /* Caller may pass a zero timeout if she thinks the socket buffer is
301          * empty enough to take the whole message immediately */
302
303         for (;;) {
304                 struct kvec  iov = {
305                         .iov_base = buffer,
306                         .iov_len  = nob
307                 };
308                 struct msghdr msg = {
309                         .msg_flags      = (timeout == 0) ? MSG_DONTWAIT : 0
310                 };
311
312                 if (timeout != 0) {
313                         /* Set send timeout to remaining time */
314                         tv = (struct timeval) {
315                                 .tv_sec = ticks / HZ,
316                                 .tv_usec = ((ticks % HZ) * 1000000) / HZ
317                         };
318                         set_fs(KERNEL_DS);
319                         rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
320                                              (char *)&tv, sizeof(tv));
321                         set_fs(oldmm);
322                         if (rc != 0) {
323                                 CERROR("Can't set socket send timeout "
324                                        "%ld.%06d: %d\n",
325                                        (long)tv.tv_sec, (int)tv.tv_usec, rc);
326                                 return rc;
327                         }
328                 }
329
330                 then = jiffies;
331                 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
332                 ticks -= jiffies - then;
333
334                 if (rc == nob)
335                         return 0;
336
337                 if (rc < 0)
338                         return rc;
339
340                 if (rc == 0) {
341                         CERROR ("Unexpected zero rc\n");
342                         return (-ECONNABORTED);
343                 }
344
345                 if (ticks <= 0)
346                         return -EAGAIN;
347
348                 buffer = ((char *)buffer) + rc;
349                 nob -= rc;
350         }
351
352         return (0);
353 }
354 EXPORT_SYMBOL(libcfs_sock_write);
355
356 int
357 libcfs_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
358 {
359         int            rc;
360         mm_segment_t   oldmm = get_fs();
361         long           ticks = timeout * HZ;
362         unsigned long  then;
363         struct timeval tv;
364
365         LASSERT (nob > 0);
366         LASSERT (ticks > 0);
367
368         for (;;) {
369                 struct kvec  iov = {
370                         .iov_base = buffer,
371                         .iov_len  = nob
372                 };
373                 struct msghdr msg = {
374                         .msg_flags      = 0
375                 };
376
377                 /* Set receive timeout to remaining time */
378                 tv = (struct timeval) {
379                         .tv_sec = ticks / HZ,
380                         .tv_usec = ((ticks % HZ) * 1000000) / HZ
381                 };
382                 set_fs(KERNEL_DS);
383                 rc = sock_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
384                                      (char *)&tv, sizeof(tv));
385                 set_fs(oldmm);
386                 if (rc != 0) {
387                         CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
388                                (long)tv.tv_sec, (int)tv.tv_usec, rc);
389                         return rc;
390                 }
391
392                 then = jiffies;
393                 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
394                 ticks -= jiffies - then;
395
396                 if (rc < 0)
397                         return rc;
398
399                 if (rc == 0)
400                         return -ECONNRESET;
401
402                 buffer = ((char *)buffer) + rc;
403                 nob -= rc;
404
405                 if (nob == 0)
406                         return 0;
407
408                 if (ticks <= 0)
409                         return -ETIMEDOUT;
410         }
411 }
412
413 EXPORT_SYMBOL(libcfs_sock_read);
414
415 static int
416 libcfs_sock_create (struct socket **sockp, int *fatal,
417                     __u32 local_ip, int local_port)
418 {
419         struct sockaddr_in  locaddr;
420         struct socket      *sock;
421         int                 rc;
422         int                 option;
423         mm_segment_t        oldmm = get_fs();
424
425         /* All errors are fatal except bind failure if the port is in use */
426         *fatal = 1;
427
428         rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
429         *sockp = sock;
430         if (rc != 0) {
431                 CERROR ("Can't create socket: %d\n", rc);
432                 return (rc);
433         }
434
435         set_fs (KERNEL_DS);
436         option = 1;
437         rc = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
438                              (char *)&option, sizeof (option));
439         set_fs (oldmm);
440         if (rc != 0) {
441                 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
442                 goto failed;
443         }
444
445         if (local_ip != 0 || local_port != 0) {
446                 memset(&locaddr, 0, sizeof(locaddr));
447                 locaddr.sin_family = AF_INET;
448                 locaddr.sin_port = htons(local_port);
449                 locaddr.sin_addr.s_addr = (local_ip == 0) ?
450                                           INADDR_ANY : htonl(local_ip);
451
452                 rc = sock->ops->bind(sock, (struct sockaddr *)&locaddr,
453                                      sizeof(locaddr));
454                 if (rc == -EADDRINUSE) {
455                         CDEBUG(D_NET, "Port %d already in use\n", local_port);
456                         *fatal = 0;
457                         goto failed;
458                 }
459                 if (rc != 0) {
460                         CERROR("Error trying to bind to port %d: %d\n",
461                                local_port, rc);
462                         goto failed;
463                 }
464         }
465
466         return 0;
467
468  failed:
469         sock_release(sock);
470         return rc;
471 }
472
473 int
474 libcfs_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
475 {
476         mm_segment_t        oldmm = get_fs();
477         int                 option;
478         int                 rc;
479
480         if (txbufsize != 0) {
481                 option = txbufsize;
482                 set_fs (KERNEL_DS);
483                 rc = sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
484                                      (char *)&option, sizeof (option));
485                 set_fs (oldmm);
486                 if (rc != 0) {
487                         CERROR ("Can't set send buffer %d: %d\n",
488                                 option, rc);
489                         return (rc);
490                 }
491         }
492
493         if (rxbufsize != 0) {
494                 option = rxbufsize;
495                 set_fs (KERNEL_DS);
496                 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
497                                       (char *)&option, sizeof (option));
498                 set_fs (oldmm);
499                 if (rc != 0) {
500                         CERROR ("Can't set receive buffer %d: %d\n",
501                                 option, rc);
502                         return (rc);
503                 }
504         }
505
506         return 0;
507 }
508
509 EXPORT_SYMBOL(libcfs_sock_setbuf);
510
511 int
512 libcfs_sock_getaddr (struct socket *sock, int remote, __u32 *ip, int *port)
513 {
514         struct sockaddr_in sin;
515         int                len = sizeof (sin);
516         int                rc;
517
518         rc = sock->ops->getname (sock, (struct sockaddr *)&sin, &len,
519                                  remote ? 2 : 0);
520         if (rc != 0) {
521                 CERROR ("Error %d getting sock %s IP/port\n",
522                         rc, remote ? "peer" : "local");
523                 return rc;
524         }
525
526         if (ip != NULL)
527                 *ip = ntohl (sin.sin_addr.s_addr);
528
529         if (port != NULL)
530                 *port = ntohs (sin.sin_port);
531
532         return 0;
533 }
534
535 EXPORT_SYMBOL(libcfs_sock_getaddr);
536
537 int
538 libcfs_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
539 {
540
541         if (txbufsize != NULL) {
542                 *txbufsize = sock->sk->sk_sndbuf;
543         }
544
545         if (rxbufsize != NULL) {
546                 *rxbufsize = sock->sk->sk_rcvbuf;
547         }
548
549         return 0;
550 }
551
552 EXPORT_SYMBOL(libcfs_sock_getbuf);
553
554 int
555 libcfs_sock_listen (struct socket **sockp,
556                     __u32 local_ip, int local_port, int backlog)
557 {
558         int      fatal;
559         int      rc;
560
561         rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
562         if (rc != 0) {
563                 if (!fatal)
564                         CERROR("Can't create socket: port %d already in use\n",
565                                local_port);
566                 return rc;
567         }
568
569         rc = (*sockp)->ops->listen(*sockp, backlog);
570         if (rc == 0)
571                 return 0;
572
573         CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
574         sock_release(*sockp);
575         return rc;
576 }
577
578 EXPORT_SYMBOL(libcfs_sock_listen);
579
580 int
581 libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
582 {
583         wait_queue_t   wait;
584         struct socket *newsock;
585         int            rc;
586
587         init_waitqueue_entry(&wait, current);
588
589         /* XXX this should add a ref to sock->ops->owner, if
590          * TCP could be a module */
591         rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
592         if (rc) {
593                 CERROR("Can't allocate socket\n");
594                 return rc;
595         }
596
597         newsock->ops = sock->ops;
598
599         set_current_state(TASK_INTERRUPTIBLE);
600         add_wait_queue(sk_sleep(sock->sk), &wait);
601
602         rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
603         if (rc == -EAGAIN) {
604                 /* Nothing ready, so wait for activity */
605                 schedule();
606                 rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
607         }
608
609         remove_wait_queue(sk_sleep(sock->sk), &wait);
610         set_current_state(TASK_RUNNING);
611
612         if (rc != 0)
613                 goto failed;
614
615         *newsockp = newsock;
616         return 0;
617
618  failed:
619         sock_release(newsock);
620         return rc;
621 }
622
623 EXPORT_SYMBOL(libcfs_sock_accept);
624
625 void
626 libcfs_sock_abort_accept (struct socket *sock)
627 {
628         wake_up_all(sk_sleep(sock->sk));
629 }
630
631 EXPORT_SYMBOL(libcfs_sock_abort_accept);
632
633 int
634 libcfs_sock_connect (struct socket **sockp, int *fatal,
635                      __u32 local_ip, int local_port,
636                      __u32 peer_ip, int peer_port)
637 {
638         struct sockaddr_in  srvaddr;
639         int                 rc;
640
641         rc = libcfs_sock_create(sockp, fatal, local_ip, local_port);
642         if (rc != 0)
643                 return rc;
644
645         memset (&srvaddr, 0, sizeof (srvaddr));
646         srvaddr.sin_family = AF_INET;
647         srvaddr.sin_port = htons(peer_port);
648         srvaddr.sin_addr.s_addr = htonl(peer_ip);
649
650         rc = (*sockp)->ops->connect(*sockp,
651                                     (struct sockaddr *)&srvaddr, sizeof(srvaddr),
652                                     0);
653         if (rc == 0)
654                 return 0;
655
656         /* EADDRNOTAVAIL probably means we're already connected to the same
657          * peer/port on the same local port on a differently typed
658          * connection.  Let our caller retry with a different local
659          * port... */
660         *fatal = !(rc == -EADDRNOTAVAIL);
661
662         CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
663                "Error %d connecting %u.%u.%u.%u/%d -> %u.%u.%u.%u/%d\n", rc,
664                HIPQUAD(local_ip), local_port, HIPQUAD(peer_ip), peer_port);
665
666         sock_release(*sockp);
667         return rc;
668 }
669
670 EXPORT_SYMBOL(libcfs_sock_connect);
671
672 void
673 libcfs_sock_release (struct socket *sock)
674 {
675         sock_release(sock);
676 }
677
678 EXPORT_SYMBOL(libcfs_sock_release);