blob: 40757a63f4553781fa036a4784bc4b987f90a816 [file] [log] [blame]
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001// 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, Mark43cc6682020-01-16 00:46:16 +000022/* 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 */
28static int so_priority;
29module_param(so_priority, int, 0644);
30MODULE_PARM_DESC(so_priority, "nvmet tcp socket optimize priority");
31
Sagi Grimberg872d26a2018-12-03 17:52:15 -080032#define NVMET_TCP_RECV_BUDGET 8
33#define NVMET_TCP_SEND_BUDGET 8
34#define NVMET_TCP_IO_WORK_BUDGET 64
35
36enum 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
44enum 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
51enum {
52 NVMET_TCP_F_INIT_FAILED = (1 << 0),
53};
54
55struct 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
87enum nvmet_tcp_queue_state {
88 NVMET_TCP_Q_CONNECTING,
89 NVMET_TCP_Q_LIVE,
90 NVMET_TCP_Q_DISCONNECTING,
91};
92
93struct 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
142struct 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
151static DEFINE_IDA(nvmet_tcp_queue_ida);
152static LIST_HEAD(nvmet_tcp_queue_list);
153static DEFINE_MUTEX(nvmet_tcp_queue_mutex);
154
155static struct workqueue_struct *nvmet_tcp_wq;
156static struct nvmet_fabrics_ops nvmet_tcp_ops;
157static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c);
158static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd);
159
160static 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
166static 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
172static inline bool nvmet_tcp_need_data_in(struct nvmet_tcp_cmd *cmd)
173{
Max Gurtovoyfc6c9732019-04-08 18:39:59 +0300174 return nvmet_tcp_has_data_in(cmd) && !cmd->req.cqe->status;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800175}
176
177static 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 Gurtovoyfc6c9732019-04-08 18:39:59 +0300181 !cmd->req.cqe->status;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800182}
183
184static 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
190static inline struct nvmet_tcp_cmd *
191nvmet_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
209static 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
217static inline u8 nvmet_tcp_hdgst_len(struct nvmet_tcp_queue *queue)
218{
219 return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
220}
221
222static inline u8 nvmet_tcp_ddgst_len(struct nvmet_tcp_queue *queue)
223{
224 return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
225}
226
227static 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
237static 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
263static 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
280static 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
291static 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
319static 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
328static 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 Gunthorpee0bace72019-10-23 10:35:39 -0600333 if (!len)
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800334 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;
360err:
Sagi Grimberg30f27d52019-09-13 10:36:40 -0700361 sgl_free(cmd->req.sg);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800362 return NVME_SC_INTERNAL;
363}
364
365static 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
373static 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 Grimberg70583292019-03-08 15:41:21 -0800384 pdu->hdr.flags = NVME_TCP_F_DATA_LAST | (queue->nvme_sq.sqhd_disabled ?
385 NVME_TCP_F_DATA_SUCCESS : 0);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800386 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 Gurtovoyfc6c9732019-04-08 18:39:59 +0300391 pdu->command_id = cmd->req.cqe->command_id;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800392 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
406static 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
431static 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
451static 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
469static 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
495static 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
505static 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 Grimberg98fd5c72020-03-12 16:06:38 -0700528static int nvmet_try_send_data(struct nvmet_tcp_cmd *cmd, bool last_in_batch)
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800529{
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 Grimberg98fd5c72020-03-12 16:06:38 -0700536 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 Grimberg872d26a2018-12-03 17:52:15 -0800542
543 ret = kernel_sendpage(cmd->queue->sock, page, cmd->offset,
Sagi Grimberg98fd5c72020-03-12 16:06:38 -0700544 left, flags);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800545 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 Grimberg70583292019-03-08 15:41:21 -0800562 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 Grimberg872d26a2018-12-03 17:52:15 -0800568 }
Sagi Grimberg70583292019-03-08 15:41:21 -0800569
570 if (queue->nvme_sq.sqhd_disabled) {
571 kfree(cmd->iov);
Sagi Grimberg30f27d52019-09-13 10:36:40 -0700572 sgl_free(cmd->req.sg);
Sagi Grimberg70583292019-03-08 15:41:21 -0800573 }
574
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800575 return 1;
576
577}
578
579static 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 Grimberg30f27d52019-09-13 10:36:40 -0700603 sgl_free(cmd->req.sg);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800604 cmd->queue->snd_cmd = NULL;
605 nvmet_tcp_put_cmd(cmd);
606 return 1;
607}
608
609static 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 Grimberge90d1722020-03-12 16:06:39 -0700635static int nvmet_try_send_ddgst(struct nvmet_tcp_cmd *cmd, bool last_in_batch)
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800636{
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 Grimberge90d1722020-03-12 16:06:39 -0700645 if (!last_in_batch && cmd->queue->send_list_len)
646 msg.msg_flags |= MSG_MORE;
647
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800648 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 Grimberg70583292019-03-08 15:41:21 -0800653
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 Grimberg872d26a2018-12-03 17:52:15 -0800660 return 1;
661}
662
663static 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 Grimberg98fd5c72020-03-12 16:06:38 -0700682 ret = nvmet_try_send_data(cmd, last_in_batch);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800683 if (ret <= 0)
684 goto done_send;
685 }
686
687 if (cmd->state == NVMET_TCP_SEND_DDGST) {
Sagi Grimberge90d1722020-03-12 16:06:39 -0700688 ret = nvmet_try_send_ddgst(cmd, last_in_batch);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800689 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
702done_send:
703 if (ret < 0) {
704 if (ret == -EAGAIN)
705 return 0;
706 return ret;
707 }
708
709 return 1;
710}
711
712static 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
727static 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
735static 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
744static 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;
763free_snd_hash:
764 ahash_request_free(queue->snd_hash);
765free_tfm:
766 crypto_free_ahash(tfm);
767 return -ENOMEM;
768}
769
770
771static 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 Grimberg872d26a2018-12-03 17:52:15 -0800796 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 Grimberg9cda34e2020-02-25 16:42:27 -0800810 icresp->maxdata = cpu_to_le32(0x400000); /* 16M arbitrary limit */
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800811 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;
826free_crypto:
827 if (queue->hdr_digest || queue->data_digest)
828 nvmet_tcp_free_crypto(queue);
829 return ret;
830}
831
832static void nvmet_tcp_handle_req_failure(struct nvmet_tcp_queue *queue,
833 struct nvmet_tcp_cmd *cmd, struct nvmet_req *req)
834{
Logan Gunthorpec73eebc2019-10-23 10:35:40 -0600835 size_t data_len = le32_to_cpu(req->cmd->common.dptr.sgl.length);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800836 int ret;
837
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800838 if (!nvme_is_write(cmd->req.cmd) ||
Logan Gunthorpec73eebc2019-10-23 10:35:40 -0600839 data_len > cmd->req.port->inline_data_size) {
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800840 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
856static 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
882static 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 Hellwigbe3f3112019-10-23 10:35:45 -0600952 queue->cmd->req.execute(&queue->cmd->req);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800953out:
954 nvmet_prepare_receive_pdu(queue);
955 return ret;
956}
957
958static 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
964static 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
973static 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
986static 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
993recv:
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
1039static 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
1049static 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 Hellwigbe3f3112019-10-23 10:35:45 -06001072 cmd->req.execute(&cmd->req);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001073 }
1074
1075 nvmet_prepare_receive_pdu(queue);
1076 return 0;
1077}
1078
1079static 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 Hellwigbe3f3112019-10-23 10:35:45 -06001112 cmd->req.execute(&cmd->req);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001113 ret = 0;
1114out:
1115 nvmet_prepare_receive_pdu(queue);
1116 return ret;
1117}
1118
1119static int nvmet_tcp_try_recv_one(struct nvmet_tcp_queue *queue)
1120{
Sagi Grimbergfb865852019-01-09 14:56:32 -08001121 int result = 0;
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001122
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
1144done_recv:
1145 if (result < 0) {
1146 if (result == -EAGAIN)
1147 return 0;
1148 return result;
1149 }
1150 return 1;
1151}
1152
1153static 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
1168static 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
1178static 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
1220static 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 Gurtovoyfc6c9732019-04-08 18:39:59 +03001238 c->req.cqe = &c->rsp_pdu->cqe;
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001239
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;
1255out_free_data:
1256 page_frag_free(c->data_pdu);
1257out_free_rsp:
1258 page_frag_free(c->rsp_pdu);
1259out_free_cmd:
1260 page_frag_free(c->cmd_pdu);
1261 return -ENOMEM;
1262}
1263
1264static 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
1272static 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;
1290out_free:
1291 while (--i >= 0)
1292 nvmet_tcp_free_cmd(cmds + i);
1293 kfree(cmds);
1294out:
1295 return ret;
1296}
1297
1298static 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
1310static 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
1322static 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 Grimberg35d1a932019-08-02 20:29:11 -07001326 kfree(cmd->iov);
Sagi Grimberg30f27d52019-09-13 10:36:40 -07001327 sgl_free(cmd->req.sg);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001328}
1329
1330static 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
1346static 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
1370static 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
1381static 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 }
1399out:
1400 read_unlock_bh(&sk->sk_callback_lock);
1401}
1402
1403static 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 }
1424done:
1425 write_unlock_bh(&sk->sk_callback_lock);
1426}
1427
1428static int nvmet_tcp_set_queue_sock(struct nvmet_tcp_queue *queue)
1429{
1430 struct socket *sock = queue->sock;
Israel Rukshin89275a92019-08-18 12:08:55 +03001431 struct inet_sock *inet = inet_sk(sock->sk);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001432 struct linger sol = { .l_onoff = 1, .l_linger = 0 };
1433 int ret;
1434
1435 ret = kernel_getsockname(sock,
1436 (struct sockaddr *)&queue->sockaddr);
1437 if (ret < 0)
1438 return ret;
1439
1440 ret = kernel_getpeername(sock,
1441 (struct sockaddr *)&queue->sockaddr_peer);
1442 if (ret < 0)
1443 return ret;
1444
1445 /*
1446 * Cleanup whatever is sitting in the TCP transmit queue on socket
1447 * close. This is done to prevent stale data from being sent should
1448 * the network connection be restored before TCP times out.
1449 */
1450 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER,
1451 (char *)&sol, sizeof(sol));
1452 if (ret)
1453 return ret;
1454
Wunderlich, Mark43cc6682020-01-16 00:46:16 +00001455 if (so_priority > 0) {
1456 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_PRIORITY,
1457 (char *)&so_priority, sizeof(so_priority));
1458 if (ret)
1459 return ret;
1460 }
1461
Israel Rukshin89275a92019-08-18 12:08:55 +03001462 /* Set socket type of service */
1463 if (inet->rcv_tos > 0) {
1464 int tos = inet->rcv_tos;
1465
1466 ret = kernel_setsockopt(sock, SOL_IP, IP_TOS,
1467 (char *)&tos, sizeof(tos));
1468 if (ret)
1469 return ret;
1470 }
1471
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001472 write_lock_bh(&sock->sk->sk_callback_lock);
1473 sock->sk->sk_user_data = queue;
1474 queue->data_ready = sock->sk->sk_data_ready;
1475 sock->sk->sk_data_ready = nvmet_tcp_data_ready;
1476 queue->state_change = sock->sk->sk_state_change;
1477 sock->sk->sk_state_change = nvmet_tcp_state_change;
1478 queue->write_space = sock->sk->sk_write_space;
1479 sock->sk->sk_write_space = nvmet_tcp_write_space;
1480 write_unlock_bh(&sock->sk->sk_callback_lock);
1481
1482 return 0;
1483}
1484
1485static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,
1486 struct socket *newsock)
1487{
1488 struct nvmet_tcp_queue *queue;
1489 int ret;
1490
1491 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
1492 if (!queue)
1493 return -ENOMEM;
1494
1495 INIT_WORK(&queue->release_work, nvmet_tcp_release_queue_work);
1496 INIT_WORK(&queue->io_work, nvmet_tcp_io_work);
1497 queue->sock = newsock;
1498 queue->port = port;
1499 queue->nr_cmds = 0;
1500 spin_lock_init(&queue->state_lock);
1501 queue->state = NVMET_TCP_Q_CONNECTING;
1502 INIT_LIST_HEAD(&queue->free_list);
1503 init_llist_head(&queue->resp_list);
1504 INIT_LIST_HEAD(&queue->resp_send_list);
1505
1506 queue->idx = ida_simple_get(&nvmet_tcp_queue_ida, 0, 0, GFP_KERNEL);
1507 if (queue->idx < 0) {
1508 ret = queue->idx;
1509 goto out_free_queue;
1510 }
1511
1512 ret = nvmet_tcp_alloc_cmd(queue, &queue->connect);
1513 if (ret)
1514 goto out_ida_remove;
1515
1516 ret = nvmet_sq_init(&queue->nvme_sq);
1517 if (ret)
1518 goto out_free_connect;
1519
1520 port->last_cpu = cpumask_next_wrap(port->last_cpu,
1521 cpu_online_mask, -1, false);
1522 queue->cpu = port->last_cpu;
1523 nvmet_prepare_receive_pdu(queue);
1524
1525 mutex_lock(&nvmet_tcp_queue_mutex);
1526 list_add_tail(&queue->queue_list, &nvmet_tcp_queue_list);
1527 mutex_unlock(&nvmet_tcp_queue_mutex);
1528
1529 ret = nvmet_tcp_set_queue_sock(queue);
1530 if (ret)
1531 goto out_destroy_sq;
1532
1533 queue_work_on(queue->cpu, nvmet_tcp_wq, &queue->io_work);
1534
1535 return 0;
1536out_destroy_sq:
1537 mutex_lock(&nvmet_tcp_queue_mutex);
1538 list_del_init(&queue->queue_list);
1539 mutex_unlock(&nvmet_tcp_queue_mutex);
1540 nvmet_sq_destroy(&queue->nvme_sq);
1541out_free_connect:
1542 nvmet_tcp_free_cmd(&queue->connect);
1543out_ida_remove:
1544 ida_simple_remove(&nvmet_tcp_queue_ida, queue->idx);
1545out_free_queue:
1546 kfree(queue);
1547 return ret;
1548}
1549
1550static void nvmet_tcp_accept_work(struct work_struct *w)
1551{
1552 struct nvmet_tcp_port *port =
1553 container_of(w, struct nvmet_tcp_port, accept_work);
1554 struct socket *newsock;
1555 int ret;
1556
1557 while (true) {
1558 ret = kernel_accept(port->sock, &newsock, O_NONBLOCK);
1559 if (ret < 0) {
1560 if (ret != -EAGAIN)
1561 pr_warn("failed to accept err=%d\n", ret);
1562 return;
1563 }
1564 ret = nvmet_tcp_alloc_queue(port, newsock);
1565 if (ret) {
1566 pr_err("failed to allocate queue\n");
1567 sock_release(newsock);
1568 }
1569 }
1570}
1571
1572static void nvmet_tcp_listen_data_ready(struct sock *sk)
1573{
1574 struct nvmet_tcp_port *port;
1575
1576 read_lock_bh(&sk->sk_callback_lock);
1577 port = sk->sk_user_data;
1578 if (!port)
1579 goto out;
1580
1581 if (sk->sk_state == TCP_LISTEN)
1582 schedule_work(&port->accept_work);
1583out:
1584 read_unlock_bh(&sk->sk_callback_lock);
1585}
1586
1587static int nvmet_tcp_add_port(struct nvmet_port *nport)
1588{
1589 struct nvmet_tcp_port *port;
1590 __kernel_sa_family_t af;
1591 int opt, ret;
1592
1593 port = kzalloc(sizeof(*port), GFP_KERNEL);
1594 if (!port)
1595 return -ENOMEM;
1596
1597 switch (nport->disc_addr.adrfam) {
1598 case NVMF_ADDR_FAMILY_IP4:
1599 af = AF_INET;
1600 break;
1601 case NVMF_ADDR_FAMILY_IP6:
1602 af = AF_INET6;
1603 break;
1604 default:
1605 pr_err("address family %d not supported\n",
1606 nport->disc_addr.adrfam);
1607 ret = -EINVAL;
1608 goto err_port;
1609 }
1610
1611 ret = inet_pton_with_scope(&init_net, af, nport->disc_addr.traddr,
1612 nport->disc_addr.trsvcid, &port->addr);
1613 if (ret) {
1614 pr_err("malformed ip/port passed: %s:%s\n",
1615 nport->disc_addr.traddr, nport->disc_addr.trsvcid);
1616 goto err_port;
1617 }
1618
1619 port->nport = nport;
1620 port->last_cpu = -1;
1621 INIT_WORK(&port->accept_work, nvmet_tcp_accept_work);
1622 if (port->nport->inline_data_size < 0)
1623 port->nport->inline_data_size = NVMET_TCP_DEF_INLINE_DATA_SIZE;
1624
1625 ret = sock_create(port->addr.ss_family, SOCK_STREAM,
1626 IPPROTO_TCP, &port->sock);
1627 if (ret) {
1628 pr_err("failed to create a socket\n");
1629 goto err_port;
1630 }
1631
1632 port->sock->sk->sk_user_data = port;
1633 port->data_ready = port->sock->sk->sk_data_ready;
1634 port->sock->sk->sk_data_ready = nvmet_tcp_listen_data_ready;
Christoph Hellwigb58f0e82020-05-28 07:12:09 +02001635 sock_set_reuseaddr(port->sock->sk);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001636
1637 opt = 1;
1638 ret = kernel_setsockopt(port->sock, IPPROTO_TCP,
1639 TCP_NODELAY, (char *)&opt, sizeof(opt));
1640 if (ret) {
1641 pr_err("failed to set TCP_NODELAY sock opt %d\n", ret);
1642 goto err_sock;
1643 }
1644
Wunderlich, Mark43cc6682020-01-16 00:46:16 +00001645 if (so_priority > 0) {
1646 ret = kernel_setsockopt(port->sock, SOL_SOCKET, SO_PRIORITY,
1647 (char *)&so_priority, sizeof(so_priority));
1648 if (ret) {
1649 pr_err("failed to set SO_PRIORITY sock opt %d\n", ret);
1650 goto err_sock;
1651 }
1652 }
1653
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001654 ret = kernel_bind(port->sock, (struct sockaddr *)&port->addr,
1655 sizeof(port->addr));
1656 if (ret) {
1657 pr_err("failed to bind port socket %d\n", ret);
1658 goto err_sock;
1659 }
1660
1661 ret = kernel_listen(port->sock, 128);
1662 if (ret) {
1663 pr_err("failed to listen %d on port sock\n", ret);
1664 goto err_sock;
1665 }
1666
1667 nport->priv = port;
1668 pr_info("enabling port %d (%pISpc)\n",
1669 le16_to_cpu(nport->disc_addr.portid), &port->addr);
1670
1671 return 0;
1672
1673err_sock:
1674 sock_release(port->sock);
1675err_port:
1676 kfree(port);
1677 return ret;
1678}
1679
1680static void nvmet_tcp_remove_port(struct nvmet_port *nport)
1681{
1682 struct nvmet_tcp_port *port = nport->priv;
1683
1684 write_lock_bh(&port->sock->sk->sk_callback_lock);
1685 port->sock->sk->sk_data_ready = port->data_ready;
1686 port->sock->sk->sk_user_data = NULL;
1687 write_unlock_bh(&port->sock->sk->sk_callback_lock);
1688 cancel_work_sync(&port->accept_work);
1689
1690 sock_release(port->sock);
1691 kfree(port);
1692}
1693
1694static void nvmet_tcp_delete_ctrl(struct nvmet_ctrl *ctrl)
1695{
1696 struct nvmet_tcp_queue *queue;
1697
1698 mutex_lock(&nvmet_tcp_queue_mutex);
1699 list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list)
1700 if (queue->nvme_sq.ctrl == ctrl)
1701 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1702 mutex_unlock(&nvmet_tcp_queue_mutex);
1703}
1704
1705static u16 nvmet_tcp_install_queue(struct nvmet_sq *sq)
1706{
1707 struct nvmet_tcp_queue *queue =
1708 container_of(sq, struct nvmet_tcp_queue, nvme_sq);
1709
1710 if (sq->qid == 0) {
1711 /* Let inflight controller teardown complete */
1712 flush_scheduled_work();
1713 }
1714
1715 queue->nr_cmds = sq->size * 2;
1716 if (nvmet_tcp_alloc_cmds(queue))
1717 return NVME_SC_INTERNAL;
1718 return 0;
1719}
1720
1721static void nvmet_tcp_disc_port_addr(struct nvmet_req *req,
1722 struct nvmet_port *nport, char *traddr)
1723{
1724 struct nvmet_tcp_port *port = nport->priv;
1725
1726 if (inet_addr_is_any((struct sockaddr *)&port->addr)) {
1727 struct nvmet_tcp_cmd *cmd =
1728 container_of(req, struct nvmet_tcp_cmd, req);
1729 struct nvmet_tcp_queue *queue = cmd->queue;
1730
1731 sprintf(traddr, "%pISc", (struct sockaddr *)&queue->sockaddr);
1732 } else {
1733 memcpy(traddr, nport->disc_addr.traddr, NVMF_TRADDR_SIZE);
1734 }
1735}
1736
1737static struct nvmet_fabrics_ops nvmet_tcp_ops = {
1738 .owner = THIS_MODULE,
1739 .type = NVMF_TRTYPE_TCP,
1740 .msdbd = 1,
1741 .has_keyed_sgls = 0,
1742 .add_port = nvmet_tcp_add_port,
1743 .remove_port = nvmet_tcp_remove_port,
1744 .queue_response = nvmet_tcp_queue_response,
1745 .delete_ctrl = nvmet_tcp_delete_ctrl,
1746 .install_queue = nvmet_tcp_install_queue,
1747 .disc_traddr = nvmet_tcp_disc_port_addr,
1748};
1749
1750static int __init nvmet_tcp_init(void)
1751{
1752 int ret;
1753
1754 nvmet_tcp_wq = alloc_workqueue("nvmet_tcp_wq", WQ_HIGHPRI, 0);
1755 if (!nvmet_tcp_wq)
1756 return -ENOMEM;
1757
1758 ret = nvmet_register_transport(&nvmet_tcp_ops);
1759 if (ret)
1760 goto err;
1761
1762 return 0;
1763err:
1764 destroy_workqueue(nvmet_tcp_wq);
1765 return ret;
1766}
1767
1768static void __exit nvmet_tcp_exit(void)
1769{
1770 struct nvmet_tcp_queue *queue;
1771
1772 nvmet_unregister_transport(&nvmet_tcp_ops);
1773
1774 flush_scheduled_work();
1775 mutex_lock(&nvmet_tcp_queue_mutex);
1776 list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list)
1777 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1778 mutex_unlock(&nvmet_tcp_queue_mutex);
1779 flush_scheduled_work();
1780
1781 destroy_workqueue(nvmet_tcp_wq);
1782}
1783
1784module_init(nvmet_tcp_init);
1785module_exit(nvmet_tcp_exit);
1786
1787MODULE_LICENSE("GPL v2");
1788MODULE_ALIAS("nvmet-transport-3"); /* 3 == NVMF_TRTYPE_TCP */