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