FD.io VPP  v17.01.1-3-gc6833f8
Vector Packet Processing
pneum.c
Go to the documentation of this file.
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 <stdio.h>
16 #include <stdlib.h>
17 #include <stddef.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/mman.h>
21 #include <sys/stat.h>
22 #include <netinet/in.h>
23 #include <netdb.h>
24 #include <signal.h>
25 #include <setjmp.h>
26 #include <stdbool.h>
27 
28 #include <vnet/vnet.h>
29 #include <vlib/vlib.h>
30 #include <vlib/unix/unix.h>
31 #include <vlibapi/api.h>
32 #include <vlibmemory/api.h>
33 
34 #include <vpp-api/vpe_msg_enum.h>
35 
36 #include "pneum.h"
37 
38 #define vl_typedefs /* define message structures */
39 #include <vpp-api/vpe_all_api_h.h>
40 #undef vl_typedefs
41 
42 #define vl_endianfun /* define message structures */
43 #include <vpp-api/vpe_all_api_h.h>
44 #undef vl_endianfun
45 
48 
49 typedef struct {
53  pthread_t rx_thread_handle;
54 } pneum_main_t;
55 
57 
59 
60 /*
61  * Satisfy external references when -lvlib is not available.
62  */
63 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
64 {
65  clib_warning ("vlib_cli_output called...");
66 }
67 
68 void
69 pneum_free (void * msg)
70 {
71  vl_msg_api_free (msg);
72 }
73 
74 static void
75 pneum_api_handler (void *msg)
76 {
77  u16 id = ntohs(*((u16 *)msg));
78  if (id == VL_API_RX_THREAD_EXIT) {
79  pneum_main_t *pm = &pneum_main;
80  vl_msg_api_free(msg);
81  longjmp(pm->rx_thread_jmpbuf, 1);
82  }
83  msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
84  int l = ntohl(msgbuf->data_len);
85  if (l == 0)
86  clib_warning("Message ID %d has wrong length: %d\n", id, l);
87 
88  /* Call Python callback */
89  ASSERT(pneum_callback);
90  (pneum_callback)(msg, l);
91  pneum_free(msg);
92 }
93 
94 static void *
95 pneum_rx_thread_fn (void *arg)
96 {
98  pneum_main_t *pm = &pneum_main;
99  api_main_t *am = &api_main;
100  uword msg;
101 
102  q = am->vl_input_queue;
103 
104  /* So we can make the rx thread terminate cleanly */
105  if (setjmp(pm->rx_thread_jmpbuf) == 0) {
106  pm->rx_thread_jmpbuf_valid = 1;
107  while (1)
108  while (!unix_shared_memory_queue_sub(q, (u8 *)&msg, 0))
109  pneum_api_handler((void *)msg);
110  }
111  pthread_exit(0);
112 }
113 
114 uword *
116 {
117  api_main_t *am = &api_main;
118  return (am->msg_index_by_name_and_crc);
119 }
120 
121 int
123 {
124  api_main_t *am = &api_main;
126 }
127 
128 int
129 pneum_connect (char * name, char * chroot_prefix, pneum_callback_t cb)
130 {
131  int rv = 0;
132  pneum_main_t *pm = &pneum_main;
133 
134  if (chroot_prefix != NULL)
135  vl_set_memory_root_path (chroot_prefix);
136 
137  if ((rv = vl_client_api_map("/vpe-api"))) {
138  clib_warning ("vl_client_api map rv %d", rv);
139  return rv;
140  }
141 
142  if (vl_client_connect(name, 0, 32) < 0) {
144  return (-1);
145  }
146 
147  if (cb) {
148  /* Start the rx queue thread */
149  rv = pthread_create(&pm->rx_thread_handle, NULL, pneum_rx_thread_fn, 0);
150  if (rv) {
151  clib_warning("pthread_create returned %d", rv);
153  return (-1);
154  }
155  pneum_callback = cb;
156  }
157 
158  pm->connected_to_vlib = 1;
159 
160  return (0);
161 }
162 
163 int
165 {
166  api_main_t *am = &api_main;
167  pneum_main_t *pm = &pneum_main;
168 
169  if (pm->rx_thread_jmpbuf_valid) {
171  uword junk;
172  ep = vl_msg_api_alloc (sizeof (*ep));
173  ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
175  pthread_join(pm->rx_thread_handle, (void **) &junk);
176  }
177  if (pm->connected_to_vlib) {
180  pneum_callback = 0;
181  }
182  memset (pm, 0, sizeof (*pm));
183 
184  return (0);
185 }
186 
187 int
188 pneum_read (char **p, int *l)
189 {
191  api_main_t *am = &api_main;
192  pneum_main_t *pm = &pneum_main;
193  uword msg;
194 
195  if (!pm->connected_to_vlib) return -1;
196 
197  *l = 0;
198 
199  if (am->our_pid == 0) return (-1);
200 
201  q = am->vl_input_queue;
202  int rv = unix_shared_memory_queue_sub(q, (u8 *)&msg, 0);
203  if (rv == 0) {
204  u16 msg_id = ntohs(*((u16 *)msg));
205  msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
206  *l = ntohl(msgbuf->data_len);
207  if (*l == 0) {
208  printf("Unregistered API message: %d\n", msg_id);
209  return (-1);
210  }
211  *p = (char *)msg;
212  } else {
213  printf("Read failed with %d\n", rv);
214  }
215  return (rv);
216 }
217 
218 /*
219  * XXX: Makes the assumption that client_index is the first member
220  */
221 typedef VL_API_PACKED(struct _vl_api_header {
222  u16 _vl_msg_id;
223  u32 client_index;
224 }) vl_api_header_t;
225 
226 static unsigned int
227 pneum_client_index (void)
228 {
229  return (api_main.my_client_index);
230 }
231 
232 int
233 pneum_write (char *p, int l)
234 {
235  int rv = -1;
236  api_main_t *am = &api_main;
237  vl_api_header_t *mp = vl_msg_api_alloc(l);
239  pneum_main_t *pm = &pneum_main;
240 
241  if (!pm->connected_to_vlib) return -1;
242  if (!mp) return (-1);
243  memcpy(mp, p, l);
244  mp->client_index = pneum_client_index();
245  q = am->shmem_hdr->vl_input_queue;
246  rv = unix_shared_memory_queue_add(q, (u8 *)&mp, 0);
247  if (rv != 0) {
248  printf("vpe_api_write fails: %d\n", rv);
249  /* Clear message */
250  pneum_free(mp);
251  }
252  return (rv);
253 }
254 
255 uint32_t
256 pneum_get_msg_index (unsigned char * name)
257 {
258  return vl_api_get_msg_index (name);
259 }
jmp_buf rx_thread_jmpbuf
Definition: pneum.c:52
u8 rx_thread_jmpbuf_valid
Definition: pneum.c:50
pthread_t rx_thread_handle
Definition: pneum.c:53
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
u8 connected_to_vlib
Definition: pneum.c:51
unix_shared_memory_queue_t * vl_input_queue
Definition: api.h:73
int my_client_index
Definition: api.h:176
#define NULL
Definition: clib.h:55
vlib_main_t ** vlib_mains
Definition: pneum.c:47
int pneum_read(char **p, int *l)
Definition: pneum.c:188
unix_shared_memory_queue_t * vl_input_queue
Definition: api.h:170
api_main_t api_main
Definition: api_shared.c:39
int our_pid
Definition: api.h:132
static void * pneum_rx_thread_fn(void *arg)
Definition: pneum.c:95
pneum_main_t pneum_main
Definition: pneum.c:56
void vl_msg_api_free(void *)
#define clib_warning(format, args...)
Definition: error.h:59
struct vl_shmem_hdr_ * shmem_hdr
Definition: api.h:135
vlib_main_t vlib_global_main
Definition: pneum.c:46
int unix_shared_memory_queue_add(unix_shared_memory_queue_t *q, u8 *elem, int nowait)
int vl_client_api_map(char *region_name)
void vl_set_memory_root_path(char *root_path)
void(* pneum_callback_t)(unsigned char *data, int len)
Definition: pneum.h:21
int pneum_connect(char *name, char *chroot_prefix, pneum_callback_t cb)
Definition: pneum.c:129
int pneum_disconnect(void)
Definition: pneum.c:164
int unix_shared_memory_queue_sub(unix_shared_memory_queue_t *q, u8 *elem, int nowait)
void * vl_msg_api_alloc(int nbytes)
static void pneum_api_handler(void *msg)
Definition: pneum.c:75
void vlib_cli_output(struct vlib_main_t *vm, char *fmt,...)
Definition: pneum.c:63
u32 vl_api_get_msg_index(u8 *name_and_crc)
int vl_client_connect(char *name, int ctx_quota, int input_queue_size)
static uword hash_elts(void *v)
Definition: hash.h:117
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u32 data_len
Definition: api.h:214
Definition: api.h:211
void pneum_free(void *msg)
Definition: pneum.c:69
u64 uword
Definition: types.h:112
void vl_client_disconnect(void)
void vl_client_api_unmap(void)
typedef VL_API_PACKED(struct _vl_api_header{u16 _vl_msg_id;u32 client_index;})
Definition: pneum.c:221
unsigned short u16
Definition: types.h:57
pneum_callback_t pneum_callback
Definition: pneum.c:58
unsigned char u8
Definition: types.h:56
uword * pneum_msg_table_get_hash(void)
Definition: pneum.c:115
uint32_t pneum_get_msg_index(unsigned char *name)
Definition: pneum.c:256
int pneum_msg_table_size(void)
Definition: pneum.c:122
uword * msg_index_by_name_and_crc
Definition: api.h:187
int pneum_write(char *p, int l)
Definition: pneum.c:233
struct _unix_shared_memory_queue unix_shared_memory_queue_t