netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Laura Garcia <nevola@gmail.com>
Cc: netfilter-devel@vger.kernel.org
Subject: Re: [PATCH v2] netfilter: nft_numgen: add increment counter offset value
Date: Mon, 12 Sep 2016 19:21:32 +0200	[thread overview]
Message-ID: <20160912172132.GA29173@salvia> (raw)
In-Reply-To: <20160912170823.GB18041@sonyv>

On Mon, Sep 12, 2016 at 07:08:24PM +0200, Laura Garcia wrote:
> On Mon, Sep 12, 2016 at 06:45:58PM +0200, Pablo Neira Ayuso wrote:
> > On Wed, Sep 07, 2016 at 07:56:49PM +0200, Laura Garcia Liebana wrote:
> > > Add support for an initialization counter value. With this option the
> > > sysadmin is able to start the counter when used with the increment type.
> > > 
> > > Example:
> > > 
> > > 	meta mark set numgen inc mod 2 sum 100
> > > 
> > > This will generate marks with the serie 100, 101, 100, 101, ...
> > > 
> > > Only supported for increment number generation.
> > > 
> > > Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
> > > ---
> > > Changes in v2:
> > > 	- Separate _SUM_ changes with _until_ attribute renaming.
> > > 
> > >  include/uapi/linux/netfilter/nf_tables.h | 2 ++
> > >  net/netfilter/nft_numgen.c               | 9 +++++++--
> > >  2 files changed, 9 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/include/uapi/linux/netfilter/nf_tables.h b/include/uapi/linux/netfilter/nf_tables.h
> > > index 24161e2..00a689d 100644
> > > --- a/include/uapi/linux/netfilter/nf_tables.h
> > > +++ b/include/uapi/linux/netfilter/nf_tables.h
> > > @@ -1128,12 +1128,14 @@ enum nft_trace_types {
> > >   * @NFTA_NG_DREG: destination register (NLA_U32)
> > >   * @NFTA_NG_MODULUS: maximum counter value (NLA_U32)
> > >   * @NFTA_NG_TYPE: operation type (NLA_U32)
> > > + * @NFTA_NG_SUM: Offset to initiate the counter (NLA_U32)
> > >   */
> > >  enum nft_ng_attributes {
> > >  	NFTA_NG_UNSPEC,
> > >  	NFTA_NG_DREG,
> > >  	NFTA_NG_MODULUS,
> > >  	NFTA_NG_TYPE,
> > > +	NFTA_NG_SUM,
> > >  	__NFTA_NG_MAX
> > >  };
> > >  #define NFTA_NG_MAX	(__NFTA_NG_MAX - 1)
> > > diff --git a/net/netfilter/nft_numgen.c b/net/netfilter/nft_numgen.c
> > > index f51a3ed..a5ea3f7 100644
> > > --- a/net/netfilter/nft_numgen.c
> > > +++ b/net/netfilter/nft_numgen.c
> > > @@ -44,6 +44,7 @@ static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = {
> > >  	[NFTA_NG_DREG]		= { .type = NLA_U32 },
> > >  	[NFTA_NG_MODULUS]	= { .type = NLA_U32 },
> > >  	[NFTA_NG_TYPE]		= { .type = NLA_U32 },
> > > +	[NFTA_NG_SUM]		= { .type = NLA_U32 },
> > >  };
> > >  
> > >  static int nft_ng_inc_init(const struct nft_ctx *ctx,
> > > @@ -51,13 +52,17 @@ static int nft_ng_inc_init(const struct nft_ctx *ctx,
> > >  			   const struct nlattr * const tb[])
> > >  {
> > >  	struct nft_ng_inc *priv = nft_expr_priv(expr);
> > > +	u32 sum = 0;
> > > +
> > > +	if (tb[NFTA_NG_SUM])
> > > +		sum = ntohl(nla_get_be32(tb[NFTA_NG_SUM]));
> > >  
> > >  	priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
> > > -	if (priv->modulus == 0)
> > > +	if (priv->modulus == 0 || sum >= priv->modulus)
> > >  		return -ERANGE;
> > >  
> > >  	priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
> > > -	atomic_set(&priv->counter, 0);
> > > +	atomic_set(&priv->counter, sum);
> > 
> > Are you sure this will work?
> > 
> >         nval = (oval + 1 < priv->modulus) ? oval + 1 : 0;
> > 
> > This is just setting priv->counter to an initial value, then it will
> > become zero at some point.
> > 
> 
> I considered that the offset in the case of 'numgen inc' will be only
> applied to initialize the counter to a certain value. Do you mean to
> add the offset in every generated value?
> 
> Should the random counter behave in the same way?

I'm expecting this works just like nft_hash, ie.

        numgen inc mod 2 offset 10

generates: 10, 11, 10, 11, ...

Then, for random:

        numgen random mod 2 offset 10

generates (well, this should actually be random): 10, 10, 11, 10, 11, 10, 10,...

  reply	other threads:[~2016-09-12 17:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-07 17:56 [PATCH v2] netfilter: nft_numgen: add increment counter offset value Laura Garcia Liebana
2016-09-12 16:45 ` Pablo Neira Ayuso
2016-09-12 17:08   ` Laura Garcia
2016-09-12 17:21     ` Pablo Neira Ayuso [this message]
2016-09-12 17:25   ` Laura Garcia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160912172132.GA29173@salvia \
    --to=pablo@netfilter.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=nevola@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).