Soft Patch Panel
 help / color / mirror / Atom feed
From: Yasufumi Ogawa <yasufum.o@gmail.com>
To: spp@dpdk.org, ferruh.yigit@intel.com, yasufum.o@gmail.com
Subject: [spp] [PATCH 3/7] cli: add filter for running pri commands
Date: Thu, 16 Jan 2020 21:02:31 +0900	[thread overview]
Message-ID: <20200116120235.31550-4-yasufum.o@gmail.com> (raw)
In-Reply-To: <20200116120235.31550-1-yasufum.o@gmail.com>

For spp_primary, some of methods such as _run_add(), _run_del() or so,
should not be executed if forwarder thread is not running. This update
is to add util function for checking the forwarder is running, and
refuse required operation if the forwarder is not running.

Signed-off-by: Yasufumi Ogawa <yasufum.o@gmail.com>
---
 src/cli/commands/pri.py | 59 ++++++++++++++++++++++++++++++++---------
 1 file changed, 46 insertions(+), 13 deletions(-)

diff --git a/src/cli/commands/pri.py b/src/cli/commands/pri.py
index 9212897..1443e9e 100644
--- a/src/cli/commands/pri.py
+++ b/src/cli/commands/pri.py
@@ -46,6 +46,20 @@ class SppPrimary(object):
         temp = temp + "{vhost_cli}"
         self.launch_template = temp
 
+    def _do_if_forwarder_exists(self, status, func, params):
+        """Execute command of func if forwarder thread is existing.
+
+        Params status is a json object, func is a method of this class such
+        as _run_add() or _run_del() or so, and params is a set of given
+        parameters.
+        """
+
+        # It includes `forwarder` if spp_primary runs a forwarder thread.
+        if 'forwarder' in status:
+            func(params)
+        else:
+            print('No forwarder is running.')
+
     def run(self, cmd, cli_config):
         """Called from do_pri() to Send command to primary process."""
 
@@ -60,11 +74,15 @@ class SppPrimary(object):
         # use short name
         common_err_codes = self.spp_ctl_cli.rest_common_error_codes
 
+        # Get status here for inspecting if forwarder exists. Do not run
+        # command such as `add` or `del` if forwarder does not exist.
+        res = self.spp_ctl_cli.get('primary/status')
+        status = res.json()
+
         if subcmd == 'status':
-            res = self.spp_ctl_cli.get('primary/status')
             if res is not None:
                 if res.status_code == 200:
-                    self.print_status(res.json())
+                    self.print_status(status)
                 elif res.status_code in common_err_codes:
                     # Print default error message
                     pass
@@ -72,16 +90,17 @@ class SppPrimary(object):
                     print('Error: unknown response from status.')
 
         elif subcmd == 'add':
-            self._run_add(params)
+            self._do_if_forwarder_exists(status, self._run_add, params)
 
         elif subcmd == 'del':
-            self._run_del(params)
+            self._do_if_forwarder_exists(status, self._run_del, params)
 
         elif subcmd == 'forward' or cmd == 'stop':
-            self._run_forward_or_stop(cmd)
+            self._do_if_forwarder_exists(status,
+                                         self._run_forward_or_stop, params)
 
         elif subcmd == 'patch':
-            self._run_patch(params)
+            self._do_if_forwarder_exists(status, self._run_patch, params)
 
         elif subcmd == 'launch':
             wait_time = float(cli_config['sec_wait_launch']['val'])
@@ -217,8 +236,10 @@ class SppPrimary(object):
                 temp = '{s6}{rid:2}  {rx:10}  {tx:10}  {rx_d:10}  {tx_d:10}'
                 for rports in json_obj['ring_ports']:
                     print(temp.format(s6=sep*6,
-                          rid=rports['id'], rx=rports['rx'], tx=rports['tx'],
-                        rx_d=rports['rx_drop'], tx_d=rports['tx_drop']))
+                                      rid=rports['id'],
+                                      rx=rports['rx'], tx=rports['tx'],
+                                      rx_d=rports['rx_drop'],
+                                      tx_d=rports['tx_drop']))
 
         except KeyError as e:
             logger.error('{} is not defined!'.format(e))
@@ -233,7 +254,11 @@ class SppPrimary(object):
         if res is not None:
             error_codes = self.spp_ctl_cli.rest_common_error_codes
             if res.status_code == 200:
-                return res.json()['forwarder']['ports']
+                if 'forwarder' in res.json():
+                    return res.json()['forwarder']['ports']
+                else:
+                    # Do nothing if there is no forwarder
+                    pass
             elif res.status_code in error_codes:
                 pass
             else:
@@ -251,7 +276,11 @@ class SppPrimary(object):
         if res is not None:
             error_codes = self.spp_ctl_cli.rest_common_error_codes
             if res.status_code == 200:
-                return res.json()['forwarder']['patches']
+                if 'forwarder' in res.json():
+                    return res.json()['forwarder']['patches']
+                else:
+                    # Do nothing if there is no forwarder
+                    pass
             elif res.status_code in error_codes:
                 pass
             else:
@@ -270,9 +299,13 @@ class SppPrimary(object):
         if res is not None:
             error_codes = self.spp_ctl_cli.rest_common_error_codes
             if res.status_code == 200:
-                ports = res.json()['forwarder']['ports']
-                patches = res.json()['forwarder']['patches']
-                return ports, patches
+                if 'forwarder' in res.json():
+                    ports = res.json()['forwarder']['ports']
+                    patches = res.json()['forwarder']['patches']
+                    return ports, patches
+                else:
+                    # Do nothing if there is no forwarder
+                    pass
             elif res.status_code in error_codes:
                 pass
             else:
-- 
2.17.1


  parent reply	other threads:[~2020-01-16 12:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-16 12:02 [spp] [PATCH 0/7] Update for fixing errors for CLI Yasufumi Ogawa
2020-01-16 12:02 ` [spp] [PATCH 1/7] shared: revise vdev prefixs of pcap and nullpmd Yasufumi Ogawa
2020-01-16 12:02 ` [spp] [PATCH 2/7] shared: fix wrong port_type in parsing dev name Yasufumi Ogawa
2020-01-16 12:02 ` Yasufumi Ogawa [this message]
2020-01-16 12:02 ` [spp] [PATCH 4/7] cli: fix error in asking ports if no pri forwarder Yasufumi Ogawa
2020-01-16 12:02 ` [spp] [PATCH 5/7] cli: fix terminated if spp_primary is not running Yasufumi Ogawa
2020-01-16 12:02 ` [spp] [PATCH 6/7] cli: move logfile to under project log dir Yasufumi Ogawa
2020-01-16 12:02 ` [spp] [PATCH 7/7] cli: fix parsing forward and stop commands Yasufumi Ogawa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200116120235.31550-4-yasufum.o@gmail.com \
    --to=yasufum.o@gmail.com \
    --cc=ferruh.yigit@intel.com \
    --cc=spp@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).