netkit: Fix setting mac address in l2 mode
Subsystems (6)
Daniel Borkmann
maintainerPATCH v2 1/4 netkit: Fix setting mac address in l2 mode
When running Cilium connectivity test suite with netkit in L2 mode, we found that it is expected to be able to specify a custom MAC address for the devices, in particular, cilium-cni obtains the specified MAC address by querying the endpoint and sets the MAC address of the interface inside the Pod. Thus, fix the missing support in netkit for L2 mode.
--- drivers/net/netkit.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
drivers/net/netkit.c +21 -5
@@ -155,6 +155,16 @@ static void netkit_set_multicast(struct net_device *dev)/* Nothing to do, we receive whatever gets pushed to us! */ } +static int netkit_set_macaddr (struct net_device *dev,void *sa) +{ +struct netkit *nk =netkit_priv (dev); + +if (nk->mode !=NETKIT_L2 ) +return -EOPNOTSUPP ; + +return eth_mac_addr (dev, sa); +} +static void netkit_set_headroom (struct net_device *dev,int headroom) {struct netkit *nk =netkit_priv (dev), *nk2;
@@ -198,6 +208,7 @@ static const struct net_device_ops netkit_netdev_ops = { .ndo_start_xmit = netkit_xmit, .ndo_set_rx_mode = netkit_set_multicast, .ndo_set_rx_headroom = netkit_set_headroom, + .ndo_set_mac_address = netkit_set_macaddr, .ndo_get_iflink = netkit_get_iflink, .ndo_get_peer_dev = netkit_peer_dev, .ndo_get_stats64 = netkit_get_stats,
@@ -300,9 +311,11 @@ static int netkit_validate(struct nlattr *tb[], struct nlattr *data[],if (!attr)return 0 ; -NL_SET_ERR_MSG_ATTR (extack, attr, -"Setting Ethernet address is not supported" ); -return -EOPNOTSUPP ; +if (nla_len (attr) !=ETH_ALEN ) +return -EINVAL ; +if (!is_valid_ether_addr (nla_data (attr))) +return -EADDRNOTAVAIL ; +return 0 ; }static struct rtnl_link_ops netkit_link_ops;
@@ -365,6 +378,9 @@ static int netkit_new_link(struct net *src_net, struct net_device *dev,strscpy (ifname,"nk%d" ,IFNAMSIZ ); ifname_assign_type =NET_NAME_ENUM ; } +if (mode !=NETKIT_L2 && + (tb[IFLA_ADDRESS ] || tbp[IFLA_ADDRESS ])) +return -EOPNOTSUPP ; net =rtnl_link_get_net (src_net, tbp);if (IS_ERR (net))
@@ -379,7 +395,7 @@ static int netkit_new_link(struct net *src_net, struct net_device *dev,netif_inherit_tso_max (peer, dev); -if (mode ==NETKIT_L2 ) +if (mode ==NETKIT_L2 && !(ifmp && tbp[IFLA_ADDRESS ]))eth_hw_addr_random (peer);if (ifmp && dev->ifindex) peer->ifindex = ifmp->ifi_index;
@@ -402,7 +418,7 @@ static int netkit_new_link(struct net *src_net, struct net_device *dev,if (err <0 ) goto err_configure_peer; -if (mode ==NETKIT_L2 ) +if (mode ==NETKIT_L2 && !tb[IFLA_ADDRESS ])eth_hw_addr_random (dev);if (tb[IFLA_IFNAME ])nla_strscpy (dev->name, tb[IFLA_IFNAME ],IFNAMSIZ );
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerPATCH v2 2/4 netkit: Fix pkt_type override upon netkit pass verdict
When running Cilium connectivity test suite with netkit in L2 mode, we found that compared to tcx a few tests were failing which pushed traffic into an L7 proxy sitting in host namespace. The problem in particular is around the invocation of eth_type_trans() in netkit.
In case of tcx, this is run before the tcx ingress is triggered inside host namespace and thus if the BPF program uses the bpf_skb_change_type() helper the newly set type is retained. However, in case of netkit, the late eth_type_trans() invocation overrides the earlier decision from the BPF program which eventually leads to the test failure.
Instead of eth_type_trans(), split out the relevant parts, meaning, reset of mac header and call to eth_skb_pkt_type() before the BPF program is run in order to have the same behavior as with tcx, and refactor a small helper called eth_skb_pull_mac() which is run in case it's passed up the stack where the mac header must be pulled. With this all connectivity tests pass.
--- drivers/net/netkit.c | 4 +++- include/linux/etherdevice.h | 8 ++++++++ net/ethernet/eth.c | 4 +--- 3 files changed, 12 insertions(+), 4 deletions(-)
drivers/net/netkit.c +3 -1
@@ -55,6 +55,7 @@ static void netkit_prep_forward(struct sk_buff *skb, bool xnet)skb_scrub_packet (skb, xnet); skb->priority =0 ;nf_skip_egress (skb, true); +skb_reset_mac_header (skb); }static struct netkit *netkit_priv (const struct net_device *dev)
@@ -78,6 +79,7 @@ static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev) skb_orphan_frags(skb, GFP_ATOMIC))) goto drop;netkit_prep_forward (skb, !net_eq (dev_net (dev),dev_net (peer))); +eth_skb_pkt_type (skb, peer); skb->dev = peer; entry =rcu_dereference (nk->active);if (entry)
@@ -85,7 +87,7 @@ static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev)switch (ret) {case NETKIT_NEXT :case NETKIT_PASS : - skb->protocol =eth_type_trans (skb, skb->dev); +eth_skb_pull_mac (skb);skb_postpull_rcsum (skb,eth_hdr (skb),ETH_HLEN );if (likely (__netif_rx (skb) ==NET_RX_SUCCESS )) {dev_sw_netstats_tx_add (dev,1 , len);
include/linux/etherdevice.h +8 -0
@@ -636,6 +636,14 @@ static inline void eth_skb_pkt_type(struct sk_buff *skb, } } +static inline struct ethhdr *eth_skb_pull_mac (struct sk_buff *skb) +{ +struct ethhdr *eth = (struct ethhdr *)skb->data; + +skb_pull_inline (skb,ETH_HLEN ); +return eth; +} +/** * eth_skb_pad - Pad buffer to mininum number of octets for Ethernet frame * @skb: Buffer to pad
net/ethernet/eth.c +1 -3
@@ -161,9 +161,7 @@ __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev) skb->dev = dev;skb_reset_mac_header (skb); - eth = (struct ethhdr *)skb->data; -skb_pull_inline (skb,ETH_HLEN ); - + eth =eth_skb_pull_mac (skb);eth_skb_pkt_type (skb, dev);/*
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerPATCH v2 3/4 selftests/bpf: Add netkit tests for mac address
In reply to Daniel BorkmannThis adds simple tests around setting MAC addresses in the different netkit modes.
--- tools/testing/selftests/bpf/prog_tests/tc_netkit.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
tools/testing/selftests/bpf/prog_tests/tc_netkit.c +10 -0
@@ -73,6 +73,16 @@ static int create_netkit(int mode, int policy, int peer_policy, int *ifindex, "up primary");ASSERT_OK (system ("ip addr add dev " netkit_name" 10.0.0.1/24" ),"addr primary" ); + +if (mode ==NETKIT_L3 ) { +ASSERT_EQ (system ("ip link set dev " netkit_name +" addr ee:ff:bb:cc:aa:dd 2> /dev/null" ),512 , +"set hwaddress" ); + }else { +ASSERT_OK (system ("ip link set dev " netkit_name +" addr ee:ff:bb:cc:aa:dd" ), +"set hwaddress" ); + }if (same_netns) {ASSERT_OK (system ("ip link set dev " netkit_peer" up" ),"up peer" );
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerPATCH v2 4/4 selftests/bpf: Add netkit test for pkt_type
In reply to Daniel BorkmannAdd a test case to assert that the skb->pkt_type which was set from the BPF program is retained from the netkit xmit side to the peer's device at tcx ingress location.
# ./vmtest.sh -- ./test_progs -t netkit [...] ./test_progs -t netkit [ 1.140780] bpf_testmod: loading out-of-tree module taints kernel. [ 1.141127] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel [ 1.284601] tsc: Refined TSC clocksource calibration: 3408.006 MHz [ 1.286672] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x311fd9b189d, max_idle_ns: 440795225691 ns [ 1.290384] clocksource: Switched to clocksource tsc #345 tc_netkit_basic:OK #346 tc_netkit_device:OK #347 tc_netkit_multi_links:OK #348 tc_netkit_multi_opts:OK #349 tc_netkit_neigh_links:OK #350 tc_netkit_pkt_type:OK Summary: 6/0 PASSED, 0 SKIPPED, 0 FAILED
--- .../selftests/bpf/prog_tests/tc_netkit.c | 84 +++++++++++++++++++ .../selftests/bpf/progs/test_tc_link.c | 33 ++++++++ 2 files changed, 117 insertions(+)
tools/testing/selftests/bpf/prog_tests/tc_netkit.c +84 -0
@@ -99,6 +99,16 @@ static int create_netkit(int mode, int policy, int peer_policy, int *ifindex,return err; } +static void move_netkit (void ) +{ +ASSERT_OK (system ("ip link set " netkit_peer" netns foo" ), +"move peer" ); +ASSERT_OK (system ("ip netns exec foo ip link set dev " + netkit_peer" up" ),"up peer" ); +ASSERT_OK (system ("ip netns exec foo ip addr add dev " + netkit_peer" 10.0.0.2/24" ),"addr peer" ); +} +static void destroy_netkit (void ) {ASSERT_OK (system ("ip link del dev " netkit_name),"del primary" );
@@ -695,3 +705,77 @@ void serial_test_tc_netkit_neigh_links(void)serial_test_tc_netkit_neigh_links_target (NETKIT_L2 ,BPF_NETKIT_PRIMARY );serial_test_tc_netkit_neigh_links_target (NETKIT_L3 ,BPF_NETKIT_PRIMARY ); } + +static void serial_test_tc_netkit_pkt_type_mode (int mode) +{ +LIBBPF_OPTS (bpf_netkit_opts, optl_nk); +LIBBPF_OPTS (bpf_tcx_opts, optl_tcx); +int err, ifindex, ifindex2; +struct test_tc_link *skel; +struct bpf_link *link; + + err =create_netkit (mode,NETKIT_PASS ,NETKIT_PASS , + &ifindex, true); +if (err) +return ; + + ifindex2 =if_nametoindex (netkit_peer); +ASSERT_NEQ (ifindex, ifindex2,"ifindex_1_2" ); + + skel =test_tc_link__open (); +if (!ASSERT_OK_PTR (skel,"skel_open" )) + goto cleanup; + +ASSERT_EQ (bpf_program__set_expected_attach_type (skel->progs.tc1, +BPF_NETKIT_PRIMARY ),0 ,"tc1_attach_type" ); +ASSERT_EQ (bpf_program__set_expected_attach_type (skel->progs.tc7, +BPF_TCX_INGRESS ),0 ,"tc7_attach_type" ); + + err =test_tc_link__load (skel); +if (!ASSERT_OK (err,"skel_load" )) + goto cleanup; + +assert_mprog_count_ifindex (ifindex,BPF_NETKIT_PRIMARY ,0 ); +assert_mprog_count_ifindex (ifindex2,BPF_TCX_INGRESS ,0 ); + + link =bpf_program__attach_netkit (skel->progs.tc1, ifindex, &optl_nk); +if (!ASSERT_OK_PTR (link,"link_attach" )) + goto cleanup; + + skel->links.tc1 = link; + +assert_mprog_count_ifindex (ifindex,BPF_NETKIT_PRIMARY ,1 ); +assert_mprog_count_ifindex (ifindex2,BPF_TCX_INGRESS ,0 ); + + link =bpf_program__attach_tcx (skel->progs.tc7, ifindex2, &optl_tcx); +if (!ASSERT_OK_PTR (link,"link_attach" )) + goto cleanup; + + skel->links.tc7 = link; + +assert_mprog_count_ifindex (ifindex,BPF_NETKIT_PRIMARY ,1 ); +assert_mprog_count_ifindex (ifindex2,BPF_TCX_INGRESS ,1 ); + +move_netkit (); + +tc_skel_reset_all_seen (skel); + skel->bss->set_type = true; +ASSERT_EQ (send_icmp (),0 ,"icmp_pkt" ); + +ASSERT_EQ (skel->bss->seen_tc1, true,"seen_tc1" ); +ASSERT_EQ (skel->bss->seen_tc7, true,"seen_tc7" ); + +ASSERT_EQ (skel->bss->seen_host, true,"seen_host" ); +ASSERT_EQ (skel->bss->seen_mcast, true,"seen_mcast" ); +cleanup: +test_tc_link__destroy (skel); + +assert_mprog_count_ifindex (ifindex,BPF_NETKIT_PRIMARY ,0 ); +destroy_netkit (); +} + +void serial_test_tc_netkit_pkt_type (void ) +{ +serial_test_tc_netkit_pkt_type_mode (NETKIT_L2 ); +serial_test_tc_netkit_pkt_type_mode (NETKIT_L3 ); +}
tools/testing/selftests/bpf/progs/test_tc_link.c +33 -0
@@ -4,6 +4,7 @@#include <linux/bpf.h> #include <linux/if_ether.h> +#include <linux/if_packet.h> #include <bpf/bpf_endian.h> #include <bpf/bpf_helpers.h>
@@ -16,7 +17,13 @@ bool seen_tc3;bool seen_tc4;bool seen_tc5;bool seen_tc6; +bool seen_tc7; + +bool set_type; +bool seen_eth; +bool seen_host; +bool seen_mcast; SEC("tc/ingress")int tc1 (struct __sk_buff *skb)
@@ -28,8 +35,16 @@ int tc1(struct __sk_buff *skb)if (bpf_skb_load_bytes (skb,0 , ð,sizeof (eth))) goto out; seen_eth = eth.h_proto ==bpf_htons (ETH_P_IP ); + seen_host = skb->pkt_type ==PACKET_HOST ; +if (seen_host && set_type) { + eth.h_dest[0 ] =4 ; +if (bpf_skb_store_bytes (skb,0 , ð,sizeof (eth),0 )) + goto fail; +bpf_skb_change_type (skb,PACKET_MULTICAST ); + } out: seen_tc1 = true; +fail:return TCX_NEXT ; }
@@ -67,3 +82,21 @@ int tc6(struct __sk_buff *skb) seen_tc6 = true;return TCX_PASS ; } + +SEC("tc/ingress") +int tc7 (struct __sk_buff *skb) +{ +struct ethhdr eth = {}; + +if (skb->protocol !=__bpf_constant_htons (ETH_P_IP )) + goto out; +if (bpf_skb_load_bytes (skb,0 , ð,sizeof (eth))) + goto out; +if (eth.h_dest[0 ] ==4 && set_type) { + seen_mcast = skb->pkt_type ==PACKET_MULTICAST ; +bpf_skb_change_type (skb,PACKET_HOST ); + } +out: + seen_tc7 = true; +return TCX_PASS ; +}
Signature · 1 line
2.34.1
Martin KaFai Lau
reviewerRe: [PATCH bpf v2 4/4] selftests/bpf: Add netkit test for pkt_type
tools/testing/selftests/bpf/progs/test_tc_link.c line 7
Quoted 9 lines · Daniel Borkmann
tools/testing/selftests/bpf/progs/test_tc_link.c +1
-0@@ -4,6 +4,7 @@#include <linux/bpf.h> #include <linux/if_ether.h> +#include <linux/if_packet.h>
The set looks good.
A minor thing is that I am hitting this compilation issue in my environment:
In file included from progs/test_tc_link.c:7:
In file included from /usr/include/linux/if_packet.h:5:
In file included from /usr/include/asm/byteorder.h:5:
In file included from /usr/include/linux/byteorder/little_endian.h:13:/usr/include/linux/swab.h:136:8: error: unknown type name '__always_inline'
136 | static __always_inline unsigned long __swab(const unsigned long y)
| ^
Adding '#include <linux/stddef.h>' solved it. If the addition is fine, this can be adjusted before landing.
Re: [PATCH bpf v2 4/4] selftests/bpf: Add netkit test for pkt_type
Quoted 24 lines · Martin KaFai Lau
tools/testing/selftests/bpf/progs/test_tc_link.c line 7
Quoted 9 lines · Daniel Borkmann
tools/testing/selftests/bpf/progs/test_tc_link.c +1
-0@@ -4,6 +4,7 @@#include <linux/bpf.h> #include <linux/if_ether.h> +#include <linux/if_packet.h> The set looks good.
A minor thing is that I am hitting this compilation issue in my environment:
In file included from
progs/test_tc_link.c:7:
In file included from/usr/include/linux/if_packet.h:5:
In file included from/usr/include/asm/byteorder.h:5:
In file included from/usr/include/linux/byteorder/little_endian.h:13:/usr/include/linux/swab.h:136:8: error: unknown type name '__always_inline'
136 | static__always_inlineunsigned long__swab(const unsigned long y)
| ^
Quoted 2 lines
Adding '#include <
linux/stddef.h>' solved it. If the addition is fine, this can be adjusted before landing.
I hit the same build issue. So added stddef.h before if_packet.h as Martin suggested before applying.
Daniel Borkmann
maintainerRe: [PATCH bpf v2 4/4] selftests/bpf: Add netkit test for pkt_type
Quoted 25 lines · Alexei Starovoitov
Quoted 24 lines · Martin KaFai Lau
tools/testing/selftests/bpf/progs/test_tc_link.c line 7
Quoted 9 lines · Daniel Borkmann
tools/testing/selftests/bpf/progs/test_tc_link.c +1
-0@@ -4,6 +4,7 @@#include <linux/bpf.h> #include <linux/if_ether.h> +#include <linux/if_packet.h> The set looks good.
A minor thing is that I am hitting this compilation issue in my environment:
In file included from
progs/test_tc_link.c:7:
In file included from/usr/include/linux/if_packet.h:5:
In file included from/usr/include/asm/byteorder.h:5:
In file included from/usr/include/linux/byteorder/little_endian.h:13:/usr/include/linux/swab.h:136:8: error: unknown type name '__always_inline'
136 | static__always_inlineunsigned long__swab(const unsigned long y)
| ^
Quoted 5 lines
Quoted 2 lines
Adding '#include <
linux/stddef.h>' solved it. If the addition is fine, this can be adjusted before landing.I hit the same build issue. So added
stddef.hbeforeif_packet.has Martin suggested before applying.
Awesome, thanks all!
Quoted 9 lines · Daniel Borkmann
When running Cilium connectivity test suite with netkit in L2 mode, we found that it is expected to be able to specify a custom MAC address for the devices, in particular, cilium-cni obtains the specified MAC address by querying the endpoint and sets the MAC address of the interface inside the Pod. Thus, fix the missing support in netkit for L2 mode.
Fixes 35dfaad7188c ("netkit, bpf: Add bpf programmable net device")Signed-off-by Daniel Borkmann <[email protected]>
For the series:
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:
Quoted 11 lines · On Fri, 24 May 2024 18:36:16 +0200 you wrote:
When running Cilium connectivity test suite with netkit in L2 mode, we found that it is expected to be able to specify a custom MAC address for the devices, in particular, cilium-cni obtains the specified MAC address by querying the endpoint and sets the MAC address of the interface inside the Pod. Thus, fix the missing support in netkit for L2 mode.
Fixes 35dfaad7188c ("netkit, bpf: Add bpf programmable net device")Signed-off-by Daniel Borkmann <[email protected]>[...]
Here is the summary with links:
- [bpf,v2,1/4] netkit: Fix setting mac address in l2 mode
https://git.kernel.org/bpf/bpf/c/d6fe532b7499 - [bpf,v2,2/4] netkit: Fix pkt_type override upon netkit pass verdict https://git.kernel.org/bpf/bpf/c/3998d184267d - [bpf,v2,3/4] selftests/bpf: Add netkit tests for mac address https://git.kernel.org/bpf/bpf/c/998ffeb2738e - [bpf,v2,4/4] selftests/bpf: Add netkit test for pkt_type https://git.kernel.org/bpf/bpf/c/95348e463eab
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html