+ @staticmethod
+ def vpp_interface_set_ip_address(node, interface, address,
+ prefix_length=None):
+ """Set IP address to VPP interface.
+
+ :param node: VPP node.
+ :param interface: Interface name.
+ :param address: IP address.
+ :param prefix_length: Prefix length.
+ :type node: dict
+ :type interface: str
+ :type address: str
+ :type prefix_length: int
+ """
+ try:
+ ip_addr = IPv6Address(unicode(address))
+ af_inet = AF_INET6
+ is_ipv6 = 1
+ except (AddressValueError, NetmaskValueError):
+ ip_addr = IPv4Address(unicode(address))
+ af_inet = AF_INET
+ is_ipv6 = 0
+
+ cmd = 'sw_interface_add_del_address'
+ args = dict(
+ sw_if_index=InterfaceUtil.get_interface_index(node, interface),
+ is_add=1,
+ is_ipv6=is_ipv6,
+ del_all=0,
+ address_length=int(prefix_length) if prefix_length else 128
+ if is_ipv6 else 32,
+ address=inet_pton(af_inet, str(ip_addr)))
+ err_msg = 'Failed to add IP address on interface {ifc}'.format(
+ ifc=interface)
+ with PapiExecutor(node) as papi_exec:
+ papi_exec.add(cmd, **args).get_replies(err_msg). \
+ verify_reply(err_msg=err_msg)
+
+ @staticmethod
+ def vpp_add_ip_neighbor(node, iface_key, ip_addr, mac_address):
+ """Add IP neighbor on DUT node.
+
+ :param node: VPP node.
+ :param iface_key: Interface key.
+ :param ip_addr: IP address of the interface.
+ :param mac_address: MAC address of the interface.
+ :type node: dict
+ :type iface_key: str
+ :type ip_addr: str
+ :type mac_address: str
+ """
+ try:
+ dst_ip = IPv6Address(unicode(ip_addr))
+ except (AddressValueError, NetmaskValueError):
+ dst_ip = IPv4Address(unicode(ip_addr))
+
+ neighbor = dict(
+ sw_if_index=Topology.get_interface_sw_index(
+ node, iface_key),
+ flags=0,
+ mac_address=str(mac_address),
+ ip_address=str(dst_ip))
+ cmd = 'ip_neighbor_add_del'
+ args = dict(
+ is_add=1,
+ neighbor=neighbor)
+ err_msg = 'Failed to add IP neighbor on interface {ifc}'.format(
+ ifc=iface_key)
+ with PapiExecutor(node) as papi_exec:
+ papi_exec.add(cmd, **args).get_replies(err_msg). \
+ verify_reply(err_msg=err_msg)
+
+ @staticmethod
+ def vpp_route_add(node, network, prefix_len, **kwargs):
+ """Add route to the VPP node.
+
+ :param node: VPP node.
+ :param network: Route destination network address.
+ :param prefix_len: Route destination network prefix length.
+ :param kwargs: Optional key-value arguments:
+
+ gateway: Route gateway address. (str)
+ interface: Route interface. (str)
+ vrf: VRF table ID. (int)
+ count: number of IP addresses to add starting from network IP (int)
+ local: The route is local with same prefix (increment is 1).
+ If None, then is not used. (bool)
+ lookup_vrf: VRF table ID for lookup. (int)
+ multipath: Enable multipath routing. (bool)
+ weight: Weight value for unequal cost multipath routing. (int)
+
+ :type node: dict
+ :type network: str
+ :type prefix_len: int
+ :type kwargs: dict
+ """
+ interface = kwargs.get('interface', None)
+ gateway = kwargs.get('gateway', None)
+
+ try:
+ net_addr = IPv6Address(unicode(network))
+ af_inet = AF_INET6
+ is_ipv6 = 1
+ except (AddressValueError, NetmaskValueError):
+ net_addr = IPv4Address(unicode(network))
+ af_inet = AF_INET
+ is_ipv6 = 0
+
+ if gateway:
+ try:
+ gt_addr = IPv6Address(unicode(gateway))
+ af_inet_gt = AF_INET6
+ except (AddressValueError, NetmaskValueError):
+ gt_addr = IPv4Address(unicode(gateway))
+ af_inet_gt = AF_INET
+
+ cmd = 'ip_add_del_route'
+ args = dict(
+ next_hop_sw_if_index=InterfaceUtil.get_interface_index(
+ node, interface) if interface else Constants.BITWISE_NON_ZERO,
+ table_id=int(kwargs.get('vrf', 0)),
+ is_add=1,
+ is_ipv6=is_ipv6,
+ is_local=int(kwargs.get('local', False)),
+ is_multipath=int(kwargs.get('multipath', False)),
+ next_hop_weight=int(kwargs.get('weight', 1)),
+ next_hop_proto=1 if is_ipv6 else 0,
+ dst_address_length=int(prefix_len),
+ next_hop_address=inet_pton(af_inet_gt, str(gt_addr)) if gateway
+ else 0,
+ next_hop_table_id=int(kwargs.get('lookup_vrf', 0)))
+ err_msg = 'Failed to add route(s) on host {host}'.format(
+ host=node['host'])
+ with PapiExecutor(node) as papi_exec:
+ for i in xrange(kwargs.get('count', 1)):
+ papi_exec.add(cmd, dst_address=inet_pton(
+ af_inet, str(net_addr+i)), **args)
+ papi_exec.get_replies(err_msg).verify_replies(err_msg=err_msg)
+
+ @staticmethod
+ def vpp_nodes_set_ipv4_addresses(nodes, nodes_addr):
+ """Set IPv4 addresses on all VPP nodes in topology.
+
+ :param nodes: Nodes of the test topology.
+ :param nodes_addr: Available nodes IPv4 addresses.
+ :type nodes: dict
+ :type nodes_addr: dict
+ :returns: Affected interfaces as list of (node, interface) tuples.
+ :rtype: list
+ """
+ interfaces = []
+ for net in nodes_addr.values():
+ for port in net['ports'].values():
+ host = port.get('node')
+ if host is None:
+ continue
+ topo = Topology()
+ node = topo.get_node_by_hostname(nodes, host)
+ if node is None:
+ continue
+ if node['type'] != NodeType.DUT:
+ continue
+ iface_key = topo.get_interface_by_name(node, port['if'])
+ IPUtil.vpp_interface_set_ip_address(
+ node, iface_key, port['addr'], net['prefix'])
+ interfaces.append((node, port['if']))
+
+ return interfaces
+
+ @staticmethod
+ def flush_ip_addresses(node, interface):
+ """Flush all IPv4addresses from specified interface.
+
+ :param node: VPP node.
+ :param interface: Interface name.
+ :type node: dict
+ :type interface: str
+ """
+ cmd = 'sw_interface_add_del_address'
+ args = dict(
+ sw_if_index=InterfaceUtil.get_interface_index(node, interface),
+ del_all=1)
+ err_msg = 'Failed to flush IP address on interface {ifc}'.format(
+ ifc=interface)
+ with PapiExecutor(node) as papi_exec:
+ papi_exec.add(cmd, **args).get_replies(err_msg). \
+ verify_reply(err_msg=err_msg)
+
+ @staticmethod
+ def add_fib_table(node, table_id, ipv6=False):
+ """Create new FIB table according to ID.