Skip to contentSkip to conversation

mediathek: mtk_eth_soc: fix netdev inside xdp_rxq_info

PATCH New Til Kaiser 5 participants 6 messages Nov 26, 2024 – Dec 3, 2024

Subsystems (4)
lore
Share & tools
lore

Til Kaiser

author
#0 lore raw

PATCH 1/1 mediathek: mtk_eth_soc: fix netdev inside xdp_rxq_info

Hi,

I have noticed on a Banana Pi BPI-R4 running OpenWrt that the ingress interface index is always 0 when I attach an eBPF/XDP program to one of its SFP ports or its switch in native/driver mode.

Attached, you can find a fix/patch and a simple eBPF/XDP program I used for testing.

Kind regards
Til

author
#1 lore raw

PATCH 1/1 mediathek: mtk_eth_soc: fix netdev inside xdp_rxq_info

Currently, the network device isn't set inside the xdp_rxq_info of the mtk_rx_ring, which means that an XDP program attached to the Mediathek ethernet driver cannot retrieve the index of the interface that received the package since it's always 0 inside the xdp_md struct.

This patch sets the network device pointer inside the xdp_rxq_info struct, which is later used to initialize the xdp_buff struct via xdp_init_buff.

This was tested using the following eBPF/XDP program attached to a network interface of the mtk_eth_soc driver. As said before, ingress_ifindex always had a value of zero. After applying the patch, ingress_ifindex holds the correct interface index.

	#include <linux/bpf.h>
	#include <bpf/bpf_helpers.h>

	SEC("pass")
	int pass_func(struct xdp_md *xdp) {
    		bpf_printk("ingress_ifindex: %u",
			xdp->ingress_ifindex);

		return XDP_PASS;
	}

	char _license[] SEC("license") = "GPL";
---
 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -0
index 53485142938c..9c6d4477e536 100644
@@ -2069,6 +2069,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 
 		netdev = eth->netdev[mac];
 		ppe_idx = eth->mac[mac]->ppe_idx;
+		ring->xdp_q.dev = netdev;
 
 		if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
 			goto release_desc;
Signature · 1 line
2.47.1
#2 lore raw
Quoted 38 lines

Currently, the network device isn't set inside the xdp_rxq_info of the mtk_rx_ring, which means that an XDP program attached to the Mediathek ethernet driver cannot retrieve the index of the interface that received the package since it's always 0 inside the xdp_md struct.

This patch sets the network device pointer inside the xdp_rxq_info struct, which is later used to initialize the xdp_buff struct via xdp_init_buff.

This was tested using the following eBPF/XDP program attached to a network interface of the mtk_eth_soc driver. As said before, ingress_ifindex always had a value of zero. After applying the patch, ingress_ifindex holds the correct interface index.

	#include <linux/bpf.h>
	#include <bpf/bpf_helpers.h>

	SEC("pass")
	int pass_func(struct xdp_md *xdp) {
    		bpf_printk("ingress_ifindex: %u",
			xdp->ingress_ifindex);

		return XDP_PASS;
	}

	char _license[] SEC("license") = "GPL";
---
 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +0 -0
index 53485142938c..9c6d4477e536 100644

drivers/net/ethernet/mediatek/mtk_eth_soc.c static int mtk_poll_rx(struct napi_struct *napi, int budget, · line 2072

Quoted 4 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -0
@@ -2069,6 +2069,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 
 		netdev = eth->netdev[mac];
 		ppe_idx = eth->mac[mac]->ppe_idx;
+		ring->xdp_q.dev = netdev;

I guess you can set it just before running xdp_init_buff(), but the change is fine.

Regards,
Lorenzo

Quoted 7 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +0 -0
 		if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
 			goto release_desc;
Signature · 1 line
2.47.1
#3 lore raw
Quoted 39 lines · Lorenzo Bianconi
Quoted 39 lines

Currently, the network device isn't set inside the xdp_rxq_info of the mtk_rx_ring, which means that an XDP program attached to the Mediathek ethernet driver cannot retrieve the index of the interface that received the package since it's always 0 inside the xdp_md struct.

This patch sets the network device pointer inside the xdp_rxq_info struct, which is later used to initialize the xdp_buff struct via xdp_init_buff.

This was tested using the following eBPF/XDP program attached to a network interface of the mtk_eth_soc driver. As said before, ingress_ifindex always had a value of zero. After applying the patch, ingress_ifindex holds the correct interface index.

	#include <linux/bpf.h>
	#include <bpf/bpf_helpers.h>

	SEC("pass")
	int pass_func(struct xdp_md *xdp) {
    		bpf_printk("ingress_ifindex: %u",
			xdp->ingress_ifindex);

		return XDP_PASS;
	}

	char _license[] SEC("license") = "GPL";
---
 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +0 -0
index 53485142938c..9c6d4477e536 100644
@@ -2069,6 +2069,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 
Quoted 5 lines

drivers/net/ethernet/mediatek/mtk_eth_soc.c static int mtk_poll_rx(struct napi_struct *napi, int budget, · line 2072

Quoted 3 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -0
 		netdev = eth->netdev[mac];
 		ppe_idx = eth->mac[mac]->ppe_idx;
+		ring->xdp_q.dev = netdev;

I guess you can set it just before running xdp_init_buff(), but the change is fine.

Lorenzo, is it legitimate to change rxq->dev post registration like that?

I am asking because we have a similar problem [1]. In our case we also register the rxq structure with a dummy netdev which is why XDP programs see an ifindex of 0.

Thanks

[1] https://lore.kernel.org/netdev/ZzYR2ZJ1mGRq12VL@shredder/

Quoted 11 lines

Regards,
Lorenzo

Quoted 7 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +0 -0
 		if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
 			goto release_desc;
Signature · 1 line
2.47.1
maintainer
#4 lore raw
Quoted 46 lines · Lorenzo Bianconi
Quoted 39 lines
Quoted 39 lines

Currently, the network device isn't set inside the xdp_rxq_info of the mtk_rx_ring, which means that an XDP program attached to the Mediathek ethernet driver cannot retrieve the index of the interface that received the package since it's always 0 inside the xdp_md struct.

This patch sets the network device pointer inside the xdp_rxq_info struct, which is later used to initialize the xdp_buff struct via xdp_init_buff.

This was tested using the following eBPF/XDP program attached to a network interface of the mtk_eth_soc driver. As said before, ingress_ifindex always had a value of zero. After applying the patch, ingress_ifindex holds the correct interface index.

	#include <linux/bpf.h>
	#include <bpf/bpf_helpers.h>

	SEC("pass")
	int pass_func(struct xdp_md *xdp) {
    		bpf_printk("ingress_ifindex: %u",
			xdp->ingress_ifindex);

		return XDP_PASS;
	}

	char _license[] SEC("license") = "GPL";
---
 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +0 -0
index 53485142938c..9c6d4477e536 100644
@@ -2069,6 +2069,7 @@ static int mtk_poll_rx(struct napi_struct *napi, int budget,
 
Quoted 5 lines

drivers/net/ethernet/mediatek/mtk_eth_soc.c static int mtk_poll_rx(struct napi_struct *napi, int budget, · line 2072

Quoted 3 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -0
 		netdev = eth->netdev[mac];
 		ppe_idx = eth->mac[mac]->ppe_idx;
+		ring->xdp_q.dev = netdev;

I guess you can set it just before running xdp_init_buff(), but the change is fine.

Quoted 6 lines

Lorenzo, is it legitimate to change rxq->dev post registration like that?

I am asking because we have a similar problem [1]. In our case we also register the rxq structure with a dummy netdev which is why XDP programs see an ifindex of 0.

to be honest I was thinking about it but I guess the dev pointer is just used running the eBPF program.
@Jesper: any concern?

Regards,
Lorenzo

Quoted 18 lines

Thanks

[1] https://lore.kernel.org/netdev/ZzYR2ZJ1mGRq12VL@shredder/

Quoted 11 lines

Regards,
Lorenzo

Quoted 7 lines
drivers/net/ethernet/mediatek/mtk_eth_soc.c +0 -0
 		if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
 			goto release_desc;
Signature · 1 line
2.47.1
maintainer
#5 lore raw
In reply to Til Kaiser
Quoted 1 line · On Tue, 26 Nov 2024 14:41:54 +0100 Til Kaiser wrote:

[PATCH net] mediathek: mtk_eth_soc: fix netdev inside xdp_rxq_info

my $0.02:
- not a fix, rxq is not a hard requirement for XDP support
- if we want to do this we should record in the queue that netdev is "unstable" (new member?), it will make it easier to track the drivers with this behavior later on

Signature · 1 line
pw-bot: cr