bitwise shift support
v2 cover bitwise shift support
The kernel supports bitwise shift operations. This patch-set adds the support to libnftnl. There are couple of preliminary housekeeping patches.
Jeremy Sowden (6):
Update gitignore.
bitwise: fix some incorrect indentation.
bitwise: add helper to print boolean expressions.
include: update nf_tables.h.
bitwise: add support for new netlink attributes.
bitwise: add support for left- and right-shifts.
.gitignore | 9 ++ include/libnftnl/expr.h | 2 + include/linux/netfilter/nf_tables.h | 24 +++- src/expr/bitwise.c | 93 +++++++++++-- tests/nft-expr_bitwise-test.c | 204 +++++++++++++++++++++++++--- 5 files changed, 304 insertions(+), 28 deletions(-)
Signature · 1 line
2.24.1
PATCH v2 1/6 Update gitignore.
Add ctags and etags tag files, and Emacs back-up files.
--- .gitignore | 9 +++++++++ 1 file changed, 9 insertions(+)
.gitignore +9 -0
@@ -29,3 +29,12 @@ examples/* tests/* !tests/*.c !tests/Makefile.am + +# Tag files for Vim and Emacs. +TAGS +tags + +# Emacs back-up files +*~ +\#*\# +.\#*
Signature · 1 line
2.24.1
PATCH v2 2/6 bitwise: fix some incorrect indentation.
In reply to Jeremy Sowden--- src/expr/bitwise.c | 12 ++++++------ tests/nft-expr_bitwise-test.c | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-)
src/expr/bitwise.c +6 -6
@@ -31,7 +31,7 @@ struct nftnl_expr_bitwise {static int nftnl_expr_bitwise_set (struct nftnl_expr *e,uint16_t type, -const void *data,uint32_t data_len) +const void *data,uint32_t data_len) {struct nftnl_expr_bitwise *bitwise =nftnl_expr_data (e);
@@ -61,7 +61,7 @@ nftnl_expr_bitwise_set(struct nftnl_expr *e, uint16_t type,static const void *nftnl_expr_bitwise_get (const struct nftnl_expr *e,uint16_t type, -uint32_t *data_len) +uint32_t *data_len) {struct nftnl_expr_bitwise *bitwise =nftnl_expr_data (e);
@@ -127,7 +127,7 @@ nftnl_expr_bitwise_build(struct nlmsghdr *nlh, const struct nftnl_expr *e) nest =mnl_attr_nest_start (nlh,NFTA_BITWISE_MASK );mnl_attr_put (nlh,NFTA_DATA_VALUE , bitwise->mask.len, - bitwise->mask.val); + bitwise->mask.val);mnl_attr_nest_end (nlh, nest); }if (e->flags & (1 <<NFTNL_EXPR_BITWISE_XOR )) {
@@ -135,7 +135,7 @@ nftnl_expr_bitwise_build(struct nlmsghdr *nlh, const struct nftnl_expr *e) nest =mnl_attr_nest_start (nlh,NFTA_BITWISE_XOR );mnl_attr_put (nlh,NFTA_DATA_VALUE , bitwise->xor.len, - bitwise->xor.val); + bitwise->xor.val);mnl_attr_nest_end (nlh, nest); } }
@@ -185,14 +185,14 @@ static int nftnl_expr_bitwise_snprintf_default(char *buf, size_t size,SNPRINTF_BUFFER_SIZE (ret, remain, offset); ret =nftnl_data_reg_snprintf (buf + offset, remain, &bitwise->mask, -NFTNL_OUTPUT_DEFAULT ,0 ,DATA_VALUE ); +NFTNL_OUTPUT_DEFAULT ,0 ,DATA_VALUE );SNPRINTF_BUFFER_SIZE (ret, remain, offset); ret =snprintf (buf + offset, remain,") ^ " );SNPRINTF_BUFFER_SIZE (ret, remain, offset); ret =nftnl_data_reg_snprintf (buf + offset, remain, &bitwise->xor, -NFTNL_OUTPUT_DEFAULT ,0 ,DATA_VALUE ); +NFTNL_OUTPUT_DEFAULT ,0 ,DATA_VALUE );SNPRINTF_BUFFER_SIZE (ret, remain, offset);return offset;
tests/nft-expr_bitwise-test.c +2 -2
@@ -28,7 +28,7 @@ static void print_err(const char *msg) }static void cmp_nftnl_expr (struct nftnl_expr *rule_a, -struct nftnl_expr *rule_b) +struct nftnl_expr *rule_b) {uint32_t maska, maskb;uint32_t xora, xorb;
@@ -50,8 +50,8 @@ static void cmp_nftnl_expr(struct nftnl_expr *rule_a,nftnl_expr_get (rule_b,NFTNL_EXPR_BITWISE_XOR , &xorb);if (xora != xorb)print_err ("Size of BITWISE_XOR mismatches" ); - } +int main (int argc,char *argv[]) {struct nftnl_rule *a, *b =NULL ;
Signature · 1 line
2.24.1
PATCH v2 3/6 bitwise: add helper to print boolean expressions.
In reply to Jeremy SowdenMove the code for printing boolean expressions into a separate function. Another function will be added for shifts later.
--- src/expr/bitwise.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
src/expr/bitwise.c +11 -3
@@ -174,10 +174,10 @@ nftnl_expr_bitwise_parse(struct nftnl_expr *e, struct nlattr *attr)return ret; } -static int nftnl_expr_bitwise_snprintf_default (char *buf,size_t size, -const struct nftnl_expr *e) +static int +nftnl_expr_bitwise_snprintf_bool (char *buf,size_t size, +const struct nftnl_expr_bitwise *bitwise) { -struct nftnl_expr_bitwise *bitwise =nftnl_expr_data (e);int remain = size, offset =0 , ret; ret = snprintf(buf, remain, "reg %u = (reg=%u & ",
@@ -198,6 +198,14 @@ static int nftnl_expr_bitwise_snprintf_default(char *buf, size_t size,return offset; } +static int nftnl_expr_bitwise_snprintf_default (char *buf,size_t size, +const struct nftnl_expr *e) +{ +struct nftnl_expr_bitwise *bitwise =nftnl_expr_data (e); + +return nftnl_expr_bitwise_snprintf_bool (buf, size, bitwise); +} +static int nftnl_expr_bitwise_snprintf (char *buf,size_t size,uint32_t type,uint32_t flags,const struct nftnl_expr *e)
Signature · 1 line
2.24.1
PATCH v2 4/6 include: update nf_tables.h.
In reply to Jeremy SowdenThe kernel UAPI header includes a couple of new bitwise netlink attributes and an enum.
--- include/libnftnl/expr.h | 2 ++ include/linux/netfilter/nf_tables.h | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-)
include/libnftnl/expr.h +2 -0
@@ -116,6 +116,8 @@ enum {NFTNL_EXPR_BITWISE_LEN ,NFTNL_EXPR_BITWISE_MASK ,NFTNL_EXPR_BITWISE_XOR , +NFTNL_EXPR_BITWISE_OP , + NFTNL_EXPR_BITWISE_DATA, }; enum {
include/linux/netfilter/nf_tables.h +22 -2
@@ -484,6 +484,20 @@ enum nft_immediate_attributes { };#define NFTA_IMMEDIATE_MAX (__NFTA_IMMEDIATE_MAX - 1) +/** +* enum nft_bitwise_ops - nf_tables bitwise operations +* +* @NFT_BITWISE_BOOL: mask-and-xor operation used to implement NOT, AND, OR and +* XOR boolean operations +* @NFT_BITWISE_LSHIFT: left-shift operation +* @NFT_BITWISE_RSHIFT: right-shift operation +*/ +enum nft_bitwise_ops { +NFT_BITWISE_BOOL , +NFT_BITWISE_LSHIFT , +NFT_BITWISE_RSHIFT , +}; +/** * enum nft_bitwise_attributes - nf_tables bitwise expression netlink attributes *
@@ -492,12 +506,16 @@ enum nft_immediate_attributes {* @NFTA_BITWISE_LEN: length of operands (NLA_U32) * @NFTA_BITWISE_MASK: mask value (NLA_NESTED: nft_data_attributes) * @NFTA_BITWISE_XOR: xor value (NLA_NESTED: nft_data_attributes) +* @NFTA_BITWISE_OP: type of operation (NLA_U32: nft_bitwise_ops) +* @NFTA_BITWISE_DATA: argument for non-boolean operations +* (NLA_NESTED: nft_data_attributes) * -* The bitwise expression performs the following operation: +* The bitwise expression supports boolean and shift operations. It implements +* the boolean operations by performing the following operation: * * dreg = (sreg & mask) ^ xor * -* which allow to express all bitwise operations: +* with these mask and xor values: * * mask xor * NOT: 1 1
@@ -512,6 +530,8 @@ enum nft_bitwise_attributes {NFTA_BITWISE_LEN ,NFTA_BITWISE_MASK ,NFTA_BITWISE_XOR , +NFTA_BITWISE_OP , +NFTA_BITWISE_DATA , __NFTA_BITWISE_MAX };#define NFTA_BITWISE_MAX (__NFTA_BITWISE_MAX - 1)
Signature · 1 line
2.24.1
PATCH v2 5/6 bitwise: add support for new netlink attributes.
In reply to Jeremy SowdenAdd code to set and get the new op and data attributes. The existing boolean bitwise expressions will only use the op attribute.
--- src/expr/bitwise.c | 47 ++++++++++++++++++++++++++++++++++- tests/nft-expr_bitwise-test.c | 4 +++ 2 files changed, 50 insertions(+), 1 deletion(-)
src/expr/bitwise.c +46 -1
@@ -24,9 +24,11 @@struct nftnl_expr_bitwise {enum nft_registers sreg;enum nft_registers dreg; +enum nft_bitwise_ops op;unsigned int len;union nftnl_data_reg mask;union nftnl_data_reg xor; +union nftnl_data_reg data; }; static int
@@ -42,6 +44,9 @@ nftnl_expr_bitwise_set(struct nftnl_expr *e, uint16_t type,case NFTNL_EXPR_BITWISE_DREG :memcpy (&bitwise->dreg, data,sizeof (bitwise->dreg));break ; +case NFTNL_EXPR_BITWISE_OP : +memcpy (&bitwise->op, data,sizeof (bitwise->op)); +break ;case NFTNL_EXPR_BITWISE_LEN :memcpy (&bitwise->len, data,sizeof (bitwise->len));break ;
@@ -53,6 +58,10 @@ nftnl_expr_bitwise_set(struct nftnl_expr *e, uint16_t type,memcpy (&bitwise->xor.val, data, data_len); bitwise->xor.len = data_len;break ; +case NFTNL_EXPR_BITWISE_DATA : +memcpy (&bitwise->data.val, data, data_len); + bitwise->data.len = data_len; +break ;default :return -1 ; }
@@ -72,6 +81,9 @@ nftnl_expr_bitwise_get(const struct nftnl_expr *e, uint16_t type,case NFTNL_EXPR_BITWISE_DREG : *data_len =sizeof (bitwise->dreg);return &bitwise->dreg; +case NFTNL_EXPR_BITWISE_OP : + *data_len =sizeof (bitwise->op); +return &bitwise->op;case NFTNL_EXPR_BITWISE_LEN : *data_len =sizeof (bitwise->len);return &bitwise->len;
@@ -81,6 +93,9 @@ nftnl_expr_bitwise_get(const struct nftnl_expr *e, uint16_t type,case NFTNL_EXPR_BITWISE_XOR : *data_len = bitwise->xor.len;return &bitwise->xor.val; +case NFTNL_EXPR_BITWISE_DATA : + *data_len = bitwise->data.len; +return &bitwise->data.val; }return NULL ; }
@@ -96,12 +111,14 @@ static int nftnl_expr_bitwise_cb(const struct nlattr *attr, void *data)switch (type) {case NFTA_BITWISE_SREG :case NFTA_BITWISE_DREG : +case NFTA_BITWISE_OP :case NFTA_BITWISE_LEN :if (mnl_attr_validate (attr,MNL_TYPE_U32 ) <0 )abi_breakage ();break ;case NFTA_BITWISE_MASK :case NFTA_BITWISE_XOR : +case NFTA_BITWISE_DATA :if (mnl_attr_validate (attr,MNL_TYPE_BINARY ) <0 )abi_breakage ();break ;
@@ -120,6 +137,8 @@ nftnl_expr_bitwise_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)mnl_attr_put_u32 (nlh,NFTA_BITWISE_SREG ,htonl (bitwise->sreg));if (e->flags & (1 <<NFTNL_EXPR_BITWISE_DREG ))mnl_attr_put_u32 (nlh,NFTA_BITWISE_DREG ,htonl (bitwise->dreg)); +if (e->flags & (1 <<NFTNL_EXPR_BITWISE_OP )) +mnl_attr_put_u32 (nlh,NFTA_BITWISE_OP ,htonl (bitwise->op));if (e->flags & (1 <<NFTNL_EXPR_BITWISE_LEN ))mnl_attr_put_u32 (nlh,NFTA_BITWISE_LEN ,htonl (bitwise->len));if (e->flags & (1 <<NFTNL_EXPR_BITWISE_MASK )) {
@@ -138,6 +157,14 @@ nftnl_expr_bitwise_build(struct nlmsghdr *nlh, const struct nftnl_expr *e) bitwise->xor.val);mnl_attr_nest_end (nlh, nest); } +if (e->flags & (1 <<NFTNL_EXPR_BITWISE_DATA )) { +struct nlattr *nest; + + nest =mnl_attr_nest_start (nlh,NFTA_BITWISE_DATA ); +mnl_attr_put (nlh,NFTA_DATA_VALUE , bitwise->data.len, + bitwise->data.val); +mnl_attr_nest_end (nlh, nest); + } } static int
@@ -158,6 +185,10 @@ nftnl_expr_bitwise_parse(struct nftnl_expr *e, struct nlattr *attr) bitwise->dreg =ntohl (mnl_attr_get_u32 (tb[NFTA_BITWISE_DREG ])); e->flags |= (1 <<NFTNL_EXPR_BITWISE_DREG ); } +if (tb[NFTA_BITWISE_OP ]) { + bitwise->op =ntohl (mnl_attr_get_u32 (tb[NFTA_BITWISE_OP ])); + e->flags |= (1 <<NFTNL_EXPR_BITWISE_OP ); + }if (tb[NFTA_BITWISE_LEN ]) { bitwise->len =ntohl (mnl_attr_get_u32 (tb[NFTA_BITWISE_LEN ])); e->flags |= (1 <<NFTNL_EXPR_BITWISE_LEN );
@@ -170,6 +201,10 @@ nftnl_expr_bitwise_parse(struct nftnl_expr *e, struct nlattr *attr) ret =nftnl_parse_data (&bitwise->xor, tb[NFTA_BITWISE_XOR ],NULL ); e->flags |= (1 <<NFTA_BITWISE_XOR ); } +if (tb[NFTA_BITWISE_DATA ]) { + ret =nftnl_parse_data (&bitwise->data, tb[NFTA_BITWISE_DATA ],NULL ); + e->flags |= (1 <<NFTNL_EXPR_BITWISE_DATA ); + }return ret; }
@@ -202,8 +237,18 @@ static int nftnl_expr_bitwise_snprintf_default(char *buf, size_t size, const struct nftnl_expr *e) {struct nftnl_expr_bitwise *bitwise =nftnl_expr_data (e); +int err =-1 ; + +switch (bitwise->op) { +case NFT_BITWISE_BOOL : + err =nftnl_expr_bitwise_snprintf_bool (buf, size, bitwise); +break ; +case NFT_BITWISE_LSHIFT : +case NFT_BITWISE_RSHIFT : +break ; + } -return nftnl_expr_bitwise_snprintf_bool (buf, size, bitwise); +return err; } static int
tests/nft-expr_bitwise-test.c +4 -0
@@ -39,6 +39,9 @@ static void cmp_nftnl_expr(struct nftnl_expr *rule_a,if (nftnl_expr_get_u32 (rule_a,NFTNL_EXPR_BITWISE_SREG ) !=nftnl_expr_get_u32 (rule_b,NFTNL_EXPR_BITWISE_SREG ))print_err ("Expr BITWISE_SREG mismatches" ); +if (nftnl_expr_get_u32 (rule_a,NFTNL_EXPR_BITWISE_OP ) != +nftnl_expr_get_u32 (rule_b,NFTNL_EXPR_BITWISE_OP )) +print_err ("Expr BITWISE_OP mismatches" );if (nftnl_expr_get_u16 (rule_a,NFTNL_EXPR_BITWISE_LEN ) !=nftnl_expr_get_u16 (rule_b,NFTNL_EXPR_BITWISE_LEN ))print_err ("Expr BITWISE_DREG mismatches" );
@@ -73,6 +76,7 @@ int main(int argc, char *argv[])nftnl_expr_set_u32 (ex,NFTNL_EXPR_BITWISE_SREG ,0x12345678 );nftnl_expr_set_u32 (ex,NFTNL_EXPR_BITWISE_DREG ,0x78123456 ); +nftnl_expr_set_u32 (ex,NFTNL_EXPR_BITWISE_OP ,NFT_BITWISE_BOOL );nftnl_expr_set_u32 (ex,NFTNL_EXPR_BITWISE_LEN ,0x56781234 );nftnl_expr_set (ex,NFTNL_EXPR_BITWISE_MASK , &mask,sizeof (mask));nftnl_expr_set (ex,NFTNL_EXPR_BITWISE_XOR , &xor,sizeof (xor));
Signature · 1 line
2.24.1
PATCH v2 6/6 bitwise: add support for left- and right-shifts.
In reply to Jeremy SowdenThe kernel supports bitwise shifts. Add support to libnftnl.
--- src/expr/bitwise.c | 22 ++++ tests/nft-expr_bitwise-test.c | 202 +++++++++++++++++++++++++++++++--- 2 files changed, 206 insertions(+), 18 deletions(-)
src/expr/bitwise.c +22 -0
@@ -233,6 +233,25 @@ nftnl_expr_bitwise_snprintf_bool(char *buf, size_t size,return offset; } +static int +nftnl_expr_bitwise_snprintf_shift (char *buf,size_t size,const char *op, +const struct nftnl_expr_bitwise *bitwise) +{int remain = size, offset =0 , ret; + + ret =snprintf (buf, remain,"reg %u = ( reg %u %s " , + bitwise->dreg, bitwise->sreg, op); +SNPRINTF_BUFFER_SIZE (ret, remain, offset); + + ret =nftnl_data_reg_snprintf (buf + offset, remain, &bitwise->data, +NFTNL_OUTPUT_DEFAULT ,0 ,DATA_VALUE ); +SNPRINTF_BUFFER_SIZE (ret, remain, offset); + + ret =snprintf (buf + offset, remain,") " ); +SNPRINTF_BUFFER_SIZE (ret, remain, offset); + +return offset; +} +static int nftnl_expr_bitwise_snprintf_default (char *buf,size_t size,const struct nftnl_expr *e) {
@@ -244,7 +263,10 @@ static int nftnl_expr_bitwise_snprintf_default(char *buf, size_t size, err =nftnl_expr_bitwise_snprintf_bool (buf, size, bitwise);break ;case NFT_BITWISE_LSHIFT : + err =nftnl_expr_bitwise_snprintf_shift (buf, size,"<<" , bitwise); +break ;case NFT_BITWISE_RSHIFT : + err =nftnl_expr_bitwise_snprintf_shift (buf, size,">>" , bitwise);break ; }
tests/nft-expr_bitwise-test.c +184 -18
265 lines not on this page · raw message
Signature · 1 line
2.24.1
Quoted 3 lines · Jeremy Sowden
The kernel supports bitwise shift operations. This patch-set adds the support to libnftnl. There are couple of preliminary housekeeping patches.
Changes since v1:
* updated to match the final kernel API;
* split the single patch that implemented the new shift expressions into several smaller ones.
Quoted 19 lines
Jeremy Sowden (6):
Update gitignore.
bitwise: fix some incorrect indentation.
bitwise: add helper to print boolean expressions.
include: updatenf_tables.h.
bitwise: add support for new netlink attributes.
bitwise: add support for left- and right-shifts..gitignore | 9 ++ include/libnftnl/expr.h | 2 + include/linux/netfilter/nf_tables.h | 24 +++- src/expr/bitwise.c | 93 +++++++++++-- tests/nft-expr_bitwise-test.c | 204 +++++++++++++++++++++++++--- 5 files changed, 304 insertions(+), 28 deletions(-)--
2.24.1
Pablo Neira Ayuso
maintainerQuoted 3 lines · Jeremy Sowden
The kernel supports bitwise shift operations. This patch-set adds the support to libnftnl. There are couple of preliminary housekeeping patches.
Series applied, thanks.