Skip to contentSkip to conversation

libipt_statistic

PATCH Applied NICOLAS BOULIANE 3 participants 15 messages Jul 2, 2007 – Jul 3, 2007 1 message not in the archive netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libipt_statistic

Hi Patrick,

I think that this calculation is erroneous:
info->u.nth.count = info->u.nth.every - info->u.nth.packet;

ex: --mode nth --every 4 --packet 6
AIUI, we want to drop the 4th packet after 6 packets have pass. Base on this example, we want to match the 10th packet.

But we are gonna match the 7th packet, wich is wrong.
count: -3
every: 3
packet: 6

-3, -2, -1, 0, 1, 2, (3) -- Increment
1, 2, 3, 4, 5, 6, 7 -- nth packet

comments are welcome.
--
I think that my patch introduce space at the beginning of each line, someone could give me a vim hint to avoid that ? thanks. --

acidfu

libipt_statistic.c +2 -2
Index: libipt_statistic.c
===================================================================
@@ -113,7 +113,7 @@
 	if (flags & 0x8 && info->mode != XT_STATISTIC_MODE_NTH)
 		exit_error(PARAMETER_PROBLEM,
 			   "--packet can only be used in nth mode");
-	info->u.nth.count = info->u.nth.every - info->u.nth.packet;
+	info->u.nth.count = -info->u.nth.packet;
 }
 
 /* Prints out the matchinfo. */
@@ -156,7 +156,7 @@
 	print_match(info, "--");
 }
 
-static struct iptables_match statistic = { 
+static struct iptables_match statistic = {
 	.name		= "statistic",
 	.version	= IPTABLES_VERSION,
 	.size		= IPT_ALIGN(sizeof(struct xt_statistic_info)),
#1 lore raw
Quoted 14 lines · NICOLAS BOULIANE

Hi Patrick,

I think that this calculation is erroneous:
info->u.nth.count = info->u.nth.every - info->u.nth.packet;

ex: --mode nth --every 4 --packet 6
AIUI, we want to drop the 4th packet after 6 packets have pass. Base on this example, we want to match the 10th packet.

But we are gonna match the 7th packet, wich is wrong.
count: -3
every: 3
packet: 6

Quoted 4 lines

-3, -2, -1, 0, 1, 2, (3) -- Increment
1, 2, 3, 4, 5, 6, 7 -- nth packet

comments are welcome.

--packet is the initial counter value and must be >= 0 and <= every - 1.

In your example, which packet do you want to drop on the second round? The fourth or the tenth?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

[Don't remove CCs please]

Quoted 7 lines · NICOLAS BOULIANE

Hi Patrick,

First, there's no upper bound check to ensure that --packet value is n-1, so I'm confused.

if (string_to_number(optarg, 0, 0xFFFFFFFF,

                        &info->u.nth.packet) == -1)

Yes, that should be fixed.

Quoted 9 lines

But even if I do:
--every 4 --packet 2
The first time it will match the 3th packet
and all next time it will match the 4th packet, which I dont understand why (technically yes, but philosophically not).

Matching pattern look like:
1,2,(3),4,5,6,(7),8,9,10,(11),12,13,14,(15)

It starts at zero and goes up to n - 1. So for the second packet you would actually use --packet 1. A bit counterintuitive, I agree, but too late to change.

#4 lore raw
Quoted 8 lines · Patrick McHardy
Quoted 6 lines · NICOLAS BOULIANE

But even if I do:
--every 4 --packet 2
The first time it will match the 3th packet
and all next time it will match the 4th packet, which I dont understand why (technically yes, but philosophically not).

Quoted 8 lines
Quoted 2 lines

Matching pattern look like:
1,2,(3),4,5,6,(7),8,9,10,(11),12,13,14,(15)

It starts at zero and goes up to n - 1. So for the second packet you would actually use --packet 1. A bit counterintuitive, I agree, but too late to change.

BTW, if this really bothers you, you could add a new option for --packet (maybe --start) that subtracts one from the starting point.

#5 lore raw
In reply to Patrick McHardy
Quoted 3 lines · Patrick McHardy

It starts at zero and goes up to n - 1. So for the second packet you would actually use --packet 1. A bit counterintuitive, I agree, but too late to change.

with --every 4 --packet 2
[ count = every - packet ]

in the final check:
count = 1
every = 3
packet = 2

I don't see what is starting at zero.

--
acidfu

#6 lore raw
Quoted 9 lines · NICOLAS BOULIANE
Quoted 3 lines · Patrick McHardy

It starts at zero and goes up to n - 1. So for the second packet you would actually use --packet 1. A bit counterintuitive, I agree, but too late to change.

with --every 4 --packet 2
[ count = every - packet ]

Quoted 6 lines

in the final check:
count = 1
every = 3
packet = 2

I don't see what is starting at zero.

The count. --packet n will match the n+1th packet.

#7 lore raw
Quoted 10 lines · Patrick McHardy
Quoted 9 lines · NICOLAS BOULIANE
Quoted 3 lines · Patrick McHardy

It starts at zero and goes up to n - 1. So for the second packet you would actually use --packet 1. A bit counterintuitive, I agree, but too late to change.

with --every 4 --packet 2
[ count = every - packet ]

Quoted 10 lines
Quoted 6 lines

in the final check:
count = 1
every = 3
packet = 2

I don't see what is starting at zero.

The count. --packet n will match the n+1th packet.

i'm not trying to bother you, but there's surely something I dont understand.

In the kernel we have:
if (info->u.nth.count++ == info->u.nth.every)
info->u.nth.count = 0;

--every 4 --packet 2
count = 1, every = 3, packet = 2

1th packet:

          if (1 == 3) { // no match

2th packet:

          if (2 == 3) { // no match

3th packet:

           if (3 == 3) { // match and set __count = 0__

next time we will match the 4th packet.
Is what I should expect ? or there's something wrong.

thanks,
acidfu

#8 lore raw
Quoted 2 lines · NICOLAS BOULIANE

i'm not trying to bother you, but there's surely something I dont understand.

Don't worry :)

Quoted 10 lines

In the kernel we have:
if (info->u.nth.count++ == info->u.nth.every)
info->u.nth.count = 0;

--every 4 --packet 2
count = 1, every = 3, packet = 2

1th packet:

          if (1 == 3) { // no match
Quoted 8 lines

2th packet:

          if (2 == 3) { // no match

3th packet:

           if (3 == 3) { // match and set __count = 0__

next time we will match the 4th packet.
Is what I should expect ? or there's something wrong.

It is. "--every 4" is the period, we'll match every 4th packet. "--packet 2" says you want to match the third (thats the non-obvious part) packet.

The main reason why you would --packet at all is for multiple subsequent rules that don't abort rule traversal:

... --every 4 --packet 0 -j MARK --set-mark 1 (match 1st packet)
... --every 4 --packet 1 -j MARK --set-mark 2 (match 2nd packet)
... --every 4 --packet 2 -j MARK --set-mark 3 (match 3rd packet)
... --every 4 --packet 3 -j MARK --set-mark 4 (match 4th packet)

So for every packet only a single rule will match. You could do something like that for load-balancing for example.

#9 lore raw
Quoted 34 lines · Patrick McHardy
Quoted 2 lines · NICOLAS BOULIANE

i'm not trying to bother you, but there's surely something I dont understand.

Don't worry :)

Quoted 10 lines

In the kernel we have:
if (info->u.nth.count++ == info->u.nth.every)
info->u.nth.count = 0;

--every 4 --packet 2
count = 1, every = 3, packet = 2

1th packet:

          if (1 == 3) { // no match
Quoted 8 lines

2th packet:

          if (2 == 3) { // no match

3th packet:

           if (3 == 3) { // match and set __count = 0__

next time we will match the 4th packet.
Is what I should expect ? or there's something wrong.

It is. "--every 4" is the period, we'll match every 4th packet. "--packet 2" says you want to match the third (thats the non-obvious part) packet.

The main reason why you would --packet at all is for multiple subsequent rules that don't abort rule traversal:

Quoted 9 lines
... --every 4 --packet 0 -j MARK --set-mark 1   (match 1st packet)
... --every 4 --packet 1 -j MARK --set-mark 2   (match 2nd packet)
... --every 4 --packet 2 -j MARK --set-mark 3   (match 3rd packet)
... --every 4 --packet 3 -j MARK --set-mark 4   (match 4th packet)

So for every packet only a single rule will match. You could do something like that for load-balancing for example.

nth-packet / --packet

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16
0 x x x x
1 x x x x
2 x x x
x
3 x x x
x

Now the pattern make sense :)
thank you Patrick,

I'll write a patch for the upperbound
We can also specify --packet without --every,
side effectly --every will be set to 1, should be fix that too I think.

have some suggestions before a begin ?

Signature · 1 line
acidfu
#10 lore raw
Quoted 6 lines · NICOLAS BOULIANE

Now the pattern make sense :)
thank you Patrick,

I'll write a patch for the upperbound
We can also specify --packet without --every,
side effectly --every will be set to 1, should be fix that too I think.

Sounds reasonable.

Quoted 2 lines

have some suggestions before a begin ?

No :)

#11 lore raw
Quoted 9 lines · Patrick McHardy
Quoted 6 lines · NICOLAS BOULIANE

Now the pattern make sense :)
thank you Patrick,

I'll write a patch for the upperbound
We can also specify --packet without --every,
side effectly --every will be set to 1, should be fix that too I think.

Quoted 9 lines

Sounds reasonable.

Quoted 2 lines

have some suggestions before a begin ?

No :)

Signed-off-by Nicolas Bouliane

comments are welcome,
thanks

libipt_statistic.c +9 -0
Index: libipt_statistic.c
===================================================================
@@ -113,6 +113,15 @@
 	if (flags & 0x8 && info->mode != XT_STATISTIC_MODE_NTH)
 		exit_error(PARAMETER_PROBLEM,
 			   "--packet can only be used in nth mode");
+	if ((flags & 0x8) && !(flags & 0x4))
+		exit_error(PARAMETER_PROBLEM,
+			   "--packet can only be used with --every");
+	/* at this point, info->u.nth.every have been decreased. */
+	if (!(info->u.nth.packet >= 0 && info->u.nth.packet <= info->u.nth.every))
+		exit_error(PARAMETER_PROBLEM,
+			  "the --packet p must be 0 <= p <= n-1");
+
+
 	info->u.nth.count = info->u.nth.every - info->u.nth.packet;
 }
 
#12 lore raw
Quoted 5 lines · NICOLAS BOULIANE
Signed-off-by Nicolas Bouliane

comments are welcome,
thanks

inline patches preferred..

libipt_statistic.c line 116

Quoted 9 lines

Index: libipt_statistic.c
===================================================================

libipt_statistic.c +1 -0
@@ -113,6 +113,15 @@
 	if (flags & 0x8 && info->mode != XT_STATISTIC_MODE_NTH)
 		exit_error(PARAMETER_PROBLEM,
 			   "--packet can only be used in nth mode");
+	if ((flags & 0x8) && !(flags & 0x4))

This could perhaps be

	if ((flags & 0xC) == 0x8)


	Jan
#13 lore raw
Quoted 22 lines · Jan Engelhardt
Quoted 5 lines · NICOLAS BOULIANE
Signed-off-by Nicolas Bouliane

comments are welcome,
thanks

inline patches preferred..

libipt_statistic.c (working copy) line 116

Quoted 9 lines

Index: libipt_statistic.c
===================================================================

libipt_statistic.c (working copy) renamed from libipt_statistic.c (revision 6898) +1 -0
@@ -113,6 +113,15 @@
      if (flags & 0x8 && info->mode != XT_STATISTIC_MODE_NTH)
              exit_error(PARAMETER_PROBLEM,
                         "--packet can only be used in nth mode");
+      if ((flags & 0x8) && !(flags & 0x4))

This could perhaps be

Quoted 6 lines
        if ((flags & 0xC) == 0x8)


        Jan

--

same result, less intuitive. why should I do that ?

acidfu

#14 lore raw
Quoted 14 lines · NICOLAS BOULIANE
Quoted 12 lines · Jan Engelhardt

libipt_statistic.c (working copy) line 116

Quoted 9 lines

Index: libipt_statistic.c
===================================================================

libipt_statistic.c (working copy) renamed from libipt_statistic.c (revision 6898) +1 -0
@@ -113,6 +113,15 @@
      if (flags & 0x8 && info->mode != XT_STATISTIC_MODE_NTH)
              exit_error(PARAMETER_PROBLEM,
                         "--packet can only be used in nth mode");
+      if ((flags & 0x8) && !(flags & 0x4))

This could perhaps be

Quoted 8 lines
Quoted 6 lines
        if ((flags & 0xC) == 0x8)


        Jan

same result, less intuitive. why should I do that ?

Indeed. What would make sense is to use a couple of symbolic constants for the flag values.