FD.io VPP  v18.04-17-g3a0d853
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  serialize_main_t _sm, *sm = &_sm;
111  api_main_t *am = &api_main;
112  u8 *tblv;
113  u32 nmsgs;
114  int i;
115  u8 *name_and_crc;
116  u32 msg_index;
117 
118  am->my_client_index = mp->index;
120 
121  /* Clean out any previous hash table (unlikely) */
123  {
124  int i;
125  u8 **keys = 0;
126  hash_pair_t *hp;
127  /* *INDENT-OFF* */
129  ({
130  vec_add1 (keys, (u8 *) hp->key);
131  }));
132  /* *INDENT-ON* */
133  for (i = 0; i < vec_len (keys); i++)
134  vec_free (keys[i]);
135  vec_free (keys);
136  }
137 
139 
140  /* Recreate the vnet-side API message handler table */
141  tblv = uword_to_pointer (mp->message_table, u8 *);
142  unserialize_open_data (sm, tblv, vec_len (tblv));
143  unserialize_integer (sm, &nmsgs, sizeof (u32));
144 
145  for (i = 0; i < nmsgs; i++)
146  {
148  unserialize_cstring (sm, (char **) &name_and_crc);
149  hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index);
150  }
151 }
152 
153 static void
154 noop_handler (void *notused)
155 {
156 }
157 
158 int
159 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
160 {
161  svm_region_t *svm;
164  svm_queue_t *vl_input_queue;
166  int rv = 0;
167  void *oldheap;
168  api_main_t *am = &api_main;
169 
170  if (am->my_registration)
171  {
172  clib_warning ("client %s already connected...", name);
173  return -1;
174  }
175 
176  if (am->vlib_rp == 0)
177  {
178  clib_warning ("am->vlib_rp NULL");
179  return -1;
180  }
181 
182  svm = am->vlib_rp;
183  shmem_hdr = am->shmem_hdr;
184 
185  if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
186  {
187  clib_warning ("shmem_hdr / input queue NULL");
188  return -1;
189  }
190 
191  pthread_mutex_lock (&svm->mutex);
192  oldheap = svm_push_data_heap (svm);
193  vl_input_queue = svm_queue_init (input_queue_size, sizeof (uword),
194  getpid (), 0);
195  pthread_mutex_unlock (&svm->mutex);
196  svm_pop_heap (oldheap);
197 
198  am->my_client_index = ~0;
199  am->my_registration = 0;
200  am->vl_input_queue = vl_input_queue;
201 
203  memset (mp, 0, sizeof (*mp));
204  mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
205  mp->ctx_quota = ctx_quota;
206  mp->input_queue = (uword) vl_input_queue;
207  strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
208 
209  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
210 
211  while (1)
212  {
213  int qstatus;
214  struct timespec ts, tsrem;
215  int i;
216 
217  /* Wait up to 10 seconds */
218  for (i = 0; i < 1000; i++)
219  {
220  qstatus = svm_queue_sub (vl_input_queue, (u8 *) & rp,
221  SVM_Q_NOWAIT, 0);
222  if (qstatus == 0)
223  goto read_one_msg;
224  ts.tv_sec = 0;
225  ts.tv_nsec = 10000 * 1000; /* 10 ms */
226  while (nanosleep (&ts, &tsrem) < 0)
227  ts = tsrem;
228  }
229  /* Timeout... */
230  clib_warning ("memclnt_create_reply timeout");
231  return -1;
232 
233  read_one_msg:
234  if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
235  {
236  clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
237  continue;
238  }
239  rv = clib_net_to_host_u32 (rp->response);
240 
241  vl_msg_api_handler ((void *) rp);
242  break;
243  }
244  return (rv);
245 }
246 
247 static void
249 {
250  void *oldheap;
251  api_main_t *am = &api_main;
252 
253  pthread_mutex_lock (&am->vlib_rp->mutex);
254  oldheap = svm_push_data_heap (am->vlib_rp);
256  pthread_mutex_unlock (&am->vlib_rp->mutex);
257  svm_pop_heap (oldheap);
258 
259  am->my_client_index = ~0;
260  am->my_registration = 0;
261  am->vl_input_queue = 0;
262 }
263 
264 int
266 {
269  svm_queue_t *vl_input_queue;
271  time_t begin;
272  api_main_t *am = &api_main;
273 
274  ASSERT (am->vlib_rp);
275  shmem_hdr = am->shmem_hdr;
276  ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
277 
278  vl_input_queue = am->vl_input_queue;
279 
281  memset (mp, 0, sizeof (*mp));
282  mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
283  mp->index = am->my_client_index;
284  mp->handle = (uword) am->my_registration;
285 
286  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
287 
288  /*
289  * Have to be careful here, in case the client is disconnecting
290  * because e.g. the vlib process died, or is unresponsive.
291  */
292  begin = time (0);
293  while (1)
294  {
295  time_t now;
296 
297  now = time (0);
298 
299  if (now >= (begin + 2))
300  {
301  clib_warning ("peer unresponsive, give up");
302  am->my_client_index = ~0;
303  am->my_registration = 0;
304  am->shmem_hdr = 0;
305  return -1;
306  }
307  if (svm_queue_sub (vl_input_queue, (u8 *) & rp, SVM_Q_NOWAIT, 0) < 0)
308  continue;
309 
310  /* drain the queue */
311  if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
312  {
313  clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
314  vl_msg_api_handler ((void *) rp);
315  continue;
316  }
317  vl_msg_api_handler ((void *) rp);
318  break;
319  }
320  return 0;
321 }
322 
323 /**
324  * Stave off the binary API dead client reaper
325  * Only sent to inactive clients
326  */
327 static void
329 {
330  vl_api_memclnt_keepalive_reply_t *rmp;
331  api_main_t *am;
333 
334  am = &api_main;
335  shmem_hdr = am->shmem_hdr;
336 
337  rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp));
338  memset (rmp, 0, sizeof (*rmp));
339  rmp->_vl_msg_id = ntohs (VL_API_MEMCLNT_KEEPALIVE_REPLY);
340  rmp->context = mp->context;
341  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
342 }
343 
344 #define foreach_api_msg \
345 _(RX_THREAD_EXIT, rx_thread_exit) \
346 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
347 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply) \
348 _(MEMCLNT_KEEPALIVE, memclnt_keepalive)
349 
350 void
352 {
353 
354 #define _(N,n) \
355  vl_msg_api_set_handlers(VL_API_##N, #n, \
356  vl_api_##n##_t_handler, \
357  noop_handler, \
358  vl_api_##n##_t_endian, \
359  vl_api_##n##_t_print, \
360  sizeof(vl_api_##n##_t), 1);
362 #undef _
363 }
364 
365 int
366 vl_client_api_map (const char *region_name)
367 {
368  int rv;
369 
370  if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
371  return rv;
372 
374  return 0;
375 }
376 
377 void
379 {
381 }
382 
383 u8
385 {
386  return (memory_client_main.connected_to_vlib != 0);
387 }
388 
389 static int
390 connect_to_vlib_internal (const char *svm_name,
391  const char *client_name,
392  int rx_queue_size, int want_pthread, int do_map)
393 {
394  int rv = 0;
396 
397  if (do_map && (rv = vl_client_api_map (svm_name)))
398  {
399  clib_warning ("vl_client_api map rv %d", rv);
400  return rv;
401  }
402 
403  if (vl_client_connect (client_name, 0 /* punt quota */ ,
404  rx_queue_size /* input queue */ ) < 0)
405  {
407  return -1;
408  }
409 
410  /* Start the rx queue thread */
411 
412  if (want_pthread)
413  {
414  rv = pthread_create (&mm->rx_thread_handle,
415  NULL /*attr */ , rx_thread_fn, 0);
416  if (rv)
417  clib_warning ("pthread_create returned %d", rv);
418  }
419 
420  mm->connected_to_vlib = 1;
421  return 0;
422 }
423 
424 int
425 vl_client_connect_to_vlib (const char *svm_name,
426  const char *client_name, int rx_queue_size)
427 {
428  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
429  1 /* want pthread */ ,
430  1 /* do map */ );
431 }
432 
433 int
435  const char *client_name,
436  int rx_queue_size)
437 {
438  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
439  0 /* want pthread */ ,
440  1 /* do map */ );
441 }
442 
443 int
444 vl_client_connect_to_vlib_no_map (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  0 /* dont map */ );
450 }
451 
452 static void
454 {
456  api_main_t *am = &api_main;
457  uword junk;
458 
459  if (mm->rx_thread_jmpbuf_valid)
460  {
462  ep = vl_msg_api_alloc (sizeof (*ep));
463  ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
464  vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep);
465  pthread_join (mm->rx_thread_handle, (void **) &junk);
466  }
467  if (mm->connected_to_vlib)
468  {
470  if (do_unmap)
472  }
473  memset (mm, 0, sizeof (*mm));
474 }
475 
476 void
478 {
480 }
481 
482 void
484 {
486 }
487 
490 {
492  i32 retval = ntohl (mp->retval);
493 
494  mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
495  mm->first_msg_id_reply_ready = 1;
496 }
497 
498 u16
499 vl_client_get_first_plugin_msg_id (const char *plugin_name)
500 {
502  api_main_t *am = &api_main;
504  f64 timeout;
505  void *old_handler;
506  clib_time_t clib_time;
507  u16 rv = ~0;
508 
509  if (strlen (plugin_name) + 1 > sizeof (mp->name))
510  return (rv);
511 
512  memset (&clib_time, 0, sizeof (clib_time));
513  clib_time_init (&clib_time);
514 
515  /* Push this plugin's first_msg_id_reply handler */
516  old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY];
517  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *)
519 
520  /* Ask the data-plane for the message-ID base of the indicated plugin */
521  mm->first_msg_id_reply_ready = 0;
522 
523  /* Not using shm client */
524  if (!am->my_registration)
525  {
526  mp = vl_socket_client_msg_alloc (sizeof (*mp));
527  memset (mp, 0, sizeof (*mp));
528  mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
529  mp->client_index = am->my_client_index;
530  strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
531 
532  if (vl_socket_client_write () <= 0)
533  goto sock_err;
534  if (vl_socket_client_read (1))
535  goto sock_err;
536 
537  if (mm->first_msg_id_reply_ready == 1)
538  {
539  rv = mm->first_msg_id_reply;
540  goto result;
541  }
542 
543  sock_err:
544  /* Restore old handler */
545  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
546 
547  return -1;
548  }
549  else
550  {
551  mp = vl_msg_api_alloc (sizeof (*mp));
552  memset (mp, 0, sizeof (*mp));
553  mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
554  mp->client_index = am->my_client_index;
555  strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
556 
558 
559  /* Synchronously wait for the answer */
560  timeout = clib_time_now (&clib_time) + 1.0;
561  while (clib_time_now (&clib_time) < timeout)
562  {
563  if (mm->first_msg_id_reply_ready == 1)
564  {
565  rv = mm->first_msg_id_reply;
566  goto result;
567  }
568  }
569  /* Restore old handler */
570  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
571 
572  return rv;
573  }
574 
575 result:
576 
577  /* Restore the old handler */
578  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
579 
580  if (rv == (u16) ~ 0)
581  clib_warning ("plugin '%s' not registered", plugin_name);
582 
583  return rv;
584 }
585 
586 /*
587  * fd.io coding-style-patch-verification: ON
588  *
589  * Local Variables:
590  * eval: (c-set-style "gnu")
591  * End:
592  */
void * clib_per_cpu_mheaps[CLIB_MAX_MHEAPS]
Definition: mem_mheap.c:46
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:307
#define NULL
Definition: clib.h:55
static f64 clib_time_now(clib_time_t *c)
Definition: time.h:200
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:274
svm_queue_t * vl_input_queue
Peer input queue pointer.
Definition: api_common.h:301
void * vl_msg_api_alloc(int nbytes)
static void noop_handler(void *notused)
void vl_client_api_unmap(void)
svm_queue_t * svm_queue_init(int nels, int elsize, int consumer_pid, int signal_when_queue_non_empty)
Definition: queue.c:51
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:313
int svm_queue_sub(svm_queue_t *q, u8 *elem, svm_q_conditional_wait_t cond, u32 time)
Definition: queue.c:303
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:890
static void * svm_push_data_heap(svm_region_t *rp)
Definition: svm.h:86
int i32
Definition: types.h:81
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:252
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:262
#define hash_create_string(elts, value_bytes)
Definition: hash.h:675
vl_shmem_hdr_t * shmem_hdr
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 clib_time_init(clib_time_t *c)
Definition: time.c:178
void unserialize_cstring(serialize_main_t *m, char **s)
Definition: serialize.c:178
#define uword_to_pointer(u, type)
Definition: types.h:136
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:199
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:44
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:336
#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:59
#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:95
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
void vl_msg_api_queue_handler(svm_queue_t *q)
Definition: api_shared.c:757
void vl_msg_api_free(void *)
u64 uword
Definition: types.h:112
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)
unsigned short u16
Definition: types.h:57
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
#define hash_foreach_pair(p, v, body)
Iterate over hash pairs.
Definition: hash.h:372
int vl_client_disconnect(void)
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:202
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
Definition: queue.h:49
void vl_msg_api_handler(void *the_msg)
Definition: api_shared.c:547