Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
| 18 | #ifndef SRC_VLIBMEMORY_MEMORY_SHARED_H_ |
| 19 | #define SRC_VLIBMEMORY_MEMORY_SHARED_H_ |
| 20 | |
| 21 | #include <vlibapi/api_common.h> |
| 22 | #include <vppinfra/error.h> |
| 23 | |
| 24 | /* Allocated in shared memory */ |
| 25 | |
| 26 | /* |
| 27 | * Ring-allocation scheme for client API messages |
| 28 | * |
| 29 | * Only one proc/thread has control of a given message buffer. |
| 30 | * To free a buffer allocated from one of these rings, we clear |
| 31 | * a field in the buffer (header), and leave. |
| 32 | * |
| 33 | * No locks, no hits, no errors... |
| 34 | */ |
| 35 | typedef struct ring_alloc_ |
| 36 | { |
| 37 | svm_queue_t *rp; |
| 38 | u16 size; |
| 39 | u16 nitems; |
| 40 | u32 hits; |
| 41 | u32 misses; |
| 42 | } ring_alloc_t; |
| 43 | |
| 44 | typedef enum |
| 45 | { |
| 46 | VL_API_VLIB_RING, |
| 47 | VL_API_CLIENT_RING, |
| 48 | VL_API_QUEUE |
| 49 | } vl_api_shm_config_type_t; |
| 50 | |
| 51 | typedef struct vl_api_shm_elem_config_ |
| 52 | { |
| 53 | u8 type; |
| 54 | u8 _pad; |
| 55 | u16 count; |
| 56 | u32 size; |
| 57 | } vl_api_shm_elem_config_t; |
| 58 | |
| 59 | STATIC_ASSERT (sizeof (vl_api_shm_elem_config_t) == 8, |
| 60 | "Size must be exactly 8 bytes"); |
| 61 | |
| 62 | /* |
| 63 | * Initializers for the (shared-memory) rings |
| 64 | * _(size, n). Note: each msg has space for a header. |
| 65 | */ |
| 66 | #define foreach_vl_aring_size \ |
| 67 | _(64+sizeof(ring_alloc_t), 1024) \ |
| 68 | _(256+sizeof(ring_alloc_t), 128) \ |
| 69 | _(1024+sizeof(ring_alloc_t), 64) |
| 70 | |
| 71 | #define foreach_clnt_aring_size \ |
| 72 | _(1024+sizeof(ring_alloc_t), 1024) \ |
| 73 | _(2048+sizeof(ring_alloc_t), 128) \ |
| 74 | _(4096+sizeof(ring_alloc_t), 8) |
| 75 | |
| 76 | typedef struct vl_shmem_hdr_ |
| 77 | { |
| 78 | int version; |
| 79 | |
| 80 | /* getpid () for the VLIB client process */ |
| 81 | volatile int vl_pid; |
| 82 | |
| 83 | /* Client sends VLIB msgs here. */ |
| 84 | svm_queue_t *vl_input_queue; |
| 85 | |
| 86 | /* Vector of rings; one for each size. */ |
| 87 | |
| 88 | /* VLIB allocates buffers to send msgs to clients here. */ |
| 89 | ring_alloc_t *vl_rings; |
| 90 | |
| 91 | /* Clients allocate buffer to send msgs to VLIB here. */ |
| 92 | ring_alloc_t *client_rings; |
| 93 | |
| 94 | /* Number of detected application restarts */ |
| 95 | u32 application_restarts; |
| 96 | |
| 97 | /* Number of messages reclaimed during application restart */ |
| 98 | u32 restart_reclaims; |
| 99 | |
| 100 | /* Number of garbage-collected messages */ |
| 101 | u32 garbage_collects; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 102 | |
| 103 | /* Socket file index used to bootstrap shmem region */ |
| 104 | u32 clib_file_index; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 105 | } vl_shmem_hdr_t; |
| 106 | |
| 107 | #define VL_SHM_VERSION 2 |
| 108 | #define VL_API_EPOCH_MASK 0xFF |
| 109 | #define VL_API_EPOCH_SHIFT 8 |
| 110 | |
| 111 | void *vl_msg_api_alloc (int nbytes); |
| 112 | void *vl_msg_api_alloc_or_null (int nbytes); |
| 113 | void *vl_msg_api_alloc_as_if_client (int nbytes); |
| 114 | void *vl_msg_api_alloc_as_if_client_or_null (int nbytes); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 115 | void *vl_mem_api_alloc_as_if_client_w_reg (vl_api_registration_t * reg, |
| 116 | int nbytes); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 117 | void vl_msg_api_free (void *a); |
| 118 | int vl_map_shmem (const char *region_name, int is_vlib); |
| 119 | void vl_unmap_shmem (void); |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 120 | void vl_unmap_shmem_client (void); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 121 | void vl_register_mapped_shmem_region (svm_region_t * rp); |
| 122 | void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem); |
| 123 | void vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem); |
Florin Coras | af0ff5a | 2018-01-10 08:17:03 -0800 | [diff] [blame] | 124 | int vl_mem_api_can_send (svm_queue_t * q); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 125 | void vl_set_memory_region_name (const char *name); |
| 126 | void vl_set_memory_root_path (const char *root_path); |
| 127 | void vl_set_memory_uid (int uid); |
| 128 | void vl_set_memory_gid (int gid); |
| 129 | void vl_set_global_memory_baseva (u64 baseva); |
| 130 | void vl_set_global_memory_size (u64 size); |
| 131 | void vl_set_api_memory_size (u64 size); |
| 132 | void vl_set_global_pvt_heap_size (u64 size); |
| 133 | void vl_set_api_pvt_heap_size (u64 size); |
| 134 | void vl_init_shmem (svm_region_t * vlib_rp, vl_api_shm_elem_config_t * config, |
| 135 | int is_vlib, int is_private_region); |
| 136 | |
| 137 | #endif /* SRC_VLIBMEMORY_MEMORY_SHARED_H_ */ |
| 138 | |
| 139 | /* |
| 140 | * fd.io coding-style-patch-verification: ON |
| 141 | * |
| 142 | * Local Variables: |
| 143 | * eval: (c-set-style "gnu") |
| 144 | * End: |
| 145 | */ |