Merge "NSB: fix trex config to use dpdk port number"
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / vpe_vnf.py
1 # Copyright (c) 2016-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 """ vPE (Power Edge router) VNF model definitions based on IETS Spec """
15
16 from __future__ import absolute_import
17 from __future__ import print_function
18
19
20 import os
21 import logging
22 import re
23 import posixpath
24
25 from six.moves import configparser, zip
26
27 from yardstick.common.process import check_if_process_failed
28 from yardstick.network_services.helpers.samplevnf_helper import PortPairs
29 from yardstick.network_services.pipeline import PipelineRules
30 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF, DpdkVnfSetupEnvHelper
31
32 LOG = logging.getLogger(__name__)
33
34 VPE_PIPELINE_COMMAND = """sudo {tool_path} -p {port_mask_hex} -f {cfg_file} -s {script}"""
35
36 VPE_COLLECT_KPI = """\
37 Pkts in:\s(\d+)\r\n\
38 \tPkts dropped by AH:\s(\d+)\r\n\
39 \tPkts dropped by other:\s(\d+)\
40 """
41
42
43 class ConfigCreate(object):
44
45     @staticmethod
46     def vpe_tmq(config, index):
47         tm_q = 'TM{0}'.format(index)
48         config.add_section(tm_q)
49         config.set(tm_q, 'burst_read', '24')
50         config.set(tm_q, 'burst_write', '32')
51         config.set(tm_q, 'cfg', '/tmp/full_tm_profile_10G.cfg')
52         return config
53
54     def __init__(self, vnfd_helper, socket):
55         super(ConfigCreate, self).__init__()
56         self.sw_q = -1
57         self.sink_q = -1
58         self.n_pipeline = 1
59         self.vnfd_helper = vnfd_helper
60         self.uplink_ports = self.vnfd_helper.port_pairs.uplink_ports
61         self.downlink_ports = self.vnfd_helper.port_pairs.downlink_ports
62         self.pipeline_per_port = 9
63         self.socket = socket
64
65     def vpe_initialize(self, config):
66         config.add_section('EAL')
67         config.set('EAL', 'log_level', '0')
68
69         config.add_section('PIPELINE0')
70         config.set('PIPELINE0', 'type', 'MASTER')
71         config.set('PIPELINE0', 'core', 's%sC0' % self.socket)
72
73         config.add_section('MEMPOOL0')
74         config.set('MEMPOOL0', 'pool_size', '256K')
75
76         config.add_section('MEMPOOL1')
77         config.set('MEMPOOL1', 'pool_size', '2M')
78         return config
79
80     def vpe_rxq(self, config):
81         for port in self.downlink_ports:
82             new_section = 'RXQ{0}.0'.format(self.vnfd_helper.port_num(port))
83             config.add_section(new_section)
84             config.set(new_section, 'mempool', 'MEMPOOL1')
85
86         return config
87
88     def get_sink_swq(self, parser, pipeline, k, index):
89         sink = ""
90         pktq = parser.get(pipeline, k)
91         if "SINK" in pktq:
92             self.sink_q += 1
93             sink = " SINK{0}".format(self.sink_q)
94         if "TM" in pktq:
95             sink = " TM{0}".format(index)
96         pktq = "SWQ{0}{1}".format(self.sw_q, sink)
97         return pktq
98
99     def vpe_upstream(self, vnf_cfg, index=0):
100         parser = configparser.ConfigParser()
101         parser.read(os.path.join(vnf_cfg, 'vpe_upstream'))
102
103         for pipeline in parser.sections():
104             for k, v in parser.items(pipeline):
105                 if k == "pktq_in":
106                     if "RXQ" in v:
107                         port = self.vnfd_helper.port_num(self.uplink_ports[index])
108                         value = "RXQ{0}.0".format(port)
109                     else:
110                         value = self.get_sink_swq(parser, pipeline, k, index)
111
112                     parser.set(pipeline, k, value)
113
114                 elif k == "pktq_out":
115                     if "TXQ" in v:
116                         port = self.vnfd_helper.port_num(self.downlink_ports[index])
117                         value = "TXQ{0}.0".format(port)
118                     else:
119                         self.sw_q += 1
120                         value = self.get_sink_swq(parser, pipeline, k, index)
121
122                     parser.set(pipeline, k, value)
123
124             new_pipeline = 'PIPELINE{0}'.format(self.n_pipeline)
125             if new_pipeline != pipeline:
126                 parser._sections[new_pipeline] = parser._sections[pipeline]
127                 parser._sections.pop(pipeline)
128             self.n_pipeline += 1
129         return parser
130
131     def vpe_downstream(self, vnf_cfg, index):
132         parser = configparser.ConfigParser()
133         parser.read(os.path.join(vnf_cfg, 'vpe_downstream'))
134         for pipeline in parser.sections():
135             for k, v in parser.items(pipeline):
136
137                 if k == "pktq_in":
138                     port = self.vnfd_helper.port_num(self.downlink_ports[index])
139                     if "RXQ" not in v:
140                         value = self.get_sink_swq(parser, pipeline, k, index)
141                     elif "TM" in v:
142                         value = "RXQ{0}.0 TM{1}".format(port, index)
143                     else:
144                         value = "RXQ{0}.0".format(port)
145
146                     parser.set(pipeline, k, value)
147
148                 if k == "pktq_out":
149                     port = self.vnfd_helper.port_num(self.uplink_ports[index])
150                     if "TXQ" not in v:
151                         self.sw_q += 1
152                         value = self.get_sink_swq(parser, pipeline, k, index)
153                     elif "TM" in v:
154                         value = "TXQ{0}.0 TM{1}".format(port, index)
155                     else:
156                         value = "TXQ{0}.0".format(port)
157
158                     parser.set(pipeline, k, value)
159
160             new_pipeline = 'PIPELINE{0}'.format(self.n_pipeline)
161             if new_pipeline != pipeline:
162                 parser._sections[new_pipeline] = parser._sections[pipeline]
163                 parser._sections.pop(pipeline)
164             self.n_pipeline += 1
165         return parser
166
167     def create_vpe_config(self, vnf_cfg):
168         config = configparser.ConfigParser()
169         vpe_cfg = os.path.join("/tmp/vpe_config")
170         with open(vpe_cfg, 'w') as cfg_file:
171             config = self.vpe_initialize(config)
172             config = self.vpe_rxq(config)
173             config.write(cfg_file)
174             for index in range(0, len(self.uplink_ports)):
175                 config = self.vpe_upstream(vnf_cfg, index)
176                 config.write(cfg_file)
177                 config = self.vpe_downstream(vnf_cfg, index)
178                 config = self.vpe_tmq(config, index)
179                 config.write(cfg_file)
180
181     def generate_vpe_script(self, interfaces):
182         rules = PipelineRules(pipeline_id=1)
183         for uplink_port, downlink_port in zip(self.uplink_ports, self.downlink_ports):
184
185             uplink_intf = \
186                 next(intf["virtual-interface"] for intf in interfaces
187                      if intf["name"] == uplink_port)
188             downlink_intf = \
189                 next(intf["virtual-interface"] for intf in interfaces
190                      if intf["name"] == downlink_port)
191
192             dst_port0_ip = uplink_intf["dst_ip"]
193             dst_port1_ip = downlink_intf["dst_ip"]
194             dst_port0_mac = uplink_intf["dst_mac"]
195             dst_port1_mac = downlink_intf["dst_mac"]
196
197             rules.add_firewall_script(dst_port0_ip)
198             rules.next_pipeline()
199             rules.add_flow_classification_script()
200             rules.next_pipeline()
201             rules.add_flow_action()
202             rules.next_pipeline()
203             rules.add_flow_action2()
204             rules.next_pipeline()
205             rules.add_route_script(dst_port1_ip, dst_port1_mac)
206             rules.next_pipeline()
207             rules.add_route_script2(dst_port0_ip, dst_port0_mac)
208             rules.next_pipeline(num=4)
209
210         return rules.get_string()
211
212     def generate_tm_cfg(self, vnf_cfg, index=0):
213         vnf_cfg = os.path.join(vnf_cfg, "full_tm_profile_10G.cfg")
214         if os.path.exists(vnf_cfg):
215             return open(vnf_cfg).read()
216
217
218 class VpeApproxSetupEnvHelper(DpdkVnfSetupEnvHelper):
219
220     APP_NAME = 'vPE_vnf'
221     CFG_CONFIG = "/tmp/vpe_config"
222     CFG_SCRIPT = "/tmp/vpe_script"
223     TM_CONFIG = "/tmp/full_tm_profile_10G.cfg"
224     CORES = ['0', '1', '2', '3', '4', '5']
225     PIPELINE_COMMAND = VPE_PIPELINE_COMMAND
226
227     def _build_vnf_ports(self):
228         self._port_pairs = PortPairs(self.vnfd_helper.interfaces)
229         self.uplink_ports = self._port_pairs.uplink_ports
230         self.downlink_ports = self._port_pairs.downlink_ports
231         self.all_ports = self._port_pairs.all_ports
232
233     def build_config(self):
234         vpe_vars = {
235             "bin_path": self.ssh_helper.bin_path,
236             "socket": self.socket,
237         }
238
239         self._build_vnf_ports()
240         vpe_conf = ConfigCreate(self.vnfd_helper, self.socket)
241         vpe_conf.create_vpe_config(self.scenario_helper.vnf_cfg)
242
243         config_basename = posixpath.basename(self.CFG_CONFIG)
244         script_basename = posixpath.basename(self.CFG_SCRIPT)
245         tm_basename = posixpath.basename(self.TM_CONFIG)
246         with open(self.CFG_CONFIG) as handle:
247             vpe_config = handle.read()
248
249         self.ssh_helper.upload_config_file(config_basename, vpe_config.format(**vpe_vars))
250
251         vpe_script = vpe_conf.generate_vpe_script(self.vnfd_helper.interfaces)
252         self.ssh_helper.upload_config_file(script_basename, vpe_script.format(**vpe_vars))
253
254         tm_config = vpe_conf.generate_tm_cfg(self.scenario_helper.vnf_cfg)
255         self.ssh_helper.upload_config_file(tm_basename, tm_config)
256
257         LOG.info("Provision and start the %s", self.APP_NAME)
258         LOG.info(self.CFG_CONFIG)
259         LOG.info(self.CFG_SCRIPT)
260         self._build_pipeline_kwargs()
261         return self.PIPELINE_COMMAND.format(**self.pipeline_kwargs)
262
263
264 class VpeApproxVnf(SampleVNF):
265     """ This class handles vPE VNF model-driver definitions """
266
267     APP_NAME = 'vPE_vnf'
268     APP_WORD = 'vpe'
269     COLLECT_KPI = VPE_COLLECT_KPI
270     WAIT_TIME = 20
271
272     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
273         if setup_env_helper_type is None:
274             setup_env_helper_type = VpeApproxSetupEnvHelper
275
276         super(VpeApproxVnf, self).__init__(name, vnfd, setup_env_helper_type, resource_helper_type)
277
278     def get_stats(self, *args, **kwargs):
279         raise NotImplementedError
280
281     def collect_kpi(self):
282         # we can't get KPIs if the VNF is down
283         check_if_process_failed(self._vnf_process)
284         result = {
285             'pkt_in_up_stream': 0,
286             'pkt_drop_up_stream': 0,
287             'pkt_in_down_stream': 0,
288             'pkt_drop_down_stream': 0,
289             'collect_stats': self.resource_helper.collect_kpi(),
290         }
291
292         indexes_in = [1]
293         indexes_drop = [2, 3]
294         command = 'p {0} stats port {1} 0'
295         for index, direction in ((5, 'up'), (9, 'down')):
296             key_in = "pkt_in_{0}_stream".format(direction)
297             key_drop = "pkt_drop_{0}_stream".format(direction)
298             for mode in ('in', 'out'):
299                 stats = self.vnf_execute(command.format(index, mode))
300                 match = re.search(self.COLLECT_KPI, stats, re.MULTILINE)
301                 if not match:
302                     continue
303                 result[key_in] += sum(int(match.group(x)) for x in indexes_in)
304                 result[key_drop] += sum(int(match.group(x)) for x in indexes_drop)
305
306         LOG.debug("%s collect KPIs %s", self.APP_NAME, result)
307         return result