Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Cisco and/or its affiliates. |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 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 |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | #ifndef SRC_VNET_SESSION_SEGMENT_MANAGER_H_ |
| 16 | #define SRC_VNET_SESSION_SEGMENT_MANAGER_H_ |
| 17 | |
| 18 | #include <vnet/vnet.h> |
| 19 | #include <svm/svm_fifo_segment.h> |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 20 | #include <svm/message_queue.h> |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 21 | #include <vlibmemory/api.h> |
| 22 | #include <vppinfra/lock.h> |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 23 | #include <vppinfra/valloc.h> |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 24 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 25 | typedef struct _segment_manager_properties |
| 26 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 27 | u32 rx_fifo_size; /**< receive fifo size */ |
| 28 | u32 tx_fifo_size; /**< transmit fifo size */ |
| 29 | u32 evt_q_size; /**< event queue length */ |
| 30 | u32 segment_size; /**< first segment size */ |
| 31 | u32 prealloc_fifos; /**< preallocated fifo pairs */ |
| 32 | u32 add_segment_size; /**< additional segment size */ |
| 33 | u8 add_segment:1; /**< can add new segments flag */ |
| 34 | u8 use_mq_eventfd:1; /**< use eventfds for mqs flag */ |
| 35 | u8 reserved:6; /**< reserved flags */ |
| 36 | ssvm_segment_type_t segment_type; /**< seg type: if set to SSVM_N_TYPES, |
| 37 | private segments are used */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 38 | } segment_manager_properties_t; |
| 39 | |
| 40 | typedef struct _segment_manager |
| 41 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 42 | /** Pool of segments allocated by this manager */ |
| 43 | svm_fifo_segment_private_t *segments; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 44 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 45 | /** rwlock that protects the segments pool */ |
| 46 | clib_rwlock_t segments_rwlock; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 47 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 48 | /** Owner app worker index */ |
| 49 | u32 app_wrk_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 50 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 51 | /** |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 52 | * First segment should not be deleted unless segment manger is deleted. |
| 53 | * This also indicates that the segment manager is the first to have been |
| 54 | * allocated for the app. |
| 55 | */ |
| 56 | u8 first_is_protected; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * App event queue allocated in first segment |
| 60 | */ |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 61 | svm_msg_q_t *event_queue; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 62 | } segment_manager_t; |
| 63 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 64 | #define segment_manager_foreach_segment_w_lock(VAR, SM, BODY) \ |
| 65 | do { \ |
| 66 | clib_rwlock_reader_lock (&(SM)->segments_rwlock); \ |
| 67 | pool_foreach((VAR), ((SM)->segments), (BODY)); \ |
| 68 | clib_rwlock_reader_unlock (&(SM)->segments_rwlock); \ |
| 69 | } while (0) |
| 70 | |
| 71 | typedef struct segment_manager_main_ |
| 72 | { |
| 73 | /** Pool of segment managers */ |
| 74 | segment_manager_t *segment_managers; |
| 75 | |
| 76 | /** Virtual address allocator */ |
| 77 | clib_valloc_main_t va_allocator; |
| 78 | |
| 79 | } segment_manager_main_t; |
| 80 | |
| 81 | extern segment_manager_main_t segment_manager_main; |
| 82 | |
| 83 | typedef struct segment_manager_main_init_args_ |
| 84 | { |
| 85 | u64 baseva; |
| 86 | u64 size; |
| 87 | } segment_manager_main_init_args_t; |
| 88 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 89 | #define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0) |
| 90 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 91 | /** Pool of segment managers */ |
| 92 | extern segment_manager_t *segment_managers; |
| 93 | |
| 94 | always_inline segment_manager_t * |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 95 | segment_manager_get (u32 index) |
| 96 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 97 | return pool_elt_at_index (segment_manager_main.segment_managers, index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | always_inline segment_manager_t * |
| 101 | segment_manager_get_if_valid (u32 index) |
| 102 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 103 | if (pool_is_free_index (segment_manager_main.segment_managers, index)) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 104 | return 0; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 105 | return pool_elt_at_index (segment_manager_main.segment_managers, index); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | always_inline u32 |
| 109 | segment_manager_index (segment_manager_t * sm) |
| 110 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 111 | return sm - segment_manager_main.segment_managers; |
| 112 | } |
| 113 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 114 | always_inline svm_msg_q_t * |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 115 | segment_manager_event_queue (segment_manager_t * sm) |
| 116 | { |
| 117 | return sm->event_queue; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 120 | always_inline u64 |
| 121 | segment_manager_make_segment_handle (u32 segment_manager_index, |
| 122 | u32 segment_index) |
| 123 | { |
| 124 | return (((u64) segment_manager_index << 32) | segment_index); |
| 125 | } |
| 126 | |
| 127 | u64 segment_manager_segment_handle (segment_manager_t * sm, |
| 128 | svm_fifo_segment_private_t * segment); |
| 129 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 130 | segment_manager_t *segment_manager_new (); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 131 | int segment_manager_init (segment_manager_t * sm, u32 first_seg_size, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 132 | u32 prealloc_fifo_pairs); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 133 | |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 134 | svm_fifo_segment_private_t *segment_manager_get_segment (segment_manager_t *, |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 135 | u32 segment_index); |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 136 | svm_fifo_segment_private_t *segment_manager_get_segment_w_handle (u64); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 137 | svm_fifo_segment_private_t |
| 138 | * segment_manager_get_segment_w_lock (segment_manager_t * sm, |
| 139 | u32 segment_index); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 140 | int segment_manager_add_segment (segment_manager_t * sm, u32 segment_size); |
| 141 | void segment_manager_del_segment (segment_manager_t * sm, |
| 142 | svm_fifo_segment_private_t * fs); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 143 | void segment_manager_segment_reader_unlock (segment_manager_t * sm); |
| 144 | void segment_manager_segment_writer_unlock (segment_manager_t * sm); |
| 145 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 146 | int segment_manager_add_first_segment (segment_manager_t * sm, |
| 147 | u32 segment_size); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 148 | void segment_manager_del_sessions (segment_manager_t * sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 149 | void segment_manager_del (segment_manager_t * sm); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 150 | void segment_manager_init_del (segment_manager_t * sm); |
| 151 | u8 segment_manager_has_fifos (segment_manager_t * sm); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 152 | int segment_manager_alloc_session_fifos (segment_manager_t * sm, |
| 153 | svm_fifo_t ** server_rx_fifo, |
| 154 | svm_fifo_t ** server_tx_fifo, |
| 155 | u32 * fifo_segment_index); |
| 156 | int segment_manager_try_alloc_fifos (svm_fifo_segment_private_t * fs, |
| 157 | u32 rx_fifo_size, u32 tx_fifo_size, |
| 158 | svm_fifo_t ** rx_fifo, |
| 159 | svm_fifo_t ** tx_fifo); |
| 160 | void segment_manager_dealloc_fifos (u32 segment_index, svm_fifo_t * rx_fifo, |
| 161 | svm_fifo_t * tx_fifo); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 162 | u32 segment_manager_evt_q_expected_size (u32 q_size); |
| 163 | svm_msg_q_t *segment_manager_alloc_queue (svm_fifo_segment_private_t * fs, |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 164 | segment_manager_properties_t * |
| 165 | props); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 166 | void segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q); |
Florin Coras | 4e4531e | 2017-11-06 23:27:56 -0800 | [diff] [blame] | 167 | void segment_manager_app_detach (segment_manager_t * sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 168 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 169 | void segment_manager_main_init (segment_manager_main_init_args_t * a); |
| 170 | segment_manager_properties_t |
| 171 | * segment_manager_properties_init (segment_manager_properties_t * sm); |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 172 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 173 | #endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */ |
| 174 | /* |
| 175 | * fd.io coding-style-patch-verification: ON |
| 176 | * |
| 177 | * Local Variables: |
| 178 | * eval: (c-set-style "gnu") |
| 179 | * End: |
| 180 | */ |