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