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.
14 """Classify utilities library."""
16 from robot.api import logger
18 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
21 # pylint: disable=too-many-arguments, invalid-name
23 class Classify(object):
24 """Classify utilities."""
27 def vpp_creates_classify_table_l3(node, ip_version, direction):
28 """Create classify table for IP address filtering.
30 :param node: VPP node to create classify table.
31 :param ip_version: Version of IP protocol.
32 :param direction: Direction of traffic - src/dst.
36 :return (table_index, skip_n, match_n)
37 table_index: Classify table index.
38 skip_n: Number of skip vectors.
39 match_n: Number of match vectors.
40 :rtype: tuple(int, int, int)
41 :raises RuntimeError: If VPP can't create table.
44 output = VatExecutor.cmd_from_template(node, "classify_add_table.vat",
45 ip_version=ip_version,
48 if output[0]["retval"] == 0:
49 table_index = output[0]["new_table_index"]
50 skip_n = output[0]["skip_n_vectors"]
51 match_n = output[0]["match_n_vectors"]
52 logger.trace('Classify table with table_index {} created on node {}'
53 .format(table_index, node['host']))
55 raise RuntimeError('Unable to create classify table on node {}'
56 .format(node['host']))
58 return table_index, skip_n, match_n
61 def vpp_creates_classify_table_l2(node, direction):
62 """Create classify table for MAC address filtering.
64 :param node: VPP node to create classify table.
65 :param direction: Direction of traffic - src/dst.
68 :return (table_index, skip_n, match_n)
69 table_index: Classify table index.
70 skip_n: Number of skip vectors.
71 match_n: Number of match vectors.
72 :rtype: tuple(int, int, int)
73 :raises RuntimeError: If VPP can't create table.
75 output = VatExecutor.cmd_from_template(node,
76 "classify_add_table_l2.vat",
79 if output[0]["retval"] == 0:
80 table_index = output[0]["new_table_index"]
81 skip_n = output[0]["skip_n_vectors"]
82 match_n = output[0]["match_n_vectors"]
83 logger.trace('Classify table with table_index {} created on node {}'
84 .format(table_index, node['host']))
86 raise RuntimeError('Unable to create classify table on node {}'
87 .format(node['host']))
89 return table_index, skip_n, match_n
92 def vpp_creates_classify_table_hex(node, hex_mask):
93 """Create classify table with hex mask.
95 :param node: VPP node to create classify table based on hex mask.
96 :param hex_mask: Classify hex mask.
99 :return (table_index, skip_n, match_n)
100 table_index: Classify table index.
101 skip_n: Number of skip vectors.
102 match_n: Number of match vectors.
103 :rtype: tuple(int, int, int)
104 :raises RuntimeError: If VPP can't create table.
106 output = VatExecutor.cmd_from_template(node,
107 "classify_add_table_hex.vat",
110 if output[0]["retval"] == 0:
111 table_index = output[0]["new_table_index"]
112 skip_n = output[0]["skip_n_vectors"]
113 match_n = output[0]["match_n_vectors"]
114 logger.trace('Classify table with table_index {} created on node {}'
115 .format(table_index, node['host']))
117 raise RuntimeError('Unable to create classify table on node {}'
118 .format(node['host']))
120 return table_index, skip_n, match_n
123 def vpp_configures_classify_session_l3(node, acl_method, table_index,
124 skip_n, match_n, ip_version,
126 """Configuration of classify session for IP address filtering.
128 :param node: VPP node to setup classify session.
129 :param acl_method: ACL method - deny/permit.
130 :param table_index: Classify table index.
131 :param skip_n: Number of skip vectors based on mask.
132 :param match_n: Number of match vectors based on mask.
133 :param ip_version: Version of IP protocol.
134 :param direction: Direction of traffic - src/dst.
135 :param address: IPv4 or IPv6 address.
137 :type acl_method: str
138 :type table_index: int
141 :type ip_version: str
145 with VatTerminal(node) as vat:
146 vat.vat_terminal_exec_cmd_from_template("classify_add_session.vat",
147 acl_method=acl_method,
148 table_index=table_index,
151 ip_version=ip_version,
156 def vpp_configures_classify_session_l2(node, acl_method, table_index,
157 skip_n, match_n, direction, address):
158 """Configuration of classify session for MAC address filtering.
160 :param node: VPP node to setup classify session.
161 :param acl_method: ACL method - deny/permit.
162 :param table_index: Classify table index.
163 :param skip_n: Number of skip vectors based on mask.
164 :param match_n: Number of match vectors based on mask.
165 :param direction: Direction of traffic - src/dst.
166 :param address: IPv4 or IPv6 address.
168 :type acl_method: str
169 :type table_index: int
175 with VatTerminal(node) as vat:
176 vat.vat_terminal_exec_cmd_from_template(
177 "classify_add_session_l2.vat",
178 acl_method=acl_method,
179 table_index=table_index,
186 def vpp_configures_classify_session_hex(node, acl_method, table_index,
187 skip_n, match_n, hex_value):
188 """Configuration of classify session with hex value.
190 :param node: VPP node to setup classify session.
191 :param acl_method: ACL method - deny/permit.
192 :param table_index: Classify table index.
193 :param skip_n: Number of skip vectors based on mask.
194 :param match_n: Number of match vectors based on mask.
195 :param hex_value: Classify hex value.
197 :type acl_method: str
198 :type table_index: int
203 with VatTerminal(node) as vat:
204 vat.vat_terminal_exec_cmd_from_template(
205 "classify_add_session_hex.vat",
206 acl_method=acl_method,
207 table_index=table_index,
213 def vpp_configures_classify_session_generic(node, session_type, table_index,
214 skip_n, match_n, match,
216 """Configuration of classify session.
218 :param node: VPP node to setup classify session.
219 :param session_type: Session type - hit-next, l2-hit-next, acl-hit-next
220 or policer-hit-next, and their respective parameters.
221 :param table_index: Classify table index.
222 :param skip_n: Number of skip vectors based on mask.
223 :param match_n: Number of match vectors based on mask.
224 :param match: Match value - l2, l3, l4 or hex, and their
225 respective parameters.
226 :param match2: Additional match values, to avoid using overly long
227 variables in RobotFramework.
229 :type session_type: str
230 :type table_index: int
237 match = ' '.join((match, match2))
239 with VatTerminal(node) as vat:
240 vat.vat_terminal_exec_cmd_from_template(
241 "classify_add_session_generic.vat",
243 table_index=table_index,
250 def compute_classify_hex_mask(ip_version, protocol, direction):
251 """Compute classify hex mask for TCP or UDP packet matching.
253 :param ip_version: Version of IP protocol.
254 :param protocol: Type of protocol.
255 :param direction: Traffic direction.
256 :type ip_version: str
259 :return: Classify hex mask.
261 :raises ValueError: If protocol is not TCP or UDP.
262 :raises ValueError: If direction is not source or destination or
263 source + destination.
265 if protocol == 'TCP' or protocol == 'UDP':
266 base_mask = Classify._compute_base_mask(ip_version)
268 if direction == 'source':
269 return base_mask + 'FFFF0000'
270 elif direction == 'destination':
271 return base_mask + '0000FFFF'
272 elif direction == 'source + destination':
273 return base_mask + 'FFFFFFFF'
275 raise ValueError("Invalid direction!")
277 raise ValueError("Invalid protocol!")
280 def compute_classify_hex_value(hex_mask, source_port, destination_port):
281 """Compute classify hex value for TCP or UDP packet matching.
283 :param hex_mask: Classify hex mask.
284 :param source_port: Source TCP/UDP port.
285 :param destination_port: Destination TCP/UDP port.
287 :type source_port: str
288 :type destination_port: str
289 :return: Classify hex value.
292 source_port_hex = Classify._port_convert(source_port)
293 destination_port_hex = Classify._port_convert(destination_port)
295 return hex_mask[:-8] + source_port_hex + destination_port_hex
298 def _port_convert(port):
299 """Convert port number for classify hex table format.
301 :param port: TCP/UDP port number.
303 :return: TCP/UDP port number in 4-digit hexadecimal format.
306 return '{0:04x}'.format(int(port))
309 def _compute_base_mask(ip_version):
310 """Compute base classify hex mask based on IP version.
312 :param ip_version: Version of IP protocol.
313 :type ip_version: str
314 :return: Base hex mask.
317 if ip_version == 'ip4':
319 # base value of classify hex table for IPv4 TCP/UDP ports
320 elif ip_version == 'ip6':
322 # base value of classify hex table for IPv6 TCP/UDP ports
324 raise ValueError("Invalid IP version!")
327 def get_classify_table_data(node, table_index):
328 """Retrieve settings for classify table by ID.
330 :param node: VPP node to retrieve classify data from.
331 :param table_index: Index of a specific classify table.
333 :type table_index: int
334 :return: Classify table settings.
337 with VatTerminal(node) as vat:
338 data = vat.vat_terminal_exec_cmd_from_template(
339 "classify_table_info.vat",
345 def get_classify_session_data(node, table_index, session_index=None):
346 """Retrieve settings for all classify sessions in a table,
347 or for a specific classify session.
349 :param node: VPP node to retrieve classify data from.
350 :param table_index: Index of a classify table.
351 :param session_index: Index of a specific classify session. (Optional)
353 :type table_index: int
354 :type session_index: int
355 :return: List of classify session settings, or a dictionary of settings
356 for a specific classify session.
359 with VatTerminal(node) as vat:
360 data = vat.vat_terminal_exec_cmd_from_template(
361 "classify_session_dump.vat",
364 if session_index is not None:
365 return data[0][session_index]