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