Skip to content

Commit

Permalink
add sendmsg test
Browse files Browse the repository at this point in the history
  • Loading branch information
yanchaomei committed Sep 30, 2024
1 parent c9634e1 commit eadc751
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
10 changes: 9 additions & 1 deletion bpf/kmesh/workload/sendmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ enum TLV_TYPE {
TLV_PAYLOAD = 0xfe,
};

struct sk_msg_md g_msg = {0};
struct bpf_sock_tuple g_dst_info = {0};
__u32 g_encoded_length = 0;

static inline int check_overflow(struct sk_msg_md *msg, __u8 *begin, __u32 length)
{
if (msg->data_end < (void *)(begin + length)) {
Expand All @@ -84,7 +88,9 @@ static inline int get_origin_dst(struct sk_msg_md *msg, struct ip_addr *dst_ip,
__u64 *current_sk = (__u64 *)msg->sk;
struct bpf_sock_tuple *dst;

dst = bpf_map_lookup_elem(&map_of_dst_info, &current_sk);
// dst = bpf_map_lookup_elem(&map_of_dst_info, &current_sk);
// 使用全局变量 g_dst_info 代替 map 查找
dst = &g_dst_info;
if (!dst)
return -ENOENT;

Expand Down Expand Up @@ -176,6 +182,8 @@ int sendmsg_prog(struct sk_msg_md *msg)

// encode org dst addr
encode_metadata_org_dst_addr(msg, &off, (msg->family == AF_INET));
// 为测试目的,保存编码后的长度
g_encoded_length = off;
return SK_PASS;
}

Expand Down
93 changes: 93 additions & 0 deletions bpf/test/workload/sendmsg_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// test_sendmsg.c
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <stdio.h>
#include <assert.h>
#include "sendmsg_test.skel.h"


void test_sendmsg_ipv4()
{
struct sendmsg_test_bpf *skel;
int prog_fd, err;

// 加载 eBPF 程序
skel = sendmsg_test_bpf__open_and_load();
assert(skel != NULL);

// 准备测试数据
skel->bss->g_msg.family = AF_INET;
skel->bss->g_msg.sk = (__u64)12345; // 模拟 socket 指针

skel->bss->g_dst_info.ipv4.daddr = 0x08080808; // 8.8.8.8
skel->bss->g_dst_info.ipv4.dport = htons(80);

struct bpf_test_run_opts opts = {
.sz = sizeof(struct bpf_test_run_opts),
.ctx_in = &skel->bss->g_msg,
.ctx_size_in = sizeof(skel->bss->g_msg),
.ctx_out = &skel->bss->g_msg,
.ctx_size_out = sizeof(skel->bss->g_msg)
};

// 运行测试
prog_fd = bpf_program__fd(skel->progs.sendmsg_prog);
err = bpf_prog_test_run_opts(prog_fd, &opts);

// 验证结果
assert(err == 0);
assert(opts.retval == SK_PASS);
assert(skel->bss->g_encoded_length == TLV_ORG_DST_ADDR4_SIZE + TLV_END_SIZE);

// 清理
sendmsg_test_bpf__destroy(skel);
}

void test_sendmsg_ipv6()
{
struct sendmsg_test_bpf *skel;
int prog_fd, err;

// 加载 eBPF 程序
skel = sendmsg_test_bpf__open_and_load();
assert(skel != NULL);

// 准备测试数据
skel->bss->g_msg.family = AF_INET6;
skel->bss->g_msg.sk = (__u64)12345; // 模拟 socket 指针

// 设置 IPv6 地址 (2001:4860:4860::8888)
skel->bss->g_dst_info.ipv6.daddr[0] = htonl(0x20010486);
skel->bss->g_dst_info.ipv6.daddr[1] = htonl(0x04860000);
skel->bss->g_dst_info.ipv6.daddr[2] = 0;
skel->bss->g_dst_info.ipv6.daddr[3] = htonl(0x8888);
skel->bss->g_dst_info.ipv6.dport = htons(80);

struct bpf_test_run_opts opts = {
.sz = sizeof(struct bpf_test_run_opts),
.ctx_in = &skel->bss->g_msg,
.ctx_size_in = sizeof(skel->bss->g_msg),
.ctx_out = &skel->bss->g_msg,
.ctx_size_out = sizeof(skel->bss->g_msg)
};

// 运行测试
prog_fd = bpf_program__fd(skel->progs.sendmsg_prog);
err = bpf_prog_test_run_opts(prog_fd, &opts);

// 验证结果
assert(err == 0);
assert(opts.retval == SK_PASS);
assert(skel->bss->g_encoded_length == TLV_ORG_DST_ADDR6_SIZE + TLV_END_SIZE);

// 清理
sendmsg_test_bpf__destroy(skel);
}

int main()
{
test_sendmsg_ipv4();
test_sendmsg_ipv6();
printf("All tests passed!\n");
return 0;
}

0 comments on commit eadc751

Please sign in to comment.