Skip to contentSkip to conversation

expr: queue: Use snprintf and SNPRINTF_BUFFER_SIZE in snprintf_default functions.

PATCH Applied Ana Rey 2 participants 4 messages Jun 16, 2014 netfilter

lore
Share & tools
lore

Ana Rey

author
#0 lore raw

PATCH 1/1 expr: queue: Use snprintf and SNPRINTF_BUFFER_SIZE in snprintf_default functions.

Code refactoring to use snprintf and SNPRINTF_BUFFER_SIZE in snprintf_default functions. Also, It adapts to the next syntax in queue (Added in
http://git.netfilter.org/nftables/commit/?id=27619ffbe503ed4d9e59a02e81db9a7ac49d37af)

Here, some examples:

ip test input 31
[ queue num 3-5 bypass fanout]

ip test input 32 31
[ queue num 0]

ip test input 33 32
[ queue num 4]

ip test input 34 33
[ queue num 2-6 bypass]

table ip test {
chain input {
type filter hook input priority 0; queue num 3-5 bypass fanout queue num 0
queue num 4
queue num 2-6 bypass
}
}

---
 src/expr/queue.c | 38 ++++++++++++++++++++++----------------
 1 file changed, 22 insertions(+), 16 deletions(-)
src/expr/queue.c +22 -16
index 78540b6..282dc8b 100644
@@ -187,24 +187,31 @@ static int nft_rule_expr_queue_snprintf_default(char *buf, size_t len,
 						struct nft_rule_expr *e)
 {
 	struct nft_expr_queue *queue = nft_expr_data(e);
-	int ret;
-	int one = 0;
-
-	ret = snprintf(buf, len, "num %u total %u", queue->queuenum,
-		       queue->queues_total);
-	if (queue->flags) {
-		ret += snprintf(buf + ret, len - ret, " options ");
-		if (queue->flags & NFT_QUEUE_FLAG_BYPASS) {
-			ret += snprintf(buf + ret, len - ret, "bypass");
-			one = 1;
+	int ret, size = len, offset = 0;
+	uint16_t total_queues;
+
+	total_queues = queue->queuenum + queue->queues_total -1;
+
+	ret = snprintf(buf + offset, len, "num %u", queue->queuenum);
+
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+	if (queue->queues_total && total_queues != queue->queuenum) {
+		ret = snprintf(buf + offset, len, "-%u", total_queues);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
+	if (e->flags & (1 << NFT_EXPR_QUEUE_FLAGS)) {
+		if (queue->flags & (NFT_QUEUE_FLAG_BYPASS)) {
+			ret = snprintf(buf + offset, len, " bypass");
+			SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 		}
-		if (queue->flags & NFT_QUEUE_FLAG_CPU_FANOUT) {
-			if (one)
-				ret += snprintf(buf + ret, len - ret, ",");
-			ret += snprintf(buf + ret, len - ret, "fanout");
+		if (queue->flags & (NFT_QUEUE_FLAG_CPU_FANOUT)) {
+			ret = snprintf(buf + offset, len, " fanout");
+			SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 		}
 	}
-	return ret;
+	return offset;
 }
 
 static int nft_rule_expr_queue_snprintf_xml(char *buf, size_t len,
@@ -213,7 +220,6 @@ static int nft_rule_expr_queue_snprintf_xml(char *buf, size_t len,
 	int ret, size = len, offset = 0;
 	struct nft_expr_queue *queue = nft_expr_data(e);
 
-
 	if (e->flags & (1 << NFT_EXPR_QUEUE_NUM)) {
 		ret = snprintf(buf + offset, len, "<num>%u</num>",
 			       queue->queuenum);
Signature · 1 line
2.0.0
author
#1 lore raw

PATCH 1/1 tests: Add json and xml test file for queue

New xml and json test files.

---
 tests/jsonfiles/66-rule-queue.json | 1 +
 tests/jsonfiles/67-rule-queue.json | 1 +
 tests/xmlfiles/77-rule-queue.xml   | 1 +
 tests/xmlfiles/78-rule-queue.xml   | 2 ++
 4 files changed, 5 insertions(+)
 create mode 100644 tests/jsonfiles/66-rule-queue.json
 create mode 100644 tests/jsonfiles/67-rule-queue.json
 create mode 100644 tests/xmlfiles/77-rule-queue.xml
 create mode 100644 tests/xmlfiles/78-rule-queue.xml
tests/jsonfiles/66-rule-queue.json new file +1 -0
new file mode 100644
index 0000000..016110e
@@ -0,0 +1 @@
+{"nftables":[{"rule":{"family":"ip","table":"filter","chain":"input","handle":43,"expr":[{"type":"queue","num":4,"total":2,"flags":0}]}}]}
tests/jsonfiles/67-rule-queue.json new file +1 -0
new file mode 100644
index 0000000..0fefd68
@@ -0,0 +1 @@
+{"nftables":[{"rule":{"family":"ip","table":"filter","chain":"input","handle":44,"position":43,"expr":[{"type":"queue","num":4,"total":2,"flags":3}]}}]}
tests/xmlfiles/77-rule-queue.xml new file +1 -0
new file mode 100644
index 0000000..3073b47
@@ -0,0 +1 @@
+<nftables><rule><family>ip</family><table>filter</table><chain>input</chain><handle>43</handle><expr type="queue"><num>4</num><total>2</total><flags>0</flags></expr></rule></nftables>
tests/xmlfiles/78-rule-queue.xml new file +2 -0
new file mode 100644
index 0000000..1876af7
@@ -0,0 +1,2 @@
+<nftables><rule><family>ip</family><table>filter</table><chain>input</chain><handle>44</handle><position>43</position><expr type="queue"><num>4</num><total>2</total><flags>3</flags></expr></rule></nftables>
+
Signature · 1 line
2.0.0
maintainer Applied
#2 lore raw

Re: [libnftnl PATCH] tests: Add json and xml test file for queue

Quoted 1 line · Ana Rey

New xml and json test files.

Applied, thanks.

maintainer Applied
#3 lore raw
In reply to Ana Rey
Quoted 19 lines · Ana Rey

Code refactoring to use snprintf and SNPRINTF_BUFFER_SIZE in snprintf_default functions. Also, It adapts to the next syntax in queue (Added in
http://git.netfilter.org/nftables/commit/?id=27619ffbe503ed4d9e59a02e81db9a7ac49d37af)

Here, some examples:

ip test input 31
[ queue num 3-5 bypass fanout]

ip test input 32 31
[ queue num 0]

ip test input 33 32
[ queue num 4]

ip test input 34 33
[ queue num 2-6 bypass]

Quoted 9 lines

table ip test {
chain input {
type filter hook input priority 0; queue num 3-5 bypass fanout queue num 0
queue num 4
queue num 2-6 bypass
}
}

Applied, thanks Ana.