selftests/bpf: Use bpf_check_mtu in selftest test_cls_redirect
authorJesper Dangaard Brouer <brouer@redhat.com>
Tue, 9 Feb 2021 13:38:34 +0000 (14:38 +0100)
committerDaniel Borkmann <daniel@iogearbox.net>
Sat, 13 Feb 2021 00:15:28 +0000 (01:15 +0100)
This demonstrate how bpf_check_mtu() helper can easily be used together
with bpf_skb_adjust_room() helper, prior to doing size adjustment, as
delta argument is already setup.

Hint: This specific test can be selected like this:
 ./test_progs -t cls_redirect

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/161287791481.790810.4444271170546646080.stgit@firesoul
tools/testing/selftests/bpf/progs/test_cls_redirect.c

index c9f8464..3c1e042 100644 (file)
@@ -70,6 +70,7 @@ typedef struct {
        uint64_t errors_total_encap_adjust_failed;
        uint64_t errors_total_encap_buffer_too_small;
        uint64_t errors_total_redirect_loop;
+       uint64_t errors_total_encap_mtu_violate;
 } metrics_t;
 
 typedef enum {
@@ -407,6 +408,7 @@ static INLINING ret_t forward_with_gre(struct __sk_buff *skb, encap_headers_t *e
                payload_off - sizeof(struct ethhdr) - sizeof(struct iphdr);
        int32_t delta = sizeof(struct gre_base_hdr) - encap_overhead;
        uint16_t proto = ETH_P_IP;
+       uint32_t mtu_len = 0;
 
        /* Loop protection: the inner packet's TTL is decremented as a safeguard
         * against any forwarding loop. As the only interesting field is the TTL
@@ -479,6 +481,11 @@ static INLINING ret_t forward_with_gre(struct __sk_buff *skb, encap_headers_t *e
                }
        }
 
+       if (bpf_check_mtu(skb, skb->ifindex, &mtu_len, delta, 0)) {
+               metrics->errors_total_encap_mtu_violate++;
+               return TC_ACT_SHOT;
+       }
+
        if (bpf_skb_adjust_room(skb, delta, BPF_ADJ_ROOM_NET,
                                BPF_F_ADJ_ROOM_FIXED_GSO |
                                BPF_F_ADJ_ROOM_NO_CSUM_RESET) ||