blob: 15fd067053f521eaed1de8a3f91e29f9dd7ba3c2 [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
18#include <vnet/vnet.h>
19#include <svm/svm_fifo_segment.h>
Florin Coras3c2fed52018-07-04 04:15:05 -070020#include <svm/message_queue.h>
Florin Corasa5464812017-04-19 13:00:05 -070021#include <vlibmemory/api.h>
22#include <vppinfra/lock.h>
Florin Corasa332c462018-01-31 06:52:17 -080023#include <vppinfra/valloc.h>
Florin Corasa5464812017-04-19 13:00:05 -070024
Florin Coras6cf30ad2017-04-04 23:08:23 -070025typedef struct _segment_manager_properties
26{
Florin Coras15531972018-08-12 23:50:53 -070027 u32 rx_fifo_size; /**< receive fifo size */
28 u32 tx_fifo_size; /**< transmit fifo size */
29 u32 evt_q_size; /**< event queue length */
30 u32 segment_size; /**< first segment size */
31 u32 prealloc_fifos; /**< preallocated fifo pairs */
32 u32 add_segment_size; /**< additional segment size */
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 */
36 ssvm_segment_type_t segment_type; /**< seg type: if set to SSVM_N_TYPES,
37 private segments are used */
Florin Coras6cf30ad2017-04-04 23:08:23 -070038} segment_manager_properties_t;
39
40typedef struct _segment_manager
41{
Florin Corasa332c462018-01-31 06:52:17 -080042 /** Pool of segments allocated by this manager */
43 svm_fifo_segment_private_t *segments;
Florin Corasa5464812017-04-19 13:00:05 -070044
Florin Corasa332c462018-01-31 06:52:17 -080045 /** rwlock that protects the segments pool */
46 clib_rwlock_t segments_rwlock;
Florin Coras6cf30ad2017-04-04 23:08:23 -070047
Florin Coras15531972018-08-12 23:50:53 -070048 /** Owner app worker index */
49 u32 app_wrk_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -070050
Florin Corasc87c91d2017-08-16 19:55:49 -070051 /**
Florin Corasc87c91d2017-08-16 19:55:49 -070052 * First segment should not be deleted unless segment manger is deleted.
53 * This also indicates that the segment manager is the first to have been
54 * allocated for the app.
55 */
56 u8 first_is_protected;
Florin Corasa332c462018-01-31 06:52:17 -080057
58 /**
59 * App event queue allocated in first segment
60 */
Florin Coras3c2fed52018-07-04 04:15:05 -070061 svm_msg_q_t *event_queue;
Florin Coras6cf30ad2017-04-04 23:08:23 -070062} segment_manager_t;
63
Florin Corasa332c462018-01-31 06:52:17 -080064#define segment_manager_foreach_segment_w_lock(VAR, SM, BODY) \
65do { \
66 clib_rwlock_reader_lock (&(SM)->segments_rwlock); \
67 pool_foreach((VAR), ((SM)->segments), (BODY)); \
68 clib_rwlock_reader_unlock (&(SM)->segments_rwlock); \
69} while (0)
70
71typedef struct segment_manager_main_
72{
73 /** Pool of segment managers */
74 segment_manager_t *segment_managers;
75
76 /** Virtual address allocator */
77 clib_valloc_main_t va_allocator;
78
79} segment_manager_main_t;
80
81extern segment_manager_main_t segment_manager_main;
82
83typedef struct segment_manager_main_init_args_
84{
85 u64 baseva;
86 u64 size;
87} segment_manager_main_init_args_t;
88
Florin Corasc87c91d2017-08-16 19:55:49 -070089#define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0)
90
Florin Coras6cf30ad2017-04-04 23:08:23 -070091/** Pool of segment managers */
92extern segment_manager_t *segment_managers;
93
94always_inline segment_manager_t *
Florin Coras6cf30ad2017-04-04 23:08:23 -070095segment_manager_get (u32 index)
96{
Florin Corasa332c462018-01-31 06:52:17 -080097 return pool_elt_at_index (segment_manager_main.segment_managers, index);
Florin Coras6cf30ad2017-04-04 23:08:23 -070098}
99
100always_inline segment_manager_t *
101segment_manager_get_if_valid (u32 index)
102{
Florin Corasa332c462018-01-31 06:52:17 -0800103 if (pool_is_free_index (segment_manager_main.segment_managers, index))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700104 return 0;
Florin Corasa332c462018-01-31 06:52:17 -0800105 return pool_elt_at_index (segment_manager_main.segment_managers, index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700106}
107
108always_inline u32
109segment_manager_index (segment_manager_t * sm)
110{
Florin Corasa332c462018-01-31 06:52:17 -0800111 return sm - segment_manager_main.segment_managers;
112}
113
Florin Coras3c2fed52018-07-04 04:15:05 -0700114always_inline svm_msg_q_t *
Florin Corasa332c462018-01-31 06:52:17 -0800115segment_manager_event_queue (segment_manager_t * sm)
116{
117 return sm->event_queue;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700118}
119
Florin Corasfa76a762018-11-29 12:40:10 -0800120always_inline u64
121segment_manager_make_segment_handle (u32 segment_manager_index,
122 u32 segment_index)
123{
124 return (((u64) segment_manager_index << 32) | segment_index);
125}
126
127u64 segment_manager_segment_handle (segment_manager_t * sm,
128 svm_fifo_segment_private_t * segment);
129
Florin Corasc87c91d2017-08-16 19:55:49 -0700130segment_manager_t *segment_manager_new ();
Florin Corasa332c462018-01-31 06:52:17 -0800131int segment_manager_init (segment_manager_t * sm, u32 first_seg_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800132 u32 prealloc_fifo_pairs);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700133
Florin Corasfa76a762018-11-29 12:40:10 -0800134svm_fifo_segment_private_t *segment_manager_get_segment (segment_manager_t *,
Florin Corasa332c462018-01-31 06:52:17 -0800135 u32 segment_index);
Florin Corasfa76a762018-11-29 12:40:10 -0800136svm_fifo_segment_private_t *segment_manager_get_segment_w_handle (u64);
Florin Corasa332c462018-01-31 06:52:17 -0800137svm_fifo_segment_private_t
138 * segment_manager_get_segment_w_lock (segment_manager_t * sm,
139 u32 segment_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800140int segment_manager_add_segment (segment_manager_t * sm, u32 segment_size);
141void segment_manager_del_segment (segment_manager_t * sm,
142 svm_fifo_segment_private_t * fs);
Florin Corasa332c462018-01-31 06:52:17 -0800143void segment_manager_segment_reader_unlock (segment_manager_t * sm);
144void segment_manager_segment_writer_unlock (segment_manager_t * sm);
145
Florin Corasb384b542018-01-15 01:08:33 -0800146int segment_manager_add_first_segment (segment_manager_t * sm,
147 u32 segment_size);
Florin Corasc87c91d2017-08-16 19:55:49 -0700148void segment_manager_del_sessions (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700149void segment_manager_del (segment_manager_t * sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700150void segment_manager_init_del (segment_manager_t * sm);
151u8 segment_manager_has_fifos (segment_manager_t * sm);
Florin Corasf8f516a2018-02-08 15:10:09 -0800152int segment_manager_alloc_session_fifos (segment_manager_t * sm,
153 svm_fifo_t ** server_rx_fifo,
154 svm_fifo_t ** server_tx_fifo,
155 u32 * fifo_segment_index);
156int segment_manager_try_alloc_fifos (svm_fifo_segment_private_t * fs,
157 u32 rx_fifo_size, u32 tx_fifo_size,
158 svm_fifo_t ** rx_fifo,
159 svm_fifo_t ** tx_fifo);
160void segment_manager_dealloc_fifos (u32 segment_index, svm_fifo_t * rx_fifo,
161 svm_fifo_t * tx_fifo);
Florin Coras3c2fed52018-07-04 04:15:05 -0700162u32 segment_manager_evt_q_expected_size (u32 q_size);
163svm_msg_q_t *segment_manager_alloc_queue (svm_fifo_segment_private_t * fs,
Florin Coras99368312018-08-02 10:45:44 -0700164 segment_manager_properties_t *
165 props);
Florin Corase86a8ed2018-01-05 03:20:25 -0800166void segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q);
Florin Coras4e4531e2017-11-06 23:27:56 -0800167void segment_manager_app_detach (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700168
Florin Corasa332c462018-01-31 06:52:17 -0800169void segment_manager_main_init (segment_manager_main_init_args_t * a);
170segment_manager_properties_t
171 * segment_manager_properties_init (segment_manager_properties_t * sm);
Florin Corasad0c77f2017-11-09 18:00:15 -0800172
Florin Coras6cf30ad2017-04-04 23:08:23 -0700173#endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */
174/*
175 * fd.io coding-style-patch-verification: ON
176 *
177 * Local Variables:
178 * eval: (c-set-style "gnu")
179 * End:
180 */