1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 6WIND S.A.
9 #include <rte_common.h>
11 #include <rte_eal_memconfig.h>
12 #include <rte_tailq.h>
13 #include <rte_errno.h>
14 #include <rte_malloc.h>
15 #include <rte_string_fns.h>
17 #include <rte_mbuf_dyn.h>
19 #define RTE_MBUF_DYN_MZNAME "rte_mbuf_dyn"
21 struct mbuf_dynfield_elt {
22 TAILQ_ENTRY(mbuf_dynfield_elt) next;
23 struct rte_mbuf_dynfield params;
26 TAILQ_HEAD(mbuf_dynfield_list, rte_tailq_entry);
28 static struct rte_tailq_elem mbuf_dynfield_tailq = {
29 .name = "RTE_MBUF_DYNFIELD",
31 EAL_REGISTER_TAILQ(mbuf_dynfield_tailq);
33 struct mbuf_dynflag_elt {
34 TAILQ_ENTRY(mbuf_dynflag_elt) next;
35 struct rte_mbuf_dynflag params;
38 TAILQ_HEAD(mbuf_dynflag_list, rte_tailq_entry);
40 static struct rte_tailq_elem mbuf_dynflag_tailq = {
41 .name = "RTE_MBUF_DYNFLAG",
43 EAL_REGISTER_TAILQ(mbuf_dynflag_tailq);
47 * For each mbuf byte, free_space[i] != 0 if space is free.
48 * The value is the size of the biggest aligned element that
49 * can fit in the zone.
51 uint8_t free_space[sizeof(struct rte_mbuf)];
52 /** Bitfield of available flags. */
55 static struct mbuf_dyn_shm *shm;
57 /* Set the value of free_space[] according to the size and alignment of
58 * the free areas. This helps to select the best place when reserving a
59 * dynamic field. Assume tailq is locked.
64 size_t off, align, size, i;
66 /* first, erase previous info */
67 for (i = 0; i < sizeof(struct rte_mbuf); i++) {
68 if (shm->free_space[i])
69 shm->free_space[i] = 1;
72 for (off = 0; off < sizeof(struct rte_mbuf); off++) {
73 /* get the size of the free zone */
74 for (size = 0; shm->free_space[off + size]; size++)
79 /* get the alignment of biggest object that can fit in
80 * the zone at this offset.
83 (off % (align << 1)) == 0 && (align << 1) <= size;
87 /* save it in free_space[] */
88 for (i = off; i < off + size; i++)
89 shm->free_space[i] = RTE_MAX(align, shm->free_space[i]);
93 /* Mark the area occupied by a mbuf field as available in the shm. */
94 #define mark_free(field) \
95 memset(&shm->free_space[offsetof(struct rte_mbuf, field)], \
96 1, sizeof(((struct rte_mbuf *)0)->field))
98 /* Allocate and initialize the shared memory. Assume tailq is locked */
100 init_shared_mem(void)
102 const struct rte_memzone *mz;
105 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
106 mz = rte_memzone_reserve_aligned(RTE_MBUF_DYN_MZNAME,
107 sizeof(struct mbuf_dyn_shm),
109 RTE_CACHE_LINE_SIZE);
111 mz = rte_memzone_lookup(RTE_MBUF_DYN_MZNAME);
118 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
119 /* init free_space, keep it sync'd with
120 * rte_mbuf_dynfield_copy().
122 memset(shm, 0, sizeof(*shm));
123 mark_free(dynfield1);
125 /* init free_flags */
126 for (mask = PKT_FIRST_FREE; mask <= PKT_LAST_FREE; mask <<= 1)
127 shm->free_flags |= mask;
135 /* check if this offset can be used */
137 check_offset(size_t offset, size_t size, size_t align)
141 if ((offset & (align - 1)) != 0)
143 if (offset + size > sizeof(struct rte_mbuf))
146 for (i = 0; i < size; i++) {
147 if (!shm->free_space[i + offset])
154 /* assume tailq is locked */
155 static struct mbuf_dynfield_elt *
156 __mbuf_dynfield_lookup(const char *name)
158 struct mbuf_dynfield_list *mbuf_dynfield_list;
159 struct mbuf_dynfield_elt *mbuf_dynfield;
160 struct rte_tailq_entry *te;
162 mbuf_dynfield_list = RTE_TAILQ_CAST(
163 mbuf_dynfield_tailq.head, mbuf_dynfield_list);
165 TAILQ_FOREACH(te, mbuf_dynfield_list, next) {
166 mbuf_dynfield = (struct mbuf_dynfield_elt *)te->data;
167 if (strcmp(name, mbuf_dynfield->params.name) == 0)
176 return mbuf_dynfield;
180 rte_mbuf_dynfield_lookup(const char *name, struct rte_mbuf_dynfield *params)
182 struct mbuf_dynfield_elt *mbuf_dynfield;
189 rte_mcfg_tailq_read_lock();
190 mbuf_dynfield = __mbuf_dynfield_lookup(name);
191 rte_mcfg_tailq_read_unlock();
193 if (mbuf_dynfield == NULL) {
199 memcpy(params, &mbuf_dynfield->params, sizeof(*params));
201 return mbuf_dynfield->offset;
204 static int mbuf_dynfield_cmp(const struct rte_mbuf_dynfield *params1,
205 const struct rte_mbuf_dynfield *params2)
207 if (strcmp(params1->name, params2->name))
209 if (params1->size != params2->size)
211 if (params1->align != params2->align)
213 if (params1->flags != params2->flags)
218 /* assume tailq is locked */
220 __rte_mbuf_dynfield_register_offset(const struct rte_mbuf_dynfield *params,
223 struct mbuf_dynfield_list *mbuf_dynfield_list;
224 struct mbuf_dynfield_elt *mbuf_dynfield = NULL;
225 struct rte_tailq_entry *te = NULL;
226 unsigned int best_zone = UINT_MAX;
230 if (shm == NULL && init_shared_mem() < 0)
233 mbuf_dynfield = __mbuf_dynfield_lookup(params->name);
234 if (mbuf_dynfield != NULL) {
235 if (req != SIZE_MAX && req != mbuf_dynfield->offset) {
239 if (mbuf_dynfield_cmp(params, &mbuf_dynfield->params) < 0) {
243 return mbuf_dynfield->offset;
246 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
251 if (req == SIZE_MAX) {
252 /* Find the best place to put this field: we search the
253 * lowest value of shm->free_space[offset]: the zones
254 * containing room for larger fields are kept for later.
257 offset < sizeof(struct rte_mbuf);
259 if (check_offset(offset, params->size,
260 params->align) == 0 &&
261 shm->free_space[offset] < best_zone) {
262 best_zone = shm->free_space[offset];
266 if (req == SIZE_MAX) {
271 if (check_offset(req, params->size, params->align) < 0) {
278 mbuf_dynfield_list = RTE_TAILQ_CAST(
279 mbuf_dynfield_tailq.head, mbuf_dynfield_list);
281 te = rte_zmalloc("MBUF_DYNFIELD_TAILQ_ENTRY", sizeof(*te), 0);
285 mbuf_dynfield = rte_zmalloc("mbuf_dynfield", sizeof(*mbuf_dynfield), 0);
286 if (mbuf_dynfield == NULL) {
291 ret = strlcpy(mbuf_dynfield->params.name, params->name,
292 sizeof(mbuf_dynfield->params.name));
293 if (ret < 0 || ret >= (int)sizeof(mbuf_dynfield->params.name)) {
294 rte_errno = ENAMETOOLONG;
295 rte_free(mbuf_dynfield);
299 memcpy(&mbuf_dynfield->params, params, sizeof(mbuf_dynfield->params));
300 mbuf_dynfield->offset = offset;
301 te->data = mbuf_dynfield;
303 TAILQ_INSERT_TAIL(mbuf_dynfield_list, te, next);
305 for (i = offset; i < offset + params->size; i++)
306 shm->free_space[i] = 0;
309 RTE_LOG(DEBUG, MBUF, "Registered dynamic field %s (sz=%zu, al=%zu, fl=0x%x) -> %zd\n",
310 params->name, params->size, params->align, params->flags,
317 rte_mbuf_dynfield_register_offset(const struct rte_mbuf_dynfield *params,
322 if (params->size >= sizeof(struct rte_mbuf)) {
326 if (!rte_is_power_of_2(params->align)) {
330 if (params->flags != 0) {
335 rte_mcfg_tailq_write_lock();
336 ret = __rte_mbuf_dynfield_register_offset(params, req);
337 rte_mcfg_tailq_write_unlock();
343 rte_mbuf_dynfield_register(const struct rte_mbuf_dynfield *params)
345 return rte_mbuf_dynfield_register_offset(params, SIZE_MAX);
348 /* assume tailq is locked */
349 static struct mbuf_dynflag_elt *
350 __mbuf_dynflag_lookup(const char *name)
352 struct mbuf_dynflag_list *mbuf_dynflag_list;
353 struct mbuf_dynflag_elt *mbuf_dynflag;
354 struct rte_tailq_entry *te;
356 mbuf_dynflag_list = RTE_TAILQ_CAST(
357 mbuf_dynflag_tailq.head, mbuf_dynflag_list);
359 TAILQ_FOREACH(te, mbuf_dynflag_list, next) {
360 mbuf_dynflag = (struct mbuf_dynflag_elt *)te->data;
361 if (strncmp(name, mbuf_dynflag->params.name,
362 RTE_MBUF_DYN_NAMESIZE) == 0)
375 rte_mbuf_dynflag_lookup(const char *name,
376 struct rte_mbuf_dynflag *params)
378 struct mbuf_dynflag_elt *mbuf_dynflag;
385 rte_mcfg_tailq_read_lock();
386 mbuf_dynflag = __mbuf_dynflag_lookup(name);
387 rte_mcfg_tailq_read_unlock();
389 if (mbuf_dynflag == NULL) {
395 memcpy(params, &mbuf_dynflag->params, sizeof(*params));
397 return mbuf_dynflag->bitnum;
400 static int mbuf_dynflag_cmp(const struct rte_mbuf_dynflag *params1,
401 const struct rte_mbuf_dynflag *params2)
403 if (strcmp(params1->name, params2->name))
405 if (params1->flags != params2->flags)
410 /* assume tailq is locked */
412 __rte_mbuf_dynflag_register_bitnum(const struct rte_mbuf_dynflag *params,
415 struct mbuf_dynflag_list *mbuf_dynflag_list;
416 struct mbuf_dynflag_elt *mbuf_dynflag = NULL;
417 struct rte_tailq_entry *te = NULL;
421 if (shm == NULL && init_shared_mem() < 0)
424 mbuf_dynflag = __mbuf_dynflag_lookup(params->name);
425 if (mbuf_dynflag != NULL) {
426 if (req != UINT_MAX && req != mbuf_dynflag->bitnum) {
430 if (mbuf_dynflag_cmp(params, &mbuf_dynflag->params) < 0) {
434 return mbuf_dynflag->bitnum;
437 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
442 if (req == UINT_MAX) {
443 if (shm->free_flags == 0) {
447 bitnum = rte_bsf64(shm->free_flags);
449 if ((shm->free_flags & (1ULL << req)) == 0) {
456 mbuf_dynflag_list = RTE_TAILQ_CAST(
457 mbuf_dynflag_tailq.head, mbuf_dynflag_list);
459 te = rte_zmalloc("MBUF_DYNFLAG_TAILQ_ENTRY", sizeof(*te), 0);
463 mbuf_dynflag = rte_zmalloc("mbuf_dynflag", sizeof(*mbuf_dynflag), 0);
464 if (mbuf_dynflag == NULL) {
469 ret = strlcpy(mbuf_dynflag->params.name, params->name,
470 sizeof(mbuf_dynflag->params.name));
471 if (ret < 0 || ret >= (int)sizeof(mbuf_dynflag->params.name)) {
472 rte_free(mbuf_dynflag);
474 rte_errno = ENAMETOOLONG;
477 mbuf_dynflag->bitnum = bitnum;
478 te->data = mbuf_dynflag;
480 TAILQ_INSERT_TAIL(mbuf_dynflag_list, te, next);
482 shm->free_flags &= ~(1ULL << bitnum);
484 RTE_LOG(DEBUG, MBUF, "Registered dynamic flag %s (fl=0x%x) -> %u\n",
485 params->name, params->flags, bitnum);
491 rte_mbuf_dynflag_register_bitnum(const struct rte_mbuf_dynflag *params,
496 if (req >= RTE_SIZEOF_FIELD(struct rte_mbuf, ol_flags) * CHAR_BIT &&
502 rte_mcfg_tailq_write_lock();
503 ret = __rte_mbuf_dynflag_register_bitnum(params, req);
504 rte_mcfg_tailq_write_unlock();
510 rte_mbuf_dynflag_register(const struct rte_mbuf_dynflag *params)
512 return rte_mbuf_dynflag_register_bitnum(params, UINT_MAX);
515 void rte_mbuf_dyn_dump(FILE *out)
517 struct mbuf_dynfield_list *mbuf_dynfield_list;
518 struct mbuf_dynfield_elt *dynfield;
519 struct mbuf_dynflag_list *mbuf_dynflag_list;
520 struct mbuf_dynflag_elt *dynflag;
521 struct rte_tailq_entry *te;
524 rte_mcfg_tailq_write_lock();
526 fprintf(out, "Reserved fields:\n");
527 mbuf_dynfield_list = RTE_TAILQ_CAST(
528 mbuf_dynfield_tailq.head, mbuf_dynfield_list);
529 TAILQ_FOREACH(te, mbuf_dynfield_list, next) {
530 dynfield = (struct mbuf_dynfield_elt *)te->data;
531 fprintf(out, " name=%s offset=%zd size=%zd align=%zd flags=%x\n",
532 dynfield->params.name, dynfield->offset,
533 dynfield->params.size, dynfield->params.align,
534 dynfield->params.flags);
536 fprintf(out, "Reserved flags:\n");
537 mbuf_dynflag_list = RTE_TAILQ_CAST(
538 mbuf_dynflag_tailq.head, mbuf_dynflag_list);
539 TAILQ_FOREACH(te, mbuf_dynflag_list, next) {
540 dynflag = (struct mbuf_dynflag_elt *)te->data;
541 fprintf(out, " name=%s bitnum=%u flags=%x\n",
542 dynflag->params.name, dynflag->bitnum,
543 dynflag->params.flags);
545 fprintf(out, "Free space in mbuf (0 = free, value = zone alignment):\n");
546 for (i = 0; i < sizeof(struct rte_mbuf); i++) {
548 fprintf(out, " %4.4zx: ", i);
549 fprintf(out, "%2.2x%s", shm->free_space[i],
550 (i % 8 != 7) ? " " : "\n");
552 rte_mcfg_tailq_write_unlock();