mirror of
https://github.com/torvalds/linux.git
synced 2026-07-27 17:47:41 +02:00
Merge branch 'fix-broken-tc_act_redirect-from-qdiscs'
Daniel Borkmann says: ==================== Fix broken TC_ACT_REDIRECT from qdiscs This is an alternative fix to [0] in order to not uglify __dev_queue_xmit() with sprinkled ifdefs given this can be simplified and isolated through a simple test into the BPF redirect helper itself. I've also added a proper BPF selftest, so there is no need to check-in a binary BPF object into selftests given we do have BPF infra for all of this. [0] https://lore.kernel.org/netdev/20260629102157.737306-1-jhs@mojatatu.com/ [1] https://lore.kernel.org/netdev/20260629102157.737306-4-jhs@mojatatu.com/ ==================== Link: https://patch.msgid.link/20260706185609.330006-1-daniel@iogearbox.net Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
commit
1572d5862c
|
|
@ -156,8 +156,20 @@ static inline int tcf_classify(struct sk_buff *skb,
|
|||
{
|
||||
return TC_ACT_UNSPEC;
|
||||
}
|
||||
|
||||
#endif
|
||||
static inline int tcf_classify_qdisc(struct sk_buff *skb,
|
||||
const struct tcf_proto *tp,
|
||||
struct tcf_result *res, bool compat_mode)
|
||||
{
|
||||
int ret = tcf_classify(skb, NULL, tp, res, compat_mode);
|
||||
|
||||
/* TC_ACT_REDIRECT from qdisc filter chains is not supported.
|
||||
* Use BPF via tcx or mirred redirect instead.
|
||||
*/
|
||||
if (unlikely(ret == TC_ACT_REDIRECT))
|
||||
ret = TC_ACT_SHOT;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline unsigned long
|
||||
__cls_set_class(unsigned long *clp, unsigned long cl)
|
||||
|
|
|
|||
|
|
@ -2552,11 +2552,13 @@ int skb_do_redirect(struct sk_buff *skb)
|
|||
|
||||
BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
|
||||
{
|
||||
struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
|
||||
struct bpf_redirect_info *ri;
|
||||
|
||||
if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
|
||||
if (unlikely(!bpf_net_ctx_get() ||
|
||||
(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL))))
|
||||
return TC_ACT_SHOT;
|
||||
|
||||
ri = bpf_net_ctx_get_ri();
|
||||
ri->flags = flags;
|
||||
ri->tgt_index = ifindex;
|
||||
|
||||
|
|
@ -2573,11 +2575,12 @@ static const struct bpf_func_proto bpf_redirect_proto = {
|
|||
|
||||
BPF_CALL_2(bpf_redirect_peer, u32, ifindex, u64, flags)
|
||||
{
|
||||
struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
|
||||
struct bpf_redirect_info *ri;
|
||||
|
||||
if (unlikely(flags))
|
||||
if (unlikely(!bpf_net_ctx_get() || flags))
|
||||
return TC_ACT_SHOT;
|
||||
|
||||
ri = bpf_net_ctx_get_ri();
|
||||
ri->flags = BPF_F_PEER;
|
||||
ri->tgt_index = ifindex;
|
||||
|
||||
|
|
@ -2595,11 +2598,13 @@ static const struct bpf_func_proto bpf_redirect_peer_proto = {
|
|||
BPF_CALL_4(bpf_redirect_neigh, u32, ifindex, struct bpf_redir_neigh *, params,
|
||||
int, plen, u64, flags)
|
||||
{
|
||||
struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
|
||||
struct bpf_redirect_info *ri;
|
||||
|
||||
if (unlikely((plen && plen < sizeof(*params)) || flags))
|
||||
if (unlikely((plen && plen < sizeof(*params)) ||
|
||||
!bpf_net_ctx_get() || flags))
|
||||
return TC_ACT_SHOT;
|
||||
|
||||
ri = bpf_net_ctx_get_ri();
|
||||
ri->flags = BPF_F_NEIGH | (plen ? BPF_F_NEXTHOP : 0);
|
||||
ri->tgt_index = ifindex;
|
||||
|
||||
|
|
|
|||
|
|
@ -4045,7 +4045,7 @@ struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, stru
|
|||
|
||||
fl = rcu_dereference_bh(qe->filter_chain);
|
||||
|
||||
switch (tcf_classify(skb, NULL, fl, &cl_res, false)) {
|
||||
switch (tcf_classify_qdisc(skb, fl, &cl_res, false)) {
|
||||
case TC_ACT_SHOT:
|
||||
qdisc_qstats_drop(sch);
|
||||
__qdisc_drop(skb, to_free);
|
||||
|
|
@ -4057,10 +4057,6 @@ struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, stru
|
|||
__qdisc_drop(skb, to_free);
|
||||
*ret = __NET_XMIT_STOLEN;
|
||||
return NULL;
|
||||
case TC_ACT_REDIRECT:
|
||||
skb_do_redirect(skb);
|
||||
*ret = __NET_XMIT_STOLEN;
|
||||
return NULL;
|
||||
case TC_ACT_CONSUMED:
|
||||
*ret = __NET_XMIT_STOLEN;
|
||||
return NULL;
|
||||
|
|
|
|||
|
|
@ -1727,7 +1727,7 @@ static u32 cake_classify(struct Qdisc *sch, struct cake_tin_data **t,
|
|||
goto hash;
|
||||
|
||||
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
||||
result = tcf_classify(skb, NULL, filter, &res, false);
|
||||
result = tcf_classify_qdisc(skb, filter, &res, false);
|
||||
|
||||
if (result >= 0) {
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
|
|
|
|||
|
|
@ -312,7 +312,7 @@ static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch,
|
|||
|
||||
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
||||
fl = rcu_dereference_bh(q->filter_list);
|
||||
result = tcf_classify(skb, NULL, fl, &res, false);
|
||||
result = tcf_classify_qdisc(skb, fl, &res, false);
|
||||
if (result >= 0) {
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (result) {
|
||||
|
|
|
|||
|
|
@ -364,7 +364,7 @@ static int dualpi2_skb_classify(struct dualpi2_sched_data *q,
|
|||
if (!fl)
|
||||
return NET_XMIT_SUCCESS;
|
||||
|
||||
result = tcf_classify(skb, NULL, fl, &res, false);
|
||||
result = tcf_classify_qdisc(skb, fl, &res, false);
|
||||
if (result >= 0) {
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (result) {
|
||||
|
|
|
|||
|
|
@ -391,7 +391,7 @@ static struct ets_class *ets_classify(struct sk_buff *skb, struct Qdisc *sch,
|
|||
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
||||
if (TC_H_MAJ(skb->priority) != sch->handle) {
|
||||
fl = rcu_dereference_bh(q->filter_list);
|
||||
err = tcf_classify(skb, NULL, fl, &res, false);
|
||||
err = tcf_classify_qdisc(skb, fl, &res, false);
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (err) {
|
||||
case TC_ACT_STOLEN:
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch,
|
|||
return fq_codel_hash(q, skb) + 1;
|
||||
|
||||
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
||||
result = tcf_classify(skb, NULL, filter, &res, false);
|
||||
result = tcf_classify_qdisc(skb, filter, &res, false);
|
||||
if (result >= 0) {
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (result) {
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ static unsigned int fq_pie_classify(struct sk_buff *skb, struct Qdisc *sch,
|
|||
return fq_pie_hash(q, skb) + 1;
|
||||
|
||||
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
||||
result = tcf_classify(skb, NULL, filter, &res, false);
|
||||
result = tcf_classify_qdisc(skb, filter, &res, false);
|
||||
if (result >= 0) {
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (result) {
|
||||
|
|
|
|||
|
|
@ -1143,7 +1143,7 @@ hfsc_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
|
|||
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
||||
head = &q->root;
|
||||
tcf = rcu_dereference_bh(q->root.filter_list);
|
||||
while (tcf && (result = tcf_classify(skb, NULL, tcf, &res, false)) >= 0) {
|
||||
while (tcf && (result = tcf_classify_qdisc(skb, tcf, &res, false)) >= 0) {
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (result) {
|
||||
case TC_ACT_QUEUED:
|
||||
|
|
|
|||
|
|
@ -243,7 +243,7 @@ static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch,
|
|||
}
|
||||
|
||||
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
||||
while (tcf && (result = tcf_classify(skb, NULL, tcf, &res, false)) >= 0) {
|
||||
while (tcf && (result = tcf_classify_qdisc(skb, tcf, &res, false)) >= 0) {
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (result) {
|
||||
case TC_ACT_QUEUED:
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ multiq_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
|
|||
int err;
|
||||
|
||||
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
||||
err = tcf_classify(skb, NULL, fl, &res, false);
|
||||
err = tcf_classify_qdisc(skb, fl, &res, false);
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (err) {
|
||||
case TC_ACT_STOLEN:
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
|
|||
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
||||
if (TC_H_MAJ(skb->priority) != sch->handle) {
|
||||
fl = rcu_dereference_bh(q->filter_list);
|
||||
err = tcf_classify(skb, NULL, fl, &res, false);
|
||||
err = tcf_classify_qdisc(skb, fl, &res, false);
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (err) {
|
||||
case TC_ACT_STOLEN:
|
||||
|
|
|
|||
|
|
@ -709,7 +709,7 @@ static struct qfq_class *qfq_classify(struct sk_buff *skb, struct Qdisc *sch,
|
|||
|
||||
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
||||
fl = rcu_dereference_bh(q->filter_list);
|
||||
result = tcf_classify(skb, NULL, fl, &res, false);
|
||||
result = tcf_classify_qdisc(skb, fl, &res, false);
|
||||
if (result >= 0) {
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (result) {
|
||||
|
|
|
|||
|
|
@ -260,7 +260,7 @@ static bool sfb_classify(struct sk_buff *skb, struct tcf_proto *fl,
|
|||
struct tcf_result res;
|
||||
int result;
|
||||
|
||||
result = tcf_classify(skb, NULL, fl, &res, false);
|
||||
result = tcf_classify_qdisc(skb, fl, &res, false);
|
||||
if (result >= 0) {
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (result) {
|
||||
|
|
|
|||
|
|
@ -171,7 +171,7 @@ static unsigned int sfq_classify(struct sk_buff *skb, struct Qdisc *sch,
|
|||
return sfq_hash(q, skb) + 1;
|
||||
|
||||
*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
|
||||
result = tcf_classify(skb, NULL, fl, &res, false);
|
||||
result = tcf_classify_qdisc(skb, fl, &res, false);
|
||||
if (result >= 0) {
|
||||
#ifdef CONFIG_NET_CLS_ACT
|
||||
switch (result) {
|
||||
|
|
|
|||
|
|
@ -82,6 +82,7 @@ CONFIG_NET_SCH_BPF=y
|
|||
CONFIG_NET_SCH_FQ=y
|
||||
CONFIG_NET_SCH_INGRESS=y
|
||||
CONFIG_NET_SCH_HTB=y
|
||||
CONFIG_NET_SCH_RED=y
|
||||
CONFIG_NET_SCHED=y
|
||||
CONFIG_NETDEVSIM=y
|
||||
CONFIG_NETFILTER=y
|
||||
|
|
|
|||
113
tools/testing/selftests/bpf/prog_tests/tc_qevent.c
Normal file
113
tools/testing/selftests/bpf/prog_tests/tc_qevent.c
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
#include <test_progs.h>
|
||||
#include <network_helpers.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "test_tc_qevent.skel.h"
|
||||
|
||||
#define NS_TX "tc_qevent_tx"
|
||||
#define NS_RX "tc_qevent_rx"
|
||||
#define IP_TX "10.255.0.1"
|
||||
#define IP_RX "10.255.0.2"
|
||||
#define PIN_PATH "/sys/fs/bpf/tc_qevent_redirect"
|
||||
|
||||
static void blast_udp(void)
|
||||
{
|
||||
struct sockaddr_in dst = {};
|
||||
char buf[1400] = {};
|
||||
int fd, i;
|
||||
|
||||
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (!ASSERT_GE(fd, 0, "udp socket"))
|
||||
return;
|
||||
|
||||
dst.sin_family = AF_INET;
|
||||
dst.sin_port = htons(12345);
|
||||
inet_pton(AF_INET, IP_RX, &dst.sin_addr);
|
||||
|
||||
/*
|
||||
* Push far more than the RED queue can hold. Once qavg crosses qth_min
|
||||
* every further packet hits the congestion_drop / early_drop qevent.
|
||||
*/
|
||||
for (i = 0; i < 50000; i++)
|
||||
sendto(fd, buf, sizeof(buf), MSG_DONTWAIT,
|
||||
(struct sockaddr *)&dst, sizeof(dst));
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static void run_qevent_redirect(struct bpf_program *prog, __u64 *counter)
|
||||
{
|
||||
struct nstoken *tok = NULL;
|
||||
int err;
|
||||
|
||||
SYS_NOFAIL("ip netns del %s", NS_TX);
|
||||
SYS_NOFAIL("ip netns del %s", NS_RX);
|
||||
unlink(PIN_PATH);
|
||||
|
||||
err = bpf_program__pin(prog, PIN_PATH);
|
||||
if (!ASSERT_OK(err, "pin prog"))
|
||||
return;
|
||||
|
||||
SYS(unpin, "ip netns add %s", NS_TX);
|
||||
SYS(del_tx, "ip netns add %s", NS_RX);
|
||||
SYS(del_rx, "ip -n %s link add veth0 type veth peer name veth1 netns %s", NS_TX, NS_RX);
|
||||
SYS(del_rx, "ip -n %s addr add %s/24 dev veth0", NS_TX, IP_TX);
|
||||
SYS(del_rx, "ip -n %s link set veth0 up", NS_TX);
|
||||
SYS(del_rx, "ip -n %s addr add %s/24 dev veth1", NS_RX, IP_RX);
|
||||
SYS(del_rx, "ip -n %s link set veth1 up", NS_RX);
|
||||
|
||||
tok = open_netns(NS_TX);
|
||||
if (!ASSERT_OK_PTR(tok, "open_netns"))
|
||||
goto del_rx;
|
||||
|
||||
SYS(close_ns, "tc qdisc add dev veth0 root handle 1: htb default 1");
|
||||
SYS(close_ns, "tc class add dev veth0 parent 1: classid 1:1 htb rate 1mbit ceil 1mbit");
|
||||
|
||||
if (system("tc qdisc add dev veth0 parent 1:1 handle 11: red "
|
||||
"limit 500000 avpkt 1000 probability 1 min 5000 max 6000 "
|
||||
"burst 6 qevent early_drop block 10 2>/dev/null")) {
|
||||
test__skip();
|
||||
goto close_ns;
|
||||
}
|
||||
|
||||
if (system("tc filter add block 10 bpf da object-pinned "
|
||||
PIN_PATH " 2>/dev/null")) {
|
||||
test__skip();
|
||||
goto close_ns;
|
||||
}
|
||||
|
||||
blast_udp();
|
||||
ASSERT_GT(*counter, 0, "qevent classifier ran");
|
||||
close_ns:
|
||||
close_netns(tok);
|
||||
del_rx:
|
||||
SYS_NOFAIL("ip netns del %s", NS_RX);
|
||||
del_tx:
|
||||
SYS_NOFAIL("ip netns del %s", NS_TX);
|
||||
unpin:
|
||||
bpf_program__unpin(prog, PIN_PATH);
|
||||
}
|
||||
|
||||
void test_tc_qevent(void)
|
||||
{
|
||||
struct test_tc_qevent *skel;
|
||||
|
||||
skel = test_tc_qevent__open_and_load();
|
||||
if (!ASSERT_OK_PTR(skel, "open_and_load"))
|
||||
return;
|
||||
|
||||
if (test__start_subtest("redirect_verdict"))
|
||||
run_qevent_redirect(skel->progs.qevent_redirect_verdict,
|
||||
&skel->bss->verdict_calls);
|
||||
if (test__start_subtest("redirect_helper"))
|
||||
run_qevent_redirect(skel->progs.qevent_redirect_helper,
|
||||
&skel->bss->helper_calls);
|
||||
|
||||
test_tc_qevent__destroy(skel);
|
||||
}
|
||||
23
tools/testing/selftests/bpf/progs/test_tc_qevent.c
Normal file
23
tools/testing/selftests/bpf/progs/test_tc_qevent.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
#include "vmlinux.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
int redirect_ifindex = 1;
|
||||
__u64 verdict_calls = 0;
|
||||
__u64 helper_calls = 0;
|
||||
|
||||
SEC("tc")
|
||||
int qevent_redirect_verdict(struct __sk_buff *skb)
|
||||
{
|
||||
__sync_fetch_and_add(&verdict_calls, 1);
|
||||
return TCX_REDIRECT;
|
||||
}
|
||||
|
||||
SEC("tc")
|
||||
int qevent_redirect_helper(struct __sk_buff *skb)
|
||||
{
|
||||
__sync_fetch_and_add(&helper_calls, 1);
|
||||
return bpf_redirect(redirect_ifindex, 0);
|
||||
}
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
||||
Loading…
Reference in New Issue
Block a user