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 14C4FA00E6 for ; Wed, 10 Jul 2019 19:14:41 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 8FF613195; Wed, 10 Jul 2019 19:14:39 +0200 (CEST) Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by dpdk.org (Postfix) with ESMTP id A24EB2F42 for ; Wed, 10 Jul 2019 19:14:37 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Jul 2019 10:14:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.63,475,1557212400"; d="scan'208";a="341132177" Received: from fmsmsx108.amr.corp.intel.com ([10.18.124.206]) by orsmga005.jf.intel.com with ESMTP; 10 Jul 2019 10:14:36 -0700 Received: from fmsmsx158.amr.corp.intel.com (10.18.116.75) by FMSMSX108.amr.corp.intel.com (10.18.124.206) with Microsoft SMTP Server (TLS) id 14.3.439.0; Wed, 10 Jul 2019 10:14:36 -0700 Received: from shsmsx102.ccr.corp.intel.com (10.239.4.154) by fmsmsx158.amr.corp.intel.com (10.18.116.75) with Microsoft SMTP Server (TLS) id 14.3.439.0; Wed, 10 Jul 2019 10:14:36 -0700 Received: from shsmsx101.ccr.corp.intel.com ([169.254.1.134]) by shsmsx102.ccr.corp.intel.com ([169.254.2.3]) with mapi id 14.03.0439.000; Thu, 11 Jul 2019 01:14:33 +0800 From: "Wang, Haiyue" To: Olivier Matz , "dev@dpdk.org" Thread-Topic: [dpdk-dev] [RFC] mbuf: support dynamic fields and flags Thread-Index: AQHVNwH9rBZt164hek6OOjYJj0DjD6bEEJBw Date: Wed, 10 Jul 2019 17:14:33 +0000 Message-ID: References: <20190710092907.5565-1-olivier.matz@6wind.com> In-Reply-To: <20190710092907.5565-1-olivier.matz@6wind.com> Accept-Language: zh-CN, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiZWM3Mzg5NjgtNzIxYi00OTExLWE4ZGItZmU4ODA4MjZjZjM2IiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX05UIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE3LjEwLjE4MDQuNDkiLCJUcnVzdGVkTGFiZWxIYXNoIjoiK1QxVndDK3BhWnRGT1VqMG5DMUFCdGpOUUJtVVwvUkdoY1VFNU1JTmFmRHczbW1UOHpvTzR2dTlEQjRsSGJXZWwifQ== x-ctpclassification: CTP_NT dlp-product: dlpe-windows dlp-version: 11.2.0.6 dlp-reaction: no-action x-originating-ip: [10.239.127.40] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: [dpdk-dev] [RFC] 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, Sounds cool, just have some questions inline. > -----Original Message----- > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Olivier Matz > Sent: Wednesday, July 10, 2019 17:29 > To: dev@dpdk.org > Subject: [dpdk-dev] [RFC] mbuf: support dynamic fields and flags >=20 > 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. >=20 > Signed-off-by: Olivier Matz > --- > app/test/test_mbuf.c | 83 +++++++- > 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 | 373 +++++++++++++++++++++++++++++= ++++++ > lib/librte_mbuf/rte_mbuf_dyn.h | 119 +++++++++++ > lib/librte_mbuf/rte_mbuf_version.map | 4 + > 7 files changed, 607 insertions(+), 5 deletions(-) > create mode 100644 lib/librte_mbuf/rte_mbuf_dyn.c > create mode 100644 lib/librte_mbuf/rte_mbuf_dyn.h >=20 > diff --git a/app/test/test_mbuf.c b/app/test/test_mbuf.c > index 2a97afe20..8008cc766 100644 > --- a/app/test/test_mbuf.c > +++ b/app/test/test_mbuf.c > @@ -28,6 +28,7 @@ > #include > #include > #include > +#include >=20 > #include "test.h" >=20 > @@ -502,7 +503,6 @@ test_attach_from_different_pool(struct rte_mempool *p= ktmbuf_pool, > rte_pktmbuf_free(clone2); > return -1; > } > -#undef GOTO_FAIL >=20 > /* > * test allocation and free of mbufs > @@ -1122,6 +1122,81 @@ test_tx_offload(void) > } >=20 > static int > +test_mbuf_dyn(struct rte_mempool *pktmbuf_pool) > +{ > + struct rte_mbuf *m =3D NULL; > + int offset, offset2; > + int flag, flag2; > + > + offset =3D rte_mbuf_dynfield_register("test-dynfield", sizeof(uint8_t), > + __alignof__(uint8_t), 0); > + if (offset =3D=3D -1) > + GOTO_FAIL("failed to register dynamic field, offset=3D%d: %s", > + offset, strerror(errno)); > + > + offset2 =3D rte_mbuf_dynfield_register("test-dynfield", sizeof(uint8_t)= , > + __alignof__(uint8_t), 0); > + if (offset2 !=3D offset) > + GOTO_FAIL("failed to lookup dynamic field, offset=3D%d, offset2=3D%d: = %s", > + offset, offset2, strerror(errno)); > + > + offset2 =3D rte_mbuf_dynfield_register("test-dynfield2", sizeof(uint16_= t), > + __alignof__(uint16_t), 0); > + if (offset2 =3D=3D -1 || offset2 =3D=3D offset || (offset & 1)) > + GOTO_FAIL("failed to register dynfield field 2, offset=3D%d, offset2= =3D%d: %s", > + offset, offset2, strerror(errno)); > + > + printf("offset =3D %d, offset2 =3D %d\n", offset, offset2); > + > + offset =3D rte_mbuf_dynfield_register("test-dynfield-fail", 256, 1, 0); > + if (offset !=3D -1) > + GOTO_FAIL("dynamic field creation should fail (too big)"); > + > + offset =3D rte_mbuf_dynfield_register("test-dynfield-fail", 1, 3, 0); > + if (offset !=3D -1) > + GOTO_FAIL("dynamic field creation should fail (bad alignment)"); > + > + flag =3D rte_mbuf_dynflag_register("test-dynflag"); > + if (flag =3D=3D -1) > + GOTO_FAIL("failed to register dynamic field, flag=3D%d: %s", > + flag, strerror(errno)); > + > + flag2 =3D rte_mbuf_dynflag_register("test-dynflag"); > + if (flag2 !=3D flag) > + GOTO_FAIL("failed to lookup dynamic field, flag=3D%d, flag2=3D%d: %s", > + flag, flag2, strerror(errno)); > + > + flag2 =3D rte_mbuf_dynflag_register("test-dynflag2"); > + if (flag2 =3D=3D -1 || flag2 =3D=3D flag) > + GOTO_FAIL("failed to register dynflag field 2, flag=3D%d, flag2=3D%d: = %s", > + flag, flag2, strerror(errno)); > + > + printf("flag =3D %d, flag2 =3D %d\n", flag, flag2); > + > + /* set, get dynamic field */ > + m =3D rte_pktmbuf_alloc(pktmbuf_pool); > + if (m =3D=3D NULL) > + GOTO_FAIL("Cannot allocate mbuf"); > + > + *RTE_MBUF_DYNFIELD(m, offset, uint8_t *) =3D 1; > + if (*RTE_MBUF_DYNFIELD(m, offset, uint8_t *) !=3D 1) > + GOTO_FAIL("failed to read dynamic field"); > + *RTE_MBUF_DYNFIELD(m, offset2, uint16_t *) =3D 1000; > + if (*RTE_MBUF_DYNFIELD(m, offset2, uint16_t *) !=3D 1000) > + GOTO_FAIL("failed to read dynamic field"); > + > + /* set a dynamic flag */ > + m->ol_flags |=3D (1ULL << flag); > + > + rte_pktmbuf_free(m); > + return 0; > +fail: > + rte_pktmbuf_free(m); > + return -1; > +} > +#undef GOTO_FAIL > + > +static int > test_mbuf(void) > { > int ret =3D -1; > @@ -1140,6 +1215,12 @@ test_mbuf(void) > goto err; > } >=20 > + /* test registration of dynamic fields and flags */ > + if (test_mbuf_dyn(pktmbuf_pool) < 0) { > + printf("mbuf dynflag test failed\n"); > + goto err; > + } > + > /* create a specific pktmbuf pool with a priv_size !=3D 0 and no data > * room size */ > pktmbuf_pool2 =3D rte_pktmbuf_pool_create("test_pktmbuf_pool2", > diff --git a/lib/librte_mbuf/Makefile b/lib/librte_mbuf/Makefile > index c8f6d2689..5a9bcee73 100644 > --- a/lib/librte_mbuf/Makefile > +++ b/lib/librte_mbuf/Makefile > @@ -17,8 +17,10 @@ LIBABIVER :=3D 5 >=20 > # all source are stored in SRCS-y > SRCS-$(CONFIG_RTE_LIBRTE_MBUF) :=3D rte_mbuf.c rte_mbuf_ptype.c rte_mbuf= _pool_ops.c > +SRCS-$(CONFIG_RTE_LIBRTE_MBUF) +=3D rte_mbuf_dyn.c >=20 > # install includes > SYMLINK-$(CONFIG_RTE_LIBRTE_MBUF)-include :=3D rte_mbuf.h rte_mbuf_ptype= .h rte_mbuf_pool_ops.h > +SYMLINK-$(CONFIG_RTE_LIBRTE_MBUF)-include +=3D rte_mbuf_dyn.h >=20 > include $(RTE_SDK)/mk/rte.lib.mk > diff --git a/lib/librte_mbuf/meson.build b/lib/librte_mbuf/meson.build > index 6cc11ebb4..9137e8f26 100644 > --- a/lib/librte_mbuf/meson.build > +++ b/lib/librte_mbuf/meson.build > @@ -2,8 +2,10 @@ > # Copyright(c) 2017 Intel Corporation >=20 > version =3D 5 > -sources =3D files('rte_mbuf.c', 'rte_mbuf_ptype.c', 'rte_mbuf_pool_ops.c= ') > -headers =3D files('rte_mbuf.h', 'rte_mbuf_ptype.h', 'rte_mbuf_pool_ops.h= ') > +sources =3D files('rte_mbuf.c', 'rte_mbuf_ptype.c', 'rte_mbuf_pool_ops.c= ', > + 'rte_mbuf_dyn.c') > +headers =3D files('rte_mbuf.h', 'rte_mbuf_ptype.h', 'rte_mbuf_pool_ops.h= ', > + 'rte_mbuf_dyn.h') > deps +=3D ['mempool'] >=20 > allow_experimental_apis =3D true > diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h > index 98225ec80..ef588cd54 100644 > --- 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. */ > } __rte_cache_aligned; >=20 > /** > @@ -1685,6 +1690,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. > * > * If the mbuf we are attaching to isn't a direct buffer and is attached= to > @@ -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..6a96a43da > --- /dev/null > +++ b/lib/librte_mbuf/rte_mbuf_dyn.c > @@ -0,0 +1,373 @@ > +/* 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 { > + TAILQ_ENTRY(mbuf_dynfield) next; > + char name[RTE_MBUF_DYN_NAMESIZE]; > + size_t size; > + size_t align; > + unsigned int flags; > + int offset; > +}; > +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 { > + TAILQ_ENTRY(mbuf_dynflag) next; > + char name[RTE_MBUF_DYN_NAMESIZE]; > + 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)) > + > + 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; > + > + 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 * > +__mbuf_dynfield_lookup(const char *name) > +{ > + struct mbuf_dynfield_list *mbuf_dynfield_list; > + struct mbuf_dynfield *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 *)te->data; > + if (strncmp(name, mbuf_dynfield->name, > + RTE_MBUF_DYN_NAMESIZE) =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, size_t *size, size_t *align) > +{ > + struct mbuf_dynfield *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 (size !=3D NULL) > + *size =3D mbuf_dynfield->size; > + if (align !=3D NULL) > + *align =3D mbuf_dynfield->align; > + > + return mbuf_dynfield->offset; > +} > + > +int > +rte_mbuf_dynfield_register(const char *name, size_t size, size_t align, > + unsigned int flags) > +{ > + struct mbuf_dynfield_list *mbuf_dynfield_list; > + struct mbuf_dynfield *mbuf_dynfield =3D NULL; > + struct rte_tailq_entry *te =3D NULL; > + int offset, ret; > + size_t i; > + > + if (shm =3D=3D NULL && init_shared_mem() < 0) > + goto fail; > + if (size >=3D sizeof(struct rte_mbuf)) { > + rte_errno =3D EINVAL; > + goto fail; > + } > + if (!rte_is_power_of_2(align)) { > + rte_errno =3D EINVAL; > + goto fail; > + } > + > + rte_mcfg_tailq_write_lock(); > + > + mbuf_dynfield =3D __mbuf_dynfield_lookup(name); > + if (mbuf_dynfield !=3D NULL) { > + if (mbuf_dynfield->size !=3D size || > + mbuf_dynfield->align !=3D align || > + mbuf_dynfield->flags !=3D flags) { > + 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, size, align, 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->name, name, sizeof(mbuf_dynfield->name))= ; > + if (ret < 0 || ret >=3D (int)sizeof(mbuf_dynfield->name)) { > + rte_errno =3D ENAMETOOLONG; > + goto fail_unlock; > + } > + mbuf_dynfield->size =3D size; > + mbuf_dynfield->align =3D align; > + mbuf_dynfield->flags =3D flags; > + mbuf_dynfield->offset =3D offset; > + te->data =3D mbuf_dynfield; > + > + TAILQ_INSERT_TAIL(mbuf_dynfield_list, te, next); > + > + for (i =3D offset; i < offset + size; i++) > + shm->free_space[i] =3D 0; > + > +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 * > +__mbuf_dynflag_lookup(const char *name) > +{ > + struct mbuf_dynflag_list *mbuf_dynflag_list; > + struct mbuf_dynflag *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 *)te->data; > + if (strncmp(name, mbuf_dynflag->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 mbuf_dynflag *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; > + } > + > + return mbuf_dynflag->bitnum; > +} > + > +int > +rte_mbuf_dynflag_register(const char *name) > +{ > + struct mbuf_dynflag_list *mbuf_dynflag_list; > + struct mbuf_dynflag *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(name); > + if (mbuf_dynflag !=3D NULL) { > + 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->name, name, sizeof(mbuf_dynflag->name)); > + if (ret < 0 || ret >=3D (int)sizeof(mbuf_dynflag->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); > + > +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..a86986a0f > --- /dev/null > +++ b/lib/librte_mbuf/rte_mbuf_dyn.h > @@ -0,0 +1,119 @@ > +/* 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. > + * > + * 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)= . > + * > + * The registration can be done at any moment, but it is not possible > + * to unregister fields or flags for now. > + * > + * Example of use: > + * > + * - RTE_MBUF_DYN__(ID|SIZE|ALIGN) are defined in this file Does it means that all PMDs define their own 'RTE_MBUF_DYN__(ID|SI= ZE|ALIGN)' here ? In other words, each PMD can expose its private DYN_ here f= or public using ? How about adding another eth_dev_ops API definitions to show the PMD's supp= orting feature names, sizes, align in run time for testpmd ? And also another eth_dev_ops = API for showing the data saved in rte_mbuf by 'dump_pkt_burst' ? Adding a new command for t= estpmd to set the dynamic feature may be good for PMD test. > + * - If the application asks for the feature, the PMD use How does the application ask for the feature ? By ' rte_mbuf_dynfield_regis= ter()' ? > + * rte_mbuf_dynfield_register() to get the dynamic offset and stores > + * in a global variable. In case, the PMD calls 'rte_mbuf_dynfield_register()' for 'dyn_feature' fir= stly, this means that PMD requests the dynamic feature itself if I understand correctl= y. Should PMD calls 'rte_mbuf_dynfield_lookup' for 'dyn_feature' to query the name ex= ists, the size and align are right as expected ? If exists, but size and align are no= t right, may be for PMD change its definition, then PMD can give a warning or error mess= age. If name exists, both size and align are expected, then PMD think that the applicati= on request the right dynamic features. > + * - The application also calls rte_mbuf_dynfield_register() to get the > + * dynamic offset and stores it in a global variable. > + * - When the field must be used by the PMD or the application, they > + * use the RTE_MBUF_DYNFIELD() helper. > + */ > + > +struct rte_mbuf; > + > +/** > + * Register space for a dynamic field in the mbuf structure. > + * > + * @param name > + * A string identifying the dynamic field. External applications or > + * libraries must not define identifers prefixed with "rte_", which > + * are reserved for standard features. > + * @param size > + * The number of bytes to reserve. > + * @param align > + * The alignment constraint, which must be a power of 2. > + * @param flags > + * Reserved for future use. > + * @return > + * The offset in the mbuf structure, or -1 on error (rte_errno is set)= . > + */ > +__rte_experimental > +int rte_mbuf_dynfield_register(const char *name, size_t size, size_t ali= gn, > + unsigned int flags); > + > +/** > + * Lookup for a registered dynamic mbuf field. > + * > + * @param name > + * A string identifying the dynamic field. > + * @param size > + * If not NULL, the number of reserved bytes for this field is stored > + * at this address. > + * @param align > + * If not NULL, the alignement constraint for this field is stored > + * at this address. > + * @return > + * The offset of this field in the mbuf structure, or -1 on error > + * (rte_errno is set). > + */ > +__rte_experimental > +int rte_mbuf_dynfield_lookup(const char *name, size_t *size, size_t *ali= gn); > + > +/** > + * Register a dynamic flag in the mbuf structure. > + * > + * @param name > + * A string identifying the dynamic flag. External applications or > + * libraries must not define identifers prefixed with "rte_", which > + * are reserved for standard features. > + * @return > + * The number of the reserved bit, or -1 on error (rte_errno is set). > + */ > +__rte_experimental > +int rte_mbuf_dynflag_register(const char *name); > + > +/** > + * Lookup for a registered dynamic mbuf flag. > + * > + * @param name > + * A string identifying the dynamic flag. > + * @return > + * The offset of this flag in the mbuf structure, or -1 on error > + * (rte_errno is set). > + */ > +__rte_experimental > +int rte_mbuf_dynflag_lookup(const char *name); > + > +/** > + * Helper macro to access to a dynamic field. > + */ > +#define RTE_MBUF_DYNFIELD(m, offset, type) ((type)((char *)(m) + (offset= ))) > + > +/** > + * Maximum length of the dynamic field or flag string. > + */ > +#define RTE_MBUF_DYN_NAMESIZE 32 > + > +#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.11.0