Skip to contentSkip to conversation

nftables trace support

PATCH Superseded Florian Westphal 4 participants 80 messages Nov 24, 2015 – Nov 26, 2015

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover nftables trace support

Hi.

This is formal first-round submission of the nft trace infrastructure.

Changes since various rfc proposals:
- old nf_log_packet based infrastructure still in place
- serializes interesting parts, such as verdict, rule handle, table and chain names into netlink attributes
- first message contains header data in exta attributes for the three bases (ll, nh, th).

I tested this with nft ingress, bridge and ip families with ipv4 and ipv6 on top.

Sample output:

trace id 848e1d00 bridge packet src 5e:95:99:72:ea:c5 dst 52:54:00:12:34:56 src 192.168.7.1 dst 192.168.7.10 len 84 ttl 64 id 39991 protocol 1 iif eth0 trace id 848e1d00 bridge raw prerouting rule verdict continue iif eth0
trace id 848e1d00 rule limit rate 1/second nftrace set 1
trace id 848e1d00 bridge raw prerouting policy verdict accept iif eth0
trace id 848e1d00 ip filter input rule verdict accept iif br0
trace id 848e1d00 rule ip protocol icmp accept
trace id 848e1c00 bridge packet src 5e:95:99:72:ea:c5 dst 52:54:ea:fe:ad:a6 src dead::47ed:79b0:2f7f dst dead::6438:3436:3a34:6430 len 64 hoplimit 64 protocol 58 iif eth0 trace id 848e1c00 bridge raw prerouting rule verdict continue iif eth0
trace id 848e1c00 rule limit rate 1/second nftrace set 1
trace id 848e1c00 bridge raw prerouting policy verdict accept iif eth0
trace id 836fe400 bridge packet src 5e:95:99:72:ea:c5 dst 52:54:00:12:34:56 arpop 0x1 iif eth0
...

Note that I did not (yet?) add json or xml format for this, I'm not sure its worth it or needed.

First three patches are for the kernel.
Next two are for libnftl (most of the print/format stuff is there). last patch is the nftables patch.

Cheers,
Florian

author maintainer
#1 lore raw

PATCH 1/6 netfilter: nf_tables: extend tracing infrastructure

nft monitor mode can then decode and display this trace data.

Parts of LL/Network/Transport headers are provided as separate attributes.

Otherwise, printing IP address data becomes virtually impossible for userspace since in the case of the netdev family we really don't want userspace to have to know all the possible link layer types and/or sizes just to display/print an ip address.

We also don't want userspace to have to follow ipv6 header chains to get the s/dport info, the kernel already did this work so just follow suit.

---
 include/net/netfilter/nf_tables.h        |   6 +
 include/uapi/linux/netfilter/nf_tables.h |  32 ++++++
 net/netfilter/nf_tables_api.c            | 190 +++++++++++++++++++++++++++++++
 net/netfilter/nf_tables_core.c           |  28 +++--
 net/netfilter/nft_meta.c                 |   3 +
 5 files changed, 252 insertions(+), 7 deletions(-)
include/net/netfilter/nf_tables.h +6 -0
index 4bd7508..5131ad4 100644
@@ -890,6 +890,12 @@ void nft_unregister_chain_type(const struct nf_chain_type *);
 int nft_register_expr(struct nft_expr_type *);
 void nft_unregister_expr(struct nft_expr_type *);
 
+void nf_tables_trace_notify(const struct nft_pktinfo *pkt,
+			    const struct nft_chain *chain,
+			    const struct nft_rule *rule,
+			    u32 verdict,
+			    enum nft_trace_types type);
+
 #define nft_dereference(p)					\
 	nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
 
include/uapi/linux/netfilter/nf_tables.h +32 -0
index d8c8a7c..88bcd00 100644
@@ -83,6 +83,7 @@ enum nft_verdicts {
  * @NFT_MSG_DELSETELEM: delete a set element (enum nft_set_elem_attributes)
  * @NFT_MSG_NEWGEN: announce a new generation, only for events (enum nft_gen_attributes)
  * @NFT_MSG_GETGEN: get the rule-set generation (enum nft_gen_attributes)
+ * @NFT_MSG_TRACE: trace event (enum nft_trace_attributes)
  */
 enum nf_tables_msg_types {
 	NFT_MSG_NEWTABLE,
@@ -102,6 +103,7 @@ enum nf_tables_msg_types {
 	NFT_MSG_DELSETELEM,
 	NFT_MSG_NEWGEN,
 	NFT_MSG_GETGEN,
+	NFT_MSG_TRACE,
 	NFT_MSG_MAX,
 };
 
@@ -970,4 +972,34 @@ enum nft_gen_attributes {
 };
 #define NFTA_GEN_MAX		(__NFTA_GEN_MAX - 1)
 
+enum nft_trace_attibutes {
+	NFTA_TRACE_UNSPEC,
+	NFTA_TRACE_CHAIN,
+	NFTA_TRACE_DEV_TYPE,
+	NFTA_TRACE_ID,
+	NFTA_TRACE_IIF,
+	NFTA_TRACE_OIF,
+	NFTA_TRACE_LL_HEADER,
+	NFTA_TRACE_MARK,
+	NFTA_TRACE_NETWORK_HEADER,
+	NFTA_TRACE_TABLE,
+	NFTA_TRACE_TRANSPORT_HEADER,
+	NFTA_TRACE_TRANSPORT_PROTO,
+	NFTA_TRACE_TYPE,
+	NFTA_TRACE_RULE_HANDLE,
+	NFTA_TRACE_VERDICT,
+	NFTA_TRACE_VLAN_TAG,
+	__NFTA_TRACE_MAX
+};
+#define NFTA_TRACE_MAX (__NFTA_TRACE_MAX - 1)
+
+enum nft_trace_types {
+	NFT_TRACETYPE_UNSPEC,
+	NFT_TRACETYPE_PACKET,
+	NFT_TRACETYPE_POLICY,
+	NFT_TRACETYPE_RETURN,
+	NFT_TRACETYPE_RULE,
+	__NFT_TRACETYPE_MAX
+};
+#define NFT_TRACETYPE_MAX (__NFT_TRACETYPE_MAX - 1)
 #endif /* _LINUX_NF_TABLES_H */
net/netfilter/nf_tables_api.c +190 -0

214 lines not on this page · raw message

net/netfilter/nf_tables_core.c +21 -7
index f3695a4..29a6ca9 100644
@@ -56,10 +56,15 @@ static void __nft_trace_packet(const struct nft_pktinfo *pkt,
 
 static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 				    const struct nft_chain *chain,
-				    int rulenum, enum nft_trace type)
+				    const struct nft_rule *rule,
+				    int rulenum,
+				    u32 verdict,
+				    enum nft_trace_types type)
 {
-	if (unlikely(pkt->skb->nf_trace))
+	if (unlikely(pkt->skb->nf_trace)) {
+		nf_tables_trace_notify(pkt, chain, rule, verdict, type);
 		__nft_trace_packet(pkt, chain, rulenum, type);
+	}
 }
 
 static void nft_cmp_fast_eval(const struct nft_expr *expr,
@@ -151,7 +156,8 @@ next_rule:
 			regs.verdict.code = NFT_CONTINUE;
 			continue;
 		case NFT_CONTINUE:
-			nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
+			nft_trace_packet(pkt, chain, rule, rulenum,
+					 regs.verdict.code, NFT_TRACETYPE_RULE);
 			continue;
 		}
 		break;
@@ -161,7 +167,9 @@ next_rule:
 	case NF_ACCEPT:
 	case NF_DROP:
 	case NF_QUEUE:
-		nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
+		nft_trace_packet(pkt, chain, rule, rulenum,
+				 regs.verdict.code & NF_VERDICT_MASK,
+				 NFT_TRACETYPE_RULE);
 		return regs.verdict.code;
 	}
 
@@ -174,7 +182,8 @@ next_rule:
 		stackptr++;
 		/* fall through */
 	case NFT_GOTO:
-		nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
+		nft_trace_packet(pkt, chain, rule, rulenum,
+				 regs.verdict.code, NFT_TRACETYPE_RULE);
 
 		chain = regs.verdict.chain;
 		goto do_chain;
@@ -182,7 +191,10 @@ next_rule:
 		rulenum++;
 		/* fall through */
 	case NFT_RETURN:
-		nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RETURN);
+		if (stackptr)
+			nft_trace_packet(pkt, chain, rule, rulenum,
+					 regs.verdict.code,
+					 NFT_TRACETYPE_RETURN);
 		break;
 	default:
 		WARN_ON(1);
@@ -196,7 +208,9 @@ next_rule:
 		goto next_rule;
 	}
 
-	nft_trace_packet(pkt, basechain, -1, NFT_TRACE_POLICY);
+	nft_trace_packet(pkt, basechain, NULL, -1,
+			 nft_base_chain(basechain)->policy,
+			 NFT_TRACETYPE_POLICY);
 
 	rcu_read_lock_bh();
 	stats = this_cpu_ptr(rcu_dereference(nft_base_chain(basechain)->stats));
net/netfilter/nft_meta.c +3 -0
index 9dfaf4d..e94526a 100644
@@ -204,6 +204,9 @@ void nft_meta_set_eval(const struct nft_expr *expr,
 		skb->priority = value;
 		break;
 	case NFT_META_NFTRACE:
+		if (skb->nf_trace == 0)
+			nf_tables_trace_notify(pkt, NULL, NULL, 0,
+					       NFT_TRACETYPE_PACKET);
 		skb->nf_trace = 1;
 		break;
 	default:
Signature · 1 line
2.4.10
#2 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 120 lines · Florian Westphal

nft monitor mode can then decode and display this trace data.

Parts of LL/Network/Transport headers are provided as separate attributes.

Otherwise, printing IP address data becomes virtually impossible for userspace since in the case of the netdev family we really don't want userspace to have to know all the possible link layer types and/or sizes just to display/print an ip address.

We also don't want userspace to have to follow ipv6 header chains to get the s/dport info, the kernel already did this work so just follow suit.

---
 include/net/netfilter/nf_tables.h        |   6 +
 include/uapi/linux/netfilter/nf_tables.h |  32 ++++++
 net/netfilter/nf_tables_api.c            | 190 +++++++++++++++++++++++++++++++
 net/netfilter/nf_tables_core.c           |  28 +++--
 net/netfilter/nft_meta.c                 |   3 +
 5 files changed, 252 insertions(+), 7 deletions(-)
include/net/netfilter/nf_tables.h +6 -0
index 4bd7508..5131ad4 100644
@@ -890,6 +890,12 @@ void nft_unregister_chain_type(const struct nf_chain_type *);
 int nft_register_expr(struct nft_expr_type *);
 void nft_unregister_expr(struct nft_expr_type *);
 
+void nf_tables_trace_notify(const struct nft_pktinfo *pkt,
+			    const struct nft_chain *chain,
+			    const struct nft_rule *rule,
+			    u32 verdict,
+			    enum nft_trace_types type);
+
 #define nft_dereference(p)					\
 	nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
 
include/uapi/linux/netfilter/nf_tables.h +32 -0
index d8c8a7c..88bcd00 100644
@@ -83,6 +83,7 @@ enum nft_verdicts {
  * @NFT_MSG_DELSETELEM: delete a set element (enum nft_set_elem_attributes)
  * @NFT_MSG_NEWGEN: announce a new generation, only for events (enum nft_gen_attributes)
  * @NFT_MSG_GETGEN: get the rule-set generation (enum nft_gen_attributes)
+ * @NFT_MSG_TRACE: trace event (enum nft_trace_attributes)
  */
 enum nf_tables_msg_types {
 	NFT_MSG_NEWTABLE,
@@ -102,6 +103,7 @@ enum nf_tables_msg_types {
 	NFT_MSG_DELSETELEM,
 	NFT_MSG_NEWGEN,
 	NFT_MSG_GETGEN,
+	NFT_MSG_TRACE,
 	NFT_MSG_MAX,
 };
 
@@ -970,4 +972,34 @@ enum nft_gen_attributes {
 };
 #define NFTA_GEN_MAX		(__NFTA_GEN_MAX - 1)
 
+enum nft_trace_attibutes {
+	NFTA_TRACE_UNSPEC,
+	NFTA_TRACE_CHAIN,
+	NFTA_TRACE_DEV_TYPE,
+	NFTA_TRACE_ID,
+	NFTA_TRACE_IIF,
+	NFTA_TRACE_OIF,
+	NFTA_TRACE_LL_HEADER,
+	NFTA_TRACE_MARK,
+	NFTA_TRACE_NETWORK_HEADER,
+	NFTA_TRACE_TABLE,
+	NFTA_TRACE_TRANSPORT_HEADER,
+	NFTA_TRACE_TRANSPORT_PROTO,
+	NFTA_TRACE_TYPE,
+	NFTA_TRACE_RULE_HANDLE,
+	NFTA_TRACE_VERDICT,
+	NFTA_TRACE_VLAN_TAG,
+	__NFTA_TRACE_MAX
+};
+#define NFTA_TRACE_MAX (__NFTA_TRACE_MAX - 1)
+
+enum nft_trace_types {
+	NFT_TRACETYPE_UNSPEC,
+	NFT_TRACETYPE_PACKET,
+	NFT_TRACETYPE_POLICY,
+	NFT_TRACETYPE_RETURN,
+	NFT_TRACETYPE_RULE,
+	__NFT_TRACETYPE_MAX
+};
+#define NFT_TRACETYPE_MAX (__NFT_TRACETYPE_MAX - 1)
 #endif /* _LINUX_NF_TABLES_H */
net/netfilter/nf_tables_api.c +6 -0
index 93cc473..25d8168 100644
@@ -9,6 +9,8 @@
  */
 
 #include <linux/module.h>
+#include <linux/hash.h>
+#include <linux/if_vlan.h>
 #include <linux/init.h>
 #include <linux/list.h>
 #include <linux/skbuff.h>
@@ -21,6 +23,10 @@
 #include <net/net_namespace.h>
 #include <net/sock.h>
 
+#define NFT_TRACETYPE_LL_HSIZE		20
+#define NFT_TRACETYPE_NETWORK_HSIZE	32
+#define NFT_TRACETYPE_TRANSPORT_HSIZE	 4
+
 static LIST_HEAD(nf_tables_expressions);
 
 /**

net/netfilter/nf_tables_api.c nla_put_failure: · line 479

Quoted 6 lines
net/netfilter/nf_tables_api.c +3 -0
@@ -468,6 +474,84 @@ nla_put_failure:
 	return -1;
 }
 
+static bool trace_notify_put_data(struct sk_buff *nlskb, u16 type,
+				  const struct sk_buff *skb,
+				  int off, unsigned int plen)

Minor nitpick: Probably you can rename this to _fill_*_info for consistency with other nf_tables netlink code.

Quoted 64 lines
net/netfilter/nf_tables_api.c +64 -0
+{
+	struct nlattr *nla;
+
+	if (skb_tailroom(nlskb) < nla_total_size(plen))
+		return false;
+
+	nla = (struct nlattr *)skb_put(nlskb, nla_total_size(plen));
+	nla->nla_type = type;
+	nla->nla_len = nla_attr_size(plen);
+
+	if (skb_copy_bits(skb, off, nla_data(nla), plen))
+		return false;
+
+	return true;
+}
+
+static bool
+trace_notify_put_packet(struct sk_buff *nlskb, const struct nft_pktinfo *pkt)
+{
+	const struct sk_buff *skb = pkt->skb;
+	unsigned int plen = min_t(unsigned int,
+				  pkt->xt.thoff - skb_network_offset(skb),
+				  NFT_TRACETYPE_NETWORK_HSIZE);
+	int mac_off;
+
+	if (plen >= 20u && /* minimum iphdr size */
+	    !trace_notify_put_data(nlskb, NFTA_TRACE_NETWORK_HEADER,
+				   skb, skb_network_offset(skb), plen))
+		return false;
+
+	if (nla_put_u8(nlskb, NFTA_TRACE_TRANSPORT_PROTO, pkt->tprot))
+		return false;
+
+	plen = min_t(unsigned int, skb->len - pkt->xt.thoff,
+		     NFT_TRACETYPE_TRANSPORT_HSIZE);
+
+	if (plen >= sizeof(u32) &&
+	    !trace_notify_put_data(nlskb, NFTA_TRACE_TRANSPORT_HEADER,
+				   skb, pkt->xt.thoff, plen))
+		return false;
+
+	switch (pkt->pf) {
+	case NFPROTO_ARP: /* fallthrough */
+	case NFPROTO_BRIDGE:
+		break;
+	case NFPROTO_NETDEV:
+		if (WARN_ON_ONCE(!skb->dev))
+			break;
+		if (nla_put_be16(nlskb, NFTA_TRACE_DEV_TYPE,
+				 htons(skb->dev->type)))
+			return false;
+		break;
+	default:
+		return true;
+	}
+
+	if (skb_vlan_tag_get(skb) &&
+	    !nla_put_be16(nlskb, NFTA_TRACE_VLAN_TAG,
+			  htons(skb_vlan_tag_get(skb))))
+		return false;
+
+	if (!skb_mac_header_was_set(skb))
+		return true;
+

net/netfilter/nf_tables_api.c nla_put_failure: · line 553

Quoted 10 lines
net/netfilter/nf_tables_api.c +10 -0
+	plen = min_t(unsigned int,
+		     skb->data - skb_mac_header(skb), NFT_TRACETYPE_LL_HSIZE);
+	mac_off = skb_mac_header(skb) - skb->data;
+
+	if (plen && !trace_notify_put_data(nlskb, NFTA_TRACE_LL_HEADER,
+					   skb, mac_off, plen))
+		return false;
+
+	return true;
+}

Do you think we can place all this new netlink code in net/netfilter/nf_tables_trace.c ? So we leave in the core file only our classifier engine.

I like the attribute definition rename in nf_tables.h, but this code we can probably place it away from here.

Quoted 170 lines
net/netfilter/nf_tables_api.c +106 -0
 static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
 {
 	struct sk_buff *skb;
@@ -499,6 +583,112 @@ err:
 	return err;
 }
 
+void nf_tables_trace_notify(const struct nft_pktinfo *pkt,
+			    const struct nft_chain *chain,
+			    const struct nft_rule *rule,
+			    u32 verdict,
+			    enum nft_trace_types type)
+{
+	struct nfgenmsg *nfmsg;
+	struct nlmsghdr *nlh;
+	struct sk_buff *skb;
+	unsigned int size;
+	int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_TRACE;
+
+	if (!nfnetlink_has_listeners(pkt->net, NFNLGRP_NFTABLES))
+		return;
+
+	/* Unlike other notifiers we need GFP_ATOMIC so use actual size
+	 * needed instead of NLMSG_GOODSIZE.
+	 */
+	size = nlmsg_total_size(sizeof(struct nfgenmsg))
+		+ nla_total_size(sizeof(__be32))	/* trace type */
+		+ nla_total_size(NFT_TABLE_MAXNAMELEN)
+		+ nla_total_size(NFT_CHAIN_MAXNAMELEN)
+		+ nla_total_size(sizeof(u32))	/* iif */
+		+ nla_total_size(sizeof(u32))	/* oif */
+		+ nla_total_size(sizeof(u32))	/* id */
+		+ nla_total_size(sizeof(u32))	/* mark */
+		+ nla_total_size(sizeof(u32))	/* verdict */
+		+ nla_total_size(sizeof(__be64)); /* rule handle */
+
+	switch (type) {
+	case NFT_TRACETYPE_PACKET:
+		size += nla_total_size(NFT_TRACETYPE_NETWORK_HSIZE)
+			+ nla_total_size(NFT_TRACETYPE_TRANSPORT_HSIZE)
+			+ nla_total_size(NFT_TRACETYPE_LL_HSIZE)
+			+ nla_total_size(sizeof(__be16)) /* vlan tag */
+			+ nla_total_size(sizeof(__be16)) /* device type */
+			+ nla_total_size(sizeof(__u8));  /* transport prot */
+		break;
+	default:
+		break;
+	}
+
+	skb = nlmsg_new(size, GFP_ATOMIC);
+	if (!skb)
+		return;
+
+	nlh = nlmsg_put(skb, 0, 0, event, sizeof(struct nfgenmsg), 0);
+	if (!nlh)
+		goto nla_put_failure;
+
+	nfmsg = nlmsg_data(nlh);
+	nfmsg->nfgen_family	= pkt->pf;
+	nfmsg->version		= NFNETLINK_V0;
+	nfmsg->res_id		= htons(pkt->net->nft.base_seq & 0xffff);
+
+	if (nla_put_be32(skb, NFTA_TRACE_TYPE, htonl(type)))
+		goto nla_put_failure;
+
+	if (nla_put_be32(skb, NFTA_TRACE_ID, htonl(hash32_ptr(pkt->skb))))
+		goto nla_put_failure;
+
+	if (chain) {
+		if (nla_put_string(skb, NFTA_TRACE_TABLE, chain->table->name))
+			goto nla_put_failure;
+		if (nla_put_string(skb, NFTA_TRACE_CHAIN, chain->name))
+			goto nla_put_failure;
+	}
+
+	if (rule && nla_put_be64(skb, NFTA_TRACE_RULE_HANDLE,
+				 cpu_to_be64(rule->handle)))
+		goto nla_put_failure;
+
+	if (pkt->in &&
+	    nla_put_be32(skb, NFTA_TRACE_IIF, htonl(pkt->in->ifindex)))
+		goto nla_put_failure;
+	if (pkt->out &&
+	    nla_put_be32(skb, NFTA_TRACE_OIF, htonl(pkt->out->ifindex)))
+		goto nla_put_failure;
+	if (pkt->skb->mark &&
+	    nla_put_be32(skb, NFTA_TRACE_MARK, htonl(pkt->skb->mark)))
+		goto nla_put_failure;
+
+	switch (type) {
+	case NFT_TRACETYPE_POLICY:
+	case NFT_TRACETYPE_RETURN:
+	case NFT_TRACETYPE_RULE:
+		if (nla_put_be32(skb, NFTA_TRACE_VERDICT, htonl(verdict)))
+			goto nla_put_failure;
+		break;
+	case NFT_TRACETYPE_PACKET:
+		if (!trace_notify_put_packet(skb, pkt))
+			goto nla_put_failure;
+		break;
+	default:
+		break;
+	}
+
+	nlmsg_end(skb, nlh);
+	nfnetlink_send(skb, pkt->net, 0, NFNLGRP_NFTABLES, 0, GFP_ATOMIC);
+	return;
+
+ nla_put_failure:
+	WARN_ON_ONCE(1);
+	kfree_skb(skb);
+}
+
 static int nf_tables_dump_tables(struct sk_buff *skb,
 				 struct netlink_callback *cb)
 {
net/netfilter/nf_tables_core.c +14 -5
index f3695a4..29a6ca9 100644
@@ -56,10 +56,15 @@ static void __nft_trace_packet(const struct nft_pktinfo *pkt,
 
 static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 				    const struct nft_chain *chain,
-				    int rulenum, enum nft_trace type)
+				    const struct nft_rule *rule,
+				    int rulenum,
+				    u32 verdict,
+				    enum nft_trace_types type)
 {
-	if (unlikely(pkt->skb->nf_trace))
+	if (unlikely(pkt->skb->nf_trace)) {
+		nf_tables_trace_notify(pkt, chain, rule, verdict, type);
 		__nft_trace_packet(pkt, chain, rulenum, type);
+	}
 }
 
 static void nft_cmp_fast_eval(const struct nft_expr *expr,
@@ -151,7 +156,8 @@ next_rule:
 			regs.verdict.code = NFT_CONTINUE;
 			continue;
 		case NFT_CONTINUE:
-			nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
+			nft_trace_packet(pkt, chain, rule, rulenum,
+					 regs.verdict.code, NFT_TRACETYPE_RULE);
 			continue;
 		}
 		break;
@@ -161,7 +167,9 @@ next_rule:
 	case NF_ACCEPT:
 	case NF_DROP:
 	case NF_QUEUE:
-		nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
+		nft_trace_packet(pkt, chain, rule, rulenum,
+				 regs.verdict.code & NF_VERDICT_MASK,
+				 NFT_TRACETYPE_RULE);
 		return regs.verdict.code;
 	}
 
@@ -174,7 +182,8 @@ next_rule:
 		stackptr++;
 		/* fall through */
 	case NFT_GOTO:
-		nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
+		nft_trace_packet(pkt, chain, rule, rulenum,
+				 regs.verdict.code, NFT_TRACETYPE_RULE);
 
 		chain = regs.verdict.chain;
 		goto do_chain;

net/netfilter/nf_tables_core.c next_rule: · line 194

Quoted 5 lines
net/netfilter/nf_tables_core.c +1 -1
@@ -182,7 +191,10 @@ next_rule:
 		rulenum++;
 		/* fall through */
 	case NFT_RETURN:
-		nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RETURN);
+		if (stackptr)

Why this new branch?

Quoted 6 lines
net/netfilter/nf_tables_core.c +3 -0
+			nft_trace_packet(pkt, chain, rule, rulenum,
+					 regs.verdict.code,
+					 NFT_TRACETYPE_RETURN);
 		break;
 	default:
 		WARN_ON(1);
author maintainer
#3 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 15 lines · Pablo Neira Ayuso
Quoted 14 lines · Florian Westphal

nft monitor mode can then decode and display this trace data.

Parts of LL/Network/Transport headers are provided as separate attributes.

Otherwise, printing IP address data becomes virtually impossible for userspace since in the case of the netdev family we really don't want userspace to have to know all the possible link layer types and/or sizes just to display/print an ip address.

We also don't want userspace to have to follow ipv6 header chains to get the s/dport info, the kernel already did this work so just follow suit.

Quoted 9 lines

net/netfilter/nf_tables_api.c nla_put_failure: · line 479

Quoted 6 lines
---
 include/net/netfilter/nf_tables.h        |   6 +
net/netfilter/nf_tables_api.c +3 -0
+static bool trace_notify_put_data(struct sk_buff *nlskb, u16 type,
+				  const struct sk_buff *skb,
+				  int off, unsigned int plen)

Minor nitpick: Probably you can rename this to _fill_*_info for consistency with other nf_tables netlink code.

Sure, will do.

Quoted 6 lines

Do you think we can place all this new netlink code in net/netfilter/nf_tables_trace.c ? So we leave in the core file only our classifier engine.

I like the attribute definition rename in nf_tables.h, but this code we can probably place it away from here.

Ok, I'll see if this is doable without exporting half a dozen of symbols.

Quoted 6 lines

net/netfilter/nf_tables_core.c next_rule: · line 194

Quoted 4 lines

static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)

net/netfilter/nf_tables_core.c +1 -1
 	case NFT_RETURN:
-		nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RETURN);
+		if (stackptr)

Why this new branch?

Right, I should move it to extra patch. I think its buggy without the extra if (stackptr) test.

If stackptr is 0, then we're returning from a base chain, i.e. the policy is evaluated and we have another trace for that.

For old code its not really imporant if rulenum counter is wrong/off, but for the new infra its important that the *rule ptr is valid.

#4 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 10 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso

Do you think we can place all this new netlink code in net/netfilter/nf_tables_trace.c ? So we leave in the core file only our classifier engine.

I like the attribute definition rename in nf_tables.h, but this code we can probably place it away from here.

Ok, I'll see if this is doable without exporting half a dozen of symbols.

If you place this in this core, we should need to export any symbol.

nf_tables-objs += nf_tables_core.o nf_tables_api.o nf_tables_trace.o

And I would expect probably only functions to enable/disabling tracing and to notify the trace via netlink.

Let me know, thanks.

#5 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 12 lines · Pablo Neira Ayuso
Quoted 10 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso

Do you think we can place all this new netlink code in net/netfilter/nf_tables_trace.c ? So we leave in the core file only our classifier engine.

I like the attribute definition rename in nf_tables.h, but this code we can probably place it away from here.

Ok, I'll see if this is doable without exporting half a dozen of symbols.

Quoted 6 lines

If you place this in this core, we should need to export any symbol.

nf_tables-objs += nf_tables_core.o nf_tables_api.o nf_tables_trace.o

And I would expect probably only functions to enable/disabling tracing and to notify the trace via netlink.

I think we can also avoid the enable/disable exports. Having meta optional doesn't make too much sense to me, it provides very fundamental features like interface matching which I don't think anyone can reasonably disable.

author maintainer
#6 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

In reply to Pablo Neira Ayuso
Quoted 3 lines · Pablo Neira Ayuso

If you place this in this core, we should need to export any symbol.

nf_tables-objs += nf_tables_core.o nf_tables_api.o nf_tables_trace.o

Argh, I'm a moron, I misunderstood what you meant. Sure, that makes sense. Will do it this way.

#7 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

In reply to Florian Westphal
Quoted 89 lines · Florian Westphal
net/netfilter/nf_tables_api.c +89 -0
+void nf_tables_trace_notify(const struct nft_pktinfo *pkt,
+			    const struct nft_chain *chain,
+			    const struct nft_rule *rule,
+			    u32 verdict,
+			    enum nft_trace_types type)
+{
+	struct nfgenmsg *nfmsg;
+	struct nlmsghdr *nlh;
+	struct sk_buff *skb;
+	unsigned int size;
+	int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_TRACE;
+
+	if (!nfnetlink_has_listeners(pkt->net, NFNLGRP_NFTABLES))
+		return;
+
+	/* Unlike other notifiers we need GFP_ATOMIC so use actual size
+	 * needed instead of NLMSG_GOODSIZE.
+	 */
+	size = nlmsg_total_size(sizeof(struct nfgenmsg))
+		+ nla_total_size(sizeof(__be32))	/* trace type */
+		+ nla_total_size(NFT_TABLE_MAXNAMELEN)
+		+ nla_total_size(NFT_CHAIN_MAXNAMELEN)
+		+ nla_total_size(sizeof(u32))	/* iif */
+		+ nla_total_size(sizeof(u32))	/* oif */
+		+ nla_total_size(sizeof(u32))	/* id */
+		+ nla_total_size(sizeof(u32))	/* mark */
+		+ nla_total_size(sizeof(u32))	/* verdict */
+		+ nla_total_size(sizeof(__be64)); /* rule handle */
+
+	switch (type) {
+	case NFT_TRACETYPE_PACKET:
+		size += nla_total_size(NFT_TRACETYPE_NETWORK_HSIZE)
+			+ nla_total_size(NFT_TRACETYPE_TRANSPORT_HSIZE)
+			+ nla_total_size(NFT_TRACETYPE_LL_HSIZE)
+			+ nla_total_size(sizeof(__be16)) /* vlan tag */
+			+ nla_total_size(sizeof(__be16)) /* device type */
+			+ nla_total_size(sizeof(__u8));  /* transport prot */
+		break;
+	default:
+		break;
+	}
+
+	skb = nlmsg_new(size, GFP_ATOMIC);
+	if (!skb)
+		return;
+
+	nlh = nlmsg_put(skb, 0, 0, event, sizeof(struct nfgenmsg), 0);
+	if (!nlh)
+		goto nla_put_failure;
+
+	nfmsg = nlmsg_data(nlh);
+	nfmsg->nfgen_family	= pkt->pf;
+	nfmsg->version		= NFNETLINK_V0;
+	nfmsg->res_id		= htons(pkt->net->nft.base_seq & 0xffff);
+
+	if (nla_put_be32(skb, NFTA_TRACE_TYPE, htonl(type)))
+		goto nla_put_failure;
+
+	if (nla_put_be32(skb, NFTA_TRACE_ID, htonl(hash32_ptr(pkt->skb))))
+		goto nla_put_failure;
+
+	if (chain) {
+		if (nla_put_string(skb, NFTA_TRACE_TABLE, chain->table->name))
+			goto nla_put_failure;
+		if (nla_put_string(skb, NFTA_TRACE_CHAIN, chain->name))
+			goto nla_put_failure;
+	}
+
+	if (rule && nla_put_be64(skb, NFTA_TRACE_RULE_HANDLE,
+				 cpu_to_be64(rule->handle)))
+		goto nla_put_failure;
+
+	if (pkt->in &&
+	    nla_put_be32(skb, NFTA_TRACE_IIF, htonl(pkt->in->ifindex)))
+		goto nla_put_failure;
+	if (pkt->out &&
+	    nla_put_be32(skb, NFTA_TRACE_OIF, htonl(pkt->out->ifindex)))
+		goto nla_put_failure;
+	if (pkt->skb->mark &&
+	    nla_put_be32(skb, NFTA_TRACE_MARK, htonl(pkt->skb->mark)))
+		goto nla_put_failure;
+
+	switch (type) {
+	case NFT_TRACETYPE_POLICY:
+	case NFT_TRACETYPE_RETURN:
+	case NFT_TRACETYPE_RULE:
+		if (nla_put_be32(skb, NFTA_TRACE_VERDICT, htonl(verdict)))
+			goto nla_put_failure;
+		break;

net/netfilter/nf_tables_api.c err: · line 684

Quoted 10 lines
net/netfilter/nf_tables_api.c +10 -0
+	case NFT_TRACETYPE_PACKET:
+		if (!trace_notify_put_packet(skb, pkt))
+			goto nla_put_failure;
+		break;
+	default:
+		break;
+	}
+
+	nlmsg_end(skb, nlh);
+	nfnetlink_send(skb, pkt->net, 0, NFNLGRP_NFTABLES, 0, GFP_ATOMIC);

BTW, do we really want to use the default NFNLGRP_NFTABLES group?

multicast group provide a simple way for filtering out what you don't need from kernelspace. And you can still subscribe both groups NFNLGRP_NFTABLES and NFNLGRP_NFTABLES_TRACE.

I'm telling this when thinking of nft-sync. Why should it be receiving this spamming tracing events when it only cares about ruleset updates?

And specifically netlink bandwidth is limited from packet context.

author maintainer
#8 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 8 lines · Pablo Neira Ayuso

BTW, do we really want to use the default NFNLGRP_NFTABLES group?

multicast group provide a simple way for filtering out what you don't need from kernelspace. And you can still subscribe both groups NFNLGRP_NFTABLES and NFNLGRP_NFTABLES_TRACE.

I'm telling this when thinking of nft-sync. Why should it be receiving this spamming tracing events when it only cares about ruleset updates?

I can add a new group but nftables will need to subscribe to both in trace mode since we need to see new rules...

#9 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 7 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso

BTW, do we really want to use the default NFNLGRP_NFTABLES group?

multicast group provide a simple way for filtering out what you don't need from kernelspace. And you can still subscribe both groups NFNLGRP_NFTABLES and NFNLGRP_NFTABLES_TRACE.

Quoted 5 lines
Quoted 2 lines

I'm telling this when thinking of nft-sync. Why should it be receiving this spamming tracing events when it only cares about ruleset updates?

I can add a new group but nftables will need to subscribe to both in trace mode since we need to see new rules...

I agree with Pablo. Under high load this has the potential to decrease reliability of rule notifications a lot for any interested listener. Since tracing is a debugging tool I don't think we should do that.

BTW and completely unrelated to the netlink group: I think it would be nicer to have a "trace" keyword instead of "nftrace set 1" in the nft frontend.

#10 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 3 lines · Patrick McHardy

BTW and completely unrelated to the netlink group: I think it would be nicer to have a "trace" keyword instead of "nftrace set 1" in the nft frontend.

I like the idea of having this "trace" statement.

#11 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 6 lines · Pablo Neira Ayuso
Quoted 3 lines · Patrick McHardy

BTW and completely unrelated to the netlink group: I think it would be nicer to have a "trace" keyword instead of "nftrace set 1" in the nft frontend.

I like the idea of having this "trace" statement.

Or you mean only "trace" keyword from the parser, not a statement, right?

#12 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 10 lines · Pablo Neira Ayuso
Quoted 6 lines · Pablo Neira Ayuso
Quoted 3 lines · Patrick McHardy

BTW and completely unrelated to the netlink group: I think it would be nicer to have a "trace" keyword instead of "nftrace set 1" in the nft frontend.

I like the idea of having this "trace" statement.

Or you mean only "trace" keyword from the parser, not a statement, right?

Probably, we can internally translate it to "meta set trace = 1" in the parser and translate it back during output.

#13 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

In reply to Florian Westphal
Quoted 7 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso

BTW, do we really want to use the default NFNLGRP_NFTABLES group?

multicast group provide a simple way for filtering out what you don't need from kernelspace. And you can still subscribe both groups NFNLGRP_NFTABLES and NFNLGRP_NFTABLES_TRACE.

Quoted 5 lines
Quoted 2 lines

I'm telling this when thinking of nft-sync. Why should it be receiving this spamming tracing events when it only cares about ruleset updates?

I can add a new group but nftables will need to subscribe to both in trace mode since we need to see new rules...

I'm talking about applications that may not need to subscribe both, eg. nft-sync will only need to subscribe to rule updates, he doesn't care about traces.

I think nft should have the ability to subscribe only rule updates only as well.

You can subscribe as many groups at the same time in netlink as you want.

#14 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

In reply to Florian Westphal
Quoted 18 lines · Florian Westphal
net/netfilter/nf_tables_api.c +18 -0
+void nf_tables_trace_notify(const struct nft_pktinfo *pkt,
+			    const struct nft_chain *chain,
+			    const struct nft_rule *rule,
+			    u32 verdict,
+			    enum nft_trace_types type)
+{
+	struct nfgenmsg *nfmsg;
+	struct nlmsghdr *nlh;
+	struct sk_buff *skb;
+	unsigned int size;
+	int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_TRACE;
+
+	if (!nfnetlink_has_listeners(pkt->net, NFNLGRP_NFTABLES))
+		return;
+
+	/* Unlike other notifiers we need GFP_ATOMIC so use actual size
+	 * needed instead of NLMSG_GOODSIZE.
+	 */

net/netfilter/nf_tables_api.c err: · line 613

Quoted 10 lines
net/netfilter/nf_tables_api.c +10 -0
+	size = nlmsg_total_size(sizeof(struct nfgenmsg))
+		+ nla_total_size(sizeof(__be32))	/* trace type */
+		+ nla_total_size(NFT_TABLE_MAXNAMELEN)
+		+ nla_total_size(NFT_CHAIN_MAXNAMELEN)
+		+ nla_total_size(sizeof(u32))	/* iif */
+		+ nla_total_size(sizeof(u32))	/* oif */
+		+ nla_total_size(sizeof(u32))	/* id */
+		+ nla_total_size(sizeof(u32))	/* mark */
+		+ nla_total_size(sizeof(u32))	/* verdict */
+		+ nla_total_size(sizeof(__be64)); /* rule handle */

Minor style issue: we got rid of putting the operator on the next line everywhere. Let's keep this consistent.

#15 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

In reply to Florian Westphal

Some more comments below:

Quoted 18 lines
include/uapi/linux/netfilter/nf_tables.h +10 -0
index d8c8a7c..88bcd00 100644
@@ -970,4 +972,34 @@ enum nft_gen_attributes {
 };
 #define NFTA_GEN_MAX		(__NFTA_GEN_MAX - 1)
 
+enum nft_trace_attibutes {
+	NFTA_TRACE_UNSPEC,
+	NFTA_TRACE_CHAIN,
+	NFTA_TRACE_DEV_TYPE,
+	NFTA_TRACE_ID,
+	NFTA_TRACE_IIF,
+	NFTA_TRACE_OIF,
+	NFTA_TRACE_LL_HEADER,
+	NFTA_TRACE_MARK,
+	NFTA_TRACE_NETWORK_HEADER,

include/uapi/linux/netfilter/nf_tables.h enum nft_gen_attributes { · line 994

Quoted 10 lines
include/uapi/linux/netfilter/nf_tables.h +10 -0
+	NFTA_TRACE_TABLE,
+	NFTA_TRACE_TRANSPORT_HEADER,
+	NFTA_TRACE_TRANSPORT_PROTO,
+	NFTA_TRACE_TYPE,
+	NFTA_TRACE_RULE_HANDLE,
+	NFTA_TRACE_VERDICT,
+	NFTA_TRACE_VLAN_TAG,
+	__NFTA_TRACE_MAX
+};
+#define NFTA_TRACE_MAX (__NFTA_TRACE_MAX - 1)

The ordering appears to be a bit random (or I'm missing something), I'd expect basic information like TABLE/CHAIN/RULE_HANDLE to be at the beginning (in that order), similar for TYPE, VERDICT etc.

net/netfilter/nf_tables_api.c line 28

Quoted 11 lines
net/netfilter/nf_tables_api.c +3 -0
index 93cc473..25d8168 100644
@@ -21,6 +23,10 @@
 #include <net/net_namespace.h>
 #include <net/sock.h>
 
+#define NFT_TRACETYPE_LL_HSIZE		20
+#define NFT_TRACETYPE_NETWORK_HSIZE	32
+#define NFT_TRACETYPE_TRANSPORT_HSIZE	 4

4 seems to be too small for some header types like GRE. If the key is present it seems like information we'd want to log.

Also since people probably want to use this to determine why or why not rules match, I think it would be good to have the entire transport header present.

net/netfilter/nf_tables_api.c nla_put_failure: · line 508

Quoted 13 lines
net/netfilter/nf_tables_api.c +13 -0
+static bool
+trace_notify_put_packet(struct sk_buff *nlskb, const struct nft_pktinfo *pkt)
+{
+	const struct sk_buff *skb = pkt->skb;
+	unsigned int plen = min_t(unsigned int,
+				  pkt->xt.thoff - skb_network_offset(skb),
+				  NFT_TRACETYPE_NETWORK_HSIZE);
+	int mac_off;
+
+	if (plen >= 20u && /* minimum iphdr size */
+	    !trace_notify_put_data(nlskb, NFTA_TRACE_NETWORK_HEADER,
+				   skb, skb_network_offset(skb), plen))
+		return false;

Does that check for 20u have any deeper meaning? It seems this will always be true for all currently supported protocols and if we were to add support for one that has a smaller size it won't work.

net/netfilter/nf_tables_api.c nla_put_failure: · line 519

Quoted 7 lines
net/netfilter/nf_tables_api.c +7 -0
+	plen = min_t(unsigned int, skb->len - pkt->xt.thoff,
+		     NFT_TRACETYPE_TRANSPORT_HSIZE);
+
+	if (plen >= sizeof(u32) &&
+	    !trace_notify_put_data(nlskb, NFTA_TRACE_TRANSPORT_HEADER,
+				   skb, pkt->xt.thoff, plen))
+		return false;

Same question here, is there anything wrong with including a smaller sized header? Especially since we're talking about a debugging tool.

net/netfilter/nf_tables_api.c err: · line 645

Quoted 9 lines
net/netfilter/nf_tables_api.c +9 -0
+void nf_tables_trace_notify(const struct nft_pktinfo *pkt,
+			    const struct nft_chain *chain,
+			    const struct nft_rule *rule,
+			    u32 verdict,
+			    enum nft_trace_types type)
+{
+ [...]
+	if (nla_put_be32(skb, NFTA_TRACE_ID, htonl(hash32_ptr(pkt->skb))))
+		goto nla_put_failure;

This could lead to duplicate IDs quite quickly. I can't think of any other values where we know for sure they will stay constant while the skb is within netfilter. How about combining this with a per CPU counter or something similar?

net/netfilter/nf_tables_api.c err: · line 655

Quoted 9 lines
net/netfilter/nf_tables_api.c +9 -0
+	if (chain) {
+		if (nla_put_string(skb, NFTA_TRACE_TABLE, chain->table->name))
+			goto nla_put_failure;
+		if (nla_put_string(skb, NFTA_TRACE_CHAIN, chain->name))
+			goto nla_put_failure;
+	}
+
+	if (rule && nla_put_be64(skb, NFTA_TRACE_RULE_HANDLE,
+				 cpu_to_be64(rule->handle)))

Minor nitpick: below you use

	if (condition &&
	    nla_put_...

which I think is more readable.

Quoted 8 lines
net/netfilter/nf_tables_api.c +8 -0
+		goto nla_put_failure;
+
+	if (pkt->in &&
+	    nla_put_be32(skb, NFTA_TRACE_IIF, htonl(pkt->in->ifindex)))
+		goto nla_put_failure;
+	if (pkt->out &&
+	    nla_put_be32(skb, NFTA_TRACE_OIF, htonl(pkt->out->ifindex)))
+		goto nla_put_failure;

net/netfilter/nf_tables_api.c err: · line 673

Quoted 10 lines
net/netfilter/nf_tables_api.c +10 -0
+	if (pkt->skb->mark &&
+	    nla_put_be32(skb, NFTA_TRACE_MARK, htonl(pkt->skb->mark)))
+		goto nla_put_failure;
+
+	switch (type) {
+	case NFT_TRACETYPE_POLICY:
+	case NFT_TRACETYPE_RETURN:
+	case NFT_TRACETYPE_RULE:
+		if (nla_put_be32(skb, NFTA_TRACE_VERDICT, htonl(verdict)))
+			goto nla_put_failure;

It would be nice to have the full verdict available, IOW also the jump target.

You could simply pass struct nft_verdict to this function and adapt nft_verdict_dump() to take the attribute value as parameter.

net/netfilter/nf_tables_core.c next_rule: · line 171

Quoted 12 lines
net/netfilter/nf_tables_core.c +2 -1
index f3695a4..29a6ca9 100644
@@ -56,10 +56,15 @@ static void __nft_trace_packet(const struct nft_pktinfo *pkt,
@@ -161,7 +167,9 @@ next_rule:
 	case NF_ACCEPT:
 	case NF_DROP:
 	case NF_QUEUE:
-		nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
+		nft_trace_packet(pkt, chain, rule, rulenum,
+				 regs.verdict.code & NF_VERDICT_MASK,

I'd suggest to include the full verdict code. Embedded errno codes or queue numbers are certainly also of interest.

author maintainer
#16 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 24 lines · Patrick McHardy
Quoted 24 lines
include/uapi/linux/netfilter/nf_tables.h +16 -0
index d8c8a7c..88bcd00 100644
@@ -970,4 +972,34 @@ enum nft_gen_attributes {
 };
 #define NFTA_GEN_MAX		(__NFTA_GEN_MAX - 1)
 
+enum nft_trace_attibutes {
+	NFTA_TRACE_UNSPEC,
+	NFTA_TRACE_CHAIN,
+	NFTA_TRACE_DEV_TYPE,
+	NFTA_TRACE_ID,
+	NFTA_TRACE_IIF,
+	NFTA_TRACE_OIF,
+	NFTA_TRACE_LL_HEADER,
+	NFTA_TRACE_MARK,
+	NFTA_TRACE_NETWORK_HEADER,
+	NFTA_TRACE_TABLE,
+	NFTA_TRACE_TRANSPORT_HEADER,
+	NFTA_TRACE_TRANSPORT_PROTO,
+	NFTA_TRACE_TYPE,
+	NFTA_TRACE_RULE_HANDLE,
+	NFTA_TRACE_VERDICT,
Quoted 8 lines

include/uapi/linux/netfilter/nf_tables.h enum nft_gen_attributes { · line 994

Quoted 4 lines
include/uapi/linux/netfilter/nf_tables.h +4 -0
+	NFTA_TRACE_VLAN_TAG,
+	__NFTA_TRACE_MAX
+};
+#define NFTA_TRACE_MAX (__NFTA_TRACE_MAX - 1)

The ordering appears to be a bit random (or I'm missing something), I'd expect basic information like TABLE/CHAIN/RULE_HANDLE to be at the beginning (in that order), similar for TYPE, VERDICT etc.

NFTA_TRACE_UNSPEC,
NFTA_TRACE_TABLE,
NFTA_TRACE_CHAIN,
NFTA_TRACE_RULE_HANDLE,
NFTA_TRACE_TYPE,
NFTA_TRACE_VERDICT,
NFTA_TRACE_DEV_TYPE,
NFTA_TRACE_ID,
NFTA_TRACE_LL_HEADER,
NFTA_TRACE_NETWORK_HEADER,
NFTA_TRACE_TRANSPORT_HEADER,
NFTA_TRACE_TRANSPORT_PROTO,
NFTA_TRACE_IIF,
NFTA_TRACE_OIF,
NFTA_TRACE_MARK,
NFTA_TRACE_VLAN_TAG,

better? If not, please just tell me what ordering you'd prefer and thats what I'll use.

Quoted 27 lines

net/netfilter/nf_tables_api.c line 28

Quoted 11 lines
net/netfilter/nf_tables_api.c +3 -0
index 93cc473..25d8168 100644
@@ -21,6 +23,10 @@
 #include <net/net_namespace.h>
 #include <net/sock.h>
 
+#define NFT_TRACETYPE_LL_HSIZE		20
+#define NFT_TRACETYPE_NETWORK_HSIZE	32
+#define NFT_TRACETYPE_TRANSPORT_HSIZE	 4

4 seems to be too small for some header types like GRE. If the key is present it seems like information we'd want to log.

Also since people probably want to use this to determine why or why not rules match, I think it would be good to have the entire transport header present.

Quoted 9 lines
net/netfilter/nf_tables_api.c +9 -0
+static bool
+trace_notify_put_packet(struct sk_buff *nlskb, const struct nft_pktinfo *pkt)
+{
+	const struct sk_buff *skb = pkt->skb;
+	unsigned int plen = min_t(unsigned int,
+				  pkt->xt.thoff - skb_network_offset(skb),
+				  NFT_TRACETYPE_NETWORK_HSIZE);
+	int mac_off;
+
Quoted 8 lines

net/netfilter/nf_tables_api.c nla_put_failure: · line 508

Quoted 4 lines
net/netfilter/nf_tables_api.c +4 -0
+	if (plen >= 20u && /* minimum iphdr size */
+	    !trace_notify_put_data(nlskb, NFTA_TRACE_NETWORK_HEADER,
+				   skb, skb_network_offset(skb), plen))
+		return false;

Does that check for 20u have any deeper meaning? It seems this will always be true for all currently supported protocols and if we were to add support for one that has a smaller size it won't work.

OK, I'll change all checks to 'len &&'.

Quoted 6 lines
Quoted 1 line

+ if (nla_put_be32(skb, NFTA_TRACE_ID, htonl(hash32_ptr(pkt->skb))))

This could lead to duplicate IDs quite quickly. I can't think of any other values where we know for sure they will stay constant while the skb is within netfilter. How about combining this with a per CPU counter or something similar?

I would not mind, it would result in ID change though for nfqueue case. If thats ok I'll use a pcpu counter that gets incremented when nft_meta sets skb->nftrace=1 and use __this_cpu_read(trace_id) instead (if we have pcpu id counter, is there any point in also using skb address...?)

Quoted 12 lines

net/netfilter/nf_tables_api.c err: · line 655

Quoted 9 lines
net/netfilter/nf_tables_api.c +9 -0
+	if (chain) {
+		if (nla_put_string(skb, NFTA_TRACE_TABLE, chain->table->name))
+			goto nla_put_failure;
+		if (nla_put_string(skb, NFTA_TRACE_CHAIN, chain->name))
+			goto nla_put_failure;
+	}
+
+	if (rule && nla_put_be64(skb, NFTA_TRACE_RULE_HANDLE,
+				 cpu_to_be64(rule->handle)))

Minor nitpick: below you use

Quoted 4 lines
	if (condition &&
	    nla_put_...

which I think is more readable.

ok. I only used the former if line would go over 80, but i can convert this too of course.

Quoted 19 lines
Quoted 6 lines
net/netfilter/nf_tables_api.c +6 -0
+	if (pkt->in &&
+	    nla_put_be32(skb, NFTA_TRACE_IIF, htonl(pkt->in->ifindex)))
+		goto nla_put_failure;
+	if (pkt->out &&
+	    nla_put_be32(skb, NFTA_TRACE_OIF, htonl(pkt->out->ifindex)))
+		goto nla_put_failure;

net/netfilter/nf_tables_api.c err: · line 673

Quoted 10 lines
net/netfilter/nf_tables_api.c +10 -0
+	if (pkt->skb->mark &&
+	    nla_put_be32(skb, NFTA_TRACE_MARK, htonl(pkt->skb->mark)))
+		goto nla_put_failure;
+
+	switch (type) {
+	case NFT_TRACETYPE_POLICY:
+	case NFT_TRACETYPE_RETURN:
+	case NFT_TRACETYPE_RULE:
+		if (nla_put_be32(skb, NFTA_TRACE_VERDICT, htonl(verdict)))
+			goto nla_put_failure;

It would be nice to have the full verdict available, IOW also the jump target.

Quoted 2 lines

You could simply pass struct nft_verdict to this function and adapt nft_verdict_dump() to take the attribute value as parameter.

Will add NFTA_TRACE_JUMP/NFTA_TRACE_GOTO for that, ok?

author maintainer
#17 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 7 lines · Florian Westphal
Quoted 4 lines · Patrick McHardy

It would be nice to have the full verdict available, IOW also the jump target.

You could simply pass struct nft_verdict to this function and adapt nft_verdict_dump() to take the attribute value as parameter.

Will add NFTA_TRACE_JUMP/NFTA_TRACE_GOTO for that, ok?

Scratch that, I'll add NFTA_TRACE_JUMP_TARGET (NLA_STRING) containing name of the jump target (if any).

GOTO case can be inferred from the verdict attribute.

#18 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

In reply to Florian Westphal
Quoted 23 lines · Florian Westphal
Quoted 4 lines · Patrick McHardy

The ordering appears to be a bit random (or I'm missing something), I'd expect basic information like TABLE/CHAIN/RULE_HANDLE to be at the beginning (in that order), similar for TYPE, VERDICT etc.

NFTA_TRACE_UNSPEC,
NFTA_TRACE_TABLE,
NFTA_TRACE_CHAIN,
NFTA_TRACE_RULE_HANDLE,
NFTA_TRACE_TYPE,
NFTA_TRACE_VERDICT,
NFTA_TRACE_DEV_TYPE,
NFTA_TRACE_ID,
NFTA_TRACE_LL_HEADER,
NFTA_TRACE_NETWORK_HEADER,
NFTA_TRACE_TRANSPORT_HEADER,
NFTA_TRACE_TRANSPORT_PROTO,
NFTA_TRACE_IIF,
NFTA_TRACE_OIF,
NFTA_TRACE_MARK,
NFTA_TRACE_VLAN_TAG,

Quoted 2 lines

better? If not, please just tell me what ordering you'd prefer and thats what I'll use.

Yep looks good.

Quoted 11 lines
Quoted 6 lines
Quoted 1 line

+ if (nla_put_be32(skb, NFTA_TRACE_ID, htonl(hash32_ptr(pkt->skb))))

This could lead to duplicate IDs quite quickly. I can't think of any other values where we know for sure they will stay constant while the skb is within netfilter. How about combining this with a per CPU counter or something similar?

I would not mind, it would result in ID change though for nfqueue case. If thats ok I'll use a pcpu counter that gets incremented when nft_meta sets skb->nftrace=1 and use __this_cpu_read(trace_id) instead (if we have pcpu id counter, is there any point in also using skb address...?)

Not sure, I guess not.

Quoted 9 lines
Quoted 9 lines

net/netfilter/nf_tables_api.c err: · line 673

Quoted 6 lines
net/netfilter/nf_tables_api.c +6 -0
+	switch (type) {
+	case NFT_TRACETYPE_POLICY:
+	case NFT_TRACETYPE_RETURN:
+	case NFT_TRACETYPE_RULE:
+		if (nla_put_be32(skb, NFTA_TRACE_VERDICT, htonl(verdict)))
+			goto nla_put_failure;

It would be nice to have the full verdict available, IOW also the jump target.

Quoted 4 lines
Quoted 2 lines

You could simply pass struct nft_verdict to this function and adapt nft_verdict_dump() to take the attribute value as parameter.

Will add NFTA_TRACE_JUMP/NFTA_TRACE_GOTO for that, ok?

Any reason not to use the standard verdict encoding? We even have an almost ready to use function for that.

author maintainer
#19 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 7 lines · Patrick McHardy
Quoted 7 lines
Quoted 6 lines
Quoted 1 line

+ if (nla_put_be32(skb, NFTA_TRACE_ID, htonl(hash32_ptr(pkt->skb))))

This could lead to duplicate IDs quite quickly. I can't think of any other values where we know for sure they will stay constant while the skb is within netfilter. How about combining this with a per CPU counter or something similar?

Quoted 6 lines
Quoted 4 lines

I would not mind, it would result in ID change though for nfqueue case. If thats ok I'll use a pcpu counter that gets incremented when nft_meta sets skb->nftrace=1 and use __this_cpu_read(trace_id) instead (if we have pcpu id counter, is there any point in also using skb address...?)

Not sure, I guess not.

Ok, I'll move to pcpu id counter in v2

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

net/netfilter/nf_tables_api.c err: · line 673

Quoted 6 lines
net/netfilter/nf_tables_api.c +6 -0
+	switch (type) {
+	case NFT_TRACETYPE_POLICY:
+	case NFT_TRACETYPE_RETURN:
+	case NFT_TRACETYPE_RULE:
+		if (nla_put_be32(skb, NFTA_TRACE_VERDICT, htonl(verdict)))
+			goto nla_put_failure;

It would be nice to have the full verdict available, IOW also the jump target.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

You could simply pass struct nft_verdict to this function and adapt nft_verdict_dump() to take the attribute value as parameter.

Will add NFTA_TRACE_JUMP/NFTA_TRACE_GOTO for that, ok?

Any reason not to use the standard verdict encoding? We even have an almost ready to use function for that.

Sorry, could you be more specific?
How would you tell userspace where the jump/goto is going to?

What I am doing now in my local version:

NFTA_TRACE_VERDICT is the verbatim result of the hook (i.e. might contain errno or queue id).

NFTA_TRACE_JUMP_TARGET is the name of the chain that we goto or jump to, set when verdict == NFT_JUMP/_GOTO.

Alternative suggestions welcome.

This approach means that userspace must try a bit harder to decode the verdict since we cannot use 'verdict & NF_VERDICT_MASK' if we're looking at a NFT_* verdict when translating the integer to a string.

#20 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 5 lines · Florian Westphal
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

It would be nice to have the full verdict available, IOW also the jump target.

You could simply pass struct nft_verdict to this function and adapt nft_verdict_dump() to take the attribute value as parameter.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Will add NFTA_TRACE_JUMP/NFTA_TRACE_GOTO for that, ok?

Any reason not to use the standard verdict encoding? We even have an almost ready to use function for that.

Sorry, could you be more specific?
How would you tell userspace where the jump/goto is going to?

We have nft_verdict_dump(), which encodes the verdict as nested attribute. I'd suggest to simply use that one since we also already have parsing functions for that in both nft and libnftnl and we can use the regular data structures.

Quoted 5 lines

What I am doing now in my local version:

NFTA_TRACE_VERDICT is the verbatim result of the hook (i.e. might contain errno or queue id).

Quoted 8 lines

NFTA_TRACE_JUMP_TARGET is the name of the chain that we goto or jump to, set when verdict == NFT_JUMP/_GOTO.

Alternative suggestions welcome.

This approach means that userspace must try a bit harder to decode the verdict since we cannot use 'verdict & NF_VERDICT_MASK' if we're looking at a NFT_* verdict when translating the integer to a string.

With the approach I mentioned we can use the normal decoding functions which (at least in nft) handle this case fine.

author maintainer
#21 lore raw

Re: [PATCH nf-next 1/6] netfilter: nf_tables: extend tracing infrastructure

Quoted 7 lines · Patrick McHardy
Quoted 2 lines

Sorry, could you be more specific?
How would you tell userspace where the jump/goto is going to?

We have nft_verdict_dump(), which encodes the verdict as nested attribute. I'd suggest to simply use that one since we also already have parsing functions for that in both nft and libnftnl and we can use the regular data structures.

Thanks for clarifying, I'll switch NFTA_TRACE_VERDICT to nested attr.

author maintainer
#22 lore raw

PATCH 2/6 netfilter: nf_tables: wrap tracing with a static key

In reply to Florian Westphal

Only needed when meta nftrace rule(s) were added.

---
 include/net/netfilter/nf_tables_core.h |  1 +
 include/net/netfilter/nft_meta.h       |  3 +++
 net/bridge/netfilter/nft_meta_bridge.c |  1 +
 net/netfilter/nf_tables_core.c         | 20 +++++++++++++++-----
 net/netfilter/nft_meta.c               | 15 +++++++++++++++
 5 files changed, 35 insertions(+), 5 deletions(-)
include/net/netfilter/nf_tables_core.h +1 -0
index c6f400c..8ad6240 100644
@@ -48,6 +48,7 @@ struct nft_payload {
 };
 
 extern const struct nft_expr_ops nft_payload_fast_ops;
+extern struct static_key nft_trace_enabled;
 
 int nft_payload_module_init(void);
 void nft_payload_module_exit(void);
include/net/netfilter/nft_meta.h +3 -0
index 711887a..d27588c 100644
@@ -33,4 +33,7 @@ void nft_meta_set_eval(const struct nft_expr *expr,
 		       struct nft_regs *regs,
 		       const struct nft_pktinfo *pkt);
 
+void nft_meta_set_destroy(const struct nft_ctx *ctx,
+			  const struct nft_expr *expr);
+
 #endif
net/bridge/netfilter/nft_meta_bridge.c +1 -0
index a21269b..4b901d9 100644
@@ -84,6 +84,7 @@ static const struct nft_expr_ops nft_meta_bridge_set_ops = {
 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_meta)),
 	.eval		= nft_meta_set_eval,
 	.init		= nft_meta_set_init,
+	.destroy	= nft_meta_set_destroy,
 	.dump		= nft_meta_set_dump,
 };
 
net/netfilter/nf_tables_core.c +15 -5
index 29a6ca9..dabf5ed 100644
@@ -16,6 +16,7 @@
 #include <linux/skbuff.h>
 #include <linux/netlink.h>
 #include <linux/netfilter.h>
+#include <linux/static_key.h>
 #include <linux/netfilter/nfnetlink.h>
 #include <linux/netfilter/nf_tables.h>
 #include <net/netfilter/nf_tables_core.h>
@@ -54,6 +55,9 @@ static void __nft_trace_packet(const struct nft_pktinfo *pkt,
 		     rulenum);
 }
 
+struct static_key nft_trace_enabled __read_mostly;
+EXPORT_SYMBOL_GPL(nft_trace_enabled);
+
 static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 				    const struct nft_chain *chain,
 				    const struct nft_rule *rule,
@@ -61,7 +65,9 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 				    u32 verdict,
 				    enum nft_trace_types type)
 {
-	if (unlikely(pkt->skb->nf_trace)) {
+	if (static_key_false(&nft_trace_enabled)) {
+		if (!pkt->skb->nf_trace)
+			return;
 		nf_tables_trace_notify(pkt, chain, rule, verdict, type);
 		__nft_trace_packet(pkt, chain, rulenum, type);
 	}
@@ -138,7 +144,8 @@ next_rule:
 		if (unlikely(rule->genmask & (1 << gencursor)))
 			continue;
 
-		rulenum++;
+		if (static_key_false(&nft_trace_enabled))
+			rulenum++;
 
 		nft_rule_for_each_expr(expr, last, rule) {
 			if (expr->ops == &nft_cmp_fast_ops)
@@ -178,7 +185,8 @@ next_rule:
 		BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE);
 		jumpstack[stackptr].chain = chain;
 		jumpstack[stackptr].rule  = rule;
-		jumpstack[stackptr].rulenum = rulenum;
+		if (static_key_false(&nft_trace_enabled))
+			jumpstack[stackptr].rulenum = rulenum;
 		stackptr++;
 		/* fall through */
 	case NFT_GOTO:
@@ -188,7 +196,8 @@ next_rule:
 		chain = regs.verdict.chain;
 		goto do_chain;
 	case NFT_CONTINUE:
-		rulenum++;
+		if (static_key_false(&nft_trace_enabled))
+			rulenum++;
 		/* fall through */
 	case NFT_RETURN:
 		if (stackptr)
@@ -204,7 +213,8 @@ next_rule:
 		stackptr--;
 		chain = jumpstack[stackptr].chain;
 		rule  = jumpstack[stackptr].rule;
-		rulenum = jumpstack[stackptr].rulenum;
+		if (static_key_false(&nft_trace_enabled))
+			rulenum = jumpstack[stackptr].rulenum;
 		goto next_rule;
 	}
 
net/netfilter/nft_meta.c +15 -0
index e94526a..c2a60ac 100644
@@ -18,10 +18,12 @@
 #include <linux/ip.h>
 #include <linux/ipv6.h>
 #include <linux/smp.h>
+#include <linux/static_key.h>
 #include <net/dst.h>
 #include <net/sock.h>
 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
 #include <net/netfilter/nf_tables.h>
+#include <net/netfilter/nf_tables_core.h>
 #include <net/netfilter/nft_meta.h>
 
 void nft_meta_get_eval(const struct nft_expr *expr,
@@ -300,6 +302,9 @@ int nft_meta_set_init(const struct nft_ctx *ctx,
 	if (err < 0)
 		return err;
 
+	if (priv->key == NFT_META_NFTRACE)
+		static_key_slow_inc(&nft_trace_enabled);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(nft_meta_set_init);
@@ -337,6 +342,15 @@ nla_put_failure:
 }
 EXPORT_SYMBOL_GPL(nft_meta_set_dump);
 
+void nft_meta_set_destroy(const struct nft_ctx *ctx,
+			  const struct nft_expr *expr)
+{
+	const struct nft_meta *priv = nft_expr_priv(expr);
+
+	if (priv->key == NFT_META_NFTRACE)
+		static_key_slow_dec(&nft_trace_enabled);
+}
+
 static struct nft_expr_type nft_meta_type;
 static const struct nft_expr_ops nft_meta_get_ops = {
 	.type		= &nft_meta_type,
@@ -351,6 +365,7 @@ static const struct nft_expr_ops nft_meta_set_ops = {
 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_meta)),
 	.eval		= nft_meta_set_eval,
 	.init		= nft_meta_set_init,
+	.destroy	= nft_meta_set_destroy,
 	.dump		= nft_meta_set_dump,
 };
 
Signature · 1 line
2.4.10
#23 lore raw

Re: [PATCH nf-next 2/6] netfilter: nf_tables: wrap tracing with a static key

net/netfilter/nf_tables_core.c next_rule: · line 148

Quoted 11 lines · Florian Westphal
net/netfilter/nf_tables_core.c +2 -1
index 29a6ca9..dabf5ed 100644
@@ -138,7 +144,8 @@ next_rule:
 		if (unlikely(rule->genmask & (1 << gencursor)))
 			continue;
 
-		rulenum++;
+		if (static_key_false(&nft_trace_enabled))
+			rulenum++;

This API is deprecated, see Documentation/static-keys.txt.

I'm also wondering if this introduces a race condition on architectures that don't support jump labels. static_key_slow_inc() simply increases the ->enabled counter without further synchronization, so this might happen while we're executing this function and some of the increments might be skipped.

author maintainer
#24 lore raw

Re: [PATCH nf-next 2/6] netfilter: nf_tables: wrap tracing with a static key

Quoted 9 lines · Patrick McHardy
Quoted 8 lines · Florian Westphal
net/netfilter/nf_tables_core.c +0 -0
index 29a6ca9..dabf5ed 100644
@@ -138,7 +144,8 @@ next_rule:
 		if (unlikely(rule->genmask & (1 << gencursor)))
 			continue;
 
Quoted 5 lines

net/netfilter/nf_tables_core.c next_rule: · line 148

Quoted 3 lines
net/netfilter/nf_tables_core.c +2 -1
-		rulenum++;
+		if (static_key_false(&nft_trace_enabled))
+			rulenum++;

This API is deprecated, see Documentation/static-keys.txt.

Oh, thats great. static_branch_unlikely() is a much better name.

I'll update this locally and send a v2 later (I want to give others time to provide feedback as well).

Quoted 5 lines

I'm also wondering if this introduces a race condition on architectures that don't support jump labels. static_key_slow_inc() simply increases the ->enabled counter without further synchronization, so this might happen while we're executing this function and some of the increments might be skipped.

Hmm, adding synchronization for this is braindead.
The new trace infrastructure doesn't need this rulenum, I only kept it since thats what Pablo asked me to do.

So we have 3 choices:
- ignore this
- don't use static key here
- kill the old nf_log_packet trace part after all

I find it sad that we have to keep this rule counting around :-/

If there are no further comments I'll remove the static key and do the rule counting unconditinally.

Thanks Patrick.

#25 lore raw

Re: [PATCH nf-next 2/6] netfilter: nf_tables: wrap tracing with a static key

Quoted 17 lines · Florian Westphal
Quoted 5 lines · Patrick McHardy

I'm also wondering if this introduces a race condition on architectures that don't support jump labels. static_key_slow_inc() simply increases the ->enabled counter without further synchronization, so this might happen while we're executing this function and some of the increments might be skipped.

Hmm, adding synchronization for this is braindead.
The new trace infrastructure doesn't need this rulenum, I only kept it since thats what Pablo asked me to do.

So we have 3 choices:
- ignore this
- don't use static key here
- kill the old nf_log_packet trace part after all

Quoted 4 lines

I find it sad that we have to keep this rule counting around :-/

If there are no further comments I'll remove the static key and do the rule counting unconditinally.

I wouldn't mind killing the old tracing off, but I think the main reason why we added it was for ipt compat. So probably not possible at this point.

I agree that having the rule counting is not particulary nice, but I don't see any other way right now than to do it unconditionally.

#26 lore raw

Re: [PATCH nf-next 2/6] netfilter: nf_tables: wrap tracing with a static key

In reply to Florian Westphal
Quoted 81 lines · Florian Westphal

Only needed when meta nftrace rule(s) were added.

---
 include/net/netfilter/nf_tables_core.h |  1 +
 include/net/netfilter/nft_meta.h       |  3 +++
 net/bridge/netfilter/nft_meta_bridge.c |  1 +
 net/netfilter/nf_tables_core.c         | 20 +++++++++++++++-----
 net/netfilter/nft_meta.c               | 15 +++++++++++++++
 5 files changed, 35 insertions(+), 5 deletions(-)
include/net/netfilter/nf_tables_core.h +1 -0
index c6f400c..8ad6240 100644
@@ -48,6 +48,7 @@ struct nft_payload {
 };
 
 extern const struct nft_expr_ops nft_payload_fast_ops;
+extern struct static_key nft_trace_enabled;
 
 int nft_payload_module_init(void);
 void nft_payload_module_exit(void);
include/net/netfilter/nft_meta.h +3 -0
index 711887a..d27588c 100644
@@ -33,4 +33,7 @@ void nft_meta_set_eval(const struct nft_expr *expr,
 		       struct nft_regs *regs,
 		       const struct nft_pktinfo *pkt);
 
+void nft_meta_set_destroy(const struct nft_ctx *ctx,
+			  const struct nft_expr *expr);
+
 #endif
net/bridge/netfilter/nft_meta_bridge.c +1 -0
index a21269b..4b901d9 100644
@@ -84,6 +84,7 @@ static const struct nft_expr_ops nft_meta_bridge_set_ops = {
 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_meta)),
 	.eval		= nft_meta_set_eval,
 	.init		= nft_meta_set_init,
+	.destroy	= nft_meta_set_destroy,
 	.dump		= nft_meta_set_dump,
 };
 
net/netfilter/nf_tables_core.c +7 -1
index 29a6ca9..dabf5ed 100644
@@ -16,6 +16,7 @@
 #include <linux/skbuff.h>
 #include <linux/netlink.h>
 #include <linux/netfilter.h>
+#include <linux/static_key.h>
 #include <linux/netfilter/nfnetlink.h>
 #include <linux/netfilter/nf_tables.h>
 #include <net/netfilter/nf_tables_core.h>
@@ -54,6 +55,9 @@ static void __nft_trace_packet(const struct nft_pktinfo *pkt,
 		     rulenum);
 }
 
+struct static_key nft_trace_enabled __read_mostly;
+EXPORT_SYMBOL_GPL(nft_trace_enabled);
+
 static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 				    const struct nft_chain *chain,
 				    const struct nft_rule *rule,
@@ -61,7 +65,9 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 				    u32 verdict,
 				    enum nft_trace_types type)
 {
-	if (unlikely(pkt->skb->nf_trace)) {
+	if (static_key_false(&nft_trace_enabled)) {
+		if (!pkt->skb->nf_trace)
+			return;
 		nf_tables_trace_notify(pkt, chain, rule, verdict, type);
 		__nft_trace_packet(pkt, chain, rulenum, type);
 	}

net/netfilter/nf_tables_core.c next_rule: · line 148

Quoted 6 lines
net/netfilter/nf_tables_core.c +2 -1
@@ -138,7 +144,8 @@ next_rule:
 		if (unlikely(rule->genmask & (1 << gencursor)))
 			continue;
 
-		rulenum++;
+		if (static_key_false(&nft_trace_enabled))
+			rulenum++;

We can probably wrap this code to annotate rule number in a function?

Quoted 14 lines
net/netfilter/nf_tables_core.c +2 -1
 		nft_rule_for_each_expr(expr, last, rule) {
 			if (expr->ops == &nft_cmp_fast_ops)
@@ -178,7 +185,8 @@ next_rule:
 		BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE);
 		jumpstack[stackptr].chain = chain;
 		jumpstack[stackptr].rule  = rule;
-		jumpstack[stackptr].rulenum = rulenum;
+		if (static_key_false(&nft_trace_enabled))
+			jumpstack[stackptr].rulenum = rulenum;
 		stackptr++;
 		/* fall through */
 	case NFT_GOTO:

net/netfilter/nf_tables_core.c next_rule: · line 200

Quoted 6 lines
net/netfilter/nf_tables_core.c +2 -1
@@ -188,7 +196,8 @@ next_rule:
 		chain = regs.verdict.chain;
 		goto do_chain;
 	case NFT_CONTINUE:
-		rulenum++;
+		if (static_key_false(&nft_trace_enabled))
+			rulenum++;

This happens again here.

net/netfilter/nf_tables_core.c next_rule: · line 217

Quoted 10 lines
net/netfilter/nf_tables_core.c +2 -1
 		/* fall through */
 	case NFT_RETURN:
 		if (stackptr)
@@ -204,7 +213,8 @@ next_rule:
 		stackptr--;
 		chain = jumpstack[stackptr].chain;
 		rule  = jumpstack[stackptr].rule;
-		rulenum = jumpstack[stackptr].rulenum;
+		if (static_key_false(&nft_trace_enabled))
+			rulenum = jumpstack[stackptr].rulenum;

This one is very similar to the one above.

Quoted 61 lines
net/netfilter/nf_tables_core.c +0 -0
 		goto next_rule;
 	}
 
net/netfilter/nft_meta.c +15 -0
index e94526a..c2a60ac 100644
@@ -18,10 +18,12 @@
 #include <linux/ip.h>
 #include <linux/ipv6.h>
 #include <linux/smp.h>
+#include <linux/static_key.h>
 #include <net/dst.h>
 #include <net/sock.h>
 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
 #include <net/netfilter/nf_tables.h>
+#include <net/netfilter/nf_tables_core.h>
 #include <net/netfilter/nft_meta.h>
 
 void nft_meta_get_eval(const struct nft_expr *expr,
@@ -300,6 +302,9 @@ int nft_meta_set_init(const struct nft_ctx *ctx,
 	if (err < 0)
 		return err;
 
+	if (priv->key == NFT_META_NFTRACE)
+		static_key_slow_inc(&nft_trace_enabled);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(nft_meta_set_init);
@@ -337,6 +342,15 @@ nla_put_failure:
 }
 EXPORT_SYMBOL_GPL(nft_meta_set_dump);
 
+void nft_meta_set_destroy(const struct nft_ctx *ctx,
+			  const struct nft_expr *expr)
+{
+	const struct nft_meta *priv = nft_expr_priv(expr);
+
+	if (priv->key == NFT_META_NFTRACE)
+		static_key_slow_dec(&nft_trace_enabled);
+}
+
 static struct nft_expr_type nft_meta_type;
 static const struct nft_expr_ops nft_meta_get_ops = {
 	.type		= &nft_meta_type,
@@ -351,6 +365,7 @@ static const struct nft_expr_ops nft_meta_set_ops = {
 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_meta)),
 	.eval		= nft_meta_set_eval,
 	.init		= nft_meta_set_init,
+	.destroy	= nft_meta_set_destroy,
 	.dump		= nft_meta_set_dump,
 };
 
Signature · 6 lines
2.4.10

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#27 lore raw

PATCH 3/6 netfilter: nf_tables: disable old tracing if listener is present

In reply to Florian Westphal

No need to clutter nflog/dmesg ring buffer with the old tracing output when the 'native' nfnetlink interface is used.

---
 net/netfilter/nf_tables_core.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_core.c +8 -1
index dabf5ed..69bdd9a 100644
@@ -55,6 +55,7 @@ static void __nft_trace_packet(const struct nft_pktinfo *pkt,
 		     rulenum);
 }
 
+static bool prefer_native_trace __read_mostly;
 struct static_key nft_trace_enabled __read_mostly;
 EXPORT_SYMBOL_GPL(nft_trace_enabled);
 
@@ -69,7 +70,13 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 		if (!pkt->skb->nf_trace)
 			return;
 		nf_tables_trace_notify(pkt, chain, rule, verdict, type);
-		__nft_trace_packet(pkt, chain, rulenum, type);
+		if (prefer_native_trace)
+			return;
+
+		if (nfnetlink_has_listeners(pkt->net, NFNLGRP_NFTABLES))
+			prefer_native_trace = true;
+		else
+			__nft_trace_packet(pkt, chain, rulenum, type);
 	}
 }
 
Signature · 1 line
2.4.10
#28 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 21 lines · Florian Westphal

No need to clutter nflog/dmesg ring buffer with the old tracing output when the 'native' nfnetlink interface is used.

---
 net/netfilter/nf_tables_core.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_core.c +1 -0
index dabf5ed..69bdd9a 100644
@@ -55,6 +55,7 @@ static void __nft_trace_packet(const struct nft_pktinfo *pkt,
 		     rulenum);
 }
 
+static bool prefer_native_trace __read_mostly;
 struct static_key nft_trace_enabled __read_mostly;
 EXPORT_SYMBOL_GPL(nft_trace_enabled);
 

net/netfilter/nf_tables_core.c static inline void nft_trace_packet(const struct nft_pktinfo *pkt, · line 77

Quoted 9 lines
net/netfilter/nf_tables_core.c +5 -1
@@ -69,7 +70,13 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 		if (!pkt->skb->nf_trace)
 			return;
 		nf_tables_trace_notify(pkt, chain, rule, verdict, type);
-		__nft_trace_packet(pkt, chain, rulenum, type);
+		if (prefer_native_trace)
+			return;
+
+		if (nfnetlink_has_listeners(pkt->net, NFNLGRP_NFTABLES))
+			prefer_native_trace = true;

This seems to be better placed in a netlink bind callback.

Quoted 4 lines
net/netfilter/nf_tables_core.c +2 -0
+		else
+			__nft_trace_packet(pkt, chain, rulenum, type);
 	}
 }
#29 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

In reply to Florian Westphal
Quoted 22 lines · Florian Westphal

No need to clutter nflog/dmesg ring buffer with the old tracing output when the 'native' nfnetlink interface is used.

---
 net/netfilter/nf_tables_core.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_core.c +1 -0
index dabf5ed..69bdd9a 100644
@@ -55,6 +55,7 @@ static void __nft_trace_packet(const struct nft_pktinfo *pkt,
 		     rulenum);
 }
 
+static bool prefer_native_trace __read_mostly;
 struct static_key nft_trace_enabled __read_mostly;
 EXPORT_SYMBOL_GPL(nft_trace_enabled);
 
@@ -69,7 +70,13 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 		if (!pkt->skb->nf_trace)

net/netfilter/nf_tables_core.c static inline void nft_trace_packet(const struct nft_pktinfo *pkt, · line 79

Quoted 10 lines
net/netfilter/nf_tables_core.c +7 -1
 			return;
 		nf_tables_trace_notify(pkt, chain, rule, verdict, type);
-		__nft_trace_packet(pkt, chain, rulenum, type);
+		if (prefer_native_trace)
+			return;
+
+		if (nfnetlink_has_listeners(pkt->net, NFNLGRP_NFTABLES))
+			prefer_native_trace = true;
+		else
+			__nft_trace_packet(pkt, chain, rulenum, type);

For this very specific case I prefer a sysctl that we can remove moving forward, then remove this code and default to the new tracing infrastructure once we have indications that adoption of this new tracing infrastructure has been massively adopted instead of the existing one.

author maintainer
#30 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 22 lines · Pablo Neira Ayuso
Quoted 22 lines
net/netfilter/nf_tables_core.c +6 -1
index dabf5ed..69bdd9a 100644
@@ -55,6 +55,7 @@ static void __nft_trace_packet(const struct nft_pktinfo *pkt,
 		     rulenum);
 }
 
+static bool prefer_native_trace __read_mostly;
 struct static_key nft_trace_enabled __read_mostly;
 EXPORT_SYMBOL_GPL(nft_trace_enabled);
 
@@ -69,7 +70,13 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 		if (!pkt->skb->nf_trace)
 			return;
 		nf_tables_trace_notify(pkt, chain, rule, verdict, type);
-		__nft_trace_packet(pkt, chain, rulenum, type);
+		if (prefer_native_trace)
+			return;
+
+		if (nfnetlink_has_listeners(pkt->net, NFNLGRP_NFTABLES))
+			prefer_native_trace = true;
Quoted 8 lines

net/netfilter/nf_tables_core.c static inline void nft_trace_packet(const struct nft_pktinfo *pkt, · line 79

Quoted 2 lines
net/netfilter/nf_tables_core.c +2 -0
+		else
+			__nft_trace_packet(pkt, chain, rulenum, type);

For this very specific case I prefer a sysctl that we can remove moving forward, then remove this code and default to the new tracing infrastructure once we have indications that adoption of this new tracing infrastructure has been massively adopted instead of the existing one.

So you're saying

sysctl nft_old_trace = 1;

and then do

nf_tables_trace_notify(..)
if (nft_old_trace)

	__nft_trace_packet();

?

#31 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 36 lines · Florian Westphal
Quoted 22 lines · Pablo Neira Ayuso
Quoted 22 lines
net/netfilter/nf_tables_core.c +6 -1
index dabf5ed..69bdd9a 100644
@@ -55,6 +55,7 @@ static void __nft_trace_packet(const struct nft_pktinfo *pkt,
 		     rulenum);
 }
 
+static bool prefer_native_trace __read_mostly;
 struct static_key nft_trace_enabled __read_mostly;
 EXPORT_SYMBOL_GPL(nft_trace_enabled);
 
@@ -69,7 +70,13 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 		if (!pkt->skb->nf_trace)
 			return;
 		nf_tables_trace_notify(pkt, chain, rule, verdict, type);
-		__nft_trace_packet(pkt, chain, rulenum, type);
+		if (prefer_native_trace)
+			return;
+
+		if (nfnetlink_has_listeners(pkt->net, NFNLGRP_NFTABLES))
+			prefer_native_trace = true;
Quoted 8 lines

net/netfilter/nf_tables_core.c static inline void nft_trace_packet(const struct nft_pktinfo *pkt, · line 79

Quoted 2 lines
net/netfilter/nf_tables_core.c +2 -0
+		else
+			__nft_trace_packet(pkt, chain, rulenum, type);

For this very specific case I prefer a sysctl that we can remove moving forward, then remove this code and default to the new tracing infrastructure once we have indications that adoption of this new tracing infrastructure has been massively adopted instead of the existing one.

So you're saying

sysctl nft_old_trace = 1;

Quoted 7 lines

and then do

nf_tables_trace_notify(..)
if (nft_old_trace)

	__nft_trace_packet();

?

What I'm trying to avoid is the initial race that we'll have.

I mean, with this approach the user will likely enable the tracing from the rule, then will launch nft trace. In that case, he will be getting traces from the old way for a little while until you get one process subscribed to this.

#32 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 22 lines · Pablo Neira Ayuso
Quoted 21 lines · Florian Westphal
Quoted 10 lines

net/netfilter/nf_tables_core.c static inline void nft_trace_packet(const struct nft_pktinfo *pkt, · line 79

Quoted 4 lines
net/netfilter/nf_tables_core.c +4 -0
+		if (nfnetlink_has_listeners(pkt->net, NFNLGRP_NFTABLES))
+			prefer_native_trace = true;
+		else
+			__nft_trace_packet(pkt, chain, rulenum, type);

For this very specific case I prefer a sysctl that we can remove moving forward, then remove this code and default to the new tracing infrastructure once we have indications that adoption of this new tracing infrastructure has been massively adopted instead of the existing one.

So you're saying

sysctl nft_old_trace = 1;

and then do

nf_tables_trace_notify(..)
if (nft_old_trace)

	__nft_trace_packet();
Quoted 8 lines
Quoted 1 line

?

What I'm trying to avoid is the initial race that we'll have.

I mean, with this approach the user will likely enable the tracing from the rule, then will launch nft trace. In that case, he will be getting traces from the old way for a little while until you get one process subscribed to this.

On more thing to consider it interaction with other netfilter subsystems.

F.i. I think it would be very useful to have per connection tracing, so we'd store a flag in the nfct and transfer it to every packet. This would make them show up in the nft ruleset, but what about tracing in specific points of conntrack/NAT itself? Can we send them to the same group and make them integrate with ruleset tracing?

author maintainer
#33 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 7 lines · Patrick McHardy
Quoted 6 lines · Pablo Neira Ayuso

What I'm trying to avoid is the initial race that we'll have.

I mean, with this approach the user will likely enable the tracing from the rule, then will launch nft trace. In that case, he will be getting traces from the old way for a little while until you get one process subscribed to this.

Right, not nice.

Quoted 5 lines

On more thing to consider it interaction with other netfilter subsystems.

F.i. I think it would be very useful to have per connection tracing, so we'd store a flag in the nfct and transfer it to every packet. This would make them show up in the nft ruleset,

I'd prefer to complete set support for labels for this, so we can do something like
chain ct_trace {
type filter hook prerouting priority 0;
tcp dport 22 ct state new label set trace
ct label trace meta set nftrace 1
}

 chain ct_trace_out {
	 type filter hook output priority 0;
	 ct label trace meta set nftrace 1

}

Which would do in/out tracing without any additional code (except the label set support, of course).

Quoted 3 lines

but what about tracing in specific
points of conntrack/NAT itself? Can we send them to the same group and make them integrate with ruleset tracing?

Good point.

I think we would first have to identify possible interesting tracing points. Did you have anything specific in mind?

#34 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 15 lines · Florian Westphal
Quoted 5 lines · Patrick McHardy

On more thing to consider it interaction with other netfilter subsystems.

F.i. I think it would be very useful to have per connection tracing, so we'd store a flag in the nfct and transfer it to every packet. This would make them show up in the nft ruleset,

I'd prefer to complete set support for labels for this, so we can do something like
chain ct_trace {
type filter hook prerouting priority 0;
tcp dport 22 ct state new label set trace
ct label trace meta set nftrace 1
}

Quoted 7 lines
 chain ct_trace_out {
	 type filter hook output priority 0;
	 ct label trace meta set nftrace 1

}

Which would do in/out tracing without any additional code (except the label set support, of course).

That's of course one possibilty but it lacks one important point, we could not trace the first packet going through conntrack.

Quoted 8 lines
Quoted 3 lines

but what about tracing in specific
points of conntrack/NAT itself? Can we send them to the same group and make them integrate with ruleset tracing?

Good point.

I think we would first have to identify possible interesting tracing points. Did you have anything specific in mind?

I guess the hooks themselves, probably helper invocation and/or results, possibly interesting points within helpers, errors in protocol tracking, basically I think a good starting point would be what we now have as global tracing points.

author maintainer
#35 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Patrick McHardy <[email protected]> wrote:

Sorry for late reply.

Quoted 11 lines
Quoted 11 lines

chain ct_trace {
type filter hook prerouting priority 0; tcp dport 22 ct state new label set trace ct label trace meta set nftrace 1
}

 chain ct_trace_out {
	 type filter hook output priority 0;
	 ct label trace meta set nftrace 1

}

Quoted 5 lines
Quoted 2 lines

Which would do in/out tracing without any additional code (except the label set support, of course).

That's of course one possibilty but it lacks one important point, we could not trace the first packet going through conntrack.

Right, hooks that run 'earlier' than conntrack would not be able to use ct state matching.

I'm not sure if thats a real issue, we could just use stateless matching based on ip addresses and ports.

Lastly, I do think that we'll eventually have a ct expression to actually do tracking for us rather than the implicit hooking we have right now (my plan was to add this for nft bridge family conntrack support).

Quoted 7 lines
Quoted 2 lines

I think we would first have to identify possible interesting tracing points. Did you have anything specific in mind?

I guess the hooks themselves, probably helper invocation and/or results, possibly interesting points within helpers, errors in protocol tracking, basically I think a good starting point would be what we now have as global tracing points.

Ok, I see. I'll think about this some more.

But I don't see why we could not build it upon the proposed infrastructure; we do not have a rule handle and we might need to add further attributes to identify where exactly we are at the moment.

In the patchset proposed here, 'where we are' is defined like this:

NFT_TRACETYPE_PACKET -> in a nft meta rule
(this trace type is followed by NFT_TRACETYPE_RULE one and only exists
so we don't include packet data all the time.)
Alternative would be to remove NFT_TRACETYPE_PACKET and always include payload info instead.

NFT_TRACETYPE_RULE -> contains rule handle to figure out where we are
NFT_TRACETYPE_RETURN -> also contains rule handle NFT_TRACETYPE_POLICY -> no rule handle, 'only' chain and table name

IOW, the current types are very much bolted to rule evaulation.

To support ct/helper etc. events we would need to add new types, e.g. NFT_TRACETYPE_CT, NFT_TRACETYPE_CT_L3PROTO, ... or whatever.

Main problem I see is how the 'where we are' info should be provided and how to include what is happening.

We already have an attribute for verdicts, but that won't be enough. Adding NFTA_TRACE_FILENAME/LINENO seems wrong to me since that would be a pure development thing and we already have tools like perf probe for that.

I don't see anything in the current proposal that would not allow to use it for further netfilter trace events, however, at least for conntrack i suspect it might make more sense to extend ctnetlink instead.

#36 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 24 lines · Florian Westphal

Patrick McHardy <[email protected]> wrote:

Sorry for late reply.

Quoted 11 lines
Quoted 11 lines

chain ct_trace {
type filter hook prerouting priority 0; tcp dport 22 ct state new label set trace ct label trace meta set nftrace 1
}

 chain ct_trace_out {
	 type filter hook output priority 0;
	 ct label trace meta set nftrace 1

}

Quoted 5 lines
Quoted 2 lines

Which would do in/out tracing without any additional code (except the label set support, of course).

That's of course one possibilty but it lacks one important point, we could not trace the first packet going through conntrack.

Right, hooks that run 'earlier' than conntrack would not be able to use ct state matching.

Quoted 7 lines

I'm not sure if thats a real issue, we could just use stateless matching based on ip addresses and ports.

Lastly, I do think that we'll eventually have a ct expression to actually do tracking for us rather than the implicit hooking we have right now (my plan was to add this for nft bridge family conntrack support).

I'm thinking mainly of expected connections, which would be a pretty interesting case for automatic tracing and which are impossible to handle without conntrack interaction. Even with manual tracking we'd still have to do a lookup for every packet to detect those and to detect replies.

Quoted 31 lines
Quoted 7 lines
Quoted 2 lines

I think we would first have to identify possible interesting tracing points. Did you have anything specific in mind?

I guess the hooks themselves, probably helper invocation and/or results, possibly interesting points within helpers, errors in protocol tracking, basically I think a good starting point would be what we now have as global tracing points.

Ok, I see. I'll think about this some more.

But I don't see why we could not build it upon the proposed infrastructure; we do not have a rule handle and we might need to add further attributes to identify where exactly we are at the moment.

In the patchset proposed here, 'where we are' is defined like this:

NFT_TRACETYPE_PACKET -> in a nft meta rule
(this trace type is followed by NFT_TRACETYPE_RULE one and only exists
so we don't include packet data all the time.)
Alternative would be to remove NFT_TRACETYPE_PACKET and always include payload info instead.

NFT_TRACETYPE_RULE -> contains rule handle to figure out where we are
NFT_TRACETYPE_RETURN -> also contains rule handle NFT_TRACETYPE_POLICY -> no rule handle, 'only' chain and table name

IOW, the current types are very much bolted to rule evaulation.

To support ct/helper etc. events we would need to add new types, e.g. NFT_TRACETYPE_CT, NFT_TRACETYPE_CT_L3PROTO, ... or whatever.

Quoted 7 lines

Main problem I see is how the 'where we are' info should be provided and how to include what is happening.

We already have an attribute for verdicts, but that won't be enough. Adding NFTA_TRACE_FILENAME/LINENO seems wrong to me since that would be a pure development thing and we already have tools like perf probe for that.

Yes absolutely. We have a few things that provide stable identifiers, f.i. hooks, helper names, protocol names, subsystems (conntrack/NAT/TCP names will not change), ...

If we need more granularity we need to define some stable sub-identifiers.

Quoted 3 lines

I don't see anything in the current proposal that would not allow to use it for further netfilter trace events, however, at least for conntrack i suspect it might make more sense to extend ctnetlink instead.

Sure, I'm just saying we should consider this. ctnetlink is more about ct events, not really about the packets. I just think it makes sense to have a unified view of tracing because you run into ordering issues if you send events using multiple subsystems. There's also a big advantage in being able to use a simple tool like nft for all your tracing needs compared to combining nft, nfnetlink_log and ctnetlink data to get the full picture.

I'm all for including all the data we need to get a good picture like you have now, I just want to make sure we don't make decisions that prevent us from extending it to other subsystems later on.

author maintainer
#37 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 9 lines · Patrick McHardy
Quoted 4 lines · Florian Westphal

We already have an attribute for verdicts, but that won't be enough. Adding NFTA_TRACE_FILENAME/LINENO seems wrong to me since that would be a pure development thing and we already have tools like perf probe for that.

Yes absolutely. We have a few things that provide stable identifiers, f.i. hooks, helper names, protocol names, subsystems (conntrack/NAT/TCP names will not change), ...

Ok, so that should work within this proposed set, we would need to add new attributes and types:

e.g. NFT_TRACETYPE_CT which then can set NFTA_TRACE_HELPER_NAME or any other new attr we want/need.

Quoted 8 lines

If we need more granularity we need to define some stable sub-identifiers.

Quoted 3 lines

I don't see anything in the current proposal that would not allow to use it for further netfilter trace events, however, at least for conntrack i suspect it might make more sense to extend ctnetlink instead.

Sure, I'm just saying we should consider this. ctnetlink is more about ct events, not really about the packets.

Right.

Quoted 5 lines

I just think it makes sense to
have a unified view of tracing because you run into ordering issues if you send events using multiple subsystems. There's also a big advantage in being able to use a simple tool like nft for all your tracing needs compared to combining nft, nfnetlink_log and ctnetlink data to get the full picture.

Yes, I see. This makes sense. I'm withdrawing the ctnetlink comment. We have extensions for nflog and nfqueue to serialize the CT object entirely withing nfnetlink_log/queue, might also add NFTA_TRACE_CT as a nested attribute that contains the entire ct object.

Quoted 3 lines

I'm all for including all the data we need to get a good picture like you have now, I just want to make sure we don't make decisions that prevent us from extending it to other subsystems later on.

Ok. Basically I'm going to ignore all of your comments here, since I would first like to get the v1 'right' before extending it to more subsystems.

IFF you see anything that prevents us from doing such extension, please yell. But AFAICS the proposed attributes and types should be fine wrt. extensibility.

I'm going to add new netlink group for the trace events and will address all the other comments.

Thanks for reviewing!

#38 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 39 lines · Florian Westphal
Quoted 9 lines · Patrick McHardy
Quoted 4 lines · Florian Westphal

We already have an attribute for verdicts, but that won't be enough. Adding NFTA_TRACE_FILENAME/LINENO seems wrong to me since that would be a pure development thing and we already have tools like perf probe for that.

Yes absolutely. We have a few things that provide stable identifiers, f.i. hooks, helper names, protocol names, subsystems (conntrack/NAT/TCP names will not change), ...

Ok, so that should work within this proposed set, we would need to add new attributes and types:

e.g. NFT_TRACETYPE_CT which then can set NFTA_TRACE_HELPER_NAME or any other new attr we want/need.

Quoted 8 lines

If we need more granularity we need to define some stable sub-identifiers.

Quoted 3 lines

I don't see anything in the current proposal that would not allow to use it for further netfilter trace events, however, at least for conntrack i suspect it might make more sense to extend ctnetlink instead.

Sure, I'm just saying we should consider this. ctnetlink is more about ct events, not really about the packets.

Right.

Quoted 5 lines

I just think it makes sense to
have a unified view of tracing because you run into ordering issues if you send events using multiple subsystems. There's also a big advantage in being able to use a simple tool like nft for all your tracing needs compared to combining nft, nfnetlink_log and ctnetlink data to get the full picture.

Yes, I see. This makes sense. I'm withdrawing the ctnetlink comment. We have extensions for nflog and nfqueue to serialize the CT object entirely withing nfnetlink_log/queue, might also add NFTA_TRACE_CT as a nested attribute that contains the entire ct object.

Quoted 7 lines
Quoted 3 lines

I'm all for including all the data we need to get a good picture like you have now, I just want to make sure we don't make decisions that prevent us from extending it to other subsystems later on.

Ok. Basically I'm going to ignore all of your comments here, since I would first like to get the v1 'right' before extending it to more subsystems.

Absolutely :)

Quoted 6 lines

IFF you see anything that prevents us from doing such extension, please yell. But AFAICS the proposed attributes and types should be fine wrt. extensibility.

I'm going to add new netlink group for the trace events and will address all the other comments.

Sounds good. I'm having first success decoding headers using the nft internal protocol structure.

Sounds good. I'm having first success decoding headers using the nft internal protocol structure. Needs more refinement but I expect to have a RFC later today.

#39 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 3 lines · Patrick McHardy

Sounds good. I'm having first success decoding headers using the nft internal protocol structure. Needs more refinement but I expect to have a RFC later today.

Just FYI, this is my current approach. Still WIP, just so you get the idea, in case you'd like to add some comments.

Basically for the headers, we allocate a constant expression using the header data, based on the hook and (not correctly done so far) ethertype we set up the proto ctx and start expanding the payload expression.

This allows us to decode all the protocols nft knows of.

For things like mark and interfaces we do something quite similar, we allocate the corresponding meta match and dump that.

All together this results in a quite small amount of code and reuses the parts we have.

The current downside is mainly the output ordering and verbosity:

The ordering corresponds to the ordering of the header fields, f.i.:

trace id 85898000 ip ip length 60 ip id 220 ip ttl 64 ip protocol tcp ip saddr 192.168.122.1 ip daddr 192.168.122.84 tcp sport 39558 tcp dport 10000 iif ens3 trace id 85898000 ip filter input verdict 4294967295 iif ens3
trace id 85898000 rule tcp dport 10000 nftrace set 1
trace id 85898000 ip filter input verdict 4294967295 iif ens3
trace id 85898000 rule tcp dport 10000 counter packets 79 bytes 4740
trace id 85898000 ip filter input verdict 4294967295 iif ens3
trace id 85898000 rule tcp dport 10000 continue
trace id 85898000 ip filter input verdict 4294967295 mark 0x00000001 iif ens3
trace id 85898000 rule tcp dport 10000 mark set 0x00000001
trace id 85898000 ip filter input verdict 1 mark 0x00000001 iif ens3

As you can see, the IP addresses are the last two members of the IP header that are dumped, which is not really ideal for readability. Not sure how to fix that yet. Any ideas?

The verbosity results out of two things:

By default we dump every field we can decode. For testing purposes I included a filter in the IPv4 header, so certain fields are skipped. A positive list would also be possible but I've chosen this one so by default we will get everything.

The second part is the we output normal nft expressions, so "ip" is repeated for every header field. Would be easy to surpress that, I'm not sure since the upside is that the expressions can be easily copy and pasted into the ruleset if necessary.

I've also tested with ARP, a couple of fixed were necessary and I've added some members to the ARP definitions for MAC and IP decoding. This is of course not 100% correct since it simply assumes all ARP is related to these protocols:

trace id 85898200 arp ether daddr c9:4b:a9:00:54:52 ether saddr 63:f6:4b:00:54:52 ether type arp arp htype 1 arp ptype ip arp hlen 6 arp plen 4 arp operation reply arp sha 63:f6:4b:00:54:52 arp sip 192.168.122.1 arp tha c9:4b:a9:00:54:52 arp tip 192.168.122.84 iif ens3 trace id 85898200 arp filter input verdict 4294967295 iif ens3
trace id 85898200 rule nftrace set 1
trace id 85898200 arp filter input verdict 4294967295 mark 0x00000001 iif ens3
trace id 85898200 rule mark set 0x00000001 counter packets 599 bytes 27554
trace id 85898200 arp filter input verdict 4294967295 mark 0x00000001 iif ens3
trace id 85898200 rule mark 0x00000001 counter packets 599 bytes 27554
trace id 85898200 arp filter input verdict 1 mark 0x00000001 iif ens3

Comments welcome, especially regarding the current limitations we have.

Cheers,
Patrick

include/proto.h +5 -0
index a43bf98..fa8ff8c 100644
@@ -78,6 +78,7 @@ struct proto_desc {
 	enum proto_bases		base;
 	unsigned int			protocol_key;
 	unsigned int			length;
+	unsigned int			filter;
 	struct {
 		unsigned int			num;
 		const struct proto_desc		*desc;
@@ -170,6 +171,10 @@ enum arp_hdr_fields {
 	ARPHDR_HLN,
 	ARPHDR_PLN,
 	ARPHDR_OP,
+	ARPHDR_SHA,
+	ARPHDR_SPA,
+	ARPHDR_THA,
+	ARPHDR_TPA,
 };
 
 enum ip_hdr_fields {
src/netlink.c +98 -13

128 lines not on this page · raw message

src/payload.c +2 -0
index b75527a..48fc723 100644
@@ -397,6 +397,7 @@ void payload_expr_expand(struct list_head *list, struct expr *expr,
 		goto raw;
 	assert(desc->base == expr->payload.base);
 
+restart:
 	for (i = 1; i < array_size(desc->templates); i++) {
 		tmpl = &desc->templates[i];
 		if (tmpl->offset != expr->payload.offset)
@@ -409,6 +410,7 @@ void payload_expr_expand(struct list_head *list, struct expr *expr,
 			expr->payload.offset += tmpl->len;
 			if (expr->len == off)
 				return;
+			goto restart;
 		} else
 			break;
 	}
src/proto.c +16 -7
index 28b93cb..88999a9 100644
@@ -503,6 +503,11 @@ const struct proto_desc proto_ip = {
 		PROTO_LINK(IPPROTO_DCCP,	&proto_dccp),
 		PROTO_LINK(IPPROTO_SCTP,	&proto_sctp),
 	},
+	.filter		= (1 << IPHDR_VERSION) |
+			  (1 << IPHDR_HDRLENGTH) |
+			  (1 << IPHDR_TOS) |
+			  (1 << IPHDR_FRAG_OFF) |
+			  (1 << IPHDR_CHECKSUM),
 	.templates	= {
 		[IPHDR_VERSION]		= HDR_BITFIELD("version", &integer_type, 4, 4),
 		[IPHDR_HDRLENGTH]	= HDR_BITFIELD("hdrlength", &integer_type, 0, 4),
@@ -663,13 +668,13 @@ const struct proto_desc proto_inet_service = {
 
 static const struct symbol_table arpop_tbl = {
 	.symbols	= {
-		SYMBOL("request",	ARPOP_REQUEST),
-		SYMBOL("reply",		ARPOP_REPLY),
-		SYMBOL("rrequest",	ARPOP_RREQUEST),
-		SYMBOL("rreply",	ARPOP_RREPLY),
-		SYMBOL("inrequest",	ARPOP_InREQUEST),
-		SYMBOL("inreply",	ARPOP_InREPLY),
-		SYMBOL("nak",		ARPOP_NAK),
+		SYMBOL("request",	__constant_htons(ARPOP_REQUEST)),
+		SYMBOL("reply",		__constant_htons(ARPOP_REPLY)),
+		SYMBOL("rrequest",	__constant_htons(ARPOP_RREQUEST)),
+		SYMBOL("rreply",	__constant_htons(ARPOP_RREPLY)),
+		SYMBOL("inrequest",	__constant_htons(ARPOP_InREQUEST)),
+		SYMBOL("inreply",	__constant_htons(ARPOP_InREPLY)),
+		SYMBOL("nak",		__constant_htons(ARPOP_NAK)),
 		SYMBOL_LIST_END
 	},
 };
@@ -698,6 +703,10 @@ const struct proto_desc proto_arp = {
 		[ARPHDR_HLN]		= ARPHDR_FIELD("hlen", ar_hln),
 		[ARPHDR_PLN]		= ARPHDR_FIELD("plen", ar_pln),
 		[ARPHDR_OP]		= ARPHDR_TYPE("operation", &arpop_type, ar_op),
+		[ARPHDR_SHA]		= PROTO_HDR_TEMPLATE("sha", &etheraddr_type, BYTEORDER_BIG_ENDIAN, 8 * BITS_PER_BYTE, 6 * BITS_PER_BYTE),
+		[ARPHDR_SPA]		= PROTO_HDR_TEMPLATE("sip", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 14 * BITS_PER_BYTE, 4 * BITS_PER_BYTE),
+		[ARPHDR_THA]		= PROTO_HDR_TEMPLATE("tha", &etheraddr_type, BYTEORDER_BIG_ENDIAN, 18 * BITS_PER_BYTE, 6 * BITS_PER_BYTE),
+		[ARPHDR_TPA]		= PROTO_HDR_TEMPLATE("tip", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 24 * BITS_PER_BYTE, 4 * BITS_PER_BYTE),
 	},
 };
 
#40 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

src/proto.c const struct proto_desc proto_arp = { · line 712

Quoted 11 lines · Patrick McHardy
src/proto.c +4 -0
@@ -698,6 +703,10 @@ const struct proto_desc proto_arp = {
 		[ARPHDR_HLN]		= ARPHDR_FIELD("hlen", ar_hln),
 		[ARPHDR_PLN]		= ARPHDR_FIELD("plen", ar_pln),
 		[ARPHDR_OP]		= ARPHDR_TYPE("operation", &arpop_type, ar_op),
+		[ARPHDR_SHA]		= PROTO_HDR_TEMPLATE("sha", &etheraddr_type, BYTEORDER_BIG_ENDIAN, 8 * BITS_PER_BYTE, 6 * BITS_PER_BYTE),
+		[ARPHDR_SPA]		= PROTO_HDR_TEMPLATE("sip", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 14 * BITS_PER_BYTE, 4 * BITS_PER_BYTE),
+		[ARPHDR_THA]		= PROTO_HDR_TEMPLATE("tha", &etheraddr_type, BYTEORDER_BIG_ENDIAN, 18 * BITS_PER_BYTE, 6 * BITS_PER_BYTE),
+		[ARPHDR_TPA]		= PROTO_HDR_TEMPLATE("tip", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 24 * BITS_PER_BYTE, 4 * BITS_PER_BYTE),
 	},
 };
 

Could you also push this chunk into the repository?

I have a similar patch here for this. Given that we only support ARP for IPv4, we don't have to worry about the variable length offset in this case.

Thanks.

#41 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 15 lines · Pablo Neira Ayuso

src/proto.c const struct proto_desc proto_arp = { · line 712

Quoted 11 lines · Patrick McHardy
src/proto.c +4 -0
@@ -698,6 +703,10 @@ const struct proto_desc proto_arp = {
 		[ARPHDR_HLN]		= ARPHDR_FIELD("hlen", ar_hln),
 		[ARPHDR_PLN]		= ARPHDR_FIELD("plen", ar_pln),
 		[ARPHDR_OP]		= ARPHDR_TYPE("operation", &arpop_type, ar_op),
+		[ARPHDR_SHA]		= PROTO_HDR_TEMPLATE("sha", &etheraddr_type, BYTEORDER_BIG_ENDIAN, 8 * BITS_PER_BYTE, 6 * BITS_PER_BYTE),
+		[ARPHDR_SPA]		= PROTO_HDR_TEMPLATE("sip", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 14 * BITS_PER_BYTE, 4 * BITS_PER_BYTE),
+		[ARPHDR_THA]		= PROTO_HDR_TEMPLATE("tha", &etheraddr_type, BYTEORDER_BIG_ENDIAN, 18 * BITS_PER_BYTE, 6 * BITS_PER_BYTE),
+		[ARPHDR_TPA]		= PROTO_HDR_TEMPLATE("tip", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 24 * BITS_PER_BYTE, 4 * BITS_PER_BYTE),
 	},
 };
 

Could you also push this chunk into the repository?

Quoted 3 lines

I have a similar patch here for this. Given that we only support ARP for IPv4, we don't have to worry about the variable length offset in this case.

Sure. So far I've only added the decoding part, so the parser has no way to support this yet. The symbol names are quite ugly, any ideas for nicer ones?

Cheers,
Patrick

author maintainer
#42 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

In reply to Patrick McHardy
Quoted 11 lines · Patrick McHardy
Quoted 3 lines · Patrick McHardy

Sounds good. I'm having first success decoding headers using the nft internal protocol structure. Needs more refinement but I expect to have a RFC later today.

Just FYI, this is my current approach. Still WIP, just so you get the idea, in case you'd like to add some comments.

Basically for the headers, we allocate a constant expression using the header data, based on the hook and (not correctly done so far) ethertype we set up the proto ctx and start expanding the payload expression.

[..]

Quoted 2 lines

All together this results in a quite small amount of code and reuses the parts we have.

Indeed.

Quoted 15 lines

The current downside is mainly the output ordering and verbosity:

The ordering corresponds to the ordering of the header fields, f.i.:

trace id 85898000 ip ip length 60 ip id 220 ip ttl 64 ip protocol tcp ip saddr 192.168.122.1 ip daddr 192.168.122.84 tcp sport 39558 tcp dport 10000 iif ens3 trace id 85898000 ip filter input verdict 4294967295 iif ens3
trace id 85898000 rule tcp dport 10000 nftrace set 1
trace id 85898000 ip filter input verdict 4294967295 iif ens3
trace id 85898000 rule tcp dport 10000 counter packets 79 bytes 4740
trace id 85898000 ip filter input verdict 4294967295 iif ens3
trace id 85898000 rule tcp dport 10000 continue
trace id 85898000 ip filter input verdict 4294967295 mark 0x00000001 iif ens3
trace id 85898000 rule tcp dport 10000 mark set 0x00000001
trace id 85898000 ip filter input verdict 1 mark 0x00000001 iif ens3

Quoted 3 lines

As you can see, the IP addresses are the last two members of the IP header that are dumped, which is not really ideal for readability. Not sure how to fix that yet. Any ideas?

Hmm, I think it actually increases readability, as all the other lines you quoted above are a lot shorter the ip saddr part is a lot more visible.

If nftrace netfilter rules are used properly (ie., with limit statement or other means to narrow matching down) those long lines even sever as kind-of delimiter to where a new round of trace output starts without looking at the id.

As wrt. fixing it, I don't think this should be over-engineered.

I would propose to annotate the address fields in the protocol description, similar to the filter you added, and then walk header several times.

1st round dumps source address
2nd round dumps dst address
3rd round dumps the next protocol field
4th round dumps the rest that isn't 'blacklisted' (such as checksum).

Ugly, but given headers differ completely I don't see a better solution.
Since headers are commonly small the overhead should not be a big deal.

In case of ethernet, dst is first header field -- not sure if we should re-order it so that src a:b.. dst d: .. is shown.

If you prefer to keep the real header ordering, then we don't need to differentiate between these two.

Quoted 1 line

Comments welcome, especially regarding the current limitations we have.

Looks fantastic, I am leaning towards dropping the header formatting from libnftl patch entirely.

src/netlink.c static void trace_print_rule(const struct nftnl_trace *nlt) · line 2179

Quoted 2 lines
src/netlink.c +1 -0
+	if (!nftnl_trace_is_set(nlt, attr))
 		return;

Eeek, sorry about that.

src/netlink.c static void trace_print_rule(const struct nftnl_trace *nlt) · line 2215

Quoted 6 lines
src/netlink.c +6 -0
+static void trace_print_packet(const struct nftnl_trace *nlt)
+{
+	struct proto_ctx ctx;
+
+	proto_ctx_init(&ctx, nftnl_trace_get_u32(nlt, NFTNL_TRACE_FAMILY));
+	// NFTA_TRACE_DEV_TYPE

This attribute should be exported already.

The limitation (same as with my patch) is that if we don't know the value then we cannot pretty-print that L2 header.
I don't think thats a problem, we can just move on to NH.

Quoted 26 lines
src/netlink.c +17 -2
+static struct expr *trace_alloc_expr(const struct nftnl_trace *nlt,
+				     unsigned int attr,
+				     struct expr *lhs)
+{
+	struct expr *rhs, *rel;
+	const void *data;
+	uint32_t len;
+
+	data = nftnl_trace_get_data(nlt, attr, &len);
+	rhs  = constant_expr_alloc(&netlink_location,
+				   lhs->dtype, lhs->byteorder,
+				   len * BITS_PER_BYTE, data);
+	rel = relational_expr_alloc(&netlink_location, OP_EQ, lhs, rhs);
+	expr_print(rel);
+	printf(" ");
+	return rel;
 }
 
 static int netlink_events_trace_cb(const struct nlmsghdr *nlh, int type,
@@ -2194,11 +2254,36 @@ static int netlink_events_trace_cb(const struct nlmsghdr *nlh, int type,
 	if (nftnl_trace_nlmsg_parse(nlh, nlt) < 0)
 		netlink_abi_error();
 
-	printf("trace ");
-	nftnl_trace_fprintf(stdout, nlt, monh->format);
+	printf("trace id %08x ", nftnl_trace_get_u32(nlt, NFTNL_TRACE_ID));

src/netlink.c static int netlink_events_trace_cb(const struct nlmsghdr *nlh, int type, · line 2267

Quoted 10 lines
src/netlink.c +10 -0
+
+	printf("%s ", family2str(nftnl_trace_get_u32(nlt, NFTNL_TRACE_FAMILY)));
+	if (nftnl_trace_is_set(nlt, NFTNL_TRACE_TABLE))
+		printf("%s ", nftnl_trace_get_str(nlt, NFTNL_TRACE_TABLE));
+	if (nftnl_trace_is_set(nlt, NFTNL_TRACE_CHAIN))
+		printf("%s ", nftnl_trace_get_str(nlt, NFTNL_TRACE_CHAIN));
+
+	// type
+	if (nftnl_trace_is_set(nlt, NFTNL_TRACE_VERDICT))
+		printf("verdict %u ", nftnl_trace_get_u32(nlt, NFTNL_TRACE_VERDICT));

I am working on this.

Dissecting the verdict will happen in libnftnl, so NFTNL_TRACE_VERDICT will return a verdict without extra information.

I'll add NFTNL_TRACE_NFQUEUE_ID to store the upper 16 bits for QUEUE verdicts.

src/proto.c const struct proto_desc proto_ip = { · line 510

Quoted 13 lines
src/proto.c +5 -0
index 28b93cb..88999a9 100644
@@ -503,6 +503,11 @@ const struct proto_desc proto_ip = {
 		PROTO_LINK(IPPROTO_DCCP,	&proto_dccp),
 		PROTO_LINK(IPPROTO_SCTP,	&proto_sctp),
 	},
+	.filter		= (1 << IPHDR_VERSION) |
+			  (1 << IPHDR_HDRLENGTH) |
+			  (1 << IPHDR_TOS) |
+			  (1 << IPHDR_FRAG_OFF) |
+			  (1 << IPHDR_CHECKSUM),

Good that we suppress checksum, it will cause lot of confusion due to offloads anyway.

#43 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 14 lines · Florian Westphal
Quoted 13 lines · Patrick McHardy

The ordering corresponds to the ordering of the header fields, f.i.:

trace id 85898000 ip ip length 60 ip id 220 ip ttl 64 ip protocol tcp ip saddr 192.168.122.1 ip daddr 192.168.122.84 tcp sport 39558 tcp dport 10000 iif ens3 trace id 85898000 ip filter input verdict 4294967295 iif ens3
trace id 85898000 rule tcp dport 10000 nftrace set 1
trace id 85898000 ip filter input verdict 4294967295 iif ens3
trace id 85898000 rule tcp dport 10000 counter packets 79 bytes 4740
trace id 85898000 ip filter input verdict 4294967295 iif ens3
trace id 85898000 rule tcp dport 10000 continue
trace id 85898000 ip filter input verdict 4294967295 mark 0x00000001 iif ens3
trace id 85898000 rule tcp dport 10000 mark set 0x00000001
trace id 85898000 ip filter input verdict 1 mark 0x00000001 iif ens3

Quoted 7 lines
Quoted 3 lines

As you can see, the IP addresses are the last two members of the IP header that are dumped, which is not really ideal for readability. Not sure how to fix that yet. Any ideas?

Hmm, I think it actually increases readability, as all the other lines you quoted above are a lot shorter the ip saddr part is a lot more visible.

They are actually still missing some minor parts from the original output :)

But if we want to shorten them, I would suggest f.i. to not repeat the devices on every line. It seems to logically belong to the "packet" part, same as vlan id. I guess the only thing we actually need to repeat is the mark since that might change while we're within the ruleset.

Quoted 4 lines

If nftrace netfilter rules are used properly (ie., with limit statement or other means to narrow matching down) those long lines even sever as kind-of delimiter to where a new round of trace output starts without looking at the id.

I agree, that part makes for a good delimiter.

Quoted 11 lines

As wrt. fixing it, I don't think this should be over-engineered.

I would propose to annotate the address fields in the protocol description, similar to the filter you added, and then walk header several times.

1st round dumps source address
2nd round dumps dst address
3rd round dumps the next protocol field
4th round dumps the rest that isn't 'blacklisted' (such as checksum).

Quoted 4 lines

Ugly, but given headers differ completely I don't see a better solution.
Since headers are commonly small the overhead should not be a big deal.

I've also considered this and so far it was my favourite approach as well. Unfortunately it requires annotations for all headers, but I guess that's an acceptable tradeoff.

Quoted 5 lines

In case of ethernet, dst is first header field -- not sure if we should re-order it so that src a:b.. dst d: .. is shown.

If you prefer to keep the real header ordering, then we don't need to differentiate between these two.

I personally generally prefer what I perceive as "logical ordering", which is src, dst, ....

Quoted 4 lines
Quoted 1 line

Comments welcome, especially regarding the current limitations we have.

Looks fantastic, I am leaning towards dropping the header formatting from libnftl patch entirely.

I agree, we could drop the header part and only print the meta data. So we can see that something is happening and associate the events with decoded messages, but full decoding seems unnecessary.

Quoted 5 lines

src/netlink.c static void trace_print_rule(const struct nftnl_trace *nlt) · line 2179

Quoted 2 lines
src/netlink.c +1 -0
+	if (!nftnl_trace_is_set(nlt, attr))
 		return;

Eeek, sorry about that.

Quoted 8 lines

src/netlink.c static void trace_print_rule(const struct nftnl_trace *nlt) · line 2215

Quoted 6 lines
src/netlink.c +6 -0
+static void trace_print_packet(const struct nftnl_trace *nlt)
+{
+	struct proto_ctx ctx;
+
+	proto_ctx_init(&ctx, nftnl_trace_get_u32(nlt, NFTNL_TRACE_FAMILY));
+	// NFTA_TRACE_DEV_TYPE

This attribute should be exported already.

Yep, I just didn't integrate it properly yet.

Quoted 3 lines

The limitation (same as with my patch) is that if we don't know the value then we cannot pretty-print that L2 header.
I don't think thats a problem, we can just move on to NH.

It depends, f.i. for bridging we can infer it from the hook values. In fact proto_ctx_init() already does that correctly for bridging.

Quoted 9 lines

src/netlink.c static int netlink_events_trace_cb(const struct nlmsghdr *nlh, int type, · line 2267

Quoted 2 lines
src/netlink.c +2 -0
+	if (nftnl_trace_is_set(nlt, NFTNL_TRACE_VERDICT))
+		printf("verdict %u ", nftnl_trace_get_u32(nlt, NFTNL_TRACE_VERDICT));

I am working on this.

Dissecting the verdict will happen in libnftnl, so NFTNL_TRACE_VERDICT will return a verdict without extra information.

I'll add NFTNL_TRACE_NFQUEUE_ID to store the upper 16 bits for QUEUE verdicts.

I don't think that's necessary. All the other verdict parts (actually nft_data) simply return the numeric verdict code and also accept them, both for encoded DROP_ERRNOS and queue numbers. Is there any reason to diverge from this?

Thanks for your feedback. I'll start incorporating it now.

Cheers,
Patrick

#44 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 11 lines · Patrick McHardy
Quoted 3 lines · Florian Westphal

Hmm, I think it actually increases readability, as all the other lines you quoted above are a lot shorter the ip saddr part is a lot more visible.

They are actually still missing some minor parts from the original output :)

But if we want to shorten them, I would suggest f.i. to not repeat the devices on every line. It seems to logically belong to the "packet" part, same as vlan id. I guess the only thing we actually need to repeat is the mark since that might change while we're within the ruleset.

Actually thinking more about this, we might want to send a new "packet" message whenever we enter nft_do_chain(). At that point the packet has been processed by other parts of the network stack since the last "packet" message and it might be helpful to know in which ways it has changed.

author maintainer
#45 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 6 lines · Patrick McHardy
Quoted 5 lines · Patrick McHardy
Quoted 3 lines · Florian Westphal

Hmm, I think it actually increases readability, as all the other lines you quoted above are a lot shorter the ip saddr part is a lot more visible.

Quoted 6 lines
Quoted 6 lines

They are actually still missing some minor parts from the original output :)

But if we want to shorten them, I would suggest f.i. to not repeat the devices on every line. It seems to logically belong to the "packet" part, same as vlan id. I guess the only thing we actually need to repeat is the mark since that might change while we're within the ruleset.

We can do this, but we'll need to make sure that the oif gets printed at one point (not available in prerouting)...

Quoted 4 lines

Actually thinking more about this, we might want to send a new "packet" message whenever we enter nft_do_chain(). At that point the packet has been processed by other parts of the network stack since the last "packet" message and it might be helpful to know in which ways it has changed.

True, good point. In that case I would propose to get rid of "packet" message type completely.

Instead we'd include all the info that we currently have in "packet" (i.e. vlanid, headers) on the first message type fired on each nft_do_chain() invocation.

We can also move IIF/OIF info to this 'initial' message (which might be of any type depending on the ruleset, due to POLICY
type we would however always send at least one, even if there are no matches).

The price to be paid would be a new variable that we have to keep on-stack to know when we can elide the extra packet data.

Does that sound reasonable?

#46 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 19 lines · Florian Westphal
Quoted 4 lines · Patrick McHardy

Actually thinking more about this, we might want to send a new "packet" message whenever we enter nft_do_chain(). At that point the packet has been processed by other parts of the network stack since the last "packet" message and it might be helpful to know in which ways it has changed.

True, good point. In that case I would propose to get rid of "packet" message type completely.

Instead we'd include all the info that we currently have in "packet" (i.e. vlanid, headers) on the first message type fired on each nft_do_chain() invocation.

We can also move IIF/OIF info to this 'initial' message (which might be of any type depending on the ruleset, due to POLICY
type we would however always send at least one, even if there are no matches).

Quoted 4 lines

The price to be paid would be a new variable that we have to keep on-stack to know when we can elide the extra packet data.

Does that sound reasonable?

Sure, but is that really easier than including an unconditional (well, skb->nf_trace == 1) call to nf_tables_trace_notify() before entering the main loop? I don't see anything wrong the the packet message itself, just thinking it might be useful to emit one more in this specific spot.

author maintainer
#47 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 15 lines · Patrick McHardy
Quoted 15 lines

True, good point. In that case I would propose to get rid of "packet" message type completely.

Instead we'd include all the info that we currently have in "packet" (i.e. vlanid, headers) on the first message type fired on each nft_do_chain() invocation.

We can also move IIF/OIF info to this 'initial' message (which might be of any type depending on the ruleset, due to POLICY
type we would however always send at least one, even if there are no matches).

The price to be paid would be a new variable that we have to keep on-stack to know when we can elide the extra packet data.

Quoted 7 lines
Quoted 1 line

Does that sound reasonable?

Sure, but is that really easier than including an unconditional (well, skb->nf_trace == 1) call to nf_tables_trace_notify() before entering the main loop? I don't see anything wrong the the packet message itself, just thinking it might be useful to emit one more in this specific spot.

Its not easier, but it will keep the number of messages lower.

AFAIU with nf_tables_trace_notify() for each nft_do_chain() we will have one more message for each table/hook combo... Hmm...

I I'll see how painful the extra test for 'i have sent packet data for this nft_do_chain invocation' is, if its too complicated I'll move the nf_tables_trace_notify from nft_meta to nft_do_chain.

Let me know in case you spot a problem with that rationale.

Thanks!

#48 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 24 lines · Florian Westphal
Quoted 15 lines · Patrick McHardy
Quoted 15 lines

True, good point. In that case I would propose to get rid of "packet" message type completely.

Instead we'd include all the info that we currently have in "packet" (i.e. vlanid, headers) on the first message type fired on each nft_do_chain() invocation.

We can also move IIF/OIF info to this 'initial' message (which might be of any type depending on the ruleset, due to POLICY
type we would however always send at least one, even if there are no matches).

The price to be paid would be a new variable that we have to keep on-stack to know when we can elide the extra packet data.

Quoted 7 lines
Quoted 1 line

Does that sound reasonable?

Sure, but is that really easier than including an unconditional (well, skb->nf_trace == 1) call to nf_tables_trace_notify() before entering the main loop? I don't see anything wrong the the packet message itself, just thinking it might be useful to emit one more in this specific spot.

Quoted 4 lines

Its not easier, but it will keep the number of messages lower.

AFAIU with nf_tables_trace_notify() for each nft_do_chain() we will have one more message for each table/hook combo... Hmm...

Yeah that's really a bit unfortunate since we actually only care about the first chain on every hook I guess. Basically once per hook, but it seems we really need to use once per base chain unless we go deeper down in the stack.

Quoted 5 lines

I I'll see how painful the extra test for 'i have sent packet data for this nft_do_chain invocation' is, if its too complicated I'll move the nf_tables_trace_notify from nft_meta to nft_do_chain.

Let me know in case you spot a problem with that rationale.

No, that seems reasonable to me.

#49 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

In reply to Florian Westphal
Quoted 7 lines · Florian Westphal
Quoted 4 lines · Patrick McHardy

Actually thinking more about this, we might want to send a new "packet" message whenever we enter nft_do_chain(). At that point the packet has been processed by other parts of the network stack since the last "packet" message and it might be helpful to know in which ways it has changed.

Quoted 6 lines

True, good point. In that case I would propose to get rid of "packet" message type completely.

Instead we'd include all the info that we currently have in "packet" (i.e. vlanid, headers) on the first message type fired on each nft_do_chain() invocation.

One more comment since I'm just in the VLAN area: I think we need to include the full vlan_tci and vlan_proto. Basically we want to be able to display a dummy VLAN header I'd say instead of treating it like something special.

Even nicer would be to include the dummy VLAN header in the payload itself and hide the offloading details, but that we can also do in userspace of course.

author maintainer
#50 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 3 lines · Patrick McHardy

Even nicer would be to include the dummy VLAN header in the payload itself and hide the offloading details, but that we can also do in userspace of course.

Will do that.

author maintainer
#51 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

In reply to Patrick McHardy
Quoted 8 lines · Patrick McHardy
Quoted 5 lines

In case of ethernet, dst is first header field -- not sure if we should re-order it so that src a:b.. dst d: .. is shown.

If you prefer to keep the real header ordering, then we don't need to differentiate between these two.

I personally generally prefer what I perceive as "logical ordering", which is src, dst, ....

Same here.

Quoted 6 lines
Quoted 2 lines

Looks fantastic, I am leaning towards dropping the header formatting from libnftl patch entirely.

I agree, we could drop the header part and only print the meta data. So we can see that something is happening and associate the events with decoded messages, but full decoding seems unnecessary.

I could also get rid of all formatting of course, i.e. remove the libnftnl_trace_*printf functions, so libnftnl would just provide the nftnl_trace struct to nftables frontend and the internal translation of netlink messages to that struct.

Quoted 6 lines
Quoted 3 lines

The limitation (same as with my patch) is that if we don't know the value then we cannot pretty-print that L2 header.
I don't think thats a problem, we can just move on to NH.

It depends, f.i. for bridging we can infer it from the hook values. In fact proto_ctx_init() already does that correctly for bridging.

Yes, this DEV_TYPE attribute is only included for NFPROTO_NETDEV where L2 could be anything ...

Quoted 5 lines
Quoted 5 lines

src/netlink.c static int netlink_events_trace_cb(const struct nlmsghdr *nlh, int type, · line 2267

Quoted 2 lines
src/netlink.c +2 -0
+	if (nftnl_trace_is_set(nlt, NFTNL_TRACE_VERDICT))
+		printf("verdict %u ", nftnl_trace_get_u32(nlt, NFTNL_TRACE_VERDICT));

I am working on this.

Quoted 8 lines
Quoted 4 lines

Dissecting the verdict will happen in libnftnl, so NFTNL_TRACE_VERDICT will return a verdict without extra information.

I'll add NFTNL_TRACE_NFQUEUE_ID to store the upper 16 bits for QUEUE verdicts.

I don't think that's necessary. All the other verdict parts (actually nft_data) simply return the numeric verdict code and also accept them, both for encoded DROP_ERRNOS and queue numbers. Is there any reason to diverge from this?

Hmm, to clarify: The kernel will dump the verdict as-is. But I don't see why nftables (nft) should have to start dissecting this to e.g. get the nfqueue number. So I'd rather put something like the following into libnftnl and provide accessors for nft rather than stuff it into the frontend tool:

verdict = mnl_get_ (netlink_data);
switch (verdict) {
case NFT_GOTO:
case NFT_JUMP: t->targetchain = get_target_chain(netlink_data); case NFT_CONTINUE:
// all other known verdicts here

	trace->verdict = verdict;
	break;
default: /* unknown verdict, or extra data in upper bits */
     switch (verdict & 0xff) {
     case NF_QUEUE:
        trace->verdict = verdict & 0xff;
        trace->queue_id = verdict >> 16;
        break;
     }

}

Without it, simple tasks like 'translate verdict to string' in nft require extra exercise to strip away the irrelevant parts of the verdict code.

Does that seem reasonable?

#52 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 5 lines · Florian Westphal
Quoted 3 lines · Patrick McHardy
Quoted 2 lines

Looks fantastic, I am leaning towards dropping the header formatting from libnftl patch entirely.

Quoted 8 lines
Quoted 3 lines

I agree, we could drop the header part and only print the meta data. So we can see that something is happening and associate the events with decoded messages, but full decoding seems unnecessary.

I could also get rid of all formatting of course, i.e. remove the libnftnl_trace_*printf functions, so libnftnl would just provide the nftnl_trace struct to nftables frontend and the internal translation of netlink messages to that struct.

I think having some printing is useful to trace/debug netlink communication itself, IOW the basic data. Just the full header decoding seems rather unnecessary.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

The limitation (same as with my patch) is that if we don't know the value then we cannot pretty-print that L2 header.
I don't think thats a problem, we can just move on to NH.

It depends, f.i. for bridging we can infer it from the hook values. In fact proto_ctx_init() already does that correctly for bridging.

Yes, this DEV_TYPE attribute is only included for NFPROTO_NETDEV where L2 could be anything ...

I actually changed that in my local version to include it whenever we have pkt->in so we can decode ARP using dev_proto_desc(). I'm also including the LL header whenever present so its now also used for f.i. incoming IPv4/v6 packets:

trace id 847cdc00 ip packet: iif ens3 ether daddr c9:4b:a9:00:54:52 ether saddr 63:f6:4b:00:54:52 ip hdrlength 4 ip version 5 ip tos 16 ip length 60 ip id 44588 ip frag-off 16384 ip ttl 64 ip checksum 5849 ip saddr 192.168.122.1 ip daddr 192.168.122.84 tcp sport 43178 tcp dport 10000

I've attached that change to the mail. At least the DEV_TYPE part is needed to make sense out of the ARP ll header in a generic way.

Quoted 35 lines
Quoted 5 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (nftnl_trace_is_set(nlt, NFTNL_TRACE_VERDICT))
+		printf("verdict %u ", nftnl_trace_get_u32(nlt, NFTNL_TRACE_VERDICT));

I am working on this.

Quoted 8 lines
Quoted 4 lines

Dissecting the verdict will happen in libnftnl, so NFTNL_TRACE_VERDICT will return a verdict without extra information.

I'll add NFTNL_TRACE_NFQUEUE_ID to store the upper 16 bits for QUEUE verdicts.

I don't think that's necessary. All the other verdict parts (actually nft_data) simply return the numeric verdict code and also accept them, both for encoded DROP_ERRNOS and queue numbers. Is there any reason to diverge from this?

Hmm, to clarify: The kernel will dump the verdict as-is. But I don't see why nftables (nft) should have to start dissecting this to e.g. get the nfqueue number. So I'd rather put something like the following into libnftnl and provide accessors for nft rather than stuff it into the frontend tool:

verdict = mnl_get_ (netlink_data);
switch (verdict) {
case NFT_GOTO:
case NFT_JUMP: t->targetchain = get_target_chain(netlink_data); case NFT_CONTINUE:
// all other known verdicts here

	trace->verdict = verdict;
	break;
default: /* unknown verdict, or extra data in upper bits */
     switch (verdict & 0xff) {
     case NF_QUEUE:
        trace->verdict = verdict & 0xff;
        trace->queue_id = verdict >> 16;
        break;
     }
Quoted 7 lines

}

Without it, simple tasks like 'translate verdict to string' in nft require extra exercise to strip away the irrelevant parts of the verdict code.

Does that seem reasonable?

Yeah, that seems fine. I though we already did that disection in verdict_type_print(), but it was in fact only a local change queued in my tree.

net/netfilter/nf_tables_api.c +4 -14
index e22e929..3a0e22b 100644
@@ -518,20 +518,9 @@ trace_notify_put_packet(struct sk_buff *nlskb, const struct nft_pktinfo *pkt)
 				   skb, pkt->xt.thoff, plen))
 		return false;
 
-	switch (pkt->pf) {
-	case NFPROTO_ARP: /* fallthrough */
-	case NFPROTO_BRIDGE:
-		break;
-	case NFPROTO_NETDEV:
-		if (WARN_ON_ONCE(!skb->dev))
-			break;
-		if (nla_put_be16(nlskb, NFTA_TRACE_DEV_TYPE,
-				 htons(skb->dev->type)))
-			return false;
-		break;
-	default:
-		return true;
-	}
+	if (pkt->in &&
+	    nla_put_be16(nlskb, NFTA_TRACE_DEV_TYPE, htons(pkt->in->type)))
+		return false;
 
 	if (skb_vlan_tag_get(skb) &&
 	    !nla_put_be16(nlskb, NFTA_TRACE_VLAN_TAG,
@@ -688,6 +677,7 @@ void nf_tables_trace_notify(const struct nft_pktinfo *pkt,
 	WARN_ON_ONCE(1);
 	kfree_skb(skb);
 }
+EXPORT_SYMBOL_GPL(nf_tables_trace_notify);
 
 static int nf_tables_dump_tables(struct sk_buff *skb,
 				 struct netlink_callback *cb)
net/netfilter/nft_meta.c +1 -0
index c2a60ac..9342779 100644
@@ -350,6 +350,7 @@ void nft_meta_set_destroy(const struct nft_ctx *ctx,
 	if (priv->key == NFT_META_NFTRACE)
 		static_key_slow_dec(&nft_trace_enabled);
 }
+EXPORT_SYMBOL_GPL(nft_meta_set_destroy);
 
 static struct nft_expr_type nft_meta_type;
 static const struct nft_expr_ops nft_meta_get_ops = {
author maintainer
#53 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 9 lines · Patrick McHardy
Quoted 4 lines · Florian Westphal

I could also get rid of all formatting of course, i.e. remove the libnftnl_trace_*printf functions, so libnftnl would just provide the nftnl_trace struct to nftables frontend and the internal translation of netlink messages to that struct.

I think having some printing is useful to trace/debug netlink communication itself, IOW the basic data. Just the full header decoding seems rather unnecessary.

Okay.

Quoted 8 lines
Quoted 2 lines

Yes, this DEV_TYPE attribute is only included for NFPROTO_NETDEV where L2 could be anything ...

I actually changed that in my local version to include it whenever we have pkt->in so we can decode ARP using dev_proto_desc(). I'm also including the LL header whenever present so its now also used for f.i. incoming IPv4/v6 packets:

Quoted 4 lines

trace id 847cdc00 ip packet: iif ens3 ether daddr c9:4b:a9:00:54:52 ether saddr 63:f6:4b:00:54:52 ip hdrlength 4 ip version 5 ip tos 16 ip length 60 ip id 44588 ip frag-off 16384 ip ttl 64 ip checksum 5849 ip saddr 192.168.122.1 ip daddr 192.168.122.84 tcp sport 43178 tcp dport 10000

I've attached that change to the mail. At least the DEV_TYPE part is needed to make sense out of the ARP ll header in a generic way.

I see, thanks!

net/netfilter/nf_tables_api.c trace_notify_put_packet(struct sk_buff *nlskb, const struct nft_pktinfo *pkt) · line 523

Quoted 3 lines
net/netfilter/nf_tables_api.c +3 -0
+	if (pkt->in &&
+	    nla_put_be16(nlskb, NFTA_TRACE_DEV_TYPE, htons(pkt->in->type)))
+		return false;

Yes, much better. Thanks Patrick!

#54 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

In reply to Patrick McHardy
Quoted 10 lines · Patrick McHardy
Quoted 9 lines · Florian Westphal

I would propose to annotate the address fields in the protocol description, similar to the filter you added, and then walk header several times.

1st round dumps source address
2nd round dumps dst address
3rd round dumps the next protocol field
4th round dumps the rest that isn't 'blacklisted' (such as checksum).

Quoted 8 lines
Quoted 4 lines

Ugly, but given headers differ completely I don't see a better solution.
Since headers are commonly small the overhead should not be a big deal.

I've also considered this and so far it was my favourite approach as well. Unfortunately it requires annotations for all headers, but I guess that's an acceptable tradeoff.

Ok here's might current state. I've added an output filter and defined output ordering, so we can surpress some fields and order the remaining ones the way we want. I've also added redundant payload dependency elimination.

Example output looks like this:

trace id 85060d00 arp packet: iif ens3 ether saddr 63:f6:4b:00:54:52 ether daddr c9:4b:a9:00:54:52 arp operation reply arp sha 63:f6:4b:00:54:52 arp sip 192.168.122.1 arp tha c9:4b:a9:00:54:52 arp tip 192.168.122.84

trace id 853ff400 ip packet: iif ens3 ether saddr 63:f6:4b:00:54:52 ether daddr c9:4b:a9:00:54:52 ip saddr 192.168.122.1 ip daddr 192.168.122.84 ip tos 16 ip ttl 64 ip id 38325 ip length 60 tcp sport 46156 tcp dport 10000

trace id 853ffc00 ip packet: oif ens3 ip saddr 192.168.122.84 ip daddr 192.168.122.1 ip tos 16 ip ttl 64 ip id 51655 ip length 40 tcp sport 10000 tcp dport 46156

If people are happy this way I'll start getting it into final shape.

Cheers,
Patrick

commit 0df38a649c851291352a410ee6ebbc9492f9b7d2

Author: Patrick McHardy <[email protected]>
Date:   Wed Nov 25 18:46:14 2015 +0000

payload: move payload depedency tracking to payload.c

include/payload.h +18 -0
index ca9b013..619a9c9 100644
@@ -15,6 +15,24 @@ struct stmt;
 extern int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr,
 				  struct stmt **res);
 
+/**
+ * struct payload_dep_ctx - payload protocol dependency tracking
+ *
+ * @pbase: protocol base of last dependency match
+ * @pdep: last dependency match
+ */
+struct payload_dep_ctx {
+	enum proto_bases	pbase;
+	struct stmt		*pdep;
+};
+
+extern void payload_dependency_store(struct payload_dep_ctx *ctx,
+				     struct stmt *stmt,
+				     enum proto_bases base);
+extern void payload_dependency_kill(struct payload_dep_ctx *ctx,
+				    struct expr *expr);
+
+
 extern bool payload_is_adjacent(const struct expr *e1, const struct expr *e2);
 extern struct expr *payload_expr_join(const struct expr *e1,
 				      const struct expr *e2);
src/netlink_delinearize.c +11 -33

87 lines not on this page · raw message

src/payload.c +36 -0
index a97041e..dabd504 100644
@@ -282,6 +282,42 @@ int payload_gen_dependency(struct eval_ctx *ctx, const struct expr *expr,
 }
 
 /**
+ * payload_dependency_store - store a possibly redundant protocol match
+ *
+ * @ctx: payload dependency context
+ * @stmt: payload match
+ * @base: base of payload match
+ */
+void payload_dependency_store(struct payload_dep_ctx *ctx,
+			      struct stmt *stmt, enum proto_bases base)
+{
+	ctx->pbase = base + 1;
+	ctx->pdep  = stmt;
+}
+
+/**
+ * payload_dependency_kill - kill a redundant payload depedency
+ *
+ * @ctx: payload dependency context
+ * @expr: higher layer payload expression
+ *
+ * Kill a redundant payload expression if a higher layer payload expression
+ * implies its existance.
+ */
+void payload_dependency_kill(struct payload_dep_ctx *ctx, struct expr *expr)
+{
+	if (ctx->pbase != PROTO_BASE_INVALID &&
+	    ctx->pbase == expr->payload.base &&
+	    ctx->pdep != NULL) {
+		list_del(&ctx->pdep->list);
+		stmt_free(ctx->pdep);
+
+		ctx->pbase = PROTO_BASE_INVALID;
+		ctx->pdep  = NULL;
+	}
+}
+
+/**
  * payload_expr_complete - fill in type information of a raw payload expr
  *
  * @expr:	the payload expression

commit a42310894bdb739f3e0e11bf500bfb580790b49f

Author: Patrick McHardy <[email protected]>
Date:   Wed Nov 25 16:51:10 2015 +0000
    .
include/payload.h +1 -0
index 619a9c9..59027a1 100644
@@ -9,6 +9,7 @@ extern struct expr *payload_expr_alloc(const struct location *loc,
 				       unsigned int type);
 extern void payload_init_raw(struct expr *expr, enum proto_bases base,
 			     unsigned int offset, unsigned int len);
+extern unsigned int payload_hdr_field(const struct expr *expr);
 
 struct eval_ctx;
 struct stmt;
include/proto.h +5 -0
index a31ab91..6397107 100644
@@ -85,6 +85,11 @@ struct proto_desc {
 		const struct proto_desc		*desc;
 	}				protocols[PROTO_UPPER_MAX];
 	struct proto_hdr_template	templates[PROTO_HDRS_MAX];
+	struct {
+		uint8_t				order[PROTO_HDRS_MAX];
+		uint32_t			filter;
+	}				format;
+
 };
 
 #define PROTO_LINK(__num, __desc)	{ .num = (__num), .desc = (__desc), }
src/netlink.c +151 -13

187 lines not on this page · raw message

src/payload.c +7 -0
index dabd504..d00ddf6 100644
@@ -138,6 +138,11 @@ void payload_init_raw(struct expr *expr, enum proto_bases base,
 	expr->len		= len;
 }
 
+unsigned int payload_hdr_field(const struct expr *expr)
+{
+	return expr->payload.tmpl - expr->payload.desc->templates;
+}
+
 static void payload_stmt_print(const struct stmt *stmt)
 {
 	expr_print(stmt->payload.expr);
@@ -457,6 +462,7 @@ void payload_expr_expand(struct list_head *list, struct expr *expr,
 		goto raw;
 	assert(desc->base == expr->payload.base);
 
+restart:
 	for (i = 1; i < array_size(desc->templates); i++) {
 		tmpl = &desc->templates[i];
 		if (tmpl->offset != expr->payload.offset)
@@ -469,6 +475,7 @@ void payload_expr_expand(struct list_head *list, struct expr *expr,
 			expr->payload.offset += tmpl->len;
 			if (expr->len == off)
 				return;
+			goto restart;
 		} else
 			break;
 	}
src/proto.c +21 -0
index 803ee97..8e1fa02 100644
@@ -331,6 +331,9 @@ const struct proto_desc proto_icmp = {
 		[ICMPHDR_GATEWAY]	= ICMPHDR_FIELD("gateway", un.gateway),
 		[ICMPHDR_MTU]		= ICMPHDR_FIELD("mtu", un.frag.mtu),
 	},
+	.format		= {
+		.filter	= (1 << ICMPHDR_CHECKSUM),
+	},
 };
 
 /*
@@ -520,6 +523,14 @@ const struct proto_desc proto_ip = {
 		[IPHDR_SADDR]		= IPHDR_ADDR("saddr",		saddr),
 		[IPHDR_DADDR]		= IPHDR_ADDR("daddr",		daddr),
 	},
+	.format		= {
+		.order	= {
+			IPHDR_SADDR, IPHDR_DADDR, IPHDR_TOS, IPHDR_TTL,
+			IPHDR_ID, IPHDR_PROTOCOL, IPHDR_LENGTH,
+		},
+		.filter	= (1 << IPHDR_VERSION)  | (1 << IPHDR_HDRLENGTH) |
+			  (1 << IPHDR_FRAG_OFF) | (1 << IPHDR_CHECKSUM),
+	},
 };
 
 /*
@@ -708,6 +719,10 @@ const struct proto_desc proto_arp = {
 		[ARPHDR_THA]		= PROTO_HDR_TEMPLATE("tha", &etheraddr_type, BYTEORDER_BIG_ENDIAN, 18 * BITS_PER_BYTE, 6 * BITS_PER_BYTE),
 		[ARPHDR_TPA]		= PROTO_HDR_TEMPLATE("tip", &ipaddr_type, BYTEORDER_BIG_ENDIAN, 24 * BITS_PER_BYTE, 4 * BITS_PER_BYTE),
 	},
+	.format		= {
+		.filter	= (1 << ARPHDR_HRD) | (1 << ARPHDR_PRO) |
+			  (1 << ARPHDR_HLN) | (1 << ARPHDR_PLN),
+	},
 };
 
 /*
@@ -807,6 +822,12 @@ const struct proto_desc proto_eth = {
 		[ETHHDR_SADDR]		= ETHHDR_ADDR("saddr", ether_shost),
 		[ETHHDR_TYPE]		= ETHHDR_TYPE("type", ether_type),
 	},
+	.format		= {
+		.order	= {
+			ETHHDR_SADDR, ETHHDR_DADDR, ETHHDR_TYPE,
+		},
+	},
+
 };
 
 static void __init proto_init(void)
author maintainer
#55 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 7 lines · Patrick McHardy

Ok here's might current state. I've added an output filter and defined output ordering, so we can surpress some fields and order the remaining ones the way we want. I've also added redundant payload dependency elimination.

Example output looks like this:

Quoted 7 lines

trace id 85060d00 arp packet: iif ens3 ether saddr 63:f6:4b:00:54:52 ether daddr c9:4b:a9:00:54:52 arp operation reply arp sha 63:f6:4b:00:54:52 arp sip 192.168.122.1 arp tha c9:4b:a9:00:54:52 arp tip 192.168.122.84

trace id 853ff400 ip packet: iif ens3 ether saddr 63:f6:4b:00:54:52 ether daddr c9:4b:a9:00:54:52 ip saddr 192.168.122.1 ip daddr 192.168.122.84 ip tos 16 ip ttl 64 ip id 38325 ip length 60 tcp sport 46156 tcp dport 10000

trace id 853ffc00 ip packet: oif ens3 ip saddr 192.168.122.84 ip daddr 192.168.122.1 ip tos 16 ip ttl 64 ip id 51655 ip length 40 tcp sport 10000 tcp dport 46156

If people are happy this way I'll start getting it into final shape.

Please do, looks great!

But note that I'm still busy with v2 of the libnftnl and kernel parts, there will be minor changes there.

1. VLAN_TAG attr will be removed, I'll insert vlan header after ethernet one in the LL_HEADER payload.
2. IIF, OIF etc attributes will be removed. Instead, I plan to reuse meta keys for this in a nested TRACE_META attribute.

Not sure yet how the libnftnl part will look like, I'd prefer to reuse meta decoding parts that we have in libnftnl already
(my thinking was that if we'd want e.g. secmark later we could
do so more easily if we'd just reuse meta key values).

3. PACKET message type has been removed. Kernel will insert the HEADER payloads in the first message sent from each do_chain invocation.

I think this will not interfere with your patch too much.

#56 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 10 lines · Florian Westphal
Quoted 9 lines · Patrick McHardy

Ok here's might current state. I've added an output filter and defined output ordering, so we can surpress some fields and order the remaining ones the way we want. I've also added redundant payload dependency elimination.

Example output looks like this:

trace id 85060d00 arp packet: iif ens3 ether saddr 63:f6:4b:00:54:52 ether daddr c9:4b:a9:00:54:52 arp operation reply arp sha 63:f6:4b:00:54:52 arp sip 192.168.122.1 arp tha c9:4b:a9:00:54:52 arp tip 192.168.122.84

Quoted 7 lines
Quoted 5 lines

trace id 853ff400 ip packet: iif ens3 ether saddr 63:f6:4b:00:54:52 ether daddr c9:4b:a9:00:54:52 ip saddr 192.168.122.1 ip daddr 192.168.122.84 ip tos 16 ip ttl 64 ip id 38325 ip length 60 tcp sport 46156 tcp dport 10000

trace id 853ffc00 ip packet: oif ens3 ip saddr 192.168.122.84 ip daddr 192.168.122.1 ip tos 16 ip ttl 64 ip id 51655 ip length 40 tcp sport 10000 tcp dport 46156

If people are happy this way I'll start getting it into final shape.

Please do, looks great!

Great :)

Quoted 7 lines

But note that I'm still busy with v2 of the libnftnl and kernel parts, there will be minor changes there.

1. VLAN_TAG attr will be removed, I'll insert vlan header after ethernet one in the LL_HEADER payload.
2. IIF, OIF etc attributes will be removed. Instead, I plan to reuse meta keys for this in a nested TRACE_META attribute.

Both sound like good ideas, however regarding meta we'd have to use the nft attributes even if we're using the tracing infrastructure for different subsystems as discussed previously. I guess that would be Ok, just wanted to mention it.

Quoted 9 lines

Not sure yet how the libnftnl part will look like, I'd prefer to reuse meta decoding parts that we have in libnftnl already
(my thinking was that if we'd want e.g. secmark later we could
do so more easily if we'd just reuse meta key values).

3. PACKET message type has been removed. Kernel will insert the HEADER payloads in the first message sent from each do_chain invocation.

I think this will not interfere with your patch too much.

Probably not, should be quite easy to adapt to any interface changes.

BTW, I had a another idea regarding the kernel side. Just mentioning it in case you find it interesting:

We're currently only sending traces for matching rules. If we get NFT_BREAK we'll skip sending a trace.

It might also be of interest *why* a rule didn't match. A possibility without too much overhead would be in the NFT_BREAK case to send:

* a trace message
* the current register contents
* the current *expr

That would allow userspace to look at the first non-matching expression, determine the meaning of the register contents and show the expression and the actual contents.

So you'd get a rule, let's say an expression "iif eth0" and the register content "iif eth1".

Just an idea.

#57 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

In reply to Patrick McHardy
Quoted 6 lines · Patrick McHardy

trace id 85898000 ip ip length 60 ip id 220 ip ttl 64 ip protocol tcp ip saddr 192.168.122.1 ip daddr 192.168.122.84 tcp sport 39558 tcp dport 10000 iif ens3 [...]
The second part is the we output normal nft expressions, so "ip" is repeated
for every header field. [...] the upside is that the expressions can be
easily copy and pasted into the ruleset if necessary.

"ip ip" looks a bit of out of place. To illustrate,

	ip && ip.length==60 && ip.id==220 && ...

is what the rule seems to mean. However, it is also conceivable that someone (who does not have the full knowledge as the .y parser does) will read it as

	ip.ip.length==60 && ip.id==220 ...

and might wonder if that is some magic sauce to test the inside header of an IPIP packet. Altogether, this causes the nft syntax to remain more confusing than the classic iptables one where the &&-segmentation points are reasonably known.

Quoted 3 lines

trace id 85898200 arp filter input verdict 1 mark 0x00000001 iif ens3

Comments welcome, especially regarding the current limitations we have.

I do not think that the far-right printing of IP addresses in the trace output is a big deal. Even the iptables TRACE mechanism ends up showing the IP address past column 80:

[239310.214731] TRACE: mangle:INPUT:policy:1 IN=wl0 OUT=

MAC=7c:7a:91:30:84:cd:18:83:bf:2d:7e:20:08:00 SRC=46.4.122.207 DST=192.168.217.18 LEN=88 TOS=0x00 PREC=0x00 TTL=56 ID=42841 DF PROTO=TCP SPT=22 DPT=34572 SEQ=1941015595 ACK=2361189253 WINDOW=1382 RES=0x00 ACK PSH URGP=0 OPT (0101080A0B6162F1038FDA1F) UID=25121 GID=100

(add syslog timestamps in front for added effect).
And perhaps, to a particular reader, the L4 information - which is even more right-shifted in both ipt and nft trace - may be more important than the L3 part.

What I think is a doable way is to output the rule in evaluation order - basically, the way it was input.
If the IP TTL is not that important, perhaps that expression should have been moved "to the back" when the rule was generated by the userspace tool before being entered into the kernel.

#58 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 17 lines · Jan Engelhardt
Quoted 6 lines · Patrick McHardy

trace id 85898000 ip ip length 60 ip id 220 ip ttl 64 ip protocol tcp ip saddr 192.168.122.1 ip daddr 192.168.122.84 tcp sport 39558 tcp dport 10000 iif ens3 [...]
The second part is the we output normal nft expressions, so "ip" is repeated
for every header field. [...] the upside is that the expressions can be
easily copy and pasted into the ruleset if necessary.

"ip ip" looks a bit of out of place. To illustrate,

	ip && ip.length==60 && ip.id==220 && ...

is what the rule seems to mean. However, it is also conceivable that someone (who does not have the full knowledge as the .y parser does) will read it as

Quoted 6 lines
	ip.ip.length==60 && ip.id==220 ...

and might wonder if that is some magic sauce to test the inside header of an IPIP packet. Altogether, this causes the nft syntax to remain more confusing than the classic iptables one where the &&-segmentation points are reasonably known.

Its WIP output. It's supposed to read "ip packet", but I agree, I will certainly not leave it like this.

Quoted 19 lines
Quoted 3 lines

trace id 85898200 arp filter input verdict 1 mark 0x00000001 iif ens3

Comments welcome, especially regarding the current limitations we have.

I do not think that the far-right printing of IP addresses in the trace output is a big deal. Even the iptables TRACE mechanism ends up showing the IP address past column 80:

[239310.214731] TRACE: mangle:INPUT:policy:1 IN=wl0 OUT=

MAC=7c:7a:91:30:84:cd:18:83:bf:2d:7e:20:08:00 SRC=46.4.122.207 DST=192.168.217.18 LEN=88 TOS=0x00 PREC=0x00 TTL=56 ID=42841 DF PROTO=TCP SPT=22 DPT=34572 SEQ=1941015595 ACK=2361189253 WINDOW=1382 RES=0x00 ACK PSH URGP=0 OPT (0101080A0B6162F1038FDA1F) UID=25121 GID=100

(add syslog timestamps in front for added effect).
And perhaps, to a particular reader, the L4 information - which is even more right-shifted in both ipt and nft trace - may be more important than the L3 part.

Quoted 5 lines

What I think is a doable way is to output the rule in evaluation order - basically, the way it was input.
If the IP TTL is not that important, perhaps that expression should have been moved "to the back" when the rule was generated by the userspace tool before being entered into the kernel.

Its a decoded packet dump, not the rule. The rule is of course output in evaluation order.

#59 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 8 lines · Patrick McHardy
Quoted 6 lines

What I think is a doable way is to output the rule in evaluation order - basically, the way it was input.
If the IP TTL is not that important, perhaps that expression should have been moved "to the back" when the rule was generated by the userspace tool before being entered into the kernel.

Its a decoded packet dump, not the rule.

Would it be possible to make the program which receives the trace over netlink to take a command-line argument or a filename that specifies which expressions to preferentially order to the left? Something like

trace --fields=ip.saddr,ip.daddr,tcp.dport,ip.ttl,*

(the ,* part being implicit if omitted)

#60 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

Quoted 11 lines · Jan Engelhardt
Quoted 8 lines · Patrick McHardy
Quoted 6 lines

What I think is a doable way is to output the rule in evaluation order - basically, the way it was input.
If the IP TTL is not that important, perhaps that expression should have been moved "to the back" when the rule was generated by the userspace tool before being entered into the kernel.

Its a decoded packet dump, not the rule.

Quoted 8 lines

Would it be possible to make the program which receives the trace over netlink to take a command-line argument or a filename that specifies which expressions to preferentially order to the left? Something like

trace --fields=ip.saddr,ip.daddr,tcp.dport,ip.ttl,*

(the ,* part being implicit if omitted)

It sounds like an interesting idea, especially if we omit fields be default.

Let's see:

I defintely don't want to add new lexing and grammar rules for all the protocols, so the syntax would have to be based on the current expressions. So it would likely not be a command line option but something which is handled by the parser:

nft monitor trace decode ip saddr, ip daddr, tcp dport, tcp sequence, *

That should be easy to integrate.

Next question would be how to deal with unspecified protocols or how to specify preferences for multiple protocols at once. In many address families and on higher layers we normally don't know what kind of packets we will receive. It might be preferrable to have an abstract way to specify the fields of interest, but I'm unable to think of something reasonable. We might be able to do some grouping, f.i. "l3proto identity" for source and destination address of any L3 protocol, but many fields are hard to group.

Alternatively we can of course always apply the default encoding and simply allow to override it for one specific packet type, like ether + ip + tcp.

Suggestions are welcome.

#61 lore raw

Re: [PATCH nf-next 3/6] netfilter: nf_tables: disable old tracing if listener is present

In reply to Florian Westphal
Quoted 14 lines · Florian Westphal

No need to clutter nflog/dmesg ring buffer with the old tracing output when the 'native' nfnetlink interface is used.

---
 net/netfilter/nf_tables_core.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_core.c +0 -0
index dabf5ed..69bdd9a 100644
@@ -69,7 +70,13 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
 		if (!pkt->skb->nf_trace)

net/netfilter/nf_tables_core.c static inline void nft_trace_packet(const struct nft_pktinfo *pkt, · line 79

Quoted 10 lines
net/netfilter/nf_tables_core.c +7 -1
 			return;
 		nf_tables_trace_notify(pkt, chain, rule, verdict, type);
-		__nft_trace_packet(pkt, chain, rulenum, type);
+		if (prefer_native_trace)
+			return;
+
+		if (nfnetlink_has_listeners(pkt->net, NFNLGRP_NFTABLES))
+			prefer_native_trace = true;
+		else
+			__nft_trace_packet(pkt, chain, rulenum, type);

Not sure how the final solution is going to look, but this inline function was meant to *only* get rid of the repeated nf_trace checks. Anything else should be done out of line IMO since its inlined into the most performance critical function of nftables.

author maintainer
#62 lore raw

PATCH 4/6 src: rename EXPORT_SYMBOL to EXPORT_SYMBOL_ALIAS

In reply to Florian Westphal

Future symbols don't need backwards-compat aliases.

---
 include/utils.h |  4 +++-
 src/batch.c     | 14 +++++------
 src/chain.c     | 68 ++++++++++++++++++++++++++--------------------------
 src/common.c    | 14 +++++------
 src/expr.c      | 32 ++++++++++++-------------
 src/gen.c       | 26 ++++++++++----------
 src/rule.c      | 74 ++++++++++++++++++++++++++++-----------------------------
 src/ruleset.c   | 32 ++++++++++++-------------
 src/set.c       | 64 ++++++++++++++++++++++++-------------------------
 src/set_elem.c  | 48 ++++++++++++++++++-------------------
 src/table.c     | 60 +++++++++++++++++++++++-----------------------
 11 files changed, 219 insertions(+), 217 deletions(-)
include/utils.h +3 -1
index f7436fa..0087dbb 100644
@@ -10,9 +10,11 @@
 #include "config.h"
 #ifdef HAVE_VISIBILITY_HIDDEN
 #	define __visible	__attribute__((visibility("default")))
-#	define EXPORT_SYMBOL(x, y)	typeof(x) (x) __visible; __typeof (y) y __attribute ((alias (#x), visibility ("default")))
+#	define EXPORT_SYMBOL(x)	typeof(x) (x) __visible;
+#	define EXPORT_SYMBOL_ALIAS(x, y)	typeof(x) (x) __visible; __typeof (y) y __attribute ((alias (#x), visibility ("default")))
 #else
 #	define EXPORT_SYMBOL
+#	define EXPORT_SYMBOL_ALIAS
 #endif
 
 #define __noreturn	__attribute__((__noreturn__))
src/batch.c +7 -7
index a001288..7299ff6 100644
@@ -80,7 +80,7 @@ err1:
 	free(batch);
 	return NULL;
 }
-EXPORT_SYMBOL(nftnl_batch_alloc, nft_batch_alloc);
+EXPORT_SYMBOL_ALIAS(nftnl_batch_alloc, nft_batch_alloc);
 
 void nftnl_batch_free(struct nftnl_batch *batch)
 {
@@ -94,7 +94,7 @@ void nftnl_batch_free(struct nftnl_batch *batch)
 
 	free(batch);
 }
-EXPORT_SYMBOL(nftnl_batch_free, nft_batch_free);
+EXPORT_SYMBOL_ALIAS(nftnl_batch_free, nft_batch_free);
 
 int nftnl_batch_update(struct nftnl_batch *batch)
 {
@@ -119,19 +119,19 @@ int nftnl_batch_update(struct nftnl_batch *batch)
 err1:
 	return -1;
 }
-EXPORT_SYMBOL(nftnl_batch_update, nft_batch_update);
+EXPORT_SYMBOL_ALIAS(nftnl_batch_update, nft_batch_update);
 
 void *nftnl_batch_buffer(struct nftnl_batch *batch)
 {
 	return mnl_nlmsg_batch_current(batch->current_page->batch);
 }
-EXPORT_SYMBOL(nftnl_batch_buffer, nft_batch_buffer);
+EXPORT_SYMBOL_ALIAS(nftnl_batch_buffer, nft_batch_buffer);
 
 uint32_t nftnl_batch_buffer_len(struct nftnl_batch *batch)
 {
 	return mnl_nlmsg_batch_size(batch->current_page->batch);
 }
-EXPORT_SYMBOL(nftnl_batch_buffer_len, nft_batch_buffer_len);
+EXPORT_SYMBOL_ALIAS(nftnl_batch_buffer_len, nft_batch_buffer_len);
 
 int nftnl_batch_iovec_len(struct nftnl_batch *batch)
 {
@@ -143,7 +143,7 @@ int nftnl_batch_iovec_len(struct nftnl_batch *batch)
 
 	return num_pages;
 }
-EXPORT_SYMBOL(nftnl_batch_iovec_len, nft_batch_iovec_len);
+EXPORT_SYMBOL_ALIAS(nftnl_batch_iovec_len, nft_batch_iovec_len);
 
 void nftnl_batch_iovec(struct nftnl_batch *batch, struct iovec *iov,
 		       uint32_t iovlen)
@@ -160,4 +160,4 @@ void nftnl_batch_iovec(struct nftnl_batch *batch, struct iovec *iov,
 		i++;
 	}
 }
-EXPORT_SYMBOL(nftnl_batch_iovec, nft_batch_iovec);
+EXPORT_SYMBOL_ALIAS(nftnl_batch_iovec, nft_batch_iovec);
src/chain.c +34 -34

256 lines not on this page · raw message

src/common.c +7 -7
index 43d2cfd..5dda52f 100644
@@ -40,7 +40,7 @@ struct nlmsghdr *nftnl_nlmsg_build_hdr(char *buf, uint16_t cmd, uint16_t family,
 
 	return nlh;
 }
-EXPORT_SYMBOL(nftnl_nlmsg_build_hdr, nft_nlmsg_build_hdr);
+EXPORT_SYMBOL_ALIAS(nftnl_nlmsg_build_hdr, nft_nlmsg_build_hdr);
 
 struct nftnl_parse_err *nftnl_parse_err_alloc(void)
 {
@@ -54,13 +54,13 @@ struct nftnl_parse_err *nftnl_parse_err_alloc(void)
 
 	return err;
 }
-EXPORT_SYMBOL(nftnl_parse_err_alloc, nft_parse_err_alloc);
+EXPORT_SYMBOL_ALIAS(nftnl_parse_err_alloc, nft_parse_err_alloc);
 
 void nftnl_parse_err_free(struct nftnl_parse_err *err)
 {
 	xfree(err);
 }
-EXPORT_SYMBOL(nftnl_parse_err_free, nft_parse_err_free);
+EXPORT_SYMBOL_ALIAS(nftnl_parse_err_free, nft_parse_err_free);
 
 int nftnl_parse_perror(const char *msg, struct nftnl_parse_err *err)
 {
@@ -80,7 +80,7 @@ int nftnl_parse_perror(const char *msg, struct nftnl_parse_err *err)
 		return fprintf(stderr, "%s: Undefined error\n", msg);
 	}
 }
-EXPORT_SYMBOL(nftnl_parse_perror, nft_parse_perror);
+EXPORT_SYMBOL_ALIAS(nftnl_parse_perror, nft_parse_perror);
 
 int nftnl_cmd_header_snprintf(char *buf, size_t size, uint32_t cmd, uint32_t type,
 			    uint32_t flags)
@@ -176,13 +176,13 @@ void nftnl_batch_begin(char *buf, uint32_t seq)
 {
 	nftnl_batch_build_hdr(buf, NFNL_MSG_BATCH_BEGIN, seq);
 }
-EXPORT_SYMBOL(nftnl_batch_begin, nft_batch_begin);
+EXPORT_SYMBOL_ALIAS(nftnl_batch_begin, nft_batch_begin);
 
 void nftnl_batch_end(char *buf, uint32_t seq)
 {
 	nftnl_batch_build_hdr(buf, NFNL_MSG_BATCH_END, seq);
 }
-EXPORT_SYMBOL(nftnl_batch_end, nft_batch_end);
+EXPORT_SYMBOL_ALIAS(nftnl_batch_end, nft_batch_end);
 
 int nftnl_batch_is_supported(void)
 {
@@ -241,4 +241,4 @@ err:
 	mnl_nlmsg_batch_stop(b);
 	return -1;
 }
-EXPORT_SYMBOL(nftnl_batch_is_supported, nft_batch_is_supported);
+EXPORT_SYMBOL_ALIAS(nftnl_batch_is_supported, nft_batch_is_supported);
src/expr.c +16 -16
index 3249a5c..16d40d6 100644
@@ -43,7 +43,7 @@ struct nftnl_expr *nftnl_expr_alloc(const char *name)
 
 	return expr;
 }
-EXPORT_SYMBOL(nftnl_expr_alloc, nft_rule_expr_alloc);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_alloc, nft_rule_expr_alloc);
 
 void nftnl_expr_free(struct nftnl_expr *expr)
 {
@@ -52,13 +52,13 @@ void nftnl_expr_free(struct nftnl_expr *expr)
 
 	xfree(expr);
 }
-EXPORT_SYMBOL(nftnl_expr_free, nft_rule_expr_free);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_free, nft_rule_expr_free);
 
 bool nftnl_expr_is_set(const struct nftnl_expr *expr, uint16_t type)
 {
 	return expr->flags & (1 << type);
 }
-EXPORT_SYMBOL(nftnl_expr_is_set, nft_rule_expr_is_set);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_is_set, nft_rule_expr_is_set);
 
 void
 nftnl_expr_set(struct nftnl_expr *expr, uint16_t type,
@@ -73,42 +73,42 @@ nftnl_expr_set(struct nftnl_expr *expr, uint16_t type,
 	}
 	expr->flags |= (1 << type);
 }
-EXPORT_SYMBOL(nftnl_expr_set, nft_rule_expr_set);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_set, nft_rule_expr_set);
 
 void
 nftnl_expr_set_u8(struct nftnl_expr *expr, uint16_t type, uint8_t data)
 {
 	nftnl_expr_set(expr, type, &data, sizeof(uint8_t));
 }
-EXPORT_SYMBOL(nftnl_expr_set_u8, nft_rule_expr_set_u8);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_set_u8, nft_rule_expr_set_u8);
 
 void
 nftnl_expr_set_u16(struct nftnl_expr *expr, uint16_t type, uint16_t data)
 {
 	nftnl_expr_set(expr, type, &data, sizeof(uint16_t));
 }
-EXPORT_SYMBOL(nftnl_expr_set_u16, nft_rule_expr_set_u16);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_set_u16, nft_rule_expr_set_u16);
 
 void
 nftnl_expr_set_u32(struct nftnl_expr *expr, uint16_t type, uint32_t data)
 {
 	nftnl_expr_set(expr, type, &data, sizeof(uint32_t));
 }
-EXPORT_SYMBOL(nftnl_expr_set_u32, nft_rule_expr_set_u32);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_set_u32, nft_rule_expr_set_u32);
 
 void
 nftnl_expr_set_u64(struct nftnl_expr *expr, uint16_t type, uint64_t data)
 {
 	nftnl_expr_set(expr, type, &data, sizeof(uint64_t));
 }
-EXPORT_SYMBOL(nftnl_expr_set_u64, nft_rule_expr_set_u64);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_set_u64, nft_rule_expr_set_u64);
 
 void
 nftnl_expr_set_str(struct nftnl_expr *expr, uint16_t type, const char *str)
 {
 	nftnl_expr_set(expr, type, str, strlen(str)+1);
 }
-EXPORT_SYMBOL(nftnl_expr_set_str, nft_rule_expr_set_str);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_set_str, nft_rule_expr_set_str);
 
 const void *nftnl_expr_get(const struct nftnl_expr *expr,
 			      uint16_t type, uint32_t *data_len)
@@ -129,7 +129,7 @@ const void *nftnl_expr_get(const struct nftnl_expr *expr,
 
 	return ret;
 }
-EXPORT_SYMBOL(nftnl_expr_get, nft_rule_expr_get);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_get, nft_rule_expr_get);
 
 uint8_t nftnl_expr_get_u8(const struct nftnl_expr *expr, uint16_t type)
 {
@@ -145,7 +145,7 @@ uint8_t nftnl_expr_get_u8(const struct nftnl_expr *expr, uint16_t type)
 
 	return *((uint8_t *)data);
 }
-EXPORT_SYMBOL(nftnl_expr_get_u8, nft_rule_expr_get_u8);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_get_u8, nft_rule_expr_get_u8);
 
 uint16_t nftnl_expr_get_u16(const struct nftnl_expr *expr, uint16_t type)
 {
@@ -161,7 +161,7 @@ uint16_t nftnl_expr_get_u16(const struct nftnl_expr *expr, uint16_t type)
 
 	return *((uint16_t *)data);
 }
-EXPORT_SYMBOL(nftnl_expr_get_u16, nft_rule_expr_get_u16);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_get_u16, nft_rule_expr_get_u16);
 
 uint32_t nftnl_expr_get_u32(const struct nftnl_expr *expr, uint16_t type)
 {
@@ -177,7 +177,7 @@ uint32_t nftnl_expr_get_u32(const struct nftnl_expr *expr, uint16_t type)
 
 	return *((uint32_t *)data);
 }
-EXPORT_SYMBOL(nftnl_expr_get_u32, nft_rule_expr_get_u32);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_get_u32, nft_rule_expr_get_u32);
 
 uint64_t nftnl_expr_get_u64(const struct nftnl_expr *expr, uint16_t type)
 {
@@ -193,7 +193,7 @@ uint64_t nftnl_expr_get_u64(const struct nftnl_expr *expr, uint16_t type)
 
 	return *((uint64_t *)data);
 }
-EXPORT_SYMBOL(nftnl_expr_get_u64, nft_rule_expr_get_u64);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_get_u64, nft_rule_expr_get_u64);
 
 const char *nftnl_expr_get_str(const struct nftnl_expr *expr, uint16_t type)
 {
@@ -201,7 +201,7 @@ const char *nftnl_expr_get_str(const struct nftnl_expr *expr, uint16_t type)
 
 	return (const char *)nftnl_expr_get(expr, type, &data_len);
 }
-EXPORT_SYMBOL(nftnl_expr_get_str, nft_rule_expr_get_str);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_get_str, nft_rule_expr_get_str);
 
 void
 nftnl_expr_build_payload(struct nlmsghdr *nlh, struct nftnl_expr *expr)
@@ -273,4 +273,4 @@ int nftnl_expr_snprintf(char *buf, size_t size, struct nftnl_expr *expr,
 
 	return offset;
 }
-EXPORT_SYMBOL(nftnl_expr_snprintf, nft_rule_expr_snprintf);
+EXPORT_SYMBOL_ALIAS(nftnl_expr_snprintf, nft_rule_expr_snprintf);
src/gen.c +13 -13
index eb3b70d..f864333 100644
@@ -33,19 +33,19 @@ struct nftnl_gen *nftnl_gen_alloc(void)
 {
 	return calloc(1, sizeof(struct nftnl_gen));
 }
-EXPORT_SYMBOL(nftnl_gen_alloc, nft_gen_alloc);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_alloc, nft_gen_alloc);
 
 void nftnl_gen_free(struct nftnl_gen *gen)
 {
 	xfree(gen);
 }
-EXPORT_SYMBOL(nftnl_gen_free, nft_gen_free);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_free, nft_gen_free);
 
 bool nftnl_gen_is_set(const struct nftnl_gen *gen, uint16_t attr)
 {
 	return gen->flags & (1 << attr);
 }
-EXPORT_SYMBOL(nftnl_gen_is_set, nft_gen_attr_is_set);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_is_set, nft_gen_attr_is_set);
 
 void nftnl_gen_unset(struct nftnl_gen *gen, uint16_t attr)
 {
@@ -58,7 +58,7 @@ void nftnl_gen_unset(struct nftnl_gen *gen, uint16_t attr)
 	}
 	gen->flags &= ~(1 << attr);
 }
-EXPORT_SYMBOL(nftnl_gen_unset, nft_gen_attr_unset);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_unset, nft_gen_attr_unset);
 
 static uint32_t nftnl_gen_validate[NFTNL_GEN_MAX + 1] = {
 	[NFTNL_GEN_ID]	= sizeof(uint32_t),
@@ -79,19 +79,19 @@ void nftnl_gen_set_data(struct nftnl_gen *gen, uint16_t attr,
 	}
 	gen->flags |= (1 << attr);
 }
-EXPORT_SYMBOL(nftnl_gen_set_data, nft_gen_attr_set_data);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_set_data, nft_gen_attr_set_data);
 
 void nftnl_gen_set(struct nftnl_gen *gen, uint16_t attr, const void *data)
 {
 	nftnl_gen_set_data(gen, attr, data, nftnl_gen_validate[attr]);
 }
-EXPORT_SYMBOL(nftnl_gen_set, nft_gen_attr_set);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_set, nft_gen_attr_set);
 
 void nftnl_gen_set_u32(struct nftnl_gen *gen, uint16_t attr, uint32_t val)
 {
 	nftnl_gen_set_data(gen, attr, &val, sizeof(uint32_t));
 }
-EXPORT_SYMBOL(nftnl_gen_set_u32, nft_gen_attr_set_u32);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_set_u32, nft_gen_attr_set_u32);
 
 const void *nftnl_gen_get_data(struct nftnl_gen *gen, uint16_t attr,
 				  uint32_t *data_len)
@@ -105,21 +105,21 @@ const void *nftnl_gen_get_data(struct nftnl_gen *gen, uint16_t attr,
 	}
 	return NULL;
 }
-EXPORT_SYMBOL(nftnl_gen_get_data, nft_gen_attr_get_data);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_get_data, nft_gen_attr_get_data);
 
 const void *nftnl_gen_get(struct nftnl_gen *gen, uint16_t attr)
 {
 	uint32_t data_len;
 	return nftnl_gen_get_data(gen, attr, &data_len);
 }
-EXPORT_SYMBOL(nftnl_gen_get, nft_gen_attr_get);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_get, nft_gen_attr_get);
 
 uint32_t nftnl_gen_get_u32(struct nftnl_gen *gen, uint16_t attr)
 {
 	const void *ret = nftnl_gen_get(gen, attr);
 	return ret == NULL ? 0 : *((uint32_t *)ret);
 }
-EXPORT_SYMBOL(nftnl_gen_get_u32, nft_gen_attr_get_u32);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_get_u32, nft_gen_attr_get_u32);
 
 static int nftnl_gen_parse_attr_cb(const struct nlattr *attr, void *data)
 {
@@ -154,7 +154,7 @@ int nftnl_gen_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_gen *gen)
 	}
 	return 0;
 }
-EXPORT_SYMBOL(nftnl_gen_nlmsg_parse, nft_gen_nlmsg_parse);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_nlmsg_parse, nft_gen_nlmsg_parse);
 
 static int nftnl_gen_snprintf_default(char *buf, size_t size, struct nftnl_gen *gen)
 {
@@ -190,7 +190,7 @@ int nftnl_gen_snprintf(char *buf, size_t size, struct nftnl_gen *gen, uint32_t t
 	return nftnl_gen_cmd_snprintf(buf, size, gen, nftnl_flag2cmd(flags), type,
 				    flags);
 }
-EXPORT_SYMBOL(nftnl_gen_snprintf, nft_gen_snprintf);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_snprintf, nft_gen_snprintf);
 
 static inline int nftnl_gen_do_snprintf(char *buf, size_t size, void *gen,
 				      uint32_t cmd, uint32_t type,
@@ -205,4 +205,4 @@ int nftnl_gen_fprintf(FILE *fp, struct nftnl_gen *gen, uint32_t type,
 	return nftnl_fprintf(fp, gen, NFTNL_CMD_UNSPEC, type, flags,
 			   nftnl_gen_do_snprintf);
 }
-EXPORT_SYMBOL(nftnl_gen_fprintf, nft_gen_fprintf);
+EXPORT_SYMBOL_ALIAS(nftnl_gen_fprintf, nft_gen_fprintf);
src/rule.c +37 -37

279 lines not on this page · raw message

src/ruleset.c +16 -16
index 7b1ab7a..641ff99 100644
@@ -62,7 +62,7 @@ struct nftnl_ruleset *nftnl_ruleset_alloc(void)
 {
 	return calloc(1, sizeof(struct nftnl_ruleset));
 }
-EXPORT_SYMBOL(nftnl_ruleset_alloc, nft_ruleset_alloc);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_alloc, nft_ruleset_alloc);
 
 void nftnl_ruleset_free(struct nftnl_ruleset *r)
 {
@@ -76,13 +76,13 @@ void nftnl_ruleset_free(struct nftnl_ruleset *r)
 		nftnl_rule_list_free(r->rule_list);
 	xfree(r);
 }
-EXPORT_SYMBOL(nftnl_ruleset_free, nft_ruleset_free);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_free, nft_ruleset_free);
 
 bool nftnl_ruleset_is_set(const struct nftnl_ruleset *r, uint16_t attr)
 {
 	return r->flags & (1 << attr);
 }
-EXPORT_SYMBOL(nftnl_ruleset_is_set, nft_ruleset_attr_is_set);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_is_set, nft_ruleset_attr_is_set);
 
 void nftnl_ruleset_unset(struct nftnl_ruleset *r, uint16_t attr)
 {
@@ -109,7 +109,7 @@ void nftnl_ruleset_unset(struct nftnl_ruleset *r, uint16_t attr)
 	}
 	r->flags &= ~(1 << attr);
 }
-EXPORT_SYMBOL(nftnl_ruleset_unset, nft_ruleset_attr_unset);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_unset, nft_ruleset_attr_unset);
 
 void nftnl_ruleset_set(struct nftnl_ruleset *r, uint16_t attr, void *data)
 {
@@ -135,7 +135,7 @@ void nftnl_ruleset_set(struct nftnl_ruleset *r, uint16_t attr, void *data)
 	}
 	r->flags |= (1 << attr);
 }
-EXPORT_SYMBOL(nftnl_ruleset_set, nft_ruleset_attr_set);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_set, nft_ruleset_attr_set);
 
 void *nftnl_ruleset_get(const struct nftnl_ruleset *r, uint16_t attr)
 {
@@ -155,7 +155,7 @@ void *nftnl_ruleset_get(const struct nftnl_ruleset *r, uint16_t attr)
 		return NULL;
 	}
 }
-EXPORT_SYMBOL(nftnl_ruleset_get, nft_ruleset_attr_get);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_get, nft_ruleset_attr_get);
 
 void nftnl_ruleset_ctx_free(const struct nftnl_parse_ctx *ctx)
 {
@@ -178,13 +178,13 @@ void nftnl_ruleset_ctx_free(const struct nftnl_parse_ctx *ctx)
 		break;
 	}
 }
-EXPORT_SYMBOL(nftnl_ruleset_ctx_free, nft_ruleset_ctx_free);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_ctx_free, nft_ruleset_ctx_free);
 
 bool nftnl_ruleset_ctx_is_set(const struct nftnl_parse_ctx *ctx, uint16_t attr)
 {
 	return ctx->flags & (1 << attr);
 }
-EXPORT_SYMBOL(nftnl_ruleset_ctx_is_set, nft_ruleset_ctx_is_set);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_ctx_is_set, nft_ruleset_ctx_is_set);
 
 void *nftnl_ruleset_ctx_get(const struct nftnl_parse_ctx *ctx, uint16_t attr)
 {
@@ -210,14 +210,14 @@ void *nftnl_ruleset_ctx_get(const struct nftnl_parse_ctx *ctx, uint16_t attr)
 		return NULL;
 	}
 }
-EXPORT_SYMBOL(nftnl_ruleset_ctx_get, nft_ruleset_ctx_get);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_ctx_get, nft_ruleset_ctx_get);
 
 uint32_t nftnl_ruleset_ctx_get_u32(const struct nftnl_parse_ctx *ctx, uint16_t attr)
 {
 	const void *ret = nftnl_ruleset_ctx_get(ctx, attr);
 	return ret == NULL ? 0 : *((uint32_t *)ret);
 }
-EXPORT_SYMBOL(nftnl_ruleset_ctx_get_u32, nft_ruleset_ctx_get_u32);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_ctx_get_u32, nft_ruleset_ctx_get_u32);
 
 #if defined(JSON_PARSING) || defined(XML_PARSING)
 static void nftnl_ruleset_ctx_set(struct nftnl_parse_ctx *ctx, uint16_t attr,
@@ -751,7 +751,7 @@ int nftnl_ruleset_parse_file_cb(enum nftnl_parse_type type, FILE *fp,
 {
 	return nftnl_ruleset_do_parse(type, fp, err, NFTNL_PARSE_FILE, data, cb);
 }
-EXPORT_SYMBOL(nftnl_ruleset_parse_file_cb, nft_ruleset_parse_file_cb);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_parse_file_cb, nft_ruleset_parse_file_cb);
 
 int nftnl_ruleset_parse_buffer_cb(enum nftnl_parse_type type, const char *buffer,
 				struct nftnl_parse_err *err, void *data,
@@ -760,7 +760,7 @@ int nftnl_ruleset_parse_buffer_cb(enum nftnl_parse_type type, const char *buffer
 	return nftnl_ruleset_do_parse(type, buffer, err, NFTNL_PARSE_BUFFER, data,
 				    cb);
 }
-EXPORT_SYMBOL(nftnl_ruleset_parse_buffer_cb, nft_ruleset_parse_buffer_cb);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_parse_buffer_cb, nft_ruleset_parse_buffer_cb);
 
 static int nftnl_ruleset_cb(const struct nftnl_parse_ctx *ctx)
 {
@@ -828,14 +828,14 @@ int nftnl_ruleset_parse(struct nftnl_ruleset *r, enum nftnl_parse_type type,
 {
 	return nftnl_ruleset_parse_buffer_cb(type, data, err, r, nftnl_ruleset_cb);
 }
-EXPORT_SYMBOL(nftnl_ruleset_parse, nft_ruleset_parse);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_parse, nft_ruleset_parse);
 
 int nftnl_ruleset_parse_file(struct nftnl_ruleset *rs, enum nftnl_parse_type type,
 			   FILE *fp, struct nftnl_parse_err *err)
 {
 	return nftnl_ruleset_parse_file_cb(type, fp, err, rs, nftnl_ruleset_cb);
 }
-EXPORT_SYMBOL(nftnl_ruleset_parse_file, nft_ruleset_parse_file);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_parse_file, nft_ruleset_parse_file);
 
 static const char *nftnl_ruleset_o_opentag(uint32_t type)
 {
@@ -1097,7 +1097,7 @@ int nftnl_ruleset_snprintf(char *buf, size_t size, const struct nftnl_ruleset *r
 		return -1;
 	}
 }
-EXPORT_SYMBOL(nftnl_ruleset_snprintf, nft_ruleset_snprintf);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_snprintf, nft_ruleset_snprintf);
 
 static int nftnl_ruleset_fprintf_tables(FILE *fp, const struct nftnl_ruleset *rs,
 				      uint32_t type, uint32_t flags)
@@ -1317,4 +1317,4 @@ int nftnl_ruleset_fprintf(FILE *fp, const struct nftnl_ruleset *rs, uint32_t typ
 	return nftnl_ruleset_cmd_fprintf(fp, rs, nftnl_flag2cmd(flags), type,
 				       flags);
 }
-EXPORT_SYMBOL(nftnl_ruleset_fprintf, nft_ruleset_fprintf);
+EXPORT_SYMBOL_ALIAS(nftnl_ruleset_fprintf, nft_ruleset_fprintf);
src/set.c +32 -32

243 lines not on this page · raw message

src/set_elem.c +24 -24

183 lines not on this page · raw message

src/table.c +30 -30

226 lines not on this page · raw message

Signature · 1 line
2.4.10
#63 lore raw

Re: [PATCH libnftnl 4/6] src: rename EXPORT_SYMBOL to EXPORT_SYMBOL_ALIAS

Quoted 1 line · Florian Westphal

Future symbols don't need backwards-compat aliases.

Florian, good point.

I have just pushed out this to make sure ongoing work uses the right macro and avoid rebases.

Thanks.

author maintainer
#64 lore raw

PATCH 5/6 src: add trace infrastructure support

In reply to Florian Westphal

parses trace monitor netlink messages from the kernel and supports printing most of the 'important' header data such as ether/ip/ip6 src/dst addresses and udp/tcp port numbers.

---
 include/libnftnl/Makefile.am        |   1 +
 include/libnftnl/trace.h            |  56 +++
 include/linux/netfilter/nf_tables.h |  32 ++
 src/Makefile.am                     |   1 +
 src/libnftnl.map                    |  17 +
 src/trace.c                         | 725 ++++++++++++++++++++++++++++++++++++
 src/utils.c                         |   4 +
 7 files changed, 836 insertions(+)
 create mode 100644 include/libnftnl/trace.h
 create mode 100644 src/trace.c
include/libnftnl/Makefile.am +1 -0
index a20aaee..84f01b6 100644
@@ -1,5 +1,6 @@
 pkginclude_HEADERS = batch.h		\
 		     table.h		\
+		     trace.h		\
 		     chain.h		\
 		     rule.h		\
 		     expr.h		\
include/libnftnl/trace.h new file +56 -0

56 lines not on this page · raw message

include/linux/netfilter/nf_tables.h +32 -0
index 9796d82..09ede2b 100644
@@ -83,6 +83,7 @@ enum nft_verdicts {
  * @NFT_MSG_DELSETELEM: delete a set element (enum nft_set_elem_attributes)
  * @NFT_MSG_NEWGEN: announce a new generation, only for events (enum nft_gen_attributes)
  * @NFT_MSG_GETGEN: get the rule-set generation (enum nft_gen_attributes)
+ * @NFT_MSG_TRACE: trace event (enum nft_trace_attributes)
  */
 enum nf_tables_msg_types {
 	NFT_MSG_NEWTABLE,
@@ -102,6 +103,7 @@ enum nf_tables_msg_types {
 	NFT_MSG_DELSETELEM,
 	NFT_MSG_NEWGEN,
 	NFT_MSG_GETGEN,
+	NFT_MSG_TRACE,
 	NFT_MSG_MAX,
 };
 
@@ -970,4 +972,34 @@ enum nft_gen_attributes {
 };
 #define NFTA_GEN_MAX		(__NFTA_GEN_MAX - 1)
 
+enum nft_trace_attibutes {
+	NFTA_TRACE_UNSPEC,
+	NFTA_TRACE_CHAIN,
+	NFTA_TRACE_DEV_TYPE,
+	NFTA_TRACE_ID,
+	NFTA_TRACE_IIF,
+	NFTA_TRACE_OIF,
+	NFTA_TRACE_LL_HEADER,
+	NFTA_TRACE_MARK,
+	NFTA_TRACE_NETWORK_HEADER,
+	NFTA_TRACE_TABLE,
+	NFTA_TRACE_TRANSPORT_HEADER,
+	NFTA_TRACE_TRANSPORT_PROTO,
+	NFTA_TRACE_TYPE,
+	NFTA_TRACE_RULE_HANDLE,
+	NFTA_TRACE_VERDICT,
+	NFTA_TRACE_VLAN_TAG,
+	__NFTA_TRACE_MAX
+};
+#define NFTA_TRACE_MAX (__NFTA_TRACE_MAX - 1)
+
+enum nft_trace_types {
+	NFT_TRACETYPE_UNSPEC,
+	NFT_TRACETYPE_PACKET,
+	NFT_TRACETYPE_POLICY,
+	NFT_TRACETYPE_RETURN,
+	NFT_TRACETYPE_RULE,
+	__NFT_TRACETYPE_MAX
+};
+#define NFT_TRACETYPE_MAX (__NFT_TRACETYPE_MAX - 1)
 #endif /* _LINUX_NF_TABLES_H */
src/Makefile.am +1 -0
index 107cae5..795307d 100644
@@ -11,6 +11,7 @@ libnftnl_la_SOURCES = utils.c		\
 		      common.c		\
 		      gen.c		\
 		      table.c		\
+		      trace.c		\
 		      chain.c		\
 		      rule.c		\
 		      set.c		\
src/libnftnl.map +17 -0
index a52b54e..7fc2eb9 100644
@@ -498,3 +498,20 @@ global:
 
 local: *;
 };
+
+LIBNFTNL_4.1 {
+	nftnl_trace_alloc;
+	nftnl_trace_free;
+
+	nftnl_trace_is_set;
+
+	nftnl_trace_get_u8;
+	nftnl_trace_get_u16;
+	nftnl_trace_get_u32;
+	nftnl_trace_get_u64;
+	nftnl_trace_get_str;
+
+	nftnl_trace_nlmsg_parse;
+	nftnl_trace_snprintf;
+	nftnl_trace_fprintf;
+} LIBNFTNL_4;
src/trace.c new file +725 -0

725 lines not on this page · raw message

src/utils.c +4 -0
index 84fbe94..fb68b1a 100644
@@ -152,6 +152,10 @@ const char *nftnl_verdict2str(uint32_t verdict)
 		return "jump";
 	case NFT_GOTO:
 		return "goto";
+	case NFT_CONTINUE:
+		return "continue";
+	case NFT_BREAK:
+		return "break";
 	default:
 		return "unknown";
 	}
Signature · 1 line
2.4.10
#65 lore raw

Re: [PATCH libnftnl 5/6] src: add trace infrastructure support

Quoted 25 lines · Florian Westphal
src/trace.c new file +25 -0
+static int
+print_th(const struct nftnl_trace *t, char *buf, size_t size)
+{
+	uint8_t proto = nftnl_trace_get_u8(t, NFTNL_TRACE_TRANSPORT_PROTO);
+	int ret, len = size, offset = 0;
+	const struct udphdr *uh;
+	uint32_t plen;
+
+	ret = snprintf(buf+offset, len, " protocol %u", proto);
+
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+	switch (proto) {
+	case IPPROTO_DCCP:
+	case IPPROTO_SCTP:
+	case IPPROTO_TCP:
+	case IPPROTO_UDP:
+	case IPPROTO_UDPLITE:
+		break;
+	default:
+		return 0;
+	}
+
+	/* warning: only sport/dport are valid */
+	uh = nftnl_trace_get_data(t, NFTNL_TRACE_TRANSPORT_HEADER, &plen);

src/trace.c

Quoted 10 lines
src/trace.c new file +10 -0
+	if (!uh)
+		return 0;
+
+	ret = snprintf(buf+offset, len, " sport %"PRIu16 " dport %"PRIu16,
+			ntohs(uh->uh_sport), ntohs(uh->uh_dport));
+
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	return offset;
+}
+ ...

Regarding this header decoding, a further upside of using nft would be that we can already fully decode all known headers based on our protocol descriptions in a generic fashion.

Basically we just splice off data from the beginning and feed it to payload expression decoding. That should result in a full decode of every protocol we support. Every improvement we will make there will automatically apply to decoding, like (I talked to Pablo about this for a couple of times) supporing encapsulated headers, like GRE/IPIP/... + inner headers.

Hope I can convince you of the upsides :)

#66 lore raw

Re: [PATCH libnftnl 5/6] src: add trace infrastructure support

Quoted 41 lines · Patrick McHardy
Quoted 25 lines · Florian Westphal
src/trace.c new file +25 -0
+static int
+print_th(const struct nftnl_trace *t, char *buf, size_t size)
+{
+	uint8_t proto = nftnl_trace_get_u8(t, NFTNL_TRACE_TRANSPORT_PROTO);
+	int ret, len = size, offset = 0;
+	const struct udphdr *uh;
+	uint32_t plen;
+
+	ret = snprintf(buf+offset, len, " protocol %u", proto);
+
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+	switch (proto) {
+	case IPPROTO_DCCP:
+	case IPPROTO_SCTP:
+	case IPPROTO_TCP:
+	case IPPROTO_UDP:
+	case IPPROTO_UDPLITE:
+		break;
+	default:
+		return 0;
+	}
+
+	/* warning: only sport/dport are valid */
+	uh = nftnl_trace_get_data(t, NFTNL_TRACE_TRANSPORT_HEADER, &plen);

src/trace.c

Quoted 10 lines
src/trace.c new file +10 -0
+	if (!uh)
+		return 0;
+
+	ret = snprintf(buf+offset, len, " sport %"PRIu16 " dport %"PRIu16,
+			ntohs(uh->uh_sport), ntohs(uh->uh_dport));
+
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	return offset;
+}
+ ...

Regarding this header decoding, a further upside of using nft would be that we can already fully decode all known headers based on our protocol descriptions in a generic fashion.

Quoted 7 lines

Basically we just splice off data from the beginning and feed it to payload expression decoding. That should result in a full decode of every protocol we support. Every improvement we will make there will automatically apply to decoding, like (I talked to Pablo about this for a couple of times) supporing encapsulated headers, like GRE/IPIP/... + inner headers.

Hope I can convince you of the upsides :)

I'm giving this a shot right now. Will let you know the result.

author maintainer
#67 lore raw

PATCH 6/6 src: add trace support to nft monitor mode

In reply to Florian Westphal

nft monitor [ trace ]

... can now display nftables nftrace debug information.

$ nft rule bridge raw prerouting add tcp dport 22 limit rate 1/second meta nftrace set 1
$ nft monitor trace
trace id 834dd100 bridge packet src 5e:95:99:72:ea:c5 dst 52:54:40:a2:3f:a6 src 192.168.7.1 dst 192.168.7.11 len 88 ttl 64 id 2719 protocol 6 sport 3628 dport 22 iif eth0
trace id 834dd100 bridge raw prerouting rule verdict continue iif eth0
trace id 834dd100 rule tcp dport ssh limit rate 1/second nftrace set 1
trace id 834dd100 bridge raw prerouting policy verdict accept iif eth0
trace id 834dd100 ip filter input rule verdict jump iif br0
trace id 834dd100 rule ip saddr . tcp dport vmap { }
trace id 834dd100 ip filter test rule verdict accept iif br0
trace id 834dd100 rule accept

based on an initial patch from Markus Kötter.

---
 include/linux/netfilter/nf_tables.h | 30 +++++++++++++
 src/evaluate.c                      | 18 ++++++++
 src/netlink.c                       | 84 +++++++++++++++++++++++++++++++++++++
 src/rule.c                          | 47 +++++++++++++++++----
 4 files changed, 170 insertions(+), 9 deletions(-)
include/linux/netfilter/nf_tables.h +30 -0
index 5ebe3d8..85d739b 100644
@@ -101,6 +101,7 @@ enum nf_tables_msg_types {
 	NFT_MSG_DELSETELEM,
 	NFT_MSG_NEWGEN,
 	NFT_MSG_GETGEN,
+	NFT_MSG_TRACE,
 	NFT_MSG_MAX,
 };
 
@@ -961,4 +962,33 @@ enum nft_gen_attributes {
 };
 #define NFTA_GEN_MAX		(__NFTA_GEN_MAX - 1)
 
+enum nft_trace_attibutes {
+	NFTA_TRACE_UNSPEC,
+	NFTA_TRACE_CHAIN,
+	NFTA_TRACE_ID,
+	NFTA_TRACE_IIF,
+	NFTA_TRACE_OIF,
+	NFTA_TRACE_LL_HEADER,
+	NFTA_TRACE_LL_TYPE,
+	NFTA_TRACE_MARK,
+	NFTA_TRACE_PAYLOAD,
+	NFTA_TRACE_TABLE,
+	NFTA_TRACE_TYPE,
+	NFTA_TRACE_RULE_HANDLE,
+	NFTA_TRACE_VERDICT,
+	NFTA_TRACE_VLAN_TAG,
+	NFTA_TRACE_DEVTYPE,
+	__NFTA_TRACE_MAX
+};
+#define NFTA_TRACE_MAX (__NFTA_TRACE_MAX - 1)
+
+enum nft_trace_types {
+	NFT_TRACETYPE_UNSPEC,
+	NFT_TRACETYPE_PACKET,
+	NFT_TRACETYPE_POLICY,
+	NFT_TRACETYPE_RETURN,
+	NFT_TRACETYPE_RULE,
+	__NFT_TRACETYPE_MAX
+};
+#define NFT_TRACETYPE_MAX (__NFT_TRACETYPE_MAX - 1)
 #endif /* _LINUX_NF_TABLES_H */
src/evaluate.c +18 -0
index 48f071f..b1ab8e9 100644
@@ -2279,6 +2279,7 @@ enum {
 	CMD_MONITOR_EVENT_ANY,
 	CMD_MONITOR_EVENT_NEW,
 	CMD_MONITOR_EVENT_DEL,
+	CMD_MONITOR_EVENT_TRACE,
 	CMD_MONITOR_EVENT_MAX
 };
 
@@ -2320,6 +2321,21 @@ static uint32_t monitor_flags[CMD_MONITOR_EVENT_MAX][CMD_MONITOR_OBJ_MAX] = {
 		[CMD_MONITOR_OBJ_SETS]		= (1 << NFT_MSG_DELSET),
 		[CMD_MONITOR_OBJ_ELEMS]		= (1 << NFT_MSG_DELSETELEM),
 	},
+	[CMD_MONITOR_EVENT_TRACE] = {
+		[CMD_MONITOR_OBJ_ANY]		= (1 << NFT_MSG_NEWTABLE) |
+						  (1 << NFT_MSG_NEWCHAIN) |
+						  (1 << NFT_MSG_NEWRULE)  |
+						  (1 << NFT_MSG_DELTABLE) |
+						  (1 << NFT_MSG_DELCHAIN) |
+						  (1 << NFT_MSG_DELRULE)  |
+						  (1 << NFT_MSG_TRACE),
+		[CMD_MONITOR_OBJ_TABLES]	= (1 << NFT_MSG_NEWTABLE) |
+						  (1 << NFT_MSG_DELTABLE),
+		[CMD_MONITOR_OBJ_CHAINS]	= (1 << NFT_MSG_NEWCHAIN) |
+						  (1 << NFT_MSG_DELCHAIN),
+		[CMD_MONITOR_OBJ_RULES]		= (1 << NFT_MSG_NEWRULE) |
+						  (1 << NFT_MSG_DELRULE),
+	},
 };
 
 static int cmd_evaluate_monitor(struct eval_ctx *ctx, struct cmd *cmd)
@@ -2332,6 +2348,8 @@ static int cmd_evaluate_monitor(struct eval_ctx *ctx, struct cmd *cmd)
 		event = CMD_MONITOR_EVENT_NEW;
 	else if (strcmp(cmd->monitor->event, "destroy") == 0)
 		event = CMD_MONITOR_EVENT_DEL;
+	else if (strcmp(cmd->monitor->event, "trace") == 0)
+		event = CMD_MONITOR_EVENT_TRACE;
 	else {
 		return monitor_error(ctx, cmd->monitor, "invalid event %s",
 				     cmd->monitor->event);
src/netlink.c +84 -0

108 lines not on this page · raw message

src/rule.c +38 -9

65 lines not on this page · raw message

Signature · 6 lines
2.4.10

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#68 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

Quoted 4 lines · Florian Westphal

nft monitor [ trace ]

... can now display nftables nftrace debug information.

Quoted 10 lines
$ nft rule bridge raw prerouting add tcp dport 22 limit rate 1/second meta nftrace set 1
$ nft monitor trace
trace id 834dd100 bridge packet src 5e:95:99:72:ea:c5 dst 52:54:40:a2:3f:a6 src 192.168.7.1 dst 192.168.7.11 len 88 ttl 64 id 2719 protocol 6 sport 3628 dport 22 iif eth0
trace id 834dd100 bridge raw prerouting rule verdict continue iif eth0
trace id 834dd100 rule tcp dport ssh limit rate 1/second nftrace set 1
trace id 834dd100 bridge raw prerouting policy verdict accept iif eth0
trace id 834dd100 ip filter input rule verdict jump iif br0
trace id 834dd100 rule ip saddr . tcp dport vmap { }
trace id 834dd100 ip filter test rule verdict accept iif br0
trace id 834dd100 rule accept

I like this *a lot*. No need for external tools and a much more readable output than using any of the other logging mechanisms. Nice work!

Quoted 4 lines
src/netlink.c +4 -0
+static void trace_print_if(const struct nftnl_trace *nlt, uint16_t attr, const char *str)
+{
+	char __name[IFNAMSIZ];
+	const char *ifname;

src/netlink.c static void netlink_events_cache_update(struct netlink_mon_handler *monh, · line 2181

Quoted 10 lines
src/netlink.c +10 -0
+
+        if (!nftnl_trace_is_set(nlt, attr))
+		return;
+
+	ifname = nft_if_indextoname(nftnl_trace_get_u32(nlt, attr), __name);
+	if (ifname)
+		printf(" %s %s", str, ifname);
+	else
+		printf(" %s %d", str, nftnl_trace_get_u32(nlt, attr));
+}

A lot of the other trace attributes are not used so far. I'm wondering if you intend to add special print functions for them as well.

An alternative would be to use our internal datatypes, IOW parse the attributes, associate the values with an internal type and use the regular printing functions. The benefit would be fully consistent output, also with respect to output options like numerical output.

Quoted 13 lines
src/rule.c +11 -0
+static int do_command_monitor(struct netlink_ctx *ctx, struct cmd *cmd)
+{
+	struct table *t;
+	struct set *s;
+	struct netlink_mon_handler monhandler;
+
+	monhandler.cache_needed = need_cache(cmd);
 	if (monhandler.cache_needed) {
+		struct rule *rule, *nrule;
+		struct chain *chain;
+		int ret;
+
 		list_for_each_entry(t, &table_list, list) {

src/rule.c static int do_command_rename(struct netlink_ctx *ctx, struct cmd *cmd) · line 1234

Quoted 10 lines
src/rule.c +8 -0
 			list_for_each_entry(s, &t->sets, list)
 				s->init = set_expr_alloc(&cmd->location);
+
+			if (!(cmd->monitor->flags & (1 << NFT_MSG_TRACE)))
+				continue;
+
+			/* When tracing we'd like to translate the rule handle
+			 * we receive in the trace messages to the actual rule
+			 * struct to print that out.  Populate rule cache now.
+			 */

Tracing might be a long running operation. The cache can go out of sync, might be better to do a lookup on demand.

Right now the caching infrastrucure has quite a lot of problems and I'd prefer to get them fixed before we base new things on it.

author maintainer
#69 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

Quoted 9 lines · Patrick McHardy
Quoted 9 lines
src/netlink.c +9 -0
+static void trace_print_if(const struct nftnl_trace *nlt, uint16_t attr, const char *str)
+{
+	char __name[IFNAMSIZ];
+	const char *ifname;
+
+        if (!nftnl_trace_is_set(nlt, attr))
+		return;
+
+	ifname = nft_if_indextoname(nftnl_trace_get_u32(nlt, attr), __name);
Quoted 8 lines

src/netlink.c static void netlink_events_cache_update(struct netlink_mon_handler *monh, · line 2181

Quoted 5 lines
src/netlink.c +5 -0
+	if (ifname)
+		printf(" %s %s", str, ifname);
+	else
+		printf(" %s %d", str, nftnl_trace_get_u32(nlt, attr));
+}

A lot of the other trace attributes are not used so far. I'm wondering if you intend to add special print functions for them as well.

All of the trace attributes are used, most of the use is limited to libnftnl though.

Wrt. to iif/oif, I do the printing for it in nftables to take advantage of the iif/oif idx<->name cache.

I would have preferred to just stick it into libnftnl like most of the other rest BUT then you either have the additional name translation overhead or just the index number...

Quoted 4 lines

An alternative would be to use our internal datatypes, IOW parse the attributes, associate the values with an internal type and use the regular printing functions. The benefit would be fully consistent output, also with respect to output options like numerical output.

Yes, right now virtually all of the printing is in libnftnl (called from nftables via nftnl_trace_fprintf() ).

Quoted 18 lines
Quoted 18 lines
src/rule.c +14 -0
+static int do_command_monitor(struct netlink_ctx *ctx, struct cmd *cmd)
+{
+	struct table *t;
+	struct set *s;
+	struct netlink_mon_handler monhandler;
+
+	monhandler.cache_needed = need_cache(cmd);
 	if (monhandler.cache_needed) {
+		struct rule *rule, *nrule;
+		struct chain *chain;
+		int ret;
+
 		list_for_each_entry(t, &table_list, list) {
 			list_for_each_entry(s, &t->sets, list)
 				s->init = set_expr_alloc(&cmd->location);
+
+			if (!(cmd->monitor->flags & (1 << NFT_MSG_TRACE)))
+				continue;
Quoted 8 lines

src/rule.c static int do_command_rename(struct netlink_ctx *ctx, struct cmd *cmd) · line 1234

Quoted 5 lines
src/rule.c +5 -0
+
+			/* When tracing we'd like to translate the rule handle
+			 * we receive in the trace messages to the actual rule
+			 * struct to print that out.  Populate rule cache now.
+			 */

Tracing might be a long running operation. The cache can go out of sync, might be better to do a lookup on demand.

Hmm, okay.

#70 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

Quoted 22 lines · Florian Westphal
Quoted 9 lines · Patrick McHardy
Quoted 9 lines
src/netlink.c +9 -0
+static void trace_print_if(const struct nftnl_trace *nlt, uint16_t attr, const char *str)
+{
+	char __name[IFNAMSIZ];
+	const char *ifname;
+
+        if (!nftnl_trace_is_set(nlt, attr))
+		return;
+
+	ifname = nft_if_indextoname(nftnl_trace_get_u32(nlt, attr), __name);
Quoted 8 lines

src/netlink.c static void netlink_events_cache_update(struct netlink_mon_handler *monh, · line 2181

Quoted 5 lines
src/netlink.c +5 -0
+	if (ifname)
+		printf(" %s %s", str, ifname);
+	else
+		printf(" %s %d", str, nftnl_trace_get_u32(nlt, attr));
+}

A lot of the other trace attributes are not used so far. I'm wondering if you intend to add special print functions for them as well.

All of the trace attributes are used, most of the use is limited to libnftnl though.

Quoted 6 lines

Wrt. to iif/oif, I do the printing for it in nftables to take advantage of the iif/oif idx<->name cache.

I would have preferred to just stick it into libnftnl like most of the other rest BUT then you either have the additional name translation overhead or just the index number...

I see. In that case this seems fine, no need to make it more complicated just for this single attribute.

Quoted 7 lines
Quoted 4 lines

An alternative would be to use our internal datatypes, IOW parse the attributes, associate the values with an internal type and use the regular printing functions. The benefit would be fully consistent output, also with respect to output options like numerical output.

Yes, right now virtually all of the printing is in libnftnl (called from nftables via nftnl_trace_fprintf() ).

The downside is that we're stuck to fprintf. I'm working on moving nft to printing to buffers for pretty printing, intending set contents, sorting flow tables in different dimensions etc. If we have external printing we'll be missing some flexibilty in handling it, also regarding logging to other outputs than stdout.

Quoted 11 lines
Quoted 10 lines

src/rule.c static int do_command_rename(struct netlink_ctx *ctx, struct cmd *cmd) · line 1234

Quoted 7 lines
src/rule.c +7 -0
+			if (!(cmd->monitor->flags & (1 << NFT_MSG_TRACE)))
+				continue;
+
+			/* When tracing we'd like to translate the rule handle
+			 * we receive in the trace messages to the actual rule
+			 * struct to print that out.  Populate rule cache now.
+			 */

Tracing might be a long running operation. The cache can go out of sync, might be better to do a lookup on demand.

Quoted 1 line

Hmm, okay.

We can certainly also update it when we receive notifications. Not sure what we do about those currently.

#71 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

Quoted 6 lines · Patrick McHardy
Quoted 5 lines · Florian Westphal
Quoted 4 lines

An alternative would be to use our internal datatypes, IOW parse the attributes, associate the values with an internal type and use the regular printing functions. The benefit would be fully consistent output, also with respect to output options like numerical output.

Quoted 8 lines
Quoted 2 lines

Yes, right now virtually all of the printing is in libnftnl (called from nftables via nftnl_trace_fprintf() ).

The downside is that we're stuck to fprintf. I'm working on moving nft to printing to buffers for pretty printing, intending set contents, sorting flow tables in different dimensions etc. If we have external printing we'll be missing some flexibilty in handling it, also regarding logging to other outputs than stdout.

What we did do far is to have the fprintf variant for quick printing, but this should be based on the printing to buffers. Both approaches would be exported.

Does this sound good to you?

#72 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

Quoted 16 lines · Pablo Neira Ayuso
Quoted 6 lines · Patrick McHardy
Quoted 5 lines · Florian Westphal
Quoted 4 lines

An alternative would be to use our internal datatypes, IOW parse the attributes, associate the values with an internal type and use the regular printing functions. The benefit would be fully consistent output, also with respect to output options like numerical output.

Quoted 8 lines
Quoted 2 lines

Yes, right now virtually all of the printing is in libnftnl (called from nftables via nftnl_trace_fprintf() ).

The downside is that we're stuck to fprintf. I'm working on moving nft to printing to buffers for pretty printing, intending set contents, sorting flow tables in different dimensions etc. If we have external printing we'll be missing some flexibilty in handling it, also regarding logging to other outputs than stdout.

Quoted 5 lines

What we did do far is to have the fprintf variant for quick printing, but this should be based on the printing to buffers. Both approaches would be exported.

Does this sound good to you?

I'm completely fine with the fprintf printing *for libnfntnl*, but I'm questing whether nft should make use of it for its own output asides from debugging netlink operations. I also wouldn't consider the libnftnl output stable in any form, the fprintf variant it basically something for debugging, at least I've always seen it that way.

So yeah, I'm not opposed to having it, just to using it for this particular purpose.

#73 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

Quoted 21 lines · Patrick McHardy
Quoted 20 lines · Pablo Neira Ayuso
Quoted 6 lines · Patrick McHardy
Quoted 5 lines · Florian Westphal
Quoted 4 lines

An alternative would be to use our internal datatypes, IOW parse the attributes, associate the values with an internal type and use the regular printing functions. The benefit would be fully consistent output, also with respect to output options like numerical output.

Quoted 8 lines
Quoted 2 lines

Yes, right now virtually all of the printing is in libnftnl (called from nftables via nftnl_trace_fprintf() ).

The downside is that we're stuck to fprintf. I'm working on moving nft to printing to buffers for pretty printing, intending set contents, sorting flow tables in different dimensions etc. If we have external printing we'll be missing some flexibilty in handling it, also regarding logging to other outputs than stdout.

What we did do far is to have the fprintf variant for quick printing, but this should be based on the printing to buffers. Both approaches would be exported.

Quoted 7 lines
Quoted 1 line

Does this sound good to you?

I'm completely fine with the fprintf printing *for libnfntnl*, but I'm questing whether nft should make use of it for its own output asides from debugging netlink operations. I also wouldn't consider the libnftnl output stable in any form, the fprintf variant it basically something for debugging, at least I've always seen it that way.

Oh I see, you were refering to nft, that makes sense to me.

Quoted 2 lines

So yeah, I'm not opposed to having it, just to using it for this particular purpose.

Thanks for your feedback, sorry if I'm insisting much, I just want to make sure we get all the same picture in our heads :)

author maintainer
#74 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

In reply to Patrick McHardy
Quoted 8 lines · Patrick McHardy
Quoted 2 lines

Yes, right now virtually all of the printing is in libnftnl (called from nftables via nftnl_trace_fprintf() ).

The downside is that we're stuck to fprintf. I'm working on moving nft to printing to buffers for pretty printing, intending set contents, sorting flow tables in different dimensions etc. If we have external printing we'll be missing some flexibilty in handling it, also regarding logging to other outputs than stdout.

The _fprint variant is a convenience wrapper for nftnl_trace_snprintf. Would that work for you?

#75 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

Quoted 4 lines · Florian Westphal
Quoted 3 lines · Patrick McHardy
Quoted 2 lines

Yes, right now virtually all of the printing is in libnftnl (called from nftables via nftnl_trace_fprintf() ).

Quoted 8 lines
Quoted 5 lines

The downside is that we're stuck to fprintf. I'm working on moving nft to printing to buffers for pretty printing, intending set contents, sorting flow tables in different dimensions etc. If we have external printing we'll be missing some flexibilty in handling it, also regarding logging to other outputs than stdout.

The _fprint variant is a convenience wrapper for nftnl_trace_snprintf. Would that work for you?

Probably, but with downsides. The nft internal buffer printing will encapsulate every printed element so we can reorder them based on an abstract meaning, f.i. for pretty printing, where we want to keep related things together and reindent other parts.

We can also do filters, sorting on individual parts both numerically and lexically, structured output etc etc, but all that requires to have the invidual parts of the output available seperately.

So yeah it would work, and at least the sorting part doesn't make sense for tracing anyways, but it would be a special case with some limitations.

#76 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

In reply to Patrick McHardy
Quoted 2 lines · Patrick McHardy

Tracing might be a long running operation. The cache can go out of sync, might be better to do a lookup on demand.

We'll need to handle generations in that approach. The kernel lookup per trace will be expensive.

Why not just keep the cache in userspace and update it only when needed? We can easily detect when we get out of sync via ENOBUFS.

Quoted 2 lines

Right now the caching infrastrucure has quite a lot of problems and I'd prefer to get them fixed before we base new things on it.

The caching infrastructure only needs to have a mode to be populated via set information, then infer existing tables from handles as you indicated.

What other problems you see with it?

#77 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

Quoted 10 lines · Pablo Neira Ayuso
Quoted 2 lines · Patrick McHardy

Tracing might be a long running operation. The cache can go out of sync, might be better to do a lookup on demand.

We'll need to handle generations in that approach. The kernel lookup per trace will be expensive.

Why not just keep the cache in userspace and update it only when needed? We can easily detect when we get out of sync via ENOBUFS.

Quoted 8 lines
Quoted 2 lines

Right now the caching infrastrucure has quite a lot of problems and I'd prefer to get them fixed before we base new things on it.

The caching infrastructure only needs to have a mode to be populated via set information, then infer existing tables from handles as you indicated.

What other problems you see with it?

Well I keep running into problems with it. We already discussed a few, we're dumping way to much information that we don't need and we're making nft require root even for unpriviledged operations and just testing ruleset syntax.

We're basing errors on a cache that might not be up to date.

When I list the bridge table, for some reason it lists *all* tables of all families. We're basically doing full dumps of everything in many cases. This will be absolutely killing performance with a big ruleset.

AFAIK (did not test) we're only listing sets for the family of the first command, then set cache_initializer to true and skip further updates. When the ruleset refers to multiple families, the contents will not be present but expected.

It basically seems like the big hammer approach + some bugs instead of selectively getting what we need when we need it and making sure its up to date, at least before generating errors based on it.

#78 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

Quoted 25 lines · Patrick McHardy
Quoted 10 lines · Pablo Neira Ayuso
Quoted 2 lines · Patrick McHardy

Tracing might be a long running operation. The cache can go out of sync, might be better to do a lookup on demand.

We'll need to handle generations in that approach. The kernel lookup per trace will be expensive.

Why not just keep the cache in userspace and update it only when needed? We can easily detect when we get out of sync via ENOBUFS.

Quoted 8 lines
Quoted 2 lines

Right now the caching infrastrucure has quite a lot of problems and I'd prefer to get them fixed before we base new things on it.

The caching infrastructure only needs to have a mode to be populated via set information, then infer existing tables from handles as you indicated.

What other problems you see with it?

Well I keep running into problems with it. We already discussed a few, we're dumping way to much information that we don't need and we're making nft require root even for unpriviledged operations and just testing ruleset syntax.

Quoted 5 lines

We're basing errors on a cache that might not be up to date.

When I list the bridge table, for some reason it lists *all* tables of all families. We're basically doing full dumps of everything in many cases. This will be absolutely killing performance with a big ruleset.

OK, I'll be refining this to allow selective dumps.

Quoted 4 lines

AFAIK (did not test) we're only listing sets for the family of the first command, then set cache_initializer to true and skip further updates. When the ruleset refers to multiple families, the contents will not be present but expected.

This may be related to the kernel patches I have to send to use generations from the dump path.

Quoted 3 lines

It basically seems like the big hammer approach + some bugs instead of selectively getting what we need when we need it and making sure its up to date, at least before generating errors based on it.

Selective dumping is good to have indeed, I'm willing to refine this.

But regarding event tracing, I think it's good to keep a cache in userspace that we update based on the events that we receive, then resync if we hit ENOBUFS, instead of inquiring the kernel for every trace.

So I think Florian can get this in. I'll be resolving the existing caching issues after him as this is not related to his work.

Fine with this approach?

#79 lore raw

Re: [PATCH nftables 6/6] src: add trace support to nft monitor mode

Quoted 15 lines · Pablo Neira Ayuso
Quoted 11 lines · Patrick McHardy

On 24.11, Pablo Neira Ayuso wrote:
Well I keep running into problems with it. We already discussed a few, we're dumping way to much information that we don't need and we're making nft require root even for unpriviledged operations and just testing ruleset syntax.

We're basing errors on a cache that might not be up to date.

When I list the bridge table, for some reason it lists *all* tables of all families. We're basically doing full dumps of everything in many cases. This will be absolutely killing performance with a big ruleset.

OK, I'll be refining this to allow selective dumps.

Quoted 7 lines
Quoted 4 lines

AFAIK (did not test) we're only listing sets for the family of the first command, then set cache_initializer to true and skip further updates. When the ruleset refers to multiple families, the contents will not be present but expected.

This may be related to the kernel patches I have to send to use generations from the dump path.

As mentioned, I did not actually test this so far, but its the caching code, it initializes for the first rule and only for that rule and bases its queries on that handle. OTOH the dumping of all tables indicates that it *does* dump more than that. Not sure, would have to test to be sure, but the code looks like it.

Quoted 10 lines
Quoted 3 lines

It basically seems like the big hammer approach + some bugs instead of selectively getting what we need when we need it and making sure its up to date, at least before generating errors based on it.

Selective dumping is good to have indeed, I'm willing to refine this.

But regarding event tracing, I think it's good to keep a cache in userspace that we update based on the events that we receive, then resync if we hit ENOBUFS, instead of inquiring the kernel for every trace.

Generally I agree.

Quoted 4 lines

So I think Florian can get this in. I'll be resolving the existing caching issues after him as this is not related to his work.

Fine with this approach?

Sure, it we fix this, no question :)