Skip to content

Commit c6ffd1f

Browse files
tehnerdborkmann
authored andcommitted
bpf: add bpf_xdp_adjust_tail sample prog
adding bpf's sample program which is using bpf_xdp_adjust_tail helper by generating ICMPv4 "packet to big" message if ingress packet's size is bigger then 600 bytes Signed-off-by: Nikita V. Shirokov <tehnerd@tehnerd.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 0367d0a commit c6ffd1f

File tree

4 files changed

+300
-0
lines changed

4 files changed

+300
-0
lines changed

samples/bpf/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ hostprogs-y += xdp_monitor
4444
hostprogs-y += xdp_rxq_info
4545
hostprogs-y += syscall_tp
4646
hostprogs-y += cpustat
47+
hostprogs-y += xdp_adjust_tail
4748

4849
# Libbpf dependencies
4950
LIBBPF := ../../tools/lib/bpf/bpf.o ../../tools/lib/bpf/nlattr.o
@@ -95,6 +96,7 @@ xdp_monitor-objs := bpf_load.o $(LIBBPF) xdp_monitor_user.o
9596
xdp_rxq_info-objs := bpf_load.o $(LIBBPF) xdp_rxq_info_user.o
9697
syscall_tp-objs := bpf_load.o $(LIBBPF) syscall_tp_user.o
9798
cpustat-objs := bpf_load.o $(LIBBPF) cpustat_user.o
99+
xdp_adjust_tail-objs := bpf_load.o $(LIBBPF) xdp_adjust_tail_user.o
98100

99101
# Tell kbuild to always build the programs
100102
always := $(hostprogs-y)
@@ -148,6 +150,7 @@ always += xdp_rxq_info_kern.o
148150
always += xdp2skb_meta_kern.o
149151
always += syscall_tp_kern.o
150152
always += cpustat_kern.o
153+
always += xdp_adjust_tail_kern.o
151154

152155
HOSTCFLAGS += -I$(objtree)/usr/include
153156
HOSTCFLAGS += -I$(srctree)/tools/lib/
@@ -193,6 +196,7 @@ HOSTLOADLIBES_xdp_monitor += -lelf
193196
HOSTLOADLIBES_xdp_rxq_info += -lelf
194197
HOSTLOADLIBES_syscall_tp += -lelf
195198
HOSTLOADLIBES_cpustat += -lelf
199+
HOSTLOADLIBES_xdp_adjust_tail += -lelf
196200

197201
# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
198202
# make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang

samples/bpf/xdp_adjust_tail_kern.c

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/* SPDX-License-Identifier: GPL-2.0
2+
* Copyright (c) 2018 Facebook
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of version 2 of the GNU General Public
6+
* License as published by the Free Software Foundation.
7+
*
8+
* This program shows how to use bpf_xdp_adjust_tail() by
9+
* generating ICMPv4 "packet to big" (unreachable/ df bit set frag needed
10+
* to be more preice in case of v4)" where receiving packets bigger then
11+
* 600 bytes.
12+
*/
13+
#define KBUILD_MODNAME "foo"
14+
#include <uapi/linux/bpf.h>
15+
#include <linux/in.h>
16+
#include <linux/if_ether.h>
17+
#include <linux/if_packet.h>
18+
#include <linux/if_vlan.h>
19+
#include <linux/ip.h>
20+
#include <linux/icmp.h>
21+
#include "bpf_helpers.h"
22+
23+
#define DEFAULT_TTL 64
24+
#define MAX_PCKT_SIZE 600
25+
#define ICMP_TOOBIG_SIZE 98
26+
#define ICMP_TOOBIG_PAYLOAD_SIZE 92
27+
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+
};
34+
35+
static __always_inline void count_icmp(void)
36+
{
37+
u64 key = 0;
38+
u64 *icmp_count;
39+
40+
icmp_count = bpf_map_lookup_elem(&icmpcnt, &key);
41+
if (icmp_count)
42+
*icmp_count += 1;
43+
}
44+
45+
static __always_inline void swap_mac(void *data, struct ethhdr *orig_eth)
46+
{
47+
struct ethhdr *eth;
48+
49+
eth = data;
50+
memcpy(eth->h_source, orig_eth->h_dest, ETH_ALEN);
51+
memcpy(eth->h_dest, orig_eth->h_source, ETH_ALEN);
52+
eth->h_proto = orig_eth->h_proto;
53+
}
54+
55+
static __always_inline __u16 csum_fold_helper(__u32 csum)
56+
{
57+
return ~((csum & 0xffff) + (csum >> 16));
58+
}
59+
60+
static __always_inline void ipv4_csum(void *data_start, int data_size,
61+
__u32 *csum)
62+
{
63+
*csum = bpf_csum_diff(0, 0, data_start, data_size, *csum);
64+
*csum = csum_fold_helper(*csum);
65+
}
66+
67+
static __always_inline int send_icmp4_too_big(struct xdp_md *xdp)
68+
{
69+
int headroom = (int)sizeof(struct iphdr) + (int)sizeof(struct icmphdr);
70+
71+
if (bpf_xdp_adjust_head(xdp, 0 - headroom))
72+
return XDP_DROP;
73+
void *data = (void *)(long)xdp->data;
74+
void *data_end = (void *)(long)xdp->data_end;
75+
76+
if (data + (ICMP_TOOBIG_SIZE + headroom) > data_end)
77+
return XDP_DROP;
78+
79+
struct iphdr *iph, *orig_iph;
80+
struct icmphdr *icmp_hdr;
81+
struct ethhdr *orig_eth;
82+
__u32 csum = 0;
83+
__u64 off = 0;
84+
85+
orig_eth = data + headroom;
86+
swap_mac(data, orig_eth);
87+
off += sizeof(struct ethhdr);
88+
iph = data + off;
89+
off += sizeof(struct iphdr);
90+
icmp_hdr = data + off;
91+
off += sizeof(struct icmphdr);
92+
orig_iph = data + off;
93+
icmp_hdr->type = ICMP_DEST_UNREACH;
94+
icmp_hdr->code = ICMP_FRAG_NEEDED;
95+
icmp_hdr->un.frag.mtu = htons(MAX_PCKT_SIZE-sizeof(struct ethhdr));
96+
icmp_hdr->checksum = 0;
97+
ipv4_csum(icmp_hdr, ICMP_TOOBIG_PAYLOAD_SIZE, &csum);
98+
icmp_hdr->checksum = csum;
99+
iph->ttl = DEFAULT_TTL;
100+
iph->daddr = orig_iph->saddr;
101+
iph->saddr = orig_iph->daddr;
102+
iph->version = 4;
103+
iph->ihl = 5;
104+
iph->protocol = IPPROTO_ICMP;
105+
iph->tos = 0;
106+
iph->tot_len = htons(
107+
ICMP_TOOBIG_SIZE + headroom - sizeof(struct ethhdr));
108+
iph->check = 0;
109+
csum = 0;
110+
ipv4_csum(iph, sizeof(struct iphdr), &csum);
111+
iph->check = csum;
112+
count_icmp();
113+
return XDP_TX;
114+
}
115+
116+
117+
static __always_inline int handle_ipv4(struct xdp_md *xdp)
118+
{
119+
void *data_end = (void *)(long)xdp->data_end;
120+
void *data = (void *)(long)xdp->data;
121+
int pckt_size = data_end - data;
122+
int offset;
123+
124+
if (pckt_size > MAX_PCKT_SIZE) {
125+
offset = pckt_size - ICMP_TOOBIG_SIZE;
126+
if (bpf_xdp_adjust_tail(xdp, 0 - offset))
127+
return XDP_PASS;
128+
return send_icmp4_too_big(xdp);
129+
}
130+
return XDP_PASS;
131+
}
132+
133+
SEC("xdp_icmp")
134+
int _xdp_icmp(struct xdp_md *xdp)
135+
{
136+
void *data_end = (void *)(long)xdp->data_end;
137+
void *data = (void *)(long)xdp->data;
138+
struct ethhdr *eth = data;
139+
__u16 h_proto;
140+
141+
if (eth + 1 > data_end)
142+
return XDP_DROP;
143+
144+
h_proto = eth->h_proto;
145+
146+
if (h_proto == htons(ETH_P_IP))
147+
return handle_ipv4(xdp);
148+
else
149+
return XDP_PASS;
150+
}
151+
152+
char _license[] SEC("license") = "GPL";

samples/bpf/xdp_adjust_tail_user.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/* SPDX-License-Identifier: GPL-2.0
2+
* Copyright (c) 2018 Facebook
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of version 2 of the GNU General Public
6+
* License as published by the Free Software Foundation.
7+
*/
8+
#include <linux/bpf.h>
9+
#include <linux/if_link.h>
10+
#include <assert.h>
11+
#include <errno.h>
12+
#include <signal.h>
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
#include <string.h>
16+
#include <sys/resource.h>
17+
#include <arpa/inet.h>
18+
#include <netinet/ether.h>
19+
#include <unistd.h>
20+
#include <time.h>
21+
#include "bpf_load.h"
22+
#include "libbpf.h"
23+
#include "bpf_util.h"
24+
25+
#define STATS_INTERVAL_S 2U
26+
27+
static int ifindex = -1;
28+
static __u32 xdp_flags;
29+
30+
static void int_exit(int sig)
31+
{
32+
if (ifindex > -1)
33+
bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
34+
exit(0);
35+
}
36+
37+
/* simple "icmp packet too big sent" counter
38+
*/
39+
static void poll_stats(unsigned int kill_after_s)
40+
{
41+
time_t started_at = time(NULL);
42+
__u64 value = 0;
43+
int key = 0;
44+
45+
46+
while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
47+
sleep(STATS_INTERVAL_S);
48+
49+
assert(bpf_map_lookup_elem(map_fd[0], &key, &value) == 0);
50+
51+
printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
52+
}
53+
}
54+
55+
static void usage(const char *cmd)
56+
{
57+
printf("Start a XDP prog which send ICMP \"packet too big\" \n"
58+
"messages if ingress packet is bigger then MAX_SIZE bytes\n");
59+
printf("Usage: %s [...]\n", cmd);
60+
printf(" -i <ifindex> Interface Index\n");
61+
printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
62+
printf(" -S use skb-mode\n");
63+
printf(" -N enforce native mode\n");
64+
printf(" -h Display this help\n");
65+
}
66+
67+
int main(int argc, char **argv)
68+
{
69+
unsigned char opt_flags[256] = {};
70+
unsigned int kill_after_s = 0;
71+
const char *optstr = "i:T:SNh";
72+
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
73+
char filename[256];
74+
int opt;
75+
int i;
76+
77+
78+
for (i = 0; i < strlen(optstr); i++)
79+
if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
80+
opt_flags[(unsigned char)optstr[i]] = 1;
81+
82+
while ((opt = getopt(argc, argv, optstr)) != -1) {
83+
84+
switch (opt) {
85+
case 'i':
86+
ifindex = atoi(optarg);
87+
break;
88+
case 'T':
89+
kill_after_s = atoi(optarg);
90+
break;
91+
case 'S':
92+
xdp_flags |= XDP_FLAGS_SKB_MODE;
93+
break;
94+
case 'N':
95+
xdp_flags |= XDP_FLAGS_DRV_MODE;
96+
break;
97+
default:
98+
usage(argv[0]);
99+
return 1;
100+
}
101+
opt_flags[opt] = 0;
102+
}
103+
104+
for (i = 0; i < strlen(optstr); i++) {
105+
if (opt_flags[(unsigned int)optstr[i]]) {
106+
fprintf(stderr, "Missing argument -%c\n", optstr[i]);
107+
usage(argv[0]);
108+
return 1;
109+
}
110+
}
111+
112+
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
113+
perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
114+
return 1;
115+
}
116+
117+
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
118+
119+
if (load_bpf_file(filename)) {
120+
printf("%s", bpf_log_buf);
121+
return 1;
122+
}
123+
124+
if (!prog_fd[0]) {
125+
printf("load_bpf_file: %s\n", strerror(errno));
126+
return 1;
127+
}
128+
129+
signal(SIGINT, int_exit);
130+
signal(SIGTERM, int_exit);
131+
132+
if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) {
133+
printf("link set xdp fd failed\n");
134+
return 1;
135+
}
136+
137+
poll_stats(kill_after_s);
138+
139+
bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
140+
141+
return 0;
142+
}

tools/testing/selftests/bpf/bpf_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flag
132132
(void *) BPF_FUNC_l3_csum_replace;
133133
static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
134134
(void *) BPF_FUNC_l4_csum_replace;
135+
static int (*bpf_csum_diff)(void *from, int from_size, void *to, int to_size, int seed) =
136+
(void *) BPF_FUNC_csum_diff;
135137
static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
136138
(void *) BPF_FUNC_skb_under_cgroup;
137139
static int (*bpf_skb_change_head)(void *, int len, int flags) =

0 commit comments

Comments
 (0)