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