Skip to content

Commit adfeae2

Browse files
borkmannMartin KaFai Lau
authored and
Martin KaFai Lau
committed
selftests/bpf: Add netkit to tc_redirect selftest
Extend the existing tc_redirect selftest to also cover netkit devices for exercising the bpf_redirect_peer() code paths, so that we have both veth as well as netkit covered, all tests still pass after this change. Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Stanislav Fomichev <[email protected]> Reviewed-by: Nikolay Aleksandrov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent eee82da commit adfeae2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tools/testing/selftests/bpf/prog_tests/tc_redirect.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "test_progs.h"
2626
#include "network_helpers.h"
27+
#include "netlink_helpers.h"
2728
#include "test_tc_neigh_fib.skel.h"
2829
#include "test_tc_neigh.skel.h"
2930
#include "test_tc_peer.skel.h"
@@ -112,6 +113,7 @@ static void netns_setup_namespaces_nofail(const char *verb)
112113

113114
enum dev_mode {
114115
MODE_VETH,
116+
MODE_NETKIT,
115117
};
116118

117119
struct netns_setup_result {
@@ -142,17 +144,65 @@ static int get_ifaddr(const char *name, char *ifaddr)
142144
return 0;
143145
}
144146

147+
static int create_netkit(int mode, char *prim, char *peer)
148+
{
149+
struct rtattr *linkinfo, *data, *peer_info;
150+
struct rtnl_handle rth = { .fd = -1 };
151+
const char *type = "netkit";
152+
struct {
153+
struct nlmsghdr n;
154+
struct ifinfomsg i;
155+
char buf[1024];
156+
} req = {};
157+
int err;
158+
159+
err = rtnl_open(&rth, 0);
160+
if (!ASSERT_OK(err, "open_rtnetlink"))
161+
return err;
162+
163+
memset(&req, 0, sizeof(req));
164+
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
165+
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
166+
req.n.nlmsg_type = RTM_NEWLINK;
167+
req.i.ifi_family = AF_UNSPEC;
168+
169+
addattr_l(&req.n, sizeof(req), IFLA_IFNAME, prim, strlen(prim));
170+
linkinfo = addattr_nest(&req.n, sizeof(req), IFLA_LINKINFO);
171+
addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type, strlen(type));
172+
data = addattr_nest(&req.n, sizeof(req), IFLA_INFO_DATA);
173+
addattr32(&req.n, sizeof(req), IFLA_NETKIT_MODE, mode);
174+
peer_info = addattr_nest(&req.n, sizeof(req), IFLA_NETKIT_PEER_INFO);
175+
req.n.nlmsg_len += sizeof(struct ifinfomsg);
176+
addattr_l(&req.n, sizeof(req), IFLA_IFNAME, peer, strlen(peer));
177+
addattr_nest_end(&req.n, peer_info);
178+
addattr_nest_end(&req.n, data);
179+
addattr_nest_end(&req.n, linkinfo);
180+
181+
err = rtnl_talk(&rth, &req.n, NULL);
182+
ASSERT_OK(err, "talk_rtnetlink");
183+
rtnl_close(&rth);
184+
return err;
185+
}
186+
145187
static int netns_setup_links_and_routes(struct netns_setup_result *result)
146188
{
147189
struct nstoken *nstoken = NULL;
148190
char src_fwd_addr[IFADDR_STR_LEN+1] = {};
191+
int err;
149192

150193
if (result->dev_mode == MODE_VETH) {
151194
SYS(fail, "ip link add src type veth peer name src_fwd");
152195
SYS(fail, "ip link add dst type veth peer name dst_fwd");
153196

154197
SYS(fail, "ip link set dst_fwd address " MAC_DST_FWD);
155198
SYS(fail, "ip link set dst address " MAC_DST);
199+
} else if (result->dev_mode == MODE_NETKIT) {
200+
err = create_netkit(NETKIT_L3, "src", "src_fwd");
201+
if (!ASSERT_OK(err, "create_ifindex_src"))
202+
goto fail;
203+
err = create_netkit(NETKIT_L3, "dst", "dst_fwd");
204+
if (!ASSERT_OK(err, "create_ifindex_dst"))
205+
goto fail;
156206
}
157207

158208
if (get_ifaddr("src_fwd", src_fwd_addr))
@@ -1134,7 +1184,9 @@ static void *test_tc_redirect_run_tests(void *arg)
11341184
netns_setup_namespaces_nofail("delete");
11351185

11361186
RUN_TEST(tc_redirect_peer, MODE_VETH);
1187+
RUN_TEST(tc_redirect_peer, MODE_NETKIT);
11371188
RUN_TEST(tc_redirect_peer_l3, MODE_VETH);
1189+
RUN_TEST(tc_redirect_peer_l3, MODE_NETKIT);
11381190
RUN_TEST(tc_redirect_neigh, MODE_VETH);
11391191
RUN_TEST(tc_redirect_neigh_fib, MODE_VETH);
11401192
RUN_TEST(tc_redirect_dtime, MODE_VETH);

0 commit comments

Comments
 (0)