From 5573d0146c1ae71ac5b3e4ba6a12c00585646a1a Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Sun, 27 Nov 2016 23:26:56 +0100 Subject: src: support for stateful objects This patch allows you to add, to delete and to get stateful objects, this support two object types: counter and quota. Signed-off-by: Pablo Neira Ayuso --- include/Makefile.am | 1 + include/buffer.h | 1 + include/libnftnl/Makefile.am | 1 + include/libnftnl/object.h | 89 ++++++++++++++++++++++++++++++++++++++++++++ include/obj.h | 55 +++++++++++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 include/libnftnl/object.h create mode 100644 include/obj.h (limited to 'include') diff --git a/include/Makefile.am b/include/Makefile.am index a049e2e..fd4cb40 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -5,6 +5,7 @@ noinst_HEADERS = internal.h \ buffer.h \ data_reg.h \ expr_ops.h \ + obj.h \ linux_list.h \ set.h \ common.h \ diff --git a/include/buffer.h b/include/buffer.h index ab1d468..c571657 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -41,6 +41,7 @@ int nftnl_buf_reg(struct nftnl_buf *b, int type, union nftnl_data_reg *reg, #define BURST "burst" #define CHAIN "chain" #define CODE "code" +#define CONSUMED "consumed" #define DATA "data" #define DEVICE "device" #define DIR "dir" diff --git a/include/libnftnl/Makefile.am b/include/libnftnl/Makefile.am index 457ec95..6dc7b2b 100644 --- a/include/libnftnl/Makefile.am +++ b/include/libnftnl/Makefile.am @@ -2,6 +2,7 @@ pkginclude_HEADERS = batch.h \ table.h \ trace.h \ chain.h \ + object.h \ rule.h \ expr.h \ set.h \ diff --git a/include/libnftnl/object.h b/include/libnftnl/object.h new file mode 100644 index 0000000..074a377 --- /dev/null +++ b/include/libnftnl/object.h @@ -0,0 +1,89 @@ +#ifndef _LIBNFTNL_OBJECT_H_ +#define _LIBNFTNL_OBJECT_H_ + +#include +#include +#include +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + NFTNL_OBJ_TABLE = 0, + NFTNL_OBJ_NAME, + NFTNL_OBJ_TYPE, + NFTNL_OBJ_FAMILY, + NFTNL_OBJ_USE, + NFTNL_OBJ_BASE = 16, + __NFTNL_OBJ_MAX +}; +#define NFTNL_OBJ_MAX (__NFTNL_OBJ_MAX - 1) + +enum { + NFTNL_OBJ_CTR_PKTS = NFTNL_OBJ_BASE, + NFTNL_OBJ_CTR_BYTES, +}; + +enum { + NFTNL_OBJ_QUOTA_BYTES = NFTNL_OBJ_BASE, + NFTNL_OBJ_QUOTA_CONSUMED, + NFTNL_OBJ_QUOTA_FLAGS, +}; + +struct nftnl_obj; + +struct nftnl_obj *nftnl_obj_alloc(void); +void nftnl_obj_free(const struct nftnl_obj *ne); + +bool nftnl_obj_is_set(const struct nftnl_obj *ne, uint16_t attr); +void nftnl_obj_unset(struct nftnl_obj *ne, uint16_t attr); +void nftnl_obj_set_data(struct nftnl_obj *ne, uint16_t attr, const void *data, + uint32_t data_len); +void nftnl_obj_set(struct nftnl_obj *ne, uint16_t attr, const void *data); +void nftnl_obj_set_u32(struct nftnl_obj *ne, uint16_t attr, uint32_t val); +void nftnl_obj_set_u64(struct nftnl_obj *obj, uint16_t attr, uint64_t val); +void nftnl_obj_set_str(struct nftnl_obj *ne, uint16_t attr, const char *str); +const void *nftnl_obj_get_data(struct nftnl_obj *ne, uint16_t attr, + uint32_t *data_len); +const void *nftnl_obj_get(struct nftnl_obj *ne, uint16_t attr); +uint32_t nftnl_obj_get_u32(struct nftnl_obj *ne, uint16_t attr); +uint64_t nftnl_obj_get_u64(struct nftnl_obj *obj, uint16_t attr); +const char *nftnl_obj_get_str(struct nftnl_obj *ne, uint16_t attr); + +void nftnl_obj_nlmsg_build_payload(struct nlmsghdr *nlh, + const struct nftnl_obj *ne); +int nftnl_obj_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_obj *ne); +int nftnl_obj_parse(struct nftnl_obj *ne, enum nftnl_parse_type type, + const char *data, struct nftnl_parse_err *err); +int nftnl_obj_parse_file(struct nftnl_obj *ne, enum nftnl_parse_type type, + FILE *fp, struct nftnl_parse_err *err); +int nftnl_obj_snprintf(char *buf, size_t size, const struct nftnl_obj *ne, + uint32_t type, uint32_t flags); +int nftnl_obj_fprintf(FILE *fp, const struct nftnl_obj *ne, uint32_t type, + uint32_t flags); + +struct nftnl_obj_list; +struct nftnl_obj_list *nftnl_obj_list_alloc(void); +void nftnl_obj_list_free(struct nftnl_obj_list *list); +int nftnl_obj_list_is_empty(struct nftnl_obj_list *list); +void nftnl_obj_list_add(struct nftnl_obj *r, struct nftnl_obj_list *list); +void nftnl_obj_list_add_tail(struct nftnl_obj *r, struct nftnl_obj_list *list); +void nftnl_obj_list_del(struct nftnl_obj *t); +int nftnl_obj_list_foreach(struct nftnl_obj_list *table_list, + int (*cb)(struct nftnl_obj *t, void *data), + void *data); + +struct nftnl_obj_list_iter; +struct nftnl_obj_list_iter *nftnl_obj_list_iter_create(struct nftnl_obj_list *l); +struct nftnl_obj *nftnl_obj_list_iter_next(struct nftnl_obj_list_iter *iter); +void nftnl_obj_list_iter_destroy(struct nftnl_obj_list_iter *iter); + +#ifdef __cplusplusg +} /* extern "C" */ +#endif + +#endif /* _OBJ_H_ */ diff --git a/include/obj.h b/include/obj.h new file mode 100644 index 0000000..edbf023 --- /dev/null +++ b/include/obj.h @@ -0,0 +1,55 @@ +#ifndef _OBJ_OPS_H_ +#define _OBJ_OPS_H_ + +#include +#include "internal.h" + +struct nlattr; +struct nlmsghdr; +struct nftnl_obj; + +struct nftnl_obj { + struct list_head head; + struct obj_ops *ops; + + const char *table; + const char *name; + + uint32_t family; + uint32_t use; + + uint32_t flags; + + union { + struct nftnl_obj_counter { + uint64_t pkts; + uint64_t bytes; + } counter; + struct nftnl_obj_quota { + uint64_t bytes; + uint64_t consumed; + uint32_t flags; + } quota; + } data; +}; + +struct obj_ops { + const char *name; + uint32_t type; + size_t alloc_len; + int max_attr; + int (*set)(struct nftnl_obj *e, uint16_t type, const void *data, uint32_t data_len); + const void *(*get)(const struct nftnl_obj *e, uint16_t type, uint32_t *data_len); + int (*parse)(struct nftnl_obj *e, struct nlattr *attr); + void (*build)(struct nlmsghdr *nlh, const struct nftnl_obj *e); + int (*snprintf)(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_obj *e); + int (*json_parse)(struct nftnl_obj *e, json_t *data, + struct nftnl_parse_err *err); +}; + +extern struct obj_ops obj_ops_counter; +extern struct obj_ops obj_ops_quota; + +#define nftnl_obj_data(obj) (void *)&obj->data + +#endif -- cgit v1.2.3