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