blob: eef2168c47759e9c86f5a5850688678623f44cd2 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 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
16#include <svm/svm_fifo_segment.h>
17
18svm_fifo_segment_main_t svm_fifo_segment_main;
19
Dave Barach10d8cc62017-05-30 09:30:07 -040020static void
21preallocate_fifo_pairs (svm_fifo_segment_header_t * fsh,
22 svm_fifo_segment_create_args_t * a)
23{
24 u32 rx_fifo_size, tx_fifo_size;
25 svm_fifo_t *f;
26 u8 *rx_fifo_space, *tx_fifo_space;
27 int i;
28
29 /* Parameter check */
30 if (a->rx_fifo_size == 0 || a->tx_fifo_size == 0
31 || a->preallocated_fifo_pairs == 0)
32 return;
33
34 /* Calculate space requirements */
35 rx_fifo_size = (sizeof (*f) + a->rx_fifo_size) * a->preallocated_fifo_pairs;
36 tx_fifo_size = (sizeof (*f) + a->tx_fifo_size) * a->preallocated_fifo_pairs;
37
38 /* Allocate rx fifo space. May fail. */
39 rx_fifo_space = clib_mem_alloc_aligned_at_offset
40 (rx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
41 0 /* os_out_of_memory */ );
42
43 /* Same for TX */
44 tx_fifo_space = clib_mem_alloc_aligned_at_offset
45 (tx_fifo_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ ,
46 0 /* os_out_of_memory */ );
47
48 /* Make sure it worked. Clean up if it didn't... */
49 if (rx_fifo_space == 0 || tx_fifo_space == 0)
50 {
51 if (rx_fifo_space)
52 clib_mem_free (rx_fifo_space);
53 else
54 clib_warning ("rx fifo preallocation failure: size %d npairs %d",
55 a->rx_fifo_size, a->preallocated_fifo_pairs);
56
57 if (tx_fifo_space)
58 clib_mem_free (tx_fifo_space);
59 else
60 clib_warning ("tx fifo preallocation failure: size %d nfifos %d",
61 a->tx_fifo_size, a->preallocated_fifo_pairs);
62 return;
63 }
64
65 /* Carve rx fifo space */
66 f = (svm_fifo_t *) rx_fifo_space;
67 for (i = 0; i < a->preallocated_fifo_pairs; i++)
68 {
69 f->next = fsh->free_fifos[FIFO_SEGMENT_RX_FREELIST];
70 fsh->free_fifos[FIFO_SEGMENT_RX_FREELIST] = f;
71 rx_fifo_space += sizeof (*f) + a->rx_fifo_size;
72 f = (svm_fifo_t *) rx_fifo_space;
73 }
74 /* Carve tx fifo space */
75 f = (svm_fifo_t *) tx_fifo_space;
76 for (i = 0; i < a->preallocated_fifo_pairs; i++)
77 {
78 f->next = fsh->free_fifos[FIFO_SEGMENT_TX_FREELIST];
79 fsh->free_fifos[FIFO_SEGMENT_TX_FREELIST] = f;
80 tx_fifo_space += sizeof (*f) + a->tx_fifo_size;
81 f = (svm_fifo_t *) tx_fifo_space;
82 }
83}
84
Dave Barach68b0fb02017-02-28 15:15:56 -050085/** (master) create an svm fifo segment */
86int
87svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
88{
89 int rv;
90 svm_fifo_segment_private_t *s;
91 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
92 ssvm_shared_header_t *sh;
93 svm_fifo_segment_header_t *fsh;
94 void *oldheap;
95
96 /* Allocate a fresh segment */
97 pool_get (sm->segments, s);
98 memset (s, 0, sizeof (*s));
99
100 s->ssvm.ssvm_size = a->segment_size;
101 s->ssvm.i_am_master = 1;
102 s->ssvm.my_pid = getpid ();
103 s->ssvm.name = (u8 *) a->segment_name;
104 s->ssvm.requested_va = sm->next_baseva;
105
106 rv = ssvm_master_init (&s->ssvm, s - sm->segments);
107
108 if (rv)
109 {
110 _vec_len (s) = vec_len (s) - 1;
111 return (rv);
112 }
113
114 /* Note; requested_va updated due to seg base addr randomization */
115 sm->next_baseva = s->ssvm.requested_va + a->segment_size;
116
117 sh = s->ssvm.sh;
118 oldheap = ssvm_push_heap (sh);
119
120 /* Set up svm_fifo_segment shared header */
121 fsh = clib_mem_alloc (sizeof (*fsh));
122 memset (fsh, 0, sizeof (*fsh));
123 sh->opaque[0] = fsh;
124 s->h = fsh;
125 fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
126
Dave Barach10d8cc62017-05-30 09:30:07 -0400127 preallocate_fifo_pairs (fsh, a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500128
129 ssvm_pop_heap (oldheap);
130
131 sh->ready = 1;
132 a->new_segment_index = s - sm->segments;
133 return (0);
134}
135
Florin Corasa5464812017-04-19 13:00:05 -0700136/** Create an svm fifo segment in process-private memory */
137int
138svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a)
139{
140 svm_fifo_segment_private_t *s;
141 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
142 ssvm_shared_header_t *sh;
143 svm_fifo_segment_header_t *fsh;
144
145 /* Allocate a fresh segment */
146 pool_get (sm->segments, s);
147 memset (s, 0, sizeof (*s));
148
149 s->ssvm.ssvm_size = ~0;
150 s->ssvm.i_am_master = 1;
151 s->ssvm.my_pid = getpid ();
152 s->ssvm.name = (u8 *) a->segment_name;
153 s->ssvm.requested_va = ~0;
154
155 /* Allocate a [sic] shared memory header, in process memory... */
156 sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
157 s->ssvm.sh = sh;
158
159 memset (sh, 0, sizeof (*sh));
160 sh->heap = clib_mem_get_heap ();
161
162 /* Set up svm_fifo_segment shared header */
163 fsh = clib_mem_alloc (sizeof (*fsh));
164 memset (fsh, 0, sizeof (*fsh));
165 sh->opaque[0] = fsh;
166 s->h = fsh;
167 fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
168
Dave Barach10d8cc62017-05-30 09:30:07 -0400169 preallocate_fifo_pairs (fsh, a);
170
Florin Corasa5464812017-04-19 13:00:05 -0700171 sh->ready = 1;
172 a->new_segment_index = s - sm->segments;
173 return (0);
174}
175
Dave Barach68b0fb02017-02-28 15:15:56 -0500176/** (slave) attach to an svm fifo segment */
177int
178svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a)
179{
180 int rv;
181 svm_fifo_segment_private_t *s;
182 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
183 ssvm_shared_header_t *sh;
184 svm_fifo_segment_header_t *fsh;
185
186 /* Allocate a fresh segment */
187 pool_get (sm->segments, s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500188 memset (s, 0, sizeof (*s));
189
190 s->ssvm.ssvm_size = a->segment_size;
191 s->ssvm.my_pid = getpid ();
192 s->ssvm.name = (u8 *) a->segment_name;
193 s->ssvm.requested_va = sm->next_baseva;
194
195 rv = ssvm_slave_init (&s->ssvm, sm->timeout_in_seconds);
196
197 if (rv)
198 {
199 _vec_len (s) = vec_len (s) - 1;
200 return (rv);
201 }
202
203 /* Fish the segment header */
204 sh = s->ssvm.sh;
205 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
206 s->h = fsh;
207
208 a->new_segment_index = s - sm->segments;
209 return (0);
210}
211
212void
213svm_fifo_segment_delete (svm_fifo_segment_private_t * s)
214{
215 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
216 ssvm_delete (&s->ssvm);
217 pool_put (sm->segments, s);
218}
219
220svm_fifo_t *
221svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s,
Dave Barach10d8cc62017-05-30 09:30:07 -0400222 u32 data_size_in_bytes,
223 svm_fifo_segment_freelist_t list_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500224{
225 ssvm_shared_header_t *sh;
226 svm_fifo_segment_header_t *fsh;
227 svm_fifo_t *f;
228 void *oldheap;
229
230 sh = s->ssvm.sh;
231 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
Florin Corasa5464812017-04-19 13:00:05 -0700232
233 ssvm_lock (sh, 1, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500234 oldheap = ssvm_push_heap (sh);
235
Dave Barach10d8cc62017-05-30 09:30:07 -0400236 switch (list_index)
237 {
238 case FIFO_SEGMENT_RX_FREELIST:
239 case FIFO_SEGMENT_TX_FREELIST:
240 f = fsh->free_fifos[list_index];
241 if (f)
242 {
243 fsh->free_fifos[list_index] = f->next;
244 /* (re)initialize the fifo, as in svm_fifo_create */
245 memset (f, 0, sizeof (*f));
246 f->nitems = data_size_in_bytes;
247 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
248 goto found;
249 }
250 /* FALLTHROUGH */
251 case FIFO_SEGMENT_FREELIST_NONE:
252 break;
253
254 default:
255 clib_warning ("ignore bogus freelist %d", list_index);
256 break;
257 }
258
Dave Barach68b0fb02017-02-28 15:15:56 -0500259 /* Note: this can fail, in which case: create another segment */
260 f = svm_fifo_create (data_size_in_bytes);
Florin Corasa5464812017-04-19 13:00:05 -0700261 if (PREDICT_FALSE (f == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500262 {
263 ssvm_pop_heap (oldheap);
Florin Corasa5464812017-04-19 13:00:05 -0700264 ssvm_unlock (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500265 return (0);
266 }
267
Dave Barach10d8cc62017-05-30 09:30:07 -0400268found:
269 /* If rx_freelist add to active fifos list. When cleaning up segment,
270 * we need a list of active sessions that should be disconnected. Since
271 * both rx and tx fifos keep pointers to the session, it's enough to track
272 * only one. */
273 if (list_index == FIFO_SEGMENT_RX_FREELIST)
274 {
275 if (fsh->fifos)
276 {
277 fsh->fifos->prev = f;
278 f->next = fsh->fifos;
279 }
280 fsh->fifos = f;
281 }
282
Dave Barach68b0fb02017-02-28 15:15:56 -0500283 ssvm_pop_heap (oldheap);
Florin Corasa5464812017-04-19 13:00:05 -0700284 ssvm_unlock (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500285 return (f);
286}
287
288void
Dave Barach10d8cc62017-05-30 09:30:07 -0400289svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f,
290 svm_fifo_segment_freelist_t list_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500291{
292 ssvm_shared_header_t *sh;
293 svm_fifo_segment_header_t *fsh;
294 void *oldheap;
Dave Barach68b0fb02017-02-28 15:15:56 -0500295
296 sh = s->ssvm.sh;
297 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
Dave Barach68b0fb02017-02-28 15:15:56 -0500298
Florin Corasa5464812017-04-19 13:00:05 -0700299 ssvm_lock (sh, 1, 0);
300 oldheap = ssvm_push_heap (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500301
Dave Barach10d8cc62017-05-30 09:30:07 -0400302 switch (list_index)
303 {
304 case FIFO_SEGMENT_RX_FREELIST:
305 /* Remove from active list */
306 if (f->prev)
307 f->prev->next = f->next;
308 if (f->next)
309 f->next->prev = f->prev;
310 /* FALLTHROUGH */
311 case FIFO_SEGMENT_TX_FREELIST:
312 /* Add to free list */
313 f->next = fsh->free_fifos[list_index];
314 fsh->free_fifos[list_index] = f;
315 /* FALLTHROUGH */
316 case FIFO_SEGMENT_FREELIST_NONE:
317 break;
318
319 default:
320 clib_warning ("ignore bogus freelist %d", list_index);
321 break;
322 }
323
Dave Barach68b0fb02017-02-28 15:15:56 -0500324 ssvm_pop_heap (oldheap);
Florin Corasa5464812017-04-19 13:00:05 -0700325 ssvm_unlock (sh);
Dave Barach68b0fb02017-02-28 15:15:56 -0500326}
327
328void
329svm_fifo_segment_init (u64 baseva, u32 timeout_in_seconds)
330{
331 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
332
333 sm->next_baseva = baseva;
334 sm->timeout_in_seconds = timeout_in_seconds;
335}
336
337u32
338svm_fifo_segment_index (svm_fifo_segment_private_t * s)
339{
340 return s - svm_fifo_segment_main.segments;
341}
342
343/*
344 * fd.io coding-style-patch-verification: ON
345 *
346 * Local Variables:
347 * eval: (c-set-style "gnu")
348 * End:
349 */