Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 1 | # NB NB NB NB NB NB NB NB NB NB NB |
| 2 | # |
| 3 | # NOTE: The API binary wrappers in this file are in the process of being |
| 4 | # deprecated. DO NOT ADD NEW WRAPPERS HERE. Call the functions using |
| 5 | # named arguments directly instead. |
| 6 | # |
| 7 | |
Paul Vinciguerra | 00671cf | 2018-11-25 12:47:04 -0800 | [diff] [blame] | 8 | import os |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 9 | import time |
Ole Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 10 | import queue |
Ole Troan | f159f58 | 2019-02-28 20:20:47 +0100 | [diff] [blame] | 11 | from six import moves, iteritems |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 12 | from config import config |
Klement Sekera | 77c821c | 2022-04-14 16:29:07 +0200 | [diff] [blame] | 13 | from vpp_papi import VPPApiClient |
Paul Vinciguerra | 00671cf | 2018-11-25 12:47:04 -0800 | [diff] [blame] | 14 | from hook import Hook |
Klement Sekera | 77c821c | 2022-04-14 16:29:07 +0200 | [diff] [blame] | 15 | from vpp_papi_exceptions import CliFailedCommandError, CliSyntaxError,\ |
| 16 | UnexpectedApiReturnValueError |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 17 | |
Ole Troan | f159f58 | 2019-02-28 20:20:47 +0100 | [diff] [blame] | 18 | # |
| 19 | # Dictionary keyed on message name to override default values for |
| 20 | # named parameters |
| 21 | # |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 22 | defaultmapping = { |
Paul Vinciguerra | c0e9441 | 2020-04-28 01:12:04 -0400 | [diff] [blame] | 23 | |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 24 | 'acl_interface_add_del': {'is_add': 1, 'is_input': 1}, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 25 | 'bd_ip_mac_add_del': {'is_add': 1, }, |
Jakub Grajciar | 4682feb | 2019-09-02 13:28:52 +0200 | [diff] [blame] | 26 | 'bfd_udp_add': {'is_authenticated': False, 'bfd_key_id': None, |
| 27 | 'conf_key_id': None}, |
| 28 | 'bfd_udp_auth_activate': {'bfd_key_id': None, 'conf_key_id': None, |
| 29 | 'is_delayed': False}, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 30 | 'bier_disp_entry_add_del': {'next_hop_rpf_id': -1, 'next_hop_is_ip4': 1, |
| 31 | 'is_add': 1, }, |
| 32 | 'bier_disp_table_add_del': {'is_add': 1, }, |
| 33 | 'bier_imp_add': {'is_add': 1, }, |
| 34 | 'bier_route_add_del': {'is_add': 1, }, |
| 35 | 'bier_table_add_del': {'is_add': 1, }, |
| 36 | 'bridge_domain_add_del': {'flood': 1, 'uu_flood': 1, 'forward': 1, |
| 37 | 'learn': 1, 'is_add': 1, }, |
Paul Vinciguerra | 9a29f79 | 2019-04-30 20:44:25 -0700 | [diff] [blame] | 38 | 'bvi_delete': {}, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 39 | 'geneve_add_del_tunnel': {'mcast_sw_if_index': 4294967295, 'is_add': 1, |
| 40 | 'decap_next_index': 4294967295, }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 41 | 'input_acl_set_interface': {'ip4_table_index': 4294967295, |
| 42 | 'ip6_table_index': 4294967295, |
| 43 | 'l2_table_index': 4294967295, }, |
| 44 | 'ip6_add_del_address_using_prefix': {'is_add': 1, }, |
| 45 | 'ip6nd_send_router_solicitation': {'irt': 1, 'mrt': 120, }, |
| 46 | 'ip_add_del_route': {'next_hop_sw_if_index': 4294967295, |
| 47 | 'next_hop_weight': 1, 'next_hop_via_label': 1048576, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 48 | 'classify_table_index': 4294967295, 'is_add': 1, }, |
| 49 | 'ip_mroute_add_del': {'is_add': 1, }, |
| 50 | 'ip_neighbor_add_del': {'is_add': 1, }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 51 | 'ipsec_interface_add_del_spd': {'is_add': 1, }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 52 | 'ipsec_spd_add_del': {'is_add': 1, }, |
| 53 | 'ipsec_spd_dump': {'sa_id': 4294967295, }, |
| 54 | 'ipsec_spd_entry_add_del': {'local_port_stop': 65535, |
| 55 | 'remote_port_stop': 65535, 'priority': 100, |
| 56 | 'is_outbound': 1, |
| 57 | 'is_add': 1, }, |
| 58 | 'ipsec_tunnel_if_add_del': {'is_add': 1, 'anti_replay': 1, }, |
| 59 | 'l2_emulation': {'enable': 1, }, |
| 60 | 'l2fib_add_del': {'is_add': 1, }, |
| 61 | 'lisp_add_del_adjacency': {'is_add': 1, }, |
| 62 | 'lisp_add_del_local_eid': {'is_add': 1, }, |
| 63 | 'lisp_add_del_locator': {'priority': 1, 'weight': 1, 'is_add': 1, }, |
| 64 | 'lisp_add_del_locator_set': {'is_add': 1, }, |
| 65 | 'lisp_add_del_remote_mapping': {'is_add': 1, }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 66 | 'macip_acl_interface_add_del': {'is_add': 1, }, |
| 67 | 'mpls_ip_bind_unbind': {'is_ip4': 1, 'is_bind': 1, }, |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 68 | 'mpls_route_add_del': {'mr_next_hop_sw_if_index': 4294967295, |
| 69 | 'mr_next_hop_weight': 1, |
| 70 | 'mr_next_hop_via_label': 1048576, |
| 71 | 'mr_is_add': 1, |
| 72 | 'mr_classify_table_index': 4294967295, }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 73 | 'mpls_table_add_del': {'is_add': 1, }, |
| 74 | 'mpls_tunnel_add_del': {'next_hop_sw_if_index': 4294967295, |
| 75 | 'next_hop_weight': 1, |
| 76 | 'next_hop_via_label': 1048576, |
| 77 | 'is_add': 1, }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 78 | 'output_acl_set_interface': {'ip4_table_index': 4294967295, |
| 79 | 'ip6_table_index': 4294967295, |
| 80 | 'l2_table_index': 4294967295, }, |
| 81 | 'pppoe_add_del_session': {'is_add': 1, }, |
Jakub Grajciar | cd01fb4 | 2020-03-02 13:16:53 +0100 | [diff] [blame] | 82 | 'policer_add_del': {'is_add': 1, 'conform_action': {'type': 1}, }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 83 | 'set_ipfix_exporter': {'collector_port': 4739, }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 84 | 'sr_policy_add': {'weight': 1, 'is_encap': 1, }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 85 | 'sw_interface_add_del_address': {'is_add': 1, }, |
| 86 | 'sw_interface_ip6nd_ra_prefix': {'val_lifetime': 4294967295, |
| 87 | 'pref_lifetime': 4294967295, }, |
| 88 | 'sw_interface_set_ip_directed_broadcast': {'enable': 1, }, |
| 89 | 'sw_interface_set_l2_bridge': {'enable': 1, }, |
| 90 | 'sw_interface_set_mpls_enable': {'enable': 1, }, |
| 91 | 'sw_interface_set_mtu': {'mtu': [0, 0, 0, 0], }, |
| 92 | 'sw_interface_set_unnumbered': {'is_add': 1, }, |
| 93 | 'sw_interface_span_enable_disable': {'state': 1, }, |
| 94 | 'vxlan_add_del_tunnel': {'mcast_sw_if_index': 4294967295, 'is_add': 1, |
| 95 | 'decap_next_index': 4294967295, |
| 96 | 'instance': 4294967295, }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 97 | 'want_bfd_events': {'enable_disable': 1, }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 98 | 'want_igmp_events': {'enable': 1, }, |
| 99 | 'want_interface_events': {'enable_disable': 1, }, |
Jakub Grajciar | 145e330 | 2019-10-24 13:52:42 +0200 | [diff] [blame] | 100 | 'want_l2_macs_events': {'enable_disable': 1, 'pid': os.getpid(), }, |
Jerome Tollet | 0f8d100 | 2021-01-07 12:44:17 +0100 | [diff] [blame] | 101 | 'want_l2_macs_events2': {'enable_disable': 1, 'pid': os.getpid(), }, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 102 | } |
Ole Troan | f159f58 | 2019-02-28 20:20:47 +0100 | [diff] [blame] | 103 | |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 104 | |
Paul Vinciguerra | 0b0a84c | 2020-05-02 10:40:33 -0400 | [diff] [blame] | 105 | def as_fn_signature(d): |
| 106 | return ", ".join(f"{k}={v}" for k, v in d.items()) |
| 107 | |
| 108 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 109 | class VppPapiProvider(object): |
| 110 | """VPP-api provider using vpp-papi |
| 111 | |
| 112 | @property hook: hook object providing before and after api/cli hooks |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 113 | """ |
| 114 | |
Klement Sekera | e0545ef | 2017-01-25 08:00:40 +0100 | [diff] [blame] | 115 | _zero, _negative = range(2) |
| 116 | |
Ole Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 117 | def __init__(self, name, test_class, read_timeout): |
Paul Vinciguerra | 895e2f8 | 2019-01-08 20:37:40 -0800 | [diff] [blame] | 118 | self.hook = Hook(test_class) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 119 | self.name = name |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 120 | self.test_class = test_class |
Klement Sekera | e0545ef | 2017-01-25 08:00:40 +0100 | [diff] [blame] | 121 | self._expect_api_retval = self._zero |
| 122 | self._expect_stack = [] |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 123 | |
Paul Vinciguerra | 1954229 | 2019-03-17 17:34:46 -0700 | [diff] [blame] | 124 | # install_dir is a class attribute. We need to set it before |
| 125 | # calling the constructor. |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 126 | VPPApiClient.apidir = config.vpp_install_dir |
Paul Vinciguerra | 04575c5 | 2019-01-13 10:26:05 -0800 | [diff] [blame] | 127 | |
Paul Vinciguerra | 1954229 | 2019-03-17 17:34:46 -0700 | [diff] [blame] | 128 | self.vpp = VPPApiClient(logger=test_class.logger, |
| 129 | read_timeout=read_timeout, |
Ole Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 130 | use_socket=True, |
Klement Sekera | e263685 | 2021-03-16 12:52:12 +0100 | [diff] [blame] | 131 | server_address=test_class.get_api_sock_path()) |
Ole Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 132 | self._events = queue.Queue() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 133 | |
Klement Sekera | e0545ef | 2017-01-25 08:00:40 +0100 | [diff] [blame] | 134 | def __enter__(self): |
| 135 | return self |
| 136 | |
Klement Sekera | 7d6afb3 | 2018-11-08 11:52:04 +0100 | [diff] [blame] | 137 | def assert_negative_api_retval(self): |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 138 | """ Expect API failure - used with with, e.g.:: |
| 139 | |
| 140 | with self.vapi.assert_negative_api_retval(): |
| 141 | self.vapi.<api call expected to fail> |
| 142 | |
| 143 | .. |
Klement Sekera | 7d6afb3 | 2018-11-08 11:52:04 +0100 | [diff] [blame] | 144 | """ |
Klement Sekera | e0545ef | 2017-01-25 08:00:40 +0100 | [diff] [blame] | 145 | self._expect_stack.append(self._expect_api_retval) |
| 146 | self._expect_api_retval = self._negative |
| 147 | return self |
| 148 | |
Klement Sekera | 7d6afb3 | 2018-11-08 11:52:04 +0100 | [diff] [blame] | 149 | def assert_zero_api_retval(self): |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 150 | """ Expect API success - used with with, e.g.:: |
Klement Sekera | 7d6afb3 | 2018-11-08 11:52:04 +0100 | [diff] [blame] | 151 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 152 | with self.vapi.assert_negative_api_retval(): |
| 153 | self.vapi.<api call expected to succeed> |
| 154 | |
| 155 | :note: this is useful only inside another with block |
| 156 | as success is the default expected value |
Klement Sekera | 7d6afb3 | 2018-11-08 11:52:04 +0100 | [diff] [blame] | 157 | """ |
Klement Sekera | e0545ef | 2017-01-25 08:00:40 +0100 | [diff] [blame] | 158 | self._expect_stack.append(self._expect_api_retval) |
| 159 | self._expect_api_retval = self._zero |
| 160 | return self |
| 161 | |
| 162 | def __exit__(self, exc_type, exc_value, traceback): |
| 163 | self._expect_api_retval = self._expect_stack.pop() |
| 164 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 165 | def register_hook(self, hook): |
| 166 | """Replace hook registration with new hook |
| 167 | |
| 168 | :param hook: |
| 169 | |
| 170 | """ |
| 171 | self.hook = hook |
| 172 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 173 | def collect_events(self): |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 174 | """ Collect all events from the internal queue and clear the queue. """ |
Ole Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 175 | result = [] |
| 176 | while True: |
| 177 | try: |
| 178 | e = self._events.get(block=False) |
| 179 | result.append(e) |
| 180 | except queue.Empty: |
| 181 | return result |
| 182 | return result |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 183 | |
| 184 | def wait_for_event(self, timeout, name=None): |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 185 | """ Wait for and return next event. """ |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 186 | if name: |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 187 | self.test_class.logger.debug("Expecting event '%s' within %ss", |
| 188 | name, timeout) |
Eyal Bari | 2019748 | 2017-09-13 12:29:08 +0300 | [diff] [blame] | 189 | else: |
| 190 | self.test_class.logger.debug("Expecting event within %ss", |
| 191 | timeout) |
Ole Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 192 | try: |
| 193 | e = self._events.get(timeout=timeout) |
| 194 | except queue.Empty: |
| 195 | raise Exception("Event did not occur within timeout") |
| 196 | msgname = type(e).__name__ |
| 197 | if name and msgname != name: |
| 198 | raise Exception("Unexpected event received: %s, expected: %s" |
| 199 | % msgname) |
| 200 | self.test_class.logger.debug("Returning event %s:%s" % (name, e)) |
| 201 | return e |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 202 | |
| 203 | def __call__(self, name, event): |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 204 | """ Enqueue event in the internal event queue. """ |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 205 | self.test_class.logger.debug("New event: %s: %s" % (name, event)) |
Ole Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 206 | self._events.put(event) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 207 | |
Ole Troan | f159f58 | 2019-02-28 20:20:47 +0100 | [diff] [blame] | 208 | def factory(self, name, apifn): |
| 209 | def f(*a, **ka): |
| 210 | fields = apifn._func.msg.fields |
| 211 | |
| 212 | # add positional and kw arguments |
| 213 | d = ka |
| 214 | for i, o in enumerate(fields[3:]): |
| 215 | try: |
| 216 | d[o] = a[i] |
Jakub Grajciar | 4682feb | 2019-09-02 13:28:52 +0200 | [diff] [blame] | 217 | except BaseException: |
Ole Troan | f159f58 | 2019-02-28 20:20:47 +0100 | [diff] [blame] | 218 | break |
| 219 | |
| 220 | # Default override |
| 221 | if name in defaultmapping: |
| 222 | for k, v in iteritems(defaultmapping[name]): |
| 223 | if k in d: |
| 224 | continue |
| 225 | d[k] = v |
| 226 | return self.api(apifn, d) |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 227 | |
Ole Troan | f159f58 | 2019-02-28 20:20:47 +0100 | [diff] [blame] | 228 | return f |
| 229 | |
Paul Vinciguerra | 7ab9970 | 2019-03-05 04:30:04 -0800 | [diff] [blame] | 230 | def __getattribute__(self, name): |
Ole Troan | f159f58 | 2019-02-28 20:20:47 +0100 | [diff] [blame] | 231 | try: |
Paul Vinciguerra | 7ab9970 | 2019-03-05 04:30:04 -0800 | [diff] [blame] | 232 | method = super(VppPapiProvider, self).__getattribute__(name) |
| 233 | except AttributeError: |
| 234 | method = self.factory(name, getattr(self.papi, name)) |
| 235 | # lazily load the method so we don't need to call factory |
| 236 | # again for this name. |
| 237 | setattr(self, name, method) |
| 238 | return method |
Ole Troan | f159f58 | 2019-02-28 20:20:47 +0100 | [diff] [blame] | 239 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 240 | def connect(self): |
| 241 | """Connect the API to VPP""" |
Ole Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 242 | # This might be called before VPP is prepared to listen to the socket |
| 243 | retries = 0 |
Klement Sekera | e263685 | 2021-03-16 12:52:12 +0100 | [diff] [blame] | 244 | while not os.path.exists(self.test_class.get_api_sock_path()): |
Ole Troan | 4376ab2 | 2021-03-03 10:40:05 +0100 | [diff] [blame] | 245 | time.sleep(0.5) |
| 246 | retries += 1 |
| 247 | if retries > 120: |
| 248 | break |
| 249 | self.vpp.connect(self.name[:63]) |
Klement Sekera | 7112c54 | 2017-03-01 09:53:19 +0100 | [diff] [blame] | 250 | self.papi = self.vpp.api |
| 251 | self.vpp.register_event_callback(self) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 252 | |
| 253 | def disconnect(self): |
| 254 | """Disconnect the API from VPP""" |
Klement Sekera | 7112c54 | 2017-03-01 09:53:19 +0100 | [diff] [blame] | 255 | self.vpp.disconnect() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 256 | |
| 257 | def api(self, api_fn, api_args, expected_retval=0): |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 258 | """ Call API function and check it's return value. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 259 | Call the appropriate hooks before and after the API call |
| 260 | |
| 261 | :param api_fn: API function to call |
| 262 | :param api_args: tuple of API function arguments |
| 263 | :param expected_retval: Expected return value (Default value = 0) |
| 264 | :returns: reply from the API |
| 265 | |
| 266 | """ |
| 267 | self.hook.before_api(api_fn.__name__, api_args) |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 268 | reply = api_fn(**api_args) |
Klement Sekera | e0545ef | 2017-01-25 08:00:40 +0100 | [diff] [blame] | 269 | if self._expect_api_retval == self._negative: |
| 270 | if hasattr(reply, 'retval') and reply.retval >= 0: |
Paul Vinciguerra | 0b0a84c | 2020-05-02 10:40:33 -0400 | [diff] [blame] | 271 | msg = "%s(%s) passed unexpectedly: expected negative " \ |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 272 | "return value instead of %d in %s" % \ |
Paul Vinciguerra | 0b0a84c | 2020-05-02 10:40:33 -0400 | [diff] [blame] | 273 | (api_fn.__name__, as_fn_signature(api_args), |
| 274 | reply.retval, |
| 275 | moves.reprlib.repr(reply)) |
Klement Sekera | e0545ef | 2017-01-25 08:00:40 +0100 | [diff] [blame] | 276 | self.test_class.logger.info(msg) |
Klement Sekera | 77c821c | 2022-04-14 16:29:07 +0200 | [diff] [blame] | 277 | raise UnexpectedApiReturnValueError(reply.retval, msg) |
Klement Sekera | e0545ef | 2017-01-25 08:00:40 +0100 | [diff] [blame] | 278 | elif self._expect_api_retval == self._zero: |
| 279 | if hasattr(reply, 'retval') and reply.retval != expected_retval: |
Paul Vinciguerra | 0b0a84c | 2020-05-02 10:40:33 -0400 | [diff] [blame] | 280 | msg = "%s(%s) failed, expected %d return value instead " \ |
| 281 | "of %d in %s" % (api_fn.__name__, |
| 282 | as_fn_signature(api_args), |
| 283 | expected_retval, reply.retval, |
Paul Vinciguerra | 1b534f5 | 2019-06-15 20:31:31 -0400 | [diff] [blame] | 284 | repr(reply)) |
Klement Sekera | e0545ef | 2017-01-25 08:00:40 +0100 | [diff] [blame] | 285 | self.test_class.logger.info(msg) |
Klement Sekera | 77c821c | 2022-04-14 16:29:07 +0200 | [diff] [blame] | 286 | raise UnexpectedApiReturnValueError(reply.retval, msg) |
Klement Sekera | e0545ef | 2017-01-25 08:00:40 +0100 | [diff] [blame] | 287 | else: |
| 288 | raise Exception("Internal error, unexpected value for " |
| 289 | "self._expect_api_retval %s" % |
| 290 | self._expect_api_retval) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 291 | self.hook.after_api(api_fn.__name__, api_args) |
| 292 | return reply |
| 293 | |
Dave Barach | 5932ce1 | 2019-10-09 17:28:04 -0400 | [diff] [blame] | 294 | def cli_return_response(self, cli): |
| 295 | """ Execute a CLI, calling the before/after hooks appropriately. |
| 296 | Return the reply without examining it |
| 297 | |
| 298 | :param cli: CLI to execute |
| 299 | :returns: response object |
| 300 | |
| 301 | """ |
| 302 | self.hook.before_cli(cli) |
| 303 | cli += '\n' |
| 304 | r = self.papi.cli_inband(cmd=cli) |
| 305 | self.hook.after_cli(cli) |
| 306 | return r |
| 307 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 308 | def cli(self, cli): |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 309 | """ Execute a CLI, calling the before/after hooks appropriately. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 310 | |
| 311 | :param cli: CLI to execute |
| 312 | :returns: CLI output |
| 313 | |
| 314 | """ |
Dave Barach | 5932ce1 | 2019-10-09 17:28:04 -0400 | [diff] [blame] | 315 | r = self.cli_return_response(cli) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 316 | if r.retval == -156: |
| 317 | raise CliSyntaxError(r.reply) |
| 318 | if r.retval != 0: |
| 319 | raise CliFailedCommandError(r.reply) |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 320 | if hasattr(r, 'reply'): |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 321 | return r.reply |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 322 | |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 323 | def ppcli(self, cli): |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 324 | """ Helper method to print CLI command in case of info logging level. |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 325 | |
| 326 | :param cli: CLI to execute |
| 327 | :returns: CLI output |
| 328 | """ |
Ole Troan | 6ed154f | 2019-10-15 19:31:55 +0200 | [diff] [blame] | 329 | return cli + "\n" + self.cli(cli) |
Jan | 49c0fca | 2016-10-26 15:44:27 +0200 | [diff] [blame] | 330 | |
Juraj Sloboda | 4b9669d | 2018-01-15 10:39:21 +0100 | [diff] [blame] | 331 | def ip6nd_send_router_solicitation(self, sw_if_index, irt=1, mrt=120, |
| 332 | mrc=0, mrd=0): |
| 333 | return self.api(self.papi.ip6nd_send_router_solicitation, |
| 334 | {'irt': irt, |
| 335 | 'mrt': mrt, |
| 336 | 'mrc': mrc, |
| 337 | 'mrd': mrd, |
| 338 | 'sw_if_index': sw_if_index}) |
| 339 | |
Juraj Sloboda | b3f9050 | 2018-10-04 15:15:16 +0200 | [diff] [blame] | 340 | def want_interface_events(self, enable_disable=1): |
| 341 | return self.api(self.papi.want_interface_events, |
| 342 | {'enable_disable': enable_disable, |
| 343 | 'pid': os.getpid(), }) |
| 344 | |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 345 | def sw_interface_set_mac_address(self, sw_if_index, mac): |
| 346 | return self.api(self.papi.sw_interface_set_mac_address, |
| 347 | {'sw_if_index': sw_if_index, |
| 348 | 'mac_address': mac}) |
| 349 | |
Ole Troan | e1ade68 | 2019-03-04 23:55:43 +0100 | [diff] [blame] | 350 | def p2p_ethernet_add(self, sw_if_index, remote_mac, subif_id): |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 351 | """Create p2p ethernet subinterface |
| 352 | |
| 353 | :param sw_if_index: main (parent) interface |
| 354 | :param remote_mac: client (remote) mac address |
| 355 | |
| 356 | """ |
| 357 | return self.api( |
| 358 | self.papi.p2p_ethernet_add, |
| 359 | {'parent_if_index': sw_if_index, |
| 360 | 'remote_mac': remote_mac, |
| 361 | 'subif_id': subif_id}) |
| 362 | |
Ole Troan | e1ade68 | 2019-03-04 23:55:43 +0100 | [diff] [blame] | 363 | def p2p_ethernet_del(self, sw_if_index, remote_mac): |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 364 | """Delete p2p ethernet subinterface |
| 365 | |
| 366 | :param sw_if_index: main (parent) interface |
| 367 | :param remote_mac: client (remote) mac address |
| 368 | |
| 369 | """ |
| 370 | return self.api( |
| 371 | self.papi.p2p_ethernet_del, |
| 372 | {'parent_if_index': sw_if_index, |
| 373 | 'remote_mac': remote_mac}) |
| 374 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 375 | def create_vlan_subif(self, sw_if_index, vlan): |
| 376 | """ |
| 377 | |
| 378 | :param vlan: |
| 379 | :param sw_if_index: |
| 380 | |
| 381 | """ |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 382 | return self.api(self.papi.create_vlan_subif, |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 383 | {'sw_if_index': sw_if_index, |
| 384 | 'vlan_id': vlan}) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 385 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 386 | def create_loopback(self, mac=''): |
| 387 | """ |
| 388 | |
| 389 | :param mac: (Optional) |
| 390 | """ |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 391 | return self.api(self.papi.create_loopback, |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 392 | {'mac_address': mac}) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 393 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 394 | def ip_route_dump(self, table_id, is_ip6=False): |
| 395 | return self.api(self.papi.ip_route_dump, |
| 396 | {'table': { |
| 397 | 'table_id': table_id, |
| 398 | 'is_ip6': is_ip6 |
| 399 | }}) |
| 400 | |
Neale Ranns | 976b259 | 2019-12-04 06:11:00 +0000 | [diff] [blame] | 401 | def ip_route_v2_dump(self, table_id, is_ip6=False, src=0): |
| 402 | return self.api(self.papi.ip_route_v2_dump, |
| 403 | { |
| 404 | 'src': src, |
| 405 | 'table': { |
| 406 | 'table_id': table_id, |
| 407 | 'is_ip6': is_ip6 |
| 408 | } |
| 409 | }) |
| 410 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 411 | def ip_neighbor_add_del(self, |
| 412 | sw_if_index, |
| 413 | mac_address, |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 414 | ip_address, |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 415 | is_add=1, |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 416 | flags=0): |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 417 | """ Add neighbor MAC to IPv4 or IPv6 address. |
| 418 | |
| 419 | :param sw_if_index: |
| 420 | :param mac_address: |
| 421 | :param dst_address: |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 422 | :param is_add: (Default value = 1) |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 423 | :param flags: (Default value = 0/NONE) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 424 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 425 | return self.api( |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 426 | self.papi.ip_neighbor_add_del, |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 427 | { |
| 428 | 'is_add': is_add, |
| 429 | 'neighbor': { |
| 430 | 'sw_if_index': sw_if_index, |
| 431 | 'flags': flags, |
| 432 | 'mac_address': mac_address, |
| 433 | 'ip_address': ip_address |
| 434 | } |
| 435 | } |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 436 | ) |
Pavel Kotucek | f6e3dc4 | 2016-11-04 09:58:01 +0100 | [diff] [blame] | 437 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 438 | def udp_encap_add(self, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 439 | src_ip, |
| 440 | dst_ip, |
| 441 | src_port, |
| 442 | dst_port, |
| 443 | table_id=0): |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 444 | """ Add a GRE tunnel |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 445 | :param src_ip: |
| 446 | :param dst_ip: |
| 447 | :param src_port: |
| 448 | :param dst_port: |
| 449 | :param outer_fib_id: (Default value = 0) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 450 | """ |
| 451 | |
| 452 | return self.api( |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 453 | self.papi.udp_encap_add, |
| 454 | { |
| 455 | 'udp_encap': { |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 456 | 'src_ip': src_ip, |
| 457 | 'dst_ip': dst_ip, |
| 458 | 'src_port': src_port, |
| 459 | 'dst_port': dst_port, |
| 460 | 'table_id': table_id |
| 461 | } |
| 462 | }) |
| 463 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 464 | def udp_encap_del(self, id): |
| 465 | return self.api(self.papi.udp_encap_del, {'id': id}) |
| 466 | |
| 467 | def udp_encap_dump(self): |
| 468 | return self.api(self.papi.udp_encap_dump, {}) |
| 469 | |
| 470 | def want_udp_encap_stats(self, enable=1): |
| 471 | return self.api(self.papi.want_udp_encap_stats, |
| 472 | {'enable': enable, |
| 473 | 'pid': os.getpid()}) |
| 474 | |
| 475 | def mpls_route_dump(self, table_id): |
| 476 | return self.api(self.papi.mpls_route_dump, |
| 477 | {'table': { |
| 478 | 'mt_table_id': table_id |
| 479 | }}) |
| 480 | |
| 481 | def mpls_table_dump(self): |
| 482 | return self.api(self.papi.mpls_table_dump, {}) |
| 483 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 484 | def mpls_table_add_del( |
| 485 | self, |
| 486 | table_id, |
| 487 | is_add=1): |
| 488 | """ |
| 489 | |
| 490 | :param table_id |
| 491 | :param is_add: (Default value = 1) |
| 492 | |
| 493 | """ |
| 494 | |
| 495 | return self.api( |
| 496 | self.papi.mpls_table_add_del, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 497 | {'mt_table': |
| 498 | { |
| 499 | 'mt_table_id': table_id, |
| 500 | }, |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 501 | 'mt_is_add': is_add}) |
| 502 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 503 | def mpls_route_add_del(self, |
| 504 | table_id, |
| 505 | label, |
| 506 | eos, |
| 507 | eos_proto, |
| 508 | is_multicast, |
| 509 | paths, |
| 510 | is_add, |
| 511 | is_multipath): |
| 512 | """ MPLS Route add/del """ |
| 513 | return self.api( |
| 514 | self.papi.mpls_route_add_del, |
| 515 | {'mr_route': |
| 516 | { |
| 517 | 'mr_table_id': table_id, |
| 518 | 'mr_label': label, |
| 519 | 'mr_eos': eos, |
| 520 | 'mr_eos_proto': eos_proto, |
| 521 | 'mr_is_multicast': is_multicast, |
| 522 | 'mr_n_paths': len(paths), |
| 523 | 'mr_paths': paths, |
| 524 | }, |
| 525 | 'mr_is_add': is_add, |
| 526 | 'mr_is_multipath': is_multipath}) |
| 527 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 528 | def mpls_ip_bind_unbind( |
| 529 | self, |
| 530 | label, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 531 | prefix, |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 532 | table_id=0, |
| 533 | ip_table_id=0, |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 534 | is_bind=1): |
| 535 | """ |
| 536 | """ |
| 537 | return self.api( |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 538 | self.papi.mpls_ip_bind_unbind, |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 539 | {'mb_mpls_table_id': table_id, |
| 540 | 'mb_label': label, |
| 541 | 'mb_ip_table_id': ip_table_id, |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 542 | 'mb_is_bind': is_bind, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 543 | 'mb_prefix': prefix}) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 544 | |
| 545 | def mpls_tunnel_add_del( |
| 546 | self, |
| 547 | tun_sw_if_index, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 548 | paths, |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 549 | is_add=1, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 550 | l2_only=0, |
| 551 | is_multicast=0): |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 552 | """ |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 553 | """ |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 554 | return self.api( |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 555 | self.papi.mpls_tunnel_add_del, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 556 | {'mt_is_add': is_add, |
| 557 | 'mt_tunnel': |
| 558 | { |
| 559 | 'mt_sw_if_index': tun_sw_if_index, |
| 560 | 'mt_l2_only': l2_only, |
| 561 | 'mt_is_multicast': is_multicast, |
| 562 | 'mt_n_paths': len(paths), |
| 563 | 'mt_paths': paths, |
| 564 | }}) |
Matus Fabian | de88675 | 2016-12-07 03:38:19 -0800 | [diff] [blame] | 565 | |
Steve Shin | 7957d6e | 2016-12-19 09:24:50 -0800 | [diff] [blame] | 566 | def input_acl_set_interface( |
| 567 | self, |
| 568 | is_add, |
| 569 | sw_if_index, |
| 570 | ip4_table_index=0xFFFFFFFF, |
| 571 | ip6_table_index=0xFFFFFFFF, |
| 572 | l2_table_index=0xFFFFFFFF): |
| 573 | """ |
| 574 | :param is_add: |
| 575 | :param sw_if_index: |
| 576 | :param ip4_table_index: (Default value = 0xFFFFFFFF) |
| 577 | :param ip6_table_index: (Default value = 0xFFFFFFFF) |
| 578 | :param l2_table_index: (Default value = 0xFFFFFFFF) |
| 579 | """ |
| 580 | |
| 581 | return self.api( |
| 582 | self.papi.input_acl_set_interface, |
Klement Sekera | 77fabdb | 2017-01-02 07:46:14 +0100 | [diff] [blame] | 583 | {'sw_if_index': sw_if_index, |
| 584 | 'ip4_table_index': ip4_table_index, |
| 585 | 'ip6_table_index': ip6_table_index, |
| 586 | 'l2_table_index': l2_table_index, |
| 587 | 'is_add': is_add}) |
| 588 | |
Andrew Yourtchenko | 815d7d5 | 2018-02-07 11:37:02 +0100 | [diff] [blame] | 589 | def output_acl_set_interface( |
| 590 | self, |
| 591 | is_add, |
| 592 | sw_if_index, |
| 593 | ip4_table_index=0xFFFFFFFF, |
| 594 | ip6_table_index=0xFFFFFFFF, |
| 595 | l2_table_index=0xFFFFFFFF): |
| 596 | """ |
| 597 | :param is_add: |
| 598 | :param sw_if_index: |
| 599 | :param ip4_table_index: (Default value = 0xFFFFFFFF) |
| 600 | :param ip6_table_index: (Default value = 0xFFFFFFFF) |
| 601 | :param l2_table_index: (Default value = 0xFFFFFFFF) |
| 602 | """ |
| 603 | |
| 604 | return self.api( |
| 605 | self.papi.output_acl_set_interface, |
| 606 | {'sw_if_index': sw_if_index, |
| 607 | 'ip4_table_index': ip4_table_index, |
| 608 | 'ip6_table_index': ip6_table_index, |
| 609 | 'l2_table_index': l2_table_index, |
| 610 | 'is_add': is_add}) |
| 611 | |
Klement Sekera | 77fabdb | 2017-01-02 07:46:14 +0100 | [diff] [blame] | 612 | def set_ipfix_exporter( |
| 613 | self, |
| 614 | collector_address, |
| 615 | src_address, |
| 616 | path_mtu, |
| 617 | template_interval, |
| 618 | vrf_id=0, |
| 619 | collector_port=4739, |
| 620 | udp_checksum=0): |
| 621 | return self.api( |
| 622 | self.papi.set_ipfix_exporter, |
| 623 | { |
| 624 | 'collector_address': collector_address, |
| 625 | 'collector_port': collector_port, |
| 626 | 'src_address': src_address, |
| 627 | 'vrf_id': vrf_id, |
| 628 | 'path_mtu': path_mtu, |
| 629 | 'template_interval': template_interval, |
| 630 | 'udp_checksum': udp_checksum, |
| 631 | }) |
Neale Ranns | fca0c24 | 2017-01-13 07:57:46 -0800 | [diff] [blame] | 632 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 633 | def mfib_signal_dump(self): |
| 634 | return self.api(self.papi.mfib_signal_dump, {}) |
| 635 | |
| 636 | def ip_mroute_dump(self, table_id, is_ip6=False): |
| 637 | return self.api(self.papi.ip_mroute_dump, |
| 638 | {'table': { |
| 639 | 'table_id': table_id, |
| 640 | 'is_ip6': is_ip6 |
| 641 | }}) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 642 | |
Hongjun Ni | 62f9cdd | 2017-07-04 20:11:57 +0800 | [diff] [blame] | 643 | def pppoe_add_del_session( |
| 644 | self, |
| 645 | client_ip, |
| 646 | client_mac, |
| 647 | session_id=0, |
| 648 | is_add=1, |
Hongjun Ni | 62f9cdd | 2017-07-04 20:11:57 +0800 | [diff] [blame] | 649 | decap_vrf_id=0): |
| 650 | """ |
| 651 | |
| 652 | :param is_add: (Default value = 1) |
| 653 | :param is_ipv6: (Default value = 0) |
| 654 | :param client_ip: |
| 655 | :param session_id: (Default value = 0) |
| 656 | :param client_mac: |
| 657 | :param decap_vrf_id: (Default value = 0) |
| 658 | |
| 659 | """ |
| 660 | return self.api(self.papi.pppoe_add_del_session, |
| 661 | {'is_add': is_add, |
Hongjun Ni | 62f9cdd | 2017-07-04 20:11:57 +0800 | [diff] [blame] | 662 | 'session_id': session_id, |
| 663 | 'client_ip': client_ip, |
| 664 | 'decap_vrf_id': decap_vrf_id, |
| 665 | 'client_mac': client_mac}) |
Kris Michielsen | 9107443 | 2017-06-22 13:00:20 +0200 | [diff] [blame] | 666 | |
Neale Ranns | 7c922dc | 2018-08-30 06:12:27 -0700 | [diff] [blame] | 667 | def sr_mpls_policy_add(self, bsid, weight, type, segments): |
| 668 | return self.api(self.papi.sr_mpls_policy_add, |
| 669 | {'bsid': bsid, |
| 670 | 'weight': weight, |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 671 | 'is_spray': type, |
Neale Ranns | 7c922dc | 2018-08-30 06:12:27 -0700 | [diff] [blame] | 672 | 'n_segments': len(segments), |
| 673 | 'segments': segments}) |
| 674 | |
| 675 | def sr_mpls_policy_del(self, bsid): |
| 676 | return self.api(self.papi.sr_mpls_policy_del, |
| 677 | {'bsid': bsid}) |
| 678 | |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 679 | def bier_table_add_del(self, |
| 680 | bti, |
| 681 | mpls_label, |
| 682 | is_add=1): |
| 683 | """ BIER Table add/del """ |
| 684 | return self.api( |
| 685 | self.papi.bier_table_add_del, |
| 686 | {'bt_tbl_id': {"bt_set": bti.set_id, |
| 687 | "bt_sub_domain": bti.sub_domain_id, |
| 688 | "bt_hdr_len_id": bti.hdr_len_id}, |
| 689 | 'bt_label': mpls_label, |
| 690 | 'bt_is_add': is_add}) |
| 691 | |
| 692 | def bier_table_dump(self): |
| 693 | return self.api(self.papi.bier_table_dump, {}) |
| 694 | |
| 695 | def bier_route_add_del(self, |
| 696 | bti, |
| 697 | bp, |
Neale Ranns | 9128637 | 2017-12-05 13:24:04 -0800 | [diff] [blame] | 698 | paths, |
Neale Ranns | ef90ed0 | 2018-09-13 08:45:12 -0700 | [diff] [blame] | 699 | is_add=1, |
| 700 | is_replace=0): |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 701 | """ BIER Route add/del """ |
| 702 | return self.api( |
| 703 | self.papi.bier_route_add_del, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 704 | { |
| 705 | 'br_route': { |
| 706 | 'br_tbl_id': {"bt_set": bti.set_id, |
| 707 | "bt_sub_domain": bti.sub_domain_id, |
| 708 | "bt_hdr_len_id": bti.hdr_len_id}, |
| 709 | 'br_bp': bp, |
| 710 | 'br_n_paths': len(paths), |
| 711 | 'br_paths': paths, |
| 712 | }, |
| 713 | 'br_is_add': is_add, |
| 714 | 'br_is_replace': is_replace |
| 715 | }) |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 716 | |
| 717 | def bier_route_dump(self, bti): |
| 718 | return self.api( |
| 719 | self.papi.bier_route_dump, |
| 720 | {'br_tbl_id': {"bt_set": bti.set_id, |
| 721 | "bt_sub_domain": bti.sub_domain_id, |
| 722 | "bt_hdr_len_id": bti.hdr_len_id}}) |
| 723 | |
| 724 | def bier_imp_add(self, |
| 725 | bti, |
| 726 | src, |
| 727 | ibytes, |
| 728 | is_add=1): |
| 729 | """ BIER Imposition Add """ |
| 730 | return self.api( |
| 731 | self.papi.bier_imp_add, |
| 732 | {'bi_tbl_id': {"bt_set": bti.set_id, |
| 733 | "bt_sub_domain": bti.sub_domain_id, |
| 734 | "bt_hdr_len_id": bti.hdr_len_id}, |
| 735 | 'bi_src': src, |
| 736 | 'bi_n_bytes': len(ibytes), |
| 737 | 'bi_bytes': ibytes}) |
| 738 | |
| 739 | def bier_imp_del(self, bi_index): |
| 740 | """ BIER Imposition del """ |
| 741 | return self.api( |
| 742 | self.papi.bier_imp_del, |
| 743 | {'bi_index': bi_index}) |
| 744 | |
| 745 | def bier_imp_dump(self): |
| 746 | return self.api(self.papi.bier_imp_dump, {}) |
| 747 | |
| 748 | def bier_disp_table_add_del(self, |
| 749 | bdti, |
| 750 | is_add=1): |
| 751 | """ BIER Disposition Table add/del """ |
| 752 | return self.api( |
| 753 | self.papi.bier_disp_table_add_del, |
| 754 | {'bdt_tbl_id': bdti, |
| 755 | 'bdt_is_add': is_add}) |
| 756 | |
| 757 | def bier_disp_table_dump(self): |
| 758 | return self.api(self.papi.bier_disp_table_dump, {}) |
| 759 | |
| 760 | def bier_disp_entry_add_del(self, |
| 761 | bdti, |
| 762 | bp, |
| 763 | payload_proto, |
Neale Ranns | f051072 | 2018-01-31 11:35:41 -0800 | [diff] [blame] | 764 | next_hop_afi, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 765 | next_hop, |
| 766 | next_hop_tbl_id=0, |
| 767 | next_hop_rpf_id=~0, |
| 768 | next_hop_is_ip4=1, |
| 769 | is_add=1): |
| 770 | """ BIER Route add/del """ |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 771 | lstack = [] |
| 772 | while (len(lstack) < 16): |
| 773 | lstack.append({}) |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 774 | return self.api( |
| 775 | self.papi.bier_disp_entry_add_del, |
| 776 | {'bde_tbl_id': bdti, |
| 777 | 'bde_bp': bp, |
| 778 | 'bde_payload_proto': payload_proto, |
| 779 | 'bde_n_paths': 1, |
Paul Vinciguerra | 79a32a4 | 2019-07-11 19:09:30 -0400 | [diff] [blame] | 780 | 'bde_paths': [{'table_id': next_hop_tbl_id, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 781 | 'rpf_id': next_hop_rpf_id, |
| 782 | 'n_labels': 0, |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 783 | 'label_stack': lstack}], |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 784 | 'bde_is_add': is_add}) |
| 785 | |
| 786 | def bier_disp_entry_dump(self, bdti): |
| 787 | return self.api( |
| 788 | self.papi.bier_disp_entry_dump, |
| 789 | {'bde_tbl_id': bdti}) |
Gabriel Ganne | 8527f12 | 2017-10-02 11:41:24 +0200 | [diff] [blame] | 790 | |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 791 | def ipsec_spd_add_del(self, spd_id, is_add=1): |
| 792 | """ SPD add/del - Wrapper to add or del ipsec SPD |
| 793 | Sample CLI : 'ipsec spd add 1' |
| 794 | |
| 795 | :param spd_id - SPD ID to be created in the vpp . mandatory |
| 796 | :param is_add - create (1) or delete(0) SPD (Default 1 - add) . |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 797 | optional |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 798 | :returns: reply from the API |
| 799 | """ |
| 800 | return self.api( |
| 801 | self.papi.ipsec_spd_add_del, { |
| 802 | 'spd_id': spd_id, 'is_add': is_add}) |
| 803 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 804 | def ipsec_spds_dump(self): |
| 805 | return self.api(self.papi.ipsec_spds_dump, {}) |
| 806 | |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 807 | def ipsec_interface_add_del_spd(self, spd_id, sw_if_index, is_add=1): |
| 808 | """ IPSEC interface SPD add/del - \ |
| 809 | Wrapper to associate/disassociate SPD to interface in VPP |
| 810 | Sample CLI : 'set interface ipsec spd GigabitEthernet0/6/0 1' |
| 811 | |
| 812 | :param spd_id - SPD ID to associate with the interface . mandatory |
| 813 | :param sw_if_index - Interface Index which needs to ipsec \ |
| 814 | association mandatory |
| 815 | :param is_add - add(1) or del(0) association with interface \ |
| 816 | (Default 1 - add) . optional |
| 817 | :returns: reply from the API |
| 818 | """ |
| 819 | return self.api( |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 820 | self.papi.ipsec_interface_add_del_spd, |
| 821 | {'spd_id': spd_id, 'sw_if_index': sw_if_index, 'is_add': is_add}) |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 822 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 823 | def ipsec_spd_interface_dump(self, spd_index=None): |
| 824 | return self.api(self.papi.ipsec_spd_interface_dump, |
| 825 | {'spd_index': spd_index if spd_index else 0, |
| 826 | 'spd_index_valid': 1 if spd_index else 0}) |
| 827 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 828 | def ipsec_spd_entry_add_del(self, |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 829 | spd_id, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 830 | sa_id, |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 831 | local_address_start, |
| 832 | local_address_stop, |
| 833 | remote_address_start, |
| 834 | remote_address_stop, |
| 835 | local_port_start=0, |
| 836 | local_port_stop=65535, |
| 837 | remote_port_start=0, |
| 838 | remote_port_stop=65535, |
| 839 | protocol=0, |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 840 | policy=0, |
| 841 | priority=100, |
| 842 | is_outbound=1, |
| 843 | is_add=1, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 844 | is_ipv6=0, |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 845 | is_ip_any=0): |
| 846 | """ IPSEC policy SPD add/del - |
| 847 | Wrapper to configure ipsec SPD policy entries in VPP |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 848 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 849 | :param spd_id: SPD ID for the policy |
| 850 | :param local_address_start: local-ip-range start address |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 851 | :param local_address_stop: local-ip-range stop address |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 852 | :param remote_address_start: remote-ip-range start address |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 853 | :param remote_address_stop: remote-ip-range stop address |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 854 | :param local_port_start: (Default value = 0) |
| 855 | :param local_port_stop: (Default value = 65535) |
| 856 | :param remote_port_start: (Default value = 0) |
| 857 | :param remote_port_stop: (Default value = 65535) |
| 858 | :param protocol: Any(0), AH(51) & ESP(50) protocol (Default value = 0) |
| 859 | :param sa_id: Security Association ID for mapping it to SPD |
| 860 | :param policy: bypass(0), discard(1), resolve(2) or protect(3) action |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 861 | (Default value = 0) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 862 | :param priority: value for the spd action (Default value = 100) |
| 863 | :param is_outbound: flag for inbound(0) or outbound(1) |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 864 | (Default value = 1) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 865 | :param is_add: (Default value = 1) |
“mystarrocks” | 23f0c45 | 2017-12-11 07:11:51 -0800 | [diff] [blame] | 866 | """ |
| 867 | return self.api( |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 868 | self.papi.ipsec_spd_entry_add_del, |
| 869 | { |
| 870 | 'is_add': is_add, |
| 871 | 'entry': |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 872 | { |
| 873 | 'spd_id': spd_id, |
| 874 | 'sa_id': sa_id, |
| 875 | 'local_address_start': local_address_start, |
| 876 | 'local_address_stop': local_address_stop, |
| 877 | 'remote_address_start': remote_address_start, |
| 878 | 'remote_address_stop': remote_address_stop, |
| 879 | 'local_port_start': local_port_start, |
| 880 | 'local_port_stop': local_port_stop, |
| 881 | 'remote_port_start': remote_port_start, |
| 882 | 'remote_port_stop': remote_port_stop, |
| 883 | 'protocol': protocol, |
| 884 | 'policy': policy, |
| 885 | 'priority': priority, |
| 886 | 'is_outbound': is_outbound, |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 887 | } |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 888 | }) |
Florin Coras | b795bd0 | 2017-12-14 11:30:48 -0800 | [diff] [blame] | 889 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 890 | def ipsec_spd_dump(self, spd_id, sa_id=0xffffffff): |
| 891 | return self.api(self.papi.ipsec_spd_dump, |
| 892 | {'spd_id': spd_id, |
| 893 | 'sa_id': sa_id}) |
| 894 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 895 | def ipsec_tunnel_if_add_del(self, local_ip, remote_ip, local_spi, |
| 896 | remote_spi, crypto_alg, local_crypto_key, |
| 897 | remote_crypto_key, integ_alg, local_integ_key, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 898 | remote_integ_key, is_add=1, esn=0, salt=0, |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 899 | anti_replay=1, renumber=0, |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 900 | udp_encap=0, show_instance=0xffffffff): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 901 | return self.api( |
| 902 | self.papi.ipsec_tunnel_if_add_del, |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 903 | { |
| 904 | 'local_ip': local_ip, |
| 905 | 'remote_ip': remote_ip, |
| 906 | 'local_spi': local_spi, |
| 907 | 'remote_spi': remote_spi, |
| 908 | 'crypto_alg': crypto_alg, |
| 909 | 'local_crypto_key_len': len(local_crypto_key), |
| 910 | 'local_crypto_key': local_crypto_key, |
| 911 | 'remote_crypto_key_len': len(remote_crypto_key), |
| 912 | 'remote_crypto_key': remote_crypto_key, |
| 913 | 'integ_alg': integ_alg, |
| 914 | 'local_integ_key_len': len(local_integ_key), |
| 915 | 'local_integ_key': local_integ_key, |
| 916 | 'remote_integ_key_len': len(remote_integ_key), |
| 917 | 'remote_integ_key': remote_integ_key, |
| 918 | 'is_add': is_add, |
| 919 | 'esn': esn, |
| 920 | 'anti_replay': anti_replay, |
| 921 | 'renumber': renumber, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 922 | 'show_instance': show_instance, |
Neale Ranns | a6bee0a | 2019-06-14 01:13:25 -0700 | [diff] [blame] | 923 | 'udp_encap': udp_encap, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 924 | 'salt': salt |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 925 | }) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 926 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 927 | def ipsec_select_backend(self, protocol, index): |
| 928 | return self.api(self.papi.ipsec_select_backend, |
| 929 | {'protocol': protocol, 'index': index}) |
| 930 | |
| 931 | def ipsec_backend_dump(self): |
| 932 | return self.api(self.papi.ipsec_backend_dump, {}) |
| 933 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 934 | def punt_socket_register(self, reg, pathname, |
| 935 | header_version=1): |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 936 | """ Register punt socket """ |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 937 | return self.api(self.papi.punt_socket_register, |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 938 | {'header_version': header_version, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 939 | 'punt': reg, |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 940 | 'pathname': pathname}) |
| 941 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 942 | def punt_socket_deregister(self, reg): |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 943 | """ Unregister punt socket """ |
| 944 | return self.api(self.papi.punt_socket_deregister, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 945 | {'punt': reg}) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 946 | |
Neale Ranns | 947ea62 | 2018-06-07 23:48:20 -0700 | [diff] [blame] | 947 | def igmp_enable_disable(self, sw_if_index, enable, host): |
| 948 | """ Enable/disable IGMP on a given interface """ |
| 949 | return self.api(self.papi.igmp_enable_disable, |
| 950 | {'enable': enable, |
| 951 | 'mode': host, |
| 952 | 'sw_if_index': sw_if_index}) |
| 953 | |
Jakub Grajciar | 97748ca | 2018-10-04 11:05:35 +0200 | [diff] [blame] | 954 | def igmp_proxy_device_add_del(self, vrf_id, sw_if_index, add): |
| 955 | """ Add/del IGMP proxy device """ |
| 956 | return self.api(self.papi.igmp_proxy_device_add_del, |
| 957 | {'vrf_id': vrf_id, 'sw_if_index': sw_if_index, |
| 958 | 'add': add}) |
| 959 | |
| 960 | def igmp_proxy_device_add_del_interface(self, vrf_id, sw_if_index, add): |
| 961 | """ Add/del interface to/from IGMP proxy device """ |
| 962 | return self.api(self.papi.igmp_proxy_device_add_del_interface, |
| 963 | {'vrf_id': vrf_id, 'sw_if_index': sw_if_index, |
| 964 | 'add': add}) |
| 965 | |
Neale Ranns | 947ea62 | 2018-06-07 23:48:20 -0700 | [diff] [blame] | 966 | def igmp_listen(self, filter, sw_if_index, saddrs, gaddr): |
Jakub Grajciar | 7b867a8 | 2017-12-08 16:28:42 +0100 | [diff] [blame] | 967 | """ Listen for new (S,G) on specified interface |
| 968 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 969 | :param enable: add/delas |
Jakub Grajciar | 7b867a8 | 2017-12-08 16:28:42 +0100 | [diff] [blame] | 970 | :param sw_if_index: interface sw index |
| 971 | :param saddr: source ip4 addr |
| 972 | :param gaddr: group ip4 addr |
| 973 | """ |
| 974 | return self.api(self.papi.igmp_listen, |
Neale Ranns | 947ea62 | 2018-06-07 23:48:20 -0700 | [diff] [blame] | 975 | { |
| 976 | 'group': |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 977 | { |
| 978 | 'filter': filter, |
| 979 | 'sw_if_index': sw_if_index, |
| 980 | 'n_srcs': len(saddrs), |
| 981 | 'saddrs': saddrs, |
| 982 | 'gaddr': gaddr |
| 983 | } |
Neale Ranns | 947ea62 | 2018-06-07 23:48:20 -0700 | [diff] [blame] | 984 | }) |
Jakub Grajciar | 7b867a8 | 2017-12-08 16:28:42 +0100 | [diff] [blame] | 985 | |
Jakub Grajciar | 7b867a8 | 2017-12-08 16:28:42 +0100 | [diff] [blame] | 986 | def igmp_clear_interface(self, sw_if_index): |
| 987 | """ Remove all (S,G)s from specified interface |
| 988 | doesn't send IGMP report! |
| 989 | """ |
| 990 | return self.api( |
| 991 | self.papi.igmp_clear_interface, { |
| 992 | 'sw_if_index': sw_if_index}) |
| 993 | |
| 994 | def want_igmp_events(self, enable=1): |
| 995 | return self.api(self.papi.want_igmp_events, {'enable': enable, |
| 996 | 'pid': os.getpid()}) |