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