xdp test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vm1 hostip 172.18.18.101(enp0s9), the XDP prog is attached on enp0s9 | |
# vm2 hostip 172.18.18.102(enp0s9) | |
# exchange id and remoteid vm2 | |
id=1 | |
remoteid=2 | |
ip=172.19.$id.2 | |
ctn=ns1 | |
brctl addbr br1 | |
ip l set br1 up | |
ip a add 172.19.$id.1/24 dev br1 | |
ip netns add $ctn | |
ip l add dev vhost type veth peer name vsbx | |
ip l set dev vsbx netns $ctn | |
ip netns exec $ctn ip ad add $ip dev vsbx | |
ip netns exec $ctn ip link set dev vsbx up | |
ip netns exec $ctn ip route add default via 172.19.$id.1 dev vsbx onlink | |
ip link set dev vhost up | |
brctl addif br1 vhost | |
ip r add 172.19.$remoteid.0/24 via 172.18.18.10$remoteid dev enp0s9 | |
sysctl -w net.ipv4.ip_forward=1 | |
iptables -t filter -P FORWARD ACCEPT | |
# make sure network setup success before attach XDP prog | |
ip netns exec ns1 ping 172.19.$remoteid.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int SEC("xdp/prog1") xdp_dummy(struct xdp_md *ctx) { | |
void* data_end = (void*)(long)ctx->data_end; | |
void* data = (void*)(long)ctx->data; | |
struct ethhdr *eth = data; | |
__u32 nh_off = sizeof(*eth); | |
if (data+nh_off > data_end){ | |
return XDP_DROP; | |
} | |
__be16 eth_proto = eth->h_proto; | |
if (eth_proto != BE_ETH_P_IP){ | |
return XDP_PASS; | |
} | |
struct iphdr *iph = data+nh_off; | |
if (iph + 1 > data_end){ | |
return XDP_DROP; | |
} | |
__u32 iph_len = iph->ihl<<2; | |
struct dev *cdev; | |
__be32 daddr = iph->daddr; | |
cdev = bpf_map_lookup_elem(&local_ips, &daddr); | |
if (cdev) { | |
memcpy(eth->h_dest, cdev->mac, ETH_ALEN); // mac addr of vsbx | |
int res = bpf_redirect_map(&dev_map, cdev->dev_idx, 0); // dev_idx is vhost or br1(both failed) | |
if (res == XDP_REDIRECT) { | |
bpf_debug("redirect success to %d\n", cdev->dev_idx); // I can see this message in trace_pipe | |
} else { | |
bpf_debug("redirect failed %d\n", res); | |
} | |
return res; | |
} else { | |
bpf_debug("no dev for %x found\n", daddr); | |
} | |
return XDP_PASS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment