#!/usr/bin/env python3 import socket from socket import inet_pton, inet_ntop import unittest from parameterized import parameterized import scapy.compat import scapy.layers.inet6 as inet6 from scapy.contrib.mpls import MPLS from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_RS, \ ICMPv6ND_RA, ICMPv6NDOptMTU, ICMPv6NDOptSrcLLAddr, ICMPv6NDOptPrefixInfo, \ ICMPv6ND_NA, ICMPv6NDOptDstLLAddr, ICMPv6DestUnreach, icmp6types, \ ICMPv6TimeExceeded, ICMPv6EchoRequest, ICMPv6EchoReply, \ IPv6ExtHdrHopByHop, ICMPv6MLReport2, ICMPv6MLDMultAddrRec from scapy.layers.l2 import Ether, Dot1Q from scapy.packet import Raw from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ptop, in6_islladdr, \ in6_mactoifaceid from six import moves from framework import VppTestCase, VppTestRunner from util import ppp, ip6_normalize, mk_ll_addr from vpp_ip import DpoProto from vpp_ip_route import VppIpRoute, VppRoutePath, find_route, VppIpMRoute, \ VppMRoutePath, MRouteItfFlags, MRouteEntryFlags, VppMplsIpBind, \ VppMplsRoute, VppMplsTable, VppIpTable, FibPathType, FibPathProto, \ VppIpInterfaceAddress, find_route_in_dump, find_mroute_in_dump, \ VppIp6LinkLocalAddress from vpp_neighbor import find_nbr, VppNeighbor from vpp_pg_interface import is_ipv6_misc from vpp_sub_interface import VppSubInterface, VppDot1QSubint from vpp_policer import VppPolicer from ipaddress import IPv6Network, IPv6Address AF_INET6 = socket.AF_INET6 try: text_type = unicode except NameError: text_type = str NUM_PKTS = 67 class TestIPv6ND(VppTestCase): def validate_ra(self, intf, rx, dst_ip=None): if not dst_ip: dst_ip = intf.remote_ip6 # unicasted packets must come to the unicast mac self.assertEqual(rx[Ether].dst, intf.remote_mac) # and from the router's MAC self.assertEqual(rx[Ether].src, intf.local_mac) # the rx'd RA should be addressed to the sender's source self.assertTrue(rx.haslayer(ICMPv6ND_RA)) self.assertEqual(in6_ptop(rx[IPv6].dst), in6_ptop(dst_ip)) # and come from the router's link local self.assertTrue(in6_islladdr(rx[IPv6].src)) self.assertEqual(in6_ptop(rx[IPv6].src), in6_ptop(mk_ll_addr(intf.local_mac))) def validate_na(self, intf, rx, dst_ip=None, tgt_ip=None): if not dst_ip: dst_ip = intf.remote_ip6 if not tgt_ip: dst_ip = intf.local_ip6 # unicasted packets must come to the unicast mac self.assertEqual(rx[Ether].dst, intf.remote_mac) # and from the router's MAC self.assertEqual(rx[Ether].src, intf.local_mac) # the rx'd NA should be addressed to the sender's source self.assertTrue(rx.haslayer(ICMPv6ND_NA)) self.assertEqual(in6_ptop(rx[IPv6].dst), in6_ptop(dst_ip)) # and come from the target address self.assertEqual( in6_ptop(rx[IPv6].src), in6_ptop(tgt_ip)) # Dest link-layer options should have the router's MAC dll = rx[ICMPv6NDOptDstLLAddr] self.assertEqual(dll.lladdr, intf.local_mac) def validate_ns(self, intf, rx, tgt_ip): nsma = in6_getnsma(inet_pton(AF_INET6, tgt_ip)) dst_ip = inet_ntop(AF_INET6, nsma) # NS is broadcast self.assertEqual(rx[Ether].dst, in6_getnsmac(nsma)) # and from the router's MAC self.assertEqual(rx[Ether].src, intf.local_mac) # the rx'd NS should be addressed to an mcast address # derived from the target address self.assertEqual( in6_ptop(rx[IPv6].dst), in6_ptop(dst_ip)) # expect the tgt IP in the NS header ns = rx[ICMPv6ND_NS] self.assertEqual(in6_ptop(ns.tgt), in6_ptop(tgt_ip)) # packet is from the router's local address self.assertEqual( in6_ptop(rx[IPv6].src), intf.local_ip6) # Src link-layer options should have the router's MAC sll = rx[ICMPv6NDOptSrcLLAddr] self.assertEqual(sll.lladdr, intf.local_mac) def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None, filter_out_fn=is_ipv6_misc): intf.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = intf.get_capture(1, filter_out_fn=filter_out_fn) self.assertEqual(len(rx), 1) rx = rx[0] self.validate_ra(intf, rx, dst_ip) def send_and_expect_na(self, intf, pkts, remark, dst_ip=None, tgt_ip=None, filter_out_fn=is_ipv6_misc): intf.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = intf.get_capture(1, filter_out_fn=filter_out_fn) self.assertEqual(len(rx), 1) rx = rx[0] self.validate_na(intf, rx, dst_ip, tgt_ip) def send_and_expect_ns(self, tx_intf, rx_intf, pkts, tgt_ip, filter_out_fn=is_ipv6_misc): self.vapi.cli("clear trace") tx_intf.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = rx_intf.get_capture(1, filter_out_fn=filter_out_fn) self.assertEqual(len(rx), 1) rx = rx[0] self.validate_ns(rx_intf, rx, tgt_ip) def verify_ip(self, rx, smac, dmac, sip, dip): ether = rx[Ether] self.assertEqual(ether.dst, dmac) self.assertEqual(ether.src, smac) ip = rx[IPv6] self.assertEqual(ip.src, sip) self.assertEqual(ip.dst, dip) class TestIPv6(TestIPv6ND): """ IPv6 Test Case """ @classmethod def setUpClass(cls): super(TestIPv6, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestIPv6, cls).tearDownClass() def setUp(self): """ Perform test setup before test case. **Config:** - create 3 pg interfaces - untagged pg0 interface - Dot1Q subinterface on pg1 - Dot1AD subinterface on pg2 - setup interfaces: - put it into UP state - set IPv6 addresses - resolve neighbor address using NDP - configure 200 fib entries :ivar list interfaces: pg interfaces and subinterfaces. :ivar dict flows: IPv4 packet flows in test. *TODO:* Create AD sub interface """ super(TestIPv6, self).setUp() # create 3 pg interfaces self.create_pg_interfaces(range(3)) # create 2 subinterfaces for p1 and pg2 self.sub_interfaces = [ VppDot1QSubint(self, self.pg1, 100), VppDot1QSubint(self, self.pg2, 200) # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400) ] # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc. self.flows = dict() self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if] self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if] self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if] # packet sizes self.pg_if_packet_sizes = [64, 1500, 9020] self.interfaces = list(self.pg_interfaces) self.interfaces.extend(self.sub_interfaces) # setup all interfaces for i in self.interfaces: i.admin_up() i.config_ip6() i.resolve_ndp() def tearDown(self): """Run standard test teardown and log ``show ip6 neighbors``.""" for i in self.interfaces: i.unconfig_ip6() i.admin_down() for i in self.sub_interfaces: i.remove_vpp_config() super(TestIPv6, self).tearDown() if not self.vpp_dead: self.logger.info(self.vapi.cli("show ip6 neighbors")) # info(self.vapi.cli("show ip6 fib")) # many entries def modify_packet(self, src_if, packet_size, pkt): """Add load, set destination IP and extend packet to required packet size for defined interface. :param VppInterface src_if: Interface to create packet for. :param int packet_size: Required packet size. :param Scapy pkt: Packet to be modified. """ dst_if_idx = int(packet_size / 10 % 2) dst_if = self.flows[src_if][dst_if_idx] info = self.create_packet_info(src_if, dst_if) payload = self.info_to_payload(info) p = pkt / Raw(payload) p[IPv6].dst = dst_if.remote_ip6 info.data = p.copy() if isinstance(src_if, VppSubInterface): p = src_if.add_dot1_layer(p) self.extend_packet(p, packet_size) return p def create_stream(self, src_if): """Create input packet stream for defined interface. :param VppInterface src_if: Interface to create packet stream for. """ hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0 pkt_tmpl = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) / IPv6(src=src_if.remote_ip6) / inet6.UDP(sport=1234, dport=1234)) pkts = [self.modify_packet(src_if, i, pkt_tmpl) for i in moves.range(self.pg_if_packet_sizes[0], self.pg_if_packet_sizes[1], 10)] pkts_b = [self.modify_packet(src_if, i, pkt_tmpl) for i in moves.range(self.pg_if_packet_sizes[1] + hdr_ext, self.pg_if_packet_sizes[2] + hdr_ext, 50)] pkts.extend(pkts_b) return pkts def verify_capture(self, dst_if, capture): """Verify captured input packet stream for defined interface. :param VppInterface dst_if: Interface to verify captured packet stream for. :param list capture: Captured packet stream. """ self.logger.info("Verifying capture on interface %s" % dst_if.name) last_info = dict() for i in self.interfaces: last_info[i.sw_if_index] = None is_sub_if = False dst_sw_if_index = dst_if.sw_if_index if hasattr(dst_if, 'parent'): is_sub_if = True for packet in capture: if is_sub_if: # Check VLAN tags and Ethernet header packet = dst_if.remove_dot1_layer(packet) self.assertTrue(Dot1Q not in packet) try: ip = packet[IPv6] udp = packet[inet6.UDP] payload_info = self.payload_to_info(packet[Raw]) packet_index = payload_info.index self.assertEqual(payload_info.dst, dst_sw_if_index) self.logger.debug( "Got packet on port %s: src=%u (id=%u)" % (dst_if.name, payload_info.src, packet_index)) next_info = self.get_next_packet_info_for_interface2( payload_info.src, dst_sw_if_index, last_info[payload_info.src]) last_info[payload_info.src] = next_info self.assertTrue(next_info is not None) self.assertEqual(packet_index, next_info.index) saved_packet = next_info.data # Check standard fields self.assertEqual( ip.src, saved_packet[IPv6].src) self.assertEqual( ip.dst, saved_packet[IPv6].dst) self.assertEqual( udp.sport, saved_packet[inet6.UDP].sport) self.assertEqual( udp.dport, saved_packet[inet6.UDP].dport) except: self.logger.error(ppp("Unexpected or invalid packet:", packet)) raise for i in self.interfaces: remaining_packet = self.get_next_packet_info_for_interface2( i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index]) self.assertTrue(remaining_packet is None, "Interface %s: Packet expected from interface %s " "didn't arrive" % (dst_if.name, i.name)) def test_next_header_anomaly(self): """ IPv6 next header anomaly test Test scenario: - ipv6 next header field = Fragment Header (44) - next header is ICMPv6 Echo Request - wait for reassembly """ pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6, nh=44) / ICMPv6EchoRequest()) self.pg0.add_stream(pkt) self.pg_start() # wait for reassembly self.sleep(10) def test_fib(self): """ IPv6 FIB test Test scenario: - Create IPv6 stream for pg0 interface - Create IPv6 tagged streams for pg1's and pg2's subinterface. - Send and verify received packets on each interface. """ pkts = self.create_stream(self.pg0) self.pg0.add_stream(pkts) for i in self.sub_interfaces: pkts = self.create_stream(i) i.parent.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() pkts = self.pg0.get_capture() self.verify_capture(self.pg0, pkts) for i in self.sub_interfaces: pkts = i.parent.get_capture() self.verify_capture(i, pkts) def test_ns(self): """ IPv6 Neighbour Solicitation Exceptions Test scenario: - Send an NS Sourced from an address not covered by the link sub-net - Send an NS to an mcast address the router has not joined - Send NS for a target address the router does not onn. """ # # An NS from a non link source address # nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6)) d = inet_ntop(AF_INET6, nsma) p = (Ether(dst=in6_getnsmac(nsma)) / IPv6(dst=d, src="2002::2") / ICMPv6ND_NS(tgt=self.pg0.local_ip6) / ICMPv6NDOptSrcLLAddr( lladdr=self.pg0.remote_mac)) pkts = [p] self.send_and_assert_no_replies( self.pg0, pkts, "No response to NS source by address not on sub-net") # # An NS for sent to a solicited mcast group the router is # not a member of FAILS # if 0: nsma = in6_getnsma(inet_pton(AF_INET6, "fd::ffff")) d = inet_ntop(AF_INET6, nsma) p = (Ether(dst=in6_getnsmac(nsma)) / IPv6(dst=d, src=self.pg0.remote_ip6) / ICMPv6ND_NS(tgt=self.pg0.local_ip6) / ICMPv6NDOptSrcLLAddr( lladdr=self.pg0.remote_mac)) pkts = [p] self.send_and_assert_no_replies( self.pg0, pkts, "No response to NS sent to unjoined mcast address") # # An NS whose target address is one the router does not own # nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6)) d = inet_ntop(AF_INET6, nsma) p = (Ether(dst=in6_getnsmac(nsma)) / IPv6(dst=d, src=self.pg0.remote_ip6) / ICMPv6ND_NS(tgt="fd::ffff") / ICMPv6NDOptSrcLLAddr( lladdr=self.pg0.remote_mac)) pkts = [p] self.send_and_assert_no_replies(self.pg0, pkts, "No response to NS for unknown target") # # A neighbor entry that has no associated FIB-entry # self.pg0.generate_remote_hosts(4) nd_entry = VppNeighbor(self, self.pg0.sw_if_index, self.pg0.remote_hosts[2].mac, self.pg0.remote_hosts[2].ip6, is_no_fib_entry=1) nd_entry.add_vpp_config() # # check we have the neighbor, but no route # self.assertTrue(find_nbr(self, self.pg0.sw_if_index, self.pg0._remote_hosts[2].ip6)) self.assertFalse(find_route(self, self.pg0._remote_hosts[2].ip6, 128)) # # send an NS from a link local address to the interface's global # address # p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) / IPv6( dst=d, src=self.pg0._remote_hosts[2].ip6_ll) / ICMPv6ND_NS(tgt=self.pg0.local_ip6) / ICMPv6NDOptSrcLLAddr( lladdr=self.pg0.remote_mac)) self.send_and_expect_na(self.pg0, p, "NS from link-local", dst_ip=self.pg0._remote_hosts[2].ip6_ll, tgt_ip=self.pg0.local_ip6) # # we should have learned an ND entry for the peer's link-local # but not inserted a route to it in the FIB # self.assertTrue(find_nbr(self, self.pg0.sw_if_index, self.pg0._remote_hosts[2].ip6_ll)) self.assertFalse(find_route(self, self.pg0._remote_hosts[2].ip6_ll, 128)) # # An NS to the route
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 *  @file
 *  @brief Functions for encapsulating VXLAN GPE tunnels
 *
*/
#include <vppinfra/error.h>
#include <vppinfra/hash.h>
#include <vnet/vnet.h>
#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/vxlan-gpe/vxlan_gpe.h>

/** Statistics (not really errors) */
#define foreach_vxlan_gpe_encap_error    \
_(ENCAPSULATED, "good packets encapsulated")

/**
 * @brief VXLAN GPE encap error strings
 */
static char * vxlan_gpe_encap_error_strings[] = {
#define _(sym,string) string,
  foreach_vxlan_gpe_encap_error
#undef _
};

/**
 * @brief Struct for VXLAN GPE errors/counters
 */
typedef enum {
#define _(sym,str) VXLAN_GPE_ENCAP_ERROR_##sym,
    foreach_vxlan_gpe_encap_error
#undef _
    VXLAN_GPE_ENCAP_N_ERROR,
} vxlan_gpe_encap_error_t;

/**
 * @brief Struct for tracing VXLAN GPE encapsulated packets
 */
typedef struct {
  u32 tunnel_index;
} vxlan_gpe_encap_trace_t;

/**
 * @brief Trace of packets encapsulated in VXLAN GPE
 *
 * @param *s
 * @param *args
 *
 * @return *s
 *
 */
u8 * format_vxlan_gpe_encap_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  vxlan_gpe_encap_trace_t * t
      = va_arg (*args, vxlan_gpe_encap_trace_t *);

  s = format (s, "VXLAN-GPE-ENCAP: tunnel %d", t->tunnel_index);
  return s;
}

/**
 * @brief Instantiates UDP + VXLAN-GPE header then set next node to IP4|6 lookup
 *
 * @param *ngm
 * @param *b0
 * @param *t0 contains rewrite header
 * @param *next0 relative index of next dispatch function (next node)
 * @param is_v4 Is this IPv4? (or IPv6)
 *
 */
always_inline void
vxlan_gpe_encap_one_inline (vxlan_gpe_main_t * ngm, vlib_buffer_t * b0,
                            vxlan_gpe_tunnel_t * t0, u32 * next0,
                            u8 is_v4)
{
  ASSERT(sizeof(ip4_vxlan_gpe_header_t) == 36);
  ASSERT(sizeof(ip6_vxlan_gpe_header_t) == 56);

  ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, t0->rewrite_size, is_v4);
  next0[0] = t0->encap_next_node;
}

/**
 * @brief Instantiates UDP + VXLAN-GPE header then set next node to IP4|6 lookup for two packets
 *
 * @param *ngm
 * @param *b0 Packet0
 * @param *b1 Packet1
 * @param *t0 contains rewrite header for Packet0
 * @param *t1 contains rewrite header for Packet1
 * @param *next0 relative index of next dispatch function (next node) for Packet0
 * @param *next1 relative index of next dispatch function (next node) for Packet1
 * @param is_v4 Is this IPv4? (or IPv6)
 *
 */
always_inline void
vxlan_gpe_encap_two_inline (vxlan_gpe_main_t * ngm, vlib_buffer_t * b0,
                            vlib_buffer_t * b1, vxlan_gpe_tunnel_t * t0,
                            vxlan_gpe_tunnel_t * t1, u32 * next0,
                            u32 * next1, u8 is_v4)
{
  ASSERT(sizeof(ip4_vxlan_gpe_header_t) == 36);
  ASSERT(sizeof(ip6_vxlan_gpe_header_t) == 56);

  ip_udp_encap_one (ngm->vlib_main, b0, t0->rewrite, t0->rewrite_size, is_v4);
  ip_udp_encap_one (ngm->vlib_main, b1, t1->rewrite, t1->rewrite_size, is_v4);
  next0[0] = next1[0] = t0->encap_next_node;
}

/**
 * @brief Common processing for IPv4 and IPv6 VXLAN GPE encap dispatch functions
 *
 * It is worth noting that other than trivial UDP forwarding (transit), VXLAN GPE
 * tunnels are "establish local". This means that we don't have a TX interface as yet
 * as we need to look up where the outer-header dest is. By setting the TX index in the
 * buffer metadata to the encap FIB, we can do a lookup to get the adjacency and real TX.
 *
 *      vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
 *
 * @node vxlan-gpe-input
 * @param *vm
 * @param *node
 * @param *from_frame
 *
 * @return from_frame->n_vectors
 *
 */
static uword
vxlan_gpe_encap (vlib_main_t * vm,
               vlib_node_runtime_t * node,
               vlib_frame_t * from_frame)
{
  u32 n_left_from, next_index, *from, *to_next;
  vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
  vnet_main_t * vnm = ngm->vnet_main;
  vnet_interface_main_t * im = &vnm->interface_main;
  u32 pkts_encapsulated = 0;
  u32 thread_index = vlib_get_thread_index ();
  u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;

  from = vlib_frame_vector_args (from_frame);
  n_left_from = from_frame->n_vectors;

  next_index = node->cached_next_index;
  stats_sw_if_index = node->runtime_data[0];
  stats_n_packets = stats_n_bytes = 0;

  while (n_left_from > 0)
  {
    u32 n_left_to_next;

    vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);

    while (n_left_from >= 4 && n_left_to_next >= 2)
    {
      u32 bi0, bi1;
      vlib_buffer_t * b0, *b1;
      u32 next0, next1;
      u32 sw_if_index0, sw_if_index1, len0, len1;
      vnet_hw_interface_t * hi0, *hi1;
      vxlan_gpe_tunnel_t * t0, *t1;
      u8 is_ip4_0, is_ip4_1;

      next0 = next1 = VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;

      /* Prefetch next iteration. */
      {
        vlib_buffer_t * p2, *p3;

        p2 = vlib_get_buffer (vm, from[2]);
        p3 = vlib_get_buffer (vm, from[3]);

        vlib_prefetch_buffer_header(p2, LOAD);
        vlib_prefetch_buffer_header(p3, LOAD);

        CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
        CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
      }

      bi0 = from[0];
      bi1 = from[1];
      to_next[0] = bi0;
      to_next[1] = bi1;
      from += 2;
      to_next += 2;
      n_left_to_next -= 2;
      n_left_from -= 2;

      b0 = vlib_get_buffer (vm, bi0);
      b1 = vlib_get_buffer (vm, bi1);

      /* 1-wide cache? */
      sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
      sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_TX];
      hi0 = vnet_get_sup_hw_interface (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
      hi1 = vnet_get_sup_hw_interface (vnm, vnet_buffer(b1)->sw_if_index[VLIB_TX]);

      t0 = pool_elt_at_index(ngm->tunnels, hi0->dev_instance);
      t1 = pool_elt_at_index(ngm->tunnels, hi1->dev_instance);

      is_ip4_0 = (t0->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
      is_ip4_1 = (t1->flags & VXLAN_GPE_TUNNEL_IS_IPV4);

      if (PREDICT_TRUE(is_ip4_0 == is_ip4_1))
      {
        vxlan_gpe_encap_two_inline (ngm, b0, b1, t0, t1, &next0, &next1,is_ip4_0);
      }
      else
      {
        vxlan_gpe_encap_one_inline (ngm, b0, t0, &next0, is_ip4_0);
        vxlan_gpe_encap_one_inline (ngm, b1, t1, &next1, is_ip4_1);
      }

      /* Reset to look up tunnel partner in the configured FIB */
      vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
      vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
      vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
      vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
      pkts_encapsulated += 2;

      len0 = vlib_buffer_length_in_chain (vm, b0);
      len1 = vlib_buffer_length_in_chain (vm, b0);
      stats_n_packets += 2;
      stats_n_bytes += len0 + len1;

      /* Batch stats increment on the same vxlan tunnel so counter is not
       incremented per packet. Note stats are still incremented for deleted
       and admin-down tunnel where packets are dropped. It is not worthwhile
       to check for this rare case and affect normal path performance. */
      if (PREDICT_FALSE((sw_if_index0 != stats_sw_if_index)
              || (sw_if_index1 != stats_sw_if_index)))
      {
        stats_n_packets -= 2;
        stats_n_bytes -= len0 + len1;
        if (sw_if_index0 == sw_if_index1)
        {
          if (stats_n_packets)
            vlib_increment_combined_counter (
                im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
                thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
          stats_sw_if_index = sw_if_index0;
          stats_n_packets = 2;
          stats_n_bytes = len0 + len1;
        }
        else
        {
          vlib_increment_combined_counter (
              im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
              thread_index, sw_if_index0, 1, len0);
          vlib_increment_combined_counter (
              im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
              thread_index, sw_if_index1, 1, len1);
        }
      }

      if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
      {
        vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof(*tr));
        tr->tunnel_index = t0 - ngm->tunnels;
      }

      if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
      {
        vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b1,
                                                      sizeof(*tr));
        tr->tunnel_index = t1 - ngm->tunnels;
      }

      vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
                                      n_left_to_next, bi0, bi1, next0, next1);
    }

    while (n_left_from > 0 && n_left_to_next > 0)
    {
      u32 bi0;
      vlib_buffer_t * b0;
      u32 next0 = VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP;
      u32 sw_if_index0, len0;
      vnet_hw_interface_t * hi0;
      vxlan_gpe_tunnel_t * t0;
      u8 is_ip4_0;

      bi0 = from[0];
      to_next[0] = bi0;
      from += 1;
      to_next += 1;
      n_left_from -= 1;
      n_left_to_next -= 1;

      b0 = vlib_get_buffer (vm, bi0);

      /* 1-wide cache? */
      sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
      hi0 = vnet_get_sup_hw_interface (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);

      t0 = pool_elt_at_index(ngm->tunnels, hi0->dev_instance);

      is_ip4_0 = (t0->flags & VXLAN_GPE_TUNNEL_IS_IPV4);

      vxlan_gpe_encap_one_inline (ngm, b0, t0, &next0, is_ip4_0);

      /* Reset to look up tunnel partner in the configured FIB */
      vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
      vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
      pkts_encapsulated++;

      len0 = vlib_buffer_length_in_chain (vm, b0);
      stats_n_packets += 1;
      stats_n_bytes += len0;

      /* Batch stats increment on the same vxlan tunnel so counter is not
       *  incremented per packet. Note stats are still incremented for deleted
       *  and admin-down tunnel where packets are dropped. It is not worthwhile
       *  to check for this rare case and affect normal path performance. */
      if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
      {
        stats_n_packets -= 1;
        stats_n_bytes -= len0;
        if (stats_n_packets)
          vlib_increment_combined_counter (
              im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
              thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
        stats_n_packets = 1;
        stats_n_bytes = len0;
        stats_sw_if_index = sw_if_index0;
      }
      if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
      {
        vxlan_gpe_encap_trace_t *tr = vlib_add_trace (vm, node, b0,
                                                      sizeof(*tr));
        tr->tunnel_index = t0 - ngm->tunnels;
      }
      vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
                                      n_left_to_next, bi0, next0);
    }

    vlib_put_next_frame (vm, node, next_index, n_left_to_next);
  }
  vlib_node_increment_counter (vm, node->node_index,
                               VXLAN_GPE_ENCAP_ERROR_ENCAPSULATED,
                               pkts_encapsulated);
  /* Increment any remaining batch stats */
  if (stats_n_packets)
  {
    vlib_increment_combined_counter (
        im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, thread_index,
        stats_sw_if_index, stats_n_packets, stats_n_bytes);
    node->runtime_data[0] = stats_sw_if_index;
  }

  return from_frame->n_vectors;
}

VLIB_REGISTER_NODE (vxlan_gpe_encap_node) = {
  .function = vxlan_gpe_encap,
  .name = "vxlan-gpe-encap",
  .vector_size = sizeof (u32),
  .format_trace = format_vxlan_gpe_encap_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_errors = ARRAY_LEN(vxlan_gpe_encap_error_strings),
  .error_strings = vxlan_gpe_encap_error_strings,

  .n_next_nodes = VXLAN_GPE_ENCAP_N_NEXT,

  .next_nodes = {
    [VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP] = "ip4-lookup",
    [VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP] = "ip6-lookup",
    [VXLAN_GPE_ENCAP_NEXT_DROP] = "error-drop",
  },
};
paths are used) # port_pkts = [] src_pkts = [] for ii in range(257): port_pkts.append((Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(dst="4000::1", src="4000:1::1") / inet6.UDP(sport=1234, dport=1234 + ii) / Raw(b'\xa5' * 100))) src_pkts.append((Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(dst="4000::1", src="4000:1::%d" % ii) / inet6.UDP(sport=1234, dport=1234) / Raw(b'\xa5' * 100))) route_3000_2 = VppIpRoute(self, "3000::2", 128, [VppRoutePath(self.pg3.remote_ip6, self.pg3.sw_if_index), VppRoutePath(self.pg4.remote_ip6, self.pg4.sw_if_index)]) route_3000_2.add_vpp_config() route_4000_1 = VppIpRoute(self, "4000::1", 128, [VppRoutePath("3000::1", 0xffffffff), VppRoutePath("3000::2", 0xffffffff)]) route_4000_1.add_vpp_config() # # inject the packet on pg0 - expect load-balancing across all 4 paths # self.vapi.cli("clear trace") self.send_and_expect_load_balancing(self.pg0, port_pkts, [self.pg1, self.pg2, self.pg3, self.pg4]) self.send_and_expect_load_balancing(self.pg0, src_pkts, [self.pg1, self.pg2, self.pg3, self.pg4]) # # Recursive prefixes # - testing that 2 stages of load-balancing no choices # port_pkts = [] for ii in range(257): port_pkts.append((Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(dst="6000::1", src="6000:1::1") / inet6.UDP(sport=1234, dport=1234 + ii) / Raw(b'\xa5' * 100))) route_5000_2 = VppIpRoute(self, "5000::2", 128, [VppRoutePath(self.pg3.remote_ip6, self.pg3.sw_if_index)]) route_5000_2.add_vpp_config() route_6000_1 = VppIpRoute(self, "6000::1", 128, [VppRoutePath("5000::2", 0xffffffff)]) route_6000_1.add_vpp_config() # # inject the packet on pg0 - expect load-balancing across all 4 paths # self.vapi.cli("clear trace") self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg3) class TestIP6Punt(VppTestCase): """ IPv6 Punt Police/Redirect """ @classmethod def setUpClass(cls): super(TestIP6Punt, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestIP6Punt, cls).tearDownClass() def setUp(self): super(TestIP6Punt, self).setUp() self.create_pg_interfaces(range(4)) for i in self.pg_interfaces: i.admin_up() i.config_ip6() i.resolve_ndp() def tearDown(self): super(TestIP6Punt, self).tearDown() for i in self.pg_interfaces: i.unconfig_ip6() i.admin_down() def test_ip_punt(self): """ IP6 punt police and redirect """ p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) / inet6.TCP(sport=1234, dport=1234) / Raw(b'\xa5' * 100)) pkts = p * 1025 # # Configure a punt redirect via pg1. # nh_addr = self.pg1.remote_ip6 self.vapi.ip_punt_redirect(self.pg0.sw_if_index, self.pg1.sw_if_index, nh_addr) self.send_and_expect(self.pg0, pkts, self.pg1) # # add a policer # policer = VppPolicer(self, "ip6-punt", 400, 0, 10, 0, rate_type=1) policer.add_vpp_config() self.vapi.ip_punt_police(policer.policer_index, is_ip6=1) self.vapi.cli("clear trace") self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() # # the number of packet received should be greater than 0, # but not equal to the number sent, since some were policed # rx = self.pg1._get_capture(1) self.assertGreater(len(rx), 0) self.assertLess(len(rx), len(pkts)) # # remove the policer. back to full rx # self.vapi.ip_punt_police(policer.policer_index, is_add=0, is_ip6=1) policer.remove_vpp_config() self.send_and_expect(self.pg0, pkts, self.pg1) # # remove the redirect. expect full drop. # self.vapi.ip_punt_redirect(self.pg0.sw_if_index, self.pg1.sw_if_index, nh_addr, is_add=0) self.send_and_assert_no_replies(self.pg0, pkts, "IP no punt config") # # Add a redirect that is not input port selective # self.vapi.ip_punt_redirect(0xffffffff, self.pg1.sw_if_index, nh_addr) self.send_and_expect(self.pg0, pkts, self.pg1) self.vapi.ip_punt_redirect(0xffffffff, self.pg1.sw_if_index, nh_addr, is_add=0) def test_ip_punt_dump(self): """ IP6 punt redirect dump""" # # Configure a punt redirects # nh_addr = self.pg3.remote_ip6 self.vapi.ip_punt_redirect(self.pg0.sw_if_index, self.pg3.sw_if_index, nh_addr) self.vapi.ip_punt_redirect(self.pg1.sw_if_index, self.pg3.sw_if_index, nh_addr) self.vapi.ip_punt_redirect(self.pg2.sw_if_index, self.pg3.sw_if_index, '0::0') # # Dump pg0 punt redirects # punts = self.vapi.ip_punt_redirect_dump(self.pg0.sw_if_index, is_ipv6=1) for p in punts: self.assertEqual(p.punt.rx_sw_if_index, self.pg0.sw_if_index) # # Dump punt redirects for all interfaces # punts = self.vapi.ip_punt_redirect_dump(0xffffffff, is_ipv6=1) self.assertEqual(len(punts), 3) for p in punts: self.assertEqual(p.punt.tx_sw_if_index, self.pg3.sw_if_index) self.assertNotEqual(punts[1].punt.nh, self.pg3.remote_ip6) self.assertEqual(str(punts[2].punt.nh), '::') class TestIPDeag(VppTestCase): """ IPv6 Deaggregate Routes """ @classmethod def setUpClass(cls): super(TestIPDeag, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestIPDeag, cls).tearDownClass() def setUp(self): super(TestIPDeag, self).setUp() self.create_pg_interfaces(range(3)) for i in self.pg_interfaces: i.admin_up() i.config_ip6() i.resolve_ndp() def tearDown(self): super(TestIPDeag, self).tearDown() for i in self.pg_interfaces: i.unconfig_ip6() i.admin_down() def test_ip_deag(self): """ IP Deag Routes """ # # Create a table to be used for: # 1 - another destination address lookup # 2 - a source address lookup # table_dst = VppIpTable(self, 1, is_ip6=1) table_src = VppIpTable(self, 2, is_ip6=1) table_dst.add_vpp_config() table_src.add_vpp_config() # # Add a route in the default table to point to a deag/ # second lookup in each of these tables # route_to_dst = VppIpRoute(self, "1::1", 128, [VppRoutePath("::", 0xffffffff, nh_table_id=1)]) route_to_src = VppIpRoute( self, "1::2", 128, [VppRoutePath("::", 0xffffffff, nh_table_id=2, type=FibPathType.FIB_PATH_TYPE_SOURCE_LOOKUP)]) route_to_dst.add_vpp_config() route_to_src.add_vpp_config() # # packets to these destination are dropped, since they'll # hit the respective default routes in the second table # p_dst = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(src="5::5", dst="1::1") / inet6.TCP(sport=1234, dport=1234) / Raw(b'\xa5' * 100)) p_src = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(src="2::2", dst="1::2") / inet6.TCP(sport=1234, dport=1234) / Raw(b'\xa5' * 100)) pkts_dst = p_dst * 257 pkts_src = p_src * 257 self.send_and_assert_no_replies(self.pg0, pkts_dst, "IP in dst table") self.send_and_assert_no_replies(self.pg0, pkts_src, "IP in src table") # # add a route in the dst table to forward via pg1 # route_in_dst = VppIpRoute(self, "1::1", 128, [VppRoutePath(self.pg1.remote_ip6, self.pg1.sw_if_index)], table_id=1) route_in_dst.add_vpp_config() self.send_and_expect(self.pg0, pkts_dst, self.pg1) # # add a route in the src table to forward via pg2 # route_in_src = VppIpRoute(self, "2::2", 128, [VppRoutePath(self.pg2.remote_ip6, self.pg2.sw_if_index)], table_id=2) route_in_src.add_vpp_config() self.send_and_expect(self.pg0, pkts_src, self.pg2) # # loop in the lookup DP # route_loop = VppIpRoute(self, "3::3", 128, [VppRoutePath("::", 0xffffffff)]) route_loop.add_vpp_config() p_l = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(src="3::4", dst="3::3") / inet6.TCP(sport=1234, dport=1234) / Raw(b'\xa5' * 100)) self.send_and_assert_no_replies(self.pg0, p_l * 257, "IP lookup loop") class TestIP6Input(VppTestCase): """ IPv6 Input Exception Test Cases """ @classmethod def setUpClass(cls): super(TestIP6Input, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestIP6Input, cls).tearDownClass() def setUp(self): super(TestIP6Input, self).setUp() self.create_pg_interfaces(range(2)) for i in self.pg_interfaces: i.admin_up() i.config_ip6() i.resolve_ndp() def tearDown(self): super(TestIP6Input, self).tearDown() for i in self.pg_interfaces: i.unconfig_ip6() i.admin_down() def test_ip_input_icmp_reply(self): """ IP6 Input Exception - Return ICMP (3,0) """ # # hop limit - ICMP replies # p_version = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6, hlim=1) / inet6.UDP(sport=1234, dport=1234) / Raw(b'\xa5' * 100)) rx = self.send_and_expect(self.pg0, p_version * NUM_PKTS, self.pg0) rx = rx[0] icmp = rx[ICMPv6TimeExceeded] # 0: "hop limit exceeded in transit", self.assertEqual((icmp.type, icmp.code), (3, 0)) icmpv6_data = '\x0a' * 18 all_0s = "::" all_1s = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF" @parameterized.expand([ # Name, src, dst, l4proto, msg, timeout ("src='iface', dst='iface'", None, None, inet6.UDP(sport=1234, dport=1234), "funky version", None), ("src='All 0's', dst='iface'", all_0s, None, ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1), ("src='iface', dst='All 0's'", None, all_0s, ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1), ("src='All 1's', dst='iface'", all_1s, None, ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1), ("src='iface', dst='All 1's'", None, all_1s, ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1), ("src='All 1's', dst='All 1's'", all_1s, all_1s, ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1), ]) def test_ip_input_no_replies(self, name, src, dst, l4, msg, timeout): self._testMethodDoc = 'IPv6 Input Exception - %s' % name p_version = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(src=src or self.pg0.remote_ip6, dst=dst or self.pg1.remote_ip6, version=3) / l4 / Raw(b'\xa5' * 100)) self.send_and_assert_no_replies(self.pg0, p_version * NUM_PKTS, remark=msg or "", timeout=timeout) def test_hop_by_hop(self): """ Hop-by-hop header test """ p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) / IPv6ExtHdrHopByHop() / inet6.UDP(sport=1234, dport=1234) / Raw(b'\xa5' * 100)) self.pg0.add_stream(p) self.pg_enable_capture(self.pg_interfaces) self.pg_start() class TestIPReplace(VppTestCase): """ IPv6 Table Replace """ @classmethod def setUpClass(cls): super(TestIPReplace, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestIPReplace, cls).tearDownClass() def setUp(self): super(TestIPReplace, self).setUp() self.create_pg_interfaces(range(4)) table_id = 1 self.tables = [] for i in self.pg_interfaces: i.admin_up() i.config_ip6() i.generate_remote_hosts(2) self.tables.append(VppIpTable(self, table_id, True).add_vpp_config()) table_id += 1 def tearDown(self): super(TestIPReplace, self).tearDown() for i in self.pg_interfaces: i.admin_down() i.unconfig_ip6() def test_replace(self): """ IP Table Replace """ N_ROUTES = 20 links = [self.pg0, self.pg1, self.pg2, self.pg3] routes = [[], [], [], []] # the sizes of 'empty' tables for t in self.tables: self.assertEqual(len(t.dump()), 2) self.assertEqual(len(t.mdump()), 5) # load up the tables with some routes for ii, t in enumerate(self.tables): for jj in range(1, N_ROUTES): uni = VppIpRoute( self, "2001::%d" % jj if jj != 0 else "2001::", 128, [VppRoutePath(links[ii].remote_hosts[0].ip6, links[ii].sw_if_index), VppRoutePath(links[ii].remote_hosts[1].ip6, links[ii].sw_if_index)], table_id=t.table_id).add_vpp_config() multi = VppIpMRoute( self, "::", "ff:2001::%d" % jj, 128, MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE, [VppMRoutePath(self.pg0.sw_if_index, MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT, proto=FibPathProto.FIB_PATH_NH_PROTO_IP6), VppMRoutePath(self.pg1.sw_if_index, MRouteItfFlags.MFIB_ITF_FLAG_FORWARD, proto=FibPathProto.FIB_PATH_NH_PROTO_IP6), VppMRoutePath(self.pg2.sw_if_index, MRouteItfFlags.MFIB_ITF_FLAG_FORWARD, proto=FibPathProto.FIB_PATH_NH_PROTO_IP6), VppMRoutePath(self.pg3.sw_if_index, MRouteItfFlags.MFIB_ITF_FLAG_FORWARD, proto=FibPathProto.FIB_PATH_NH_PROTO_IP6)], table_id=t.table_id).add_vpp_config() routes[ii].append({'uni': uni, 'multi': multi}) # # replace the tables a few times # for kk in range(3): # replace each table for t in self.tables: t.replace_begin() # all the routes are still there for ii, t in enumerate(self.tables): dump = t.dump() mdump = t.mdump() for r in routes[ii]: self.assertTrue(find_route_in_dump(dump, r['uni'], t)) self.assertTrue(find_mroute_in_dump(mdump, r['multi'], t)) # redownload the even numbered routes for ii, t in enumerate(self.tables): for jj in range(0, N_ROUTES, 2): routes[ii][jj]['uni'].add_vpp_config() routes[ii][jj]['multi'].add_vpp_config() # signal each table converged for t in self.tables: t.replace_end() # we should find the even routes, but not the odd for ii, t in enumerate(self.tables): dump = t.dump() mdump = t.mdump() for jj in range(0, N_ROUTES, 2): self.assertTrue(find_route_in_dump( dump, routes[ii][jj]['uni'], t)) self.assertTrue(find_mroute_in_dump( mdump, routes[ii][jj]['multi'], t)) for jj in range(1, N_ROUTES - 1, 2): self.assertFalse(find_route_in_dump( dump, routes[ii][jj]['uni'], t)) self.assertFalse(find_mroute_in_dump( mdump, routes[ii][jj]['multi'], t)) # reload all the routes for ii, t in enumerate(self.tables): for r in routes[ii]: r['uni'].add_vpp_config() r['multi'].add_vpp_config() # all the routes are still there for ii, t in enumerate(self.tables): dump = t.dump() mdump = t.mdump() for r in routes[ii]: self.assertTrue(find_route_in_dump(dump, r['uni'], t)) self.assertTrue(find_mroute_in_dump(mdump, r['multi'], t)) # # finally flush the tables for good measure # for t in self.tables: t.flush() self.assertEqual(len(t.dump()), 2) self.assertEqual(len(t.mdump()), 5) class TestIP6Replace(VppTestCase): """ IPv4 Interface Address Replace """ @classmethod def setUpClass(cls): super(TestIP6Replace, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestIP6Replace, cls).tearDownClass() def setUp(self): super(TestIP6Replace, self).setUp() self.create_pg_interfaces(range(4)) for i in self.pg_interfaces: i.admin_up() def tearDown(self): super(TestIP6Replace, self).tearDown() for i in self.pg_interfaces: i.admin_down() def get_n_pfxs(self, intf): return len(self.vapi.ip_address_dump(intf.sw_if_index, True)) def test_replace(self): """ IP interface address replace """ intf_pfxs = [[], [], [], []] # add prefixes to each of the interfaces for i in range(len(self.pg_interfaces)): intf = self.pg_interfaces[i] # 2001:16:x::1/64 addr = "2001:16:%d::1" % intf.sw_if_index a = VppIpInterfaceAddress(self, intf, addr, 64).add_vpp_config() intf_pfxs[i].append(a) # 2001:16:x::2/64 - a different address in the same subnet as above addr = "2001:16:%d::2" % intf.sw_if_index a = VppIpInterfaceAddress(self, intf, addr, 64).add_vpp_config() intf_pfxs[i].append(a) # 2001:15:x::2/64 - a different address and subnet addr = "2001:15:%d::2" % intf.sw_if_index a = VppIpInterfaceAddress(self, intf, addr, 64).add_vpp_config() intf_pfxs[i].append(a) # a dump should n_address in it for intf in self.pg_interfaces: self.assertEqual(self.get_n_pfxs(intf), 3) # # remove all the address thru a replace # self.vapi.sw_interface_address_replace_begin() self.vapi.sw_interface_address_replace_end() for intf in self.pg_interfaces: self.assertEqual(self.get_n_pfxs(intf), 0) # # add all the interface addresses back # for p in intf_pfxs: for v in p: v.add_vpp_config() for intf in self.pg_interfaces: self.assertEqual(self.get_n_pfxs(intf), 3) # # replace again, but this time update/re-add the address on the first # two interfaces # self.vapi.sw_interface_address_replace_begin() for p in intf_pfxs[:2]: for v in p: v.add_vpp_config() self.vapi.sw_interface_address_replace_end() # on the first two the address still exist, # on the other two they do not for intf in self.pg_interfaces[:2]: self.assertEqual(self.get_n_pfxs(intf), 3) for p in intf_pfxs[:2]: for v in p: self.assertTrue(v.query_vpp_config()) for intf in self.pg_interfaces[2:]: self.assertEqual(self.get_n_pfxs(intf), 0) # # add all the interface addresses back on the last two # for p in intf_pfxs[2:]: for v in p: v.add_vpp_config() for intf in self.pg_interfaces: self.assertEqual(self.get_n_pfxs(intf), 3) # # replace again, this time add different prefixes on all the interfaces # self.vapi.sw_interface_address_replace_begin() pfxs = [] for intf in self.pg_interfaces: # 2001:18:x::1/64 addr = "2001:18:%d::1" % intf.sw_if_index pfxs.append(VppIpInterfaceAddress(self, intf, addr, 64).add_vpp_config()) self.vapi.sw_interface_address_replace_end() # only .18 should exist on each interface for intf in self.pg_interfaces: self.assertEqual(self.get_n_pfxs(intf), 1) for pfx in pfxs: self.assertTrue(pfx.query_vpp_config()) # # remove everything # self.vapi.sw_interface_address_replace_begin() self.vapi.sw_interface_address_replace_end() for intf in self.pg_interfaces: self.assertEqual(self.get_n_pfxs(intf), 0) # # add prefixes to each interface. post-begin add the prefix from # interface X onto interface Y. this would normally be an error # since it would generate a 'duplicate address' warning. but in # this case, since what is newly downloaded is sane, it's ok # for intf in self.pg_interfaces: # 2001:18:x::1/64 addr = "2001:18:%d::1" % intf.sw_if_index VppIpInterfaceAddress(self, intf, addr, 64).add_vpp_config() self.vapi.sw_interface_address_replace_begin() pfxs = [] for intf in self.pg_interfaces: # 2001:18:x::1/64 addr = "2001:18:%d::1" % (intf.sw_if_index + 1) pfxs.append(VppIpInterfaceAddress(self, intf, addr, 64).add_vpp_config()) self.vapi.sw_interface_address_replace_end() self.logger.info(self.vapi.cli("sh int addr")) for intf in self.pg_interfaces: self.assertEqual(self.get_n_pfxs(intf), 1) for pfx in pfxs: self.assertTrue(pfx.query_vpp_config()) class TestIP6LinkLocal(VppTestCase): """ IPv6 Link Local """ @classmethod def setUpClass(cls): super(TestIP6LinkLocal, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestIP6LinkLocal, cls).tearDownClass() def setUp(self): super(TestIP6LinkLocal, self).setUp() self.create_pg_interfaces(range(2)) for i in self.pg_interfaces: i.admin_up() def tearDown(self): super(TestIP6LinkLocal, self).tearDown() for i in self.pg_interfaces: i.admin_down() def test_ip6_ll(self): """ IPv6 Link Local """ # # two APIs to add a link local address. # 1 - just like any other prefix # 2 - with the special set LL API # # # First with the API to set a 'normal' prefix # ll1 = "fe80:1::1" ll2 = "fe80:2::2" ll3 = "fe80:3::3" VppIpInterfaceAddress(self, self.pg0, ll1, 128).add_vpp_config() # # should be able to ping the ll # p_echo_request_1 = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(src=ll2, dst=ll1) / ICMPv6EchoRequest()) self.send_and_expect(self.pg0, [p_echo_request_1], self.pg0) # # change the link-local on pg0 # v_ll3 = VppIpInterfaceAddress(self, self.pg0, ll3, 128).add_vpp_config() p_echo_request_3 = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IPv6(src=ll2, dst=ll3) / ICMPv6EchoRequest()) self.send_and_expect(self.pg0, [p_echo_request_3], self.pg0) # # set a normal v6 prefix on the link # self.pg0.config_ip6() self.send_and_expect(self.pg0, [p_echo_request_3], self.pg0) # the link-local cannot be removed with self.vapi.assert_negative_api_retval(): v_ll3.remove_vpp_config() # # Use the specific link-local API on pg1 # VppIp6LinkLocalAddress(self, self.pg1, ll1).add_vpp_config() self.send_and_expect(self.pg1, [p_echo_request_1], self.pg1) VppIp6LinkLocalAddress(self, self.pg1, ll3).add_vpp_config() self.send_and_expect(self.pg1, [p_echo_request_3], self.pg1) if __name__ == '__main__': unittest.main(testRunner=VppTestRunner)