1 # Copyright (c) 2017 Intel Corporation
\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
7 # http://www.apache.org/licenses/LICENSE-2.0
\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
15 from __future__ import absolute_import
\r
16 from __future__ import print_function
\r
19 from six.moves import zip
\r
21 FIREWALL_ADD_DEFAULT = "p {0} firewall add default 1"
\r
22 FIREWALL_ADD_PRIO = """\
\r
23 p {0} firewall add priority 1 ipv4 {1} 24 0.0.0.0 0 0 65535 0 65535 6 0xFF port 0"""
\r
25 FLOW_ADD_QINQ_RULES = """\
\r
26 p {0} flow add qinq 128 512 port 0 id 1
\r
27 p {0} flow add default 1"""
\r
29 ACTION_FLOW_BULK = "p {0} action flow bulk /tmp/action_bulk_512.txt"
\r
30 ACTION_DSCP_CLASS_COLOR = "p {0} action dscp {1} class {2} color {3}"
\r
31 ROUTE_ADD_DEFAULT = "p {0} route add default 1"
\r
32 ROUTE_ADD_ETHER_QINQ = 'p {0} route add {1} {2} port 0 ether {3} qinq 0 {4}'
\r
33 ROUTE_ADD_ETHER_MPLS = "p {0} route add {1} 21 port 0 ether {2} mpls 0:{3}"
\r
36 class PipelineRules(object):
\r
38 def __init__(self, pipeline_id=0):
\r
39 super(PipelineRules, self).__init__()
\r
41 self.pipeline_id = pipeline_id
\r
44 return '\n'.join(self.rule_list)
\r
46 def get_string(self):
\r
49 def next_pipeline(self, num=1):
\r
50 self.pipeline_id += num
\r
52 def add_newline(self):
\r
53 self.rule_list.append('')
\r
55 def add_rule(self, base, *args):
\r
56 self.rule_list.append(base.format(self.pipeline_id, *args))
\r
58 def add_firewall_prio(self, ip):
\r
59 self.add_rule(FIREWALL_ADD_PRIO, ip)
\r
61 def add_firewall_script(self, ip):
\r
62 ip_addr = ip.split('.')
\r
63 assert len(ip_addr) == 4
\r
65 for i in range(256):
\r
66 ip_addr[-2] = str(i)
\r
67 ip = '.'.join(ip_addr)
\r
68 self.add_firewall_prio(ip)
\r
69 self.add_rule(FIREWALL_ADD_DEFAULT)
\r
72 def add_flow_classification_script(self):
\r
73 self.add_rule(FLOW_ADD_QINQ_RULES)
\r
75 def add_flow_action(self):
\r
76 self.add_rule(ACTION_FLOW_BULK)
\r
78 def add_dscp_class_color(self, dscp, color):
\r
79 self.add_rule(ACTION_DSCP_CLASS_COLOR, dscp, dscp % 4, color)
\r
81 def add_flow_action2(self):
\r
82 self.add_rule(ACTION_FLOW_BULK)
\r
83 for dscp, color in zip(range(64), itertools.cycle('GYR')):
\r
84 self.add_dscp_class_color(dscp, color)
\r
86 def add_route_ether_mpls(self, ip, mac_addr, index):
\r
87 self.add_rule(ROUTE_ADD_ETHER_MPLS, ip, mac_addr, index)
\r
89 def add_route_script(self, ip, mac_addr):
\r
90 ip_addr = ip.split('.')
\r
91 assert len(ip_addr) == 4
\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
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
103 def add_route_script2(self, ip, mac_addr):
\r
104 ip_addr = ip.split('.')
\r
105 assert len(ip_addr) == 4
\r
108 for i in range(0, 256):
\r
109 ip_addr[-2] = str(i)
\r
110 ip = '.'.join(ip_addr)
\r
111 self.add_ether_qinq(ip, mask, mac_addr, i)
\r
112 self.add_rule(ROUTE_ADD_DEFAULT)
\r