#!/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",
},
};