Line data Source code
1 : #include "fd_quic_log_tx.h"
2 : #include "../../../tango/dcache/fd_dcache.h"
3 :
4 : /* fd_quic_log_buf API ************************************************/
5 :
6 : FD_FN_CONST ulong
7 933 : fd_quic_log_buf_align( void ) {
8 933 : return FD_QUIC_LOG_BUF_ALIGN;
9 933 : }
10 :
11 : FD_FN_CONST ulong
12 1083 : fd_quic_log_buf_footprint( ulong depth ) {
13 1083 : if( FD_UNLIKELY( depth>INT_MAX ) ) return 0UL;
14 1083 : depth = fd_ulong_max( depth, FD_MCACHE_BLOCK );
15 :
16 1083 : ulong mcache_footprint = fd_mcache_footprint( depth, 0 );
17 1083 : ulong req_data_sz = fd_dcache_req_data_sz( FD_QUIC_LOG_MTU, depth, 1, 1 );
18 1083 : ulong dcache_footprint = fd_dcache_footprint( req_data_sz, 0UL );
19 :
20 1083 : if( FD_UNLIKELY( !mcache_footprint ) ) return 0UL;
21 1083 : if( FD_UNLIKELY( !req_data_sz ) ) return 0UL;
22 1083 : if( FD_UNLIKELY( !dcache_footprint ) ) return 0UL;
23 :
24 1083 : if( FD_UNLIKELY( mcache_footprint > INT_MAX ) ) return 0UL;
25 1083 : if( FD_UNLIKELY( dcache_footprint > INT_MAX ) ) return 0UL;
26 :
27 : /* Keep this in sync with FD_QUIC_LOG_BUF_FOOTPRINT */
28 1083 : ulong l = FD_LAYOUT_INIT;
29 1083 : l = FD_LAYOUT_APPEND( l, FD_QUIC_LOG_BUF_ALIGN, sizeof(fd_quic_log_buf_t) );
30 1083 : l = FD_LAYOUT_APPEND( l, FD_MCACHE_ALIGN, mcache_footprint );
31 1083 : l = FD_LAYOUT_APPEND( l, FD_DCACHE_ALIGN, dcache_footprint );
32 1083 : l = FD_LAYOUT_FINI( l, FD_QUIC_LOG_BUF_ALIGN );
33 1083 : return l;
34 1083 : }
35 :
36 : void *
37 : fd_quic_log_buf_new( void * shmlog,
38 150 : ulong depth ) {
39 :
40 150 : depth = fd_ulong_max( depth, FD_MCACHE_BLOCK );
41 150 : if( FD_UNLIKELY( !shmlog ) ) {
42 0 : FD_LOG_WARNING(( "NULL shmlog" ));
43 0 : return NULL;
44 0 : }
45 150 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmlog, FD_QUIC_LOG_BUF_ALIGN ) ) ) {
46 0 : FD_LOG_WARNING(( "misaligned shmlog" ));
47 0 : return NULL;
48 0 : }
49 150 : ulong sz = fd_quic_log_buf_footprint( depth );
50 150 : if( FD_UNLIKELY( !sz ) ) {
51 0 : FD_LOG_WARNING(( "invalid footprint for depth %lu", depth ));
52 0 : return NULL;
53 0 : }
54 150 : fd_memset( shmlog, 0, sz );
55 :
56 : /* Keep this in sync with FD_QUIC_LOG_BUF_FOOTPRINT */
57 150 : ulong mcache_footprint = fd_mcache_footprint( depth, 0 );
58 150 : ulong req_data_sz = fd_dcache_req_data_sz( FD_QUIC_LOG_MTU, depth, 1, 1 );
59 150 : ulong dcache_footprint = fd_dcache_footprint( req_data_sz, 0UL );
60 150 : FD_SCRATCH_ALLOC_INIT( l, shmlog );
61 150 : fd_quic_log_buf_t * log = FD_SCRATCH_ALLOC_APPEND( l, FD_QUIC_LOG_BUF_ALIGN, sizeof(fd_quic_log_buf_t) );
62 150 : void * mcache_mem = FD_SCRATCH_ALLOC_APPEND( l, FD_MCACHE_ALIGN, mcache_footprint );
63 150 : void * dcache_mem = FD_SCRATCH_ALLOC_APPEND( l, FD_DCACHE_ALIGN, dcache_footprint );
64 150 : FD_SCRATCH_ALLOC_FINI( l, FD_QUIC_LOG_BUF_ALIGN );
65 :
66 150 : ulong seq0 = 0UL;
67 150 : fd_frag_meta_t * mcache = fd_mcache_join( fd_mcache_new( mcache_mem, depth, 0UL, seq0 ) );
68 150 : void * dcache = fd_dcache_join( fd_dcache_new( dcache_mem, req_data_sz, 0UL ) );
69 150 : if( FD_UNLIKELY( !mcache ) ) return NULL;
70 150 : if( FD_UNLIKELY( !dcache ) ) return NULL;
71 150 : fd_mcache_seq_laddr( mcache )[0] = seq0;
72 :
73 150 : uint chunk0 = (uint)fd_dcache_compact_chunk0( log, dcache );
74 150 : uint wmark = (uint)fd_dcache_compact_wmark ( log, dcache, FD_QUIC_LOG_MTU );
75 150 : uint chunk1 = (uint)fd_dcache_compact_chunk1( log, dcache );
76 :
77 150 : *log = (fd_quic_log_buf_t) {
78 150 : .abi = {
79 150 : .magic = 0UL,
80 150 : .mcache_off = (uint)( (ulong)mcache_mem - (ulong)log ),
81 150 : .chunk0 = chunk0,
82 150 : .chunk1 = chunk1
83 150 : },
84 150 : .dcache_off = (uint)( (ulong)dcache_mem - (ulong)log ),
85 150 : .chunk0 = chunk0,
86 150 : .wmark = wmark,
87 150 : };
88 :
89 150 : FD_COMPILER_MFENCE();
90 150 : log->magic = FD_QUIC_LOG_BUF_MAGIC;
91 150 : FD_COMPILER_MFENCE();
92 150 : log->abi.magic = FD_QUIC_LOG_MAGIC;
93 150 : FD_COMPILER_MFENCE();
94 :
95 150 : return shmlog;
96 150 : }
97 :
98 : void *
99 84 : fd_quic_log_buf_delete( void * shmlog ) {
100 84 : if( FD_UNLIKELY( !shmlog ) ) {
101 0 : FD_LOG_WARNING(( "NULL shmlog" ));
102 0 : return NULL;
103 0 : }
104 84 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmlog, FD_QUIC_LOG_BUF_ALIGN ) ) ) {
105 0 : FD_LOG_WARNING(( "misaligned shmlog" ));
106 0 : return NULL;
107 0 : }
108 :
109 84 : fd_quic_log_buf_t * log = shmlog;
110 84 : if( FD_UNLIKELY( log->magic!=FD_QUIC_LOG_BUF_MAGIC ) ) {
111 0 : FD_LOG_WARNING(( "bad magic" ));
112 0 : return NULL;
113 0 : }
114 :
115 84 : void * mcache_mem = (void *)( (ulong)log + log->abi.mcache_off );
116 84 : fd_mcache_delete( mcache_mem );
117 :
118 84 : void * dcache_mem = (void *)( (ulong)log + log->dcache_off );
119 84 : fd_dcache_delete( dcache_mem );
120 :
121 84 : log->abi.magic = 0UL;
122 84 : FD_COMPILER_MFENCE();
123 84 : log->magic = 0UL;
124 84 : FD_COMPILER_MFENCE();
125 84 : return log;
126 84 : }
127 :
128 : /* fd_quic_log_tx API *************************************************/
129 :
130 : fd_quic_log_tx_t *
131 : fd_quic_log_tx_join( fd_quic_log_tx_t * tx,
132 429 : void * shmlog ) {
133 :
134 429 : if( FD_UNLIKELY( !shmlog ) ) {
135 0 : FD_LOG_WARNING(( "NULL shmlog" ));
136 0 : return NULL;
137 0 : }
138 429 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmlog, FD_QUIC_LOG_BUF_ALIGN ) ) ) {
139 0 : FD_LOG_WARNING(( "misaligned shmlog" ));
140 0 : return NULL;
141 0 : }
142 :
143 429 : fd_quic_log_buf_t * log = shmlog;
144 429 : if( FD_UNLIKELY( log->magic != FD_QUIC_LOG_BUF_MAGIC ) ) {
145 0 : FD_LOG_WARNING(( "bad magic" ));
146 0 : return NULL;
147 0 : }
148 :
149 429 : fd_frag_meta_t * mcache = fd_mcache_join( (void *)( (ulong)log + log->abi.mcache_off ) );
150 429 : if( FD_UNLIKELY( !mcache ) ) return NULL;
151 429 : void * dcache = fd_dcache_join( (void *)( (ulong)log + log->dcache_off ) );
152 429 : if( FD_UNLIKELY( !dcache ) ) return NULL;
153 :
154 429 : ulong * mcache_seq = fd_mcache_seq_laddr( mcache );
155 :
156 429 : *tx = (fd_quic_log_tx_t) {
157 429 : .mcache = mcache,
158 429 : .mcache_seq = mcache_seq,
159 429 : .base = shmlog,
160 429 : .depth = fd_mcache_depth( mcache ),
161 429 : .seq = mcache_seq[0],
162 429 : .chunk = log->chunk0,
163 429 : .chunk0 = log->chunk0,
164 429 : .wmark = log->wmark,
165 429 : };
166 429 : return tx;
167 429 : }
168 :
169 : void *
170 0 : fd_quic_log_tx_leave( fd_quic_log_tx_t * log ) {
171 0 : if( FD_UNLIKELY( !log ) ) {
172 0 : FD_LOG_WARNING(( "NULL log" ));
173 0 : return NULL;
174 0 : }
175 0 : fd_quic_log_tx_seq_update( log );
176 0 : memset( log, 0, sizeof(fd_quic_log_tx_t) );
177 0 : return log;
178 0 : }
179 :
180 : /* fd_quic_log_rx API *************************************************/
181 :
182 : fd_quic_log_rx_t *
183 : fd_quic_log_rx_join( fd_quic_log_rx_t * rx,
184 9 : void * shmlog ) {
185 :
186 9 : if( FD_UNLIKELY( !shmlog ) ) {
187 0 : FD_LOG_WARNING(( "NULL shmlog" ));
188 0 : return NULL;
189 0 : }
190 9 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmlog, FD_QUIC_LOG_ALIGN ) ) ) {
191 0 : FD_LOG_WARNING(( "misaligned shmlog" ));
192 0 : return NULL;
193 0 : }
194 :
195 9 : fd_quic_log_abi_t * abi = shmlog;
196 9 : if( FD_UNLIKELY( abi->magic != FD_QUIC_LOG_MAGIC ) ) {
197 0 : FD_LOG_WARNING(( "bad magic" ));
198 0 : return NULL;
199 0 : }
200 :
201 9 : fd_frag_meta_t * mcache = fd_mcache_join( (void *)( (ulong)shmlog + abi->mcache_off ) );
202 9 : if( FD_UNLIKELY( !mcache ) ) return NULL;
203 9 : ulong const * mcache_seq = fd_mcache_seq_laddr_const( mcache );
204 :
205 9 : void * base = shmlog;
206 9 : ulong seq = fd_mcache_seq_query( mcache_seq );
207 :
208 9 : *rx = (fd_quic_log_rx_t) {
209 9 : .mcache = mcache,
210 9 : .mcache_seq = mcache_seq,
211 9 : .base = base,
212 9 : .data_lo_laddr = (ulong)fd_chunk_to_laddr_const( base, abi->chunk0 ),
213 9 : .data_hi_laddr = (ulong)fd_chunk_to_laddr_const( base, abi->chunk1 ),
214 9 : .depth = fd_mcache_depth( mcache ),
215 9 : .seq = seq
216 9 : };
217 9 : return rx;
218 9 : }
219 :
220 : void *
221 0 : fd_quic_log_rx_leave( fd_quic_log_rx_t * log ) {
222 0 : if( FD_UNLIKELY( !log ) ) {
223 0 : FD_LOG_WARNING(( "NULL log" ));
224 0 : return NULL;
225 0 : }
226 0 : memset( log, 0, sizeof(fd_quic_log_rx_t) );
227 0 : return log;
228 0 : }
|