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