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 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 18 | #include <svm/message_queue.h> |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 19 | #include <vppinfra/lock.h> |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 20 | #include <vppinfra/valloc.h> |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 21 | #include <svm/fifo_segment.h> |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 22 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 23 | typedef struct _segment_manager_props |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 24 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 25 | u32 rx_fifo_size; /**< receive fifo size */ |
| 26 | u32 tx_fifo_size; /**< transmit fifo size */ |
| 27 | u32 evt_q_size; /**< event queue length */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 28 | u32 prealloc_fifos; /**< preallocated fifo pairs */ |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 29 | uword segment_size; /**< first segment size */ |
| 30 | uword add_segment_size; /**< additional segment size */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 31 | u8 add_segment:1; /**< can add new segments flag */ |
| 32 | u8 use_mq_eventfd:1; /**< use eventfds for mqs flag */ |
| 33 | u8 reserved:6; /**< reserved flags */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 34 | u8 n_slices; /**< number of fs slices/threads */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 35 | ssvm_segment_type_t segment_type; /**< seg type: if set to SSVM_N_TYPES, |
| 36 | private segments are used */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 37 | } segment_manager_props_t; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 38 | |
| 39 | typedef struct _segment_manager |
| 40 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 41 | /** Pool of segments allocated by this manager */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 42 | fifo_segment_t *segments; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 43 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 44 | /** rwlock that protects the segments pool */ |
| 45 | clib_rwlock_t segments_rwlock; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 46 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 47 | /** Owner app worker index */ |
| 48 | u32 app_wrk_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 49 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 50 | /** |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 51 | * First segment should not be deleted unless segment manger is deleted. |
| 52 | * This also indicates that the segment manager is the first to have been |
| 53 | * allocated for the app. |
| 54 | */ |
| 55 | u8 first_is_protected; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * App event queue allocated in first segment |
| 59 | */ |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 60 | svm_msg_q_t *event_queue; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 61 | } segment_manager_t; |
| 62 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 63 | typedef struct segment_manager_main_init_args_ |
| 64 | { |
| 65 | u64 baseva; |
| 66 | u64 size; |
| 67 | } segment_manager_main_init_args_t; |
| 68 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 69 | #define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0) |
| 70 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 71 | segment_manager_t *segment_manager_alloc (void); |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 72 | int segment_manager_init (segment_manager_t * sm, uword first_seg_size, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 73 | u32 prealloc_fifo_pairs); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 74 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Cleanup segment manager |
| 77 | * |
| 78 | * @param sm segment manager to be freed |
| 79 | */ |
| 80 | void segment_manager_free (segment_manager_t * sm); |
| 81 | |
| 82 | /** |
| 83 | * Initiate segment manager cleanup |
| 84 | * |
| 85 | * @param sm segment manager to be freed |
| 86 | */ |
| 87 | void segment_manager_init_free (segment_manager_t * sm); |
| 88 | segment_manager_t *segment_manager_get (u32 index); |
| 89 | segment_manager_t *segment_manager_get_if_valid (u32 index); |
| 90 | u32 segment_manager_index (segment_manager_t * sm); |
| 91 | |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 92 | int segment_manager_add_segment (segment_manager_t * sm, uword segment_size); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 93 | void segment_manager_del_segment (segment_manager_t * sm, |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 94 | fifo_segment_t * fs); |
| 95 | fifo_segment_t *segment_manager_get_segment (segment_manager_t * sm, |
| 96 | u32 segment_index); |
| 97 | fifo_segment_t *segment_manager_get_segment_w_handle (u64 sh); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 98 | fifo_segment_t *segment_manager_get_segment_w_lock (segment_manager_t * sm, |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 99 | u32 segment_index); |
| 100 | int segment_manager_add_first_segment (segment_manager_t * sm, |
| 101 | u32 segment_size); |
| 102 | u64 segment_manager_make_segment_handle (u32 segment_manager_index, |
| 103 | u32 segment_index); |
| 104 | u64 segment_manager_segment_handle (segment_manager_t * sm, |
| 105 | fifo_segment_t * segment); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 106 | void segment_manager_segment_reader_unlock (segment_manager_t * sm); |
| 107 | void segment_manager_segment_writer_unlock (segment_manager_t * sm); |
| 108 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 109 | int segment_manager_alloc_session_fifos (segment_manager_t * sm, |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 110 | u32 thread_index, |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 111 | svm_fifo_t ** rx_fifo, |
| 112 | svm_fifo_t ** tx_fifo); |
| 113 | int segment_manager_try_alloc_fifos (fifo_segment_t * fs, |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 114 | u32 thread_index, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 115 | u32 rx_fifo_size, u32 tx_fifo_size, |
| 116 | svm_fifo_t ** rx_fifo, |
| 117 | svm_fifo_t ** tx_fifo); |
Florin Coras | 19223e0 | 2019-03-03 14:56:05 -0800 | [diff] [blame] | 118 | void segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 119 | svm_fifo_t * tx_fifo); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Grows fifo owned by segment manager |
| 123 | * |
| 124 | * @param sm segment manager that owns the fifo |
| 125 | * @param f fifo to be grown |
| 126 | * @param size amount of bytes to add to fifo |
| 127 | * @return 0 on success, negative number otherwise |
| 128 | */ |
| 129 | int segment_manager_grow_fifo (segment_manager_t * sm, svm_fifo_t * f, |
| 130 | u32 size); |
Florin Coras | 344ce42 | 2019-05-03 11:46:55 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Request to shrink fifo owned by segment manager |
| 134 | * |
| 135 | * If this is not called by the producer, no attempt is made to reduce the |
| 136 | * size until the producer tries to enqueue more data. To collect the chunks |
| 137 | * that are to be removed call @ref segment_manager_collect_fifo_chunks |
| 138 | * |
| 139 | * Size reduction does not affect fifo chunk boundaries. Therefore chunks are |
| 140 | * not split and the amount of bytes to be removed can be equal to or less |
| 141 | * than what was requested. |
| 142 | * |
| 143 | * @param sm segment manager that owns the fifo |
| 144 | * @param f fifo to be shrunk |
| 145 | * @param size amount of bytes to remove from fifo |
| 146 | * @param is_producer flag that indicates is caller is the producer for the |
| 147 | * fifo. |
| 148 | * @return actual number of bytes to be removed |
| 149 | */ |
| 150 | int segment_manager_shrink_fifo (segment_manager_t * sm, svm_fifo_t * f, |
| 151 | u32 size, u8 is_producer); |
| 152 | |
| 153 | /** |
| 154 | * Collect fifo chunks that are no longer used |
| 155 | * |
| 156 | * This should not be called unless SVM_FIFO_F_COLLECT_CHUNKS is set for |
| 157 | * the fifo. The chunks are returned to the fifo segment freelist. |
| 158 | * |
| 159 | * @param sm segment manager that owns the fifo |
| 160 | * @param f fifo whose chunks are to be collected |
| 161 | * @return 0 on success, error otherwise |
| 162 | */ |
| 163 | int segment_manager_collect_fifo_chunks (segment_manager_t * sm, |
| 164 | svm_fifo_t * f); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 165 | u8 segment_manager_has_fifos (segment_manager_t * sm); |
| 166 | |
| 167 | svm_msg_q_t *segment_manager_alloc_queue (fifo_segment_t * fs, |
| 168 | segment_manager_props_t * props); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 169 | void segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 170 | svm_msg_q_t *segment_manager_event_queue (segment_manager_t * sm); |
| 171 | u32 segment_manager_evt_q_expected_size (u32 q_size); |
| 172 | |
Florin Coras | 4e4531e | 2017-11-06 23:27:56 -0800 | [diff] [blame] | 173 | void segment_manager_app_detach (segment_manager_t * sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 174 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Cleanup segment manager sessions |
| 177 | * |
| 178 | * Initiates disconnects for all sessions 'owned' by a segment manager by |
| 179 | * leveraging the backpointers that fifos keep. |
| 180 | * |
| 181 | * @param sm segment manager whose sessions are to be disconnected |
| 182 | */ |
| 183 | void segment_manager_del_sessions (segment_manager_t * sm); |
| 184 | void segment_manager_format_sessions (segment_manager_t * sm, int verbose); |
| 185 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 186 | void segment_manager_main_init (segment_manager_main_init_args_t * a); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 187 | |
| 188 | segment_manager_props_t *segment_manager_props_init (segment_manager_props_t * |
| 189 | sm); |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 190 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 191 | #endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */ |
| 192 | /* |
| 193 | * fd.io coding-style-patch-verification: ON |
| 194 | * |
| 195 | * Local Variables: |
| 196 | * eval: (c-set-style "gnu") |
| 197 | * End: |
| 198 | */ |