/* * 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. */ /* * ip/ip_lookup.c: ip4/6 adjacency and lookup table managment * * Copyright (c) 2008 Eliot Dresselhaus * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include /** * @file * @brief IPv4 and IPv6 adjacency and lookup table managment. * */ clib_error_t * ip_interface_address_add_del (ip_lookup_main_t * lm, u32 sw_if_index, void *addr_fib, u32 address_length, u32 is_del, u32 * result_if_address_index) { vnet_main_t *vnm = vnet_get_main (); ip_interface_address_t *a, *prev, *next; uword *p = mhash_get (&lm->address_to_if_address_index, addr_fib); vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index, sw_if_index, ~0); a = p ? pool_elt_at_index (lm->if_address_pool, p[0]) : 0; /* Verify given length. */ if ((a && (address_length != a->address_length)) || (address_length == 0) || (lm->is_ip6 && address_length > 128) || (!lm->is_ip6 && address_length > 32)) { vnm->api_errno = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH; return clib_error_create ("%U wrong length (expected %d) for interface %U", lm->format_address_and_length, addr_fib, address_length, a ? a->address_length : -1, format_vnet_sw_if_index_name, vnm, sw_if_index); } if (is_del) { if (!a) { vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index); vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE; return clib_error_create ("%U not found for interface %U", lm->format_address_and_length, addr_fib, address_length, format_vnet_sw_interface_name, vnm, si); } if (a->prev_this_sw_interface != ~0) { prev = pool_elt_at_index (lm->if_address_pool, a->prev_this_sw_interface); prev->next_this_sw_interface = a->next_this_sw_interface; } if (a->next_this_sw_interface != ~0) { next = pool_elt_at_index (lm->if_address_pool, a->next_this_sw_interface); next->prev_this_sw_interface = a->prev_this_sw_interface; if (a->prev_this_sw_interface == ~0) lm->if_address_pool_index_by_sw_if_index[sw_if_index] = a->next_this_sw_interface; } if ((a->next_this_sw_interface == ~0) && (a->prev_this_sw_interface == ~0)) lm->if_address_pool_index_by_sw_if_index[sw_if_index] = ~0; mhash_unset (&lm->address_to_if_address_index, addr_fib, /* old_value */ 0); pool_put (lm->if_address_pool, a); if (result_if_address_index) *result_if_address_index = ~0; } else if (!a) { u32 pi; /* previous index */ u32 ai; u32 hi; /* head index */ pool_get (lm->if_address_pool, a); clib_memset (a, ~0, sizeof (a[0])); ai = a - lm->if_address_pool; hi = pi = lm->if_address_pool_index_by_sw_if_index[sw_if_index]; prev = 0; while (pi != (u32) ~ 0) { prev = pool_elt_at_index (lm->if_address_pool, pi); pi = prev->next_this_sw_interface; } pi = prev ? prev - lm->if_address_pool : (u32) ~ 0; a->address_key = mhash_set (&lm->address_to_if_address_index, addr_fib, ai, /* old_value */ 0); a->address_length = address_length; a->sw_if_index = sw_if_index; a->flags = 0; a->prev_this_sw_interface = pi; a->next_this_sw_interface = ~0; if (prev) prev->next_this_sw_interface = ai; lm->if_address_pool_index_by_sw_if_index[sw_if_index] = (hi != ~0) ? hi : ai; if (result_if_address_index) *result_if_address_index = ai; } else { if (sw_if_index != a->sw_if_index) { if (result_if_address_index) *result_if_address_index = ~0; vnm->api_errno = VNET_API_ERROR_DUPLICATE_IF_ADDRESS; return clib_error_create ("Prefix %U already found on interface %U", lm->format_address_and_length, addr_fib, address_length, format_vnet_sw_if_index_name, vnm, a->sw_if_index); } if (result_if_address_index) *result_if_address_index = a - lm->if_address_pool; } return /* no error */ 0; } static clib_error_t * ip_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add) { vec_validate_init_empty (ip4_main. lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0); vec_validate_init_empty (ip6_main. lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0); return (NULL); } VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip_sw_interface_add_del); void ip_lookup_init (ip_lookup_main_t * lm, u32 is_ip6) { if (!lm->fib_result_n_bytes) lm->fib_result_n_bytes = sizeof (uword); lm->is_ip6 = is_ip6; if (is_ip6) { lm->format_address_and_length = format_ip6_address_and_length; mhash_init (&lm->address_to_if_address_index, sizeof (uword), sizeof (ip6_address_fib_t)); } else { lm->format_address_and_length = format_ip4_address_and_length; mhash_init (&lm->address_to_if_address_index, sizeof (uword), sizeof (ip4_address_fib_t)); } { int i; /* Setup all IP protocols to be punted and builtin-unknown. */ for (i = 0; i < 256; i++) { lm->local_next_by_ip_protocol[i] = IP_LOCAL_NEXT_PUNT; lm->builtin_protocol_by_ip_protocol[i] = IP_BUILTIN_PROTOCOL_UNKNOWN; } lm->local_next_by_ip_protocol[IP_PROTOCOL_UDP] = IP_LOCAL_NEXT_UDP_LOOKUP; lm->local_next_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 : IP_PROTOCOL_ICMP] = IP_LOCAL_NEXT_ICMP; lm->builtin_protocol_by_ip_protocol[IP_PROTOCOL_UDP] = IP_BUILTIN_PROTOCOL_UDP; lm->builtin_protocol_by_ip_protocol[is_ip6 ? IP_PROTOCOL_ICMP6 : IP_PROTOCOL_ICMP] = IP_BUILTIN_PROTOCOL_ICMP; } } u8 * format_ip_flow_hash_config (u8 * s, va_list * args) { flow_hash_config_t flow_hash_config = va_arg (*args, u32); #define _(n,v) if (flow_hash_config & v) s = format (s, "%s ", #n); foreach_flow_hash_bit; #undef _ return s; } u8 * format_ip_adjacency_packet_data (u8 * s, va_list * args) { u32 adj_index = va_arg (*args, u32); u8 *packet_data = va_arg (*args, u8 *); u32 n_packet_data_bytes = va_arg (*args, u32); ip_adjacency_t *adj = adj_get (adj_index); switch (adj->lookup_next_index) { case IP_LOOKUP_NEXT_REWRITE: case IP_LOOKUP_NEXT_MCAST: s = format (s, "%U", format_hex_bytes, packet_data, n_packet_data_bytes); break; default: break; } return s; } static uword unformat_dpo (unformat_input_t * input, va_list * args) { dpo_id_t *dpo = va_arg (*args, dpo_id_t *); fib_protocol_t fp = va_arg (*args, int); dpo_proto_t proto; proto = fib_proto_to_dpo (fp); if (unformat (input, "drop")) dpo_copy (dpo, drop_dpo_get (proto)); else if (unformat (input, "punt")) dpo_copy (dpo, punt_dpo_get (proto)); else if (unformat (input, "local")) receive_dpo_add_or_lock (proto, ~0, NULL, dpo); else if (unformat (input, "null-send-unreach")) ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_SEND_ICMP_UNREACH, dpo); else if (unformat (input, "null-send-prohibit")) ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_SEND_ICMP_PROHIBIT, dpo); else if (unformat (input, "null")) ip_null_dpo_add_and_lock (proto, IP_NULL_ACTION_NONE, dpo); else if (unformat (input, "classify")) { u32 classify_table_index; if (!unformat (input, "%d", &classify_table_index)) { clib_warning ("classify adj must specify table index"); return 0; } dpo_set (dpo, DPO_CLASSIFY, proto, classify_dpo_create (proto, classify_table_index)); } else return 0; return 1; } const ip46_address_t zero_addr = { .as_u64 = { 0, 0}, }; static clib_error_t * vnet_ip_route_cmd (vlib_main_t * vm, unformat_input_t * main_input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; u32 table_id, is_del, fib_index, payload_proto; dpo_id_t dpo = DPO_INVALID, *dpos = NULL; fib_route_path_t *rpaths = NULL, rpath; fib_prefix_t *prefixs = NULL, pfx; clib_error_t *error = NULL; f64 count; int i; is_del = 0; table_id = 0; count = 1; clib_memset (&pfx, 0, sizeof (pfx)); /* Get a line of input. */ if (!unformat_user (main_input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { clib_memset (&rpath, 0, sizeof (rpath)); if (unformat (line_input, "table %d", &table_id)) ; else if (unformat (line_input, "count %f", &count)) ; else if (unformat (line_input, "%U/%d", unformat_ip4_address, &pfx.fp_addr.ip4, &pfx.fp_len)) { payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP4; vec_add1 (prefixs, pfx); } else if (unformat (line_input, "%U/%d", unformat_ip6_address, &pfx.fp_addr.ip6, &pfx.fp_len)) { payload_proto = pfx.fp_proto = FIB_PROTOCOL_IP6; vec_add1 (prefixs, pfx); } else if (unformat (line_input, "via %U", unformat_fib_route_path, &rpath, &payload_proto)) { vec_add1 (rpaths, rpath); } else if (vec_len (prefixs) > 0 && unformat (line_input, "via %U", unformat_dpo, &dpo, prefixs[0].fp_proto)) { vec_add1 (dpos, dpo); } else if (unformat (line_input, "del")) is_del = 1; else if (unformat (line_input, "add")) is_del = 0; else { error = unformat_parse_error (line_input); goto done; } } if (vec_len (prefixs) == 0) { error = clib_error_return (0, "expected ip4/ip6 destination address/length."); goto done; } if (!is_del && vec_len (rpaths) + vec_len (dpos) == 0) { error = clib_error_return (0, "expected paths."); goto done; } if (~0 == table_id) { /* * if no table_id is passed we will manipulate the default */ fib_index = 0;
#!/usr/bin/env python

import unittest
import random

from scapy.packet import Raw
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, UDP

from framework import VppTestCase, VppTestRunner
from util import Host, ppp


class TestL2xc(VppTestCase):
    """ L2XC Test Case """

    @classmethod
    def setUpClass(cls):
        """
        Perform standard class setup (defined by class method setUpClass in
        class VppTestCase) before running the test case, set test case related
        variables and configure VPP.

        :var int hosts_nr: Number of hosts to be created.
        :var int dl_pkts_per_burst: Number of packets in burst for dual-loop
            test.
        :var int sl_pkts_per_burst: Number of packets in burst for single-loop
            test.
        """
        super(TestL2xc, cls).setUpClass()

        # Test variables
        cls.hosts_nr = 10
        cls.dl_pkts_per_burst = 257
        cls.sl_pkts_per_burst = 2

        try:
            # create 4 pg interfaces
            cls.create_pg_interfaces(range(4))

            # packet flows mapping pg0 -> pg1, pg2 -> pg3, etc.
            cls.flows = dict()
            cls.flows[cls.pg0] = [cls.pg1]
            cls.flows[cls.pg1] = [cls.pg0]
            cls.flows[cls.pg2] = [cls.pg3]
            cls.flows[cls.pg3] = [cls.pg2]

            # packet sizes
            cls.pg_if_packet_sizes = [64, 512, 1518, 9018]

            cls.interfaces = list(cls.pg_interfaces)

            # Create bi-directional cross-connects between pg0 and pg1
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg0.sw_if_index, cls.pg1.sw_if_index, enable=1)
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg1.sw_if_index, cls.pg0.sw_if_index, enable=1)

            # Create bi-directional cross-connects between pg2 and pg3
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg2.sw_if_index, cls.pg3.sw_if_index, enable=1)
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg3.sw_if_index, cls.pg2.sw_if_index, enable=1)

            # mapping between packet-generator index and lists of test hosts
            cls.hosts_by_pg_idx = dict()

            # Create host MAC and IPv4 lists
            cls.create_host_lists(cls.hosts_nr)

            # setup all interfaces
            for i in cls.interfaces:
                i.admin_up()

        except Exception:
            super(TestL2xc, cls).tearDownClass()
            raise

    def setUp(self):
        super(TestL2xc, self).setUp()
        self.reset_packet_infos()

    def tearDown(self):
        """
        Show various debug prints after each test.
        """
        super(TestL2xc, self).tearDown()
        if not self.vpp_dead:
            self.logger.info(self.vapi.ppcli("show l2patch"))

    @classmethod
    def create_host_lists(cls, count):
        """
        Method to create required number of MAC and IPv4 addresses.
        Create required number of host MAC addresses and distribute them among
        interfaces. Create host IPv4 address for every host MAC address too.

        :param count: Number of hosts to create MAC and IPv4 addresses for.
        """
        for pg_if in cls.pg_interfaces:
            cls.hosts_by_pg_idx[pg_if.sw_if_index] = []
            hosts = cls.hosts_by_pg_idx[pg_if.sw_if_index]
            for j in range(0, count):
                host = Host(
                    "00:00:00:ff:%02x:%02x" % (pg_if.sw_if_index, j),
                    "172.17.1%02x.%u" % (pg_if.sw_if_index, j))
                hosts.append(host)

    def create_stream(self, src_if, packet_sizes, packets_per_burst):
        """
        Create input packet stream for defined interface.

        :param object src_if: Interface to create packet stream for.
        :param list packet_sizes: List of required packet sizes.
        :param int packets_per_burst: Number of packets in burst.
        :return: Stream of packets.
        """
        pkts = []
        for i in range(0, packets_per_burst):
            dst_if = self.flows[src_if][0]
            dst_host = random.choice(self.hosts_by_pg_idx[dst_if.sw_if_index])
            src_host = random.choice(self.hosts_by_pg_idx[src_if.sw_if_index])
            pkt_info = self.create_packet_info(src_if, dst_if)
            payload = self.info_to_payload(pkt_info)
            p = (Ether(dst=dst_host.mac, src=src_host.mac) /
                 IP(src=src_host.ip4, dst=dst_host.ip4) /
                 UDP(sport=1234, dport=1234) /
                 Raw(payload))
            pkt_info.data = p.copy()
            size = random.choice(packet_sizes)
            self.extend_packet(p, size)
            pkts.append(p)
        return pkts

    def verify_capture(self, pg_if, capture):
        """
        Verify captured input packet stream for defined interface.

        :param object pg_if: Interface to verify captured packet stream for.
        :param list capture: Captured packet stream.
        """
        last_info = dict()
        for i in self.interfaces:
            last_info[i.sw_if_index] = None
        dst_sw_if_index = pg_if.sw_if_index
        for packet in capture:
            try:
                ip = packet[IP]
                udp = packet[UDP]
                payload_info = self.payload_to_info(str(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)" %
                                  (pg_if.name,