EM-ODP 4.4.0
Event Machine on ODP
Loading...
Searching...
No Matches
event_machine_packet.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024-2026, Nokia Solutions and Networks
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef EVENT_MACHINE_PACKET_H_
32#define EVENT_MACHINE_PACKET_H_
33
34#pragma GCC visibility push(default)
35
36/**
37 * @file
38 * @defgroup em_packet Packet events
39 * Operations on packet events.
40 * @{
41 * ### Packet events
42 * Event (major) Type: EM_EVENT_TYPE_PACKET
43 *
44 * EM APIs for basic manipulation of packet events (i.e. events allocated from
45 * EM pools created with event type EM_EVENT_TYPE_PACKET).
46 * More advanced packet manipulation should be done via ODP APIs by first
47 * converting the EM event to an ODP packet
48 * (see include/event_machine/platform/event_machine_odp_ext.h).
49 *
50 * Packet events contain a certain amount of headroom before the start of the
51 * actual packet data. The headroom allows new data, e.g. additional protocol
52 * headers, to be inserted before the start of the existing packet data. The
53 * amount of headroom available for newly allocated packets can be configured
54 * globally via the EM config file or, specifically for a pool via the
55 * em_pool_cfg_t struct passed to em_pool_create().
56 * Similarly, the packet may contain a certain amount of tailroom that can be
57 * used to extend the payload with additional data. The tailroom is located
58 * after the end of the packet payload until the end of the packet buffer.
59 * Pushing out the head/tail via APIs is required to include head- or tailroom
60 * into the packet data, see below.
61 *
62 * The start of the packet data area can be modified by "pushing out" the head
63 * into the headroom or "pulling in" the head from the headroom. Similarly, the
64 * end of the packet data area can be modified by "pushing out" or "pulling in"
65 * the tail.
66 * Pushing out the head of the packet increases the packet data area from the
67 * beginning. This allows for e.g. insertion of new packet headers (prepend).
68 * Pulling in the head of the packet allows for e.g. dropping of packet headers
69 * from the beginning of the packet data.
70 * Pushing out the tail of the packet increases the packet data area at the end
71 * of the packet. This allows for extending the packet with new data (append).
72 * Pulling in the tail can be used to trim the end of unwanted data or set the
73 * packet size exactly so no extra data is present (avoids extra data over
74 * packet I/O).
75 */
76
79
80#ifdef __cplusplus
81extern "C" {
82#endif
83
84/**
85 * Get the packet event data pointer
86 *
87 * Returns a pointer to the first byte of the packet data.
88 *
89 * The data pointer can be adjusted with em_packet_push/pull_head().
90 *
91 * Only events allocated from EM pools created with event type
92 * EM_EVENT_TYPE_PACKET can be used with this API.
93 *
94 * @param pktev Packet event handle
95 *
96 * @return Pointer to the beginning of the packet data area
97 * @retval NULL on error
98 *
99 * @see em_event_pointer()
100 */
101void *em_packet_pointer(em_event_t pktev);
102
103/**
104 * @brief Get the packet size
105 *
106 * Returns the number of linearly accessible data bytes that follows after the
107 * current packet data pointer position.
108 *
109 * Only events allocated from EM pools created with event type
110 * EM_EVENT_TYPE_PACKET can be used with this API.
111 *
112 * @param pktev Packet event handle
113 *
114 * @return Packet data size in bytes following em_packet_pointer()
115 * @retval 0 on error
116 */
117uint32_t em_packet_size(em_event_t pktev);
118
119/**
120 * @brief Get the packet event data pointer as well as the packet size.
121 *
122 * Returns a pointer to the first byte of the packet data and optionally
123 * outputs the packet payload size.
124 *
125 * This API is a combination of em_packet_pointer() and em_packet_size() since
126 * both are often needed at the same time.
127 *
128 * Only events allocated from EM pools created with event type
129 * EM_EVENT_TYPE_PACKET can be used with this API.
130 *
131 * @param pktev Packet event handle
132 * @param[out] size Optional output arg into which the packet event
133 * payload size (in bytes) is stored. Use 'size=NULL' if no
134 * size information is needed. Only set by the function when
135 * no errors occurred.
136 *
137 * @return Pointer to the beginning of the packet data area
138 * @retval NULL on error ('size' not touched)
139 *
140 * @see em_packet_pointer(), em_packet_size()
141 * @see em_event_pointer(), em_event_size(), em_event_pointer_and_size()
142 */
143void *em_packet_pointer_and_size(em_event_t pktev, uint32_t *size /*out*/);
144
145/**
146 * @brief Resize a packet event
147 *
148 * Set a new size for a packet, by adjusting the end of the packet via the
149 * packet tailroom.
150 * Only the end (tail) of the packet is modified to change the size,
151 * the beginning (head) is untouched.
152 *
153 * The packet can be enlarged with max 'em_packet_tailroom()' bytes or
154 * reduced to min one (1) byte:
155 * 1 <= 'size' <= (em_packet_size() + em_packet_tailroom())
156 *
157 * Only events allocated from EM pools created with event type
158 * EM_EVENT_TYPE_PACKET can be used with this API.
159 *
160 * The event is not modified on error.
161 *
162 * @param pktev Packet event handle
163 * @param size New size of the packet event (1 ... current-size + tailroom)
164 *
165 * @return Pointer to the beginning of the packet data
166 * @retval NULL on unsupported event type, invalid size or other error
167 *
168 * @see em_packet_push_tail(), em_packet_pull_tail()
169 */
170void *em_packet_resize(em_event_t pktev, uint32_t size);
171
172/**
173 * @brief Packet event headroom length
174 *
175 * Returns the current length of the packet headroom.
176 *
177 * Only events allocated from EM pools created with event type
178 * EM_EVENT_TYPE_PACKET can be used with this API.
179 *
180 * @param pktev Packet event handle
181 *
182 * @return Headroom length
183 * @retval 0 if no headroom, but also on error (e.g. if the event isn't a packet)
184 */
185uint32_t em_packet_headroom(em_event_t pktev);
186
187/**
188 * @brief Packet event tailroom length
189 *
190 * Returns the current length of the packet tailroom.
191 *
192 * Only events allocated from EM pools created with event type
193 * EM_EVENT_TYPE_PACKET can be used with this API.
194 *
195 * @param pktev Packet event handle
196 *
197 * @return Tailroom length
198 * @retval 0 if no tailroom, but also on error (e.g. if the event isn't a packet)
199 */
200uint32_t em_packet_tailroom(em_event_t pktev);
201
202/**
203 * @brief Push out the beginning of the packet into the headroom
204 *
205 * Increase the packet data length by moving the beginning (head) of the packet
206 * into the packet headroom. The packet headroom is decreased by the same amount.
207 * The packet head may be pushed out by up to 'headroom' bytes.
208 * The packet is not modified if there's not enough space in the headroom.
209 *
210 * Only events allocated from EM pools created with event type
211 * EM_EVENT_TYPE_PACKET can be used with this API.
212 *
213 * The event is not modified on error.
214 *
215 * @param pktev Packet event handle
216 * @param len The number of bytes to push out the head (0 ... headroom)
217 *
218 * @return Pointer to the new beginning of the increased packet data area
219 * @retval NULL on unsupported event type, invalid length or other error
220 */
221void *em_packet_push_head(em_event_t pktev, uint32_t len);
222
223/**
224 * @brief Pull in the beginning of the packet from the headroom.
225 *
226 * Decrease the packet data length by removing data from the beginning (head) of
227 * the packet. The packet headroom is increased by the same amount.
228 * The packet head may be pulled in until only 1 byte of the packet data
229 * remains.
230 * The packet is not modified if there's not enough data.
231 *
232 * Only events allocated from EM pools created with event type
233 * EM_EVENT_TYPE_PACKET can be used with this API.
234 *
235 * The event is not modified on error.
236 *
237 * @param pktev Packet event handle
238 * @param len The number of bytes to pull in the head with
239 * (0 ... packet data len - 1)
240 *
241 * @return Pointer to the new beginning of the decreased packet data area
242 * @retval NULL on unsupported event type, invalid length or other error
243 */
244void *em_packet_pull_head(em_event_t pktev, uint32_t len);
245
246/**
247 * @brief Push out the end of the packet into the tailroom
248 *
249 * Increase the packet data length by moving the end (tail) of the packet into
250 * the packet tailroom. The packet tailroom is decreased by the same amount.
251 * The packet tail may be pushed out by up to 'tailroom' bytes.
252 * The packet is not modified if there's not enough space in the tailroom.
253 *
254 * Only events allocated from EM pools created with event type
255 * EM_EVENT_TYPE_PACKET can be used with this API.
256 *
257 * The event is not modified on error.
258 *
259 * @param pktev Packet event handle
260 * @param len The number of bytes to push out the tail (0 ... tailroom)
261 *
262 * @return Pointer to the start of the newly added data at the end of the packet
263 * @retval NULL on unsupported event type or other error
264 */
265void *em_packet_push_tail(em_event_t pktev, uint32_t len);
266
267/**
268 * Pull in the end of the packet from the tailroom.
269 *
270 * Decrease the packet data length by removing data from the end (tail) of the
271 * packet. The packet tailroom is increased by the same amount.
272 * The packet tail may be pulled in until only 1 byte of the packet data
273 * remains.
274 * The packet is not modified if there's not enough data.
275 *
276 * Only events allocated from EM pools created with event type
277 * EM_EVENT_TYPE_PACKET can be used with this API.
278 *
279 * The event is not modified on error.
280 *
281 * @param pktev Packet event handle
282 * @param len The number of bytes to pull in the tail with
283 * (0 ... packet data len - 1)
284 *
285 * @return Pointer to the end of the packet
286 * @retval NULL on unsupported event type, invalid length or other error
287 */
288void *em_packet_pull_tail(em_event_t pktev, uint32_t len);
289
290/**
291 * Reset a packet event
292 *
293 * Resets packet event metadata and adjusts the start of the packet data back to
294 * pool (and subpool) defaults. The packet data size is set to 'size' and must
295 * fit the used subpool sizing.
296 *
297 * The event type, set by em_alloc...() (or em_event_set_type()) is untouched by
298 * the reset-operation.
299 *
300 * The event user area metadata is reset, i.e. the user area id is unset but the
301 * actual user area content is untouched.
302 *
303 * Logically, this reset operation is similar to freeing the event and then
304 * allocating the same event again with a given 'size' and the same type (but
305 * without any actual free & alloc via the event pool being done).
306 *
307 * This function must not be called for events with references.
308 *
309 * Only events allocated from EM pools created with event type
310 * EM_EVENT_TYPE_PACKET can be used with this API.
311 *
312 * @param pktev Packet event handle
313 * @param size Packet data size
314 *
315 * @return EM_OK when successful or an EM error code on error.
316 */
317em_status_t em_packet_reset(em_event_t pktev, uint32_t size);
318
319/**
320 * @brief Concatenate packet events.
321 *
322 * Concatenates the data from second_pkt to the end of first_pkt's data.
323 * The operation is performed either by linking packet segments or,
324 * if necessary, by copying data. The first_pkt must not have any
325 * existing references. The second_pkt may have references.
326 *
327 * The return value indicates whether pointers taken before the call
328 * (pointing to packet data or event headers) remain valid. A return
329 * value of 0 means that neither packet data nor metadata was moved
330 * in memory, and all such pointers remain valid. A return value > 0
331 * indicates that packet data was relocated, and any pointers must be
332 * refreshed using the returned first_pkt handle.
333 *
334 * On success, the first_pkt event handle is updated to a new handle,
335 * and the second_pkt event handle becomes invalid.
336 *
337 * If the operation fails, both event handles remain valid.
338 *
339 * @param[in,out] first_pkt Pointer to first packet event handle,
340 * a successful operation outputs a new
341 * event handle
342 * @param second_pkt Second packet event handle
343 *
344 * @retval 0 Success, old data pointers remain valid.
345 * @retval > 0 Success, old data pointers are invalid.
346 * @retval < 0 Failure
347 */
348int em_packet_concat(em_event_t *first_pkt /*in,out*/, em_event_t second_pkt);
349
350/**
351 * @}
352 */
353#ifdef __cplusplus
354}
355#endif
356
357#pragma GCC visibility pop
358#endif /* EVENT_MACHINE_PACKET_H_ */
uint32_t em_status_t
void * em_packet_pull_head(em_event_t pktev, uint32_t len)
Pull in the beginning of the packet from the headroom.
void * em_packet_pull_tail(em_event_t pktev, uint32_t len)
void * em_packet_push_head(em_event_t pktev, uint32_t len)
Push out the beginning of the packet into the headroom.
void * em_packet_pointer(em_event_t pktev)
void * em_packet_pointer_and_size(em_event_t pktev, uint32_t *size)
Get the packet event data pointer as well as the packet size.
em_status_t em_packet_reset(em_event_t pktev, uint32_t size)
uint32_t em_packet_headroom(em_event_t pktev)
Packet event headroom length.
int em_packet_concat(em_event_t *first_pkt, em_event_t second_pkt)
Concatenate packet events.
uint32_t em_packet_size(em_event_t pktev)
Get the packet size.
void * em_packet_push_tail(em_event_t pktev, uint32_t len)
Push out the end of the packet into the tailroom.
void * em_packet_resize(em_event_t pktev, uint32_t size)
Resize a packet event.
uint32_t em_packet_tailroom(em_event_t pktev)
Packet event tailroom length.