LCOV - code coverage report
Current view: top level - vnet/ip-neighbor - ip_neighbor_api.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 143 153 93.5 %
Date: 2023-10-26 01:39:38 Functions: 17 17 100.0 %

          Line data    Source code
       1             : /*
       2             :  *------------------------------------------------------------------
       3             :  * ip_api.c - vnet ip api
       4             :  *
       5             :  * Copyright (c) 2016 Cisco and/or its affiliates.
       6             :  * Licensed under the Apache License, Version 2.0 (the "License");
       7             :  * you may not use this file except in compliance with the License.
       8             :  * You may obtain a copy of the License at:
       9             :  *
      10             :  *     http://www.apache.org/licenses/LICENSE-2.0
      11             :  *
      12             :  * Unless required by applicable law or agreed to in writing, software
      13             :  * distributed under the License is distributed on an "AS IS" BASIS,
      14             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      15             :  * See the License for the specific language governing permissions and
      16             :  * limitations under the License.
      17             :  *------------------------------------------------------------------
      18             :  */
      19             : 
      20             : #include <stddef.h>
      21             : 
      22             : #include <vnet/ip-neighbor/ip_neighbor.h>
      23             : #include <vnet/ip-neighbor/ip_neighbor_watch.h>
      24             : #include <vnet/ip/ip_types_api.h>
      25             : #include <vnet/ethernet/ethernet_types_api.h>
      26             : 
      27             : #include <vlibapi/api.h>
      28             : #include <vlibmemory/api.h>
      29             : 
      30             : #include <vnet/ip-neighbor/ip_neighbor.api_enum.h>
      31             : #include <vnet/ip-neighbor/ip_neighbor.api_types.h>
      32             : 
      33             : static u16 msg_id_base;
      34             : #define REPLY_MSG_ID_BASE msg_id_base
      35             : 
      36             : #include <vlibapi/api_helper_macros.h>
      37             : 
      38             : #include <vnet/format_fns.h>
      39             : 
      40             : static vl_api_ip_neighbor_flags_t
      41        5666 : ip_neighbor_flags_encode (ip_neighbor_flags_t f)
      42             : {
      43        5666 :   vl_api_ip_neighbor_flags_t v = IP_API_NEIGHBOR_FLAG_NONE;
      44             : 
      45        5666 :   if (f & IP_NEIGHBOR_FLAG_STATIC)
      46         530 :     v |= IP_API_NEIGHBOR_FLAG_STATIC;
      47        5666 :   if (f & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY)
      48           4 :     v |= IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY;
      49             : 
      50        5666 :   return (v);
      51             : }
      52             : 
      53             : static void
      54        5666 : ip_neighbor_encode (vl_api_ip_neighbor_t * api, const ip_neighbor_t * ipn)
      55             : {
      56        5666 :   api->sw_if_index = htonl (ipn->ipn_key->ipnk_sw_if_index);
      57        5666 :   api->flags = ip_neighbor_flags_encode (ipn->ipn_flags);
      58             : 
      59        5666 :   ip_address_encode2 (&ipn->ipn_key->ipnk_ip, &api->ip_address);
      60        5666 :   mac_address_encode (&ipn->ipn_mac, api->mac_address);
      61        5666 : }
      62             : 
      63             : void
      64          46 : ip_neighbor_handle_event (ip_neighbor_event_t * ipne)
      65             : {
      66             :   vl_api_registration_t *reg;
      67             :   ip_neighbor_t *ipn;
      68             : 
      69          46 :   ipn = &ipne->ipne_nbr;
      70             : 
      71          46 :   if (NULL == ipn)
      72             :     /* Client can cancel, die, etc. */
      73           0 :     return;
      74             : 
      75             :   /* Customer(s) requesting event for this neighbor */
      76          46 :   reg = vl_api_client_index_to_registration (ipne->ipne_watch.ipw_client);
      77          46 :   if (!reg)
      78           0 :     return;
      79             : 
      80          46 :   if (vl_api_can_send_msg (reg))
      81             :     {
      82          46 :       if (1 == ipne->ipne_watch.ipw_api_version)
      83             :         {
      84             :           vl_api_ip_neighbor_event_t *mp;
      85             : 
      86           5 :           mp = vl_msg_api_alloc (sizeof (*mp));
      87           5 :           clib_memset (mp, 0, sizeof (*mp));
      88           5 :           mp->_vl_msg_id =
      89           5 :             ntohs (VL_API_IP_NEIGHBOR_EVENT + REPLY_MSG_ID_BASE);
      90           5 :           mp->client_index = ipne->ipne_watch.ipw_client;
      91           5 :           mp->pid = ipne->ipne_watch.ipw_pid;
      92             : 
      93           5 :           ip_neighbor_encode (&mp->neighbor, ipn);
      94             : 
      95           5 :           vl_api_send_msg (reg, (u8 *) mp);
      96             :         }
      97          41 :       else if (2 == ipne->ipne_watch.ipw_api_version)
      98             :         {
      99             :           vl_api_ip_neighbor_event_v2_t *mp;
     100             : 
     101          41 :           mp = vl_msg_api_alloc (sizeof (*mp));
     102          41 :           clib_memset (mp, 0, sizeof (*mp));
     103          41 :           mp->_vl_msg_id =
     104          41 :             ntohs (VL_API_IP_NEIGHBOR_EVENT_V2 + REPLY_MSG_ID_BASE);
     105          41 :           mp->client_index = ipne->ipne_watch.ipw_client;
     106          41 :           mp->pid = ipne->ipne_watch.ipw_pid;
     107          41 :           mp->flags = clib_host_to_net_u32 (ipne->ipne_flags);
     108             : 
     109          41 :           ip_neighbor_encode (&mp->neighbor, ipn);
     110             : 
     111          41 :           vl_api_send_msg (reg, (u8 *) mp);
     112             :         }
     113             :     }
     114             :   else
     115             :     {
     116             :       static f64 last_time;
     117             :       /*
     118             :        * Throttle syslog msgs.
     119             :        * It's pretty tempting to just revoke the registration...
     120             :        */
     121           0 :       if (vlib_time_now (vlib_get_main ()) > last_time + 10.0)
     122             :         {
     123           0 :           clib_warning ("neighbor event for %U to pid %d: queue stuffed!",
     124             :                         format_ip_address, &ipn->ipn_key->ipnk_ip,
     125             :                         ipne->ipne_watch.ipw_pid);
     126           0 :           last_time = vlib_time_now (vlib_get_main ());
     127             :         }
     128             :     }
     129             : 
     130          46 :   ip_neighbor_free (ipn);
     131             : }
     132             : 
     133             : typedef struct ip_neighbor_dump_ctx_t_
     134             : {
     135             :   vl_api_registration_t *reg;
     136             :   u32 context;
     137             : } ip_neighbor_dump_ctx_t;
     138             : 
     139             : static walk_rc_t
     140        5620 : send_ip_neighbor_details (index_t ipni, void *arg)
     141             : {
     142        5620 :   ip_neighbor_dump_ctx_t *ctx = arg;
     143             :   vl_api_ip_neighbor_details_t *mp;
     144             :   ip_neighbor_t *ipn;
     145             : 
     146        5620 :   ipn = ip_neighbor_get (ipni);
     147        5620 :   mp = vl_msg_api_alloc (sizeof (*mp));
     148        5620 :   clib_memset (mp, 0, sizeof (*mp));
     149        5620 :   mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS + REPLY_MSG_ID_BASE);
     150        5620 :   mp->context = ctx->context;
     151        5620 :   mp->age =
     152        5620 :     clib_host_to_net_f64 ((vlib_time_now (vlib_get_main ()) -
     153        5620 :                            ipn->ipn_time_last_updated));
     154        5620 :   ip_neighbor_encode (&mp->neighbor, ipn);
     155             : 
     156        5620 :   vl_api_send_msg (ctx->reg, (u8 *) mp);
     157             : 
     158        5620 :   return (WALK_CONTINUE);
     159             : }
     160             : 
     161             : static void
     162         984 : vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
     163             : {
     164             :   vl_api_registration_t *reg;
     165             :   ip_address_family_t af;
     166             :   int rv;
     167             : 
     168         984 :   reg = vl_api_client_index_to_registration (mp->client_index);
     169         984 :   if (!reg)
     170           0 :     return;
     171             : 
     172         984 :   u32 sw_if_index = ntohl (mp->sw_if_index);
     173             : 
     174         984 :   rv = ip_address_family_decode (mp->af, &af);
     175             : 
     176         984 :   if (rv)
     177           0 :     return;
     178             : 
     179         984 :   ip_neighbor_dump_ctx_t ctx = {
     180             :     .reg = reg,
     181         984 :     .context = mp->context,
     182             :   };
     183             : 
     184             :   // walk all neighbours on all interfaces
     185         984 :   ip_neighbor_walk (af, sw_if_index, send_ip_neighbor_details, &ctx);
     186             : }
     187             : 
     188             : static ip_neighbor_flags_t
     189        5936 : ip_neighbor_flags_decode (vl_api_ip_neighbor_flags_t v)
     190             : {
     191        5936 :   ip_neighbor_flags_t f = IP_NEIGHBOR_FLAG_NONE;
     192             : 
     193        5936 :   if (v & IP_API_NEIGHBOR_FLAG_STATIC)
     194          86 :     f |= IP_NEIGHBOR_FLAG_STATIC;
     195        5936 :   if (v & IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY)
     196           3 :     f |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
     197             : 
     198        5936 :   return (f);
     199             : }
     200             : 
     201             : static void
     202        5936 : vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
     203             :                                       vlib_main_t * vm)
     204             : {
     205             :   vl_api_ip_neighbor_add_del_reply_t *rmp;
     206             :   ip_neighbor_flags_t flags;
     207        5936 :   u32 stats_index = ~0;
     208        5936 :   ip_address_t ip = ip_address_initializer;
     209             :   mac_address_t mac;
     210             :   int rv;
     211             : 
     212        5936 :   VALIDATE_SW_IF_INDEX ((&mp->neighbor));
     213             : 
     214        5936 :   flags = ip_neighbor_flags_decode (mp->neighbor.flags);
     215        5936 :   ip_address_decode2 (&mp->neighbor.ip_address, &ip);
     216        5936 :   mac_address_decode (mp->neighbor.mac_address, &mac);
     217             : 
     218             :   /* must be static or dynamic, default to dynamic */
     219        5936 :   if (!(flags & IP_NEIGHBOR_FLAG_STATIC) &&
     220        5850 :       !(flags & IP_NEIGHBOR_FLAG_DYNAMIC))
     221        5850 :     flags |= IP_NEIGHBOR_FLAG_DYNAMIC;
     222             : 
     223             :   /*
     224             :    * there's no validation here of the ND/ARP entry being added.
     225             :    * The expectation is that the FIB will ensure that nothing bad
     226             :    * will come of adding bogus entries.
     227             :    */
     228        5936 :   if (mp->is_add)
     229        5835 :     rv = ip_neighbor_add (&ip, &mac,
     230             :                           ntohl (mp->neighbor.sw_if_index),
     231             :                           flags, &stats_index);
     232             :   else
     233         101 :     rv = ip_neighbor_del (&ip, ntohl (mp->neighbor.sw_if_index));
     234             : 
     235        5936 :   BAD_SW_IF_INDEX_LABEL;
     236             : 
     237             :   /* *INDENT-OFF* */
     238        5936 :   REPLY_MACRO2 (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY,
     239             :   ({
     240             :     rmp->stats_index = htonl (stats_index);
     241             :   }));
     242             :   /* *INDENT-ON* */
     243             : }
     244             : 
     245             : static void
     246           6 : vl_api_want_ip_neighbor_events_t_handler (vl_api_want_ip_neighbor_events_t *
     247             :                                           mp)
     248             : {
     249             :   vl_api_want_ip_neighbor_events_reply_t *rmp;
     250             :   ip_address_t ip;
     251           6 :   int rv = 0;
     252             : 
     253           6 :   if (mp->sw_if_index != ~0)
     254           4 :     VALIDATE_SW_IF_INDEX (mp);
     255           6 :   ip_address_decode2 (&mp->ip, &ip);
     256             : 
     257           6 :   ip_neighbor_watcher_t watch = {
     258           6 :     .ipw_client = mp->client_index,
     259           6 :     .ipw_pid = mp->pid,
     260             :     .ipw_api_version = 1,
     261             :   };
     262             : 
     263           6 :   if (mp->enable)
     264           3 :     ip_neighbor_watch (&ip, ntohl (mp->sw_if_index), &watch);
     265             :   else
     266           3 :     ip_neighbor_unwatch (&ip, ntohl (mp->sw_if_index), &watch);
     267             : 
     268           6 :   BAD_SW_IF_INDEX_LABEL;
     269           6 :   REPLY_MACRO (VL_API_WANT_IP_NEIGHBOR_EVENTS_REPLY);
     270             : }
     271             : 
     272             : static void
     273           1 :   vl_api_want_ip_neighbor_events_v2_t_handler
     274             :   (vl_api_want_ip_neighbor_events_v2_t * mp)
     275             : {
     276             :   vl_api_want_ip_neighbor_events_reply_t *rmp;
     277             :   ip_address_t ip;
     278           1 :   int rv = 0;
     279             : 
     280           1 :   if (mp->sw_if_index != ~0)
     281           0 :     VALIDATE_SW_IF_INDEX (mp);
     282           1 :   ip_address_decode2 (&mp->ip, &ip);
     283             : 
     284           1 :   ip_neighbor_watcher_t watch = {
     285           1 :     .ipw_client = mp->client_index,
     286           1 :     .ipw_pid = mp->pid,
     287             :     .ipw_api_version = 2,
     288             :   };
     289             : 
     290           1 :   if (mp->enable)
     291           1 :     ip_neighbor_watch (&ip, ntohl (mp->sw_if_index), &watch);
     292             :   else
     293           0 :     ip_neighbor_unwatch (&ip, ntohl (mp->sw_if_index), &watch);
     294             : 
     295           1 :   BAD_SW_IF_INDEX_LABEL;
     296           1 :   REPLY_MACRO (VL_API_WANT_IP_NEIGHBOR_EVENTS_V2_REPLY);
     297             : }
     298             : 
     299             : static void
     300           5 : vl_api_ip_neighbor_config_t_handler (vl_api_ip_neighbor_config_t * mp)
     301             : {
     302             :   vl_api_ip_neighbor_config_reply_t *rmp;
     303             :   ip_address_family_t af;
     304             :   int rv;
     305             : 
     306           5 :   rv = ip_address_family_decode (mp->af, &af);
     307             : 
     308           5 :   if (!rv)
     309           5 :     rv = ip_neighbor_config (af,
     310             :                              ntohl (mp->max_number),
     311           5 :                              ntohl (mp->max_age), mp->recycle);
     312             : 
     313           5 :   REPLY_MACRO (VL_API_IP_NEIGHBOR_CONFIG_REPLY);
     314             : }
     315             : 
     316             : static void
     317           6 : vl_api_ip_neighbor_config_get_t_handler (vl_api_ip_neighbor_config_get_t *mp)
     318             : {
     319             :   vl_api_ip_neighbor_config_get_reply_t *rmp;
     320             :   int rv;
     321           6 :   ip_address_family_t af = AF_IP4;
     322           6 :   u32 max_number = ~0;
     323           6 :   u32 max_age = ~0;
     324           6 :   bool recycle = false;
     325             : 
     326           6 :   rv = ip_address_family_decode (mp->af, &af);
     327             : 
     328           6 :   if (!rv)
     329           6 :     rv = ip_neighbor_get_config (af, &max_number, &max_age, &recycle);
     330             : 
     331             :   // clang-format off
     332           6 :   REPLY_MACRO2 (VL_API_IP_NEIGHBOR_CONFIG_GET_REPLY,
     333             :   ({
     334             :     rmp->af = ip_address_family_encode (af);
     335             :     rmp->max_number = htonl (max_number);
     336             :     rmp->max_age = htonl (max_age);
     337             :     rmp->recycle = recycle;
     338             :   }));
     339             :   // clang-format on
     340             : }
     341             : 
     342             : static void
     343           2 : vl_api_ip_neighbor_replace_begin_t_handler (vl_api_ip_neighbor_replace_begin_t
     344             :                                             * mp)
     345             : {
     346             :   vl_api_ip_neighbor_replace_begin_reply_t *rmp;
     347           2 :   int rv = 0;
     348             : 
     349           2 :   ip_neighbor_mark (AF_IP4);
     350           2 :   ip_neighbor_mark (AF_IP6);
     351             : 
     352           2 :   REPLY_MACRO (VL_API_IP_NEIGHBOR_REPLACE_BEGIN_REPLY);
     353             : }
     354             : 
     355             : static void
     356           2 : vl_api_ip_neighbor_replace_end_t_handler (vl_api_ip_neighbor_replace_end_t *
     357             :                                           mp)
     358             : {
     359             :   vl_api_ip_neighbor_replace_end_reply_t *rmp;
     360           2 :   int rv = 0;
     361             : 
     362           2 :   ip_neighbor_sweep (AF_IP4);
     363           2 :   ip_neighbor_sweep (AF_IP6);
     364             : 
     365           2 :   REPLY_MACRO (VL_API_IP_NEIGHBOR_REPLACE_END_REPLY);
     366             : }
     367             : 
     368             : static void
     369           9 : vl_api_ip_neighbor_flush_t_handler (vl_api_ip_neighbor_flush_t * mp)
     370             : {
     371             :   vl_api_ip_neighbor_flush_reply_t *rmp;
     372             :   ip_address_family_t af;
     373             :   int rv;
     374             : 
     375           9 :   if (mp->sw_if_index != ~0)
     376           5 :     VALIDATE_SW_IF_INDEX (mp);
     377             : 
     378           9 :   rv = ip_address_family_decode (mp->af, &af);
     379             : 
     380           9 :   if (!rv)
     381           9 :     ip_neighbor_del_all (af, ntohl (mp->sw_if_index));
     382             : 
     383           0 :   BAD_SW_IF_INDEX_LABEL;
     384           9 :   REPLY_MACRO (VL_API_IP_NEIGHBOR_FLUSH_REPLY);
     385             : }
     386             : 
     387             : #define vl_msg_name_crc_list
     388             : #include <vnet/ip-neighbor/ip_neighbor.api.h>
     389             : #undef vl_msg_name_crc_list
     390             : 
     391             : #include <vnet/ip-neighbor/ip_neighbor.api.c>
     392             : 
     393             : static clib_error_t *
     394         575 : ip_neighbor_api_init (vlib_main_t * vm)
     395             : {
     396             :   /* Ask for a correctly-sized block of API message decode slots */
     397         575 :   msg_id_base = setup_message_id_table ();
     398             : 
     399         575 :   return 0;
     400             : }
     401             : 
     402        6335 : VLIB_API_INIT_FUNCTION (ip_neighbor_api_init);
     403             : 
     404             : /*
     405             :  * fd.io coding-style-patch-verification: ON
     406             :  *
     407             :  * Local Variables:
     408             :  * eval: (c-set-style "gnu")
     409             :  * End:
     410             :  */

Generated by: LCOV version 1.14