API: Handle pthread cond signal case where signal is missed.
[vpp.git] / src / vlibmemory / memory_client.c
1 /*
2  *------------------------------------------------------------------
3  * memory_client.c - API message handling, client code.
4  *
5  * Copyright (c) 2010 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 <setjmp.h>
21
22 #include <svm/svm.h>
23 #include <svm/ssvm.h>
24 #include <vppinfra/serialize.h>
25 #include <vppinfra/hash.h>
26 #include <vlibmemory/memory_client.h>
27
28 /* A hack. vl_client_get_first_plugin_msg_id depends on it */
29 #include <vlibmemory/socket_client.h>
30
31 #include <vlibmemory/vl_memory_msg_enum.h>
32
33 #define vl_typedefs             /* define message structures */
34 #include <vlibmemory/vl_memory_api_h.h>
35 #undef vl_typedefs
36
37 #define vl_endianfun            /* define message structures */
38 #include <vlibmemory/vl_memory_api_h.h>
39 #undef vl_endianfun
40
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) clib_warning (__VA_ARGS__)
43 #define vl_printfun
44 #include <vlibmemory/vl_memory_api_h.h>
45 #undef vl_printfun
46
47 typedef struct
48 {
49   u8 rx_thread_jmpbuf_valid;
50   u8 connected_to_vlib;
51   jmp_buf rx_thread_jmpbuf;
52   pthread_t rx_thread_handle;
53   /* Plugin message base lookup scheme */
54   volatile u8 first_msg_id_reply_ready;
55   u16 first_msg_id_reply;
56 } memory_client_main_t;
57
58 memory_client_main_t memory_client_main;
59
60 static void *
61 rx_thread_fn (void *arg)
62 {
63   svm_queue_t *q;
64   memory_client_main_t *mm = &memory_client_main;
65   api_main_t *am = &api_main;
66   int i;
67
68   q = am->vl_input_queue;
69
70   /* So we can make the rx thread terminate cleanly */
71   if (setjmp (mm->rx_thread_jmpbuf) == 0)
72     {
73       mm->rx_thread_jmpbuf_valid = 1;
74       /*
75        * Find an unused slot in the per-cpu-mheaps array,
76        * and grab it for this thread. We need to be able to
77        * push/pop the thread heap without affecting other thread(s).
78        */
79       if (__os_thread_index == 0)
80         {
81           for (i = 0; i < ARRAY_LEN (clib_per_cpu_mheaps); i++)
82             {
83               if (clib_per_cpu_mheaps[i] == 0)
84                 {
85                   /* Copy the main thread mheap pointer */
86                   clib_per_cpu_mheaps[i] = clib_per_cpu_mheaps[0];
87                   __os_thread_index = i;
88                   break;
89                 }
90             }
91           ASSERT (__os_thread_index > 0);
92         }
93       while (1)
94         vl_msg_api_queue_handler (q);
95     }
96   pthread_exit (0);
97 }
98
99 static void
100 vl_api_rx_thread_exit_t_handler (vl_api_rx_thread_exit_t * mp)
101 {
102   memory_client_main_t *mm = &memory_client_main;
103   if (mm->rx_thread_jmpbuf_valid)
104     longjmp (mm->rx_thread_jmpbuf, 1);
105 }
106
107 static void
108 vl_api_name_and_crc_free (void)
109 {
110   api_main_t *am = &api_main;
111   int i;
112   u8 **keys = 0;
113   hash_pair_t *hp;
114
115   if (!am->msg_index_by_name_and_crc)
116     return;
117
118   /* *INDENT-OFF* */
119   hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
120       ({
121         vec_add1 (keys, (u8 *) hp->key);
122       }));
123   /* *INDENT-ON* */
124   for (i = 0; i < vec_len (keys); i++)
125     vec_free (keys[i]);
126   vec_free (keys);
127   hash_free (am->msg_index_by_name_and_crc);
128 }
129
130 static void
131 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
132 {
133   serialize_main_t _sm, *sm = &_sm;
134   api_main_t *am = &api_main;
135   u8 *tblv;
136   u32 nmsgs;
137   int i;
138   u8 *name_and_crc;
139   u32 msg_index;
140
141   am->my_client_index = mp->index;
142   am->my_registration = (vl_api_registration_t *) (uword) mp->handle;
143
144   /* Clean out any previous hash table (unlikely) */
145   vl_api_name_and_crc_free ();
146
147   am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
148
149   /* Recreate the vnet-side API message handler table */
150   tblv = uword_to_pointer (mp->message_table, u8 *);
151   unserialize_open_data (sm, tblv, vec_len (tblv));
152   unserialize_integer (sm, &nmsgs, sizeof (u32));
153
154   for (i = 0; i < nmsgs; i++)
155     {
156       msg_index = unserialize_likely_small_unsigned_integer (sm);
157       unserialize_cstring (sm, (char **) &name_and_crc);
158       hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index);
159     }
160 }
161
162 static void
163 noop_handler (void *notused)
164 {
165 }
166
167 void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem);
168 int
169 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
170 {
171   svm_region_t *svm;
172   vl_api_memclnt_create_t *mp;
173   vl_api_memclnt_create_reply_t *rp;
174   svm_queue_t *vl_input_queue;
175   vl_shmem_hdr_t *shmem_hdr;
176   int rv = 0;
177   void *oldheap;
178   api_main_t *am = &api_main;
179
180   if (am->my_registration)
181     {
182       clib_warning ("client %s already connected...", name);
183       return -1;
184     }
185
186   if (am->vlib_rp == 0)
187     {
188       clib_warning ("am->vlib_rp NULL");
189       return -1;
190     }
191
192   svm = am->vlib_rp;
193   shmem_hdr = am->shmem_hdr;
194
195   if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
196     {
197       clib_warning ("shmem_hdr / input queue NULL");
198       return -1;
199     }
200
201   pthread_mutex_lock (&svm->mutex);
202   oldheap = svm_push_data_heap (svm);
203   vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
204                                              getpid ());
205   svm_pop_heap (oldheap);
206   pthread_mutex_unlock (&svm->mutex);
207
208   am->my_client_index = ~0;
209   am->my_registration = 0;
210   am->vl_input_queue = vl_input_queue;
211
212   mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_create_t));
213   clib_memset (mp, 0, sizeof (*mp));
214   mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
215   mp->ctx_quota = ctx_quota;
216   mp->input_queue = (uword) vl_input_queue;
217   strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
218
219   vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
220
221   while (1)
222     {
223       int qstatus;
224       struct timespec ts, tsrem;
225       int i;
226
227       /* Wait up to 10 seconds */
228       for (i = 0; i < 1000; i++)
229         {
230           qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp,
231                                    SVM_Q_NOWAIT, 0);
232           if (qstatus == 0)
233             goto read_one_msg;
234           ts.tv_sec = 0;
235           ts.tv_nsec = 10000 * 1000;    /* 10 ms */
236           while (nanosleep (&ts, &tsrem) < 0)
237             ts = tsrem;
238         }
239       /* Timeout... */
240       clib_warning ("memclnt_create_reply timeout");
241       return -1;
242
243     read_one_msg:
244       if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
245         {
246           clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
247           continue;
248         }
249       rv = clib_net_to_host_u32 (rp->response);
250
251       vl_msg_api_handler ((void *) rp);
252       break;
253     }
254   return (rv);
255 }
256
257 static void
258 vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
259 {
260   void *oldheap;
261   api_main_t *am = &api_main;
262
263   pthread_mutex_lock (&am->vlib_rp->mutex);
264   oldheap = svm_push_data_heap (am->vlib_rp);
265   svm_queue_free (am->vl_input_queue);
266   pthread_mutex_unlock (&am->vlib_rp->mutex);
267   svm_pop_heap (oldheap);
268
269   am->my_client_index = ~0;
270   am->my_registration = 0;
271   am->vl_input_queue = 0;
272 }
273
274 void
275 vl_client_send_disconnect (u8 do_cleanup)
276 {
277   vl_api_memclnt_delete_t *mp;
278   vl_shmem_hdr_t *shmem_hdr;
279   api_main_t *am = &api_main;
280
281   ASSERT (am->vlib_rp);
282   shmem_hdr = am->shmem_hdr;
283   ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
284
285   mp = vl_msg_api_alloc (sizeof (vl_api_memclnt_delete_t));
286   clib_memset (mp, 0, sizeof (*mp));
287   mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
288   mp->index = am->my_client_index;
289   mp->handle = (uword) am->my_registration;
290   mp->do_cleanup = do_cleanup;
291
292   vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
293 }
294
295 int
296 vl_client_disconnect (void)
297 {
298   vl_api_memclnt_delete_reply_t *rp;
299   svm_queue_t *vl_input_queue;
300   api_main_t *am = &api_main;
301   time_t begin;
302
303   vl_input_queue = am->vl_input_queue;
304   vl_client_send_disconnect (0 /* wait for reply */ );
305
306   /*
307    * Have to be careful here, in case the client is disconnecting
308    * because e.g. the vlib process died, or is unresponsive.
309    */
310   begin = time (0);
311   while (1)
312     {
313       time_t now;
314
315       now = time (0);
316
317       if (now >= (begin + 2))
318         {
319           clib_warning ("peer unresponsive, give up");
320           am->my_client_index = ~0;
321           am->my_registration = 0;
322           am->shmem_hdr = 0;
323           return -1;
324         }
325       if (svm_queue_sub (vl_input_queue, (u8 *) & rp, SVM_Q_NOWAIT, 0) < 0)
326         continue;
327
328       /* drain the queue */
329       if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
330         {
331           clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
332           vl_msg_api_handler ((void *) rp);
333           continue;
334         }
335       vl_msg_api_handler ((void *) rp);
336       break;
337     }
338
339   vl_api_name_and_crc_free ();
340   return 0;
341 }
342
343 /**
344  * Stave off the binary API dead client reaper
345  * Only sent to inactive clients
346  */
347 static void
348 vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
349 {
350   vl_api_memclnt_keepalive_reply_t *rmp;
351   api_main_t *am;
352   vl_shmem_hdr_t *shmem_hdr;
353
354   am = &api_main;
355   shmem_hdr = am->shmem_hdr;
356
357   rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
358   clib_memset (rmp, 0, sizeof (*rmp));
359   rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
360   rmp->context = mp->context;
361   vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
362 }
363
364 #define foreach_api_msg                         \
365 _(RX_THREAD_EXIT, rx_thread_exit)               \
366 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply)   \
367 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)   \
368 _(MEMCLNT_KEEPALIVE, memclnt_keepalive)
369
370 void
371 vl_client_install_client_message_handlers (void)
372 {
373
374 #define _(N,n)                                                  \
375     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
376                             vl_api_##n##_t_handler,             \
377                             noop_handler,                       \
378                             vl_api_##n##_t_endian,              \
379                             vl_api_##n##_t_print,               \
380                             sizeof(vl_api_##n##_t), 1);
381   foreach_api_msg;
382 #undef _
383 }
384
385 int
386 vl_client_api_map (const char *region_name)
387 {
388   int rv;
389
390   if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
391     return rv;
392
393   vl_client_install_client_message_handlers ();
394   return 0;
395 }
396
397 void
398 vl_client_api_unmap (void)
399 {
400   vl_unmap_shmem_client ();
401 }
402
403 u8
404 vl_mem_client_is_connected (void)
405 {
406   return (memory_client_main.connected_to_vlib != 0);
407 }
408
409 static int
410 connect_to_vlib_internal (const char *svm_name,
411                           const char *client_name,
412                           int rx_queue_size, int want_pthread, int do_map)
413 {
414   int rv = 0;
415   memory_client_main_t *mm = &memory_client_main;
416
417   if (do_map && (rv = vl_client_api_map (svm_name)))
418     {
419       clib_warning ("vl_client_api map rv %d", rv);
420       return rv;
421     }
422
423   if (vl_client_connect (client_name, 0 /* punt quota */ ,
424                          rx_queue_size /* input queue */ ) < 0)
425     {
426       vl_client_api_unmap ();
427       return -1;
428     }
429
430   /* Start the rx queue thread */
431
432   if (want_pthread)
433     {
434       rv = pthread_create (&mm->rx_thread_handle,
435                            NULL /*attr */ , rx_thread_fn, 0);
436       if (rv)
437         clib_warning ("pthread_create returned %d", rv);
438     }
439
440   mm->connected_to_vlib = 1;
441   return 0;
442 }
443
444 int
445 vl_client_connect_to_vlib (const char *svm_name,
446                            const char *client_name, int rx_queue_size)
447 {
448   return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
449                                    1 /* want pthread */ ,
450                                    1 /* do map */ );
451 }
452
453 int
454 vl_client_connect_to_vlib_no_rx_pthread (const char *svm_name,
455                                          const char *client_name,
456                                          int rx_queue_size)
457 {
458   return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
459                                    0 /* want pthread */ ,
460                                    1 /* do map */ );
461 }
462
463 int
464 vl_client_connect_to_vlib_no_map (const char *svm_name,
465                                   const char *client_name, int rx_queue_size)
466 {
467   return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
468                                    1 /* want pthread */ ,
469                                    0 /* dont map */ );
470 }
471
472 int
473 vl_client_connect_to_vlib_no_rx_pthread_no_map (const char *svm_name,
474                                                 const char *client_name,
475                                                 int rx_queue_size)
476 {
477   return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
478                                    0 /* want pthread */ ,
479                                    0 /* dont map */ );
480 }
481
482 static void
483 disconnect_from_vlib_internal (u8 do_unmap)
484 {
485   memory_client_main_t *mm = &memory_client_main;
486   api_main_t *am = &api_main;
487   uword junk;
488
489   if (mm->rx_thread_jmpbuf_valid)
490     {
491       vl_api_rx_thread_exit_t *ep;
492       ep = vl_msg_api_alloc (sizeof (*ep));
493       ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
494       vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep);
495       pthread_join (mm->rx_thread_handle, (void **) &junk);
496     }
497   if (mm->connected_to_vlib)
498     {
499       vl_client_disconnect ();
500       if (do_unmap)
501         vl_client_api_unmap ();
502     }
503   clib_memset (mm, 0, sizeof (*mm));
504 }
505
506 void
507 vl_client_disconnect_from_vlib (void)
508 {
509   disconnect_from_vlib_internal (1);
510 }
511
512 void
513 vl_client_disconnect_from_vlib_no_unmap (void)
514 {
515   disconnect_from_vlib_internal (0);
516 }
517
518 static void vl_api_get_first_msg_id_reply_t_handler
519   (vl_api_get_first_msg_id_reply_t * mp)
520 {
521   memory_client_main_t *mm = &memory_client_main;
522   i32 retval = ntohl (mp->retval);
523
524   mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
525   mm->first_msg_id_reply_ready = 1;
526 }
527
528 u16
529 vl_client_get_first_plugin_msg_id (const char *plugin_name)
530 {
531   vl_api_get_first_msg_id_t *mp;
532   api_main_t *am = &api_main;
533   memory_client_main_t *mm = &memory_client_main;
534   f64 timeout;
535   void *old_handler;
536   clib_time_t clib_time;
537   u16 rv = ~0;
538
539   if (strlen (plugin_name) + 1 > sizeof (mp->name))
540     return (rv);
541
542   clib_memset (&clib_time, 0, sizeof (clib_time));
543   clib_time_init (&clib_time);
544
545   /* Push this plugin's first_msg_id_reply handler */
546   old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY];
547   am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *)
548     vl_api_get_first_msg_id_reply_t_handler;
549
550   /* Ask the data-plane for the message-ID base of the indicated plugin */
551   mm->first_msg_id_reply_ready = 0;
552
553   /* Not using shm client */
554   if (!am->my_registration)
555     {
556       mp = vl_socket_client_msg_alloc (sizeof (*mp));
557       clib_memset (mp, 0, sizeof (*mp));
558       mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
559       mp->client_index = am->my_client_index;
560       strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
561
562       if (vl_socket_client_write () <= 0)
563         goto sock_err;
564       if (vl_socket_client_read (1))
565         goto sock_err;
566
567       if (mm->first_msg_id_reply_ready == 1)
568         {
569           rv = mm->first_msg_id_reply;
570           goto result;
571         }
572
573     sock_err:
574       /* Restore old handler */
575       am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
576
577       return -1;
578     }
579   else
580     {
581       mp = vl_msg_api_alloc (sizeof (*mp));
582       clib_memset (mp, 0, sizeof (*mp));
583       mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
584       mp->client_index = am->my_client_index;
585       strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
586
587       vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp);
588
589       /* Synchronously wait for the answer */
590       timeout = clib_time_now (&clib_time) + 1.0;
591       while (clib_time_now (&clib_time) < timeout)
592         {
593           if (mm->first_msg_id_reply_ready == 1)
594             {
595               rv = mm->first_msg_id_reply;
596               goto result;
597             }
598         }
599       /* Restore old handler */
600       am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
601
602       return rv;
603     }
604
605 result:
606
607   /* Restore the old handler */
608   am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
609
610   if (rv == (u16) ~ 0)
611     clib_warning ("plugin '%s' not registered", plugin_name);
612
613   return rv;
614 }
615
616 /*
617  * fd.io coding-style-patch-verification: ON
618  *
619  * Local Variables:
620  * eval: (c-set-style "gnu")
621  * End:
622  */