Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
| 48 | #include <vlibmemory/vl_memory_msg_enum.h> |
| 49 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 50 | #define vl_typedefs /* define message structures */ |
| 51 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | #undef vl_typedefs |
| 53 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 54 | #define vl_endianfun /* define message structures */ |
| 55 | #include <vlibmemory/vl_memory_api_h.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 |
| 61 | #include <vlibmemory/vl_memory_api_h.h> |
| 62 | #undef vl_printfun |
| 63 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 64 | typedef struct |
| 65 | { |
| 66 | u8 rx_thread_jmpbuf_valid; |
| 67 | u8 connected_to_vlib; |
| 68 | jmp_buf rx_thread_jmpbuf; |
| 69 | pthread_t rx_thread_handle; |
| 70 | /* Plugin message base lookup scheme */ |
| 71 | volatile u8 first_msg_id_reply_ready; |
| 72 | u16 first_msg_id_reply; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | } memory_client_main_t; |
| 74 | |
| 75 | memory_client_main_t memory_client_main; |
| 76 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 77 | static void * |
| 78 | rx_thread_fn (void *arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 80 | unix_shared_memory_queue_t *q; |
| 81 | memory_client_main_t *mm = &memory_client_main; |
| 82 | api_main_t *am = &api_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 84 | q = am->vl_input_queue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 86 | /* So we can make the rx thread terminate cleanly */ |
| 87 | if (setjmp (mm->rx_thread_jmpbuf) == 0) |
| 88 | { |
| 89 | mm->rx_thread_jmpbuf_valid = 1; |
| 90 | while (1) |
| 91 | { |
| 92 | vl_msg_api_queue_handler (q); |
| 93 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 95 | pthread_exit (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 98 | static void |
| 99 | vl_api_rx_thread_exit_t_handler (vl_api_rx_thread_exit_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 101 | memory_client_main_t *mm = &memory_client_main; |
| 102 | vl_msg_api_free (mp); |
| 103 | longjmp (mm->rx_thread_jmpbuf, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 106 | static void |
| 107 | noop_handler (void *notused) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | { |
| 109 | } |
| 110 | |
| 111 | #define foreach_api_msg \ |
| 112 | _(RX_THREAD_EXIT, rx_thread_exit) |
| 113 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 114 | static int |
| 115 | connect_to_vlib_internal (char *svm_name, char *client_name, |
| 116 | int rx_queue_size, int want_pthread) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 118 | int rv = 0; |
| 119 | memory_client_main_t *mm = &memory_client_main; |
| 120 | |
| 121 | if ((rv = vl_client_api_map (svm_name))) |
| 122 | { |
| 123 | clib_warning ("vl_client_api map rv %d", rv); |
| 124 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 126 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | #define _(N,n) \ |
| 128 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 129 | vl_api_##n##_t_handler, \ |
| 130 | noop_handler, \ |
| 131 | vl_api_##n##_t_endian, \ |
| 132 | vl_api_##n##_t_print, \ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 133 | sizeof(vl_api_##n##_t), 1); |
| 134 | foreach_api_msg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | #undef _ |
| 136 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 137 | if (vl_client_connect (client_name, 0 /* punt quota */ , |
| 138 | rx_queue_size /* input queue */ ) < 0) |
| 139 | { |
| 140 | vl_client_api_unmap (); |
| 141 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 144 | /* Start the rx queue thread */ |
| 145 | |
| 146 | if (want_pthread) |
| 147 | { |
| 148 | rv = pthread_create (&mm->rx_thread_handle, |
| 149 | NULL /*attr */ , rx_thread_fn, 0); |
| 150 | if (rv) |
| 151 | clib_warning ("pthread_create returned %d", rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 153 | |
| 154 | mm->connected_to_vlib = 1; |
| 155 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 158 | int |
| 159 | vl_client_connect_to_vlib (char *svm_name, char *client_name, |
| 160 | int rx_queue_size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 162 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
| 163 | 1 /* want pthread */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 166 | int |
| 167 | vl_client_connect_to_vlib_no_rx_pthread (char *svm_name, char *client_name, |
| 168 | int rx_queue_size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 170 | return connect_to_vlib_internal (svm_name, client_name, rx_queue_size, |
| 171 | 0 /* want pthread */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 174 | void |
| 175 | vl_client_disconnect_from_vlib (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 177 | memory_client_main_t *mm = &memory_client_main; |
| 178 | api_main_t *am = &api_main; |
| 179 | uword junk; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 181 | if (mm->rx_thread_jmpbuf_valid) |
| 182 | { |
| 183 | vl_api_rx_thread_exit_t *ep; |
| 184 | ep = vl_msg_api_alloc (sizeof (*ep)); |
| 185 | ep->_vl_msg_id = ntohs (VL_API_RX_THREAD_EXIT); |
| 186 | vl_msg_api_send_shmem (am->vl_input_queue, (u8 *) & ep); |
| 187 | pthread_join (mm->rx_thread_handle, (void **) &junk); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 189 | if (mm->connected_to_vlib) |
| 190 | { |
| 191 | vl_client_disconnect (); |
| 192 | vl_client_api_unmap (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 194 | memset (mm, 0, sizeof (*mm)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void vl_api_get_first_msg_id_reply_t_handler |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 198 | (vl_api_get_first_msg_id_reply_t * mp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 200 | memory_client_main_t *mm = &memory_client_main; |
| 201 | i32 retval = ntohl (mp->retval); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 203 | mm->first_msg_id_reply = (retval >= 0) ? ntohs (mp->first_msg_id) : ~0; |
| 204 | mm->first_msg_id_reply_ready = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 207 | u16 |
| 208 | vl_client_get_first_plugin_msg_id (char *plugin_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 210 | vl_api_get_first_msg_id_t *mp; |
| 211 | api_main_t *am = &api_main; |
| 212 | memory_client_main_t *mm = &memory_client_main; |
| 213 | f64 timeout; |
| 214 | void *old_handler; |
| 215 | clib_time_t clib_time; |
| 216 | u16 rv = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 218 | if (strlen (plugin_name) + 1 > sizeof (mp->name)) |
| 219 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 221 | memset (&clib_time, 0, sizeof (clib_time)); |
| 222 | clib_time_init (&clib_time); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 224 | /* Push this plugin's first_msg_id_reply handler */ |
| 225 | old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY]; |
| 226 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *) |
| 227 | vl_api_get_first_msg_id_reply_t_handler; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 229 | /* Ask the data-plane for the message-ID base of the indicated plugin */ |
| 230 | mm->first_msg_id_reply_ready = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 232 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 233 | memset (mp, 0, sizeof (*mp)); |
| 234 | mp->_vl_msg_id = ntohs (VL_API_GET_FIRST_MSG_ID); |
| 235 | mp->client_index = am->my_client_index; |
| 236 | strncpy ((char *) mp->name, plugin_name, sizeof (mp->name) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 238 | vl_msg_api_send_shmem (am->shmem_hdr->vl_input_queue, (u8 *) & mp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 240 | /* Synchronously wait for the answer */ |
| 241 | do |
| 242 | { |
| 243 | timeout = clib_time_now (&clib_time) + 1.0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 245 | while (clib_time_now (&clib_time) < timeout) |
| 246 | { |
| 247 | if (mm->first_msg_id_reply_ready == 1) |
| 248 | { |
| 249 | rv = mm->first_msg_id_reply; |
| 250 | goto result; |
| 251 | } |
| 252 | } |
| 253 | /* Restore old handler */ |
| 254 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
| 255 | |
| 256 | return rv; |
| 257 | } |
| 258 | while (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 259 | |
| 260 | result: |
| 261 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 262 | /* Restore the old handler */ |
| 263 | am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = old_handler; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 265 | if (rv == (u16) ~ 0) |
| 266 | clib_warning ("plugin '%s' not registered", plugin_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 267 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 268 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | } |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 270 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 271 | void |
| 272 | vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n) |
| 273 | { |
| 274 | clib_warning ("STUB called..."); |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * fd.io coding-style-patch-verification: ON |
| 279 | * |
| 280 | * Local Variables: |
| 281 | * eval: (c-set-style "gnu") |
| 282 | * End: |
| 283 | */ |