afe7d6b1b2bf480efe83d27ba7a6806d5aec7763
[dpdk.git] / lib / librte_mbuf / rte_mbuf_dyn.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 6WIND S.A.
3  */
4
5 #include <sys/queue.h>
6 #include <stdint.h>
7 #include <limits.h>
8
9 #include <rte_common.h>
10 #include <rte_eal.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>
17 #include <rte_mbuf.h>
18 #include <rte_mbuf_dyn.h>
19
20 #define RTE_MBUF_DYN_MZNAME "rte_mbuf_dyn"
21
22 struct mbuf_dynfield_elt {
23         struct rte_mbuf_dynfield params;
24         size_t offset;
25 };
26 TAILQ_HEAD(mbuf_dynfield_list, rte_tailq_entry);
27
28 static struct rte_tailq_elem mbuf_dynfield_tailq = {
29         .name = "RTE_MBUF_DYNFIELD",
30 };
31 EAL_REGISTER_TAILQ(mbuf_dynfield_tailq);
32
33 struct mbuf_dynflag_elt {
34         struct rte_mbuf_dynflag params;
35         unsigned int bitnum;
36 };
37 TAILQ_HEAD(mbuf_dynflag_list, rte_tailq_entry);
38
39 static struct rte_tailq_elem mbuf_dynflag_tailq = {
40         .name = "RTE_MBUF_DYNFLAG",
41 };
42 EAL_REGISTER_TAILQ(mbuf_dynflag_tailq);
43
44 struct mbuf_dyn_shm {
45         /**
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.
49          */
50         uint8_t free_space[sizeof(struct rte_mbuf)];
51         /** Bitfield of available flags. */
52         uint64_t free_flags;
53 };
54 static struct mbuf_dyn_shm *shm;
55
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.
59  */
60 static void
61 process_score(void)
62 {
63         size_t off, align, size, i;
64
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;
69         }
70
71         off = 0;
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++)
76                         ;
77                 if (size == 0) {
78                         off++;
79                         continue;
80                 }
81
82                 /* get the alignment of biggest object that can fit in
83                  * the zone at this offset.
84                  */
85                 for (align = 1;
86                      (off % (align << 1)) == 0 && (align << 1) <= size;
87                      align <<= 1)
88                         ;
89
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]);
93
94                 off += align;
95         }
96 }
97
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))
102
103 /* Allocate and initialize the shared memory. Assume tailq is locked */
104 static int
105 init_shared_mem(void)
106 {
107         const struct rte_memzone *mz;
108         uint64_t mask;
109
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),
113                                                 SOCKET_ID_ANY, 0,
114                                                 RTE_CACHE_LINE_SIZE);
115         } else {
116                 mz = rte_memzone_lookup(RTE_MBUF_DYN_MZNAME);
117         }
118         if (mz == NULL)
119                 return -1;
120
121         shm = mz->addr;
122
123         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
124                 /* init free_space, keep it sync'd with
125                  * rte_mbuf_dynfield_copy().
126                  */
127                 memset(shm, 0, sizeof(*shm));
128                 mark_free(dynfield1);
129
130                 /* init free_flags */
131                 for (mask = PKT_FIRST_FREE; mask <= PKT_LAST_FREE; mask <<= 1)
132                         shm->free_flags |= mask;
133
134                 process_score();
135         }
136
137         return 0;
138 }
139
140 /* check if this offset can be used */
141 static int
142 check_offset(size_t offset, size_t size, size_t align)
143 {
144         size_t i;
145
146         if ((offset & (align - 1)) != 0)
147                 return -1;
148         if (offset + size > sizeof(struct rte_mbuf))
149                 return -1;
150
151         for (i = 0; i < size; i++) {
152                 if (!shm->free_space[i + offset])
153                         return -1;
154         }
155
156         return 0;
157 }
158
159 /* assume tailq is locked */
160 static struct mbuf_dynfield_elt *
161 __mbuf_dynfield_lookup(const char *name)
162 {
163         struct mbuf_dynfield_list *mbuf_dynfield_list;
164         struct mbuf_dynfield_elt *mbuf_dynfield;
165         struct rte_tailq_entry *te;
166
167         mbuf_dynfield_list = RTE_TAILQ_CAST(
168                 mbuf_dynfield_tailq.head, mbuf_dynfield_list);
169
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)
173                         break;
174         }
175
176         if (te == NULL) {
177                 rte_errno = ENOENT;
178                 return NULL;
179         }
180
181         return mbuf_dynfield;
182 }
183
184 int
185 rte_mbuf_dynfield_lookup(const char *name, struct rte_mbuf_dynfield *params)
186 {
187         struct mbuf_dynfield_elt *mbuf_dynfield;
188
189         if (shm == NULL) {
190                 rte_errno = ENOENT;
191                 return -1;
192         }
193
194         rte_mcfg_tailq_read_lock();
195         mbuf_dynfield = __mbuf_dynfield_lookup(name);
196         rte_mcfg_tailq_read_unlock();
197
198         if (mbuf_dynfield == NULL) {
199                 rte_errno = ENOENT;
200                 return -1;
201         }
202
203         if (params != NULL)
204                 memcpy(params, &mbuf_dynfield->params, sizeof(*params));
205
206         return mbuf_dynfield->offset;
207 }
208
209 static int mbuf_dynfield_cmp(const struct rte_mbuf_dynfield *params1,
210                 const struct rte_mbuf_dynfield *params2)
211 {
212         if (strcmp(params1->name, params2->name))
213                 return -1;
214         if (params1->size != params2->size)
215                 return -1;
216         if (params1->align != params2->align)
217                 return -1;
218         if (params1->flags != params2->flags)
219                 return -1;
220         return 0;
221 }
222
223 /* assume tailq is locked */
224 static int
225 __rte_mbuf_dynfield_register_offset(const struct rte_mbuf_dynfield *params,
226                                 size_t req)
227 {
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;
232         size_t i, offset;
233         int ret;
234
235         if (shm == NULL && init_shared_mem() < 0)
236                 return -1;
237
238         mbuf_dynfield = __mbuf_dynfield_lookup(params->name);
239         if (mbuf_dynfield != NULL) {
240                 if (req != SIZE_MAX && req != mbuf_dynfield->offset) {
241                         rte_errno = EEXIST;
242                         return -1;
243                 }
244                 if (mbuf_dynfield_cmp(params, &mbuf_dynfield->params) < 0) {
245                         rte_errno = EEXIST;
246                         return -1;
247                 }
248                 return mbuf_dynfield->offset;
249         }
250
251         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
252                 rte_errno = EPERM;
253                 return -1;
254         }
255
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.
260                  */
261                 for (offset = 0;
262                      offset < sizeof(struct rte_mbuf);
263                      offset++) {
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];
268                                 req = offset;
269                         }
270                 }
271                 if (req == SIZE_MAX) {
272                         rte_errno = ENOENT;
273                         return -1;
274                 }
275         } else {
276                 if (check_offset(req, params->size, params->align) < 0) {
277                         rte_errno = EBUSY;
278                         return -1;
279                 }
280         }
281
282         offset = req;
283         mbuf_dynfield_list = RTE_TAILQ_CAST(
284                 mbuf_dynfield_tailq.head, mbuf_dynfield_list);
285
286         te = rte_zmalloc("MBUF_DYNFIELD_TAILQ_ENTRY", sizeof(*te), 0);
287         if (te == NULL) {
288                 rte_errno = ENOMEM;
289                 return -1;
290         }
291
292         mbuf_dynfield = rte_zmalloc("mbuf_dynfield", sizeof(*mbuf_dynfield), 0);
293         if (mbuf_dynfield == NULL) {
294                 rte_free(te);
295                 rte_errno = ENOMEM;
296                 return -1;
297         }
298
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);
304                 rte_free(te);
305                 return -1;
306         }
307         memcpy(&mbuf_dynfield->params, params, sizeof(mbuf_dynfield->params));
308         mbuf_dynfield->offset = offset;
309         te->data = mbuf_dynfield;
310
311         TAILQ_INSERT_TAIL(mbuf_dynfield_list, te, next);
312
313         for (i = offset; i < offset + params->size; i++)
314                 shm->free_space[i] = 0;
315         process_score();
316
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,
319                 offset);
320
321         return offset;
322 }
323
324 int
325 rte_mbuf_dynfield_register_offset(const struct rte_mbuf_dynfield *params,
326                                 size_t req)
327 {
328         int ret;
329
330         if (params->size >= sizeof(struct rte_mbuf)) {
331                 rte_errno = EINVAL;
332                 return -1;
333         }
334         if (!rte_is_power_of_2(params->align)) {
335                 rte_errno = EINVAL;
336                 return -1;
337         }
338         if (params->flags != 0) {
339                 rte_errno = EINVAL;
340                 return -1;
341         }
342
343         rte_mcfg_tailq_write_lock();
344         ret = __rte_mbuf_dynfield_register_offset(params, req);
345         rte_mcfg_tailq_write_unlock();
346
347         return ret;
348 }
349
350 int
351 rte_mbuf_dynfield_register(const struct rte_mbuf_dynfield *params)
352 {
353         return rte_mbuf_dynfield_register_offset(params, SIZE_MAX);
354 }
355
356 /* assume tailq is locked */
357 static struct mbuf_dynflag_elt *
358 __mbuf_dynflag_lookup(const char *name)
359 {
360         struct mbuf_dynflag_list *mbuf_dynflag_list;
361         struct mbuf_dynflag_elt *mbuf_dynflag;
362         struct rte_tailq_entry *te;
363
364         mbuf_dynflag_list = RTE_TAILQ_CAST(
365                 mbuf_dynflag_tailq.head, mbuf_dynflag_list);
366
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)
371                         break;
372         }
373
374         if (te == NULL) {
375                 rte_errno = ENOENT;
376                 return NULL;
377         }
378
379         return mbuf_dynflag;
380 }
381
382 int
383 rte_mbuf_dynflag_lookup(const char *name,
384                         struct rte_mbuf_dynflag *params)
385 {
386         struct mbuf_dynflag_elt *mbuf_dynflag;
387
388         if (shm == NULL) {
389                 rte_errno = ENOENT;
390                 return -1;
391         }
392
393         rte_mcfg_tailq_read_lock();
394         mbuf_dynflag = __mbuf_dynflag_lookup(name);
395         rte_mcfg_tailq_read_unlock();
396
397         if (mbuf_dynflag == NULL) {
398                 rte_errno = ENOENT;
399                 return -1;
400         }
401
402         if (params != NULL)
403                 memcpy(params, &mbuf_dynflag->params, sizeof(*params));
404
405         return mbuf_dynflag->bitnum;
406 }
407
408 static int mbuf_dynflag_cmp(const struct rte_mbuf_dynflag *params1,
409                 const struct rte_mbuf_dynflag *params2)
410 {
411         if (strcmp(params1->name, params2->name))
412                 return -1;
413         if (params1->flags != params2->flags)
414                 return -1;
415         return 0;
416 }
417
418 /* assume tailq is locked */
419 static int
420 __rte_mbuf_dynflag_register_bitnum(const struct rte_mbuf_dynflag *params,
421                                 unsigned int req)
422 {
423         struct mbuf_dynflag_list *mbuf_dynflag_list;
424         struct mbuf_dynflag_elt *mbuf_dynflag = NULL;
425         struct rte_tailq_entry *te = NULL;
426         unsigned int bitnum;
427         int ret;
428
429         if (shm == NULL && init_shared_mem() < 0)
430                 return -1;
431
432         mbuf_dynflag = __mbuf_dynflag_lookup(params->name);
433         if (mbuf_dynflag != NULL) {
434                 if (req != UINT_MAX && req != mbuf_dynflag->bitnum) {
435                         rte_errno = EEXIST;
436                         return -1;
437                 }
438                 if (mbuf_dynflag_cmp(params, &mbuf_dynflag->params) < 0) {
439                         rte_errno = EEXIST;
440                         return -1;
441                 }
442                 return mbuf_dynflag->bitnum;
443         }
444
445         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
446                 rte_errno = EPERM;
447                 return -1;
448         }
449
450         if (req == UINT_MAX) {
451                 if (shm->free_flags == 0) {
452                         rte_errno = ENOENT;
453                         return -1;
454                 }
455                 bitnum = rte_bsf64(shm->free_flags);
456         } else {
457                 if ((shm->free_flags & (1ULL << req)) == 0) {
458                         rte_errno = EBUSY;
459                         return -1;
460                 }
461                 bitnum = req;
462         }
463
464         mbuf_dynflag_list = RTE_TAILQ_CAST(
465                 mbuf_dynflag_tailq.head, mbuf_dynflag_list);
466
467         te = rte_zmalloc("MBUF_DYNFLAG_TAILQ_ENTRY", sizeof(*te), 0);
468         if (te == NULL) {
469                 rte_errno = ENOMEM;
470                 return -1;
471         }
472
473         mbuf_dynflag = rte_zmalloc("mbuf_dynflag", sizeof(*mbuf_dynflag), 0);
474         if (mbuf_dynflag == NULL) {
475                 rte_free(te);
476                 rte_errno = ENOMEM;
477                 return -1;
478         }
479
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);
484                 rte_free(te);
485                 rte_errno = ENAMETOOLONG;
486                 return -1;
487         }
488         mbuf_dynflag->bitnum = bitnum;
489         te->data = mbuf_dynflag;
490
491         TAILQ_INSERT_TAIL(mbuf_dynflag_list, te, next);
492
493         shm->free_flags &= ~(1ULL << bitnum);
494
495         RTE_LOG(DEBUG, MBUF, "Registered dynamic flag %s (fl=0x%x) -> %u\n",
496                 params->name, params->flags, bitnum);
497
498         return bitnum;
499 }
500
501 int
502 rte_mbuf_dynflag_register_bitnum(const struct rte_mbuf_dynflag *params,
503                                 unsigned int req)
504 {
505         int ret;
506
507         if (req >= RTE_SIZEOF_FIELD(struct rte_mbuf, ol_flags) * CHAR_BIT &&
508                         req != UINT_MAX) {
509                 rte_errno = EINVAL;
510                 return -1;
511         }
512
513         rte_mcfg_tailq_write_lock();
514         ret = __rte_mbuf_dynflag_register_bitnum(params, req);
515         rte_mcfg_tailq_write_unlock();
516
517         return ret;
518 }
519
520 int
521 rte_mbuf_dynflag_register(const struct rte_mbuf_dynflag *params)
522 {
523         return rte_mbuf_dynflag_register_bitnum(params, UINT_MAX);
524 }
525
526 void rte_mbuf_dyn_dump(FILE *out)
527 {
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;
533         size_t i;
534
535         rte_mcfg_tailq_write_lock();
536         init_shared_mem();
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);
546         }
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);
555         }
556         fprintf(out, "Free space in mbuf (0 = occupied, value = free zone alignment):\n");
557         for (i = 0; i < sizeof(struct rte_mbuf); i++) {
558                 if ((i % 8) == 0)
559                         fprintf(out, "  %4.4zx: ", i);
560                 fprintf(out, "%2.2x%s", shm->free_space[i],
561                         (i % 8 != 7) ? " " : "\n");
562         }
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++) {
565                 if ((i % 8) == 0)
566                         fprintf(out, "  %4.4zx: ", i);
567                 fprintf(out, "%1.1x%s", (shm->free_flags & (1ULL << i)) ? 1 : 0,
568                         (i % 8 != 7) ? " " : "\n");
569         }
570
571         rte_mcfg_tailq_write_unlock();
572 }
573
574 static int
575 rte_mbuf_dyn_timestamp_register(int *field_offset, uint64_t *flag,
576                 const char *direction, const char *flag_name)
577 {
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),
582         };
583         struct rte_mbuf_dynflag flag_desc = {};
584         int offset;
585
586         offset = rte_mbuf_dynfield_register(&field_desc);
587         if (offset < 0) {
588                 RTE_LOG(ERR, MBUF,
589                         "Failed to register mbuf field for timestamp\n");
590                 return -1;
591         }
592         if (field_offset != NULL)
593                 *field_offset = offset;
594
595         strlcpy(flag_desc.name, flag_name, sizeof(flag_desc.name));
596         offset = rte_mbuf_dynflag_register(&flag_desc);
597         if (offset < 0) {
598                 RTE_LOG(ERR, MBUF,
599                         "Failed to register mbuf flag for %s timestamp\n",
600                         direction);
601                 return -1;
602         }
603         if (flag != NULL)
604                 *flag = RTE_BIT64(offset);
605
606         return 0;
607 }
608
609 int
610 rte_mbuf_dyn_rx_timestamp_register(int *field_offset, uint64_t *rx_flag)
611 {
612         return rte_mbuf_dyn_timestamp_register(field_offset, rx_flag,
613                         "Rx", RTE_MBUF_DYNFLAG_RX_TIMESTAMP_NAME);
614 }