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> |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 22 | #include <vnet/session/session_types.h> |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 23 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 24 | typedef struct _segment_manager_props |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 25 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 26 | u32 rx_fifo_size; /**< receive fifo size */ |
| 27 | u32 tx_fifo_size; /**< transmit fifo size */ |
| 28 | u32 evt_q_size; /**< event queue length */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 29 | u32 prealloc_fifos; /**< preallocated fifo pairs */ |
Florin Coras | 9845c20 | 2020-04-28 01:54:22 +0000 | [diff] [blame] | 30 | u32 prealloc_fifo_hdrs; /**< preallocated fifo hdrs */ |
Florin Coras | ef4f3e7 | 2019-12-11 14:27:53 -0800 | [diff] [blame] | 31 | uword segment_size; /**< first segment size */ |
| 32 | uword add_segment_size; /**< additional segment size */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 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 */ |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 36 | u8 n_slices; /**< number of fs slices/threads */ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 37 | ssvm_segment_type_t segment_type; /**< seg type: if set to SSVM_N_TYPES, |
| 38 | private segments are used */ |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 39 | u32 max_fifo_size; /**< max fifo size */ |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 40 | u8 high_watermark; /**< memory usage high watermark % */ |
| 41 | u8 low_watermark; /**< memory usage low watermark % */ |
Florin Coras | 2de9c0f | 2020-02-02 19:30:39 +0000 | [diff] [blame] | 42 | u8 pct_first_alloc; /**< pct of fifo size to alloc */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 43 | } segment_manager_props_t; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 44 | |
Florin Coras | c8e812f | 2020-05-14 05:32:18 +0000 | [diff] [blame] | 45 | typedef enum seg_manager_flag_ |
| 46 | { |
| 47 | SEG_MANAGER_F_DETACHED = 1 << 0, |
| 48 | SEG_MANAGER_F_DETACHED_LISTENER = 1 << 1, |
| 49 | } seg_manager_flag_t; |
| 50 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 51 | typedef struct _segment_manager |
| 52 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 53 | /** Pool of segments allocated by this manager */ |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 54 | fifo_segment_t *segments; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 55 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 56 | /** rwlock that protects the segments pool */ |
| 57 | clib_rwlock_t segments_rwlock; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 58 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 59 | /** Owner app worker index */ |
| 60 | u32 app_wrk_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 61 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 62 | /** |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 63 | * First segment should not be deleted unless segment manger is deleted. |
| 64 | * This also indicates that the segment manager is the first to have been |
| 65 | * allocated for the app. |
| 66 | */ |
| 67 | u8 first_is_protected; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * App event queue allocated in first segment |
| 71 | */ |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 72 | svm_msg_q_t *event_queue; |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 73 | |
Florin Coras | c8e812f | 2020-05-14 05:32:18 +0000 | [diff] [blame] | 74 | u8 flags; |
| 75 | |
Ryujiro Shibuya | d8f48e2 | 2020-01-22 12:11:42 +0000 | [diff] [blame] | 76 | u32 max_fifo_size; |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 77 | u8 high_watermark; |
| 78 | u8 low_watermark; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 79 | } segment_manager_t; |
| 80 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 81 | #define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0) |
| 82 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 83 | segment_manager_t *segment_manager_alloc (void); |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 84 | int segment_manager_init (segment_manager_t * sm); |
Florin Coras | a107f40 | 2020-09-29 19:18:46 -0700 | [diff] [blame] | 85 | int segment_manager_init_first (segment_manager_t * sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 86 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Cleanup segment manager |
| 89 | * |
| 90 | * @param sm segment manager to be freed |
| 91 | */ |
| 92 | void segment_manager_free (segment_manager_t * sm); |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 93 | void segment_manager_free_safe (segment_manager_t *sm); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Initiate segment manager cleanup |
| 97 | * |
| 98 | * @param sm segment manager to be freed |
| 99 | */ |
| 100 | void segment_manager_init_free (segment_manager_t * sm); |
| 101 | segment_manager_t *segment_manager_get (u32 index); |
| 102 | segment_manager_t *segment_manager_get_if_valid (u32 index); |
| 103 | u32 segment_manager_index (segment_manager_t * sm); |
| 104 | |
liuyacan | 9f29903 | 2021-04-25 20:11:30 +0800 | [diff] [blame] | 105 | int segment_manager_add_segment (segment_manager_t *sm, uword segment_size, |
| 106 | u8 notify_app); |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 107 | int segment_manager_add_segment2 (segment_manager_t *sm, uword segment_size, |
| 108 | u8 flags); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 109 | void segment_manager_del_segment (segment_manager_t * sm, |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 110 | fifo_segment_t * fs); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 111 | void segment_manager_lock_and_del_segment (segment_manager_t * sm, |
| 112 | u32 fs_index); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 113 | fifo_segment_t *segment_manager_get_segment (segment_manager_t * sm, |
| 114 | u32 segment_index); |
| 115 | fifo_segment_t *segment_manager_get_segment_w_handle (u64 sh); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 116 | 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] | 117 | u32 segment_index); |
| 118 | int segment_manager_add_first_segment (segment_manager_t * sm, |
| 119 | u32 segment_size); |
| 120 | u64 segment_manager_make_segment_handle (u32 segment_manager_index, |
| 121 | u32 segment_index); |
| 122 | u64 segment_manager_segment_handle (segment_manager_t * sm, |
| 123 | fifo_segment_t * segment); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 124 | void segment_manager_segment_reader_unlock (segment_manager_t * sm); |
| 125 | void segment_manager_segment_writer_unlock (segment_manager_t * sm); |
| 126 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 127 | int segment_manager_alloc_session_fifos (segment_manager_t * sm, |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 128 | u32 thread_index, |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 129 | svm_fifo_t ** rx_fifo, |
| 130 | svm_fifo_t ** tx_fifo); |
| 131 | int segment_manager_try_alloc_fifos (fifo_segment_t * fs, |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 132 | u32 thread_index, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 133 | u32 rx_fifo_size, u32 tx_fifo_size, |
| 134 | svm_fifo_t ** rx_fifo, |
| 135 | svm_fifo_t ** tx_fifo); |
Florin Coras | 19223e0 | 2019-03-03 14:56:05 -0800 | [diff] [blame] | 136 | void segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo, |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 137 | svm_fifo_t * tx_fifo); |
Florin Coras | 0bc78d8 | 2021-01-09 14:34:01 -0800 | [diff] [blame] | 138 | void segment_manager_detach_fifo (segment_manager_t *sm, svm_fifo_t **f); |
| 139 | void segment_manager_attach_fifo (segment_manager_t *sm, svm_fifo_t **f, |
| 140 | session_t *s); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 141 | |
Ryujiro Shibuya | 234fe89 | 2019-12-25 07:40:54 +0000 | [diff] [blame] | 142 | void segment_manager_set_watermarks (segment_manager_t * sm, |
| 143 | u8 high_watermark, u8 low_watermark); |
| 144 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 145 | u8 segment_manager_has_fifos (segment_manager_t * sm); |
| 146 | |
| 147 | svm_msg_q_t *segment_manager_alloc_queue (fifo_segment_t * fs, |
| 148 | segment_manager_props_t * props); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 149 | 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] | 150 | svm_msg_q_t *segment_manager_event_queue (segment_manager_t * sm); |
| 151 | u32 segment_manager_evt_q_expected_size (u32 q_size); |
| 152 | |
Florin Coras | bf39597 | 2020-04-30 15:05:24 +0000 | [diff] [blame] | 153 | u8 segment_manager_app_detached (segment_manager_t * sm); |
Florin Coras | 4e4531e | 2017-11-06 23:27:56 -0800 | [diff] [blame] | 154 | void segment_manager_app_detach (segment_manager_t * sm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 155 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Cleanup segment manager sessions |
| 158 | * |
| 159 | * Initiates disconnects for all sessions 'owned' by a segment manager by |
| 160 | * leveraging the backpointers that fifos keep. |
| 161 | * |
| 162 | * @param sm segment manager whose sessions are to be disconnected |
| 163 | */ |
| 164 | void segment_manager_del_sessions (segment_manager_t * sm); |
liuyacan | 87d48ad | 2021-04-28 11:34:03 +0000 | [diff] [blame] | 165 | void segment_manager_del_sessions_filter (segment_manager_t *sm, |
| 166 | session_state_t *states); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 167 | void segment_manager_format_sessions (segment_manager_t * sm, int verbose); |
| 168 | |
Florin Coras | 9a45bd8 | 2020-12-28 16:28:07 -0800 | [diff] [blame] | 169 | void segment_manager_main_init (void); |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 170 | |
| 171 | segment_manager_props_t *segment_manager_props_init (segment_manager_props_t * |
| 172 | sm); |
Florin Coras | ad0c77f | 2017-11-09 18:00:15 -0800 | [diff] [blame] | 173 | |
Florin Coras | 9845c20 | 2020-04-28 01:54:22 +0000 | [diff] [blame] | 174 | static inline void |
| 175 | segment_manager_parse_segment_handle (u64 segment_handle, u32 * sm_index, |
| 176 | u32 * segment_index) |
| 177 | { |
| 178 | *sm_index = segment_handle >> 32; |
| 179 | *segment_index = segment_handle & 0xFFFFFFFF; |
| 180 | } |
| 181 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 182 | #endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */ |
| 183 | /* |
| 184 | * fd.io coding-style-patch-verification: ON |
| 185 | * |
| 186 | * Local Variables: |
| 187 | * eval: (c-set-style "gnu") |
| 188 | * End: |
| 189 | */ |