Python3: resources and libraries
[csit.git] / resources / libraries / python / GBP.py
1 # Copyright (c) 2019 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 """GBP utilities library."""
15
16 from enum import IntEnum
17
18 from ipaddress import ip_address
19
20 from resources.libraries.python.IPUtil import IPUtil
21 from resources.libraries.python.L2Util import L2Util
22 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
23
24
25 class GBPEndpointFlags(IntEnum):
26     """GBP Endpoint Flags."""
27     GBP_API_ENDPOINT_FLAG_NONE = 0
28     GBP_API_ENDPOINT_FLAG_BOUNCE = 1
29     GBP_API_ENDPOINT_FLAG_REMOTE = 2
30     GBP_API_ENDPOINT_FLAG_LEARNT = 4
31     GBP_API_ENDPOINT_FLAG_EXTERNAL = 8
32
33
34 class GBPBridgeDomainFlags(IntEnum):
35     """GBP Bridge Domain Flags."""
36     GBP_BD_API_FLAG_NONE = 0
37     GBP_BD_API_FLAG_DO_NOT_LEARN = 1
38     GBP_BD_API_FLAG_UU_FWD_DROP = 2
39     GBP_BD_API_FLAG_MCAST_DROP = 4
40     GBP_BD_API_FLAG_UCAST_ARP = 8
41
42
43 class GBPSubnetType(IntEnum):
44     """GBP Subnet Type."""
45     GBP_API_SUBNET_TRANSPORT = 1
46     GBP_API_SUBNET_STITCHED_INTERNAL = 2  # pylint: disable=invalid-name
47     GBP_API_SUBNET_STITCHED_EXTERNAL = 3  # pylint: disable=invalid-name
48     GBP_API_SUBNET_L3_OUT = 4
49     GBP_API_SUBNET_ANON_L3_OUT = 5
50
51
52 class GBPExtItfFlags(IntEnum):
53     """GBP External Interface Flags."""
54     GBP_API_EXT_ITF_F_NONE = 0
55     GBP_API_EXT_ITF_F_ANON = 1
56
57
58 class GBPRuleAction(IntEnum):
59     """GBP Rule Action."""
60     GBP_API_RULE_PERMIT = 1
61     GBP_API_RULE_DENY = 2
62     GBP_API_RULE_REDIRECT = 3
63
64
65 class GBPHashMode(IntEnum):
66     """GBP Hash Mode."""
67     GBP_API_HASH_MODE_SRC_IP = 1
68     GBP_API_HASH_MODE_DST_IP = 2
69     GBP_API_HASH_MODE_SYMETRIC = 3
70
71
72 class GBP:
73     """GBP utilities."""
74
75     @staticmethod
76     def gbp_route_domain_add(
77             node, rd_id=1, ip4_table_id=1, ip6_table_id=0,
78             ip4_uu_sw_if_index=0xffffffff, ip6_uu_sw_if_index=0xffffffff):
79         """Add GBP route domain.
80
81         :param node: Node to add GBP route domain on.
82         :param rd_id: GBP route domain ID.
83         :param ip4_table_id: IPv4 table.
84         :param ip6_table_id: IPv6 table.
85         :param ip4_uu_sw_if_index: IPv4 unicast interface index.
86         :param ip6_uu_sw_if_index: IPv6 unicast interface index.
87         :type node: dict
88         :type rd_id: int
89         :type ip4_table_id: int
90         :type ip6_table_id: int
91         :type ip4_uu_sw_if_index: int
92         :type ip6_uu_sw_if_index: int
93         """
94         cmd = u"gbp_route_domain_add"
95         err_msg = f"Failed to add GBP route domain on {node[u'host']}!"
96
97         args_in = dict(
98             rd=dict(
99                 rd_id=rd_id,
100                 ip4_table_id=ip4_table_id,
101                 ip6_table_id=ip6_table_id,
102                 ip4_uu_sw_if_index=ip4_uu_sw_if_index,
103                 ip6_uu_sw_if_index=ip6_uu_sw_if_index
104             )
105         )
106
107         with PapiSocketExecutor(node) as papi_exec:
108             papi_exec.add(cmd, **args_in).get_reply(err_msg)
109
110     @staticmethod
111     def gbp_bridge_domain_add(
112             node, bvi_sw_if_index, bd_id=1, rd_id=1,
113             uu_fwd_sw_if_index=0xffffffff, bm_flood_sw_if_index=0xffffffff):
114         """Add GBP bridge domain.
115
116         :param node: Node to add GBP bridge domain on.
117         :param bvi_sw_if_index: SW index of BVI/loopback interface.
118         :param bd_id: GBP bridge domain ID.
119         :param rd_id: GBP route domain ID.
120         :param uu_fwd_sw_if_index: Unicast forward interface index.
121         :param bm_flood_sw_if_index: Bcast/Mcast flood interface index.
122         :type node: dict
123         :type bvi_sw_if_index: int
124         :type bd_id: int
125         :type rd_id: int
126         :type uu_fwd_sw_if_index: int
127         :type bm_flood_sw_if_index: int
128         """
129         cmd = u"gbp_bridge_domain_add"
130         err_msg = f"Failed to add GBP bridge domain on {node[u'host']}!"
131
132         args_in = dict(
133             bd=dict(
134                 flags=getattr(
135                     GBPBridgeDomainFlags, u"GBP_BD_API_FLAG_NONE"
136                 ).value,
137                 bvi_sw_if_index=bvi_sw_if_index,
138                 uu_fwd_sw_if_index=uu_fwd_sw_if_index,
139                 bm_flood_sw_if_index=bm_flood_sw_if_index,
140                 bd_id=bd_id,
141                 rd_id=rd_id
142             )
143         )
144
145         with PapiSocketExecutor(node) as papi_exec:
146             papi_exec.add(cmd, **args_in).get_reply(err_msg)
147
148     @staticmethod
149     def gbp_endpoint_group_add(
150             node, sclass, bd_id=1, rd_id=1, vnid=1,
151             uplink_sw_if_index=0xffffffff, remote_ep_timeout=0xffffffff):
152         """Add GBP endpoint group.
153
154         :param node: Node to add GBP endpoint group on.
155         :param sclass: Source CLASS.
156         :param bd_id: GBP bridge domain ID.
157         :param rd_id: GBP route domain ID.
158         :param uplink_sw_if_index: Uplink interface index.
159         :param remote_ep_timeout: Remote endpoint interface index.
160         :param vnid: VNID.
161         :type node: dict
162         :type sclass: int
163         :type bd_id: int
164         :type rd_id: int
165         :type vnid: int
166         :type uplink_sw_if_index: int
167         :type remote_ep_timeout: int
168         """
169         cmd = u"gbp_endpoint_group_add"
170         err_msg = f"Failed to add GBP endpoint group on {node[u'host']}!"
171
172         args_in = dict(
173             epg=dict(
174                 uplink_sw_if_index=uplink_sw_if_index,
175                 bd_id=bd_id,
176                 rd_id=rd_id,
177                 vnid=vnid,
178                 sclass=sclass,
179                 retention=dict(
180                     remote_ep_timeout=remote_ep_timeout
181                 )
182             )
183         )
184
185         with PapiSocketExecutor(node) as papi_exec:
186             papi_exec.add(cmd, **args_in).get_reply(err_msg)
187
188     @staticmethod
189     def gbp_endpoint_add(node, sw_if_index, ip_addr, mac_addr, sclass):
190         """Add GBP endpoint.
191
192         :param node: Node to add GBP endpoint on.
193         :param sw_if_index: SW index of interface.
194         :param ip_addr: GBP route domain ID.
195         :param mac_addr: MAC address.
196         :param sclass: Source CLASS.
197         :type node: dict
198         :type sw_if_index: int
199         :type ip_addr: str
200         :type mac_addr: str
201         :type sclass: int
202         """
203         cmd = u"gbp_endpoint_add"
204         err_msg = f"Failed to add GBP endpoint on {node[u'host']}!"
205
206         ips = list()
207         ips.append(IPUtil.create_ip_address_object(ip_address(ip_addr)))
208         tun_src = IPUtil.create_ip_address_object(ip_address(u"0.0.0.0"))
209         tun_dst = IPUtil.create_ip_address_object(ip_address(u"0.0.0.0"))
210
211         args_in = dict(
212             endpoint=dict(
213                 sw_if_index=sw_if_index,
214                 ips=ips,
215                 n_ips=len(ips),
216                 mac=L2Util.mac_to_bin(mac_addr),
217                 sclass=sclass,
218                 flags=getattr(
219                     GBPEndpointFlags, u"GBP_API_ENDPOINT_FLAG_EXTERNAL"
220                 ).value,
221                 tun=dict(
222                     src=tun_src,
223                     dst=tun_dst
224                 )
225             )
226         )
227
228         with PapiSocketExecutor(node) as papi_exec:
229             papi_exec.add(cmd, **args_in).get_reply(err_msg)
230
231     @staticmethod
232     def gbp_ext_itf_add_del(node, sw_if_index, bd_id=1, rd_id=1):
233         """Add external interface to GBP.
234
235         :param node: Node to add external GBP interface on.
236         :param sw_if_index: SW index of interface.
237         :param bd_id: GBP bridge domain ID.
238         :param rd_id: GBP route domain ID.
239         :type node: dict
240         :type sw_if_index: int
241         :type bd_id: int
242         :type rd_id: int
243         """
244         cmd = u"gbp_ext_itf_add_del"
245         err_msg = u"Failed to add external GBP interface on {node[u'host']}!"
246
247         args_in = dict(
248             is_add=1,
249             ext_itf=dict(
250                 sw_if_index=sw_if_index,
251                 bd_id=bd_id,
252                 rd_id=rd_id,
253                 flags=getattr(GBPExtItfFlags, u"GBP_API_EXT_ITF_F_NONE").value
254             )
255         )
256
257         with PapiSocketExecutor(node) as papi_exec:
258             papi_exec.add(cmd, **args_in).get_reply(err_msg)
259
260     @staticmethod
261     def gbp_subnet_add_del(
262             node, address, subnet_length, sclass, rd_id=1,
263             sw_if_index=0xffffffff):
264         """Add external interface to GBP.
265
266         :param node: Node to add GBP subnet on.
267         :param address: IPv4 adddress.
268         :param subnet_length: IPv4 address subnet.
269         :param sclass: Source CLASS.
270         :param rd_id: GBP route domain ID.
271         :param sw_if_index: Interface index.
272         :type node: dict
273         :type address: int
274         :type subnet_length: int
275         :type sclass: int
276         :type rd_id: int
277         :type sw_if_index: int
278         """
279         cmd = u"gbp_subnet_add_del"
280         err_msg = f"Failed to add GBP subnet on {node[u'host']}!"
281
282         args_in = dict(
283             is_add=1,
284             subnet=dict(
285                 type=getattr(GBPSubnetType, u"GBP_API_SUBNET_L3_OUT").value,
286                 sw_if_index=sw_if_index,
287                 sclass=sclass,
288                 prefix=dict(
289                     address=IPUtil.create_ip_address_object(
290                         ip_address(address)
291                     ),
292                     len=int(subnet_length)
293                 ),
294                 rd_id=rd_id
295             )
296         )
297
298         with PapiSocketExecutor(node) as papi_exec:
299             papi_exec.add(cmd, **args_in).get_reply(err_msg)
300
301     @staticmethod
302     def gbp_contract_add_del(node, sclass, dclass, acl_index=0, hash_mode=None):
303         """Add GBP contract.
304
305         :param node: Node to add GBP contract on.
306         :param sclass: Source CLASS.
307         :param dclass: Destination CLASS.
308         :param acl_index: Index of ACL rule.
309         :param hash_mode: GBP contract hash mode.
310         :type node: dict
311         :type sclass: int
312         :type dclass: int
313         :type acl_index: int
314         :type hash_mode: str
315         """
316         cmd = u"gbp_contract_add_del"
317         err_msg = f"Failed to add GBP contract on {node[u'host']}!"
318
319         hash_mode = u"GBP_API_HASH_MODE_SRC_IP" if hash_mode is None \
320             else hash_mode
321         rule_permit = dict(
322             action=getattr(GBPRuleAction, u"GBP_API_RULE_PERMIT").value,
323             nh_set=dict(
324                 hash_mode=getattr(GBPHashMode, hash_mode).value,
325                 n_nhs=8,
326                 nhs=[dict()]*8,
327             )
328         )
329         rules = [rule_permit, rule_permit]
330
331         args_in = dict(
332             is_add=1,
333             contract=dict(
334                 acl_index=acl_index,
335                 sclass=sclass,
336                 dclass=dclass,
337                 n_rules=len(rules),
338                 rules=rules,
339                 n_ether_types=16,
340                 allowed_ethertypes=[0x800, 0x86dd] + [0]*14
341             )
342         )
343
344         with PapiSocketExecutor(node) as papi_exec:
345             papi_exec.add(cmd, **args_in).get_reply(err_msg)