Skip to contentSkip to conversation

New multiple DSCP match by "-m dscp --dscp-multi value,value,..."

PATCH Kyeong Yoo 3 participants 4 messages Aug 3, 2015 – Aug 4, 2015

Subsystems (2)
lore
Share & tools
lore

Kyeong Yoo

author
#0 lore raw

I found this is useful for me to match multiple DSCP values in a rule.

For example, if you want to handle traffic with a list of DSCP same way, instead of using this:

  -A FORWARD ...cond1... -m dscp --dscp-class AF11 -j TARGET
  -A FORWARD ...cond1... -m dscp --dscp-class AF21 -j TARGET
  -A FORWARD ...cond1... -m dscp --dscp-class AF31 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp 10 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp 20 -j TARGET

you can use:

  -A FORWARD ...cond1... -m dscp --dscp-multi AF11,AF21,AF31 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp-multi 10,20 -j TARGET

This is mainly convenient if you want to have 1-to-1 mapping from your app to iptables rule.
Also you don't need to add up packet counts (for example) from multiple rules.

Does anyone have an idea whether this will be useful for others and worth to push?

Thanks.

commit 97958cdb209a6babc88fdeb10893597dcb8856d5

Author: Kyeong Yoo <[email protected]>
Date:   Mon Jul 27 23:46:05 2015 +1200

[NETFILTER]: support multiple DSCP value match by "-m dscp --dscp-multi"

Current "dscp" match supports one DSCP value in a rule.

This patch enhances "dscp" match with multiple values:

-m dscp --dscp-multi value,value,...

As there are 64 values possible in DSCP field (6 bits), list of values are stored in a 64-bit bitmap.

include/uapi/linux/netfilter/xt_dscp.h +7 -0
index 15f8932..bea99fa 100644
@@ -16,10 +16,17 @@
 #define XT_DSCP_SHIFT	2
 #define XT_DSCP_MAX	0x3f	/* 00111111 */
 
+#define dscp_set_bit(bmap, idx) \
+	(bmap[(idx) >> 5] |= 1U << (idx & 31))
+#define dscp_test_bit(bmap, idx) \
+	(((1U << (idx & 31)) & bmap[(idx) >> 5]) != 0)
+
 /* match info */
 struct xt_dscp_info {
 	__u8 dscp;
 	__u8 invert;
+	__u8 multi;
+	__u32 dscp_bitmap[(XT_DSCP_MAX >> 5) + 1];
 };
 
 struct xt_tos_match_info {
net/netfilter/xt_dscp.c +4 -0
index 64670fc..f127f04 100644
@@ -30,6 +30,8 @@ dscp_mt(const struct sk_buff *skb, struct xt_action_param *par)
 	const struct xt_dscp_info *info = par->matchinfo;
 	u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
 
+	if (info->multi)
+		return (dscp_test_bit(info->dscp_bitmap, dscp) != 0) ^ !!info->invert;
 	return (dscp == info->dscp) ^ !!info->invert;
 }
 
@@ -39,6 +41,8 @@ dscp_mt6(const struct sk_buff *skb, struct xt_action_param *par)
 	const struct xt_dscp_info *info = par->matchinfo;
 	u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
 
+	if (info->multi)
+		return (dscp_test_bit(info->dscp_bitmap, dscp) != 0) ^ !!info->invert;
 	return (dscp == info->dscp) ^ !!info->invert;
 }
 

commit a6370996fd612a538474209195c3fbdd7dfeef76

Author: Kyeong Yoo <[email protected]>
Date:   Wed Jul 29 09:38:51 2015 +1200

extensions: libxt_dscp: allow multiple DSCP value match (--dscp-multi)

Current "dscp" match supports one DSCP value in a rule.

This patch enhances "dscp" match with multiple values ("--dscp-multi" option):

-m dscp --dscp-multi value,value,...

DSCP value should be between 0 and 63 or valid DSCP name like AF11, etc.

extensions/libxt_dscp.c +79 -6

140 lines not on this page · raw message

include/linux/netfilter/xt_dscp.h +7 -0
index 15f8932..bea99fa 100644
@@ -16,10 +16,17 @@
 #define XT_DSCP_SHIFT	2
 #define XT_DSCP_MAX	0x3f	/* 00111111 */
 
+#define dscp_set_bit(bmap, idx) \
+	(bmap[(idx) >> 5] |= 1U << (idx & 31))
+#define dscp_test_bit(bmap, idx) \
+	(((1U << (idx & 31)) & bmap[(idx) >> 5]) != 0)
+
 /* match info */
 struct xt_dscp_info {
 	__u8 dscp;
 	__u8 invert;
+	__u8 multi;
+	__u32 dscp_bitmap[(XT_DSCP_MAX >> 5) + 1];
 };
 
 struct xt_tos_match_info {
#1 lore raw
Quoted 11 lines · Kyeong Yoo

I found this is useful for me to match multiple DSCP values in a rule.

For example, if you want to handle traffic with a list of DSCP same way, instead of using this:

  -A FORWARD ...cond1... -m dscp --dscp-class AF11 -j TARGET
  -A FORWARD ...cond1... -m dscp --dscp-class AF21 -j TARGET
  -A FORWARD ...cond1... -m dscp --dscp-class AF31 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp 10 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp 20 -j TARGET
Quoted 4 lines

you can use:

  -A FORWARD ...cond1... -m dscp --dscp-multi AF11,AF21,AF31 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp-multi 10,20 -j TARGET

We support multiple matches in a rule for long time already:

        -A FORWARD ...cond1...  -m dscp --dscp-class AF11 \
                                -m dscp --dscp-class AF21 \
                                -m dscp --dscp-class AF31 \
                                -j TARGET
maintainer
#2 lore raw
Quoted 14 lines · Pablo Neira Ayuso
Quoted 13 lines · Kyeong Yoo

I found this is useful for me to match multiple DSCP values in a rule.

For example, if you want to handle traffic with a list of DSCP same way, instead of using this:

  -A FORWARD ...cond1... -m dscp --dscp-class AF11 -j TARGET
  -A FORWARD ...cond1... -m dscp --dscp-class AF21 -j TARGET
  -A FORWARD ...cond1... -m dscp --dscp-class AF31 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp 10 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp 20 -j TARGET

you can use:

Quoted 8 lines
Quoted 2 lines
  -A FORWARD ...cond1... -m dscp --dscp-multi AF11,AF21,AF31 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp-multi 10,20 -j TARGET

We support multiple matches in a rule for long time already:

        -A FORWARD ...cond1...  -m dscp --dscp-class AF11 \
                                -m dscp --dscp-class AF21 \
                                -m dscp --dscp-class AF31 \

Yes, but that won't work since this is foo && bar, not foo || bar.

#3 lore raw
Quoted 18 lines · Florian Westphal
Quoted 17 lines · Pablo Neira Ayuso
Quoted 15 lines · Kyeong Yoo

I found this is useful for me to match multiple DSCP values in a rule.

For example, if you want to handle traffic with a list of DSCP same way, instead of using this:

  -A FORWARD ...cond1... -m dscp --dscp-class AF11 -j TARGET
  -A FORWARD ...cond1... -m dscp --dscp-class AF21 -j TARGET
  -A FORWARD ...cond1... -m dscp --dscp-class AF31 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp 10 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp 20 -j TARGET

you can use:

  -A FORWARD ...cond1... -m dscp --dscp-multi AF11,AF21,AF31 -j TARGET
  -A FORWARD ...cond2... -m dscp --dscp-multi 10,20 -j TARGET
Quoted 7 lines
Quoted 5 lines

We support multiple matches in a rule for long time already:

        -A FORWARD ...cond1...  -m dscp --dscp-class AF11 \
                                -m dscp --dscp-class AF21 \
                                -m dscp --dscp-class AF31 \

Yes, but that won't work since this is foo && bar, not foo || bar.

Oh I see. I don't like this combo updates, we may keep receiving patches to do the same thing for other matches in iptables.

nftables resolves this problem through dictionaries, please have a look at that.