Skip to contentSkip to conversation

net: flow_dissector: trigger BPF hook when called from eth_get_headlen

PATCH Superseded Stanislav Fomichev 3 participants 14 messages Apr 15, 2019 – Apr 19, 2019

Subsystems (14)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover net: flow_dissector: trigger BPF hook when called from eth_get_headlen

Currently, when eth_get_headlen calls flow dissector, it doesn't pass any skb. Because we use passed skb to lookup associated networking namespace to find whether we have a BPF program attached or not, we always use C-based flow dissector in this case.

The goal of this patch series is to add new networking namespace argument to the eth_get_headlen and make BPF flow dissector programs be able to work in the skb-less case.

The series goes like this:
* use new kernel context (struct bpf_flow_dissector) for flow dissector programs; this makes it easy to distinguish between skb and no-skb case and supports calling BPF flow dissector on a chunk of raw data
* convert BPF_PROG_TEST_RUN to use raw data
* plumb network namespace into __skb_flow_dissect from all callers
* handle no-skb case in __skb_flow_dissect
* update eth_get_headlen to include net namespace argument and convert all existing users
* add selftest to make sure bpf_skb_load_bytes is not allowed in the no-skb mode

v5:
* API changes have been submitted via bpf/stable tree

v4:
* prohibit access to vlan fields as well (otherwise, inconsistent between skb/skb-less cases)
* drop extra unneeded check for skb->vlan_present in bpf_flow.c

v3:
* new kernel xdp_buff-like context per Alexei suggestion
* drop skb_net helper
* properly clamp flow_keys->nhoff

v2:
* moved temporary skb from stack into percpu (avoids memset of ~200 bytes per packet)
* tightened down access to __sk_buff fields from flow dissector programs to avoid touching shinfo (whitelist only relevant fields)
* addressed suggestions from Willem

Stanislav Fomichev (6):
flow_dissector: switch kernel context to struct bpf_flow_dissector
bpf: when doing BPF_PROG_TEST_RUN for flow dissector use no-skb mode
net: plumb network namespace into __skb_flow_dissect
flow_dissector: handle no-skb use case
net: pass net argument to the eth_get_headlen selftests/bpf: add flow dissector bpf_skb_load_bytes helper test

 .../net/ethernet/aquantia/atlantic/aq_ring.c  |   3 +-
 drivers/net/ethernet/broadcom/bnxt/bnxt.c     |   2 +-
 drivers/net/ethernet/hisilicon/hns/hns_enet.c |   3 +-
 .../net/ethernet/hisilicon/hns3/hns3_enet.c   |   3 +-
 drivers/net/ethernet/intel/fm10k/fm10k_main.c |   2 +-
 drivers/net/ethernet/intel/i40e/i40e_txrx.c   |   3 +-
 drivers/net/ethernet/intel/iavf/iavf_txrx.c   |   3 +-
 drivers/net/ethernet/intel/ice/ice_txrx.c     |   3 +-
 drivers/net/ethernet/intel/igb/igb_main.c     |   3 +-
 drivers/net/ethernet/intel/igc/igc_main.c     |   3 +-
 drivers/net/ethernet/intel/ixgbe/ixgbe_main.c |   2 +-
 .../net/ethernet/intel/ixgbevf/ixgbevf_main.c |   3 +-
 .../net/ethernet/mellanox/mlx5/core/en_tx.c   |   3 +-
 drivers/net/tun.c                             |   3 +-
 include/linux/etherdevice.h                   |   2 +-
 include/linux/skbuff.h                        |  28 ++---
 include/net/flow_dissector.h                  |   7 ++
 include/net/sch_generic.h                     |  11 +-
 net/bpf/test_run.c                            |  51 +++------
 net/core/filter.c                             | 105 ++++++++++++++----
 net/core/flow_dissector.c                     |  90 +++++++--------
 net/ethernet/eth.c                            |   8 +-
 .../prog_tests/flow_dissector_load_bytes.c    |  48 ++++++++
 23 files changed, 249 insertions(+), 140 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/flow_dissector_load_bytes.c
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
#1 lore raw

PATCH v5 1/6 flow_dissector: switch kernel context to struct bpf_flow_dissector

struct bpf_flow_dissector has a small subset of sk_buff fields that flow dissector BPF program is allowed to access and an optional pointer to real skb. Real skb is used only in bpf_skb_load_bytes helper to read non-linear data.

The real motivation for this is to be able to call flow dissector from eth_get_headlen context where we don't have an skb and need to dissect raw bytes.

---
 include/linux/skbuff.h       |   4 ++
 include/net/flow_dissector.h |   7 +++
 include/net/sch_generic.h    |  11 ++--
 net/bpf/test_run.c           |   4 --
 net/core/filter.c            | 105 +++++++++++++++++++++++++++--------
 net/core/flow_dissector.c    |  45 +++++++--------
 6 files changed, 117 insertions(+), 59 deletions(-)
include/linux/skbuff.h +4 -0
index a06275a618f0..4d2365ffa444 100644
@@ -1273,6 +1273,10 @@ static inline int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
 }
 #endif
 
+struct bpf_flow_dissector;
+bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
+		      __be16 proto, int nhoff, int hlen);
+
 struct bpf_flow_keys;
 bool __skb_flow_bpf_dissect(struct bpf_prog *prog,
 			    const struct sk_buff *skb,
include/net/flow_dissector.h +7 -0
index 2b26979efb48..7c5a8d9a8d2a 100644
@@ -305,4 +305,11 @@ static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissec
 	return ((char *)target_container) + flow_dissector->offset[key_id];
 }
 
+struct bpf_flow_dissector {
+	struct bpf_flow_keys	*flow_keys;
+	const struct sk_buff	*skb;
+	void			*data;
+	void			*data_end;
+};
+
 #endif
include/net/sch_generic.h +4 -7
index e8f85cd2afce..21f434f3ac9e 100644
@@ -364,13 +364,10 @@ struct tcf_proto {
 };
 
 struct qdisc_skb_cb {
-	union {
-		struct {
-			unsigned int		pkt_len;
-			u16			slave_dev_queue_mapping;
-			u16			tc_classid;
-		};
-		struct bpf_flow_keys *flow_keys;
+	struct {
+		unsigned int		pkt_len;
+		u16			slave_dev_queue_mapping;
+		u16			tc_classid;
 	};
 #define QDISC_CB_PRIV_LEN 20
 	unsigned char		data[QDISC_CB_PRIV_LEN];
net/bpf/test_run.c +0 -4
index 2221573dacdb..006ad865f7fb 100644
@@ -382,7 +382,6 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
 	u32 repeat = kattr->test.repeat;
 	struct bpf_flow_keys flow_keys;
 	u64 time_start, time_spent = 0;
-	struct bpf_skb_data_end *cb;
 	u32 retval, duration;
 	struct sk_buff *skb;
 	struct sock *sk;
@@ -423,9 +422,6 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
 				       current->nsproxy->net_ns->loopback_dev);
 	skb_reset_network_header(skb);
 
-	cb = (struct bpf_skb_data_end *)skb->cb;
-	cb->qdisc_cb.flow_keys = &flow_keys;
-
 	if (!repeat)
 		repeat = 1;
 
net/core/filter.c +82 -23

185 lines not on this page · raw message

net/core/flow_dissector.c +20 -25

59 lines not on this page · raw message

Signature · 1 line
2.21.0.392.gf8f6787159e-goog
#2 lore raw

PATCH v5 2/6 bpf: when doing BPF_PROG_TEST_RUN for flow dissector use no-skb mode

In reply to Stanislav Fomichev

Now that we have bpf_flow_dissect which can work on raw data, use it when doing BPF_PROG_TEST_RUN for flow dissector.

Simplifies bpf_prog_test_run_flow_dissector and allows us to test no-skb mode.

Note, that previously, with bpf_flow_dissect_skb we used to call eth_type_trans which pulled L2 (ETH_HLEN) header and we explicitly called skb_reset_network_header. That means flow_keys->nhoff would be initialized to 0 (skb_network_offset) in init_flow_keys.
Now we call bpf_flow_dissect with nhoff set to ETH_HLEN and need to undo it once the dissection is done to preserve the existing behavior.

---
 net/bpf/test_run.c | 47 +++++++++++++++++-----------------------------
 1 file changed, 17 insertions(+), 30 deletions(-)
net/bpf/test_run.c +17 -30
index 006ad865f7fb..db2ec88ab129 100644
@@ -379,12 +379,12 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
 				     union bpf_attr __user *uattr)
 {
 	u32 size = kattr->test.data_size_in;
+	struct bpf_flow_dissector ctx = {};
 	u32 repeat = kattr->test.repeat;
 	struct bpf_flow_keys flow_keys;
 	u64 time_start, time_spent = 0;
+	const struct ethhdr *eth;
 	u32 retval, duration;
-	struct sk_buff *skb;
-	struct sock *sk;
 	void *data;
 	int ret;
 	u32 i;
@@ -395,43 +395,31 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
 	if (kattr->test.ctx_in || kattr->test.ctx_out)
 		return -EINVAL;
 
-	data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
-			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
+	if (size < ETH_HLEN)
+		return -EINVAL;
+
+	data = bpf_test_init(kattr, size, 0, 0);
 	if (IS_ERR(data))
 		return PTR_ERR(data);
 
-	sk = kzalloc(sizeof(*sk), GFP_USER);
-	if (!sk) {
-		kfree(data);
-		return -ENOMEM;
-	}
-	sock_net_set(sk, current->nsproxy->net_ns);
-	sock_init_data(NULL, sk);
-
-	skb = build_skb(data, 0);
-	if (!skb) {
-		kfree(data);
-		kfree(sk);
-		return -ENOMEM;
-	}
-	skb->sk = sk;
-
-	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
-	__skb_put(skb, size);
-	skb->protocol = eth_type_trans(skb,
-				       current->nsproxy->net_ns->loopback_dev);
-	skb_reset_network_header(skb);
+	eth = (struct ethhdr *)data;
 
 	if (!repeat)
 		repeat = 1;
 
+	ctx.flow_keys = &flow_keys;
+	ctx.data = data;
+	ctx.data_end = (__u8 *)data + size;
+
 	rcu_read_lock();
 	preempt_disable();
 	time_start = ktime_get_ns();
 	for (i = 0; i < repeat; i++) {
-		retval = __skb_flow_bpf_dissect(prog, skb,
-						&flow_keys_dissector,
-						&flow_keys);
+		retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
+					  size);
+
+		flow_keys.nhoff -= ETH_HLEN;
+		flow_keys.thoff -= ETH_HLEN;
 
 		if (signal_pending(current)) {
 			preempt_enable();
@@ -464,7 +452,6 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
 			      retval, duration);
 
 out:
-	kfree_skb(skb);
-	kfree(sk);
+	kfree(data);
 	return ret;
 }
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
#3 lore raw

PATCH v5 3/6 net: plumb network namespace into __skb_flow_dissect

In reply to Stanislav Fomichev

This new argument will be used in the next patches for the eth_get_headlen use case. eth_get_headlen calls flow dissector with only data (without skb) so there is currently no way to pull attached BPF flow dissector program. With this new argument, we can amend the callers to explicitly pass network namespace so we can use attached BPF program.

---
 include/linux/skbuff.h    | 19 +++++++++++--------
 net/core/flow_dissector.c | 27 +++++++++++++++++----------
 net/ethernet/eth.c        |  5 +++--
 3 files changed, 31 insertions(+), 20 deletions(-)
include/linux/skbuff.h +11 -8
index 4d2365ffa444..60cdbcde751b 100644
@@ -1282,7 +1282,8 @@ bool __skb_flow_bpf_dissect(struct bpf_prog *prog,
 			    const struct sk_buff *skb,
 			    struct flow_dissector *flow_dissector,
 			    struct bpf_flow_keys *flow_keys);
-bool __skb_flow_dissect(const struct sk_buff *skb,
+bool __skb_flow_dissect(const struct net *net,
+			const struct sk_buff *skb,
 			struct flow_dissector *flow_dissector,
 			void *target_container,
 			void *data, __be16 proto, int nhoff, int hlen,
@@ -1292,8 +1293,8 @@ static inline bool skb_flow_dissect(const struct sk_buff *skb,
 				    struct flow_dissector *flow_dissector,
 				    void *target_container, unsigned int flags)
 {
-	return __skb_flow_dissect(skb, flow_dissector, target_container,
-				  NULL, 0, 0, 0, flags);
+	return __skb_flow_dissect(NULL, skb, flow_dissector,
+				  target_container, NULL, 0, 0, 0, flags);
 }
 
 static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb,
@@ -1301,18 +1302,19 @@ static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb,
 					      unsigned int flags)
 {
 	memset(flow, 0, sizeof(*flow));
-	return __skb_flow_dissect(skb, &flow_keys_dissector, flow,
-				  NULL, 0, 0, 0, flags);
+	return __skb_flow_dissect(NULL, skb, &flow_keys_dissector,
+				  flow, NULL, 0, 0, 0, flags);
 }
 
 static inline bool
-skb_flow_dissect_flow_keys_basic(const struct sk_buff *skb,
+skb_flow_dissect_flow_keys_basic(const struct net *net,
+				 const struct sk_buff *skb,
 				 struct flow_keys_basic *flow, void *data,
 				 __be16 proto, int nhoff, int hlen,
 				 unsigned int flags)
 {
 	memset(flow, 0, sizeof(*flow));
-	return __skb_flow_dissect(skb, &flow_keys_basic_dissector, flow,
+	return __skb_flow_dissect(net, skb, &flow_keys_basic_dissector, flow,
 				  data, proto, nhoff, hlen, flags);
 }
 
@@ -2492,7 +2494,8 @@ static inline void skb_probe_transport_header(struct sk_buff *skb)
 	if (skb_transport_header_was_set(skb))
 		return;
 
-	if (skb_flow_dissect_flow_keys_basic(skb, &keys, NULL, 0, 0, 0, 0))
+	if (skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
+					     NULL, 0, 0, 0, 0))
 		skb_set_transport_header(skb, keys.control.thoff);
 }
 
net/core/flow_dissector.c +17 -10
index ef6d9443cc75..f32c7e737fc6 100644
@@ -722,6 +722,7 @@ bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
 
 /**
  * __skb_flow_dissect - extract the flow_keys struct and return it
+ * @net: associated network namespace, derived from @skb if NULL
  * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
  * @flow_dissector: list of keys to dissect
  * @target_container: target structure to put dissected values into
@@ -738,7 +739,8 @@ bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
  *
  * Caller must take care of zeroing target container memory.
  */
-bool __skb_flow_dissect(const struct sk_buff *skb,
+bool __skb_flow_dissect(const struct net *net,
+			const struct sk_buff *skb,
 			struct flow_dissector *flow_dissector,
 			void *target_container,
 			void *data, __be16 proto, int nhoff, int hlen,
@@ -797,13 +799,17 @@ bool __skb_flow_dissect(const struct sk_buff *skb,
 		struct bpf_prog *attached = NULL;
 
 		rcu_read_lock();
+		if (!net) {
+			if (skb->dev)
+				net = dev_net(skb->dev);
+			else if (skb->sk)
+				net = sock_net(skb->sk);
+			else
+				WARN_ON_ONCE(1);
+		}
 
-		if (skb->dev)
-			attached = rcu_dereference(dev_net(skb->dev)->flow_dissector_prog);
-		else if (skb->sk)
-			attached = rcu_dereference(sock_net(skb->sk)->flow_dissector_prog);
-		else
-			WARN_ON_ONCE(1);
+		if (net)
+			attached = rcu_dereference(net->flow_dissector_prog);
 
 		if (attached) {
 			ret = __skb_flow_bpf_dissect(attached, skb,
@@ -1405,8 +1411,8 @@ u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
 	__flow_hash_secret_init();
 
 	memset(&keys, 0, sizeof(keys));
-	__skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
-			   NULL, 0, 0, 0,
+	__skb_flow_dissect(NULL, skb, &flow_keys_dissector_symmetric,
+			   &keys, NULL, 0, 0, 0,
 			   FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
 
 	return __flow_hash_from_keys(&keys, hashrnd);
@@ -1507,7 +1513,8 @@ u32 skb_get_poff(const struct sk_buff *skb)
 {
 	struct flow_keys_basic keys;
 
-	if (!skb_flow_dissect_flow_keys_basic(skb, &keys, NULL, 0, 0, 0, 0))
+	if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
+					      NULL, 0, 0, 0, 0))
 		return 0;
 
 	return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
net/ethernet/eth.c +3 -2
index f7a3d7a171c7..1e439549c419 100644
@@ -136,8 +136,9 @@ u32 eth_get_headlen(void *data, unsigned int len)
 		return len;
 
 	/* parse any remaining L2/L3 headers, check for L4 */
-	if (!skb_flow_dissect_flow_keys_basic(NULL, &keys, data, eth->h_proto,
-					      sizeof(*eth), len, flags))
+	if (!skb_flow_dissect_flow_keys_basic(NULL, NULL, &keys, data,
+					      eth->h_proto, sizeof(*eth),
+					      len, flags))
 		return max_t(u32, keys.control.thoff, sizeof(*eth));
 
 	/* parse for any L4 headers */
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
#4 lore raw

PATCH v5 4/6 flow_dissector: handle no-skb use case

In reply to Stanislav Fomichev

When called without skb, gather all required data from the __skb_flow_dissect's arguments and use recently introduces no-skb mode of bpf flow dissector.

Note: WARN_ON_ONCE(!net) will now trigger for eth_get_headlen users.

---
 include/linux/skbuff.h    |  5 ----
 net/core/flow_dissector.c | 52 +++++++++++++++++++--------------------
 2 files changed, 25 insertions(+), 32 deletions(-)
include/linux/skbuff.h +0 -5
index 60cdbcde751b..0bfc16d719b5 100644
@@ -1277,11 +1277,6 @@ struct bpf_flow_dissector;
 bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
 		      __be16 proto, int nhoff, int hlen);
 
-struct bpf_flow_keys;
-bool __skb_flow_bpf_dissect(struct bpf_prog *prog,
-			    const struct sk_buff *skb,
-			    struct flow_dissector *flow_dissector,
-			    struct bpf_flow_keys *flow_keys);
 bool __skb_flow_dissect(const struct net *net,
 			const struct sk_buff *skb,
 			struct flow_dissector *flow_dissector,
net/core/flow_dissector.c +25 -27

79 lines not on this page · raw message

Signature · 1 line
2.21.0.392.gf8f6787159e-goog
#5 lore raw

PATCH v5 5/6 net: pass net argument to the eth_get_headlen

In reply to Stanislav Fomichev

Update all users eth_get_headlen to pass network namespace and pass it down to the flow dissector. This commit is a noop until administrator inserts BPF flow dissector program.

Cc Maxim Krasnyansky <[email protected]>
Cc Saeed Mahameed <[email protected]>
Cc Jeff Kirsher <[email protected]>
Cc intel-wired-lan at lists.osuosl.org
Cc Yisen Zhuang <[email protected]>
Cc Salil Mehta <[email protected]>
Cc Michael Chan <[email protected]>
Cc Igor Russkikh <[email protected]>
---
 drivers/net/ethernet/aquantia/atlantic/aq_ring.c  | 3 ++-
 drivers/net/ethernet/broadcom/bnxt/bnxt.c         | 2 +-
 drivers/net/ethernet/hisilicon/hns/hns_enet.c     | 3 ++-
 drivers/net/ethernet/hisilicon/hns3/hns3_enet.c   | 3 ++-
 drivers/net/ethernet/intel/fm10k/fm10k_main.c     | 2 +-
 drivers/net/ethernet/intel/i40e/i40e_txrx.c       | 3 ++-
 drivers/net/ethernet/intel/iavf/iavf_txrx.c       | 3 ++-
 drivers/net/ethernet/intel/ice/ice_txrx.c         | 3 ++-
 drivers/net/ethernet/intel/igb/igb_main.c         | 3 ++-
 drivers/net/ethernet/intel/igc/igc_main.c         | 3 ++-
 drivers/net/ethernet/intel/ixgbe/ixgbe_main.c     | 2 +-
 drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c | 3 ++-
 drivers/net/ethernet/mellanox/mlx5/core/en_tx.c   | 3 ++-
 drivers/net/tun.c                                 | 3 ++-
 include/linux/etherdevice.h                       | 2 +-
 net/ethernet/eth.c                                | 5 +++--
 16 files changed, 29 insertions(+), 17 deletions(-)
drivers/net/ethernet/aquantia/atlantic/aq_ring.c +2 -1
index c64e2fb5a4f1..1b3181f757b7 100644
@@ -354,7 +354,8 @@ int aq_ring_rx_clean(struct aq_ring_s *self,
 
 			hdr_len = buff->len;
 			if (hdr_len > AQ_CFG_RX_HDR_SIZE)
-				hdr_len = eth_get_headlen(aq_buf_vaddr(&buff->rxdata),
+				hdr_len = eth_get_headlen(dev_net(skb->dev),
+							  aq_buf_vaddr(&buff->rxdata),
 							  AQ_CFG_RX_HDR_SIZE);
 
 			memcpy(__skb_put(skb, hdr_len), aq_buf_vaddr(&buff->rxdata),
drivers/net/ethernet/broadcom/bnxt/bnxt.c +1 -1
index 6528a597367b..8bb5f708ccc6 100644
@@ -899,7 +899,7 @@ static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
 			     DMA_ATTR_WEAK_ORDERING);
 
 	if (unlikely(!payload))
-		payload = eth_get_headlen(data_ptr, len);
+		payload = eth_get_headlen(dev_net(bp->dev), data_ptr, len);
 
 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
 	if (!skb) {
drivers/net/ethernet/hisilicon/hns/hns_enet.c +2 -1
index 297b95c1b3c1..f1ecc78d2323 100644
@@ -598,7 +598,8 @@ static int hns_nic_poll_rx_skb(struct hns_nic_ring_data *ring_data,
 	} else {
 		ring->stats.seg_pkt_cnt++;
 
-		pull_len = eth_get_headlen(va, HNS_RX_HEAD_SIZE);
+		pull_len = eth_get_headlen(dev_net(ndev), va,
+					   HNS_RX_HEAD_SIZE);
 		memcpy(__skb_put(skb, pull_len), va,
 		       ALIGN(pull_len, sizeof(long)));
 
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +2 -1
index b53b0911ec24..423d9ce0f6f8 100644
@@ -2457,7 +2457,8 @@ static int hns3_alloc_skb(struct hns3_enet_ring *ring, int length,
 	ring->stats.seg_pkt_cnt++;
 	u64_stats_update_end(&ring->syncp);
 
-	ring->pull_len = eth_get_headlen(va, HNS3_RX_HEAD_SIZE);
+	ring->pull_len = eth_get_headlen(dev_net(netdev), va,
+					 HNS3_RX_HEAD_SIZE);
 	__skb_put(skb, ring->pull_len);
 	hns3_nic_reuse_page(skb, ring->frag_num++, ring, ring->pull_len,
 			    desc_cb);
drivers/net/ethernet/intel/fm10k/fm10k_main.c +1 -1
index 2325cee76211..e2bee187d652 100644
@@ -280,7 +280,7 @@ static bool fm10k_add_rx_frag(struct fm10k_rx_buffer *rx_buffer,
 	/* we need the header to contain the greater of either ETH_HLEN or
 	 * 60 bytes if the skb->len is less than 60 for skb_pad.
 	 */
-	pull_len = eth_get_headlen(va, FM10K_RX_HDR_LEN);
+	pull_len = eth_get_headlen(dev_net(skb->dev), va, FM10K_RX_HDR_LEN);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, pull_len), va, ALIGN(pull_len, sizeof(long)));
drivers/net/ethernet/intel/i40e/i40e_txrx.c +2 -1
index 1a95223c9f99..85c5b503e0a0 100644
@@ -2035,7 +2035,8 @@ static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > I40E_RX_HDR_SIZE)
-		headlen = eth_get_headlen(xdp->data, I40E_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), xdp->data,
+					  I40E_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), xdp->data,
drivers/net/ethernet/intel/iavf/iavf_txrx.c +2 -1
index b64187753ad6..23a62d7d0f9f 100644
@@ -1315,7 +1315,8 @@ static struct sk_buff *iavf_construct_skb(struct iavf_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IAVF_RX_HDR_SIZE)
-		headlen = eth_get_headlen(va, IAVF_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  IAVF_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/ice/ice_txrx.c +2 -1
index a6f7b7feaf3c..2692b9333055 100644
@@ -698,7 +698,8 @@ ice_construct_skb(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > ICE_RX_HDR_SIZE)
-		headlen = eth_get_headlen(va, ICE_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  ICE_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/igb/igb_main.c +2 -1
index acbb5b4f333d..2023e1800c8d 100644
@@ -8051,7 +8051,8 @@ static struct sk_buff *igb_construct_skb(struct igb_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IGB_RX_HDR_LEN)
-		headlen = eth_get_headlen(va, IGB_RX_HDR_LEN);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  IGB_RX_HDR_LEN);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/igc/igc_main.c +2 -1
index f79728381e8a..265a9d8a8421 100644
@@ -1199,7 +1199,8 @@ static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IGC_RX_HDR_LEN)
-		headlen = eth_get_headlen(va, IGC_RX_HDR_LEN);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  IGC_RX_HDR_LEN);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/ixgbe/ixgbe_main.c +1 -1
index 60cec3540dd7..5e5294567ca1 100644
@@ -1800,7 +1800,7 @@ static void ixgbe_pull_tail(struct ixgbe_ring *rx_ring,
 	 * we need the header to contain the greater of either ETH_HLEN or
 	 * 60 bytes if the skb->len is less than 60 for skb_pad.
 	 */
-	pull_len = eth_get_headlen(va, IXGBE_RX_HDR_SIZE);
+	pull_len = eth_get_headlen(dev_net(skb->dev), va, IXGBE_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c +2 -1
index 49e23afa05a2..252fe0de6b56 100644
@@ -895,7 +895,8 @@ struct sk_buff *ixgbevf_construct_skb(struct ixgbevf_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IXGBEVF_RX_HDR_SIZE)
-		headlen = eth_get_headlen(xdp->data, IXGBEVF_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), xdp->data,
+					  IXGBEVF_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), xdp->data,
drivers/net/ethernet/mellanox/mlx5/core/en_tx.c +2 -1
index 40f3f98aa279..efcc27756c7e 100644
@@ -163,7 +163,8 @@ static inline u16 mlx5e_calc_min_inline(enum mlx5_inline_modes mode,
 	case MLX5_INLINE_MODE_NONE:
 		return 0;
 	case MLX5_INLINE_MODE_TCP_UDP:
-		hlen = eth_get_headlen(skb->data, skb_headlen(skb));
+		hlen = eth_get_headlen(dev_net(skb->dev), skb->data,
+				       skb_headlen(skb));
 		if (hlen == ETH_HLEN && !skb_vlan_tag_present(skb))
 			hlen += VLAN_HLEN;
 		break;
drivers/net/tun.c +2 -1
index 24d0220b9ba0..6d5c8ecfea1e 100644
@@ -1965,7 +1965,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 
 	if (frags) {
 		/* Exercise flow dissector code path. */
-		u32 headlen = eth_get_headlen(skb->data, skb_headlen(skb));
+		u32 headlen = eth_get_headlen(dev_net(tun->dev), skb->data,
+					      skb_headlen(skb));
 
 		if (unlikely(headlen > skb_headlen(skb))) {
 			this_cpu_inc(tun->pcpu_stats->rx_dropped);
include/linux/etherdevice.h +1 -1
index e2f3b21cd72a..71a441ffab3f 100644
@@ -33,7 +33,7 @@ struct device;
 int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr);
 unsigned char *arch_get_platform_mac_address(void);
 int nvmem_get_mac_address(struct device *dev, void *addrbuf);
-u32 eth_get_headlen(void *data, unsigned int max_len);
+u32 eth_get_headlen(const struct net *net, void *data, unsigned int max_len);
 __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
 extern const struct header_ops eth_header_ops;
 
net/ethernet/eth.c +3 -2
index 1e439549c419..0202e72e20a4 100644
@@ -119,13 +119,14 @@ EXPORT_SYMBOL(eth_header);
 
 /**
  * eth_get_headlen - determine the length of header for an ethernet frame
+ * @net: pointer to device network namespace
  * @data: pointer to start of frame
  * @len: total length of frame
  *
  * Make a best effort attempt to pull the length for all of the headers for
  * a given frame in a linear buffer.
  */
-u32 eth_get_headlen(void *data, unsigned int len)
+u32 eth_get_headlen(const struct net *net, void *data, unsigned int len)
 {
 	const unsigned int flags = FLOW_DISSECTOR_F_PARSE_1ST_FRAG;
 	const struct ethhdr *eth = (const struct ethhdr *)data;
@@ -136,7 +137,7 @@ u32 eth_get_headlen(void *data, unsigned int len)
 		return len;
 
 	/* parse any remaining L2/L3 headers, check for L4 */
-	if (!skb_flow_dissect_flow_keys_basic(NULL, NULL, &keys, data,
+	if (!skb_flow_dissect_flow_keys_basic(net, NULL, &keys, data,
 					      eth->h_proto, sizeof(*eth),
 					      len, flags))
 		return max_t(u32, keys.control.thoff, sizeof(*eth));
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
#6 lore raw

Re: [PATCH bpf-next v5 5/6] net: pass net argument to the eth_get_headlen

Quoted 245 lines · Stanislav Fomichev

Update all users eth_get_headlen to pass network namespace and pass it down to the flow dissector. This commit is a noop until administrator inserts BPF flow dissector program.

Cc Maxim Krasnyansky <[email protected]>
Cc Saeed Mahameed <[email protected]>
Cc Jeff Kirsher <[email protected]>
Cc intel-wired-lan at lists.osuosl.org
Cc Yisen Zhuang <[email protected]>
Cc Salil Mehta <[email protected]>
Cc Michael Chan <[email protected]>
Cc Igor Russkikh <[email protected]>
---
 drivers/net/ethernet/aquantia/atlantic/aq_ring.c  | 3 ++-
 drivers/net/ethernet/broadcom/bnxt/bnxt.c         | 2 +-
 drivers/net/ethernet/hisilicon/hns/hns_enet.c     | 3 ++-
 drivers/net/ethernet/hisilicon/hns3/hns3_enet.c   | 3 ++-
 drivers/net/ethernet/intel/fm10k/fm10k_main.c     | 2 +-
 drivers/net/ethernet/intel/i40e/i40e_txrx.c       | 3 ++-
 drivers/net/ethernet/intel/iavf/iavf_txrx.c       | 3 ++-
 drivers/net/ethernet/intel/ice/ice_txrx.c         | 3 ++-
 drivers/net/ethernet/intel/igb/igb_main.c         | 3 ++-
 drivers/net/ethernet/intel/igc/igc_main.c         | 3 ++-
 drivers/net/ethernet/intel/ixgbe/ixgbe_main.c     | 2 +-
 drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c | 3 ++-
 drivers/net/ethernet/mellanox/mlx5/core/en_tx.c   | 3 ++-
 drivers/net/tun.c                                 | 3 ++-
 include/linux/etherdevice.h                       | 2 +-
 net/ethernet/eth.c                                | 5 +++--
 16 files changed, 29 insertions(+), 17 deletions(-)
drivers/net/ethernet/aquantia/atlantic/aq_ring.c +2 -1
index c64e2fb5a4f1..1b3181f757b7 100644
@@ -354,7 +354,8 @@ int aq_ring_rx_clean(struct aq_ring_s *self,
 
 			hdr_len = buff->len;
 			if (hdr_len > AQ_CFG_RX_HDR_SIZE)
-				hdr_len = eth_get_headlen(aq_buf_vaddr(&buff->rxdata),
+				hdr_len = eth_get_headlen(dev_net(skb->dev),
+							  aq_buf_vaddr(&buff->rxdata),
 							  AQ_CFG_RX_HDR_SIZE);
 
 			memcpy(__skb_put(skb, hdr_len), aq_buf_vaddr(&buff->rxdata),
drivers/net/ethernet/broadcom/bnxt/bnxt.c +1 -1
index 6528a597367b..8bb5f708ccc6 100644
@@ -899,7 +899,7 @@ static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
 			     DMA_ATTR_WEAK_ORDERING);
 
 	if (unlikely(!payload))
-		payload = eth_get_headlen(data_ptr, len);
+		payload = eth_get_headlen(dev_net(bp->dev), data_ptr, len);
 
 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
 	if (!skb) {
drivers/net/ethernet/hisilicon/hns/hns_enet.c +2 -1
index 297b95c1b3c1..f1ecc78d2323 100644
@@ -598,7 +598,8 @@ static int hns_nic_poll_rx_skb(struct hns_nic_ring_data *ring_data,
 	} else {
 		ring->stats.seg_pkt_cnt++;
 
-		pull_len = eth_get_headlen(va, HNS_RX_HEAD_SIZE);
+		pull_len = eth_get_headlen(dev_net(ndev), va,
+					   HNS_RX_HEAD_SIZE);
 		memcpy(__skb_put(skb, pull_len), va,
 		       ALIGN(pull_len, sizeof(long)));
 
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +2 -1
index b53b0911ec24..423d9ce0f6f8 100644
@@ -2457,7 +2457,8 @@ static int hns3_alloc_skb(struct hns3_enet_ring *ring, int length,
 	ring->stats.seg_pkt_cnt++;
 	u64_stats_update_end(&ring->syncp);
 
-	ring->pull_len = eth_get_headlen(va, HNS3_RX_HEAD_SIZE);
+	ring->pull_len = eth_get_headlen(dev_net(netdev), va,
+					 HNS3_RX_HEAD_SIZE);
 	__skb_put(skb, ring->pull_len);
 	hns3_nic_reuse_page(skb, ring->frag_num++, ring, ring->pull_len,
 			    desc_cb);
drivers/net/ethernet/intel/fm10k/fm10k_main.c +1 -1
index 2325cee76211..e2bee187d652 100644
@@ -280,7 +280,7 @@ static bool fm10k_add_rx_frag(struct fm10k_rx_buffer *rx_buffer,
 	/* we need the header to contain the greater of either ETH_HLEN or
 	 * 60 bytes if the skb->len is less than 60 for skb_pad.
 	 */
-	pull_len = eth_get_headlen(va, FM10K_RX_HDR_LEN);
+	pull_len = eth_get_headlen(dev_net(skb->dev), va, FM10K_RX_HDR_LEN);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, pull_len), va, ALIGN(pull_len, sizeof(long)));
drivers/net/ethernet/intel/i40e/i40e_txrx.c +2 -1
index 1a95223c9f99..85c5b503e0a0 100644
@@ -2035,7 +2035,8 @@ static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > I40E_RX_HDR_SIZE)
-		headlen = eth_get_headlen(xdp->data, I40E_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), xdp->data,
+					  I40E_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), xdp->data,
drivers/net/ethernet/intel/iavf/iavf_txrx.c +2 -1
index b64187753ad6..23a62d7d0f9f 100644
@@ -1315,7 +1315,8 @@ static struct sk_buff *iavf_construct_skb(struct iavf_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IAVF_RX_HDR_SIZE)
-		headlen = eth_get_headlen(va, IAVF_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  IAVF_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/ice/ice_txrx.c +2 -1
index a6f7b7feaf3c..2692b9333055 100644
@@ -698,7 +698,8 @@ ice_construct_skb(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > ICE_RX_HDR_SIZE)
-		headlen = eth_get_headlen(va, ICE_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  ICE_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/igb/igb_main.c +2 -1
index acbb5b4f333d..2023e1800c8d 100644
@@ -8051,7 +8051,8 @@ static struct sk_buff *igb_construct_skb(struct igb_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IGB_RX_HDR_LEN)
-		headlen = eth_get_headlen(va, IGB_RX_HDR_LEN);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  IGB_RX_HDR_LEN);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/igc/igc_main.c +2 -1
index f79728381e8a..265a9d8a8421 100644
@@ -1199,7 +1199,8 @@ static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IGC_RX_HDR_LEN)
-		headlen = eth_get_headlen(va, IGC_RX_HDR_LEN);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  IGC_RX_HDR_LEN);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/ixgbe/ixgbe_main.c +1 -1
index 60cec3540dd7..5e5294567ca1 100644
@@ -1800,7 +1800,7 @@ static void ixgbe_pull_tail(struct ixgbe_ring *rx_ring,
 	 * we need the header to contain the greater of either ETH_HLEN or
 	 * 60 bytes if the skb->len is less than 60 for skb_pad.
 	 */
-	pull_len = eth_get_headlen(va, IXGBE_RX_HDR_SIZE);
+	pull_len = eth_get_headlen(dev_net(skb->dev), va, IXGBE_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c +2 -1
index 49e23afa05a2..252fe0de6b56 100644
@@ -895,7 +895,8 @@ struct sk_buff *ixgbevf_construct_skb(struct ixgbevf_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IXGBEVF_RX_HDR_SIZE)
-		headlen = eth_get_headlen(xdp->data, IXGBEVF_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), xdp->data,
+					  IXGBEVF_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), xdp->data,
drivers/net/ethernet/mellanox/mlx5/core/en_tx.c +2 -1
index 40f3f98aa279..efcc27756c7e 100644
@@ -163,7 +163,8 @@ static inline u16 mlx5e_calc_min_inline(enum mlx5_inline_modes mode,
 	case MLX5_INLINE_MODE_NONE:
 		return 0;
 	case MLX5_INLINE_MODE_TCP_UDP:
-		hlen = eth_get_headlen(skb->data, skb_headlen(skb));
+		hlen = eth_get_headlen(dev_net(skb->dev), skb->data,
+				       skb_headlen(skb));
 		if (hlen == ETH_HLEN && !skb_vlan_tag_present(skb))
 			hlen += VLAN_HLEN;
 		break;
drivers/net/tun.c +2 -1
index 24d0220b9ba0..6d5c8ecfea1e 100644
@@ -1965,7 +1965,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 
 	if (frags) {
 		/* Exercise flow dissector code path. */
-		u32 headlen = eth_get_headlen(skb->data, skb_headlen(skb));
+		u32 headlen = eth_get_headlen(dev_net(tun->dev), skb->data,
+					      skb_headlen(skb));
 
 		if (unlikely(headlen > skb_headlen(skb))) {
 			this_cpu_inc(tun->pcpu_stats->rx_dropped);
include/linux/etherdevice.h +1 -1
index e2f3b21cd72a..71a441ffab3f 100644
@@ -33,7 +33,7 @@ struct device;
 int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr);
 unsigned char *arch_get_platform_mac_address(void);
 int nvmem_get_mac_address(struct device *dev, void *addrbuf);
-u32 eth_get_headlen(void *data, unsigned int max_len);
+u32 eth_get_headlen(const struct net *net, void *data, unsigned int max_len);
 __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
 extern const struct header_ops eth_header_ops;
 
net/ethernet/eth.c +0 -0
index 1e439549c419..0202e72e20a4 100644
@@ -119,13 +119,14 @@ EXPORT_SYMBOL(eth_header);
 
 /**

net/ethernet/eth.c EXPORT_SYMBOL(eth_header); · line 129

Quoted 10 lines
net/ethernet/eth.c +2 -1
  * eth_get_headlen - determine the length of header for an ethernet frame
+ * @net: pointer to device network namespace
  * @data: pointer to start of frame
  * @len: total length of frame
  *
  * Make a best effort attempt to pull the length for all of the headers for
  * a given frame in a linear buffer.
  */
-u32 eth_get_headlen(void *data, unsigned int len)
+u32 eth_get_headlen(const struct net *net, void *data, unsigned int len)

would it make sense to future proof it a little bit and pass 'dev' into eth_get_headlen() instead of 'net' ?
May be tomorrow we'd want different flow_dissectors per-device in addition to per-net ?

Also please add C based test for skb-less flow_dissector. Current test_flow_dissector.sh doesn't seem to cover it.

#7 lore raw

Re: [PATCH bpf-next v5 5/6] net: pass net argument to the eth_get_headlen

Quoted 245 lines · Alexei Starovoitov
Quoted 244 lines · Stanislav Fomichev

Update all users eth_get_headlen to pass network namespace and pass it down to the flow dissector. This commit is a noop until administrator inserts BPF flow dissector program.

Cc Maxim Krasnyansky <[email protected]>
Cc Saeed Mahameed <[email protected]>
Cc Jeff Kirsher <[email protected]>
Cc intel-wired-lan at lists.osuosl.org
Cc Yisen Zhuang <[email protected]>
Cc Salil Mehta <[email protected]>
Cc Michael Chan <[email protected]>
Cc Igor Russkikh <[email protected]>
---
 drivers/net/ethernet/aquantia/atlantic/aq_ring.c  | 3 ++-
 drivers/net/ethernet/broadcom/bnxt/bnxt.c         | 2 +-
 drivers/net/ethernet/hisilicon/hns/hns_enet.c     | 3 ++-
 drivers/net/ethernet/hisilicon/hns3/hns3_enet.c   | 3 ++-
 drivers/net/ethernet/intel/fm10k/fm10k_main.c     | 2 +-
 drivers/net/ethernet/intel/i40e/i40e_txrx.c       | 3 ++-
 drivers/net/ethernet/intel/iavf/iavf_txrx.c       | 3 ++-
 drivers/net/ethernet/intel/ice/ice_txrx.c         | 3 ++-
 drivers/net/ethernet/intel/igb/igb_main.c         | 3 ++-
 drivers/net/ethernet/intel/igc/igc_main.c         | 3 ++-
 drivers/net/ethernet/intel/ixgbe/ixgbe_main.c     | 2 +-
 drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c | 3 ++-
 drivers/net/ethernet/mellanox/mlx5/core/en_tx.c   | 3 ++-
 drivers/net/tun.c                                 | 3 ++-
 include/linux/etherdevice.h                       | 2 +-
 net/ethernet/eth.c                                | 5 +++--
 16 files changed, 29 insertions(+), 17 deletions(-)
drivers/net/ethernet/aquantia/atlantic/aq_ring.c +2 -1
index c64e2fb5a4f1..1b3181f757b7 100644
@@ -354,7 +354,8 @@ int aq_ring_rx_clean(struct aq_ring_s *self,
 
 			hdr_len = buff->len;
 			if (hdr_len > AQ_CFG_RX_HDR_SIZE)
-				hdr_len = eth_get_headlen(aq_buf_vaddr(&buff->rxdata),
+				hdr_len = eth_get_headlen(dev_net(skb->dev),
+							  aq_buf_vaddr(&buff->rxdata),
 							  AQ_CFG_RX_HDR_SIZE);
 
 			memcpy(__skb_put(skb, hdr_len), aq_buf_vaddr(&buff->rxdata),
drivers/net/ethernet/broadcom/bnxt/bnxt.c +1 -1
index 6528a597367b..8bb5f708ccc6 100644
@@ -899,7 +899,7 @@ static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
 			     DMA_ATTR_WEAK_ORDERING);
 
 	if (unlikely(!payload))
-		payload = eth_get_headlen(data_ptr, len);
+		payload = eth_get_headlen(dev_net(bp->dev), data_ptr, len);
 
 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
 	if (!skb) {
drivers/net/ethernet/hisilicon/hns/hns_enet.c +2 -1
index 297b95c1b3c1..f1ecc78d2323 100644
@@ -598,7 +598,8 @@ static int hns_nic_poll_rx_skb(struct hns_nic_ring_data *ring_data,
 	} else {
 		ring->stats.seg_pkt_cnt++;
 
-		pull_len = eth_get_headlen(va, HNS_RX_HEAD_SIZE);
+		pull_len = eth_get_headlen(dev_net(ndev), va,
+					   HNS_RX_HEAD_SIZE);
 		memcpy(__skb_put(skb, pull_len), va,
 		       ALIGN(pull_len, sizeof(long)));
 
drivers/net/ethernet/hisilicon/hns3/hns3_enet.c +2 -1
index b53b0911ec24..423d9ce0f6f8 100644
@@ -2457,7 +2457,8 @@ static int hns3_alloc_skb(struct hns3_enet_ring *ring, int length,
 	ring->stats.seg_pkt_cnt++;
 	u64_stats_update_end(&ring->syncp);
 
-	ring->pull_len = eth_get_headlen(va, HNS3_RX_HEAD_SIZE);
+	ring->pull_len = eth_get_headlen(dev_net(netdev), va,
+					 HNS3_RX_HEAD_SIZE);
 	__skb_put(skb, ring->pull_len);
 	hns3_nic_reuse_page(skb, ring->frag_num++, ring, ring->pull_len,
 			    desc_cb);
drivers/net/ethernet/intel/fm10k/fm10k_main.c +1 -1
index 2325cee76211..e2bee187d652 100644
@@ -280,7 +280,7 @@ static bool fm10k_add_rx_frag(struct fm10k_rx_buffer *rx_buffer,
 	/* we need the header to contain the greater of either ETH_HLEN or
 	 * 60 bytes if the skb->len is less than 60 for skb_pad.
 	 */
-	pull_len = eth_get_headlen(va, FM10K_RX_HDR_LEN);
+	pull_len = eth_get_headlen(dev_net(skb->dev), va, FM10K_RX_HDR_LEN);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, pull_len), va, ALIGN(pull_len, sizeof(long)));
drivers/net/ethernet/intel/i40e/i40e_txrx.c +2 -1
index 1a95223c9f99..85c5b503e0a0 100644
@@ -2035,7 +2035,8 @@ static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > I40E_RX_HDR_SIZE)
-		headlen = eth_get_headlen(xdp->data, I40E_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), xdp->data,
+					  I40E_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), xdp->data,
drivers/net/ethernet/intel/iavf/iavf_txrx.c +2 -1
index b64187753ad6..23a62d7d0f9f 100644
@@ -1315,7 +1315,8 @@ static struct sk_buff *iavf_construct_skb(struct iavf_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IAVF_RX_HDR_SIZE)
-		headlen = eth_get_headlen(va, IAVF_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  IAVF_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/ice/ice_txrx.c +2 -1
index a6f7b7feaf3c..2692b9333055 100644
@@ -698,7 +698,8 @@ ice_construct_skb(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > ICE_RX_HDR_SIZE)
-		headlen = eth_get_headlen(va, ICE_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  ICE_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/igb/igb_main.c +2 -1
index acbb5b4f333d..2023e1800c8d 100644
@@ -8051,7 +8051,8 @@ static struct sk_buff *igb_construct_skb(struct igb_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IGB_RX_HDR_LEN)
-		headlen = eth_get_headlen(va, IGB_RX_HDR_LEN);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  IGB_RX_HDR_LEN);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/igc/igc_main.c +2 -1
index f79728381e8a..265a9d8a8421 100644
@@ -1199,7 +1199,8 @@ static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IGC_RX_HDR_LEN)
-		headlen = eth_get_headlen(va, IGC_RX_HDR_LEN);
+		headlen = eth_get_headlen(dev_net(skb->dev), va,
+					  IGC_RX_HDR_LEN);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
drivers/net/ethernet/intel/ixgbe/ixgbe_main.c +1 -1
index 60cec3540dd7..5e5294567ca1 100644
@@ -1800,7 +1800,7 @@ static void ixgbe_pull_tail(struct ixgbe_ring *rx_ring,
 	 * we need the header to contain the greater of either ETH_HLEN or
 	 * 60 bytes if the skb->len is less than 60 for skb_pad.
 	 */
-	pull_len = eth_get_headlen(va, IXGBE_RX_HDR_SIZE);
+	pull_len = eth_get_headlen(dev_net(skb->dev), va, IXGBE_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c +2 -1
index 49e23afa05a2..252fe0de6b56 100644
@@ -895,7 +895,8 @@ struct sk_buff *ixgbevf_construct_skb(struct ixgbevf_ring *rx_ring,
 	/* Determine available headroom for copy */
 	headlen = size;
 	if (headlen > IXGBEVF_RX_HDR_SIZE)
-		headlen = eth_get_headlen(xdp->data, IXGBEVF_RX_HDR_SIZE);
+		headlen = eth_get_headlen(dev_net(skb->dev), xdp->data,
+					  IXGBEVF_RX_HDR_SIZE);
 
 	/* align pull length to size of long to optimize memcpy performance */
 	memcpy(__skb_put(skb, headlen), xdp->data,
drivers/net/ethernet/mellanox/mlx5/core/en_tx.c +2 -1
index 40f3f98aa279..efcc27756c7e 100644
@@ -163,7 +163,8 @@ static inline u16 mlx5e_calc_min_inline(enum mlx5_inline_modes mode,
 	case MLX5_INLINE_MODE_NONE:
 		return 0;
 	case MLX5_INLINE_MODE_TCP_UDP:
-		hlen = eth_get_headlen(skb->data, skb_headlen(skb));
+		hlen = eth_get_headlen(dev_net(skb->dev), skb->data,
+				       skb_headlen(skb));
 		if (hlen == ETH_HLEN && !skb_vlan_tag_present(skb))
 			hlen += VLAN_HLEN;
 		break;
drivers/net/tun.c +2 -1
index 24d0220b9ba0..6d5c8ecfea1e 100644
@@ -1965,7 +1965,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 
 	if (frags) {
 		/* Exercise flow dissector code path. */
-		u32 headlen = eth_get_headlen(skb->data, skb_headlen(skb));
+		u32 headlen = eth_get_headlen(dev_net(tun->dev), skb->data,
+					      skb_headlen(skb));
 
 		if (unlikely(headlen > skb_headlen(skb))) {
 			this_cpu_inc(tun->pcpu_stats->rx_dropped);
include/linux/etherdevice.h +1 -1
index e2f3b21cd72a..71a441ffab3f 100644
@@ -33,7 +33,7 @@ struct device;
 int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr);
 unsigned char *arch_get_platform_mac_address(void);
 int nvmem_get_mac_address(struct device *dev, void *addrbuf);
-u32 eth_get_headlen(void *data, unsigned int max_len);
+u32 eth_get_headlen(const struct net *net, void *data, unsigned int max_len);
 __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
 extern const struct header_ops eth_header_ops;
 
net/ethernet/eth.c +0 -0
index 1e439549c419..0202e72e20a4 100644
@@ -119,13 +119,14 @@ EXPORT_SYMBOL(eth_header);
 
Quoted 16 lines

net/ethernet/eth.c EXPORT_SYMBOL(eth_header); · line 129

Quoted 11 lines
net/ethernet/eth.c +2 -1
 /**
  * eth_get_headlen - determine the length of header for an ethernet frame
+ * @net: pointer to device network namespace
  * @data: pointer to start of frame
  * @len: total length of frame
  *
  * Make a best effort attempt to pull the length for all of the headers for
  * a given frame in a linear buffer.
  */
-u32 eth_get_headlen(void *data, unsigned int len)
+u32 eth_get_headlen(const struct net *net, void *data, unsigned int len)

would it make sense to future proof it a little bit and pass 'dev' into eth_get_headlen() instead of 'net' ?
May be tomorrow we'd want different flow_dissectors per-device in addition to per-net ?

Good point, will use net_device.

Quoted 2 lines

Also please add C based test for skb-less flow_dissector. Current test_flow_dissector.sh doesn't seem to cover it.

It doesn't look like we can exercise skb-less flow dissector from test_flow_dissector.sh; we need to trigger some driver code, which is hard when we send the packets on the localhost in test_flow_dissector.sh.

To test skb-less dissector I convert BPF_PROG_TEST_RUN to always use skb-less mode. test_flow_dissector.sh tests skb-mode, prog_tests/flow_dissector.c tests skb-less mode.

#8 lore raw

Re: [PATCH bpf-next v5 5/6] net: pass net argument to the eth_get_headlen

Quoted 15 lines · Stanislav Fomichev
Quoted 14 lines · Alexei Starovoitov
Quoted 13 lines · Stanislav Fomichev

Update all users eth_get_headlen to pass network namespace and pass it down to the flow dissector. This commit is a noop until administrator inserts BPF flow dissector program.

Cc Maxim Krasnyansky <[email protected]>
Cc Saeed Mahameed <[email protected]>
Cc Jeff Kirsher <[email protected]>
Cc intel-wired-lan at lists.osuosl.org
Cc Yisen Zhuang <[email protected]>
Cc Salil Mehta <[email protected]>
Cc Michael Chan <[email protected]>
Cc Igor Russkikh <[email protected]>

...

Quoted 10 lines
Quoted 2 lines

Also please add C based test for skb-less flow_dissector. Current test_flow_dissector.sh doesn't seem to cover it.

It doesn't look like we can exercise skb-less flow dissector from test_flow_dissector.sh; we need to trigger some driver code, which is hard when we send the packets on the localhost in test_flow_dissector.sh.

To test skb-less dissector I convert BPF_PROG_TEST_RUN to always use skb-less mode. test_flow_dissector.sh tests skb-mode, prog_tests/flow_dissector.c tests skb-less mode.

I saw that but I'm afraid it's not enough. tun_get_user() is calling it, so it should be possible to test skb-less mode via tun.

#9 lore raw

Re: [PATCH bpf-next v5 5/6] net: pass net argument to the eth_get_headlen

Quoted 24 lines · Alexei Starovoitov
Quoted 15 lines · Stanislav Fomichev
Quoted 14 lines · Alexei Starovoitov
Quoted 13 lines · Stanislav Fomichev

Update all users eth_get_headlen to pass network namespace and pass it down to the flow dissector. This commit is a noop until administrator inserts BPF flow dissector program.

Cc Maxim Krasnyansky <[email protected]>
Cc Saeed Mahameed <[email protected]>
Cc Jeff Kirsher <[email protected]>
Cc intel-wired-lan at lists.osuosl.org
Cc Yisen Zhuang <[email protected]>
Cc Salil Mehta <[email protected]>
Cc Michael Chan <[email protected]>
Cc Igor Russkikh <[email protected]>

...

Quoted 7 lines
Quoted 2 lines

Also please add C based test for skb-less flow_dissector. Current test_flow_dissector.sh doesn't seem to cover it.

It doesn't look like we can exercise skb-less flow dissector from test_flow_dissector.sh; we need to trigger some driver code, which is hard when we send the packets on the localhost in test_flow_dissector.sh.

Quoted 7 lines
Quoted 3 lines

To test skb-less dissector I convert BPF_PROG_TEST_RUN to always use skb-less mode. test_flow_dissector.sh tests skb-mode, prog_tests/flow_dissector.c tests skb-less mode.

I saw that but I'm afraid it's not enough. tun_get_user() is calling it, so it should be possible to test skb-less mode via tun.

Spent some time today looking into how to exercise this path in the tun
driver: doing writev() with IFF_NAPI_FRAGS IFF_TAP device would trigger eth_get_headlen, but it looks like there is no way to do a test with pass/no-pass result around that.

The problem is - we don't actually do anything with the result of eth_get_headlen, there is only a sanity check for "headlen > skb_headlen(skb)" which can't trigger for BPF flow dissector; we carefully clamp thoff/nhoff and should not return offset outside the input buffer.

By reading git history it looks like this call to eth_get_headlen was added there to only make it possible for tools like syzbot to fuzz flow dissector. That's why we don't care about the result, we just do that simple sanity check. The main goal is to trigger some problem (loop/warning) in the flow dissector code.

tl;dr - no mater which bpf flow dissector is attached to the namespace, it would not change behavior of the tun device; even empty 'return false' program would not alter it.

Let me know if you had something different in mind; because so far I don't see how to do a test around that. Changing that "headlen > skb_headlen(skb)" check into something meaningful also doesn't seem possible. I thought about checking the result of eth_get_headlen against skb_transport_offset(), but at that point transport offset of the skb is not yet set (napi_gro_frags and gro layer later does that) :-(

#10 lore raw

Re: [PATCH bpf-next v5 5/6] net: pass net argument to the eth_get_headlen

Quoted 49 lines · Stanislav Fomichev
Quoted 24 lines · Alexei Starovoitov
Quoted 15 lines · Stanislav Fomichev
Quoted 14 lines · Alexei Starovoitov
Quoted 13 lines · Stanislav Fomichev

Update all users eth_get_headlen to pass network namespace and pass it down to the flow dissector. This commit is a noop until administrator inserts BPF flow dissector program.

Cc Maxim Krasnyansky <[email protected]>
Cc Saeed Mahameed <[email protected]>
Cc Jeff Kirsher <[email protected]>
Cc intel-wired-lan at lists.osuosl.org
Cc Yisen Zhuang <[email protected]>
Cc Salil Mehta <[email protected]>
Cc Michael Chan <[email protected]>
Cc Igor Russkikh <[email protected]>

...

Quoted 7 lines
Quoted 2 lines

Also please add C based test for skb-less flow_dissector. Current test_flow_dissector.sh doesn't seem to cover it.

It doesn't look like we can exercise skb-less flow dissector from test_flow_dissector.sh; we need to trigger some driver code, which is hard when we send the packets on the localhost in test_flow_dissector.sh.

Quoted 7 lines
Quoted 3 lines

To test skb-less dissector I convert BPF_PROG_TEST_RUN to always use skb-less mode. test_flow_dissector.sh tests skb-mode, prog_tests/flow_dissector.c tests skb-less mode.

I saw that but I'm afraid it's not enough. tun_get_user() is calling it, so it should be possible to test skb-less mode via tun.

Spent some time today looking into how to exercise this path in the tun
driver: doing writev() with IFF_NAPI_FRAGS IFF_TAP device would trigger eth_get_headlen, but it looks like there is no way to do a test with pass/no-pass result around that.

The problem is - we don't actually do anything with the result of eth_get_headlen, there is only a sanity check for "headlen > skb_headlen(skb)" which can't trigger for BPF flow dissector; we carefully clamp thoff/nhoff and should not return offset outside the input buffer.

By reading git history it looks like this call to eth_get_headlen was added there to only make it possible for tools like syzbot to fuzz flow dissector. That's why we don't care about the result, we just do that simple sanity check. The main goal is to trigger some problem (loop/warning) in the flow dissector code.

Quoted 3 lines

tl;dr - no mater which bpf flow dissector is attached to the namespace, it would not change behavior of the tun device; even empty 'return false' program would not alter it.

sure, but the program will run and the test can validate that the program saw valid packet, parsed it correctly and returned correct dissection. The results can be stored in a map and validated by the test. iirc you were saying that you'll have one program doing dissection for with-skb and skb-less cases.
I think it's important to have such program in selftests and being run continuously for both cases.

#11 lore raw

Re: [PATCH bpf-next v5 5/6] net: pass net argument to the eth_get_headlen

Quoted 50 lines · Alexei Starovoitov
Quoted 49 lines · Stanislav Fomichev
Quoted 24 lines · Alexei Starovoitov
Quoted 15 lines · Stanislav Fomichev
Quoted 14 lines · Alexei Starovoitov
Quoted 13 lines · Stanislav Fomichev

Update all users eth_get_headlen to pass network namespace and pass it down to the flow dissector. This commit is a noop until administrator inserts BPF flow dissector program.

Cc Maxim Krasnyansky <[email protected]>
Cc Saeed Mahameed <[email protected]>
Cc Jeff Kirsher <[email protected]>
Cc intel-wired-lan at lists.osuosl.org
Cc Yisen Zhuang <[email protected]>
Cc Salil Mehta <[email protected]>
Cc Michael Chan <[email protected]>
Cc Igor Russkikh <[email protected]>

...

Quoted 7 lines
Quoted 2 lines

Also please add C based test for skb-less flow_dissector. Current test_flow_dissector.sh doesn't seem to cover it.

It doesn't look like we can exercise skb-less flow dissector from test_flow_dissector.sh; we need to trigger some driver code, which is hard when we send the packets on the localhost in test_flow_dissector.sh.

Quoted 7 lines
Quoted 3 lines

To test skb-less dissector I convert BPF_PROG_TEST_RUN to always use skb-less mode. test_flow_dissector.sh tests skb-mode, prog_tests/flow_dissector.c tests skb-less mode.

I saw that but I'm afraid it's not enough. tun_get_user() is calling it, so it should be possible to test skb-less mode via tun.

Spent some time today looking into how to exercise this path in the tun
driver: doing writev() with IFF_NAPI_FRAGS IFF_TAP device would trigger eth_get_headlen, but it looks like there is no way to do a test with pass/no-pass result around that.

The problem is - we don't actually do anything with the result of eth_get_headlen, there is only a sanity check for "headlen > skb_headlen(skb)" which can't trigger for BPF flow dissector; we carefully clamp thoff/nhoff and should not return offset outside the input buffer.

By reading git history it looks like this call to eth_get_headlen was added there to only make it possible for tools like syzbot to fuzz flow dissector. That's why we don't care about the result, we just do that simple sanity check. The main goal is to trigger some problem (loop/warning) in the flow dissector code.

Quoted 7 lines
Quoted 3 lines

tl;dr - no mater which bpf flow dissector is attached to the namespace, it would not change behavior of the tun device; even empty 'return false' program would not alter it.

sure, but the program will run and the test can validate that the program saw valid packet, parsed it correctly and returned correct dissection. The results can be stored in a map and validated by the test.

SG, that's doable; that would make bpf_flow.c less generic because it would have to have this map which would export the last dissection, but that should be fine, I guess.

(I planned to use bpf_flow.c internally instead of writing another one).

Quoted 2 lines

iirc you were saying that you'll have one program doing dissection for with-skb and skb-less cases.

Correct.

Quoted 2 lines

I think it's important to have such program in selftests and being run continuously for both cases.

Ok, in this case, I can write a small userspace program that writes some dummy packet into a tap device (triggers eth_get_headlen) and reads back and verifies bpf_flow_keys from the shared map.

Correct me if I misunderstood something. Otherwise, I'll get back to you with a v6+test.

#12 lore raw

Re: [PATCH bpf-next v5 5/6] net: pass net argument to the eth_get_headlen

Quoted 66 lines · Stanislav Fomichev
Quoted 50 lines · Alexei Starovoitov
Quoted 49 lines · Stanislav Fomichev
Quoted 24 lines · Alexei Starovoitov
Quoted 15 lines · Stanislav Fomichev
Quoted 14 lines · Alexei Starovoitov
Quoted 13 lines · Stanislav Fomichev

Update all users eth_get_headlen to pass network namespace and pass it down to the flow dissector. This commit is a noop until administrator inserts BPF flow dissector program.

Cc Maxim Krasnyansky <[email protected]>
Cc Saeed Mahameed <[email protected]>
Cc Jeff Kirsher <[email protected]>
Cc intel-wired-lan at lists.osuosl.org
Cc Yisen Zhuang <[email protected]>
Cc Salil Mehta <[email protected]>
Cc Michael Chan <[email protected]>
Cc Igor Russkikh <[email protected]>

...

Quoted 7 lines
Quoted 2 lines

Also please add C based test for skb-less flow_dissector. Current test_flow_dissector.sh doesn't seem to cover it.

It doesn't look like we can exercise skb-less flow dissector from test_flow_dissector.sh; we need to trigger some driver code, which is hard when we send the packets on the localhost in test_flow_dissector.sh.

Quoted 7 lines
Quoted 3 lines

To test skb-less dissector I convert BPF_PROG_TEST_RUN to always use skb-less mode. test_flow_dissector.sh tests skb-mode, prog_tests/flow_dissector.c tests skb-less mode.

I saw that but I'm afraid it's not enough. tun_get_user() is calling it, so it should be possible to test skb-less mode via tun.

Spent some time today looking into how to exercise this path in the tun
driver: doing writev() with IFF_NAPI_FRAGS IFF_TAP device would trigger eth_get_headlen, but it looks like there is no way to do a test with pass/no-pass result around that.

The problem is - we don't actually do anything with the result of eth_get_headlen, there is only a sanity check for "headlen > skb_headlen(skb)" which can't trigger for BPF flow dissector; we carefully clamp thoff/nhoff and should not return offset outside the input buffer.

By reading git history it looks like this call to eth_get_headlen was added there to only make it possible for tools like syzbot to fuzz flow dissector. That's why we don't care about the result, we just do that simple sanity check. The main goal is to trigger some problem (loop/warning) in the flow dissector code.

Quoted 7 lines
Quoted 3 lines

tl;dr - no mater which bpf flow dissector is attached to the namespace, it would not change behavior of the tun device; even empty 'return false' program would not alter it.

sure, but the program will run and the test can validate that the program saw valid packet, parsed it correctly and returned correct dissection. The results can be stored in a map and validated by the test.

SG, that's doable; that would make bpf_flow.c less generic because it would have to have this map which would export the last dissection, but that should be fine, I guess.

(I planned to use bpf_flow.c internally instead of writing another one).

Quoted 2 lines

iirc you were saying that you'll have one program doing dissection for with-skb and skb-less cases.

Quoted 7 lines

Correct.

Quoted 2 lines

I think it's important to have such program in selftests and being run continuously for both cases.

Ok, in this case, I can write a small userspace program that writes some dummy packet into a tap device (triggers eth_get_headlen) and reads back and verifies bpf_flow_keys from the shared map.

that sounds good. there could be two test programs. bpf_flow.c that you use internally and another one, but please make that other one to test both with-skb and skb-less paths.

#13 lore raw

PATCH v5 6/6 selftests/bpf: add flow dissector bpf_skb_load_bytes helper test

In reply to Stanislav Fomichev

When flow dissector is called without skb, we want to make sure bpf_skb_load_bytes invocations return error. Add small test which tries to read single byte from a packet.

bpf_skb_load_bytes should always fail under BPF_PROG_TEST_RUN because it was converted to the skb-less mode.

---
 .../prog_tests/flow_dissector_load_bytes.c    | 48 +++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/flow_dissector_load_bytes.c
tools/testing/selftests/bpf/prog_tests/flow_dissector_load_bytes.c new file +48 -0
new file mode 100644
index 000000000000..dc5ef155ec28
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+
+void test_flow_dissector_load_bytes(void)
+{
+	struct bpf_flow_keys flow_keys;
+	__u32 duration = 0, retval, size;
+	struct bpf_insn prog[] = {
+		// BPF_REG_1 - 1st argument: context
+		// BPF_REG_2 - 2nd argument: offset, start at first byte
+		BPF_MOV64_IMM(BPF_REG_2, 0),
+		// BPF_REG_3 - 3rd argument: destination, reserve byte on stack
+		BPF_ALU64_REG(BPF_MOV, BPF_REG_3, BPF_REG_10),
+		BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -1),
+		// BPF_REG_4 - 4th argument: copy one byte
+		BPF_MOV64_IMM(BPF_REG_4, 1),
+		// bpf_skb_load_bytes(ctx, sizeof(pkt_v4), ptr, 1)
+		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
+			     BPF_FUNC_skb_load_bytes),
+		BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 2),
+		// if (ret == 0) return BPF_DROP (2)
+		BPF_MOV64_IMM(BPF_REG_0, BPF_DROP),
+		BPF_EXIT_INSN(),
+		// if (ret != 0) return BPF_OK (0)
+		BPF_MOV64_IMM(BPF_REG_0, BPF_OK),
+		BPF_EXIT_INSN(),
+	};
+	int fd, err;
+
+	/* make sure bpf_skb_load_bytes is not allowed from skb-less context
+	 */
+	fd = bpf_load_program(BPF_PROG_TYPE_FLOW_DISSECTOR, prog,
+			      ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
+	CHECK(fd < 0,
+	      "flow_dissector-bpf_skb_load_bytes-load",
+	      "fd %d errno %d\n",
+	      fd, errno);
+
+	err = bpf_prog_test_run(fd, 1, &pkt_v4, sizeof(pkt_v4),
+				&flow_keys, &size, &retval, &duration);
+	CHECK(size != sizeof(flow_keys) || err || retval != 1,
+	      "flow_dissector-bpf_skb_load_bytes",
+	      "err %d errno %d retval %d duration %d size %u/%zu\n",
+	      err, errno, retval, duration, size, sizeof(flow_keys));
+
+	if (fd >= -1)
+		close(fd);
+}
Signature · 1 line
2.21.0.392.gf8f6787159e-goog