FD.io VPP  v19.01.2-3-gf61a1a8
Vector Packet Processing
memory_client.c
Go to the documentation of this file.
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>
27 
28 /* A hack. vl_client_get_first_plugin_msg_id depends on it */
30 
32 
33 #define vl_typedefs /* define message structures */
35 #undef vl_typedefs
36 
37 #define vl_endianfun /* define message structures */
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
45 #undef vl_printfun
46 
47 typedef struct
48 {
52  pthread_t rx_thread_handle;
53  /* Plugin message base lookup scheme */
57 
59 
60 static void *
61 rx_thread_fn (void *arg)
62 {
63  svm_queue_t *q;
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 */
87  __os_thread_index = i;
88  break;
89  }
90  }
91  ASSERT (__os_thread_index > 0);
92  }
93  while (1)
95  }
96  pthread_exit (0);
97 }
98 
99 static void
101 {
103  vl_msg_api_free (mp);
104  longjmp (mm->rx_thread_jmpbuf, 1);
105 }
106 
107 static 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* */
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);
128 }
129 
130 static void
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;
143 
144  /* Clean out any previous hash table (unlikely) */
146 
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  {
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 int
168 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
169 {
170  svm_region_t *svm;
173  svm_queue_t *vl_input_queue;
174  vl_shmem_hdr_t *shmem_hdr;
175  int rv = 0;
176  void *oldheap;
177  api_main_t *am = &api_main;
178 
179  if (am->my_registration)
180  {
181  clib_warning ("client %s already connected...", name);
182  return -1;
183  }
184 
185  if (am->vlib_rp == 0)
186  {
187  clib_warning ("am->vlib_rp NULL");
188  return -1;
189  }
190 
191  svm = am->vlib_rp;
192  shmem_hdr = am->shmem_hdr;
193 
194  if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
195  {
196  clib_warning ("shmem_hdr / input queue NULL");
197  return -1;
198  }
199 
200  pthread_mutex_lock (&svm->mutex);
201  oldheap = svm_push_data_heap (svm);
202  vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
203  getpid ());
204  svm_pop_heap (oldheap);
205  pthread_mutex_unlock (&svm->mutex);
206 
207  am->my_client_index = ~0;
208  am->my_registration = 0;
209  am->vl_input_queue = vl_input_queue;
210 
212  clib_memset (mp, 0, sizeof (*mp));
213  mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
214  mp->ctx_quota = ctx_quota;
215  mp->input_queue = (uword) vl_input_queue;
216  strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
217 
218  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
219 
220  while (1)
221  {
222  int qstatus;
223  struct timespec ts, tsrem;
224  int i;
225 
226  /* Wait up to 10 seconds */
227  for (i = 0; i < 1000; i++)
228  {
229  qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp,
230  SVM_Q_NOWAIT, 0);
231  if (qstatus == 0)
232  goto read_one_msg;
233  ts.tv_sec = 0;
234  ts.tv_nsec = 10000 * 1000; /* 10 ms */
235  while (nanosleep (&ts, &tsrem) < 0)
236  ts = tsrem;
237  }
238  /* Timeout... */
239  clib_warning ("memclnt_create_reply timeout");
240  return -1;
241 
242  read_one_msg:
243  if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
244  {
245  clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
246  continue;
247  }
248  rv = clib_net_to_host_u32 (rp->response);
249 
250  vl_msg_api_handler ((void *) rp);
251  break;
252  }
253  return (rv);
254 }
255 
256 static void
258 {
259  void *oldheap;
260  api_main_t *am = &api_main;
261 
262  pthread_mutex_lock (&am->vlib_rp->mutex);
263  oldheap = svm_push_data_heap (am->vlib_rp);
265  pthread_mutex_unlock (&am->vlib_rp->mutex);
266  svm_pop_heap (oldheap);
267 
268  am->my_client_index = ~0;
269  am->my_registration = 0;
270  am->vl_input_queue = 0;
271 }
272 
273 void
275 {
277  vl_shmem_hdr_t *shmem_hdr;
278  api_main_t *am = &api_main;
279 
280  ASSERT (am->vlib_rp);
281  shmem_hdr = am->shmem_hdr;
282  ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
283 
285  clib_memset (mp, 0, sizeof (*mp));
286  mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
287  mp->index = am->my_client_index;
288  mp->handle = (uword) am->my_registration;
289  mp->do_cleanup = do_cleanup;
290 
291  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
292 }
293 
294 int
296 {
298  svm_queue_t *vl_input_queue;
299  api_main_t *am = &api_main;
300  time_t begin;
301 
302  vl_input_queue = am->vl_input_queue;
303  vl_client_send_disconnect (0 /* wait for reply */ );
304 
305  /*
306  * Have to be careful here, in case the client is disconnecting
307  * because e.g. the vlib process died, or is unresponsive.
308  */
309  begin = time (0);
310  while (1)
311  {
312  time_t now;
313 
314  now = time (0);
315 
316  if (now >= (begin + 2))
317  {
318  clib_warning ("peer unresponsive, give up");
319  am->my_client_index = ~0;
320  am->my_registration = 0;
321  am->shmem_hdr = 0;
322  return -1;
323  }
324  if (svm_queue_sub (vl_input_queue, (u8 *) & rp, SVM_Q_NOWAIT, 0) < 0)
325  continue;
326 
327  /* drain the queue */
328  if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
329  {
330  clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
331  vl_msg_api_handler ((void *) rp);
332  continue;
333  }
334  vl_msg_api_handler ((void *) rp);
335  break;
336  }
337 
339  return 0;
340 }
341 
342 /**
343  * Stave off the binary API dead client reaper
344  * Only sent to inactive clients
345  */
346 static void
348 {
349  vl_api_memclnt_keepalive_reply_t *rmp;
350  api_main_t *am;
351  vl_shmem_hdr_t *shmem_hdr;
352 
353  am = &api_main;
354  shmem_hdr = am->shmem_hdr;
355 
356  rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
357  clib_memset (rmp, 0, sizeof (*rmp));
358  rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
359  rmp->context = mp->context;
360  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
361 }
362 
363 #define foreach_api_msg \
364 _(RX_THREAD_EXIT, rx_thread_exit) \
365 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
366 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \
367 _(MEMCLNT_KEEPALIVE, memclnt_keepalive)
368 
369 void
371 {
372 
373 #define _(N,n) \
374  vl_msg_api_set_handlers(VL_API_##N, #n, \
375  vl_api_##n##_t_handler, \
376  noop_handler, \
377  vl_api_##n##_t_endian, \
378  vl_api_##n##_t_print, \
379  sizeof(vl_api_##n##_t), 1);
381 #undef _
382 }
383 
384 int
385 vl_client_api_map (const char *region_name)
386 {
387  int rv;
388 
389  if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
390  return rv;
391 
393  return 0;
394 }
395 
396 void
398 {
400 }
401 
402 u8
404 {
405  return (memory_client_main.connected_to_vlib != 0);
406 }
407 
408 static int
409 connect_to_vlib_internal (const char *svm_name,
410  const char *client_name,
411  int rx_queue_size, int want_pthread, int do_map)
412 {
413  int rv = 0;
415 
416  if (do_map && (rv = vl_client_api_map (svm_name)))
417  {
418  clib_warning ("vl_client_api map rv %d", rv);
419  return rv;
420  }
421 
422  if (vl_client_connect (client_name, 0 /* punt quota */ ,
423  rx_queue_size /* input queue */ ) < 0)
424  {
426  return -1;
427  }
428 
429  /* Start the rx queue thread */
430 
431  if (want_pthread)
432  {
433  rv = pthread_create (&mm->rx_thread_handle,
434  NULL /*attr */ , rx_thread_fn, 0);
435  if (rv)
436  clib_warning ("pthread_create returned %d", rv);
437  }
438 
439  mm->connected_to_vlib = 1;
440  return 0;
441 }
442 
443 int
444 vl_client_connect_to_vlib (const char *svm_name,
445  const char *client_name, int rx_queue_size)
446 {
447  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
448  1 /* want pthread */ ,
449  1 /* do map */ );
450 }
451 
452 int
454  const char *client_name,
455  int rx_queue_size)
456 {
457  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
458  0 /* want pthread */ ,
459  1 /* do map */ );
460 }
461 
462 int
463 vl_client_connect_to_vlib_no_map (const char *svm_name,
464  const char *client_name, int rx_queue_size)
465 {
466  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
467  1 /* want pthread */ ,
468  0 /* dont map */ );
469 }
470 
471 static void
473 {
475  api_main_t *am = &api_main;
476  uword junk;
477 
478  if (mm->rx_thread_jmpbuf_valid)
479  {
481  ep = vl_msg_api_alloc (sizeof (*ep));
482  ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
483  vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep);
484  pthread_join (mm->rx_thread_handle, (void **) &junk);
485  }
486  if (mm->connected_to_vlib)
487  {
489  if (do_unmap)
491  }
492  clib_memset (mm, 0, sizeof (*mm));
493 }
494 
495 void
497 {
499 }
500 
501 void
503 {
505 }
506 
509 {
511  i32 retval = ntohl (mp->retval);
512 
513  mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
514  mm->first_msg_id_reply_ready = 1;
515 }
516 
517 u16
518 vl_client_get_first_plugin_msg_id (const char *plugin_name)
519 {
521  api_main_t *am = &api_main;
523  f64 timeout;
524  void *old_handler;
525  clib_time_t clib_time;
526  u16 rv = ~0;
527 
528  if (strlen (plugin_name) + 1 > sizeof (mp->name))
529  return (rv);
530 
531  clib_memset (&clib_time, 0, sizeof (clib_time));
532  clib_time_init (&clib_time);
533 
534  /* Push this plugin's first_msg_id_reply handler */
535  old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY];
536  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *)
538 
539  /* Ask the data-plane for the message-ID base of the indicated plugin */
540  mm->first_msg_id_reply_ready = 0;
541 
542  /* Not using shm client */
543  if (!am->my_registration)
544  {
545  mp = vl_socket_client_msg_alloc (sizeof (*mp));
546  clib_memset (mp, 0, sizeof (*mp));
547  mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
548  mp->client_index = am->my_client_index;
549  strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
550 
551  if (vl_socket_client_write () <= 0)
552  goto sock_err;
553  if (vl_socket_client_read (1))
554  goto sock_err;
555 
556  if (mm->first_msg_id_reply_ready == 1)
557  {
558  rv = mm->first_msg_id_reply;
559  goto result;
560  }
561 
562  sock_err:
563  /* Restore old handler */
564  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
565 
566  return -1;
567  }
568  else
569  {
570  mp = vl_msg_api_alloc (sizeof (*mp));
571  clib_memset (mp, 0, sizeof (*mp));
572  mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
573  mp->client_index = am->my_client_index;
574  strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
575 
577 
578  /* Synchronously wait for the answer */
579  timeout = clib_time_now (&clib_time) + 1.0;
580  while (clib_time_now (&clib_time) < timeout)
581  {
582  if (mm->first_msg_id_reply_ready == 1)
583  {
584  rv = mm->first_msg_id_reply;
585  goto result;
586  }
587  }
588  /* Restore old handler */
589  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
590 
591  return rv;
592  }
593 
594 result:
595 
596  /* Restore the old handler */
597  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
598 
599  if (rv == (u16) ~ 0)
600  clib_warning ("plugin '%s' not registered", plugin_name);
601 
602  return rv;
603 }
604 
605 /*
606  * fd.io coding-style-patch-verification: ON
607  *
608  * Local Variables:
609  * eval: (c-set-style "gnu")
610  * End:
611  */
void * clib_per_cpu_mheaps[CLIB_MAX_MHEAPS]
Definition: mem_dlmalloc.c:23
static void svm_pop_heap(void *oldheap)
Definition: svm.h:94
int vl_client_connect_to_vlib(const char *svm_name, const char *client_name, int rx_queue_size)
static u64 unserialize_likely_small_unsigned_integer(serialize_main_t *m)
Definition: serialize.h:254
u8 vl_mem_client_is_connected(void)
int vl_map_shmem(const char *region_name, int is_vlib)
int vl_client_connect_to_vlib_no_map(const char *svm_name, const char *client_name, int rx_queue_size)
int vl_socket_client_read(int wait)
Definition: socket_client.c:56
int vl_client_connect_to_vlib_no_rx_pthread(const char *svm_name, const char *client_name, int rx_queue_size)
int my_client_index
All VLIB-side message handlers use my_client_index to identify the queue / client.
Definition: api_common.h:310
#define NULL
Definition: clib.h:58
static f64 clib_time_now(clib_time_t *c)
Definition: time.h:215
static void disconnect_from_vlib_internal(u8 do_unmap)
static void vl_api_memclnt_keepalive_t_handler(vl_api_memclnt_keepalive_t *mp)
Stave off the binary API dead client reaper Only sent to inactive clients.
int i
#define hash_set_mem(h, key, value)
Definition: hash.h:275
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
svm_queue_t * vl_input_queue
Peer input queue pointer.
Definition: api_common.h:304
void * vl_msg_api_alloc(int nbytes)
static void noop_handler(void *notused)
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
void vl_client_api_unmap(void)
void * vl_socket_client_msg_alloc(int nbytes)
vl_api_registration_t * my_registration
This is the (shared VM) address of the registration, don&#39;t use it to id the connection since it can&#39;t...
Definition: api_common.h:316
int svm_queue_sub(svm_queue_t *q, u8 *elem, svm_q_conditional_wait_t cond, u32 time)
Definition: queue.c:348
pthread_t rx_thread_handle
Definition: memory_client.c:52
void unserialize_open_data(serialize_main_t *m, u8 *data, uword n_data_bytes)
Definition: serialize.c:891
static void * svm_push_data_heap(svm_region_t *rp)
Definition: svm.h:86
static void * rx_thread_fn(void *arg)
Definition: memory_client.c:61
svm_region_t * vlib_rp
Current binary api segment descriptor.
Definition: api_common.h:255
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:265
unsigned int u32
Definition: types.h:88
#define hash_create_string(elts, value_bytes)
Definition: hash.h:690
static void vl_api_memclnt_create_reply_t_handler(vl_api_memclnt_create_reply_t *mp)
int vl_client_connect(const char *name, int ctx_quota, int input_queue_size)
void vl_client_send_disconnect(u8 do_cleanup)
unsigned short u16
Definition: types.h:57
#define hash_free(h)
Definition: hash.h:310
void clib_time_init(clib_time_t *c)
Definition: time.c:178
u8 name[64]
Definition: memclnt.api:152
void unserialize_cstring(serialize_main_t *m, char **s)
Definition: serialize.c:178
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:202
static void vl_api_memclnt_delete_reply_t_handler(vl_api_memclnt_delete_reply_t *mp)
An API client registration, only in vpp/vlib.
Definition: api_common.h:45
void vl_msg_api_send_shmem(svm_queue_t *q, u8 *elem)
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
#define clib_warning(format, args...)
Definition: error.h:59
int vl_client_api_map(const char *region_name)
static void unserialize_integer(serialize_main_t *m, void *x, u32 n_bytes)
Definition: serialize.h:201
u16 vl_client_get_first_plugin_msg_id(const char *plugin_name)
#define ARRAY_LEN(x)
Definition: clib.h:62
#define foreach_api_msg
svm_queue_t * vl_input_queue
Definition: memory_shared.h:84
void vl_unmap_shmem_client(void)
void svm_queue_free(svm_queue_t *q)
Definition: queue.c:89
signed int i32
Definition: types.h:77
#define uword_to_pointer(u, type)
Definition: types.h:136
#define ASSERT(truth)
svm_queue_t * svm_queue_alloc_and_init(int nels, int elsize, int consumer_pid)
Allocate and initialize svm queue.
Definition: queue.c:72
void vl_msg_api_queue_handler(svm_queue_t *q)
Definition: api_shared.c:750
void vl_msg_api_free(void *)
void vl_client_install_client_message_handlers(void)
void vl_client_disconnect_from_vlib_no_unmap(void)
static void vl_api_get_first_msg_id_reply_t_handler(vl_api_get_first_msg_id_reply_t *mp)
static void vl_api_name_and_crc_free(void)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:373
int vl_client_disconnect(void)
u64 uword
Definition: types.h:112
static void vl_api_rx_thread_exit_t_handler(vl_api_rx_thread_exit_t *mp)
struct _svm_queue svm_queue_t
memory_client_main_t memory_client_main
Definition: memory_client.c:58
void(** msg_handlers)(void *)
Message handler vector.
Definition: api_common.h:205
void vl_client_disconnect_from_vlib(void)
static int connect_to_vlib_internal(const char *svm_name, const char *client_name, int rx_queue_size, int want_pthread, int do_map)
int vl_socket_client_write(void)
void * vl_msg_api_alloc_as_if_client(int nbytes)
uword * msg_index_by_name_and_crc
client message index hash table
Definition: api_common.h:322
api_main_t api_main
Definition: api_shared.c:35
volatile u8 first_msg_id_reply_ready
Definition: memory_client.c:54
pthread_mutex_t mutex
Definition: svm_common.h:37
non-blocking call - works with both condvar and eventfd signaling
Definition: queue.h:44
void vl_msg_api_handler(void *the_msg)
Definition: api_shared.c:540