1 # Copyright (c) 2017 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 class Classify(object):
22 """Classify utilities."""
25 def vpp_creates_classify_table_l3(node, ip_version, direction):
26 """Create classify table for IP address filtering.
28 :param node: VPP node to create classify table.
29 :param ip_version: Version of IP protocol.
30 :param direction: Direction of traffic - src/dst.
34 :returns (table_index, skip_n, match_n)
35 table_index: Classify table index.
36 skip_n: Number of skip vectors.
37 match_n: Number of match vectors.
38 :rtype: tuple(int, int, int)
39 :raises RuntimeError: If VPP can't create table.
42 output = VatExecutor.cmd_from_template(node, "classify_add_table.vat",
43 ip_version=ip_version,
46 if output[0]["retval"] == 0:
47 table_index = output[0]["new_table_index"]
48 skip_n = output[0]["skip_n_vectors"]
49 match_n = output[0]["match_n_vectors"]
50 logger.trace('Classify table with table_index {} created on node {}'
51 .format(table_index, node['host']))
53 raise RuntimeError('Unable to create classify table on node {}'
54 .format(node['host']))
56 return table_index, skip_n, match_n
59 def vpp_creates_classify_table_l2(node, direction):
60 """Create classify table for MAC address filtering.
62 :param node: VPP node to create classify table.
63 :param direction: Direction of traffic - src/dst.
66 :returns (table_index, skip_n, match_n)
67 table_index: Classify table index.
68 skip_n: Number of skip vectors.
69 match_n: Number of match vectors.
70 :rtype: tuple(int, int, int)
71 :raises RuntimeError: If VPP can't create table.
73 output = VatExecutor.cmd_from_template(node,
74 "classify_add_table_l2.vat",
77 if output[0]["retval"] == 0:
78 table_index = output[0]["new_table_index"]
79 skip_n = output[0]["skip_n_vectors"]
80 match_n = output[0]["match_n_vectors"]
81 logger.trace('Classify table with table_index {} created on node {}'
82 .format(table_index, node['host']))
84 raise RuntimeError('Unable to create classify table on node {}'
85 .format(node['host']))
87 return table_index, skip_n, match_n
90 def vpp_creates_classify_table_hex(node, hex_mask):
91 """Create classify table with hex mask.
93 :param node: VPP node to create classify table based on hex mask.
94 :param hex_mask: Classify hex mask.
97 :returns (table_index, skip_n, match_n)
98 table_index: Classify table index.
99 skip_n: Number of skip vectors.
100 match_n: Number of match vectors.
101 :rtype: tuple(int, int, int)
102 :raises RuntimeError: If VPP can't create table.
104 output = VatExecutor.cmd_from_template(node,
105 "classify_add_table_hex.vat",
108 if output[0]["retval"] == 0:
109 table_index = output[0]["new_table_index"]
110 skip_n = output[0]["skip_n_vectors"]
111 match_n = output[0]["match_n_vectors"]
112 logger.trace('Classify table with table_index {} created on node {}'
113 .format(table_index, node['host']))
115 raise RuntimeError('Unable to create classify table on node {}'
116 .format(node['host']))
118 return table_index, skip_n, match_n
121 def vpp_configures_classify_session_l3(node, acl_method, table_index,
122 skip_n, match_n, ip_version,
124 """Configuration of classify session for IP address filtering.
126 :param node: VPP node to setup classify session.
127 :param acl_method: ACL method - deny/permit.
128 :param table_index: Classify table index.
129 :param skip_n: Number of skip vectors based on mask.
130 :param match_n: Number of match vectors based on mask.
131 :param ip_version: Version of IP protocol.
132 :param direction: Direction of traffic - src/dst.
133 :param address: IPv4 or IPv6 address.
135 :type acl_method: str
136 :type table_index: int
139 :type ip_version: str
143 with VatTerminal(node) as vat:
144 vat.vat_terminal_exec_cmd_from_template("classify_add_session.vat",
145 acl_method=acl_method,
146 table_index=table_index,
149 ip_version=ip_version,
154 def vpp_configures_classify_session_l2(node, acl_method, table_index,
155 skip_n, match_n, direction, address):
156 """Configuration of classify session for MAC address filtering.
158 :param node: VPP node to setup classify session.
159 :param acl_method: ACL method - deny/permit.
160 :param table_index: Classify table index.
161 :param skip_n: Number of skip vectors based on mask.
162 :param match_n: Number of match vectors based on mask.
163 :param direction: Direction of traffic - src/dst.
164 :param address: IPv4 or IPv6 address.
166 :type acl_method: str
167 :type table_index: int
173 with VatTerminal(node) as vat:
174 vat.vat_terminal_exec_cmd_from_template(
175 "classify_add_session_l2.vat",
176 acl_method=acl_method,
177 table_index=table_index,
184 def vpp_configures_classify_session_hex(node, acl_method, table_index,
185 skip_n, match_n, hex_value):
186 """Configuration of classify session with hex value.
188 :param node: VPP node to setup classify session.
189 :param acl_method: ACL method - deny/permit.
190 :param table_index: Classify table index.
191 :param skip_n: Number of skip vectors based on mask.
192 :param match_n: Number of match vectors based on mask.
193 :param hex_value: Classify hex value.
195 :type acl_method: str
196 :type table_index: int
201 with VatTerminal(node) as vat:
202 vat.vat_terminal_exec_cmd_from_template(
203 "classify_add_session_hex.vat",
204 acl_method=acl_method,
205 table_index=table_index,
211 def vpp_configures_classify_session_generic(node, session_type, table_index,
212 skip_n, match_n, match,
214 """Configuration of classify session.
216 :param node: VPP node to setup classify session.
217 :param session_type: Session type - hit-next, l2-hit-next, acl-hit-next
218 or policer-hit-next, and their respective parameters.
219 :param table_index: Classify table index.
220 :param skip_n: Number of skip vectors based on mask.
221 :param match_n: Number of match vectors based on mask.
222 :param match: Match value - l2, l3, l4 or hex, and their
223 respective parameters.
224 :param match2: Additional match values, to avoid using overly long
225 variables in RobotFramework.
227 :type session_type: str
228 :type table_index: int
235 match = ' '.join((match, match2))
237 with VatTerminal(node) as vat:
238 vat.vat_terminal_exec_cmd_from_template(
239 "classify_add_session_generic.vat",
241 table_index=table_index,
248 def compute_classify_hex_mask(ip_version, protocol, direction):
249 """Compute classify hex mask for TCP or UDP packet matching.
251 :param ip_version: Version of IP protocol.
252 :param protocol: Type of protocol.
253 :param direction: Traffic direction.
254 :type ip_version: str
257 :returns: Classify hex mask.
259 :raises ValueError: If protocol is not TCP or UDP.
260 :raises ValueError: If direction is not source or destination or
261 source + destination.
263 if protocol == 'TCP' or protocol == 'UDP':
264 base_mask = Classify._compute_base_mask(ip_version)
266 if direction == 'source':
267 return base_mask + 'FFFF0000'
268 elif direction == 'destination':
269 return base_mask + '0000FFFF'
270 elif direction == 'source + destination':
271 return base_mask + 'FFFFFFFF'
273 raise ValueError("Invalid direction!")
275 raise ValueError("Invalid protocol!")
278 def compute_classify_hex_value(hex_mask, source_port, destination_port):
279 """Compute classify hex value for TCP or UDP packet matching.
281 :param hex_mask: Classify hex mask.
282 :param source_port: Source TCP/UDP port.
283 :param destination_port: Destination TCP/UDP port.
285 :type source_port: str
286 :type destination_port: str
287 :returns: Classify hex value.
290 source_port_hex = Classify._port_convert(source_port)
291 destination_port_hex = Classify._port_convert(destination_port)
293 return hex_mask[:-8] + source_port_hex + destination_port_hex
296 def _port_convert(port):
297 """Convert port number for classify hex table format.
299 :param port: TCP/UDP port number.
301 :returns: TCP/UDP port number in 4-digit hexadecimal format.
304 return '{0:04x}'.format(int(port))
307 def _compute_base_mask(ip_version):
308 """Compute base classify hex mask based on IP version.
310 :param ip_version: Version of IP protocol.
311 :type ip_version: str
312 :return: Base hex mask.
315 if ip_version == 'ip4':
317 # base value of classify hex table for IPv4 TCP/UDP ports
318 elif ip_version == 'ip6':
320 # base value of classify hex table for IPv6 TCP/UDP ports
322 raise ValueError("Invalid IP version!")
325 def get_classify_table_data(node, table_index):
326 """Retrieve settings for classify table by ID.
328 :param node: VPP node to retrieve classify data from.
329 :param table_index: Index of a specific classify table.
331 :type table_index: int
332 :returns: Classify table settings.
335 with VatTerminal(node) as vat:
336 data = vat.vat_terminal_exec_cmd_from_template(
337 "classify_table_info.vat",
343 def get_classify_session_data(node, table_index, session_index=None):
344 """Retrieve settings for all classify sessions in a table,
345 or for a specific classify session.
347 :param node: VPP node to retrieve classify data from.
348 :param table_index: Index of a classify table.
349 :param session_index: Index of a specific classify session. (Optional)
351 :type table_index: int
352 :type session_index: int
353 :returns: List of classify session settings, or a dictionary of settings
354 for a specific classify session.
357 with VatTerminal(node) as vat:
358 data = vat.vat_terminal_exec_cmd_from_template(
359 "classify_session_dump.vat",
362 if session_index is not None:
363 return data[0][session_index]
368 def vpp_log_plugin_acl_settings(node):
369 """Retrieve configured settings from the ACL plugin
370 and write to robot log.
372 :param node: VPP node.
376 VatExecutor.cmd_from_template(
377 node, "acl_plugin/acl_dump.vat")
378 except (ValueError, RuntimeError):
379 # Fails to parse JSON data in response, but it is still logged
383 def vpp_log_plugin_acl_interface_assignment(node):
384 """Retrieve interface assignment from the ACL plugin
385 and write to robot log.
387 :param node: VPP node.
392 VatExecutor.cmd_from_template(
393 node, "acl_plugin/acl_interface_dump.vat", json_out=False)
395 # Fails to parse response, but it is still logged