4 Vhosts setup test
[csit.git] / resources / libraries / python / Classify.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14
15 from robot.api import logger
16
17 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
18
19
20 class Classify(object):
21     """Classify utilities."""
22
23     @staticmethod
24     def vpp_create_classify_table(node, ip_version, direction):
25         """Create classify table.
26
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.
30         :type node: dict
31         :type ip_version: str
32         :type direction: str
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.
39         """
40         output = VatExecutor.cmd_from_template(node, "classify_add_table.vat",
41                                                ip_version=ip_version,
42                                                direction=direction)
43
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']))
50         else:
51             raise RuntimeError('Unable to create classify table on node {}'
52                                .format(node['host']))
53
54         return table_index, skip_n, match_n
55
56     @staticmethod
57     def vpp_create_classify_table_hex(node, hex_mask):
58         """Create classify table with hex mask.
59
60         :param node: VPP node to create classify table based on hex mask.
61         :param hex_mask: Classify hex mask.
62         :type node: dict
63         :type hex_mask: str
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.
70         """
71         output = VatExecutor.cmd_from_template(node,
72                                                "classify_add_table_hex.vat",
73                                                hex_mask=hex_mask)
74
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']))
81         else:
82             raise RuntimeError('Unable to create classify table on node {}'
83                                .format(node['host']))
84
85         return table_index, skip_n, match_n
86
87     @staticmethod
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.
91
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.
100         :type node: dict
101         :type acl_method: str
102         :type table_index: int
103         :type skip_n: int
104         :type match_n: int
105         :type ip_version: str
106         :type direction: str
107         :type address: str
108         """
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,
113                                                     skip_n=skip_n,
114                                                     match_n=match_n,
115                                                     ip_version=ip_version,
116                                                     direction=direction,
117                                                     address=address)
118
119     @staticmethod
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.
123
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.
130         :type node: dict
131         :type acl_method: str
132         :type table_index: int
133         :type skip_n: int
134         :type match_n: int
135         :type hex_value: str
136         """
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,
142                 skip_n=skip_n,
143                 match_n=match_n,
144                 hex_value=hex_value)
145
146     @staticmethod
147     def compute_classify_hex_mask(ip_version, protocol, direction):
148         """Compute classify hex mask for TCP or UDP packet matching.
149
150         :param ip_version: Version of IP protocol.
151         :param protocol: Type of protocol.
152         :param direction: Traffic direction.
153         :type ip_version: str
154         :type protocol: str
155         :type direction: str
156         :return: Classify hex mask.
157         :rtype : str
158         :raises ValueError: If protocol is not TCP or UDP.
159         :raises ValueError: If direction is not source or destination or
160                             source + destination.
161         """
162         if protocol == 'TCP' or protocol == 'UDP':
163             base_mask = Classify._compute_base_mask(ip_version)
164
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'
171             else:
172                 raise ValueError("Invalid direction!")
173         else:
174             raise ValueError("Invalid protocol!")
175
176     @staticmethod
177     def compute_classify_hex_value(hex_mask, source_port, destination_port):
178         """Compute classify hex value for TCP or UDP packet matching.
179
180         :param hex_mask: Classify hex mask.
181         :param source_port: Source TCP/UDP port.
182         :param destination_port: Destination TCP/UDP port.
183         :type hex_mask: str
184         :type source_port: str
185         :type destination_port: str
186         :return: Classify hex value.
187         :rtype: str
188         """
189         source_port_hex = Classify._port_convert(source_port)
190         destination_port_hex = Classify._port_convert(destination_port)
191
192         return hex_mask[:-8] + source_port_hex + destination_port_hex
193
194     @staticmethod
195     def _port_convert(port):
196         """Convert port number for classify hex table format.
197
198         :param port: TCP/UDP port number.
199         :type port: str
200         :return: TCP/UDP port number in 4-digit hexadecimal format.
201         :rtype: str
202         """
203         return '{0:04x}'.format(int(port))
204
205     @staticmethod
206     def _compute_base_mask(ip_version):
207         """Compute base classify hex mask based on IP version.
208
209         :param ip_version: Version of IP protocol.
210         :type ip_version: str
211         :return: Base hex mask.
212         :rtype: str
213         """
214         if ip_version == 'ip4':
215             return 68 * '0'
216             # base value of classify hex table for IPv4 TCP/UDP ports
217         elif ip_version == 'ip6':
218             return 108 * '0'
219             # base value of classify hex table for IPv6 TCP/UDP ports
220         else:
221             raise ValueError("Invalid IP version!")