blob: 62e5e97e70334d64a95c820f19c741d9622c6706 [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;
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 */
64 svm_queue_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
117always_inline svm_queue_t *
118segment_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);
155svm_queue_t *segment_manager_alloc_queue (svm_fifo_segment_private_t * fs,
Florin Corasb384b542018-01-15 01:08:33 -0800156 u32 queue_size);
Florin Corase86a8ed2018-01-05 03:20:25 -0800157void segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q);
Florin Coras4e4531e2017-11-06 23:27:56 -0800158void segment_manager_app_detach (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700159
Florin Corasa332c462018-01-31 06:52:17 -0800160void segment_manager_main_init (segment_manager_main_init_args_t * a);
161segment_manager_properties_t
162 * segment_manager_properties_init (segment_manager_properties_t * sm);
Florin Corasad0c77f2017-11-09 18:00:15 -0800163
Florin Coras6cf30ad2017-04-04 23:08:23 -0700164#endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */
165/*
166 * fd.io coding-style-patch-verification: ON
167 *
168 * Local Variables:
169 * eval: (c-set-style "gnu")
170 * End:
171 */