Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * NVMe over Fabrics TCP target. |
| 4 | * Copyright (c) 2018 Lightbits Labs. All rights reserved. |
| 5 | */ |
| 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/nvme-tcp.h> |
| 12 | #include <net/sock.h> |
| 13 | #include <net/tcp.h> |
| 14 | #include <linux/inet.h> |
| 15 | #include <linux/llist.h> |
| 16 | #include <crypto/hash.h> |
| 17 | |
| 18 | #include "nvmet.h" |
| 19 | |
| 20 | #define NVMET_TCP_DEF_INLINE_DATA_SIZE (4 * PAGE_SIZE) |
| 21 | |
Wunderlich, Mark | 43cc668 | 2020-01-16 00:46:16 +0000 | [diff] [blame] | 22 | /* Define the socket priority to use for connections were it is desirable |
| 23 | * that the NIC consider performing optimized packet processing or filtering. |
| 24 | * A non-zero value being sufficient to indicate general consideration of any |
| 25 | * possible optimization. Making it a module param allows for alternative |
| 26 | * values that may be unique for some NIC implementations. |
| 27 | */ |
| 28 | static int so_priority; |
| 29 | module_param(so_priority, int, 0644); |
| 30 | MODULE_PARM_DESC(so_priority, "nvmet tcp socket optimize priority"); |
| 31 | |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 32 | #define NVMET_TCP_RECV_BUDGET 8 |
| 33 | #define NVMET_TCP_SEND_BUDGET 8 |
| 34 | #define NVMET_TCP_IO_WORK_BUDGET 64 |
| 35 | |
| 36 | enum nvmet_tcp_send_state { |
| 37 | NVMET_TCP_SEND_DATA_PDU, |
| 38 | NVMET_TCP_SEND_DATA, |
| 39 | NVMET_TCP_SEND_R2T, |
| 40 | NVMET_TCP_SEND_DDGST, |
| 41 | NVMET_TCP_SEND_RESPONSE |
| 42 | }; |
| 43 | |
| 44 | enum nvmet_tcp_recv_state { |
| 45 | NVMET_TCP_RECV_PDU, |
| 46 | NVMET_TCP_RECV_DATA, |
| 47 | NVMET_TCP_RECV_DDGST, |
| 48 | NVMET_TCP_RECV_ERR, |
| 49 | }; |
| 50 | |
| 51 | enum { |
| 52 | NVMET_TCP_F_INIT_FAILED = (1 << 0), |
| 53 | }; |
| 54 | |
| 55 | struct nvmet_tcp_cmd { |
| 56 | struct nvmet_tcp_queue *queue; |
| 57 | struct nvmet_req req; |
| 58 | |
| 59 | struct nvme_tcp_cmd_pdu *cmd_pdu; |
| 60 | struct nvme_tcp_rsp_pdu *rsp_pdu; |
| 61 | struct nvme_tcp_data_pdu *data_pdu; |
| 62 | struct nvme_tcp_r2t_pdu *r2t_pdu; |
| 63 | |
| 64 | u32 rbytes_done; |
| 65 | u32 wbytes_done; |
| 66 | |
| 67 | u32 pdu_len; |
| 68 | u32 pdu_recv; |
| 69 | int sg_idx; |
| 70 | int nr_mapped; |
| 71 | struct msghdr recv_msg; |
| 72 | struct kvec *iov; |
| 73 | u32 flags; |
| 74 | |
| 75 | struct list_head entry; |
| 76 | struct llist_node lentry; |
| 77 | |
| 78 | /* send state */ |
| 79 | u32 offset; |
| 80 | struct scatterlist *cur_sg; |
| 81 | enum nvmet_tcp_send_state state; |
| 82 | |
| 83 | __le32 exp_ddgst; |
| 84 | __le32 recv_ddgst; |
| 85 | }; |
| 86 | |
| 87 | enum nvmet_tcp_queue_state { |
| 88 | NVMET_TCP_Q_CONNECTING, |
| 89 | NVMET_TCP_Q_LIVE, |
| 90 | NVMET_TCP_Q_DISCONNECTING, |
| 91 | }; |
| 92 | |
| 93 | struct nvmet_tcp_queue { |
| 94 | struct socket *sock; |
| 95 | struct nvmet_tcp_port *port; |
| 96 | struct work_struct io_work; |
| 97 | int cpu; |
| 98 | struct nvmet_cq nvme_cq; |
| 99 | struct nvmet_sq nvme_sq; |
| 100 | |
| 101 | /* send state */ |
| 102 | struct nvmet_tcp_cmd *cmds; |
| 103 | unsigned int nr_cmds; |
| 104 | struct list_head free_list; |
| 105 | struct llist_head resp_list; |
| 106 | struct list_head resp_send_list; |
| 107 | int send_list_len; |
| 108 | struct nvmet_tcp_cmd *snd_cmd; |
| 109 | |
| 110 | /* recv state */ |
| 111 | int offset; |
| 112 | int left; |
| 113 | enum nvmet_tcp_recv_state rcv_state; |
| 114 | struct nvmet_tcp_cmd *cmd; |
| 115 | union nvme_tcp_pdu pdu; |
| 116 | |
| 117 | /* digest state */ |
| 118 | bool hdr_digest; |
| 119 | bool data_digest; |
| 120 | struct ahash_request *snd_hash; |
| 121 | struct ahash_request *rcv_hash; |
| 122 | |
| 123 | spinlock_t state_lock; |
| 124 | enum nvmet_tcp_queue_state state; |
| 125 | |
| 126 | struct sockaddr_storage sockaddr; |
| 127 | struct sockaddr_storage sockaddr_peer; |
| 128 | struct work_struct release_work; |
| 129 | |
| 130 | int idx; |
| 131 | struct list_head queue_list; |
| 132 | |
| 133 | struct nvmet_tcp_cmd connect; |
| 134 | |
| 135 | struct page_frag_cache pf_cache; |
| 136 | |
| 137 | void (*data_ready)(struct sock *); |
| 138 | void (*state_change)(struct sock *); |
| 139 | void (*write_space)(struct sock *); |
| 140 | }; |
| 141 | |
| 142 | struct nvmet_tcp_port { |
| 143 | struct socket *sock; |
| 144 | struct work_struct accept_work; |
| 145 | struct nvmet_port *nport; |
| 146 | struct sockaddr_storage addr; |
| 147 | int last_cpu; |
| 148 | void (*data_ready)(struct sock *); |
| 149 | }; |
| 150 | |
| 151 | static DEFINE_IDA(nvmet_tcp_queue_ida); |
| 152 | static LIST_HEAD(nvmet_tcp_queue_list); |
| 153 | static DEFINE_MUTEX(nvmet_tcp_queue_mutex); |
| 154 | |
| 155 | static struct workqueue_struct *nvmet_tcp_wq; |
| 156 | static struct nvmet_fabrics_ops nvmet_tcp_ops; |
| 157 | static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c); |
| 158 | static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd); |
| 159 | |
| 160 | static inline u16 nvmet_tcp_cmd_tag(struct nvmet_tcp_queue *queue, |
| 161 | struct nvmet_tcp_cmd *cmd) |
| 162 | { |
| 163 | return cmd - queue->cmds; |
| 164 | } |
| 165 | |
| 166 | static inline bool nvmet_tcp_has_data_in(struct nvmet_tcp_cmd *cmd) |
| 167 | { |
| 168 | return nvme_is_write(cmd->req.cmd) && |
| 169 | cmd->rbytes_done < cmd->req.transfer_len; |
| 170 | } |
| 171 | |
| 172 | static inline bool nvmet_tcp_need_data_in(struct nvmet_tcp_cmd *cmd) |
| 173 | { |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 174 | return nvmet_tcp_has_data_in(cmd) && !cmd->req.cqe->status; |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static inline bool nvmet_tcp_need_data_out(struct nvmet_tcp_cmd *cmd) |
| 178 | { |
| 179 | return !nvme_is_write(cmd->req.cmd) && |
| 180 | cmd->req.transfer_len > 0 && |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 181 | !cmd->req.cqe->status; |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static inline bool nvmet_tcp_has_inline_data(struct nvmet_tcp_cmd *cmd) |
| 185 | { |
| 186 | return nvme_is_write(cmd->req.cmd) && cmd->pdu_len && |
| 187 | !cmd->rbytes_done; |
| 188 | } |
| 189 | |
| 190 | static inline struct nvmet_tcp_cmd * |
| 191 | nvmet_tcp_get_cmd(struct nvmet_tcp_queue *queue) |
| 192 | { |
| 193 | struct nvmet_tcp_cmd *cmd; |
| 194 | |
| 195 | cmd = list_first_entry_or_null(&queue->free_list, |
| 196 | struct nvmet_tcp_cmd, entry); |
| 197 | if (!cmd) |
| 198 | return NULL; |
| 199 | list_del_init(&cmd->entry); |
| 200 | |
| 201 | cmd->rbytes_done = cmd->wbytes_done = 0; |
| 202 | cmd->pdu_len = 0; |
| 203 | cmd->pdu_recv = 0; |
| 204 | cmd->iov = NULL; |
| 205 | cmd->flags = 0; |
| 206 | return cmd; |
| 207 | } |
| 208 | |
| 209 | static inline void nvmet_tcp_put_cmd(struct nvmet_tcp_cmd *cmd) |
| 210 | { |
| 211 | if (unlikely(cmd == &cmd->queue->connect)) |
| 212 | return; |
| 213 | |
| 214 | list_add_tail(&cmd->entry, &cmd->queue->free_list); |
| 215 | } |
| 216 | |
| 217 | static inline u8 nvmet_tcp_hdgst_len(struct nvmet_tcp_queue *queue) |
| 218 | { |
| 219 | return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0; |
| 220 | } |
| 221 | |
| 222 | static inline u8 nvmet_tcp_ddgst_len(struct nvmet_tcp_queue *queue) |
| 223 | { |
| 224 | return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0; |
| 225 | } |
| 226 | |
| 227 | static inline void nvmet_tcp_hdgst(struct ahash_request *hash, |
| 228 | void *pdu, size_t len) |
| 229 | { |
| 230 | struct scatterlist sg; |
| 231 | |
| 232 | sg_init_one(&sg, pdu, len); |
| 233 | ahash_request_set_crypt(hash, &sg, pdu + len, len); |
| 234 | crypto_ahash_digest(hash); |
| 235 | } |
| 236 | |
| 237 | static int nvmet_tcp_verify_hdgst(struct nvmet_tcp_queue *queue, |
| 238 | void *pdu, size_t len) |
| 239 | { |
| 240 | struct nvme_tcp_hdr *hdr = pdu; |
| 241 | __le32 recv_digest; |
| 242 | __le32 exp_digest; |
| 243 | |
| 244 | if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) { |
| 245 | pr_err("queue %d: header digest enabled but no header digest\n", |
| 246 | queue->idx); |
| 247 | return -EPROTO; |
| 248 | } |
| 249 | |
| 250 | recv_digest = *(__le32 *)(pdu + hdr->hlen); |
| 251 | nvmet_tcp_hdgst(queue->rcv_hash, pdu, len); |
| 252 | exp_digest = *(__le32 *)(pdu + hdr->hlen); |
| 253 | if (recv_digest != exp_digest) { |
| 254 | pr_err("queue %d: header digest error: recv %#x expected %#x\n", |
| 255 | queue->idx, le32_to_cpu(recv_digest), |
| 256 | le32_to_cpu(exp_digest)); |
| 257 | return -EPROTO; |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int nvmet_tcp_check_ddgst(struct nvmet_tcp_queue *queue, void *pdu) |
| 264 | { |
| 265 | struct nvme_tcp_hdr *hdr = pdu; |
| 266 | u8 digest_len = nvmet_tcp_hdgst_len(queue); |
| 267 | u32 len; |
| 268 | |
| 269 | len = le32_to_cpu(hdr->plen) - hdr->hlen - |
| 270 | (hdr->flags & NVME_TCP_F_HDGST ? digest_len : 0); |
| 271 | |
| 272 | if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) { |
| 273 | pr_err("queue %d: data digest flag is cleared\n", queue->idx); |
| 274 | return -EPROTO; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static void nvmet_tcp_unmap_pdu_iovec(struct nvmet_tcp_cmd *cmd) |
| 281 | { |
| 282 | struct scatterlist *sg; |
| 283 | int i; |
| 284 | |
| 285 | sg = &cmd->req.sg[cmd->sg_idx]; |
| 286 | |
| 287 | for (i = 0; i < cmd->nr_mapped; i++) |
| 288 | kunmap(sg_page(&sg[i])); |
| 289 | } |
| 290 | |
| 291 | static void nvmet_tcp_map_pdu_iovec(struct nvmet_tcp_cmd *cmd) |
| 292 | { |
| 293 | struct kvec *iov = cmd->iov; |
| 294 | struct scatterlist *sg; |
| 295 | u32 length, offset, sg_offset; |
| 296 | |
| 297 | length = cmd->pdu_len; |
| 298 | cmd->nr_mapped = DIV_ROUND_UP(length, PAGE_SIZE); |
| 299 | offset = cmd->rbytes_done; |
| 300 | cmd->sg_idx = DIV_ROUND_UP(offset, PAGE_SIZE); |
| 301 | sg_offset = offset % PAGE_SIZE; |
| 302 | sg = &cmd->req.sg[cmd->sg_idx]; |
| 303 | |
| 304 | while (length) { |
| 305 | u32 iov_len = min_t(u32, length, sg->length - sg_offset); |
| 306 | |
| 307 | iov->iov_base = kmap(sg_page(sg)) + sg->offset + sg_offset; |
| 308 | iov->iov_len = iov_len; |
| 309 | |
| 310 | length -= iov_len; |
| 311 | sg = sg_next(sg); |
| 312 | iov++; |
| 313 | } |
| 314 | |
| 315 | iov_iter_kvec(&cmd->recv_msg.msg_iter, READ, cmd->iov, |
| 316 | cmd->nr_mapped, cmd->pdu_len); |
| 317 | } |
| 318 | |
| 319 | static void nvmet_tcp_fatal_error(struct nvmet_tcp_queue *queue) |
| 320 | { |
| 321 | queue->rcv_state = NVMET_TCP_RECV_ERR; |
| 322 | if (queue->nvme_sq.ctrl) |
| 323 | nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl); |
| 324 | else |
| 325 | kernel_sock_shutdown(queue->sock, SHUT_RDWR); |
| 326 | } |
| 327 | |
| 328 | static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd) |
| 329 | { |
| 330 | struct nvme_sgl_desc *sgl = &cmd->req.cmd->common.dptr.sgl; |
| 331 | u32 len = le32_to_cpu(sgl->length); |
| 332 | |
Logan Gunthorpe | e0bace7 | 2019-10-23 10:35:39 -0600 | [diff] [blame] | 333 | if (!len) |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 334 | return 0; |
| 335 | |
| 336 | if (sgl->type == ((NVME_SGL_FMT_DATA_DESC << 4) | |
| 337 | NVME_SGL_FMT_OFFSET)) { |
| 338 | if (!nvme_is_write(cmd->req.cmd)) |
| 339 | return NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
| 340 | |
| 341 | if (len > cmd->req.port->inline_data_size) |
| 342 | return NVME_SC_SGL_INVALID_OFFSET | NVME_SC_DNR; |
| 343 | cmd->pdu_len = len; |
| 344 | } |
| 345 | cmd->req.transfer_len += len; |
| 346 | |
| 347 | cmd->req.sg = sgl_alloc(len, GFP_KERNEL, &cmd->req.sg_cnt); |
| 348 | if (!cmd->req.sg) |
| 349 | return NVME_SC_INTERNAL; |
| 350 | cmd->cur_sg = cmd->req.sg; |
| 351 | |
| 352 | if (nvmet_tcp_has_data_in(cmd)) { |
| 353 | cmd->iov = kmalloc_array(cmd->req.sg_cnt, |
| 354 | sizeof(*cmd->iov), GFP_KERNEL); |
| 355 | if (!cmd->iov) |
| 356 | goto err; |
| 357 | } |
| 358 | |
| 359 | return 0; |
| 360 | err: |
Sagi Grimberg | 30f27d5 | 2019-09-13 10:36:40 -0700 | [diff] [blame] | 361 | sgl_free(cmd->req.sg); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 362 | return NVME_SC_INTERNAL; |
| 363 | } |
| 364 | |
| 365 | static void nvmet_tcp_ddgst(struct ahash_request *hash, |
| 366 | struct nvmet_tcp_cmd *cmd) |
| 367 | { |
| 368 | ahash_request_set_crypt(hash, cmd->req.sg, |
| 369 | (void *)&cmd->exp_ddgst, cmd->req.transfer_len); |
| 370 | crypto_ahash_digest(hash); |
| 371 | } |
| 372 | |
| 373 | static void nvmet_setup_c2h_data_pdu(struct nvmet_tcp_cmd *cmd) |
| 374 | { |
| 375 | struct nvme_tcp_data_pdu *pdu = cmd->data_pdu; |
| 376 | struct nvmet_tcp_queue *queue = cmd->queue; |
| 377 | u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue); |
| 378 | u8 ddgst = nvmet_tcp_ddgst_len(cmd->queue); |
| 379 | |
| 380 | cmd->offset = 0; |
| 381 | cmd->state = NVMET_TCP_SEND_DATA_PDU; |
| 382 | |
| 383 | pdu->hdr.type = nvme_tcp_c2h_data; |
Sagi Grimberg | 7058329 | 2019-03-08 15:41:21 -0800 | [diff] [blame] | 384 | pdu->hdr.flags = NVME_TCP_F_DATA_LAST | (queue->nvme_sq.sqhd_disabled ? |
| 385 | NVME_TCP_F_DATA_SUCCESS : 0); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 386 | pdu->hdr.hlen = sizeof(*pdu); |
| 387 | pdu->hdr.pdo = pdu->hdr.hlen + hdgst; |
| 388 | pdu->hdr.plen = |
| 389 | cpu_to_le32(pdu->hdr.hlen + hdgst + |
| 390 | cmd->req.transfer_len + ddgst); |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 391 | pdu->command_id = cmd->req.cqe->command_id; |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 392 | pdu->data_length = cpu_to_le32(cmd->req.transfer_len); |
| 393 | pdu->data_offset = cpu_to_le32(cmd->wbytes_done); |
| 394 | |
| 395 | if (queue->data_digest) { |
| 396 | pdu->hdr.flags |= NVME_TCP_F_DDGST; |
| 397 | nvmet_tcp_ddgst(queue->snd_hash, cmd); |
| 398 | } |
| 399 | |
| 400 | if (cmd->queue->hdr_digest) { |
| 401 | pdu->hdr.flags |= NVME_TCP_F_HDGST; |
| 402 | nvmet_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu)); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | static void nvmet_setup_r2t_pdu(struct nvmet_tcp_cmd *cmd) |
| 407 | { |
| 408 | struct nvme_tcp_r2t_pdu *pdu = cmd->r2t_pdu; |
| 409 | struct nvmet_tcp_queue *queue = cmd->queue; |
| 410 | u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue); |
| 411 | |
| 412 | cmd->offset = 0; |
| 413 | cmd->state = NVMET_TCP_SEND_R2T; |
| 414 | |
| 415 | pdu->hdr.type = nvme_tcp_r2t; |
| 416 | pdu->hdr.flags = 0; |
| 417 | pdu->hdr.hlen = sizeof(*pdu); |
| 418 | pdu->hdr.pdo = 0; |
| 419 | pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst); |
| 420 | |
| 421 | pdu->command_id = cmd->req.cmd->common.command_id; |
| 422 | pdu->ttag = nvmet_tcp_cmd_tag(cmd->queue, cmd); |
| 423 | pdu->r2t_length = cpu_to_le32(cmd->req.transfer_len - cmd->rbytes_done); |
| 424 | pdu->r2t_offset = cpu_to_le32(cmd->rbytes_done); |
| 425 | if (cmd->queue->hdr_digest) { |
| 426 | pdu->hdr.flags |= NVME_TCP_F_HDGST; |
| 427 | nvmet_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu)); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | static void nvmet_setup_response_pdu(struct nvmet_tcp_cmd *cmd) |
| 432 | { |
| 433 | struct nvme_tcp_rsp_pdu *pdu = cmd->rsp_pdu; |
| 434 | struct nvmet_tcp_queue *queue = cmd->queue; |
| 435 | u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue); |
| 436 | |
| 437 | cmd->offset = 0; |
| 438 | cmd->state = NVMET_TCP_SEND_RESPONSE; |
| 439 | |
| 440 | pdu->hdr.type = nvme_tcp_rsp; |
| 441 | pdu->hdr.flags = 0; |
| 442 | pdu->hdr.hlen = sizeof(*pdu); |
| 443 | pdu->hdr.pdo = 0; |
| 444 | pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst); |
| 445 | if (cmd->queue->hdr_digest) { |
| 446 | pdu->hdr.flags |= NVME_TCP_F_HDGST; |
| 447 | nvmet_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu)); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | static void nvmet_tcp_process_resp_list(struct nvmet_tcp_queue *queue) |
| 452 | { |
| 453 | struct llist_node *node; |
| 454 | |
| 455 | node = llist_del_all(&queue->resp_list); |
| 456 | if (!node) |
| 457 | return; |
| 458 | |
| 459 | while (node) { |
| 460 | struct nvmet_tcp_cmd *cmd = llist_entry(node, |
| 461 | struct nvmet_tcp_cmd, lentry); |
| 462 | |
| 463 | list_add(&cmd->entry, &queue->resp_send_list); |
| 464 | node = node->next; |
| 465 | queue->send_list_len++; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | static struct nvmet_tcp_cmd *nvmet_tcp_fetch_cmd(struct nvmet_tcp_queue *queue) |
| 470 | { |
| 471 | queue->snd_cmd = list_first_entry_or_null(&queue->resp_send_list, |
| 472 | struct nvmet_tcp_cmd, entry); |
| 473 | if (!queue->snd_cmd) { |
| 474 | nvmet_tcp_process_resp_list(queue); |
| 475 | queue->snd_cmd = |
| 476 | list_first_entry_or_null(&queue->resp_send_list, |
| 477 | struct nvmet_tcp_cmd, entry); |
| 478 | if (unlikely(!queue->snd_cmd)) |
| 479 | return NULL; |
| 480 | } |
| 481 | |
| 482 | list_del_init(&queue->snd_cmd->entry); |
| 483 | queue->send_list_len--; |
| 484 | |
| 485 | if (nvmet_tcp_need_data_out(queue->snd_cmd)) |
| 486 | nvmet_setup_c2h_data_pdu(queue->snd_cmd); |
| 487 | else if (nvmet_tcp_need_data_in(queue->snd_cmd)) |
| 488 | nvmet_setup_r2t_pdu(queue->snd_cmd); |
| 489 | else |
| 490 | nvmet_setup_response_pdu(queue->snd_cmd); |
| 491 | |
| 492 | return queue->snd_cmd; |
| 493 | } |
| 494 | |
| 495 | static void nvmet_tcp_queue_response(struct nvmet_req *req) |
| 496 | { |
| 497 | struct nvmet_tcp_cmd *cmd = |
| 498 | container_of(req, struct nvmet_tcp_cmd, req); |
| 499 | struct nvmet_tcp_queue *queue = cmd->queue; |
| 500 | |
| 501 | llist_add(&cmd->lentry, &queue->resp_list); |
| 502 | queue_work_on(cmd->queue->cpu, nvmet_tcp_wq, &cmd->queue->io_work); |
| 503 | } |
| 504 | |
| 505 | static int nvmet_try_send_data_pdu(struct nvmet_tcp_cmd *cmd) |
| 506 | { |
| 507 | u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue); |
| 508 | int left = sizeof(*cmd->data_pdu) - cmd->offset + hdgst; |
| 509 | int ret; |
| 510 | |
| 511 | ret = kernel_sendpage(cmd->queue->sock, virt_to_page(cmd->data_pdu), |
| 512 | offset_in_page(cmd->data_pdu) + cmd->offset, |
| 513 | left, MSG_DONTWAIT | MSG_MORE); |
| 514 | if (ret <= 0) |
| 515 | return ret; |
| 516 | |
| 517 | cmd->offset += ret; |
| 518 | left -= ret; |
| 519 | |
| 520 | if (left) |
| 521 | return -EAGAIN; |
| 522 | |
| 523 | cmd->state = NVMET_TCP_SEND_DATA; |
| 524 | cmd->offset = 0; |
| 525 | return 1; |
| 526 | } |
| 527 | |
Sagi Grimberg | 98fd5c7 | 2020-03-12 16:06:38 -0700 | [diff] [blame] | 528 | static int nvmet_try_send_data(struct nvmet_tcp_cmd *cmd, bool last_in_batch) |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 529 | { |
| 530 | struct nvmet_tcp_queue *queue = cmd->queue; |
| 531 | int ret; |
| 532 | |
| 533 | while (cmd->cur_sg) { |
| 534 | struct page *page = sg_page(cmd->cur_sg); |
| 535 | u32 left = cmd->cur_sg->length - cmd->offset; |
Sagi Grimberg | 98fd5c7 | 2020-03-12 16:06:38 -0700 | [diff] [blame] | 536 | int flags = MSG_DONTWAIT; |
| 537 | |
| 538 | if ((!last_in_batch && cmd->queue->send_list_len) || |
| 539 | cmd->wbytes_done + left < cmd->req.transfer_len || |
| 540 | queue->data_digest || !queue->nvme_sq.sqhd_disabled) |
| 541 | flags |= MSG_MORE; |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 542 | |
| 543 | ret = kernel_sendpage(cmd->queue->sock, page, cmd->offset, |
Sagi Grimberg | 98fd5c7 | 2020-03-12 16:06:38 -0700 | [diff] [blame] | 544 | left, flags); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 545 | if (ret <= 0) |
| 546 | return ret; |
| 547 | |
| 548 | cmd->offset += ret; |
| 549 | cmd->wbytes_done += ret; |
| 550 | |
| 551 | /* Done with sg?*/ |
| 552 | if (cmd->offset == cmd->cur_sg->length) { |
| 553 | cmd->cur_sg = sg_next(cmd->cur_sg); |
| 554 | cmd->offset = 0; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if (queue->data_digest) { |
| 559 | cmd->state = NVMET_TCP_SEND_DDGST; |
| 560 | cmd->offset = 0; |
| 561 | } else { |
Sagi Grimberg | 7058329 | 2019-03-08 15:41:21 -0800 | [diff] [blame] | 562 | if (queue->nvme_sq.sqhd_disabled) { |
| 563 | cmd->queue->snd_cmd = NULL; |
| 564 | nvmet_tcp_put_cmd(cmd); |
| 565 | } else { |
| 566 | nvmet_setup_response_pdu(cmd); |
| 567 | } |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 568 | } |
Sagi Grimberg | 7058329 | 2019-03-08 15:41:21 -0800 | [diff] [blame] | 569 | |
| 570 | if (queue->nvme_sq.sqhd_disabled) { |
| 571 | kfree(cmd->iov); |
Sagi Grimberg | 30f27d5 | 2019-09-13 10:36:40 -0700 | [diff] [blame] | 572 | sgl_free(cmd->req.sg); |
Sagi Grimberg | 7058329 | 2019-03-08 15:41:21 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 575 | return 1; |
| 576 | |
| 577 | } |
| 578 | |
| 579 | static int nvmet_try_send_response(struct nvmet_tcp_cmd *cmd, |
| 580 | bool last_in_batch) |
| 581 | { |
| 582 | u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue); |
| 583 | int left = sizeof(*cmd->rsp_pdu) - cmd->offset + hdgst; |
| 584 | int flags = MSG_DONTWAIT; |
| 585 | int ret; |
| 586 | |
| 587 | if (!last_in_batch && cmd->queue->send_list_len) |
| 588 | flags |= MSG_MORE; |
| 589 | else |
| 590 | flags |= MSG_EOR; |
| 591 | |
| 592 | ret = kernel_sendpage(cmd->queue->sock, virt_to_page(cmd->rsp_pdu), |
| 593 | offset_in_page(cmd->rsp_pdu) + cmd->offset, left, flags); |
| 594 | if (ret <= 0) |
| 595 | return ret; |
| 596 | cmd->offset += ret; |
| 597 | left -= ret; |
| 598 | |
| 599 | if (left) |
| 600 | return -EAGAIN; |
| 601 | |
| 602 | kfree(cmd->iov); |
Sagi Grimberg | 30f27d5 | 2019-09-13 10:36:40 -0700 | [diff] [blame] | 603 | sgl_free(cmd->req.sg); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 604 | cmd->queue->snd_cmd = NULL; |
| 605 | nvmet_tcp_put_cmd(cmd); |
| 606 | return 1; |
| 607 | } |
| 608 | |
| 609 | static int nvmet_try_send_r2t(struct nvmet_tcp_cmd *cmd, bool last_in_batch) |
| 610 | { |
| 611 | u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue); |
| 612 | int left = sizeof(*cmd->r2t_pdu) - cmd->offset + hdgst; |
| 613 | int flags = MSG_DONTWAIT; |
| 614 | int ret; |
| 615 | |
| 616 | if (!last_in_batch && cmd->queue->send_list_len) |
| 617 | flags |= MSG_MORE; |
| 618 | else |
| 619 | flags |= MSG_EOR; |
| 620 | |
| 621 | ret = kernel_sendpage(cmd->queue->sock, virt_to_page(cmd->r2t_pdu), |
| 622 | offset_in_page(cmd->r2t_pdu) + cmd->offset, left, flags); |
| 623 | if (ret <= 0) |
| 624 | return ret; |
| 625 | cmd->offset += ret; |
| 626 | left -= ret; |
| 627 | |
| 628 | if (left) |
| 629 | return -EAGAIN; |
| 630 | |
| 631 | cmd->queue->snd_cmd = NULL; |
| 632 | return 1; |
| 633 | } |
| 634 | |
Sagi Grimberg | e90d172 | 2020-03-12 16:06:39 -0700 | [diff] [blame] | 635 | static int nvmet_try_send_ddgst(struct nvmet_tcp_cmd *cmd, bool last_in_batch) |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 636 | { |
| 637 | struct nvmet_tcp_queue *queue = cmd->queue; |
| 638 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT }; |
| 639 | struct kvec iov = { |
| 640 | .iov_base = &cmd->exp_ddgst + cmd->offset, |
| 641 | .iov_len = NVME_TCP_DIGEST_LENGTH - cmd->offset |
| 642 | }; |
| 643 | int ret; |
| 644 | |
Sagi Grimberg | e90d172 | 2020-03-12 16:06:39 -0700 | [diff] [blame] | 645 | if (!last_in_batch && cmd->queue->send_list_len) |
| 646 | msg.msg_flags |= MSG_MORE; |
| 647 | |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 648 | ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len); |
| 649 | if (unlikely(ret <= 0)) |
| 650 | return ret; |
| 651 | |
| 652 | cmd->offset += ret; |
Sagi Grimberg | 7058329 | 2019-03-08 15:41:21 -0800 | [diff] [blame] | 653 | |
| 654 | if (queue->nvme_sq.sqhd_disabled) { |
| 655 | cmd->queue->snd_cmd = NULL; |
| 656 | nvmet_tcp_put_cmd(cmd); |
| 657 | } else { |
| 658 | nvmet_setup_response_pdu(cmd); |
| 659 | } |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 660 | return 1; |
| 661 | } |
| 662 | |
| 663 | static int nvmet_tcp_try_send_one(struct nvmet_tcp_queue *queue, |
| 664 | bool last_in_batch) |
| 665 | { |
| 666 | struct nvmet_tcp_cmd *cmd = queue->snd_cmd; |
| 667 | int ret = 0; |
| 668 | |
| 669 | if (!cmd || queue->state == NVMET_TCP_Q_DISCONNECTING) { |
| 670 | cmd = nvmet_tcp_fetch_cmd(queue); |
| 671 | if (unlikely(!cmd)) |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | if (cmd->state == NVMET_TCP_SEND_DATA_PDU) { |
| 676 | ret = nvmet_try_send_data_pdu(cmd); |
| 677 | if (ret <= 0) |
| 678 | goto done_send; |
| 679 | } |
| 680 | |
| 681 | if (cmd->state == NVMET_TCP_SEND_DATA) { |
Sagi Grimberg | 98fd5c7 | 2020-03-12 16:06:38 -0700 | [diff] [blame] | 682 | ret = nvmet_try_send_data(cmd, last_in_batch); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 683 | if (ret <= 0) |
| 684 | goto done_send; |
| 685 | } |
| 686 | |
| 687 | if (cmd->state == NVMET_TCP_SEND_DDGST) { |
Sagi Grimberg | e90d172 | 2020-03-12 16:06:39 -0700 | [diff] [blame] | 688 | ret = nvmet_try_send_ddgst(cmd, last_in_batch); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 689 | if (ret <= 0) |
| 690 | goto done_send; |
| 691 | } |
| 692 | |
| 693 | if (cmd->state == NVMET_TCP_SEND_R2T) { |
| 694 | ret = nvmet_try_send_r2t(cmd, last_in_batch); |
| 695 | if (ret <= 0) |
| 696 | goto done_send; |
| 697 | } |
| 698 | |
| 699 | if (cmd->state == NVMET_TCP_SEND_RESPONSE) |
| 700 | ret = nvmet_try_send_response(cmd, last_in_batch); |
| 701 | |
| 702 | done_send: |
| 703 | if (ret < 0) { |
| 704 | if (ret == -EAGAIN) |
| 705 | return 0; |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | return 1; |
| 710 | } |
| 711 | |
| 712 | static int nvmet_tcp_try_send(struct nvmet_tcp_queue *queue, |
| 713 | int budget, int *sends) |
| 714 | { |
| 715 | int i, ret = 0; |
| 716 | |
| 717 | for (i = 0; i < budget; i++) { |
| 718 | ret = nvmet_tcp_try_send_one(queue, i == budget - 1); |
| 719 | if (ret <= 0) |
| 720 | break; |
| 721 | (*sends)++; |
| 722 | } |
| 723 | |
| 724 | return ret; |
| 725 | } |
| 726 | |
| 727 | static void nvmet_prepare_receive_pdu(struct nvmet_tcp_queue *queue) |
| 728 | { |
| 729 | queue->offset = 0; |
| 730 | queue->left = sizeof(struct nvme_tcp_hdr); |
| 731 | queue->cmd = NULL; |
| 732 | queue->rcv_state = NVMET_TCP_RECV_PDU; |
| 733 | } |
| 734 | |
| 735 | static void nvmet_tcp_free_crypto(struct nvmet_tcp_queue *queue) |
| 736 | { |
| 737 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash); |
| 738 | |
| 739 | ahash_request_free(queue->rcv_hash); |
| 740 | ahash_request_free(queue->snd_hash); |
| 741 | crypto_free_ahash(tfm); |
| 742 | } |
| 743 | |
| 744 | static int nvmet_tcp_alloc_crypto(struct nvmet_tcp_queue *queue) |
| 745 | { |
| 746 | struct crypto_ahash *tfm; |
| 747 | |
| 748 | tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC); |
| 749 | if (IS_ERR(tfm)) |
| 750 | return PTR_ERR(tfm); |
| 751 | |
| 752 | queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL); |
| 753 | if (!queue->snd_hash) |
| 754 | goto free_tfm; |
| 755 | ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL); |
| 756 | |
| 757 | queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL); |
| 758 | if (!queue->rcv_hash) |
| 759 | goto free_snd_hash; |
| 760 | ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL); |
| 761 | |
| 762 | return 0; |
| 763 | free_snd_hash: |
| 764 | ahash_request_free(queue->snd_hash); |
| 765 | free_tfm: |
| 766 | crypto_free_ahash(tfm); |
| 767 | return -ENOMEM; |
| 768 | } |
| 769 | |
| 770 | |
| 771 | static int nvmet_tcp_handle_icreq(struct nvmet_tcp_queue *queue) |
| 772 | { |
| 773 | struct nvme_tcp_icreq_pdu *icreq = &queue->pdu.icreq; |
| 774 | struct nvme_tcp_icresp_pdu *icresp = &queue->pdu.icresp; |
| 775 | struct msghdr msg = {}; |
| 776 | struct kvec iov; |
| 777 | int ret; |
| 778 | |
| 779 | if (le32_to_cpu(icreq->hdr.plen) != sizeof(struct nvme_tcp_icreq_pdu)) { |
| 780 | pr_err("bad nvme-tcp pdu length (%d)\n", |
| 781 | le32_to_cpu(icreq->hdr.plen)); |
| 782 | nvmet_tcp_fatal_error(queue); |
| 783 | } |
| 784 | |
| 785 | if (icreq->pfv != NVME_TCP_PFV_1_0) { |
| 786 | pr_err("queue %d: bad pfv %d\n", queue->idx, icreq->pfv); |
| 787 | return -EPROTO; |
| 788 | } |
| 789 | |
| 790 | if (icreq->hpda != 0) { |
| 791 | pr_err("queue %d: unsupported hpda %d\n", queue->idx, |
| 792 | icreq->hpda); |
| 793 | return -EPROTO; |
| 794 | } |
| 795 | |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 796 | queue->hdr_digest = !!(icreq->digest & NVME_TCP_HDR_DIGEST_ENABLE); |
| 797 | queue->data_digest = !!(icreq->digest & NVME_TCP_DATA_DIGEST_ENABLE); |
| 798 | if (queue->hdr_digest || queue->data_digest) { |
| 799 | ret = nvmet_tcp_alloc_crypto(queue); |
| 800 | if (ret) |
| 801 | return ret; |
| 802 | } |
| 803 | |
| 804 | memset(icresp, 0, sizeof(*icresp)); |
| 805 | icresp->hdr.type = nvme_tcp_icresp; |
| 806 | icresp->hdr.hlen = sizeof(*icresp); |
| 807 | icresp->hdr.pdo = 0; |
| 808 | icresp->hdr.plen = cpu_to_le32(icresp->hdr.hlen); |
| 809 | icresp->pfv = cpu_to_le16(NVME_TCP_PFV_1_0); |
Sagi Grimberg | 9cda34e | 2020-02-25 16:42:27 -0800 | [diff] [blame] | 810 | icresp->maxdata = cpu_to_le32(0x400000); /* 16M arbitrary limit */ |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 811 | icresp->cpda = 0; |
| 812 | if (queue->hdr_digest) |
| 813 | icresp->digest |= NVME_TCP_HDR_DIGEST_ENABLE; |
| 814 | if (queue->data_digest) |
| 815 | icresp->digest |= NVME_TCP_DATA_DIGEST_ENABLE; |
| 816 | |
| 817 | iov.iov_base = icresp; |
| 818 | iov.iov_len = sizeof(*icresp); |
| 819 | ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len); |
| 820 | if (ret < 0) |
| 821 | goto free_crypto; |
| 822 | |
| 823 | queue->state = NVMET_TCP_Q_LIVE; |
| 824 | nvmet_prepare_receive_pdu(queue); |
| 825 | return 0; |
| 826 | free_crypto: |
| 827 | if (queue->hdr_digest || queue->data_digest) |
| 828 | nvmet_tcp_free_crypto(queue); |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | static void nvmet_tcp_handle_req_failure(struct nvmet_tcp_queue *queue, |
| 833 | struct nvmet_tcp_cmd *cmd, struct nvmet_req *req) |
| 834 | { |
Logan Gunthorpe | c73eebc | 2019-10-23 10:35:40 -0600 | [diff] [blame] | 835 | size_t data_len = le32_to_cpu(req->cmd->common.dptr.sgl.length); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 836 | int ret; |
| 837 | |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 838 | if (!nvme_is_write(cmd->req.cmd) || |
Logan Gunthorpe | c73eebc | 2019-10-23 10:35:40 -0600 | [diff] [blame] | 839 | data_len > cmd->req.port->inline_data_size) { |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 840 | nvmet_prepare_receive_pdu(queue); |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | ret = nvmet_tcp_map_data(cmd); |
| 845 | if (unlikely(ret)) { |
| 846 | pr_err("queue %d: failed to map data\n", queue->idx); |
| 847 | nvmet_tcp_fatal_error(queue); |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | queue->rcv_state = NVMET_TCP_RECV_DATA; |
| 852 | nvmet_tcp_map_pdu_iovec(cmd); |
| 853 | cmd->flags |= NVMET_TCP_F_INIT_FAILED; |
| 854 | } |
| 855 | |
| 856 | static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue) |
| 857 | { |
| 858 | struct nvme_tcp_data_pdu *data = &queue->pdu.data; |
| 859 | struct nvmet_tcp_cmd *cmd; |
| 860 | |
| 861 | cmd = &queue->cmds[data->ttag]; |
| 862 | |
| 863 | if (le32_to_cpu(data->data_offset) != cmd->rbytes_done) { |
| 864 | pr_err("ttag %u unexpected data offset %u (expected %u)\n", |
| 865 | data->ttag, le32_to_cpu(data->data_offset), |
| 866 | cmd->rbytes_done); |
| 867 | /* FIXME: use path and transport errors */ |
| 868 | nvmet_req_complete(&cmd->req, |
| 869 | NVME_SC_INVALID_FIELD | NVME_SC_DNR); |
| 870 | return -EPROTO; |
| 871 | } |
| 872 | |
| 873 | cmd->pdu_len = le32_to_cpu(data->data_length); |
| 874 | cmd->pdu_recv = 0; |
| 875 | nvmet_tcp_map_pdu_iovec(cmd); |
| 876 | queue->cmd = cmd; |
| 877 | queue->rcv_state = NVMET_TCP_RECV_DATA; |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | static int nvmet_tcp_done_recv_pdu(struct nvmet_tcp_queue *queue) |
| 883 | { |
| 884 | struct nvme_tcp_hdr *hdr = &queue->pdu.cmd.hdr; |
| 885 | struct nvme_command *nvme_cmd = &queue->pdu.cmd.cmd; |
| 886 | struct nvmet_req *req; |
| 887 | int ret; |
| 888 | |
| 889 | if (unlikely(queue->state == NVMET_TCP_Q_CONNECTING)) { |
| 890 | if (hdr->type != nvme_tcp_icreq) { |
| 891 | pr_err("unexpected pdu type (%d) before icreq\n", |
| 892 | hdr->type); |
| 893 | nvmet_tcp_fatal_error(queue); |
| 894 | return -EPROTO; |
| 895 | } |
| 896 | return nvmet_tcp_handle_icreq(queue); |
| 897 | } |
| 898 | |
| 899 | if (hdr->type == nvme_tcp_h2c_data) { |
| 900 | ret = nvmet_tcp_handle_h2c_data_pdu(queue); |
| 901 | if (unlikely(ret)) |
| 902 | return ret; |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | queue->cmd = nvmet_tcp_get_cmd(queue); |
| 907 | if (unlikely(!queue->cmd)) { |
| 908 | /* This should never happen */ |
| 909 | pr_err("queue %d: out of commands (%d) send_list_len: %d, opcode: %d", |
| 910 | queue->idx, queue->nr_cmds, queue->send_list_len, |
| 911 | nvme_cmd->common.opcode); |
| 912 | nvmet_tcp_fatal_error(queue); |
| 913 | return -ENOMEM; |
| 914 | } |
| 915 | |
| 916 | req = &queue->cmd->req; |
| 917 | memcpy(req->cmd, nvme_cmd, sizeof(*nvme_cmd)); |
| 918 | |
| 919 | if (unlikely(!nvmet_req_init(req, &queue->nvme_cq, |
| 920 | &queue->nvme_sq, &nvmet_tcp_ops))) { |
| 921 | pr_err("failed cmd %p id %d opcode %d, data_len: %d\n", |
| 922 | req->cmd, req->cmd->common.command_id, |
| 923 | req->cmd->common.opcode, |
| 924 | le32_to_cpu(req->cmd->common.dptr.sgl.length)); |
| 925 | |
| 926 | nvmet_tcp_handle_req_failure(queue, queue->cmd, req); |
| 927 | return -EAGAIN; |
| 928 | } |
| 929 | |
| 930 | ret = nvmet_tcp_map_data(queue->cmd); |
| 931 | if (unlikely(ret)) { |
| 932 | pr_err("queue %d: failed to map data\n", queue->idx); |
| 933 | if (nvmet_tcp_has_inline_data(queue->cmd)) |
| 934 | nvmet_tcp_fatal_error(queue); |
| 935 | else |
| 936 | nvmet_req_complete(req, ret); |
| 937 | ret = -EAGAIN; |
| 938 | goto out; |
| 939 | } |
| 940 | |
| 941 | if (nvmet_tcp_need_data_in(queue->cmd)) { |
| 942 | if (nvmet_tcp_has_inline_data(queue->cmd)) { |
| 943 | queue->rcv_state = NVMET_TCP_RECV_DATA; |
| 944 | nvmet_tcp_map_pdu_iovec(queue->cmd); |
| 945 | return 0; |
| 946 | } |
| 947 | /* send back R2T */ |
| 948 | nvmet_tcp_queue_response(&queue->cmd->req); |
| 949 | goto out; |
| 950 | } |
| 951 | |
Christoph Hellwig | be3f311 | 2019-10-23 10:35:45 -0600 | [diff] [blame] | 952 | queue->cmd->req.execute(&queue->cmd->req); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 953 | out: |
| 954 | nvmet_prepare_receive_pdu(queue); |
| 955 | return ret; |
| 956 | } |
| 957 | |
| 958 | static const u8 nvme_tcp_pdu_sizes[] = { |
| 959 | [nvme_tcp_icreq] = sizeof(struct nvme_tcp_icreq_pdu), |
| 960 | [nvme_tcp_cmd] = sizeof(struct nvme_tcp_cmd_pdu), |
| 961 | [nvme_tcp_h2c_data] = sizeof(struct nvme_tcp_data_pdu), |
| 962 | }; |
| 963 | |
| 964 | static inline u8 nvmet_tcp_pdu_size(u8 type) |
| 965 | { |
| 966 | size_t idx = type; |
| 967 | |
| 968 | return (idx < ARRAY_SIZE(nvme_tcp_pdu_sizes) && |
| 969 | nvme_tcp_pdu_sizes[idx]) ? |
| 970 | nvme_tcp_pdu_sizes[idx] : 0; |
| 971 | } |
| 972 | |
| 973 | static inline bool nvmet_tcp_pdu_valid(u8 type) |
| 974 | { |
| 975 | switch (type) { |
| 976 | case nvme_tcp_icreq: |
| 977 | case nvme_tcp_cmd: |
| 978 | case nvme_tcp_h2c_data: |
| 979 | /* fallthru */ |
| 980 | return true; |
| 981 | } |
| 982 | |
| 983 | return false; |
| 984 | } |
| 985 | |
| 986 | static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue) |
| 987 | { |
| 988 | struct nvme_tcp_hdr *hdr = &queue->pdu.cmd.hdr; |
| 989 | int len; |
| 990 | struct kvec iov; |
| 991 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT }; |
| 992 | |
| 993 | recv: |
| 994 | iov.iov_base = (void *)&queue->pdu + queue->offset; |
| 995 | iov.iov_len = queue->left; |
| 996 | len = kernel_recvmsg(queue->sock, &msg, &iov, 1, |
| 997 | iov.iov_len, msg.msg_flags); |
| 998 | if (unlikely(len < 0)) |
| 999 | return len; |
| 1000 | |
| 1001 | queue->offset += len; |
| 1002 | queue->left -= len; |
| 1003 | if (queue->left) |
| 1004 | return -EAGAIN; |
| 1005 | |
| 1006 | if (queue->offset == sizeof(struct nvme_tcp_hdr)) { |
| 1007 | u8 hdgst = nvmet_tcp_hdgst_len(queue); |
| 1008 | |
| 1009 | if (unlikely(!nvmet_tcp_pdu_valid(hdr->type))) { |
| 1010 | pr_err("unexpected pdu type %d\n", hdr->type); |
| 1011 | nvmet_tcp_fatal_error(queue); |
| 1012 | return -EIO; |
| 1013 | } |
| 1014 | |
| 1015 | if (unlikely(hdr->hlen != nvmet_tcp_pdu_size(hdr->type))) { |
| 1016 | pr_err("pdu %d bad hlen %d\n", hdr->type, hdr->hlen); |
| 1017 | return -EIO; |
| 1018 | } |
| 1019 | |
| 1020 | queue->left = hdr->hlen - queue->offset + hdgst; |
| 1021 | goto recv; |
| 1022 | } |
| 1023 | |
| 1024 | if (queue->hdr_digest && |
| 1025 | nvmet_tcp_verify_hdgst(queue, &queue->pdu, queue->offset)) { |
| 1026 | nvmet_tcp_fatal_error(queue); /* fatal */ |
| 1027 | return -EPROTO; |
| 1028 | } |
| 1029 | |
| 1030 | if (queue->data_digest && |
| 1031 | nvmet_tcp_check_ddgst(queue, &queue->pdu)) { |
| 1032 | nvmet_tcp_fatal_error(queue); /* fatal */ |
| 1033 | return -EPROTO; |
| 1034 | } |
| 1035 | |
| 1036 | return nvmet_tcp_done_recv_pdu(queue); |
| 1037 | } |
| 1038 | |
| 1039 | static void nvmet_tcp_prep_recv_ddgst(struct nvmet_tcp_cmd *cmd) |
| 1040 | { |
| 1041 | struct nvmet_tcp_queue *queue = cmd->queue; |
| 1042 | |
| 1043 | nvmet_tcp_ddgst(queue->rcv_hash, cmd); |
| 1044 | queue->offset = 0; |
| 1045 | queue->left = NVME_TCP_DIGEST_LENGTH; |
| 1046 | queue->rcv_state = NVMET_TCP_RECV_DDGST; |
| 1047 | } |
| 1048 | |
| 1049 | static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) |
| 1050 | { |
| 1051 | struct nvmet_tcp_cmd *cmd = queue->cmd; |
| 1052 | int ret; |
| 1053 | |
| 1054 | while (msg_data_left(&cmd->recv_msg)) { |
| 1055 | ret = sock_recvmsg(cmd->queue->sock, &cmd->recv_msg, |
| 1056 | cmd->recv_msg.msg_flags); |
| 1057 | if (ret <= 0) |
| 1058 | return ret; |
| 1059 | |
| 1060 | cmd->pdu_recv += ret; |
| 1061 | cmd->rbytes_done += ret; |
| 1062 | } |
| 1063 | |
| 1064 | nvmet_tcp_unmap_pdu_iovec(cmd); |
| 1065 | |
| 1066 | if (!(cmd->flags & NVMET_TCP_F_INIT_FAILED) && |
| 1067 | cmd->rbytes_done == cmd->req.transfer_len) { |
| 1068 | if (queue->data_digest) { |
| 1069 | nvmet_tcp_prep_recv_ddgst(cmd); |
| 1070 | return 0; |
| 1071 | } |
Christoph Hellwig | be3f311 | 2019-10-23 10:35:45 -0600 | [diff] [blame] | 1072 | cmd->req.execute(&cmd->req); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | nvmet_prepare_receive_pdu(queue); |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue) |
| 1080 | { |
| 1081 | struct nvmet_tcp_cmd *cmd = queue->cmd; |
| 1082 | int ret; |
| 1083 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT }; |
| 1084 | struct kvec iov = { |
| 1085 | .iov_base = (void *)&cmd->recv_ddgst + queue->offset, |
| 1086 | .iov_len = queue->left |
| 1087 | }; |
| 1088 | |
| 1089 | ret = kernel_recvmsg(queue->sock, &msg, &iov, 1, |
| 1090 | iov.iov_len, msg.msg_flags); |
| 1091 | if (unlikely(ret < 0)) |
| 1092 | return ret; |
| 1093 | |
| 1094 | queue->offset += ret; |
| 1095 | queue->left -= ret; |
| 1096 | if (queue->left) |
| 1097 | return -EAGAIN; |
| 1098 | |
| 1099 | if (queue->data_digest && cmd->exp_ddgst != cmd->recv_ddgst) { |
| 1100 | pr_err("queue %d: cmd %d pdu (%d) data digest error: recv %#x expected %#x\n", |
| 1101 | queue->idx, cmd->req.cmd->common.command_id, |
| 1102 | queue->pdu.cmd.hdr.type, le32_to_cpu(cmd->recv_ddgst), |
| 1103 | le32_to_cpu(cmd->exp_ddgst)); |
| 1104 | nvmet_tcp_finish_cmd(cmd); |
| 1105 | nvmet_tcp_fatal_error(queue); |
| 1106 | ret = -EPROTO; |
| 1107 | goto out; |
| 1108 | } |
| 1109 | |
| 1110 | if (!(cmd->flags & NVMET_TCP_F_INIT_FAILED) && |
| 1111 | cmd->rbytes_done == cmd->req.transfer_len) |
Christoph Hellwig | be3f311 | 2019-10-23 10:35:45 -0600 | [diff] [blame] | 1112 | cmd->req.execute(&cmd->req); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 1113 | ret = 0; |
| 1114 | out: |
| 1115 | nvmet_prepare_receive_pdu(queue); |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | static int nvmet_tcp_try_recv_one(struct nvmet_tcp_queue *queue) |
| 1120 | { |
Sagi Grimberg | fb86585 | 2019-01-09 14:56:32 -0800 | [diff] [blame] | 1121 | int result = 0; |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 1122 | |
| 1123 | if (unlikely(queue->rcv_state == NVMET_TCP_RECV_ERR)) |
| 1124 | return 0; |
| 1125 | |
| 1126 | if (queue->rcv_state == NVMET_TCP_RECV_PDU) { |
| 1127 | result = nvmet_tcp_try_recv_pdu(queue); |
| 1128 | if (result != 0) |
| 1129 | goto done_recv; |
| 1130 | } |
| 1131 | |
| 1132 | if (queue->rcv_state == NVMET_TCP_RECV_DATA) { |
| 1133 | result = nvmet_tcp_try_recv_data(queue); |
| 1134 | if (result != 0) |
| 1135 | goto done_recv; |
| 1136 | } |
| 1137 | |
| 1138 | if (queue->rcv_state == NVMET_TCP_RECV_DDGST) { |
| 1139 | result = nvmet_tcp_try_recv_ddgst(queue); |
| 1140 | if (result != 0) |
| 1141 | goto done_recv; |
| 1142 | } |
| 1143 | |
| 1144 | done_recv: |
| 1145 | if (result < 0) { |
| 1146 | if (result == -EAGAIN) |
| 1147 | return 0; |
| 1148 | return result; |
| 1149 | } |
| 1150 | return 1; |
| 1151 | } |
| 1152 | |
| 1153 | static int nvmet_tcp_try_recv(struct nvmet_tcp_queue *queue, |
| 1154 | int budget, int *recvs) |
| 1155 | { |
| 1156 | int i, ret = 0; |
| 1157 | |
| 1158 | for (i = 0; i < budget; i++) { |
| 1159 | ret = nvmet_tcp_try_recv_one(queue); |
| 1160 | if (ret <= 0) |
| 1161 | break; |
| 1162 | (*recvs)++; |
| 1163 | } |
| 1164 | |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
| 1168 | static void nvmet_tcp_schedule_release_queue(struct nvmet_tcp_queue *queue) |
| 1169 | { |
| 1170 | spin_lock(&queue->state_lock); |
| 1171 | if (queue->state != NVMET_TCP_Q_DISCONNECTING) { |
| 1172 | queue->state = NVMET_TCP_Q_DISCONNECTING; |
| 1173 | schedule_work(&queue->release_work); |
| 1174 | } |
| 1175 | spin_unlock(&queue->state_lock); |
| 1176 | } |
| 1177 | |
| 1178 | static void nvmet_tcp_io_work(struct work_struct *w) |
| 1179 | { |
| 1180 | struct nvmet_tcp_queue *queue = |
| 1181 | container_of(w, struct nvmet_tcp_queue, io_work); |
| 1182 | bool pending; |
| 1183 | int ret, ops = 0; |
| 1184 | |
| 1185 | do { |
| 1186 | pending = false; |
| 1187 | |
| 1188 | ret = nvmet_tcp_try_recv(queue, NVMET_TCP_RECV_BUDGET, &ops); |
| 1189 | if (ret > 0) { |
| 1190 | pending = true; |
| 1191 | } else if (ret < 0) { |
| 1192 | if (ret == -EPIPE || ret == -ECONNRESET) |
| 1193 | kernel_sock_shutdown(queue->sock, SHUT_RDWR); |
| 1194 | else |
| 1195 | nvmet_tcp_fatal_error(queue); |
| 1196 | return; |
| 1197 | } |
| 1198 | |
| 1199 | ret = nvmet_tcp_try_send(queue, NVMET_TCP_SEND_BUDGET, &ops); |
| 1200 | if (ret > 0) { |
| 1201 | /* transmitted message/data */ |
| 1202 | pending = true; |
| 1203 | } else if (ret < 0) { |
| 1204 | if (ret == -EPIPE || ret == -ECONNRESET) |
| 1205 | kernel_sock_shutdown(queue->sock, SHUT_RDWR); |
| 1206 | else |
| 1207 | nvmet_tcp_fatal_error(queue); |
| 1208 | return; |
| 1209 | } |
| 1210 | |
| 1211 | } while (pending && ops < NVMET_TCP_IO_WORK_BUDGET); |
| 1212 | |
| 1213 | /* |
| 1214 | * We exahusted our budget, requeue our selves |
| 1215 | */ |
| 1216 | if (pending) |
| 1217 | queue_work_on(queue->cpu, nvmet_tcp_wq, &queue->io_work); |
| 1218 | } |
| 1219 | |
| 1220 | static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue, |
| 1221 | struct nvmet_tcp_cmd *c) |
| 1222 | { |
| 1223 | u8 hdgst = nvmet_tcp_hdgst_len(queue); |
| 1224 | |
| 1225 | c->queue = queue; |
| 1226 | c->req.port = queue->port->nport; |
| 1227 | |
| 1228 | c->cmd_pdu = page_frag_alloc(&queue->pf_cache, |
| 1229 | sizeof(*c->cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); |
| 1230 | if (!c->cmd_pdu) |
| 1231 | return -ENOMEM; |
| 1232 | c->req.cmd = &c->cmd_pdu->cmd; |
| 1233 | |
| 1234 | c->rsp_pdu = page_frag_alloc(&queue->pf_cache, |
| 1235 | sizeof(*c->rsp_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); |
| 1236 | if (!c->rsp_pdu) |
| 1237 | goto out_free_cmd; |
Max Gurtovoy | fc6c973 | 2019-04-08 18:39:59 +0300 | [diff] [blame] | 1238 | c->req.cqe = &c->rsp_pdu->cqe; |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 1239 | |
| 1240 | c->data_pdu = page_frag_alloc(&queue->pf_cache, |
| 1241 | sizeof(*c->data_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); |
| 1242 | if (!c->data_pdu) |
| 1243 | goto out_free_rsp; |
| 1244 | |
| 1245 | c->r2t_pdu = page_frag_alloc(&queue->pf_cache, |
| 1246 | sizeof(*c->r2t_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); |
| 1247 | if (!c->r2t_pdu) |
| 1248 | goto out_free_data; |
| 1249 | |
| 1250 | c->recv_msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 1251 | |
| 1252 | list_add_tail(&c->entry, &queue->free_list); |
| 1253 | |
| 1254 | return 0; |
| 1255 | out_free_data: |
| 1256 | page_frag_free(c->data_pdu); |
| 1257 | out_free_rsp: |
| 1258 | page_frag_free(c->rsp_pdu); |
| 1259 | out_free_cmd: |
| 1260 | page_frag_free(c->cmd_pdu); |
| 1261 | return -ENOMEM; |
| 1262 | } |
| 1263 | |
| 1264 | static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c) |
| 1265 | { |
| 1266 | page_frag_free(c->r2t_pdu); |
| 1267 | page_frag_free(c->data_pdu); |
| 1268 | page_frag_free(c->rsp_pdu); |
| 1269 | page_frag_free(c->cmd_pdu); |
| 1270 | } |
| 1271 | |
| 1272 | static int nvmet_tcp_alloc_cmds(struct nvmet_tcp_queue *queue) |
| 1273 | { |
| 1274 | struct nvmet_tcp_cmd *cmds; |
| 1275 | int i, ret = -EINVAL, nr_cmds = queue->nr_cmds; |
| 1276 | |
| 1277 | cmds = kcalloc(nr_cmds, sizeof(struct nvmet_tcp_cmd), GFP_KERNEL); |
| 1278 | if (!cmds) |
| 1279 | goto out; |
| 1280 | |
| 1281 | for (i = 0; i < nr_cmds; i++) { |
| 1282 | ret = nvmet_tcp_alloc_cmd(queue, cmds + i); |
| 1283 | if (ret) |
| 1284 | goto out_free; |
| 1285 | } |
| 1286 | |
| 1287 | queue->cmds = cmds; |
| 1288 | |
| 1289 | return 0; |
| 1290 | out_free: |
| 1291 | while (--i >= 0) |
| 1292 | nvmet_tcp_free_cmd(cmds + i); |
| 1293 | kfree(cmds); |
| 1294 | out: |
| 1295 | return ret; |
| 1296 | } |
| 1297 | |
| 1298 | static void nvmet_tcp_free_cmds(struct nvmet_tcp_queue *queue) |
| 1299 | { |
| 1300 | struct nvmet_tcp_cmd *cmds = queue->cmds; |
| 1301 | int i; |
| 1302 | |
| 1303 | for (i = 0; i < queue->nr_cmds; i++) |
| 1304 | nvmet_tcp_free_cmd(cmds + i); |
| 1305 | |
| 1306 | nvmet_tcp_free_cmd(&queue->connect); |
| 1307 | kfree(cmds); |
| 1308 | } |
| 1309 | |
| 1310 | static void nvmet_tcp_restore_socket_callbacks(struct nvmet_tcp_queue *queue) |
| 1311 | { |
| 1312 | struct socket *sock = queue->sock; |
| 1313 | |
| 1314 | write_lock_bh(&sock->sk->sk_callback_lock); |
| 1315 | sock->sk->sk_data_ready = queue->data_ready; |
| 1316 | sock->sk->sk_state_change = queue->state_change; |
| 1317 | sock->sk->sk_write_space = queue->write_space; |
| 1318 | sock->sk->sk_user_data = NULL; |
| 1319 | write_unlock_bh(&sock->sk->sk_callback_lock); |
| 1320 | } |
| 1321 | |
| 1322 | static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd) |
| 1323 | { |
| 1324 | nvmet_req_uninit(&cmd->req); |
| 1325 | nvmet_tcp_unmap_pdu_iovec(cmd); |
Sagi Grimberg | 35d1a93 | 2019-08-02 20:29:11 -0700 | [diff] [blame] | 1326 | kfree(cmd->iov); |
Sagi Grimberg | 30f27d5 | 2019-09-13 10:36:40 -0700 | [diff] [blame] | 1327 | sgl_free(cmd->req.sg); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | static void nvmet_tcp_uninit_data_in_cmds(struct nvmet_tcp_queue *queue) |
| 1331 | { |
| 1332 | struct nvmet_tcp_cmd *cmd = queue->cmds; |
| 1333 | int i; |
| 1334 | |
| 1335 | for (i = 0; i < queue->nr_cmds; i++, cmd++) { |
| 1336 | if (nvmet_tcp_need_data_in(cmd)) |
| 1337 | nvmet_tcp_finish_cmd(cmd); |
| 1338 | } |
| 1339 | |
| 1340 | if (!queue->nr_cmds && nvmet_tcp_need_data_in(&queue->connect)) { |
| 1341 | /* failed in connect */ |
| 1342 | nvmet_tcp_finish_cmd(&queue->connect); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | static void nvmet_tcp_release_queue_work(struct work_struct *w) |
| 1347 | { |
| 1348 | struct nvmet_tcp_queue *queue = |
| 1349 | container_of(w, struct nvmet_tcp_queue, release_work); |
| 1350 | |
| 1351 | mutex_lock(&nvmet_tcp_queue_mutex); |
| 1352 | list_del_init(&queue->queue_list); |
| 1353 | mutex_unlock(&nvmet_tcp_queue_mutex); |
| 1354 | |
| 1355 | nvmet_tcp_restore_socket_callbacks(queue); |
| 1356 | flush_work(&queue->io_work); |
| 1357 | |
| 1358 | nvmet_tcp_uninit_data_in_cmds(queue); |
| 1359 | nvmet_sq_destroy(&queue->nvme_sq); |
| 1360 | cancel_work_sync(&queue->io_work); |
| 1361 | sock_release(queue->sock); |
| 1362 | nvmet_tcp_free_cmds(queue); |
| 1363 | if (queue->hdr_digest || queue->data_digest) |
| 1364 | nvmet_tcp_free_crypto(queue); |
| 1365 | ida_simple_remove(&nvmet_tcp_queue_ida, queue->idx); |
| 1366 | |
| 1367 | kfree(queue); |
| 1368 | } |
| 1369 | |
| 1370 | static void nvmet_tcp_data_ready(struct sock *sk) |
| 1371 | { |
| 1372 | struct nvmet_tcp_queue *queue; |
| 1373 | |
| 1374 | read_lock_bh(&sk->sk_callback_lock); |
| 1375 | queue = sk->sk_user_data; |
| 1376 | if (likely(queue)) |
| 1377 | queue_work_on(queue->cpu, nvmet_tcp_wq, &queue->io_work); |
| 1378 | read_unlock_bh(&sk->sk_callback_lock); |
| 1379 | } |
| 1380 | |
| 1381 | static void nvmet_tcp_write_space(struct sock *sk) |
| 1382 | { |
| 1383 | struct nvmet_tcp_queue *queue; |
| 1384 | |
| 1385 | read_lock_bh(&sk->sk_callback_lock); |
| 1386 | queue = sk->sk_user_data; |
| 1387 | if (unlikely(!queue)) |
| 1388 | goto out; |
| 1389 | |
| 1390 | if (unlikely(queue->state == NVMET_TCP_Q_CONNECTING)) { |
| 1391 | queue->write_space(sk); |
| 1392 | goto out; |
| 1393 | } |
| 1394 | |
| 1395 | if (sk_stream_is_writeable(sk)) { |
| 1396 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 1397 | queue_work_on(queue->cpu, nvmet_tcp_wq, &queue->io_work); |
| 1398 | } |
| 1399 | out: |
| 1400 | read_unlock_bh(&sk->sk_callback_lock); |
| 1401 | } |
| 1402 | |
| 1403 | static void nvmet_tcp_state_change(struct sock *sk) |
| 1404 | { |
| 1405 | struct nvmet_tcp_queue *queue; |
| 1406 | |
| 1407 | write_lock_bh(&sk->sk_callback_lock); |
| 1408 | queue = sk->sk_user_data; |
| 1409 | if (!queue) |
| 1410 | goto done; |
| 1411 | |
| 1412 | switch (sk->sk_state) { |
| 1413 | case TCP_FIN_WAIT1: |
| 1414 | case TCP_CLOSE_WAIT: |
| 1415 | case TCP_CLOSE: |
| 1416 | /* FALLTHRU */ |
| 1417 | sk->sk_user_data = NULL; |
| 1418 | nvmet_tcp_schedule_release_queue(queue); |
| 1419 | break; |
| 1420 | default: |
| 1421 | pr_warn("queue %d unhandled state %d\n", |
| 1422 | queue->idx, sk->sk_state); |
| 1423 | } |
| 1424 | done: |
| 1425 | write_unlock_bh(&sk->sk_callback_lock); |
| 1426 | } |
| 1427 | |
| 1428 | static int nvmet_tcp_set_queue_sock(struct nvmet_tcp_queue *queue) |
| 1429 | { |
| 1430 | struct socket *sock = queue->sock; |
Israel Rukshin | 89275a9 | 2019-08-18 12:08:55 +0300 | [diff] [blame] | 1431 | struct inet_sock *inet = inet_sk(sock->sk); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 1432 | int ret; |
| 1433 | |
| 1434 | ret = kernel_getsockname(sock, |
| 1435 | (struct sockaddr *)&queue->sockaddr); |
| 1436 | if (ret < 0) |
| 1437 | return ret; |
| 1438 | |
| 1439 | ret = kernel_getpeername(sock, |
| 1440 | (struct sockaddr *)&queue->sockaddr_peer); |
| 1441 | if (ret < 0) |
| 1442 | return ret; |
| 1443 | |
| 1444 | /* |
| 1445 | * Cleanup whatever is sitting in the TCP transmit queue on socket |
| 1446 | * close. This is done to prevent stale data from being sent should |
| 1447 | * the network connection be restored before TCP times out. |
| 1448 | */ |
Christoph Hellwig | c433594 | 2020-05-28 07:12:10 +0200 | [diff] [blame] | 1449 | sock_no_linger(sock->sk); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 1450 | |
Christoph Hellwig | 6e43496 | 2020-05-28 07:12:11 +0200 | [diff] [blame^] | 1451 | if (so_priority > 0) |
| 1452 | sock_set_priority(sock->sk, so_priority); |
Wunderlich, Mark | 43cc668 | 2020-01-16 00:46:16 +0000 | [diff] [blame] | 1453 | |
Israel Rukshin | 89275a9 | 2019-08-18 12:08:55 +0300 | [diff] [blame] | 1454 | /* Set socket type of service */ |
| 1455 | if (inet->rcv_tos > 0) { |
| 1456 | int tos = inet->rcv_tos; |
| 1457 | |
| 1458 | ret = kernel_setsockopt(sock, SOL_IP, IP_TOS, |
| 1459 | (char *)&tos, sizeof(tos)); |
| 1460 | if (ret) |
| 1461 | return ret; |
| 1462 | } |
| 1463 | |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 1464 | write_lock_bh(&sock->sk->sk_callback_lock); |
| 1465 | sock->sk->sk_user_data = queue; |
| 1466 | queue->data_ready = sock->sk->sk_data_ready; |
| 1467 | sock->sk->sk_data_ready = nvmet_tcp_data_ready; |
| 1468 | queue->state_change = sock->sk->sk_state_change; |
| 1469 | sock->sk->sk_state_change = nvmet_tcp_state_change; |
| 1470 | queue->write_space = sock->sk->sk_write_space; |
| 1471 | sock->sk->sk_write_space = nvmet_tcp_write_space; |
| 1472 | write_unlock_bh(&sock->sk->sk_callback_lock); |
| 1473 | |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
| 1477 | static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port, |
| 1478 | struct socket *newsock) |
| 1479 | { |
| 1480 | struct nvmet_tcp_queue *queue; |
| 1481 | int ret; |
| 1482 | |
| 1483 | queue = kzalloc(sizeof(*queue), GFP_KERNEL); |
| 1484 | if (!queue) |
| 1485 | return -ENOMEM; |
| 1486 | |
| 1487 | INIT_WORK(&queue->release_work, nvmet_tcp_release_queue_work); |
| 1488 | INIT_WORK(&queue->io_work, nvmet_tcp_io_work); |
| 1489 | queue->sock = newsock; |
| 1490 | queue->port = port; |
| 1491 | queue->nr_cmds = 0; |
| 1492 | spin_lock_init(&queue->state_lock); |
| 1493 | queue->state = NVMET_TCP_Q_CONNECTING; |
| 1494 | INIT_LIST_HEAD(&queue->free_list); |
| 1495 | init_llist_head(&queue->resp_list); |
| 1496 | INIT_LIST_HEAD(&queue->resp_send_list); |
| 1497 | |
| 1498 | queue->idx = ida_simple_get(&nvmet_tcp_queue_ida, 0, 0, GFP_KERNEL); |
| 1499 | if (queue->idx < 0) { |
| 1500 | ret = queue->idx; |
| 1501 | goto out_free_queue; |
| 1502 | } |
| 1503 | |
| 1504 | ret = nvmet_tcp_alloc_cmd(queue, &queue->connect); |
| 1505 | if (ret) |
| 1506 | goto out_ida_remove; |
| 1507 | |
| 1508 | ret = nvmet_sq_init(&queue->nvme_sq); |
| 1509 | if (ret) |
| 1510 | goto out_free_connect; |
| 1511 | |
| 1512 | port->last_cpu = cpumask_next_wrap(port->last_cpu, |
| 1513 | cpu_online_mask, -1, false); |
| 1514 | queue->cpu = port->last_cpu; |
| 1515 | nvmet_prepare_receive_pdu(queue); |
| 1516 | |
| 1517 | mutex_lock(&nvmet_tcp_queue_mutex); |
| 1518 | list_add_tail(&queue->queue_list, &nvmet_tcp_queue_list); |
| 1519 | mutex_unlock(&nvmet_tcp_queue_mutex); |
| 1520 | |
| 1521 | ret = nvmet_tcp_set_queue_sock(queue); |
| 1522 | if (ret) |
| 1523 | goto out_destroy_sq; |
| 1524 | |
| 1525 | queue_work_on(queue->cpu, nvmet_tcp_wq, &queue->io_work); |
| 1526 | |
| 1527 | return 0; |
| 1528 | out_destroy_sq: |
| 1529 | mutex_lock(&nvmet_tcp_queue_mutex); |
| 1530 | list_del_init(&queue->queue_list); |
| 1531 | mutex_unlock(&nvmet_tcp_queue_mutex); |
| 1532 | nvmet_sq_destroy(&queue->nvme_sq); |
| 1533 | out_free_connect: |
| 1534 | nvmet_tcp_free_cmd(&queue->connect); |
| 1535 | out_ida_remove: |
| 1536 | ida_simple_remove(&nvmet_tcp_queue_ida, queue->idx); |
| 1537 | out_free_queue: |
| 1538 | kfree(queue); |
| 1539 | return ret; |
| 1540 | } |
| 1541 | |
| 1542 | static void nvmet_tcp_accept_work(struct work_struct *w) |
| 1543 | { |
| 1544 | struct nvmet_tcp_port *port = |
| 1545 | container_of(w, struct nvmet_tcp_port, accept_work); |
| 1546 | struct socket *newsock; |
| 1547 | int ret; |
| 1548 | |
| 1549 | while (true) { |
| 1550 | ret = kernel_accept(port->sock, &newsock, O_NONBLOCK); |
| 1551 | if (ret < 0) { |
| 1552 | if (ret != -EAGAIN) |
| 1553 | pr_warn("failed to accept err=%d\n", ret); |
| 1554 | return; |
| 1555 | } |
| 1556 | ret = nvmet_tcp_alloc_queue(port, newsock); |
| 1557 | if (ret) { |
| 1558 | pr_err("failed to allocate queue\n"); |
| 1559 | sock_release(newsock); |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | static void nvmet_tcp_listen_data_ready(struct sock *sk) |
| 1565 | { |
| 1566 | struct nvmet_tcp_port *port; |
| 1567 | |
| 1568 | read_lock_bh(&sk->sk_callback_lock); |
| 1569 | port = sk->sk_user_data; |
| 1570 | if (!port) |
| 1571 | goto out; |
| 1572 | |
| 1573 | if (sk->sk_state == TCP_LISTEN) |
| 1574 | schedule_work(&port->accept_work); |
| 1575 | out: |
| 1576 | read_unlock_bh(&sk->sk_callback_lock); |
| 1577 | } |
| 1578 | |
| 1579 | static int nvmet_tcp_add_port(struct nvmet_port *nport) |
| 1580 | { |
| 1581 | struct nvmet_tcp_port *port; |
| 1582 | __kernel_sa_family_t af; |
| 1583 | int opt, ret; |
| 1584 | |
| 1585 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
| 1586 | if (!port) |
| 1587 | return -ENOMEM; |
| 1588 | |
| 1589 | switch (nport->disc_addr.adrfam) { |
| 1590 | case NVMF_ADDR_FAMILY_IP4: |
| 1591 | af = AF_INET; |
| 1592 | break; |
| 1593 | case NVMF_ADDR_FAMILY_IP6: |
| 1594 | af = AF_INET6; |
| 1595 | break; |
| 1596 | default: |
| 1597 | pr_err("address family %d not supported\n", |
| 1598 | nport->disc_addr.adrfam); |
| 1599 | ret = -EINVAL; |
| 1600 | goto err_port; |
| 1601 | } |
| 1602 | |
| 1603 | ret = inet_pton_with_scope(&init_net, af, nport->disc_addr.traddr, |
| 1604 | nport->disc_addr.trsvcid, &port->addr); |
| 1605 | if (ret) { |
| 1606 | pr_err("malformed ip/port passed: %s:%s\n", |
| 1607 | nport->disc_addr.traddr, nport->disc_addr.trsvcid); |
| 1608 | goto err_port; |
| 1609 | } |
| 1610 | |
| 1611 | port->nport = nport; |
| 1612 | port->last_cpu = -1; |
| 1613 | INIT_WORK(&port->accept_work, nvmet_tcp_accept_work); |
| 1614 | if (port->nport->inline_data_size < 0) |
| 1615 | port->nport->inline_data_size = NVMET_TCP_DEF_INLINE_DATA_SIZE; |
| 1616 | |
| 1617 | ret = sock_create(port->addr.ss_family, SOCK_STREAM, |
| 1618 | IPPROTO_TCP, &port->sock); |
| 1619 | if (ret) { |
| 1620 | pr_err("failed to create a socket\n"); |
| 1621 | goto err_port; |
| 1622 | } |
| 1623 | |
| 1624 | port->sock->sk->sk_user_data = port; |
| 1625 | port->data_ready = port->sock->sk->sk_data_ready; |
| 1626 | port->sock->sk->sk_data_ready = nvmet_tcp_listen_data_ready; |
Christoph Hellwig | b58f0e8 | 2020-05-28 07:12:09 +0200 | [diff] [blame] | 1627 | sock_set_reuseaddr(port->sock->sk); |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 1628 | |
| 1629 | opt = 1; |
| 1630 | ret = kernel_setsockopt(port->sock, IPPROTO_TCP, |
| 1631 | TCP_NODELAY, (char *)&opt, sizeof(opt)); |
| 1632 | if (ret) { |
| 1633 | pr_err("failed to set TCP_NODELAY sock opt %d\n", ret); |
| 1634 | goto err_sock; |
| 1635 | } |
| 1636 | |
Christoph Hellwig | 6e43496 | 2020-05-28 07:12:11 +0200 | [diff] [blame^] | 1637 | if (so_priority > 0) |
| 1638 | sock_set_priority(port->sock->sk, so_priority); |
Wunderlich, Mark | 43cc668 | 2020-01-16 00:46:16 +0000 | [diff] [blame] | 1639 | |
Sagi Grimberg | 872d26a | 2018-12-03 17:52:15 -0800 | [diff] [blame] | 1640 | ret = kernel_bind(port->sock, (struct sockaddr *)&port->addr, |
| 1641 | sizeof(port->addr)); |
| 1642 | if (ret) { |
| 1643 | pr_err("failed to bind port socket %d\n", ret); |
| 1644 | goto err_sock; |
| 1645 | } |
| 1646 | |
| 1647 | ret = kernel_listen(port->sock, 128); |
| 1648 | if (ret) { |
| 1649 | pr_err("failed to listen %d on port sock\n", ret); |
| 1650 | goto err_sock; |
| 1651 | } |
| 1652 | |
| 1653 | nport->priv = port; |
| 1654 | pr_info("enabling port %d (%pISpc)\n", |
| 1655 | le16_to_cpu(nport->disc_addr.portid), &port->addr); |
| 1656 | |
| 1657 | return 0; |
| 1658 | |
| 1659 | err_sock: |
| 1660 | sock_release(port->sock); |
| 1661 | err_port: |
| 1662 | kfree(port); |
| 1663 | return ret; |
| 1664 | } |
| 1665 | |
| 1666 | static void nvmet_tcp_remove_port(struct nvmet_port *nport) |
| 1667 | { |
| 1668 | struct nvmet_tcp_port *port = nport->priv; |
| 1669 | |
| 1670 | write_lock_bh(&port->sock->sk->sk_callback_lock); |
| 1671 | port->sock->sk->sk_data_ready = port->data_ready; |
| 1672 | port->sock->sk->sk_user_data = NULL; |
| 1673 | write_unlock_bh(&port->sock->sk->sk_callback_lock); |
| 1674 | cancel_work_sync(&port->accept_work); |
| 1675 | |
| 1676 | sock_release(port->sock); |
| 1677 | kfree(port); |
| 1678 | } |
| 1679 | |
| 1680 | static void nvmet_tcp_delete_ctrl(struct nvmet_ctrl *ctrl) |
| 1681 | { |
| 1682 | struct nvmet_tcp_queue *queue; |
| 1683 | |
| 1684 | mutex_lock(&nvmet_tcp_queue_mutex); |
| 1685 | list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list) |
| 1686 | if (queue->nvme_sq.ctrl == ctrl) |
| 1687 | kernel_sock_shutdown(queue->sock, SHUT_RDWR); |
| 1688 | mutex_unlock(&nvmet_tcp_queue_mutex); |
| 1689 | } |
| 1690 | |
| 1691 | static u16 nvmet_tcp_install_queue(struct nvmet_sq *sq) |
| 1692 | { |
| 1693 | struct nvmet_tcp_queue *queue = |
| 1694 | container_of(sq, struct nvmet_tcp_queue, nvme_sq); |
| 1695 | |
| 1696 | if (sq->qid == 0) { |
| 1697 | /* Let inflight controller teardown complete */ |
| 1698 | flush_scheduled_work(); |
| 1699 | } |
| 1700 | |
| 1701 | queue->nr_cmds = sq->size * 2; |
| 1702 | if (nvmet_tcp_alloc_cmds(queue)) |
| 1703 | return NVME_SC_INTERNAL; |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
| 1707 | static void nvmet_tcp_disc_port_addr(struct nvmet_req *req, |
| 1708 | struct nvmet_port *nport, char *traddr) |
| 1709 | { |
| 1710 | struct nvmet_tcp_port *port = nport->priv; |
| 1711 | |
| 1712 | if (inet_addr_is_any((struct sockaddr *)&port->addr)) { |
| 1713 | struct nvmet_tcp_cmd *cmd = |
| 1714 | container_of(req, struct nvmet_tcp_cmd, req); |
| 1715 | struct nvmet_tcp_queue *queue = cmd->queue; |
| 1716 | |
| 1717 | sprintf(traddr, "%pISc", (struct sockaddr *)&queue->sockaddr); |
| 1718 | } else { |
| 1719 | memcpy(traddr, nport->disc_addr.traddr, NVMF_TRADDR_SIZE); |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | static struct nvmet_fabrics_ops nvmet_tcp_ops = { |
| 1724 | .owner = THIS_MODULE, |
| 1725 | .type = NVMF_TRTYPE_TCP, |
| 1726 | .msdbd = 1, |
| 1727 | .has_keyed_sgls = 0, |
| 1728 | .add_port = nvmet_tcp_add_port, |
| 1729 | .remove_port = nvmet_tcp_remove_port, |
| 1730 | .queue_response = nvmet_tcp_queue_response, |
| 1731 | .delete_ctrl = nvmet_tcp_delete_ctrl, |
| 1732 | .install_queue = nvmet_tcp_install_queue, |
| 1733 | .disc_traddr = nvmet_tcp_disc_port_addr, |
| 1734 | }; |
| 1735 | |
| 1736 | static int __init nvmet_tcp_init(void) |
| 1737 | { |
| 1738 | int ret; |
| 1739 | |
| 1740 | nvmet_tcp_wq = alloc_workqueue("nvmet_tcp_wq", WQ_HIGHPRI, 0); |
| 1741 | if (!nvmet_tcp_wq) |
| 1742 | return -ENOMEM; |
| 1743 | |
| 1744 | ret = nvmet_register_transport(&nvmet_tcp_ops); |
| 1745 | if (ret) |
| 1746 | goto err; |
| 1747 | |
| 1748 | return 0; |
| 1749 | err: |
| 1750 | destroy_workqueue(nvmet_tcp_wq); |
| 1751 | return ret; |
| 1752 | } |
| 1753 | |
| 1754 | static void __exit nvmet_tcp_exit(void) |
| 1755 | { |
| 1756 | struct nvmet_tcp_queue *queue; |
| 1757 | |
| 1758 | nvmet_unregister_transport(&nvmet_tcp_ops); |
| 1759 | |
| 1760 | flush_scheduled_work(); |
| 1761 | mutex_lock(&nvmet_tcp_queue_mutex); |
| 1762 | list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list) |
| 1763 | kernel_sock_shutdown(queue->sock, SHUT_RDWR); |
| 1764 | mutex_unlock(&nvmet_tcp_queue_mutex); |
| 1765 | flush_scheduled_work(); |
| 1766 | |
| 1767 | destroy_workqueue(nvmet_tcp_wq); |
| 1768 | } |
| 1769 | |
| 1770 | module_init(nvmet_tcp_init); |
| 1771 | module_exit(nvmet_tcp_exit); |
| 1772 | |
| 1773 | MODULE_LICENSE("GPL v2"); |
| 1774 | MODULE_ALIAS("nvmet-transport-3"); /* 3 == NVMF_TRTYPE_TCP */ |