src: add dynamic register allocation infrastructure
Pablo Neira Ayuso
maintainerPATCH v2 1/1 src: add dynamic register allocation infrastructure
Starting Linux kernel 5.18-rc, operations on registers that already contain the expected data are turned into noop.
Track operation on registers to use the same register through nftnl_reg_get(). This patch introduces an LRU eviction strategy when all the registers are in used.
nftnl_reg_get_scratch() is used to allocate a register as scratchpad
area: no tracking is performed in this case, although register eviction might occur.
Signed-off-by Pablo Neira Ayuso <[email protected]>
---
v2: fix buggy register cancel logic.
include/expr_ops.h | 6 + include/internal.h | 1 + include/libnftnl/Makefile.am | 1 + include/regs.h | 32 +++++ src/Makefile.am | 1 + src/expr/meta.c | 44 +++++++ src/expr/payload.c | 31 +++++ src/libnftnl.map | 7 ++ src/regs.c | 226 +++++++++++++++++++++++++++++++++++ 9 files changed, 349 insertions(+) create mode 100644 include/regs.h create mode 100644 src/regs.c
include/expr_ops.h +6 -0
@@ -7,6 +7,7 @@struct nlattr ;struct nlmsghdr ;struct nftnl_expr ; +struct nftnl_reg ;struct expr_ops {const char *name;
@@ -19,6 +20,11 @@ struct expr_ops {int (*parse)(struct nftnl_expr *e,struct nlattr *attr);void (*build)(struct nlmsghdr *nlh,const struct nftnl_expr *e);int (*snprintf)(char *buf,size_t len,uint32_t flags,const struct nftnl_expr *e); +struct { +int (*len)(const struct nftnl_expr *e); +bool (*cmp)(const struct nftnl_reg *reg,const struct nftnl_expr *e); +void (*update)(struct nftnl_reg *reg,const struct nftnl_expr *e); + } reg; };struct expr_ops *nftnl_expr_ops_lookup (const char *name);
include/internal.h +1 -0
@@ -12,5 +12,6 @@#include "expr.h" #include "expr_ops.h" #include "rule.h" +#include "regs.h" #endif /* _LIBNFTNL_INTERNAL_H_ */
include/libnftnl/Makefile.am +1 -0
@@ -3,6 +3,7 @@ pkginclude_HEADERS = batch.h \ trace.h \ chain.h \ object.h \ + regs.h \ rule.h \ expr.h \ set.h \
include/regs.h new file +32 -0
@@ -0,0 +1,32 @@ +#ifndef _LIBNFTNL_REGS_INTERNAL_H_ +#define _LIBNFTNL_REGS_INTERNAL_H_ + +enum nftnl_expr_type { +NFT_EXPR_UNSPEC =0 , +NFT_EXPR_PAYLOAD , +NFT_EXPR_META , +}; + +struct nftnl_reg { +enum nftnl_expr_type type; +uint32_t len; +uint64_t genid; +uint8_t word; +union { +struct { +enum nft_meta_keys key; + } meta; +struct { +enum nft_payload_bases base; +uint32_t offset; + } payload; + }; +}; + +struct nftnl_regs { +uint32_t num_regs; +struct nftnl_reg *reg; +uint64_t genid; +}; + +#endif
src/Makefile.am +1 -0
@@ -14,6 +14,7 @@ libnftnl_la_SOURCES = utils.c \ trace.c \ chain.c \ object.c \ + regs.c \ rule.c \ set.c \ set_elem.c \
src/expr/meta.c +44 -0
60 lines not on this page · raw message
src/expr/payload.c +31 -0
@@ -203,6 +203,32 @@ nftnl_expr_payload_parse(struct nftnl_expr *e, struct nlattr *attr)return 0 ; } +static int nftnl_payload_reg_len (const struct nftnl_expr *expr) +{ +const struct nftnl_expr_payload *payload =nftnl_expr_data (expr); + +return payload->len; +} + +static bool nftnl_payload_reg_cmp (const struct nftnl_reg *reg, +const struct nftnl_expr *e) +{ +const struct nftnl_expr_payload *payload =nftnl_expr_data (e); + +return reg->payload.base == payload->base && + reg->payload.offset == payload->offset && + reg->len >= payload->len; +} + +static void nftnl_payload_reg_update (struct nftnl_reg *reg, +const struct nftnl_expr *e) +{ +const struct nftnl_expr_payload *payload =nftnl_expr_data (e); + + reg->payload.base = payload->base; + reg->payload.offset = payload->offset; +} +static const char *base2str_array[NFT_PAYLOAD_INNER_HEADER +1 ] = { [NFT_PAYLOAD_LL_HEADER ] ="link" , [NFT_PAYLOAD_NETWORK_HEADER ] ="network" ,
@@ -260,4 +286,9 @@ struct expr_ops expr_ops_payload = { .parse = nftnl_expr_payload_parse, .build = nftnl_expr_payload_build, .snprintf = nftnl_expr_payload_snprintf, + .reg = { + .len = nftnl_payload_reg_len, + .cmp = nftnl_payload_reg_cmp, + .update = nftnl_payload_reg_update, + }, };
src/libnftnl.map +7 -0
@@ -387,3 +387,10 @@ LIBNFTNL_16 { LIBNFTNL_17 { nftnl_set_elem_nlmsg_build; } LIBNFTNL_16; + +LIBNFTNL_18 { + nftnl_regs_alloc; + nftnl_regs_free; + nftnl_reg_get; + nftnl_reg_get_scratch; +} LIBNFTNL_17;
src/regs.c new file +226 -0
226 lines not on this page · raw message
Signature · 1 line
2.30.2