// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/* * Test suite of lwt BPF programs that reroutes packets * The file tests focus not only if these programs work as expected normally, * but also if they can handle abnormal situations gracefully. This test * suite currently only covers lwt_xmit hook. lwt_in tests have not been * implemented. * * WARNING * ------- * This test suite can crash the kernel, thus should be run in a VM. * * Setup: * --------- * all tests are performed in a single netns. A lwt encap route is setup for * each subtest: * * ip route add 10.0.0.0/24 encap bpf xmit <obj> sec "<section_N>" dev link_err * * Here <obj> is statically defined to test_lwt_reroute.bpf.o, and it contains * a single test program entry. This program sets packet mark by last byte of * the IPv4 daddr. For example, a packet going to 1.2.3.4 will receive a skb * mark 4. A packet will only be marked once, and IP x.x.x.0 will be skipped * to avoid route loop. We didn't use generated BPF skeleton since the * attachment for lwt programs are not supported by libbpf yet. * * The test program will bring up a tun device, and sets up the following * routes: * * ip rule add pref 100 from all fwmark <tun_index> lookup 100 * ip route add table 100 default dev tun0 * * For normal testing, a ping command is running in the test netns: * * ping 10.0.0.<tun_index> -c 1 -w 1 -s 100 * * For abnormal testing, fq is used as the qdisc of the tun device. Then a UDP * socket will try to overflow the fq queue and trigger qdisc drop error. * * Scenarios: * -------------------------------- * 1. Reroute to a running tun device * 2. Reroute to a device where qdisc drop * * For case 1, ping packets should be received by the tun device. * * For case 2, force UDP packets to overflow fq limit. As long as kernel * is not crashed, it is considered successful.
*/ #define NETNS "ns_lwt_reroute" #include <netinet/in.h> #include"lwt_helpers.h" #include"network_helpers.h" #include <linux/net_tstamp.h>
/* send a ping to be rerouted to the target device */ staticvoid ping_once(constchar *ip)
{ /* We won't get a reply. Don't fail here */
SYS_NOFAIL("ping %s -c1 -W1 -s %d",
ip, ICMP_PAYLOAD_SIZE);
}
/* Send snd_target UDP packets to overflow the fq queue and trigger qdisc drop * error. This is done via TX tstamp to force buffering delayed packets.
*/ staticint overflow_fq(int snd_target, constchar *target_ip)
{ struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(1234),
};
char data_buf[8]; /* only #pkts matter, so use a random small buffer */ char control_buf[CMSG_SPACE(sizeof(uint64_t))]; struct iovec iov = {
.iov_base = data_buf,
.iov_len = sizeof(data_buf),
}; int err = -1; int s = -1; struct sock_txtime txtime_on = {
.clockid = CLOCK_MONOTONIC,
.flags = 0,
}; struct msghdr msg = {
.msg_name = &addr,
.msg_namelen = sizeof(addr),
.msg_control = control_buf,
.msg_controllen = sizeof(control_buf),
.msg_iovlen = 1,
.msg_iov = &iov,
}; struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
memset(data_buf, 0, sizeof(data_buf));
s = socket(AF_INET, SOCK_DGRAM, 0); if (!ASSERT_GE(s, 0, "socket")) goto out;
/* we will intentionally send more than fq limit, so ignore * the error here.
*/
sendmsg(s, &msg, MSG_NOSIGNAL);
snd_target--;
}
/* no kernel crash so far is considered success */
err = 0;
out: if (s >= 0)
close(s);
return err;
}
staticint setup(constchar *tun_dev)
{ int target_index = -1; int tap_fd = -1;
tap_fd = open_tuntap(tun_dev, false); if (!ASSERT_GE(tap_fd, 0, "open_tun")) return -1;
target_index = if_nametoindex(tun_dev); if (!ASSERT_GE(target_index, 0, "if_nametoindex")) return -1;
SYS(fail, "ip link add link_err type dummy");
SYS(fail, "ip link set lo up");
SYS(fail, "ip addr add dev lo " LOCAL_SRC "/32");
SYS(fail, "ip link set link_err up");
SYS(fail, "ip link set %s up", tun_dev);
tun_fd = setup(tun_dev); if (!ASSERT_GE(tun_fd, 0, "setup_reroute")) return;
ifindex = if_nametoindex(tun_dev); if (!ASSERT_GE(ifindex, 0, "if_nametoindex")) return;
snprintf(ip, 256, "10.0.0.%d", ifindex);
/* ping packets should be received by the tun device */
ping_once(ip);
if (!ASSERT_EQ(wait_for_packet(tun_fd, __expect_icmp_ipv4, &timeo), 1, "wait_for_packet"))
log_err("%s xmit", __func__);
}
/* * Test the failure case when the skb is dropped at the qdisc. This is a * regression prevention at the xmit hook only.
*/ staticvoid test_lwt_reroute_qdisc_dropped(void)
{ constchar *tun_dev = "tun0"; int tun_fd = -1; int ifindex = -1; char ip[256];
tun_fd = setup(tun_dev); if (!ASSERT_GE(tun_fd, 0, "setup_reroute")) goto fail;
void test_lwt_reroute(void)
{
pthread_t test_thread; int err;
/* Run the tests in their own thread to isolate the namespace changes * so they do not affect the environment of other tests. * (specifically needed because of unshare(CLONE_NEWNS) in open_netns())
*/
err = pthread_create(&test_thread, NULL, &test_lwt_reroute_run, NULL); if (ASSERT_OK(err, "pthread_create"))
ASSERT_OK(pthread_join(test_thread, NULL), "pthread_join");
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.