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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
19 #include <sys/socket.h>
22 #include <netinet/in.h>
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>
34 #include <vpp-api/vpe_msg_enum.h>
38 #define vl_typedefs /* define message structures */
39 #include <vpp-api/vpe_all_api_h.h>
42 #define vl_endianfun /* define message structures */
43 #include <vpp-api/vpe_all_api_h.h>
47 u8 rx_thread_jmpbuf_valid;
49 jmp_buf rx_thread_jmpbuf;
50 pthread_t rx_thread_handle;
53 pneum_main_t pneum_main;
55 extern int wrap_pneum_callback(char *data, int len);
58 * Satisfy external references when -lvlib is not available.
60 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
62 clib_warning ("vlib_cli_output called...");
65 #define vl_api_version(n,v) static u32 vpe_api_version = v;
66 #include <vpp-api/vpe.api.h>
69 vl_client_add_api_signatures (vl_api_memclnt_create_t *mp)
72 * Send the main API signature in slot 0. This bit of code must
73 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
75 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
79 pneum_api_handler (void *msg)
81 u16 id = ntohs(*((u16 *)msg));
83 if (id == VL_API_RX_THREAD_EXIT) {
84 pneum_main_t *pm = &pneum_main;
86 longjmp(pm->rx_thread_jmpbuf, 1);
88 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
89 int l = ntohl(msgbuf->data_len);
91 clib_warning("Message ID %d has wrong length: %d\n", id, l);
93 /* Call Python callback */
94 (void)wrap_pneum_callback(msg, l);
99 pneum_rx_thread_fn (void *arg)
101 unix_shared_memory_queue_t *q;
102 pneum_main_t *pm = &pneum_main;
103 api_main_t *am = &api_main;
106 q = am->vl_input_queue;
108 /* So we can make the rx thread terminate cleanly */
109 if (setjmp(pm->rx_thread_jmpbuf) == 0) {
110 pm->rx_thread_jmpbuf_valid = 1;
112 while (!unix_shared_memory_queue_sub(q, (u8 *)&msg, 0))
113 pneum_api_handler((void *)msg);
119 pneum_connect (char * name, char * chroot_prefix)
122 pneum_main_t *pm = &pneum_main;
124 if (chroot_prefix != NULL)
125 vl_set_memory_root_path (chroot_prefix);
127 if ((rv = vl_client_api_map("/vpe-api"))) {
128 clib_warning ("vl_client_api map rv %d", rv);
132 if (vl_client_connect(name, 0, 32) < 0) {
133 vl_client_api_unmap();
137 /* Start the rx queue thread */
138 rv = pthread_create(&pm->rx_thread_handle, NULL, pneum_rx_thread_fn, 0);
140 clib_warning("pthread_create returned %d", rv);
141 vl_client_api_unmap();
145 pm->connected_to_vlib = 1;
151 pneum_disconnect (void)
153 api_main_t *am = &api_main;
154 pneum_main_t *pm = &pneum_main;
156 if (pm->rx_thread_jmpbuf_valid) {
157 vl_api_rx_thread_exit_t *ep;
159 ep = vl_msg_api_alloc (sizeof (*ep));
160 ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT);
161 vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep);
162 pthread_join(pm->rx_thread_handle, (void **) &junk);
164 if (pm->connected_to_vlib) {
165 vl_client_disconnect();
166 vl_client_api_unmap();
168 memset (pm, 0, sizeof (*pm));
174 pneum_read (char **p, int *l)
176 unix_shared_memory_queue_t *q;
177 api_main_t *am = &api_main;
182 if (am->our_pid == 0) return (-1);
184 q = am->vl_input_queue;
185 int rv = unix_shared_memory_queue_sub(q, (u8 *)&msg, 0);
187 u16 msg_id = ntohs(*((u16 *)msg));
188 msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data));
189 *l = ntohl(msgbuf->data_len);
191 printf("Unregistered API message: %d\n", msg_id);
196 printf("Read failed with %d\n", rv);
202 * XXX: Makes the assumption that client_index is the first member
204 typedef VL_API_PACKED(struct _vl_api_header {
210 pneum_client_index (void)
212 return (api_main.my_client_index);
216 pneum_write (char *p, int l)
219 api_main_t *am = &api_main;
220 vl_api_header_t *mp = vl_msg_api_alloc(l);
221 unix_shared_memory_queue_t *q;
223 if (!mp) return (-1);
225 mp->client_index = pneum_client_index();
226 q = am->shmem_hdr->vl_input_queue;
227 rv = unix_shared_memory_queue_add(q, (u8 *)&mp, 0);
229 printf("vpe_api_write fails: %d\n", rv);