/* *------------------------------------------------------------------ * l2_api.c - layer 2 forwarding api * * Copyright (c) 2016 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. *------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define REPLY_MSG_ID_BASE l2input_main.msg_id_base #include static void send_l2_xconnect_details (vl_api_registration_t * reg, u32 context, u32 rx_sw_if_index, u32 tx_sw_if_index) { vl_api_l2_xconnect_details_t *mp; mp = vl_msg_api_alloc (sizeof (*mp)); clib_memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_L2_XCONNECT_DETAILS); mp->context = context; mp->rx_sw_if_index = htonl (rx_sw_if_index); mp->tx_sw_if_index = htonl (tx_sw_if_index); vl_api_send_msg (reg, (u8 *) mp); } static void vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp) { vl_api_registration_t *reg; l2input_main_t *l2im = &l2input_main; u32 sw_if_index; l2_input_config_t *config; reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; /* *INDENT-OFF* */ vec_foreach_index (sw_if_index, l2im->configs) { config = vec_elt_at_index (l2im->configs, sw_if_index); if (l2_input_is_xconnect (config)) send_l2_xconnect_details (reg, mp->context, sw_if_index, config->output_sw_if_index); } /* *INDENT-ON* */ } static void vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp) { int rv = 0; vl_api_l2_fib_clear_table_reply_t *rmp; /* Clear all MACs including static MACs */ l2fib_clear_table (); REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY); } static void send_l2fib_table_entry (vpe_api_main_t * am, vl_api_registration_t * reg, l2fib_entry_key_t * l2fe_key, l2fib_entry_result_t * l2fe_res, u32 context) { vl_api_l2_fib_table_details_t *mp; mp = vl_msg_api_alloc (sizeof (*mp)); clib_memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_L2_FIB_TABLE_DETAILS); mp->bd_id = ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id); mac_address_encode ((mac_address_t *) l2fe_key->fields.mac, mp->mac); mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index); mp->static_mac = (l2fib_entry_result_is_set_STATIC (l2fe_res) ? 1 : 0); mp->filter_mac = (l2fib_entry_result_is_set_FILTER (l2fe_res) ? 1 : 0); mp->bvi_mac = (l2fib_entry_result_is_set_BVI (l2fe_res) ? 1 : 0); mp->context = context; vl_api_send_msg (reg, (u8 *) mp); } static void vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp) { vpe_api_main_t *am = &vpe_api_main; bd_main_t *bdm = &bd_main; l2fib_entry_key_t *l2fe_key = NULL; l2fib_entry_result_t *l2fe_res = NULL; u32 ni, bd_id = ntohl (mp->bd_id); u32 bd_index; vl_api_registration_t *reg; uword *p; reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; /* see l2fib_table_dump: ~0 means "any" */ if (bd_id == ~0) bd_index = ~0; else { p = hash_get (bdm->bd_index_by_bd_id, bd_id); if (p == 0) return; bd_index = p[0]; } l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res); vec_foreach_index (ni, l2fe_key) { send_l2fib_table_entry (am, reg, vec_elt_at_index (l2fe_key, ni), vec_elt_at_index (l2fe_res, ni), mp->context); } vec_free (l2fe_key); vec_free (l2fe_res); } static void vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp) { bd_main_t *bdm = &bd_main; l2input_main_t *l2im = &l2input_main; vl_api_l2fib_add_del_reply_t *rmp; int rv = 0; u32 bd_id = ntohl (mp->bd_id); uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id); if (!p) { rv = VNET_API_ERROR_NO_SUCH_ENTRY; goto bad_sw_if_index; } u32 bd_index = p[0]; mac_address_t mac; mac_address_decode (mp->mac, &mac); if (mp->is_add) { if (mp->filter_mac) l2fib_add_filter_entry (mac.bytes, bd_index); else { l2fib_entry_result_flags_t flags = L2FIB_ENTRY_RESULT_FLAG_NONE; u32 sw_if_index = ntohl (mp->sw_if_index); VALIDATE_SW_IF_INDEX (mp); if (vec_len (l2im->configs) <= sw_if_index) { rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; goto bad_sw_if_index; } else { l2_input_config_t *config; config = vec_elt_at_index (l2im->configs, sw_if_index); if (!l2_input_is_bridge (config)) { rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; goto bad_sw_if_index; } } if (mp->static_mac) flags |= L2FIB_ENTRY_RESULT_FLAG_STATIC; if (mp->bvi_mac) flags |= L2FIB_ENTRY_RESULT_FLAG_BVI; l2fib_add_entry (mac.bytes, bd_index, sw_if_index, flags); } } else { u32 sw_if_index = ntohl (mp->sw_if_index); if (l2fib_del_entry (mac.bytes, bd_index, sw_if_index)) rv = VNET_API_ERROR_NO_SUCH_ENTRY; } BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY); } static void vl_api_want_l2_macs_events2_t_handler (vl_api_want_l2_macs_events2_t *mp) { int rv = 0; vl_api_want_l2_macs_events2_reply_t *rmp; l2learn_main_t *lm = &l2learn_main; l2fib_main_t *fm = &l2fib_main; u32 pid = ntohl (mp->pid); if (mp->enable_disable) { if ((lm->client_pid == 0) || (lm->client_pid == pid)) { if (mp->max_macs_
#!/usr/bin/env python3

import sys
import os
import unittest
import importlib
import argparse


def discover_tests(directory, callback, ignore_path):
    do_insert = True
    for _f in os.listdir(directory):
        f = "%s/%s" % (directory, _f)
        if os.path.isdir(f):
            if ignore_path is not None and f.startswith(ignore_path):
                continue
            discover_tests(f, callback, ignore_path)
            continue
        if not os.path.isfile(f):
            continue
        if do_insert:
            sys.path.insert(0, directory)
            do_insert = False
        if not _f.startswith("test_") or not _f.endswith(".py"):
            continue
        name = "".join(f.split("/")[-1].split(".")[:-1])
        module = importlib.import_module(name)
        for name, cls in module.__dict__.items():
            if not isinstance(cls, type):
                continue
            if not issubclass(cls, unittest.TestCase):
                continue
            if name == "VppTestCase" or name.startswith("Template"):
                continue
            for method in dir(cls):
                if not callable(getattr(cls, method)):
                    continue
                if method.startswith("test_"):
                    callback(_f, cls, method)


def print_callback(file_name, cls, method):
    print("%s.%s.%s" % (file_name, cls.__name__, method))


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Discover VPP unit tests")
    parser.add_argument("-d", "--dir", action='append', type=str,
                        help="directory containing test files "
                             "(may be specified multiple times)")
    args = parser.parse_args()
    if args.dir is None:
        args.dir = "."

    ignore_path = os.getenv("VENV_PATH", "")
    suite = unittest.TestSuite()
    for d in args.dir:
        discover_tests(d, print_callback, ignore_path)
/ if (bd_id == ~0) { start = 1; end = vec_len (l2input_main.bd_configs); } else { p = hash_get (bdm->bd_index_by_bd_id, bd_id); if (p == 0) return; bd_index = p[0]; vec_validate (l2input_main.bd_configs, bd_index); start = bd_index; end = start + 1; } for (bd_index = start; bd_index < end; bd_index++) { bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index); if (bd_is_valid (bd_config)) { ip4_address_t ip4_addr; ip6_address_t *ip6_addr; mac_address_t mac; u64 mac64; bd_id = bd_config->bd_id; /* *INDENT-OFF* */ hash_foreach (ip4_addr.as_u32, mac64, bd_config->mac_by_ip4, ({ ip46_address_t ip = { .ip4 = ip4_addr, }; mac_address_from_u64(&mac, mac64); send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP4, &mac, mp->context); })); hash_foreach_mem (ip6_addr, mac64, bd_config->mac_by_ip6, ({ ip46_address_t ip = { .ip6 = *ip6_addr, }; mac_address_from_u64(&mac, mac64); send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6, &mac, mp->context); })); /* *INDENT-ON* */ } } } static void vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp) { ip46_address_t ip_addr = ip46_address_initializer; vl_api_bd_ip_mac_add_del_reply_t *rmp; bd_main_t *bdm = &bd_main; u32 bd_index, bd_id; mac_address_t mac; ip46_type_t type; int rv = 0; uword *p; bd_id = ntohl (mp->entry.bd_id); if (bd_id == 0) { rv = VNET_API_ERROR_BD_NOT_MODIFIABLE; goto out; } p = hash_get (bdm->bd_index_by_bd_id, bd_id); if (p == 0) { rv = VNET_API_ERROR_NO_SUCH_ENTRY; goto out; } bd_index = p[0]; type = ip_address_decode (&mp->entry.ip, &ip_addr); mac_address_decode (mp->entry.mac, &mac); if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, mp->is_add)) rv = VNET_API_ERROR_UNSPECIFIED; out: REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY); } static void vl_api_bd_ip_mac_flush_t_handler (vl_api_bd_ip_mac_flush_t * mp) { vl_api_bd_ip_mac_flush_reply_t *rmp; bd_main_t *bdm = &bd_main; u32 bd_index, bd_id; int rv = 0; uword *p; bd_id = ntohl (mp->bd_id); if (bd_id == 0) { rv = VNET_API_ERROR_BD_NOT_MODIFIABLE; goto out; } p = hash_get (bdm->bd_index_by_bd_id, bd_id); if (p == 0) { rv = VNET_API_ERROR_NO_SUCH_ENTRY; goto out; } bd_index = p[0]; bd_flush_ip_mac (bd_index); out: REPLY_MACRO (VL_API_BD_IP_MAC_FLUSH_REPLY); } extern void l2_efp_filter_configure (vnet_main_t * vnet_main, u32 sw_if_index, u8 enable); static void vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t * mp) { int rv; vl_api_l2_interface_efp_filter_reply_t *rmp; vnet_main_t *vnm = vnet_get_main (); VALIDATE_SW_IF_INDEX (mp); // enable/disable the feature l2_efp_filter_configure (vnm, ntohl (mp->sw_if_index), mp->enable_disable); rv = vnm->api_errno; BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY); } static void vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp) { extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, int is_add); vl_api_l2_patch_add_del_reply_t *rmp; int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, int is_add); int rv = 0; VALIDATE_RX_SW_IF_INDEX (mp); VALIDATE_TX_SW_IF_INDEX (mp); rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index), ntohl (mp->tx_sw_if_index), (int) (mp->is_add != 0)); BAD_RX_SW_IF_INDEX_LABEL; BAD_TX_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY); } static void vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp) { vl_api_sw_interface_set_vpath_reply_t *rmp; int rv = 0; u32 sw_if_index = ntohl (mp->sw_if_index); VALIDATE_SW_IF_INDEX (mp); l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable); vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4", sw_if_index, mp->enable, 0, 0); vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4", sw_if_index, mp->enable, 0, 0); vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6", sw_if_index, mp->enable, 0, 0); vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6", sw_if_index, mp->enable, 0, 0); BAD_SW_IF_INDEX_LABEL; REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY); } static void vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp) { vl_api_bvi_create_reply_t *rmp; mac_address_t mac; u32 sw_if_index; int rv; mac_address_decode (mp->mac, &mac); rv = l2_bvi_create (ntohl (mp->user_instance), &mac, &sw_if_index); /* *INDENT-OFF* */ REPLY_MACRO2(VL_API_BVI_CREATE_REPLY, ({ rmp->sw_if_index = ntohl (sw_if_index); })); /* *INDENT-ON* */ } static void vl_api_bvi_delete_t_handler (vl_api_bvi_delete_t * mp) { vl_api_bvi_delete_reply_t *rmp; int rv; rv = l2_bvi_delete (ntohl (mp->sw_if_index)); REPLY_MACRO (VL_API_BVI_DELETE_REPLY); } static bool l2_arp_term_publish_event_is_equal (const l2_arp_term_publish_event_t * e1, const l2_arp_term_publish_event_t * e2) { if (e1 == NULL || e2 == NULL) return false; return (ip46_address_is_equal (&e1->ip, &e2->ip) && (e1->sw_if_index == e2->sw_if_index) && (mac_address_equal (&e1->mac, &e2->mac))); } static uword l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f) { /* These cross the longjmp boundary (vlib_process_wait_for_event) * and need to be volatile - to prevent them from being optimized into * a register - which could change during suspension */ volatile f64 last = vlib_time_now (vm); volatile l2_arp_term_publish_event_t last_event = { }; l2_arp_term_main_t *l2am = &l2_arp_term_main; while (1) { uword event_type = L2_ARP_TERM_EVENT_PUBLISH; vpe_client_registration_t *reg; f64 now; vlib_process_wait_for_event (vm); vlib_process_get_event_data (vm, &event_type); now = vlib_time_now (vm); if (event_type == L2_ARP_TERM_EVENT_PUBLISH) { l2_arp_term_publish_event_t *event; vec_foreach (event, l2am->publish_events) { /* dampen duplicate events - cast away volatile */ if (l2_arp_term_publish_event_is_equal (event, (l2_arp_term_publish_event_t *) & last_event) && (now - last) < 10.0) { continue; } last_event = *event; last = now; pool_foreach (reg, vpe_api_main.l2_arp_term_events_registrations) { vl_api_registration_t *vl_reg; vl_reg = vl_api_client_index_to_registration (reg->client_index); ALWAYS_ASSERT (vl_reg != NULL); if (vl_reg && vl_api_can_send_msg (vl_reg)) { vl_api_l2_arp_term_event_t *vevent; vevent = vl_msg_api_alloc (sizeof *vevent); clib_memset (vevent, 0, sizeof *vevent); vevent->_vl_msg_id = htons (REPLY_MSG_ID_BASE + VL_API_L2_ARP_TERM_EVENT); vevent->client_index = reg->client_index; vevent->pid = reg->client_pid; ip_address_encode (&event->ip, event->type, &vevent->ip); vevent->sw_if_index = htonl (event->sw_if_index); mac_address_encode (&event->mac, vevent->mac); vl_api_send_msg (vl_reg, (u8 *) vevent); } } } vec_reset_length (l2am->publish_events); } } return 0; } /* *INDENT-OFF* */ VLIB_REGISTER_NODE (l2_arp_term_process_node) = { .function = l2_arp_term_process, .type = VLIB_NODE_TYPE_PROCESS, .name = "l2-arp-term-publisher", }; /* *INDENT-ON* */ static void vl_api_want_l2_arp_term_events_t_handler (vl_api_want_l2_arp_term_events_t * mp) { vl_api_want_l2_arp_term_events_reply_t *rmp; vpe_api_main_t *am = &vpe_api_main; vpe_client_registration_t *rp; int rv = 0; uword *p; p = hash_get (am->l2_arp_term_events_registration_hash, mp->client_index); if (p) { if (mp->enable) { clib_warning ("pid %d: already enabled...", mp->pid); rv = VNET_API_ERROR_INVALID_REGISTRATION; goto reply; } else { rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]); pool_put (am->l2_arp_term_events_registrations, rp); hash_unset (am->l2_arp_term_events_registration_hash, mp->client_index); if (pool_elts (am->l2_arp_term_events_registrations) == 0) l2_arp_term_set_publisher_node (false); goto reply; } } if (mp->enable == 0) { clib_warning ("pid %d: already disabled...", mp->pid); rv = VNET_API_ERROR_INVALID_REGISTRATION; goto reply; } pool_get (am->l2_arp_term_events_registrations, rp); rp->client_index = mp->client_index; rp->client_pid = mp->pid; hash_set (am->l2_arp_term_events_registration_hash, rp->client_index, rp - am->l2_arp_term_events_registrations); l2_arp_term_set_publisher_node (true); reply: REPLY_MACRO (VL_API_WANT_L2_ARP_TERM_EVENTS_REPLY); } static clib_error_t * want_l2_arp_term_events_reaper (u32 client_index) { vpe_client_registration_t *rp; vpe_api_main_t *am; uword *p; am = &vpe_api_main; /* remove from the registration hash */ p = hash_get (am->l2_arp_term_events_registration_hash, client_index); if (p) { rp = pool_elt_at_index (am->l2_arp_term_events_registrations, p[0]); pool_put (am->l2_arp_term_events_registrations, rp); hash_unset (am->l2_arp_term_events_registration_hash, client_index); if (pool_elts (am->l2_arp_term_events_registrations) == 0) l2_arp_term_set_publisher_node (false); } return (NULL); } VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper); #include static clib_error_t * l2_api_hookup (vlib_main_t * vm) { api_main_t *am = vlibapi_get_main (); /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */ vl_api_set_msg_thread_safe (am, VL_API_BRIDGE_DOMAIN_DUMP, 1); /* * Set up the (msg_name, crc, message-id) table */ REPLY_MSG_ID_BASE = setup_message_id_table (); return 0; } VLIB_API_INIT_FUNCTION (l2_api_hookup); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */