FD.io VPP  v17.10-9-gd594711
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 <stdio.h>
21 #include <stdlib.h>
22 #include <setjmp.h>
23 #include <sys/types.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <netinet/in.h>
27 #include <signal.h>
28 #include <pthread.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <vppinfra/clib.h>
34 #include <vppinfra/vec.h>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/bitmap.h>
37 #include <vppinfra/fifo.h>
38 #include <vppinfra/time.h>
39 #include <vppinfra/mheap.h>
40 #include <vppinfra/heap.h>
41 #include <vppinfra/pool.h>
42 #include <vppinfra/format.h>
43 
44 #include <vlib/vlib.h>
45 #include <vlib/unix/unix.h>
46 #include <vlibmemory/api.h>
47 
49 
50 #define vl_typedefs /* define message structures */
52 #undef vl_typedefs
53 
54 #define vl_endianfun /* define message structures */
56 #undef vl_endianfun
57 
58 /* instantiate all the print functions we know about */
59 #define vl_print(handle, ...) clib_warning (__VA_ARGS__)
60 #define vl_printfun
62 #undef vl_printfun
63 
64 typedef struct
65 {
69  pthread_t rx_thread_handle;
70  /* Plugin message base lookup scheme */
74 
76 
77 static void *
78 rx_thread_fn (void *arg)
79 {
82  api_main_t *am = &api_main;
83  int i;
84 
85  q = am->vl_input_queue;
86 
87  /* So we can make the rx thread terminate cleanly */
88  if (setjmp (mm->rx_thread_jmpbuf) == 0)
89  {
90  mm->rx_thread_jmpbuf_valid = 1;
91  /*
92  * Find an unused slot in the per-cpu-mheaps array,
93  * and grab it for this thread. We need to be able to
94  * push/pop the thread heap without affecting other thread(s).
95  */
96  if (__os_thread_index == 0)
97  {
98  for (i = 0; i < ARRAY_LEN (clib_per_cpu_mheaps); i++)
99  {
100  if (clib_per_cpu_mheaps[i] == 0)
101  {
102  /* Copy the main thread mheap pointer */
104  __os_thread_index = i;
105  break;
106  }
107  }
108  ASSERT (__os_thread_index > 0);
109  }
110  while (1)
112  }
113  pthread_exit (0);
114 }
115 
116 static void
118 {
120  vl_msg_api_free (mp);
121  longjmp (mm->rx_thread_jmpbuf, 1);
122 }
123 
124 static void
126 {
127  serialize_main_t _sm, *sm = &_sm;
128  api_main_t *am = &api_main;
129  u8 *tblv;
130  u32 nmsgs;
131  int i;
132  u8 *name_and_crc;
133  u32 msg_index;
134 
135  am->my_client_index = mp->index;
137 
138  /* Clean out any previous hash table (unlikely) */
140  {
141  int i;
142  u8 **keys = 0;
143  hash_pair_t *hp;
144  /* *INDENT-OFF* */
146  ({
147  vec_add1 (keys, (u8 *) hp->key);
148  }));
149  /* *INDENT-ON* */
150  for (i = 0; i < vec_len (keys); i++)
151  vec_free (keys[i]);
152  vec_free (keys);
153  }
154 
156 
157  /* Recreate the vnet-side API message handler table */
158  tblv = uword_to_pointer (mp->message_table, u8 *);
159  unserialize_open_data (sm, tblv, vec_len (tblv));
160  unserialize_integer (sm, &nmsgs, sizeof (u32));
161 
162  for (i = 0; i < nmsgs; i++)
163  {
165  unserialize_cstring (sm, (char **) &name_and_crc);
166  hash_set_mem (am->msg_index_by_name_and_crc, name_and_crc, msg_index);
167  }
168 }
169 
170 static void
171 noop_handler (void *notused)
172 {
173 }
174 
175 int
176 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
177 {
178  svm_region_t *svm;
181  unix_shared_memory_queue_t *vl_input_queue;
183  int rv = 0;
184  void *oldheap;
185  api_main_t *am = &api_main;
186 
187  if (am->my_registration)
188  {
189  clib_warning ("client %s already connected...", name);
190  return -1;
191  }
192 
193  if (am->vlib_rp == 0)
194  {
195  clib_warning ("am->vlib_rp NULL");
196  return -1;
197  }
198 
199  svm = am->vlib_rp;
200  shmem_hdr = am->shmem_hdr;
201 
202  if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
203  {
204  clib_warning ("shmem_hdr / input queue NULL");
205  return -1;
206  }
207 
208  pthread_mutex_lock (&svm->mutex);
209  oldheap = svm_push_data_heap (svm);
210  vl_input_queue =
211  unix_shared_memory_queue_init (input_queue_size, sizeof (uword),
212  getpid (), 0);
213  pthread_mutex_unlock (&svm->mutex);
214  svm_pop_heap (oldheap);
215 
216  am->my_client_index = ~0;
217  am->my_registration = 0;
218  am->vl_input_queue = vl_input_queue;
219 
221  memset (mp, 0, sizeof (*mp));
222  mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_CREATE);
223  mp->ctx_quota = ctx_quota;
224  mp->input_queue = (uword) vl_input_queue;
225  strncpy ((char *) mp->name, name, sizeof (mp->name) - 1);
226 
227  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
228 
229  while (1)
230  {
231  int qstatus;
232  struct timespec ts, tsrem;
233  int i;
234 
235  /* Wait up to 10 seconds */
236  for (i = 0; i < 1000; i++)
237  {
238  qstatus = unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp,
239  1 /* nowait */ );
240  if (qstatus == 0)
241  goto read_one_msg;
242  ts.tv_sec = 0;
243  ts.tv_nsec = 10000 * 1000; /* 10 ms */
244  while (nanosleep (&ts, &tsrem) < 0)
245  ts = tsrem;
246  }
247  /* Timeout... */
248  clib_warning ("memclnt_create_reply timeout");
249  return -1;
250 
251  read_one_msg:
252  if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_CREATE_REPLY)
253  {
254  clib_warning ("unexpected reply: id %d", ntohs (rp->_vl_msg_id));
255  continue;
256  }
257  rv = clib_net_to_host_u32 (rp->response);
258 
259  vl_msg_api_handler ((void *) rp);
260  break;
261  }
262  return (rv);
263 }
264 
265 static void
267 {
268  void *oldheap;
269  api_main_t *am = &api_main;
270 
271  pthread_mutex_lock (&am->vlib_rp->mutex);
272  oldheap = svm_push_data_heap (am->vlib_rp);
274  pthread_mutex_unlock (&am->vlib_rp->mutex);
275  svm_pop_heap (oldheap);
276 
277  am->my_client_index = ~0;
278  am->my_registration = 0;
279  am->vl_input_queue = 0;
280 }
281 
282 void
284 {
287  unix_shared_memory_queue_t *vl_input_queue;
289  time_t begin;
290  api_main_t *am = &api_main;
291 
292  ASSERT (am->vlib_rp);
293  shmem_hdr = am->shmem_hdr;
294  ASSERT (shmem_hdr && shmem_hdr->vl_input_queue);
295 
296  vl_input_queue = am->vl_input_queue;
297 
299  memset (mp, 0, sizeof (*mp));
300  mp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE);
301  mp->index = am->my_client_index;
302  mp->handle = (uword) am->my_registration;
303 
304  vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
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 
311  begin = time (0);
312  while (1)
313  {
314  time_t now;
315 
316  now = time (0);
317 
318  if (now >= (begin + 2))
319  {
320  clib_warning ("peer unresponsive, give up");
321  am->my_client_index = ~0;
322  am->my_registration = 0;
323  am->shmem_hdr = 0;
324  break;
325  }
326  if (unix_shared_memory_queue_sub (vl_input_queue, (u8 *) & rp, 1) < 0)
327  continue;
328 
329  /* drain the queue */
330  if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
331  {
332  clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
333  vl_msg_api_handler ((void *) rp);
334  continue;
335  }
336  vl_msg_api_handler ((void *) rp);
337  break;
338  }
339 }
340 
341 #define foreach_api_msg \
342 _(RX_THREAD_EXIT, rx_thread_exit) \
343 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
344 _(MEMCLNT_DELETE_REPLY, memclnt_delete_reply)
345 
346 
347 int
348 vl_client_api_map (const char *region_name)
349 {
350  int rv;
351 
352  if ((rv = vl_map_shmem (region_name, 0 /* is_vlib */ )) < 0)
353  {
354  return rv;
355  }
356 
357 #define _(N,n) \
358  vl_msg_api_set_handlers(VL_API_##N, #n, \
359  vl_api_##n##_t_handler, \
360  noop_handler, \
361  vl_api_##n##_t_endian, \
362  vl_api_##n##_t_print, \
363  sizeof(vl_api_##n##_t), 1);
365 #undef _
366  return 0;
367 }
368 
369 void
371 {
372  vl_unmap_shmem ();
373 }
374 
375 static int
376 connect_to_vlib_internal (const char *svm_name,
377  const char *client_name,
378  int rx_queue_size, int want_pthread)
379 {
380  int rv = 0;
382 
383  if ((rv = vl_client_api_map (svm_name)))
384  {
385  clib_warning ("vl_client_api map rv %d", rv);
386  return rv;
387  }
388 
389  if (vl_client_connect (client_name, 0 /* punt quota */ ,
390  rx_queue_size /* input queue */ ) < 0)
391  {
393  return -1;
394  }
395 
396  /* Start the rx queue thread */
397 
398  if (want_pthread)
399  {
400  rv = pthread_create (&mm->rx_thread_handle,
401  NULL /*attr */ , rx_thread_fn, 0);
402  if (rv)
403  clib_warning ("pthread_create returned %d", rv);
404  }
405 
406  mm->connected_to_vlib = 1;
407  return 0;
408 }
409 
410 int
411 vl_client_connect_to_vlib (const char *svm_name,
412  const char *client_name, int rx_queue_size)
413 {
414  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
415  1 /* want pthread */ );
416 }
417 
418 int
420  const char *client_name,
421  int rx_queue_size)
422 {
423  return connect_to_vlib_internal (svm_name, client_name, rx_queue_size,
424  0 /* want pthread */ );
425 }
426 
427 void
429 {
431  api_main_t *am = &api_main;
432  uword junk;
433 
434  if (mm->rx_thread_jmpbuf_valid)
435  {
437  ep = vl_msg_api_alloc (sizeof (*ep));
438  ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT);
439  vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep);
440  pthread_join (mm->rx_thread_handle, (void **) &junk);
441  }
442  if (mm->connected_to_vlib)
443  {
446  }
447  memset (mm, 0, sizeof (*mm));
448 }
449 
452 {
454  i32 retval = ntohl (mp->retval);
455 
456  mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0;
457  mm->first_msg_id_reply_ready = 1;
458 }
459 
460 u16
461 vl_client_get_first_plugin_msg_id (const char *plugin_name)
462 {
464  api_main_t *am = &api_main;
466  f64 timeout;
467  void *old_handler;
468  clib_time_t clib_time;
469  u16 rv = ~0;
470 
471  if (strlen (plugin_name) + 1 > sizeof (mp->name))
472  return (rv);
473 
474  memset (&clib_time, 0, sizeof (clib_time));
475  clib_time_init (&clib_time);
476 
477  /* Push this plugin's first_msg_id_reply handler */
478  old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY];
479  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *)
481 
482  /* Ask the data-plane for the message-ID base of the indicated plugin */
483  mm->first_msg_id_reply_ready = 0;
484 
485  mp = vl_msg_api_alloc (sizeof (*mp));
486  memset (mp, 0, sizeof (*mp));
487  mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID);
488  mp->client_index = am->my_client_index;
489  strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1);
490 
492 
493  /* Synchronously wait for the answer */
494  do
495  {
496  timeout = clib_time_now (&clib_time) + 1.0;
497 
498  while (clib_time_now (&clib_time) < timeout)
499  {
500  if (mm->first_msg_id_reply_ready == 1)
501  {
502  rv = mm->first_msg_id_reply;
503  goto result;
504  }
505  }
506  /* Restore old handler */
507  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
508 
509  return rv;
510  }
511  while (0);
512 
513 result:
514 
515  /* Restore the old handler */
516  am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler;
517 
518  if (rv == (u16) ~ 0)
519  clib_warning ("plugin '%s' not registered", plugin_name);
520 
521  return rv;
522 }
523 
524 void
526 {
527  clib_warning ("STUB called...");
528 }
529 
530 /*
531  * fd.io coding-style-patch-verification: ON
532  *
533  * Local Variables:
534  * eval: (c-set-style "gnu")
535  * End:
536  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
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
int vl_client_connect_to_vlib_no_rx_pthread(const char *svm_name, const char *client_name, int rx_queue_size)
Fixed length block allocator.
unix_shared_memory_queue_t * vl_input_queue
Definition: api_common.h:68
int my_client_index
All VLIB-side message handlers use my_client_index to identify the queue / client.
Definition: api_common.h:286
void unix_shared_memory_queue_free(unix_shared_memory_queue_t *q)
#define NULL
Definition: clib.h:55
static f64 clib_time_now(clib_time_t *c)
Definition: time.h:202
#define hash_set_mem(h, key, value)
Definition: hash.h:274
static void noop_handler(void *notused)
unix_shared_memory_queue_t * vl_input_queue
Peer input queue pointer.
Definition: api_common.h:280
void vl_client_api_unmap(void)
static int connect_to_vlib_internal(const char *svm_name, const char *client_name, int rx_queue_size, int want_pthread)
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:292
pthread_t rx_thread_handle
Definition: memory_client.c:69
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:78
svm_region_t * vlib_rp
Binary api segment descriptor.
Definition: api_common.h:235
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:241
void * vl_msg_api_alloc(int nbytes)
#define hash_create_string(elts, value_bytes)
Definition: hash.h:652
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 vl_msg_api_free(void *)
void vl_msg_api_handler(void *the_msg)
Definition: api_shared.c:547
void clib_time_init(clib_time_t *c)
Definition: time.c:175
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:182
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
int unix_shared_memory_queue_sub(unix_shared_memory_queue_t *q, u8 *elem, int nowait)
api_main_t api_main
Definition: api_shared.c:35
vlib_main_t * vm
Definition: buffer.c:283
void vl_unmap_shmem(void)
#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)
void vlib_node_sync_stats(vlib_main_t *vm, vlib_node_t *n)
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
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
unix_shared_memory_queue_t * unix_shared_memory_queue_init(int nels, int elsize, int consumer_pid, int signal_when_queue_non_empty)
Bitmaps built as vectors of machine words.
void vl_client_disconnect(void)
u64 uword
Definition: types.h:112
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:349
static void vl_api_rx_thread_exit_t_handler(vl_api_rx_thread_exit_t *mp)
void vl_msg_api_queue_handler(unix_shared_memory_queue_t *q)
Definition: api_shared.c:742
memory_client_main_t memory_client_main
Definition: memory_client.c:75
void(** msg_handlers)(void *)
Message handler vector.
Definition: api_common.h:185
void vl_client_disconnect_from_vlib(void)
int vl_map_shmem(const char *region_name, int is_vlib)
uword * msg_index_by_name_and_crc
client message index hash table
Definition: api_common.h:301
volatile u8 first_msg_id_reply_ready
Definition: memory_client.c:71
pthread_mutex_t mutex
Definition: svm_common.h:37
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
struct _unix_shared_memory_queue unix_shared_memory_queue_t