2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, 2012, Intel Corporation.
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>
11 * This file is part of Portals, http://www.sf.net/projects/sandiaportals/
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.
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.
27 ksocknal_alloc_tx(int type, int size)
29 struct ksock_tx *tx = NULL;
31 if (type == KSOCK_MSG_NOOP) {
32 LASSERT(size == KSOCK_NOOP_TX_SIZE);
34 /* searching for a noop tx in free list */
35 spin_lock(&ksocknal_data.ksnd_tx_lock);
37 if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
38 tx = list_entry(ksocknal_data.ksnd_idle_noop_txs.next,
39 struct ksock_tx, tx_list);
40 LASSERT(tx->tx_desc_size == size);
41 list_del(&tx->tx_list);
44 spin_unlock(&ksocknal_data.ksnd_tx_lock);
48 LIBCFS_ALLOC(tx, size);
53 atomic_set(&tx->tx_refcount, 1);
54 tx->tx_zc_aborted = 0;
55 tx->tx_zc_capable = 0;
56 tx->tx_zc_checked = 0;
57 tx->tx_desc_size = size;
59 atomic_inc(&ksocknal_data.ksnd_nactive_txs);
65 ksocknal_alloc_tx_noop(__u64 cookie, int nonblk)
69 tx = ksocknal_alloc_tx(KSOCK_MSG_NOOP, KSOCK_NOOP_TX_SIZE);
71 CERROR("Can't allocate noop tx desc\n");
76 tx->tx_lnetmsg = NULL;
79 tx->tx_iov = tx->tx_frags.virt.iov;
81 tx->tx_nonblk = nonblk;
83 tx->tx_msg.ksm_csum = 0;
84 tx->tx_msg.ksm_type = KSOCK_MSG_NOOP;
85 tx->tx_msg.ksm_zc_cookies[0] = 0;
86 tx->tx_msg.ksm_zc_cookies[1] = cookie;
92 ksocknal_free_tx(struct ksock_tx *tx)
94 atomic_dec(&ksocknal_data.ksnd_nactive_txs);
96 if (!tx->tx_lnetmsg && tx->tx_desc_size == KSOCK_NOOP_TX_SIZE) {
98 spin_lock(&ksocknal_data.ksnd_tx_lock);
100 list_add(&tx->tx_list, &ksocknal_data.ksnd_idle_noop_txs);
102 spin_unlock(&ksocknal_data.ksnd_tx_lock);
104 LIBCFS_FREE(tx, tx->tx_desc_size);
109 ksocknal_send_iov(struct ksock_conn *conn, struct ksock_tx *tx)
111 struct kvec *iov = tx->tx_iov;
115 LASSERT(tx->tx_niov > 0);
117 /* Never touch tx->tx_iov inside ksocknal_lib_send_iov() */
118 rc = ksocknal_lib_send_iov(conn, tx);
120 if (rc <= 0) /* sent nothing? */
124 LASSERT(nob <= tx->tx_resid);
129 LASSERT(tx->tx_niov > 0);
131 if (nob < (int)iov->iov_len) {
132 iov->iov_base = (void *)((char *)iov->iov_base + nob);
146 ksocknal_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx)
148 struct bio_vec *kiov = tx->tx_kiov;
152 LASSERT(!tx->tx_niov);
153 LASSERT(tx->tx_nkiov > 0);
155 /* Never touch tx->tx_kiov inside ksocknal_lib_send_kiov() */
156 rc = ksocknal_lib_send_kiov(conn, tx);
158 if (rc <= 0) /* sent nothing? */
162 LASSERT(nob <= tx->tx_resid);
167 LASSERT(tx->tx_nkiov > 0);
169 if (nob < (int)kiov->bv_len) {
170 kiov->bv_offset += nob;
175 nob -= (int)kiov->bv_len;
176 tx->tx_kiov = ++kiov;
184 ksocknal_transmit(struct ksock_conn *conn, struct ksock_tx *tx)
189 if (ksocknal_data.ksnd_stall_tx) {
190 set_current_state(TASK_UNINTERRUPTIBLE);
191 schedule_timeout(cfs_time_seconds(ksocknal_data.ksnd_stall_tx));
194 LASSERT(tx->tx_resid);
196 rc = ksocknal_connsock_addref(conn);
198 LASSERT(conn->ksnc_closing);
203 if (ksocknal_data.ksnd_enomem_tx > 0) {
205 ksocknal_data.ksnd_enomem_tx--;
207 } else if (tx->tx_niov) {
208 rc = ksocknal_send_iov(conn, tx);
210 rc = ksocknal_send_kiov(conn, tx);
213 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
214 if (rc > 0) /* sent something? */
215 conn->ksnc_tx_bufnob += rc; /* account it */
217 if (bufnob < conn->ksnc_tx_bufnob) {
219 * allocated send buffer bytes < computed; infer
220 * something got ACKed
222 conn->ksnc_tx_deadline =
223 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
224 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
225 conn->ksnc_tx_bufnob = bufnob;
229 if (rc <= 0) { /* Didn't write anything? */
231 if (!rc) /* some stacks return 0 instead of -EAGAIN */
234 /* Check if EAGAIN is due to memory pressure */
235 if (rc == -EAGAIN && ksocknal_lib_memory_pressure(conn))
241 /* socket's wmem_queued now includes 'rc' bytes */
242 atomic_sub(rc, &conn->ksnc_tx_nob);
245 } while (tx->tx_resid);
247 ksocknal_connsock_decref(conn);
252 ksocknal_recv_iov(struct ksock_conn *conn)
254 struct kvec *iov = conn->ksnc_rx_iov;
258 LASSERT(conn->ksnc_rx_niov > 0);
261 * Never touch conn->ksnc_rx_iov or change connection
262 * status inside ksocknal_lib_recv_iov
264 rc = ksocknal_lib_recv_iov(conn);
269 /* received something... */
272 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
273 conn->ksnc_rx_deadline =
274 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
275 mb(); /* order with setting rx_started */
276 conn->ksnc_rx_started = 1;
278 conn->ksnc_rx_nob_wanted -= nob;
279 conn->ksnc_rx_nob_left -= nob;
282 LASSERT(conn->ksnc_rx_niov > 0);
284 if (nob < (int)iov->iov_len) {
286 iov->iov_base += nob;
291 conn->ksnc_rx_iov = ++iov;
292 conn->ksnc_rx_niov--;
299 ksocknal_recv_kiov(struct ksock_conn *conn)
301 struct bio_vec *kiov = conn->ksnc_rx_kiov;
305 LASSERT(conn->ksnc_rx_nkiov > 0);
308 * Never touch conn->ksnc_rx_kiov or change connection
309 * status inside ksocknal_lib_recv_iov
311 rc = ksocknal_lib_recv_kiov(conn);
316 /* received something... */
319 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
320 conn->ksnc_rx_deadline =
321 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
322 mb(); /* order with setting rx_started */
323 conn->ksnc_rx_started = 1;
325 conn->ksnc_rx_nob_wanted -= nob;
326 conn->ksnc_rx_nob_left -= nob;
329 LASSERT(conn->ksnc_rx_nkiov > 0);
331 if (nob < (int)kiov->bv_len) {
332 kiov->bv_offset += nob;
338 conn->ksnc_rx_kiov = ++kiov;
339 conn->ksnc_rx_nkiov--;
346 ksocknal_receive(struct ksock_conn *conn)
349 * Return 1 on success, 0 on EOF, < 0 on error.
350 * Caller checks ksnc_rx_nob_wanted to determine
351 * progress/completion.
355 if (ksocknal_data.ksnd_stall_rx) {
356 set_current_state(TASK_UNINTERRUPTIBLE);
357 schedule_timeout(cfs_time_seconds(ksocknal_data.ksnd_stall_rx));
360 rc = ksocknal_connsock_addref(conn);
362 LASSERT(conn->ksnc_closing);
367 if (conn->ksnc_rx_niov)
368 rc = ksocknal_recv_iov(conn);
370 rc = ksocknal_recv_kiov(conn);
373 /* error/EOF or partial receive */
376 } else if (!rc && conn->ksnc_rx_started) {
377 /* EOF in the middle of a message */
383 /* Completed a fragment */
385 if (!conn->ksnc_rx_nob_wanted) {
391 ksocknal_connsock_decref(conn);
396 ksocknal_tx_done(struct lnet_ni *ni, struct ksock_tx *tx)
398 struct lnet_msg *lnetmsg = tx->tx_lnetmsg;
399 int rc = (!tx->tx_resid && !tx->tx_zc_aborted) ? 0 : -EIO;
401 LASSERT(ni || tx->tx_conn);
404 ksocknal_conn_decref(tx->tx_conn);
406 if (!ni && tx->tx_conn)
407 ni = tx->tx_conn->ksnc_peer->ksnp_ni;
409 ksocknal_free_tx(tx);
410 if (lnetmsg) /* KSOCK_MSG_NOOP go without lnetmsg */
411 lnet_finalize(ni, lnetmsg, rc);
415 ksocknal_txlist_done(struct lnet_ni *ni, struct list_head *txlist, int error)
419 while (!list_empty(txlist)) {
420 tx = list_entry(txlist->next, struct ksock_tx, tx_list);
422 if (error && tx->tx_lnetmsg) {
423 CNETERR("Deleting packet type %d len %d %s->%s\n",
424 le32_to_cpu(tx->tx_lnetmsg->msg_hdr.type),
425 le32_to_cpu(tx->tx_lnetmsg->msg_hdr.payload_length),
426 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.src_nid)),
427 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.dest_nid)));
429 CNETERR("Deleting noop packet\n");
432 list_del(&tx->tx_list);
434 LASSERT(atomic_read(&tx->tx_refcount) == 1);
435 ksocknal_tx_done(ni, tx);
440 ksocknal_check_zc_req(struct ksock_tx *tx)
442 struct ksock_conn *conn = tx->tx_conn;
443 struct ksock_peer *peer = conn->ksnc_peer;
446 * Set tx_msg.ksm_zc_cookies[0] to a unique non-zero cookie and add tx
447 * to ksnp_zc_req_list if some fragment of this message should be sent
448 * zero-copy. Our peer will send an ACK containing this cookie when
449 * she has received this message to tell us we can signal completion.
450 * tx_msg.ksm_zc_cookies[0] remains non-zero while tx is on
453 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
454 LASSERT(tx->tx_zc_capable);
456 tx->tx_zc_checked = 1;
458 if (conn->ksnc_proto == &ksocknal_protocol_v1x ||
459 !conn->ksnc_zc_capable)
463 * assign cookie and queue tx to pending list, it will be released when
464 * a matching ack is received. See ksocknal_handle_zcack()
466 ksocknal_tx_addref(tx);
468 spin_lock(&peer->ksnp_lock);
470 /* ZC_REQ is going to be pinned to the peer */
472 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
474 LASSERT(!tx->tx_msg.ksm_zc_cookies[0]);
476 tx->tx_msg.ksm_zc_cookies[0] = peer->ksnp_zc_next_cookie++;
478 if (!peer->ksnp_zc_next_cookie)
479 peer->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
481 list_add_tail(&tx->tx_zc_list, &peer->ksnp_zc_req_list);
483 spin_unlock(&peer->ksnp_lock);
487 ksocknal_uncheck_zc_req(struct ksock_tx *tx)
489 struct ksock_peer *peer = tx->tx_conn->ksnc_peer;
491 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
492 LASSERT(tx->tx_zc_capable);
494 tx->tx_zc_checked = 0;
496 spin_lock(&peer->ksnp_lock);
498 if (!tx->tx_msg.ksm_zc_cookies[0]) {
499 /* Not waiting for an ACK */
500 spin_unlock(&peer->ksnp_lock);
504 tx->tx_msg.ksm_zc_cookies[0] = 0;
505 list_del(&tx->tx_zc_list);
507 spin_unlock(&peer->ksnp_lock);
509 ksocknal_tx_decref(tx);
513 ksocknal_process_transmit(struct ksock_conn *conn, struct ksock_tx *tx)
517 if (tx->tx_zc_capable && !tx->tx_zc_checked)
518 ksocknal_check_zc_req(tx);
520 rc = ksocknal_transmit(conn, tx);
522 CDEBUG(D_NET, "send(%d) %d\n", tx->tx_resid, rc);
525 /* Sent everything OK */
537 counter++; /* exponential backoff warnings */
538 if ((counter & (-counter)) == counter)
539 CWARN("%u ENOMEM tx %p\n", counter, conn);
541 /* Queue on ksnd_enomem_conns for retry after a timeout */
542 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
544 /* enomem list takes over scheduler's ref... */
545 LASSERT(conn->ksnc_tx_scheduled);
546 list_add_tail(&conn->ksnc_tx_list,
547 &ksocknal_data.ksnd_enomem_conns);
548 if (!cfs_time_aftereq(cfs_time_add(cfs_time_current(),
549 SOCKNAL_ENOMEM_RETRY),
550 ksocknal_data.ksnd_reaper_waketime))
551 wake_up(&ksocknal_data.ksnd_reaper_waitq);
553 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
560 if (!conn->ksnc_closing) {
563 LCONSOLE_WARN("Host %pI4h reset our connection while we were sending data; it may have rebooted.\n",
567 LCONSOLE_WARN("There was an unexpected network error while writing to %pI4h: %d.\n",
568 &conn->ksnc_ipaddr, rc);
571 CDEBUG(D_NET, "[%p] Error %d on write to %s ip %pI4h:%d\n",
573 libcfs_id2str(conn->ksnc_peer->ksnp_id),
578 if (tx->tx_zc_checked)
579 ksocknal_uncheck_zc_req(tx);
581 /* it's not an error if conn is being closed */
582 ksocknal_close_conn_and_siblings(conn, (conn->ksnc_closing) ? 0 : rc);
588 ksocknal_launch_connection_locked(struct ksock_route *route)
590 /* called holding write lock on ksnd_global_lock */
592 LASSERT(!route->ksnr_scheduled);
593 LASSERT(!route->ksnr_connecting);
594 LASSERT(ksocknal_route_mask() & ~route->ksnr_connected);
596 route->ksnr_scheduled = 1; /* scheduling conn for connd */
597 ksocknal_route_addref(route); /* extra ref for connd */
599 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
601 list_add_tail(&route->ksnr_connd_list,
602 &ksocknal_data.ksnd_connd_routes);
603 wake_up(&ksocknal_data.ksnd_connd_waitq);
605 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
609 ksocknal_launch_all_connections_locked(struct ksock_peer *peer)
611 struct ksock_route *route;
613 /* called holding write lock on ksnd_global_lock */
615 /* launch any/all connections that need it */
616 route = ksocknal_find_connectable_route_locked(peer);
620 ksocknal_launch_connection_locked(route);
625 ksocknal_find_conn_locked(struct ksock_peer *peer, struct ksock_tx *tx,
628 struct list_head *tmp;
629 struct ksock_conn *conn;
630 struct ksock_conn *typed = NULL;
631 struct ksock_conn *fallback = NULL;
635 list_for_each(tmp, &peer->ksnp_conns) {
636 struct ksock_conn *c;
639 c = list_entry(tmp, struct ksock_conn, ksnc_list);
640 nob = atomic_read(&c->ksnc_tx_nob) +
641 c->ksnc_sock->sk->sk_wmem_queued;
643 LASSERT(!c->ksnc_closing);
644 LASSERT(c->ksnc_proto &&
645 c->ksnc_proto->pro_match_tx);
647 rc = c->ksnc_proto->pro_match_tx(c, tx, nonblk);
652 case SOCKNAL_MATCH_NO: /* protocol rejected the tx */
655 case SOCKNAL_MATCH_YES: /* typed connection */
656 if (!typed || tnob > nob ||
657 (tnob == nob && *ksocknal_tunables.ksnd_round_robin &&
658 cfs_time_after(typed->ksnc_tx_last_post, c->ksnc_tx_last_post))) {
664 case SOCKNAL_MATCH_MAY: /* fallback connection */
665 if (!fallback || fnob > nob ||
666 (fnob == nob && *ksocknal_tunables.ksnd_round_robin &&
667 cfs_time_after(fallback->ksnc_tx_last_post, c->ksnc_tx_last_post))) {
675 /* prefer the typed selection */
676 conn = (typed) ? typed : fallback;
679 conn->ksnc_tx_last_post = cfs_time_current();
685 ksocknal_tx_prep(struct ksock_conn *conn, struct ksock_tx *tx)
687 conn->ksnc_proto->pro_pack(tx);
689 atomic_add(tx->tx_nob, &conn->ksnc_tx_nob);
690 ksocknal_conn_addref(conn); /* +1 ref for tx */
695 ksocknal_queue_tx_locked(struct ksock_tx *tx, struct ksock_conn *conn)
697 struct ksock_sched *sched = conn->ksnc_scheduler;
698 struct ksock_msg *msg = &tx->tx_msg;
699 struct ksock_tx *ztx = NULL;
703 * called holding global lock (read or irq-write) and caller may
704 * not have dropped this lock between finding conn and calling me,
705 * so we don't need the {get,put}connsock dance to deref
708 LASSERT(!conn->ksnc_closing);
710 CDEBUG(D_NET, "Sending to %s ip %pI4h:%d\n",
711 libcfs_id2str(conn->ksnc_peer->ksnp_id),
712 &conn->ksnc_ipaddr, conn->ksnc_port);
714 ksocknal_tx_prep(conn, tx);
717 * Ensure the frags we've been given EXACTLY match the number of
718 * bytes we want to send. Many TCP/IP stacks disregard any total
719 * size parameters passed to them and just look at the frags.
721 * We always expect at least 1 mapped fragment containing the
722 * complete ksocknal message header.
724 LASSERT(lnet_iov_nob(tx->tx_niov, tx->tx_iov) +
725 lnet_kiov_nob(tx->tx_nkiov, tx->tx_kiov) ==
726 (unsigned int)tx->tx_nob);
727 LASSERT(tx->tx_niov >= 1);
728 LASSERT(tx->tx_resid == tx->tx_nob);
730 CDEBUG(D_NET, "Packet %p type %d, nob %d niov %d nkiov %d\n",
731 tx, (tx->tx_lnetmsg) ? tx->tx_lnetmsg->msg_hdr.type :
733 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
736 * FIXME: SOCK_WMEM_QUEUED and SOCK_ERROR could block in __DARWIN8__
737 * but they're used inside spinlocks a lot.
739 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
740 spin_lock_bh(&sched->kss_lock);
742 if (list_empty(&conn->ksnc_tx_queue) && !bufnob) {
743 /* First packet starts the timeout */
744 conn->ksnc_tx_deadline =
745 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
746 if (conn->ksnc_tx_bufnob > 0) /* something got ACKed */
747 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
748 conn->ksnc_tx_bufnob = 0;
749 mb(); /* order with adding to tx_queue */
752 if (msg->ksm_type == KSOCK_MSG_NOOP) {
754 * The packet is noop ZC ACK, try to piggyback the ack_cookie
755 * on a normal packet so I don't need to send it
757 LASSERT(msg->ksm_zc_cookies[1]);
758 LASSERT(conn->ksnc_proto->pro_queue_tx_zcack);
760 /* ZC ACK piggybacked on ztx release tx later */
761 if (conn->ksnc_proto->pro_queue_tx_zcack(conn, tx, 0))
765 * It's a normal packet - can it piggback a noop zc-ack that
766 * has been queued already?
768 LASSERT(!msg->ksm_zc_cookies[1]);
769 LASSERT(conn->ksnc_proto->pro_queue_tx_msg);
771 ztx = conn->ksnc_proto->pro_queue_tx_msg(conn, tx);
772 /* ztx will be released later */
776 atomic_sub(ztx->tx_nob, &conn->ksnc_tx_nob);
777 list_add_tail(&ztx->tx_list, &sched->kss_zombie_noop_txs);
780 if (conn->ksnc_tx_ready && /* able to send */
781 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
782 /* +1 ref for scheduler */
783 ksocknal_conn_addref(conn);
784 list_add_tail(&conn->ksnc_tx_list, &sched->kss_tx_conns);
785 conn->ksnc_tx_scheduled = 1;
786 wake_up(&sched->kss_waitq);
789 spin_unlock_bh(&sched->kss_lock);
793 ksocknal_find_connectable_route_locked(struct ksock_peer *peer)
795 unsigned long now = cfs_time_current();
796 struct list_head *tmp;
797 struct ksock_route *route;
799 list_for_each(tmp, &peer->ksnp_routes) {
800 route = list_entry(tmp, struct ksock_route, ksnr_list);
802 LASSERT(!route->ksnr_connecting || route->ksnr_scheduled);
804 /* connections being established */
805 if (route->ksnr_scheduled)
808 /* all route types connected ? */
809 if (!(ksocknal_route_mask() & ~route->ksnr_connected))
812 if (!(!route->ksnr_retry_interval || /* first attempt */
813 cfs_time_aftereq(now, route->ksnr_timeout))) {
815 "Too soon to retry route %pI4h (cnted %d, interval %ld, %ld secs later)\n",
817 route->ksnr_connected,
818 route->ksnr_retry_interval,
819 cfs_duration_sec(route->ksnr_timeout - now));
830 ksocknal_find_connecting_route_locked(struct ksock_peer *peer)
832 struct list_head *tmp;
833 struct ksock_route *route;
835 list_for_each(tmp, &peer->ksnp_routes) {
836 route = list_entry(tmp, struct ksock_route, ksnr_list);
838 LASSERT(!route->ksnr_connecting || route->ksnr_scheduled);
840 if (route->ksnr_scheduled)
848 ksocknal_launch_packet(struct lnet_ni *ni, struct ksock_tx *tx,
849 struct lnet_process_id id)
851 struct ksock_peer *peer;
852 struct ksock_conn *conn;
857 LASSERT(!tx->tx_conn);
859 g_lock = &ksocknal_data.ksnd_global_lock;
861 for (retry = 0;; retry = 1) {
863 peer = ksocknal_find_peer_locked(ni, id);
865 if (!ksocknal_find_connectable_route_locked(peer)) {
866 conn = ksocknal_find_conn_locked(peer, tx, tx->tx_nonblk);
869 * I've got no routes that need to be
870 * connecting and I do have an actual
873 ksocknal_queue_tx_locked(tx, conn);
880 /* I'll need a write lock... */
883 write_lock_bh(g_lock);
885 peer = ksocknal_find_peer_locked(ni, id);
889 write_unlock_bh(g_lock);
891 if (id.pid & LNET_PID_USERFLAG) {
892 CERROR("Refusing to create a connection to userspace process %s\n",
894 return -EHOSTUNREACH;
898 CERROR("Can't find peer %s\n", libcfs_id2str(id));
899 return -EHOSTUNREACH;
902 rc = ksocknal_add_peer(ni, id,
903 LNET_NIDADDR(id.nid),
904 lnet_acceptor_port());
906 CERROR("Can't add peer %s: %d\n",
907 libcfs_id2str(id), rc);
912 ksocknal_launch_all_connections_locked(peer);
914 conn = ksocknal_find_conn_locked(peer, tx, tx->tx_nonblk);
916 /* Connection exists; queue message on it */
917 ksocknal_queue_tx_locked(tx, conn);
918 write_unlock_bh(g_lock);
922 if (peer->ksnp_accepting > 0 ||
923 ksocknal_find_connecting_route_locked(peer)) {
924 /* the message is going to be pinned to the peer */
926 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
928 /* Queue the message until a connection is established */
929 list_add_tail(&tx->tx_list, &peer->ksnp_tx_queue);
930 write_unlock_bh(g_lock);
934 write_unlock_bh(g_lock);
936 /* NB Routes may be ignored if connections to them failed recently */
937 CNETERR("No usable routes to %s\n", libcfs_id2str(id));
938 return -EHOSTUNREACH;
942 ksocknal_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
945 int type = lntmsg->msg_type;
946 struct lnet_process_id target = lntmsg->msg_target;
947 unsigned int payload_niov = lntmsg->msg_niov;
948 struct kvec *payload_iov = lntmsg->msg_iov;
949 struct bio_vec *payload_kiov = lntmsg->msg_kiov;
950 unsigned int payload_offset = lntmsg->msg_offset;
951 unsigned int payload_nob = lntmsg->msg_len;
957 * NB 'private' is different depending on what we're sending.
960 CDEBUG(D_NET, "sending %u bytes in %d frags to %s\n",
961 payload_nob, payload_niov, libcfs_id2str(target));
963 LASSERT(!payload_nob || payload_niov > 0);
964 LASSERT(payload_niov <= LNET_MAX_IOV);
965 /* payload is either all vaddrs or all pages */
966 LASSERT(!(payload_kiov && payload_iov));
967 LASSERT(!in_interrupt());
970 desc_size = offsetof(struct ksock_tx,
971 tx_frags.virt.iov[1 + payload_niov]);
973 desc_size = offsetof(struct ksock_tx,
974 tx_frags.paged.kiov[payload_niov]);
976 if (lntmsg->msg_vmflush)
977 mpflag = cfs_memory_pressure_get_and_set();
978 tx = ksocknal_alloc_tx(KSOCK_MSG_LNET, desc_size);
980 CERROR("Can't allocate tx desc type %d size %d\n",
982 if (lntmsg->msg_vmflush)
983 cfs_memory_pressure_restore(mpflag);
987 tx->tx_conn = NULL; /* set when assigned a conn */
988 tx->tx_lnetmsg = lntmsg;
993 tx->tx_iov = tx->tx_frags.virt.iov;
995 lnet_extract_iov(payload_niov, &tx->tx_iov[1],
996 payload_niov, payload_iov,
997 payload_offset, payload_nob);
1000 tx->tx_iov = &tx->tx_frags.paged.iov;
1001 tx->tx_kiov = tx->tx_frags.paged.kiov;
1002 tx->tx_nkiov = lnet_extract_kiov(payload_niov, tx->tx_kiov,
1003 payload_niov, payload_kiov,
1004 payload_offset, payload_nob);
1006 if (payload_nob >= *ksocknal_tunables.ksnd_zc_min_payload)
1007 tx->tx_zc_capable = 1;
1010 tx->tx_msg.ksm_csum = 0;
1011 tx->tx_msg.ksm_type = KSOCK_MSG_LNET;
1012 tx->tx_msg.ksm_zc_cookies[0] = 0;
1013 tx->tx_msg.ksm_zc_cookies[1] = 0;
1015 /* The first fragment will be set later in pro_pack */
1016 rc = ksocknal_launch_packet(ni, tx, target);
1018 cfs_memory_pressure_restore(mpflag);
1023 ksocknal_free_tx(tx);
1028 ksocknal_thread_start(int (*fn)(void *arg), void *arg, char *name)
1030 struct task_struct *task = kthread_run(fn, arg, "%s", name);
1033 return PTR_ERR(task);
1035 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1036 ksocknal_data.ksnd_nthreads++;
1037 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1042 ksocknal_thread_fini(void)
1044 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1045 ksocknal_data.ksnd_nthreads--;
1046 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1050 ksocknal_new_packet(struct ksock_conn *conn, int nob_to_skip)
1052 static char ksocknal_slop_buffer[4096];
1058 LASSERT(conn->ksnc_proto);
1060 if (*ksocknal_tunables.ksnd_eager_ack & conn->ksnc_type) {
1061 /* Remind the socket to ack eagerly... */
1062 ksocknal_lib_eager_ack(conn);
1065 if (!nob_to_skip) { /* right at next packet boundary now */
1066 conn->ksnc_rx_started = 0;
1067 mb(); /* racing with timeout thread */
1069 switch (conn->ksnc_proto->pro_version) {
1070 case KSOCK_PROTO_V2:
1071 case KSOCK_PROTO_V3:
1072 conn->ksnc_rx_state = SOCKNAL_RX_KSM_HEADER;
1073 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1074 conn->ksnc_rx_iov[0].iov_base = &conn->ksnc_msg;
1076 conn->ksnc_rx_nob_wanted = offsetof(struct ksock_msg, ksm_u);
1077 conn->ksnc_rx_nob_left = offsetof(struct ksock_msg, ksm_u);
1078 conn->ksnc_rx_iov[0].iov_len = offsetof(struct ksock_msg, ksm_u);
1081 case KSOCK_PROTO_V1:
1082 /* Receiving bare struct lnet_hdr */
1083 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1084 conn->ksnc_rx_nob_wanted = sizeof(struct lnet_hdr);
1085 conn->ksnc_rx_nob_left = sizeof(struct lnet_hdr);
1087 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1088 conn->ksnc_rx_iov[0].iov_base = &conn->ksnc_msg.ksm_u.lnetmsg;
1089 conn->ksnc_rx_iov[0].iov_len = sizeof(struct lnet_hdr);
1095 conn->ksnc_rx_niov = 1;
1097 conn->ksnc_rx_kiov = NULL;
1098 conn->ksnc_rx_nkiov = 0;
1099 conn->ksnc_rx_csum = ~0;
1104 * Set up to skip as much as possible now. If there's more left
1105 * (ran out of iov entries) we'll get called again
1107 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1108 conn->ksnc_rx_nob_left = nob_to_skip;
1109 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1114 nob = min_t(int, nob_to_skip, sizeof(ksocknal_slop_buffer));
1116 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1117 conn->ksnc_rx_iov[niov].iov_len = nob;
1122 } while (nob_to_skip && /* mustn't overflow conn's rx iov */
1123 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof(struct iovec));
1125 conn->ksnc_rx_niov = niov;
1126 conn->ksnc_rx_kiov = NULL;
1127 conn->ksnc_rx_nkiov = 0;
1128 conn->ksnc_rx_nob_wanted = skipped;
1133 ksocknal_process_receive(struct ksock_conn *conn)
1135 struct lnet_hdr *lhdr;
1136 struct lnet_process_id *id;
1139 LASSERT(atomic_read(&conn->ksnc_conn_refcount) > 0);
1141 /* NB: sched lock NOT held */
1142 /* SOCKNAL_RX_LNET_HEADER is here for backward compatibility */
1143 LASSERT(conn->ksnc_rx_state == SOCKNAL_RX_KSM_HEADER ||
1144 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD ||
1145 conn->ksnc_rx_state == SOCKNAL_RX_LNET_HEADER ||
1146 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1148 if (conn->ksnc_rx_nob_wanted) {
1149 rc = ksocknal_receive(conn);
1152 LASSERT(rc != -EAGAIN);
1155 CDEBUG(D_NET, "[%p] EOF from %s ip %pI4h:%d\n",
1157 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1160 else if (!conn->ksnc_closing)
1161 CERROR("[%p] Error %d on read from %s ip %pI4h:%d\n",
1163 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1167 /* it's not an error if conn is being closed */
1168 ksocknal_close_conn_and_siblings(conn,
1169 (conn->ksnc_closing) ? 0 : rc);
1170 return (!rc ? -ESHUTDOWN : rc);
1173 if (conn->ksnc_rx_nob_wanted) {
1178 switch (conn->ksnc_rx_state) {
1179 case SOCKNAL_RX_KSM_HEADER:
1180 if (conn->ksnc_flip) {
1181 __swab32s(&conn->ksnc_msg.ksm_type);
1182 __swab32s(&conn->ksnc_msg.ksm_csum);
1183 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[0]);
1184 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[1]);
1187 if (conn->ksnc_msg.ksm_type != KSOCK_MSG_NOOP &&
1188 conn->ksnc_msg.ksm_type != KSOCK_MSG_LNET) {
1189 CERROR("%s: Unknown message type: %x\n",
1190 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1191 conn->ksnc_msg.ksm_type);
1192 ksocknal_new_packet(conn, 0);
1193 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1197 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP &&
1198 conn->ksnc_msg.ksm_csum && /* has checksum */
1199 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1200 /* NOOP Checksum error */
1201 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1202 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1203 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1204 ksocknal_new_packet(conn, 0);
1205 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1209 if (conn->ksnc_msg.ksm_zc_cookies[1]) {
1212 LASSERT(conn->ksnc_proto != &ksocknal_protocol_v1x);
1214 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP)
1215 cookie = conn->ksnc_msg.ksm_zc_cookies[0];
1217 rc = conn->ksnc_proto->pro_handle_zcack(conn, cookie,
1218 conn->ksnc_msg.ksm_zc_cookies[1]);
1221 CERROR("%s: Unknown ZC-ACK cookie: %llu, %llu\n",
1222 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1223 cookie, conn->ksnc_msg.ksm_zc_cookies[1]);
1224 ksocknal_new_packet(conn, 0);
1225 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1230 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP) {
1231 ksocknal_new_packet(conn, 0);
1232 return 0; /* NOOP is done and just return */
1235 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1236 conn->ksnc_rx_nob_wanted = sizeof(struct ksock_lnet_msg);
1237 conn->ksnc_rx_nob_left = sizeof(struct ksock_lnet_msg);
1239 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1240 conn->ksnc_rx_iov[0].iov_base = &conn->ksnc_msg.ksm_u.lnetmsg;
1241 conn->ksnc_rx_iov[0].iov_len = sizeof(struct ksock_lnet_msg);
1243 conn->ksnc_rx_niov = 1;
1244 conn->ksnc_rx_kiov = NULL;
1245 conn->ksnc_rx_nkiov = 0;
1247 goto again; /* read lnet header now */
1249 case SOCKNAL_RX_LNET_HEADER:
1250 /* unpack message header */
1251 conn->ksnc_proto->pro_unpack(&conn->ksnc_msg);
1253 if (conn->ksnc_peer->ksnp_id.pid & LNET_PID_USERFLAG) {
1254 /* Userspace peer */
1255 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1256 id = &conn->ksnc_peer->ksnp_id;
1258 /* Substitute process ID assigned at connection time */
1259 lhdr->src_pid = cpu_to_le32(id->pid);
1260 lhdr->src_nid = cpu_to_le64(id->nid);
1263 conn->ksnc_rx_state = SOCKNAL_RX_PARSE;
1264 ksocknal_conn_addref(conn); /* ++ref while parsing */
1266 rc = lnet_parse(conn->ksnc_peer->ksnp_ni,
1267 &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr,
1268 conn->ksnc_peer->ksnp_id.nid, conn, 0);
1270 /* I just received garbage: give up on this conn */
1271 ksocknal_new_packet(conn, 0);
1272 ksocknal_close_conn_and_siblings(conn, rc);
1273 ksocknal_conn_decref(conn);
1277 /* I'm racing with ksocknal_recv() */
1278 LASSERT(conn->ksnc_rx_state == SOCKNAL_RX_PARSE ||
1279 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD);
1281 if (conn->ksnc_rx_state != SOCKNAL_RX_LNET_PAYLOAD)
1284 /* ksocknal_recv() got called */
1287 case SOCKNAL_RX_LNET_PAYLOAD:
1288 /* payload all received */
1291 if (!conn->ksnc_rx_nob_left && /* not truncating */
1292 conn->ksnc_msg.ksm_csum && /* has checksum */
1293 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1294 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1295 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1296 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1300 if (!rc && conn->ksnc_msg.ksm_zc_cookies[0]) {
1301 LASSERT(conn->ksnc_proto != &ksocknal_protocol_v1x);
1303 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1304 id = &conn->ksnc_peer->ksnp_id;
1306 rc = conn->ksnc_proto->pro_handle_zcreq(conn,
1307 conn->ksnc_msg.ksm_zc_cookies[0],
1308 *ksocknal_tunables.ksnd_nonblk_zcack ||
1309 le64_to_cpu(lhdr->src_nid) != id->nid);
1312 lnet_finalize(conn->ksnc_peer->ksnp_ni, conn->ksnc_cookie, rc);
1315 ksocknal_new_packet(conn, 0);
1316 ksocknal_close_conn_and_siblings(conn, rc);
1321 case SOCKNAL_RX_SLOP:
1322 /* starting new packet? */
1323 if (ksocknal_new_packet(conn, conn->ksnc_rx_nob_left))
1324 return 0; /* come back later */
1325 goto again; /* try to finish reading slop now */
1333 return -EINVAL; /* keep gcc happy */
1337 ksocknal_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
1338 int delayed, struct iov_iter *to, unsigned int rlen)
1340 struct ksock_conn *conn = private;
1341 struct ksock_sched *sched = conn->ksnc_scheduler;
1343 LASSERT(iov_iter_count(to) <= rlen);
1344 LASSERT(to->nr_segs <= LNET_MAX_IOV);
1346 conn->ksnc_cookie = msg;
1347 conn->ksnc_rx_nob_wanted = iov_iter_count(to);
1348 conn->ksnc_rx_nob_left = rlen;
1350 if (to->type & ITER_KVEC) {
1351 conn->ksnc_rx_nkiov = 0;
1352 conn->ksnc_rx_kiov = NULL;
1353 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1354 conn->ksnc_rx_niov =
1355 lnet_extract_iov(LNET_MAX_IOV, conn->ksnc_rx_iov,
1356 to->nr_segs, to->kvec,
1357 to->iov_offset, iov_iter_count(to));
1359 conn->ksnc_rx_niov = 0;
1360 conn->ksnc_rx_iov = NULL;
1361 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1362 conn->ksnc_rx_nkiov =
1363 lnet_extract_kiov(LNET_MAX_IOV, conn->ksnc_rx_kiov,
1364 to->nr_segs, to->bvec,
1365 to->iov_offset, iov_iter_count(to));
1368 LASSERT(conn->ksnc_rx_scheduled);
1370 spin_lock_bh(&sched->kss_lock);
1372 switch (conn->ksnc_rx_state) {
1373 case SOCKNAL_RX_PARSE_WAIT:
1374 list_add_tail(&conn->ksnc_rx_list, &sched->kss_rx_conns);
1375 wake_up(&sched->kss_waitq);
1376 LASSERT(conn->ksnc_rx_ready);
1379 case SOCKNAL_RX_PARSE:
1380 /* scheduler hasn't noticed I'm parsing yet */
1384 conn->ksnc_rx_state = SOCKNAL_RX_LNET_PAYLOAD;
1386 spin_unlock_bh(&sched->kss_lock);
1387 ksocknal_conn_decref(conn);
1392 ksocknal_sched_cansleep(struct ksock_sched *sched)
1396 spin_lock_bh(&sched->kss_lock);
1398 rc = !ksocknal_data.ksnd_shuttingdown &&
1399 list_empty(&sched->kss_rx_conns) &&
1400 list_empty(&sched->kss_tx_conns);
1402 spin_unlock_bh(&sched->kss_lock);
1406 int ksocknal_scheduler(void *arg)
1408 struct ksock_sched_info *info;
1409 struct ksock_sched *sched;
1410 struct ksock_conn *conn;
1411 struct ksock_tx *tx;
1414 long id = (long)arg;
1416 info = ksocknal_data.ksnd_sched_info[KSOCK_THREAD_CPT(id)];
1417 sched = &info->ksi_scheds[KSOCK_THREAD_SID(id)];
1419 cfs_block_allsigs();
1421 rc = cfs_cpt_bind(lnet_cpt_table(), info->ksi_cpt);
1423 CWARN("Can't set CPU partition affinity to %d: %d\n",
1427 spin_lock_bh(&sched->kss_lock);
1429 while (!ksocknal_data.ksnd_shuttingdown) {
1430 int did_something = 0;
1432 /* Ensure I progress everything semi-fairly */
1434 if (!list_empty(&sched->kss_rx_conns)) {
1435 conn = list_entry(sched->kss_rx_conns.next,
1436 struct ksock_conn, ksnc_rx_list);
1437 list_del(&conn->ksnc_rx_list);
1439 LASSERT(conn->ksnc_rx_scheduled);
1440 LASSERT(conn->ksnc_rx_ready);
1443 * clear rx_ready in case receive isn't complete.
1444 * Do it BEFORE we call process_recv, since
1445 * data_ready can set it any time after we release
1448 conn->ksnc_rx_ready = 0;
1449 spin_unlock_bh(&sched->kss_lock);
1451 rc = ksocknal_process_receive(conn);
1453 spin_lock_bh(&sched->kss_lock);
1455 /* I'm the only one that can clear this flag */
1456 LASSERT(conn->ksnc_rx_scheduled);
1458 /* Did process_receive get everything it wanted? */
1460 conn->ksnc_rx_ready = 1;
1462 if (conn->ksnc_rx_state == SOCKNAL_RX_PARSE) {
1464 * Conn blocked waiting for ksocknal_recv()
1465 * I change its state (under lock) to signal
1466 * it can be rescheduled
1468 conn->ksnc_rx_state = SOCKNAL_RX_PARSE_WAIT;
1469 } else if (conn->ksnc_rx_ready) {
1470 /* reschedule for rx */
1471 list_add_tail(&conn->ksnc_rx_list,
1472 &sched->kss_rx_conns);
1474 conn->ksnc_rx_scheduled = 0;
1476 ksocknal_conn_decref(conn);
1482 if (!list_empty(&sched->kss_tx_conns)) {
1485 if (!list_empty(&sched->kss_zombie_noop_txs)) {
1486 list_add(&zlist, &sched->kss_zombie_noop_txs);
1487 list_del_init(&sched->kss_zombie_noop_txs);
1490 conn = list_entry(sched->kss_tx_conns.next,
1491 struct ksock_conn, ksnc_tx_list);
1492 list_del(&conn->ksnc_tx_list);
1494 LASSERT(conn->ksnc_tx_scheduled);
1495 LASSERT(conn->ksnc_tx_ready);
1496 LASSERT(!list_empty(&conn->ksnc_tx_queue));
1498 tx = list_entry(conn->ksnc_tx_queue.next,
1499 struct ksock_tx, tx_list);
1501 if (conn->ksnc_tx_carrier == tx)
1502 ksocknal_next_tx_carrier(conn);
1504 /* dequeue now so empty list => more to send */
1505 list_del(&tx->tx_list);
1508 * Clear tx_ready in case send isn't complete. Do
1509 * it BEFORE we call process_transmit, since
1510 * write_space can set it any time after we release
1513 conn->ksnc_tx_ready = 0;
1514 spin_unlock_bh(&sched->kss_lock);
1516 if (!list_empty(&zlist)) {
1518 * free zombie noop txs, it's fast because
1519 * noop txs are just put in freelist
1521 ksocknal_txlist_done(NULL, &zlist, 0);
1524 rc = ksocknal_process_transmit(conn, tx);
1526 if (rc == -ENOMEM || rc == -EAGAIN) {
1528 * Incomplete send: replace tx on HEAD of
1531 spin_lock_bh(&sched->kss_lock);
1532 list_add(&tx->tx_list, &conn->ksnc_tx_queue);
1534 /* Complete send; tx -ref */
1535 ksocknal_tx_decref(tx);
1537 spin_lock_bh(&sched->kss_lock);
1538 /* assume space for more */
1539 conn->ksnc_tx_ready = 1;
1542 if (rc == -ENOMEM) {
1544 * Do nothing; after a short timeout, this
1545 * conn will be reposted on kss_tx_conns.
1547 } else if (conn->ksnc_tx_ready &&
1548 !list_empty(&conn->ksnc_tx_queue)) {
1549 /* reschedule for tx */
1550 list_add_tail(&conn->ksnc_tx_list,
1551 &sched->kss_tx_conns);
1553 conn->ksnc_tx_scheduled = 0;
1555 ksocknal_conn_decref(conn);
1560 if (!did_something || /* nothing to do */
1561 ++nloops == SOCKNAL_RESCHED) { /* hogging CPU? */
1562 spin_unlock_bh(&sched->kss_lock);
1566 if (!did_something) { /* wait for something to do */
1567 rc = wait_event_interruptible_exclusive(
1569 !ksocknal_sched_cansleep(sched));
1575 spin_lock_bh(&sched->kss_lock);
1579 spin_unlock_bh(&sched->kss_lock);
1580 ksocknal_thread_fini();
1585 * Add connection to kss_rx_conns of scheduler
1586 * and wakeup the scheduler.
1588 void ksocknal_read_callback(struct ksock_conn *conn)
1590 struct ksock_sched *sched;
1592 sched = conn->ksnc_scheduler;
1594 spin_lock_bh(&sched->kss_lock);
1596 conn->ksnc_rx_ready = 1;
1598 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1599 list_add_tail(&conn->ksnc_rx_list, &sched->kss_rx_conns);
1600 conn->ksnc_rx_scheduled = 1;
1601 /* extra ref for scheduler */
1602 ksocknal_conn_addref(conn);
1604 wake_up(&sched->kss_waitq);
1606 spin_unlock_bh(&sched->kss_lock);
1610 * Add connection to kss_tx_conns of scheduler
1611 * and wakeup the scheduler.
1613 void ksocknal_write_callback(struct ksock_conn *conn)
1615 struct ksock_sched *sched;
1617 sched = conn->ksnc_scheduler;
1619 spin_lock_bh(&sched->kss_lock);
1621 conn->ksnc_tx_ready = 1;
1623 if (!conn->ksnc_tx_scheduled && /* not being progressed */
1624 !list_empty(&conn->ksnc_tx_queue)) { /* packets to send */
1625 list_add_tail(&conn->ksnc_tx_list, &sched->kss_tx_conns);
1626 conn->ksnc_tx_scheduled = 1;
1627 /* extra ref for scheduler */
1628 ksocknal_conn_addref(conn);
1630 wake_up(&sched->kss_waitq);
1633 spin_unlock_bh(&sched->kss_lock);
1636 static struct ksock_proto *
1637 ksocknal_parse_proto_version(struct ksock_hello_msg *hello)
1641 if (hello->kshm_magic == LNET_PROTO_MAGIC)
1642 version = hello->kshm_version;
1643 else if (hello->kshm_magic == __swab32(LNET_PROTO_MAGIC))
1644 version = __swab32(hello->kshm_version);
1647 #if SOCKNAL_VERSION_DEBUG
1648 if (*ksocknal_tunables.ksnd_protocol == 1)
1651 if (*ksocknal_tunables.ksnd_protocol == 2 &&
1652 version == KSOCK_PROTO_V3)
1655 if (version == KSOCK_PROTO_V2)
1656 return &ksocknal_protocol_v2x;
1658 if (version == KSOCK_PROTO_V3)
1659 return &ksocknal_protocol_v3x;
1664 if (hello->kshm_magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1665 struct lnet_magicversion *hmv = (struct lnet_magicversion *)hello;
1667 BUILD_BUG_ON(sizeof(struct lnet_magicversion) !=
1668 offsetof(struct ksock_hello_msg, kshm_src_nid));
1670 if (hmv->version_major == cpu_to_le16(KSOCK_PROTO_V1_MAJOR) &&
1671 hmv->version_minor == cpu_to_le16(KSOCK_PROTO_V1_MINOR))
1672 return &ksocknal_protocol_v1x;
1679 ksocknal_send_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1680 lnet_nid_t peer_nid, struct ksock_hello_msg *hello)
1682 /* CAVEAT EMPTOR: this byte flips 'ipaddrs' */
1683 struct ksock_net *net = (struct ksock_net *)ni->ni_data;
1685 LASSERT(hello->kshm_nips <= LNET_MAX_INTERFACES);
1687 /* rely on caller to hold a ref on socket so it wouldn't disappear */
1688 LASSERT(conn->ksnc_proto);
1690 hello->kshm_src_nid = ni->ni_nid;
1691 hello->kshm_dst_nid = peer_nid;
1692 hello->kshm_src_pid = the_lnet.ln_pid;
1694 hello->kshm_src_incarnation = net->ksnn_incarnation;
1695 hello->kshm_ctype = conn->ksnc_type;
1697 return conn->ksnc_proto->pro_send_hello(conn, hello);
1701 ksocknal_invert_type(int type)
1704 case SOCKLND_CONN_ANY:
1705 case SOCKLND_CONN_CONTROL:
1707 case SOCKLND_CONN_BULK_IN:
1708 return SOCKLND_CONN_BULK_OUT;
1709 case SOCKLND_CONN_BULK_OUT:
1710 return SOCKLND_CONN_BULK_IN;
1712 return SOCKLND_CONN_NONE;
1717 ksocknal_recv_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1718 struct ksock_hello_msg *hello,
1719 struct lnet_process_id *peerid,
1722 /* Return < 0 fatal error
1724 * EALREADY lost connection race
1725 * EPROTO protocol version mismatch
1727 struct socket *sock = conn->ksnc_sock;
1728 int active = !!conn->ksnc_proto;
1732 struct ksock_proto *proto;
1733 struct lnet_process_id recv_id;
1735 /* socket type set on active connections - not set on passive */
1736 LASSERT(!active == !(conn->ksnc_type != SOCKLND_CONN_NONE));
1738 timeout = active ? *ksocknal_tunables.ksnd_timeout :
1739 lnet_acceptor_timeout();
1741 rc = lnet_sock_read(sock, &hello->kshm_magic,
1742 sizeof(hello->kshm_magic), timeout);
1744 CERROR("Error %d reading HELLO from %pI4h\n",
1745 rc, &conn->ksnc_ipaddr);
1750 if (hello->kshm_magic != LNET_PROTO_MAGIC &&
1751 hello->kshm_magic != __swab32(LNET_PROTO_MAGIC) &&
1752 hello->kshm_magic != le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1753 /* Unexpected magic! */
1754 CERROR("Bad magic(1) %#08x (%#08x expected) from %pI4h\n",
1755 __cpu_to_le32(hello->kshm_magic),
1756 LNET_PROTO_TCP_MAGIC,
1757 &conn->ksnc_ipaddr);
1761 rc = lnet_sock_read(sock, &hello->kshm_version,
1762 sizeof(hello->kshm_version), timeout);
1764 CERROR("Error %d reading HELLO from %pI4h\n",
1765 rc, &conn->ksnc_ipaddr);
1770 proto = ksocknal_parse_proto_version(hello);
1773 /* unknown protocol from peer, tell peer my protocol */
1774 conn->ksnc_proto = &ksocknal_protocol_v3x;
1775 #if SOCKNAL_VERSION_DEBUG
1776 if (*ksocknal_tunables.ksnd_protocol == 2)
1777 conn->ksnc_proto = &ksocknal_protocol_v2x;
1778 else if (*ksocknal_tunables.ksnd_protocol == 1)
1779 conn->ksnc_proto = &ksocknal_protocol_v1x;
1781 hello->kshm_nips = 0;
1782 ksocknal_send_hello(ni, conn, ni->ni_nid, hello);
1785 CERROR("Unknown protocol version (%d.x expected) from %pI4h\n",
1786 conn->ksnc_proto->pro_version,
1787 &conn->ksnc_ipaddr);
1792 proto_match = (conn->ksnc_proto == proto);
1793 conn->ksnc_proto = proto;
1795 /* receive the rest of hello message anyway */
1796 rc = conn->ksnc_proto->pro_recv_hello(conn, hello, timeout);
1798 CERROR("Error %d reading or checking hello from from %pI4h\n",
1799 rc, &conn->ksnc_ipaddr);
1804 *incarnation = hello->kshm_src_incarnation;
1806 if (hello->kshm_src_nid == LNET_NID_ANY) {
1807 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY from %pI4h\n",
1808 &conn->ksnc_ipaddr);
1813 conn->ksnc_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
1814 /* Userspace NAL assigns peer process ID from socket */
1815 recv_id.pid = conn->ksnc_port | LNET_PID_USERFLAG;
1816 recv_id.nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid),
1819 recv_id.nid = hello->kshm_src_nid;
1820 recv_id.pid = hello->kshm_src_pid;
1826 /* peer determines type */
1827 conn->ksnc_type = ksocknal_invert_type(hello->kshm_ctype);
1828 if (conn->ksnc_type == SOCKLND_CONN_NONE) {
1829 CERROR("Unexpected type %d from %s ip %pI4h\n",
1830 hello->kshm_ctype, libcfs_id2str(*peerid),
1831 &conn->ksnc_ipaddr);
1838 if (peerid->pid != recv_id.pid ||
1839 peerid->nid != recv_id.nid) {
1840 LCONSOLE_ERROR_MSG(0x130, "Connected successfully to %s on host %pI4h, but they claimed they were %s; please check your Lustre configuration.\n",
1841 libcfs_id2str(*peerid),
1843 libcfs_id2str(recv_id));
1847 if (hello->kshm_ctype == SOCKLND_CONN_NONE) {
1848 /* Possible protocol mismatch or I lost the connection race */
1849 return proto_match ? EALREADY : EPROTO;
1852 if (ksocknal_invert_type(hello->kshm_ctype) != conn->ksnc_type) {
1853 CERROR("Mismatched types: me %d, %s ip %pI4h %d\n",
1854 conn->ksnc_type, libcfs_id2str(*peerid),
1855 &conn->ksnc_ipaddr, hello->kshm_ctype);
1863 ksocknal_connect(struct ksock_route *route)
1866 struct ksock_peer *peer = route->ksnr_peer;
1869 struct socket *sock;
1870 unsigned long deadline;
1871 int retry_later = 0;
1874 deadline = cfs_time_add(cfs_time_current(),
1875 cfs_time_seconds(*ksocknal_tunables.ksnd_timeout));
1877 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1879 LASSERT(route->ksnr_scheduled);
1880 LASSERT(!route->ksnr_connecting);
1882 route->ksnr_connecting = 1;
1885 wanted = ksocknal_route_mask() & ~route->ksnr_connected;
1888 * stop connecting if peer/route got closed under me, or
1889 * route got connected while queued
1891 if (peer->ksnp_closing || route->ksnr_deleted ||
1897 /* reschedule if peer is connecting to me */
1898 if (peer->ksnp_accepting > 0) {
1900 "peer %s(%d) already connecting to me, retry later.\n",
1901 libcfs_nid2str(peer->ksnp_id.nid),
1902 peer->ksnp_accepting);
1906 if (retry_later) /* needs reschedule */
1909 if (wanted & BIT(SOCKLND_CONN_ANY)) {
1910 type = SOCKLND_CONN_ANY;
1911 } else if (wanted & BIT(SOCKLND_CONN_CONTROL)) {
1912 type = SOCKLND_CONN_CONTROL;
1913 } else if (wanted & BIT(SOCKLND_CONN_BULK_IN)) {
1914 type = SOCKLND_CONN_BULK_IN;
1916 LASSERT(wanted & BIT(SOCKLND_CONN_BULK_OUT));
1917 type = SOCKLND_CONN_BULK_OUT;
1920 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1922 if (cfs_time_aftereq(cfs_time_current(), deadline)) {
1924 lnet_connect_console_error(rc, peer->ksnp_id.nid,
1930 rc = lnet_connect(&sock, peer->ksnp_id.nid,
1931 route->ksnr_myipaddr,
1932 route->ksnr_ipaddr, route->ksnr_port);
1936 rc = ksocknal_create_conn(peer->ksnp_ni, route, sock, type);
1938 lnet_connect_console_error(rc, peer->ksnp_id.nid,
1945 * A +ve RC means I have to retry because I lost the connection
1946 * race or I have to renegotiate protocol version
1950 CDEBUG(D_NET, "peer %s: conn race, retry later.\n",
1951 libcfs_nid2str(peer->ksnp_id.nid));
1953 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1956 route->ksnr_scheduled = 0;
1957 route->ksnr_connecting = 0;
1961 * re-queue for attention; this frees me up to handle
1962 * the peer's incoming connection request
1964 if (rc == EALREADY ||
1965 (!rc && peer->ksnp_accepting > 0)) {
1967 * We want to introduce a delay before next
1968 * attempt to connect if we lost conn race,
1969 * but the race is resolved quickly usually,
1970 * so min_reconnectms should be good heuristic
1972 route->ksnr_retry_interval =
1973 cfs_time_seconds(*ksocknal_tunables.ksnd_min_reconnectms) / 1000;
1974 route->ksnr_timeout = cfs_time_add(cfs_time_current(),
1975 route->ksnr_retry_interval);
1978 ksocknal_launch_connection_locked(route);
1981 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1985 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1987 route->ksnr_scheduled = 0;
1988 route->ksnr_connecting = 0;
1990 /* This is a retry rather than a new connection */
1991 route->ksnr_retry_interval *= 2;
1992 route->ksnr_retry_interval =
1993 max(route->ksnr_retry_interval,
1994 cfs_time_seconds(*ksocknal_tunables.ksnd_min_reconnectms) / 1000);
1995 route->ksnr_retry_interval =
1996 min(route->ksnr_retry_interval,
1997 cfs_time_seconds(*ksocknal_tunables.ksnd_max_reconnectms) / 1000);
1999 LASSERT(route->ksnr_retry_interval);
2000 route->ksnr_timeout = cfs_time_add(cfs_time_current(),
2001 route->ksnr_retry_interval);
2003 if (!list_empty(&peer->ksnp_tx_queue) &&
2004 !peer->ksnp_accepting &&
2005 !ksocknal_find_connecting_route_locked(peer)) {
2006 struct ksock_conn *conn;
2009 * ksnp_tx_queue is queued on a conn on successful
2010 * connection for V1.x and V2.x
2012 if (!list_empty(&peer->ksnp_conns)) {
2013 conn = list_entry(peer->ksnp_conns.next,
2014 struct ksock_conn, ksnc_list);
2015 LASSERT(conn->ksnc_proto == &ksocknal_protocol_v3x);
2019 * take all the blocked packets while I've got the lock and
2022 list_splice_init(&peer->ksnp_tx_queue, &zombies);
2025 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2027 ksocknal_peer_failed(peer);
2028 ksocknal_txlist_done(peer->ksnp_ni, &zombies, 1);
2033 * check whether we need to create more connds.
2034 * It will try to create new thread if it's necessary, @timeout can
2035 * be updated if failed to create, so caller wouldn't keep try while
2036 * running out of resource.
2039 ksocknal_connd_check_start(time64_t sec, long *timeout)
2043 int total = ksocknal_data.ksnd_connd_starting +
2044 ksocknal_data.ksnd_connd_running;
2046 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2047 /* still in initializing */
2051 if (total >= *ksocknal_tunables.ksnd_nconnds_max ||
2052 total > ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV) {
2054 * can't create more connd, or still have enough
2055 * threads to handle more connecting
2060 if (list_empty(&ksocknal_data.ksnd_connd_routes)) {
2061 /* no pending connecting request */
2065 if (sec - ksocknal_data.ksnd_connd_failed_stamp <= 1) {
2066 /* may run out of resource, retry later */
2067 *timeout = cfs_time_seconds(1);
2071 if (ksocknal_data.ksnd_connd_starting > 0) {
2072 /* serialize starting to avoid flood */
2076 ksocknal_data.ksnd_connd_starting_stamp = sec;
2077 ksocknal_data.ksnd_connd_starting++;
2078 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2080 /* NB: total is the next id */
2081 snprintf(name, sizeof(name), "socknal_cd%02d", total);
2082 rc = ksocknal_thread_start(ksocknal_connd, NULL, name);
2084 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2089 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2090 ksocknal_data.ksnd_connd_starting--;
2091 ksocknal_data.ksnd_connd_failed_stamp = ktime_get_real_seconds();
2097 * check whether current thread can exit, it will return 1 if there are too
2098 * many threads and no creating in past 120 seconds.
2099 * Also, this function may update @timeout to make caller come back
2100 * again to recheck these conditions.
2103 ksocknal_connd_check_stop(time64_t sec, long *timeout)
2107 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2108 /* still in initializing */
2112 if (ksocknal_data.ksnd_connd_starting > 0) {
2113 /* in progress of starting new thread */
2117 if (ksocknal_data.ksnd_connd_running <=
2118 *ksocknal_tunables.ksnd_nconnds) { /* can't shrink */
2122 /* created thread in past 120 seconds? */
2123 val = (int)(ksocknal_data.ksnd_connd_starting_stamp +
2124 SOCKNAL_CONND_TIMEOUT - sec);
2126 *timeout = (val > 0) ? cfs_time_seconds(val) :
2127 cfs_time_seconds(SOCKNAL_CONND_TIMEOUT);
2131 /* no creating in past 120 seconds */
2133 return ksocknal_data.ksnd_connd_running >
2134 ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV;
2138 * Go through connd_routes queue looking for a route that we can process
2139 * right now, @timeout_p can be updated if we need to come back later
2141 static struct ksock_route *
2142 ksocknal_connd_get_route_locked(signed long *timeout_p)
2144 struct ksock_route *route;
2147 now = cfs_time_current();
2149 /* connd_routes can contain both pending and ordinary routes */
2150 list_for_each_entry(route, &ksocknal_data.ksnd_connd_routes,
2152 if (!route->ksnr_retry_interval ||
2153 cfs_time_aftereq(now, route->ksnr_timeout))
2156 if (*timeout_p == MAX_SCHEDULE_TIMEOUT ||
2157 (int)*timeout_p > (int)(route->ksnr_timeout - now))
2158 *timeout_p = (int)(route->ksnr_timeout - now);
2165 ksocknal_connd(void *arg)
2167 spinlock_t *connd_lock = &ksocknal_data.ksnd_connd_lock;
2168 struct ksock_connreq *cr;
2169 wait_queue_entry_t wait;
2173 cfs_block_allsigs();
2175 init_waitqueue_entry(&wait, current);
2177 spin_lock_bh(connd_lock);
2179 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2180 ksocknal_data.ksnd_connd_starting--;
2181 ksocknal_data.ksnd_connd_running++;
2183 while (!ksocknal_data.ksnd_shuttingdown) {
2184 struct ksock_route *route = NULL;
2185 time64_t sec = ktime_get_real_seconds();
2186 long timeout = MAX_SCHEDULE_TIMEOUT;
2187 int dropped_lock = 0;
2189 if (ksocknal_connd_check_stop(sec, &timeout)) {
2190 /* wakeup another one to check stop */
2191 wake_up(&ksocknal_data.ksnd_connd_waitq);
2195 if (ksocknal_connd_check_start(sec, &timeout)) {
2196 /* created new thread */
2200 if (!list_empty(&ksocknal_data.ksnd_connd_connreqs)) {
2201 /* Connection accepted by the listener */
2202 cr = list_entry(ksocknal_data.ksnd_connd_connreqs.next,
2203 struct ksock_connreq, ksncr_list);
2205 list_del(&cr->ksncr_list);
2206 spin_unlock_bh(connd_lock);
2209 ksocknal_create_conn(cr->ksncr_ni, NULL,
2210 cr->ksncr_sock, SOCKLND_CONN_NONE);
2211 lnet_ni_decref(cr->ksncr_ni);
2212 LIBCFS_FREE(cr, sizeof(*cr));
2214 spin_lock_bh(connd_lock);
2218 * Only handle an outgoing connection request if there
2219 * is a thread left to handle incoming connections and
2222 if (ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV <
2223 ksocknal_data.ksnd_connd_running) {
2224 route = ksocknal_connd_get_route_locked(&timeout);
2227 list_del(&route->ksnr_connd_list);
2228 ksocknal_data.ksnd_connd_connecting++;
2229 spin_unlock_bh(connd_lock);
2232 if (ksocknal_connect(route)) {
2233 /* consecutive retry */
2234 if (cons_retry++ > SOCKNAL_INSANITY_RECONN) {
2235 CWARN("massive consecutive re-connecting to %pI4h\n",
2236 &route->ksnr_ipaddr);
2243 ksocknal_route_decref(route);
2245 spin_lock_bh(connd_lock);
2246 ksocknal_data.ksnd_connd_connecting--;
2250 if (++nloops < SOCKNAL_RESCHED)
2252 spin_unlock_bh(connd_lock);
2255 spin_lock_bh(connd_lock);
2259 /* Nothing to do for 'timeout' */
2260 set_current_state(TASK_INTERRUPTIBLE);
2261 add_wait_queue_exclusive(&ksocknal_data.ksnd_connd_waitq,
2263 spin_unlock_bh(connd_lock);
2266 schedule_timeout(timeout);
2268 remove_wait_queue(&ksocknal_data.ksnd_connd_waitq, &wait);
2269 spin_lock_bh(connd_lock);
2271 ksocknal_data.ksnd_connd_running--;
2272 spin_unlock_bh(connd_lock);
2274 ksocknal_thread_fini();
2278 static struct ksock_conn *
2279 ksocknal_find_timed_out_conn(struct ksock_peer *peer)
2281 /* We're called with a shared lock on ksnd_global_lock */
2282 struct ksock_conn *conn;
2283 struct list_head *ctmp;
2285 list_for_each(ctmp, &peer->ksnp_conns) {
2288 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
2290 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2291 LASSERT(!conn->ksnc_closing);
2294 * SOCK_ERROR will reset error code of socket in
2295 * some platform (like Darwin8.x)
2297 error = conn->ksnc_sock->sk->sk_err;
2299 ksocknal_conn_addref(conn);
2303 CNETERR("A connection with %s (%pI4h:%d) was reset; it may have rebooted.\n",
2304 libcfs_id2str(peer->ksnp_id),
2309 CNETERR("A connection with %s (%pI4h:%d) timed out; the network or node may be down.\n",
2310 libcfs_id2str(peer->ksnp_id),
2315 CNETERR("An unexpected network error %d occurred with %s (%pI4h:%d\n",
2317 libcfs_id2str(peer->ksnp_id),
2326 if (conn->ksnc_rx_started &&
2327 cfs_time_aftereq(cfs_time_current(),
2328 conn->ksnc_rx_deadline)) {
2329 /* Timed out incomplete incoming message */
2330 ksocknal_conn_addref(conn);
2331 CNETERR("Timeout receiving from %s (%pI4h:%d), state %d wanted %d left %d\n",
2332 libcfs_id2str(peer->ksnp_id),
2335 conn->ksnc_rx_state,
2336 conn->ksnc_rx_nob_wanted,
2337 conn->ksnc_rx_nob_left);
2341 if ((!list_empty(&conn->ksnc_tx_queue) ||
2342 conn->ksnc_sock->sk->sk_wmem_queued) &&
2343 cfs_time_aftereq(cfs_time_current(),
2344 conn->ksnc_tx_deadline)) {
2346 * Timed out messages queued for sending or
2347 * buffered in the socket's send buffer
2349 ksocknal_conn_addref(conn);
2350 CNETERR("Timeout sending data to %s (%pI4h:%d) the network or that node may be down.\n",
2351 libcfs_id2str(peer->ksnp_id),
2362 ksocknal_flush_stale_txs(struct ksock_peer *peer)
2364 struct ksock_tx *tx;
2365 struct ksock_tx *tmp;
2366 LIST_HEAD(stale_txs);
2368 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2370 list_for_each_entry_safe(tx, tmp, &peer->ksnp_tx_queue, tx_list) {
2371 if (!cfs_time_aftereq(cfs_time_current(),
2375 list_del(&tx->tx_list);
2376 list_add_tail(&tx->tx_list, &stale_txs);
2379 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2381 ksocknal_txlist_done(peer->ksnp_ni, &stale_txs, 1);
2385 ksocknal_send_keepalive_locked(struct ksock_peer *peer)
2386 __must_hold(&ksocknal_data.ksnd_global_lock)
2388 struct ksock_sched *sched;
2389 struct ksock_conn *conn;
2390 struct ksock_tx *tx;
2392 /* last_alive will be updated by create_conn */
2393 if (list_empty(&peer->ksnp_conns))
2396 if (peer->ksnp_proto != &ksocknal_protocol_v3x)
2399 if (*ksocknal_tunables.ksnd_keepalive <= 0 ||
2400 time_before(cfs_time_current(),
2401 cfs_time_add(peer->ksnp_last_alive,
2402 cfs_time_seconds(*ksocknal_tunables.ksnd_keepalive))))
2405 if (time_before(cfs_time_current(), peer->ksnp_send_keepalive))
2409 * retry 10 secs later, so we wouldn't put pressure
2410 * on this peer if we failed to send keepalive this time
2412 peer->ksnp_send_keepalive = cfs_time_shift(10);
2414 conn = ksocknal_find_conn_locked(peer, NULL, 1);
2416 sched = conn->ksnc_scheduler;
2418 spin_lock_bh(&sched->kss_lock);
2419 if (!list_empty(&conn->ksnc_tx_queue)) {
2420 spin_unlock_bh(&sched->kss_lock);
2421 /* there is an queued ACK, don't need keepalive */
2425 spin_unlock_bh(&sched->kss_lock);
2428 read_unlock(&ksocknal_data.ksnd_global_lock);
2430 /* cookie = 1 is reserved for keepalive PING */
2431 tx = ksocknal_alloc_tx_noop(1, 1);
2433 read_lock(&ksocknal_data.ksnd_global_lock);
2437 if (!ksocknal_launch_packet(peer->ksnp_ni, tx, peer->ksnp_id)) {
2438 read_lock(&ksocknal_data.ksnd_global_lock);
2442 ksocknal_free_tx(tx);
2443 read_lock(&ksocknal_data.ksnd_global_lock);
2449 ksocknal_check_peer_timeouts(int idx)
2451 struct list_head *peers = &ksocknal_data.ksnd_peers[idx];
2452 struct ksock_peer *peer;
2453 struct ksock_conn *conn;
2454 struct ksock_tx *tx;
2458 * NB. We expect to have a look at all the peers and not find any
2459 * connections to time out, so we just use a shared lock while we
2462 read_lock(&ksocknal_data.ksnd_global_lock);
2464 list_for_each_entry(peer, peers, ksnp_list) {
2465 unsigned long deadline = 0;
2466 struct ksock_tx *tx_stale;
2470 if (ksocknal_send_keepalive_locked(peer)) {
2471 read_unlock(&ksocknal_data.ksnd_global_lock);
2475 conn = ksocknal_find_timed_out_conn(peer);
2478 read_unlock(&ksocknal_data.ksnd_global_lock);
2480 ksocknal_close_conn_and_siblings(conn, -ETIMEDOUT);
2483 * NB we won't find this one again, but we can't
2484 * just proceed with the next peer, since we dropped
2485 * ksnd_global_lock and it might be dead already!
2487 ksocknal_conn_decref(conn);
2492 * we can't process stale txs right here because we're
2493 * holding only shared lock
2495 if (!list_empty(&peer->ksnp_tx_queue)) {
2496 tx = list_entry(peer->ksnp_tx_queue.next,
2497 struct ksock_tx, tx_list);
2499 if (cfs_time_aftereq(cfs_time_current(),
2501 ksocknal_peer_addref(peer);
2502 read_unlock(&ksocknal_data.ksnd_global_lock);
2504 ksocknal_flush_stale_txs(peer);
2506 ksocknal_peer_decref(peer);
2511 if (list_empty(&peer->ksnp_zc_req_list))
2515 spin_lock(&peer->ksnp_lock);
2516 list_for_each_entry(tx, &peer->ksnp_zc_req_list, tx_zc_list) {
2517 if (!cfs_time_aftereq(cfs_time_current(),
2520 /* ignore the TX if connection is being closed */
2521 if (tx->tx_conn->ksnc_closing)
2529 spin_unlock(&peer->ksnp_lock);
2533 deadline = tx_stale->tx_deadline;
2534 resid = tx_stale->tx_resid;
2535 conn = tx_stale->tx_conn;
2536 ksocknal_conn_addref(conn);
2538 spin_unlock(&peer->ksnp_lock);
2539 read_unlock(&ksocknal_data.ksnd_global_lock);
2541 CERROR("Total %d stale ZC_REQs for peer %s detected; the oldest(%p) timed out %ld secs ago, resid: %d, wmem: %d\n",
2542 n, libcfs_nid2str(peer->ksnp_id.nid), tx_stale,
2543 cfs_duration_sec(cfs_time_current() - deadline),
2544 resid, conn->ksnc_sock->sk->sk_wmem_queued);
2546 ksocknal_close_conn_and_siblings(conn, -ETIMEDOUT);
2547 ksocknal_conn_decref(conn);
2551 read_unlock(&ksocknal_data.ksnd_global_lock);
2555 ksocknal_reaper(void *arg)
2557 wait_queue_entry_t wait;
2558 struct ksock_conn *conn;
2559 struct ksock_sched *sched;
2560 struct list_head enomem_conns;
2565 unsigned long deadline = cfs_time_current();
2567 cfs_block_allsigs();
2569 INIT_LIST_HEAD(&enomem_conns);
2570 init_waitqueue_entry(&wait, current);
2572 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2574 while (!ksocknal_data.ksnd_shuttingdown) {
2575 if (!list_empty(&ksocknal_data.ksnd_deathrow_conns)) {
2576 conn = list_entry(ksocknal_data.ksnd_deathrow_conns.next,
2577 struct ksock_conn, ksnc_list);
2578 list_del(&conn->ksnc_list);
2580 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2582 ksocknal_terminate_conn(conn);
2583 ksocknal_conn_decref(conn);
2585 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2589 if (!list_empty(&ksocknal_data.ksnd_zombie_conns)) {
2590 conn = list_entry(ksocknal_data.ksnd_zombie_conns.next,
2591 struct ksock_conn, ksnc_list);
2592 list_del(&conn->ksnc_list);
2594 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2596 ksocknal_destroy_conn(conn);
2598 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2602 if (!list_empty(&ksocknal_data.ksnd_enomem_conns)) {
2603 list_add(&enomem_conns,
2604 &ksocknal_data.ksnd_enomem_conns);
2605 list_del_init(&ksocknal_data.ksnd_enomem_conns);
2608 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2610 /* reschedule all the connections that stalled with ENOMEM... */
2612 while (!list_empty(&enomem_conns)) {
2613 conn = list_entry(enomem_conns.next, struct ksock_conn,
2615 list_del(&conn->ksnc_tx_list);
2617 sched = conn->ksnc_scheduler;
2619 spin_lock_bh(&sched->kss_lock);
2621 LASSERT(conn->ksnc_tx_scheduled);
2622 conn->ksnc_tx_ready = 1;
2623 list_add_tail(&conn->ksnc_tx_list,
2624 &sched->kss_tx_conns);
2625 wake_up(&sched->kss_waitq);
2627 spin_unlock_bh(&sched->kss_lock);
2631 /* careful with the jiffy wrap... */
2632 while ((timeout = cfs_time_sub(deadline,
2633 cfs_time_current())) <= 0) {
2636 int chunk = ksocknal_data.ksnd_peer_hash_size;
2639 * Time to check for timeouts on a few more peers: I do
2640 * checks every 'p' seconds on a proportion of the peer
2641 * table and I need to check every connection 'n' times
2642 * within a timeout interval, to ensure I detect a
2643 * timeout on any connection within (n+1)/n times the
2646 if (*ksocknal_tunables.ksnd_timeout > n * p)
2647 chunk = (chunk * n * p) /
2648 *ksocknal_tunables.ksnd_timeout;
2652 for (i = 0; i < chunk; i++) {
2653 ksocknal_check_peer_timeouts(peer_index);
2654 peer_index = (peer_index + 1) %
2655 ksocknal_data.ksnd_peer_hash_size;
2658 deadline = cfs_time_add(deadline, cfs_time_seconds(p));
2661 if (nenomem_conns) {
2663 * Reduce my timeout if I rescheduled ENOMEM conns.
2664 * This also prevents me getting woken immediately
2665 * if any go back on my enomem list.
2667 timeout = SOCKNAL_ENOMEM_RETRY;
2669 ksocknal_data.ksnd_reaper_waketime =
2670 cfs_time_add(cfs_time_current(), timeout);
2672 set_current_state(TASK_INTERRUPTIBLE);
2673 add_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2675 if (!ksocknal_data.ksnd_shuttingdown &&
2676 list_empty(&ksocknal_data.ksnd_deathrow_conns) &&
2677 list_empty(&ksocknal_data.ksnd_zombie_conns))
2678 schedule_timeout(timeout);
2680 set_current_state(TASK_RUNNING);
2681 remove_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2683 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2686 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2688 ksocknal_thread_fini();