l2: coverity woe in l2_api.c
[vpp.git] / src / vnet / l2 / l2_api.c
index 97b429d..01e4ed1 100644 (file)
@@ -58,7 +58,9 @@
   _ (L2FIB_FLUSH_INT, l2fib_flush_int)                                        \
   _ (L2FIB_FLUSH_BD, l2fib_flush_bd)                                          \
   _ (L2FIB_ADD_DEL, l2fib_add_del)                                            \
+  _ (L2FIB_SET_SCAN_DELAY, l2fib_set_scan_delay)                              \
   _ (WANT_L2_MACS_EVENTS, want_l2_macs_events)                                \
+  _ (WANT_L2_MACS_EVENTS2, want_l2_macs_events2)                              \
   _ (L2_FLAGS, l2_flags)                                                      \
   _ (SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)              \
   _ (SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)                  \
@@ -101,10 +103,8 @@ 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);
@@ -112,13 +112,13 @@ vl_api_l2_xconnect_dump_t_handler (vl_api_l2_xconnect_dump_t * mp)
     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);
-  }
+  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* */
 }
 
@@ -262,22 +262,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 +282,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 +346,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 +402,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)
 {
@@ -1153,30 +1207,27 @@ 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* */
+             {
+               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 (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);
        }