hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/samples/bpf/xdp_adjust_tail_kern.c
....@@ -18,19 +18,22 @@
1818 #include <linux/if_vlan.h>
1919 #include <linux/ip.h>
2020 #include <linux/icmp.h>
21
-#include "bpf_helpers.h"
21
+#include <bpf/bpf_helpers.h>
2222
2323 #define DEFAULT_TTL 64
2424 #define MAX_PCKT_SIZE 600
2525 #define ICMP_TOOBIG_SIZE 98
2626 #define ICMP_TOOBIG_PAYLOAD_SIZE 92
2727
28
-struct bpf_map_def SEC("maps") icmpcnt = {
29
- .type = BPF_MAP_TYPE_ARRAY,
30
- .key_size = sizeof(__u32),
31
- .value_size = sizeof(__u64),
32
- .max_entries = 1,
33
-};
28
+/* volatile to prevent compiler optimizations */
29
+static volatile __u32 max_pcktsz = MAX_PCKT_SIZE;
30
+
31
+struct {
32
+ __uint(type, BPF_MAP_TYPE_ARRAY);
33
+ __type(key, __u32);
34
+ __type(value, __u64);
35
+ __uint(max_entries, 1);
36
+} icmpcnt SEC(".maps");
3437
3538 static __always_inline void count_icmp(void)
3639 {
....@@ -92,7 +95,7 @@
9295 orig_iph = data + off;
9396 icmp_hdr->type = ICMP_DEST_UNREACH;
9497 icmp_hdr->code = ICMP_FRAG_NEEDED;
95
- icmp_hdr->un.frag.mtu = htons(MAX_PCKT_SIZE-sizeof(struct ethhdr));
98
+ icmp_hdr->un.frag.mtu = htons(max_pcktsz - sizeof(struct ethhdr));
9699 icmp_hdr->checksum = 0;
97100 ipv4_csum(icmp_hdr, ICMP_TOOBIG_PAYLOAD_SIZE, &csum);
98101 icmp_hdr->checksum = csum;
....@@ -121,7 +124,7 @@
121124 int pckt_size = data_end - data;
122125 int offset;
123126
124
- if (pckt_size > MAX_PCKT_SIZE) {
127
+ if (pckt_size > max(max_pcktsz, ICMP_TOOBIG_SIZE)) {
125128 offset = pckt_size - ICMP_TOOBIG_SIZE;
126129 if (bpf_xdp_adjust_tail(xdp, 0 - offset))
127130 return XDP_PASS;