http_static: refactor to use http transport
[vpp.git] / src / plugins / http / http_buffer.c
1 /*
2  * Copyright (c) 2022 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 <http/http_buffer.h>
17 #include <http/http.h>
18
19 static http_buffer_vft_t buf_vfts[HTTP_BUFFER_PTR + 1];
20
21 #define HTTP_BUFFER_REGISTER_VFT(type, vft)                                   \
22   static void __attribute__ ((constructor)) http_buf_init_##type (void)       \
23   {                                                                           \
24     buf_vfts[type] = vft;                                                     \
25   }
26
27 typedef struct http_buffer_fifo_
28 {
29   svm_fifo_t *src;
30   svm_fifo_seg_t *segs;
31   u32 len;
32   u32 offset;
33 } http_buffer_fifo_t;
34
35 STATIC_ASSERT (sizeof (http_buffer_fifo_t) <= HTTP_BUFFER_DATA_SZ, "buf data");
36
37 static void
38 buf_fifo_init (http_buffer_t *hb, void *data, u32 len)
39 {
40   svm_fifo_t *f = (svm_fifo_t *) data;
41   http_buffer_fifo_t *bf;
42
43   bf = (http_buffer_fifo_t *) &hb->data;
44
45   bf->len = len;
46   bf->offset = 0;
47   bf->src = f;
48   bf->segs = 0;
49 }
50
51 static void
52 buf_fifo_free (http_buffer_t *hb)
53 {
54   http_buffer_fifo_t *bf = (http_buffer_fifo_t *) &hb->data;
55
56   bf->src = 0;
57   vec_free (bf->segs);
58 }
59
60 static svm_fifo_seg_t *
61 buf_fifo_get_segs (http_buffer_t *hb, u32 max_len, u32 *n_segs)
62 {
63   http_buffer_fifo_t *bf = (http_buffer_fifo_t *) &hb->data;
64
65   u32 _n_segs = 5;
66   int len;
67
68   max_len = clib_max (bf->len - bf->offset, max_len);
69
70   vec_validate (bf->segs, _n_segs);
71
72   len = svm_fifo_segments (bf->src, 0, bf->segs, &_n_segs, max_len);
73   if (len < 0)
74     return 0;
75
76   *n_segs = _n_segs;
77
78   HTTP_DBG (1, "available to send %u n_segs %u", len, *n_segs);
79
80   return bf->segs;
81 }
82
83 static int
84 buf_fifo_drain (http_buffer_t *hb, u32 len)
85 {
86   http_buffer_fifo_t *bf = (http_buffer_fifo_t *) &hb->data;
87
88   bf->offset += len;
89   svm_fifo_dequeue_drop (bf->src, len);
90   HTTP_DBG (1, "drained %u len %u offset %u", len, bf->len, bf->offset);
91
92   return len;
93 }
94
95 static u8
96 buf_fifo_is_drained (http_buffer_t *hb)
97 {
98   http_buffer_fifo_t *bf = (http_buffer_fifo_t *) &hb->data;
99
100   ASSERT (bf->offset <= bf->len);
101   return (bf->offset == bf->len);
102 }
103
104 const static http_buffer_vft_t buf_fifo_vft = {
105   .init = buf_fifo_init,
106   .free = buf_fifo_free,
107   .get_segs = buf_fifo_get_segs,
108   .drain = buf_fifo_drain,
109   .is_drained = buf_fifo_is_drained,
110 };
111
112 HTTP_BUFFER_REGISTER_VFT (HTTP_BUFFER_FIFO, buf_fifo_vft);
113
114 typedef struct http_buffer_ptr_
115 {
116   svm_fifo_seg_t *segs;
117   svm_fifo_t *f;
118 } http_buffer_ptr_t;
119
120 STATIC_ASSERT (sizeof (http_buffer_ptr_t) <= HTTP_BUFFER_DATA_SZ, "buf data");
121
122 static void
123 buf_ptr_init (http_buffer_t *hb, void *data, u32 len)
124 {
125   svm_fifo_t *f = (svm_fifo_t *) data;
126   http_buffer_ptr_t *bf;
127   uword ptr;
128   int rv;
129
130   bf = (http_buffer_ptr_t *) &hb->data;
131
132   /* Peek the pointer, do not drain the fifo until done with transfer */
133   rv = svm_fifo_peek (f, 0, sizeof (ptr), (u8 *) &ptr);
134   ASSERT (rv == sizeof (ptr));
135
136   bf->f = f;
137   bf->segs = 0;
138   vec_validate (bf->segs, 1);
139
140   bf->segs[0].data = uword_to_pointer (ptr, u8 *);
141   bf->segs[0].len = len;
142
143   bf->segs[1] = bf->segs[0];
144 }
145
146 static void
147 buf_ptr_free (http_buffer_t *hb)
148 {
149   http_buffer_ptr_t *bf = (http_buffer_ptr_t *) &hb->data;
150
151   bf->f = 0;
152   vec_free (bf->segs);
153 }
154
155 static svm_fifo_seg_t *
156 buf_ptr_get_segs (http_buffer_t *hb, u32 max_len, u32 *n_segs)
157 {
158   http_buffer_ptr_t *bf = (http_buffer_ptr_t *) &hb->data;
159
160   *n_segs = 1;
161
162   return &bf->segs[1];
163 }
164
165 static int
166 buf_ptr_drain (http_buffer_t *hb, u32 len)
167 {
168   http_buffer_ptr_t *bf = (http_buffer_ptr_t *) &hb->data;
169
170   bf->segs[1].data += len;
171   bf->segs[1].len -= len;
172
173   HTTP_DBG (1, "drained %u left %u", len, bf->segs[1].len);
174
175   if (!bf->segs[1].len)
176     {
177       svm_fifo_dequeue_drop (bf->f, sizeof (uword));
178       return sizeof (uword);
179     }
180
181   return 0;
182 }
183
184 static u8
185 buf_ptr_is_drained (http_buffer_t *hb)
186 {
187   http_buffer_ptr_t *bf = (http_buffer_ptr_t *) &hb->data;
188
189   ASSERT (bf->segs[1].len <= bf->segs[0].len);
190   return (bf->segs[1].len == 0);
191 }
192
193 const static http_buffer_vft_t buf_ptr_vft = {
194   .init = buf_ptr_init,
195   .free = buf_ptr_free,
196   .get_segs = buf_ptr_get_segs,
197   .drain = buf_ptr_drain,
198   .is_drained = buf_ptr_is_drained,
199 };
200
201 HTTP_BUFFER_REGISTER_VFT (HTTP_BUFFER_PTR, buf_ptr_vft);
202
203 void
204 http_buffer_init (http_buffer_t *hb, http_buffer_type_t type, svm_fifo_t *f,
205                   u32 data_len)
206 {
207   hb->vft = &buf_vfts[type];
208   hb->vft->init (hb, f, data_len);
209 }
210
211 /*
212  * fd.io coding-style-patch-verification: ON
213  *
214  * Local Variables:
215  * eval: (c-set-style "gnu")
216  * End:
217  */