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_creates_classify_table_l3(node, ip_version, direction):
25 """Create classify table for IP address filtering.
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_creates_classify_table_l2(node, direction):
58 """Create classify table for MAC address filtering.
60 :param node: VPP node to create classify table.
61 :param direction: Direction of traffic - src/dst.
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_l2.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_creates_classify_table_hex(node, hex_mask):
89 """Create classify table with hex mask.
91 :param node: VPP node to create classify table based on hex mask.
92 :param hex_mask: Classify hex mask.
95 :return (table_index, skip_n, match_n)
96 table_index: Classify table index.
97 skip_n: Number of skip vectors.
98 match_n: Number of match vectors.
99 :rtype: tuple(int, int, int)
100 :raises RuntimeError: If VPP can't create table.
102 output = VatExecutor.cmd_from_template(node,
103 "classify_add_table_hex.vat",
106 if output[0]["retval"] == 0:
107 table_index = output[0]["new_table_index"]
108 skip_n = output[0]["skip_n_vectors"]
109 match_n = output[0]["match_n_vectors"]
110 logger.trace('Classify table with table_index {} created on node {}'
111 .format(table_index, node['host']))
113 raise RuntimeError('Unable to create classify table on node {}'
114 .format(node['host']))
116 return table_index, skip_n, match_n
119 def vpp_configures_classify_session_l3(node, acl_method, table_index, skip_n,
120 match_n, ip_version, direction,
122 """Configuration of classify session for IP address filtering.
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 ip_version: Version of IP protocol.
130 :param direction: Direction of traffic - src/dst.
131 :param address: IPv4 or IPv6 address.
133 :type acl_method: str
134 :type table_index: int
137 :type ip_version: str
141 with VatTerminal(node) as vat:
142 vat.vat_terminal_exec_cmd_from_template("classify_add_session.vat",
143 acl_method=acl_method,
144 table_index=table_index,
147 ip_version=ip_version,
152 def vpp_configures_classify_session_l2(node, acl_method, table_index, skip_n,
153 match_n, direction, address):
154 """Configuration of classify session for MAC address filtering.
156 :param node: VPP node to setup classify session.
157 :param acl_method: ACL method - deny/permit.
158 :param table_index: Classify table index.
159 :param skip_n: Number of skip vectors based on mask.
160 :param match_n: Number of match vectors based on mask.
161 :param direction: Direction of traffic - src/dst.
162 :param address: IPv4 or IPv6 address.
164 :type acl_method: str
165 :type table_index: int
171 with VatTerminal(node) as vat:
172 vat.vat_terminal_exec_cmd_from_template("classify_add_session_l2.vat",
173 acl_method=acl_method,
174 table_index=table_index,
181 def vpp_configures_classify_session_hex(node, acl_method, table_index,
182 skip_n, match_n, hex_value):
183 """Configuration of classify session with hex value.
185 :param node: VPP node to setup classify session.
186 :param acl_method: ACL method - deny/permit.
187 :param table_index: Classify table index.
188 :param skip_n: Number of skip vectors based on mask.
189 :param match_n: Number of match vectors based on mask.
190 :param hex_value: Classify hex value.
192 :type acl_method: str
193 :type table_index: int
198 with VatTerminal(node) as vat:
199 vat.vat_terminal_exec_cmd_from_template(
200 "classify_add_session_hex.vat",
201 acl_method=acl_method,
202 table_index=table_index,
208 def compute_classify_hex_mask(ip_version, protocol, direction):
209 """Compute classify hex mask for TCP or UDP packet matching.
211 :param ip_version: Version of IP protocol.
212 :param protocol: Type of protocol.
213 :param direction: Traffic direction.
214 :type ip_version: str
217 :return: Classify hex mask.
219 :raises ValueError: If protocol is not TCP or UDP.
220 :raises ValueError: If direction is not source or destination or
221 source + destination.
223 if protocol == 'TCP' or protocol == 'UDP':
224 base_mask = Classify._compute_base_mask(ip_version)
226 if direction == 'source':
227 return base_mask + 'FFFF0000'
228 elif direction == 'destination':
229 return base_mask + '0000FFFF'
230 elif direction == 'source + destination':
231 return base_mask + 'FFFFFFFF'
233 raise ValueError("Invalid direction!")
235 raise ValueError("Invalid protocol!")
238 def compute_classify_hex_value(hex_mask, source_port, destination_port):
239 """Compute classify hex value for TCP or UDP packet matching.
241 :param hex_mask: Classify hex mask.
242 :param source_port: Source TCP/UDP port.
243 :param destination_port: Destination TCP/UDP port.
245 :type source_port: str
246 :type destination_port: str
247 :return: Classify hex value.
250 source_port_hex = Classify._port_convert(source_port)
251 destination_port_hex = Classify._port_convert(destination_port)
253 return hex_mask[:-8] + source_port_hex + destination_port_hex
256 def _port_convert(port):
257 """Convert port number for classify hex table format.
259 :param port: TCP/UDP port number.
261 :return: TCP/UDP port number in 4-digit hexadecimal format.
264 return '{0:04x}'.format(int(port))
267 def _compute_base_mask(ip_version):
268 """Compute base classify hex mask based on IP version.
270 :param ip_version: Version of IP protocol.
271 :type ip_version: str
272 :return: Base hex mask.
275 if ip_version == 'ip4':
277 # base value of classify hex table for IPv4 TCP/UDP ports
278 elif ip_version == 'ip6':
280 # base value of classify hex table for IPv6 TCP/UDP ports
282 raise ValueError("Invalid IP version!")
285 def get_classify_table_data(node, table_index):
286 """Retrieve settings for classify table by ID.
288 :param node: VPP node to retrieve classify data from.
289 :param table_index: Index of a specific classify table.
291 :type table_index: int
292 :return: Classify table settings.
295 with VatTerminal(node) as vat:
296 data = vat.vat_terminal_exec_cmd_from_template(
297 "classify_table_info.vat",
303 def get_classify_session_data(node, table_index, session_index=None):
304 """Retrieve settings for all classify sessions in a table,
305 or for a specific classify session.
307 :param node: VPP node to retrieve classify data from.
308 :param table_index: Index of a classify table.
309 :param session_index: Index of a specific classify session. (Optional)
311 :type table_index: int
312 :type session_index: int
313 :return: List of classify session settings, or a dictionary of settings
314 for a specific classify session.
317 with VatTerminal(node) as vat:
318 data = vat.vat_terminal_exec_cmd_from_template(
319 "classify_session_dump.vat",
322 if session_index is not None:
323 return data[0][session_index]