blob: 52f89eef1717fce5af8d2e570e471b6c87d4a94f [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>
Florin Corasa5464812017-04-19 13:00:05 -070022
Florin Coras88001c62019-04-24 14:44:46 -070023typedef struct _segment_manager_props
Florin Coras6cf30ad2017-04-04 23:08:23 -070024{
Florin Coras15531972018-08-12 23:50:53 -070025 u32 rx_fifo_size; /**< receive fifo size */
26 u32 tx_fifo_size; /**< transmit fifo size */
27 u32 evt_q_size; /**< event queue length */
Florin Coras15531972018-08-12 23:50:53 -070028 u32 prealloc_fifos; /**< preallocated fifo pairs */
Florin Corasef4f3e72019-12-11 14:27:53 -080029 uword segment_size; /**< first segment size */
30 uword add_segment_size; /**< additional segment size */
Florin Coras15531972018-08-12 23:50:53 -070031 u8 add_segment:1; /**< can add new segments flag */
32 u8 use_mq_eventfd:1; /**< use eventfds for mqs flag */
33 u8 reserved:6; /**< reserved flags */
Florin Coras62ddc032019-12-08 18:30:42 -080034 u8 n_slices; /**< number of fs slices/threads */
Florin Coras15531972018-08-12 23:50:53 -070035 ssvm_segment_type_t segment_type; /**< seg type: if set to SSVM_N_TYPES,
36 private segments are used */
Florin Coras88001c62019-04-24 14:44:46 -070037} segment_manager_props_t;
Florin Coras6cf30ad2017-04-04 23:08:23 -070038
39typedef struct _segment_manager
40{
Florin Corasa332c462018-01-31 06:52:17 -080041 /** Pool of segments allocated by this manager */
Florin Coras88001c62019-04-24 14:44:46 -070042 fifo_segment_t *segments;
Florin Corasa5464812017-04-19 13:00:05 -070043
Florin Corasa332c462018-01-31 06:52:17 -080044 /** rwlock that protects the segments pool */
45 clib_rwlock_t segments_rwlock;
Florin Coras6cf30ad2017-04-04 23:08:23 -070046
Florin Coras15531972018-08-12 23:50:53 -070047 /** Owner app worker index */
48 u32 app_wrk_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -070049
Florin Corasc87c91d2017-08-16 19:55:49 -070050 /**
Florin Corasc87c91d2017-08-16 19:55:49 -070051 * First segment should not be deleted unless segment manger is deleted.
52 * This also indicates that the segment manager is the first to have been
53 * allocated for the app.
54 */
55 u8 first_is_protected;
Florin Corasa332c462018-01-31 06:52:17 -080056
57 /**
58 * App event queue allocated in first segment
59 */
Florin Coras3c2fed52018-07-04 04:15:05 -070060 svm_msg_q_t *event_queue;
Florin Coras6cf30ad2017-04-04 23:08:23 -070061} segment_manager_t;
62
Florin Corasa332c462018-01-31 06:52:17 -080063typedef struct segment_manager_main_init_args_
64{
65 u64 baseva;
66 u64 size;
67} segment_manager_main_init_args_t;
68
Florin Corasc87c91d2017-08-16 19:55:49 -070069#define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0)
70
Florin Coras88001c62019-04-24 14:44:46 -070071segment_manager_t *segment_manager_alloc (void);
Florin Corasef4f3e72019-12-11 14:27:53 -080072int segment_manager_init (segment_manager_t * sm, uword first_seg_size,
Florin Corasf8f516a2018-02-08 15:10:09 -080073 u32 prealloc_fifo_pairs);
Florin Coras6cf30ad2017-04-04 23:08:23 -070074
Florin Coras88001c62019-04-24 14:44:46 -070075/**
76 * Cleanup segment manager
77 *
78 * @param sm segment manager to be freed
79 */
80void segment_manager_free (segment_manager_t * sm);
81
82/**
83 * Initiate segment manager cleanup
84 *
85 * @param sm segment manager to be freed
86 */
87void segment_manager_init_free (segment_manager_t * sm);
88segment_manager_t *segment_manager_get (u32 index);
89segment_manager_t *segment_manager_get_if_valid (u32 index);
90u32 segment_manager_index (segment_manager_t * sm);
91
Florin Corasef4f3e72019-12-11 14:27:53 -080092int segment_manager_add_segment (segment_manager_t * sm, uword segment_size);
Florin Corasf8f516a2018-02-08 15:10:09 -080093void segment_manager_del_segment (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -070094 fifo_segment_t * fs);
95fifo_segment_t *segment_manager_get_segment (segment_manager_t * sm,
96 u32 segment_index);
97fifo_segment_t *segment_manager_get_segment_w_handle (u64 sh);
Florin Corasb095a3c2019-04-25 12:58:46 -070098fifo_segment_t *segment_manager_get_segment_w_lock (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -070099 u32 segment_index);
100int segment_manager_add_first_segment (segment_manager_t * sm,
101 u32 segment_size);
102u64 segment_manager_make_segment_handle (u32 segment_manager_index,
103 u32 segment_index);
104u64 segment_manager_segment_handle (segment_manager_t * sm,
105 fifo_segment_t * segment);
Florin Corasa332c462018-01-31 06:52:17 -0800106void segment_manager_segment_reader_unlock (segment_manager_t * sm);
107void segment_manager_segment_writer_unlock (segment_manager_t * sm);
108
Florin Corasf8f516a2018-02-08 15:10:09 -0800109int segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Coras62ddc032019-12-08 18:30:42 -0800110 u32 thread_index,
Florin Coras88001c62019-04-24 14:44:46 -0700111 svm_fifo_t ** rx_fifo,
112 svm_fifo_t ** tx_fifo);
113int segment_manager_try_alloc_fifos (fifo_segment_t * fs,
Florin Coras62ddc032019-12-08 18:30:42 -0800114 u32 thread_index,
Florin Corasf8f516a2018-02-08 15:10:09 -0800115 u32 rx_fifo_size, u32 tx_fifo_size,
116 svm_fifo_t ** rx_fifo,
117 svm_fifo_t ** tx_fifo);
Florin Coras19223e02019-03-03 14:56:05 -0800118void segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo,
Florin Corasf8f516a2018-02-08 15:10:09 -0800119 svm_fifo_t * tx_fifo);
Florin Corasb095a3c2019-04-25 12:58:46 -0700120
Florin Coras88001c62019-04-24 14:44:46 -0700121u8 segment_manager_has_fifos (segment_manager_t * sm);
122
123svm_msg_q_t *segment_manager_alloc_queue (fifo_segment_t * fs,
124 segment_manager_props_t * props);
Florin Corase86a8ed2018-01-05 03:20:25 -0800125void segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q);
Florin Coras88001c62019-04-24 14:44:46 -0700126svm_msg_q_t *segment_manager_event_queue (segment_manager_t * sm);
127u32 segment_manager_evt_q_expected_size (u32 q_size);
128
Florin Coras4e4531e2017-11-06 23:27:56 -0800129void segment_manager_app_detach (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700130
Florin Coras88001c62019-04-24 14:44:46 -0700131/**
132 * Cleanup segment manager sessions
133 *
134 * Initiates disconnects for all sessions 'owned' by a segment manager by
135 * leveraging the backpointers that fifos keep.
136 *
137 * @param sm segment manager whose sessions are to be disconnected
138 */
139void segment_manager_del_sessions (segment_manager_t * sm);
140void segment_manager_format_sessions (segment_manager_t * sm, int verbose);
141
Florin Corasa332c462018-01-31 06:52:17 -0800142void segment_manager_main_init (segment_manager_main_init_args_t * a);
Florin Coras88001c62019-04-24 14:44:46 -0700143
144segment_manager_props_t *segment_manager_props_init (segment_manager_props_t *
145 sm);
Florin Corasad0c77f2017-11-09 18:00:15 -0800146
Florin Coras6cf30ad2017-04-04 23:08:23 -0700147#endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */
148/*
149 * fd.io coding-style-patch-verification: ON
150 *
151 * Local Variables:
152 * eval: (c-set-style "gnu")
153 * End:
154 */