[PATCH 2/2] nvmet-tcp: add keep-alive

From: zhenwei pi
Date: Tue Oct 27 2020 - 08:16:15 EST


Add tcp keep-alive to detect dead connections for zero KATO case.

Signed-off-by: zhenwei pi <pizhenwei@xxxxxxxxxxxxx>
---
drivers/nvme/target/tcp.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)

diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c
index dc1f0f647189..58800e914e61 100644
--- a/drivers/nvme/target/tcp.c
+++ b/drivers/nvme/target/tcp.c
@@ -1714,6 +1714,30 @@ static void nvmet_tcp_disc_port_addr(struct nvmet_req *req,
}
}

+static int nvmet_tcp_keep_alive(struct nvmet_req *req)
+{
+ struct nvmet_tcp_cmd *cmd = container_of(req, struct nvmet_tcp_cmd, req);
+ struct nvmet_tcp_queue *queue = cmd->queue;
+ struct socket *sock = queue->sock;
+ int ret;
+
+ /* TCP Keepalive options, max 5+1*3 = 8s */
+ sock_set_keepalive(sock->sk);
+ ret = tcp_sock_set_keepidle(sock->sk, 5);
+ if (ret)
+ return ret;
+
+ ret = tcp_sock_set_keepintvl(sock->sk, 1);
+ if (ret)
+ return ret;
+
+ ret = tcp_sock_set_keepcnt(sock->sk, 3);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
static const struct nvmet_fabrics_ops nvmet_tcp_ops = {
.owner = THIS_MODULE,
.type = NVMF_TRTYPE_TCP,
@@ -1724,6 +1748,7 @@ static const struct nvmet_fabrics_ops nvmet_tcp_ops = {
.delete_ctrl = nvmet_tcp_delete_ctrl,
.install_queue = nvmet_tcp_install_queue,
.disc_traddr = nvmet_tcp_disc_port_addr,
+ .keep_alive = nvmet_tcp_keep_alive,
};

static int __init nvmet_tcp_init(void)
--
2.11.0