1 # Copyright (c) 2017 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 from yardstick.common.process import check_if_process_failed
20 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxDpdkVnfSetupEnvHelper
21 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxResourceHelper
22 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF, PROCESS_JOIN_TIMEOUT
24 LOG = logging.getLogger(__name__)
27 class ProxApproxVnf(SampleVNF):
31 PROX_MODE = "Workload"
32 VNF_PROMPT = "PROX started"
33 LUA_PARAMETER_NAME = "sut"
35 def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
36 if setup_env_helper_type is None:
37 setup_env_helper_type = ProxDpdkVnfSetupEnvHelper
39 if resource_helper_type is None:
40 resource_helper_type = ProxResourceHelper
42 super(ProxApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
45 def _vnf_up_post(self):
46 self.resource_helper.up_post()
48 def vnf_execute(self, cmd, *args, **kwargs):
49 # try to execute with socket commands
50 # ignore socket errors, e.g. when using force_quit
51 ignore_errors = kwargs.pop("_ignore_errors", False)
53 return self.resource_helper.execute(cmd, *args, **kwargs)
55 if e.errno in {errno.EPIPE, errno.ESHUTDOWN, errno.ECONNRESET}:
57 LOG.debug("ignoring vnf_execute exception %s for command %s", e, cmd)
63 def collect_kpi(self):
64 # we can't get KPIs if the VNF is down
65 check_if_process_failed(self._vnf_process)
67 if self.resource_helper is None:
72 "collect_stats": {"core": {}},
76 # use all_ports so we only use ports matched in topology
77 port_count = len(self.vnfd_helper.port_pairs.all_ports)
78 if port_count not in {1, 2, 4}:
79 raise RuntimeError("Failed ..Invalid no of ports .. "
80 "1, 2 or 4 ports only supported at this time")
82 port_stats = self.vnf_execute('port_stats', range(port_count))
84 rx_total = port_stats[6]
85 tx_total = port_stats[7]
87 LOG.error("port_stats parse fail %s", port_stats)
88 # return empty dict so we don't mess up existing KPIs
92 "packets_in": tx_total,
93 "packets_dropped": (tx_total - rx_total),
94 "packets_fwd": rx_total,
95 # we share ProxResourceHelper with TG, but we want to collect
96 # collectd KPIs here and not TG KPIs, so use a different method name
97 "collect_stats": self.resource_helper.collect_collectd_kpi(),
99 LOG.debug("%s collect KPIs %s", self.APP_NAME, result)
102 def _tear_down(self):
103 # this should be standardized for all VNFs or removed
104 self.setup_helper.tear_down()
107 # stop collectd first or we get pika errors?
108 self.resource_helper.stop_collect()
109 # try to quit with socket commands
110 # pkill is not matching, debug with pgrep
111 self.ssh_helper.execute("sudo pgrep -lax %s" % self.setup_helper.APP_NAME)
112 self.ssh_helper.execute("sudo ps aux | grep -i %s" % self.setup_helper.APP_NAME)
113 if self._vnf_process.is_alive():
114 self.vnf_execute("stop_all")
115 self.vnf_execute("quit")
116 # hopefully quit succeeds and socket closes, so ignore force_quit socket errors
117 self.vnf_execute("force_quit", _ignore_errors=True)
118 self.setup_helper.kill_vnf()
120 if self._vnf_process is not None:
121 LOG.debug("joining before terminate %s", self._vnf_process.name)
122 self._vnf_process.join(PROCESS_JOIN_TIMEOUT)
123 self._vnf_process.terminate()