Merge "UDP relay"
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / cgnapt_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
15 from __future__ import absolute_import
16 import time
17 import logging
18
19 from six.moves import zip
20 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF, DpdkVnfSetupEnvHelper
21
22 LOG = logging.getLogger(__name__)
23
24 # CGNAPT should work the same on all systems, we can provide the binary
25 CGNAPT_PIPELINE_COMMAND = 'sudo {tool_path} -p {ports_len_hex} -f {cfg_file} -s {script}'
26 WAIT_FOR_STATIC_NAPT = 4
27
28 CGNAPT_COLLECT_KPI = """\
29 CG-NAPT(.*\n)*\
30 Received\s(\d+),\
31 Missed\s(\d+),\
32 Dropped\s(\d+),\
33 Translated\s(\d+),\
34 ingress\
35 """
36
37
38 class CgnaptApproxSetupEnvHelper(DpdkVnfSetupEnvHelper):
39
40     APP_NAME = "vCGNAPT"
41     CFG_CONFIG = "/tmp/cgnapt_config"
42     CFG_SCRIPT = "/tmp/cgnapt_script"
43     DEFAULT_CONFIG_TPL_CFG = "cgnat.cfg"
44     PIPELINE_COMMAND = CGNAPT_PIPELINE_COMMAND
45     SW_DEFAULT_CORE = 6
46     HW_DEFAULT_CORE = 3
47     VNF_TYPE = "CGNAPT"
48
49     @staticmethod
50     def _generate_ip_from_pool(ip):
51         ip_parts = ip.split('.')
52         assert len(ip_parts) == 4
53         iter1 = (str(n) for n in range(int(ip_parts[2]), 256))
54         for ip_parts[2] in iter1:
55             yield '.'.join(ip_parts)
56
57     @staticmethod
58     def _update_cgnat_script_file(ip_pipeline_cfg, mcpi, vnf_str):
59         pipeline_config_str = str(ip_pipeline_cfg)
60         input_cmds = '\n'.join(mcpi)
61         icmp_flag = 'link 0 down' in input_cmds
62         if icmp_flag:
63             pipeline_config_str = ''
64         return '\n'.join([pipeline_config_str, input_cmds])
65
66     def scale(self, flavor=""):
67         raise NotImplementedError
68
69     def _get_cgnapt_config(self, interfaces=None):
70         if interfaces is None:
71             interfaces = self.vnfd_helper.interfaces
72
73         gateway_ips = []
74
75         # fixme: Get private port and gateway from port list
76         priv_ports = interfaces[::2]
77         for interface in priv_ports:
78             gateway_ips.append(self._get_ports_gateway(interface["name"]))
79         return gateway_ips
80
81
82 class CgnaptApproxVnf(SampleVNF):
83
84     APP_NAME = "vCGNAPT"
85     APP_WORD = 'cgnapt'
86     COLLECT_KPI = CGNAPT_COLLECT_KPI
87
88     COLLECT_MAP = {
89         "packets_in": 2,
90         "packets_fwd": 5,
91         "packets_dropped": 4,
92     }
93
94     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
95         if setup_env_helper_type is None:
96             setup_env_helper_type = CgnaptApproxSetupEnvHelper
97
98         super(CgnaptApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
99                                               resource_helper_type)
100
101     def _vnf_up_post(self):
102         super(CgnaptApproxVnf, self)._vnf_up_post()
103         if self.scenario_helper.options.get('napt', 'static') != 'static':
104             return
105
106         ip_iter = self.setup_helper._generate_ip_from_pool("152.16.40.10")
107         gw_ips = self.setup_helper._get_cgnapt_config()
108         if self.scenario_helper.vnf_cfg.get("lb_config", "SW") == 'HW':
109             pipeline = self.setup_helper.HW_DEFAULT_CORE
110             offset = 3
111         else:
112             pipeline = self.setup_helper.SW_DEFAULT_CORE - 1
113             offset = 0
114
115         worker_threads = int(self.scenario_helper.vnf_cfg["worker_threads"])
116         cmd_template = "p {0} entry addm {1} 1 {2} 1 0 32 65535 65535 65535"
117         for gw, ip in zip(gw_ips, ip_iter):
118             cmd = cmd_template.format(pipeline, gw, ip)
119             pipeline += worker_threads
120             pipeline += offset
121             self.vnf_execute(cmd)
122
123         time.sleep(WAIT_FOR_STATIC_NAPT)