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