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