[spp] [PATCH 2/5] controller: refactor pri launch command

ogawa.yasufumi at lab.ntt.co.jp ogawa.yasufumi at lab.ntt.co.jp
Mon Feb 4 04:11:58 CET 2019


From: Yasufumi Ogawa <ogawa.yasufumi at lab.ntt.co.jp>

This patch is to move variables related to launch command from Shell
class to SppPrimary because these vars are used only in SppPrimary
actually.

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi at lab.ntt.co.jp>
---
 src/controller/commands/pri.py | 58 ++++++++++++++++++++++++++++++++++--------
 src/controller/shell.py        | 17 ++++---------
 2 files changed, 53 insertions(+), 22 deletions(-)

diff --git a/src/controller/commands/pri.py b/src/controller/commands/pri.py
index b03524b..aa78cef 100644
--- a/src/controller/commands/pri.py
+++ b/src/controller/commands/pri.py
@@ -6,7 +6,6 @@ from __future__ import absolute_import
 from .. import spp_common
 from ..shell_lib import common
 from ..spp_common import logger
-#from .. import spp_common
 
 
 class SppPrimary(object):
@@ -25,6 +24,22 @@ class SppPrimary(object):
     def __init__(self, spp_ctl_cli):
         self.spp_ctl_cli = spp_ctl_cli
 
+        # Default args for `pri; launch`, used if given cli_config is invalid
+        self.launch_default = {
+                'mem': '-m 512',
+                'base_lcore': '1',
+                'vhost_cli': ''
+                }
+
+        # Setup template of args for `pri; launch`
+        temp = "-l __BASE_LCORE__,{} "
+        temp = temp + "__MEM__ "
+        temp = temp + "-- "
+        temp = temp + "{} {} "  # '-n 1' or '--client-id 1'
+        temp = temp + "-s {} "  # '-s 192.168.1.100:6666'
+        temp = temp + "__VHOST_CLI__"
+        self.launch_template = temp
+
     def run(self, cmd):
         """Called from do_pri() to Send command to primary process."""
 
@@ -141,7 +156,7 @@ class SppPrimary(object):
                     rports['id'], rports['rx'],  rports['tx'],
                     rports['rx_drop'], rports['tx_drop']))
 
-    def complete(self, text, line, begidx, endidx, cli_config, template):
+    def complete(self, text, line, begidx, endidx, cli_config):
         """Completion for primary process commands.
 
         Called from complete_pri() to complete primary command.
@@ -150,13 +165,7 @@ class SppPrimary(object):
         candidates = []
         tokens = line.split(' ')
 
-        template = template.replace('__MEM__', cli_config['sec_mem']['val'])
-        template = template.replace('__BASE_LCORE__', cli_config['sec_base_lcore']['val'])
-        if cli_config['sec_vhost_cli']['val']:
-            template = template.replace('__VHOST_CLI__', '--vhost-client')
-        else:
-            template = template.replace('__VHOST_CLI__', '')
-
+        # Parse command line
         if tokens[0].endswith(';'):
 
             # Show sub commands
@@ -199,7 +208,10 @@ class SppPrimary(object):
                     elif ptype == 'pcap':  # at least two cores
                         rest_core = '{}-{}'.format(int(sid), int(sid)+1)
 
-                    candidates = [template.format(
+                    temp = self._setup_launch_template(
+                            cli_config, self.launch_template,
+                            self.launch_default)
+                    candidates = [temp.format(
                         rest_core, opt_sid, sid, server_addr)]
 
         if not text:
@@ -211,6 +223,32 @@ class SppPrimary(object):
 
         return completions
 
+    def _setup_launch_template(self, cli_config, template, defaults):
+        """Check given `cli_config` for params of launch."""
+
+        if 'sec_mem' in cli_config.keys():
+            sec_mem = cli_config['sec_mem']['val']
+        else:
+            sec_mem = defaults['mem']
+        template = template.replace('__MEM__', sec_mem)
+
+        if 'sec_base_lcore' in cli_config.keys():
+            sec_base_lcore = cli_config['sec_base_lcore']['val']
+        else:
+            sec_base_lcore = defaults['base_lcore']
+        template = template.replace('__BASE_LCORE__', sec_base_lcore)
+
+        if 'sec_vhost_cli' in cli_config.keys():
+            if cli_config['sec_vhost_cli']['val']:
+                vhost_client = '--vhost-client'
+            else:
+                vhost_client = ''
+        else:
+            vhost_client = defaults['vhost_cli']
+        template = template.replace('__VHOST_CLI__', vhost_client)
+
+        return template
+
     def _get_sec_ids(self):
         sec_ids = []
         res = self.spp_ctl_cli.get('processes')
diff --git a/src/controller/shell.py b/src/controller/shell.py
index 8cf10e6..78795f7 100644
--- a/src/controller/shell.py
+++ b/src/controller/shell.py
@@ -30,7 +30,7 @@ class Shell(cmd.Cmd, object):
             'topo_size': {
                 'val': '60%', 'desc': 'Percentage or ratio of topo'},
             'sec_mem': {
-                'val':'-m 512', 'desc': 'Mem size'},
+                'val': '-m 512', 'desc': 'Mem size'},
             'sec_base_lcore': {
                 'val': '1', 'desc': 'Shared lcore among secondaryes'},
             'sec_vf_nof_lcores': {
@@ -39,14 +39,6 @@ class Shell(cmd.Cmd, object):
                 'val': '', 'desc': 'Vhost client mode'},
             }
 
-    # Setup template of `pri; launch`
-    template = "-l __BASE_LCORE__,{} "
-    template = template + "__MEM__ "
-    template = template + "-- "
-    template = template + "{} {} "  # '-n 1' or '--client-id 1'
-    template = template + "-s {} "  # '-s 192.168.1.100:6666'
-    template = template + "__VHOST_CLI__"
-
     hist_file = os.path.expanduser('~/.spp_history')
     PLUGIN_DIR = 'plugins'
 
@@ -358,8 +350,9 @@ class Shell(cmd.Cmd, object):
         """Completion for primary process commands."""
 
         line = re.sub(r'\s+', " ", line)
-        return self.primary.complete(text, line, begidx, endidx,
-                self.cli_config, self.template)
+        return self.primary.complete(
+                text, line, begidx, endidx,
+                self.cli_config)
 
     def do_nfv(self, cmd):
         """Send a command to spp_nfv specified with ID.
@@ -660,7 +653,7 @@ class Shell(cmd.Cmd, object):
         if len(tokens) == 1:
             key = tokens[0]
             if key == '':
-                for k,v in self.cli_config.items():
+                for k, v in self.cli_config.items():
                     print('- {}: "{}"\t# {}'.format(k, v['val'], v['desc']))
             elif key in self.cli_config.keys():
                 print('- {}: "{}"\t# {}'.format(
-- 
2.7.4



More information about the spp mailing list