Merge "Remove instantiated contexts in "test_task""
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / prox_vnf.py
1 # Copyright (c) 2017 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
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
23 from yardstick.network_services import constants
24
25 LOG = logging.getLogger(__name__)
26
27
28 class ProxApproxVnf(SampleVNF):
29
30     APP_NAME = 'PROX'
31     APP_WORD = 'PROX'
32     PROX_MODE = "Workload"
33     VNF_PROMPT = "PROX started"
34     LUA_PARAMETER_NAME = "sut"
35
36     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
37         if setup_env_helper_type is None:
38             setup_env_helper_type = ProxDpdkVnfSetupEnvHelper
39
40         if resource_helper_type is None:
41             resource_helper_type = ProxResourceHelper
42
43         self.prev_packets_in = 0
44         self.prev_packets_sent = 0
45         self.prev_tsc = 0
46         self.tsc_hz = 0
47         super(ProxApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
48                                             resource_helper_type)
49
50     def _vnf_up_post(self):
51         self.resource_helper.up_post()
52
53     def vnf_execute(self, cmd, *args, **kwargs):
54         # try to execute with socket commands
55         # ignore socket errors, e.g. when using force_quit
56         ignore_errors = kwargs.pop("_ignore_errors", False)
57         try:
58             return self.resource_helper.execute(cmd, *args, **kwargs)
59         except OSError as e:
60             if e.errno in {errno.EPIPE, errno.ESHUTDOWN, errno.ECONNRESET}:
61                 if ignore_errors:
62                     LOG.debug("ignoring vnf_execute exception %s for command %s", e, cmd)
63                 else:
64                     raise
65             else:
66                 raise
67
68     def collect_kpi(self):
69         # we can't get KPIs if the VNF is down
70         check_if_process_failed(self._vnf_process, 0.01)
71         if self.resource_helper is None:
72             result = {
73                 "packets_in": 0,
74                 "packets_dropped": 0,
75                 "packets_fwd": 0,
76                 "collect_stats": {"core": {}},
77             }
78             return result
79
80         if (self.tsc_hz == 0):
81             self.tsc_hz = float(self.resource_helper.sut.hz())
82             LOG.debug("TSC = %f", self.tsc_hz)
83             if (self.tsc_hz == 0):
84                 raise RuntimeError("Unable to retrieve TSC")
85
86         # use all_ports so we only use ports matched in topology
87         port_count = len(self.vnfd_helper.port_pairs.all_ports)
88         if port_count not in {1, 2, 4}:
89             raise RuntimeError("Failed ..Invalid no of ports .. "
90                                "1, 2 or 4 ports only supported at this time")
91
92         self.port_stats = self.vnf_execute('port_stats', range(port_count))
93         try:
94             rx_total = self.port_stats[6]
95             tx_total = self.port_stats[7]
96             tsc = self.port_stats[10]
97         except IndexError:
98             LOG.debug("port_stats parse fail ")
99             # return empty dict so we don't mess up existing KPIs
100             return {}
101
102         result = {
103             "packets_in": rx_total,
104             "packets_dropped": max((tx_total - rx_total), 0),
105             "packets_fwd": tx_total,
106             # we share ProxResourceHelper with TG, but we want to collect
107             # collectd KPIs here and not TG KPIs, so use a different method name
108             "collect_stats": self.resource_helper.collect_collectd_kpi(),
109         }
110         curr_packets_in = int(((rx_total - self.prev_packets_in) * self.tsc_hz)
111                                 / (tsc - self.prev_tsc) * port_count)
112         curr_packets_fwd = int(((tx_total - self.prev_packets_sent) * self.tsc_hz)
113                                 / (tsc - self.prev_tsc) * port_count)
114
115         result["curr_packets_in"] = curr_packets_in
116         result["curr_packets_fwd"] = curr_packets_fwd
117
118         self.prev_packets_in = rx_total
119         self.prev_packets_sent = tx_total
120         self.prev_tsc = tsc
121
122         LOG.debug("%s collect KPIs %s %s", self.APP_NAME, datetime.datetime.now(), result)
123         return result
124
125     def _tear_down(self):
126         # this should be standardized for all VNFs or removed
127         self.setup_helper.tear_down()
128
129     def terminate(self):
130         # stop collectd first or we get pika errors?
131         self.resource_helper.stop_collect()
132         # try to quit with socket commands
133         # pkill is not matching, debug with pgrep
134         self.ssh_helper.execute("sudo pgrep -lax  %s" % self.setup_helper.APP_NAME)
135         self.ssh_helper.execute("sudo ps aux | grep -i %s" % self.setup_helper.APP_NAME)
136         if self._vnf_process.is_alive():
137             self.vnf_execute("stop_all")
138             self.vnf_execute("quit")
139             # hopefully quit succeeds and socket closes, so ignore force_quit socket errors
140             self.vnf_execute("force_quit", _ignore_errors=True)
141         self.setup_helper.kill_vnf()
142         self._tear_down()
143         if self._vnf_process is not None:
144             LOG.debug("joining before terminate %s", self._vnf_process.name)
145             self._vnf_process.join(constants.PROCESS_JOIN_TIMEOUT)
146             self._vnf_process.terminate()