Skip to content

Commit

Permalink
Do not allow using the discovery NQN in the CLI commands.
Browse files Browse the repository at this point in the history
Fixes #299

Signed-off-by: Gil Bregman <gbregman@il.ibm.com>
  • Loading branch information
gbregman committed Oct 31, 2023
1 parent 6fa029d commit 76ae9ec
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions control/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ class DiscoveryService:
discovery_port: Discovery controller's listening port
"""

DISCOVERY_NQN = "nqn.2014-08.org.nvmexpress.discovery"

def __init__(self, config):
self.version = 1
self.config = config
Expand Down
45 changes: 43 additions & 2 deletions control/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .proto import gateway_pb2 as pb2
from .proto import gateway_pb2_grpc as pb2_grpc
from .config import GatewayConfig
from .discovery import DiscoveryService

MAX_ANA_GROUPS = 4

Expand Down Expand Up @@ -244,11 +245,18 @@ def delete_bdev(self, request, context=None):
with self.rpc_lock:
return self.delete_bdev_safe(request, context)

def is_discovery_nqn(self, nqn) -> bool:
return nqn == DiscoveryService.DISCOVERY_NQN

def create_subsystem_safe(self, request, context=None):
"""Creates a subsystem."""

self.logger.info(
f"Received request to create subsystem {request.subsystem_nqn}, ana reporting: {request.ana_reporting} ")

if self.is_discovery_nqn(request.subsystem_nqn):
raise Exception(f"Can't create a discovery subsystem")

min_cntlid = self.config.getint_with_default("gateway", "min_controller_id", 1)
max_cntlid = self.config.getint_with_default("gateway", "max_controller_id", 65519)
if not request.serial_number:
Expand Down Expand Up @@ -296,6 +304,10 @@ def delete_subsystem_safe(self, request, context=None):

self.logger.info(
f"Received request to delete subsystem {request.subsystem_nqn}")

if self.is_discovery_nqn(request.subsystem_nqn):
raise Exception(f"Can't delete a discovery subsystem")

try:
ret = rpc_nvmf.nvmf_delete_subsystem(
self.spdk_rpc_client,
Expand Down Expand Up @@ -326,11 +338,16 @@ def delete_subsystem(self, request, context=None):

def add_namespace_safe(self, request, context=None):
"""Adds a namespace to a subsystem."""
if request.anagrpid > MAX_ANA_GROUPS:
raise Exception(f"Error group ID {request.anagrpid} is more than configured maximum {MAX_ANA_GROUPS}\n")

self.logger.info(f"Received request to add {request.bdev_name} to"
f" {request.subsystem_nqn}")

if request.anagrpid > MAX_ANA_GROUPS:
raise Exception(f"Error group ID {request.anagrpid} is more than configured maximum {MAX_ANA_GROUPS}")

if self.is_discovery_nqn(request.subsystem_nqn):
raise Exception(f"Can't add a namespace to a discovery subsystem")

try:
nsid = rpc_nvmf.nvmf_subsystem_add_ns(
self.spdk_rpc_client,
Expand Down Expand Up @@ -372,6 +389,10 @@ def remove_namespace_safe(self, request, context=None):

self.logger.info(f"Received request to remove nsid {request.nsid} from"
f" {request.subsystem_nqn}")

if self.is_discovery_nqn(request.subsystem_nqn):
raise Exception(f"Can't remove a namespace from a discovery subsystem")

try:
ret = rpc_nvmf.nvmf_subsystem_remove_ns(
self.spdk_rpc_client,
Expand Down Expand Up @@ -415,6 +436,12 @@ def matching_host_exists(self, context, subsys_nqn, host_nqn) -> bool:
def add_host_safe(self, request, context=None):
"""Adds a host to a subsystem."""

if self.is_discovery_nqn(request.subsystem_nqn):
raise Exception(f"Can't allow a host to a discovery subsystem")

if self.is_discovery_nqn(request.host_nqn):
raise Exception(f"Can't use a discovery NQN as host NQN")

try:
host_already_exist = self.matching_host_exists(context, request.subsystem_nqn, request.host_nqn)
if host_already_exist:
Expand Down Expand Up @@ -480,6 +507,12 @@ def add_host(self, request, context=None):
def remove_host_safe(self, request, context=None):
"""Removes a host from a subsystem."""

if self.is_discovery_nqn(request.subsystem_nqn):
raise Exception(f"Can't remove a host from a discovery subsystem")

if self.is_discovery_nqn(request.host_nqn):
raise Exception(f"Can't use a discovery NQN as host NQN")

try:
if request.host_nqn == "*": # Disable allow any host access
self.logger.info(
Expand Down Expand Up @@ -540,6 +573,10 @@ def create_listener_safe(self, request, context=None):
self.logger.info(f"Received request to create {request.gateway_name}"
f" {request.trtype} listener for {request.nqn} at"
f" {traddr}:{request.trsvcid}.")

if self.is_discovery_nqn(request.nqn):
raise Exception(f"Can't create a listener for a discovery subsystem")

try:
if request.gateway_name == self.gateway_name:
listener_already_exist = self.matching_listener_exists(
Expand Down Expand Up @@ -633,6 +670,10 @@ def delete_listener_safe(self, request, context=None):
self.logger.info(f"Received request to delete {request.gateway_name}"
f" {request.trtype} listener for {request.nqn} at"
f" {traddr}:{request.trsvcid}.")

if self.is_discovery_nqn(request.nqn):
raise Exception(f"Can't delete a listener from a discovery subsystem")

try:
if request.gateway_name == self.gateway_name:
ret = rpc_nvmf.nvmf_subsystem_remove_listener(
Expand Down
2 changes: 1 addition & 1 deletion control/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _start_discovery_service(self):
return

try:
rpc_nvmf.nvmf_delete_subsystem(self.spdk_rpc_ping_client, "nqn.2014-08.org.nvmexpress.discovery")
rpc_nvmf.nvmf_delete_subsystem(self.spdk_rpc_ping_client, DiscoveryService.DISCOVERY_NQN)
except Exception as ex:
self.logger.error(f" Delete Discovery subsystem returned with error: \n {ex}")
raise
Expand Down

0 comments on commit 76ae9ec

Please sign in to comment.