Merge "NSB: fix trex config to use dpdk port number"
[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
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, PROCESS_JOIN_TIMEOUT
23
24 LOG = logging.getLogger(__name__)
25
26
27 class ProxApproxVnf(SampleVNF):
28
29     APP_NAME = 'PROX'
30     APP_WORD = 'PROX'
31     PROX_MODE = "Workload"
32     VNF_PROMPT = "PROX started"
33     LUA_PARAMETER_NAME = "sut"
34
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
38
39         if resource_helper_type is None:
40             resource_helper_type = ProxResourceHelper
41
42         super(ProxApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
43                                             resource_helper_type)
44
45     def _vnf_up_post(self):
46         self.resource_helper.up_post()
47
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)
52         try:
53             return self.resource_helper.execute(cmd, *args, **kwargs)
54         except OSError as e:
55             if e.errno in {errno.EPIPE, errno.ESHUTDOWN, errno.ECONNRESET}:
56                 if ignore_errors:
57                     LOG.debug("ignoring vnf_execute exception %s for command %s", e, cmd)
58                 else:
59                     raise
60             else:
61                 raise
62
63     def collect_kpi(self):
64         # we can't get KPIs if the VNF is down
65         check_if_process_failed(self._vnf_process)
66
67         if self.resource_helper is None:
68             result = {
69                 "packets_in": 0,
70                 "packets_dropped": 0,
71                 "packets_fwd": 0,
72                 "collect_stats": {"core": {}},
73             }
74             return result
75
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")
81
82         port_stats = self.vnf_execute('port_stats', range(port_count))
83         try:
84             rx_total = port_stats[6]
85             tx_total = port_stats[7]
86         except IndexError:
87             LOG.error("port_stats parse fail %s", port_stats)
88             # return empty dict so we don't mess up existing KPIs
89             return {}
90
91         result = {
92             "packets_in": rx_total,
93             "packets_dropped": max((tx_total - rx_total), 0),
94             "packets_fwd": tx_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(),
98         }
99         LOG.debug("%s collect KPIs %s", self.APP_NAME, result)
100         return result
101
102     def _tear_down(self):
103         # this should be standardized for all VNFs or removed
104         self.setup_helper.tear_down()
105
106     def terminate(self):
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()
119         self._tear_down()
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()