various cleanups related to enums and struct datatype
cover various cleanups related to enums and struct datatype
Various loosely related patches in the hope to improve something.
Thomas Haller (9):
src: fix indentation/whitespace
include: fix missing definitions in <cache.h>/<headers.h>
datatype: drop flags field from datatype
datatype: use "enum byteorder" instead of int in set_datatype_alloc()
payload: use enum icmp_hdr_field_type in
payload_may_dependency_kill_icmp()
netlink: handle invalid etype in set_make_key()
expression: cleanup expr_ops_by_type() and handle u32 input
datatype: use __attribute__((packed)) instead of enum bitfields
proto: add missing proto_definitions for PROTO_DESC_GENEVE
include/cache.h | 9 +++++++++ include/datatype.h | 27 +++++++++++---------------- include/expression.h | 10 ++++++---- include/headers.h | 2 ++ include/proto.h | 11 +++++++---- src/datatype.c | 22 +++++++++------------- src/evaluate.c | 2 +- src/expression.c | 23 +++++++++++------------ src/meta.c | 6 +++--- src/netlink.c | 6 ++++-- src/netlink_delinearize.c | 2 +- src/payload.c | 10 ++++------ src/proto.c | 3 ++- src/rt.c | 2 +- src/segtree.c | 5 ++--- 15 files changed, 73 insertions(+), 67 deletions(-)
Signature · 1 line
2.41.0
PATCH 1/9 src: fix indentation/whitespace
--- src/meta.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
src/meta.c +2 -2
@@ -968,8 +968,8 @@ struct stmt *meta_stmt_alloc(const struct location *loc, enum nft_meta_keys key, stmt->meta.key = key; stmt->meta.expr = expr; -if (key <array_size (meta_templates)) - stmt->meta.tmpl = &meta_templates[key]; +if (key <array_size (meta_templates)) + stmt->meta.tmpl = &meta_templates[key];return stmt; }
Signature · 1 line
2.41.0
Re: [PATCH nft 1/9] src: fix indentation/whitespace
Applied, thanks
PATCH 2/9 include: fix missing definitions in <cache.h>/<headers.h>
In reply to Thomas HallerThe headers should be self-contained so they can be included in any order. With exception of <nft.h>, which any internal header can rely on.
Some fixes for <cache.h>/<headers.h>.
In case of <cache.h>, forward declare some of the structs instead of including the headers. <headers.h> uses struct in6_addr.
--- include/cache.h | 9 +++++++++ include/headers.h | 2 ++ 2 files changed, 11 insertions(+)
include/cache.h +9 -0
@@ -3,6 +3,8 @@#include <string.h> +#include <list.h> +struct handle ;enum cache_level_bits {
@@ -66,6 +68,7 @@ struct nft_cache_filter { };struct nft_cache ; +struct nft_ctx ;enum cmd_ops ; int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds,
@@ -97,6 +100,8 @@ void chain_cache_add(struct chain *chain, struct table *table);void chain_cache_del (struct chain *chain);struct chain *chain_cache_find (const struct table *table,const char *name); +struct set ; +void set_cache_add (struct set *set,struct table *table);void set_cache_del (struct set *set);struct set *set_cache_find (const struct table *table,const char *name);
@@ -121,6 +126,8 @@ void table_cache_del(struct table *table);struct table *table_cache_find (const struct cache *cache,const char *name,uint32_t family); +struct obj ; +void obj_cache_add (struct obj *obj,struct table *table);void obj_cache_del (struct obj *obj); struct obj *obj_cache_find(const struct table *table, const char *name,
@@ -138,6 +145,8 @@ struct nft_cache {uint32_t flags; }; +struct netlink_ctx ; +void nft_chain_cache_update (struct netlink_ctx *ctx,struct table *table,const char *chain);
include/headers.h +2 -0
@@ -1,6 +1,8 @@#ifndef NFTABLES_HEADERS_H #define NFTABLES_HEADERS_H +#include <netinet/in.h> +#ifndef IPPROTO_UDPLITE # define IPPROTO_UDPLITE 136#endif
Signature · 1 line
2.41.0
PATCH 3/9 datatype: drop flags field from datatype
In reply to Thomas HallerFlags are not always bad. For example, as a function argument they allow easier extension in the future. But with datatype's "flags" argument and enum datatype_flags there are no advantages of this approach.
- replace DTYPE_F_PREFIX with a "bool f_prefix" field. This could even be a bool:1 bitfield if we cared to represent the information with one bit only. For now it's not done because that would not help reducing the size of the struct, so a bitfield is less preferable.
- instead of DTYPE_F_ALLOC, use the refcnt of zero to represent static instances. Drop this redundant flag.
- move the integer field "refcnt" in struct datatype beside other fields of similar size/alignment. This makes the size of the struct by one pointer size smaller (on x86-64).
--- include/datatype.h | 24 +++++++++--------------- src/datatype.c | 20 ++++++++------------ src/meta.c | 2 +- src/netlink_delinearize.c | 2 +- src/rt.c | 2 +- src/segtree.c | 5 ++--- 6 files changed, 22 insertions(+), 33 deletions(-)
include/datatype.h +9 -15
@@ -120,24 +120,13 @@ enum byteorder {struct expr ; -/** -* enum datatype_flags -* -* @DTYPE_F_ALLOC: datatype is dynamically allocated -* @DTYPE_F_PREFIX: preferred representation for ranges is a prefix -*/ -enum datatype_flags { -DTYPE_F_ALLOC = (1 <<0 ), -DTYPE_F_PREFIX = (1 <<1 ), -}; -struct parse_ctx ;/** * struct datatype * * @type: numeric identifier * @byteorder: byteorder of type (non-basetypes only) -* @flags: flags +* @f_prefix: preferred representation for ranges is a prefix * @size: type size (fixed sized non-basetypes only) * @subtypes: number of subtypes (concat type) * @name: type name
@@ -147,14 +136,20 @@ struct parse_ctx;* @print: function to print a constant of this type * @parse: function to parse a symbol and return an expression * @sym_tbl: symbol table for this type -* @refcnt: reference counter (only for DTYPE_F_ALLOC) +* @refcnt: reference counter for dynamically allocated instances. */ struct datatype {uint32_t type;enum byteorder byteorder; -unsigned int flags; +bool f_prefix;unsigned int size;unsigned int subtypes; + +/* Refcount for dynamically allocated instances. For static instances +* this is zero (get() and free() are NOPs). +*/ +unsigned int refcnt; +const char *name;const char *desc;const struct datatype *basetype;
@@ -169,7 +164,6 @@ struct datatype {struct error_record *(*err)(const struct expr *sym);void (*describe)(struct output_ctx *octx);const struct symbol_table *sym_tbl; -unsigned int refcnt; };extern const struct datatype *datatype_lookup (enum datatypes type);
src/datatype.c +8 -12
@@ -641,7 +641,7 @@ const struct datatype ipaddr_type = { .basetype = &integer_type, .print = ipaddr_type_print, .parse = ipaddr_type_parse, - .flags = DTYPE_F_PREFIX, + .f_prefix = true, };static void ip6addr_type_print (const struct expr *expr,struct output_ctx *octx)
@@ -708,7 +708,7 @@ const struct datatype ip6addr_type = { .basetype = &integer_type, .print = ip6addr_type_print, .parse = ip6addr_type_parse, - .flags = DTYPE_F_PREFIX, + .f_prefix = true, }; static void inet_protocol_type_print(const struct expr *expr,
@@ -944,7 +944,7 @@ const struct datatype mark_type = { .print = mark_type_print, .json = mark_type_json, .parse = mark_type_parse, - .flags = DTYPE_F_PREFIX, + .f_prefix = true, };static const struct symbol_table icmp_code_tbl = {
@@ -1203,9 +1203,7 @@ static struct datatype *datatype_alloc(void)struct datatype *dtype; dtype =xzalloc (sizeof (*dtype)); - dtype->flags =DTYPE_F_ALLOC ; dtype->refcnt =1 ; -return dtype; }
@@ -1215,10 +1213,10 @@ struct datatype *datatype_get(const struct datatype *ptr)if (!dtype)return NULL ; -if (!(dtype->flags &DTYPE_F_ALLOC )) -return dtype; - dtype->refcnt++; +if (dtype->refcnt >0 ) + dtype->refcnt++; +return dtype; }
@@ -1245,7 +1243,6 @@ struct datatype *datatype_clone(const struct datatype *orig_dtype) *dtype = *orig_dtype; dtype->name =xstrdup (orig_dtype->name); dtype->desc =xstrdup (orig_dtype->desc); - dtype->flags =DTYPE_F_ALLOC | orig_dtype->flags; dtype->refcnt =1 ;return dtype;
@@ -1257,10 +1254,9 @@ void datatype_free(const struct datatype *ptr)if (!dtype)return ; -if (!(dtype->flags &DTYPE_F_ALLOC )) -return ; -assert (dtype->refcnt !=0 ); +if (dtype->refcnt ==0 ) +return ;if (--dtype->refcnt >0 )return ;
src/meta.c +1 -1
@@ -368,7 +368,7 @@ const struct datatype devgroup_type = { .print = devgroup_type_print, .json = devgroup_type_json, .parse = devgroup_type_parse, - .flags = DTYPE_F_PREFIX, + .f_prefix = true, };const struct datatype ifname_type = {
src/netlink_delinearize.c +1 -1
@@ -2568,7 +2568,7 @@ static void relational_binop_postprocess(struct rule_pp_ctx *ctx,default :break ; } - }else if (binop->left->dtype->flags &DTYPE_F_PREFIX && + }else if (binop->left->dtype->f_prefix && binop->op ==OP_AND && expr->right->etype ==EXPR_VALUE &&expr_mask_is_prefix (binop->right)) { expr->left =expr_get (binop->left);
src/rt.c +1 -1
@@ -55,7 +55,7 @@ const struct datatype realm_type = { .basetype = &integer_type, .print = realm_type_print, .parse = realm_type_parse, - .flags = DTYPE_F_PREFIX, + .f_prefix = true, };const struct rt_template rt_templates[] = {
src/segtree.c +2 -3
@@ -402,8 +402,7 @@ void concat_range_aggregate(struct expr *set) goto next; } -if (prefix_len <0 || - !(r1->dtype->flags &DTYPE_F_PREFIX )) { +if (prefix_len <0 || !r1->dtype->f_prefix) { tmp =range_expr_alloc (&r1->location, r1, r2);
@@ -518,7 +517,7 @@ add_interval(struct expr *set, struct expr *low, struct expr *i) expr =expr_get (low); }else if (range_is_prefix (range) && !mpz_cmp_ui (p,0 )) { -if (i->dtype->flags &DTYPE_F_PREFIX ) +if (i->dtype->f_prefix) expr =interval_to_prefix (low, i, range);else if (expr_basetype (i)->type ==TYPE_STRING ) expr =interval_to_string (low, i, range);
Signature · 1 line
2.41.0
Pablo Neira Ayuso
maintainerRe: [PATCH nft 3/9] datatype: drop flags field from datatype
Quoted 11 lines · Thomas Haller
Flags are not always bad. For example, as a function argument they allow easier extension in the future. But with datatype's "flags" argument and enum
datatype_flagsthere are no advantages of this approach.- replace
DTYPE_F_PREFIXwith a "boolf_prefix" field. This could even be a bool:1 bitfield if we cared to represent the information with one bit only. For now it's not done because that would not help reducing the size of the struct, so a bitfield is less preferable.- instead of
DTYPE_F_ALLOC, use the refcnt of zero to represent static instances. Drop this redundant flag.
Not sure I want to rely on refcnt to zero to identify dynamic datatypes. I think we need to consolidate datatype_set() to be used not only where this deals with dynamic datatypes, it might help improve traceability of datatype assignment.
Quoted 216 lines
- move the integer field "refcnt" in struct datatype beside other fields of similar size/alignment. This makes the size of the struct by one pointer size smaller (on x86-64).
Signed-off-by Thomas Haller <[email protected]>--- include/datatype.h | 24 +++++++++--------------- src/datatype.c | 20 ++++++++------------ src/meta.c | 2 +- src/netlink_delinearize.c | 2 +- src/rt.c | 2 +- src/segtree.c | 5 ++--- 6 files changed, 22 insertions(+), 33 deletions(-)include/datatype.h +9
-15@@ -120,24 +120,13 @@ enum byteorder {struct expr ; -/** -* enum datatype_flags -* -* @DTYPE_F_ALLOC: datatype is dynamically allocated -* @DTYPE_F_PREFIX: preferred representation for ranges is a prefix -*/ -enum datatype_flags { -DTYPE_F_ALLOC = (1 <<0 ), -DTYPE_F_PREFIX = (1 <<1 ), -}; -struct parse_ctx ;/** * struct datatype * * @type: numeric identifier * @byteorder: byteorder of type (non-basetypes only) -* @flags: flags +* @f_prefix: preferred representation for ranges is a prefix * @size: type size (fixed sized non-basetypes only) * @subtypes: number of subtypes (concat type) * @name: type name @@ -147,14 +136,20 @@ struct parse_ctx;* @print: function to print a constant of this type * @parse: function to parse a symbol and return an expression * @sym_tbl: symbol table for this type -* @refcnt: reference counter (only for DTYPE_F_ALLOC) +* @refcnt: reference counter for dynamically allocated instances. */ struct datatype {uint32_t type;enum byteorder byteorder; -unsigned int flags; +bool f_prefix;unsigned int size;unsigned int subtypes; + +/* Refcount for dynamically allocated instances. For static instances +* this is zero (get() and free() are NOPs). +*/ +unsigned int refcnt; +const char *name;const char *desc;const struct datatype *basetype;@@ -169,7 +164,6 @@ struct datatype {struct error_record *(*err)(const struct expr *sym);void (*describe)(struct output_ctx *octx);const struct symbol_table *sym_tbl; -unsigned int refcnt; };extern const struct datatype *datatype_lookup (enum datatypes type);src/datatype.c +8
-12@@ -641,7 +641,7 @@ const struct datatype ipaddr_type = { .basetype = &integer_type, .print = ipaddr_type_print, .parse = ipaddr_type_parse, - .flags = DTYPE_F_PREFIX, + .f_prefix = true, };static void ip6addr_type_print (const struct expr *expr,struct output_ctx *octx)@@ -708,7 +708,7 @@ const struct datatype ip6addr_type = { .basetype = &integer_type, .print = ip6addr_type_print, .parse = ip6addr_type_parse, - .flags = DTYPE_F_PREFIX, + .f_prefix = true, }; static void inet_protocol_type_print(const struct expr *expr,@@ -944,7 +944,7 @@ const struct datatype mark_type = { .print = mark_type_print, .json = mark_type_json, .parse = mark_type_parse, - .flags = DTYPE_F_PREFIX, + .f_prefix = true, };static const struct symbol_table icmp_code_tbl = {@@ -1203,9 +1203,7 @@ static struct datatype *datatype_alloc(void)struct datatype *dtype; dtype =xzalloc (sizeof (*dtype)); - dtype->flags =DTYPE_F_ALLOC ; dtype->refcnt =1 ; -return dtype; }@@ -1215,10 +1213,10 @@ struct datatype *datatype_get(const struct datatype *ptr)if (!dtype)return NULL ; -if (!(dtype->flags &DTYPE_F_ALLOC )) -return dtype; - dtype->refcnt++; +if (dtype->refcnt >0 ) + dtype->refcnt++; +return dtype; }@@ -1245,7 +1243,6 @@ struct datatype *datatype_clone(const struct datatype *orig_dtype) *dtype = *orig_dtype; dtype->name =xstrdup (orig_dtype->name); dtype->desc =xstrdup (orig_dtype->desc); - dtype->flags =DTYPE_F_ALLOC | orig_dtype->flags; dtype->refcnt =1 ;return dtype;@@ -1257,10 +1254,9 @@ void datatype_free(const struct datatype *ptr)if (!dtype)return ; -if (!(dtype->flags &DTYPE_F_ALLOC )) -return ; -assert (dtype->refcnt !=0 ); +if (dtype->refcnt ==0 ) +return ;if (--dtype->refcnt >0 )return ;src/meta.c +1
-1@@ -368,7 +368,7 @@ const struct datatype devgroup_type = { .print = devgroup_type_print, .json = devgroup_type_json, .parse = devgroup_type_parse, - .flags = DTYPE_F_PREFIX, + .f_prefix = true, };const struct datatype ifname_type = {src/netlink_delinearize.c +1
-1@@ -2568,7 +2568,7 @@ static void relational_binop_postprocess(struct rule_pp_ctx *ctx,default :break ; } - }else if (binop->left->dtype->flags &DTYPE_F_PREFIX && + }else if (binop->left->dtype->f_prefix && binop->op ==OP_AND && expr->right->etype ==EXPR_VALUE &&expr_mask_is_prefix (binop->right)) { expr->left =expr_get (binop->left);src/rt.c +1
-1@@ -55,7 +55,7 @@ const struct datatype realm_type = { .basetype = &integer_type, .print = realm_type_print, .parse = realm_type_parse, - .flags = DTYPE_F_PREFIX, + .f_prefix = true, };const struct rt_template rt_templates[] = {src/segtree.c +2
-3@@ -402,8 +402,7 @@ void concat_range_aggregate(struct expr *set) goto next; } -if (prefix_len <0 || - !(r1->dtype->flags &DTYPE_F_PREFIX )) { +if (prefix_len <0 || !r1->dtype->f_prefix) { tmp =range_expr_alloc (&r1->location, r1, r2);@@ -518,7 +517,7 @@ add_interval(struct expr *set, struct expr *low, struct expr *i) expr =expr_get (low); }else if (range_is_prefix (range) && !mpz_cmp_ui (p,0 )) { -if (i->dtype->flags &DTYPE_F_PREFIX ) +if (i->dtype->f_prefix) expr =interval_to_prefix (low, i, range);else if (expr_basetype (i)->type ==TYPE_STRING ) expr =interval_to_string (low, i, range);Signature · 1 line
2.41.0
Re: [PATCH nft 3/9] datatype: drop flags field from datatype
Quoted 15 lines · Pablo Neira Ayuso
Quoted 14 lines · Thomas Haller
Flags are not always bad. For example, as a function argument they allow
easier extension in the future. But with datatype's "flags" argument and
enumdatatype_flagsthere are no advantages of this approach.- replace
DTYPE_F_PREFIXwith a "boolf_prefix" field. This could even
be a bool:1 bitfield if we cared to represent the information with
one bit only. For now it's not done because that would not help reducing
the size of the struct, so a bitfield is less preferable.
Quoted 8 lines
Quoted 3 lines
- instead of
DTYPE_F_ALLOC, use the refcnt of zero to represent static
instances. Drop this redundant flag.Not sure I want to rely on refcnt to zero to identify dynamic datatypes. I think we need to consolidate
datatype_set()to be used not only where this deals with dynamic datatypes, it might help improve traceability of datatype assignment.
I don't understand. Could you elaborate about datatype_set()?
Btw, for dynamically allocated instances the refcnt is always positive, and for static ones it's always zero. The DTYPE_F_ALLOC flag is redundant.
Thomas
Pablo Neira Ayuso
maintainerRe: [PATCH nft 3/9] datatype: drop flags field from datatype
Quoted 20 lines · Thomas Haller
Quoted 19 lines · Pablo Neira Ayuso
Quoted 17 lines · Thomas Haller
Flags are not always bad. For example, as a function argument they allow
easier extension in the future. But with datatype's "flags" argument and
enumdatatype_flagsthere are no advantages of this approach.- replace
DTYPE_F_PREFIXwith a "boolf_prefix" field. This could even
be a bool:1 bitfield if we cared to represent the information with
one bit only. For now it's not done because that would not help reducing
the size of the struct, so a bitfield is less preferable.- instead of
DTYPE_F_ALLOC, use the refcnt of zero to represent static
instances. Drop this redundant flag.
Quoted 6 lines
Quoted 4 lines
Not sure I want to rely on refcnt to zero to identify dynamic datatypes. I think we need to consolidate
datatype_set()to be used not only where this deals with dynamic datatypes, it might help improve traceability of datatype assignment.I don't understand. Could you elaborate about
datatype_set()?
I wonder if we could use datatype_set() to attach static datatypes too, instead of manually attaching datatypes, such as:
expr->dtype = &integer_type;
in case of future extensions, using consistently this helper function will help to identify datatype attachments.
Quoted 3 lines
Btw, for dynamically allocated instances the refcnt is always positive, and for static ones it's always zero. The
DTYPE_F_ALLOCflag is redundant.
That is a correct observation, but a (hipothetical) subtle bug in refcnt might lead to a dynamic datatype get to refcnt to zero, and that might be harder to track?
Let me have a look if I can come up with some counter proposal to get rid of this flag, I would prefer not to infer the datatype class from reference counter value.
Re: [PATCH nft 3/9] datatype: drop flags field from datatype
Quoted 32 lines · Pablo Neira Ayuso
Quoted 23 lines · Thomas Haller
Quoted 22 lines · Pablo Neira Ayuso
Quoted 20 lines · Thomas Haller
Flags are not always bad. For example, as a function argument they
allow
easier extension in the future. But with datatype's "flags" argument and
enumdatatype_flagsthere are no advantages of this approach.- replace
DTYPE_F_PREFIXwith a "boolf_prefix" field. This could
even
be a bool:1 bitfield if we cared to represent the information with
one bit only. For now it's not done because that would not help
reducing
the size of the struct, so a bitfield is less preferable.- instead of
DTYPE_F_ALLOC, use the refcnt of zero to represent static
instances. Drop this redundant flag.Quoted 7 lines
Quoted 5 lines
Not sure I want to rely on refcnt to zero to identify dynamic datatypes. I think we need to consolidate
datatype_set()to be used
not only where this deals with dynamic datatypes, it might help improve traceability of datatype assignment.I don't understand. Could you elaborate about
datatype_set()?
Quoted 7 lines
I wonder if we could use
datatype_set()to attach static datatypes too, instead of manually attaching datatypes, such as:
expr->dtype=&integer_type;in case of future extensions, using consistently this helper function will help to identify datatype attachments.
I think expr->dtype = &integer_type is fine, if
- expr->dtype doesn't previously point to a datatype that requires datatype_free() (e.g. because it's NULL).
- the new datatype requires no datatype_get() (e.g. because it's static).
Quoted 6 lines
Quoted 5 lines
Btw, for dynamically allocated instances the refcnt is always positive,
and for static ones it's always zero. TheDTYPE_F_ALLOCflag is redundant.
Quoted 7 lines
That is a correct observation, but a (hipothetical) subtle bug in refcnt might lead to a dynamic datatype get to refcnt to zero, and that might be harder to track?
Let me have a look if I can come up with some counter proposal to get rid of this flag, I would prefer not to infer the datatype class from reference counter value.
If the reference counting is messed up, there is either a leak, a use- after-free or modification of static data. These are all bad bugs that needs fixing and are avoided by best practices and testing.
IMO keeping redundant state does not help with that or with readability.
I'd like to replace the "unsigned int flags" field with individual boolean fields like "bool f_prefix" or "bool f_alloc".
Dropping DTYPE_F_ALLOC/f_alloc flag altogether can be done (or not done) independently from that.
Thomas
Pablo Neira Ayuso
maintainerRe: [PATCH nft 3/9] datatype: drop flags field from datatype
Quoted 59 lines · Thomas Haller
Quoted 32 lines · Pablo Neira Ayuso
Quoted 23 lines · Thomas Haller
Quoted 22 lines · Pablo Neira Ayuso
Quoted 20 lines · Thomas Haller
Flags are not always bad. For example, as a function argument they
allow
easier extension in the future. But with datatype's "flags" argument and
enumdatatype_flagsthere are no advantages of this approach.- replace
DTYPE_F_PREFIXwith a "boolf_prefix" field. This could
even
be a bool:1 bitfield if we cared to represent the information with
one bit only. For now it's not done because that would not help
reducing
the size of the struct, so a bitfield is less preferable.- instead of
DTYPE_F_ALLOC, use the refcnt of zero to represent static
instances. Drop this redundant flag.Quoted 7 lines
Quoted 5 lines
Not sure I want to rely on refcnt to zero to identify dynamic datatypes. I think we need to consolidate
datatype_set()to be used
not only where this deals with dynamic datatypes, it might help improve traceability of datatype assignment.I don't understand. Could you elaborate about
datatype_set()?Quoted 7 lines
I wonder if we could use
datatype_set()to attach static datatypes too, instead of manually attaching datatypes, such as:
expr->dtype=&integer_type;in case of future extensions, using consistently this helper function will help to identify datatype attachments.
I think
expr->dtype = &integer_typeis fine, if-
expr->dtypedoesn't previously point to a datatype that requiresdatatype_free()(e.g. because it's NULL).- the new datatype requires no
datatype_get()(e.g. because it's static).Quoted 10 lines
Quoted 5 lines
Btw, for dynamically allocated instances the refcnt is always positive,
and for static ones it's always zero. TheDTYPE_F_ALLOCflag is redundant.That is a correct observation, but a (hipothetical) subtle bug in refcnt might lead to a dynamic datatype get to refcnt to zero, and that might be harder to track?
Quoted 6 lines
Quoted 3 lines
Let me have a look if I can come up with some counter proposal to get rid of this flag, I would prefer not to infer the datatype class from reference counter value.
If the reference counting is messed up, there is either a leak, a use- after-free or modification of static data.
I am refering to a hypothetical dynamic datatype object reaching refcnt == 0.
Quoted 8 lines
These are all bad bugs that needs fixing and are avoided by best practices and testing.
IMO keeping redundant state does not help with that or with readability.
I'd like to replace the "unsigned int flags" field with individual boolean fields like "bool
f_prefix" or "boolf_alloc".
As for f_prefix, there is probably a way to infer this from context, I would need to have a look.
Quoted 2 lines
Dropping DTYPE_F_ALLOC/f_alloc flag altogether can be done (or not done) independently from that.
PATCH 4/9 datatype: use "enum byteorder" instead of int in set_datatype_alloc()
In reply to Thomas HallerUse the enum types as we have them.
--- include/datatype.h | 2 +- src/datatype.c | 2 +- src/evaluate.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
include/datatype.h +1 -1
@@ -295,7 +295,7 @@ concat_subtype_lookup(uint32_t type, unsigned int n) }extern const struct datatype * -set_datatype_alloc (const struct datatype *orig_dtype,unsigned int byteorder); +set_datatype_alloc (const struct datatype *orig_dtype,enum byteorder byteorder);extern void time_print (uint64_t msec,struct output_ctx *octx); extern struct error_record *time_parse(const struct location *loc,
src/datatype.c +1 -1
@@ -1304,7 +1304,7 @@ const struct datatype *concat_type_alloc(uint32_t type) }const struct datatype *set_datatype_alloc (const struct datatype *orig_dtype, -unsigned int byteorder) +enum byteorder byteorder) {struct datatype *dtype;
src/evaluate.c +1 -1
@@ -1341,7 +1341,7 @@ static int expr_evaluate_bitwise(struct eval_ctx *ctx, struct expr **expr)struct expr *op = *expr, *left = op->left;const struct datatype *dtype;unsigned int max_len; -int byteorder; +enum byteorder byteorder;if (ctx->stmt_len > left->len) { max_len = ctx->stmt_len;
Signature · 1 line
2.41.0
Re: [PATCH nft 4/9] datatype: use "enum byteorder" instead of int in set_datatype_alloc()
Quoted 1 line · Thomas Haller
Use the enum types as we have them.
Applied, thanks
PATCH 5/9 payload: use enum icmp_hdr_field_type in payload_may_dependency_kill_icmp()
In reply to Thomas HallerDon't mix icmp_dep (enum icmp_hdr_field_type) and the uint8_t icmp_type.
--- src/payload.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
src/payload.c +4 -6
@@ -802,18 +802,16 @@ static uint8_t icmp_dep_to_type(enum icmp_hdr_field_type t)static bool payload_may_dependency_kill_icmp (struct payload_dep_ctx *ctx,struct expr *expr) {const struct expr *dep =payload_dependency_get (ctx, expr->payload.base); -uint8_t icmp_type; +enum icmp_hdr_field_type icmp_dep; - icmp_type = expr->payload.tmpl->icmp_dep; -if (icmp_type ==PROTO_ICMP_ANY ) + icmp_dep = expr->payload.tmpl->icmp_dep; +if (icmp_dep ==PROTO_ICMP_ANY )return false;if (dep->left->payload.desc != expr->payload.desc)return false; - icmp_type =icmp_dep_to_type (expr->payload.tmpl->icmp_dep); - -return ctx->icmp_type == icmp_type; +return ctx->icmp_type ==icmp_dep_to_type (icmp_dep); }static bool payload_may_dependency_kill_ll (struct payload_dep_ctx *ctx,struct expr *expr)
Signature · 1 line
2.41.0
Re: [PATCH nft 5/9] payload: use enum icmp_hdr_field_type in payload_may_dependency_kill_icmp()
Quoted 1 line · Thomas Haller
Don't mix
icmp_dep(enumicmp_hdr_field_type) and theuint8_ticmp_type.
Applied, thanks
PATCH 6/9 netlink: handle invalid etype in set_make_key()
In reply to Thomas HallerIt's not clear to me, what ensures that the etype is always valid. Handle a NULL.
--- src/netlink.c | 2 ++ 1 file changed, 2 insertions(+)
src/netlink.c +2 -0
@@ -896,6 +896,8 @@ static struct expr *set_make_key(const struct nftnl_udata *attr) etype =nftnl_udata_get_u32 (ud[NFTNL_UDATA_SET_TYPEOF_EXPR ]); ops =expr_ops_by_type (etype); +if (!ops) +return NULL ; expr = ops->parse_udata (ud[NFTNL_UDATA_SET_TYPEOF_DATA ]);if (!expr)
Signature · 1 line
2.41.0
Re: [PATCH nft 6/9] netlink: handle invalid etype in set_make_key()
Quoted 2 lines · Thomas Haller
It's not clear to me, what ensures that the etype is always valid. Handle a NULL.
Applied, thanks
Re: [PATCH nft 6/9] netlink: handle invalid etype in set_make_key()
In reply to Thomas HallerQuoted 2 lines · Thomas Haller
It's not clear to me, what ensures that the etype is always valid. Handle a NULL.
Applied, thanks.
Quoted 1 line
Fixes 6e48df5329ea ('src: add "typeof" build/parse/print support')
Please remove empty line break between Fixes: tag and Signed-off-by:
in your future patches.
Thanks!
PATCH 7/9 expression: cleanup expr_ops_by_type() and handle u32 input
In reply to Thomas HallerBe more careful about casting an uint32_t value to "enum expr_types" and make fewer assumptions about the underlying integer type of the enum. Instead, be clear about where we have an untrusted uint32_t from netlink and an enum. Rename expr_ops_by_type() to expr_ops_by_type_u32() to make this clearer. Later we might make the enum as packed, when this starts to matter more.
Also, only the code path expr_ops() wants strict validation and assert against valid enum values. Move the assertion out of __expr_ops_by_type(). Then expr_ops_by_type_u32() does not need to duplicate the handling of EXPR_INVALID. We still need to duplicate the check against EXPR_MAX, to ensure that the uint32_t value can be cast to an enum value.
--- include/expression.h | 2 +- src/expression.c | 23 +++++++++++------------ src/netlink.c | 4 ++-- 3 files changed, 14 insertions(+), 15 deletions(-)
include/expression.h +1 -1
@@ -189,7 +189,7 @@ struct expr_ops { };const struct expr_ops *expr_ops (const struct expr *e); -const struct expr_ops *expr_ops_by_type (enum expr_types etype); +const struct expr_ops *expr_ops_by_type_u32 (uint32_t value);/** * enum expr_flags
src/expression.c +11 -12
@@ -995,7 +995,7 @@ static struct expr *concat_expr_parse_udata(const struct nftnl_udata *attr) goto err_free; etype =nftnl_udata_get_u32 (nest_ud[NFTNL_UDATA_SET_KEY_CONCAT_SUB_TYPE ]); - ops =expr_ops_by_type (etype); + ops =expr_ops_by_type_u32 (etype);if (!ops || !ops->parse_udata) goto err_free;
@@ -1509,9 +1509,7 @@ void range_expr_value_high(mpz_t rop, const struct expr *expr)static const struct expr_ops *__expr_ops_by_type (enum expr_types etype) {switch (etype) { -case EXPR_INVALID : -BUG ("Invalid expression ops requested" ); -break ; +case EXPR_INVALID :break ;case EXPR_VERDICT :return &verdict_expr_ops;case EXPR_SYMBOL :return &symbol_expr_ops;case EXPR_VARIABLE :return &variable_expr_ops;
@@ -1543,21 +1541,22 @@ static const struct expr_ops *__expr_ops_by_type(enum expr_types etype)case EXPR_FLAGCMP :return &flagcmp_expr_ops; } -BUG ("Unknown expression type %d\n" , etype); +return NULL ; }const struct expr_ops *expr_ops (const struct expr *e) { -return __expr_ops_by_type (e->etype); +const struct expr_ops *ops; + + ops =__expr_ops_by_type (e->etype); +if (!ops) +BUG ("Unknown expression type %d\n" , e->etype); +return ops; } -const struct expr_ops *expr_ops_by_type (enum expr_types value) +const struct expr_ops *expr_ops_by_type_u32 (uint32_t value) { -/* value might come from unreliable source, such as "udata" -* annotation of set keys. Avoid BUG() assertion. -*/ -if (value ==EXPR_INVALID || value >EXPR_MAX ) +if (value > (uint32_t )EXPR_MAX )return NULL ; -return __expr_ops_by_type (value); }
src/netlink.c +2 -2
@@ -878,8 +878,8 @@ static struct expr *set_make_key(const struct nftnl_udata *attr) {const struct nftnl_udata *ud[NFTNL_UDATA_SET_TYPEOF_MAX +1 ] = {};const struct expr_ops *ops; -enum expr_types etype;struct expr *expr; +uint32_t etype;int err;if (!attr)
@@ -895,7 +895,7 @@ static struct expr *set_make_key(const struct nftnl_udata *attr)return NULL ; etype =nftnl_udata_get_u32 (ud[NFTNL_UDATA_SET_TYPEOF_EXPR ]); - ops =expr_ops_by_type (etype); + ops =expr_ops_by_type_u32 (etype);if (!ops)return NULL ;
Signature · 1 line
2.41.0
Pablo Neira Ayuso
maintainerRe: [PATCH nft 7/9] expression: cleanup expr_ops_by_type() and handle u32 input
Quoted 75 lines · Thomas Haller
Be more careful about casting an
uint32_tvalue to "enumexpr_types" and make fewer assumptions about the underlying integer type of the enum. Instead, be clear about where we have an untrusteduint32_tfrom netlink and an enum. Renameexpr_ops_by_type()toexpr_ops_by_type_u32()to make this clearer. Later we might make the enum as packed, when this starts to matter more.Also, only the code path
expr_ops()wants strict validation and assert against valid enum values. Move the assertion out of__expr_ops_by_type(). Thenexpr_ops_by_type_u32()does not need to duplicate the handling ofEXPR_INVALID. We still need to duplicate the check againstEXPR_MAX, to ensure that theuint32_tvalue can be cast to an enum value.Signed-off-by Thomas Haller <[email protected]>--- include/expression.h | 2 +- src/expression.c | 23 +++++++++++------------ src/netlink.c | 4 ++-- 3 files changed, 14 insertions(+), 15 deletions(-)include/expression.h +1
-1@@ -189,7 +189,7 @@ struct expr_ops { };const struct expr_ops *expr_ops (const struct expr *e); -const struct expr_ops *expr_ops_by_type (enum expr_types etype); +const struct expr_ops *expr_ops_by_type_u32 (uint32_t value);/** * enum expr_flags src/expression.c +9
-6@@ -995,7 +995,7 @@ static struct expr *concat_expr_parse_udata(const struct nftnl_udata *attr) goto err_free; etype =nftnl_udata_get_u32 (nest_ud[NFTNL_UDATA_SET_KEY_CONCAT_SUB_TYPE ]); - ops =expr_ops_by_type (etype); + ops =expr_ops_by_type_u32 (etype);if (!ops || !ops->parse_udata) goto err_free;@@ -1509,9 +1509,7 @@ void range_expr_value_high(mpz_t rop, const struct expr *expr)static const struct expr_ops *__expr_ops_by_type (enum expr_types etype) {switch (etype) { -case EXPR_INVALID : -BUG ("Invalid expression ops requested" ); -break ; +case EXPR_INVALID :break ;case EXPR_VERDICT :return &verdict_expr_ops;case EXPR_SYMBOL :return &symbol_expr_ops;case EXPR_VARIABLE :return &variable_expr_ops;@@ -1543,21 +1541,22 @@ static const struct expr_ops *__expr_ops_by_type(enum expr_types etype)case EXPR_FLAGCMP :return &flagcmp_expr_ops; } -BUG ("Unknown expression type %d\n" , etype); +return NULL ; }const struct expr_ops *expr_ops (const struct expr *e) { -return __expr_ops_by_type (e->etype); +const struct expr_ops *ops; + + ops =__expr_ops_by_type (e->etype); +if (!ops) +BUG ("Unknown expression type %d\n" , e->etype); +return ops;
src/expression.c static const struct expr_ops *__expr_ops_by_type(enum expr_types etype) · line 1559
Quoted 10 lines
src/expression.c +2
-5} -const struct expr_ops *expr_ops_by_type (enum expr_types value) +const struct expr_ops *expr_ops_by_type_u32 (uint32_t value) { -/* value might come from unreliable source, such as "udata" -* annotation of set keys. Avoid BUG() assertion. -*/ -if (value ==EXPR_INVALID || value >EXPR_MAX ) +if (value > (uint32_t )EXPR_MAX )
I think this still allows a third party to set EXPR_INVALID in the netlink userdata attribute, right?
Quoted 30 lines
src/expression.c +0
-1return NULL ; -return __expr_ops_by_type (value); }src/netlink.c +2
-2@@ -878,8 +878,8 @@ static struct expr *set_make_key(const struct nftnl_udata *attr) {const struct nftnl_udata *ud[NFTNL_UDATA_SET_TYPEOF_MAX +1 ] = {};const struct expr_ops *ops; -enum expr_types etype;struct expr *expr; +uint32_t etype;int err;if (!attr)@@ -895,7 +895,7 @@ static struct expr *set_make_key(const struct nftnl_udata *attr)return NULL ; etype =nftnl_udata_get_u32 (ud[NFTNL_UDATA_SET_TYPEOF_EXPR ]); - ops =expr_ops_by_type (etype); + ops =expr_ops_by_type_u32 (etype);if (!ops)return NULL ;Signature · 1 line
2.41.0
Re: [PATCH nft 7/9] expression: cleanup expr_ops_by_type() and handle u32 input
Quoted 18 lines · Pablo Neira Ayuso
Quoted 9 lines · Thomas Haller
src/expression.c +1
-1-const struct expr_ops *expr_ops_by_type (enum expr_types value) +const struct expr_ops *expr_ops_by_type_u32 (uint32_t value){
- /* value might come from unreliable source, such as "udata" - * annotation of set keys. AvoidBUG()assertion. - */
- if (value ==EXPR_INVALID|| value >EXPR_MAX)
+ if (value > (uint32_t)EXPR_MAX)I think this still allows a third party to set
EXPR_INVALIDin the netlink userdata attribute, right?Quoted 4 lines
return NULL;
-
return__expr_ops_by_type(value);
}
Yes, it still allows that. It's handled by the following __expr_ops_by_type(), which returns NULL for invalid types (like EXPR_INVALID).
The check "if (value > (uint32_t) EXPR_MAX)" is only here to ensure that nothing is lost while casting the uint32_t "value" to the enum expr_types.
Thomas
Pablo Neira Ayuso
maintainerRe: [PATCH nft 7/9] expression: cleanup expr_ops_by_type() and handle u32 input
Quoted 15 lines · Thomas Haller
Quoted 14 lines · Pablo Neira Ayuso
Quoted 9 lines · Thomas Haller
src/expression.c +1
-1-const struct expr_ops *expr_ops_by_type (enum expr_types value) +const struct expr_ops *expr_ops_by_type_u32 (uint32_t value){
- /* value might come from unreliable source, such as "udata" - * annotation of set keys. AvoidBUG()assertion. - */
- if (value ==EXPR_INVALID|| value >EXPR_MAX)
+ if (value > (uint32_t)EXPR_MAX)I think this still allows a third party to set
EXPR_INVALIDin the netlink userdata attribute, right?
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
return NULL;
-
return__expr_ops_by_type(value);
}Yes, it still allows that. It's handled by the following
__expr_ops_by_type(), which returns NULL for invalid types (likeEXPR_INVALID).
Oh indeed.
Quoted 3 lines
The check "if (value > (
uint32_t)EXPR_MAX)" is only here to ensure that nothing is lost while casting theuint32_t"value" to the enumexpr_types.
Is this cast really required? This is to handle the hypothetical case where EXPR_MAX ever gets a negative value?
Re: [PATCH nft 7/9] expression: cleanup expr_ops_by_type() and handle u32 input
Quoted 10 lines · Pablo Neira Ayuso
Quoted 3 lines · Thomas Haller
The check "if (value > (
uint32_t)EXPR_MAX)" is only here to ensure that nothing is lost while casting theuint32_t"value" to the enumexpr_types.Is this cast really required? This is to handle the hypothetical case where
EXPR_MAXever gets a negative value?
EXPR_MAX is never negative.
If EXPR_MAX is treated as a signed integer, then it will be implicitly cast to unsigned when comparing with the uint32_t. The behavior will be correct without a cast.
But won't the compiler warn about comparing integers of different signedness?
The cast is probably not needed. But it doesn't hurt.
Thomas
Pablo Neira Ayuso
maintainerRe: [PATCH nft 7/9] expression: cleanup expr_ops_by_type() and handle u32 input
Quoted 14 lines · Thomas Haller
Quoted 10 lines · Pablo Neira Ayuso
Quoted 3 lines · Thomas Haller
The check "if (value > (
uint32_t)EXPR_MAX)" is only here to ensure that nothing is lost while casting theuint32_t"value" to the enumexpr_types.Is this cast really required? This is to handle the hypothetical case where
EXPR_MAXever gets a negative value?
EXPR_MAXis never negative.
Quoted 8 lines
If
EXPR_MAXis treated as a signed integer, then it will be implicitly cast to unsigned when comparing with theuint32_t. The behavior will be correct without a cast.But won't the compiler warn about comparing integers of different signedness?
The cast is probably not needed. But it doesn't hurt.
I'd prefer to remove, if not strictly necessary. EXPR_* are never expected to see a negative value.
Re: [PATCH nft 7/9] expression: cleanup expr_ops_by_type() and handle u32 input
In reply to Thomas HallerQuoted 14 lines · Thomas Haller
Quoted 10 lines · Pablo Neira Ayuso
Quoted 3 lines · Thomas Haller
The check "if (value > (
uint32_t)EXPR_MAX)" is only here to ensure that nothing is lost while casting theuint32_t"value" to the enumexpr_types.Is this cast really required? This is to handle the hypothetical case where
EXPR_MAXever gets a negative value?
EXPR_MAXis never negative.
Quoted 8 lines
If
EXPR_MAXis treated as a signed integer, then it will be implicitly cast to unsigned when comparing with theuint32_t. The behavior will be correct without a cast.But won't the compiler warn about comparing integers of different signedness?
The cast is probably not needed. But it doesn't hurt.
Applied, I removed the cast and I added a note in the commit that I mangled this myself, it is really turns out we really need it, we can recover it. Thanks.
PATCH 8/9 datatype: use __attribute__((packed)) instead of enum bitfields
In reply to Thomas HallerAt some places we use bitfields of those enums, to save space inside the structure. We can achieve that in a better way, by using GCC's __attribute__((packed)) on the enum type.
It's better because a :8 bitfield makes the assumption that all enum values (valid or invalid) fit into that field. With packed enums, we don't need that assumption as the field can hold all possible numbers that the enum type can hold. This reduces the places we need to worry about truncating a value to casts between other types and the enum. Those places already require us to be careful.
On the other hand, previously casting an int (or uint32_t) likely didn't cause a truncation as the underlying type was large enough. So we could check for invalid enum values after the cast. We might do that at places. For example, we do
key = nftnl_expr_get_u32(nle, NFTNL_EXPR_META_KEY); expr = meta_expr_alloc(loc, key);
where we cast from an uint32_t to an enum without checking. Note that enum nft_meta_keys is not packed by this patch. But this is an example how things could be wrong. But the bug already exits before: don't make assumption about the underlying enum type and take care of truncation during casts.
This makes the change potentially dangerous, and it's hard to be sure that it doesn't uncover bugs (due tow rong assumptions about enum types).
Note that we were already using the GCC-ism __attribute__((packed)) previously, however on a struct and not on an enum. Anyway. It seems unlikely that we support any other compilers than GCC/Clang. Those both support this attribute. We should not worry about portability towards hypothetical compilers (the C standard), unless there is a real compiler that we can use and test and shows a problem with this. Especially when we support both GCC and Clang, which themselves are ubiquitous and accessible to all users (as they also need to build the kernel in the first place).
--- include/datatype.h | 1 + include/expression.h | 8 +++++--- include/proto.h | 11 +++++++---- 3 files changed, 13 insertions(+), 7 deletions(-)
include/datatype.h +1 -0
@@ -112,6 +112,7 @@ enum datatypes {* @BYTEORDER_HOST_ENDIAN: host endian * @BYTEORDER_BIG_ENDIAN: big endian */ +__attribute__((packed)) enum byteorder { BYTEORDER_INVALID, BYTEORDER_HOST_ENDIAN,
include/expression.h +5 -3
@@ -45,6 +45,7 @@* @EXPR_SET_ELEM_CATCHALL catchall element expression * @EXPR_FLAGCMP flagcmp expression */ +__attribute__((packed)) enum expr_types { EXPR_INVALID, EXPR_VERDICT,
@@ -80,6 +81,7 @@ enum expr_types {EXPR_MAX =EXPR_FLAGCMP }; +__attribute__((packed)) enum ops { OP_INVALID, OP_IMPLICIT,
@@ -247,9 +249,9 @@ struct expr {unsigned int flags;const struct datatype *dtype; - enum byteorder byteorder:8; - enum expr_types etype:8; - enum ops op:8; +enum byteorder byteorder; +enum expr_types etype; +enum ops op;unsigned int len;struct cmd *cmd;
include/proto.h +7 -4
@@ -13,6 +13,7 @@* @PROTO_BASE_NETWORK_HDR: network layer header * @PROTO_BASE_TRANSPORT_HDR: transport layer header */ +__attribute__((packed)) enum proto_bases { PROTO_BASE_INVALID, PROTO_BASE_LL_HDR,
@@ -26,6 +27,7 @@ enum proto_bases {extern const char *proto_base_names[];extern const char *proto_base_tokens[]; +__attribute__((packed)) enum icmp_hdr_field_type { PROTO_ICMP_ANY = 0, PROTO_ICMP_ECHO, /* echo and reply */
@@ -52,9 +54,9 @@ struct proto_hdr_template {const struct datatype *dtype;uint16_t offset;uint16_t len; - enum byteorder byteorder:8; +enum byteorder byteorder; enum nft_meta_keys meta_key:8; - enum icmp_hdr_field_type icmp_dep:8; +enum icmp_hdr_field_type icmp_dep; };#define PROTO_HDR_TEMPLATE (__token, __dtype, __byteorder, __offset, __len)\
@@ -77,6 +79,7 @@ struct proto_hdr_template {#define PROTO_UPPER_MAX 16#define PROTO_HDRS_MAX 20 +__attribute__((packed)) enum proto_desc_id { PROTO_DESC_UNKNOWN = 0, PROTO_DESC_AH,
@@ -119,8 +122,8 @@ enum proto_desc_id {*/ struct proto_desc {const char *name; -enum proto_desc_id id:8 ; -enum proto_bases base:8 ; +enum proto_desc_id id; +enum proto_bases base;enum nft_payload_csum_types checksum_type:8 ;uint16_t checksum_key;uint16_t protocol_key;
Signature · 1 line
2.41.0
Pablo Neira Ayuso
maintainerRe: [PATCH nft 8/9] datatype: use __attribute__((packed)) instead of enum bitfields
Quoted 62 lines · Thomas Haller
At some places we use bitfields of those enums, to save space inside the structure. We can achieve that in a better way, by using GCC's __attribute__((packed)) on the enum type.
It's better because a :8 bitfield makes the assumption that all enum values (valid or invalid) fit into that field. With packed enums, we don't need that assumption as the field can hold all possible numbers that the enum type can hold. This reduces the places we need to worry about truncating a value to casts between other types and the enum. Those places already require us to be careful.
On the other hand, previously casting an int (or
uint32_t) likely didn't cause a truncation as the underlying type was large enough. So we could check for invalid enum values after the cast. We might do that at places. For example, we dokey = nftnl_expr_get_u32(nle, NFTNL_EXPR_META_KEY); expr = meta_expr_alloc(loc, key);where we cast from an
uint32_tto an enum without checking. Note thatenum nft_meta_keysis not packed by this patch. But this is an example how things could be wrong. But the bug already exits before: don't make assumption about the underlying enum type and take care of truncation during casts.This makes the change potentially dangerous, and it's hard to be sure that it doesn't uncover bugs (due tow rong assumptions about enum types).
Note that we were already using the GCC-ism __attribute__((packed)) previously, however on a struct and not on an enum. Anyway. It seems unlikely that we support any other compilers than GCC/Clang. Those both support this attribute. We should not worry about portability towards hypothetical compilers (the C standard), unless there is a real compiler that we can use and test and shows a problem with this. Especially when we support both GCC and Clang, which themselves are ubiquitous and accessible to all users (as they also need to build the kernel in the first place).
Signed-off-by Thomas Haller <[email protected]>--- include/datatype.h | 1 + include/expression.h | 8 +++++--- include/proto.h | 11 +++++++---- 3 files changed, 13 insertions(+), 7 deletions(-)include/datatype.h +1
-0@@ -112,6 +112,7 @@ enum datatypes {* @BYTEORDER_HOST_ENDIAN: host endian * @BYTEORDER_BIG_ENDIAN: big endian */ +__attribute__((packed)) enum byteorder { BYTEORDER_INVALID, BYTEORDER_HOST_ENDIAN,include/expression.h +0
-0
include/expression.h line 48
Quoted 4 lines
include/expression.h +1
-0@@ -45,6 +45,7 @@* @EXPR_SET_ELEM_CATCHALL catchall element expression * @EXPR_FLAGCMP flagcmp expression */ +__attribute__((packed))
No need for ((packed)) here, we never send this over the wire.
Quoted 12 lines
include/expression.h +1
-0enum expr_types {EXPR_INVALID ,EXPR_VERDICT ,@@ -80,6 +81,7 @@ enum expr_types {EXPR_MAX =EXPR_FLAGCMP }; +__attribute__((packed)) enum ops { OP_INVALID, OP_IMPLICIT,
include/expression.h struct expr { · line 254
Quoted 9 lines
include/expression.h +3
-3@@ -247,9 +249,9 @@ struct expr {unsigned int flags;const struct datatype *dtype; - enum byteorder byteorder:8; - enum expr_types etype:8; - enum ops op:8; +enum byteorder byteorder; +enum expr_types etype; +enum ops op;
This is saving _a lot of space_ for us, we currently have a problem with memory consumption, this is going in the opposite direction.
I prefer to ditch this patch.
Re: [PATCH nft 8/9] datatype: use __attribute__((packed)) instead of enum bitfields
Quoted 8 lines · Pablo Neira Ayuso
Quoted 1 line · Thomas Haller
+ enum ops op;
This is saving _a lot of
space_for us, we currently have a problem with memory consumption, this is going in the opposite direction.I prefer to ditch this patch.
The packed enums are only one uint8_t large. The memory-saving compared to a :8 bitfield is exactly the same.
Thomas
Pablo Neira Ayuso
maintainerRe: [PATCH nft 8/9] datatype: use __attribute__((packed)) instead of enum bitfields
Quoted 5 lines · Thomas Haller
Quoted 4 lines · Pablo Neira Ayuso
Quoted 1 line · Thomas Haller
+ enum ops op;
Quoted 7 lines
Quoted 4 lines
This is saving _a lot of
space_for us, we currently have a problem with memory consumption, this is going in the opposite direction.I prefer to ditch this patch.
The packed enums are only one
uint8_tlarge. The memory-saving compared to a :8 bitfield is exactly the same.
I see, I am not sure yet I want to give control to the compiler on the layout of these structures, I have been using pahole to shrink them further, specifically struct expr is critical (I have at least one patch to reduce the size of constant expressions to safe % memory with very 13.5%. And location for error reporting is still consuming 48 bytes per struct expr.
Pablo Neira Ayuso
maintainerRe: [PATCH nft 8/9] datatype: use __attribute__((packed)) instead of enum bitfields
In reply to Thomas Hallerinclude/proto.h
Quoted 1 line · Thomas Haller
include/proto.h +0
-0
Not related, but this reminds me, there are duplicated protocol header definitions in include/proto.h that can possibly go away by using libc definitions.
PATCH 9/9 proto: add missing proto_definitions for PROTO_DESC_GENEVE
In reply to Thomas HallerWhile at it, make proto_definitions const. For global variables, this allows the linker to mark the memory as read only. It's just good to do by default.
--- src/proto.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
src/proto.c +2 -1
@@ -1281,7 +1281,7 @@ const struct proto_desc proto_netdev = { }, }; -static const struct proto_desc *proto_definitions[PROTO_DESC_MAX +1 ] = { +static const struct proto_desc *const proto_definitions[PROTO_DESC_MAX +1 ] = { [PROTO_DESC_AH ] = &proto_ah, [PROTO_DESC_ESP ] = &proto_esp, [PROTO_DESC_COMP ] = &proto_comp,
@@ -1300,6 +1300,7 @@ static const struct proto_desc *proto_definitions[PROTO_DESC_MAX + 1] = { [PROTO_DESC_VLAN] = &proto_vlan, [PROTO_DESC_ETHER] = &proto_eth, [PROTO_DESC_VXLAN] = &proto_vxlan, + [PROTO_DESC_GENEVE] = &proto_geneve, [PROTO_DESC_GRE] = &proto_gre, [PROTO_DESC_GRETAP] = &proto_gretap, };
Signature · 1 line
2.41.0
Re: [PATCH nft 9/9] proto: add missing proto_definitions for PROTO_DESC_GENEVE
Quoted 3 lines · Thomas Haller
While at it, make
proto_definitionsconst. For global variables, this allows the linker to mark the memory as read only. It's just good to do by default.
Applied, thanks