ethernet: check destination mac for L3 in ethernet-input node
[vpp.git] / src / vpp-api / client / client.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <assert.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stddef.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/mman.h>
22 #include <sys/stat.h>
23 #include <netinet/in.h>
24 #include <netdb.h>
25 #include <signal.h>
26 #include <stdbool.h>
27 #include <vnet/vnet.h>
28 #include <vlib/vlib.h>
29 #include <vlib/unix/unix.h>
30 #include <vlibapi/api.h>
31 #include <vlibmemory/api.h>
32
33 #include <vlibmemory/memclnt.api_enum.h>
34 #include <vlibmemory/memclnt.api_types.h>
35
36 #include "vppapiclient.h"
37
38 bool timeout_cancelled;
39 bool timeout_in_progress;
40 bool rx_thread_done;
41
42 /*
43  * Asynchronous mode:
44  *  Client registers a callback. All messages are sent to the callback.
45  * Synchronous mode:
46  *  Client calls blocking read().
47  *  Clients are expected to collate events on a queue.
48  *  vac_write() -> suspends RX thread
49  *  vac_read() -> resumes RX thread
50  */
51
52 typedef struct {
53   u8 connected_to_vlib;
54   pthread_t rx_thread_handle;
55   pthread_t timeout_thread_handle;
56   pthread_mutex_t queue_lock;
57   pthread_cond_t suspend_cv;
58   pthread_cond_t resume_cv;
59   pthread_mutex_t timeout_lock;
60   u8 timeout_loop;
61   pthread_cond_t timeout_cv;
62   pthread_cond_t timeout_cancel_cv;
63   pthread_cond_t terminate_cv;
64 } vac_main_t;
65
66 vac_main_t vac_main;
67 vac_callback_t vac_callback;
68 u16 read_timeout = 0;
69 bool rx_is_running = false;
70 bool timeout_thread_cancelled = false;
71
72 /* Only ever allocate one heap */
73 bool mem_initialized = false;
74
75 static void
76 init (void)
77 {
78   vac_main_t *pm = &vac_main;
79   clib_memset(pm, 0, sizeof(*pm));
80   pthread_mutex_init(&pm->queue_lock, NULL);
81   pthread_cond_init(&pm->suspend_cv, NULL);
82   pthread_cond_init(&pm->resume_cv, NULL);
83   pthread_mutex_init(&pm->timeout_lock, NULL);
84   pm->timeout_loop = 1;
85   pthread_cond_init(&pm->timeout_cv, NULL);
86   pthread_cond_init(&pm->timeout_cancel_cv, NULL);
87   pthread_cond_init(&pm->terminate_cv, NULL);
88 }
89
90 static void
91 cleanup (void)
92 {
93   vac_main_t *pm = &vac_main;
94   pthread_mutex_destroy(&pm->queue_lock);
95   pthread_cond_destroy(&pm->suspend_cv);
96   pthread_cond_destroy(&pm->resume_cv);
97   pthread_mutex_destroy(&pm->timeout_lock);
98   pthread_cond_destroy(&pm->timeout_cv);
99   pthread_cond_destroy(&pm->timeout_cancel_cv);
100   pthread_cond_destroy(&pm->terminate_cv);
101   clib_memset(pm, 0, sizeof(*pm));
102 }
103
104 void
105 vac_free (void * msg)
106 {
107   vl_msg_api_free (msg);
108 }
109
110 static void
111 vac_api_handler (void *msg)
112 {
113   u16 id = ntohs(*((u16 *)msg));
114   msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
115   int l = ntohl(msgbuf->data_len);
116   if (l == 0)
117     clib_warning("Message ID %d has wrong length: %d\n", id, l);
118
119   /* Call Python callback */
120   ASSERT(vac_callback);
121   (vac_callback)(msg, l);
122   vac_free(msg);
123 }
124
125 static void *
126 vac_rx_thread_fn (void *arg)
127 {
128   svm_queue_t *q;
129   vl_api_memclnt_keepalive_t *mp;
130   vl_api_memclnt_keepalive_reply_t *rmp;
131   vac_main_t *pm = &vac_main;
132   api_main_t *am = vlibapi_get_main();
133   vl_shmem_hdr_t *shmem_hdr;
134   uword msg;
135
136   q = am->vl_input_queue;
137
138   while (1)
139     while (!svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0))
140       {
141         VL_MSG_API_UNPOISON((void *)msg);
142         u16 id = ntohs(*((u16 *)msg));
143         switch (id) {
144         case VL_API_RX_THREAD_EXIT:
145           vl_msg_api_free((void *) msg);
146           /* signal waiting threads that this thread is about to terminate */
147           pthread_mutex_lock(&pm->queue_lock);
148           rx_thread_done = true;
149           pthread_cond_signal(&pm->terminate_cv);
150           pthread_mutex_unlock(&pm->queue_lock);
151           pthread_exit(0);
152           return 0;
153           break;
154
155         case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
156           vl_msg_api_free((void * )msg);
157           /* Suspend thread and signal reader */
158           pthread_mutex_lock(&pm->queue_lock);
159           pthread_cond_signal(&pm->suspend_cv);
160           /* Wait for the resume signal */
161           pthread_cond_wait (&pm->resume_cv, &pm->queue_lock);
162           pthread_mutex_unlock(&pm->queue_lock);
163           break;
164
165         case VL_API_MEMCLNT_READ_TIMEOUT:
166           clib_warning("Received read timeout in async thread\n");
167           vl_msg_api_free((void *) msg);
168           break;
169
170         case VL_API_MEMCLNT_KEEPALIVE:
171           mp = (void *)msg;
172           rmp = vl_msg_api_alloc (sizeof (*rmp));
173           clib_memset (rmp, 0, sizeof (*rmp));
174           rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
175           rmp->context = mp->context;
176           shmem_hdr = am->shmem_hdr;
177           vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
178           vl_msg_api_free((void *) msg);
179           break;
180
181         default:
182           vac_api_handler((void *)msg);
183         }
184       }
185 }
186
187 static void *
188 vac_timeout_thread_fn (void *arg)
189 {
190   vl_api_memclnt_read_timeout_t *ep;
191   vac_main_t *pm = &vac_main;
192   api_main_t *am = vlibapi_get_main();
193   struct timespec ts;
194   struct timeval tv;
195   int rv;
196
197   while (pm->timeout_loop)
198     {
199       /* Wait for poke */
200       pthread_mutex_lock(&pm->timeout_lock);
201       while (!timeout_in_progress)
202         pthread_cond_wait (&pm->timeout_cv, &pm->timeout_lock);
203
204       /* Starting timer */
205       gettimeofday(&tv, NULL);
206       ts.tv_sec = tv.tv_sec + read_timeout;
207       ts.tv_nsec = 0;
208
209       if (!timeout_cancelled) {
210         rv = pthread_cond_timedwait (&pm->timeout_cancel_cv,
211                                      &pm->timeout_lock, &ts);
212         if (rv == ETIMEDOUT && !timeout_thread_cancelled) {
213           ep = vl_msg_api_alloc (sizeof (*ep));
214           ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_READ_TIMEOUT);
215           vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
216         }
217       }
218
219       pthread_mutex_unlock(&pm->timeout_lock);
220     }
221   pthread_exit(0);
222 }
223
224 void
225 vac_rx_suspend (void)
226 {
227   api_main_t *am = vlibapi_get_main();
228   vac_main_t *pm = &vac_main;
229   vl_api_memclnt_rx_thread_suspend_t *ep;
230
231   if (!pm->rx_thread_handle) return;
232   pthread_mutex_lock(&pm->queue_lock);
233   if (rx_is_running)
234     {
235       ep = vl_msg_api_alloc (sizeof (*ep));
236       ep->_vl_msg_id = ntohs(VL_API_MEMCLNT_RX_THREAD_SUSPEND);
237       vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
238       /* Wait for RX thread to tell us it has suspended */
239       pthread_cond_wait(&pm->suspend_cv, &pm->queue_lock);
240       rx_is_running = false;
241     }
242   pthread_mutex_unlock(&pm->queue_lock);
243 }
244
245 void
246 vac_rx_resume (void)
247 {
248   vac_main_t *pm = &vac_main;
249   if (!pm->rx_thread_handle) return;
250   pthread_mutex_lock(&pm->queue_lock);
251   if (rx_is_running) goto unlock;
252   pthread_cond_signal(&pm->resume_cv);
253   rx_is_running = true;
254  unlock:
255   pthread_mutex_unlock(&pm->queue_lock);
256 }
257
258 static uword *
259 vac_msg_table_get_hash (void)
260 {
261   api_main_t *am = vlibapi_get_main();
262   return (am->msg_index_by_name_and_crc);
263 }
264
265 int
266 vac_msg_table_size(void)
267 {
268   api_main_t *am = vlibapi_get_main();
269   return hash_elts(am->msg_index_by_name_and_crc);
270 }
271
272 int
273 vac_connect (char * name, char * chroot_prefix, vac_callback_t cb,
274              int rx_qlen)
275 {
276   rx_thread_done = false;
277   int rv = 0;
278   vac_main_t *pm = &vac_main;
279
280   assert (clib_mem_get_heap ());
281   init();
282   if (chroot_prefix != NULL)
283     vl_set_memory_root_path (chroot_prefix);
284
285   if ((rv = vl_client_api_map("/vpe-api"))) {
286     clib_warning ("vl_client_api_map returned %d", rv);
287     return rv;
288   }
289
290   if (vl_client_connect(name, 0, rx_qlen) < 0) {
291     vl_client_api_unmap();
292     return (-1);
293   }
294
295   if (cb) {
296     /* Start the rx queue thread */
297     rv = pthread_create(&pm->rx_thread_handle, NULL, vac_rx_thread_fn, 0);
298     if (rv) {
299       clib_warning("pthread_create returned %d", rv);
300       vl_client_api_unmap();
301       return (-1);
302     }
303     vac_callback = cb;
304     rx_is_running = true;
305   }
306
307   /* Start read timeout thread */
308   timeout_in_progress = false;
309   timeout_thread_cancelled = false;
310   rv = pthread_create(&pm->timeout_thread_handle, NULL,
311                       vac_timeout_thread_fn, 0);
312   if (rv) {
313     clib_warning("pthread_create returned %d", rv);
314     vl_client_api_unmap();
315     return (-1);
316   }
317
318   pm->connected_to_vlib = 1;
319
320   return (0);
321 }
322 static void
323 set_timeout (unsigned short timeout)
324 {
325   vac_main_t *pm = &vac_main;
326   pthread_mutex_lock(&pm->timeout_lock);
327   read_timeout = timeout;
328   timeout_in_progress = true;
329   timeout_cancelled = false;
330   pthread_cond_signal(&pm->timeout_cv);
331   pthread_mutex_unlock(&pm->timeout_lock);
332 }
333
334 static void
335 unset_timeout (void)
336 {
337   vac_main_t *pm = &vac_main;
338   pthread_mutex_lock(&pm->timeout_lock);
339   timeout_in_progress = false;
340   timeout_cancelled = true;
341   pthread_cond_signal(&pm->timeout_cancel_cv);
342   pthread_mutex_unlock(&pm->timeout_lock);
343 }
344
345 int
346 vac_disconnect (void)
347 {
348   api_main_t *am = vlibapi_get_main();
349   vac_main_t *pm = &vac_main;
350   uword junk;
351   int rv = 0;
352
353   if (!pm->connected_to_vlib) return 0;
354
355   if (pm->rx_thread_handle) {
356     vl_api_rx_thread_exit_t *ep;
357     ep = vl_msg_api_alloc (sizeof (*ep));
358     ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
359     vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
360
361     /* wait (with timeout) until RX thread has finished */
362     struct timespec ts;
363     struct timeval tv;
364     gettimeofday(&tv, NULL);
365     ts.tv_sec = tv.tv_sec + 5;
366     ts.tv_nsec = 0;
367
368     pthread_mutex_lock(&pm->queue_lock);
369     if (rx_thread_done == false)
370       rv = pthread_cond_timedwait(&pm->terminate_cv, &pm->queue_lock, &ts);
371     pthread_mutex_unlock(&pm->queue_lock);
372
373     /* now join so we wait until thread has -really- finished */
374     if (rv == ETIMEDOUT)
375       pthread_cancel(pm->rx_thread_handle);
376     else
377       pthread_join(pm->rx_thread_handle, (void **) &junk);
378   }
379   if (pm->timeout_thread_handle) {
380     /* cancel, wake then join the timeout thread */
381     pm->timeout_loop = 0;
382     timeout_thread_cancelled = true;
383     set_timeout(0);
384     pthread_join(pm->timeout_thread_handle, (void **) &junk);
385   }
386
387   vl_client_disconnect();
388   vl_client_api_unmap();
389   //vac_callback = 0;
390
391   cleanup();
392
393   return (0);
394 }
395
396 int
397 vac_read (char **p, int *l, u16 timeout)
398 {
399   svm_queue_t *q;
400   api_main_t *am = vlibapi_get_main();
401   vac_main_t *pm = &vac_main;
402   vl_api_memclnt_keepalive_t *mp;
403   vl_api_memclnt_keepalive_reply_t *rmp;
404   uword msg;
405   msgbuf_t *msgbuf;
406   int rv;
407   vl_shmem_hdr_t *shmem_hdr;
408
409   /* svm_queue_sub(below) returns {-1, -2} */
410   if (!pm->connected_to_vlib)
411     return VAC_NOT_CONNECTED;
412
413   *l = 0;
414
415   /* svm_queue_sub(below) returns {-1, -2} */
416   if (am->our_pid == 0)
417     return (VAC_SHM_NOT_READY);
418
419   /* Poke timeout thread */
420   if (timeout)
421     set_timeout(timeout);
422
423   q = am->vl_input_queue;
424
425  again:
426   rv = svm_queue_sub(q, (u8 *)&msg, SVM_Q_WAIT, 0);
427
428   if (rv == 0) {
429     VL_MSG_API_UNPOISON((void *)msg);
430     u16 msg_id = ntohs(*((u16 *)msg));
431     switch (msg_id) {
432     case VL_API_RX_THREAD_EXIT:
433       vl_msg_api_free((void *) msg);
434       goto error;
435     case VL_API_MEMCLNT_RX_THREAD_SUSPEND:
436       goto error;
437     case VL_API_MEMCLNT_READ_TIMEOUT:
438       goto error;
439     case VL_API_MEMCLNT_KEEPALIVE:
440       /* Handle an alive-check ping from vpp. */
441       mp = (void *)msg;
442       rmp = vl_msg_api_alloc (sizeof (*rmp));
443       clib_memset (rmp, 0, sizeof (*rmp));
444       rmp->_vl_msg_id = ntohs(VL_API_MEMCLNT_KEEPALIVE_REPLY);
445       rmp->context = mp->context;
446       shmem_hdr = am->shmem_hdr;
447       vl_msg_api_send_shmem(shmem_hdr->vl_input_queue, (u8 *)&rmp);
448       vl_msg_api_free((void *) msg);
449       /*
450        * Python code is blissfully unaware of these pings, so
451        * act as if it never happened...
452        */
453       goto again;
454
455     default:
456       msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
457       *l = ntohl(msgbuf->data_len);
458       if (*l == 0) {
459         fprintf(stderr, "Unregistered API message: %d\n", msg_id);
460         goto error;
461       }
462     }
463     *p = (char *)msg;
464
465
466   } else {
467     fprintf(stderr, "Read failed with %d\n", rv);
468   }
469   /* Let timeout notification thread know we're done */
470   if (timeout)
471     unset_timeout();
472
473   return (rv);
474
475  error:
476   if (timeout)
477     unset_timeout();
478   vl_msg_api_free((void *) msg);
479   /* Client might forget to resume RX thread on failure */
480   vac_rx_resume ();
481   return VAC_TIMEOUT;
482 }
483
484 /*
485  * XXX: Makes the assumption that client_index is the first member
486  */
487 typedef struct _vl_api_header
488 {
489   u16 _vl_msg_id;
490   u32 client_index;
491 } __attribute__ ((packed)) vl_api_header_t;
492
493 static u32
494 vac_client_index (void)
495 {
496   return (vlibapi_get_main()->my_client_index);
497 }
498
499 int
500 vac_write (char *p, int l)
501 {
502   int rv = -1;
503   api_main_t *am = vlibapi_get_main();
504   vl_api_header_t *mp = vl_msg_api_alloc(l);
505   svm_queue_t *q;
506   vac_main_t *pm = &vac_main;
507
508   if (!pm->connected_to_vlib)
509     return VAC_NOT_CONNECTED;
510   if (!mp) return (-1);
511
512   memcpy(mp, p, l);
513   mp->client_index = vac_client_index();
514   q = am->shmem_hdr->vl_input_queue;
515   rv = svm_queue_add(q, (u8 *)&mp, 0);
516   if (rv != 0) {
517     fprintf(stderr, "vpe_api_write fails: %d\n", rv);
518     /* Clear message */
519     vac_free(mp);
520   }
521   return (rv);
522 }
523
524 int
525 vac_get_msg_index (char * name)
526 {
527   return vl_msg_api_get_msg_index ((u8 *)name);
528 }
529
530 int
531 vac_msg_table_max_index(void)
532 {
533   int max = 0;
534   hash_pair_t *hp;
535   uword *h = vac_msg_table_get_hash();
536   hash_foreach_pair (hp, h,
537   ({
538     if (hp->value[0] > max)
539       max = hp->value[0];
540   }));
541
542   return max;
543 }
544
545 void
546 vac_set_error_handler (vac_error_callback_t cb)
547 {
548   assert (clib_mem_get_heap ());
549   if (cb) clib_error_register_handler (cb, 0);
550 }
551
552 /*
553  * Required if application doesn't use a VPP heap.
554  */
555 void
556 vac_mem_init (size_t size)
557 {
558   if (mem_initialized)
559     return;
560   if (size == 0)
561     clib_mem_init (0, 1 << 30);  // default
562   else
563     clib_mem_init (0, size);
564   mem_initialized = true;
565 }