LCOV - code coverage report
Current view: top level - vlibmemory - memclnt_api.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 177 286 61.9 %
Date: 2023-10-26 01:39:38 Functions: 19 24 79.2 %

          Line data    Source code
       1             : /*
       2             :  *------------------------------------------------------------------
       3             :  * memclnt_api.c VLIB API implementation
       4             :  *
       5             :  * Copyright (c) 2009 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 <fcntl.h>
      21             : #include <pthread.h>
      22             : #include <vppinfra/vec.h>
      23             : #include <vppinfra/hash.h>
      24             : #include <vppinfra/pool.h>
      25             : #include <vppinfra/format.h>
      26             : #include <vppinfra/byte_order.h>
      27             : #include <vppinfra/elog.h>
      28             : #include <vlib/vlib.h>
      29             : #include <vlib/unix/unix.h>
      30             : #include <vlibapi/api.h>
      31             : #include <vlibmemory/api.h>
      32             : #include <vlibapi/api_helper_macros.h>
      33             : 
      34             : /**
      35             :  * @file
      36             :  * @brief Binary API messaging via shared memory
      37             :  * Low-level, primary provisioning interface
      38             :  */
      39             : /*? %%clicmd:group_label Binary API CLI %% ?*/
      40             : /*? %%syscfg:group_label Binary API configuration %% ?*/
      41             : 
      42             : #define TRACE_VLIB_MEMORY_QUEUE 0
      43             : 
      44             : #include <vlibmemory/vl_memory_msg_enum.h> /* enumerate all vlib messages */
      45             : 
      46             : #define vl_typedefs /* define message structures */
      47             : #include <vlibmemory/vl_memory_api_h.h>
      48             : #undef vl_typedefs
      49             : 
      50             : /* instantiate all the print functions we know about */
      51             : #define vl_printfun
      52             : #include <vlibmemory/vl_memory_api_h.h>
      53             : #undef vl_printfun
      54             : 
      55             : /* instantiate all the endian swap functions we know about */
      56             : #define vl_endianfun
      57             : #include <vlibmemory/vl_memory_api_h.h>
      58             : #undef vl_endianfun
      59             : 
      60             : #define vl_calcsizefun
      61             : #include <vlibmemory/vl_memory_api_h.h>
      62             : #undef vl_calcsizefun
      63             : 
      64             : static void
      65          93 : vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t *mp)
      66             : {
      67             :   vl_api_get_first_msg_id_reply_t *rmp;
      68             :   vl_api_registration_t *regp;
      69             :   uword *p;
      70          93 :   api_main_t *am = vlibapi_get_main ();
      71             :   vl_api_msg_range_t *rp;
      72             :   u8 name[64];
      73          93 :   u16 first_msg_id = ~0;
      74          93 :   int rv = -7; /* VNET_API_ERROR_INVALID_VALUE */
      75             : 
      76          93 :   regp = vl_api_client_index_to_registration (mp->client_index);
      77          93 :   if (!regp)
      78           0 :     return;
      79             : 
      80          93 :   if (am->msg_range_by_name == 0)
      81           0 :     goto out;
      82          93 :   strncpy ((char *) name, (char *) mp->name, ARRAY_LEN (name));
      83          93 :   name[ARRAY_LEN (name) - 1] = '\0';
      84          93 :   p = hash_get_mem (am->msg_range_by_name, name);
      85          93 :   if (p == 0)
      86           3 :     goto out;
      87             : 
      88          90 :   rp = vec_elt_at_index (am->msg_ranges, p[0]);
      89          90 :   first_msg_id = rp->first_msg_id;
      90          90 :   rv = 0;
      91             : 
      92          93 : out:
      93          93 :   rmp = vl_msg_api_alloc (sizeof (*rmp));
      94          93 :   rmp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID_REPLY);
      95          93 :   rmp->context = mp->context;
      96          93 :   rmp->retval = ntohl (rv);
      97          93 :   rmp->first_msg_id = ntohs (first_msg_id);
      98          93 :   vl_api_send_msg (regp, (u8 *) rmp);
      99             : }
     100             : 
     101             : void
     102           0 : vl_api_api_versions_t_handler (vl_api_api_versions_t *mp)
     103             : {
     104           0 :   api_main_t *am = vlibapi_get_main ();
     105             :   vl_api_api_versions_reply_t *rmp;
     106             :   vl_api_registration_t *reg;
     107           0 :   u32 nmsg = vec_len (am->api_version_list);
     108           0 :   int msg_size = sizeof (*rmp) + sizeof (rmp->api_versions[0]) * nmsg;
     109             :   int i;
     110             : 
     111           0 :   reg = vl_api_client_index_to_registration (mp->client_index);
     112           0 :   if (!reg)
     113           0 :     return;
     114             : 
     115           0 :   rmp = vl_msg_api_alloc (msg_size);
     116           0 :   clib_memset (rmp, 0, msg_size);
     117           0 :   rmp->_vl_msg_id = ntohs (VL_API_API_VERSIONS_REPLY);
     118             : 
     119             :   /* fill in the message */
     120           0 :   rmp->context = mp->context;
     121           0 :   rmp->count = htonl (nmsg);
     122             : 
     123           0 :   for (i = 0; i < nmsg; ++i)
     124             :     {
     125           0 :       api_version_t *vl = &am->api_version_list[i];
     126           0 :       rmp->api_versions[i].major = htonl (vl->major);
     127           0 :       rmp->api_versions[i].minor = htonl (vl->minor);
     128           0 :       rmp->api_versions[i].patch = htonl (vl->patch);
     129           0 :       strncpy ((char *) rmp->api_versions[i].name, vl->name,
     130             :                ARRAY_LEN (rmp->api_versions[i].name));
     131           0 :       rmp->api_versions[i].name[ARRAY_LEN (rmp->api_versions[i].name) - 1] =
     132             :         '\0';
     133             :     }
     134             : 
     135           0 :   vl_api_send_msg (reg, (u8 *) rmp);
     136             : }
     137             : 
     138             : static void
     139       29074 : vl_api_control_ping_t_handler (vl_api_control_ping_t *mp)
     140             : {
     141             :   vl_api_control_ping_reply_t *rmp;
     142       29074 :   int rv = 0;
     143             : 
     144       29074 :   REPLY_MACRO2 (VL_API_CONTROL_PING_REPLY,
     145             :                 ({ rmp->vpe_pid = ntohl (getpid ()); }));
     146             : }
     147             : 
     148             : #define foreach_vlib_api_msg                                                  \
     149             :   _ (GET_FIRST_MSG_ID, get_first_msg_id)                                      \
     150             :   _ (API_VERSIONS, api_versions)                                              \
     151             :   _ (CONTROL_PING, control_ping)
     152             : 
     153             : /*
     154             :  * vl_api_init
     155             :  */
     156             : static int
     157         575 : vlib_api_init (void)
     158             : {
     159         575 :   api_main_t *am = vlibapi_get_main ();
     160             :   vl_msg_api_msg_config_t cfg;
     161         575 :   vl_msg_api_msg_config_t *c = &cfg;
     162             : 
     163         575 :   cJSON_Hooks cjson_hooks = {
     164             :     .malloc_fn = clib_mem_alloc,
     165             :     .free_fn = clib_mem_free,
     166             :   };
     167         575 :   cJSON_InitHooks (&cjson_hooks);
     168             : 
     169         575 :   clib_memset (c, 0, sizeof (*c));
     170             : 
     171             : #define _(N, n)                                                               \
     172             :   do                                                                          \
     173             :     {                                                                         \
     174             :       c->id = VL_API_##N;                                                     \
     175             :       c->name = #n;                                                           \
     176             :       c->handler = vl_api_##n##_t_handler;                                    \
     177             :       c->endian = vl_api_##n##_t_endian;                                      \
     178             :       c->format_fn = vl_api_##n##_t_format;                                   \
     179             :       c->tojson = vl_api_##n##_t_tojson;                                      \
     180             :       c->fromjson = vl_api_##n##_t_fromjson;                                  \
     181             :       c->calc_size = vl_api_##n##_t_calc_size;                                \
     182             :       c->size = sizeof (vl_api_##n##_t);                                      \
     183             :       c->traced = 1;      /* trace, so these msgs print */                 \
     184             :       c->replay = 0;      /* don't replay client create/delete msgs */     \
     185             :       c->message_bounce = 0; /* don't bounce this message */                  \
     186             :       vl_msg_api_config (c);                                                  \
     187             :     }                                                                         \
     188             :   while (0);
     189             : 
     190         575 :   foreach_vlib_api_msg;
     191             : #undef _
     192             : 
     193             :   /* Mark messages as mp safe */
     194         575 :   vl_api_set_msg_thread_safe (am, VL_API_GET_FIRST_MSG_ID, 1);
     195         575 :   vl_api_set_msg_thread_safe (am, VL_API_API_VERSIONS, 1);
     196         575 :   vl_api_set_msg_thread_safe (am, VL_API_CONTROL_PING, 1);
     197         575 :   vl_api_set_msg_thread_safe (am, VL_API_CONTROL_PING_REPLY, 1);
     198             : 
     199         575 :   return 0;
     200             : }
     201             : 
     202             : u64 vector_rate_histogram[SLEEP_N_BUCKETS];
     203             : 
     204             : /*
     205             :  * Callback to send ourselves a plugin numbering-space trace msg
     206             :  */
     207             : static void
     208       62103 : send_one_plugin_msg_ids_msg (u8 *name, u16 first_msg_id, u16 last_msg_id)
     209             : {
     210             :   vl_api_trace_plugin_msg_ids_t *mp;
     211       62103 :   api_main_t *am = vlibapi_get_main ();
     212       62103 :   vl_shmem_hdr_t *shmem_hdr = am->shmem_hdr;
     213             :   svm_queue_t *q;
     214             : 
     215       62103 :   mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
     216       62103 :   clib_memset (mp, 0, sizeof (*mp));
     217             : 
     218       62103 :   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_TRACE_PLUGIN_MSG_IDS);
     219       62103 :   strncpy ((char *) mp->plugin_name, (char *) name,
     220             :            sizeof (mp->plugin_name) - 1);
     221       62103 :   mp->first_msg_id = clib_host_to_net_u16 (first_msg_id);
     222       62103 :   mp->last_msg_id = clib_host_to_net_u16 (last_msg_id);
     223             : 
     224       62103 :   q = shmem_hdr->vl_input_queue;
     225             : 
     226       62103 :   vl_msg_api_send_shmem (q, (u8 *) &mp);
     227       62103 : }
     228             : 
     229             : void
     230           0 : vl_api_save_msg_table (void)
     231             : {
     232             :   u8 *serialized_message_table;
     233           0 :   api_main_t *am = vlibapi_get_main ();
     234             :   u8 *chroot_file;
     235             :   int fd, rv;
     236             : 
     237             :   /*
     238             :    * Snapshoot the api message table.
     239             :    */
     240           0 :   if (strstr ((char *) am->save_msg_table_filename, "..") ||
     241           0 :       index ((char *) am->save_msg_table_filename, '/'))
     242             :     {
     243           0 :       clib_warning ("illegal save-message-table filename '%s'",
     244             :                     am->save_msg_table_filename);
     245           0 :       return;
     246             :     }
     247             : 
     248           0 :   chroot_file = format (0, "/tmp/%s%c", am->save_msg_table_filename, 0);
     249             : 
     250           0 :   fd = creat ((char *) chroot_file, 0644);
     251             : 
     252           0 :   if (fd < 0)
     253             :     {
     254           0 :       clib_unix_warning ("creat");
     255           0 :       return;
     256             :     }
     257             : 
     258           0 :   serialized_message_table = vl_api_serialize_message_table (am, 0);
     259             : 
     260           0 :   rv =
     261           0 :     write (fd, serialized_message_table, vec_len (serialized_message_table));
     262             : 
     263           0 :   if (rv != vec_len (serialized_message_table))
     264           0 :     clib_unix_warning ("write");
     265             : 
     266           0 :   rv = close (fd);
     267           0 :   if (rv < 0)
     268           0 :     clib_unix_warning ("close");
     269             : 
     270           0 :   vec_free (chroot_file);
     271           0 :   vec_free (serialized_message_table);
     272             : }
     273             : 
     274             : clib_error_t *vat_builtin_main_init (vlib_main_t *vm) __attribute__ ((weak));
     275             : clib_error_t *
     276           0 : vat_builtin_main_init (vlib_main_t *vm)
     277             : {
     278           0 :   return 0;
     279             : }
     280             : 
     281             : static uword
     282         575 : vl_api_clnt_process (vlib_main_t *vm, vlib_node_runtime_t *node,
     283             :                      vlib_frame_t *f)
     284             : {
     285         575 :   vlib_global_main_t *vgm = vlib_get_global_main ();
     286         575 :   int private_segment_rotor = 0, i, rv;
     287             :   vl_socket_args_for_process_t *a;
     288             :   vl_shmem_hdr_t *shm;
     289             :   svm_queue_t *q;
     290             :   clib_error_t *e;
     291         575 :   api_main_t *am = vlibapi_get_main ();
     292             :   f64 dead_client_scan_time;
     293             :   f64 sleep_time, start_time;
     294             :   f64 vector_rate;
     295             :   clib_error_t *error;
     296             :   uword event_type;
     297         575 :   uword *event_data = 0;
     298             :   f64 now;
     299             : 
     300         575 :   if ((error = vl_sock_api_init (vm)))
     301             :     {
     302           0 :       clib_error_report (error);
     303           0 :       clib_warning ("socksvr_api_init failed, quitting...");
     304           0 :       return 0;
     305             :     }
     306             : 
     307         575 :   if ((rv = vlib_api_init ()) < 0)
     308             :     {
     309           0 :       clib_warning ("vlib_api_init returned %d, quitting...", rv);
     310           0 :       return 0;
     311             :     }
     312             : 
     313         575 :   shm = am->shmem_hdr;
     314         575 :   q = shm->vl_input_queue;
     315             : 
     316         575 :   e = vlib_call_init_exit_functions (vm, &vgm->api_init_function_registrations,
     317             :                                      1 /* call_once */, 1 /* is_global */);
     318         575 :   if (e)
     319           0 :     clib_error_report (e);
     320             : 
     321         575 :   e = vat_builtin_main_init (vm);
     322         575 :   if (e)
     323           0 :     clib_error_report (e);
     324             : 
     325         575 :   sleep_time = 10.0;
     326         575 :   dead_client_scan_time = vlib_time_now (vm) + 10.0;
     327             : 
     328             :   /*
     329             :    * Send plugin message range messages for each plugin we loaded
     330             :    */
     331       62678 :   for (i = 0; i < vec_len (am->msg_ranges); i++)
     332             :     {
     333       62103 :       vl_api_msg_range_t *rp = am->msg_ranges + i;
     334       62103 :       send_one_plugin_msg_ids_msg (rp->name, rp->first_msg_id,
     335       62103 :                                    rp->last_msg_id);
     336             :     }
     337             : 
     338             :   /*
     339             :    * Save the api message table snapshot, if configured
     340             :    */
     341         575 :   if (am->save_msg_table_filename)
     342           0 :     vl_api_save_msg_table ();
     343             : 
     344             :   /* $$$ pay attention to frame size, control CPU usage */
     345             :   while (1)
     346             :     {
     347             :       /*
     348             :        * There's a reason for checking the queue before
     349             :        * sleeping. If the vlib application crashes, it's entirely
     350             :        * possible for a client to enqueue a connect request
     351             :        * during the process restart interval.
     352             :        *
     353             :        * Unless some force of physics causes the new incarnation
     354             :        * of the application to process the request, the client will
     355             :        * sit and wait for Godot...
     356             :        */
     357      442200 :       vector_rate = (f64) vlib_last_vectors_per_main_loop (vm);
     358      442200 :       start_time = vlib_time_now (vm);
     359             :       while (1)
     360             :         {
     361      988700 :           if (vl_mem_api_handle_rpc (vm, node) ||
     362      494350 :               vl_mem_api_handle_msg_main (vm, node))
     363             :             {
     364      431676 :               vm->api_queue_nonempty = 0;
     365             :               VL_MEM_API_LOG_Q_LEN ("q-underflow: len %d", 0);
     366      431676 :               sleep_time = 20.0;
     367      431676 :               break;
     368             :             }
     369             : 
     370             :           /* Allow no more than 10us without a pause */
     371       62674 :           if (vlib_time_now (vm) > start_time + 10e-6)
     372             :             {
     373       10524 :               int index = SLEEP_400_US;
     374       10524 :               if (vector_rate > 40.0)
     375           0 :                 sleep_time = 400e-6;
     376       10524 :               else if (vector_rate > 20.0)
     377             :                 {
     378           0 :                   index = SLEEP_200_US;
     379           0 :                   sleep_time = 200e-6;
     380             :                 }
     381       10524 :               else if (vector_rate >= 1.0)
     382             :                 {
     383           0 :                   index = SLEEP_100_US;
     384           0 :                   sleep_time = 100e-6;
     385             :                 }
     386             :               else
     387             :                 {
     388       10524 :                   index = SLEEP_10_US;
     389       10524 :                   sleep_time = 10e-6;
     390             :                 }
     391       10524 :               vector_rate_histogram[index] += 1;
     392       10524 :               break;
     393             :             }
     394             :         }
     395             : 
     396             :       /*
     397             :        * see if we have any private api shared-memory segments
     398             :        * If so, push required context variables, and process
     399             :        * a message.
     400             :        */
     401      442200 :       if (PREDICT_FALSE (vec_len (am->vlib_private_rps)))
     402             :         {
     403         739 :           if (private_segment_rotor >= vec_len (am->vlib_private_rps))
     404         326 :             private_segment_rotor = 0;
     405         739 :           vl_mem_api_handle_msg_private (vm, node, private_segment_rotor++);
     406             :         }
     407             : 
     408      442200 :       vlib_process_wait_for_event_or_clock (vm, sleep_time);
     409      441625 :       vec_reset_length (event_data);
     410      441625 :       event_type = vlib_process_get_events (vm, &event_data);
     411      441625 :       now = vlib_time_now (vm);
     412             : 
     413      441625 :       switch (event_type)
     414             :         {
     415       11259 :         case QUEUE_SIGNAL_EVENT:
     416       11259 :           vm->queue_signal_pending = 0;
     417             :           VL_MEM_API_LOG_Q_LEN ("q-awake: len %d", q->cursize);
     418             : 
     419       11259 :           break;
     420      430122 :         case SOCKET_READ_EVENT:
     421      871982 :           for (i = 0; i < vec_len (event_data); i++)
     422             :             {
     423             :               vl_api_registration_t *regp;
     424             : 
     425      441860 :               a = pool_elt_at_index (socket_main.process_args, event_data[i]);
     426      441860 :               regp = vl_socket_get_registration (a->reg_index);
     427      441860 :               if (regp)
     428             :                 {
     429      441860 :                   vl_socket_process_api_msg (regp, (i8 *) a->data);
     430      441860 :                   a = pool_elt_at_index (socket_main.process_args,
     431             :                                          event_data[i]);
     432             :                 }
     433      441860 :               vec_free (a->data);
     434      441860 :               pool_put (socket_main.process_args, a);
     435             :             }
     436      430122 :           break;
     437             : 
     438             :           /* Timeout... */
     439         244 :         case -1:
     440         244 :           break;
     441             : 
     442           0 :         default:
     443           0 :           clib_warning ("unknown event type %d", event_type);
     444           0 :           break;
     445             :         }
     446             : 
     447      441625 :       if (now > dead_client_scan_time)
     448             :         {
     449         498 :           vl_mem_api_dead_client_scan (am, shm, now);
     450         498 :           dead_client_scan_time = vlib_time_now (vm) + 10.0;
     451             :         }
     452             :     }
     453             : 
     454             :   return 0;
     455             : }
     456             : 
     457      183788 : VLIB_REGISTER_NODE (vl_api_clnt_node) = {
     458             :   .function = vl_api_clnt_process,
     459             :   .type = VLIB_NODE_TYPE_PROCESS,
     460             :   .name = "api-rx-from-ring",
     461             :   .state = VLIB_NODE_STATE_DISABLED,
     462             :   .process_log2_n_stack_bytes = 18,
     463             : };
     464             : 
     465             : void
     466         575 : vl_mem_api_enable_disable (vlib_main_t *vm, int enable)
     467             : {
     468         575 :   vlib_node_set_state (
     469             :     vm, vl_api_clnt_node.index,
     470             :     (enable ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED));
     471         575 : }
     472             : 
     473             : static uword
     474           0 : api_rx_from_node (vlib_main_t *vm, vlib_node_runtime_t *node,
     475             :                   vlib_frame_t *frame)
     476             : {
     477           0 :   uword n_packets = frame->n_vectors;
     478             :   uword n_left_from;
     479             :   u32 *from;
     480             :   static u8 *long_msg;
     481             : 
     482           0 :   vec_validate (long_msg, 4095);
     483           0 :   n_left_from = frame->n_vectors;
     484           0 :   from = vlib_frame_vector_args (frame);
     485             : 
     486           0 :   while (n_left_from > 0)
     487             :     {
     488             :       u32 bi0;
     489             :       vlib_buffer_t *b0;
     490             :       void *msg;
     491             :       uword msg_len;
     492             : 
     493           0 :       bi0 = from[0];
     494           0 :       b0 = vlib_get_buffer (vm, bi0);
     495           0 :       from += 1;
     496           0 :       n_left_from -= 1;
     497             : 
     498           0 :       msg = b0->data + b0->current_data;
     499           0 :       msg_len = b0->current_length;
     500           0 :       if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
     501             :         {
     502           0 :           ASSERT (long_msg != 0);
     503           0 :           vec_set_len (long_msg, 0);
     504           0 :           vec_add (long_msg, msg, msg_len);
     505           0 :           while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
     506             :             {
     507           0 :               b0 = vlib_get_buffer (vm, b0->next_buffer);
     508           0 :               msg = b0->data + b0->current_data;
     509           0 :               msg_len = b0->current_length;
     510           0 :               vec_add (long_msg, msg, msg_len);
     511             :             }
     512           0 :           msg = long_msg;
     513           0 :           msg_len = vec_len (long_msg);
     514             :         }
     515           0 :       vl_msg_api_handler_no_trace_no_free (msg, msg_len);
     516             :     }
     517             : 
     518             :   /* Free what we've been given. */
     519           0 :   vlib_buffer_free (vm, vlib_frame_vector_args (frame), n_packets);
     520             : 
     521           0 :   return n_packets;
     522             : }
     523             : 
     524      183788 : VLIB_REGISTER_NODE (api_rx_from_node_node, static) = {
     525             :   .function = api_rx_from_node,
     526             :   .type = VLIB_NODE_TYPE_INTERNAL,
     527             :   .vector_size = 4,
     528             :   .name = "api-rx-from-node",
     529             : };
     530             : 
     531             : static void
     532         223 : vl_api_rpc_call_t_handler (vl_api_rpc_call_t *mp)
     533             : {
     534             :   vl_api_rpc_call_reply_t *rmp;
     535             :   int (*fp) (void *);
     536         223 :   i32 rv = 0;
     537         223 :   vlib_main_t *vm = vlib_get_main ();
     538             : 
     539         223 :   if (mp->function == 0)
     540             :     {
     541           0 :       rv = -1;
     542           0 :       clib_warning ("rpc NULL function pointer");
     543             :     }
     544             : 
     545             :   else
     546             :     {
     547         223 :       if (mp->need_barrier_sync)
     548         223 :         vlib_worker_thread_barrier_sync (vm);
     549             : 
     550         223 :       fp = uword_to_pointer (mp->function, int (*) (void *));
     551         223 :       rv = fp (mp->data);
     552             : 
     553         223 :       if (mp->need_barrier_sync)
     554         223 :         vlib_worker_thread_barrier_release (vm);
     555             :     }
     556             : 
     557         223 :   if (mp->send_reply)
     558             :     {
     559           0 :       svm_queue_t *q = vl_api_client_index_to_input_queue (mp->client_index);
     560           0 :       if (q)
     561             :         {
     562           0 :           rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
     563           0 :           rmp->_vl_msg_id = ntohs (VL_API_RPC_CALL_REPLY);
     564           0 :           rmp->context = mp->context;
     565           0 :           rmp->retval = rv;
     566           0 :           vl_msg_api_send_shmem (q, (u8 *) &rmp);
     567             :         }
     568             :     }
     569         223 :   if (mp->multicast)
     570             :     {
     571           0 :       clib_warning ("multicast not yet implemented...");
     572             :     }
     573         223 : }
     574             : 
     575             : static void
     576           0 : vl_api_rpc_call_reply_t_handler (vl_api_rpc_call_reply_t *mp)
     577             : {
     578           0 :   clib_warning ("unimplemented");
     579           0 : }
     580             : 
     581             : void
     582         166 : vl_api_send_pending_rpc_requests (vlib_main_t *vm)
     583             : {
     584         166 :   vlib_main_t *vm_global = vlib_get_first_main ();
     585             : 
     586         166 :   ASSERT (vm != vm_global);
     587             : 
     588         166 :   clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
     589         166 :   vec_append (vm_global->pending_rpc_requests, vm->pending_rpc_requests);
     590         166 :   vec_reset_length (vm->pending_rpc_requests);
     591         166 :   clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
     592         166 : }
     593             : 
     594             : always_inline void
     595        5219 : vl_api_rpc_call_main_thread_inline (void *fp, u8 *data, u32 data_length,
     596             :                                     u8 force_rpc)
     597             : {
     598             :   vl_api_rpc_call_t *mp;
     599        5219 :   vlib_main_t *vm_global = vlib_get_first_main ();
     600        5219 :   vlib_main_t *vm = vlib_get_main ();
     601             : 
     602             :   /* Main thread and not a forced RPC: call the function directly */
     603        5219 :   if ((force_rpc == 0) && (vlib_get_thread_index () == 0))
     604             :     {
     605             :       void (*call_fp) (void *);
     606             : 
     607        4996 :       vlib_worker_thread_barrier_sync (vm);
     608             : 
     609        4996 :       call_fp = fp;
     610        4996 :       call_fp (data);
     611             : 
     612        4996 :       vlib_worker_thread_barrier_release (vm);
     613        4996 :       return;
     614             :     }
     615             : 
     616             :   /* Otherwise, actually do an RPC */
     617         223 :   mp = vl_msg_api_alloc_as_if_client (sizeof (*mp) + data_length);
     618             : 
     619         223 :   clib_memset (mp, 0, sizeof (*mp));
     620         223 :   clib_memcpy_fast (mp->data, data, data_length);
     621         223 :   mp->_vl_msg_id = ntohs (VL_API_RPC_CALL);
     622         223 :   mp->function = pointer_to_uword (fp);
     623         223 :   mp->need_barrier_sync = 1;
     624             : 
     625             :   /* Add to the pending vector. Thread 0 requires locking. */
     626         223 :   if (vm == vm_global)
     627          48 :     clib_spinlock_lock_if_init (&vm_global->pending_rpc_lock);
     628         223 :   vec_add1 (vm->pending_rpc_requests, (uword) mp);
     629         223 :   if (vm == vm_global)
     630          48 :     clib_spinlock_unlock_if_init (&vm_global->pending_rpc_lock);
     631             : }
     632             : 
     633             : /*
     634             :  * Check if called from worker threads.
     635             :  * If so, make rpc call of fp through shmem.
     636             :  * Otherwise, call fp directly
     637             :  */
     638             : void
     639        5171 : vl_api_rpc_call_main_thread (void *fp, u8 *data, u32 data_length)
     640             : {
     641        5171 :   vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
     642             :                                       0);
     643        5171 : }
     644             : 
     645             : /*
     646             :  * Always make rpc call of fp through shmem, useful for calling from threads
     647             :  * not setup as worker threads, such as DPDK callback thread
     648             :  */
     649             : void
     650          48 : vl_api_force_rpc_call_main_thread (void *fp, u8 *data, u32 data_length)
     651             : {
     652          48 :   vl_api_rpc_call_main_thread_inline (fp, data, data_length, /*force_rpc */
     653             :                                       1);
     654          48 : }
     655             : 
     656             : static void
     657       62103 : vl_api_trace_plugin_msg_ids_t_handler (vl_api_trace_plugin_msg_ids_t *mp)
     658             : {
     659       62103 :   api_main_t *am = vlibapi_get_main ();
     660             :   vl_api_msg_range_t *rp;
     661             :   uword *p;
     662             : 
     663             :   /* Noop (except for tracing) during normal operation */
     664       62103 :   if (am->replay_in_progress == 0)
     665       62103 :     return;
     666             : 
     667           0 :   p = hash_get_mem (am->msg_range_by_name, mp->plugin_name);
     668           0 :   if (p == 0)
     669             :     {
     670           0 :       clib_warning ("WARNING: traced plugin '%s' not in current image",
     671             :                     mp->plugin_name);
     672           0 :       return;
     673             :     }
     674             : 
     675           0 :   rp = vec_elt_at_index (am->msg_ranges, p[0]);
     676           0 :   if (rp->first_msg_id != clib_net_to_host_u16 (mp->first_msg_id))
     677             :     {
     678           0 :       clib_warning ("WARNING: traced plugin '%s' first message id %d not %d",
     679             :                     mp->plugin_name, clib_net_to_host_u16 (mp->first_msg_id),
     680             :                     rp->first_msg_id);
     681             :     }
     682             : 
     683           0 :   if (rp->last_msg_id != clib_net_to_host_u16 (mp->last_msg_id))
     684             :     {
     685           0 :       clib_warning ("WARNING: traced plugin '%s' last message id %d not %d",
     686             :                     mp->plugin_name, clib_net_to_host_u16 (mp->last_msg_id),
     687             :                     rp->last_msg_id);
     688             :     }
     689             : }
     690             : 
     691             : #define foreach_rpc_api_msg                                                   \
     692             :   _ (RPC_CALL, rpc_call)                                                      \
     693             :   _ (RPC_CALL_REPLY, rpc_call_reply)
     694             : 
     695             : #define foreach_plugin_trace_msg _ (TRACE_PLUGIN_MSG_IDS, trace_plugin_msg_ids)
     696             : 
     697             : /*
     698             :  * Set the rpc callback at our earliest possible convenience.
     699             :  * This avoids ordering issues between thread_init() -> start_workers and
     700             :  * an init function which we could define here. If we ever intend to use
     701             :  * vlib all by itself, we can't create a link-time dependency on
     702             :  * an init function here and a typical "call foo_init first"
     703             :  * guitar lick.
     704             :  */
     705             : 
     706             : extern void *rpc_call_main_thread_cb_fn;
     707             : 
     708             : static clib_error_t *
     709         575 : rpc_api_hookup (vlib_main_t *vm)
     710             : {
     711         575 :   api_main_t *am = vlibapi_get_main ();
     712             : #define _(N, n)                                                               \
     713             :   vl_msg_api_config (&(vl_msg_api_msg_config_t){                              \
     714             :     .id = VL_API_##N,                                                         \
     715             :     .name = #n,                                                               \
     716             :     .handler = vl_api_##n##_t_handler,                                        \
     717             :     .format_fn = vl_api_##n##_t_format,                                       \
     718             :     .size = sizeof (vl_api_##n##_t),                                          \
     719             :     .traced = 0,                                                              \
     720             :     .tojson = vl_api_##n##_t_tojson,                                          \
     721             :     .fromjson = vl_api_##n##_t_fromjson,                                      \
     722             :     .calc_size = vl_api_##n##_t_calc_size,                                    \
     723             :   });
     724         575 :   foreach_rpc_api_msg;
     725             : #undef _
     726             : 
     727             : #define _(N, n)                                                               \
     728             :   vl_msg_api_config (&(vl_msg_api_msg_config_t){                              \
     729             :     .id = VL_API_##N,                                                         \
     730             :     .name = #n,                                                               \
     731             :     .handler = vl_api_##n##_t_handler,                                        \
     732             :     .endian = vl_api_##n##_t_endian,                                          \
     733             :     .format_fn = vl_api_##n##_t_format,                                       \
     734             :     .size = sizeof (vl_api_##n##_t),                                          \
     735             :     .traced = 1,                                                              \
     736             :     .tojson = vl_api_##n##_t_tojson,                                          \
     737             :     .fromjson = vl_api_##n##_t_fromjson,                                      \
     738             :     .calc_size = vl_api_##n##_t_calc_size,                                    \
     739             :   });
     740         575 :   foreach_plugin_trace_msg
     741             : #undef _
     742             : 
     743         575 :     vl_api_allow_msg_replay (am, VL_API_TRACE_PLUGIN_MSG_IDS, 0);
     744             : 
     745             :   /* No reason to halt the parade to create a trace record... */
     746         575 :   vl_api_set_msg_thread_safe (am, VL_API_TRACE_PLUGIN_MSG_IDS, 1);
     747         575 :   rpc_call_main_thread_cb_fn = vl_api_rpc_call_main_thread;
     748         575 :   return 0;
     749             : }
     750             : 
     751       21311 : VLIB_API_INIT_FUNCTION (rpc_api_hookup);
     752             : 
     753             : /*
     754             :  * fd.io coding-style-patch-verification: ON
     755             :  *
     756             :  * Local Variables:
     757             :  * eval: (c-set-style "gnu")
     758             :  * End:
     759             :  */

Generated by: LCOV version 1.14