1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
15 from robot.api import logger
17 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
20 class Classify(object):
21 """Classify utilities."""
24 def vpp_create_classify_table(node, ip_version, direction):
25 """Create classify table.
27 :param node: VPP node to create classify table.
28 :param ip_version: Version of IP protocol.
29 :param direction: Direction of traffic - src/dst.
33 :return (table_index, skip_n, match_n)
34 table_index: Classify table index.
35 skip_n: Number of skip vectors.
36 match_n: Number of match vectors.
37 :rtype: tuple(int, int, int)
38 :raises RuntimeError: If VPP can't create table.
40 output = VatExecutor.cmd_from_template(node, "classify_add_table.vat",
41 ip_version=ip_version,
44 if output[0]["retval"] == 0:
45 table_index = output[0]["new_table_index"]
46 skip_n = output[0]["skip_n_vectors"]
47 match_n = output[0]["match_n_vectors"]
48 logger.trace('Classify table with table_index {} created on node {}'
49 .format(table_index, node['host']))
51 raise RuntimeError('Unable to create classify table on node {}'
52 .format(node['host']))
54 return table_index, skip_n, match_n
57 def vpp_create_classify_table_hex(node, hex_mask):
58 """Create classify table with hex mask.
60 :param node: VPP node to create classify table based on hex mask.
61 :param hex_mask: Classify hex mask.
64 :return (table_index, skip_n, match_n)
65 table_index: Classify table index.
66 skip_n: Number of skip vectors.
67 match_n: Number of match vectors.
68 :rtype: tuple(int, int, int)
69 :raises RuntimeError: If VPP can't create table.
71 output = VatExecutor.cmd_from_template(node,
72 "classify_add_table_hex.vat",
75 if output[0]["retval"] == 0:
76 table_index = output[0]["new_table_index"]
77 skip_n = output[0]["skip_n_vectors"]
78 match_n = output[0]["match_n_vectors"]
79 logger.trace('Classify table with table_index {} created on node {}'
80 .format(table_index, node['host']))
82 raise RuntimeError('Unable to create classify table on node {}'
83 .format(node['host']))
85 return table_index, skip_n, match_n
88 def vpp_configure_classify_session(node, acl_method, table_index, skip_n,
89 match_n, ip_version, direction, address):
90 """Configuration of classify session.
92 :param node: VPP node to setup classify session.
93 :param acl_method: ACL method - deny/permit.
94 :param table_index: Classify table index.
95 :param skip_n: Number of skip vectors based on mask.
96 :param match_n: Number of match vectors based on mask.
97 :param ip_version: Version of IP protocol.
98 :param direction: Direction of traffic - src/dst.
99 :param address: IPv4 or IPv6 address.
101 :type acl_method: str
102 :type table_index: int
105 :type ip_version: str
109 with VatTerminal(node) as vat:
110 vat.vat_terminal_exec_cmd_from_template("classify_add_session.vat",
111 acl_method=acl_method,
112 table_index=table_index,
115 ip_version=ip_version,
120 def vpp_configure_classify_session_hex(node, acl_method, table_index,
121 skip_n, match_n, hex_value):
122 """Configuration of classify session with hex value.
124 :param node: VPP node to setup classify session.
125 :param acl_method: ACL method - deny/permit.
126 :param table_index: Classify table index.
127 :param skip_n: Number of skip vectors based on mask.
128 :param match_n: Number of match vectors based on mask.
129 :param hex_value: Classify hex value.
131 :type acl_method: str
132 :type table_index: int
137 with VatTerminal(node) as vat:
138 vat.vat_terminal_exec_cmd_from_template(
139 "classify_add_session_hex.vat",
140 acl_method=acl_method,
141 table_index=table_index,
147 def compute_classify_hex_mask(ip_version, protocol, direction):
148 """Compute classify hex mask for TCP or UDP packet matching.
150 :param ip_version: Version of IP protocol.
151 :param protocol: Type of protocol.
152 :param direction: Traffic direction.
153 :type ip_version: str
156 :return: Classify hex mask.
158 :raises ValueError: If protocol is not TCP or UDP.
159 :raises ValueError: If direction is not source or destination or
160 source + destination.
162 if protocol == 'TCP' or protocol == 'UDP':
163 base_mask = Classify._compute_base_mask(ip_version)
165 if direction == 'source':
166 return base_mask + 'FFFF0000'
167 elif direction == 'destination':
168 return base_mask + '0000FFFF'
169 elif direction == 'source + destination':
170 return base_mask + 'FFFFFFFF'
172 raise ValueError("Invalid direction!")
174 raise ValueError("Invalid protocol!")
177 def compute_classify_hex_value(hex_mask, source_port, destination_port):
178 """Compute classify hex value for TCP or UDP packet matching.
180 :param hex_mask: Classify hex mask.
181 :param source_port: Source TCP/UDP port.
182 :param destination_port: Destination TCP/UDP port.
184 :type source_port: str
185 :type destination_port: str
186 :return: Classify hex value.
189 source_port_hex = Classify._port_convert(source_port)
190 destination_port_hex = Classify._port_convert(destination_port)
192 return hex_mask[:-8] + source_port_hex + destination_port_hex
195 def _port_convert(port):
196 """Convert port number for classify hex table format.
198 :param port: TCP/UDP port number.
200 :return: TCP/UDP port number in 4-digit hexadecimal format.
203 return '{0:04x}'.format(int(port))
206 def _compute_base_mask(ip_version):
207 """Compute base classify hex mask based on IP version.
209 :param ip_version: Version of IP protocol.
210 :type ip_version: str
211 :return: Base hex mask.
214 if ip_version == 'ip4':
216 # base value of classify hex table for IPv4 TCP/UDP ports
217 elif ip_version == 'ip6':
219 # base value of classify hex table for IPv6 TCP/UDP ports
221 raise ValueError("Invalid IP version!")