blob: 6ff538fcc07467ed578f88fb9d2313355c65a7df [file] [log] [blame]
Florin Coras88001c62019-04-24 14:44:46 -07001/*
2 * Copyright (c) 2016-2019 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 __included_fifo_segment_h__
16#define __included_fifo_segment_h__
17
18#include <svm/ssvm.h>
19#include <svm/svm_fifo.h>
20
21typedef enum
22{
23 FIFO_SEGMENT_FTYPE_NONE = -1,
24 FIFO_SEGMENT_RX_FIFO = 0,
25 FIFO_SEGMENT_TX_FIFO,
26 FIFO_SEGMENT_N_FTYPES
27} fifo_segment_ftype_t;
28
29#define FIFO_SEGMENT_MIN_FIFO_SIZE 4096 /* 4kB min fifo size */
30#define FIFO_SEGMENT_MAX_FIFO_SIZE (2 << 30) /* 2GB max fifo size */
31#define FIFO_SEGMENT_ALLOC_BATCH_SIZE 32 /* Allocation quantum */
32
33typedef enum fifo_segment_flags_
34{
35 FIFO_SEGMENT_F_IS_PREALLOCATED = 1 << 0,
36 FIFO_SEGMENT_F_WILL_DELETE = 1 << 1,
37} fifo_segment_flags_t;
38
39typedef struct
40{
Florin Corasb095a3c2019-04-25 12:58:46 -070041 svm_fifo_t *fifos; /**< Linked list of active RX fifos */
42 svm_fifo_t **free_fifos; /**< Freelists by fifo size */
43 svm_fifo_chunk_t **free_chunks; /**< Freelists by chunk size */
44 u32 n_active_fifos; /**< Number of active fifos */
45 u8 flags; /**< Segment flags */
Florin Coras88001c62019-04-24 14:44:46 -070046} fifo_segment_header_t;
47
48typedef struct
49{
50 ssvm_private_t ssvm; /**< ssvm segment data */
51 fifo_segment_header_t *h; /**< fifo segment data */
52} fifo_segment_t;
53
54typedef struct
55{
56 fifo_segment_t *segments; /**< pool of fifo segments */
57 u64 next_baseva; /**< Where to put the next one */
58 u32 timeout_in_seconds; /**< Time to wait during attach */
59} fifo_segment_main_t;
60
61typedef struct
62{
63 ssvm_segment_type_t segment_type; /**< type of segment requested */
64 u32 segment_size; /**< size of the segment */
65 int memfd_fd; /**< fd for memfd segments */
66 char *segment_name; /**< segment name */
67 u32 *new_segment_indices; /**< return vec of new seg indices */
68} fifo_segment_create_args_t;
69
70#define fifo_segment_flags(_fs) _fs->h->flags
71
72int fifo_segment_init (fifo_segment_t * fs);
73int fifo_segment_create (fifo_segment_main_t * sm,
74 fifo_segment_create_args_t * a);
75int fifo_segment_attach (fifo_segment_main_t * sm,
76 fifo_segment_create_args_t * a);
77void fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * fs);
78fifo_segment_t *fifo_segment_get_segment (fifo_segment_main_t * sm,
79 u32 fs_index);
80u32 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * fs);
81void fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size);
82
83/**
84 * Allocate fifo in fifo segment
85 *
Florin Corasb095a3c2019-04-25 12:58:46 -070086 * @param fs fifo segment for fifo
Florin Coras88001c62019-04-24 14:44:46 -070087 * @param data_bytes size of default fifo chunk in bytes
88 * @param ftype fifo type @ref fifo_segment_ftype_t
89 * @return new fifo or 0 if alloc failed
90 */
91svm_fifo_t *fifo_segment_alloc_fifo (fifo_segment_t * fs,
92 u32 data_bytes,
93 fifo_segment_ftype_t ftype);
94
95/**
96 * Free fifo allocated in fifo segment
97 *
Florin Corasb095a3c2019-04-25 12:58:46 -070098 * @param fs fifo segment for fifo
Florin Coras88001c62019-04-24 14:44:46 -070099 * @param f fifo to be freed
100 */
101void fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f);
102
103/**
104 * Pre-allocates fifo pairs in fifo segment
105 *
106 * The number of fifos pre-allocated is the minimum of the requested number
107 * of pairs and the maximum number that fit within the segment. If the maximum
108 * is hit, the number of fifo pairs requested is updated by subtracting the
109 * number of fifos that have been successfully allocated.
110 *
Florin Corasb095a3c2019-04-25 12:58:46 -0700111 * @param fs fifo segment for fifo
Florin Coras88001c62019-04-24 14:44:46 -0700112 * @param rx_fifo_size data size of rx fifos
113 * @param tx_fifo_size data size of tx fifos
114 * @param n_fifo_pairs number of pairs requested. Prior to returning, this
115 * is decremented by the the number of pairs allocated.
116 */
117void fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
118 u32 rx_fifo_size,
119 u32 tx_fifo_size,
120 u32 * n_fifo_pairs);
Florin Corasb095a3c2019-04-25 12:58:46 -0700121/**
122 * Grow fifo size by adding an additional chunk of memory
123 *
124 * @param fs fifo segment for fifo
125 * @param f fifo to be grown
126 * @param chunk_size number of bytes to be added to fifo
127 * @return 0 on success or a negative number otherwise
128 */
129int fifo_segment_grow_fifo (fifo_segment_t * fs, svm_fifo_t * f,
130 u32 chunk_size);
Florin Coras88001c62019-04-24 14:44:46 -0700131u8 fifo_segment_has_fifos (fifo_segment_t * fs);
132svm_fifo_t *fifo_segment_get_fifo_list (fifo_segment_t * fs);
133u32 fifo_segment_num_fifos (fifo_segment_t * fs);
134u32 fifo_segment_num_free_fifos (fifo_segment_t * fs, u32 fifo_size_in_bytes);
Florin Corasb095a3c2019-04-25 12:58:46 -0700135/**
136 * Find number of free chunks of given size
137 *
138 * @param fs fifo segment
139 * @param size chunk size of interest or ~0 if all should be counted
140 * @return number of chunks of given size
141 */
142u32 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size);
Florin Coras88001c62019-04-24 14:44:46 -0700143
144void fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
145 u32 timeout_in_seconds);
146
147format_function_t format_fifo_segment;
148format_function_t format_fifo_segment_type;
149
150#endif /* __included_fifo_segment_h__ */
151
152/*
153 * fd.io coding-style-patch-verification: ON
154 *
155 * Local Variables:
156 * eval: (c-set-style "gnu")
157 * End:
158 */