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