blob: 73cb4827a8be377d69ca628e489dba72913225c3 [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 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{
27 /** Session fifo sizes. */
28 u32 rx_fifo_size;
29 u32 tx_fifo_size;
Florin Corasf8f516a2018-02-08 15:10:09 -080030 u32 evt_q_size;
Dave Barach10d8cc62017-05-30 09:30:07 -040031
Florin Coras6cf30ad2017-04-04 23:08:23 -070032 /** 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 Corasb384b542018-01-15 01:08:33 -080038 /** Segment type: if set to SSVM_N_TYPES, private segments are used */
39 ssvm_segment_type_t segment_type;
Dave Barach2c25a622017-06-26 11:35:07 -040040
Florin Coras6cf30ad2017-04-04 23:08:23 -070041} segment_manager_properties_t;
42
43typedef struct _segment_manager
44{
Florin Corasa332c462018-01-31 06:52:17 -080045 /** Pool of segments allocated by this manager */
46 svm_fifo_segment_private_t *segments;
Florin Corasa5464812017-04-19 13:00:05 -070047
Florin Corasa332c462018-01-31 06:52:17 -080048 /** rwlock that protects the segments pool */
49 clib_rwlock_t segments_rwlock;
Florin Coras6cf30ad2017-04-04 23:08:23 -070050
51 /** Owner app index */
52 u32 app_index;
53
Florin Corasc87c91d2017-08-16 19:55:49 -070054 /**
Florin Corasc87c91d2017-08-16 19:55:49 -070055 * 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 Corasa332c462018-01-31 06:52:17 -080060
61 /**
62 * App event queue allocated in first segment
63 */
Florin Coras3c2fed52018-07-04 04:15:05 -070064 svm_msg_q_t *event_queue;
Florin Coras6cf30ad2017-04-04 23:08:23 -070065} segment_manager_t;
66
Florin Corasa332c462018-01-31 06:52:17 -080067#define segment_manager_foreach_segment_w_lock(VAR, SM, BODY) \
68do { \
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
74typedef 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
84extern segment_manager_main_t segment_manager_main;
85
86typedef struct segment_manager_main_init_args_
87{
88 u64 baseva;
89 u64 size;
90} segment_manager_main_init_args_t;
91
Florin Corasc87c91d2017-08-16 19:55:49 -070092#define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0)
93
Florin Coras6cf30ad2017-04-04 23:08:23 -070094/** Pool of segment managers */
95extern segment_manager_t *segment_managers;
96
97always_inline segment_manager_t *
Florin Coras6cf30ad2017-04-04 23:08:23 -070098segment_manager_get (u32 index)
99{
Florin Corasa332c462018-01-31 06:52:17 -0800100 return pool_elt_at_index (segment_manager_main.segment_managers, index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700101}
102
103always_inline segment_manager_t *
104segment_manager_get_if_valid (u32 index)
105{
Florin Corasa332c462018-01-31 06:52:17 -0800106 if (pool_is_free_index (segment_manager_main.segment_managers, index))
Florin Coras6cf30ad2017-04-04 23:08:23 -0700107 return 0;
Florin Corasa332c462018-01-31 06:52:17 -0800108 return pool_elt_at_index (segment_manager_main.segment_managers, index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700109}
110
111always_inline u32
112segment_manager_index (segment_manager_t * sm)
113{
Florin Corasa332c462018-01-31 06:52:17 -0800114 return sm - segment_manager_main.segment_managers;
115}
116
Florin Coras3c2fed52018-07-04 04:15:05 -0700117always_inline svm_msg_q_t *
Florin Corasa332c462018-01-31 06:52:17 -0800118segment_manager_event_queue (segment_manager_t * sm)
119{
120 return sm->event_queue;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700121}
122
Florin Corasc87c91d2017-08-16 19:55:49 -0700123segment_manager_t *segment_manager_new ();
Florin Corasa332c462018-01-31 06:52:17 -0800124int segment_manager_init (segment_manager_t * sm, u32 first_seg_size,
Florin Corasf8f516a2018-02-08 15:10:09 -0800125 u32 prealloc_fifo_pairs);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700126
Florin Corasa332c462018-01-31 06:52:17 -0800127svm_fifo_segment_private_t *segment_manager_get_segment (segment_manager_t *
128 sm,
129 u32 segment_index);
130svm_fifo_segment_private_t
131 * segment_manager_get_segment_w_lock (segment_manager_t * sm,
132 u32 segment_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800133int segment_manager_add_segment (segment_manager_t * sm, u32 segment_size);
134void segment_manager_del_segment (segment_manager_t * sm,
135 svm_fifo_segment_private_t * fs);
Florin Corasa332c462018-01-31 06:52:17 -0800136void segment_manager_segment_reader_unlock (segment_manager_t * sm);
137void segment_manager_segment_writer_unlock (segment_manager_t * sm);
138
Florin Corasb384b542018-01-15 01:08:33 -0800139int segment_manager_add_first_segment (segment_manager_t * sm,
140 u32 segment_size);
Florin Corasc87c91d2017-08-16 19:55:49 -0700141void segment_manager_del_sessions (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700142void segment_manager_del (segment_manager_t * sm);
Florin Corasc87c91d2017-08-16 19:55:49 -0700143void segment_manager_init_del (segment_manager_t * sm);
144u8 segment_manager_has_fifos (segment_manager_t * sm);
Florin Corasf8f516a2018-02-08 15:10:09 -0800145int 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);
149int 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);
153void segment_manager_dealloc_fifos (u32 segment_index, svm_fifo_t * rx_fifo,
154 svm_fifo_t * tx_fifo);
Florin Coras3c2fed52018-07-04 04:15:05 -0700155u32 segment_manager_evt_q_expected_size (u32 q_size);
156svm_msg_q_t *segment_manager_alloc_queue (svm_fifo_segment_private_t * fs,
Florin Corasb384b542018-01-15 01:08:33 -0800157 u32 queue_size);
Florin Corase86a8ed2018-01-05 03:20:25 -0800158void segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q);
Florin Coras4e4531e2017-11-06 23:27:56 -0800159void segment_manager_app_detach (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700160
Florin Corasa332c462018-01-31 06:52:17 -0800161void segment_manager_main_init (segment_manager_main_init_args_t * a);
162segment_manager_properties_t
163 * segment_manager_properties_init (segment_manager_properties_t * sm);
Florin Corasad0c77f2017-11-09 18:00:15 -0800164
Florin Coras6cf30ad2017-04-04 23:08:23 -0700165#endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */
166/*
167 * fd.io coding-style-patch-verification: ON
168 *
169 * Local Variables:
170 * eval: (c-set-style "gnu")
171 * End:
172 */