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