svm: split fifo into private and shared structs
[vpp.git] / src / svm / fifo_types.h
1 /*
2  * Copyright (c) 2020 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 #ifndef SRC_SVM_FIFO_TYPES_H_
17 #define SRC_SVM_FIFO_TYPES_H_
18
19 #include <svm/ssvm.h>
20 #include <vppinfra/clib.h>
21 #include <vppinfra/rbtree.h>
22 #include <vppinfra/lock.h>
23
24 #define FS_MIN_LOG2_CHUNK_SZ 12 /**< also min fifo size */
25 #define FS_MAX_LOG2_CHUNK_SZ 22 /**< 4MB max chunk size */
26 #define FS_CHUNK_VEC_LEN     11 /**< number of chunk sizes */
27
28 STATIC_ASSERT ((FS_MAX_LOG2_CHUNK_SZ - FS_MIN_LOG2_CHUNK_SZ) ==
29                  FS_CHUNK_VEC_LEN - 1,
30                "update chunk sizes");
31
32 #define SVM_FIFO_TRACE                  (0)
33 #define SVM_FIFO_MAX_EVT_SUBSCRIBERS    7
34
35 typedef struct fifo_segment_header_ fifo_segment_header_t;
36
37 typedef struct svm_fifo_chunk_
38 {
39   u32 start_byte;               /**< chunk start byte */
40   u32 length;                   /**< length of chunk in bytes */
41   struct svm_fifo_chunk_ *next; /**< pointer to next chunk in linked-lists */
42   rb_node_index_t enq_rb_index; /**< enq node index if chunk in rbtree */
43   rb_node_index_t deq_rb_index; /**< deq node index if chunk in rbtree */
44   u8 data[0];                   /**< start of chunk data */
45 } svm_fifo_chunk_t;
46
47 typedef struct
48 {
49   u32 next;     /**< Next linked-list element pool index */
50   u32 prev;     /**< Previous linked-list element pool index */
51   u32 start;    /**< Start of segment, normalized*/
52   u32 length;   /**< Length of segment */
53 } ooo_segment_t;
54
55 typedef struct
56 {
57   u32 offset;
58   u32 len;
59   u32 action;
60 } svm_fifo_trace_elem_t;
61
62 typedef struct svm_fifo_shr_
63 {
64   CLIB_CACHE_LINE_ALIGN_MARK (shared);
65   svm_fifo_chunk_t *start_chunk;/**< first chunk in fifo chunk list */
66   svm_fifo_chunk_t *end_chunk;  /**< end chunk in fifo chunk list */
67   volatile u32 has_event;       /**< non-zero if deq event exists */
68   u32 min_alloc;                /**< min chunk alloc if space available */
69   u32 size;                     /**< size of the fifo in bytes */
70   u32 master_session_index;     /**< session layer session index */
71   u32 client_session_index;     /**< app session index */
72   u8 slice_index;               /**< segment slice for fifo */
73   struct svm_fifo_shr_ *next;   /**< next in freelist/active chain */
74
75   CLIB_CACHE_LINE_ALIGN_MARK (consumer);
76   svm_fifo_chunk_t *head_chunk; /**< tracks chunk where head lands */
77   u32 head;                     /**< fifo head position/byte */
78   volatile u32 want_deq_ntf;    /**< producer wants nudge */
79   volatile u32 has_deq_ntf;
80
81   CLIB_CACHE_LINE_ALIGN_MARK (producer);
82   u32 tail;                     /**< fifo tail position/byte */
83   svm_fifo_chunk_t *tail_chunk; /**< tracks chunk where tail lands */
84   volatile u8 n_subscribers;    /**< Number of subscribers for io events */
85   u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
86 } svm_fifo_shared_t;
87
88 typedef struct _svm_fifo
89 {
90   CLIB_CACHE_LINE_ALIGN_MARK (cacheline);
91   svm_fifo_shared_t *shr;        /**< shared fifo in fifo segment memory */
92   fifo_segment_header_t *fs_hdr; /**< fifo segment header for fifo */
93   rb_tree_t ooo_enq_lookup;      /**< rbtree for ooo enq chunk lookup */
94   rb_tree_t ooo_deq_lookup;      /**< rbtree for ooo deq chunk lookup */
95   svm_fifo_chunk_t *ooo_deq;     /**< last chunk used for ooo dequeue */
96   svm_fifo_chunk_t *ooo_enq;    /**< last chunk used for ooo enqueue */
97   ooo_segment_t *ooo_segments;  /**< Pool of ooo segments */
98   u32 ooos_list_head;           /**< Head of out-of-order linked-list */
99   u32 ooos_newest;              /**< Last segment to have been updated */
100
101   u8 flags;               /**< fifo flags */
102   u8 master_thread_index; /**< session layer thread index */
103   u8 client_thread_index; /**< app worker index */
104   i8 refcnt;              /**< reference count  */
105   u32 segment_manager;    /**< session layer segment manager index */
106   u32 segment_index;      /**< segment index in segment manager */
107
108   struct _svm_fifo *next; /**< prev in active chain */
109   struct _svm_fifo *prev; /**< prev in active chain */
110
111 #if SVM_FIFO_TRACE
112   svm_fifo_trace_elem_t *trace;
113 #endif
114 } svm_fifo_t;
115
116 typedef struct fifo_segment_slice_
117 {
118   svm_fifo_chunk_t *free_chunks[FS_CHUNK_VEC_LEN]; /**< Free chunks by size */
119   svm_fifo_t *fifos;                    /**< Linked list of active RX fifos */
120   svm_fifo_shared_t *free_fifos;        /**< Freelists of fifo shared hdrs  */
121   uword n_fl_chunk_bytes;               /**< Chunk bytes on freelist */
122   uword virtual_mem;                    /**< Slice sum of all fifo sizes */
123   u32 num_chunks[FS_CHUNK_VEC_LEN];     /**< Allocated chunks by chunk size */
124
125   CLIB_CACHE_LINE_ALIGN_MARK (lock);
126   u32 chunk_lock;
127 } fifo_segment_slice_t;
128
129 typedef struct fifo_slice_private_
130 {
131   clib_mem_bulk_handle_t fifos; /**< Bulk fifo allocator */
132   uword virtual_mem;            /**< Slice sum of all fifo sizes */
133 } fifo_slice_private_t;
134
135 struct fifo_segment_header_
136 {
137   uword n_cached_bytes;                 /**< Cached bytes */
138   u32 n_active_fifos;                   /**< Number of active fifos */
139   u32 n_reserved_bytes;                 /**< Bytes not to be allocated */
140   u32 max_log2_fifo_size;               /**< Max log2(chunk size) for fs */
141   u8 flags;                             /**< Segment flags */
142   u8 n_slices;                          /**< Number of slices */
143   u8 high_watermark;                    /**< Memory pressure watermark high */
144   u8 low_watermark;                     /**< Memory pressure watermark low */
145   u8 pct_first_alloc;                   /**< Pct of fifo size to alloc */
146   CLIB_CACHE_LINE_ALIGN_MARK (allocator);
147   uword byte_index;
148   uword max_byte_index;
149   CLIB_CACHE_LINE_ALIGN_MARK (slice);
150   fifo_segment_slice_t slices[0]; /** Fixed array of slices */
151 };
152
153 void fsh_virtual_mem_update (fifo_segment_header_t * fsh, u32 slice_index,
154                              int n_bytes);
155
156 #endif /* SRC_SVM_FIFO_TYPES_H_ */
157
158 /*
159  * fd.io coding-style-patch-verification: ON
160  *
161  * Local Variables:
162  * eval: (c-set-style "gnu")
163  * End:
164  */