]> git.ipfire.org Git - thirdparty/linux.git/blob - drivers/staging/lustre/lnet/lnet/lib-socket.c
Merge remote-tracking branches 'asoc/topic/samsung', 'asoc/topic/sgtl5000', 'asoc...
[thirdparty/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.gnu.org/licenses/gpl-2.0.html
19 *
20 * GPL HEADER END
21 */
22 /*
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
25 *
26 * Copyright (c) 2012, 2015, Intel Corporation.
27 */
28 /*
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Seagate, Inc.
31 */
32 #define DEBUG_SUBSYSTEM S_LNET
33
34 #include <linux/if.h>
35 #include <linux/in.h>
36 #include <linux/net.h>
37 #include <linux/file.h>
38 #include <linux/pagemap.h>
39 /* For sys_open & sys_close */
40 #include <linux/syscalls.h>
41 #include <net/sock.h>
42
43 #include "../../include/linux/libcfs/libcfs.h"
44 #include "../../include/linux/lnet/lib-lnet.h"
45
46 static int
47 kernel_sock_unlocked_ioctl(struct file *filp, int cmd, unsigned long arg)
48 {
49 mm_segment_t oldfs = get_fs();
50 int err;
51
52 set_fs(KERNEL_DS);
53 err = filp->f_op->unlocked_ioctl(filp, cmd, arg);
54 set_fs(oldfs);
55
56 return err;
57 }
58
59 static int
60 lnet_sock_ioctl(int cmd, unsigned long arg)
61 {
62 struct file *sock_filp;
63 struct socket *sock;
64 int rc;
65
66 rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
67 if (rc) {
68 CERROR("Can't create socket: %d\n", rc);
69 return rc;
70 }
71
72 sock_filp = sock_alloc_file(sock, 0, NULL);
73 if (IS_ERR(sock_filp)) {
74 sock_release(sock);
75 rc = PTR_ERR(sock_filp);
76 goto out;
77 }
78
79 rc = kernel_sock_unlocked_ioctl(sock_filp, cmd, arg);
80
81 fput(sock_filp);
82 out:
83 return rc;
84 }
85
86 int
87 lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask)
88 {
89 struct ifreq ifr;
90 int nob;
91 int rc;
92 __u32 val;
93
94 nob = strnlen(name, IFNAMSIZ);
95 if (nob == IFNAMSIZ) {
96 CERROR("Interface name %s too long\n", name);
97 return -EINVAL;
98 }
99
100 BUILD_BUG_ON(sizeof(ifr.ifr_name) < IFNAMSIZ);
101
102 if (strlen(name) > sizeof(ifr.ifr_name) - 1)
103 return -E2BIG;
104 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
105
106 rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
107 if (rc) {
108 CERROR("Can't get flags for interface %s\n", name);
109 return rc;
110 }
111
112 if (!(ifr.ifr_flags & IFF_UP)) {
113 CDEBUG(D_NET, "Interface %s down\n", name);
114 *up = 0;
115 *ip = *mask = 0;
116 return 0;
117 }
118 *up = 1;
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 ifr.ifr_addr.sa_family = AF_INET;
125 rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
126 if (rc) {
127 CERROR("Can't get IP address for interface %s\n", name);
128 return rc;
129 }
130
131 val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
132 *ip = ntohl(val);
133
134 if (strlen(name) > sizeof(ifr.ifr_name) - 1)
135 return -E2BIG;
136 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
137
138 ifr.ifr_addr.sa_family = AF_INET;
139 rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
140 if (rc) {
141 CERROR("Can't get netmask for interface %s\n", name);
142 return rc;
143 }
144
145 val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
146 *mask = ntohl(val);
147
148 return 0;
149 }
150 EXPORT_SYMBOL(lnet_ipif_query);
151
152 int
153 lnet_ipif_enumerate(char ***namesp)
154 {
155 /* Allocate and fill in 'names', returning # interfaces/error */
156 char **names;
157 int toobig;
158 int nalloc;
159 int nfound;
160 struct ifreq *ifr;
161 struct ifconf ifc;
162 int rc;
163 int nob;
164 int i;
165
166 nalloc = 16; /* first guess at max interfaces */
167 toobig = 0;
168 for (;;) {
169 if (nalloc * sizeof(*ifr) > PAGE_SIZE) {
170 toobig = 1;
171 nalloc = PAGE_SIZE / sizeof(*ifr);
172 CWARN("Too many interfaces: only enumerating first %d\n",
173 nalloc);
174 }
175
176 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
177 if (!ifr) {
178 CERROR("ENOMEM enumerating up to %d interfaces\n",
179 nalloc);
180 rc = -ENOMEM;
181 goto out0;
182 }
183
184 ifc.ifc_buf = (char *)ifr;
185 ifc.ifc_len = nalloc * sizeof(*ifr);
186
187 rc = lnet_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
188 if (rc < 0) {
189 CERROR("Error %d enumerating interfaces\n", rc);
190 goto out1;
191 }
192
193 LASSERT(!rc);
194
195 nfound = ifc.ifc_len / sizeof(*ifr);
196 LASSERT(nfound <= nalloc);
197
198 if (nfound < nalloc || toobig)
199 break;
200
201 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
202 nalloc *= 2;
203 }
204
205 if (!nfound)
206 goto out1;
207
208 LIBCFS_ALLOC(names, nfound * sizeof(*names));
209 if (!names) {
210 rc = -ENOMEM;
211 goto out1;
212 }
213
214 for (i = 0; i < nfound; i++) {
215 nob = strnlen(ifr[i].ifr_name, IFNAMSIZ);
216 if (nob == IFNAMSIZ) {
217 /* no space for terminating NULL */
218 CERROR("interface name %.*s too long (%d max)\n",
219 nob, ifr[i].ifr_name, IFNAMSIZ);
220 rc = -ENAMETOOLONG;
221 goto out2;
222 }
223
224 LIBCFS_ALLOC(names[i], IFNAMSIZ);
225 if (!names[i]) {
226 rc = -ENOMEM;
227 goto out2;
228 }
229
230 memcpy(names[i], ifr[i].ifr_name, nob);
231 names[i][nob] = 0;
232 }
233
234 *namesp = names;
235 rc = nfound;
236
237 out2:
238 if (rc < 0)
239 lnet_ipif_free_enumeration(names, nfound);
240 out1:
241 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
242 out0:
243 return rc;
244 }
245 EXPORT_SYMBOL(lnet_ipif_enumerate);
246
247 void
248 lnet_ipif_free_enumeration(char **names, int n)
249 {
250 int i;
251
252 LASSERT(n > 0);
253
254 for (i = 0; i < n && names[i]; i++)
255 LIBCFS_FREE(names[i], IFNAMSIZ);
256
257 LIBCFS_FREE(names, n * sizeof(*names));
258 }
259 EXPORT_SYMBOL(lnet_ipif_free_enumeration);
260
261 int
262 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
263 {
264 int rc;
265 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
266 unsigned long then;
267 struct timeval tv;
268 struct kvec iov = { .iov_base = buffer, .iov_len = nob };
269 struct msghdr msg = {NULL,};
270
271 LASSERT(nob > 0);
272 /*
273 * Caller may pass a zero timeout if she thinks the socket buffer is
274 * empty enough to take the whole message immediately
275 */
276 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iov, 1, nob);
277 for (;;) {
278 msg.msg_flags = !timeout ? MSG_DONTWAIT : 0;
279 if (timeout) {
280 /* Set send timeout to remaining time */
281 jiffies_to_timeval(jiffies_left, &tv);
282 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
283 (char *)&tv, sizeof(tv));
284 if (rc) {
285 CERROR("Can't set socket send timeout %ld.%06d: %d\n",
286 (long)tv.tv_sec, (int)tv.tv_usec, rc);
287 return rc;
288 }
289 }
290
291 then = jiffies;
292 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
293 jiffies_left -= jiffies - then;
294
295 if (rc < 0)
296 return rc;
297
298 if (!rc) {
299 CERROR("Unexpected zero rc\n");
300 return -ECONNABORTED;
301 }
302
303 if (!msg_data_left(&msg))
304 break;
305
306 if (jiffies_left <= 0)
307 return -EAGAIN;
308 }
309 return 0;
310 }
311 EXPORT_SYMBOL(lnet_sock_write);
312
313 int
314 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
315 {
316 int rc;
317 long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
318 unsigned long then;
319 struct timeval tv;
320
321 LASSERT(nob > 0);
322 LASSERT(jiffies_left > 0);
323
324 for (;;) {
325 struct kvec iov = {
326 .iov_base = buffer,
327 .iov_len = nob
328 };
329 struct msghdr msg = {
330 .msg_flags = 0
331 };
332
333 /* Set receive timeout to remaining time */
334 jiffies_to_timeval(jiffies_left, &tv);
335 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
336 (char *)&tv, sizeof(tv));
337 if (rc) {
338 CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
339 (long)tv.tv_sec, (int)tv.tv_usec, rc);
340 return rc;
341 }
342
343 then = jiffies;
344 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
345 jiffies_left -= jiffies - then;
346
347 if (rc < 0)
348 return rc;
349
350 if (!rc)
351 return -ECONNRESET;
352
353 buffer = ((char *)buffer) + rc;
354 nob -= rc;
355
356 if (!nob)
357 return 0;
358
359 if (jiffies_left <= 0)
360 return -ETIMEDOUT;
361 }
362 }
363 EXPORT_SYMBOL(lnet_sock_read);
364
365 static int
366 lnet_sock_create(struct socket **sockp, int *fatal, __u32 local_ip,
367 int local_port)
368 {
369 struct sockaddr_in locaddr;
370 struct socket *sock;
371 int rc;
372 int option;
373
374 /* All errors are fatal except bind failure if the port is in use */
375 *fatal = 1;
376
377 rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
378 *sockp = sock;
379 if (rc) {
380 CERROR("Can't create socket: %d\n", rc);
381 return rc;
382 }
383
384 option = 1;
385 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
386 (char *)&option, sizeof(option));
387 if (rc) {
388 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
389 goto failed;
390 }
391
392 if (local_ip || local_port) {
393 memset(&locaddr, 0, sizeof(locaddr));
394 locaddr.sin_family = AF_INET;
395 locaddr.sin_port = htons(local_port);
396 if (!local_ip)
397 locaddr.sin_addr.s_addr = htonl(INADDR_ANY);
398 else
399 locaddr.sin_addr.s_addr = htonl(local_ip);
400
401 rc = kernel_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) {
409 CERROR("Error trying to bind to port %d: %d\n",
410 local_port, rc);
411 goto failed;
412 }
413 }
414 return 0;
415
416 failed:
417 sock_release(sock);
418 return rc;
419 }
420
421 int
422 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
423 {
424 int option;
425 int rc;
426
427 if (txbufsize) {
428 option = txbufsize;
429 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
430 (char *)&option, sizeof(option));
431 if (rc) {
432 CERROR("Can't set send buffer %d: %d\n",
433 option, rc);
434 return rc;
435 }
436 }
437
438 if (rxbufsize) {
439 option = rxbufsize;
440 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
441 (char *)&option, sizeof(option));
442 if (rc) {
443 CERROR("Can't set receive buffer %d: %d\n",
444 option, rc);
445 return rc;
446 }
447 }
448 return 0;
449 }
450 EXPORT_SYMBOL(lnet_sock_setbuf);
451
452 int
453 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
454 {
455 struct sockaddr_in sin;
456 int len = sizeof(sin);
457 int rc;
458
459 if (remote)
460 rc = kernel_getpeername(sock, (struct sockaddr *)&sin, &len);
461 else
462 rc = kernel_getsockname(sock, (struct sockaddr *)&sin, &len);
463 if (rc) {
464 CERROR("Error %d getting sock %s IP/port\n",
465 rc, remote ? "peer" : "local");
466 return rc;
467 }
468
469 if (ip)
470 *ip = ntohl(sin.sin_addr.s_addr);
471
472 if (port)
473 *port = ntohs(sin.sin_port);
474
475 return 0;
476 }
477 EXPORT_SYMBOL(lnet_sock_getaddr);
478
479 int
480 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
481 {
482 if (txbufsize)
483 *txbufsize = sock->sk->sk_sndbuf;
484
485 if (rxbufsize)
486 *rxbufsize = sock->sk->sk_rcvbuf;
487
488 return 0;
489 }
490 EXPORT_SYMBOL(lnet_sock_getbuf);
491
492 int
493 lnet_sock_listen(struct socket **sockp, __u32 local_ip, int local_port,
494 int backlog)
495 {
496 int fatal;
497 int rc;
498
499 rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
500 if (rc) {
501 if (!fatal)
502 CERROR("Can't create socket: port %d already in use\n",
503 local_port);
504 return rc;
505 }
506
507 rc = kernel_listen(*sockp, backlog);
508 if (!rc)
509 return 0;
510
511 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
512 sock_release(*sockp);
513 return rc;
514 }
515
516 int
517 lnet_sock_accept(struct socket **newsockp, struct socket *sock)
518 {
519 wait_queue_t wait;
520 struct socket *newsock;
521 int rc;
522
523 /*
524 * XXX this should add a ref to sock->ops->owner, if
525 * TCP could be a module
526 */
527 rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
528 if (rc) {
529 CERROR("Can't allocate socket\n");
530 return rc;
531 }
532
533 newsock->ops = sock->ops;
534
535 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
536 if (rc == -EAGAIN) {
537 /* Nothing ready, so wait for activity */
538 init_waitqueue_entry(&wait, current);
539 add_wait_queue(sk_sleep(sock->sk), &wait);
540 set_current_state(TASK_INTERRUPTIBLE);
541 schedule();
542 remove_wait_queue(sk_sleep(sock->sk), &wait);
543 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
544 }
545
546 if (rc)
547 goto failed;
548
549 *newsockp = newsock;
550 return 0;
551
552 failed:
553 sock_release(newsock);
554 return rc;
555 }
556
557 int
558 lnet_sock_connect(struct socket **sockp, int *fatal, __u32 local_ip,
559 int local_port, __u32 peer_ip, int peer_port)
560 {
561 struct sockaddr_in srvaddr;
562 int rc;
563
564 rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
565 if (rc)
566 return rc;
567
568 memset(&srvaddr, 0, sizeof(srvaddr));
569 srvaddr.sin_family = AF_INET;
570 srvaddr.sin_port = htons(peer_port);
571 srvaddr.sin_addr.s_addr = htonl(peer_ip);
572
573 rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
574 sizeof(srvaddr), 0);
575 if (!rc)
576 return 0;
577
578 /*
579 * EADDRNOTAVAIL probably means we're already connected to the same
580 * peer/port on the same local port on a differently typed
581 * connection. Let our caller retry with a different local
582 * port...
583 */
584 *fatal = !(rc == -EADDRNOTAVAIL);
585
586 CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
587 "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
588 &local_ip, local_port, &peer_ip, peer_port);
589
590 sock_release(*sockp);
591 return rc;
592 }