session: cleanup segment manager and fifo segment
[vpp.git] / src / svm / fifo_segment.h
1 /*
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
21 typedef 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
33 typedef 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
39 typedef struct
40 {
41   svm_fifo_t *fifos;            /**< Linked list of active RX fifos */
42   svm_fifo_t **free_fifos;      /**< Freelists, by fifo size  */
43   u32 n_active_fifos;           /**< Number of active fifos */
44   u8 flags;                     /**< Segment flags */
45 } fifo_segment_header_t;
46
47 typedef struct
48 {
49   ssvm_private_t ssvm;          /**< ssvm segment data */
50   fifo_segment_header_t *h;     /**< fifo segment data */
51 } fifo_segment_t;
52
53 typedef struct
54 {
55   fifo_segment_t *segments;     /**< pool of fifo segments */
56   u64 next_baseva;              /**< Where to put the next one */
57   u32 timeout_in_seconds;       /**< Time to wait during attach */
58 } fifo_segment_main_t;
59
60 typedef struct
61 {
62   ssvm_segment_type_t segment_type;     /**< type of segment requested */
63   u32 segment_size;                     /**< size of the segment */
64   int memfd_fd;                         /**< fd for memfd segments */
65   char *segment_name;                   /**< segment name */
66   u32 *new_segment_indices;             /**< return vec of new seg indices */
67 } fifo_segment_create_args_t;
68
69 #define fifo_segment_flags(_fs) _fs->h->flags
70
71 int fifo_segment_init (fifo_segment_t * fs);
72 int fifo_segment_create (fifo_segment_main_t * sm,
73                          fifo_segment_create_args_t * a);
74 int fifo_segment_attach (fifo_segment_main_t * sm,
75                          fifo_segment_create_args_t * a);
76 void fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * fs);
77 fifo_segment_t *fifo_segment_get_segment (fifo_segment_main_t * sm,
78                                           u32 fs_index);
79 u32 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * fs);
80 void fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size);
81
82 /**
83  * Allocate fifo in fifo segment
84  *
85  * @param fs            fifo segment
86  * @param data_bytes    size of default fifo chunk in bytes
87  * @param ftype         fifo type @ref fifo_segment_ftype_t
88  * @return              new fifo or 0 if alloc failed
89  */
90 svm_fifo_t *fifo_segment_alloc_fifo (fifo_segment_t * fs,
91                                      u32 data_bytes,
92                                      fifo_segment_ftype_t ftype);
93
94 /**
95  * Free fifo allocated in fifo segment
96  *
97  * @param fs            fifo segment
98  * @param f             fifo to be freed
99  */
100 void fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f);
101
102 /**
103  * Pre-allocates fifo pairs in fifo segment
104  *
105  * The number of fifos pre-allocated is the minimum of the requested number
106  * of pairs and the maximum number that fit within the segment. If the maximum
107  * is hit, the number of fifo pairs requested is updated by subtracting the
108  * number of fifos that have been successfully allocated.
109  *
110  * @param fs            fifo segment
111  * @param rx_fifo_size  data size of rx fifos
112  * @param tx_fifo_size  data size of tx fifos
113  * @param n_fifo_pairs  number of pairs requested. Prior to returning, this
114  *                      is decremented by the the number of pairs allocated.
115  */
116 void fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
117                                           u32 rx_fifo_size,
118                                           u32 tx_fifo_size,
119                                           u32 * n_fifo_pairs);
120 u8 fifo_segment_has_fifos (fifo_segment_t * fs);
121 svm_fifo_t *fifo_segment_get_fifo_list (fifo_segment_t * fs);
122 u32 fifo_segment_num_fifos (fifo_segment_t * fs);
123 u32 fifo_segment_num_free_fifos (fifo_segment_t * fs, u32 fifo_size_in_bytes);
124
125 void fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
126                              u32 timeout_in_seconds);
127
128 format_function_t format_fifo_segment;
129 format_function_t format_fifo_segment_type;
130
131 #endif /* __included_fifo_segment_h__ */
132
133 /*
134  * fd.io coding-style-patch-verification: ON
135  *
136  * Local Variables:
137  * eval: (c-set-style "gnu")
138  * End:
139  */