Add vBNG test cases stats processing functionality
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / prox_vnf.py
1 # Copyright (c) 2018 Intel Corporation
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import errno
16 import logging
17 import datetime
18 import time
19
20 from yardstick.common.process import check_if_process_failed
21 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxDpdkVnfSetupEnvHelper
22 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxResourceHelper
23 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF
24 from yardstick.network_services import constants
25 from yardstick.benchmark.contexts import base as context_base
26
27 LOG = logging.getLogger(__name__)
28
29
30 class ProxApproxVnf(SampleVNF):
31
32     APP_NAME = 'PROX'
33     APP_WORD = 'PROX'
34     PROX_MODE = "Workload"
35     VNF_PROMPT = "PROX started"
36     LUA_PARAMETER_NAME = "sut"
37
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
41
42         if resource_helper_type is None:
43             resource_helper_type = ProxResourceHelper
44
45         self.prev_packets_in = 0
46         self.prev_packets_sent = 0
47         self.prev_tsc = 0
48         self.tsc_hz = 0
49         super(ProxApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
50                                             resource_helper_type)
51
52     def _vnf_up_post(self):
53         self.resource_helper.up_post()
54
55     def vnf_execute(self, cmd, *args, **kwargs):
56         # try to execute with socket commands
57         # ignore socket errors, e.g. when using force_quit
58         ignore_errors = kwargs.pop("_ignore_errors", False)
59         try:
60             return self.resource_helper.execute(cmd, *args, **kwargs)
61         except OSError as e:
62             if e.errno in {errno.EPIPE, errno.ESHUTDOWN, errno.ECONNRESET}:
63                 if ignore_errors:
64                     LOG.debug("ignoring vnf_execute exception %s for command %s", e, cmd)
65                 else:
66                     raise
67             else:
68                 raise
69
70     def collect_kpi(self):
71         # we can't get KPIs if the VNF is down
72         check_if_process_failed(self._vnf_process, 0.01)
73
74         physical_node = context_base.Context.get_physical_node_from_server(
75             self.scenario_helper.nodes[self.name])
76
77         result = {"physical_node": physical_node}
78
79         if self.resource_helper is None:
80             result.update({
81                 "packets_in": 0,
82                 "packets_dropped": 0,
83                 "packets_fwd": 0,
84                 "curr_packets_in": 0,
85                 "curr_packets_fwd": 0,
86                 "collect_stats": {"core": {}},
87             })
88             return result
89
90         if (self.tsc_hz == 0):
91             self.tsc_hz = float(self.resource_helper.sut.hz())
92             LOG.debug("TSC = %f", self.tsc_hz)
93             if (self.tsc_hz == 0):
94                 raise RuntimeError("Unable to retrieve TSC")
95
96         # use all_ports so we only use ports matched in topology
97         port_count = len(self.vnfd_helper.port_pairs.all_ports)
98         if port_count not in {1, 2, 4}:
99             raise RuntimeError("Failed ..Invalid no of ports .. "
100                                "1, 2 or 4 ports only supported at this time")
101
102         tmpPorts = [self.vnfd_helper.port_num(port_name)
103                     for port_name in self.vnfd_helper.port_pairs.all_ports]
104         ok = False
105         timeout = time.time() + constants.RETRY_TIMEOUT
106         while not ok:
107             ok, all_port_stats = self.vnf_execute('multi_port_stats', tmpPorts)
108             if time.time() > timeout:
109                 break
110
111         if ok:
112             rx_total = tx_total = tsc = 0
113             try:
114                 for single_port_stats in all_port_stats:
115                     rx_total = rx_total + single_port_stats[1]
116                     tx_total = tx_total + single_port_stats[2]
117                     tsc = tsc + single_port_stats[5]
118             except (TypeError, IndexError):
119                 LOG.error("Invalid data ...")
120                 return {}
121         else:
122             return {}
123
124         tsc = tsc / port_count
125
126         result.update({
127             "packets_in": rx_total,
128             "packets_dropped": max((tx_total - rx_total), 0),
129             "packets_fwd": tx_total,
130             # we share ProxResourceHelper with TG, but we want to collect
131             # collectd KPIs here and not TG KPIs, so use a different method name
132             "collect_stats": self.resource_helper.collect_collectd_kpi(),
133         })
134         try:
135             curr_packets_in = int(((rx_total - self.prev_packets_in) * self.tsc_hz)
136                                 / (tsc - self.prev_tsc))
137         except ZeroDivisionError:
138             LOG.error("Error.... Divide by Zero")
139             curr_packets_in = 0
140
141         try:
142             curr_packets_fwd = int(((tx_total - self.prev_packets_sent) * self.tsc_hz)
143                                 / (tsc - self.prev_tsc))
144         except ZeroDivisionError:
145             LOG.error("Error.... Divide by Zero")
146             curr_packets_fwd = 0
147
148         result["curr_packets_in"] = curr_packets_in
149         result["curr_packets_fwd"] = curr_packets_fwd
150
151         self.prev_packets_in = rx_total
152         self.prev_packets_sent = tx_total
153         self.prev_tsc = tsc
154
155         LOG.debug("%s collect KPIs %s %s", self.APP_NAME, datetime.datetime.now(), result)
156         return result
157
158     def _tear_down(self):
159         # this should be standardized for all VNFs or removed
160         self.setup_helper.tear_down()
161
162     def terminate(self):
163         # stop collectd first or we get pika errors?
164         self.resource_helper.stop_collect()
165         # try to quit with socket commands
166         # pkill is not matching, debug with pgrep
167         self.ssh_helper.execute("sudo pgrep -lax  %s" % self.setup_helper.APP_NAME)
168         self.ssh_helper.execute("sudo ps aux | grep -i %s" % self.setup_helper.APP_NAME)
169         if self._vnf_process.is_alive():
170             self.vnf_execute("stop_all")
171             self.vnf_execute("quit")
172             # hopefully quit succeeds and socket closes, so ignore force_quit socket errors
173             self.vnf_execute("force_quit", _ignore_errors=True)
174         self.setup_helper.kill_vnf()
175         self._tear_down()
176         if self._vnf_process is not None:
177             LOG.debug("joining before terminate %s", self._vnf_process.name)
178             self._vnf_process.join(constants.PROCESS_JOIN_TIMEOUT)
179             self._vnf_process.terminate()