1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01:00
linux/tools/testing/selftests/bpf/progs/test_sockmap_strp.c
Jiayuan Chen 6fcfe96e0f selftests/bpf: Add strparser test for bpf
Add test cases for bpf + strparser and separated them from
sockmap_basic, as strparser has more encapsulation and parsing
capabilities compared to standard sockmap.

Signed-off-by: Jiayuan Chen <mrpre@163.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Acked-by: Jakub Sitnicki <jakub@cloudflare.com>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://patch.msgid.link/20250122100917.49845-6-mrpre@163.com
2025-01-29 13:32:48 -08:00

53 lines
1.1 KiB
C

// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
int verdict_max_size = 10000;
struct {
__uint(type, BPF_MAP_TYPE_SOCKMAP);
__uint(max_entries, 20);
__type(key, int);
__type(value, int);
} sock_map SEC(".maps");
SEC("sk_skb/stream_verdict")
int prog_skb_verdict(struct __sk_buff *skb)
{
__u32 one = 1;
if (skb->len > verdict_max_size)
return SK_PASS;
return bpf_sk_redirect_map(skb, &sock_map, one, 0);
}
SEC("sk_skb/stream_verdict")
int prog_skb_verdict_pass(struct __sk_buff *skb)
{
return SK_PASS;
}
SEC("sk_skb/stream_parser")
int prog_skb_parser(struct __sk_buff *skb)
{
return skb->len;
}
SEC("sk_skb/stream_parser")
int prog_skb_parser_partial(struct __sk_buff *skb)
{
/* agreement with the test program on a 4-byte size header
* and 6-byte body.
*/
if (skb->len < 4) {
/* need more header to determine full length */
return 0;
}
/* return full length decoded from header.
* the return value may be larger than skb->len which
* means framework must wait body coming.
*/
return 10;
}
char _license[] SEC("license") = "GPL";