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.
21 from yardstick.common.process import check_if_process_failed
22 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxDpdkVnfSetupEnvHelper
23 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxResourceHelper
24 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF
25 from yardstick.network_services.constants import PROCESS_JOIN_TIMEOUT
27 LOG = logging.getLogger(__name__)
30 class ProxApproxVnf(SampleVNF):
34 PROX_MODE = "Workload"
35 VNF_PROMPT = "PROX started"
36 LUA_PARAMETER_NAME = "sut"
38 def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
39 if setup_env_helper_type is None:
40 setup_env_helper_type = ProxDpdkVnfSetupEnvHelper
42 if resource_helper_type is None:
43 resource_helper_type = ProxResourceHelper
45 self.prev_packets_in = 0
46 self.prev_packets_sent = 0
47 self.prev_time = time.time()
48 super(ProxApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
51 def _vnf_up_post(self):
52 self.resource_helper.up_post()
54 def vnf_execute(self, cmd, *args, **kwargs):
55 # try to execute with socket commands
56 # ignore socket errors, e.g. when using force_quit
57 ignore_errors = kwargs.pop("_ignore_errors", False)
59 return self.resource_helper.execute(cmd, *args, **kwargs)
61 if e.errno in {errno.EPIPE, errno.ESHUTDOWN, errno.ECONNRESET}:
63 LOG.debug("ignoring vnf_execute exception %s for command %s", e, cmd)
69 def collect_kpi(self):
70 # we can't get KPIs if the VNF is down
71 check_if_process_failed(self._vnf_process)
73 if self.resource_helper is None:
78 "collect_stats": {"core": {}},
82 # use all_ports so we only use ports matched in topology
83 port_count = len(self.vnfd_helper.port_pairs.all_ports)
84 if port_count not in {1, 2, 4}:
85 raise RuntimeError("Failed ..Invalid no of ports .. "
86 "1, 2 or 4 ports only supported at this time")
88 self.port_stats = self.vnf_execute('port_stats', range(port_count))
89 curr_time = time.time()
91 rx_total = self.port_stats[6]
92 tx_total = self.port_stats[7]
94 LOG.debug("port_stats parse fail ")
95 # return empty dict so we don't mess up existing KPIs
99 "packets_in": rx_total,
100 "packets_dropped": max((tx_total - rx_total), 0),
101 "packets_fwd": tx_total,
102 # we share ProxResourceHelper with TG, but we want to collect
103 # collectd KPIs here and not TG KPIs, so use a different method name
104 "collect_stats": self.resource_helper.collect_collectd_kpi(),
106 curr_packets_in = int((rx_total - self.prev_packets_in) / (curr_time - self.prev_time))
107 curr_packets_fwd = int((tx_total - self.prev_packets_sent) / (curr_time - self.prev_time))
109 result["curr_packets_in"] = curr_packets_in
110 result["curr_packets_fwd"] = curr_packets_fwd
112 self.prev_packets_in = rx_total
113 self.prev_packets_sent = tx_total
114 self.prev_time = curr_time
116 LOG.debug("%s collect KPIs %s %s", self.APP_NAME, datetime.datetime.now(), result)
119 def _tear_down(self):
120 # this should be standardized for all VNFs or removed
121 self.setup_helper.tear_down()
124 # stop collectd first or we get pika errors?
125 self.resource_helper.stop_collect()
126 # try to quit with socket commands
127 # pkill is not matching, debug with pgrep
128 self.ssh_helper.execute("sudo pgrep -lax %s" % self.setup_helper.APP_NAME)
129 self.ssh_helper.execute("sudo ps aux | grep -i %s" % self.setup_helper.APP_NAME)
130 if self._vnf_process.is_alive():
131 self.vnf_execute("stop_all")
132 self.vnf_execute("quit")
133 # hopefully quit succeeds and socket closes, so ignore force_quit socket errors
134 self.vnf_execute("force_quit", _ignore_errors=True)
135 self.setup_helper.kill_vnf()
137 if self._vnf_process is not None:
138 LOG.debug("joining before terminate %s", self._vnf_process.name)
139 self._vnf_process.join(PROCESS_JOIN_TIMEOUT)
140 self._vnf_process.terminate()