Skip to content

Commit

Permalink
Use enum and not string for some GRPC fields.
Browse files Browse the repository at this point in the history
Fixes #253

Signed-off-by: Gil Bregman <[email protected]>
  • Loading branch information
gbregman committed Nov 29, 2023
1 parent a9a7b72 commit 9321816
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 79 deletions.
98 changes: 68 additions & 30 deletions control/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,45 +296,73 @@ def remove_host(self, args):
argument("-n", "--subnqn", help="Subsystem NQN", required=True),
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("-f", "--adrfam", help="Address family", default="IPV4"),
argument("-a", "--traddr", help="NVMe host IP", required=True),
argument("-s", "--trsvcid", help="Port number", default="4420", required=False),
])
def create_listener(self, args):
"""Creates a listener for a subsystem at a given IP/Port."""
traddr = GatewayConfig.escape_address_if_ipv6(args.traddr)
req = pb2.create_listener_req(
nqn=args.subnqn,
gateway_name=args.gateway_name,
trtype=args.trtype,
adrfam=args.adrfam,
traddr=traddr,
trsvcid=args.trsvcid,
)
ret = self.stub.create_listener(req)
self.logger.info(f"Created {args.subnqn} listener at {traddr}:{args.trsvcid}: {ret.status}")
trtype = None
adrfam = None
if args.trtype:
trtype = args.trtype.upper()
if args.adrfam:
adrfam = args.adrfam.lower()
try:
req = pb2.create_listener_req(
nqn=args.subnqn,
gateway_name=args.gateway_name,
trtype=trtype,
adrfam=adrfam,
traddr=traddr,
trsvcid=args.trsvcid,
)
ret = self.stub.create_listener(req)
self.logger.info(f"Created {args.subnqn} listener at {traddr}:{args.trsvcid}: {ret.status}")
except ValueError as err:
self.logger.error(f"{err}")
self.logger.info(f"Created {args.subnqn} listener at {traddr}:{args.trsvcid}: {False}")
raise
except Exception as ex:
self.logger.info(f"Created {args.subnqn} listener at {traddr}:{args.trsvcid}: {False}")
raise

@cli.cmd([
argument("-n", "--subnqn", help="Subsystem NQN", required=True),
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("-f", "--adrfam", help="Address family", default="IPV4"),
argument("-a", "--traddr", help="NVMe host IP", required=True),
argument("-s", "--trsvcid", help="Port number", default="4420", required=False),
])
def delete_listener(self, args):
"""Deletes a listener from a subsystem at a given IP/Port."""
traddr = GatewayConfig.escape_address_if_ipv6(args.traddr)
req = pb2.delete_listener_req(
nqn=args.subnqn,
gateway_name=args.gateway_name,
trtype=args.trtype,
adrfam=args.adrfam,
traddr=traddr,
trsvcid=args.trsvcid,
)
ret = self.stub.delete_listener(req)
self.logger.info(f"Deleted {traddr}:{args.trsvcid} from {args.subnqn}: {ret.status}")
trtype = None
adrfam = None
if args.trtype:
trtype = args.trtype.upper()
if args.adrfam:
adrfam = args.adrfam.lower()
try:
req = pb2.delete_listener_req(
nqn=args.subnqn,
gateway_name=args.gateway_name,
trtype=trtype,
adrfam=adrfam,
traddr=traddr,
trsvcid=args.trsvcid,
)
ret = self.stub.delete_listener(req)
self.logger.info(f"Deleted {traddr}:{args.trsvcid} from {args.subnqn}: {ret.status}")
except ValueError as err:
self.logger.error(f"{err}")
self.logger.info(f"Deleted {traddr}:{args.trsvcid} from {args.subnqn}: {False}")
raise
except Exception as ex:
self.logger.info(f"Deleted {traddr}:{args.trsvcid} from {args.subnqn}: {False}")
raise

@cli.cmd()
def get_subsystems(self, args):
Expand Down Expand Up @@ -363,21 +391,31 @@ def disable_spdk_nvmf_logs(self, args):
f"Disable SPDK nvmf logs: {ret.status}")

@cli.cmd([
argument("-f", "--flags", help="SPDK nvmf enable flags", \
action='store_true', required=True),
argument("-l", "--log_level", \
help="SPDK nvmf log level (ERROR, WARNING, NOTICE, INFO, DEBUG)", required=False),
argument("-p", "--log_print_level", \
help="SPDK nvmf log print level (ERROR, WARNING, NOTICE, INFO, DEBUG)", \
required=False),
])
def set_spdk_nvmf_logs(self, args):
"""Set spdk nvmf log and flags"""
req = pb2.set_spdk_nvmf_logs_req(flags=args.flags, log_level=args.log_level, \
print_level=args.log_print_level)
ret = self.stub.set_spdk_nvmf_logs(req)
self.logger.info(
f"Set SPDK nvmf logs : {ret.status}")
"""Set spdk nvmf log and print levels"""
log_level = None
print_level = None
if args.log_level:
log_level = args.log_level.upper()
if args.log_print_level:
print_level = args.log_print_level.upper()
try:
req = pb2.set_spdk_nvmf_logs_req(log_level=log_level, print_level=print_level)
ret = self.stub.set_spdk_nvmf_logs(req)
self.logger.info(f"Set SPDK nvmf logs: {ret.status}")
except ValueError as err:
self.logger.error(f"{err}")
self.logger.info(f"Set SPDK nvmf logs: {False}")
raise
except Exception as ex:
self.logger.info(f"Set SPDK nvmf logs: {False}")
raise

@cli.cmd()
def get_gateway_info(self, args):
Expand Down
74 changes: 54 additions & 20 deletions control/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,18 @@ def create_listener_safe(self, request, context=None):
"""Creates a listener for a subsystem at a given IP/Port."""
ret = True
traddr = GatewayConfig.escape_address_if_ipv6(request.traddr)
try:
trtype = pb2.TransportType.keys()[request.trtype]
except Exception:
raise Exception(f"Unknown transport type {request.trtype}")

try:
adrfam = pb2.AddressFamily.keys()[request.adrfam]
except Exception:
raise Exception(f"Unknown address family {request.adrfam}")

self.logger.info(f"Received request to create {request.gateway_name}"
f" {request.trtype} {request.adrfam} listener for {request.nqn} at"
f" {trtype} {adrfam} listener for {request.nqn} at"
f" {traddr}:{request.trsvcid}., context: {context}")

if self.is_discovery_nqn(request.nqn):
Expand All @@ -653,12 +663,12 @@ def create_listener_safe(self, request, context=None):
try:
if request.gateway_name == self.gateway_name:
listener_already_exist = self.matching_listener_exists(
context, request.nqn, request.gateway_name, request.trtype, request.traddr, request.trsvcid)
context, request.nqn, request.gateway_name, trtype, request.traddr, request.trsvcid)
if listener_already_exist:
self.logger.error(f"{request.nqn} already listens on address {request.traddr} port {request.trsvcid}")
req = {"nqn": request.nqn, "trtype": request.trtype, "traddr": request.traddr,
req = {"nqn": request.nqn, "trtype": trtype, "traddr": request.traddr,
"gateway_name": request.gateway_name,
"trsvcid": request.trsvcid, "adrfam": request.adrfam,
"trsvcid": request.trsvcid, "adrfam": adrfam,
"method": "nvmf_subsystem_add_listener", "req_id": 0}
ret = {"code": -errno.EEXIST, "message": f"{request.nqn} already listens on address {request.traddr} port {request.trsvcid}"}
msg = "\n".join(["request:", "%s" % json.dumps(req, indent=2),
Expand All @@ -669,10 +679,10 @@ def create_listener_safe(self, request, context=None):
ret = rpc_nvmf.nvmf_subsystem_add_listener(
self.spdk_rpc_client,
nqn=request.nqn,
trtype=request.trtype,
trtype=trtype,
traddr=request.traddr,
trsvcid=request.trsvcid,
adrfam=request.adrfam,
adrfam=adrfam,
)
self.logger.info(f"create_listener: {ret}")
else:
Expand Down Expand Up @@ -710,10 +720,10 @@ def create_listener_safe(self, request, context=None):
self.spdk_rpc_client,
nqn=request.nqn,
ana_state="inaccessible",
trtype=request.trtype,
trtype=trtype,
traddr=request.traddr,
trsvcid=request.trsvcid,
adrfam=request.adrfam,
adrfam=adrfam,
anagrpid=(x+1) )
except Exception as ex:
self.logger.error(f" set_listener_ana_state failed with: \n {ex}")
Expand All @@ -726,7 +736,7 @@ def create_listener_safe(self, request, context=None):
request, preserving_proto_field_name=True)
self.gateway_state.add_listener(request.nqn,
request.gateway_name,
request.trtype, request.traddr,
trtype, request.traddr,
request.trsvcid, json_req)
except Exception as ex:
self.logger.error(
Expand All @@ -743,8 +753,18 @@ def delete_listener_safe(self, request, context=None):

ret = True
traddr = GatewayConfig.escape_address_if_ipv6(request.traddr)
try:
trtype = pb2.TransportType.keys()[request.trtype]
except Exception:
raise Exception(f"Unknown transport type {request.trtype}")

try:
adrfam = pb2.AddressFamily.keys()[request.adrfam]
except Exception:
raise Exception(f"Unknown address family {request.adrfam}")

self.logger.info(f"Received request to delete {request.gateway_name}"
f" {request.trtype} listener for {request.nqn} at"
f" {trtype} listener for {request.nqn} at"
f" {traddr}:{request.trsvcid}., context: {context}")

if self.is_discovery_nqn(request.nqn):
Expand All @@ -756,10 +776,10 @@ def delete_listener_safe(self, request, context=None):
ret = rpc_nvmf.nvmf_subsystem_remove_listener(
self.spdk_rpc_client,
nqn=request.nqn,
trtype=request.trtype,
trtype=trtype,
traddr=request.traddr,
trsvcid=request.trsvcid,
adrfam=request.adrfam,
adrfam=adrfam,
)
self.logger.info(f"delete_listener: {ret}")
else:
Expand All @@ -777,7 +797,7 @@ def delete_listener_safe(self, request, context=None):
try:
self.gateway_state.remove_listener(request.nqn,
request.gateway_name,
request.trtype,
trtype,
request.traddr,
request.trsvcid)
except Exception as ex:
Expand Down Expand Up @@ -849,23 +869,37 @@ def get_spdk_nvmf_log_flags_and_level(self, request, context):
def set_spdk_nvmf_logs_safe(self, request, context):
"""Enables spdk nvmf logs"""
self.logger.info(f"Received request to set SPDK nvmf logs")
log_level = None
print_level = None
if request.log_level:
try:
log_level = pb2.LogLevel.keys()[request.log_level]
except Exception:
raise Exception(f"Unknown log level {request.log_level}")

if request.print_level:
try:
print_level = pb2.LogLevel.keys()[request.print_level]
except Exception:
raise Exception(f"Unknown print level {request.print_level}")

try:
nvmf_log_flags = [key for key in rpc_log.log_get_flags(self.spdk_rpc_client).keys() \
if key.startswith('nvmf')]
ret = [rpc_log.log_set_flag(
self.spdk_rpc_client, flag=flag) for flag in nvmf_log_flags]
self.logger.info(f"Set SPDK log flags {nvmf_log_flags} to TRUE")
if request.log_level:
ret_log = rpc_log.log_set_level(self.spdk_rpc_client, level=request.log_level)
self.logger.info(f"Set log level to: {request.log_level}")
if log_level:
ret_log = rpc_log.log_set_level(self.spdk_rpc_client, level=log_level)
self.logger.info(f"Set log level to: {log_level}")
ret.append(ret_log)
if request.print_level:
if print_level:
ret_print = rpc_log.log_set_print_level(
self.spdk_rpc_client, level=request.print_level)
self.logger.info(f"Set log print level to: {request.print_level}")
self.spdk_rpc_client, level=print_level)
self.logger.info(f"Set log print level to: {print_level}")
ret.append(ret_print)
except Exception as ex:
self.logger.error(f"set_spdk_nvmf_logs failed with: \n {ex}")
self.logger.error(f"set_spdk_nvmf_logs failed with:\n{ex}")
context.set_code(grpc.StatusCode.INTERNAL)
context.set_details(f"{ex}")
for flag in nvmf_log_flags:
Expand Down
45 changes: 36 additions & 9 deletions control/proto/gateway.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@

syntax = "proto3";

enum TransportType {
TCP = 0;
RDMA = 1;
FC = 2;
INTRA_HOST = 3;
PCIE = 4;
VFIOUSER = 5;
CUSTOM = 6;
CUSTOM_FABRICS = 7;
}

enum AddressFamily {
ipv4 = 0;
ipv6 = 1;
ib = 2;
fc = 3;
intra_host = 4;
}

enum LogLevel {
DISABLED = 0;
ERROR = 1;
WARN = 2;
NOTICE = 3;
INFO = 4;
DEBUG = 5;
}

service Gateway {
// Creates a bdev from an RBD image
rpc create_bdev(create_bdev_req) returns (bdev) {}
Expand Down Expand Up @@ -117,17 +145,17 @@ message remove_host_req {
message create_listener_req {
string nqn = 1;
string gateway_name = 2;
string trtype = 3;
string adrfam = 4;
TransportType trtype = 3;
AddressFamily adrfam = 4;
string traddr = 5;
string trsvcid = 6;
}

message delete_listener_req {
string nqn = 1;
string gateway_name = 2;
string trtype = 3;
string adrfam = 4;
TransportType trtype = 3;
AddressFamily adrfam = 4;
string traddr = 5;
string trsvcid = 6;
}
Expand All @@ -142,9 +170,8 @@ message disable_spdk_nvmf_logs_req {
}

message set_spdk_nvmf_logs_req {
bool flags = 1;
optional string log_level = 2;
optional string print_level = 3;
optional LogLevel log_level = 1;
optional LogLevel print_level = 2;
}

message get_gateway_info_req {
Expand Down Expand Up @@ -197,8 +224,8 @@ message gateway_info {

message listen_address {
string transport = 1;
string trtype = 2;
string adrfam = 3;
TransportType trtype = 2;
AddressFamily adrfam = 3;
string traddr = 4;
string trsvcid = 5;
}
Expand Down
Loading

0 comments on commit 9321816

Please sign in to comment.