From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2C913A2EDB for ; Tue, 1 Oct 2019 12:49:48 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 4058C4C8D; Tue, 1 Oct 2019 12:49:47 +0200 (CEST) Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by dpdk.org (Postfix) with ESMTP id 5914449DF for ; Tue, 1 Oct 2019 12:49:44 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Oct 2019 03:49:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,570,1559545200"; d="scan'208";a="274958390" Received: from irsmsx104.ger.corp.intel.com ([163.33.3.159]) by orsmga001.jf.intel.com with ESMTP; 01 Oct 2019 03:49:41 -0700 Received: from irsmsx105.ger.corp.intel.com ([169.254.7.164]) by IRSMSX104.ger.corp.intel.com ([169.254.5.103]) with mapi id 14.03.0439.000; Tue, 1 Oct 2019 11:49:40 +0100 From: "Ananyev, Konstantin" To: Olivier Matz , "dev@dpdk.org" CC: Thomas Monjalon , "Wang, Haiyue" , Stephen Hemminger , Andrew Rybchenko , "Wiles, Keith" , Jerin Jacob Kollanukkaran Thread-Topic: [dpdk-dev] [PATCH] mbuf: support dynamic fields and flags Thread-Index: AQHVbkHY1JuInOseYE2F0xUhHJUX86dEXv8Q Date: Tue, 1 Oct 2019 10:49:39 +0000 Message-ID: <2601191342CEEE43887BDE71AB977258019196E0B7@irsmsx105.ger.corp.intel.com> References: <20190710092907.5565-1-olivier.matz@6wind.com> <20190918165448.22409-1-olivier.matz@6wind.com> In-Reply-To: <20190918165448.22409-1-olivier.matz@6wind.com> Accept-Language: en-IE, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiNTQ0ZWEwNmYtMTk3Zi00YTFkLWFlN2QtNjlmNWRkMWZmMDYyIiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX05UIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE3LjEwLjE4MDQuNDkiLCJUcnVzdGVkTGFiZWxIYXNoIjoiUFNuWElWSVVjY3pIZXhYK0E5SnR3MTBqUlpPbWFQZW15QmpHcng4M0NUNlBTdFRTRWhzd2lwSmxJQWk0M1pPUiJ9 x-ctpclassification: CTP_NT dlp-product: dlpe-windows dlp-version: 11.2.0.6 dlp-reaction: no-action x-originating-ip: [163.33.239.180] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: [dpdk-dev] [PATCH] mbuf: support dynamic fields and flags X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Hi Olivier, > Many features require to store data inside the mbuf. As the room in mbuf > structure is limited, it is not possible to have a field for each > feature. Also, changing fields in the mbuf structure can break the API > or ABI. >=20 > This commit addresses these issues, by enabling the dynamic registration > of fields or flags: >=20 > - a dynamic field is a named area in the rte_mbuf structure, with a > given size (>=3D 1 byte) and alignment constraint. > - a dynamic flag is a named bit in the rte_mbuf structure. >=20 > The typical use case is a PMD that registers space for an offload > feature, when the application requests to enable this feature. As > the space in mbuf is limited, the space should only be reserved if it > is going to be used (i.e when the application explicitly asks for it). >=20 > The registration can be done at any moment, but it is not possible > to unregister fields or flags for now. Looks ok to me in general. Some comments/suggestions inline. Konstantin >=20 > Signed-off-by: Olivier Matz > Acked-by: Thomas Monjalon > --- >=20 > rfc -> v1 >=20 > * Rebase on top of master > * Change registration API to use a structure instead of > variables, getting rid of #defines (Stephen's comment) > * Update flag registration to use a similar API as fields. > * Change max name length from 32 to 64 (sugg. by Thomas) > * Enhance API documentation (Haiyue's and Andrew's comments) > * Add a debug log at registration > * Add some words in release note > * Did some performance tests (sugg. by Andrew): > On my platform, reading a dynamic field takes ~3 cycles more > than a static field, and ~2 cycles more for writing. >=20 > app/test/test_mbuf.c | 114 ++++++- > doc/guides/rel_notes/release_19_11.rst | 7 + > lib/librte_mbuf/Makefile | 2 + > lib/librte_mbuf/meson.build | 6 +- > lib/librte_mbuf/rte_mbuf.h | 25 +- > lib/librte_mbuf/rte_mbuf_dyn.c | 408 +++++++++++++++++++++++++ > lib/librte_mbuf/rte_mbuf_dyn.h | 163 ++++++++++ > lib/librte_mbuf/rte_mbuf_version.map | 4 + > 8 files changed, 724 insertions(+), 5 deletions(-) > create mode 100644 lib/librte_mbuf/rte_mbuf_dyn.c > create mode 100644 lib/librte_mbuf/rte_mbuf_dyn.h >=20 > --- a/lib/librte_mbuf/rte_mbuf.h > +++ b/lib/librte_mbuf/rte_mbuf.h > @@ -198,9 +198,12 @@ extern "C" { > #define PKT_RX_OUTER_L4_CKSUM_GOOD (1ULL << 22) > #define PKT_RX_OUTER_L4_CKSUM_INVALID ((1ULL << 21) | (1ULL << 22)) >=20 > -/* add new RX flags here */ > +/* add new RX flags here, don't forget to update PKT_FIRST_FREE */ >=20 > -/* add new TX flags here */ > +#define PKT_FIRST_FREE (1ULL << 23) > +#define PKT_LAST_FREE (1ULL << 39) > + > +/* add new TX flags here, don't forget to update PKT_LAST_FREE */ >=20 > /** > * Indicate that the metadata field in the mbuf is in use. > @@ -738,6 +741,8 @@ struct rte_mbuf { > */ > struct rte_mbuf_ext_shared_info *shinfo; >=20 > + uint64_t dynfield1; /**< Reserved for dynamic fields. */ > + uint64_t dynfield2; /**< Reserved for dynamic fields. */ Wonder why just not one field: union { uint8_t u8[16]; ... uint64_t u64[2]; } dyn_field1; ? Probably would be a bit handy, to refer, register, etc. no? > } __rte_cache_aligned; >=20 > /** > @@ -1684,6 +1689,21 @@ rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void= *buf_addr, > */ > #define rte_pktmbuf_detach_extbuf(m) rte_pktmbuf_detach(m) >=20 > +/** > + * Copy dynamic fields from m_src to m_dst. > + * > + * @param m_dst > + * The destination mbuf. > + * @param m_src > + * The source mbuf. > + */ > +static inline void > +rte_mbuf_dynfield_copy(struct rte_mbuf *m_dst, const struct rte_mbuf *m_= src) > +{ > + m_dst->dynfield1 =3D m_src->dynfield1; > + m_dst->dynfield2 =3D m_src->dynfield2; > +} > + > /** > * Attach packet mbuf to another packet mbuf. > * > @@ -1732,6 +1752,7 @@ static inline void rte_pktmbuf_attach(struct rte_mb= uf *mi, struct rte_mbuf *m) > mi->vlan_tci_outer =3D m->vlan_tci_outer; > mi->tx_offload =3D m->tx_offload; > mi->hash =3D m->hash; > + rte_mbuf_dynfield_copy(mi, m); >=20 > mi->next =3D NULL; > mi->pkt_len =3D mi->data_len; > diff --git a/lib/librte_mbuf/rte_mbuf_dyn.c b/lib/librte_mbuf/rte_mbuf_dy= n.c > new file mode 100644 > index 000000000..13b8742d0 > --- /dev/null > +++ b/lib/librte_mbuf/rte_mbuf_dyn.c > @@ -0,0 +1,408 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright 2019 6WIND S.A. > + */ > + > +#include > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define RTE_MBUF_DYN_MZNAME "rte_mbuf_dyn" > + > +struct mbuf_dynfield_elt { > + TAILQ_ENTRY(mbuf_dynfield_elt) next; > + struct rte_mbuf_dynfield params; > + int offset; Why not 'size_t offset', to avoid any explicit conversions, etc? > +}; > +TAILQ_HEAD(mbuf_dynfield_list, rte_tailq_entry); > + > +static struct rte_tailq_elem mbuf_dynfield_tailq =3D { > + .name =3D "RTE_MBUF_DYNFIELD", > +}; > +EAL_REGISTER_TAILQ(mbuf_dynfield_tailq); > + > +struct mbuf_dynflag_elt { > + TAILQ_ENTRY(mbuf_dynflag_elt) next; > + struct rte_mbuf_dynflag params; > + int bitnum; > +}; > +TAILQ_HEAD(mbuf_dynflag_list, rte_tailq_entry); > + > +static struct rte_tailq_elem mbuf_dynflag_tailq =3D { > + .name =3D "RTE_MBUF_DYNFLAG", > +}; > +EAL_REGISTER_TAILQ(mbuf_dynflag_tailq); > + > +struct mbuf_dyn_shm { > + /** For each mbuf byte, free_space[i] =3D=3D 1 if space is free. */ > + uint8_t free_space[sizeof(struct rte_mbuf)]; > + /** Bitfield of available flags. */ > + uint64_t free_flags; > +}; > +static struct mbuf_dyn_shm *shm; > + > +/* allocate and initialize the shared memory */ > +static int > +init_shared_mem(void) > +{ > + const struct rte_memzone *mz; > + uint64_t mask; > + > + if (rte_eal_process_type() =3D=3D RTE_PROC_PRIMARY) { > + mz =3D rte_memzone_reserve_aligned(RTE_MBUF_DYN_MZNAME, > + sizeof(struct mbuf_dyn_shm), > + SOCKET_ID_ANY, 0, > + RTE_CACHE_LINE_SIZE); > + } else { > + mz =3D rte_memzone_lookup(RTE_MBUF_DYN_MZNAME); > + } > + if (mz =3D=3D NULL) > + return -1; > + > + shm =3D mz->addr; > + > +#define mark_free(field) \ > + memset(&shm->free_space[offsetof(struct rte_mbuf, field)], \ > + 0xff, sizeof(((struct rte_mbuf *)0)->field)) I think you can avoid defining/unedifying macros here by something like tha= t: static const struct { size_t offset; size_t size; } dyn_syms[] =3D { [0] =3D {.offset =3D offsetof(struct rte_mbuf, dynfield1), sizeof((stru= ct rte_mbuf *)0)->dynfield1), [1] =3D {.offset =3D offsetof(struct rte_mbuf, dynfield2), sizeof((stru= ct rte_mbuf *)0)->dynfield2), }; ... for (i =3D 0; i !=3D RTE_DIM(dyn_syms); i++) memset(shm->free_space + dym_syms[i].offset, UINT8_MAX, dym_syms[i].siz= e); > + > + if (rte_eal_process_type() =3D=3D RTE_PROC_PRIMARY) { > + /* init free_space, keep it sync'd with > + * rte_mbuf_dynfield_copy(). > + */ > + memset(shm, 0, sizeof(*shm)); > + mark_free(dynfield1); > + mark_free(dynfield2); > + > + /* init free_flags */ > + for (mask =3D PKT_FIRST_FREE; mask <=3D PKT_LAST_FREE; mask <<=3D 1) > + shm->free_flags |=3D mask; > + } > +#undef mark_free > + > + return 0; > +} > + > +/* check if this offset can be used */ > +static int > +check_offset(size_t offset, size_t size, size_t align, unsigned int flag= s) > +{ > + size_t i; > + > + (void)flags; We have RTE_SET_USED() for such cases... Though as it is an internal function probably better not to introduce unused parameters at all. > + > + if ((offset & (align - 1)) !=3D 0) > + return -1; > + if (offset + size > sizeof(struct rte_mbuf)) > + return -1; > + > + for (i =3D 0; i < size; i++) { > + if (!shm->free_space[i + offset]) > + return -1; > + } > + > + return 0; > +} > + > +/* assume tailq is locked */ > +static struct mbuf_dynfield_elt * > +__mbuf_dynfield_lookup(const char *name) > +{ > + struct mbuf_dynfield_list *mbuf_dynfield_list; > + struct mbuf_dynfield_elt *mbuf_dynfield; > + struct rte_tailq_entry *te; > + > + mbuf_dynfield_list =3D RTE_TAILQ_CAST( > + mbuf_dynfield_tailq.head, mbuf_dynfield_list); > + > + TAILQ_FOREACH(te, mbuf_dynfield_list, next) { > + mbuf_dynfield =3D (struct mbuf_dynfield_elt *)te->data; > + if (strcmp(name, mbuf_dynfield->params.name) =3D=3D 0) > + break; > + } > + > + if (te =3D=3D NULL) { > + rte_errno =3D ENOENT; > + return NULL; > + } > + > + return mbuf_dynfield; > +} > + > +int > +rte_mbuf_dynfield_lookup(const char *name, struct rte_mbuf_dynfield *par= ams) > +{ > + struct mbuf_dynfield_elt *mbuf_dynfield; > + > + if (shm =3D=3D NULL) { > + rte_errno =3D ENOENT; > + return -1; > + } > + > + rte_mcfg_tailq_read_lock(); > + mbuf_dynfield =3D __mbuf_dynfield_lookup(name); > + rte_mcfg_tailq_read_unlock(); > + > + if (mbuf_dynfield =3D=3D NULL) { > + rte_errno =3D ENOENT; > + return -1; > + } > + > + if (params !=3D NULL) > + memcpy(params, &mbuf_dynfield->params, sizeof(*params)); > + > + return mbuf_dynfield->offset; > +} > + > +static int mbuf_dynfield_cmp(const struct rte_mbuf_dynfield *params1, > + const struct rte_mbuf_dynfield *params2) > +{ > + if (strcmp(params1->name, params2->name)) > + return -1; > + if (params1->size !=3D params2->size) > + return -1; > + if (params1->align !=3D params2->align) > + return -1; > + if (params1->flags !=3D params2->flags) > + return -1; > + return 0; > +} > + > +int > +rte_mbuf_dynfield_register(const struct rte_mbuf_dynfield *params) What I meant at user-space - if we can also have another function that woul= d allow user to specify required offset for dynfield explicitly, then user can defi= ne it as constant value and let compiler do optimization work and hopefully generate faster c= ode to access this field. Something like that: int rte_mbuf_dynfiled_register_offset(const struct rte_mbuf_dynfield *param= s, size_t offset); #define RTE_MBUF_DYNFIELD_OFFSET(fld, off) (offsetof(struct rte_mbuf, fld)= + (off)) And then somewhere in user code: /* to let say reserve first 4B in dynfield1*/ #define MBUF_DYNFIELD_A RTE_MBUF_DYNFIELD_OFFSET(dynfiled1, 0) ... params.name =3D RTE_STR(MBUF_DYNFIELD_A); params.size =3D sizeof(uint32_t); params.align =3D sizeof(uint32_t); ret =3D rte_mbuf_dynfiled_register_offset(¶ms, MBUF_DYNFIELD_A); if (ret !=3D MBUF_DYNFIELD_A) { /* handle it somehow, probably just terminate gracefully... */ } ... /* to let say reserve last 2B in dynfield2*/ #define MBUF_DYNFIELD_B RTE_MBUF_DYNFIELD_OFFSET(dynfiled2, 6) ... params.name =3D RTE_STR(MBUF_DYNFIELD_B); params.size =3D sizeof(uint16_t); params.align =3D sizeof(uint16_t); ret =3D rte_mbuf_dynfiled_register_offset(¶ms, MBUF_DYNFIELD_B); After that user can use constant offsets MBUF_DYNFIELD_A/ MBUF_DYNFIELD_B to access these fields. Same thoughts for DYNFLAG. > +{ > + struct mbuf_dynfield_list *mbuf_dynfield_list; > + struct mbuf_dynfield_elt *mbuf_dynfield =3D NULL; > + struct rte_tailq_entry *te =3D NULL; > + int offset, ret; size_t offset to avoid explicit conversions, etc.? > + size_t i; > + > + if (shm =3D=3D NULL && init_shared_mem() < 0) > + goto fail; As I understand, here you allocate/initialize your shm without any lock pro= tection, though later you protect it via rte_mcfg_tailq_write_lock(). That seems a bit flakey to me. Why not to store information about free dynfield bytes inside mbuf_dynfield= _tailq? Let say at init() create and add an entry into that list with some reserve= d name. Then at register - grab mcfg_tailq_write_lock and do lookup for such entry and then read/update it as needed. It would help to avoid racing problem, plus you wouldn't need to allocate/lookup for memzone. =20 > + if (params->size >=3D sizeof(struct rte_mbuf)) { > + rte_errno =3D EINVAL; > + goto fail; > + } > + if (!rte_is_power_of_2(params->align)) { > + rte_errno =3D EINVAL; > + goto fail; > + } > + if (params->flags !=3D 0) { > + rte_errno =3D EINVAL; > + goto fail; > + } > + > + rte_mcfg_tailq_write_lock(); > + I think it probably would be cleaner and easier to read/maintain, if you'll= put actual code under lock protection into a separate function - as you did for __mbuf= _dynfield_lookup(). > + mbuf_dynfield =3D __mbuf_dynfield_lookup(params->name); > + if (mbuf_dynfield !=3D NULL) { > + if (mbuf_dynfield_cmp(params, &mbuf_dynfield->params) < 0) { > + rte_errno =3D EEXIST; > + goto fail_unlock; > + } > + offset =3D mbuf_dynfield->offset; > + goto out_unlock; > + } > + > + if (rte_eal_process_type() !=3D RTE_PROC_PRIMARY) { > + rte_errno =3D EPERM; > + goto fail_unlock; > + } > + > + for (offset =3D 0; > + offset < (int)sizeof(struct rte_mbuf); > + offset++) { > + if (check_offset(offset, params->size, params->align, > + params->flags) =3D=3D 0) > + break; > + } > + > + if (offset =3D=3D sizeof(struct rte_mbuf)) { > + rte_errno =3D ENOENT; > + goto fail_unlock; > + } > + > + mbuf_dynfield_list =3D RTE_TAILQ_CAST( > + mbuf_dynfield_tailq.head, mbuf_dynfield_list); > + > + te =3D rte_zmalloc("MBUF_DYNFIELD_TAILQ_ENTRY", sizeof(*te), 0); > + if (te =3D=3D NULL) > + goto fail_unlock; > + > + mbuf_dynfield =3D rte_zmalloc("mbuf_dynfield", sizeof(*mbuf_dynfield), = 0); > + if (mbuf_dynfield =3D=3D NULL) > + goto fail_unlock; > + > + ret =3D strlcpy(mbuf_dynfield->params.name, params->name, > + sizeof(mbuf_dynfield->params.name)); > + if (ret < 0 || ret >=3D (int)sizeof(mbuf_dynfield->params.name)) { > + rte_errno =3D ENAMETOOLONG; > + goto fail_unlock; > + } > + memcpy(&mbuf_dynfield->params, params, sizeof(mbuf_dynfield->params)); > + mbuf_dynfield->offset =3D offset; > + te->data =3D mbuf_dynfield; > + > + TAILQ_INSERT_TAIL(mbuf_dynfield_list, te, next); > + > + for (i =3D offset; i < offset + params->size; i++) > + shm->free_space[i] =3D 0; > + > + RTE_LOG(DEBUG, MBUF, "Registered dynamic field %s (sz=3D%zu, al=3D%zu, = fl=3D0x%x) -> %d\n", > + params->name, params->size, params->align, params->flags, > + offset); > + > +out_unlock: > + rte_mcfg_tailq_write_unlock(); > + > + return offset; > + > +fail_unlock: > + rte_mcfg_tailq_write_unlock(); > +fail: > + rte_free(mbuf_dynfield); > + rte_free(te); > + return -1; > +} > + > +/* assume tailq is locked */ > +static struct mbuf_dynflag_elt * > +__mbuf_dynflag_lookup(const char *name) > +{ > + struct mbuf_dynflag_list *mbuf_dynflag_list; > + struct mbuf_dynflag_elt *mbuf_dynflag; > + struct rte_tailq_entry *te; > + > + mbuf_dynflag_list =3D RTE_TAILQ_CAST( > + mbuf_dynflag_tailq.head, mbuf_dynflag_list); > + > + TAILQ_FOREACH(te, mbuf_dynflag_list, next) { > + mbuf_dynflag =3D (struct mbuf_dynflag_elt *)te->data; > + if (strncmp(name, mbuf_dynflag->params.name, > + RTE_MBUF_DYN_NAMESIZE) =3D=3D 0) > + break; > + } > + > + if (te =3D=3D NULL) { > + rte_errno =3D ENOENT; > + return NULL; > + } > + > + return mbuf_dynflag; > +} > + > +int > +rte_mbuf_dynflag_lookup(const char *name, > + struct rte_mbuf_dynflag *params) > +{ > + struct mbuf_dynflag_elt *mbuf_dynflag; > + > + if (shm =3D=3D NULL) { > + rte_errno =3D ENOENT; > + return -1; > + } > + > + rte_mcfg_tailq_read_lock(); > + mbuf_dynflag =3D __mbuf_dynflag_lookup(name); > + rte_mcfg_tailq_read_unlock(); > + > + if (mbuf_dynflag =3D=3D NULL) { > + rte_errno =3D ENOENT; > + return -1; > + } > + > + if (params !=3D NULL) > + memcpy(params, &mbuf_dynflag->params, sizeof(*params)); > + > + return mbuf_dynflag->bitnum; > +} > + > +static int mbuf_dynflag_cmp(const struct rte_mbuf_dynflag *params1, > + const struct rte_mbuf_dynflag *params2) > +{ > + if (strcmp(params1->name, params2->name)) > + return -1; > + if (params1->flags !=3D params2->flags) > + return -1; > + return 0; > +} > + > +int > +rte_mbuf_dynflag_register(const struct rte_mbuf_dynflag *params) > +{ > + struct mbuf_dynflag_list *mbuf_dynflag_list; > + struct mbuf_dynflag_elt *mbuf_dynflag =3D NULL; > + struct rte_tailq_entry *te =3D NULL; > + int bitnum, ret; > + > + if (shm =3D=3D NULL && init_shared_mem() < 0) > + goto fail; > + > + rte_mcfg_tailq_write_lock(); > + > + mbuf_dynflag =3D __mbuf_dynflag_lookup(params->name); > + if (mbuf_dynflag !=3D NULL) { > + if (mbuf_dynflag_cmp(params, &mbuf_dynflag->params) < 0) { > + rte_errno =3D EEXIST; > + goto fail_unlock; > + } > + bitnum =3D mbuf_dynflag->bitnum; > + goto out_unlock; > + } > + > + if (rte_eal_process_type() !=3D RTE_PROC_PRIMARY) { > + rte_errno =3D EPERM; > + goto fail_unlock; > + } > + > + if (shm->free_flags =3D=3D 0) { > + rte_errno =3D ENOENT; > + goto fail_unlock; > + } > + bitnum =3D rte_bsf64(shm->free_flags); > + > + mbuf_dynflag_list =3D RTE_TAILQ_CAST( > + mbuf_dynflag_tailq.head, mbuf_dynflag_list); > + > + te =3D rte_zmalloc("MBUF_DYNFLAG_TAILQ_ENTRY", sizeof(*te), 0); > + if (te =3D=3D NULL) > + goto fail_unlock; > + > + mbuf_dynflag =3D rte_zmalloc("mbuf_dynflag", sizeof(*mbuf_dynflag), 0); > + if (mbuf_dynflag =3D=3D NULL) > + goto fail_unlock; > + > + ret =3D strlcpy(mbuf_dynflag->params.name, params->name, > + sizeof(mbuf_dynflag->params.name)); > + if (ret < 0 || ret >=3D (int)sizeof(mbuf_dynflag->params.name)) { > + rte_errno =3D ENAMETOOLONG; > + goto fail_unlock; > + } > + mbuf_dynflag->bitnum =3D bitnum; > + te->data =3D mbuf_dynflag; > + > + TAILQ_INSERT_TAIL(mbuf_dynflag_list, te, next); > + > + shm->free_flags &=3D ~(1ULL << bitnum); > + > + RTE_LOG(DEBUG, MBUF, "Registered dynamic flag %s (fl=3D0x%x) -> %u\n", > + params->name, params->flags, bitnum); > + > +out_unlock: > + rte_mcfg_tailq_write_unlock(); > + > + return bitnum; > + > +fail_unlock: > + rte_mcfg_tailq_write_unlock(); > +fail: > + rte_free(mbuf_dynflag); > + rte_free(te); > + return -1; > +} > diff --git a/lib/librte_mbuf/rte_mbuf_dyn.h b/lib/librte_mbuf/rte_mbuf_dy= n.h > new file mode 100644 > index 000000000..6e2c81654 > --- /dev/null > +++ b/lib/librte_mbuf/rte_mbuf_dyn.h > @@ -0,0 +1,163 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright 2019 6WIND S.A. > + */ > + > +#ifndef _RTE_MBUF_DYN_H_ > +#define _RTE_MBUF_DYN_H_ > + > +/** > + * @file > + * RTE Mbuf dynamic fields and flags > + * > + * Many features require to store data inside the mbuf. As the room in > + * mbuf structure is limited, it is not possible to have a field for > + * each feature. Also, changing fields in the mbuf structure can break > + * the API or ABI. > + * > + * This module addresses this issue, by enabling the dynamic > + * registration of fields or flags: > + * > + * - a dynamic field is a named area in the rte_mbuf structure, with a > + * given size (>=3D 1 byte) and alignment constraint. > + * - a dynamic flag is a named bit in the rte_mbuf structure, stored > + * in mbuf->ol_flags. > + * > + * The typical use case is when a specific offload feature requires to > + * register a dedicated offload field in the mbuf structure, and adding > + * a static field or flag is not justified. > + * > + * Example of use: > + * > + * - A rte_mbuf_dynfield structure is defined, containing the parameters > + * of the dynamic field to be registered: > + * const struct rte_mbuf_dynfield rte_dynfield_my_feature =3D { ... }; > + * - The application initializes the PMD, and asks for this feature > + * at port initialization by passing DEV_RX_OFFLOAD_MY_FEATURE in > + * rxconf. This will make the PMD to register the field by calling > + * rte_mbuf_dynfield_register(&rte_dynfield_my_feature). The PMD > + * stores the returned offset. > + * - The application that uses the offload feature also registers > + * the field to retrieve the same offset. > + * - When the PMD receives a packet, it can set the field: > + * *RTE_MBUF_DYNFIELD(m, offset, ) =3D value; > + * - In the main loop, the application can retrieve the value with > + * the same macro. > + * > + * To avoid wasting space, the dynamic fields or flags must only be > + * reserved on demand, when an application asks for the related feature. > + * > + * The registration can be done at any moment, but it is not possible > + * to unregister fields or flags for now. > + * > + * A dynamic field can be reserved and used by an application only. > + * It can for instance be a packet mark. > + */ > + > +#include > +/** > + * Maximum length of the dynamic field or flag string. > + */ > +#define RTE_MBUF_DYN_NAMESIZE 64 > + > +/** > + * Structure describing the parameters of a mbuf dynamic field. > + */ > +struct rte_mbuf_dynfield { > + char name[RTE_MBUF_DYN_NAMESIZE]; /**< Name of the field. */ > + size_t size; /**< The number of bytes to reserve. */ > + size_t align; /**< The alignment constraint (power of 2). */ > + unsigned int flags; /**< Reserved for future use, must be 0. */ > +}; > + > +/** > + * Structure describing the parameters of a mbuf dynamic flag. > + */ > +struct rte_mbuf_dynflag { > + char name[RTE_MBUF_DYN_NAMESIZE]; /**< Name of the dynamic flag. */ > + unsigned int flags; /**< Reserved for future use, must be 0. */ > +}; > + > +/** > + * Register space for a dynamic field in the mbuf structure. > + * > + * If the field is already registered (same name and parameters), its > + * offset is returned. > + * > + * @param params > + * A structure containing the requested parameters (name, size, > + * alignment constraint and flags). > + * @return > + * The offset in the mbuf structure, or -1 on error. > + * Possible values for rte_errno: > + * - EINVAL: invalid parameters (size, align, or flags). > + * - EEXIST: this name is already register with different parameters. > + * - EPERM: called from a secondary process. > + * - ENOENT: not enough room in mbuf. > + * - ENOMEM: allocation failure. > + * - ENAMETOOLONG: name does not ends with \0. > + */ > +__rte_experimental > +int rte_mbuf_dynfield_register(const struct rte_mbuf_dynfield *params); > + > +/** > + * Lookup for a registered dynamic mbuf field. > + * > + * @param name > + * A string identifying the dynamic field. > + * @param params > + * If not NULL, and if the lookup is successful, the structure is > + * filled with the parameters of the dynamic field. > + * @return > + * The offset of this field in the mbuf structure, or -1 on error. > + * Possible values for rte_errno: > + * - ENOENT: no dynamic field matches this name. > + */ > +__rte_experimental > +int rte_mbuf_dynfield_lookup(const char *name, > + struct rte_mbuf_dynfield *params); > + > +/** > + * Register a dynamic flag in the mbuf structure. > + * > + * If the flag is already registered (same name and parameters), its > + * offset is returned. > + * > + * @param params > + * A structure containing the requested parameters of the dynamic > + * flag (name and options). > + * @return > + * The number of the reserved bit, or -1 on error. > + * Possible values for rte_errno: > + * - EINVAL: invalid parameters (size, align, or flags). > + * - EEXIST: this name is already register with different parameters. > + * - EPERM: called from a secondary process. > + * - ENOENT: no more flag available. > + * - ENOMEM: allocation failure. > + * - ENAMETOOLONG: name is longer than RTE_MBUF_DYN_NAMESIZE - 1. > + */ > +__rte_experimental > +int rte_mbuf_dynflag_register(const struct rte_mbuf_dynflag *params); > + > +/** > + * Lookup for a registered dynamic mbuf flag. > + * > + * @param name > + * A string identifying the dynamic flag. > + * @param params > + * If not NULL, and if the lookup is successful, the structure is > + * filled with the parameters of the dynamic flag. > + * @return > + * The offset of this flag in the mbuf structure, or -1 on error. > + * Possible values for rte_errno: > + * - ENOENT: no dynamic flag matches this name. > + */ > +__rte_experimental > +int rte_mbuf_dynflag_lookup(const char *name, > + struct rte_mbuf_dynflag *params); > + > +/** > + * Helper macro to access to a dynamic field. > + */ > +#define RTE_MBUF_DYNFIELD(m, offset, type) ((type)((uintptr_t)(m) + (off= set))) > + > +#endif > diff --git a/lib/librte_mbuf/rte_mbuf_version.map b/lib/librte_mbuf/rte_m= buf_version.map > index 2662a37bf..a98310570 100644 > --- a/lib/librte_mbuf/rte_mbuf_version.map > +++ b/lib/librte_mbuf/rte_mbuf_version.map > @@ -50,4 +50,8 @@ EXPERIMENTAL { > global: >=20 > rte_mbuf_check; > + rte_mbuf_dynfield_lookup; > + rte_mbuf_dynfield_register; > + rte_mbuf_dynflag_lookup; > + rte_mbuf_dynflag_register; > } DPDK_18.08; > -- > 2.20.1