blob: e786b3144c2ef080152a79f6b9f481f77aa9409a [file] [log] [blame]
Florin Coras6cf30ad2017-04-04 23:08:23 -07001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Coras6cf30ad2017-04-04 23:08:23 -07003 * 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 Coras3c2fed52018-07-04 04:15:05 -070018#include <svm/message_queue.h>
Florin Corasa5464812017-04-19 13:00:05 -070019#include <vppinfra/lock.h>
Florin Corasa332c462018-01-31 06:52:17 -080020#include <vppinfra/valloc.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +000022#include <vnet/session/session_types.h>
Florin Corasa5464812017-04-19 13:00:05 -070023
Florin Coras88001c62019-04-24 14:44:46 -070024typedef struct _segment_manager_props
Florin Coras6cf30ad2017-04-04 23:08:23 -070025{
Florin Coras15531972018-08-12 23:50:53 -070026 u32 rx_fifo_size; /**< receive fifo size */
27 u32 tx_fifo_size; /**< transmit fifo size */
28 u32 evt_q_size; /**< event queue length */
Florin Coras15531972018-08-12 23:50:53 -070029 u32 prealloc_fifos; /**< preallocated fifo pairs */
Florin Coras9845c202020-04-28 01:54:22 +000030 u32 prealloc_fifo_hdrs; /**< preallocated fifo hdrs */
Florin Corasef4f3e72019-12-11 14:27:53 -080031 uword segment_size; /**< first segment size */
32 uword add_segment_size; /**< additional segment size */
Florin Coras15531972018-08-12 23:50:53 -070033 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 Coras62ddc032019-12-08 18:30:42 -080036 u8 n_slices; /**< number of fs slices/threads */
Florin Coras15531972018-08-12 23:50:53 -070037 ssvm_segment_type_t segment_type; /**< seg type: if set to SSVM_N_TYPES,
38 private segments are used */
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +000039 u32 max_fifo_size; /**< max fifo size */
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000040 u8 high_watermark; /**< memory usage high watermark % */
41 u8 low_watermark; /**< memory usage low watermark % */
Florin Coras2de9c0f2020-02-02 19:30:39 +000042 u8 pct_first_alloc; /**< pct of fifo size to alloc */
Junfeng Wangc795b882022-08-12 16:24:46 +080043 u8 huge_page; /**< use hugepage */
Florin Coras88001c62019-04-24 14:44:46 -070044} segment_manager_props_t;
Florin Coras6cf30ad2017-04-04 23:08:23 -070045
Florin Corasc8e812f2020-05-14 05:32:18 +000046typedef enum seg_manager_flag_
47{
48 SEG_MANAGER_F_DETACHED = 1 << 0,
49 SEG_MANAGER_F_DETACHED_LISTENER = 1 << 1,
50} seg_manager_flag_t;
51
Florin Coras6cf30ad2017-04-04 23:08:23 -070052typedef struct _segment_manager
53{
Florin Corasa332c462018-01-31 06:52:17 -080054 /** Pool of segments allocated by this manager */
Florin Coras88001c62019-04-24 14:44:46 -070055 fifo_segment_t *segments;
Florin Corasa5464812017-04-19 13:00:05 -070056
Florin Corasa332c462018-01-31 06:52:17 -080057 /** rwlock that protects the segments pool */
58 clib_rwlock_t segments_rwlock;
Florin Coras6cf30ad2017-04-04 23:08:23 -070059
Florin Coras15531972018-08-12 23:50:53 -070060 /** Owner app worker index */
61 u32 app_wrk_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -070062
Florin Corasc87c91d2017-08-16 19:55:49 -070063 /**
Florin Corasc87c91d2017-08-16 19:55:49 -070064 * First segment should not be deleted unless segment manger is deleted.
65 * This also indicates that the segment manager is the first to have been
66 * allocated for the app.
67 */
68 u8 first_is_protected;
Florin Corasa332c462018-01-31 06:52:17 -080069
70 /**
71 * App event queue allocated in first segment
72 */
Florin Coras3c2fed52018-07-04 04:15:05 -070073 svm_msg_q_t *event_queue;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000074
Florin Corasc8e812f2020-05-14 05:32:18 +000075 u8 flags;
76
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +000077 u32 max_fifo_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000078 u8 high_watermark;
79 u8 low_watermark;
Florin Coras6cf30ad2017-04-04 23:08:23 -070080} segment_manager_t;
81
Florin Corasc87c91d2017-08-16 19:55:49 -070082#define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0)
83
Florin Coras88001c62019-04-24 14:44:46 -070084segment_manager_t *segment_manager_alloc (void);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000085int segment_manager_init (segment_manager_t * sm);
Florin Corasa107f402020-09-29 19:18:46 -070086int segment_manager_init_first (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -070087
Florin Coras88001c62019-04-24 14:44:46 -070088/**
89 * Cleanup segment manager
90 *
91 * @param sm segment manager to be freed
92 */
93void segment_manager_free (segment_manager_t * sm);
Florin Corasda78c5a2021-06-09 14:55:24 -070094void segment_manager_free_safe (segment_manager_t *sm);
Florin Coras88001c62019-04-24 14:44:46 -070095
96/**
97 * Initiate segment manager cleanup
98 *
99 * @param sm segment manager to be freed
100 */
101void segment_manager_init_free (segment_manager_t * sm);
102segment_manager_t *segment_manager_get (u32 index);
103segment_manager_t *segment_manager_get_if_valid (u32 index);
104u32 segment_manager_index (segment_manager_t * sm);
105
Florin Corasc8249772021-11-30 20:21:57 -0800106/**
107 * Add segment without lock
108 *
109 * @param sm Segment manager
110 * @param segment_size Size of segment to be added
111 * @param notify_app Flag set if app notification requested
112 */
liuyacan9f299032021-04-25 20:11:30 +0800113int segment_manager_add_segment (segment_manager_t *sm, uword segment_size,
114 u8 notify_app);
Florin Corasc8249772021-11-30 20:21:57 -0800115
116/**
117 * Add segment with lock
118 *
119 * @param sm Segment manager
120 * @param segment_size Size of segment to be added
121 * @param flags Flags to be set on segment
122 */
Florin Coras6973c732021-06-17 15:53:38 -0700123int segment_manager_add_segment2 (segment_manager_t *sm, uword segment_size,
124 u8 flags);
Florin Corasf8f516a2018-02-08 15:10:09 -0800125void segment_manager_del_segment (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700126 fifo_segment_t * fs);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700127void segment_manager_lock_and_del_segment (segment_manager_t * sm,
128 u32 fs_index);
Florin Coras88001c62019-04-24 14:44:46 -0700129fifo_segment_t *segment_manager_get_segment (segment_manager_t * sm,
130 u32 segment_index);
131fifo_segment_t *segment_manager_get_segment_w_handle (u64 sh);
Florin Corasb095a3c2019-04-25 12:58:46 -0700132fifo_segment_t *segment_manager_get_segment_w_lock (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700133 u32 segment_index);
134int segment_manager_add_first_segment (segment_manager_t * sm,
135 u32 segment_size);
136u64 segment_manager_make_segment_handle (u32 segment_manager_index,
137 u32 segment_index);
138u64 segment_manager_segment_handle (segment_manager_t * sm,
139 fifo_segment_t * segment);
Florin Corasa332c462018-01-31 06:52:17 -0800140void segment_manager_segment_reader_unlock (segment_manager_t * sm);
Florin Corasa332c462018-01-31 06:52:17 -0800141
Florin Corasf8f516a2018-02-08 15:10:09 -0800142int segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Coras62ddc032019-12-08 18:30:42 -0800143 u32 thread_index,
Florin Coras88001c62019-04-24 14:44:46 -0700144 svm_fifo_t ** rx_fifo,
145 svm_fifo_t ** tx_fifo);
146int segment_manager_try_alloc_fifos (fifo_segment_t * fs,
Florin Coras62ddc032019-12-08 18:30:42 -0800147 u32 thread_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800148 u32 rx_fifo_size, u32 tx_fifo_size,
149 svm_fifo_t ** rx_fifo,
150 svm_fifo_t ** tx_fifo);
Florin Coras19223e02019-03-03 14:56:05 -0800151void segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo,
Florin Corasf8f516a2018-02-08 15:10:09 -0800152 svm_fifo_t * tx_fifo);
Florin Coras0bc78d82021-01-09 14:34:01 -0800153void segment_manager_detach_fifo (segment_manager_t *sm, svm_fifo_t **f);
154void segment_manager_attach_fifo (segment_manager_t *sm, svm_fifo_t **f,
155 session_t *s);
Florin Corasb095a3c2019-04-25 12:58:46 -0700156
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000157void segment_manager_set_watermarks (segment_manager_t * sm,
158 u8 high_watermark, u8 low_watermark);
159
Florin Coras88001c62019-04-24 14:44:46 -0700160u8 segment_manager_has_fifos (segment_manager_t * sm);
161
162svm_msg_q_t *segment_manager_alloc_queue (fifo_segment_t * fs,
163 segment_manager_props_t * props);
Florin Corase86a8ed2018-01-05 03:20:25 -0800164void segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q);
Florin Coras88001c62019-04-24 14:44:46 -0700165svm_msg_q_t *segment_manager_event_queue (segment_manager_t * sm);
166u32 segment_manager_evt_q_expected_size (u32 q_size);
167
Florin Corasbf395972020-04-30 15:05:24 +0000168u8 segment_manager_app_detached (segment_manager_t * sm);
Florin Coras4e4531e2017-11-06 23:27:56 -0800169void segment_manager_app_detach (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700170
Florin Coras88001c62019-04-24 14:44:46 -0700171/**
172 * Cleanup segment manager sessions
173 *
174 * Initiates disconnects for all sessions 'owned' by a segment manager by
175 * leveraging the backpointers that fifos keep.
176 *
177 * @param sm segment manager whose sessions are to be disconnected
178 */
179void segment_manager_del_sessions (segment_manager_t * sm);
liuyacan87d48ad2021-04-28 11:34:03 +0000180void segment_manager_del_sessions_filter (segment_manager_t *sm,
181 session_state_t *states);
Florin Coras88001c62019-04-24 14:44:46 -0700182void segment_manager_format_sessions (segment_manager_t * sm, int verbose);
183
Florin Coras9a45bd82020-12-28 16:28:07 -0800184void segment_manager_main_init (void);
Florin Coras88001c62019-04-24 14:44:46 -0700185
186segment_manager_props_t *segment_manager_props_init (segment_manager_props_t *
187 sm);
Florin Corasad0c77f2017-11-09 18:00:15 -0800188
Florin Coras9845c202020-04-28 01:54:22 +0000189static inline void
190segment_manager_parse_segment_handle (u64 segment_handle, u32 * sm_index,
191 u32 * segment_index)
192{
193 *sm_index = segment_handle >> 32;
194 *segment_index = segment_handle & 0xFFFFFFFF;
195}
196
Florin Coras6cf30ad2017-04-04 23:08:23 -0700197#endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */
198/*
199 * fd.io coding-style-patch-verification: ON
200 *
201 * Local Variables:
202 * eval: (c-set-style "gnu")
203 * End:
204 */