From 52c1d3e1d1423746c2a9a86b336b49add3397d55 Mon Sep 17 00:00:00 2001 From: Yin Congmin Date: Wed, 10 May 2023 17:35:40 +0800 Subject: [PATCH] control/cli: add ip requirement for listener discovert controller need a specific ip/port. fixes: https://github.com/ceph/ceph-nvmeof/issues/87 Signed-off-by: Yin Congmin --- README.md | 2 +- control/cli.py | 8 +++--- control/grpc.py | 70 ++++++++++++++++------------------------------- tests/test_cli.py | 4 +-- 4 files changed, 30 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 78ace56c9..6890492fd 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ Indicate the location of the keys and certificates in the config file: $ python3 -m control.cli add_host -n nqn.2016-06.io.spdk:cnode1 -t '*' INFO:root:Allowed open host access to nqn.2016-06.io.spdk:cnode1: True - $ python3 -m control.cli create_listener -n nqn.2016-06.io.spdk:cnode1 -s 5001 + $ python3 -m control.cli create_listener -n nqn.2016-06.io.spdk:cnode1 -g gateway_name -a gateway_addr -s 5001 INFO:root:Created nqn.2016-06.io.spdk:cnode1 listener: True 3. On the storage client system (ubuntu-21.04): diff --git a/control/cli.py b/control/cli.py index 5973c3080..fb0752b13 100644 --- a/control/cli.py +++ b/control/cli.py @@ -270,10 +270,10 @@ def remove_host(self, args): @cli.cmd([ argument("-n", "--subnqn", help="Subsystem NQN", required=True), - argument("-g", "--gateway-name", help="Gateway name", default=""), + argument("-g", "--gateway-name", help="Gateway name", required=True), argument("-t", "--trtype", help="Transport type", default="TCP"), argument("-f", "--adrfam", help="Address family", default="ipv4"), - argument("-a", "--traddr", help="NVMe host IP", default=""), + argument("-a", "--traddr", help="NVMe host IP", required=True), argument("-s", "--trsvcid", help="Port number", required=True), ]) def create_listener(self, args): @@ -295,10 +295,10 @@ def create_listener(self, args): @cli.cmd([ argument("-n", "--subnqn", help="Subsystem NQN", required=True), - argument("-g", "--gateway-name", help="Gateway name", default=""), + argument("-g", "--gateway-name", help="Gateway name", required=True), argument("-t", "--trtype", help="Transport type", default="TCP"), argument("-f", "--adrfam", help="Address family", default="ipv4"), - argument("-a", "--traddr", help="NVMe host IP", default=""), + argument("-a", "--traddr", help="NVMe host IP", required=True), argument("-s", "--trsvcid", help="Port number", required=True), ]) def delete_listener(self, args): diff --git a/control/grpc.py b/control/grpc.py index 6183053d0..d6ef4c21c 100644 --- a/control/grpc.py +++ b/control/grpc.py @@ -329,30 +329,19 @@ def create_listener(self, request, context=None): self.logger.info(f"Received request to create {request.gateway_name}" f" {request.trtype} listener for {request.nqn} at" f" {request.traddr}:{request.trsvcid}.") + if request.gateway_name != self.gateway_name: + self.logger.error("gateway name mismatch.") + return False try: - if (request.gateway_name and not request.traddr) or \ - (not request.gateway_name and request.traddr): - raise Exception( - "both gateway_name and traddr or neither must be specified") - - if not request.gateway_name or \ - request.gateway_name == self.gateway_name: - if not request.traddr: - traddr = self.config.get("gateway", "addr") - if not traddr: - raise Exception("gateway.addr option is not set") - else: - traddr = request.traddr - - ret = self.spdk_rpc.nvmf.nvmf_subsystem_add_listener( - self.spdk_rpc_client, - nqn=request.nqn, - trtype=request.trtype, - traddr=traddr, - trsvcid=request.trsvcid, - adrfam=request.adrfam, - ) - self.logger.info(f"create_listener: {ret}") + ret = self.spdk_rpc.nvmf.nvmf_subsystem_add_listener( + self.spdk_rpc_client, + nqn=request.nqn, + trtype=request.trtype, + traddr=request.traddr, + trsvcid=request.trsvcid, + adrfam=request.adrfam, + ) + self.logger.info(f"create_listener: {ret}") except Exception as ex: self.logger.error(f"create_listener failed with: \n {ex}") if context: @@ -383,30 +372,19 @@ def delete_listener(self, request, context=None): self.logger.info(f"Received request to delete {request.gateway_name}" f" {request.trtype} listener for {request.nqn} at" f" {request.traddr}:{request.trsvcid}.") + if request.gateway_name != self.gateway_name: + self.logger.error("gateway name mismatch.") + return False try: - if (request.gateway_name and not request.traddr) or \ - (not request.gateway_name and request.traddr): - raise Exception( - "both gateway_name and traddr or neither must be specified") - - if not request.gateway_name or \ - request.gateway_name == self.gateway_name: - if not request.traddr: - traddr = self.config.get("gateway", "addr") - if not traddr: - raise Exception("gateway.addr option is not set") - else: - traddr = request.traddr - - ret = self.spdk_rpc.nvmf.nvmf_subsystem_remove_listener( - self.spdk_rpc_client, - nqn=request.nqn, - trtype=request.trtype, - traddr=traddr, - trsvcid=request.trsvcid, - adrfam=request.adrfam, - ) - self.logger.info(f"delete_listener: {ret}") + ret = self.spdk_rpc.nvmf.nvmf_subsystem_remove_listener( + self.spdk_rpc_client, + nqn=request.nqn, + trtype=request.trtype, + traddr=request.traddr, + trsvcid=request.trsvcid, + adrfam=request.adrfam, + ) + self.logger.info(f"delete_listener: {ret}") except Exception as ex: self.logger.error(f"delete_listener failed with: \n {ex}") if context: diff --git a/tests/test_cli.py b/tests/test_cli.py index 6a9588b95..783935f79 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -12,7 +12,7 @@ trtype = "TCP" gateway_name = socket.gethostname() addr = "127.0.0.1" -listener_list = [["-g", gateway_name, "-a", addr, "-s", "5001"], ["-s", "5002"]] +listener = ["-g", gateway_name, "-a", addr, "-s", "5001"] config = "ceph-nvmeof.conf" @@ -40,7 +40,6 @@ def test_add_host(self, caplog, host): cli(["-c", config, "add_host", "-n", subsystem, "-t", host]) assert "Failed to add" not in caplog.text - @pytest.mark.parametrize("listener", listener_list) def test_create_listener(self, caplog, listener): cli(["-c", config, "create_listener", "-n", subsystem] + listener) assert "Failed to create" not in caplog.text @@ -52,7 +51,6 @@ def test_remove_host(self, caplog, host): cli(["-c", config, "remove_host", "-n", subsystem, "-t", host]) assert "Failed to remove" not in caplog.text - @pytest.mark.parametrize("listener", listener_list) def test_delete_listener(self, caplog, listener): cli(["-c", config, "delete_listener", "-n", subsystem] + listener) assert "Failed to delete" not in caplog.text