2018-03-09 21:19:34 +01:00
|
|
|
import json
|
|
|
|
import sys
|
2017-06-06 14:22:03 -07:00
|
|
|
|
2018-03-19 17:37:58 -07:00
|
|
|
from . import app
|
|
|
|
from . import bdev
|
2018-06-13 08:31:20 +09:00
|
|
|
from . import ioat
|
2018-03-19 17:37:58 -07:00
|
|
|
from . import iscsi
|
|
|
|
from . import log
|
|
|
|
from . import lvol
|
|
|
|
from . import nbd
|
|
|
|
from . import net
|
2018-07-05 03:46:48 -04:00
|
|
|
from . import nvme
|
2018-03-19 17:37:58 -07:00
|
|
|
from . import nvmf
|
|
|
|
from . import pmem
|
|
|
|
from . import subsystem
|
2018-11-29 06:12:26 -05:00
|
|
|
from . import trace
|
2018-03-19 17:37:58 -07:00
|
|
|
from . import vhost
|
2018-05-30 22:49:45 -04:00
|
|
|
from . import client as rpc_client
|
2018-03-19 17:37:58 -07:00
|
|
|
|
2017-06-06 14:22:03 -07:00
|
|
|
|
2018-05-02 13:50:39 +09:00
|
|
|
def start_subsystem_init(client):
|
2018-07-17 12:00:20 +02:00
|
|
|
"""Start initialization of subsystems"""
|
2018-05-02 13:50:39 +09:00
|
|
|
return client.call('start_subsystem_init')
|
|
|
|
|
|
|
|
|
2018-11-08 13:24:50 -07:00
|
|
|
def wait_subsystem_init(client):
|
|
|
|
"""Block until subsystems have been initialized"""
|
|
|
|
return client.call('wait_subsystem_init')
|
|
|
|
|
|
|
|
|
2018-07-17 12:00:20 +02:00
|
|
|
def get_rpc_methods(client, current=None):
|
|
|
|
"""Get list of supported RPC methods.
|
|
|
|
Args:
|
|
|
|
current: Get list of RPC methods only callable in the current state.
|
|
|
|
"""
|
2018-05-04 10:39:27 +09:00
|
|
|
params = {}
|
|
|
|
|
2018-07-17 12:00:20 +02:00
|
|
|
if current:
|
|
|
|
params['current'] = current
|
2018-05-04 10:39:27 +09:00
|
|
|
|
|
|
|
return client.call('get_rpc_methods', params)
|
2018-03-09 21:19:34 +01:00
|
|
|
|
|
|
|
|
2019-03-21 18:02:04 -04:00
|
|
|
def get_spdk_version(client):
|
|
|
|
"""Get SPDK version"""
|
|
|
|
return client.call('get_spdk_version')
|
|
|
|
|
|
|
|
|
2018-10-09 14:00:56 -04:00
|
|
|
def _json_dump(config, fd, indent):
|
2018-09-10 11:18:08 -07:00
|
|
|
if indent is None:
|
|
|
|
indent = 2
|
|
|
|
elif indent < 0:
|
|
|
|
indent = None
|
2018-10-09 14:00:56 -04:00
|
|
|
json.dump(config, fd, indent=indent)
|
|
|
|
fd.write('\n')
|
2018-09-10 11:18:08 -07:00
|
|
|
|
|
|
|
|
2018-10-09 14:00:56 -04:00
|
|
|
def save_config(client, fd, indent=2):
|
2018-09-10 11:18:08 -07:00
|
|
|
"""Write current (live) configuration of SPDK subsystems and targets to stdout.
|
2018-07-17 12:00:20 +02:00
|
|
|
Args:
|
2018-10-09 14:00:56 -04:00
|
|
|
fd: opened file descriptor where data will be saved
|
2018-07-17 12:00:20 +02:00
|
|
|
indent: Indent level. Value less than 0 mean compact mode.
|
2018-09-10 11:18:08 -07:00
|
|
|
Default indent level is 2.
|
2018-07-17 12:00:20 +02:00
|
|
|
"""
|
2018-03-09 21:19:34 +01:00
|
|
|
config = {
|
|
|
|
'subsystems': []
|
|
|
|
}
|
|
|
|
|
2018-03-27 14:31:52 -07:00
|
|
|
for elem in client.call('get_subsystems'):
|
2018-03-09 21:19:34 +01:00
|
|
|
cfg = {
|
|
|
|
'subsystem': elem['subsystem'],
|
2018-03-27 14:31:52 -07:00
|
|
|
'config': client.call('get_subsystem_config', {"name": elem['subsystem']})
|
2018-03-09 21:19:34 +01:00
|
|
|
}
|
|
|
|
config['subsystems'].append(cfg)
|
|
|
|
|
2018-10-09 14:00:56 -04:00
|
|
|
_json_dump(config, fd, indent)
|
2018-03-09 21:19:34 +01:00
|
|
|
|
|
|
|
|
2018-10-09 14:00:56 -04:00
|
|
|
def load_config(client, fd):
|
2018-09-10 11:18:08 -07:00
|
|
|
"""Configure SPDK subsystems and targets using JSON RPC read from stdin.
|
2018-07-17 12:00:20 +02:00
|
|
|
Args:
|
2018-10-09 14:00:56 -04:00
|
|
|
fd: opened file descriptor where data will be taken from
|
2018-07-17 12:00:20 +02:00
|
|
|
"""
|
2018-10-09 14:00:56 -04:00
|
|
|
json_config = json.load(fd)
|
2018-05-14 16:58:01 +02:00
|
|
|
|
2018-06-21 10:53:46 +09:00
|
|
|
# remove subsystems with no config
|
2018-05-14 16:58:01 +02:00
|
|
|
subsystems = json_config['subsystems']
|
2018-06-21 10:53:46 +09:00
|
|
|
for subsystem in list(subsystems):
|
|
|
|
if not subsystem['config']:
|
|
|
|
subsystems.remove(subsystem)
|
|
|
|
|
|
|
|
# check if methods in the config file are known
|
|
|
|
allowed_methods = client.call('get_rpc_methods')
|
2018-11-09 13:21:34 +01:00
|
|
|
if not subsystems and 'start_subsystem_init' in allowed_methods:
|
|
|
|
start_subsystem_init(client)
|
|
|
|
return
|
|
|
|
|
2018-06-21 10:53:46 +09:00
|
|
|
for subsystem in list(subsystems):
|
|
|
|
config = subsystem['config']
|
|
|
|
for elem in list(config):
|
|
|
|
if 'method' not in elem or elem['method'] not in allowed_methods:
|
|
|
|
raise rpc_client.JSONRPCException("Unknown method was included in the config file")
|
|
|
|
|
2018-05-14 16:58:01 +02:00
|
|
|
while subsystems:
|
|
|
|
allowed_methods = client.call('get_rpc_methods', {'current': True})
|
|
|
|
allowed_found = False
|
|
|
|
|
|
|
|
for subsystem in list(subsystems):
|
|
|
|
config = subsystem['config']
|
|
|
|
for elem in list(config):
|
2018-06-21 10:53:46 +09:00
|
|
|
if 'method' not in elem or elem['method'] not in allowed_methods:
|
2018-05-14 16:58:01 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
client.call(elem['method'], elem['params'])
|
|
|
|
config.remove(elem)
|
|
|
|
allowed_found = True
|
|
|
|
|
|
|
|
if not config:
|
|
|
|
subsystems.remove(subsystem)
|
|
|
|
|
|
|
|
if 'start_subsystem_init' in allowed_methods:
|
2018-11-09 13:21:34 +01:00
|
|
|
start_subsystem_init(client)
|
2018-05-14 16:58:01 +02:00
|
|
|
allowed_found = True
|
|
|
|
|
2018-06-21 10:53:46 +09:00
|
|
|
if not allowed_found:
|
|
|
|
break
|
|
|
|
|
|
|
|
if subsystems:
|
|
|
|
print("Some configs were skipped because the RPC state that can call them passed over.")
|
2018-05-16 21:45:39 +02:00
|
|
|
|
|
|
|
|
2018-10-09 14:00:56 -04:00
|
|
|
def save_subsystem_config(client, fd, indent=2, name=None):
|
2018-09-10 11:18:08 -07:00
|
|
|
"""Write current (live) configuration of SPDK subsystem to stdout.
|
2018-07-17 12:00:20 +02:00
|
|
|
Args:
|
2018-10-09 14:00:56 -04:00
|
|
|
fd: opened file descriptor where data will be saved
|
2018-07-17 12:00:20 +02:00
|
|
|
indent: Indent level. Value less than 0 mean compact mode.
|
2018-09-10 11:18:08 -07:00
|
|
|
Default is indent level 2.
|
2018-07-17 12:00:20 +02:00
|
|
|
"""
|
2018-06-22 08:44:54 +09:00
|
|
|
cfg = {
|
2018-07-17 12:00:20 +02:00
|
|
|
'subsystem': name,
|
|
|
|
'config': client.call('get_subsystem_config', {"name": name})
|
2018-06-22 08:44:54 +09:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:00:56 -04:00
|
|
|
_json_dump(cfg, fd, indent)
|
2018-06-22 08:44:54 +09:00
|
|
|
|
|
|
|
|
2018-10-09 14:00:56 -04:00
|
|
|
def load_subsystem_config(client, fd):
|
2018-09-10 11:18:08 -07:00
|
|
|
"""Configure SPDK subsystem using JSON RPC read from stdin.
|
2018-07-17 12:00:20 +02:00
|
|
|
Args:
|
2018-10-09 14:00:56 -04:00
|
|
|
fd: opened file descriptor where data will be taken from
|
2018-07-17 12:00:20 +02:00
|
|
|
"""
|
2018-10-09 14:00:56 -04:00
|
|
|
subsystem = json.load(fd)
|
2018-05-16 21:45:39 +02:00
|
|
|
|
2018-06-25 08:06:09 +09:00
|
|
|
if not subsystem['config']:
|
|
|
|
return
|
|
|
|
|
|
|
|
allowed_methods = client.call('get_rpc_methods')
|
|
|
|
config = subsystem['config']
|
|
|
|
for elem in list(config):
|
|
|
|
if 'method' not in elem or elem['method'] not in allowed_methods:
|
|
|
|
raise rpc_client.JSONRPCException("Unknown method was included in the config file")
|
|
|
|
|
|
|
|
allowed_methods = client.call('get_rpc_methods', {'current': True})
|
|
|
|
for elem in list(config):
|
|
|
|
if 'method' not in elem or elem['method'] not in allowed_methods:
|
2018-05-16 21:45:39 +02:00
|
|
|
continue
|
2018-06-25 08:06:09 +09:00
|
|
|
|
2018-05-16 21:45:39 +02:00
|
|
|
client.call(elem['method'], elem['params'])
|
2018-06-25 08:06:09 +09:00
|
|
|
config.remove(elem)
|
|
|
|
|
|
|
|
if config:
|
|
|
|
print("Some configs were skipped because they cannot be called in the current RPC state.")
|