blob: cbf8e3535224d26b884cad21392f6ed4ca2887ae [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 */
28 u32 segment_size; /**< first segment size */
29 u32 prealloc_fifos; /**< preallocated fifo pairs */
30 u32 add_segment_size; /**< additional segment size */
31 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 */
34 ssvm_segment_type_t segment_type; /**< seg type: if set to SSVM_N_TYPES,
35 private segments are used */
Florin Coras88001c62019-04-24 14:44:46 -070036} segment_manager_props_t;
Florin Coras6cf30ad2017-04-04 23:08:23 -070037
38typedef struct _segment_manager
39{
Florin Corasa332c462018-01-31 06:52:17 -080040 /** Pool of segments allocated by this manager */
Florin Coras88001c62019-04-24 14:44:46 -070041 fifo_segment_t *segments;
Florin Corasa5464812017-04-19 13:00:05 -070042
Florin Corasa332c462018-01-31 06:52:17 -080043 /** rwlock that protects the segments pool */
44 clib_rwlock_t segments_rwlock;
Florin Coras6cf30ad2017-04-04 23:08:23 -070045
Florin Coras15531972018-08-12 23:50:53 -070046 /** Owner app worker index */
47 u32 app_wrk_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -070048
Florin Corasc87c91d2017-08-16 19:55:49 -070049 /**
Florin Corasc87c91d2017-08-16 19:55:49 -070050 * First segment should not be deleted unless segment manger is deleted.
51 * This also indicates that the segment manager is the first to have been
52 * allocated for the app.
53 */
54 u8 first_is_protected;
Florin Corasa332c462018-01-31 06:52:17 -080055
56 /**
57 * App event queue allocated in first segment
58 */
Florin Coras3c2fed52018-07-04 04:15:05 -070059 svm_msg_q_t *event_queue;
Florin Coras6cf30ad2017-04-04 23:08:23 -070060} segment_manager_t;
61
Florin Corasa332c462018-01-31 06:52:17 -080062typedef struct segment_manager_main_init_args_
63{
64 u64 baseva;
65 u64 size;
66} segment_manager_main_init_args_t;
67
Florin Corasc87c91d2017-08-16 19:55:49 -070068#define SEGMENT_MANAGER_INVALID_APP_INDEX ((u32) ~0)
69
Florin Coras88001c62019-04-24 14:44:46 -070070segment_manager_t *segment_manager_alloc (void);
Florin Corasa332c462018-01-31 06:52:17 -080071int segment_manager_init (segment_manager_t * sm, u32 first_seg_size,
Florin Corasf8f516a2018-02-08 15:10:09 -080072 u32 prealloc_fifo_pairs);
Florin Coras6cf30ad2017-04-04 23:08:23 -070073
Florin Coras88001c62019-04-24 14:44:46 -070074/**
75 * Cleanup segment manager
76 *
77 * @param sm segment manager to be freed
78 */
79void segment_manager_free (segment_manager_t * sm);
80
81/**
82 * Initiate segment manager cleanup
83 *
84 * @param sm segment manager to be freed
85 */
86void segment_manager_init_free (segment_manager_t * sm);
87segment_manager_t *segment_manager_get (u32 index);
88segment_manager_t *segment_manager_get_if_valid (u32 index);
89u32 segment_manager_index (segment_manager_t * sm);
90
Florin Corasf8f516a2018-02-08 15:10:09 -080091int segment_manager_add_segment (segment_manager_t * sm, u32 segment_size);
92void segment_manager_del_segment (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -070093 fifo_segment_t * fs);
94fifo_segment_t *segment_manager_get_segment (segment_manager_t * sm,
95 u32 segment_index);
96fifo_segment_t *segment_manager_get_segment_w_handle (u64 sh);
Florin Corasb095a3c2019-04-25 12:58:46 -070097fifo_segment_t *segment_manager_get_segment_w_lock (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -070098 u32 segment_index);
99int segment_manager_add_first_segment (segment_manager_t * sm,
100 u32 segment_size);
101u64 segment_manager_make_segment_handle (u32 segment_manager_index,
102 u32 segment_index);
103u64 segment_manager_segment_handle (segment_manager_t * sm,
104 fifo_segment_t * segment);
Florin Corasa332c462018-01-31 06:52:17 -0800105void segment_manager_segment_reader_unlock (segment_manager_t * sm);
106void segment_manager_segment_writer_unlock (segment_manager_t * sm);
107
Florin Corasf8f516a2018-02-08 15:10:09 -0800108int segment_manager_alloc_session_fifos (segment_manager_t * sm,
Florin Coras88001c62019-04-24 14:44:46 -0700109 svm_fifo_t ** rx_fifo,
110 svm_fifo_t ** tx_fifo);
111int segment_manager_try_alloc_fifos (fifo_segment_t * fs,
Florin Corasf8f516a2018-02-08 15:10:09 -0800112 u32 rx_fifo_size, u32 tx_fifo_size,
113 svm_fifo_t ** rx_fifo,
114 svm_fifo_t ** tx_fifo);
Florin Coras19223e02019-03-03 14:56:05 -0800115void segment_manager_dealloc_fifos (svm_fifo_t * rx_fifo,
Florin Corasf8f516a2018-02-08 15:10:09 -0800116 svm_fifo_t * tx_fifo);
Florin Corasb095a3c2019-04-25 12:58:46 -0700117
118/**
119 * Grows fifo owned by segment manager
120 *
121 * @param sm segment manager that owns the fifo
122 * @param f fifo to be grown
123 * @param size amount of bytes to add to fifo
124 * @return 0 on success, negative number otherwise
125 */
126int segment_manager_grow_fifo (segment_manager_t * sm, svm_fifo_t * f,
127 u32 size);
Florin Coras88001c62019-04-24 14:44:46 -0700128u8 segment_manager_has_fifos (segment_manager_t * sm);
129
130svm_msg_q_t *segment_manager_alloc_queue (fifo_segment_t * fs,
131 segment_manager_props_t * props);
Florin Corase86a8ed2018-01-05 03:20:25 -0800132void segment_manager_dealloc_queue (segment_manager_t * sm, svm_queue_t * q);
Florin Coras88001c62019-04-24 14:44:46 -0700133svm_msg_q_t *segment_manager_event_queue (segment_manager_t * sm);
134u32 segment_manager_evt_q_expected_size (u32 q_size);
135
Florin Coras4e4531e2017-11-06 23:27:56 -0800136void segment_manager_app_detach (segment_manager_t * sm);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700137
Florin Coras88001c62019-04-24 14:44:46 -0700138/**
139 * Cleanup segment manager sessions
140 *
141 * Initiates disconnects for all sessions 'owned' by a segment manager by
142 * leveraging the backpointers that fifos keep.
143 *
144 * @param sm segment manager whose sessions are to be disconnected
145 */
146void segment_manager_del_sessions (segment_manager_t * sm);
147void segment_manager_format_sessions (segment_manager_t * sm, int verbose);
148
Florin Corasa332c462018-01-31 06:52:17 -0800149void segment_manager_main_init (segment_manager_main_init_args_t * a);
Florin Coras88001c62019-04-24 14:44:46 -0700150
151segment_manager_props_t *segment_manager_props_init (segment_manager_props_t *
152 sm);
Florin Corasad0c77f2017-11-09 18:00:15 -0800153
Florin Coras6cf30ad2017-04-04 23:08:23 -0700154#endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */
155/*
156 * fd.io coding-style-patch-verification: ON
157 *
158 * Local Variables:
159 * eval: (c-set-style "gnu")
160 * End:
161 */