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