blob: 454f3edce3f14bf3521b5fa8e83b63ecae8e24dc [file] [log] [blame]
Klement Sekeraf62ae122016-10-11 11:47:09 +02001import vpp_papi
2from logging import error
3from hook import Hook
4
5# from vnet/vnet/mpls/mpls_types.h
6MPLS_IETF_MAX_LABEL = 0xfffff
7MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
8
9
10class VppPapiProvider(object):
11 """VPP-api provider using vpp-papi
12
13 @property hook: hook object providing before and after api/cli hooks
14
15
16 """
17
18 def __init__(self, name, shm_prefix):
19 self.hook = Hook()
20 self.name = name
21 self.shm_prefix = shm_prefix
22
23 def register_hook(self, hook):
24 """Replace hook registration with new hook
25
26 :param hook:
27
28 """
29 self.hook = hook
30
31 def connect(self):
32 """Connect the API to VPP"""
33 vpp_papi.connect(self.name, self.shm_prefix)
34
35 def disconnect(self):
36 """Disconnect the API from VPP"""
37 vpp_papi.disconnect()
38
39 def api(self, api_fn, api_args, expected_retval=0):
40 """Call API function and check it's return value
41 Call the appropriate hooks before and after the API call
42
43 :param api_fn: API function to call
44 :param api_args: tuple of API function arguments
45 :param expected_retval: Expected return value (Default value = 0)
46 :returns: reply from the API
47
48 """
49 self.hook.before_api(api_fn.__name__, api_args)
50 reply = api_fn(*api_args)
51 if hasattr(reply, 'retval') and reply.retval != expected_retval:
52 msg = "API call failed, expected retval == %d, got %s" % (
53 expected_retval, repr(reply))
54 error(msg)
55 raise Exception(msg)
56 self.hook.after_api(api_fn.__name__, api_args)
57 return reply
58
59 def cli(self, cli):
60 """Execute a CLI, calling the before/after hooks appropriately
61
62 :param cli: CLI to execute
63 :returns: CLI output
64
65 """
66 self.hook.before_cli(cli)
67 cli += '\n'
68 r = vpp_papi.cli_inband(len(cli), cli)
69 self.hook.after_cli(cli)
70 if(hasattr(r, 'reply')):
71 return r.reply[0].decode().rstrip('\x00')
72
73 def show_version(self):
74 """ """
75 return vpp_papi.show_version()
76
77 def pg_create_interface(self, pg_index):
78 """
79
80 :param pg_index:
81
82 """
83 return self.api(vpp_papi.pg_create_interface, (pg_index, ))
84
85 def sw_interface_dump(self, filter=None):
86 """
87
88 :param filter: (Default value = None)
89
90 """
91 if filter is not None:
92 args = (1, filter)
93 else:
94 args = (0, b'')
95 return self.api(vpp_papi.sw_interface_dump, args)
96
97 def sw_interface_add_del_address(self, sw_if_index, addr, addr_len,
98 is_ipv6=0, is_add=1, del_all=0):
99 """
100
101 :param addr: param is_ipv6: (Default value = 0)
102 :param sw_if_index:
103 :param addr_len:
104 :param is_ipv6: (Default value = 0)
105 :param is_add: (Default value = 1)
106 :param del_all: (Default value = 0)
107
108 """
109 return self.api(vpp_papi.sw_interface_add_del_address,
110 (sw_if_index, is_add, is_ipv6, del_all, addr_len, addr))
111
112 def sw_interface_ra_suppress(self, sw_if_index):
113 suppress = 1
114 managed = 0
115 other = 0
116 ll_option = 0
117 send_unicast = 0
118 cease = 0
119 is_no = 0
120 default_router = 0
121 max_interval = 0
122 min_interval = 0
123 lifetime = 0
124 initial_count = 0
125 initial_interval = 0
126 async = False
127 return self.api(vpp_papi.sw_interface_ip6nd_ra_config,
128 (sw_if_index, suppress, managed, other,
129 ll_option, send_unicast, cease, is_no,
130 default_router, max_interval, min_interval,
131 lifetime, initial_count, initial_interval, async))
132
133
134 def vxlan_add_del_tunnel(
135 self,
136 src_addr,
137 dst_addr,
138 is_add=1,
139 is_ipv6=0,
140 encap_vrf_id=0,
141 decap_next_index=0xFFFFFFFF,
142 vni=0):
143 """
144
145 :param dst_addr:
146 :param src_addr:
147 :param is_add: (Default value = 1)
148 :param is_ipv6: (Default value = 0)
149 :param encap_vrf_id: (Default value = 0)
150 :param decap_next_index: (Default value = 0xFFFFFFFF)
151 :param vni: (Default value = 0)
152
153 """
154 return self.api(vpp_papi.vxlan_add_del_tunnel,
155 (is_add, is_ipv6, src_addr, dst_addr, encap_vrf_id,
156 decap_next_index, vni))
157
158 def sw_interface_set_l2_bridge(self, sw_if_index, bd_id,
159 shg=0, bvi=0, enable=1):
160 """
161
162 :param bd_id:
163 :param sw_if_index:
164 :param shg: (Default value = 0)
165 :param bvi: (Default value = 0)
166 :param enable: (Default value = 1)
167
168 """
169 return self.api(vpp_papi.sw_interface_set_l2_bridge,
170 (sw_if_index, bd_id, shg, bvi, enable))
171
172 def sw_interface_set_l2_xconnect(self, rx_sw_if_index, tx_sw_if_index,
173 enable):
174 """Create or delete unidirectional cross-connect from Tx interface to
175 Rx interface.
176
177 :param rx_sw_if_index: Software interface index of Rx interface.
178 :param tx_sw_if_index: Software interface index of Tx interface.
179 :param enable: Create cross-connect if equal to 1, delete cross-connect
180 if equal to 0.
181 :type rx_sw_if_index: str or int
182 :type rx_sw_if_index: str or int
183 :type enable: int
184
185 """
186 return self.api(vpp_papi.sw_interface_set_l2_xconnect,
187 (rx_sw_if_index, tx_sw_if_index, enable))
188
189 def sw_interface_set_flags(self, sw_if_index, admin_up_down,
190 link_up_down=0, deleted=0):
191 """
192
193 :param admin_up_down:
194 :param sw_if_index:
195 :param link_up_down: (Default value = 0)
196 :param deleted: (Default value = 0)
197
198 """
199 return self.api(vpp_papi.sw_interface_set_flags,
200 (sw_if_index, admin_up_down, link_up_down, deleted))
201
202 def create_subif(self, sw_if_index, sub_id, outer_vlan, inner_vlan,
203 no_tags=0, one_tag=0, two_tags=0, dot1ad=0, exact_match=0,
204 default_sub=0, outer_vlan_id_any=0, inner_vlan_id_any=0):
205 """Create subinterface
206 from vpe.api: set dot1ad = 0 for dot1q, set dot1ad = 1 for dot1ad
207
208 :param sub_id: param inner_vlan:
209 :param sw_if_index:
210 :param outer_vlan:
211 :param inner_vlan:
212 :param no_tags: (Default value = 0)
213 :param one_tag: (Default value = 0)
214 :param two_tags: (Default value = 0)
215 :param dot1ad: (Default value = 0)
216 :param exact_match: (Default value = 0)
217 :param default_sub: (Default value = 0)
218 :param outer_vlan_id_any: (Default value = 0)
219 :param inner_vlan_id_any: (Default value = 0)
220
221 """
222 return self.api(
223 vpp_papi.create_subif,
224 (sw_if_index,
225 sub_id,
226 no_tags,
227 one_tag,
228 two_tags,
229 dot1ad,
230 exact_match,
231 default_sub,
232 outer_vlan_id_any,
233 inner_vlan_id_any,
234 outer_vlan,
235 inner_vlan))
236
237 def create_vlan_subif(self, sw_if_index, vlan):
238 """
239
240 :param vlan:
241 :param sw_if_index:
242
243 """
244 return self.api(vpp_papi.create_vlan_subif, (sw_if_index, vlan))
245
246 def ip_add_del_route(
247 self,
248 dst_address,
249 dst_address_length,
250 next_hop_address,
251 next_hop_sw_if_index=0xFFFFFFFF,
252 table_id=0,
253 resolve_attempts=0,
254 classify_table_index=0xFFFFFFFF,
255 next_hop_out_label=MPLS_LABEL_INVALID,
256 next_hop_table_id=0,
257 create_vrf_if_needed=0,
258 resolve_if_needed=0,
259 is_add=1,
260 is_drop=0,
Juraj Sloboda86a2c572016-10-27 10:44:25 +0200261 is_unreach=0,
262 is_prohibit=0,
Klement Sekeraf62ae122016-10-11 11:47:09 +0200263 is_ipv6=0,
264 is_local=0,
265 is_classify=0,
266 is_multipath=0,
267 is_resolve_host=0,
268 is_resolve_attached=0,
269 not_last=0,
270 next_hop_weight=1):
271 """
272
273 :param dst_address_length:
274 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
275 :param dst_address:
276 :param next_hop_address:
277 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
278 :param vrf_id: (Default value = 0)
279 :param lookup_in_vrf: (Default value = 0)
280 :param resolve_attempts: (Default value = 0)
281 :param classify_table_index: (Default value = 0xFFFFFFFF)
282 :param create_vrf_if_needed: (Default value = 0)
283 :param resolve_if_needed: (Default value = 0)
284 :param is_add: (Default value = 1)
285 :param is_drop: (Default value = 0)
286 :param is_ipv6: (Default value = 0)
287 :param is_local: (Default value = 0)
288 :param is_classify: (Default value = 0)
289 :param is_multipath: (Default value = 0)
290 :param is_resolve_host: (Default value = 0)
291 :param is_resolve_attached: (Default value = 0)
292 :param not_last: (Default value = 0)
293 :param next_hop_weight: (Default value = 1)
294
295 """
296 return self.api(
297 vpp_papi.ip_add_del_route,
298 (next_hop_sw_if_index,
299 table_id,
300 resolve_attempts,
301 classify_table_index,
302 next_hop_out_label,
303 next_hop_table_id,
304 create_vrf_if_needed,
305 resolve_if_needed,
306 is_add,
307 is_drop,
Juraj Sloboda86a2c572016-10-27 10:44:25 +0200308 is_unreach,
309 is_prohibit,
Klement Sekeraf62ae122016-10-11 11:47:09 +0200310 is_ipv6,
311 is_local,
312 is_classify,
313 is_multipath,
314 is_resolve_host,
315 is_resolve_attached,
316 not_last,
317 next_hop_weight,
318 dst_address_length,
319 dst_address,
320 next_hop_address))