Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * api.h |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 4 | * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 5 | * Copyright (c) 2009 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 | #ifndef included_vlibmemory_api_h |
| 21 | #define included_vlibmemory_api_h |
| 22 | |
| 23 | #include <vppinfra/error.h> |
| 24 | #include <svm.h> |
| 25 | #include <vlib/vlib.h> |
| 26 | #include <vlibmemory/unix_shared_memory_queue.h> |
| 27 | #include <vlib/unix/unix.h> |
| 28 | #include <vlibapi/api.h> |
| 29 | |
| 30 | /* Allocated in shared memory */ |
| 31 | |
| 32 | /* |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 33 | * Ring-allocation scheme for client API messages |
| 34 | * |
| 35 | * Only one proc/thread has control of a given message buffer. |
| 36 | * To free a buffer allocated from one of these rings, we clear |
| 37 | * a field in the buffer (header), and leave. |
| 38 | * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | * No locks, no hits, no errors... |
| 40 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 41 | typedef struct ring_alloc_ |
| 42 | { |
| 43 | unix_shared_memory_queue_t *rp; |
| 44 | u16 size; |
| 45 | u16 nitems; |
| 46 | u32 hits; |
| 47 | u32 misses; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | } ring_alloc_t; |
| 49 | |
| 50 | /* |
| 51 | * Initializers for the (shared-memory) rings |
| 52 | * _(size, n). Note: each msg has an 8 byte header. |
| 53 | * Might want to change that to an index sometime. |
| 54 | */ |
| 55 | #define foreach_vl_aring_size \ |
| 56 | _(64+8, 1024) \ |
| 57 | _(256+8, 128) \ |
| 58 | _(1024+8, 64) |
| 59 | |
| 60 | #define foreach_clnt_aring_size \ |
| 61 | _(1024+8, 1024) \ |
| 62 | _(2048+8, 128) \ |
| 63 | _(4096+8, 8) |
| 64 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 65 | typedef struct vl_shmem_hdr_ |
| 66 | { |
| 67 | int version; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 69 | /* getpid () for the VLIB client process */ |
| 70 | volatile int vl_pid; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 72 | /* Client sends VLIB msgs here. */ |
| 73 | unix_shared_memory_queue_t *vl_input_queue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 75 | /* Vector of rings; one for each size. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 77 | /* VLIB allocates buffers to send msgs to clients here. */ |
| 78 | ring_alloc_t *vl_rings; |
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 | /* Clients allocate buffer to send msgs to VLIB here. */ |
| 81 | ring_alloc_t *client_rings; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 83 | /* Number of detected application restarts */ |
| 84 | u32 application_restarts; |
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 | /* Number of messages reclaimed during application restart */ |
| 87 | u32 restart_reclaims; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
| 89 | } vl_shmem_hdr_t; |
| 90 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 91 | /* Note that the size of the structure is 16 bytes, with 4 bytes of padding after data[0]. */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 92 | typedef struct msgbuf_ |
| 93 | { |
| 94 | unix_shared_memory_queue_t *q; |
| 95 | u32 data_len; |
| 96 | u8 data[0]; |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 97 | } msgbuf_t; |
| 98 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | #define VL_SHM_VERSION 2 |
| 100 | |
| 101 | #define VL_API_EPOCH_MASK 0xFF |
| 102 | #define VL_API_EPOCH_SHIFT 8 |
| 103 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 104 | static inline u32 |
| 105 | vl_msg_api_handle_get_epoch (u32 index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 107 | return (index & VL_API_EPOCH_MASK); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 110 | static inline u32 |
| 111 | vl_msg_api_handle_get_index (u32 index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 113 | return (index >> VL_API_EPOCH_SHIFT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 115 | |
| 116 | static inline u32 |
| 117 | vl_msg_api_handle_from_index_and_epoch (u32 index, u32 epoch) |
| 118 | { |
| 119 | u32 handle; |
| 120 | ASSERT (index < 0x00FFFFFF); |
| 121 | |
| 122 | handle = (index << VL_API_EPOCH_SHIFT) | (epoch & VL_API_EPOCH_MASK); |
| 123 | return handle; |
| 124 | } |
| 125 | |
| 126 | void *vl_msg_api_alloc (int nbytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | void *vl_msg_api_alloc_as_if_client (int nbytes); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 128 | void vl_msg_api_free (void *a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | int vl_map_shmem (char *region_name, int is_vlib); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 130 | void vl_register_mapped_shmem_region (svm_region_t * rp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | void vl_unmap_shmem (void); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 132 | void vl_msg_api_send_shmem (unix_shared_memory_queue_t * q, u8 * elem); |
| 133 | void vl_msg_api_send_shmem_nolock (unix_shared_memory_queue_t * q, u8 * elem); |
| 134 | void vl_msg_api_send (vl_api_registration_t * rp, u8 * elem); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | int vl_client_connect (char *name, int ctx_quota, int input_queue_size); |
| 136 | void vl_client_disconnect (void); |
| 137 | unix_shared_memory_queue_t *vl_api_client_index_to_input_queue (u32 index); |
| 138 | vl_api_registration_t *vl_api_client_index_to_registration (u32 index); |
| 139 | int vl_client_api_map (char *region_name); |
| 140 | void vl_client_api_unmap (void); |
| 141 | void vl_set_memory_region_name (char *name); |
| 142 | void vl_set_memory_root_path (char *root_path); |
Dave Barach | 16c75df | 2016-05-31 14:05:46 -0400 | [diff] [blame] | 143 | void vl_set_memory_uid (int uid); |
| 144 | void vl_set_memory_gid (int gid); |
Dave Barach | b3d93da | 2016-08-03 14:34:38 -0400 | [diff] [blame] | 145 | void vl_set_global_memory_baseva (u64 baseva); |
| 146 | void vl_set_global_memory_size (u64 size); |
| 147 | void vl_set_api_memory_size (u64 size); |
| 148 | void vl_set_global_pvt_heap_size (u64 size); |
| 149 | void vl_set_api_pvt_heap_size (u64 size); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 150 | void vl_enable_disable_memory_api (vlib_main_t * vm, int yesno); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | void vl_client_disconnect_from_vlib (void); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 152 | int vl_client_connect_to_vlib (char *svm_name, char *client_name, |
| 153 | int rx_queue_size); |
| 154 | int vl_client_connect_to_vlib_no_rx_pthread (char *svm_name, |
| 155 | char *client_name, |
| 156 | int rx_queue_size); |
| 157 | u16 vl_client_get_first_plugin_msg_id (char *plugin_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | |
Dave Barach | 4e281a4 | 2015-12-14 11:13:29 -0500 | [diff] [blame] | 159 | void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length); |
| 160 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | #endif /* included_vlibmemory_api_h */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * fd.io coding-style-patch-verification: ON |
| 165 | * |
| 166 | * Local Variables: |
| 167 | * eval: (c-set-style "gnu") |
| 168 | * End: |
| 169 | */ |