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