Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 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 |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 13 | * limitations under the License. |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 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 | |
Dave Barach | aa6920e | 2016-06-27 09:25:13 -0400 | [diff] [blame] | 34 | #include <vpp-api/vpe_msg_enum.h> |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 35 | |
| 36 | #include "pneum.h" |
| 37 | |
| 38 | #define vl_typedefs /* define message structures */ |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 39 | #include <vpp-api/vpe_all_api_h.h> |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 40 | #undef vl_typedefs |
| 41 | |
| 42 | #define vl_endianfun /* define message structures */ |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 43 | #include <vpp-api/vpe_all_api_h.h> |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 44 | #undef vl_endianfun |
| 45 | |
| 46 | typedef struct { |
| 47 | u8 rx_thread_jmpbuf_valid; |
| 48 | u8 connected_to_vlib; |
| 49 | jmp_buf rx_thread_jmpbuf; |
| 50 | pthread_t rx_thread_handle; |
| 51 | } pneum_main_t; |
| 52 | |
| 53 | pneum_main_t pneum_main; |
| 54 | |
| 55 | extern int wrap_pneum_callback(char *data, int len); |
| 56 | |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 57 | /* |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 58 | * Satisfy external references when -lvlib is not available. |
| 59 | */ |
| 60 | void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...) |
| 61 | { |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 62 | clib_warning ("vlib_cli_output called..."); |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | #define vl_api_version(n,v) static u32 vpe_api_version = v; |
Dave Barach | aa6920e | 2016-06-27 09:25:13 -0400 | [diff] [blame] | 66 | #include <vpp-api/vpe.api.h> |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 67 | #undef vl_api_version |
| 68 | void |
| 69 | vl_client_add_api_signatures (vl_api_memclnt_create_t *mp) |
| 70 | { |
| 71 | /* |
| 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(). |
| 74 | */ |
| 75 | mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version); |
| 76 | } |
| 77 | |
| 78 | static void |
| 79 | pneum_api_handler (void *msg) |
| 80 | { |
| 81 | u16 id = ntohs(*((u16 *)msg)); |
| 82 | |
| 83 | if (id == VL_API_RX_THREAD_EXIT) { |
| 84 | pneum_main_t *pm = &pneum_main; |
| 85 | vl_msg_api_free(msg); |
| 86 | longjmp(pm->rx_thread_jmpbuf, 1); |
| 87 | } |
| 88 | msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data)); |
| 89 | int l = ntohl(msgbuf->data_len); |
| 90 | if (l == 0) |
| 91 | clib_warning("Message ID %d has wrong length: %d\n", id, l); |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 92 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 93 | /* Call Python callback */ |
| 94 | (void)wrap_pneum_callback(msg, l); |
| 95 | vl_msg_api_free(msg); |
| 96 | } |
| 97 | |
| 98 | static void * |
| 99 | pneum_rx_thread_fn (void *arg) |
| 100 | { |
| 101 | unix_shared_memory_queue_t *q; |
| 102 | pneum_main_t *pm = &pneum_main; |
| 103 | api_main_t *am = &api_main; |
| 104 | uword msg; |
| 105 | |
| 106 | q = am->vl_input_queue; |
| 107 | |
| 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; |
| 111 | while (1) |
| 112 | while (!unix_shared_memory_queue_sub(q, (u8 *)&msg, 0)) |
| 113 | pneum_api_handler((void *)msg); |
| 114 | } |
| 115 | pthread_exit(0); |
| 116 | } |
| 117 | |
| 118 | int |
| 119 | pneum_connect (char *name) |
| 120 | { |
| 121 | int rv = 0; |
| 122 | pneum_main_t *pm = &pneum_main; |
| 123 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 124 | if ((rv = vl_client_api_map("/vpe-api"))) { |
| 125 | clib_warning ("vl_client_api map rv %d", rv); |
| 126 | return rv; |
| 127 | } |
| 128 | |
| 129 | if (vl_client_connect(name, 0, 32) < 0) { |
| 130 | vl_client_api_unmap(); |
| 131 | return (-1); |
| 132 | } |
| 133 | |
| 134 | /* Start the rx queue thread */ |
| 135 | rv = pthread_create(&pm->rx_thread_handle, NULL, pneum_rx_thread_fn, 0); |
| 136 | if (rv) { |
| 137 | clib_warning("pthread_create returned %d", rv); |
| 138 | vl_client_api_unmap(); |
| 139 | return (-1); |
| 140 | } |
| 141 | |
| 142 | pm->connected_to_vlib = 1; |
| 143 | |
| 144 | return (0); |
| 145 | } |
| 146 | |
| 147 | int |
| 148 | pneum_disconnect (void) |
| 149 | { |
| 150 | api_main_t *am = &api_main; |
| 151 | pneum_main_t *pm = &pneum_main; |
| 152 | |
| 153 | fformat (stdout,"disconnecting from vpe \n"); |
| 154 | |
| 155 | if (pm->rx_thread_jmpbuf_valid) { |
| 156 | vl_api_rx_thread_exit_t *ep; |
| 157 | uword junk; |
| 158 | ep = vl_msg_api_alloc (sizeof (*ep)); |
| 159 | ep->_vl_msg_id = ntohs(VL_API_RX_THREAD_EXIT); |
| 160 | vl_msg_api_send_shmem(am->vl_input_queue, (u8 *)&ep); |
| 161 | pthread_join(pm->rx_thread_handle, (void **) &junk); |
| 162 | } |
| 163 | if (pm->connected_to_vlib) { |
| 164 | vl_client_disconnect(); |
| 165 | vl_client_api_unmap(); |
| 166 | } |
| 167 | memset (pm, 0, sizeof (*pm)); |
| 168 | |
| 169 | return (0); |
| 170 | } |
| 171 | |
| 172 | int |
| 173 | pneum_read (char **p, int *l) |
| 174 | { |
| 175 | unix_shared_memory_queue_t *q; |
| 176 | api_main_t *am = &api_main; |
| 177 | uword msg; |
| 178 | |
| 179 | *l = 0; |
| 180 | |
| 181 | if (am->our_pid == 0) return (-1); |
| 182 | |
| 183 | q = am->vl_input_queue; |
| 184 | int rv = unix_shared_memory_queue_sub(q, (u8 *)&msg, 0); |
| 185 | if (rv == 0) { |
| 186 | u16 msg_id = ntohs(*((u16 *)msg)); |
| 187 | msgbuf_t *msgbuf = (msgbuf_t *)(((u8 *)msg) - offsetof(msgbuf_t, data)); |
| 188 | *l = ntohl(msgbuf->data_len); |
| 189 | if (*l == 0) { |
| 190 | printf("Unregistered API message: %d\n", msg_id); |
| 191 | return (-1); |
| 192 | } |
| 193 | *p = (char *)msg; |
| 194 | } else { |
| 195 | printf("Read failed with %d\n", rv); |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 196 | } |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 197 | return (rv); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * XXX: Makes the assumption that client_index is the first member |
| 202 | */ |
| 203 | typedef VL_API_PACKED(struct _vl_api_header { |
| 204 | u16 _vl_msg_id; |
| 205 | u32 client_index; |
| 206 | }) vl_api_header_t; |
| 207 | |
| 208 | static unsigned int |
| 209 | pneum_client_index (void) |
| 210 | { |
| 211 | return (api_main.my_client_index); |
| 212 | } |
| 213 | |
| 214 | int |
| 215 | pneum_write (char *p, int l) |
| 216 | { |
| 217 | int rv = -1; |
| 218 | api_main_t *am = &api_main; |
| 219 | vl_api_header_t *mp = vl_msg_api_alloc(l); |
| 220 | unix_shared_memory_queue_t *q; |
| 221 | |
| 222 | if (!mp) return (-1); |
| 223 | memcpy(mp, p, l); |
| 224 | mp->client_index = pneum_client_index(); |
| 225 | q = am->shmem_hdr->vl_input_queue; |
| 226 | rv = unix_shared_memory_queue_add(q, (u8 *)&mp, 0); |
| 227 | if (rv != 0) { |
| 228 | printf("vpe_api_write fails: %d\n", rv); |
| 229 | /* Clear message */ |
| 230 | vl_msg_api_free(mp); |
| 231 | } |
| 232 | return (rv); |
| 233 | } |