#!/usr/bin/env python3 import binascii import random import socket import unittest import scapy.compat from scapy.contrib.mpls import MPLS from scapy.contrib.gtp import GTP_U_Header from scapy.layers.inet import IP, UDP, TCP, ICMP, icmptypes, icmpcodes from scapy.layers.inet6 import IPv6 from scapy.layers.l2 import Ether, Dot1Q, ARP from scapy.packet import Raw from six import moves from framework import tag_fixme_vpp_workers from framework import VppTestCase, VppTestRunner from util import ppp from vpp_ip_route import ( VppIpRoute, VppRoutePath, VppIpMRoute, VppMRoutePath, VppMplsIpBind, VppMplsTable, VppIpTable, FibPathType, find_route, VppIpInterfaceAddress, find_route_in_dump, find_mroute_in_dump, ) from vpp_ip import VppIpPuntPolicer, VppIpPuntRedirect, VppIpPathMtu from vpp_sub_interface import VppSubInterface, VppDot1QSubint, VppDot1ADSubint from vpp_papi import vpp_papi, VppEnum from vpp_neighbor import VppNeighbor from vpp_lo_interface import VppLoInterface from vpp_policer import VppPolicer, PolicerAction NUM_PKTS = 67 class TestIPv4(VppTestCase): """IPv4 Test Case""" @classmethod def setUpClass(cls): super(TestIPv4, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestIPv4, 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 IPv4 addresses - resolve neighbor address using ARP - configure 200 fib entries :ivar list interfaces: pg interfaces and subinterfaces. :ivar dict flows: IPv4 packet flows in test. """ super(TestIPv4, self).setUp() # create 3 pg interfaces self.create_pg_interfaces(range(3)) # create 2 subinterfaces for pg1 and pg2 self.sub_interfaces = [ VppDot1QSubint(self, self.pg1, 100), 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_ip4() i.resolve_arp() # config 2M FIB entries def tearDown(self): """Run standard test teardown and log ``show ip arp``.""" super(TestIPv4, self).tearDown() def show_commands_at_teardown(self): self.logger.info(self.vapi.cli("show ip4 neighbors")) # info(self.vapi.cli("show ip 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[IP].dst = dst_if.remote_ip4 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) / IP(src=src_if.remote_ip4) / 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[IP] udp = packet[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[IP].src) self.assertEqual(ip.dst, saved_packet[IP].dst) self.assertEqual(udp.sport, saved_packet[UDP].sport) self.assertEqual(udp.dport, saved_packet[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_fib(self): """IPv4 FIB test Test scenario: - Create IPv4 stream for pg0 interface - Create IPv4 tagged streams for pg1's and pg2's sub-interface. - 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) class TestIPv4RouteLookup(VppTestCase): """IPv4 Route Lookup Test Case""" routes = [] def route_lookup(self, prefix, exact): return self.vapi.api( self.vapi.papi.ip_route_lookup, { "table_id": 0, "exact": exact, "prefix": prefix, }, ) @classmethod def setUpClass(cls): super(TestIPv4RouteLookup, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestIPv4RouteLookup, cls).tearDownClass() def setUp(self): super(TestIPv4RouteLookup, self).setUp() drop_nh = VppRoutePath( "127.0.0.1", 0xFFFFFFFF, type=FibPathType.FIB_PATH_TYPE_DROP ) # Add 3 routes r = VppIpRoute(self, "1.1.0.0", 16, [drop_nh]) r.add_vpp_config() self.routes.append(r) r = VppIpRoute(self, "1.1.1.0", 24, [drop_nh]) r.add_vpp_config() self.routes.append(r) r = VppIpRoute(self, "1.1.1.1", 32, [drop_nh]) r.add_vpp_config() self.routes.append(r) def tearDown(self): # Remove the routes we added for r in self.routes: r.remove_vpp_config() super(TestIPv4RouteLookup, self).tearDown() def test_exact_match(self): # Verify we find the host route prefix = "1.1.1.1/32" result = self.route_lookup(prefix, True) assert prefix == str(result.route.prefix) # Verify we find a middle prefix route prefix = "1.1.1.0/24" result = self.route_lookup(prefix, True) assert prefix == str(result.route.prefix) # Verify we do not find an available LPM. with self.vapi.assert_negative_api_retval(): self.route_lookup("1.1.1.2/32", True) def test_longest_prefix_match(self): # verify we find lpm lpm_prefix = "1.1.1.0/24" result = self.route_lookup("1.1.1.2/32", False) assert lpm_prefix == str(result.route.prefix) # Verify we find the exact when not requested result = self.route_lookup(lpm_prefix, False) assert lpm_prefix == str(result.route.prefix) # Can't seem to delete the default route so no negative LPM test. class TestIPv4IfAddrRoute(VppTestCase): """IPv4 Interface Addr Route Test Case""" @classmethod def setUpClass(cls): super(TestIPv4IfAddrRoute, cls).setUpClass() @classmethod def tearDownClass(cls): super(TestIPv4IfAddrRoute, cls).tearDownClass() def setUp(self): super(TestIPv4IfAddrRoute, self).setUp() # create 1 pg interface self.create_pg_interfaces(range(1)) for i in self.pg_interfaces: i.admin_up() i.config_ip4() i.resolve_arp() def tearDown(self): super(TestIPv4IfAddrRoute, self).tearDown() for i in self.pg_interfaces: i.unconfig_ip4() i.admin_down() def test_ipv4_ifaddrs_same_prefix(self): """IPv4 Interface Addresses Same Prefix test Test scenario: - Verify no route in FIB for prefix 10.10.10.0/24 - Configure IPv4 address 10.10.10.10/24 on an interface - Verify route in FIB for prefix 10.10.10.0/24 - Configure IPv4 address 10.10.10.20/24 on an interface - Delete 10.10.10.10/24 from interface - Verify route in FIB for prefix 10.10.10.0/24 - Delete 10.10.10.20/24 from interface - Verify no route in FIB for prefix 10.10.10.0/24 """ # create two addresses, verify route not present if_addr1 = VppIpInterfaceAddress(self, self.pg0, "10.10.10.10", 24) if_addr2 = VppIpInterfaceAddress(self, self.pg0, "10.10.10.20", 24) self.assertFalse(if_addr1.query_vpp_config()) # 10.10.10.10/24 self.assertFalse(find_route(self, "10.10.10.10", 32)) self.assertFalse(find_route(self, "10.10.10.20", 32)) self.assertFalse(find_route(self, "10.10.10.255", 32)) self.assertFalse(find_route(self, "10.10.10.0", 32)) # configure first address, verify route present if_addr1.add_vpp_config() self.assertTrue(if_addr1.query_vpp_config()) # 10.10.10.10/24 self.assertTrue(find_route(self, "10.10.10.10", 32)) self.assertFalse(find_route(self, "10.10.10.20", 32)) self.assertTrue(find_route(self, "10.10.10.255", 32)) self.assertTrue(find_route(self, "10.10.10.0", 32)) # configure second address, delete first, verify route not removed if_addr2.add_vpp_config() if_addr1.remove_vpp_config() self.assertFalse(if_addr1.query_vpp_config()) # 10.10.10.10/24 self.assertTrue(if_addr2.query_vpp_config()) # 10.10.10.20/24 self.assertFalse(find_route(self, "10.10.1
/*
* 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 Host utility functions
*/
#include <vppinfra/format.h>
#include <vlib/vlib.h>
#include <vlib/threads.h>
#include <vnet/vnet.h>
#include <vppinfra/format.h>
/**
* @brief GDB callable function: vl - Return vector length of vector
*
* @param *p - void - address of vector
*
* @return length - u32
*
*/
u32
vl (void *p)
{
return vec_len (p);
}
/**
* @brief GDB callable function: pe - call pool_elts - number of elements in a pool
*
* @param *v - void - address of pool
*
* @return number - uword
*
*/
uword
pe (void *v)
{
return (pool_elts (v));
}
/**
* @brief GDB callable function: pifi - call pool_is_free_index - is passed index free?
*
* @param *p - void - address of pool
* @param *index - u32
*
* @return 0|1 - int
*
*/
int
pifi (void *p, u32 index)
{
return pool_is_free_index (p, index);
}
/**
* @brief GDB callable function: debug_hex_bytes - return formatted hex string
*
* @param *s - u8
* @param n - u32 - number of bytes to format
*
*/
void
debug_hex_bytes (u8 * s, u32 n)
{
fformat (stderr, "%U\n", format_hex_bytes, s, n);
}
/**
* @brief GDB callable function: vlib_dump_frame_ownership
*
*/
void
vlib_dump_frame_ownership (void)
{
vlib_main_t *vm = vlib_get_main ();
vlib_node_main_t *nm = &vm->node_main;
vlib_node_runtime_t *this_node_runtime;
vlib_next_frame_t *nf;
u32 first_nf_index;
u32 index;
vec_foreach (this_node_runtime, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
{
first_nf_index = this_node_runtime->next_frame_index;
for (index = first_nf_index; index < first_nf_index +
this_node_runtime->n_next_nodes; index++)
{
vlib_node_runtime_t *owned_runtime;
nf = vec_elt_at_index (vm->node_main.next_frames, index);
if (nf->flags & VLIB_FRAME_OWNER)
{
owned_runtime = vec_elt_at_index (nm->nodes_by_type[0],
nf->node_runtime_index);
fformat (stderr,
"%s next index %d owns enqueue rights to %s\n",
nm->nodes[this_node_runtime->node_index]->name,
index - first_nf_index,
nm->nodes[owned_runtime->node_index]->name);
fformat (stderr, " nf index %d nf->frame_index %d\n",
nf - vm->node_main.next_frames, nf->frame_index);
}
}
}
}
/**
* @brief GDB callable function: vlib_runtime_index_to_node_name
*
* Takes node index and will return the node name.
*
* @param index - u32
*/
void
vlib_runtime_index_to_node_name (u32 index)
{
vlib_main_t *vm = vlib_get_main ();
vlib_node_main_t *nm = &vm->node_main;
if (index >= vec_len (nm->nodes))
{
fformat (stderr, "%d out of range, max %d\n", vec_len (nm->nodes));
return;
}
fformat (stderr, "node runtime index %d name %s\n", index,
nm->nodes[index]->name);
}
void
gdb_show_errors (int verbose)
{
extern vlib_cli_command_t vlib_cli_show_errors;
unformat_input_t input;
vlib_main_t *vm = vlib_get_main ();
if (verbose == 0)
unformat_init_string (&input, "verbose 0", 9);
else if (verbose == 1)
unformat_init_string (&input, "verbose 1", 9);
else
{
fformat (stderr, "verbose not 0 or 1\n");
return;
}
vlib_cli_show_errors.function (vm, &input, 0 /* cmd */ );
unformat_free (&input);
}
void
gdb_show_session (int verbose)
{
extern vlib_cli_command_t vlib_cli_show_session_command;
unformat_input_t input;
vlib_main_t *vm = vlib_get_main ();
if (verbose == 0)
unformat_init_string (&input, "verbose 0", 9);
else if (verbose == 1)
unformat_init_string (&input, "verbose 1", 9);
else if (verbose == 2)
unformat_init_string (&input, "verbose 2", 9);
else
{
fformat (stderr, "verbose not 0 - 2\n");
return;
}
vlib_cli_show_session_command.function (vm, &input, 0 /* cmd */ );
unformat_free (&input);
}
/**
* @brief GDB callable function: show_gdb_command_fn - show gdb
*
* Shows list of functions for VPP available in GDB
*
* @return error - clib_error_t
*/
static clib_error_t *
show_gdb_command_fn (vlib_main_t * vm,
unformat_input_t * input, vlib_cli_command_t * cmd)
{
vlib_cli_output (vm, "vl(p) returns vec_len(p)");
vlib_cli_output (vm, "vb(b) returns vnet_buffer(b) [opaque]");
vlib_cli_output (vm, "vb2(b) returns vnet_buffer2(b) [opaque2]");
vlib_cli_output (vm, "pe(p) returns pool_elts(p)");
vlib_cli_output (vm, "pifi(p, i) returns pool_is_free_index(p, i)");
vlib_cli_output (vm, "gdb_show_errors(0|1) dumps error counters");
vlib_cli_output (vm, "gdb_show_session dumps session counters");
vlib_cli_output (vm, "debug_hex_bytes (ptr, n_bytes) dumps n_bytes in hex");
vlib_cli_output (vm, "vlib_dump_frame_ownership() does what it says");
vlib_cli_output (vm, "vlib_runtime_index_to_node_name (index) prints NN");
return 0;
}
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (show_gdb_funcs_command, static) = {
.path = "show gdb",
.short_help = "Describe functions which can be called from gdb",
.function = show_gdb_command_fn,
};
/* *INDENT-ON* */
vnet_buffer_opaque_t *
vb (void *vb_arg)
{
vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
vnet_buffer_opaque_t *rv;
rv = vnet_buffer (b);
return rv;
}
vnet_buffer_opaque2_t *
vb2 (void *vb_arg)
{
vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
vnet_buffer_opaque2_t *rv;
rv = vnet_buffer2 (b);
return rv;
}
/* Cafeteria plan, maybe you don't want these functions */
clib_error_t *
gdb_func_init (vlib_main_t * vm)
{
return 0;
}
VLIB_INIT_FUNCTION (gdb_func_init);
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/