staging: lustre: remove RETURN macro
[deliverable/linux.git] / drivers / staging / lustre / lnet / klnds / socklnd / socklnd_cb.c
CommitLineData
d7e09d03
PT
1/*
2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Copyright (c) 2011, 2012, Intel Corporation.
5 *
6 * Author: Zach Brown <zab@zabbo.net>
7 * Author: Peter J. Braam <braam@clusterfs.com>
8 * Author: Phil Schwan <phil@clusterfs.com>
9 * Author: Eric Barton <eric@bartonsoftware.com>
10 *
11 * This file is part of Portals, http://www.sf.net/projects/sandiaportals/
12 *
13 * Portals is free software; you can redistribute it and/or
14 * modify it under the terms of version 2 of the GNU General Public
15 * License as published by the Free Software Foundation.
16 *
17 * Portals is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Portals; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include "socklnd.h"
28
29ksock_tx_t *
30ksocknal_alloc_tx(int type, int size)
31{
32 ksock_tx_t *tx = NULL;
33
34 if (type == KSOCK_MSG_NOOP) {
35 LASSERT(size == KSOCK_NOOP_TX_SIZE);
36
37 /* searching for a noop tx in free list */
38 spin_lock(&ksocknal_data.ksnd_tx_lock);
39
40 if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
41 tx = list_entry(ksocknal_data.ksnd_idle_noop_txs. \
42 next, ksock_tx_t, tx_list);
43 LASSERT(tx->tx_desc_size == size);
44 list_del(&tx->tx_list);
45 }
46
47 spin_unlock(&ksocknal_data.ksnd_tx_lock);
48 }
49
50 if (tx == NULL)
51 LIBCFS_ALLOC(tx, size);
52
53 if (tx == NULL)
54 return NULL;
55
56 atomic_set(&tx->tx_refcount, 1);
57 tx->tx_zc_aborted = 0;
58 tx->tx_zc_capable = 0;
59 tx->tx_zc_checked = 0;
60 tx->tx_desc_size = size;
61
62 atomic_inc(&ksocknal_data.ksnd_nactive_txs);
63
64 return tx;
65}
66
67ksock_tx_t *
68ksocknal_alloc_tx_noop(__u64 cookie, int nonblk)
69{
70 ksock_tx_t *tx;
71
72 tx = ksocknal_alloc_tx(KSOCK_MSG_NOOP, KSOCK_NOOP_TX_SIZE);
73 if (tx == NULL) {
74 CERROR("Can't allocate noop tx desc\n");
75 return NULL;
76 }
77
78 tx->tx_conn = NULL;
79 tx->tx_lnetmsg = NULL;
80 tx->tx_kiov = NULL;
81 tx->tx_nkiov = 0;
82 tx->tx_iov = tx->tx_frags.virt.iov;
83 tx->tx_niov = 1;
84 tx->tx_nonblk = nonblk;
85
86 socklnd_init_msg(&tx->tx_msg, KSOCK_MSG_NOOP);
87 tx->tx_msg.ksm_zc_cookies[1] = cookie;
88
89 return tx;
90}
91
92
93void
94ksocknal_free_tx (ksock_tx_t *tx)
95{
96 atomic_dec(&ksocknal_data.ksnd_nactive_txs);
97
98 if (tx->tx_lnetmsg == NULL && tx->tx_desc_size == KSOCK_NOOP_TX_SIZE) {
99 /* it's a noop tx */
100 spin_lock(&ksocknal_data.ksnd_tx_lock);
101
102 list_add(&tx->tx_list, &ksocknal_data.ksnd_idle_noop_txs);
103
104 spin_unlock(&ksocknal_data.ksnd_tx_lock);
105 } else {
106 LIBCFS_FREE(tx, tx->tx_desc_size);
107 }
108}
109
110int
111ksocknal_send_iov (ksock_conn_t *conn, ksock_tx_t *tx)
112{
113 struct iovec *iov = tx->tx_iov;
114 int nob;
115 int rc;
116
117 LASSERT (tx->tx_niov > 0);
118
119 /* Never touch tx->tx_iov inside ksocknal_lib_send_iov() */
120 rc = ksocknal_lib_send_iov(conn, tx);
121
122 if (rc <= 0) /* sent nothing? */
123 return (rc);
124
125 nob = rc;
126 LASSERT (nob <= tx->tx_resid);
127 tx->tx_resid -= nob;
128
129 /* "consume" iov */
130 do {
131 LASSERT (tx->tx_niov > 0);
132
133 if (nob < (int) iov->iov_len) {
134 iov->iov_base = (void *)((char *)iov->iov_base + nob);
135 iov->iov_len -= nob;
136 return (rc);
137 }
138
139 nob -= iov->iov_len;
140 tx->tx_iov = ++iov;
141 tx->tx_niov--;
142 } while (nob != 0);
143
144 return (rc);
145}
146
147int
148ksocknal_send_kiov (ksock_conn_t *conn, ksock_tx_t *tx)
149{
150 lnet_kiov_t *kiov = tx->tx_kiov;
151 int nob;
152 int rc;
153
154 LASSERT (tx->tx_niov == 0);
155 LASSERT (tx->tx_nkiov > 0);
156
157 /* Never touch tx->tx_kiov inside ksocknal_lib_send_kiov() */
158 rc = ksocknal_lib_send_kiov(conn, tx);
159
160 if (rc <= 0) /* sent nothing? */
161 return (rc);
162
163 nob = rc;
164 LASSERT (nob <= tx->tx_resid);
165 tx->tx_resid -= nob;
166
167 /* "consume" kiov */
168 do {
169 LASSERT(tx->tx_nkiov > 0);
170
171 if (nob < (int)kiov->kiov_len) {
172 kiov->kiov_offset += nob;
173 kiov->kiov_len -= nob;
174 return rc;
175 }
176
177 nob -= (int)kiov->kiov_len;
178 tx->tx_kiov = ++kiov;
179 tx->tx_nkiov--;
180 } while (nob != 0);
181
182 return (rc);
183}
184
185int
186ksocknal_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
187{
188 int rc;
189 int bufnob;
190
191 if (ksocknal_data.ksnd_stall_tx != 0) {
192 cfs_pause(cfs_time_seconds(ksocknal_data.ksnd_stall_tx));
193 }
194
195 LASSERT (tx->tx_resid != 0);
196
197 rc = ksocknal_connsock_addref(conn);
198 if (rc != 0) {
199 LASSERT (conn->ksnc_closing);
200 return (-ESHUTDOWN);
201 }
202
203 do {
204 if (ksocknal_data.ksnd_enomem_tx > 0) {
205 /* testing... */
206 ksocknal_data.ksnd_enomem_tx--;
207 rc = -EAGAIN;
208 } else if (tx->tx_niov != 0) {
209 rc = ksocknal_send_iov (conn, tx);
210 } else {
211 rc = ksocknal_send_kiov (conn, tx);
212 }
213
214 bufnob = cfs_sock_wmem_queued(conn->ksnc_sock);
215 if (rc > 0) /* sent something? */
216 conn->ksnc_tx_bufnob += rc; /* account it */
217
218 if (bufnob < conn->ksnc_tx_bufnob) {
219 /* allocated send buffer bytes < computed; infer
220 * something got ACKed */
221 conn->ksnc_tx_deadline =
222 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
223 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
224 conn->ksnc_tx_bufnob = bufnob;
225 mb();
226 }
227
228 if (rc <= 0) { /* Didn't write anything? */
229
230 if (rc == 0) /* some stacks return 0 instead of -EAGAIN */
231 rc = -EAGAIN;
232
233 /* Check if EAGAIN is due to memory pressure */
234 if(rc == -EAGAIN && ksocknal_lib_memory_pressure(conn))
235 rc = -ENOMEM;
236
237 break;
238 }
239
240 /* socket's wmem_queued now includes 'rc' bytes */
241 atomic_sub (rc, &conn->ksnc_tx_nob);
242 rc = 0;
243
244 } while (tx->tx_resid != 0);
245
246 ksocknal_connsock_decref(conn);
247 return (rc);
248}
249
250int
251ksocknal_recv_iov (ksock_conn_t *conn)
252{
253 struct iovec *iov = conn->ksnc_rx_iov;
254 int nob;
255 int rc;
256
257 LASSERT (conn->ksnc_rx_niov > 0);
258
259 /* Never touch conn->ksnc_rx_iov or change connection
260 * status inside ksocknal_lib_recv_iov */
261 rc = ksocknal_lib_recv_iov(conn);
262
263 if (rc <= 0)
264 return (rc);
265
266 /* received something... */
267 nob = rc;
268
269 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
270 conn->ksnc_rx_deadline =
271 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
272 mb(); /* order with setting rx_started */
273 conn->ksnc_rx_started = 1;
274
275 conn->ksnc_rx_nob_wanted -= nob;
276 conn->ksnc_rx_nob_left -= nob;
277
278 do {
279 LASSERT (conn->ksnc_rx_niov > 0);
280
281 if (nob < (int)iov->iov_len) {
282 iov->iov_len -= nob;
283 iov->iov_base = (void *)((char *)iov->iov_base + nob);
284 return (-EAGAIN);
285 }
286
287 nob -= iov->iov_len;
288 conn->ksnc_rx_iov = ++iov;
289 conn->ksnc_rx_niov--;
290 } while (nob != 0);
291
292 return (rc);
293}
294
295int
296ksocknal_recv_kiov (ksock_conn_t *conn)
297{
298 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
299 int nob;
300 int rc;
301 LASSERT (conn->ksnc_rx_nkiov > 0);
302
303 /* Never touch conn->ksnc_rx_kiov or change connection
304 * status inside ksocknal_lib_recv_iov */
305 rc = ksocknal_lib_recv_kiov(conn);
306
307 if (rc <= 0)
308 return (rc);
309
310 /* received something... */
311 nob = rc;
312
313 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
314 conn->ksnc_rx_deadline =
315 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
316 mb(); /* order with setting rx_started */
317 conn->ksnc_rx_started = 1;
318
319 conn->ksnc_rx_nob_wanted -= nob;
320 conn->ksnc_rx_nob_left -= nob;
321
322 do {
323 LASSERT (conn->ksnc_rx_nkiov > 0);
324
325 if (nob < (int) kiov->kiov_len) {
326 kiov->kiov_offset += nob;
327 kiov->kiov_len -= nob;
328 return -EAGAIN;
329 }
330
331 nob -= kiov->kiov_len;
332 conn->ksnc_rx_kiov = ++kiov;
333 conn->ksnc_rx_nkiov--;
334 } while (nob != 0);
335
336 return 1;
337}
338
339int
340ksocknal_receive (ksock_conn_t *conn)
341{
342 /* Return 1 on success, 0 on EOF, < 0 on error.
343 * Caller checks ksnc_rx_nob_wanted to determine
344 * progress/completion. */
345 int rc;
d7e09d03
PT
346
347 if (ksocknal_data.ksnd_stall_rx != 0) {
348 cfs_pause(cfs_time_seconds (ksocknal_data.ksnd_stall_rx));
349 }
350
351 rc = ksocknal_connsock_addref(conn);
352 if (rc != 0) {
353 LASSERT (conn->ksnc_closing);
354 return (-ESHUTDOWN);
355 }
356
357 for (;;) {
358 if (conn->ksnc_rx_niov != 0)
359 rc = ksocknal_recv_iov (conn);
360 else
361 rc = ksocknal_recv_kiov (conn);
362
363 if (rc <= 0) {
364 /* error/EOF or partial receive */
365 if (rc == -EAGAIN) {
366 rc = 1;
367 } else if (rc == 0 && conn->ksnc_rx_started) {
368 /* EOF in the middle of a message */
369 rc = -EPROTO;
370 }
371 break;
372 }
373
374 /* Completed a fragment */
375
376 if (conn->ksnc_rx_nob_wanted == 0) {
377 rc = 1;
378 break;
379 }
380 }
381
382 ksocknal_connsock_decref(conn);
0a3bdb00 383 return rc;
d7e09d03
PT
384}
385
386void
387ksocknal_tx_done (lnet_ni_t *ni, ksock_tx_t *tx)
388{
389 lnet_msg_t *lnetmsg = tx->tx_lnetmsg;
390 int rc = (tx->tx_resid == 0 && !tx->tx_zc_aborted) ? 0 : -EIO;
d7e09d03
PT
391
392 LASSERT(ni != NULL || tx->tx_conn != NULL);
393
394 if (tx->tx_conn != NULL)
395 ksocknal_conn_decref(tx->tx_conn);
396
397 if (ni == NULL && tx->tx_conn != NULL)
398 ni = tx->tx_conn->ksnc_peer->ksnp_ni;
399
400 ksocknal_free_tx (tx);
401 if (lnetmsg != NULL) /* KSOCK_MSG_NOOP go without lnetmsg */
402 lnet_finalize (ni, lnetmsg, rc);
d7e09d03
PT
403}
404
405void
406ksocknal_txlist_done (lnet_ni_t *ni, struct list_head *txlist, int error)
407{
408 ksock_tx_t *tx;
409
410 while (!list_empty (txlist)) {
411 tx = list_entry (txlist->next, ksock_tx_t, tx_list);
412
413 if (error && tx->tx_lnetmsg != NULL) {
414 CNETERR("Deleting packet type %d len %d %s->%s\n",
415 le32_to_cpu (tx->tx_lnetmsg->msg_hdr.type),
416 le32_to_cpu (tx->tx_lnetmsg->msg_hdr.payload_length),
417 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.src_nid)),
418 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.dest_nid)));
419 } else if (error) {
420 CNETERR("Deleting noop packet\n");
421 }
422
423 list_del (&tx->tx_list);
424
425 LASSERT (atomic_read(&tx->tx_refcount) == 1);
426 ksocknal_tx_done (ni, tx);
427 }
428}
429
430static void
431ksocknal_check_zc_req(ksock_tx_t *tx)
432{
433 ksock_conn_t *conn = tx->tx_conn;
434 ksock_peer_t *peer = conn->ksnc_peer;
435
436 /* Set tx_msg.ksm_zc_cookies[0] to a unique non-zero cookie and add tx
437 * to ksnp_zc_req_list if some fragment of this message should be sent
438 * zero-copy. Our peer will send an ACK containing this cookie when
439 * she has received this message to tell us we can signal completion.
440 * tx_msg.ksm_zc_cookies[0] remains non-zero while tx is on
441 * ksnp_zc_req_list. */
442 LASSERT (tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
443 LASSERT (tx->tx_zc_capable);
444
445 tx->tx_zc_checked = 1;
446
447 if (conn->ksnc_proto == &ksocknal_protocol_v1x ||
448 !conn->ksnc_zc_capable)
449 return;
450
451 /* assign cookie and queue tx to pending list, it will be released when
452 * a matching ack is received. See ksocknal_handle_zcack() */
453
454 ksocknal_tx_addref(tx);
455
456 spin_lock(&peer->ksnp_lock);
457
458 /* ZC_REQ is going to be pinned to the peer */
459 tx->tx_deadline =
460 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
461
462 LASSERT (tx->tx_msg.ksm_zc_cookies[0] == 0);
463
464 tx->tx_msg.ksm_zc_cookies[0] = peer->ksnp_zc_next_cookie++;
465
466 if (peer->ksnp_zc_next_cookie == 0)
467 peer->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
468
469 list_add_tail(&tx->tx_zc_list, &peer->ksnp_zc_req_list);
470
471 spin_unlock(&peer->ksnp_lock);
472}
473
474static void
475ksocknal_uncheck_zc_req(ksock_tx_t *tx)
476{
477 ksock_peer_t *peer = tx->tx_conn->ksnc_peer;
478
479 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
480 LASSERT(tx->tx_zc_capable);
481
482 tx->tx_zc_checked = 0;
483
484 spin_lock(&peer->ksnp_lock);
485
486 if (tx->tx_msg.ksm_zc_cookies[0] == 0) {
487 /* Not waiting for an ACK */
488 spin_unlock(&peer->ksnp_lock);
489 return;
490 }
491
492 tx->tx_msg.ksm_zc_cookies[0] = 0;
493 list_del(&tx->tx_zc_list);
494
495 spin_unlock(&peer->ksnp_lock);
496
497 ksocknal_tx_decref(tx);
498}
499
500int
501ksocknal_process_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
502{
503 int rc;
504
505 if (tx->tx_zc_capable && !tx->tx_zc_checked)
506 ksocknal_check_zc_req(tx);
507
508 rc = ksocknal_transmit (conn, tx);
509
510 CDEBUG (D_NET, "send(%d) %d\n", tx->tx_resid, rc);
511
512 if (tx->tx_resid == 0) {
513 /* Sent everything OK */
514 LASSERT (rc == 0);
515
516 return (0);
517 }
518
519 if (rc == -EAGAIN)
520 return (rc);
521
522 if (rc == -ENOMEM) {
523 static int counter;
524
525 counter++; /* exponential backoff warnings */
526 if ((counter & (-counter)) == counter)
527 CWARN("%u ENOMEM tx %p (%u allocated)\n",
528 counter, conn, atomic_read(&libcfs_kmemory));
529
530 /* Queue on ksnd_enomem_conns for retry after a timeout */
531 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
532
533 /* enomem list takes over scheduler's ref... */
534 LASSERT (conn->ksnc_tx_scheduled);
535 list_add_tail(&conn->ksnc_tx_list,
536 &ksocknal_data.ksnd_enomem_conns);
537 if (!cfs_time_aftereq(cfs_time_add(cfs_time_current(),
538 SOCKNAL_ENOMEM_RETRY),
539 ksocknal_data.ksnd_reaper_waketime))
540 wake_up (&ksocknal_data.ksnd_reaper_waitq);
541
542 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
543 return (rc);
544 }
545
546 /* Actual error */
547 LASSERT (rc < 0);
548
549 if (!conn->ksnc_closing) {
550 switch (rc) {
551 case -ECONNRESET:
5e8f6920 552 LCONSOLE_WARN("Host %pI4h reset our connection "
d7e09d03
PT
553 "while we were sending data; it may have "
554 "rebooted.\n",
5e8f6920 555 &conn->ksnc_ipaddr);
d7e09d03
PT
556 break;
557 default:
558 LCONSOLE_WARN("There was an unexpected network error "
5e8f6920
PT
559 "while writing to %pI4h: %d.\n",
560 &conn->ksnc_ipaddr, rc);
d7e09d03
PT
561 break;
562 }
563 CDEBUG(D_NET, "[%p] Error %d on write to %s"
5e8f6920 564 " ip %pI4h:%d\n", conn, rc,
d7e09d03 565 libcfs_id2str(conn->ksnc_peer->ksnp_id),
5e8f6920 566 &conn->ksnc_ipaddr,
d7e09d03
PT
567 conn->ksnc_port);
568 }
569
570 if (tx->tx_zc_checked)
571 ksocknal_uncheck_zc_req(tx);
572
573 /* it's not an error if conn is being closed */
574 ksocknal_close_conn_and_siblings (conn,
575 (conn->ksnc_closing) ? 0 : rc);
576
577 return (rc);
578}
579
580void
581ksocknal_launch_connection_locked (ksock_route_t *route)
582{
583
584 /* called holding write lock on ksnd_global_lock */
585
586 LASSERT (!route->ksnr_scheduled);
587 LASSERT (!route->ksnr_connecting);
588 LASSERT ((ksocknal_route_mask() & ~route->ksnr_connected) != 0);
589
590 route->ksnr_scheduled = 1; /* scheduling conn for connd */
591 ksocknal_route_addref(route); /* extra ref for connd */
592
593 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
594
595 list_add_tail(&route->ksnr_connd_list,
596 &ksocknal_data.ksnd_connd_routes);
597 wake_up(&ksocknal_data.ksnd_connd_waitq);
598
599 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
600}
601
602void
603ksocknal_launch_all_connections_locked (ksock_peer_t *peer)
604{
605 ksock_route_t *route;
606
607 /* called holding write lock on ksnd_global_lock */
608 for (;;) {
609 /* launch any/all connections that need it */
610 route = ksocknal_find_connectable_route_locked(peer);
611 if (route == NULL)
612 return;
613
614 ksocknal_launch_connection_locked(route);
615 }
616}
617
618ksock_conn_t *
619ksocknal_find_conn_locked(ksock_peer_t *peer, ksock_tx_t *tx, int nonblk)
620{
621 struct list_head *tmp;
622 ksock_conn_t *conn;
623 ksock_conn_t *typed = NULL;
624 ksock_conn_t *fallback = NULL;
625 int tnob = 0;
626 int fnob = 0;
627
628 list_for_each (tmp, &peer->ksnp_conns) {
629 ksock_conn_t *c = list_entry(tmp, ksock_conn_t, ksnc_list);
630 int nob = atomic_read(&c->ksnc_tx_nob) +
631 cfs_sock_wmem_queued(c->ksnc_sock);
632 int rc;
633
634 LASSERT (!c->ksnc_closing);
635 LASSERT (c->ksnc_proto != NULL &&
636 c->ksnc_proto->pro_match_tx != NULL);
637
638 rc = c->ksnc_proto->pro_match_tx(c, tx, nonblk);
639
640 switch (rc) {
641 default:
642 LBUG();
643 case SOCKNAL_MATCH_NO: /* protocol rejected the tx */
644 continue;
645
646 case SOCKNAL_MATCH_YES: /* typed connection */
647 if (typed == NULL || tnob > nob ||
648 (tnob == nob && *ksocknal_tunables.ksnd_round_robin &&
649 cfs_time_after(typed->ksnc_tx_last_post, c->ksnc_tx_last_post))) {
650 typed = c;
651 tnob = nob;
652 }
653 break;
654
655 case SOCKNAL_MATCH_MAY: /* fallback connection */
656 if (fallback == NULL || fnob > nob ||
657 (fnob == nob && *ksocknal_tunables.ksnd_round_robin &&
658 cfs_time_after(fallback->ksnc_tx_last_post, c->ksnc_tx_last_post))) {
659 fallback = c;
660 fnob = nob;
661 }
662 break;
663 }
664 }
665
666 /* prefer the typed selection */
667 conn = (typed != NULL) ? typed : fallback;
668
669 if (conn != NULL)
670 conn->ksnc_tx_last_post = cfs_time_current();
671
672 return conn;
673}
674
675void
676ksocknal_tx_prep(ksock_conn_t *conn, ksock_tx_t *tx)
677{
678 conn->ksnc_proto->pro_pack(tx);
679
680 atomic_add (tx->tx_nob, &conn->ksnc_tx_nob);
681 ksocknal_conn_addref(conn); /* +1 ref for tx */
682 tx->tx_conn = conn;
683}
684
685void
686ksocknal_queue_tx_locked (ksock_tx_t *tx, ksock_conn_t *conn)
687{
688 ksock_sched_t *sched = conn->ksnc_scheduler;
689 ksock_msg_t *msg = &tx->tx_msg;
690 ksock_tx_t *ztx = NULL;
691 int bufnob = 0;
692
693 /* called holding global lock (read or irq-write) and caller may
694 * not have dropped this lock between finding conn and calling me,
695 * so we don't need the {get,put}connsock dance to deref
696 * ksnc_sock... */
697 LASSERT(!conn->ksnc_closing);
698
5e8f6920 699 CDEBUG(D_NET, "Sending to %s ip %pI4h:%d\n",
d7e09d03 700 libcfs_id2str(conn->ksnc_peer->ksnp_id),
5e8f6920 701 &conn->ksnc_ipaddr,
d7e09d03
PT
702 conn->ksnc_port);
703
704 ksocknal_tx_prep(conn, tx);
705
706 /* Ensure the frags we've been given EXACTLY match the number of
707 * bytes we want to send. Many TCP/IP stacks disregard any total
708 * size parameters passed to them and just look at the frags.
709 *
710 * We always expect at least 1 mapped fragment containing the
711 * complete ksocknal message header. */
712 LASSERT (lnet_iov_nob (tx->tx_niov, tx->tx_iov) +
713 lnet_kiov_nob(tx->tx_nkiov, tx->tx_kiov) ==
714 (unsigned int)tx->tx_nob);
715 LASSERT (tx->tx_niov >= 1);
716 LASSERT (tx->tx_resid == tx->tx_nob);
717
718 CDEBUG (D_NET, "Packet %p type %d, nob %d niov %d nkiov %d\n",
719 tx, (tx->tx_lnetmsg != NULL) ? tx->tx_lnetmsg->msg_hdr.type:
720 KSOCK_MSG_NOOP,
721 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
722
723 /*
724 * FIXME: SOCK_WMEM_QUEUED and SOCK_ERROR could block in __DARWIN8__
725 * but they're used inside spinlocks a lot.
726 */
727 bufnob = cfs_sock_wmem_queued(conn->ksnc_sock);
728 spin_lock_bh(&sched->kss_lock);
729
730 if (list_empty(&conn->ksnc_tx_queue) && bufnob == 0) {
731 /* First packet starts the timeout */
732 conn->ksnc_tx_deadline =
733 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
734 if (conn->ksnc_tx_bufnob > 0) /* something got ACKed */
735 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
736 conn->ksnc_tx_bufnob = 0;
737 mb(); /* order with adding to tx_queue */
738 }
739
740 if (msg->ksm_type == KSOCK_MSG_NOOP) {
741 /* The packet is noop ZC ACK, try to piggyback the ack_cookie
742 * on a normal packet so I don't need to send it */
743 LASSERT (msg->ksm_zc_cookies[1] != 0);
744 LASSERT (conn->ksnc_proto->pro_queue_tx_zcack != NULL);
745
746 if (conn->ksnc_proto->pro_queue_tx_zcack(conn, tx, 0))
747 ztx = tx; /* ZC ACK piggybacked on ztx release tx later */
748
749 } else {
750 /* It's a normal packet - can it piggback a noop zc-ack that
751 * has been queued already? */
752 LASSERT (msg->ksm_zc_cookies[1] == 0);
753 LASSERT (conn->ksnc_proto->pro_queue_tx_msg != NULL);
754
755 ztx = conn->ksnc_proto->pro_queue_tx_msg(conn, tx);
756 /* ztx will be released later */
757 }
758
759 if (ztx != NULL) {
760 atomic_sub (ztx->tx_nob, &conn->ksnc_tx_nob);
761 list_add_tail(&ztx->tx_list, &sched->kss_zombie_noop_txs);
762 }
763
764 if (conn->ksnc_tx_ready && /* able to send */
765 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
766 /* +1 ref for scheduler */
767 ksocknal_conn_addref(conn);
768 list_add_tail (&conn->ksnc_tx_list,
769 &sched->kss_tx_conns);
770 conn->ksnc_tx_scheduled = 1;
771 wake_up (&sched->kss_waitq);
772 }
773
774 spin_unlock_bh(&sched->kss_lock);
775}
776
777
778ksock_route_t *
779ksocknal_find_connectable_route_locked (ksock_peer_t *peer)
780{
781 cfs_time_t now = cfs_time_current();
782 struct list_head *tmp;
783 ksock_route_t *route;
784
785 list_for_each (tmp, &peer->ksnp_routes) {
786 route = list_entry (tmp, ksock_route_t, ksnr_list);
787
788 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
789
790 if (route->ksnr_scheduled) /* connections being established */
791 continue;
792
793 /* all route types connected ? */
794 if ((ksocknal_route_mask() & ~route->ksnr_connected) == 0)
795 continue;
796
797 if (!(route->ksnr_retry_interval == 0 || /* first attempt */
798 cfs_time_aftereq(now, route->ksnr_timeout))) {
799 CDEBUG(D_NET,
5e8f6920 800 "Too soon to retry route %pI4h "
d7e09d03 801 "(cnted %d, interval %ld, %ld secs later)\n",
5e8f6920 802 &route->ksnr_ipaddr,
d7e09d03
PT
803 route->ksnr_connected,
804 route->ksnr_retry_interval,
805 cfs_duration_sec(route->ksnr_timeout - now));
806 continue;
807 }
808
809 return (route);
810 }
811
812 return (NULL);
813}
814
815ksock_route_t *
816ksocknal_find_connecting_route_locked (ksock_peer_t *peer)
817{
818 struct list_head *tmp;
819 ksock_route_t *route;
820
821 list_for_each (tmp, &peer->ksnp_routes) {
822 route = list_entry (tmp, ksock_route_t, ksnr_list);
823
824 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
825
826 if (route->ksnr_scheduled)
827 return (route);
828 }
829
830 return (NULL);
831}
832
833int
834ksocknal_launch_packet (lnet_ni_t *ni, ksock_tx_t *tx, lnet_process_id_t id)
835{
836 ksock_peer_t *peer;
837 ksock_conn_t *conn;
838 rwlock_t *g_lock;
839 int retry;
840 int rc;
841
842 LASSERT (tx->tx_conn == NULL);
843
844 g_lock = &ksocknal_data.ksnd_global_lock;
845
846 for (retry = 0;; retry = 1) {
847 read_lock(g_lock);
848 peer = ksocknal_find_peer_locked(ni, id);
849 if (peer != NULL) {
850 if (ksocknal_find_connectable_route_locked(peer) == NULL) {
851 conn = ksocknal_find_conn_locked(peer, tx, tx->tx_nonblk);
852 if (conn != NULL) {
853 /* I've got no routes that need to be
854 * connecting and I do have an actual
855 * connection... */
856 ksocknal_queue_tx_locked (tx, conn);
857 read_unlock(g_lock);
858 return (0);
859 }
860 }
861 }
862
863 /* I'll need a write lock... */
864 read_unlock(g_lock);
865
866 write_lock_bh(g_lock);
867
868 peer = ksocknal_find_peer_locked(ni, id);
869 if (peer != NULL)
870 break;
871
872 write_unlock_bh(g_lock);
873
874 if ((id.pid & LNET_PID_USERFLAG) != 0) {
875 CERROR("Refusing to create a connection to "
876 "userspace process %s\n", libcfs_id2str(id));
877 return -EHOSTUNREACH;
878 }
879
880 if (retry) {
881 CERROR("Can't find peer %s\n", libcfs_id2str(id));
882 return -EHOSTUNREACH;
883 }
884
885 rc = ksocknal_add_peer(ni, id,
886 LNET_NIDADDR(id.nid),
887 lnet_acceptor_port());
888 if (rc != 0) {
889 CERROR("Can't add peer %s: %d\n",
890 libcfs_id2str(id), rc);
891 return rc;
892 }
893 }
894
895 ksocknal_launch_all_connections_locked(peer);
896
897 conn = ksocknal_find_conn_locked(peer, tx, tx->tx_nonblk);
898 if (conn != NULL) {
899 /* Connection exists; queue message on it */
900 ksocknal_queue_tx_locked (tx, conn);
901 write_unlock_bh(g_lock);
902 return (0);
903 }
904
905 if (peer->ksnp_accepting > 0 ||
906 ksocknal_find_connecting_route_locked (peer) != NULL) {
907 /* the message is going to be pinned to the peer */
908 tx->tx_deadline =
909 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
910
911 /* Queue the message until a connection is established */
912 list_add_tail (&tx->tx_list, &peer->ksnp_tx_queue);
913 write_unlock_bh(g_lock);
914 return 0;
915 }
916
917 write_unlock_bh(g_lock);
918
919 /* NB Routes may be ignored if connections to them failed recently */
920 CNETERR("No usable routes to %s\n", libcfs_id2str(id));
921 return (-EHOSTUNREACH);
922}
923
924int
925ksocknal_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
926{
927 int mpflag = 0;
928 int type = lntmsg->msg_type;
929 lnet_process_id_t target = lntmsg->msg_target;
930 unsigned int payload_niov = lntmsg->msg_niov;
931 struct iovec *payload_iov = lntmsg->msg_iov;
932 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
933 unsigned int payload_offset = lntmsg->msg_offset;
934 unsigned int payload_nob = lntmsg->msg_len;
935 ksock_tx_t *tx;
936 int desc_size;
937 int rc;
938
939 /* NB 'private' is different depending on what we're sending.
940 * Just ignore it... */
941
942 CDEBUG(D_NET, "sending %u bytes in %d frags to %s\n",
943 payload_nob, payload_niov, libcfs_id2str(target));
944
945 LASSERT (payload_nob == 0 || payload_niov > 0);
946 LASSERT (payload_niov <= LNET_MAX_IOV);
947 /* payload is either all vaddrs or all pages */
948 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
949 LASSERT (!in_interrupt ());
950
951 if (payload_iov != NULL)
952 desc_size = offsetof(ksock_tx_t,
953 tx_frags.virt.iov[1 + payload_niov]);
954 else
955 desc_size = offsetof(ksock_tx_t,
956 tx_frags.paged.kiov[payload_niov]);
957
958 if (lntmsg->msg_vmflush)
959 mpflag = cfs_memory_pressure_get_and_set();
960 tx = ksocknal_alloc_tx(KSOCK_MSG_LNET, desc_size);
961 if (tx == NULL) {
962 CERROR("Can't allocate tx desc type %d size %d\n",
963 type, desc_size);
964 if (lntmsg->msg_vmflush)
965 cfs_memory_pressure_restore(mpflag);
966 return (-ENOMEM);
967 }
968
969 tx->tx_conn = NULL; /* set when assigned a conn */
970 tx->tx_lnetmsg = lntmsg;
971
972 if (payload_iov != NULL) {
973 tx->tx_kiov = NULL;
974 tx->tx_nkiov = 0;
975 tx->tx_iov = tx->tx_frags.virt.iov;
976 tx->tx_niov = 1 +
977 lnet_extract_iov(payload_niov, &tx->tx_iov[1],
978 payload_niov, payload_iov,
979 payload_offset, payload_nob);
980 } else {
981 tx->tx_niov = 1;
982 tx->tx_iov = &tx->tx_frags.paged.iov;
983 tx->tx_kiov = tx->tx_frags.paged.kiov;
984 tx->tx_nkiov = lnet_extract_kiov(payload_niov, tx->tx_kiov,
985 payload_niov, payload_kiov,
986 payload_offset, payload_nob);
987
988 if (payload_nob >= *ksocknal_tunables.ksnd_zc_min_payload)
989 tx->tx_zc_capable = 1;
990 }
991
992 socklnd_init_msg(&tx->tx_msg, KSOCK_MSG_LNET);
993
994 /* The first fragment will be set later in pro_pack */
995 rc = ksocknal_launch_packet(ni, tx, target);
996 if (lntmsg->msg_vmflush)
997 cfs_memory_pressure_restore(mpflag);
998 if (rc == 0)
999 return (0);
1000
1001 ksocknal_free_tx(tx);
1002 return (-EIO);
1003}
1004
1005int
1006ksocknal_thread_start(int (*fn)(void *arg), void *arg, char *name)
1007{
1008 task_t *task = kthread_run(fn, arg, name);
1009
1010 if (IS_ERR(task))
1011 return PTR_ERR(task);
1012
1013 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1014 ksocknal_data.ksnd_nthreads++;
1015 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1016 return 0;
1017}
1018
1019void
1020ksocknal_thread_fini (void)
1021{
1022 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1023 ksocknal_data.ksnd_nthreads--;
1024 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1025}
1026
1027int
1028ksocknal_new_packet (ksock_conn_t *conn, int nob_to_skip)
1029{
1030 static char ksocknal_slop_buffer[4096];
1031
1032 int nob;
1033 unsigned int niov;
1034 int skipped;
1035
1036 LASSERT(conn->ksnc_proto != NULL);
1037
1038 if ((*ksocknal_tunables.ksnd_eager_ack & conn->ksnc_type) != 0) {
1039 /* Remind the socket to ack eagerly... */
1040 ksocknal_lib_eager_ack(conn);
1041 }
1042
1043 if (nob_to_skip == 0) { /* right at next packet boundary now */
1044 conn->ksnc_rx_started = 0;
1045 mb(); /* racing with timeout thread */
1046
1047 switch (conn->ksnc_proto->pro_version) {
1048 case KSOCK_PROTO_V2:
1049 case KSOCK_PROTO_V3:
1050 conn->ksnc_rx_state = SOCKNAL_RX_KSM_HEADER;
1051 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1052 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg;
1053
1054 conn->ksnc_rx_nob_wanted = offsetof(ksock_msg_t, ksm_u);
1055 conn->ksnc_rx_nob_left = offsetof(ksock_msg_t, ksm_u);
1056 conn->ksnc_rx_iov[0].iov_len = offsetof(ksock_msg_t, ksm_u);
1057 break;
1058
1059 case KSOCK_PROTO_V1:
1060 /* Receiving bare lnet_hdr_t */
1061 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1062 conn->ksnc_rx_nob_wanted = sizeof(lnet_hdr_t);
1063 conn->ksnc_rx_nob_left = sizeof(lnet_hdr_t);
1064
1065 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1066 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1067 conn->ksnc_rx_iov[0].iov_len = sizeof (lnet_hdr_t);
1068 break;
1069
1070 default:
1071 LBUG ();
1072 }
1073 conn->ksnc_rx_niov = 1;
1074
1075 conn->ksnc_rx_kiov = NULL;
1076 conn->ksnc_rx_nkiov = 0;
1077 conn->ksnc_rx_csum = ~0;
1078 return (1);
1079 }
1080
1081 /* Set up to skip as much as possible now. If there's more left
1082 * (ran out of iov entries) we'll get called again */
1083
1084 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1085 conn->ksnc_rx_nob_left = nob_to_skip;
1086 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1087 skipped = 0;
1088 niov = 0;
1089
1090 do {
1091 nob = MIN (nob_to_skip, sizeof (ksocknal_slop_buffer));
1092
1093 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1094 conn->ksnc_rx_iov[niov].iov_len = nob;
1095 niov++;
1096 skipped += nob;
1097 nob_to_skip -=nob;
1098
1099 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1100 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof (struct iovec));
1101
1102 conn->ksnc_rx_niov = niov;
1103 conn->ksnc_rx_kiov = NULL;
1104 conn->ksnc_rx_nkiov = 0;
1105 conn->ksnc_rx_nob_wanted = skipped;
1106 return (0);
1107}
1108
1109int
1110ksocknal_process_receive (ksock_conn_t *conn)
1111{
1112 lnet_hdr_t *lhdr;
1113 lnet_process_id_t *id;
1114 int rc;
1115
1116 LASSERT (atomic_read(&conn->ksnc_conn_refcount) > 0);
1117
1118 /* NB: sched lock NOT held */
1119 /* SOCKNAL_RX_LNET_HEADER is here for backward compatability */
1120 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_KSM_HEADER ||
1121 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD ||
1122 conn->ksnc_rx_state == SOCKNAL_RX_LNET_HEADER ||
1123 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1124 again:
1125 if (conn->ksnc_rx_nob_wanted != 0) {
1126 rc = ksocknal_receive(conn);
1127
1128 if (rc <= 0) {
1129 LASSERT (rc != -EAGAIN);
1130
1131 if (rc == 0)
5e8f6920
PT
1132 CDEBUG(D_NET, "[%p] EOF from %s"
1133 " ip %pI4h:%d\n", conn,
d7e09d03 1134 libcfs_id2str(conn->ksnc_peer->ksnp_id),
5e8f6920 1135 &conn->ksnc_ipaddr,
d7e09d03
PT
1136 conn->ksnc_port);
1137 else if (!conn->ksnc_closing)
5e8f6920
PT
1138 CERROR("[%p] Error %d on read from %s"
1139 " ip %pI4h:%d\n",
d7e09d03
PT
1140 conn, rc,
1141 libcfs_id2str(conn->ksnc_peer->ksnp_id),
5e8f6920 1142 &conn->ksnc_ipaddr,
d7e09d03
PT
1143 conn->ksnc_port);
1144
1145 /* it's not an error if conn is being closed */
1146 ksocknal_close_conn_and_siblings (conn,
1147 (conn->ksnc_closing) ? 0 : rc);
1148 return (rc == 0 ? -ESHUTDOWN : rc);
1149 }
1150
1151 if (conn->ksnc_rx_nob_wanted != 0) {
1152 /* short read */
1153 return (-EAGAIN);
1154 }
1155 }
1156 switch (conn->ksnc_rx_state) {
1157 case SOCKNAL_RX_KSM_HEADER:
1158 if (conn->ksnc_flip) {
1159 __swab32s(&conn->ksnc_msg.ksm_type);
1160 __swab32s(&conn->ksnc_msg.ksm_csum);
1161 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[0]);
1162 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[1]);
1163 }
1164
1165 if (conn->ksnc_msg.ksm_type != KSOCK_MSG_NOOP &&
1166 conn->ksnc_msg.ksm_type != KSOCK_MSG_LNET) {
1167 CERROR("%s: Unknown message type: %x\n",
1168 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1169 conn->ksnc_msg.ksm_type);
1170 ksocknal_new_packet(conn, 0);
1171 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1172 return (-EPROTO);
1173 }
1174
1175 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP &&
1176 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1177 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1178 /* NOOP Checksum error */
1179 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1180 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1181 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1182 ksocknal_new_packet(conn, 0);
1183 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1184 return (-EIO);
1185 }
1186
1187 if (conn->ksnc_msg.ksm_zc_cookies[1] != 0) {
1188 __u64 cookie = 0;
1189
1190 LASSERT (conn->ksnc_proto != &ksocknal_protocol_v1x);
1191
1192 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP)
1193 cookie = conn->ksnc_msg.ksm_zc_cookies[0];
1194
1195 rc = conn->ksnc_proto->pro_handle_zcack(conn, cookie,
1196 conn->ksnc_msg.ksm_zc_cookies[1]);
1197
1198 if (rc != 0) {
1199 CERROR("%s: Unknown ZC-ACK cookie: "LPU64", "LPU64"\n",
1200 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1201 cookie, conn->ksnc_msg.ksm_zc_cookies[1]);
1202 ksocknal_new_packet(conn, 0);
1203 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1204 return (rc);
1205 }
1206 }
1207
1208 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP) {
1209 ksocknal_new_packet (conn, 0);
1210 return 0; /* NOOP is done and just return */
1211 }
1212
1213 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1214 conn->ksnc_rx_nob_wanted = sizeof(ksock_lnet_msg_t);
1215 conn->ksnc_rx_nob_left = sizeof(ksock_lnet_msg_t);
1216
1217 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1218 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1219 conn->ksnc_rx_iov[0].iov_len = sizeof(ksock_lnet_msg_t);
1220
1221 conn->ksnc_rx_niov = 1;
1222 conn->ksnc_rx_kiov = NULL;
1223 conn->ksnc_rx_nkiov = 0;
1224
1225 goto again; /* read lnet header now */
1226
1227 case SOCKNAL_RX_LNET_HEADER:
1228 /* unpack message header */
1229 conn->ksnc_proto->pro_unpack(&conn->ksnc_msg);
1230
1231 if ((conn->ksnc_peer->ksnp_id.pid & LNET_PID_USERFLAG) != 0) {
1232 /* Userspace peer */
1233 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1234 id = &conn->ksnc_peer->ksnp_id;
1235
1236 /* Substitute process ID assigned at connection time */
1237 lhdr->src_pid = cpu_to_le32(id->pid);
1238 lhdr->src_nid = cpu_to_le64(id->nid);
1239 }
1240
1241 conn->ksnc_rx_state = SOCKNAL_RX_PARSE;
1242 ksocknal_conn_addref(conn); /* ++ref while parsing */
1243
1244 rc = lnet_parse(conn->ksnc_peer->ksnp_ni,
1245 &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr,
1246 conn->ksnc_peer->ksnp_id.nid, conn, 0);
1247 if (rc < 0) {
1248 /* I just received garbage: give up on this conn */
1249 ksocknal_new_packet(conn, 0);
1250 ksocknal_close_conn_and_siblings (conn, rc);
1251 ksocknal_conn_decref(conn);
1252 return (-EPROTO);
1253 }
1254
1255 /* I'm racing with ksocknal_recv() */
1256 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_PARSE ||
1257 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD);
1258
1259 if (conn->ksnc_rx_state != SOCKNAL_RX_LNET_PAYLOAD)
1260 return 0;
1261
1262 /* ksocknal_recv() got called */
1263 goto again;
1264
1265 case SOCKNAL_RX_LNET_PAYLOAD:
1266 /* payload all received */
1267 rc = 0;
1268
1269 if (conn->ksnc_rx_nob_left == 0 && /* not truncating */
1270 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1271 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1272 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1273 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1274 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1275 rc = -EIO;
1276 }
1277
1278 if (rc == 0 && conn->ksnc_msg.ksm_zc_cookies[0] != 0) {
1279 LASSERT(conn->ksnc_proto != &ksocknal_protocol_v1x);
1280
1281 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1282 id = &conn->ksnc_peer->ksnp_id;
1283
1284 rc = conn->ksnc_proto->pro_handle_zcreq(conn,
1285 conn->ksnc_msg.ksm_zc_cookies[0],
1286 *ksocknal_tunables.ksnd_nonblk_zcack ||
1287 le64_to_cpu(lhdr->src_nid) != id->nid);
1288 }
1289
1290 lnet_finalize(conn->ksnc_peer->ksnp_ni, conn->ksnc_cookie, rc);
1291
1292 if (rc != 0) {
1293 ksocknal_new_packet(conn, 0);
1294 ksocknal_close_conn_and_siblings (conn, rc);
1295 return (-EPROTO);
1296 }
1297 /* Fall through */
1298
1299 case SOCKNAL_RX_SLOP:
1300 /* starting new packet? */
1301 if (ksocknal_new_packet (conn, conn->ksnc_rx_nob_left))
1302 return 0; /* come back later */
1303 goto again; /* try to finish reading slop now */
1304
1305 default:
1306 break;
1307 }
1308
1309 /* Not Reached */
1310 LBUG ();
1311 return (-EINVAL); /* keep gcc happy */
1312}
1313
1314int
1315ksocknal_recv (lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
1316 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1317 unsigned int offset, unsigned int mlen, unsigned int rlen)
1318{
1319 ksock_conn_t *conn = (ksock_conn_t *)private;
1320 ksock_sched_t *sched = conn->ksnc_scheduler;
1321
1322 LASSERT (mlen <= rlen);
1323 LASSERT (niov <= LNET_MAX_IOV);
1324
1325 conn->ksnc_cookie = msg;
1326 conn->ksnc_rx_nob_wanted = mlen;
1327 conn->ksnc_rx_nob_left = rlen;
1328
1329 if (mlen == 0 || iov != NULL) {
1330 conn->ksnc_rx_nkiov = 0;
1331 conn->ksnc_rx_kiov = NULL;
1332 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1333 conn->ksnc_rx_niov =
1334 lnet_extract_iov(LNET_MAX_IOV, conn->ksnc_rx_iov,
1335 niov, iov, offset, mlen);
1336 } else {
1337 conn->ksnc_rx_niov = 0;
1338 conn->ksnc_rx_iov = NULL;
1339 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1340 conn->ksnc_rx_nkiov =
1341 lnet_extract_kiov(LNET_MAX_IOV, conn->ksnc_rx_kiov,
1342 niov, kiov, offset, mlen);
1343 }
1344
1345 LASSERT (mlen ==
1346 lnet_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1347 lnet_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1348
1349 LASSERT (conn->ksnc_rx_scheduled);
1350
1351 spin_lock_bh(&sched->kss_lock);
1352
1353 switch (conn->ksnc_rx_state) {
1354 case SOCKNAL_RX_PARSE_WAIT:
1355 list_add_tail(&conn->ksnc_rx_list, &sched->kss_rx_conns);
1356 wake_up (&sched->kss_waitq);
1357 LASSERT (conn->ksnc_rx_ready);
1358 break;
1359
1360 case SOCKNAL_RX_PARSE:
1361 /* scheduler hasn't noticed I'm parsing yet */
1362 break;
1363 }
1364
1365 conn->ksnc_rx_state = SOCKNAL_RX_LNET_PAYLOAD;
1366
1367 spin_unlock_bh(&sched->kss_lock);
1368 ksocknal_conn_decref(conn);
1369 return 0;
1370}
1371
1372static inline int
1373ksocknal_sched_cansleep(ksock_sched_t *sched)
1374{
1375 int rc;
1376
1377 spin_lock_bh(&sched->kss_lock);
1378
1379 rc = (!ksocknal_data.ksnd_shuttingdown &&
1380 list_empty(&sched->kss_rx_conns) &&
1381 list_empty(&sched->kss_tx_conns));
1382
1383 spin_unlock_bh(&sched->kss_lock);
1384 return rc;
1385}
1386
1387int ksocknal_scheduler(void *arg)
1388{
1389 struct ksock_sched_info *info;
1390 ksock_sched_t *sched;
1391 ksock_conn_t *conn;
1392 ksock_tx_t *tx;
1393 int rc;
1394 int nloops = 0;
1395 long id = (long)arg;
1396
1397 info = ksocknal_data.ksnd_sched_info[KSOCK_THREAD_CPT(id)];
1398 sched = &info->ksi_scheds[KSOCK_THREAD_SID(id)];
1399
1400 cfs_block_allsigs();
1401
1402 rc = cfs_cpt_bind(lnet_cpt_table(), info->ksi_cpt);
1403 if (rc != 0) {
1404 CERROR("Can't set CPT affinity to %d: %d\n",
1405 info->ksi_cpt, rc);
1406 }
1407
1408 spin_lock_bh(&sched->kss_lock);
1409
1410 while (!ksocknal_data.ksnd_shuttingdown) {
1411 int did_something = 0;
1412
1413 /* Ensure I progress everything semi-fairly */
1414
1415 if (!list_empty (&sched->kss_rx_conns)) {
1416 conn = list_entry(sched->kss_rx_conns.next,
1417 ksock_conn_t, ksnc_rx_list);
1418 list_del(&conn->ksnc_rx_list);
1419
1420 LASSERT(conn->ksnc_rx_scheduled);
1421 LASSERT(conn->ksnc_rx_ready);
1422
1423 /* clear rx_ready in case receive isn't complete.
1424 * Do it BEFORE we call process_recv, since
1425 * data_ready can set it any time after we release
1426 * kss_lock. */
1427 conn->ksnc_rx_ready = 0;
1428 spin_unlock_bh(&sched->kss_lock);
1429
1430 rc = ksocknal_process_receive(conn);
1431
1432 spin_lock_bh(&sched->kss_lock);
1433
1434 /* I'm the only one that can clear this flag */
1435 LASSERT(conn->ksnc_rx_scheduled);
1436
1437 /* Did process_receive get everything it wanted? */
1438 if (rc == 0)
1439 conn->ksnc_rx_ready = 1;
1440
1441 if (conn->ksnc_rx_state == SOCKNAL_RX_PARSE) {
1442 /* Conn blocked waiting for ksocknal_recv()
1443 * I change its state (under lock) to signal
1444 * it can be rescheduled */
1445 conn->ksnc_rx_state = SOCKNAL_RX_PARSE_WAIT;
1446 } else if (conn->ksnc_rx_ready) {
1447 /* reschedule for rx */
1448 list_add_tail (&conn->ksnc_rx_list,
1449 &sched->kss_rx_conns);
1450 } else {
1451 conn->ksnc_rx_scheduled = 0;
1452 /* drop my ref */
1453 ksocknal_conn_decref(conn);
1454 }
1455
1456 did_something = 1;
1457 }
1458
1459 if (!list_empty (&sched->kss_tx_conns)) {
1460 LIST_HEAD (zlist);
1461
1462 if (!list_empty(&sched->kss_zombie_noop_txs)) {
1463 list_add(&zlist,
1464 &sched->kss_zombie_noop_txs);
1465 list_del_init(&sched->kss_zombie_noop_txs);
1466 }
1467
1468 conn = list_entry(sched->kss_tx_conns.next,
1469 ksock_conn_t, ksnc_tx_list);
1470 list_del (&conn->ksnc_tx_list);
1471
1472 LASSERT(conn->ksnc_tx_scheduled);
1473 LASSERT(conn->ksnc_tx_ready);
1474 LASSERT(!list_empty(&conn->ksnc_tx_queue));
1475
1476 tx = list_entry(conn->ksnc_tx_queue.next,
1477 ksock_tx_t, tx_list);
1478
1479 if (conn->ksnc_tx_carrier == tx)
1480 ksocknal_next_tx_carrier(conn);
1481
1482 /* dequeue now so empty list => more to send */
1483 list_del(&tx->tx_list);
1484
1485 /* Clear tx_ready in case send isn't complete. Do
1486 * it BEFORE we call process_transmit, since
1487 * write_space can set it any time after we release
1488 * kss_lock. */
1489 conn->ksnc_tx_ready = 0;
1490 spin_unlock_bh(&sched->kss_lock);
1491
1492 if (!list_empty(&zlist)) {
1493 /* free zombie noop txs, it's fast because
1494 * noop txs are just put in freelist */
1495 ksocknal_txlist_done(NULL, &zlist, 0);
1496 }
1497
1498 rc = ksocknal_process_transmit(conn, tx);
1499
1500 if (rc == -ENOMEM || rc == -EAGAIN) {
1501 /* Incomplete send: replace tx on HEAD of tx_queue */
1502 spin_lock_bh(&sched->kss_lock);
1503 list_add(&tx->tx_list,
1504 &conn->ksnc_tx_queue);
1505 } else {
1506 /* Complete send; tx -ref */
1507 ksocknal_tx_decref(tx);
1508
1509 spin_lock_bh(&sched->kss_lock);
1510 /* assume space for more */
1511 conn->ksnc_tx_ready = 1;
1512 }
1513
1514 if (rc == -ENOMEM) {
1515 /* Do nothing; after a short timeout, this
1516 * conn will be reposted on kss_tx_conns. */
1517 } else if (conn->ksnc_tx_ready &&
1518 !list_empty (&conn->ksnc_tx_queue)) {
1519 /* reschedule for tx */
1520 list_add_tail (&conn->ksnc_tx_list,
1521 &sched->kss_tx_conns);
1522 } else {
1523 conn->ksnc_tx_scheduled = 0;
1524 /* drop my ref */
1525 ksocknal_conn_decref(conn);
1526 }
1527
1528 did_something = 1;
1529 }
1530 if (!did_something || /* nothing to do */
1531 ++nloops == SOCKNAL_RESCHED) { /* hogging CPU? */
1532 spin_unlock_bh(&sched->kss_lock);
1533
1534 nloops = 0;
1535
1536 if (!did_something) { /* wait for something to do */
1537 cfs_wait_event_interruptible_exclusive(
1538 sched->kss_waitq,
1539 !ksocknal_sched_cansleep(sched), rc);
1540 LASSERT (rc == 0);
1541 } else {
1542 cond_resched();
1543 }
1544
1545 spin_lock_bh(&sched->kss_lock);
1546 }
1547 }
1548
1549 spin_unlock_bh(&sched->kss_lock);
1550 ksocknal_thread_fini();
1551 return 0;
1552}
1553
1554/*
1555 * Add connection to kss_rx_conns of scheduler
1556 * and wakeup the scheduler.
1557 */
1558void ksocknal_read_callback (ksock_conn_t *conn)
1559{
1560 ksock_sched_t *sched;
d7e09d03
PT
1561
1562 sched = conn->ksnc_scheduler;
1563
1564 spin_lock_bh(&sched->kss_lock);
1565
1566 conn->ksnc_rx_ready = 1;
1567
1568 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1569 list_add_tail(&conn->ksnc_rx_list,
1570 &sched->kss_rx_conns);
1571 conn->ksnc_rx_scheduled = 1;
1572 /* extra ref for scheduler */
1573 ksocknal_conn_addref(conn);
1574
1575 wake_up (&sched->kss_waitq);
1576 }
1577 spin_unlock_bh(&sched->kss_lock);
d7e09d03
PT
1578}
1579
1580/*
1581 * Add connection to kss_tx_conns of scheduler
1582 * and wakeup the scheduler.
1583 */
1584void ksocknal_write_callback (ksock_conn_t *conn)
1585{
1586 ksock_sched_t *sched;
d7e09d03
PT
1587
1588 sched = conn->ksnc_scheduler;
1589
1590 spin_lock_bh(&sched->kss_lock);
1591
1592 conn->ksnc_tx_ready = 1;
1593
1594 if (!conn->ksnc_tx_scheduled && // not being progressed
1595 !list_empty(&conn->ksnc_tx_queue)){//packets to send
1596 list_add_tail (&conn->ksnc_tx_list,
1597 &sched->kss_tx_conns);
1598 conn->ksnc_tx_scheduled = 1;
1599 /* extra ref for scheduler */
1600 ksocknal_conn_addref(conn);
1601
1602 wake_up (&sched->kss_waitq);
1603 }
1604
1605 spin_unlock_bh(&sched->kss_lock);
d7e09d03
PT
1606}
1607
1608ksock_proto_t *
1609ksocknal_parse_proto_version (ksock_hello_msg_t *hello)
1610{
1611 __u32 version = 0;
1612
1613 if (hello->kshm_magic == LNET_PROTO_MAGIC)
1614 version = hello->kshm_version;
1615 else if (hello->kshm_magic == __swab32(LNET_PROTO_MAGIC))
1616 version = __swab32(hello->kshm_version);
1617
1618 if (version != 0) {
1619#if SOCKNAL_VERSION_DEBUG
1620 if (*ksocknal_tunables.ksnd_protocol == 1)
1621 return NULL;
1622
1623 if (*ksocknal_tunables.ksnd_protocol == 2 &&
1624 version == KSOCK_PROTO_V3)
1625 return NULL;
1626#endif
1627 if (version == KSOCK_PROTO_V2)
1628 return &ksocknal_protocol_v2x;
1629
1630 if (version == KSOCK_PROTO_V3)
1631 return &ksocknal_protocol_v3x;
1632
1633 return NULL;
1634 }
1635
1636 if (hello->kshm_magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1637 lnet_magicversion_t *hmv = (lnet_magicversion_t *)hello;
1638
1639 CLASSERT (sizeof (lnet_magicversion_t) ==
1640 offsetof (ksock_hello_msg_t, kshm_src_nid));
1641
1642 if (hmv->version_major == cpu_to_le16 (KSOCK_PROTO_V1_MAJOR) &&
1643 hmv->version_minor == cpu_to_le16 (KSOCK_PROTO_V1_MINOR))
1644 return &ksocknal_protocol_v1x;
1645 }
1646
1647 return NULL;
1648}
1649
1650int
1651ksocknal_send_hello (lnet_ni_t *ni, ksock_conn_t *conn,
1652 lnet_nid_t peer_nid, ksock_hello_msg_t *hello)
1653{
1654 /* CAVEAT EMPTOR: this byte flips 'ipaddrs' */
1655 ksock_net_t *net = (ksock_net_t *)ni->ni_data;
1656
1657 LASSERT (hello->kshm_nips <= LNET_MAX_INTERFACES);
1658
1659 /* rely on caller to hold a ref on socket so it wouldn't disappear */
1660 LASSERT (conn->ksnc_proto != NULL);
1661
1662 hello->kshm_src_nid = ni->ni_nid;
1663 hello->kshm_dst_nid = peer_nid;
1664 hello->kshm_src_pid = the_lnet.ln_pid;
1665
1666 hello->kshm_src_incarnation = net->ksnn_incarnation;
1667 hello->kshm_ctype = conn->ksnc_type;
1668
1669 return conn->ksnc_proto->pro_send_hello(conn, hello);
1670}
1671
1672int
1673ksocknal_invert_type(int type)
1674{
1675 switch (type)
1676 {
1677 case SOCKLND_CONN_ANY:
1678 case SOCKLND_CONN_CONTROL:
1679 return (type);
1680 case SOCKLND_CONN_BULK_IN:
1681 return SOCKLND_CONN_BULK_OUT;
1682 case SOCKLND_CONN_BULK_OUT:
1683 return SOCKLND_CONN_BULK_IN;
1684 default:
1685 return (SOCKLND_CONN_NONE);
1686 }
1687}
1688
1689int
1690ksocknal_recv_hello (lnet_ni_t *ni, ksock_conn_t *conn,
1691 ksock_hello_msg_t *hello, lnet_process_id_t *peerid,
1692 __u64 *incarnation)
1693{
1694 /* Return < 0 fatal error
1695 * 0 success
1696 * EALREADY lost connection race
1697 * EPROTO protocol version mismatch
1698 */
1699 socket_t *sock = conn->ksnc_sock;
1700 int active = (conn->ksnc_proto != NULL);
1701 int timeout;
1702 int proto_match;
1703 int rc;
1704 ksock_proto_t *proto;
1705 lnet_process_id_t recv_id;
1706
1707 /* socket type set on active connections - not set on passive */
1708 LASSERT (!active == !(conn->ksnc_type != SOCKLND_CONN_NONE));
1709
1710 timeout = active ? *ksocknal_tunables.ksnd_timeout :
1711 lnet_acceptor_timeout();
1712
1713 rc = libcfs_sock_read(sock, &hello->kshm_magic, sizeof (hello->kshm_magic), timeout);
1714 if (rc != 0) {
5e8f6920
PT
1715 CERROR("Error %d reading HELLO from %pI4h\n",
1716 rc, &conn->ksnc_ipaddr);
d7e09d03
PT
1717 LASSERT (rc < 0);
1718 return rc;
1719 }
1720
1721 if (hello->kshm_magic != LNET_PROTO_MAGIC &&
1722 hello->kshm_magic != __swab32(LNET_PROTO_MAGIC) &&
1723 hello->kshm_magic != le32_to_cpu (LNET_PROTO_TCP_MAGIC)) {
1724 /* Unexpected magic! */
5e8f6920
PT
1725 CERROR("Bad magic(1) %#08x (%#08x expected) from "
1726 "%pI4h\n", __cpu_to_le32 (hello->kshm_magic),
d7e09d03 1727 LNET_PROTO_TCP_MAGIC,
5e8f6920 1728 &conn->ksnc_ipaddr);
d7e09d03
PT
1729 return -EPROTO;
1730 }
1731
1732 rc = libcfs_sock_read(sock, &hello->kshm_version,
1733 sizeof(hello->kshm_version), timeout);
1734 if (rc != 0) {
5e8f6920
PT
1735 CERROR("Error %d reading HELLO from %pI4h\n",
1736 rc, &conn->ksnc_ipaddr);
d7e09d03
PT
1737 LASSERT (rc < 0);
1738 return rc;
1739 }
1740
1741 proto = ksocknal_parse_proto_version(hello);
1742 if (proto == NULL) {
1743 if (!active) {
1744 /* unknown protocol from peer, tell peer my protocol */
1745 conn->ksnc_proto = &ksocknal_protocol_v3x;
1746#if SOCKNAL_VERSION_DEBUG
1747 if (*ksocknal_tunables.ksnd_protocol == 2)
1748 conn->ksnc_proto = &ksocknal_protocol_v2x;
1749 else if (*ksocknal_tunables.ksnd_protocol == 1)
1750 conn->ksnc_proto = &ksocknal_protocol_v1x;
1751#endif
1752 hello->kshm_nips = 0;
1753 ksocknal_send_hello(ni, conn, ni->ni_nid, hello);
1754 }
1755
5e8f6920
PT
1756 CERROR("Unknown protocol version (%d.x expected)"
1757 " from %pI4h\n",
d7e09d03 1758 conn->ksnc_proto->pro_version,
5e8f6920 1759 &conn->ksnc_ipaddr);
d7e09d03
PT
1760
1761 return -EPROTO;
1762 }
1763
1764 proto_match = (conn->ksnc_proto == proto);
1765 conn->ksnc_proto = proto;
1766
1767 /* receive the rest of hello message anyway */
1768 rc = conn->ksnc_proto->pro_recv_hello(conn, hello, timeout);
1769 if (rc != 0) {
5e8f6920
PT
1770 CERROR("Error %d reading or checking hello from from %pI4h\n",
1771 rc, &conn->ksnc_ipaddr);
d7e09d03
PT
1772 LASSERT (rc < 0);
1773 return rc;
1774 }
1775
1776 *incarnation = hello->kshm_src_incarnation;
1777
1778 if (hello->kshm_src_nid == LNET_NID_ANY) {
1779 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY"
5e8f6920 1780 "from %pI4h\n", &conn->ksnc_ipaddr);
d7e09d03
PT
1781 return -EPROTO;
1782 }
1783
1784 if (!active &&
1785 conn->ksnc_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
1786 /* Userspace NAL assigns peer process ID from socket */
1787 recv_id.pid = conn->ksnc_port | LNET_PID_USERFLAG;
1788 recv_id.nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), conn->ksnc_ipaddr);
1789 } else {
1790 recv_id.nid = hello->kshm_src_nid;
1791 recv_id.pid = hello->kshm_src_pid;
1792 }
1793
1794 if (!active) {
1795 *peerid = recv_id;
1796
1797 /* peer determines type */
1798 conn->ksnc_type = ksocknal_invert_type(hello->kshm_ctype);
1799 if (conn->ksnc_type == SOCKLND_CONN_NONE) {
5e8f6920 1800 CERROR("Unexpected type %d from %s ip %pI4h\n",
d7e09d03 1801 hello->kshm_ctype, libcfs_id2str(*peerid),
5e8f6920 1802 &conn->ksnc_ipaddr);
d7e09d03
PT
1803 return -EPROTO;
1804 }
1805
1806 return 0;
1807 }
1808
1809 if (peerid->pid != recv_id.pid ||
1810 peerid->nid != recv_id.nid) {
1811 LCONSOLE_ERROR_MSG(0x130, "Connected successfully to %s on host"
5e8f6920 1812 " %pI4h, but they claimed they were "
d7e09d03
PT
1813 "%s; please check your Lustre "
1814 "configuration.\n",
1815 libcfs_id2str(*peerid),
5e8f6920 1816 &conn->ksnc_ipaddr,
d7e09d03
PT
1817 libcfs_id2str(recv_id));
1818 return -EPROTO;
1819 }
1820
1821 if (hello->kshm_ctype == SOCKLND_CONN_NONE) {
1822 /* Possible protocol mismatch or I lost the connection race */
1823 return proto_match ? EALREADY : EPROTO;
1824 }
1825
1826 if (ksocknal_invert_type(hello->kshm_ctype) != conn->ksnc_type) {
5e8f6920 1827 CERROR("Mismatched types: me %d, %s ip %pI4h %d\n",
d7e09d03 1828 conn->ksnc_type, libcfs_id2str(*peerid),
5e8f6920 1829 &conn->ksnc_ipaddr,
d7e09d03
PT
1830 hello->kshm_ctype);
1831 return -EPROTO;
1832 }
1833
1834 return 0;
1835}
1836
1837int
1838ksocknal_connect (ksock_route_t *route)
1839{
1840 LIST_HEAD (zombies);
1841 ksock_peer_t *peer = route->ksnr_peer;
1842 int type;
1843 int wanted;
1844 socket_t *sock;
1845 cfs_time_t deadline;
1846 int retry_later = 0;
1847 int rc = 0;
1848
1849 deadline = cfs_time_add(cfs_time_current(),
1850 cfs_time_seconds(*ksocknal_tunables.ksnd_timeout));
1851
1852 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1853
1854 LASSERT (route->ksnr_scheduled);
1855 LASSERT (!route->ksnr_connecting);
1856
1857 route->ksnr_connecting = 1;
1858
1859 for (;;) {
1860 wanted = ksocknal_route_mask() & ~route->ksnr_connected;
1861
1862 /* stop connecting if peer/route got closed under me, or
1863 * route got connected while queued */
1864 if (peer->ksnp_closing || route->ksnr_deleted ||
1865 wanted == 0) {
1866 retry_later = 0;
1867 break;
1868 }
1869
1870 /* reschedule if peer is connecting to me */
1871 if (peer->ksnp_accepting > 0) {
1872 CDEBUG(D_NET,
1873 "peer %s(%d) already connecting to me, retry later.\n",
1874 libcfs_nid2str(peer->ksnp_id.nid), peer->ksnp_accepting);
1875 retry_later = 1;
1876 }
1877
1878 if (retry_later) /* needs reschedule */
1879 break;
1880
1881 if ((wanted & (1 << SOCKLND_CONN_ANY)) != 0) {
1882 type = SOCKLND_CONN_ANY;
1883 } else if ((wanted & (1 << SOCKLND_CONN_CONTROL)) != 0) {
1884 type = SOCKLND_CONN_CONTROL;
1885 } else if ((wanted & (1 << SOCKLND_CONN_BULK_IN)) != 0) {
1886 type = SOCKLND_CONN_BULK_IN;
1887 } else {
1888 LASSERT ((wanted & (1 << SOCKLND_CONN_BULK_OUT)) != 0);
1889 type = SOCKLND_CONN_BULK_OUT;
1890 }
1891
1892 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1893
1894 if (cfs_time_aftereq(cfs_time_current(), deadline)) {
1895 rc = -ETIMEDOUT;
1896 lnet_connect_console_error(rc, peer->ksnp_id.nid,
1897 route->ksnr_ipaddr,
1898 route->ksnr_port);
1899 goto failed;
1900 }
1901
1902 rc = lnet_connect(&sock, peer->ksnp_id.nid,
1903 route->ksnr_myipaddr,
1904 route->ksnr_ipaddr, route->ksnr_port);
1905 if (rc != 0)
1906 goto failed;
1907
1908 rc = ksocknal_create_conn(peer->ksnp_ni, route, sock, type);
1909 if (rc < 0) {
1910 lnet_connect_console_error(rc, peer->ksnp_id.nid,
1911 route->ksnr_ipaddr,
1912 route->ksnr_port);
1913 goto failed;
1914 }
1915
1916 /* A +ve RC means I have to retry because I lost the connection
1917 * race or I have to renegotiate protocol version */
1918 retry_later = (rc != 0);
1919 if (retry_later)
1920 CDEBUG(D_NET, "peer %s: conn race, retry later.\n",
1921 libcfs_nid2str(peer->ksnp_id.nid));
1922
1923 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1924 }
1925
1926 route->ksnr_scheduled = 0;
1927 route->ksnr_connecting = 0;
1928
1929 if (retry_later) {
1930 /* re-queue for attention; this frees me up to handle
1931 * the peer's incoming connection request */
1932
1933 if (rc == EALREADY ||
1934 (rc == 0 && peer->ksnp_accepting > 0)) {
1935 /* We want to introduce a delay before next
1936 * attempt to connect if we lost conn race,
1937 * but the race is resolved quickly usually,
1938 * so min_reconnectms should be good heuristic */
1939 route->ksnr_retry_interval =
1940 cfs_time_seconds(*ksocknal_tunables.ksnd_min_reconnectms)/1000;
1941 route->ksnr_timeout = cfs_time_add(cfs_time_current(),
1942 route->ksnr_retry_interval);
1943 }
1944
1945 ksocknal_launch_connection_locked(route);
1946 }
1947
1948 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1949 return retry_later;
1950
1951 failed:
1952 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1953
1954 route->ksnr_scheduled = 0;
1955 route->ksnr_connecting = 0;
1956
1957 /* This is a retry rather than a new connection */
1958 route->ksnr_retry_interval *= 2;
1959 route->ksnr_retry_interval =
1960 MAX(route->ksnr_retry_interval,
1961 cfs_time_seconds(*ksocknal_tunables.ksnd_min_reconnectms)/1000);
1962 route->ksnr_retry_interval =
1963 MIN(route->ksnr_retry_interval,
1964 cfs_time_seconds(*ksocknal_tunables.ksnd_max_reconnectms)/1000);
1965
1966 LASSERT (route->ksnr_retry_interval != 0);
1967 route->ksnr_timeout = cfs_time_add(cfs_time_current(),
1968 route->ksnr_retry_interval);
1969
1970 if (!list_empty(&peer->ksnp_tx_queue) &&
1971 peer->ksnp_accepting == 0 &&
1972 ksocknal_find_connecting_route_locked(peer) == NULL) {
1973 ksock_conn_t *conn;
1974
1975 /* ksnp_tx_queue is queued on a conn on successful
1976 * connection for V1.x and V2.x */
1977 if (!list_empty (&peer->ksnp_conns)) {
1978 conn = list_entry(peer->ksnp_conns.next,
1979 ksock_conn_t, ksnc_list);
1980 LASSERT (conn->ksnc_proto == &ksocknal_protocol_v3x);
1981 }
1982
1983 /* take all the blocked packets while I've got the lock and
1984 * complete below... */
1985 list_splice_init(&peer->ksnp_tx_queue, &zombies);
1986 }
1987
1988#if 0 /* irrelevent with only eager routes */
1989 if (!route->ksnr_deleted) {
1990 /* make this route least-favourite for re-selection */
1991 list_del(&route->ksnr_list);
1992 list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
1993 }
1994#endif
1995 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1996
1997 ksocknal_peer_failed(peer);
1998 ksocknal_txlist_done(peer->ksnp_ni, &zombies, 1);
1999 return 0;
2000}
2001
2002/*
2003 * check whether we need to create more connds.
2004 * It will try to create new thread if it's necessary, @timeout can
2005 * be updated if failed to create, so caller wouldn't keep try while
2006 * running out of resource.
2007 */
2008static int
2009ksocknal_connd_check_start(long sec, long *timeout)
2010{
2011 char name[16];
2012 int rc;
2013 int total = ksocknal_data.ksnd_connd_starting +
2014 ksocknal_data.ksnd_connd_running;
2015
2016 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2017 /* still in initializing */
2018 return 0;
2019 }
2020
2021 if (total >= *ksocknal_tunables.ksnd_nconnds_max ||
2022 total > ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV) {
2023 /* can't create more connd, or still have enough
2024 * threads to handle more connecting */
2025 return 0;
2026 }
2027
2028 if (list_empty(&ksocknal_data.ksnd_connd_routes)) {
2029 /* no pending connecting request */
2030 return 0;
2031 }
2032
2033 if (sec - ksocknal_data.ksnd_connd_failed_stamp <= 1) {
2034 /* may run out of resource, retry later */
2035 *timeout = cfs_time_seconds(1);
2036 return 0;
2037 }
2038
2039 if (ksocknal_data.ksnd_connd_starting > 0) {
2040 /* serialize starting to avoid flood */
2041 return 0;
2042 }
2043
2044 ksocknal_data.ksnd_connd_starting_stamp = sec;
2045 ksocknal_data.ksnd_connd_starting++;
2046 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2047
2048 /* NB: total is the next id */
2049 snprintf(name, sizeof(name), "socknal_cd%02d", total);
2050 rc = ksocknal_thread_start(ksocknal_connd, NULL, name);
2051
2052 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2053 if (rc == 0)
2054 return 1;
2055
2056 /* we tried ... */
2057 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2058 ksocknal_data.ksnd_connd_starting--;
2059 ksocknal_data.ksnd_connd_failed_stamp = cfs_time_current_sec();
2060
2061 return 1;
2062}
2063
2064/*
2065 * check whether current thread can exit, it will return 1 if there are too
2066 * many threads and no creating in past 120 seconds.
2067 * Also, this function may update @timeout to make caller come back
2068 * again to recheck these conditions.
2069 */
2070static int
2071ksocknal_connd_check_stop(long sec, long *timeout)
2072{
2073 int val;
2074
2075 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2076 /* still in initializing */
2077 return 0;
2078 }
2079
2080 if (ksocknal_data.ksnd_connd_starting > 0) {
2081 /* in progress of starting new thread */
2082 return 0;
2083 }
2084
2085 if (ksocknal_data.ksnd_connd_running <=
2086 *ksocknal_tunables.ksnd_nconnds) { /* can't shrink */
2087 return 0;
2088 }
2089
2090 /* created thread in past 120 seconds? */
2091 val = (int)(ksocknal_data.ksnd_connd_starting_stamp +
2092 SOCKNAL_CONND_TIMEOUT - sec);
2093
2094 *timeout = (val > 0) ? cfs_time_seconds(val) :
2095 cfs_time_seconds(SOCKNAL_CONND_TIMEOUT);
2096 if (val > 0)
2097 return 0;
2098
2099 /* no creating in past 120 seconds */
2100
2101 return ksocknal_data.ksnd_connd_running >
2102 ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV;
2103}
2104
2105/* Go through connd_routes queue looking for a route that we can process
2106 * right now, @timeout_p can be updated if we need to come back later */
2107static ksock_route_t *
2108ksocknal_connd_get_route_locked(signed long *timeout_p)
2109{
2110 ksock_route_t *route;
2111 cfs_time_t now;
2112
2113 now = cfs_time_current();
2114
2115 /* connd_routes can contain both pending and ordinary routes */
2116 list_for_each_entry (route, &ksocknal_data.ksnd_connd_routes,
2117 ksnr_connd_list) {
2118
2119 if (route->ksnr_retry_interval == 0 ||
2120 cfs_time_aftereq(now, route->ksnr_timeout))
2121 return route;
2122
2123 if (*timeout_p == MAX_SCHEDULE_TIMEOUT ||
2124 (int)*timeout_p > (int)(route->ksnr_timeout - now))
2125 *timeout_p = (int)(route->ksnr_timeout - now);
2126 }
2127
2128 return NULL;
2129}
2130
2131int
2132ksocknal_connd (void *arg)
2133{
2134 spinlock_t *connd_lock = &ksocknal_data.ksnd_connd_lock;
2135 ksock_connreq_t *cr;
2136 wait_queue_t wait;
2137 int nloops = 0;
2138 int cons_retry = 0;
2139
2140 cfs_block_allsigs ();
2141
2142 init_waitqueue_entry_current (&wait);
2143
2144 spin_lock_bh(connd_lock);
2145
2146 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2147 ksocknal_data.ksnd_connd_starting--;
2148 ksocknal_data.ksnd_connd_running++;
2149
2150 while (!ksocknal_data.ksnd_shuttingdown) {
2151 ksock_route_t *route = NULL;
2152 long sec = cfs_time_current_sec();
2153 long timeout = MAX_SCHEDULE_TIMEOUT;
2154 int dropped_lock = 0;
2155
2156 if (ksocknal_connd_check_stop(sec, &timeout)) {
2157 /* wakeup another one to check stop */
2158 wake_up(&ksocknal_data.ksnd_connd_waitq);
2159 break;
2160 }
2161
2162 if (ksocknal_connd_check_start(sec, &timeout)) {
2163 /* created new thread */
2164 dropped_lock = 1;
2165 }
2166
2167 if (!list_empty(&ksocknal_data.ksnd_connd_connreqs)) {
2168 /* Connection accepted by the listener */
2169 cr = list_entry(ksocknal_data.ksnd_connd_connreqs. \
2170 next, ksock_connreq_t, ksncr_list);
2171
2172 list_del(&cr->ksncr_list);
2173 spin_unlock_bh(connd_lock);
2174 dropped_lock = 1;
2175
2176 ksocknal_create_conn(cr->ksncr_ni, NULL,
2177 cr->ksncr_sock, SOCKLND_CONN_NONE);
2178 lnet_ni_decref(cr->ksncr_ni);
2179 LIBCFS_FREE(cr, sizeof(*cr));
2180
2181 spin_lock_bh(connd_lock);
2182 }
2183
2184 /* Only handle an outgoing connection request if there
2185 * is a thread left to handle incoming connections and
2186 * create new connd */
2187 if (ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV <
2188 ksocknal_data.ksnd_connd_running) {
2189 route = ksocknal_connd_get_route_locked(&timeout);
2190 }
2191 if (route != NULL) {
2192 list_del (&route->ksnr_connd_list);
2193 ksocknal_data.ksnd_connd_connecting++;
2194 spin_unlock_bh(connd_lock);
2195 dropped_lock = 1;
2196
2197 if (ksocknal_connect(route)) {
2198 /* consecutive retry */
2199 if (cons_retry++ > SOCKNAL_INSANITY_RECONN) {
2200 CWARN("massive consecutive "
5e8f6920
PT
2201 "re-connecting to %pI4h\n",
2202 &route->ksnr_ipaddr);
d7e09d03
PT
2203 cons_retry = 0;
2204 }
2205 } else {
2206 cons_retry = 0;
2207 }
2208
2209 ksocknal_route_decref(route);
2210
2211 spin_lock_bh(connd_lock);
2212 ksocknal_data.ksnd_connd_connecting--;
2213 }
2214
2215 if (dropped_lock) {
2216 if (++nloops < SOCKNAL_RESCHED)
2217 continue;
2218 spin_unlock_bh(connd_lock);
2219 nloops = 0;
2220 cond_resched();
2221 spin_lock_bh(connd_lock);
2222 continue;
2223 }
2224
2225 /* Nothing to do for 'timeout' */
2226 set_current_state(TASK_INTERRUPTIBLE);
2227 add_wait_queue_exclusive(&ksocknal_data.ksnd_connd_waitq, &wait);
2228 spin_unlock_bh(connd_lock);
2229
2230 nloops = 0;
2231 waitq_timedwait(&wait, TASK_INTERRUPTIBLE, timeout);
2232
2233 set_current_state(TASK_RUNNING);
2234 remove_wait_queue(&ksocknal_data.ksnd_connd_waitq, &wait);
2235 spin_lock_bh(connd_lock);
2236 }
2237 ksocknal_data.ksnd_connd_running--;
2238 spin_unlock_bh(connd_lock);
2239
2240 ksocknal_thread_fini();
2241 return 0;
2242}
2243
2244ksock_conn_t *
2245ksocknal_find_timed_out_conn (ksock_peer_t *peer)
2246{
2247 /* We're called with a shared lock on ksnd_global_lock */
2248 ksock_conn_t *conn;
2249 struct list_head *ctmp;
2250
2251 list_for_each (ctmp, &peer->ksnp_conns) {
2252 int error;
2253 conn = list_entry (ctmp, ksock_conn_t, ksnc_list);
2254
2255 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2256 LASSERT (!conn->ksnc_closing);
2257
2258 /* SOCK_ERROR will reset error code of socket in
2259 * some platform (like Darwin8.x) */
2260 error = cfs_sock_error(conn->ksnc_sock);
2261 if (error != 0) {
2262 ksocknal_conn_addref(conn);
2263
2264 switch (error) {
2265 case ECONNRESET:
2266 CNETERR("A connection with %s "
5e8f6920 2267 "(%pI4h:%d) was reset; "
d7e09d03
PT
2268 "it may have rebooted.\n",
2269 libcfs_id2str(peer->ksnp_id),
5e8f6920 2270 &conn->ksnc_ipaddr,
d7e09d03
PT
2271 conn->ksnc_port);
2272 break;
2273 case ETIMEDOUT:
2274 CNETERR("A connection with %s "
5e8f6920 2275 "(%pI4h:%d) timed out; the "
d7e09d03
PT
2276 "network or node may be down.\n",
2277 libcfs_id2str(peer->ksnp_id),
5e8f6920 2278 &conn->ksnc_ipaddr,
d7e09d03
PT
2279 conn->ksnc_port);
2280 break;
2281 default:
2282 CNETERR("An unexpected network error %d "
2283 "occurred with %s "
5e8f6920 2284 "(%pI4h:%d\n", error,
d7e09d03 2285 libcfs_id2str(peer->ksnp_id),
5e8f6920 2286 &conn->ksnc_ipaddr,
d7e09d03
PT
2287 conn->ksnc_port);
2288 break;
2289 }
2290
2291 return (conn);
2292 }
2293
2294 if (conn->ksnc_rx_started &&
2295 cfs_time_aftereq(cfs_time_current(),
2296 conn->ksnc_rx_deadline)) {
2297 /* Timed out incomplete incoming message */
2298 ksocknal_conn_addref(conn);
5e8f6920 2299 CNETERR("Timeout receiving from %s (%pI4h:%d), "
d7e09d03
PT
2300 "state %d wanted %d left %d\n",
2301 libcfs_id2str(peer->ksnp_id),
5e8f6920 2302 &conn->ksnc_ipaddr,
d7e09d03
PT
2303 conn->ksnc_port,
2304 conn->ksnc_rx_state,
2305 conn->ksnc_rx_nob_wanted,
2306 conn->ksnc_rx_nob_left);
2307 return (conn);
2308 }
2309
2310 if ((!list_empty(&conn->ksnc_tx_queue) ||
2311 cfs_sock_wmem_queued(conn->ksnc_sock) != 0) &&
2312 cfs_time_aftereq(cfs_time_current(),
2313 conn->ksnc_tx_deadline)) {
2314 /* Timed out messages queued for sending or
2315 * buffered in the socket's send buffer */
2316 ksocknal_conn_addref(conn);
5e8f6920 2317 CNETERR("Timeout sending data to %s (%pI4h:%d) "
d7e09d03
PT
2318 "the network or that node may be down.\n",
2319 libcfs_id2str(peer->ksnp_id),
5e8f6920 2320 &conn->ksnc_ipaddr,
d7e09d03
PT
2321 conn->ksnc_port);
2322 return (conn);
2323 }
2324 }
2325
2326 return (NULL);
2327}
2328
2329static inline void
2330ksocknal_flush_stale_txs(ksock_peer_t *peer)
2331{
2332 ksock_tx_t *tx;
2333 LIST_HEAD (stale_txs);
2334
2335 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2336
2337 while (!list_empty (&peer->ksnp_tx_queue)) {
2338 tx = list_entry (peer->ksnp_tx_queue.next,
2339 ksock_tx_t, tx_list);
2340
2341 if (!cfs_time_aftereq(cfs_time_current(),
2342 tx->tx_deadline))
2343 break;
2344
2345 list_del (&tx->tx_list);
2346 list_add_tail (&tx->tx_list, &stale_txs);
2347 }
2348
2349 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2350
2351 ksocknal_txlist_done(peer->ksnp_ni, &stale_txs, 1);
2352}
2353
2354int
2355ksocknal_send_keepalive_locked(ksock_peer_t *peer)
2356{
2357 ksock_sched_t *sched;
2358 ksock_conn_t *conn;
2359 ksock_tx_t *tx;
2360
2361 if (list_empty(&peer->ksnp_conns)) /* last_alive will be updated by create_conn */
2362 return 0;
2363
2364 if (peer->ksnp_proto != &ksocknal_protocol_v3x)
2365 return 0;
2366
2367 if (*ksocknal_tunables.ksnd_keepalive <= 0 ||
2368 cfs_time_before(cfs_time_current(),
2369 cfs_time_add(peer->ksnp_last_alive,
2370 cfs_time_seconds(*ksocknal_tunables.ksnd_keepalive))))
2371 return 0;
2372
2373 if (cfs_time_before(cfs_time_current(),
2374 peer->ksnp_send_keepalive))
2375 return 0;
2376
2377 /* retry 10 secs later, so we wouldn't put pressure
2378 * on this peer if we failed to send keepalive this time */
2379 peer->ksnp_send_keepalive = cfs_time_shift(10);
2380
2381 conn = ksocknal_find_conn_locked(peer, NULL, 1);
2382 if (conn != NULL) {
2383 sched = conn->ksnc_scheduler;
2384
2385 spin_lock_bh(&sched->kss_lock);
2386 if (!list_empty(&conn->ksnc_tx_queue)) {
2387 spin_unlock_bh(&sched->kss_lock);
2388 /* there is an queued ACK, don't need keepalive */
2389 return 0;
2390 }
2391
2392 spin_unlock_bh(&sched->kss_lock);
2393 }
2394
2395 read_unlock(&ksocknal_data.ksnd_global_lock);
2396
2397 /* cookie = 1 is reserved for keepalive PING */
2398 tx = ksocknal_alloc_tx_noop(1, 1);
2399 if (tx == NULL) {
2400 read_lock(&ksocknal_data.ksnd_global_lock);
2401 return -ENOMEM;
2402 }
2403
2404 if (ksocknal_launch_packet(peer->ksnp_ni, tx, peer->ksnp_id) == 0) {
2405 read_lock(&ksocknal_data.ksnd_global_lock);
2406 return 1;
2407 }
2408
2409 ksocknal_free_tx(tx);
2410 read_lock(&ksocknal_data.ksnd_global_lock);
2411
2412 return -EIO;
2413}
2414
2415
2416void
2417ksocknal_check_peer_timeouts (int idx)
2418{
2419 struct list_head *peers = &ksocknal_data.ksnd_peers[idx];
2420 ksock_peer_t *peer;
2421 ksock_conn_t *conn;
2422 ksock_tx_t *tx;
2423
2424 again:
2425 /* NB. We expect to have a look at all the peers and not find any
2426 * connections to time out, so we just use a shared lock while we
2427 * take a look... */
2428 read_lock(&ksocknal_data.ksnd_global_lock);
2429
2430 list_for_each_entry(peer, peers, ksnp_list) {
2431 cfs_time_t deadline = 0;
2432 int resid = 0;
2433 int n = 0;
2434
2435 if (ksocknal_send_keepalive_locked(peer) != 0) {
2436 read_unlock(&ksocknal_data.ksnd_global_lock);
2437 goto again;
2438 }
2439
2440 conn = ksocknal_find_timed_out_conn (peer);
2441
2442 if (conn != NULL) {
2443 read_unlock(&ksocknal_data.ksnd_global_lock);
2444
2445 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2446
2447 /* NB we won't find this one again, but we can't
2448 * just proceed with the next peer, since we dropped
2449 * ksnd_global_lock and it might be dead already! */
2450 ksocknal_conn_decref(conn);
2451 goto again;
2452 }
2453
2454 /* we can't process stale txs right here because we're
2455 * holding only shared lock */
2456 if (!list_empty (&peer->ksnp_tx_queue)) {
2457 ksock_tx_t *tx =
2458 list_entry (peer->ksnp_tx_queue.next,
2459 ksock_tx_t, tx_list);
2460
2461 if (cfs_time_aftereq(cfs_time_current(),
2462 tx->tx_deadline)) {
2463
2464 ksocknal_peer_addref(peer);
2465 read_unlock(&ksocknal_data.ksnd_global_lock);
2466
2467 ksocknal_flush_stale_txs(peer);
2468
2469 ksocknal_peer_decref(peer);
2470 goto again;
2471 }
2472 }
2473
2474 if (list_empty(&peer->ksnp_zc_req_list))
2475 continue;
2476
2477 spin_lock(&peer->ksnp_lock);
2478 list_for_each_entry(tx, &peer->ksnp_zc_req_list, tx_zc_list) {
2479 if (!cfs_time_aftereq(cfs_time_current(),
2480 tx->tx_deadline))
2481 break;
2482 /* ignore the TX if connection is being closed */
2483 if (tx->tx_conn->ksnc_closing)
2484 continue;
2485 n++;
2486 }
2487
2488 if (n == 0) {
2489 spin_unlock(&peer->ksnp_lock);
2490 continue;
2491 }
2492
2493 tx = list_entry(peer->ksnp_zc_req_list.next,
2494 ksock_tx_t, tx_zc_list);
2495 deadline = tx->tx_deadline;
2496 resid = tx->tx_resid;
2497 conn = tx->tx_conn;
2498 ksocknal_conn_addref(conn);
2499
2500 spin_unlock(&peer->ksnp_lock);
2501 read_unlock(&ksocknal_data.ksnd_global_lock);
2502
2503 CERROR("Total %d stale ZC_REQs for peer %s detected; the "
2504 "oldest(%p) timed out %ld secs ago, "
2505 "resid: %d, wmem: %d\n",
2506 n, libcfs_nid2str(peer->ksnp_id.nid), tx,
2507 cfs_duration_sec(cfs_time_current() - deadline),
2508 resid, cfs_sock_wmem_queued(conn->ksnc_sock));
2509
2510 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2511 ksocknal_conn_decref(conn);
2512 goto again;
2513 }
2514
2515 read_unlock(&ksocknal_data.ksnd_global_lock);
2516}
2517
2518int
2519ksocknal_reaper (void *arg)
2520{
2521 wait_queue_t wait;
2522 ksock_conn_t *conn;
2523 ksock_sched_t *sched;
2524 struct list_head enomem_conns;
2525 int nenomem_conns;
2526 cfs_duration_t timeout;
2527 int i;
2528 int peer_index = 0;
2529 cfs_time_t deadline = cfs_time_current();
2530
2531 cfs_block_allsigs ();
2532
2533 INIT_LIST_HEAD(&enomem_conns);
2534 init_waitqueue_entry_current (&wait);
2535
2536 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2537
2538 while (!ksocknal_data.ksnd_shuttingdown) {
2539
2540 if (!list_empty (&ksocknal_data.ksnd_deathrow_conns)) {
2541 conn = list_entry (ksocknal_data. \
2542 ksnd_deathrow_conns.next,
2543 ksock_conn_t, ksnc_list);
2544 list_del (&conn->ksnc_list);
2545
2546 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2547
2548 ksocknal_terminate_conn(conn);
2549 ksocknal_conn_decref(conn);
2550
2551 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2552 continue;
2553 }
2554
2555 if (!list_empty (&ksocknal_data.ksnd_zombie_conns)) {
2556 conn = list_entry (ksocknal_data.ksnd_zombie_conns.\
2557 next, ksock_conn_t, ksnc_list);
2558 list_del (&conn->ksnc_list);
2559
2560 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2561
2562 ksocknal_destroy_conn(conn);
2563
2564 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2565 continue;
2566 }
2567
2568 if (!list_empty (&ksocknal_data.ksnd_enomem_conns)) {
2569 list_add(&enomem_conns,
2570 &ksocknal_data.ksnd_enomem_conns);
2571 list_del_init(&ksocknal_data.ksnd_enomem_conns);
2572 }
2573
2574 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2575
2576 /* reschedule all the connections that stalled with ENOMEM... */
2577 nenomem_conns = 0;
2578 while (!list_empty (&enomem_conns)) {
2579 conn = list_entry (enomem_conns.next,
2580 ksock_conn_t, ksnc_tx_list);
2581 list_del (&conn->ksnc_tx_list);
2582
2583 sched = conn->ksnc_scheduler;
2584
2585 spin_lock_bh(&sched->kss_lock);
2586
2587 LASSERT(conn->ksnc_tx_scheduled);
2588 conn->ksnc_tx_ready = 1;
2589 list_add_tail(&conn->ksnc_tx_list,
2590 &sched->kss_tx_conns);
2591 wake_up(&sched->kss_waitq);
2592
2593 spin_unlock_bh(&sched->kss_lock);
2594 nenomem_conns++;
2595 }
2596
2597 /* careful with the jiffy wrap... */
2598 while ((timeout = cfs_time_sub(deadline,
2599 cfs_time_current())) <= 0) {
2600 const int n = 4;
2601 const int p = 1;
2602 int chunk = ksocknal_data.ksnd_peer_hash_size;
2603
2604 /* Time to check for timeouts on a few more peers: I do
2605 * checks every 'p' seconds on a proportion of the peer
2606 * table and I need to check every connection 'n' times
2607 * within a timeout interval, to ensure I detect a
2608 * timeout on any connection within (n+1)/n times the
2609 * timeout interval. */
2610
2611 if (*ksocknal_tunables.ksnd_timeout > n * p)
2612 chunk = (chunk * n * p) /
2613 *ksocknal_tunables.ksnd_timeout;
2614 if (chunk == 0)
2615 chunk = 1;
2616
2617 for (i = 0; i < chunk; i++) {
2618 ksocknal_check_peer_timeouts (peer_index);
2619 peer_index = (peer_index + 1) %
2620 ksocknal_data.ksnd_peer_hash_size;
2621 }
2622
2623 deadline = cfs_time_add(deadline, cfs_time_seconds(p));
2624 }
2625
2626 if (nenomem_conns != 0) {
2627 /* Reduce my timeout if I rescheduled ENOMEM conns.
2628 * This also prevents me getting woken immediately
2629 * if any go back on my enomem list. */
2630 timeout = SOCKNAL_ENOMEM_RETRY;
2631 }
2632 ksocknal_data.ksnd_reaper_waketime =
2633 cfs_time_add(cfs_time_current(), timeout);
2634
2635 set_current_state (TASK_INTERRUPTIBLE);
2636 add_wait_queue (&ksocknal_data.ksnd_reaper_waitq, &wait);
2637
2638 if (!ksocknal_data.ksnd_shuttingdown &&
2639 list_empty (&ksocknal_data.ksnd_deathrow_conns) &&
2640 list_empty (&ksocknal_data.ksnd_zombie_conns))
2641 waitq_timedwait (&wait, TASK_INTERRUPTIBLE,
2642 timeout);
2643
2644 set_current_state (TASK_RUNNING);
2645 remove_wait_queue (&ksocknal_data.ksnd_reaper_waitq, &wait);
2646
2647 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2648 }
2649
2650 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2651
2652 ksocknal_thread_fini();
2653 return 0;
2654}
This page took 0.284936 seconds and 5 git commands to generate.