blob: 9b1d4cd0c06a18170b1f47b2468309b3de48d3c1 [file] [log] [blame]
Florin Coras6cf30ad2017-04-04 23:08:23 -07001/*
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 Corase86a8ed2018-01-05 03:20:25 -080020#include <svm/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{
27 /** Session fifo sizes. */
28 u32 rx_fifo_size;
29 u32 tx_fifo_size;
30
Dave Barach10d8cc62017-05-30 09:30:07 -040031 /** Preallocated pool sizes */
Florin Corasa332c462018-01-31 06:52:17 -080032// u32 preallocated_fifo_pairs;
Dave Barach10d8cc62017-05-30 09:30:07 -040033
Florin Coras6cf30ad2017-04-04 23:08:23 -070034 /** Configured additional segment size */
35 u32 add_segment_size;
36
37 /** Flag that indicates if additional segments should be created */
38 u8 add_segment;
39
Florin Corasb384b542018-01-15 01:08:33 -080040 /** Segment type: if set to SSVM_N_TYPES, private segments are used */
41 ssvm_segment_type_t segment_type;
Dave Barach2c25a622017-06-26 11:35:07 -040042
43 /** Use one or more private mheaps, instead of the global heap */
Florin Corasa332c462018-01-31 06:52:17 -080044// u32 private_segment_count;
Florin Coras6cf30ad2017-04-04 23:08:23 -070045} segment_manager_properties_t;
46
47typedef struct _segment_manager
48{
Florin Corasa332c462018-01-31 06:52:17 -080049 /** Pool of segments allocated by this manager */
50 svm_fifo_segment_private_t *segments;
Florin Corasa5464812017-04-19 13:00:05 -070051
Florin Corasa332c462018-01-31 06:52:17 -080052 /** rwlock that protects the segments pool */
53 clib_rwlock_t segments_rwlock;
Florin Coras6cf30ad2017-04-04 23:08:23 -070054
55 /** Owner app index */
56 u32 app_index;
57
Florin Corasc87c91d2017-08-16 19:55:49 -070058 /**
Florin Corasc87c91d2017-08-16 19:55:49 -070059 * First segment should not be deleted unless segment manger is deleted.
60 * This also indicates that the segment manager is the first to have been
61 * allocated for the app.
62 */
63 u8 first_is_protected;
Florin Corasa332c462018-01-31 06:52:17 -080064
65 /**
66 * App event queue allocated in first segment
67 */
68 svm_queue_t *event_queue;
Florin Coras6cf30ad2017-04-04 23:08:23 -070069} segment_manager_t;
70
Florin Corasa332c462018-01-31 06:52:17 -080071#define segment_manager_foreach_segment_w_lock(VAR, SM, BODY) \
72do { \
73 clib_rwlock_reader_lock (&(SM)->segments_rwlock); \
74 pool_foreach((VAR), ((SM)->segments), (BODY)); \
75 clib_rwlock_reader_unlock (&(SM)->segments_rwlock); \
76} while (0)
77
78typedef struct segment_manager_main_
79{
80 /** Pool of segment managers */
81 segment_manager_t *segment_managers;
82
83 /** Virtual address allocator */
84 clib_valloc_main_t va_allocator;
85
86} segment_manager_main_t;
87
88extern segment_manager_main_t segment_manager_main;
89
90typedef struct segment_manager_main_init_args_
91{
92 u64 baseva;
93 u64 size;
94} segment_manager_main_init_args_t;
95
Florin Corasc87c91d2017-08-16 19:55:49 -070096#define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0)
97
Florin Coras6cf30ad2017-04-04 23:08:23 -070098/** Pool of segment managers */
99extern segment_manager_t *segment_managers;
100
101always_inline segment_manager_t *
Florin Coras6cf30ad2017-04-04 23:08:23 -0700102segment_manager_get (u32 index)
103{
Florin Corasa332c462018-01-31 06:52:17 -0800104 return pool_elt_at_index (segment_manager_main.segment_managers, index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700105}
106
107always_inline segment_manager_t *
108segment_manager_get_if_valid (u32 index)
109{
Florin Corasa332c462018-01-31 06:52:17 -0800110 if (pool_is_free_index (segment_manager_main.segment_managers, index))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700111 return 0;
Florin Corasa332c462018-01-31 06:52:17 -0800112 return pool_elt_at_index (segment_manager_main.segment_managers, index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700113}
114
115always_inline u32
116segment_manager_index (segment_manager_t * sm)
117{
Florin Corasa332c462018-01-31 06:52:17 -0800118 return sm - segment_manager_main.segment_managers;
119}
120
121always_inline svm_queue_t *
122segment_manager_event_queue (segment_manager_t * sm)
123{
124 return sm->event_queue;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700125}
126
Florin Corasc87c91d2017-08-16 19:55:49 -0700127segment_manager_t *segment_manager_new ();
Florin Corasa332c462018-01-31 06:52:17 -0800128int segment_manager_init (segment_manager_t * sm, u32 first_seg_size,
129 u32 evt_q_size, u32 prealloc_fifo_pairs);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700130
Florin Corasa332c462018-01-31 06:52:17 -0800131svm_fifo_segment_private_t *segment_manager_get_segment (segment_manager_t *
132 sm,
133 u32 segment_index);
134svm_fifo_segment_private_t
135 * segment_manager_get_segment_w_lock (segment_manager_t * sm,
136 u32 segment_index);
137void segment_manager_segment_reader_unlock (segment_manager_t * sm);
138void segment_manager_segment_writer_unlock (segment_manager_t * sm);
139
Florin Corasb384b542018-01-15 01:08:33 -0800140int segment_manager_add_first_segment (segment_manager_t * sm,
141 u32 segment_size);
Florin Corasc87c91d2017-08-16 19:55:49 -0700142void segment_manager_del_sessions (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700143void segment_manager_del (segment_manager_t * sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700144void segment_manager_init_del (segment_manager_t * sm);
145u8 segment_manager_has_fifos (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700146int
147segment_manager_alloc_session_fifos (segment_manager_t * sm,
148 svm_fifo_t ** server_rx_fifo,
149 svm_fifo_t ** server_tx_fifo,
150 u32 * fifo_segment_index);
151void
152segment_manager_dealloc_fifos (u32 svm_segment_index, svm_fifo_t * rx_fifo,
153 svm_fifo_t * tx_fifo);
Florin Corasb384b542018-01-15 01:08:33 -0800154svm_queue_t *segment_manager_alloc_queue (segment_manager_t * sm,
155 u32 queue_size);
Florin Corase86a8ed2018-01-05 03:20:25 -0800156void segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q);
Florin Coras4e4531e2017-11-06 23:27:56 -0800157void segment_manager_app_detach (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700158
Florin Corasa332c462018-01-31 06:52:17 -0800159void segment_manager_main_init (segment_manager_main_init_args_t * a);
160segment_manager_properties_t
161 * segment_manager_properties_init (segment_manager_properties_t * sm);
Florin Corasad0c77f2017-11-09 18:00:15 -0800162
Florin Coras6cf30ad2017-04-04 23:08:23 -0700163#endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */
164/*
165 * fd.io coding-style-patch-verification: ON
166 *
167 * Local Variables:
168 * eval: (c-set-style "gnu")
169 * End:
170 */