Spdk/python/spdk/sma/device/nvmf_tcp.py
Konrad Sztyber 048fb36ac3 sma: initial Storage Management Agent structures
Storage Management Agent is a gRPC server that provides an abstraction
layer above the SPDK RPC interface.  The interface aims to provide a set
of methods for managing various protocols (e.g. NVMe, virtio-blk) while
hiding the details of a particular transport.

The external API is defined by `lib/python/spdk/sma/proto/sma.proto`
protobuf file.  It defines the generic gRPC service methods and their
requests/responses.  Device-specific messages are defined in their own
files.  This patch also defines messages for creating NVMe and NVMe/TCP
devices.

This patch implements a gRPC service that delegates the work to a
specific device type.  A DeviceManager is a class that implements some
of the methods defined by the service for a given type of devices (e.g.
NVMe, virtio-blk, NVMe/TCP, etc.).  For now, only the RPC for creating a
device is implemented, others are added in subsequent patches.

The series implements the generic calls as well as their NVMe/TCP
implementation.  Support for other devce types could be easily added by
creating a new device manager and defining its protobuf parameter
definition.

Signed-off-by: Konrad Sztyber <konrad.sztyber@intel.com>
Change-Id: I17cde3b31d3514878f1027cfcd112b48848f6123
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/10273
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
2022-05-18 07:24:06 +00:00

107 lines
4.7 KiB
Python

import grpc
import logging
from spdk.rpc.client import JSONRPCException
from .device import DeviceManager, DeviceException
from ..common import format_volume_id
from ..proto import sma_pb2
from ..proto import nvmf_tcp_pb2
class NvmfTcpDeviceManager(DeviceManager):
def __init__(self, client):
super().__init__('nvmf_tcp', client)
def init(self, config):
self._has_transport = self._create_transport()
def _create_transport(self):
try:
with self._client() as client:
transports = client.call('nvmf_get_transports')
for transport in transports:
if transport['trtype'].lower() == 'tcp':
return True
# TODO: take the transport params from config
return client.call('nvmf_create_transport',
{'trtype': 'tcp'})
except JSONRPCException:
logging.error('Failed to query for NVMe/TCP transport')
return False
def _check_transport(f):
def wrapper(self, *args):
if not self._has_transport:
raise DeviceException(grpc.StatusCode.INTERNAL,
'NVMe/TCP transport is unavailable')
return f(self, *args)
return wrapper
def _get_params(self, request, params):
result = {}
for grpc_param, *rpc_param in params:
rpc_param = rpc_param[0] if rpc_param else grpc_param
result[rpc_param] = getattr(request, grpc_param)
return result
def _check_addr(self, addr, addrlist):
return next(filter(lambda a: (
a['trtype'].lower() == 'tcp' and
a['adrfam'].lower() == addr['adrfam'].lower() and
a['traddr'].lower() == addr['traddr'].lower() and
a['trsvcid'].lower() == addr['trsvcid'].lower()), addrlist), None) is not None
@_check_transport
def create_device(self, request):
params = request.nvmf_tcp
with self._client() as client:
try:
subsystems = client.call('nvmf_get_subsystems')
for subsystem in subsystems:
if subsystem['nqn'] == params.subnqn:
break
else:
subsystem = None
result = client.call('nvmf_create_subsystem',
{**self._get_params(params, [
('subnqn', 'nqn'),
('allow_any_host',)])})
except JSONRPCException:
raise DeviceException(grpc.StatusCode.INTERNAL,
'Failed to create NVMe/TCP device')
try:
for host in params.hosts:
client.call('nvmf_subsystem_add_host',
{'nqn': params.subnqn,
'host': host})
if subsystem is not None:
for host in [h['nqn'] for h in subsystem['hosts']]:
if host not in params.hosts:
client.call('nvmf_subsystem_remove_host',
{'nqn': params.subnqn,
'host': host})
addr = self._get_params(params, [
('adrfam',),
('traddr',),
('trsvcid',)])
if subsystem is None or not self._check_addr(addr,
subsystem['listen_addresses']):
client.call('nvmf_subsystem_add_listener',
{'nqn': params.subnqn,
'listen_address': {'trtype': 'tcp', **addr}})
volume_id = format_volume_id(request.volume.volume_id)
if volume_id is not None:
result = client.call('nvmf_subsystem_add_ns',
{'nqn': params.subnqn,
'namespace': {
'bdev_name': volume_id}})
except JSONRPCException:
try:
client.call('nvmf_delete_subsystem', {'nqn': params.subnqn})
except JSONRPCException:
logging.warning(f'Failed to delete subsystem: {params.subnqn}')
raise DeviceException(grpc.StatusCode.INTERNAL,
'Failed to create NVMe/TCP device')
return sma_pb2.CreateDeviceResponse(handle=f'nvmf-tcp:{params.subnqn}')