blob: 13b1fffa1e22d63a3e945610f3c43e60f19ae0dc [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 Corasef4f3e72019-12-11 14:27:53 -080030 uword segment_size; /**< first segment size */
31 uword add_segment_size; /**< additional segment size */
Florin Coras15531972018-08-12 23:50:53 -070032 u8 add_segment:1; /**< can add new segments flag */
33 u8 use_mq_eventfd:1; /**< use eventfds for mqs flag */
34 u8 reserved:6; /**< reserved flags */
Florin Coras62ddc032019-12-08 18:30:42 -080035 u8 n_slices; /**< number of fs slices/threads */
Florin Coras15531972018-08-12 23:50:53 -070036 ssvm_segment_type_t segment_type; /**< seg type: if set to SSVM_N_TYPES,
37 private segments are used */
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +000038 u32 max_fifo_size; /**< max fifo size */
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000039 u8 high_watermark; /**< memory usage high watermark % */
40 u8 low_watermark; /**< memory usage low watermark % */
Florin Coras2de9c0f2020-02-02 19:30:39 +000041 u8 pct_first_alloc; /**< pct of fifo size to alloc */
Florin Coras88001c62019-04-24 14:44:46 -070042} segment_manager_props_t;
Florin Coras6cf30ad2017-04-04 23:08:23 -070043
44typedef struct _segment_manager
45{
Florin Corasa332c462018-01-31 06:52:17 -080046 /** Pool of segments allocated by this manager */
Florin Coras88001c62019-04-24 14:44:46 -070047 fifo_segment_t *segments;
Florin Corasa5464812017-04-19 13:00:05 -070048
Florin Corasa332c462018-01-31 06:52:17 -080049 /** rwlock that protects the segments pool */
50 clib_rwlock_t segments_rwlock;
Florin Coras6cf30ad2017-04-04 23:08:23 -070051
Florin Coras15531972018-08-12 23:50:53 -070052 /** Owner app worker index */
53 u32 app_wrk_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -070054
Florin Corasc87c91d2017-08-16 19:55:49 -070055 /**
Florin Corasc87c91d2017-08-16 19:55:49 -070056 * First segment should not be deleted unless segment manger is deleted.
57 * This also indicates that the segment manager is the first to have been
58 * allocated for the app.
59 */
60 u8 first_is_protected;
Florin Corasa332c462018-01-31 06:52:17 -080061
62 /**
63 * App event queue allocated in first segment
64 */
Florin Coras3c2fed52018-07-04 04:15:05 -070065 svm_msg_q_t *event_queue;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000066
Ryujiro Shibuyad8f48e22020-01-22 12:11:42 +000067 u32 max_fifo_size;
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000068 u8 high_watermark;
69 u8 low_watermark;
Florin Coras6cf30ad2017-04-04 23:08:23 -070070} segment_manager_t;
71
Florin Corasa332c462018-01-31 06:52:17 -080072typedef struct segment_manager_main_init_args_
73{
74 u64 baseva;
75 u64 size;
76} segment_manager_main_init_args_t;
77
Florin Corasc87c91d2017-08-16 19:55:49 -070078#define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0)
79
Florin Coras88001c62019-04-24 14:44:46 -070080segment_manager_t *segment_manager_alloc (void);
Ryujiro Shibuya234fe892019-12-25 07:40:54 +000081int segment_manager_init (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -070082
Florin Coras88001c62019-04-24 14:44:46 -070083/**
84 * Cleanup segment manager
85 *
86 * @param sm segment manager to be freed
87 */
88void segment_manager_free (segment_manager_t * sm);
89
90/**
91 * Initiate segment manager cleanup
92 *
93 * @param sm segment manager to be freed
94 */
95void segment_manager_init_free (segment_manager_t * sm);
96segment_manager_t *segment_manager_get (u32 index);
97segment_manager_t *segment_manager_get_if_valid (u32 index);
98u32 segment_manager_index (segment_manager_t * sm);
99
Florin Corasef4f3e72019-12-11 14:27:53 -0800100int segment_manager_add_segment (segment_manager_t * sm, uword segment_size);
Florin Corasf8f516a2018-02-08 15:10:09 -0800101void segment_manager_del_segment (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700102 fifo_segment_t * fs);
103fifo_segment_t *segment_manager_get_segment (segment_manager_t * sm,
104 u32 segment_index);
105fifo_segment_t *segment_manager_get_segment_w_handle (u64 sh);
Florin Corasb095a3c2019-04-25 12:58:46 -0700106fifo_segment_t *segment_manager_get_segment_w_lock (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700107 u32 segment_index);
108int segment_manager_add_first_segment (segment_manager_t * sm,
109 u32 segment_size);
110u64 segment_manager_make_segment_handle (u32 segment_manager_index,
111 u32 segment_index);
112u64 segment_manager_segment_handle (segment_manager_t * sm,
113 fifo_segment_t * segment);
Florin Corasa332c462018-01-31 06:52:17 -0800114void segment_manager_segment_reader_unlock (segment_manager_t * sm);
115void segment_manager_segment_writer_unlock (segment_manager_t * sm);
116
Florin Corasf8f516a2018-02-08 15:10:09 -0800117int segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Coras62ddc032019-12-08 18:30:42 -0800118 u32 thread_index,
Florin Coras88001c62019-04-24 14:44:46 -0700119 svm_fifo_t ** rx_fifo,
120 svm_fifo_t ** tx_fifo);
121int segment_manager_try_alloc_fifos (fifo_segment_t * fs,
Florin Coras62ddc032019-12-08 18:30:42 -0800122 u32 thread_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800123 u32 rx_fifo_size, u32 tx_fifo_size,
124 svm_fifo_t ** rx_fifo,
125 svm_fifo_t ** tx_fifo);
Florin Coras19223e02019-03-03 14:56:05 -0800126void segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo,
Florin Corasf8f516a2018-02-08 15:10:09 -0800127 svm_fifo_t * tx_fifo);
Florin Coras6d7552c2020-04-09 01:49:45 +0000128void segment_manager_detach_fifo (segment_manager_t * sm, svm_fifo_t * f);
129void segment_manager_attach_fifo (segment_manager_t * sm, svm_fifo_t * f,
130 session_t * s);
Florin Corasb095a3c2019-04-25 12:58:46 -0700131
Ryujiro Shibuya234fe892019-12-25 07:40:54 +0000132void segment_manager_set_watermarks (segment_manager_t * sm,
133 u8 high_watermark, u8 low_watermark);
134
Florin Coras88001c62019-04-24 14:44:46 -0700135u8 segment_manager_has_fifos (segment_manager_t * sm);
136
137svm_msg_q_t *segment_manager_alloc_queue (fifo_segment_t * fs,
138 segment_manager_props_t * props);
Florin Corase86a8ed2018-01-05 03:20:25 -0800139void segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q);
Florin Coras88001c62019-04-24 14:44:46 -0700140svm_msg_q_t *segment_manager_event_queue (segment_manager_t * sm);
141u32 segment_manager_evt_q_expected_size (u32 q_size);
142
Florin Coras4e4531e2017-11-06 23:27:56 -0800143void segment_manager_app_detach (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700144
Florin Coras88001c62019-04-24 14:44:46 -0700145/**
146 * Cleanup segment manager sessions
147 *
148 * Initiates disconnects for all sessions 'owned' by a segment manager by
149 * leveraging the backpointers that fifos keep.
150 *
151 * @param sm segment manager whose sessions are to be disconnected
152 */
153void segment_manager_del_sessions (segment_manager_t * sm);
154void segment_manager_format_sessions (segment_manager_t * sm, int verbose);
155
Florin Corasa332c462018-01-31 06:52:17 -0800156void segment_manager_main_init (segment_manager_main_init_args_t * a);
Florin Coras88001c62019-04-24 14:44:46 -0700157
158segment_manager_props_t *segment_manager_props_init (segment_manager_props_t *
159 sm);
Florin Corasad0c77f2017-11-09 18:00:15 -0800160
Florin Coras6cf30ad2017-04-04 23:08:23 -0700161#endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */
162/*
163 * fd.io coding-style-patch-verification: ON
164 *
165 * Local Variables:
166 * eval: (c-set-style "gnu")
167 * End:
168 */