Commit | Line | Data |
---|---|---|
d7e09d03 PT |
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 | |
60ecf96e | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
d7e09d03 PT |
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 | * | |
1dc563a6 | 26 | * Copyright (c) 2012, 2015, Intel Corporation. |
d7e09d03 PT |
27 | */ |
28 | /* | |
29 | * This file is part of Lustre, http://www.lustre.org/ | |
60ecf96e | 30 | * Lustre is a trademark of Seagate, Inc. |
d7e09d03 PT |
31 | */ |
32 | #define DEBUG_SUBSYSTEM S_LNET | |
33 | ||
d7e09d03 PT |
34 | #include <linux/if.h> |
35 | #include <linux/in.h> | |
5c2414ef | 36 | #include <linux/net.h> |
d7e09d03 | 37 | #include <linux/file.h> |
5c2414ef | 38 | #include <linux/pagemap.h> |
d7e09d03 PT |
39 | /* For sys_open & sys_close */ |
40 | #include <linux/syscalls.h> | |
5c2414ef JS |
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 | } | |
d7e09d03 | 58 | |
2a74b9bd | 59 | static int |
1ad6a73e | 60 | lnet_sock_ioctl(int cmd, unsigned long arg) |
d7e09d03 | 61 | { |
73092892 JS |
62 | struct file *sock_filp; |
63 | struct socket *sock; | |
64 | int rc; | |
d7e09d03 | 65 | |
73092892 | 66 | rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock); |
5fd88337 | 67 | if (rc) { |
73092892 | 68 | CERROR("Can't create socket: %d\n", rc); |
d7e09d03 PT |
69 | return rc; |
70 | } | |
3eeb821e OD |
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 | ||
5c2414ef | 79 | rc = kernel_sock_unlocked_ioctl(sock_filp, cmd, arg); |
3eeb821e OD |
80 | |
81 | fput(sock_filp); | |
82 | out: | |
d7e09d03 PT |
83 | return rc; |
84 | } | |
85 | ||
86 | int | |
73092892 | 87 | lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask) |
d7e09d03 | 88 | { |
73092892 JS |
89 | struct ifreq ifr; |
90 | int nob; | |
91 | int rc; | |
92 | __u32 val; | |
d7e09d03 PT |
93 | |
94 | nob = strnlen(name, IFNAMSIZ); | |
95 | if (nob == IFNAMSIZ) { | |
96 | CERROR("Interface name %s too long\n", name); | |
97 | return -EINVAL; | |
98 | } | |
99 | ||
73092892 | 100 | CLASSERT(sizeof(ifr.ifr_name) >= IFNAMSIZ); |
d7e09d03 | 101 | |
661489ad SB |
102 | if (strlen(name) > sizeof(ifr.ifr_name) - 1) |
103 | return -E2BIG; | |
104 | strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); | |
105 | ||
1ad6a73e | 106 | rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr); |
5fd88337 | 107 | if (rc) { |
d7e09d03 PT |
108 | CERROR("Can't get flags for interface %s\n", name); |
109 | return rc; | |
110 | } | |
111 | ||
5fd88337 | 112 | if (!(ifr.ifr_flags & IFF_UP)) { |
d7e09d03 PT |
113 | CDEBUG(D_NET, "Interface %s down\n", name); |
114 | *up = 0; | |
115 | *ip = *mask = 0; | |
116 | return 0; | |
117 | } | |
d7e09d03 PT |
118 | *up = 1; |
119 | ||
661489ad SB |
120 | if (strlen(name) > sizeof(ifr.ifr_name) - 1) |
121 | return -E2BIG; | |
122 | strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); | |
123 | ||
d7e09d03 | 124 | ifr.ifr_addr.sa_family = AF_INET; |
1ad6a73e | 125 | rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr); |
5fd88337 | 126 | if (rc) { |
d7e09d03 PT |
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 | ||
661489ad SB |
134 | if (strlen(name) > sizeof(ifr.ifr_name) - 1) |
135 | return -E2BIG; | |
136 | strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); | |
137 | ||
d7e09d03 | 138 | ifr.ifr_addr.sa_family = AF_INET; |
1ad6a73e | 139 | rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr); |
5fd88337 | 140 | if (rc) { |
d7e09d03 PT |
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 | } | |
1ad6a73e | 150 | EXPORT_SYMBOL(lnet_ipif_query); |
d7e09d03 PT |
151 | |
152 | int | |
73092892 | 153 | lnet_ipif_enumerate(char ***namesp) |
d7e09d03 PT |
154 | { |
155 | /* Allocate and fill in 'names', returning # interfaces/error */ | |
73092892 JS |
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; | |
d7e09d03 PT |
165 | |
166 | nalloc = 16; /* first guess at max interfaces */ | |
167 | toobig = 0; | |
168 | for (;;) { | |
09cbfeaf | 169 | if (nalloc * sizeof(*ifr) > PAGE_SIZE) { |
d7e09d03 | 170 | toobig = 1; |
09cbfeaf | 171 | nalloc = PAGE_SIZE / sizeof(*ifr); |
d7e09d03 PT |
172 | CWARN("Too many interfaces: only enumerating first %d\n", |
173 | nalloc); | |
174 | } | |
175 | ||
176 | LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr)); | |
06ace26e | 177 | if (!ifr) { |
73092892 JS |
178 | CERROR("ENOMEM enumerating up to %d interfaces\n", |
179 | nalloc); | |
d7e09d03 PT |
180 | rc = -ENOMEM; |
181 | goto out0; | |
182 | } | |
183 | ||
184 | ifc.ifc_buf = (char *)ifr; | |
185 | ifc.ifc_len = nalloc * sizeof(*ifr); | |
186 | ||
1ad6a73e | 187 | rc = lnet_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc); |
d7e09d03 | 188 | if (rc < 0) { |
73092892 | 189 | CERROR("Error %d enumerating interfaces\n", rc); |
d7e09d03 PT |
190 | goto out1; |
191 | } | |
192 | ||
5fd88337 | 193 | LASSERT(!rc); |
d7e09d03 | 194 | |
51078e25 | 195 | nfound = ifc.ifc_len / sizeof(*ifr); |
73092892 | 196 | LASSERT(nfound <= nalloc); |
d7e09d03 PT |
197 | |
198 | if (nfound < nalloc || toobig) | |
199 | break; | |
200 | ||
201 | LIBCFS_FREE(ifr, nalloc * sizeof(*ifr)); | |
202 | nalloc *= 2; | |
203 | } | |
204 | ||
5fd88337 | 205 | if (!nfound) |
d7e09d03 PT |
206 | goto out1; |
207 | ||
208 | LIBCFS_ALLOC(names, nfound * sizeof(*names)); | |
06ace26e | 209 | if (!names) { |
d7e09d03 PT |
210 | rc = -ENOMEM; |
211 | goto out1; | |
212 | } | |
d7e09d03 PT |
213 | |
214 | for (i = 0; i < nfound; i++) { | |
73092892 | 215 | nob = strnlen(ifr[i].ifr_name, IFNAMSIZ); |
d7e09d03 PT |
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); | |
06ace26e | 225 | if (!names[i]) { |
d7e09d03 PT |
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 | ||
73092892 | 237 | out2: |
d7e09d03 | 238 | if (rc < 0) |
1ad6a73e | 239 | lnet_ipif_free_enumeration(names, nfound); |
73092892 | 240 | out1: |
d7e09d03 | 241 | LIBCFS_FREE(ifr, nalloc * sizeof(*ifr)); |
73092892 | 242 | out0: |
d7e09d03 PT |
243 | return rc; |
244 | } | |
1ad6a73e | 245 | EXPORT_SYMBOL(lnet_ipif_enumerate); |
d7e09d03 PT |
246 | |
247 | void | |
73092892 | 248 | lnet_ipif_free_enumeration(char **names, int n) |
d7e09d03 | 249 | { |
73092892 | 250 | int i; |
d7e09d03 | 251 | |
73092892 | 252 | LASSERT(n > 0); |
d7e09d03 | 253 | |
06ace26e | 254 | for (i = 0; i < n && names[i]; i++) |
d7e09d03 PT |
255 | LIBCFS_FREE(names[i], IFNAMSIZ); |
256 | ||
257 | LIBCFS_FREE(names, n * sizeof(*names)); | |
258 | } | |
1ad6a73e | 259 | EXPORT_SYMBOL(lnet_ipif_free_enumeration); |
d7e09d03 PT |
260 | |
261 | int | |
73092892 | 262 | lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout) |
d7e09d03 | 263 | { |
73092892 | 264 | int rc; |
27d81ace | 265 | long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC); |
73092892 | 266 | unsigned long then; |
d7e09d03 | 267 | struct timeval tv; |
fd25dc34 AV |
268 | struct kvec iov = { .iov_base = buffer, .iov_len = nob }; |
269 | struct msghdr msg = {NULL,}; | |
d7e09d03 | 270 | |
73092892 | 271 | LASSERT(nob > 0); |
4420cfd3 JS |
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 | */ | |
fd25dc34 | 276 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iov, 1, nob); |
d7e09d03 | 277 | for (;;) { |
fd25dc34 | 278 | msg.msg_flags = !timeout ? MSG_DONTWAIT : 0; |
5fd88337 | 279 | if (timeout) { |
d7e09d03 | 280 | /* Set send timeout to remaining time */ |
27d81ace | 281 | jiffies_to_timeval(jiffies_left, &tv); |
80db2734 | 282 | rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, |
73092892 | 283 | (char *)&tv, sizeof(tv)); |
5fd88337 | 284 | if (rc) { |
2d00bd17 | 285 | CERROR("Can't set socket send timeout %ld.%06d: %d\n", |
d7e09d03 PT |
286 | (long)tv.tv_sec, (int)tv.tv_usec, rc); |
287 | return rc; | |
288 | } | |
289 | } | |
290 | ||
d7e09d03 | 291 | then = jiffies; |
480f40de | 292 | rc = kernel_sendmsg(sock, &msg, &iov, 1, nob); |
27d81ace | 293 | jiffies_left -= jiffies - then; |
d7e09d03 | 294 | |
d7e09d03 PT |
295 | if (rc < 0) |
296 | return rc; | |
297 | ||
5fd88337 | 298 | if (!rc) { |
73092892 | 299 | CERROR("Unexpected zero rc\n"); |
fbe7c6c7 | 300 | return -ECONNABORTED; |
d7e09d03 PT |
301 | } |
302 | ||
fd25dc34 AV |
303 | if (!msg_data_left(&msg)) |
304 | break; | |
305 | ||
27d81ace | 306 | if (jiffies_left <= 0) |
d7e09d03 | 307 | return -EAGAIN; |
d7e09d03 | 308 | } |
fbe7c6c7 | 309 | return 0; |
d7e09d03 | 310 | } |
1ad6a73e | 311 | EXPORT_SYMBOL(lnet_sock_write); |
d7e09d03 PT |
312 | |
313 | int | |
73092892 | 314 | lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout) |
d7e09d03 | 315 | { |
73092892 | 316 | int rc; |
27d81ace | 317 | long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC); |
73092892 | 318 | unsigned long then; |
d7e09d03 PT |
319 | struct timeval tv; |
320 | ||
73092892 | 321 | LASSERT(nob > 0); |
27d81ace | 322 | LASSERT(jiffies_left > 0); |
d7e09d03 PT |
323 | |
324 | for (;;) { | |
b2f42cfe | 325 | struct kvec iov = { |
d7e09d03 PT |
326 | .iov_base = buffer, |
327 | .iov_len = nob | |
328 | }; | |
329 | struct msghdr msg = { | |
73092892 | 330 | .msg_flags = 0 |
d7e09d03 PT |
331 | }; |
332 | ||
333 | /* Set receive timeout to remaining time */ | |
27d81ace | 334 | jiffies_to_timeval(jiffies_left, &tv); |
80db2734 | 335 | rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, |
73092892 | 336 | (char *)&tv, sizeof(tv)); |
5fd88337 | 337 | if (rc) { |
d7e09d03 PT |
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 | ||
d7e09d03 | 343 | then = jiffies; |
b2f42cfe | 344 | rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0); |
27d81ace | 345 | jiffies_left -= jiffies - then; |
d7e09d03 PT |
346 | |
347 | if (rc < 0) | |
348 | return rc; | |
349 | ||
5fd88337 | 350 | if (!rc) |
d7e09d03 PT |
351 | return -ECONNRESET; |
352 | ||
353 | buffer = ((char *)buffer) + rc; | |
354 | nob -= rc; | |
355 | ||
5fd88337 | 356 | if (!nob) |
d7e09d03 PT |
357 | return 0; |
358 | ||
27d81ace | 359 | if (jiffies_left <= 0) |
d7e09d03 PT |
360 | return -ETIMEDOUT; |
361 | } | |
362 | } | |
1ad6a73e | 363 | EXPORT_SYMBOL(lnet_sock_read); |
d7e09d03 PT |
364 | |
365 | static int | |
73092892 JS |
366 | lnet_sock_create(struct socket **sockp, int *fatal, __u32 local_ip, |
367 | int local_port) | |
d7e09d03 | 368 | { |
73092892 JS |
369 | struct sockaddr_in locaddr; |
370 | struct socket *sock; | |
371 | int rc; | |
372 | int option; | |
d7e09d03 PT |
373 | |
374 | /* All errors are fatal except bind failure if the port is in use */ | |
375 | *fatal = 1; | |
376 | ||
73092892 | 377 | rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock); |
d7e09d03 | 378 | *sockp = sock; |
5fd88337 | 379 | if (rc) { |
73092892 | 380 | CERROR("Can't create socket: %d\n", rc); |
fbe7c6c7 | 381 | return rc; |
d7e09d03 PT |
382 | } |
383 | ||
d7e09d03 | 384 | option = 1; |
80db2734 | 385 | rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, |
73092892 | 386 | (char *)&option, sizeof(option)); |
5fd88337 | 387 | if (rc) { |
d7e09d03 PT |
388 | CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc); |
389 | goto failed; | |
390 | } | |
391 | ||
5fd88337 | 392 | if (local_ip || local_port) { |
d7e09d03 PT |
393 | memset(&locaddr, 0, sizeof(locaddr)); |
394 | locaddr.sin_family = AF_INET; | |
395 | locaddr.sin_port = htons(local_port); | |
5fd88337 | 396 | locaddr.sin_addr.s_addr = !local_ip ? |
d7e09d03 PT |
397 | INADDR_ANY : htonl(local_ip); |
398 | ||
5c2414ef JS |
399 | rc = kernel_bind(sock, (struct sockaddr *)&locaddr, |
400 | sizeof(locaddr)); | |
d7e09d03 PT |
401 | if (rc == -EADDRINUSE) { |
402 | CDEBUG(D_NET, "Port %d already in use\n", local_port); | |
403 | *fatal = 0; | |
404 | goto failed; | |
405 | } | |
5fd88337 | 406 | if (rc) { |
d7e09d03 PT |
407 | CERROR("Error trying to bind to port %d: %d\n", |
408 | local_port, rc); | |
409 | goto failed; | |
410 | } | |
411 | } | |
d7e09d03 PT |
412 | return 0; |
413 | ||
73092892 | 414 | failed: |
d7e09d03 PT |
415 | sock_release(sock); |
416 | return rc; | |
417 | } | |
418 | ||
419 | int | |
73092892 | 420 | lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize) |
d7e09d03 | 421 | { |
73092892 JS |
422 | int option; |
423 | int rc; | |
d7e09d03 | 424 | |
5fd88337 | 425 | if (txbufsize) { |
d7e09d03 | 426 | option = txbufsize; |
80db2734 | 427 | rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, |
73092892 | 428 | (char *)&option, sizeof(option)); |
5fd88337 | 429 | if (rc) { |
73092892 JS |
430 | CERROR("Can't set send buffer %d: %d\n", |
431 | option, rc); | |
fbe7c6c7 | 432 | return rc; |
d7e09d03 PT |
433 | } |
434 | } | |
435 | ||
5fd88337 | 436 | if (rxbufsize) { |
d7e09d03 | 437 | option = rxbufsize; |
80db2734 | 438 | rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, |
c314c319 | 439 | (char *)&option, sizeof(option)); |
5fd88337 | 440 | if (rc) { |
73092892 JS |
441 | CERROR("Can't set receive buffer %d: %d\n", |
442 | option, rc); | |
fbe7c6c7 | 443 | return rc; |
d7e09d03 PT |
444 | } |
445 | } | |
d7e09d03 PT |
446 | return 0; |
447 | } | |
1ad6a73e | 448 | EXPORT_SYMBOL(lnet_sock_setbuf); |
d7e09d03 PT |
449 | |
450 | int | |
73092892 | 451 | lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port) |
d7e09d03 PT |
452 | { |
453 | struct sockaddr_in sin; | |
73092892 JS |
454 | int len = sizeof(sin); |
455 | int rc; | |
d7e09d03 | 456 | |
5c2414ef JS |
457 | if (remote) |
458 | rc = kernel_getpeername(sock, (struct sockaddr *)&sin, &len); | |
459 | else | |
460 | rc = kernel_getsockname(sock, (struct sockaddr *)&sin, &len); | |
5fd88337 | 461 | if (rc) { |
73092892 JS |
462 | CERROR("Error %d getting sock %s IP/port\n", |
463 | rc, remote ? "peer" : "local"); | |
d7e09d03 PT |
464 | return rc; |
465 | } | |
466 | ||
06ace26e | 467 | if (ip) |
73092892 | 468 | *ip = ntohl(sin.sin_addr.s_addr); |
d7e09d03 | 469 | |
06ace26e | 470 | if (port) |
73092892 | 471 | *port = ntohs(sin.sin_port); |
d7e09d03 PT |
472 | |
473 | return 0; | |
474 | } | |
1ad6a73e | 475 | EXPORT_SYMBOL(lnet_sock_getaddr); |
d7e09d03 PT |
476 | |
477 | int | |
73092892 | 478 | lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize) |
d7e09d03 | 479 | { |
06ace26e | 480 | if (txbufsize) |
d7e09d03 | 481 | *txbufsize = sock->sk->sk_sndbuf; |
d7e09d03 | 482 | |
06ace26e | 483 | if (rxbufsize) |
d7e09d03 | 484 | *rxbufsize = sock->sk->sk_rcvbuf; |
d7e09d03 PT |
485 | |
486 | return 0; | |
487 | } | |
1ad6a73e | 488 | EXPORT_SYMBOL(lnet_sock_getbuf); |
d7e09d03 PT |
489 | |
490 | int | |
73092892 JS |
491 | lnet_sock_listen(struct socket **sockp, __u32 local_ip, int local_port, |
492 | int backlog) | |
d7e09d03 | 493 | { |
73092892 JS |
494 | int fatal; |
495 | int rc; | |
d7e09d03 | 496 | |
1ad6a73e | 497 | rc = lnet_sock_create(sockp, &fatal, local_ip, local_port); |
5fd88337 | 498 | if (rc) { |
d7e09d03 PT |
499 | if (!fatal) |
500 | CERROR("Can't create socket: port %d already in use\n", | |
501 | local_port); | |
502 | return rc; | |
503 | } | |
504 | ||
5c2414ef | 505 | rc = kernel_listen(*sockp, backlog); |
5fd88337 | 506 | if (!rc) |
d7e09d03 PT |
507 | return 0; |
508 | ||
509 | CERROR("Can't set listen backlog %d: %d\n", backlog, rc); | |
510 | sock_release(*sockp); | |
511 | return rc; | |
512 | } | |
d7e09d03 PT |
513 | |
514 | int | |
73092892 | 515 | lnet_sock_accept(struct socket **newsockp, struct socket *sock) |
d7e09d03 | 516 | { |
73092892 | 517 | wait_queue_t wait; |
d7e09d03 | 518 | struct socket *newsock; |
73092892 | 519 | int rc; |
d7e09d03 | 520 | |
4420cfd3 JS |
521 | /* |
522 | * XXX this should add a ref to sock->ops->owner, if | |
523 | * TCP could be a module | |
524 | */ | |
d7e09d03 PT |
525 | rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock); |
526 | if (rc) { | |
527 | CERROR("Can't allocate socket\n"); | |
528 | return rc; | |
529 | } | |
530 | ||
531 | newsock->ops = sock->ops; | |
532 | ||
d7e09d03 PT |
533 | rc = sock->ops->accept(sock, newsock, O_NONBLOCK); |
534 | if (rc == -EAGAIN) { | |
535 | /* Nothing ready, so wait for activity */ | |
933d36ba | 536 | init_waitqueue_entry(&wait, current); |
834fe2e1 | 537 | add_wait_queue(sk_sleep(sock->sk), &wait); |
933d36ba | 538 | set_current_state(TASK_INTERRUPTIBLE); |
d7e09d03 | 539 | schedule(); |
834fe2e1 | 540 | remove_wait_queue(sk_sleep(sock->sk), &wait); |
d7e09d03 PT |
541 | rc = sock->ops->accept(sock, newsock, O_NONBLOCK); |
542 | } | |
543 | ||
5fd88337 | 544 | if (rc) |
d7e09d03 PT |
545 | goto failed; |
546 | ||
547 | *newsockp = newsock; | |
548 | return 0; | |
549 | ||
73092892 | 550 | failed: |
d7e09d03 PT |
551 | sock_release(newsock); |
552 | return rc; | |
553 | } | |
d7e09d03 | 554 | |
d7e09d03 | 555 | int |
73092892 JS |
556 | lnet_sock_connect(struct socket **sockp, int *fatal, __u32 local_ip, |
557 | int local_port, __u32 peer_ip, int peer_port) | |
d7e09d03 | 558 | { |
73092892 JS |
559 | struct sockaddr_in srvaddr; |
560 | int rc; | |
d7e09d03 | 561 | |
1ad6a73e | 562 | rc = lnet_sock_create(sockp, fatal, local_ip, local_port); |
5fd88337 | 563 | if (rc) |
d7e09d03 PT |
564 | return rc; |
565 | ||
73092892 | 566 | memset(&srvaddr, 0, sizeof(srvaddr)); |
d7e09d03 PT |
567 | srvaddr.sin_family = AF_INET; |
568 | srvaddr.sin_port = htons(peer_port); | |
569 | srvaddr.sin_addr.s_addr = htonl(peer_ip); | |
570 | ||
5c2414ef JS |
571 | rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr, |
572 | sizeof(srvaddr), 0); | |
5fd88337 | 573 | if (!rc) |
d7e09d03 PT |
574 | return 0; |
575 | ||
4420cfd3 JS |
576 | /* |
577 | * EADDRNOTAVAIL probably means we're already connected to the same | |
d7e09d03 PT |
578 | * peer/port on the same local port on a differently typed |
579 | * connection. Let our caller retry with a different local | |
4420cfd3 JS |
580 | * port... |
581 | */ | |
d7e09d03 PT |
582 | *fatal = !(rc == -EADDRNOTAVAIL); |
583 | ||
584 | CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET, | |
73092892 JS |
585 | "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc, |
586 | &local_ip, local_port, &peer_ip, peer_port); | |
d7e09d03 PT |
587 | |
588 | sock_release(*sockp); | |
589 | return rc; | |
590 | } |