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