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