History

netfilter: nft_queue: add _SREG_QNUM attr to select the queue number

Currently, the user can specify the queue numbers by _QUEUE_NUM and _QUEUE_TOTAL attributes, this is enough in most situations. But acctually, it is not very flexible, for example: tcp dport 80 mapped to queue0 tcp dport 81 mapped to queue1 tcp dport 82 mapped to queue2 In order to do this thing, we must add 3 nft rules, and more mapping meant more rules ... So take one register to select the queue number, then we can add one simple rule to mapping queues, maybe like this: queue num tcp dport map { 80:0, 81:1, 82:2 ... } Florian Westphal also proposed wider usage scenarios: queue num jhash ip saddr . ip daddr mod ... queue num meta cpu ... queue num meta mark ... The last point is how to load a queue number from sreg, although we can use *(u16*)&regs->data[reg] to load the queue number, just like nat expr to load its l4port do. But we will cooperate with hash expr, meta cpu, meta mark expr and so on. They all store the result to u32 type, so cast it to u16 pointer and dereference it will generate wrong result in the big endian system. So just keep it simple, we treat queue number as u32 type, although u16 type is already enough.

Liping Zhang committed 10 years ago

Commit 8061bb51 parent 36b701f2 files · +92 −12Browse files

Unified · Split

2 files
  1. include/uapi/linux/netfilter/nf_tables.h +2 −0
  2. net/netfilter/nft_queue.c +90 −12
include/uapi/linux/netfilter/nf_tables.h+2−0
@@ -894,12 +894,14 @@ enum nft_log_attributes { · 893 unmodified lines
894894 * @NFTA_QUEUE_NUM: netlink queue to send messages to (NLA_U16)
895895 * @NFTA_QUEUE_TOTAL: number of queues to load balance packets on (NLA_U16)
896896 * @NFTA_QUEUE_FLAGS: various flags (NLA_U16)
897+ * @NFTA_QUEUE_SREG_QNUM: source register of queue number (NLA_U32: nft_registers)
897898 */
898899 enum nft_queue_attributes {
899900 NFTA_QUEUE_UNSPEC,
900901 NFTA_QUEUE_NUM,
901902 NFTA_QUEUE_TOTAL,
902903 NFTA_QUEUE_FLAGS,
904+ NFTA_QUEUE_SREG_QNUM,
903905 __NFTA_QUEUE_MAX
904906 };
905907 #define NFTA_QUEUE_MAX (__NFTA_QUEUE_MAX - 1)
net/netfilter/nft_queue.c+90−12
@@ -22,9 +22,10 @@ · 21 unmodified lines
2222 static u32 jhash_initval __read_mostly;
2323
2424 struct nft_queue {
25− u16 queuenum;
26− u16 queues_total;
27− u16 flags;
25+ enum nft_registers sreg_qnum:8;
26+ u16 queuenum;
27+ u16 queues_total;
28+ u16 flags;
2829 };
2930
3031 static void nft_queue_eval(const struct nft_expr *expr,
@@ -54,26 +55,39 @@ static void nft_queue_eval(const struct nft_expr *expr, · 23 unmodified lines
5455 regs->verdict.code = ret;
5556 }
5657
58+static void nft_queue_sreg_eval(const struct nft_expr *expr,
59+ struct nft_regs *regs,
60+ const struct nft_pktinfo *pkt)
61+{
62+ struct nft_queue *priv = nft_expr_priv(expr);
63+ u32 queue, ret;
64+
65+ queue = regs->data[priv->sreg_qnum];
66+
67+ ret = NF_QUEUE_NR(queue);
68+ if (priv->flags & NFT_QUEUE_FLAG_BYPASS)
69+ ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
70+
71+ regs->verdict.code = ret;
72+}
73+
5774 static const struct nla_policy nft_queue_policy[NFTA_QUEUE_MAX + 1] = {
5875 [NFTA_QUEUE_NUM] = { .type = NLA_U16 },
5976 [NFTA_QUEUE_TOTAL] = { .type = NLA_U16 },
6077 [NFTA_QUEUE_FLAGS] = { .type = NLA_U16 },
78+ [NFTA_QUEUE_SREG_QNUM] = { .type = NLA_U32 },
6179 };
6280
6381 static int nft_queue_init(const struct nft_ctx *ctx,
64− const struct nft_expr *expr,
65− const struct nlattr * const tb[])
82+ const struct nft_expr *expr,
83+ const struct nlattr * const tb[])
6684 {
6785 struct nft_queue *priv = nft_expr_priv(expr);
6886 u32 maxid;
6987
70− if (tb[NFTA_QUEUE_NUM] == NULL)
71− return -EINVAL;
72−
73− init_hashrandom(&jhash_initval);
7488 priv->queuenum = ntohs(nla_get_be16(tb[NFTA_QUEUE_NUM]));
7589
76− if (tb[NFTA_QUEUE_TOTAL] != NULL)
90+ if (tb[NFTA_QUEUE_TOTAL])
7791 priv->queues_total = ntohs(nla_get_be16(tb[NFTA_QUEUE_TOTAL]));
7892 else
7993 priv->queues_total = 1;
@@ -85,11 +99,34 @@ static int nft_queue_init(const struct nft_ctx *ctx, · 5 unmodified lines
8599 if (maxid > U16_MAX)
86100 return -ERANGE;
87101
88− if (tb[NFTA_QUEUE_FLAGS] != NULL) {
102+ if (tb[NFTA_QUEUE_FLAGS]) {
103+ priv->flags = ntohs(nla_get_be16(tb[NFTA_QUEUE_FLAGS]));
104+ if (priv->flags & ~NFT_QUEUE_FLAG_MASK)
105+ return -EINVAL;
106+ }
107+ return 0;
108+}
109+
110+static int nft_queue_sreg_init(const struct nft_ctx *ctx,
111+ const struct nft_expr *expr,
112+ const struct nlattr * const tb[])
113+{
114+ struct nft_queue *priv = nft_expr_priv(expr);
115+ int err;
116+
117+ priv->sreg_qnum = nft_parse_register(tb[NFTA_QUEUE_SREG_QNUM]);
118+ err = nft_validate_register_load(priv->sreg_qnum, sizeof(u32));
119+ if (err < 0)
120+ return err;
121+
122+ if (tb[NFTA_QUEUE_FLAGS]) {
89123 priv->flags = ntohs(nla_get_be16(tb[NFTA_QUEUE_FLAGS]));
90124 if (priv->flags & ~NFT_QUEUE_FLAG_MASK)
91125 return -EINVAL;
126+ if (priv->flags & NFT_QUEUE_FLAG_CPU_FANOUT)
127+ return -EOPNOTSUPP;
92128 }
129+
93130 return 0;
94131 }
95132
@@ -108,6 +145,21 @@ nla_put_failure: · 12 unmodified lines
108145 return -1;
109146 }
110147
148+static int
149+nft_queue_sreg_dump(struct sk_buff *skb, const struct nft_expr *expr)
150+{
151+ const struct nft_queue *priv = nft_expr_priv(expr);
152+
153+ if (nft_dump_register(skb, NFTA_QUEUE_SREG_QNUM, priv->sreg_qnum) ||
154+ nla_put_be16(skb, NFTA_QUEUE_FLAGS, htons(priv->flags)))
155+ goto nla_put_failure;
156+
157+ return 0;
158+
159+nla_put_failure:
160+ return -1;
161+}
162+
111163 static struct nft_expr_type nft_queue_type;
112164 static const struct nft_expr_ops nft_queue_ops = {
113165 .type = &nft_queue_type,
@@ -117,9 +169,35 @@ static const struct nft_expr_ops nft_queue_ops = { · 3 unmodified lines
117169 .dump = nft_queue_dump,
118170 };
119171
172+static const struct nft_expr_ops nft_queue_sreg_ops = {
173+ .type = &nft_queue_type,
174+ .size = NFT_EXPR_SIZE(sizeof(struct nft_queue)),
175+ .eval = nft_queue_sreg_eval,
176+ .init = nft_queue_sreg_init,
177+ .dump = nft_queue_sreg_dump,
178+};
179+
180+static const struct nft_expr_ops *
181+nft_queue_select_ops(const struct nft_ctx *ctx,
182+ const struct nlattr * const tb[])
183+{
184+ if (tb[NFTA_QUEUE_NUM] && tb[NFTA_QUEUE_SREG_QNUM])
185+ return ERR_PTR(-EINVAL);
186+
187+ init_hashrandom(&jhash_initval);
188+
189+ if (tb[NFTA_QUEUE_NUM])
190+ return &nft_queue_ops;
191+
192+ if (tb[NFTA_QUEUE_SREG_QNUM])
193+ return &nft_queue_sreg_ops;
194+
195+ return ERR_PTR(-EINVAL);
196+}
197+
120198 static struct nft_expr_type nft_queue_type __read_mostly = {
121199 .name = "queue",
122− .ops = &nft_queue_ops,
200+ .select_ops = &nft_queue_select_ops,
123201 .policy = nft_queue_policy,
124202 .maxattr = NFTA_QUEUE_MAX,
125203 .owner = THIS_MODULE,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t