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>
16 #include <rte_bitops.h>
18 #include <rte_mbuf_dyn.h>
20 #define RTE_MBUF_DYN_MZNAME "rte_mbuf_dyn"
22 struct mbuf_dynfield_elt {
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 struct rte_mbuf_dynflag params;
37 TAILQ_HEAD(mbuf_dynflag_list, rte_tailq_entry);
39 static struct rte_tailq_elem mbuf_dynflag_tailq = {
40 .name = "RTE_MBUF_DYNFLAG",
42 EAL_REGISTER_TAILQ(mbuf_dynflag_tailq);
46 * For each mbuf byte, free_space[i] != 0 if space is free.
47 * The value is the size of the biggest aligned element that
48 * can fit in the zone.
50 uint8_t free_space[sizeof(struct rte_mbuf)];
51 /** Bitfield of available flags. */
54 static struct mbuf_dyn_shm *shm;
56 /* Set the value of free_space[] according to the size and alignment of
57 * the free areas. This helps to select the best place when reserving a
58 * dynamic field. Assume tailq is locked.
63 size_t off, align, size, i;
65 /* first, erase previous info */
66 for (i = 0; i < sizeof(struct rte_mbuf); i++) {
67 if (shm->free_space[i])
68 shm->free_space[i] = 1;
72 while (off < sizeof(struct rte_mbuf)) {
73 /* get the size of the free zone */
74 for (size = 0; (off + size) < sizeof(struct rte_mbuf) &&
75 shm->free_space[off + size]; size++)
82 /* get the alignment of biggest object that can fit in
83 * the zone at this offset.
86 (off % (align << 1)) == 0 && (align << 1) <= size;
90 /* save it in free_space[] */
91 for (i = off; i < off + align; i++)
92 shm->free_space[i] = RTE_MAX(align, shm->free_space[i]);
98 /* Mark the area occupied by a mbuf field as available in the shm. */
99 #define mark_free(field) \
100 memset(&shm->free_space[offsetof(struct rte_mbuf, field)], \
101 1, sizeof(((struct rte_mbuf *)0)->field))
103 /* Allocate and initialize the shared memory. Assume tailq is locked */
105 init_shared_mem(void)
107 const struct rte_memzone *mz;
110 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
111 mz = rte_memzone_reserve_aligned(RTE_MBUF_DYN_MZNAME,
112 sizeof(struct mbuf_dyn_shm),
114 RTE_CACHE_LINE_SIZE);
116 mz = rte_memzone_lookup(RTE_MBUF_DYN_MZNAME);
123 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
124 /* init free_space, keep it sync'd with
125 * rte_mbuf_dynfield_copy().
127 memset(shm, 0, sizeof(*shm));
128 mark_free(dynfield1);
130 /* init free_flags */
131 for (mask = PKT_FIRST_FREE; mask <= PKT_LAST_FREE; mask <<= 1)
132 shm->free_flags |= mask;
140 /* check if this offset can be used */
142 check_offset(size_t offset, size_t size, size_t align)
146 if ((offset & (align - 1)) != 0)
148 if (offset + size > sizeof(struct rte_mbuf))
151 for (i = 0; i < size; i++) {
152 if (!shm->free_space[i + offset])
159 /* assume tailq is locked */
160 static struct mbuf_dynfield_elt *
161 __mbuf_dynfield_lookup(const char *name)
163 struct mbuf_dynfield_list *mbuf_dynfield_list;
164 struct mbuf_dynfield_elt *mbuf_dynfield;
165 struct rte_tailq_entry *te;
167 mbuf_dynfield_list = RTE_TAILQ_CAST(
168 mbuf_dynfield_tailq.head, mbuf_dynfield_list);
170 TAILQ_FOREACH(te, mbuf_dynfield_list, next) {
171 mbuf_dynfield = (struct mbuf_dynfield_elt *)te->data;
172 if (strcmp(name, mbuf_dynfield->params.name) == 0)
181 return mbuf_dynfield;
185 rte_mbuf_dynfield_lookup(const char *name, struct rte_mbuf_dynfield *params)
187 struct mbuf_dynfield_elt *mbuf_dynfield;
194 rte_mcfg_tailq_read_lock();
195 mbuf_dynfield = __mbuf_dynfield_lookup(name);
196 rte_mcfg_tailq_read_unlock();
198 if (mbuf_dynfield == NULL) {
204 memcpy(params, &mbuf_dynfield->params, sizeof(*params));
206 return mbuf_dynfield->offset;
209 static int mbuf_dynfield_cmp(const struct rte_mbuf_dynfield *params1,
210 const struct rte_mbuf_dynfield *params2)
212 if (strcmp(params1->name, params2->name))
214 if (params1->size != params2->size)
216 if (params1->align != params2->align)
218 if (params1->flags != params2->flags)
223 /* assume tailq is locked */
225 __rte_mbuf_dynfield_register_offset(const struct rte_mbuf_dynfield *params,
228 struct mbuf_dynfield_list *mbuf_dynfield_list;
229 struct mbuf_dynfield_elt *mbuf_dynfield = NULL;
230 struct rte_tailq_entry *te = NULL;
231 unsigned int best_zone = UINT_MAX;
235 if (shm == NULL && init_shared_mem() < 0)
238 mbuf_dynfield = __mbuf_dynfield_lookup(params->name);
239 if (mbuf_dynfield != NULL) {
240 if (req != SIZE_MAX && req != mbuf_dynfield->offset) {
244 if (mbuf_dynfield_cmp(params, &mbuf_dynfield->params) < 0) {
248 return mbuf_dynfield->offset;
251 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
256 if (req == SIZE_MAX) {
257 /* Find the best place to put this field: we search the
258 * lowest value of shm->free_space[offset]: the zones
259 * containing room for larger fields are kept for later.
262 offset < sizeof(struct rte_mbuf);
264 if (check_offset(offset, params->size,
265 params->align) == 0 &&
266 shm->free_space[offset] < best_zone) {
267 best_zone = shm->free_space[offset];
271 if (req == SIZE_MAX) {
276 if (check_offset(req, params->size, params->align) < 0) {
283 mbuf_dynfield_list = RTE_TAILQ_CAST(
284 mbuf_dynfield_tailq.head, mbuf_dynfield_list);
286 te = rte_zmalloc("MBUF_DYNFIELD_TAILQ_ENTRY", sizeof(*te), 0);
292 mbuf_dynfield = rte_zmalloc("mbuf_dynfield", sizeof(*mbuf_dynfield), 0);
293 if (mbuf_dynfield == NULL) {
299 ret = strlcpy(mbuf_dynfield->params.name, params->name,
300 sizeof(mbuf_dynfield->params.name));
301 if (ret < 0 || ret >= (int)sizeof(mbuf_dynfield->params.name)) {
302 rte_errno = ENAMETOOLONG;
303 rte_free(mbuf_dynfield);
307 memcpy(&mbuf_dynfield->params, params, sizeof(mbuf_dynfield->params));
308 mbuf_dynfield->offset = offset;
309 te->data = mbuf_dynfield;
311 TAILQ_INSERT_TAIL(mbuf_dynfield_list, te, next);
313 for (i = offset; i < offset + params->size; i++)
314 shm->free_space[i] = 0;
317 RTE_LOG(DEBUG, MBUF, "Registered dynamic field %s (sz=%zu, al=%zu, fl=0x%x) -> %zd\n",
318 params->name, params->size, params->align, params->flags,
325 rte_mbuf_dynfield_register_offset(const struct rte_mbuf_dynfield *params,
330 if (params->size >= sizeof(struct rte_mbuf)) {
334 if (!rte_is_power_of_2(params->align)) {
338 if (params->flags != 0) {
343 rte_mcfg_tailq_write_lock();
344 ret = __rte_mbuf_dynfield_register_offset(params, req);
345 rte_mcfg_tailq_write_unlock();
351 rte_mbuf_dynfield_register(const struct rte_mbuf_dynfield *params)
353 return rte_mbuf_dynfield_register_offset(params, SIZE_MAX);
356 /* assume tailq is locked */
357 static struct mbuf_dynflag_elt *
358 __mbuf_dynflag_lookup(const char *name)
360 struct mbuf_dynflag_list *mbuf_dynflag_list;
361 struct mbuf_dynflag_elt *mbuf_dynflag;
362 struct rte_tailq_entry *te;
364 mbuf_dynflag_list = RTE_TAILQ_CAST(
365 mbuf_dynflag_tailq.head, mbuf_dynflag_list);
367 TAILQ_FOREACH(te, mbuf_dynflag_list, next) {
368 mbuf_dynflag = (struct mbuf_dynflag_elt *)te->data;
369 if (strncmp(name, mbuf_dynflag->params.name,
370 RTE_MBUF_DYN_NAMESIZE) == 0)
383 rte_mbuf_dynflag_lookup(const char *name,
384 struct rte_mbuf_dynflag *params)
386 struct mbuf_dynflag_elt *mbuf_dynflag;
393 rte_mcfg_tailq_read_lock();
394 mbuf_dynflag = __mbuf_dynflag_lookup(name);
395 rte_mcfg_tailq_read_unlock();
397 if (mbuf_dynflag == NULL) {
403 memcpy(params, &mbuf_dynflag->params, sizeof(*params));
405 return mbuf_dynflag->bitnum;
408 static int mbuf_dynflag_cmp(const struct rte_mbuf_dynflag *params1,
409 const struct rte_mbuf_dynflag *params2)
411 if (strcmp(params1->name, params2->name))
413 if (params1->flags != params2->flags)
418 /* assume tailq is locked */
420 __rte_mbuf_dynflag_register_bitnum(const struct rte_mbuf_dynflag *params,
423 struct mbuf_dynflag_list *mbuf_dynflag_list;
424 struct mbuf_dynflag_elt *mbuf_dynflag = NULL;
425 struct rte_tailq_entry *te = NULL;
429 if (shm == NULL && init_shared_mem() < 0)
432 mbuf_dynflag = __mbuf_dynflag_lookup(params->name);
433 if (mbuf_dynflag != NULL) {
434 if (req != UINT_MAX && req != mbuf_dynflag->bitnum) {
438 if (mbuf_dynflag_cmp(params, &mbuf_dynflag->params) < 0) {
442 return mbuf_dynflag->bitnum;
445 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
450 if (req == UINT_MAX) {
451 if (shm->free_flags == 0) {
455 bitnum = rte_bsf64(shm->free_flags);
457 if ((shm->free_flags & (1ULL << req)) == 0) {
464 mbuf_dynflag_list = RTE_TAILQ_CAST(
465 mbuf_dynflag_tailq.head, mbuf_dynflag_list);
467 te = rte_zmalloc("MBUF_DYNFLAG_TAILQ_ENTRY", sizeof(*te), 0);
473 mbuf_dynflag = rte_zmalloc("mbuf_dynflag", sizeof(*mbuf_dynflag), 0);
474 if (mbuf_dynflag == NULL) {
480 ret = strlcpy(mbuf_dynflag->params.name, params->name,
481 sizeof(mbuf_dynflag->params.name));
482 if (ret < 0 || ret >= (int)sizeof(mbuf_dynflag->params.name)) {
483 rte_free(mbuf_dynflag);
485 rte_errno = ENAMETOOLONG;
488 mbuf_dynflag->bitnum = bitnum;
489 te->data = mbuf_dynflag;
491 TAILQ_INSERT_TAIL(mbuf_dynflag_list, te, next);
493 shm->free_flags &= ~(1ULL << bitnum);
495 RTE_LOG(DEBUG, MBUF, "Registered dynamic flag %s (fl=0x%x) -> %u\n",
496 params->name, params->flags, bitnum);
502 rte_mbuf_dynflag_register_bitnum(const struct rte_mbuf_dynflag *params,
507 if (req >= RTE_SIZEOF_FIELD(struct rte_mbuf, ol_flags) * CHAR_BIT &&
513 rte_mcfg_tailq_write_lock();
514 ret = __rte_mbuf_dynflag_register_bitnum(params, req);
515 rte_mcfg_tailq_write_unlock();
521 rte_mbuf_dynflag_register(const struct rte_mbuf_dynflag *params)
523 return rte_mbuf_dynflag_register_bitnum(params, UINT_MAX);
526 void rte_mbuf_dyn_dump(FILE *out)
528 struct mbuf_dynfield_list *mbuf_dynfield_list;
529 struct mbuf_dynfield_elt *dynfield;
530 struct mbuf_dynflag_list *mbuf_dynflag_list;
531 struct mbuf_dynflag_elt *dynflag;
532 struct rte_tailq_entry *te;
535 rte_mcfg_tailq_write_lock();
537 fprintf(out, "Reserved fields:\n");
538 mbuf_dynfield_list = RTE_TAILQ_CAST(
539 mbuf_dynfield_tailq.head, mbuf_dynfield_list);
540 TAILQ_FOREACH(te, mbuf_dynfield_list, next) {
541 dynfield = (struct mbuf_dynfield_elt *)te->data;
542 fprintf(out, " name=%s offset=%zd size=%zd align=%zd flags=%x\n",
543 dynfield->params.name, dynfield->offset,
544 dynfield->params.size, dynfield->params.align,
545 dynfield->params.flags);
547 fprintf(out, "Reserved flags:\n");
548 mbuf_dynflag_list = RTE_TAILQ_CAST(
549 mbuf_dynflag_tailq.head, mbuf_dynflag_list);
550 TAILQ_FOREACH(te, mbuf_dynflag_list, next) {
551 dynflag = (struct mbuf_dynflag_elt *)te->data;
552 fprintf(out, " name=%s bitnum=%u flags=%x\n",
553 dynflag->params.name, dynflag->bitnum,
554 dynflag->params.flags);
556 fprintf(out, "Free space in mbuf (0 = occupied, value = free zone alignment):\n");
557 for (i = 0; i < sizeof(struct rte_mbuf); i++) {
559 fprintf(out, " %4.4zx: ", i);
560 fprintf(out, "%2.2x%s", shm->free_space[i],
561 (i % 8 != 7) ? " " : "\n");
563 fprintf(out, "Free bit in mbuf->ol_flags (0 = occupied, 1 = free):\n");
564 for (i = 0; i < sizeof(uint64_t) * CHAR_BIT; i++) {
566 fprintf(out, " %4.4zx: ", i);
567 fprintf(out, "%1.1x%s", (shm->free_flags & (1ULL << i)) ? 1 : 0,
568 (i % 8 != 7) ? " " : "\n");
571 rte_mcfg_tailq_write_unlock();
575 rte_mbuf_dyn_timestamp_register(int *field_offset, uint64_t *flag,
576 const char *direction, const char *flag_name)
578 static const struct rte_mbuf_dynfield field_desc = {
579 .name = RTE_MBUF_DYNFIELD_TIMESTAMP_NAME,
580 .size = sizeof(rte_mbuf_timestamp_t),
581 .align = __alignof__(rte_mbuf_timestamp_t),
583 struct rte_mbuf_dynflag flag_desc = {};
586 offset = rte_mbuf_dynfield_register(&field_desc);
589 "Failed to register mbuf field for timestamp\n");
592 if (field_offset != NULL)
593 *field_offset = offset;
595 strlcpy(flag_desc.name, flag_name, sizeof(flag_desc.name));
596 offset = rte_mbuf_dynflag_register(&flag_desc);
599 "Failed to register mbuf flag for %s timestamp\n",
604 *flag = RTE_BIT64(offset);
610 rte_mbuf_dyn_rx_timestamp_register(int *field_offset, uint64_t *rx_flag)
612 return rte_mbuf_dyn_timestamp_register(field_offset, rx_flag,
613 "Rx", RTE_MBUF_DYNFLAG_RX_TIMESTAMP_NAME);