blob: 558a973277fd7063c486bf4884ad55297a2cb3f8 [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
Wunderlich, Markd8e7b462021-03-31 21:38:30 +000032/* Define a time period (in usecs) that io_work() shall sample an activated
33 * queue before determining it to be idle. This optional module behavior
34 * can enable NIC solutions that support socket optimized packet processing
35 * using advanced interrupt moderation techniques.
36 */
37static int idle_poll_period_usecs;
38module_param(idle_poll_period_usecs, int, 0644);
39MODULE_PARM_DESC(idle_poll_period_usecs,
40 "nvmet tcp io_work poll till idle time period in usecs");
41
Sagi Grimberg872d26a2018-12-03 17:52:15 -080042#define NVMET_TCP_RECV_BUDGET 8
43#define NVMET_TCP_SEND_BUDGET 8
44#define NVMET_TCP_IO_WORK_BUDGET 64
45
46enum nvmet_tcp_send_state {
47 NVMET_TCP_SEND_DATA_PDU,
48 NVMET_TCP_SEND_DATA,
49 NVMET_TCP_SEND_R2T,
50 NVMET_TCP_SEND_DDGST,
51 NVMET_TCP_SEND_RESPONSE
52};
53
54enum nvmet_tcp_recv_state {
55 NVMET_TCP_RECV_PDU,
56 NVMET_TCP_RECV_DATA,
57 NVMET_TCP_RECV_DDGST,
58 NVMET_TCP_RECV_ERR,
59};
60
61enum {
62 NVMET_TCP_F_INIT_FAILED = (1 << 0),
63};
64
65struct nvmet_tcp_cmd {
66 struct nvmet_tcp_queue *queue;
67 struct nvmet_req req;
68
69 struct nvme_tcp_cmd_pdu *cmd_pdu;
70 struct nvme_tcp_rsp_pdu *rsp_pdu;
71 struct nvme_tcp_data_pdu *data_pdu;
72 struct nvme_tcp_r2t_pdu *r2t_pdu;
73
74 u32 rbytes_done;
75 u32 wbytes_done;
76
77 u32 pdu_len;
78 u32 pdu_recv;
79 int sg_idx;
80 int nr_mapped;
81 struct msghdr recv_msg;
82 struct kvec *iov;
83 u32 flags;
84
85 struct list_head entry;
86 struct llist_node lentry;
87
88 /* send state */
89 u32 offset;
90 struct scatterlist *cur_sg;
91 enum nvmet_tcp_send_state state;
92
93 __le32 exp_ddgst;
94 __le32 recv_ddgst;
95};
96
97enum nvmet_tcp_queue_state {
98 NVMET_TCP_Q_CONNECTING,
99 NVMET_TCP_Q_LIVE,
100 NVMET_TCP_Q_DISCONNECTING,
101};
102
103struct nvmet_tcp_queue {
104 struct socket *sock;
105 struct nvmet_tcp_port *port;
106 struct work_struct io_work;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800107 struct nvmet_cq nvme_cq;
108 struct nvmet_sq nvme_sq;
109
110 /* send state */
111 struct nvmet_tcp_cmd *cmds;
112 unsigned int nr_cmds;
113 struct list_head free_list;
114 struct llist_head resp_list;
115 struct list_head resp_send_list;
116 int send_list_len;
117 struct nvmet_tcp_cmd *snd_cmd;
118
119 /* recv state */
120 int offset;
121 int left;
122 enum nvmet_tcp_recv_state rcv_state;
123 struct nvmet_tcp_cmd *cmd;
124 union nvme_tcp_pdu pdu;
125
126 /* digest state */
127 bool hdr_digest;
128 bool data_digest;
129 struct ahash_request *snd_hash;
130 struct ahash_request *rcv_hash;
131
Wunderlich, Markd8e7b462021-03-31 21:38:30 +0000132 unsigned long poll_end;
133
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800134 spinlock_t state_lock;
135 enum nvmet_tcp_queue_state state;
136
137 struct sockaddr_storage sockaddr;
138 struct sockaddr_storage sockaddr_peer;
139 struct work_struct release_work;
140
141 int idx;
142 struct list_head queue_list;
143
144 struct nvmet_tcp_cmd connect;
145
146 struct page_frag_cache pf_cache;
147
148 void (*data_ready)(struct sock *);
149 void (*state_change)(struct sock *);
150 void (*write_space)(struct sock *);
151};
152
153struct nvmet_tcp_port {
154 struct socket *sock;
155 struct work_struct accept_work;
156 struct nvmet_port *nport;
157 struct sockaddr_storage addr;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800158 void (*data_ready)(struct sock *);
159};
160
161static DEFINE_IDA(nvmet_tcp_queue_ida);
162static LIST_HEAD(nvmet_tcp_queue_list);
163static DEFINE_MUTEX(nvmet_tcp_queue_mutex);
164
165static struct workqueue_struct *nvmet_tcp_wq;
Max Gurtovoya40aae62020-06-01 20:05:20 +0300166static const struct nvmet_fabrics_ops nvmet_tcp_ops;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800167static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c);
168static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd);
169
170static inline u16 nvmet_tcp_cmd_tag(struct nvmet_tcp_queue *queue,
171 struct nvmet_tcp_cmd *cmd)
172{
Ziye Yanga6ce7d72020-08-22 00:48:10 +0800173 if (unlikely(!queue->nr_cmds)) {
174 /* We didn't allocate cmds yet, send 0xffff */
175 return USHRT_MAX;
176 }
177
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800178 return cmd - queue->cmds;
179}
180
181static inline bool nvmet_tcp_has_data_in(struct nvmet_tcp_cmd *cmd)
182{
183 return nvme_is_write(cmd->req.cmd) &&
184 cmd->rbytes_done < cmd->req.transfer_len;
185}
186
187static inline bool nvmet_tcp_need_data_in(struct nvmet_tcp_cmd *cmd)
188{
Max Gurtovoyfc6c9732019-04-08 18:39:59 +0300189 return nvmet_tcp_has_data_in(cmd) && !cmd->req.cqe->status;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800190}
191
192static inline bool nvmet_tcp_need_data_out(struct nvmet_tcp_cmd *cmd)
193{
194 return !nvme_is_write(cmd->req.cmd) &&
195 cmd->req.transfer_len > 0 &&
Max Gurtovoyfc6c9732019-04-08 18:39:59 +0300196 !cmd->req.cqe->status;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800197}
198
199static inline bool nvmet_tcp_has_inline_data(struct nvmet_tcp_cmd *cmd)
200{
201 return nvme_is_write(cmd->req.cmd) && cmd->pdu_len &&
202 !cmd->rbytes_done;
203}
204
205static inline struct nvmet_tcp_cmd *
206nvmet_tcp_get_cmd(struct nvmet_tcp_queue *queue)
207{
208 struct nvmet_tcp_cmd *cmd;
209
210 cmd = list_first_entry_or_null(&queue->free_list,
211 struct nvmet_tcp_cmd, entry);
212 if (!cmd)
213 return NULL;
214 list_del_init(&cmd->entry);
215
216 cmd->rbytes_done = cmd->wbytes_done = 0;
217 cmd->pdu_len = 0;
218 cmd->pdu_recv = 0;
219 cmd->iov = NULL;
220 cmd->flags = 0;
221 return cmd;
222}
223
224static inline void nvmet_tcp_put_cmd(struct nvmet_tcp_cmd *cmd)
225{
226 if (unlikely(cmd == &cmd->queue->connect))
227 return;
228
229 list_add_tail(&cmd->entry, &cmd->queue->free_list);
230}
231
Mark Wunderlichf7790e52020-08-28 01:00:53 +0000232static inline int queue_cpu(struct nvmet_tcp_queue *queue)
233{
234 return queue->sock->sk->sk_incoming_cpu;
235}
236
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800237static inline u8 nvmet_tcp_hdgst_len(struct nvmet_tcp_queue *queue)
238{
239 return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
240}
241
242static inline u8 nvmet_tcp_ddgst_len(struct nvmet_tcp_queue *queue)
243{
244 return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
245}
246
247static inline void nvmet_tcp_hdgst(struct ahash_request *hash,
248 void *pdu, size_t len)
249{
250 struct scatterlist sg;
251
252 sg_init_one(&sg, pdu, len);
253 ahash_request_set_crypt(hash, &sg, pdu + len, len);
254 crypto_ahash_digest(hash);
255}
256
257static int nvmet_tcp_verify_hdgst(struct nvmet_tcp_queue *queue,
258 void *pdu, size_t len)
259{
260 struct nvme_tcp_hdr *hdr = pdu;
261 __le32 recv_digest;
262 __le32 exp_digest;
263
264 if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
265 pr_err("queue %d: header digest enabled but no header digest\n",
266 queue->idx);
267 return -EPROTO;
268 }
269
270 recv_digest = *(__le32 *)(pdu + hdr->hlen);
271 nvmet_tcp_hdgst(queue->rcv_hash, pdu, len);
272 exp_digest = *(__le32 *)(pdu + hdr->hlen);
273 if (recv_digest != exp_digest) {
274 pr_err("queue %d: header digest error: recv %#x expected %#x\n",
275 queue->idx, le32_to_cpu(recv_digest),
276 le32_to_cpu(exp_digest));
277 return -EPROTO;
278 }
279
280 return 0;
281}
282
283static int nvmet_tcp_check_ddgst(struct nvmet_tcp_queue *queue, void *pdu)
284{
285 struct nvme_tcp_hdr *hdr = pdu;
286 u8 digest_len = nvmet_tcp_hdgst_len(queue);
287 u32 len;
288
289 len = le32_to_cpu(hdr->plen) - hdr->hlen -
290 (hdr->flags & NVME_TCP_F_HDGST ? digest_len : 0);
291
292 if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
293 pr_err("queue %d: data digest flag is cleared\n", queue->idx);
294 return -EPROTO;
295 }
296
297 return 0;
298}
299
300static void nvmet_tcp_unmap_pdu_iovec(struct nvmet_tcp_cmd *cmd)
301{
302 struct scatterlist *sg;
303 int i;
304
305 sg = &cmd->req.sg[cmd->sg_idx];
306
307 for (i = 0; i < cmd->nr_mapped; i++)
308 kunmap(sg_page(&sg[i]));
309}
310
311static void nvmet_tcp_map_pdu_iovec(struct nvmet_tcp_cmd *cmd)
312{
313 struct kvec *iov = cmd->iov;
314 struct scatterlist *sg;
315 u32 length, offset, sg_offset;
316
317 length = cmd->pdu_len;
318 cmd->nr_mapped = DIV_ROUND_UP(length, PAGE_SIZE);
319 offset = cmd->rbytes_done;
Sagi Grimbergcb8563f2021-02-03 01:20:25 -0800320 cmd->sg_idx = offset / PAGE_SIZE;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800321 sg_offset = offset % PAGE_SIZE;
322 sg = &cmd->req.sg[cmd->sg_idx];
323
324 while (length) {
325 u32 iov_len = min_t(u32, length, sg->length - sg_offset);
326
327 iov->iov_base = kmap(sg_page(sg)) + sg->offset + sg_offset;
328 iov->iov_len = iov_len;
329
330 length -= iov_len;
331 sg = sg_next(sg);
332 iov++;
Sagi Grimbergcb8563f2021-02-03 01:20:25 -0800333 sg_offset = 0;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800334 }
335
336 iov_iter_kvec(&cmd->recv_msg.msg_iter, READ, cmd->iov,
337 cmd->nr_mapped, cmd->pdu_len);
338}
339
340static void nvmet_tcp_fatal_error(struct nvmet_tcp_queue *queue)
341{
342 queue->rcv_state = NVMET_TCP_RECV_ERR;
343 if (queue->nvme_sq.ctrl)
344 nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl);
345 else
346 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
347}
348
Sagi Grimberg0236d342020-05-18 10:47:48 -0700349static void nvmet_tcp_socket_error(struct nvmet_tcp_queue *queue, int status)
350{
351 if (status == -EPIPE || status == -ECONNRESET)
352 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
353 else
354 nvmet_tcp_fatal_error(queue);
355}
356
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800357static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd)
358{
359 struct nvme_sgl_desc *sgl = &cmd->req.cmd->common.dptr.sgl;
360 u32 len = le32_to_cpu(sgl->length);
361
Logan Gunthorpee0bace72019-10-23 10:35:39 -0600362 if (!len)
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800363 return 0;
364
365 if (sgl->type == ((NVME_SGL_FMT_DATA_DESC << 4) |
366 NVME_SGL_FMT_OFFSET)) {
367 if (!nvme_is_write(cmd->req.cmd))
368 return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
369
370 if (len > cmd->req.port->inline_data_size)
371 return NVME_SC_SGL_INVALID_OFFSET | NVME_SC_DNR;
372 cmd->pdu_len = len;
373 }
374 cmd->req.transfer_len += len;
375
376 cmd->req.sg = sgl_alloc(len, GFP_KERNEL, &cmd->req.sg_cnt);
377 if (!cmd->req.sg)
378 return NVME_SC_INTERNAL;
379 cmd->cur_sg = cmd->req.sg;
380
381 if (nvmet_tcp_has_data_in(cmd)) {
382 cmd->iov = kmalloc_array(cmd->req.sg_cnt,
383 sizeof(*cmd->iov), GFP_KERNEL);
384 if (!cmd->iov)
385 goto err;
386 }
387
388 return 0;
389err:
Sagi Grimberg30f27d52019-09-13 10:36:40 -0700390 sgl_free(cmd->req.sg);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800391 return NVME_SC_INTERNAL;
392}
393
Sagi Grimbergfda871c2021-02-03 15:00:01 -0800394static void nvmet_tcp_send_ddgst(struct ahash_request *hash,
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800395 struct nvmet_tcp_cmd *cmd)
396{
397 ahash_request_set_crypt(hash, cmd->req.sg,
398 (void *)&cmd->exp_ddgst, cmd->req.transfer_len);
399 crypto_ahash_digest(hash);
400}
401
Sagi Grimbergfda871c2021-02-03 15:00:01 -0800402static void nvmet_tcp_recv_ddgst(struct ahash_request *hash,
403 struct nvmet_tcp_cmd *cmd)
404{
405 struct scatterlist sg;
406 struct kvec *iov;
407 int i;
408
409 crypto_ahash_init(hash);
410 for (i = 0, iov = cmd->iov; i < cmd->nr_mapped; i++, iov++) {
411 sg_init_one(&sg, iov->iov_base, iov->iov_len);
412 ahash_request_set_crypt(hash, &sg, NULL, iov->iov_len);
413 crypto_ahash_update(hash);
414 }
415 ahash_request_set_crypt(hash, NULL, (void *)&cmd->exp_ddgst, 0);
416 crypto_ahash_final(hash);
417}
418
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800419static void nvmet_setup_c2h_data_pdu(struct nvmet_tcp_cmd *cmd)
420{
421 struct nvme_tcp_data_pdu *pdu = cmd->data_pdu;
422 struct nvmet_tcp_queue *queue = cmd->queue;
423 u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
424 u8 ddgst = nvmet_tcp_ddgst_len(cmd->queue);
425
426 cmd->offset = 0;
427 cmd->state = NVMET_TCP_SEND_DATA_PDU;
428
429 pdu->hdr.type = nvme_tcp_c2h_data;
Sagi Grimberg70583292019-03-08 15:41:21 -0800430 pdu->hdr.flags = NVME_TCP_F_DATA_LAST | (queue->nvme_sq.sqhd_disabled ?
431 NVME_TCP_F_DATA_SUCCESS : 0);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800432 pdu->hdr.hlen = sizeof(*pdu);
433 pdu->hdr.pdo = pdu->hdr.hlen + hdgst;
434 pdu->hdr.plen =
435 cpu_to_le32(pdu->hdr.hlen + hdgst +
436 cmd->req.transfer_len + ddgst);
Max Gurtovoyfc6c9732019-04-08 18:39:59 +0300437 pdu->command_id = cmd->req.cqe->command_id;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800438 pdu->data_length = cpu_to_le32(cmd->req.transfer_len);
439 pdu->data_offset = cpu_to_le32(cmd->wbytes_done);
440
441 if (queue->data_digest) {
442 pdu->hdr.flags |= NVME_TCP_F_DDGST;
Sagi Grimbergfda871c2021-02-03 15:00:01 -0800443 nvmet_tcp_send_ddgst(queue->snd_hash, cmd);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800444 }
445
446 if (cmd->queue->hdr_digest) {
447 pdu->hdr.flags |= NVME_TCP_F_HDGST;
448 nvmet_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
449 }
450}
451
452static void nvmet_setup_r2t_pdu(struct nvmet_tcp_cmd *cmd)
453{
454 struct nvme_tcp_r2t_pdu *pdu = cmd->r2t_pdu;
455 struct nvmet_tcp_queue *queue = cmd->queue;
456 u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
457
458 cmd->offset = 0;
459 cmd->state = NVMET_TCP_SEND_R2T;
460
461 pdu->hdr.type = nvme_tcp_r2t;
462 pdu->hdr.flags = 0;
463 pdu->hdr.hlen = sizeof(*pdu);
464 pdu->hdr.pdo = 0;
465 pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
466
467 pdu->command_id = cmd->req.cmd->common.command_id;
468 pdu->ttag = nvmet_tcp_cmd_tag(cmd->queue, cmd);
469 pdu->r2t_length = cpu_to_le32(cmd->req.transfer_len - cmd->rbytes_done);
470 pdu->r2t_offset = cpu_to_le32(cmd->rbytes_done);
471 if (cmd->queue->hdr_digest) {
472 pdu->hdr.flags |= NVME_TCP_F_HDGST;
473 nvmet_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
474 }
475}
476
477static void nvmet_setup_response_pdu(struct nvmet_tcp_cmd *cmd)
478{
479 struct nvme_tcp_rsp_pdu *pdu = cmd->rsp_pdu;
480 struct nvmet_tcp_queue *queue = cmd->queue;
481 u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
482
483 cmd->offset = 0;
484 cmd->state = NVMET_TCP_SEND_RESPONSE;
485
486 pdu->hdr.type = nvme_tcp_rsp;
487 pdu->hdr.flags = 0;
488 pdu->hdr.hlen = sizeof(*pdu);
489 pdu->hdr.pdo = 0;
490 pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
491 if (cmd->queue->hdr_digest) {
492 pdu->hdr.flags |= NVME_TCP_F_HDGST;
493 nvmet_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
494 }
495}
496
497static void nvmet_tcp_process_resp_list(struct nvmet_tcp_queue *queue)
498{
499 struct llist_node *node;
Sagi Grimbergb8a12e92020-06-24 12:27:16 -0700500 struct nvmet_tcp_cmd *cmd;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800501
Sagi Grimbergb8a12e92020-06-24 12:27:16 -0700502 for (node = llist_del_all(&queue->resp_list); node; node = node->next) {
503 cmd = llist_entry(node, struct nvmet_tcp_cmd, lentry);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800504 list_add(&cmd->entry, &queue->resp_send_list);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800505 queue->send_list_len++;
506 }
507}
508
509static struct nvmet_tcp_cmd *nvmet_tcp_fetch_cmd(struct nvmet_tcp_queue *queue)
510{
511 queue->snd_cmd = list_first_entry_or_null(&queue->resp_send_list,
512 struct nvmet_tcp_cmd, entry);
513 if (!queue->snd_cmd) {
514 nvmet_tcp_process_resp_list(queue);
515 queue->snd_cmd =
516 list_first_entry_or_null(&queue->resp_send_list,
517 struct nvmet_tcp_cmd, entry);
518 if (unlikely(!queue->snd_cmd))
519 return NULL;
520 }
521
522 list_del_init(&queue->snd_cmd->entry);
523 queue->send_list_len--;
524
525 if (nvmet_tcp_need_data_out(queue->snd_cmd))
526 nvmet_setup_c2h_data_pdu(queue->snd_cmd);
527 else if (nvmet_tcp_need_data_in(queue->snd_cmd))
528 nvmet_setup_r2t_pdu(queue->snd_cmd);
529 else
530 nvmet_setup_response_pdu(queue->snd_cmd);
531
532 return queue->snd_cmd;
533}
534
535static void nvmet_tcp_queue_response(struct nvmet_req *req)
536{
537 struct nvmet_tcp_cmd *cmd =
538 container_of(req, struct nvmet_tcp_cmd, req);
539 struct nvmet_tcp_queue *queue = cmd->queue;
540
541 llist_add(&cmd->lentry, &queue->resp_list);
Mark Wunderlichf7790e52020-08-28 01:00:53 +0000542 queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &cmd->queue->io_work);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800543}
544
545static int nvmet_try_send_data_pdu(struct nvmet_tcp_cmd *cmd)
546{
547 u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
548 int left = sizeof(*cmd->data_pdu) - cmd->offset + hdgst;
549 int ret;
550
551 ret = kernel_sendpage(cmd->queue->sock, virt_to_page(cmd->data_pdu),
552 offset_in_page(cmd->data_pdu) + cmd->offset,
Sagi Grimberg4eea8042020-05-04 22:20:02 -0700553 left, MSG_DONTWAIT | MSG_MORE | MSG_SENDPAGE_NOTLAST);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800554 if (ret <= 0)
555 return ret;
556
557 cmd->offset += ret;
558 left -= ret;
559
560 if (left)
561 return -EAGAIN;
562
563 cmd->state = NVMET_TCP_SEND_DATA;
564 cmd->offset = 0;
565 return 1;
566}
567
Sagi Grimberg98fd5c72020-03-12 16:06:38 -0700568static int nvmet_try_send_data(struct nvmet_tcp_cmd *cmd, bool last_in_batch)
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800569{
570 struct nvmet_tcp_queue *queue = cmd->queue;
571 int ret;
572
573 while (cmd->cur_sg) {
574 struct page *page = sg_page(cmd->cur_sg);
575 u32 left = cmd->cur_sg->length - cmd->offset;
Sagi Grimberg98fd5c72020-03-12 16:06:38 -0700576 int flags = MSG_DONTWAIT;
577
578 if ((!last_in_batch && cmd->queue->send_list_len) ||
579 cmd->wbytes_done + left < cmd->req.transfer_len ||
580 queue->data_digest || !queue->nvme_sq.sqhd_disabled)
Sagi Grimberg4eea8042020-05-04 22:20:02 -0700581 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800582
583 ret = kernel_sendpage(cmd->queue->sock, page, cmd->offset,
Sagi Grimberg98fd5c72020-03-12 16:06:38 -0700584 left, flags);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800585 if (ret <= 0)
586 return ret;
587
588 cmd->offset += ret;
589 cmd->wbytes_done += ret;
590
591 /* Done with sg?*/
592 if (cmd->offset == cmd->cur_sg->length) {
593 cmd->cur_sg = sg_next(cmd->cur_sg);
594 cmd->offset = 0;
595 }
596 }
597
598 if (queue->data_digest) {
599 cmd->state = NVMET_TCP_SEND_DDGST;
600 cmd->offset = 0;
601 } else {
Sagi Grimberg70583292019-03-08 15:41:21 -0800602 if (queue->nvme_sq.sqhd_disabled) {
603 cmd->queue->snd_cmd = NULL;
604 nvmet_tcp_put_cmd(cmd);
605 } else {
606 nvmet_setup_response_pdu(cmd);
607 }
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800608 }
Sagi Grimberg70583292019-03-08 15:41:21 -0800609
610 if (queue->nvme_sq.sqhd_disabled) {
611 kfree(cmd->iov);
Sagi Grimberg30f27d52019-09-13 10:36:40 -0700612 sgl_free(cmd->req.sg);
Sagi Grimberg70583292019-03-08 15:41:21 -0800613 }
614
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800615 return 1;
616
617}
618
619static int nvmet_try_send_response(struct nvmet_tcp_cmd *cmd,
620 bool last_in_batch)
621{
622 u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
623 int left = sizeof(*cmd->rsp_pdu) - cmd->offset + hdgst;
624 int flags = MSG_DONTWAIT;
625 int ret;
626
627 if (!last_in_batch && cmd->queue->send_list_len)
Sagi Grimberg4eea8042020-05-04 22:20:02 -0700628 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800629 else
630 flags |= MSG_EOR;
631
632 ret = kernel_sendpage(cmd->queue->sock, virt_to_page(cmd->rsp_pdu),
633 offset_in_page(cmd->rsp_pdu) + cmd->offset, left, flags);
634 if (ret <= 0)
635 return ret;
636 cmd->offset += ret;
637 left -= ret;
638
639 if (left)
640 return -EAGAIN;
641
642 kfree(cmd->iov);
Sagi Grimberg30f27d52019-09-13 10:36:40 -0700643 sgl_free(cmd->req.sg);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800644 cmd->queue->snd_cmd = NULL;
645 nvmet_tcp_put_cmd(cmd);
646 return 1;
647}
648
649static int nvmet_try_send_r2t(struct nvmet_tcp_cmd *cmd, bool last_in_batch)
650{
651 u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
652 int left = sizeof(*cmd->r2t_pdu) - cmd->offset + hdgst;
653 int flags = MSG_DONTWAIT;
654 int ret;
655
656 if (!last_in_batch && cmd->queue->send_list_len)
Sagi Grimberg4eea8042020-05-04 22:20:02 -0700657 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800658 else
659 flags |= MSG_EOR;
660
661 ret = kernel_sendpage(cmd->queue->sock, virt_to_page(cmd->r2t_pdu),
662 offset_in_page(cmd->r2t_pdu) + cmd->offset, left, flags);
663 if (ret <= 0)
664 return ret;
665 cmd->offset += ret;
666 left -= ret;
667
668 if (left)
669 return -EAGAIN;
670
671 cmd->queue->snd_cmd = NULL;
672 return 1;
673}
674
Sagi Grimberge90d1722020-03-12 16:06:39 -0700675static int nvmet_try_send_ddgst(struct nvmet_tcp_cmd *cmd, bool last_in_batch)
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800676{
677 struct nvmet_tcp_queue *queue = cmd->queue;
678 struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
679 struct kvec iov = {
680 .iov_base = &cmd->exp_ddgst + cmd->offset,
681 .iov_len = NVME_TCP_DIGEST_LENGTH - cmd->offset
682 };
683 int ret;
684
Sagi Grimberge90d1722020-03-12 16:06:39 -0700685 if (!last_in_batch && cmd->queue->send_list_len)
686 msg.msg_flags |= MSG_MORE;
Sagi Grimbergf381ab12020-05-12 18:01:43 -0700687 else
688 msg.msg_flags |= MSG_EOR;
Sagi Grimberge90d1722020-03-12 16:06:39 -0700689
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800690 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
691 if (unlikely(ret <= 0))
692 return ret;
693
694 cmd->offset += ret;
Sagi Grimberg70583292019-03-08 15:41:21 -0800695
696 if (queue->nvme_sq.sqhd_disabled) {
697 cmd->queue->snd_cmd = NULL;
698 nvmet_tcp_put_cmd(cmd);
699 } else {
700 nvmet_setup_response_pdu(cmd);
701 }
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800702 return 1;
703}
704
705static int nvmet_tcp_try_send_one(struct nvmet_tcp_queue *queue,
706 bool last_in_batch)
707{
708 struct nvmet_tcp_cmd *cmd = queue->snd_cmd;
709 int ret = 0;
710
711 if (!cmd || queue->state == NVMET_TCP_Q_DISCONNECTING) {
712 cmd = nvmet_tcp_fetch_cmd(queue);
713 if (unlikely(!cmd))
714 return 0;
715 }
716
717 if (cmd->state == NVMET_TCP_SEND_DATA_PDU) {
718 ret = nvmet_try_send_data_pdu(cmd);
719 if (ret <= 0)
720 goto done_send;
721 }
722
723 if (cmd->state == NVMET_TCP_SEND_DATA) {
Sagi Grimberg98fd5c72020-03-12 16:06:38 -0700724 ret = nvmet_try_send_data(cmd, last_in_batch);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800725 if (ret <= 0)
726 goto done_send;
727 }
728
729 if (cmd->state == NVMET_TCP_SEND_DDGST) {
Sagi Grimberge90d1722020-03-12 16:06:39 -0700730 ret = nvmet_try_send_ddgst(cmd, last_in_batch);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800731 if (ret <= 0)
732 goto done_send;
733 }
734
735 if (cmd->state == NVMET_TCP_SEND_R2T) {
736 ret = nvmet_try_send_r2t(cmd, last_in_batch);
737 if (ret <= 0)
738 goto done_send;
739 }
740
741 if (cmd->state == NVMET_TCP_SEND_RESPONSE)
742 ret = nvmet_try_send_response(cmd, last_in_batch);
743
744done_send:
745 if (ret < 0) {
746 if (ret == -EAGAIN)
747 return 0;
748 return ret;
749 }
750
751 return 1;
752}
753
754static int nvmet_tcp_try_send(struct nvmet_tcp_queue *queue,
755 int budget, int *sends)
756{
757 int i, ret = 0;
758
759 for (i = 0; i < budget; i++) {
760 ret = nvmet_tcp_try_send_one(queue, i == budget - 1);
Sagi Grimberg0236d342020-05-18 10:47:48 -0700761 if (unlikely(ret < 0)) {
762 nvmet_tcp_socket_error(queue, ret);
763 goto done;
764 } else if (ret == 0) {
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800765 break;
Sagi Grimberg0236d342020-05-18 10:47:48 -0700766 }
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800767 (*sends)++;
768 }
Sagi Grimberg0236d342020-05-18 10:47:48 -0700769done:
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800770 return ret;
771}
772
773static void nvmet_prepare_receive_pdu(struct nvmet_tcp_queue *queue)
774{
775 queue->offset = 0;
776 queue->left = sizeof(struct nvme_tcp_hdr);
777 queue->cmd = NULL;
778 queue->rcv_state = NVMET_TCP_RECV_PDU;
779}
780
781static void nvmet_tcp_free_crypto(struct nvmet_tcp_queue *queue)
782{
783 struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
784
785 ahash_request_free(queue->rcv_hash);
786 ahash_request_free(queue->snd_hash);
787 crypto_free_ahash(tfm);
788}
789
790static int nvmet_tcp_alloc_crypto(struct nvmet_tcp_queue *queue)
791{
792 struct crypto_ahash *tfm;
793
794 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
795 if (IS_ERR(tfm))
796 return PTR_ERR(tfm);
797
798 queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
799 if (!queue->snd_hash)
800 goto free_tfm;
801 ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
802
803 queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
804 if (!queue->rcv_hash)
805 goto free_snd_hash;
806 ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
807
808 return 0;
809free_snd_hash:
810 ahash_request_free(queue->snd_hash);
811free_tfm:
812 crypto_free_ahash(tfm);
813 return -ENOMEM;
814}
815
816
817static int nvmet_tcp_handle_icreq(struct nvmet_tcp_queue *queue)
818{
819 struct nvme_tcp_icreq_pdu *icreq = &queue->pdu.icreq;
820 struct nvme_tcp_icresp_pdu *icresp = &queue->pdu.icresp;
821 struct msghdr msg = {};
822 struct kvec iov;
823 int ret;
824
825 if (le32_to_cpu(icreq->hdr.plen) != sizeof(struct nvme_tcp_icreq_pdu)) {
826 pr_err("bad nvme-tcp pdu length (%d)\n",
827 le32_to_cpu(icreq->hdr.plen));
828 nvmet_tcp_fatal_error(queue);
829 }
830
831 if (icreq->pfv != NVME_TCP_PFV_1_0) {
832 pr_err("queue %d: bad pfv %d\n", queue->idx, icreq->pfv);
833 return -EPROTO;
834 }
835
836 if (icreq->hpda != 0) {
837 pr_err("queue %d: unsupported hpda %d\n", queue->idx,
838 icreq->hpda);
839 return -EPROTO;
840 }
841
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800842 queue->hdr_digest = !!(icreq->digest & NVME_TCP_HDR_DIGEST_ENABLE);
843 queue->data_digest = !!(icreq->digest & NVME_TCP_DATA_DIGEST_ENABLE);
844 if (queue->hdr_digest || queue->data_digest) {
845 ret = nvmet_tcp_alloc_crypto(queue);
846 if (ret)
847 return ret;
848 }
849
850 memset(icresp, 0, sizeof(*icresp));
851 icresp->hdr.type = nvme_tcp_icresp;
852 icresp->hdr.hlen = sizeof(*icresp);
853 icresp->hdr.pdo = 0;
854 icresp->hdr.plen = cpu_to_le32(icresp->hdr.hlen);
855 icresp->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
Sagi Grimberg9cda34e2020-02-25 16:42:27 -0800856 icresp->maxdata = cpu_to_le32(0x400000); /* 16M arbitrary limit */
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800857 icresp->cpda = 0;
858 if (queue->hdr_digest)
859 icresp->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
860 if (queue->data_digest)
861 icresp->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
862
863 iov.iov_base = icresp;
864 iov.iov_len = sizeof(*icresp);
865 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
866 if (ret < 0)
867 goto free_crypto;
868
869 queue->state = NVMET_TCP_Q_LIVE;
870 nvmet_prepare_receive_pdu(queue);
871 return 0;
872free_crypto:
873 if (queue->hdr_digest || queue->data_digest)
874 nvmet_tcp_free_crypto(queue);
875 return ret;
876}
877
878static void nvmet_tcp_handle_req_failure(struct nvmet_tcp_queue *queue,
879 struct nvmet_tcp_cmd *cmd, struct nvmet_req *req)
880{
Logan Gunthorpec73eebc2019-10-23 10:35:40 -0600881 size_t data_len = le32_to_cpu(req->cmd->common.dptr.sgl.length);
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800882 int ret;
883
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800884 if (!nvme_is_write(cmd->req.cmd) ||
Logan Gunthorpec73eebc2019-10-23 10:35:40 -0600885 data_len > cmd->req.port->inline_data_size) {
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800886 nvmet_prepare_receive_pdu(queue);
887 return;
888 }
889
890 ret = nvmet_tcp_map_data(cmd);
891 if (unlikely(ret)) {
892 pr_err("queue %d: failed to map data\n", queue->idx);
893 nvmet_tcp_fatal_error(queue);
894 return;
895 }
896
897 queue->rcv_state = NVMET_TCP_RECV_DATA;
898 nvmet_tcp_map_pdu_iovec(cmd);
899 cmd->flags |= NVMET_TCP_F_INIT_FAILED;
900}
901
902static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue)
903{
904 struct nvme_tcp_data_pdu *data = &queue->pdu.data;
905 struct nvmet_tcp_cmd *cmd;
906
Ziye Yanga6ce7d72020-08-22 00:48:10 +0800907 if (likely(queue->nr_cmds))
908 cmd = &queue->cmds[data->ttag];
909 else
910 cmd = &queue->connect;
Sagi Grimberg872d26a2018-12-03 17:52:15 -0800911
912 if (le32_to_cpu(data->data_offset) != cmd->rbytes_done) {
913 pr_err("ttag %u unexpected data offset %u (expected %u)\n",
914 data->ttag, le32_to_cpu(data->data_offset),
915 cmd->rbytes_done);
916 /* FIXME: use path and transport errors */
917 nvmet_req_complete(&cmd->req,
918 NVME_SC_INVALID_FIELD | NVME_SC_DNR);
919 return -EPROTO;
920 }
921
922 cmd->pdu_len = le32_to_cpu(data->data_length);
923 cmd->pdu_recv = 0;
924 nvmet_tcp_map_pdu_iovec(cmd);
925 queue->cmd = cmd;
926 queue->rcv_state = NVMET_TCP_RECV_DATA;
927
928 return 0;
929}
930
931static int nvmet_tcp_done_recv_pdu(struct nvmet_tcp_queue *queue)
932{
933 struct nvme_tcp_hdr *hdr = &queue->pdu.cmd.hdr;
934 struct nvme_command *nvme_cmd = &queue->pdu.cmd.cmd;
935 struct nvmet_req *req;
936 int ret;
937
938 if (unlikely(queue->state == NVMET_TCP_Q_CONNECTING)) {
939 if (hdr->type != nvme_tcp_icreq) {
940 pr_err("unexpected pdu type (%d) before icreq\n",
941 hdr->type);
942 nvmet_tcp_fatal_error(queue);
943 return -EPROTO;
944 }
945 return nvmet_tcp_handle_icreq(queue);
946 }
947
948 if (hdr->type == nvme_tcp_h2c_data) {
949 ret = nvmet_tcp_handle_h2c_data_pdu(queue);
950 if (unlikely(ret))
951 return ret;
952 return 0;
953 }
954
955 queue->cmd = nvmet_tcp_get_cmd(queue);
956 if (unlikely(!queue->cmd)) {
957 /* This should never happen */
958 pr_err("queue %d: out of commands (%d) send_list_len: %d, opcode: %d",
959 queue->idx, queue->nr_cmds, queue->send_list_len,
960 nvme_cmd->common.opcode);
961 nvmet_tcp_fatal_error(queue);
962 return -ENOMEM;
963 }
964
965 req = &queue->cmd->req;
966 memcpy(req->cmd, nvme_cmd, sizeof(*nvme_cmd));
967
968 if (unlikely(!nvmet_req_init(req, &queue->nvme_cq,
969 &queue->nvme_sq, &nvmet_tcp_ops))) {
970 pr_err("failed cmd %p id %d opcode %d, data_len: %d\n",
971 req->cmd, req->cmd->common.command_id,
972 req->cmd->common.opcode,
973 le32_to_cpu(req->cmd->common.dptr.sgl.length));
974
975 nvmet_tcp_handle_req_failure(queue, queue->cmd, req);
976 return -EAGAIN;
977 }
978
979 ret = nvmet_tcp_map_data(queue->cmd);
980 if (unlikely(ret)) {
981 pr_err("queue %d: failed to map data\n", queue->idx);
982 if (nvmet_tcp_has_inline_data(queue->cmd))
983 nvmet_tcp_fatal_error(queue);
984 else
985 nvmet_req_complete(req, ret);
986 ret = -EAGAIN;
987 goto out;
988 }
989
990 if (nvmet_tcp_need_data_in(queue->cmd)) {
991 if (nvmet_tcp_has_inline_data(queue->cmd)) {
992 queue->rcv_state = NVMET_TCP_RECV_DATA;
993 nvmet_tcp_map_pdu_iovec(queue->cmd);
994 return 0;
995 }
996 /* send back R2T */
997 nvmet_tcp_queue_response(&queue->cmd->req);
998 goto out;
999 }
1000
Christoph Hellwigbe3f3112019-10-23 10:35:45 -06001001 queue->cmd->req.execute(&queue->cmd->req);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001002out:
1003 nvmet_prepare_receive_pdu(queue);
1004 return ret;
1005}
1006
1007static const u8 nvme_tcp_pdu_sizes[] = {
1008 [nvme_tcp_icreq] = sizeof(struct nvme_tcp_icreq_pdu),
1009 [nvme_tcp_cmd] = sizeof(struct nvme_tcp_cmd_pdu),
1010 [nvme_tcp_h2c_data] = sizeof(struct nvme_tcp_data_pdu),
1011};
1012
1013static inline u8 nvmet_tcp_pdu_size(u8 type)
1014{
1015 size_t idx = type;
1016
1017 return (idx < ARRAY_SIZE(nvme_tcp_pdu_sizes) &&
1018 nvme_tcp_pdu_sizes[idx]) ?
1019 nvme_tcp_pdu_sizes[idx] : 0;
1020}
1021
1022static inline bool nvmet_tcp_pdu_valid(u8 type)
1023{
1024 switch (type) {
1025 case nvme_tcp_icreq:
1026 case nvme_tcp_cmd:
1027 case nvme_tcp_h2c_data:
1028 /* fallthru */
1029 return true;
1030 }
1031
1032 return false;
1033}
1034
1035static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue)
1036{
1037 struct nvme_tcp_hdr *hdr = &queue->pdu.cmd.hdr;
1038 int len;
1039 struct kvec iov;
1040 struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
1041
1042recv:
1043 iov.iov_base = (void *)&queue->pdu + queue->offset;
1044 iov.iov_len = queue->left;
1045 len = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1046 iov.iov_len, msg.msg_flags);
1047 if (unlikely(len < 0))
1048 return len;
1049
1050 queue->offset += len;
1051 queue->left -= len;
1052 if (queue->left)
1053 return -EAGAIN;
1054
1055 if (queue->offset == sizeof(struct nvme_tcp_hdr)) {
1056 u8 hdgst = nvmet_tcp_hdgst_len(queue);
1057
1058 if (unlikely(!nvmet_tcp_pdu_valid(hdr->type))) {
1059 pr_err("unexpected pdu type %d\n", hdr->type);
1060 nvmet_tcp_fatal_error(queue);
1061 return -EIO;
1062 }
1063
1064 if (unlikely(hdr->hlen != nvmet_tcp_pdu_size(hdr->type))) {
1065 pr_err("pdu %d bad hlen %d\n", hdr->type, hdr->hlen);
1066 return -EIO;
1067 }
1068
1069 queue->left = hdr->hlen - queue->offset + hdgst;
1070 goto recv;
1071 }
1072
1073 if (queue->hdr_digest &&
1074 nvmet_tcp_verify_hdgst(queue, &queue->pdu, queue->offset)) {
1075 nvmet_tcp_fatal_error(queue); /* fatal */
1076 return -EPROTO;
1077 }
1078
1079 if (queue->data_digest &&
1080 nvmet_tcp_check_ddgst(queue, &queue->pdu)) {
1081 nvmet_tcp_fatal_error(queue); /* fatal */
1082 return -EPROTO;
1083 }
1084
1085 return nvmet_tcp_done_recv_pdu(queue);
1086}
1087
1088static void nvmet_tcp_prep_recv_ddgst(struct nvmet_tcp_cmd *cmd)
1089{
1090 struct nvmet_tcp_queue *queue = cmd->queue;
1091
Sagi Grimbergfda871c2021-02-03 15:00:01 -08001092 nvmet_tcp_recv_ddgst(queue->rcv_hash, cmd);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001093 queue->offset = 0;
1094 queue->left = NVME_TCP_DIGEST_LENGTH;
1095 queue->rcv_state = NVMET_TCP_RECV_DDGST;
1096}
1097
1098static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue)
1099{
1100 struct nvmet_tcp_cmd *cmd = queue->cmd;
1101 int ret;
1102
1103 while (msg_data_left(&cmd->recv_msg)) {
1104 ret = sock_recvmsg(cmd->queue->sock, &cmd->recv_msg,
1105 cmd->recv_msg.msg_flags);
1106 if (ret <= 0)
1107 return ret;
1108
1109 cmd->pdu_recv += ret;
1110 cmd->rbytes_done += ret;
1111 }
1112
Sagi Grimbergfda871c2021-02-03 15:00:01 -08001113 if (queue->data_digest) {
1114 nvmet_tcp_prep_recv_ddgst(cmd);
1115 return 0;
1116 }
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001117 nvmet_tcp_unmap_pdu_iovec(cmd);
1118
1119 if (!(cmd->flags & NVMET_TCP_F_INIT_FAILED) &&
1120 cmd->rbytes_done == cmd->req.transfer_len) {
Christoph Hellwigbe3f3112019-10-23 10:35:45 -06001121 cmd->req.execute(&cmd->req);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001122 }
1123
1124 nvmet_prepare_receive_pdu(queue);
1125 return 0;
1126}
1127
1128static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue)
1129{
1130 struct nvmet_tcp_cmd *cmd = queue->cmd;
1131 int ret;
1132 struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
1133 struct kvec iov = {
1134 .iov_base = (void *)&cmd->recv_ddgst + queue->offset,
1135 .iov_len = queue->left
1136 };
1137
1138 ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1139 iov.iov_len, msg.msg_flags);
1140 if (unlikely(ret < 0))
1141 return ret;
1142
1143 queue->offset += ret;
1144 queue->left -= ret;
1145 if (queue->left)
1146 return -EAGAIN;
1147
1148 if (queue->data_digest && cmd->exp_ddgst != cmd->recv_ddgst) {
1149 pr_err("queue %d: cmd %d pdu (%d) data digest error: recv %#x expected %#x\n",
1150 queue->idx, cmd->req.cmd->common.command_id,
1151 queue->pdu.cmd.hdr.type, le32_to_cpu(cmd->recv_ddgst),
1152 le32_to_cpu(cmd->exp_ddgst));
1153 nvmet_tcp_finish_cmd(cmd);
1154 nvmet_tcp_fatal_error(queue);
1155 ret = -EPROTO;
1156 goto out;
1157 }
1158
1159 if (!(cmd->flags & NVMET_TCP_F_INIT_FAILED) &&
1160 cmd->rbytes_done == cmd->req.transfer_len)
Christoph Hellwigbe3f3112019-10-23 10:35:45 -06001161 cmd->req.execute(&cmd->req);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001162 ret = 0;
1163out:
1164 nvmet_prepare_receive_pdu(queue);
1165 return ret;
1166}
1167
1168static int nvmet_tcp_try_recv_one(struct nvmet_tcp_queue *queue)
1169{
Sagi Grimbergfb865852019-01-09 14:56:32 -08001170 int result = 0;
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001171
1172 if (unlikely(queue->rcv_state == NVMET_TCP_RECV_ERR))
1173 return 0;
1174
1175 if (queue->rcv_state == NVMET_TCP_RECV_PDU) {
1176 result = nvmet_tcp_try_recv_pdu(queue);
1177 if (result != 0)
1178 goto done_recv;
1179 }
1180
1181 if (queue->rcv_state == NVMET_TCP_RECV_DATA) {
1182 result = nvmet_tcp_try_recv_data(queue);
1183 if (result != 0)
1184 goto done_recv;
1185 }
1186
1187 if (queue->rcv_state == NVMET_TCP_RECV_DDGST) {
1188 result = nvmet_tcp_try_recv_ddgst(queue);
1189 if (result != 0)
1190 goto done_recv;
1191 }
1192
1193done_recv:
1194 if (result < 0) {
1195 if (result == -EAGAIN)
1196 return 0;
1197 return result;
1198 }
1199 return 1;
1200}
1201
1202static int nvmet_tcp_try_recv(struct nvmet_tcp_queue *queue,
1203 int budget, int *recvs)
1204{
1205 int i, ret = 0;
1206
1207 for (i = 0; i < budget; i++) {
1208 ret = nvmet_tcp_try_recv_one(queue);
Sagi Grimberg0236d342020-05-18 10:47:48 -07001209 if (unlikely(ret < 0)) {
1210 nvmet_tcp_socket_error(queue, ret);
1211 goto done;
1212 } else if (ret == 0) {
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001213 break;
Sagi Grimberg0236d342020-05-18 10:47:48 -07001214 }
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001215 (*recvs)++;
1216 }
Sagi Grimberg0236d342020-05-18 10:47:48 -07001217done:
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001218 return ret;
1219}
1220
1221static void nvmet_tcp_schedule_release_queue(struct nvmet_tcp_queue *queue)
1222{
1223 spin_lock(&queue->state_lock);
1224 if (queue->state != NVMET_TCP_Q_DISCONNECTING) {
1225 queue->state = NVMET_TCP_Q_DISCONNECTING;
1226 schedule_work(&queue->release_work);
1227 }
1228 spin_unlock(&queue->state_lock);
1229}
1230
Wunderlich, Markd8e7b462021-03-31 21:38:30 +00001231static inline void nvmet_tcp_arm_queue_deadline(struct nvmet_tcp_queue *queue)
1232{
1233 queue->poll_end = jiffies + usecs_to_jiffies(idle_poll_period_usecs);
1234}
1235
1236static bool nvmet_tcp_check_queue_deadline(struct nvmet_tcp_queue *queue,
1237 int ops)
1238{
1239 if (!idle_poll_period_usecs)
1240 return false;
1241
1242 if (ops)
1243 nvmet_tcp_arm_queue_deadline(queue);
1244
1245 return !time_after(jiffies, queue->poll_end);
1246}
1247
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001248static void nvmet_tcp_io_work(struct work_struct *w)
1249{
1250 struct nvmet_tcp_queue *queue =
1251 container_of(w, struct nvmet_tcp_queue, io_work);
1252 bool pending;
1253 int ret, ops = 0;
1254
1255 do {
1256 pending = false;
1257
1258 ret = nvmet_tcp_try_recv(queue, NVMET_TCP_RECV_BUDGET, &ops);
Sagi Grimberg0236d342020-05-18 10:47:48 -07001259 if (ret > 0)
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001260 pending = true;
Sagi Grimberg0236d342020-05-18 10:47:48 -07001261 else if (ret < 0)
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001262 return;
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001263
1264 ret = nvmet_tcp_try_send(queue, NVMET_TCP_SEND_BUDGET, &ops);
Sagi Grimberg0236d342020-05-18 10:47:48 -07001265 if (ret > 0)
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001266 pending = true;
Sagi Grimberg0236d342020-05-18 10:47:48 -07001267 else if (ret < 0)
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001268 return;
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001269
1270 } while (pending && ops < NVMET_TCP_IO_WORK_BUDGET);
1271
1272 /*
Wunderlich, Markd8e7b462021-03-31 21:38:30 +00001273 * Requeue the worker if idle deadline period is in progress or any
1274 * ops activity was recorded during the do-while loop above.
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001275 */
Wunderlich, Markd8e7b462021-03-31 21:38:30 +00001276 if (nvmet_tcp_check_queue_deadline(queue, ops) || pending)
Mark Wunderlichf7790e52020-08-28 01:00:53 +00001277 queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &queue->io_work);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001278}
1279
1280static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue,
1281 struct nvmet_tcp_cmd *c)
1282{
1283 u8 hdgst = nvmet_tcp_hdgst_len(queue);
1284
1285 c->queue = queue;
1286 c->req.port = queue->port->nport;
1287
1288 c->cmd_pdu = page_frag_alloc(&queue->pf_cache,
1289 sizeof(*c->cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1290 if (!c->cmd_pdu)
1291 return -ENOMEM;
1292 c->req.cmd = &c->cmd_pdu->cmd;
1293
1294 c->rsp_pdu = page_frag_alloc(&queue->pf_cache,
1295 sizeof(*c->rsp_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1296 if (!c->rsp_pdu)
1297 goto out_free_cmd;
Max Gurtovoyfc6c9732019-04-08 18:39:59 +03001298 c->req.cqe = &c->rsp_pdu->cqe;
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001299
1300 c->data_pdu = page_frag_alloc(&queue->pf_cache,
1301 sizeof(*c->data_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1302 if (!c->data_pdu)
1303 goto out_free_rsp;
1304
1305 c->r2t_pdu = page_frag_alloc(&queue->pf_cache,
1306 sizeof(*c->r2t_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1307 if (!c->r2t_pdu)
1308 goto out_free_data;
1309
1310 c->recv_msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1311
1312 list_add_tail(&c->entry, &queue->free_list);
1313
1314 return 0;
1315out_free_data:
1316 page_frag_free(c->data_pdu);
1317out_free_rsp:
1318 page_frag_free(c->rsp_pdu);
1319out_free_cmd:
1320 page_frag_free(c->cmd_pdu);
1321 return -ENOMEM;
1322}
1323
1324static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c)
1325{
1326 page_frag_free(c->r2t_pdu);
1327 page_frag_free(c->data_pdu);
1328 page_frag_free(c->rsp_pdu);
1329 page_frag_free(c->cmd_pdu);
1330}
1331
1332static int nvmet_tcp_alloc_cmds(struct nvmet_tcp_queue *queue)
1333{
1334 struct nvmet_tcp_cmd *cmds;
1335 int i, ret = -EINVAL, nr_cmds = queue->nr_cmds;
1336
1337 cmds = kcalloc(nr_cmds, sizeof(struct nvmet_tcp_cmd), GFP_KERNEL);
1338 if (!cmds)
1339 goto out;
1340
1341 for (i = 0; i < nr_cmds; i++) {
1342 ret = nvmet_tcp_alloc_cmd(queue, cmds + i);
1343 if (ret)
1344 goto out_free;
1345 }
1346
1347 queue->cmds = cmds;
1348
1349 return 0;
1350out_free:
1351 while (--i >= 0)
1352 nvmet_tcp_free_cmd(cmds + i);
1353 kfree(cmds);
1354out:
1355 return ret;
1356}
1357
1358static void nvmet_tcp_free_cmds(struct nvmet_tcp_queue *queue)
1359{
1360 struct nvmet_tcp_cmd *cmds = queue->cmds;
1361 int i;
1362
1363 for (i = 0; i < queue->nr_cmds; i++)
1364 nvmet_tcp_free_cmd(cmds + i);
1365
1366 nvmet_tcp_free_cmd(&queue->connect);
1367 kfree(cmds);
1368}
1369
1370static void nvmet_tcp_restore_socket_callbacks(struct nvmet_tcp_queue *queue)
1371{
1372 struct socket *sock = queue->sock;
1373
1374 write_lock_bh(&sock->sk->sk_callback_lock);
1375 sock->sk->sk_data_ready = queue->data_ready;
1376 sock->sk->sk_state_change = queue->state_change;
1377 sock->sk->sk_write_space = queue->write_space;
1378 sock->sk->sk_user_data = NULL;
1379 write_unlock_bh(&sock->sk->sk_callback_lock);
1380}
1381
1382static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd)
1383{
1384 nvmet_req_uninit(&cmd->req);
1385 nvmet_tcp_unmap_pdu_iovec(cmd);
Sagi Grimberg35d1a932019-08-02 20:29:11 -07001386 kfree(cmd->iov);
Sagi Grimberg30f27d52019-09-13 10:36:40 -07001387 sgl_free(cmd->req.sg);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001388}
1389
1390static void nvmet_tcp_uninit_data_in_cmds(struct nvmet_tcp_queue *queue)
1391{
1392 struct nvmet_tcp_cmd *cmd = queue->cmds;
1393 int i;
1394
1395 for (i = 0; i < queue->nr_cmds; i++, cmd++) {
1396 if (nvmet_tcp_need_data_in(cmd))
1397 nvmet_tcp_finish_cmd(cmd);
1398 }
1399
1400 if (!queue->nr_cmds && nvmet_tcp_need_data_in(&queue->connect)) {
1401 /* failed in connect */
1402 nvmet_tcp_finish_cmd(&queue->connect);
1403 }
1404}
1405
1406static void nvmet_tcp_release_queue_work(struct work_struct *w)
1407{
1408 struct nvmet_tcp_queue *queue =
1409 container_of(w, struct nvmet_tcp_queue, release_work);
1410
1411 mutex_lock(&nvmet_tcp_queue_mutex);
1412 list_del_init(&queue->queue_list);
1413 mutex_unlock(&nvmet_tcp_queue_mutex);
1414
1415 nvmet_tcp_restore_socket_callbacks(queue);
1416 flush_work(&queue->io_work);
1417
1418 nvmet_tcp_uninit_data_in_cmds(queue);
1419 nvmet_sq_destroy(&queue->nvme_sq);
1420 cancel_work_sync(&queue->io_work);
1421 sock_release(queue->sock);
1422 nvmet_tcp_free_cmds(queue);
1423 if (queue->hdr_digest || queue->data_digest)
1424 nvmet_tcp_free_crypto(queue);
1425 ida_simple_remove(&nvmet_tcp_queue_ida, queue->idx);
1426
1427 kfree(queue);
1428}
1429
1430static void nvmet_tcp_data_ready(struct sock *sk)
1431{
1432 struct nvmet_tcp_queue *queue;
1433
1434 read_lock_bh(&sk->sk_callback_lock);
1435 queue = sk->sk_user_data;
1436 if (likely(queue))
Mark Wunderlichf7790e52020-08-28 01:00:53 +00001437 queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &queue->io_work);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001438 read_unlock_bh(&sk->sk_callback_lock);
1439}
1440
1441static void nvmet_tcp_write_space(struct sock *sk)
1442{
1443 struct nvmet_tcp_queue *queue;
1444
1445 read_lock_bh(&sk->sk_callback_lock);
1446 queue = sk->sk_user_data;
1447 if (unlikely(!queue))
1448 goto out;
1449
1450 if (unlikely(queue->state == NVMET_TCP_Q_CONNECTING)) {
1451 queue->write_space(sk);
1452 goto out;
1453 }
1454
1455 if (sk_stream_is_writeable(sk)) {
1456 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Mark Wunderlichf7790e52020-08-28 01:00:53 +00001457 queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &queue->io_work);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001458 }
1459out:
1460 read_unlock_bh(&sk->sk_callback_lock);
1461}
1462
1463static void nvmet_tcp_state_change(struct sock *sk)
1464{
1465 struct nvmet_tcp_queue *queue;
1466
Sagi Grimbergb5332a92021-03-21 00:08:49 -07001467 read_lock_bh(&sk->sk_callback_lock);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001468 queue = sk->sk_user_data;
1469 if (!queue)
1470 goto done;
1471
1472 switch (sk->sk_state) {
1473 case TCP_FIN_WAIT1:
1474 case TCP_CLOSE_WAIT:
1475 case TCP_CLOSE:
1476 /* FALLTHRU */
1477 sk->sk_user_data = NULL;
1478 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:
Sagi Grimbergb5332a92021-03-21 00:08:49 -07001485 read_unlock_bh(&sk->sk_callback_lock);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001486}
1487
1488static int nvmet_tcp_set_queue_sock(struct nvmet_tcp_queue *queue)
1489{
1490 struct socket *sock = queue->sock;
Israel Rukshin89275a92019-08-18 12:08:55 +03001491 struct inet_sock *inet = inet_sk(sock->sk);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001492 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 */
Christoph Hellwigc4335942020-05-28 07:12:10 +02001509 sock_no_linger(sock->sk);
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001510
Christoph Hellwig6e434962020-05-28 07:12:11 +02001511 if (so_priority > 0)
1512 sock_set_priority(sock->sk, so_priority);
Wunderlich, Mark43cc6682020-01-16 00:46:16 +00001513
Israel Rukshin89275a92019-08-18 12:08:55 +03001514 /* Set socket type of service */
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +02001515 if (inet->rcv_tos > 0)
1516 ip_sock_set_tos(sock->sk, inet->rcv_tos);
Israel Rukshin89275a92019-08-18 12:08:55 +03001517
Sagi Grimberg0fbcfb02021-02-05 11:47:25 -08001518 ret = 0;
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001519 write_lock_bh(&sock->sk->sk_callback_lock);
Sagi Grimberg0fbcfb02021-02-05 11:47:25 -08001520 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;
Wunderlich, Markd8e7b462021-03-31 21:38:30 +00001534 if (idle_poll_period_usecs)
1535 nvmet_tcp_arm_queue_deadline(queue);
Sagi Grimberg0fbcfb02021-02-05 11:47:25 -08001536 queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &queue->io_work);
1537 }
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001538 write_unlock_bh(&sock->sk->sk_callback_lock);
1539
Sagi Grimberg0fbcfb02021-02-05 11:47:25 -08001540 return ret;
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001541}
1542
1543static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,
1544 struct socket *newsock)
1545{
1546 struct nvmet_tcp_queue *queue;
1547 int ret;
1548
1549 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
1550 if (!queue)
1551 return -ENOMEM;
1552
1553 INIT_WORK(&queue->release_work, nvmet_tcp_release_queue_work);
1554 INIT_WORK(&queue->io_work, nvmet_tcp_io_work);
1555 queue->sock = newsock;
1556 queue->port = port;
1557 queue->nr_cmds = 0;
1558 spin_lock_init(&queue->state_lock);
1559 queue->state = NVMET_TCP_Q_CONNECTING;
1560 INIT_LIST_HEAD(&queue->free_list);
1561 init_llist_head(&queue->resp_list);
1562 INIT_LIST_HEAD(&queue->resp_send_list);
1563
1564 queue->idx = ida_simple_get(&nvmet_tcp_queue_ida, 0, 0, GFP_KERNEL);
1565 if (queue->idx < 0) {
1566 ret = queue->idx;
1567 goto out_free_queue;
1568 }
1569
1570 ret = nvmet_tcp_alloc_cmd(queue, &queue->connect);
1571 if (ret)
1572 goto out_ida_remove;
1573
1574 ret = nvmet_sq_init(&queue->nvme_sq);
1575 if (ret)
1576 goto out_free_connect;
1577
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001578 nvmet_prepare_receive_pdu(queue);
1579
1580 mutex_lock(&nvmet_tcp_queue_mutex);
1581 list_add_tail(&queue->queue_list, &nvmet_tcp_queue_list);
1582 mutex_unlock(&nvmet_tcp_queue_mutex);
1583
1584 ret = nvmet_tcp_set_queue_sock(queue);
1585 if (ret)
1586 goto out_destroy_sq;
1587
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001588 return 0;
1589out_destroy_sq:
1590 mutex_lock(&nvmet_tcp_queue_mutex);
1591 list_del_init(&queue->queue_list);
1592 mutex_unlock(&nvmet_tcp_queue_mutex);
1593 nvmet_sq_destroy(&queue->nvme_sq);
1594out_free_connect:
1595 nvmet_tcp_free_cmd(&queue->connect);
1596out_ida_remove:
1597 ida_simple_remove(&nvmet_tcp_queue_ida, queue->idx);
1598out_free_queue:
1599 kfree(queue);
1600 return ret;
1601}
1602
1603static void nvmet_tcp_accept_work(struct work_struct *w)
1604{
1605 struct nvmet_tcp_port *port =
1606 container_of(w, struct nvmet_tcp_port, accept_work);
1607 struct socket *newsock;
1608 int ret;
1609
1610 while (true) {
1611 ret = kernel_accept(port->sock, &newsock, O_NONBLOCK);
1612 if (ret < 0) {
1613 if (ret != -EAGAIN)
1614 pr_warn("failed to accept err=%d\n", ret);
1615 return;
1616 }
1617 ret = nvmet_tcp_alloc_queue(port, newsock);
1618 if (ret) {
1619 pr_err("failed to allocate queue\n");
1620 sock_release(newsock);
1621 }
1622 }
1623}
1624
1625static void nvmet_tcp_listen_data_ready(struct sock *sk)
1626{
1627 struct nvmet_tcp_port *port;
1628
1629 read_lock_bh(&sk->sk_callback_lock);
1630 port = sk->sk_user_data;
1631 if (!port)
1632 goto out;
1633
1634 if (sk->sk_state == TCP_LISTEN)
1635 schedule_work(&port->accept_work);
1636out:
1637 read_unlock_bh(&sk->sk_callback_lock);
1638}
1639
1640static int nvmet_tcp_add_port(struct nvmet_port *nport)
1641{
1642 struct nvmet_tcp_port *port;
1643 __kernel_sa_family_t af;
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02001644 int ret;
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001645
1646 port = kzalloc(sizeof(*port), GFP_KERNEL);
1647 if (!port)
1648 return -ENOMEM;
1649
1650 switch (nport->disc_addr.adrfam) {
1651 case NVMF_ADDR_FAMILY_IP4:
1652 af = AF_INET;
1653 break;
1654 case NVMF_ADDR_FAMILY_IP6:
1655 af = AF_INET6;
1656 break;
1657 default:
1658 pr_err("address family %d not supported\n",
1659 nport->disc_addr.adrfam);
1660 ret = -EINVAL;
1661 goto err_port;
1662 }
1663
1664 ret = inet_pton_with_scope(&init_net, af, nport->disc_addr.traddr,
1665 nport->disc_addr.trsvcid, &port->addr);
1666 if (ret) {
1667 pr_err("malformed ip/port passed: %s:%s\n",
1668 nport->disc_addr.traddr, nport->disc_addr.trsvcid);
1669 goto err_port;
1670 }
1671
1672 port->nport = nport;
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001673 INIT_WORK(&port->accept_work, nvmet_tcp_accept_work);
1674 if (port->nport->inline_data_size < 0)
1675 port->nport->inline_data_size = NVMET_TCP_DEF_INLINE_DATA_SIZE;
1676
1677 ret = sock_create(port->addr.ss_family, SOCK_STREAM,
1678 IPPROTO_TCP, &port->sock);
1679 if (ret) {
1680 pr_err("failed to create a socket\n");
1681 goto err_port;
1682 }
1683
1684 port->sock->sk->sk_user_data = port;
1685 port->data_ready = port->sock->sk->sk_data_ready;
1686 port->sock->sk->sk_data_ready = nvmet_tcp_listen_data_ready;
Christoph Hellwigb58f0e82020-05-28 07:12:09 +02001687 sock_set_reuseaddr(port->sock->sk);
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02001688 tcp_sock_set_nodelay(port->sock->sk);
Christoph Hellwig6e434962020-05-28 07:12:11 +02001689 if (so_priority > 0)
1690 sock_set_priority(port->sock->sk, so_priority);
Wunderlich, Mark43cc6682020-01-16 00:46:16 +00001691
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001692 ret = kernel_bind(port->sock, (struct sockaddr *)&port->addr,
1693 sizeof(port->addr));
1694 if (ret) {
1695 pr_err("failed to bind port socket %d\n", ret);
1696 goto err_sock;
1697 }
1698
1699 ret = kernel_listen(port->sock, 128);
1700 if (ret) {
1701 pr_err("failed to listen %d on port sock\n", ret);
1702 goto err_sock;
1703 }
1704
1705 nport->priv = port;
1706 pr_info("enabling port %d (%pISpc)\n",
1707 le16_to_cpu(nport->disc_addr.portid), &port->addr);
1708
1709 return 0;
1710
1711err_sock:
1712 sock_release(port->sock);
1713err_port:
1714 kfree(port);
1715 return ret;
1716}
1717
1718static void nvmet_tcp_remove_port(struct nvmet_port *nport)
1719{
1720 struct nvmet_tcp_port *port = nport->priv;
1721
1722 write_lock_bh(&port->sock->sk->sk_callback_lock);
1723 port->sock->sk->sk_data_ready = port->data_ready;
1724 port->sock->sk->sk_user_data = NULL;
1725 write_unlock_bh(&port->sock->sk->sk_callback_lock);
1726 cancel_work_sync(&port->accept_work);
1727
1728 sock_release(port->sock);
1729 kfree(port);
1730}
1731
1732static void nvmet_tcp_delete_ctrl(struct nvmet_ctrl *ctrl)
1733{
1734 struct nvmet_tcp_queue *queue;
1735
1736 mutex_lock(&nvmet_tcp_queue_mutex);
1737 list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list)
1738 if (queue->nvme_sq.ctrl == ctrl)
1739 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1740 mutex_unlock(&nvmet_tcp_queue_mutex);
1741}
1742
1743static u16 nvmet_tcp_install_queue(struct nvmet_sq *sq)
1744{
1745 struct nvmet_tcp_queue *queue =
1746 container_of(sq, struct nvmet_tcp_queue, nvme_sq);
1747
1748 if (sq->qid == 0) {
1749 /* Let inflight controller teardown complete */
1750 flush_scheduled_work();
1751 }
1752
1753 queue->nr_cmds = sq->size * 2;
1754 if (nvmet_tcp_alloc_cmds(queue))
1755 return NVME_SC_INTERNAL;
1756 return 0;
1757}
1758
1759static void nvmet_tcp_disc_port_addr(struct nvmet_req *req,
1760 struct nvmet_port *nport, char *traddr)
1761{
1762 struct nvmet_tcp_port *port = nport->priv;
1763
1764 if (inet_addr_is_any((struct sockaddr *)&port->addr)) {
1765 struct nvmet_tcp_cmd *cmd =
1766 container_of(req, struct nvmet_tcp_cmd, req);
1767 struct nvmet_tcp_queue *queue = cmd->queue;
1768
1769 sprintf(traddr, "%pISc", (struct sockaddr *)&queue->sockaddr);
1770 } else {
1771 memcpy(traddr, nport->disc_addr.traddr, NVMF_TRADDR_SIZE);
1772 }
1773}
1774
Max Gurtovoya40aae62020-06-01 20:05:20 +03001775static const struct nvmet_fabrics_ops nvmet_tcp_ops = {
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001776 .owner = THIS_MODULE,
1777 .type = NVMF_TRTYPE_TCP,
1778 .msdbd = 1,
Sagi Grimberg872d26a2018-12-03 17:52:15 -08001779 .add_port = nvmet_tcp_add_port,
1780 .remove_port = nvmet_tcp_remove_port,
1781 .queue_response = nvmet_tcp_queue_response,
1782 .delete_ctrl = nvmet_tcp_delete_ctrl,
1783 .install_queue = nvmet_tcp_install_queue,
1784 .disc_traddr = nvmet_tcp_disc_port_addr,
1785};
1786
1787static int __init nvmet_tcp_init(void)
1788{
1789 int ret;
1790
1791 nvmet_tcp_wq = alloc_workqueue("nvmet_tcp_wq", WQ_HIGHPRI, 0);
1792 if (!nvmet_tcp_wq)
1793 return -ENOMEM;
1794
1795 ret = nvmet_register_transport(&nvmet_tcp_ops);
1796 if (ret)
1797 goto err;
1798
1799 return 0;
1800err:
1801 destroy_workqueue(nvmet_tcp_wq);
1802 return ret;
1803}
1804
1805static void __exit nvmet_tcp_exit(void)
1806{
1807 struct nvmet_tcp_queue *queue;
1808
1809 nvmet_unregister_transport(&nvmet_tcp_ops);
1810
1811 flush_scheduled_work();
1812 mutex_lock(&nvmet_tcp_queue_mutex);
1813 list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list)
1814 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1815 mutex_unlock(&nvmet_tcp_queue_mutex);
1816 flush_scheduled_work();
1817
1818 destroy_workqueue(nvmet_tcp_wq);
1819}
1820
1821module_init(nvmet_tcp_init);
1822module_exit(nvmet_tcp_exit);
1823
1824MODULE_LICENSE("GPL v2");
1825MODULE_ALIAS("nvmet-transport-3"); /* 3 == NVMF_TRTYPE_TCP */