bfd:fix handling session creation batch
[vpp.git] / src / vnet / bfd / bfd_udp.c
index 06b843c..ab530ed 100644 (file)
@@ -59,6 +59,8 @@ typedef struct
   u32 ip4_rewrite_idx;
   /* node index of "ip6-rewrite" node */
   u32 ip6_rewrite_idx;
+  /* log class */
+  vlib_log_class_t log_class;
 } bfd_udp_main_t;
 
 static vlib_node_registration_t bfd_udp4_input_node;
@@ -246,7 +248,7 @@ bfd_add_udp4_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
   const bfd_udp_key_t *key = &bus->key;
   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
 
-  b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
+  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
   vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
   vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
   vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
@@ -301,7 +303,7 @@ bfd_add_udp6_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
   const bfd_udp_key_t *key = &bus->key;
   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
 
-  b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
+  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
   vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
   vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
   vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
@@ -471,7 +473,6 @@ bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index,
   bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
   if (!bs)
     {
-      bfd_put_session (bum->bfd_main, bs);
       return VNET_API_ERROR_BFD_EAGAIN;
     }
   bfd_udp_session_t *bus = &bs->udp;
@@ -481,7 +482,8 @@ bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index,
   const bfd_session_t *tmp = bfd_lookup_session (bum, key);
   if (tmp)
     {
-      clib_warning ("duplicate bfd-udp session, existing bs_idx=%d",
+      vlib_log_err (bum->log_class,
+                   "duplicate bfd-udp session, existing bs_idx=%d",
                    tmp->bs_idx);
       bfd_put_session (bum->bfd_main, bs);
       return VNET_API_ERROR_BFD_EEXIST;
@@ -491,6 +493,8 @@ bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index,
           bs->bs_idx, key->sw_if_index, format_ip46_address,
           &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
           &key->peer_addr, IP46_TYPE_ANY);
+  vlib_log_info (bum->log_class, "create BFD session: %U",
+                format_bfd_session, bs);
   if (BFD_TRANSPORT_UDP4 == t)
     {
       bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
@@ -519,20 +523,24 @@ bfd_udp_validate_api_input (u32 sw_if_index,
                            const ip46_address_t * local_addr,
                            const ip46_address_t * peer_addr)
 {
+  bfd_udp_main_t *bum = &bfd_udp_main;
   vnet_sw_interface_t *sw_if =
     vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
   u8 local_ip_valid = 0;
   ip_interface_address_t *ia = NULL;
   if (!sw_if)
     {
-      clib_warning ("got NULL sw_if");
+      vlib_log_err (bum->log_class,
+                   "got NULL sw_if when getting interface by index %u",
+                   sw_if_index);
       return VNET_API_ERROR_INVALID_SW_IF_INDEX;
     }
   if (ip46_address_is_ip4 (local_addr))
     {
       if (!ip46_address_is_ip4 (peer_addr))
        {
-         clib_warning ("IP family mismatch");
+         vlib_log_err (bum->log_class,
+                       "IP family mismatch (local is ipv4, peer is ipv6)");
          return VNET_API_ERROR_INVALID_ARGUMENT;
        }
       ip4_main_t *im = &ip4_main;
@@ -555,7 +563,8 @@ bfd_udp_validate_api_input (u32 sw_if_index,
     {
       if (ip46_address_is_ip4 (peer_addr))
        {
-         clib_warning ("IP family mismatch");
+         vlib_log_err (bum->log_class,
+                       "IP family mismatch (local is ipv6, peer is ipv4)");
          return VNET_API_ERROR_INVALID_ARGUMENT;
        }
       ip6_main_t *im = &ip6_main;
@@ -577,7 +586,10 @@ bfd_udp_validate_api_input (u32 sw_if_index,
 
   if (!local_ip_valid)
     {
-      clib_warning ("address not found on interface");
+      vlib_log_err (bum->log_class,
+                   "local address %U not found on interface with index %u",
+                   format_ip46_address, local_addr, IP46_TYPE_ANY,
+                   sw_if_index);
       return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
     }
 
@@ -604,10 +616,11 @@ bfd_udp_find_session_by_api_input (u32 sw_if_index,
        }
       else
        {
-         clib_warning
-           ("BFD session not found (sw_if_index=%u, local=%U, peer=%U",
-            sw_if_index, format_ip46_address, local_addr, IP46_TYPE_ANY,
-            format_ip46_address, peer_addr, IP46_TYPE_ANY);
+         vlib_log_err (bum->log_class,
+                       "BFD session not found, sw_if_index=%u, local=%U, peer=%U",
+                       sw_if_index, format_ip46_address, local_addr,
+                       IP46_TYPE_ANY, format_ip46_address, peer_addr,
+                       IP46_TYPE_ANY);
          return VNET_API_ERROR_BFD_ENOENT;
        }
     }
@@ -620,6 +633,7 @@ bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
                       const ip46_address_t * local_addr,
                       const ip46_address_t * peer_addr)
 {
+  bfd_udp_main_t *bum = &bfd_udp_main;
   vnet_api_error_t rv =
     bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
   if (rv)
@@ -628,12 +642,12 @@ bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
     }
   if (detect_mult < 1)
     {
-      clib_warning ("detect_mult < 1");
+      vlib_log_err (bum->log_class, "detect_mult < 1");
       return VNET_API_ERROR_INVALID_ARGUMENT;
     }
   if (desired_min_tx_usec < 1)
     {
-      clib_warning ("desired_min_tx_usec < 1");
+      vlib_log_err (bum->log_class, "desired_min_tx_usec < 1");
       return VNET_API_ERROR_INVALID_ARGUMENT;
     }
   return 0;
@@ -656,6 +670,9 @@ bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
                     u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
                     u8 bfd_key_id)
 {
+  bfd_main_t *bm = &bfd_main;
+  bfd_lock (bm);
+
   vnet_api_error_t rv =
     bfd_api_verify_common (sw_if_index, desired_min_tx_usec,
                           required_min_rx_usec, detect_mult,
@@ -675,7 +692,8 @@ bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
       rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
                              0 /* is not delayed */ );
 #else
-      clib_warning ("SSL missing, cannot add authenticated BFD session");
+      vlib_log_err (bfd_udp_main.log_class,
+                   "SSL missing, cannot add authenticated BFD session");
       rv = VNET_API_ERROR_BFD_NOTSUPP;
 #endif
       if (rv)
@@ -688,6 +706,7 @@ bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
       bfd_session_start (bfd_udp_main.bfd_main, bs);
     }
 
+  bfd_unlock (bm);
   return rv;
 }
 
@@ -699,17 +718,23 @@ bfd_udp_mod_session (u32 sw_if_index,
                     u32 required_min_rx_usec, u8 detect_mult)
 {
   bfd_session_t *bs = NULL;
+  bfd_main_t *bm = &bfd_main;
+  vnet_api_error_t error;
+  bfd_lock (bm);
   vnet_api_error_t rv =
     bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
                                       &bs);
   if (rv)
     {
+      bfd_unlock (bm);
       return rv;
     }
 
-  return bfd_session_set_params (bfd_udp_main.bfd_main, bs,
-                                desired_min_tx_usec, required_min_rx_usec,
-                                detect_mult);
+  error = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
+                                 desired_min_tx_usec, required_min_rx_usec,
+                                 detect_mult);
+  bfd_unlock (bm);
+  return error;
 }
 
 vnet_api_error_t
@@ -718,14 +743,18 @@ bfd_udp_del_session (u32 sw_if_index,
                     const ip46_address_t * peer_addr)
 {
   bfd_session_t *bs = NULL;
+  bfd_main_t *bm = &bfd_main;
+  bfd_lock (bm);
   vnet_api_error_t rv =
     bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
                                       &bs);
   if (rv)
     {
+      bfd_unlock (bm);
       return rv;
     }
   bfd_udp_del_session_internal (bs);
+  bfd_unlock (bm);
   return 0;
 }
 
@@ -735,14 +764,18 @@ bfd_udp_session_set_flags (u32 sw_if_index,
                           const ip46_address_t * peer_addr, u8 admin_up_down)
 {
   bfd_session_t *bs = NULL;
+  bfd_main_t *bm = &bfd_main;
+  bfd_lock (bm);
   vnet_api_error_t rv =
     bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
                                       &bs);
   if (rv)
     {
+      bfd_unlock (bm);
       return rv;
     }
   bfd_session_set_flags (bs, admin_up_down);
+  bfd_unlock (bm);
   return 0;
 }
 
@@ -752,6 +785,10 @@ bfd_udp_auth_activate (u32 sw_if_index,
                       const ip46_address_t * peer_addr,
                       u32 conf_key_id, u8 key_id, u8 is_delayed)
 {
+  bfd_main_t *bm = &bfd_main;
+  bfd_lock (bm);
+  vnet_api_error_t error;
+
 #if WITH_LIBSSL > 0
   bfd_session_t *bs = NULL;
   vnet_api_error_t rv =
@@ -759,11 +796,16 @@ bfd_udp_auth_activate (u32 sw_if_index,
                                       &bs);
   if (rv)
     {
+      bfd_unlock (bm);
       return rv;
     }
-  return bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
+  error = bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
+  bfd_unlock (bm);
+  return error;
 #else
-  clib_warning ("SSL missing, cannot activate BFD authentication");
+  vlib_log_err (bfd_udp_main->log_class,
+               "SSL missing, cannot activate BFD authentication");
+  bfd_unlock (bm);
   return VNET_API_ERROR_BFD_NOTSUPP;
 #endif
 }
@@ -773,15 +815,21 @@ bfd_udp_auth_deactivate (u32 sw_if_index,
                         const ip46_address_t * local_addr,
                         const ip46_address_t * peer_addr, u8 is_delayed)
 {
+  bfd_main_t *bm = &bfd_main;
+  vnet_api_error_t error;
+  bfd_lock (bm);
   bfd_session_t *bs = NULL;
   vnet_api_error_t rv =
     bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
                                       &bs);
   if (rv)
     {
+      bfd_unlock (bm);
       return rv;
     }
-  return bfd_auth_deactivate (bs, is_delayed);
+  error = bfd_auth_deactivate (bs, is_delayed);
+  bfd_unlock (bm);
+  return error;
 }
 
 typedef enum
@@ -903,23 +951,13 @@ typedef struct
   bfd_pkt_t pkt;
 } bfd_rpc_update_t;
 
-static void
-bfd_rpc_update_session_cb (const bfd_rpc_update_t * a)
-{
-  bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx);
-}
-
 static void
 bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
 {
-  /* packet length was already verified to be correct by the caller */
-  const u32 data_size = sizeof (bfd_rpc_update_t) -
-    STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) + pkt->head.length;
-  u8 data[data_size];
-  bfd_rpc_update_t *update = (bfd_rpc_update_t *) data;
-  update->bs_idx = bs_idx;
-  clib_memcpy (&update->pkt, pkt, pkt->head.length);
-  vl_api_rpc_call_main_thread (bfd_rpc_update_session_cb, data, data_size);
+  bfd_main_t *bm = &bfd_main;
+  bfd_lock (bm);
+  bfd_consume_pkt (bm, pkt, bs_idx);
+  bfd_unlock (bm);
 }
 
 static bfd_udp_error_t
@@ -1149,6 +1187,7 @@ bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
 {
   u32 n_left_from, *from;
   bfd_input_trace_t *t0;
+  bfd_main_t *bm = &bfd_main;
 
   from = vlib_frame_vector_args (f);   /* array of buffer indices */
   n_left_from = f->n_vectors;  /* number of buffer indices */
@@ -1176,6 +1215,7 @@ bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
        }
 
       /* scan this bfd pkt. error0 is the counter index to bmp */
+      bfd_lock (bm);
       if (is_ipv6)
        {
          error0 = bfd_udp6_scan (vm, rt, b0, &bs);
@@ -1228,6 +1268,7 @@ bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
                }
            }
        }
+      bfd_unlock (bm);
       vlib_set_next_frame_buffer (vm, rt, next0, bi0);
 
       from += 1;
@@ -1306,6 +1347,7 @@ bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
 {
   u32 n_left_from, *from;
   bfd_input_trace_t *t0;
+  bfd_main_t *bm = &bfd_main;
 
   from = vlib_frame_vector_args (f);   /* array of buffer indices */
   n_left_from = f->n_vectors;  /* number of buffer indices */
@@ -1330,6 +1372,7 @@ bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
          clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
        }
 
+      bfd_lock (bm);
       if (bfd_consume_echo_pkt (bfd_udp_main.bfd_main, b0))
        {
          b0->error = rt->errors[BFD_UDP_ERROR_NONE];
@@ -1352,6 +1395,7 @@ bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
          next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
        }
 
+      bfd_unlock (bm);
       vlib_set_next_frame_buffer (vm, rt, next0, bi0);
 
       from += 1;
@@ -1443,6 +1487,7 @@ static clib_error_t *
 bfd_udp_sw_if_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_create)
 {
   bfd_session_t **to_be_freed = NULL;
+  bfd_udp_main_t *bum = &bfd_udp_main;
   BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index,
           is_create);
   if (!is_create)
@@ -1463,8 +1508,9 @@ bfd_udp_sw_if_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_create)
   bfd_session_t **bs;
   vec_foreach (bs, to_be_freed)
   {
-    clib_warning ("removal of sw_if_index=%u forces removal of bfd session "
-                 "with bs_idx=%u", sw_if_index, (*bs)->bs_idx);
+    vlib_log_notice (bum->log_class,
+                    "removal of sw_if_index=%u forces removal of bfd session "
+                    "with bs_idx=%u", sw_if_index, (*bs)->bs_idx);
     bfd_session_set_flags (*bs, 0);
     bfd_udp_del_session_internal (*bs);
   }
@@ -1502,6 +1548,8 @@ bfd_udp_init (vlib_main_t * vm)
   ASSERT (node);
   bfd_udp_main.ip6_rewrite_idx = node->index;
 
+  bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp");
+  vlib_log_debug (bfd_udp_main.log_class, "initialized");
   return 0;
 }