nat: Include platform specific headers on FreeBSD
[vpp.git] / src / vnet / l2 / l2_api.c
index c8690c6..035542d 100644 (file)
@@ -3,6 +3,7 @@
  * l2_api.c - layer 2 forwarding api
  *
  * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Copyright (c) 2022 Nordix Foundation.
  * 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:
 #include <vnet/ip/ip_types_api.h>
 #include <vnet/ethernet/ethernet_types_api.h>
 
-#include <vnet/vnet_msg_enum.h>
-
-#define vl_typedefs            /* define message structures */
-#include <vnet/vnet_all_api_h.h>
-#undef vl_typedefs
-
-#define vl_endianfun           /* define message structures */
-#include <vnet/vnet_all_api_h.h>
-#undef vl_endianfun
-
-#define vl_api_bridge_domain_details_t_endian vl_noop_handler
-#define vl_api_bridge_domain_details_t_print vl_noop_handler
-
-/* instantiate all the print functions we know about */
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
-#define vl_printfun
-#include <vnet/vnet_all_api_h.h>
-#undef vl_printfun
+#include <vnet/format_fns.h>
+#include <vnet/l2/l2.api_enum.h>
+#include <vnet/l2/l2.api_types.h>
 
+#define REPLY_MSG_ID_BASE l2input_main.msg_id_base
 #include <vlibapi/api_helper_macros.h>
 
-#define foreach_vpe_api_msg                                 \
-_(L2_XCONNECT_DUMP, l2_xconnect_dump)                       \
-_(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                   \
-_(L2_FIB_TABLE_DUMP, l2_fib_table_dump)                     \
-_(L2FIB_FLUSH_ALL, l2fib_flush_all)                         \
-_(L2FIB_FLUSH_INT, l2fib_flush_int)                         \
-_(L2FIB_FLUSH_BD, l2fib_flush_bd)                           \
-_(L2FIB_ADD_DEL, l2fib_add_del)                             \
-_(WANT_L2_MACS_EVENTS, want_l2_macs_events)                \
-_(L2_FLAGS, l2_flags)                                       \
-_(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)   \
-_(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)       \
-_(L2_PATCH_ADD_DEL, l2_patch_add_del)                          \
-_(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)             \
-_(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                         \
-_(BD_IP_MAC_FLUSH, bd_ip_mac_flush)                             \
-_(BD_IP_MAC_DUMP, bd_ip_mac_dump)                              \
-_(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del)                 \
-_(BRIDGE_DOMAIN_DUMP, bridge_domain_dump)                       \
-_(BRIDGE_FLAGS, bridge_flags)                                   \
-_(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite) \
-_(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite)   \
-_(BRIDGE_DOMAIN_SET_MAC_AGE, bridge_domain_set_mac_age)         \
-_(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)               \
-_(BVI_CREATE, bvi_create)                                       \
-_(BVI_DELETE, bvi_delete)                                       \
-_(WANT_L2_ARP_TERM_EVENTS, want_l2_arp_term_events)
-
 static void
 send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
                          u32 rx_sw_if_index, u32 tx_sw_if_index)
@@ -89,7 +48,7 @@ send_l2_xconnect_details (vl_api_registration_t * reg, u32 context,
 
   mp = vl_msg_api_alloc (sizeof (*mp));
   clib_memset (mp, 0, sizeof (*mp));
-  mp->_vl_msg_id = ntohs (VL_API_L2_XCONNECT_DETAILS);
+  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);
@@ -101,25 +60,21 @@ static void
 vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
 {
   vl_api_registration_t *reg;
-  vnet_main_t *vnm = vnet_get_main ();
-  vnet_interface_main_t *im = &vnm->interface_main;
   l2input_main_t *l2im = &l2input_main;
-  vnet_sw_interface_t *swif;
+  u32 sw_if_index;
   l2_input_config_t *config;
 
   reg = vl_api_client_index_to_registration (mp->client_index);
   if (!reg)
     return;
 
-  /* *INDENT-OFF* */
-  pool_foreach (swif, im->sw_interfaces,
-  ({
-    config = vec_elt_at_index (l2im->configs, swif->sw_if_index);
-    if (l2_input_is_xconnect(config))
-      send_l2_xconnect_details (reg, mp->context, swif->sw_if_index,
-                                config->output_sw_if_index);
-  }));
-  /* *INDENT-ON* */
+  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);
+    }
 }
 
 static void
@@ -144,7 +99,7 @@ send_l2fib_table_entry (vpe_api_main_t * am,
 
   mp = vl_msg_api_alloc (sizeof (*mp));
   clib_memset (mp, 0, sizeof (*mp));
-  mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_DETAILS);
+  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);
@@ -262,22 +217,18 @@ vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
 }
 
 static void
-vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t * mp)
+vl_api_want_l2_macs_events2_t_handler (vl_api_want_l2_macs_events2_t *mp)
 {
   int rv = 0;
-  vl_api_want_l2_macs_events_reply_t *rmp;
+  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);
-  u32 learn_limit = ntohl (mp->learn_limit);
 
   if (mp->enable_disable)
     {
-      if (lm->client_pid == 0)
+      if ((lm->client_pid == 0) || (lm->client_pid == pid))
        {
-         lm->client_pid = pid;
-         lm->client_index = mp->client_index;
-
          if (mp->max_macs_in_event)
            fm->max_macs_in_event = mp->max_macs_in_event * 10;
          else
@@ -286,23 +237,58 @@ vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t * mp)
              goto exit;
            }
 
-         if (mp->scan_delay)
-           fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
-         else
-           {
-             rv = VNET_API_ERROR_INVALID_VALUE;
-             goto exit;
-           }
+         /* if scan_delay was not set before */
+         if (fm->event_scan_delay == 0.0)
+           fm->event_scan_delay = (f64) (10) * 10e-3;
 
-         /* change learn limit and flush all learned MACs */
-         if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
-           lm->global_learn_limit = learn_limit;
-         else
+         lm->client_pid = pid;
+         lm->client_index = mp->client_index;
+         l2fib_flush_all_mac (vlib_get_main ());
+       }
+      else if (lm->client_pid != pid)
+       {
+         rv = VNET_API_ERROR_L2_MACS_EVENT_CLINET_PRESENT;
+         goto exit;
+       }
+    }
+  else if (lm->client_pid)
+    {
+      lm->client_pid = 0;
+      lm->client_index = 0;
+    }
+
+exit:
+  REPLY_MACRO (VL_API_WANT_L2_MACS_EVENTS2_REPLY);
+}
+
+static void
+vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t *mp)
+{
+  int rv = 0;
+  vl_api_want_l2_macs_events_reply_t *rmp;
+  l2learn_main_t *lm = &l2learn_main;
+  l2fib_main_t *fm = &l2fib_main;
+  u32 pid = ntohl (mp->pid);
+  u32 learn_limit = ntohl (mp->learn_limit);
+
+  if (mp->enable_disable)
+    {
+      if ((lm->client_pid == 0) || (lm->client_pid == pid))
+       {
+         if ((mp->max_macs_in_event == 0) || (mp->scan_delay == 0) ||
+             (learn_limit == 0) || (learn_limit > L2LEARN_DEFAULT_LIMIT))
            {
              rv = VNET_API_ERROR_INVALID_VALUE;
              goto exit;
            }
+         lm->client_pid = pid;
+         lm->client_index = mp->client_index;
 
+         fm->max_macs_in_event = mp->max_macs_in_event * 10;
+         fm->event_scan_delay = (f64) (mp->scan_delay) * 10e-3;
+
+         /* change learn limit and flush all learned MACs */
+         lm->global_learn_limit = learn_limit;
          l2fib_flush_all_mac (vlib_get_main ());
        }
       else if (lm->client_pid != pid)
@@ -315,7 +301,7 @@ vl_api_want_l2_macs_events_t_handler (vl_api_want_l2_macs_events_t * mp)
     {
       lm->client_pid = 0;
       lm->client_index = 0;
-      if (learn_limit && (learn_limit < L2LEARN_DEFAULT_LIMIT))
+      if (learn_limit && (learn_limit <= L2LEARN_DEFAULT_LIMIT))
        lm->global_learn_limit = learn_limit;
       else
        lm->global_learn_limit = L2LEARN_DEFAULT_LIMIT;
@@ -371,6 +357,29 @@ out:
   REPLY_MACRO (VL_API_L2FIB_FLUSH_BD_REPLY);
 }
 
+static void
+vl_api_l2fib_set_scan_delay_t_handler (vl_api_l2fib_set_scan_delay_t *mp)
+{
+  int rv = 0;
+  l2fib_main_t *fm = &l2fib_main;
+  vl_api_l2fib_set_scan_delay_reply_t *rmp;
+  u16 scan_delay = ntohs (mp->scan_delay);
+
+  if (mp->scan_delay)
+    {
+      fm->event_scan_delay = (f64) (scan_delay) *10e-3;
+      l2fib_flush_all_mac (vlib_get_main ());
+    }
+  else
+    {
+      rv = VNET_API_ERROR_INVALID_VALUE;
+      goto exit;
+    }
+
+exit:
+  REPLY_MACRO (VL_API_L2FIB_SET_SCAN_DELAY_REPLY);
+}
+
 static void
 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
 {
@@ -403,12 +412,49 @@ vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
 
   BAD_SW_IF_INDEX_LABEL;
 
-  /* *INDENT-OFF* */
   REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
   ({
     rmp->resulting_feature_bitmap = ntohl(rbm);
   }));
-  /* *INDENT-ON* */
+}
+
+static void
+vl_api_bridge_domain_set_default_learn_limit_t_handler (
+  vl_api_bridge_domain_set_default_learn_limit_t *mp)
+{
+  vl_api_bridge_domain_set_default_learn_limit_reply_t *rmp;
+  int rv = 0;
+
+  l2learn_main.bd_default_learn_limit = ntohl (mp->learn_limit);
+  REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_DEFAULT_LEARN_LIMIT_REPLY);
+}
+
+static void
+vl_api_bridge_domain_set_learn_limit_t_handler (
+  vl_api_bridge_domain_set_learn_limit_t *mp)
+{
+  vlib_main_t *vm = vlib_get_main ();
+  bd_main_t *bdm = &bd_main;
+  vl_api_bridge_domain_set_learn_limit_reply_t *rmp;
+  int rv = 0;
+  u32 bd_id = ntohl (mp->bd_id);
+  uword *p;
+
+  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_set_learn_limit (vm, *p, ntohl (mp->learn_limit));
+out:
+  REPLY_MACRO (VL_API_BRIDGE_DOMAIN_SET_LEARN_LIMIT_REPLY);
 }
 
 static void
@@ -461,6 +507,37 @@ vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
 }
 
+static void
+vl_api_bridge_domain_add_del_v2_t_handler (
+  vl_api_bridge_domain_add_del_v2_t *mp)
+{
+  vl_api_bridge_domain_add_del_v2_reply_t *rmp;
+  u32 bd_id = ntohl (mp->bd_id);
+  int rv = 0;
+
+  if ((~0 == bd_id) && (mp->is_add))
+    bd_id = bd_get_unused_id ();
+
+  if ((~0 == bd_id) && (mp->is_add))
+    rv = VNET_API_ERROR_EAGAIN;
+  else
+    {
+      l2_bridge_domain_add_del_args_t a = { .is_add = mp->is_add,
+                                           .flood = mp->flood,
+                                           .uu_flood = mp->uu_flood,
+                                           .forward = mp->forward,
+                                           .learn = mp->learn,
+                                           .arp_term = mp->arp_term,
+                                           .arp_ufwd = mp->arp_ufwd,
+                                           .mac_age = mp->mac_age,
+                                           .bd_id = bd_id,
+                                           .bd_tag = mp->bd_tag };
+      rv = bd_add_del (&a);
+    }
+  REPLY_MACRO2 (VL_API_BRIDGE_DOMAIN_ADD_DEL_V2_REPLY,
+               ({ rmp->bd_id = htonl (bd_id); }));
+}
+
 static void
 send_bridge_domain_details (l2input_main_t * l2im,
                            vl_api_registration_t * reg,
@@ -475,7 +552,7 @@ send_bridge_domain_details (l2input_main_t * l2im,
   mp = vl_msg_api_alloc (sizeof (*mp) +
                         (n_sw_ifs * sizeof (vl_api_bridge_domain_sw_if_t)));
   clib_memset (mp, 0, sizeof (*mp));
-  mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
+  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BRIDGE_DOMAIN_DETAILS);
   mp->bd_id = ntohl (bd_config->bd_id);
   mp->flood = bd_feature_flood (bd_config);
   mp->uu_flood = bd_feature_uu_flood (bd_config);
@@ -602,12 +679,10 @@ vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
   bitmap = bd_set_flags (vm, bd_index, flags, mp->is_set);
 
 out:
-  /* *INDENT-OFF* */
   REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
   ({
     rmp->resulting_feature_bitmap = ntohl(bitmap);
   }));
-  /* *INDENT-ON* */
 }
 
 static void
@@ -814,7 +889,7 @@ send_bd_ip_mac_entry (vpe_api_main_t * am,
 
   mp = vl_msg_api_alloc (sizeof (*mp));
   clib_memset (mp, 0, sizeof (*mp));
-  mp->_vl_msg_id = ntohs (VL_API_BD_IP_MAC_DETAILS);
+  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_BD_IP_MAC_DETAILS);
 
   mp->context = context;
   mp->entry.bd_id = ntohl (bd_id);
@@ -869,7 +944,6 @@ vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
          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 = {
@@ -891,7 +965,6 @@ vl_api_bd_ip_mac_dump_t_handler (vl_api_bd_ip_mac_dump_t * mp)
            send_bd_ip_mac_entry (am, reg, bd_id, &ip, IP46_TYPE_IP6,
                                  &mac, mp->context);
          }));
-         /* *INDENT-ON* */
        }
     }
 }
@@ -1045,12 +1118,10 @@ vl_api_bvi_create_t_handler (vl_api_bvi_create_t * mp)
 
   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
@@ -1114,30 +1185,28 @@ l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
            last_event = *event;
            last = now;
 
-            /* *INDENT-OFF* */
-            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 (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 (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);
-                }
-            }));
-            /* *INDENT-ON* */
+            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);
        }
@@ -1146,13 +1215,11 @@ l2_arp_term_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
   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 *
@@ -1227,47 +1294,20 @@ want_l2_arp_term_events_reaper (u32 client_index)
 
 VL_MSG_API_REAPER_FUNCTION (want_l2_arp_term_events_reaper);
 
-/*
- * l2_api_hookup
- * Add vpe's API message handlers to the table.
- * vlib has already mapped shared memory and
- * added the client registration handlers.
- * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
- */
-#define vl_msg_name_crc_list
-#include <vnet/vnet_all_api_h.h>
-#undef vl_msg_name_crc_list
-
-static void
-setup_message_id_table (api_main_t * am)
-{
-#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
-  foreach_vl_msg_name_crc_l2;
-#undef _
-}
-
+#include <vnet/l2/l2.api.c>
 static clib_error_t *
 l2_api_hookup (vlib_main_t * vm)
 {
   api_main_t *am = vlibapi_get_main ();
 
-#define _(N,n)                                                  \
-    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
-                           vl_api_##n##_t_handler,              \
-                           vl_noop_handler,                     \
-                           vl_api_##n##_t_endian,               \
-                           vl_api_##n##_t_print,                \
-                           sizeof(vl_api_##n##_t), 1);
-  foreach_vpe_api_msg;
-#undef _
-
-  /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
-  am->is_mp_safe[VL_API_BRIDGE_DOMAIN_DUMP] = 1;
-
   /*
    * Set up the (msg_name, crc, message-id) table
    */
-  setup_message_id_table (am);
+  REPLY_MSG_ID_BASE = setup_message_id_table ();
+
+  /* Mark VL_API_BRIDGE_DOMAIN_DUMP as mp safe */
+  vl_api_set_msg_thread_safe (
+    am, REPLY_MSG_ID_BASE + VL_API_BRIDGE_DOMAIN_DUMP, 1);
 
   return 0;
 }