import os
import time
-from collections import deque
-
+import queue
from six import moves, iteritems
-from vpp_papi import VPPApiClient, mac_pton
+from config import config
+from vpp_papi import VPPApiClient
from hook import Hook
-from vpp_ip_route import MPLS_IETF_MAX_LABEL, MPLS_LABEL_INVALID
-
+from vpp_papi_exceptions import CliFailedCommandError, CliSyntaxError,\
+ UnexpectedApiReturnValueError
#
# Dictionary keyed on message name to override default values for
'bridge_domain_add_del': {'flood': 1, 'uu_flood': 1, 'forward': 1,
'learn': 1, 'is_add': 1, },
'bvi_delete': {},
- 'gbp_subnet_add_del': {'sw_if_index': 4294967295, 'epg_id': 65535, },
'geneve_add_del_tunnel': {'mcast_sw_if_index': 4294967295, 'is_add': 1,
'decap_next_index': 4294967295, },
'input_acl_set_interface': {'ip4_table_index': 4294967295,
'classify_table_index': 4294967295, 'is_add': 1, },
'ip_mroute_add_del': {'is_add': 1, },
'ip_neighbor_add_del': {'is_add': 1, },
- 'ip_punt_police': {'is_add': 1, },
- 'ip_punt_redirect': {'is_add': 1, },
- 'ip_route_add_del': {'is_add': 1, },
'ipsec_interface_add_del_spd': {'is_add': 1, },
'ipsec_spd_add_del': {'is_add': 1, },
'ipsec_spd_dump': {'sa_id': 4294967295, },
'l2_table_index': 4294967295, },
'pppoe_add_del_session': {'is_add': 1, },
'policer_add_del': {'is_add': 1, 'conform_action': {'type': 1}, },
- 'proxy_arp_add_del': {'is_add': 1, },
- 'proxy_arp_intfc_enable_disable': {'is_enable': 1, },
- 'set_ip_flow_hash': {'src': 1, 'dst': 1, 'sport': 1, 'dport': 1,
- 'proto': 1, },
'set_ipfix_exporter': {'collector_port': 4739, },
'sr_policy_add': {'weight': 1, 'is_encap': 1, },
- 'svs_enable_disable': {'is_enable': 1, },
- 'svs_route_add_del': {'is_add': 1, },
- 'svs_table_add_del': {'is_add': 1, },
'sw_interface_add_del_address': {'is_add': 1, },
'sw_interface_ip6nd_ra_prefix': {'val_lifetime': 4294967295,
'pref_lifetime': 4294967295, },
'vxlan_add_del_tunnel': {'mcast_sw_if_index': 4294967295, 'is_add': 1,
'decap_next_index': 4294967295,
'instance': 4294967295, },
- 'vxlan_gpe_add_del_tunnel': {'mcast_sw_if_index': 4294967295, 'is_add': 1,
- 'protocol': 3, },
'want_bfd_events': {'enable_disable': 1, },
'want_igmp_events': {'enable': 1, },
'want_interface_events': {'enable_disable': 1, },
'want_l2_macs_events': {'enable_disable': 1, 'pid': os.getpid(), },
+ 'want_l2_macs_events2': {'enable_disable': 1, 'pid': os.getpid(), },
}
return ", ".join(f"{k}={v}" for k, v in d.items())
-class CliFailedCommandError(Exception):
- """ cli command failed."""
-
-
-class CliSyntaxError(Exception):
- """ cli command had a syntax error."""
-
-
-class UnexpectedApiReturnValueError(Exception):
- """ exception raised when the API return value is unexpected """
- pass
-
-
class VppPapiProvider(object):
"""VPP-api provider using vpp-papi
_zero, _negative = range(2)
- def __init__(self, name, shm_prefix, test_class, read_timeout):
+ def __init__(self, name, test_class, read_timeout):
self.hook = Hook(test_class)
self.name = name
- self.shm_prefix = shm_prefix
self.test_class = test_class
self._expect_api_retval = self._zero
self._expect_stack = []
# install_dir is a class attribute. We need to set it before
# calling the constructor.
- VPPApiClient.apidir = os.getenv('VPP_INSTALL_PATH')
-
- use_socket = False
- try:
- if os.environ['SOCKET'] == '1':
- use_socket = True
- except KeyError:
- pass
+ VPPApiClient.apidir = config.vpp_install_dir
self.vpp = VPPApiClient(logger=test_class.logger,
read_timeout=read_timeout,
- use_socket=use_socket,
- server_address=test_class.api_sock)
- self._events = deque()
+ use_socket=True,
+ server_address=test_class.get_api_sock_path())
+ self._events = queue.Queue()
def __enter__(self):
return self
def assert_negative_api_retval(self):
- """ Expect API failure - used with with, e.g.:
- with self.vapi.assert_negative_api_retval():
- self.vapi.<api call expected to fail>
+ """ Expect API failure - used with with, e.g.::
+
+ with self.vapi.assert_negative_api_retval():
+ self.vapi.<api call expected to fail>
+
+ ..
"""
self._expect_stack.append(self._expect_api_retval)
self._expect_api_retval = self._negative
return self
def assert_zero_api_retval(self):
- """ Expect API success - used with with, e.g.:
- with self.vapi.assert_negative_api_retval():
- self.vapi.<api call expected to succeed>
+ """ Expect API success - used with with, e.g.::
+
+ with self.vapi.assert_negative_api_retval():
+ self.vapi.<api call expected to succeed>
- note: this is useful only inside another with block
- as success is the default expected value
+ :note: this is useful only inside another with block
+ as success is the default expected value
"""
self._expect_stack.append(self._expect_api_retval)
self._expect_api_retval = self._zero
def collect_events(self):
""" Collect all events from the internal queue and clear the queue. """
- e = self._events
- self._events = deque()
- return e
+ result = []
+ while True:
+ try:
+ e = self._events.get(block=False)
+ result.append(e)
+ except queue.Empty:
+ return result
+ return result
def wait_for_event(self, timeout, name=None):
""" Wait for and return next event. """
else:
self.test_class.logger.debug("Expecting event within %ss",
timeout)
- if self._events:
- self.test_class.logger.debug("Not waiting, event already queued")
- limit = time.time() + timeout
- while time.time() < limit:
- if self._events:
- e = self._events.popleft()
- if name and type(e).__name__ != name:
- raise Exception(
- "Unexpected event received: %s, expected: %s" %
- (type(e).__name__, name))
- self.test_class.logger.debug("Returning event %s:%s" %
- (name, e))
- return e
- self.test_class.sleep(0) # yield
- raise Exception("Event did not occur within timeout")
+ try:
+ e = self._events.get(timeout=timeout)
+ except queue.Empty:
+ raise Exception("Event did not occur within timeout")
+ msgname = type(e).__name__
+ if name and msgname != name:
+ raise Exception("Unexpected event received: %s, expected: %s"
+ % msgname)
+ self.test_class.logger.debug("Returning event %s:%s" % (name, e))
+ return e
def __call__(self, name, event):
""" Enqueue event in the internal event queue. """
- # FIXME use the name instead of relying on type(e).__name__ ?
- # FIXME #2 if this throws, it is eaten silently, Ole?
self.test_class.logger.debug("New event: %s: %s" % (name, event))
- self._events.append(event)
+ self._events.put(event)
def factory(self, name, apifn):
def f(*a, **ka):
def connect(self):
"""Connect the API to VPP"""
- self.vpp.connect(self.name, self.shm_prefix)
+ # This might be called before VPP is prepared to listen to the socket
+ retries = 0
+ while not os.path.exists(self.test_class.get_api_sock_path()):
+ time.sleep(0.5)
+ retries += 1
+ if retries > 120:
+ break
+ self.vpp.connect(self.name[:63])
self.papi = self.vpp.api
self.vpp.register_event_callback(self)
reply.retval,
moves.reprlib.repr(reply))
self.test_class.logger.info(msg)
- raise UnexpectedApiReturnValueError(msg)
+ raise UnexpectedApiReturnValueError(reply.retval, msg)
elif self._expect_api_retval == self._zero:
if hasattr(reply, 'retval') and reply.retval != expected_retval:
msg = "%s(%s) failed, expected %d return value instead " \
expected_retval, reply.retval,
repr(reply))
self.test_class.logger.info(msg)
- raise UnexpectedApiReturnValueError(msg)
+ raise UnexpectedApiReturnValueError(reply.retval, msg)
else:
raise Exception("Internal error, unexpected value for "
"self._expect_api_retval %s" %
'is_ip6': is_ip6
}})
+ def ip_route_v2_dump(self, table_id, is_ip6=False, src=0):
+ return self.api(self.papi.ip_route_v2_dump,
+ {
+ 'src': src,
+ 'table': {
+ 'table_id': table_id,
+ 'is_ip6': is_ip6
+ }
+ })
+
def ip_neighbor_add_del(self,
sw_if_index,
mac_address,
}
)
- def proxy_arp_add_del(self,
- low,
- hi,
- table_id=0,
- is_add=1):
- """ Config Proxy Arp Range.
-
- :param low_address: Start address in the rnage to Proxy for
- :param hi_address: End address in the rnage to Proxy for
- :param vrf_id: The VRF/table in which to proxy
- """
-
- return self.api(
- self.papi.proxy_arp_add_del,
- {'proxy':
- {
- 'table_id': table_id,
- 'low': low,
- 'hi': hi,
- },
- 'is_add': is_add})
-
- def proxy_arp_intfc_enable_disable(self,
- sw_if_index,
- is_enable=1):
- """ Enable/Disable an interface for proxy ARP requests
-
- :param sw_if_index: Interface
- :param enable_disable: Enable/Disable
- """
-
- return self.api(
- self.papi.proxy_arp_intfc_enable_disable,
- {'sw_if_index': sw_if_index,
- 'enable': is_enable
- }
- )
-
def udp_encap_add(self,
src_ip,
dst_ip,
'is_ip6': is_ip6
}})
- def vxlan_gpe_add_del_tunnel(
- self,
- src_addr,
- dst_addr,
- mcast_sw_if_index=0xFFFFFFFF,
- is_add=1,
- is_ipv6=0,
- encap_vrf_id=0,
- decap_vrf_id=0,
- protocol=3,
- vni=0):
- """
-
- :param local:
- :param remote:
- :param is_add: (Default value = 1)
- :param is_ipv6: (Default value = 0)
- :param encap_vrf_id: (Default value = 0)
- :param decap_vrf_id: (Default value = 0)
- :param mcast_sw_if_index: (Default value = 0xFFFFFFFF)
- :param protocol: (Default value = 3)
- :param vni: (Default value = 0)
-
- """
- return self.api(self.papi.vxlan_gpe_add_del_tunnel,
- {'is_add': is_add,
- 'is_ipv6': is_ipv6,
- 'local': src_addr,
- 'remote': dst_addr,
- 'mcast_sw_if_index': mcast_sw_if_index,
- 'encap_vrf_id': encap_vrf_id,
- 'decap_vrf_id': decap_vrf_id,
- 'protocol': protocol,
- 'vni': vni})
-
- def vxlan_gbp_tunnel_dump(self, sw_if_index=0xffffffff):
- return self.api(self.papi.vxlan_gbp_tunnel_dump,
- {'sw_if_index': sw_if_index})
-
def pppoe_add_del_session(
self,
client_ip,
return self.api(self.papi.sr_mpls_policy_del,
{'bsid': bsid})
- def ip_punt_police(self,
- policer_index,
- is_ip6=0,
- is_add=1):
- return self.api(self.papi.ip_punt_police,
- {'policer_index': policer_index,
- 'is_add': is_add,
- 'is_ip6': is_ip6})
-
- def ip_punt_redirect(self,
- rx_sw_if_index,
- tx_sw_if_index,
- address,
- is_add=1):
- return self.api(self.papi.ip_punt_redirect,
- {'punt': {'rx_sw_if_index': rx_sw_if_index,
- 'tx_sw_if_index': tx_sw_if_index,
- 'nh': address},
- 'is_add': is_add})
-
- def ip_punt_redirect_dump(self, sw_if_index, is_ipv6=0):
- return self.api(self.papi.ip_punt_redirect_dump,
- {'sw_if_index': sw_if_index,
- 'is_ipv6': is_ipv6})
-
def bier_table_add_del(self,
bti,
mpls_label,
self.papi.bier_disp_entry_dump,
{'bde_tbl_id': bdti})
- def session_enable_disable(self, is_enabled):
- return self.api(
- self.papi.session_enable_disable,
- {'is_enable': is_enabled})
-
def ipsec_spd_add_del(self, spd_id, is_add=1):
""" SPD add/del - Wrapper to add or del ipsec SPD
Sample CLI : 'ipsec spd add 1'
:param spd_id - SPD ID to be created in the vpp . mandatory
:param is_add - create (1) or delete(0) SPD (Default 1 - add) .
- optional
+ optional
:returns: reply from the API
"""
return self.api(
{'spd_index': spd_index if spd_index else 0,
'spd_index_valid': 1 if spd_index else 0})
- def ipsec_sa_dump(self, sa_id=None):
- return self.api(self.papi.ipsec_sa_dump,
- {'sa_id': sa_id if sa_id else 0xffffffff})
-
def ipsec_spd_entry_add_del(self,
spd_id,
sa_id,
is_ip_any=0):
""" IPSEC policy SPD add/del -
Wrapper to configure ipsec SPD policy entries in VPP
+
:param spd_id: SPD ID for the policy
:param local_address_start: local-ip-range start address
- :param local_address_stop : local-ip-range stop address
+ :param local_address_stop: local-ip-range stop address
:param remote_address_start: remote-ip-range start address
- :param remote_address_stop : remote-ip-range stop address
+ :param remote_address_stop: remote-ip-range stop address
:param local_port_start: (Default value = 0)
:param local_port_stop: (Default value = 65535)
:param remote_port_start: (Default value = 0)
:param protocol: Any(0), AH(51) & ESP(50) protocol (Default value = 0)
:param sa_id: Security Association ID for mapping it to SPD
:param policy: bypass(0), discard(1), resolve(2) or protect(3) action
- (Default value = 0)
+ (Default value = 0)
:param priority: value for the spd action (Default value = 100)
:param is_outbound: flag for inbound(0) or outbound(1)
- (Default value = 1)
+ (Default value = 1)
:param is_add: (Default value = 1)
"""
return self.api(
def ipsec_backend_dump(self):
return self.api(self.papi.ipsec_backend_dump, {})
- def app_namespace_add_del(self,
- namespace_id,
- ip4_fib_id=0,
- ip6_fib_id=0,
- sw_if_index=0xFFFFFFFF,
- secret=0):
- return self.api(
- self.papi.app_namespace_add_del,
- {'secret': secret,
- 'sw_if_index': sw_if_index,
- 'ip4_fib_id': ip4_fib_id,
- 'ip6_fib_id': ip6_fib_id,
- 'namespace_id': namespace_id})
-
def punt_socket_register(self, reg, pathname,
header_version=1):
""" Register punt socket """
return self.api(self.papi.punt_socket_deregister,
{'punt': reg})
- def gbp_endpoint_add(self, sw_if_index, ips, mac, sclass, flags,
- tun_src, tun_dst):
- """ GBP endpoint Add """
- return self.api(self.papi.gbp_endpoint_add,
- {'endpoint': {
- 'sw_if_index': sw_if_index,
- 'ips': ips,
- 'n_ips': len(ips),
- 'mac': mac,
- 'sclass': sclass,
- 'flags': flags,
- 'tun': {
- 'src': tun_src,
- 'dst': tun_dst,
- }}})
-
- def gbp_endpoint_del(self, handle):
- """ GBP endpoint Del """
- return self.api(self.papi.gbp_endpoint_del,
- {'handle': handle})
-
- def gbp_endpoint_dump(self):
- """ GBP endpoint Dump """
- return self.api(self.papi.gbp_endpoint_dump, {})
-
- def gbp_endpoint_group_add(self, vnid, sclass, bd,
- rd, uplink_sw_if_index,
- retention):
- """ GBP endpoint group Add """
- return self.api(self.papi.gbp_endpoint_group_add,
- {'epg':
- {
- 'uplink_sw_if_index': uplink_sw_if_index,
- 'bd_id': bd,
- 'rd_id': rd,
- 'vnid': vnid,
- 'sclass': sclass,
- 'retention': retention
- }})
-
- def gbp_endpoint_group_del(self, sclass):
- """ GBP endpoint group Del """
- return self.api(self.papi.gbp_endpoint_group_del,
- {'sclass': sclass})
-
- def gbp_bridge_domain_add(self, bd_id, rd_id, flags,
- bvi_sw_if_index,
- uu_fwd_sw_if_index,
- bm_flood_sw_if_index):
- """ GBP bridge-domain Add """
- return self.api(self.papi.gbp_bridge_domain_add,
- {'bd':
- {
- 'flags': flags,
- 'bvi_sw_if_index': bvi_sw_if_index,
- 'uu_fwd_sw_if_index': uu_fwd_sw_if_index,
- 'bm_flood_sw_if_index': bm_flood_sw_if_index,
- 'bd_id': bd_id,
- 'rd_id': rd_id
- }})
-
- def gbp_bridge_domain_del(self, bd_id):
- """ GBP bridge-domain Del """
- return self.api(self.papi.gbp_bridge_domain_del,
- {'bd_id': bd_id})
-
- def gbp_route_domain_add(self, rd_id,
- scope,
- ip4_table_id,
- ip6_table_id,
- ip4_uu_sw_if_index,
- ip6_uu_sw_if_index):
- """ GBP route-domain Add """
- return self.api(self.papi.gbp_route_domain_add,
- {'rd':
- {
- 'scope': scope,
- 'ip4_table_id': ip4_table_id,
- 'ip6_table_id': ip6_table_id,
- 'ip4_uu_sw_if_index': ip4_uu_sw_if_index,
- 'ip6_uu_sw_if_index': ip6_uu_sw_if_index,
- 'rd_id': rd_id
- }})
-
- def gbp_route_domain_del(self, rd_id):
- """ GBP route-domain Del """
- return self.api(self.papi.gbp_route_domain_del,
- {'rd_id': rd_id})
-
- def gbp_recirc_add_del(self, is_add, sw_if_index, sclass, is_ext):
- """ GBP recirc Add/Del """
- return self.api(self.papi.gbp_recirc_add_del,
- {'is_add': is_add,
- 'recirc': {
- 'is_ext': is_ext,
- 'sw_if_index': sw_if_index,
- 'sclass': sclass}})
-
- def gbp_recirc_dump(self):
- """ GBP recirc Dump """
- return self.api(self.papi.gbp_recirc_dump, {})
-
- def gbp_ext_itf_add_del(self, is_add, sw_if_index, bd_id, rd_id, flags):
- """ GBP recirc Add/Del """
- return self.api(self.papi.gbp_ext_itf_add_del,
- {'is_add': is_add,
- 'ext_itf': {
- 'sw_if_index': sw_if_index,
- 'bd_id': bd_id,
- 'rd_id': rd_id,
- 'flags': flags}})
-
- def gbp_ext_itf_dump(self):
- """ GBP recirc Dump """
- return self.api(self.papi.gbp_ext_itf_dump, {})
-
- def gbp_subnet_add_del(self, is_add, rd_id,
- prefix, type,
- sw_if_index=0xffffffff,
- sclass=0xffff):
- """ GBP Subnet Add/Del """
- return self.api(self.papi.gbp_subnet_add_del,
- {'is_add': is_add,
- 'subnet': {
- 'type': type,
- 'sw_if_index': sw_if_index,
- 'sclass': sclass,
- 'prefix': prefix,
- 'rd_id': rd_id}})
-
- def gbp_subnet_dump(self):
- """ GBP Subnet Dump """
- return self.api(self.papi.gbp_subnet_dump, {})
-
- def gbp_contract_dump(self):
- """ GBP contract Dump """
- return self.api(self.papi.gbp_contract_dump, {})
-
- def gbp_vxlan_tunnel_add(self, vni, bd_rd_id, mode, src):
- """ GBP VXLAN tunnel add """
- return self.api(self.papi.gbp_vxlan_tunnel_add,
- {
- 'tunnel': {
- 'vni': vni,
- 'mode': mode,
- 'bd_rd_id': bd_rd_id,
- 'src': src
- }
- })
-
- def gbp_vxlan_tunnel_del(self, vni):
- """ GBP VXLAN tunnel del """
- return self.api(self.papi.gbp_vxlan_tunnel_del,
- {
- 'vni': vni,
- })
-
- def gbp_vxlan_tunnel_dump(self):
- """ GBP VXLAN tunnel add/del """
- return self.api(self.papi.gbp_vxlan_tunnel_dump, {})
-
def igmp_enable_disable(self, sw_if_index, enable, host):
""" Enable/disable IGMP on a given interface """
return self.api(self.papi.igmp_enable_disable,
def igmp_listen(self, filter, sw_if_index, saddrs, gaddr):
""" Listen for new (S,G) on specified interface
- :param enable: add/del
+ :param enable: add/delas
:param sw_if_index: interface sw index
:param saddr: source ip4 addr
:param gaddr: group ip4 addr
def want_igmp_events(self, enable=1):
return self.api(self.papi.want_igmp_events, {'enable': enable,
'pid': os.getpid()})
-
- def bond_create(
- self,
- mode,
- lb,
- numa_only,
- use_custom_mac,
- mac_address='',
- interface_id=0xFFFFFFFF):
- """
- :param mode: mode
- :param lb: load balance
- :param numa_only: tx on local numa node for lacp mode
- :param use_custom_mac: use custom mac
- :param mac_address: mac address
- :param interface_id: custom interface ID
- """
- return self.api(
- self.papi.bond_create,
- {'mode': mode,
- 'lb': lb,
- 'numa_only': numa_only,
- 'use_custom_mac': use_custom_mac,
- 'mac_address': mac_address,
- 'id': interface_id
- })
-
- def pipe_delete(self, parent_sw_if_index):
- return self.api(self.papi.pipe_delete,
- {'parent_sw_if_index': parent_sw_if_index})
-
- def svs_table_add_del(self, af, table_id, is_add=1):
- return self.api(self.papi.svs_table_add_del,
- {
- 'table_id': table_id,
- 'is_add': is_add,
- 'af': af,
- })
-
- def svs_route_add_del(self, table_id, prefix, src_table_id, is_add=1):
- return self.api(self.papi.svs_route_add_del,
- {
- 'table_id': table_id,
- 'source_table_id': src_table_id,
- 'prefix': prefix,
- 'is_add': is_add,
- })
-
- def svs_enable_disable(self, af, table_id, sw_if_index, is_enable=1):
- return self.api(self.papi.svs_enable_disable,
- {
- 'af': af,
- 'table_id': table_id,
- 'sw_if_index': sw_if_index,
- 'is_enable': is_enable,
- })
-
- def feature_gso_enable_disable(self, sw_if_index, enable_disable=1):
- return self.api(self.papi.feature_gso_enable_disable,
- {
- 'sw_if_index': sw_if_index,
- 'enable_disable': enable_disable,
- })