Build VPP startup configuration file
[yardstick.git] / yardstick / network_services / pipeline.py
1 # Copyright (c) 2017 Intel Corporation\r
2 #\r
3 # Licensed under the Apache License, Version 2.0 (the "License");\r
4 # you may not use this file except in compliance with the License.\r
5 # You may obtain a copy of the License at\r
6 #\r
7 #      http://www.apache.org/licenses/LICENSE-2.0\r
8 #\r
9 # Unless required by applicable law or agreed to in writing, software\r
10 # distributed under the License is distributed on an "AS IS" BASIS,\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 # See the License for the specific language governing permissions and\r
13 # limitations under the License.\r
14 \r
15 from __future__ import absolute_import\r
16 from __future__ import print_function\r
17 import itertools\r
18 \r
19 from six.moves import zip\r
20 \r
21 from yardstick.common import utils\r
22 \r
23 FIREWALL_ADD_DEFAULT = "p {0} firewall add default 1"\r
24 FIREWALL_ADD_PRIO = """\\r
25 p {0} firewall add priority 1 ipv4  {1} 24 0.0.0.0 0 0 65535 0 65535 17 0xFF port 0"""\r
26 \r
27 FLOW_ADD_QINQ_RULES = """\\r
28 p {0} flow add qinq 128 512 port 0 id 1\r
29 p {0} flow add default 1"""\r
30 \r
31 ACTION_FLOW_BULK = "p {0} action flow bulk /tmp/action_bulk_512.txt"\r
32 ACTION_DSCP_CLASS_COLOR = "p {0} action dscp {1} class {2} color {3}"\r
33 ROUTE_ADD_DEFAULT = "p {0} route add default 1"\r
34 ROUTE_ADD_ETHER_QINQ = 'p {0} route add {1} {2} port 0 ether {3} qinq 0 {4}'\r
35 ROUTE_ADD_ETHER_MPLS = "p {0} route add {1} 21 port 0 ether {2} mpls 0:{3}"\r
36 \r
37 \r
38 class PipelineRules(object):\r
39 \r
40     def __init__(self, pipeline_id=0):\r
41         super(PipelineRules, self).__init__()\r
42         self.rule_list = []\r
43         self.pipeline_id = pipeline_id\r
44 \r
45     def __str__(self):\r
46         return '\n'.join(self.rule_list)\r
47 \r
48     def get_string(self):\r
49         return str(self)\r
50 \r
51     def next_pipeline(self, num=1):\r
52         self.pipeline_id += num\r
53 \r
54     def add_newline(self):\r
55         self.rule_list.append('')\r
56 \r
57     def add_rule(self, base, *args):\r
58         self.rule_list.append(base.format(self.pipeline_id, *args))\r
59 \r
60     def add_firewall_prio(self, ip):\r
61         self.add_rule(FIREWALL_ADD_PRIO, ip)\r
62 \r
63     def add_firewall_script(self, ip):\r
64         ip_addr = str(utils.make_ipv4_address(ip)).split('.')\r
65         ip_addr[-1] = '0'\r
66         for i in range(256):\r
67             ip_addr[-2] = str(i)\r
68             ip = '.'.join(ip_addr)\r
69             self.add_firewall_prio(ip)\r
70         self.add_rule(FIREWALL_ADD_DEFAULT)\r
71         self.add_newline()\r
72 \r
73     def add_flow_classification_script(self):\r
74         self.add_rule(FLOW_ADD_QINQ_RULES)\r
75 \r
76     def add_flow_action(self):\r
77         self.add_rule(ACTION_FLOW_BULK)\r
78 \r
79     def add_dscp_class_color(self, dscp, color):\r
80         self.add_rule(ACTION_DSCP_CLASS_COLOR, dscp, dscp % 4, color)\r
81 \r
82     def add_flow_action2(self):\r
83         self.add_rule(ACTION_FLOW_BULK)\r
84         for dscp, color in zip(range(64), itertools.cycle('GYR')):\r
85             self.add_dscp_class_color(dscp, color)\r
86 \r
87     def add_route_ether_mpls(self, ip, mac_addr, index):\r
88         self.add_rule(ROUTE_ADD_ETHER_MPLS, ip, mac_addr, index)\r
89 \r
90     def add_route_script(self, ip, mac_addr):\r
91         ip_addr = str(utils.make_ipv4_address(ip)).split('.')\r
92         ip_addr[-1] = '0'\r
93         for index in range(0, 256, 8):\r
94             ip_addr[-2] = str(index)\r
95             ip = '.'.join(ip_addr)\r
96             self.add_route_ether_mpls(ip, mac_addr, index)\r
97         self.add_rule(ROUTE_ADD_DEFAULT)\r
98         self.add_newline()\r
99 \r
100     def add_ether_qinq(self, ip, mask, mac_addr, index):\r
101         self.add_rule(ROUTE_ADD_ETHER_QINQ, ip, mask, mac_addr, index)\r
102 \r
103     def add_route_script2(self, ip, mac_addr):\r
104         ip_addr = str(utils.make_ipv4_address(ip)).split('.')\r
105         ip_addr[-1] = '0'\r
106         mask = 24\r
107         for i in range(0, 256):\r
108             ip_addr[-2] = str(i)\r
109             ip = '.'.join(ip_addr)\r
110             self.add_ether_qinq(ip, mask, mac_addr, i)\r
111         self.add_rule(ROUTE_ADD_DEFAULT)\r
112         self.add_newline()\r