Commit 98cdabcd authored by Willem de Bruijn's avatar Willem de Bruijn Committed by Alexei Starovoitov

selftests/bpf: bpf tunnel encap test

Validate basic tunnel encapsulation using ipip.

Set up two namespaces connected by veth. Connect a client and server.
Do this with and without bpf encap.
Signed-off-by: default avatarWillem de Bruijn <willemb@google.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 908adce6
...@@ -52,7 +52,8 @@ TEST_PROGS := test_kmod.sh \ ...@@ -52,7 +52,8 @@ TEST_PROGS := test_kmod.sh \
test_flow_dissector.sh \ test_flow_dissector.sh \
test_xdp_vlan.sh \ test_xdp_vlan.sh \
test_lwt_ip_encap.sh \ test_lwt_ip_encap.sh \
test_tcp_check_syncookie.sh test_tcp_check_syncookie.sh \
test_tc_tunnel.sh
TEST_PROGS_EXTENDED := with_addr.sh \ TEST_PROGS_EXTENDED := with_addr.sh \
with_tunnels.sh \ with_tunnels.sh \
......
// SPDX-License-Identifier: GPL-2.0
/* In-place tunneling */
#include <linux/stddef.h>
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/pkt_cls.h>
#include <linux/types.h>
#include "bpf_endian.h"
#include "bpf_helpers.h"
static const int cfg_port = 8000;
static __always_inline void set_ipv4_csum(struct iphdr *iph)
{
__u16 *iph16 = (__u16 *)iph;
__u32 csum;
int i;
iph->check = 0;
#pragma clang loop unroll(full)
for (i = 0, csum = 0; i < sizeof(*iph) >> 1; i++)
csum += *iph16++;
iph->check = ~((csum & 0xffff) + (csum >> 16));
}
SEC("encap")
int encap_f(struct __sk_buff *skb)
{
struct iphdr iph_outer, iph_inner;
struct tcphdr tcph;
if (skb->protocol != __bpf_constant_htons(ETH_P_IP))
return TC_ACT_OK;
if (bpf_skb_load_bytes(skb, ETH_HLEN, &iph_inner,
sizeof(iph_inner)) < 0)
return TC_ACT_OK;
/* filter only packets we want */
if (iph_inner.ihl != 5 || iph_inner.protocol != IPPROTO_TCP)
return TC_ACT_OK;
if (bpf_skb_load_bytes(skb, ETH_HLEN + sizeof(iph_inner),
&tcph, sizeof(tcph)) < 0)
return TC_ACT_OK;
if (tcph.dest != __bpf_constant_htons(cfg_port))
return TC_ACT_OK;
/* add room between mac and network header */
if (bpf_skb_adjust_room(skb, sizeof(iph_outer), BPF_ADJ_ROOM_NET, 0))
return TC_ACT_SHOT;
/* prepare new outer network header */
iph_outer = iph_inner;
iph_outer.protocol = IPPROTO_IPIP;
iph_outer.tot_len = bpf_htons(sizeof(iph_outer) +
bpf_htons(iph_outer.tot_len));
set_ipv4_csum(&iph_outer);
/* store new outer network header */
if (bpf_skb_store_bytes(skb, ETH_HLEN, &iph_outer, sizeof(iph_outer),
BPF_F_INVALIDATE_HASH) < 0)
return TC_ACT_SHOT;
/* bpf_skb_adjust_room has moved header to start of room: restore */
if (bpf_skb_store_bytes(skb, ETH_HLEN + sizeof(iph_outer),
&iph_inner, sizeof(iph_inner),
BPF_F_INVALIDATE_HASH) < 0)
return TC_ACT_SHOT;
return TC_ACT_OK;
}
char __license[] SEC("license") = "GPL";
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# In-place tunneling
# must match the port that the bpf program filters on
readonly port=8000
readonly ns_prefix="ns-$$-"
readonly ns1="${ns_prefix}1"
readonly ns2="${ns_prefix}2"
readonly ns1_v4=192.168.1.1
readonly ns2_v4=192.168.1.2
setup() {
ip netns add "${ns1}"
ip netns add "${ns2}"
ip link add dev veth1 mtu 1500 netns "${ns1}" type veth \
peer name veth2 mtu 1500 netns "${ns2}"
ip -netns "${ns1}" link set veth1 up
ip -netns "${ns2}" link set veth2 up
ip -netns "${ns1}" -4 addr add "${ns1_v4}/24" dev veth1
ip -netns "${ns2}" -4 addr add "${ns2_v4}/24" dev veth2
sleep 1
}
cleanup() {
ip netns del "${ns2}"
ip netns del "${ns1}"
}
server_listen() {
ip netns exec "${ns2}" nc -l -p "${port}" &
sleep 0.2
}
client_connect() {
ip netns exec "${ns1}" nc -z -w 1 "${ns2_v4}" "${port}"
echo $?
}
set -e
trap cleanup EXIT
setup
# basic communication works
echo "test basic connectivity"
server_listen
client_connect
# clientside, insert bpf program to encap all TCP to port ${port}
# client can no longer connect
ip netns exec "${ns1}" tc qdisc add dev veth1 clsact
ip netns exec "${ns1}" tc filter add dev veth1 egress \
bpf direct-action object-file ./test_tc_tunnel.o section encap
echo "test bpf encap without decap (expect failure)"
server_listen
! client_connect
# serverside, insert decap module
# server is still running
# client can connect again
ip netns exec "${ns2}" ip link add dev testtun0 type ipip \
remote "${ns1_v4}" local "${ns2_v4}"
ip netns exec "${ns2}" ip link set dev testtun0 up
echo "test bpf encap with tunnel device decap"
client_connect
echo OK
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment