FD.io VPP  v19.08.2-294-g37e99c22d
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 
48 
49 static void *
50 rx_thread_fn (void *arg)
51 {
52  svm_queue_t *q;
54  api_main_t *am = &api_main;
55 
56  q = am->vl_input_queue;
57 
58  /* So we can make the rx thread terminate cleanly */
59  if (setjmp (mm->rx_thread_jmpbuf) == 0)
60  {
61  mm->rx_thread_jmpbuf_valid = 1;
63  while (1)
65  }
66  pthread_exit (0);
67 }
68 
69 static void
71 {
73  if (mm->rx_thread_jmpbuf_valid)
74  longjmp (mm->rx_thread_jmpbuf, 1);
75 }
76 
77 static void
79 {
80  api_main_t *am = &api_main;
81  int i;
82  u8 **keys = 0;
83  hash_pair_t *hp;
84 
86  return;
87 
88  /* *INDENT-OFF* */
90  ({
91  vec_add1 (keys, (u8 *) hp->key);
92  }));
93  /* *INDENT-ON* */
94  for (i = 0; i < vec_len (keys); i++)
95  vec_free (keys[i]);
96  vec_free (keys);
98 }
99 
100 static void
102 {
103  serialize_main_t _sm, *sm = &_sm;
104  api_main_t *am = &api_main;
105  u8 *tblv;
106  u32 nmsgs;
107  int i;
108  u8 *name_and_crc;
109  u32 msg_index;
110 
111  am->my_client_index = mp->index;
113 
114  /* Clean out any previous hash table (unlikely) */
116 
118 
119  /* Recreate the vnet-side API message handler table */
120  tblv = uword_to_pointer (mp->message_table, u8 *);
121  unserialize_open_data (sm, tblv, vec_len (tblv));
122  unserialize_integer (sm, &nmsgs, sizeof (u32));
123 
124  for (i = 0; i < nmsgs; i++)
125  {
127  unserialize_cstring (sm, (char **) &name_and_crc);
128  hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index);
129  }
130 }
131 
132 static void
133 noop_handler (void *notused)
134 {
135 }
136 
137 void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem);
138 int
139 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
140 {
141  svm_region_t *svm;
144  svm_queue_t *vl_input_queue;
145  vl_shmem_hdr_t *shmem_hdr;
146  int rv = 0;
147  void *oldheap;
148  api_main_t *am = &api_main;
149 
150  if (am->my_registration)
151  {
152  clib_warning ("client %s already connected...", name);
153  return -1;
154  }
155 
156  if (am->vlib_rp == 0)
157  {
158  clib_warning ("am->vlib_rp NULL");
159  return -1;
160  }
161 
162  svm = am->vlib_rp;
163  shmem_hdr = am->shmem_hdr;
164 
165  if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
166  {
167  clib_warning ("shmem_hdr / input queue NULL");
168  return -1;
169  }
170 
171  pthread_mutex_lock (&svm->mutex);
172  oldheap = svm_push_data_heap (svm);
173  vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
174  getpid ());
175  svm_pop_heap (oldheap);
176  pthread_mutex_unlock (&svm->mutex);
177 
178  am->my_client_index = ~0;
179  am->my_registration = 0;
180  am->vl_input_queue = vl_input_queue;
181 
183  clib_memset (mp, 0, sizeof (*mp));
184  mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
185  mp->ctx_quota = ctx_quota;
186  mp->input_queue = (uword) vl_input_queue;
187  strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
188 
189  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
190 
191  while (1)
192  {
193  int qstatus;
194  struct timespec ts, tsrem;
195  int i;
196 
197  /* Wait up to 10 seconds */
198  for (i = 0; i < 1000; i++)
199  {
200  qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp,
201  SVM_Q_NOWAIT, 0);
202  if (qstatus == 0)
203  goto read_one_msg;
204  ts.tv_sec = 0;
205  ts.tv_nsec = 10000 * 1000; /* 10 ms */
206  while (nanosleep (&ts, &tsrem) < 0)
207  ts = tsrem;
208  }
209  /* Timeout... */
210  clib_warning ("memclnt_create_reply timeout");
211  return -1;
212 
213  read_one_msg:
214  if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
215  {
216  clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
217  continue;
218  }
219  rv = clib_net_to_host_u32 (rp->response);
220 
221  vl_msg_api_handler ((void *) rp);
222  break;
223  }
224  return (rv);
225 }
226 
227 static void
229 {
230  void *oldheap;
231  api_main_t *am = &api_main;
232 
233  pthread_mutex_lock (&am->vlib_rp->mutex);
234  oldheap = svm_push_data_heap (am->vlib_rp);
236  pthread_mutex_unlock (&am->vlib_rp->mutex);
237  svm_pop_heap (oldheap);
238 
239  am->my_client_index = ~0;
240  am->my_registration = 0;
241  am->vl_input_queue = 0;
242 }
243 
244 void
246 {
248  vl_shmem_hdr_t *shmem_hdr;
249  api_main_t *am = &api_main;
250 
251  ASSERT (am->vlib_rp);
252  shmem_hdr = am->shmem_hdr;
253  ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
254 
256  clib_memset (mp, 0, sizeof (*mp));
257  mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
258  mp->index = am->my_client_index;
259  mp->handle = (uword) am->my_registration;
260  mp->do_cleanup = do_cleanup;
261 
262  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
263 }
264 
265 int
267 {
269  svm_queue_t *vl_input_queue;
270  api_main_t *am = &api_main;
271  time_t begin;
272 
273  vl_input_queue = am->vl_input_queue;
274  vl_client_send_disconnect (0 /* wait for reply */ );
275 
276  /*
277  * Have to be careful here, in case the client is disconnecting
278  * because e.g. the vlib process died, or is unresponsive.
279  */
280  begin = time (0);
281  while (1)
282  {
283  time_t now;
284 
285  now = time (0);
286 
287  if (now >= (begin + 2))
288  {
289  clib_warning ("peer unresponsive, give up");
290  am->my_client_index = ~0;
291  am->my_registration = 0;
292  am->shmem_hdr = 0;
293  return -1;
294  }
295  if (svm_queue_sub (vl_input_queue, (u8 *) & rp, SVM_Q_NOWAIT, 0) < 0)
296  continue;
297 
298  /* drain the queue */
299  if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
300  {
301  clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
302  vl_msg_api_handler ((void *) rp);
303  continue;
304  }
305  vl_msg_api_handler ((void *) rp);
306  break;
307  }
308 
310  return 0;
311 }
312 
313 /**
314  * Stave off the binary API dead client reaper
315  * Only sent to inactive clients
316  */
317 static void
319 {
320  vl_api_memclnt_keepalive_reply_t *rmp;
321  api_main_t *am;
322  vl_shmem_hdr_t *shmem_hdr;
323 
324  am = &api_main;
325  shmem_hdr = am->shmem_hdr;
326 
327  rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
328  clib_memset (rmp, 0, sizeof (*rmp));
329  rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
330  rmp->context = mp->context;
331  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
332 }
333 
334 #define foreach_api_msg \
335 _(RX_THREAD_EXIT, rx_thread_exit) \
336 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
337 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \
338 _(MEMCLNT_KEEPALIVE, memclnt_keepalive)
339 
340 void
342 {
343 
344 #define _(N,n) \
345  vl_msg_api_set_handlers(VL_API_##N, #n, \
346  vl_api_##n##_t_handler, \
347  noop_handler, \
348  vl_api_##n##_t_endian, \
349  vl_api_##n##_t_print, \
350  sizeof(vl_api_##n##_t), 1);
352 #undef _
353 }
354 
355 int
356 vl_client_api_map (const char *region_name)
357 {
358  int rv;
359 
360  if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
361  return rv;
362 
364  return 0;
365 }
366 
367 void
369 {
371 }
372 
373 u8
375 {
376  return (memory_client_main.connected_to_vlib != 0);
377 }
378 
379 static int
380 connect_to_vlib_internal (const char *svm_name,
381  const char *client_name,
382  int rx_queue_size, void *(*thread_fn) (void *),
383  int do_map)
384 {
385  int rv = 0;
387 
388  if (do_map && (rv = vl_client_api_map (svm_name)))
389  {
390  clib_warning ("vl_client_api map rv %d", rv);
391  return rv;
392  }
393 
394  if (vl_client_connect (client_name, 0 /* punt quota */ ,
395  rx_queue_size /* input queue */ ) < 0)
396  {
398  return -1;
399  }
400 
401  /* Start the rx queue thread */
402 
403  if (thread_fn)
404  {
405  rv = pthread_create (&mm->rx_thread_handle,
406  NULL /*attr */ , thread_fn, 0);
407  if (rv)
408  clib_warning ("pthread_create returned %d", rv);
409  }
410 
411  mm->connected_to_vlib = 1;
412  return 0;
413 }
414 
415 int
416 vl_client_connect_to_vlib (const char *svm_name,
417  const char *client_name, int rx_queue_size)
418 {
419  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
420  rx_thread_fn, 1 /* do map */ );
421 }
422 
423 int
425  const char *client_name,
426  int rx_queue_size)
427 {
428  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
429  0 /* no rx_thread_fn */ ,
430  1 /* do map */ );
431 }
432 
433 int
434 vl_client_connect_to_vlib_no_map (const char *svm_name,
435  const char *client_name, int rx_queue_size)
436 {
437  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
438  rx_thread_fn, 0 /* dont map */ );
439 }
440 
441 int
443  const char *client_name,
444  int rx_queue_size)
445 {
446  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
447  0 /* want pthread */ ,
448  0 /* dont map */ );
449 }
450 
451 int
453  const char *client_name,
454  int rx_queue_size,
455  void *(*thread_fn) (void *))
456 {
457  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
458  thread_fn, 1 /* do map */ );
459 }
460 
461 
462 static void
464 {
466  api_main_t *am = &api_main;
467  uword junk;
468 
469  if (mm->rx_thread_jmpbuf_valid)
470  {
472  ep = vl_msg_api_alloc (sizeof (*ep));
473  ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
474  vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep);
475  pthread_join (mm->rx_thread_handle, (void **) &junk);
476  }
477  if (mm->connected_to_vlib)
478  {
480  if (do_unmap)
482  }
483  clib_memset (mm, 0, sizeof (*mm));
484 }
485 
486 void
488 {
490 }
491 
492 void
494 {
496 }
497 
500 {
502  i32 retval = ntohl (mp->retval);
503 
504  mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
505  mm->first_msg_id_reply_ready = 1;
506 }
507 
508 u16
509 vl_client_get_first_plugin_msg_id (const char *plugin_name)
510 {
512  api_main_t *am = &api_main;
514  f64 timeout;
515  void *old_handler;
516  clib_time_t clib_time;
517  u16 rv = ~0;
518 
519  if (strlen (plugin_name) + 1 > sizeof (mp->name))
520  return (rv);
521 
522  clib_memset (&clib_time, 0, sizeof (clib_time));
523  clib_time_init (&clib_time);
524 
525  /* Push this plugin's first_msg_id_reply handler */
526  old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY];
527  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *)
529 
530  /* Ask the data-plane for the message-ID base of the indicated plugin */
531  mm->first_msg_id_reply_ready = 0;
532 
533  /* Not using shm client */
534  if (!am->my_registration)
535  {
536  mp = vl_socket_client_msg_alloc (sizeof (*mp));
537  clib_memset (mp, 0, sizeof (*mp));
538  mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
539  mp->client_index = am->my_client_index;
540  strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
541 
542  if (vl_socket_client_write () <= 0)
543  goto sock_err;
544  if (vl_socket_client_read (1))
545  goto sock_err;
546 
547  if (mm->first_msg_id_reply_ready == 1)
548  {
549  rv = mm->first_msg_id_reply;
550  goto result;
551  }
552 
553  sock_err:
554  /* Restore old handler */
555  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
556 
557  return -1;
558  }
559  else
560  {
561  mp = vl_msg_api_alloc (sizeof (*mp));
562  clib_memset (mp, 0, sizeof (*mp));
563  mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
564  mp->client_index = am->my_client_index;
565  strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
566 
568 
569  /* Synchronously wait for the answer */
570  timeout = clib_time_now (&clib_time) + 1.0;
571  while (clib_time_now (&clib_time) < timeout)
572  {
573  if (mm->first_msg_id_reply_ready == 1)
574  {
575  rv = mm->first_msg_id_reply;
576  goto result;
577  }
578  }
579  /* Restore old handler */
580  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
581 
582  return rv;
583  }
584 
585 result:
586 
587  /* Restore the old handler */
588  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
589 
590  if (rv == (u16) ~ 0)
591  clib_warning ("plugin '%s' not registered", plugin_name);
592 
593  return rv;
594 }
595 
596 /*
597  * fd.io coding-style-patch-verification: ON
598  *
599  * Local Variables:
600  * eval: (c-set-style "gnu")
601  * End:
602  */
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:311
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 clib_time_now(clib_time_t *c)
Definition: time.h:230
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
svm_queue_t * vl_input_queue
Peer input queue pointer.
Definition: api_common.h:305
void * vl_msg_api_alloc(int nbytes)
static void noop_handler(void *notused)
int vl_client_connect_to_vlib_thread_fn(const char *svm_name, const char *client_name, int rx_queue_size, void *(*thread_fn)(void *))
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:317
int svm_queue_sub(svm_queue_t *q, u8 *elem, svm_q_conditional_wait_t cond, u32 time)
Definition: queue.c:356
static void clib_mem_set_thread_index(void)
Definition: mem.h:64
pthread_t rx_thread_handle
Definition: memory_client.h:32
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:50
svm_region_t * vlib_rp
Current binary api segment descriptor.
Definition: api_common.h:256
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:266
unsigned int u32
Definition: types.h:88
void vl_msg_api_send_shmem(svm_queue_t *q, u8 *elem)
#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
int vl_client_connect_to_vlib_no_rx_pthread_no_map(const char *svm_name, const char *client_name, int rx_queue_size)
void clib_time_init(clib_time_t *c)
Definition: time.c:207
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:203
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:46
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static int connect_to_vlib_internal(const char *svm_name, const char *client_name, int rx_queue_size, void *(*thread_fn)(void *), int do_map)
#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 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:842
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)
Definition: memory_client.c:78
#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)
Definition: memory_client.c:70
struct _svm_queue svm_queue_t
memory_client_main_t memory_client_main
Definition: memory_client.c:47
void(** msg_handlers)(void *)
Message handler vector.
Definition: api_common.h:206
void vl_client_disconnect_from_vlib(void)
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:323
api_main_t api_main
Definition: api_shared.c:35
volatile u8 first_msg_id_reply_ready
Definition: memory_client.h:34
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:632