support for mini-gmp / embedded friendliness
v2 cover support for mini-gmp / embedded friendliness
mini-gmp is a minimal subset of libgmp maintained by the same people While libgmp compiles to >400KB, adding mini-gmp only results in a ~30KB size increase of nft without the need to link to gmplib. This increase usability on embedded platforms optimizing for size and having no other use for libgmp.
This patchset:
* Adds mini-gmp from gmplib 6.0 and adds a configure switch --with-mini-gmp to enable it
* Replaces the use of unsupported functions (gmp_printf, gmp_sscanf) with suitable replacements
* Fixes some minor issues related to the change
Steven Barth (4):
parser: rename VERSION token to HDRVERSION
datatype: use mpz_set_str instead of gmp_sscanf
erec: use stdio vasprintf instead of gmp_vasprintf
build: add --with-mini-gmp switch to use mini-gmp instead of libgmp
INSTALL | 6 + configure.ac | 17 +- include/expression.h | 2 +- include/gmputil.h | 10 + include/mini-gmp.h | 294 ++++ include/utils.h | 4 +- src/Makefile.am | 4 + src/datatype.c | 4 +- src/erec.c | 6 +- src/evaluate.c | 8 +- src/gmputil.c | 57 +- src/mini-gmp.c | 4386 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/parser_bison.y | 6 +- src/scanner.l | 2 +- 14 files changed, 4788 insertions(+), 18 deletions(-) create mode 100644 include/mini-gmp.h create mode 100644 src/mini-gmp.c
Signature · 1 line
2.1.4
PATCH v2 1/4 parser: rename VERSION token to HDRVERSION
A token name of VERSION results in a macro being defined with the same name. This prevents inclusion of config.h in commonly used headers.
--- src/parser_bison.y | 6 +++--- src/scanner.l | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)
src/parser_bison.y +3 -3
@@ -238,7 +238,7 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token OPERATION "operation" %token IP "ip" -%token VERSION "version" +%token HDRVERSION "version" %token HDRLENGTH "hdrlength" %token TOS "tos" %token LENGTH "length"
@@ -1966,7 +1966,7 @@ ip_hdr_expr : IP ip_hdr_field } ; -ip_hdr_field : VERSION { $$ = IPHDR_VERSION; } +ip_hdr_field : HDRVERSION { $$ = IPHDR_VERSION; } | HDRLENGTH { $$ = IPHDR_HDRLENGTH; } | TOS { $$ = IPHDR_TOS; } | LENGTH { $$ = IPHDR_LENGTH; }
@@ -2013,7 +2013,7 @@ ip6_hdr_expr : IP6 ip6_hdr_field } ; -ip6_hdr_field : VERSION { $$ = IP6HDR_VERSION; } +ip6_hdr_field : HDRVERSION { $$ = IP6HDR_VERSION; } | PRIORITY { $$ = IP6HDR_PRIORITY; } | FLOWLABEL { $$ = IP6HDR_FLOWLABEL; } | LENGTH { $$ = IP6HDR_LENGTH; }
src/scanner.l +1 -1
@@ -349,7 +349,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "operation" { return OPERATION; } "ip" { return IP; } -"version" { return VERSION; } +"version" { return HDRVERSION; } "hdrlength" { return HDRLENGTH; } "tos" { return TOS; } "length" { return LENGTH; }
Signature · 1 line
2.1.4
PATCH v2 2/4 datatype: use mpz_set_str instead of gmp_sscanf
In reply to Steven BarthThis simplifies the integer parsing logic and restricts it to functions being part of the mini-gmp subset.
--- src/datatype.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
src/datatype.c +1 -3
@@ -276,11 +276,9 @@ static struct error_record *integer_type_parse(const struct expr *sym, struct expr **res) {mpz_t v; -int len;mpz_init (v); -if (gmp_sscanf (sym->identifier,"%Zu%n" , v, &len) !=1 || - (int )strlen (sym->identifier) != len) { +if (mpz_set_str (v, sym->identifier,0 )) {mpz_clear (v);return error (&sym->location,"Could not parse %s" , sym->dtype->desc);
Signature · 1 line
2.1.4
PATCH v2 3/4 erec: use stdio vasprintf instead of gmp_vasprintf
In reply to Steven BarthUse stdio's vasprintf instead of gmp_vasprintf which is not part of the mini-gmp function subset. Furthermore convert the only gmp-specific user and allow the compiler to verify format-strings.
--- src/erec.c | 6 +++++- src/evaluate.c | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-)
src/erec.c +5 -1
@@ -44,6 +44,7 @@ static void erec_destroy(struct error_record *erec)xfree (erec); } +__attribute__((format (printf,3 ,0 )))struct error_record *erec_vcreate (enum error_record_types type,const struct location *loc,const char *fmt,va_list ap)
@@ -55,10 +56,13 @@ struct error_record *erec_vcreate(enum error_record_types type, erec->num_locations =0 ;erec_add_location (erec, loc); -gmp_vasprintf (&erec->msg, fmt, ap); +if (vasprintf (&erec->msg, fmt, ap) <0 ) + erec->msg =NULL ; +return erec; } +__attribute__((format (printf,3 ,4 )))struct error_record *erec_create (enum error_record_types type,const struct location *loc,const char *fmt, ...)
src/evaluate.c +6 -2
@@ -232,9 +232,13 @@ static int expr_evaluate_value(struct eval_ctx *ctx, struct expr **expr)case TYPE_INTEGER :mpz_init_bitmask (mask, ctx->ectx.len);if (mpz_cmp ((*expr)->value, mask) >0 ) { +char *valstr =mpz_get_str (NULL ,10 , (*expr)->value); +char *rangestr =mpz_get_str (NULL ,10 , mask);expr_error (ctx->msgs, *expr, -"Value %Zu exceeds valid range 0-%Zu" , - (*expr)->value, mask); +"Value %s exceeds valid range 0-%s" , + valstr, rangestr); +free (valstr); +free (rangestr);mpz_clear (mask);return -1 ; }
Signature · 1 line
2.1.4
PATCH v2 4/4 build: add --with-mini-gmp switch to use mini-gmp instead of libgmp
In reply to Steven BarthThis disables linking the >400 KB big libgmp and replace it with the builtin mini-gmp which only increases size by ~30KB.
This is mainly useful for embedded use.
--- INSTALL | 6 + configure.ac | 17 +- include/expression.h | 2 +- include/gmputil.h | 10 + include/mini-gmp.h | 294 ++++ include/utils.h | 4 +- src/Makefile.am | 4 + src/gmputil.c | 57 +- src/mini-gmp.c | 4386 ++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 4772 insertions(+), 8 deletions(-) create mode 100644 include/mini-gmp.h create mode 100644 src/mini-gmp.c
INSTALL +6 -0
@@ -44,6 +44,12 @@ Installation instructions for nftables Disable debugging + --with-mini-gmp + + Use builtin mini-gmp instead of linking with a shared libgmp. + This is useful for embedded platforms optimizing for size and + having no other use for libgmp. Requires --disable-debug. + Suggested configuration options: --prefix=/ --datarootdir=/usr/share Run "make" to compile nftables, "make install" to install it in the
configure.ac +14 -3
@@ -73,8 +73,18 @@ AM_CONDITIONAL([BUILD_PDF], [test "$DBLATEX" == "found"]) PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3]) PKG_CHECK_MODULES([LIBNFTNL], [libnftnl >= 1.0.2]) -AC_CHECK_LIB([gmp], [__gmpz_init], , - AC_MSG_ERROR([No suitable version of libgmp found])) +AC_ARG_WITH([mini-gmp], [AS_HELP_STRING([--with-mini-gmp], + [Use builtin mini-gmp (for embedded builds)])], [], + [with_mini_gmp=no]) +AS_IF([test "x$with_mini_gmp" != xyes], [ +AC_CHECK_LIB([gmp],[__gmpz_init], , AC_MSG_ERROR([No suitable version of libgmp found])) +]) +AM_CONDITIONAL([BUILD_MINIGMP], [test "x$with_mini_gmp" == xyes]) + + +AS_IF([test "x$with_mini_gmp" == xyes -a "x$with_debug" != xno], [ +AC_MSG_ERROR([--with-mini-gmp MUST be used with --disable-debug]) +]) AC_ARG_WITH([cli], [AS_HELP_STRING([--without-cli], [disable interactive CLI (libreadline support)])],
@@ -130,4 +140,5 @@ AC_OUTPUT echo " nft configuration: cli support: ${with_cli} - enable debugging: ${with_debug}" + enable debugging: ${with_debug} + use mini-gmp: ${with_mini_gmp}"
include/expression.h +1 -1
@@ -2,7 +2,7 @@#define NFTABLES_EXPRESSION_H #include <stdbool.h> -#include <gmp.h> +#include <gmputil.h> #include <linux/netfilter/nf_tables.h> #include <nftables.h>
include/gmputil.h +10 -0
@@ -1,7 +1,17 @@#ifndef NFTABLES_GMPUTIL_H #define NFTABLES_GMPUTIL_H +#include <config.h> + +#ifdef HAVE_LIBGMP #include <gmp.h> +#else +#include <mini-gmp.h> +/* mini-gmp doesn't come with gmp_printf, so we use our own minimal variant */ +extern int mpz_printf (const char *format,const mpz_t value); +#define gmp_printf mpz_printf +#endif +#include <asm/byteorder.h> enum mpz_word_order {
include/mini-gmp.h new file +294 -0
294 lines not on this page · raw message
include/utils.h +2 -2
@@ -9,14 +9,14 @@#include <unistd.h> #include <assert.h> #include <list.h> -#include <gmp.h> +#include <gmputil.h> #define BITS_PER_BYTE 8#ifdef DEBUG #define pr_debug(fmt, arg...) gmp_printf(fmt, ##arg)#else -#define pr_debug(fmt, arg...) ({ if (false) gmp_printf(fmt, ##arg); 0; }) +#define pr_debug(fmt, arg...) ({ if (false) {}; 0; })#endif #define __fmtstring (x, y) __attribute__((format(printf, x, y)))
src/Makefile.am +4 -0
@@ -51,4 +51,8 @@ if BUILD_CLI nft_SOURCES += cli.c endif +if BUILD_MINIGMP +nft_SOURCES += mini-gmp.c +endif + nft_LDADD = ${LIBMNL_LIBS} ${LIBNFTNL_LIBS}
src/gmputil.c +55 -2
72 lines not on this page · raw message
src/mini-gmp.c new file +4386 -0
2,547 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 6 lines
2.1.4 -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Part of the diff is not shown here. View the raw message.
Pablo Neira Ayuso
maintainerQuoted 6 lines · Steven Barth
mini-gmp is a minimal subset of libgmp maintained by the same people While libgmp compiles to >400KB, adding mini-gmp only results in a ~30KB size increase of nft without the need to link to gmplib. This increase usability on embedded platforms optimizing for size and having no other use for libgmp.
Quoted 6 lines
This patchset:
* Adds mini-gmp from gmplib 6.0 and adds a configure switch --with-mini-gmp to enable it
* Replaces the use of unsupported functions (gmp_printf,gmp_sscanf) with suitable replacements
* Fixes some minor issues related to the change
I think you mentioned to me already but
configure: error: --with-mini-gmp MUST be used with --disable-debug
any reason to enforce this?
Quoted 6 lines · Pablo Neira Ayuso
I think you mentioned to me already but
configure: error: --with-mini-gmp MUST be used with --disable-debug
any reason to enforce this?
The problem here is that pr_debug expands to gmp_printf which is not part of mini-gmp and the little replacement mpz_printf I added only allows for formatting a single mpz_t (which is enough for all the non-debug cases).
So there are 3 options basically.
1. Try to make gmp_printf standalone and port it from libgmp to mini-gmp.
This looked like a huge and complicated effort and would likely
blow up stuff considerably again.
2. Replace pr_debug's gmp_printf with regular printf and rewrite all the debug-calls to mpz_get_str() and free() which sounds painful.
3. Simply disabling debugging with mini-gmp.
Easy way out without obstructing things too much otherwise.
Pablo Neira Ayuso
maintainerQuoted 14 lines · Steven Barth
Quoted 6 lines · Pablo Neira Ayuso
I think you mentioned to me already but
configure: error: --with-mini-gmp MUST be used with --disable-debug
any reason to enforce this?
The problem here is that
pr_debugexpands togmp_printfwhich is not part of mini-gmp and the little replacementmpz_printfI added only allows for formatting a singlempz_t(which is enough for all the non-debug cases).
Quoted 9 lines
So there are 3 options basically.
1. Try to makegmp_printfstandalone and port it from libgmp to mini-gmp. This looked like a huge and complicated effort and would likely
blow up stuff considerably again.
2. Replace pr_debug'sgmp_printfwith regular printf and rewrite all the debug-calls tompz_get_str()
andfree()which sounds painful.
3. Simply disabling debugging with mini-gmp.
Easy way out without obstructing things too much otherwise.
I see. pr_debug() seems to be only relevant in sec/segtree.c, other spots can be replaced by printf in src/proto.c
I think it would be good for us to have the debugging option enabled in embedded builds at this stage, at least as much as we can get in some easy way. We can ask people to run commands with --debug and paste the result of it.
We can probably have something like:
#ifdef DEBUG && !MINIGMP
#define pr_debug(fmt, arg...) gmp_printf(fmt, ##arg) #else
#define pr_debug(fmt, arg...) ({ if (false) {}; 0; }) #endif
so only segtree debugging is turned off and document this in the INSTALL file.
Pablo Neira Ayuso
maintainerQuoted 13 lines · Steven Barth
mini-gmp is a minimal subset of libgmp maintained by the same people While libgmp compiles to >400KB, adding mini-gmp only results in a ~30KB size increase of nft without the need to link to gmplib. This increase usability on embedded platforms optimizing for size and having no other use for libgmp.
This patchset:
* Adds mini-gmp from gmplib 6.0 and adds a configure switch --with-mini-gmp to enable it
* Replaces the use of unsupported functions (gmp_printf,gmp_sscanf) with suitable replacements
* Fixes some minor issues related to the change
Quoted 5 lines
Steven Barth (4):
parser: rename VERSION token to HDRVERSION
datatype: usempz_set_strinstead ofgmp_sscanf
erec: use stdio vasprintf instead ofgmp_vasprintf
build: add --with-mini-gmp switch to use mini-gmp instead of libgmp
Applied from 1 to 3 so you don't need to resend these 3 initial patches. Thanks.