需改ebpf程序探测内核,测试性能,验证想法,更新笔记。
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
|
||||
/* Copyright (c) 2020 Facebook */
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
@@ -12,6 +10,7 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "replica.h"
|
||||
|
||||
@@ -20,10 +19,9 @@
|
||||
typedef enum {
|
||||
OFFLINE = 0,
|
||||
ONLINE = 1,
|
||||
}replica_state_e ;
|
||||
} replica_state_e;
|
||||
|
||||
struct cmd_node {
|
||||
__u64 seq;
|
||||
__u32 len;
|
||||
uint8_t *cmd;
|
||||
struct cmd_node *next;
|
||||
@@ -32,7 +30,7 @@ struct cmd_node {
|
||||
struct pending_queue {
|
||||
struct cmd_node *head;
|
||||
struct cmd_node *tail;
|
||||
int count;
|
||||
int count;
|
||||
};
|
||||
|
||||
/* ================= 全局状态 ================= */
|
||||
@@ -43,7 +41,6 @@ static int epollfd = -1;
|
||||
|
||||
static char peer_ip[MAX_IP_LEN];
|
||||
static int peer_port;
|
||||
static __u64 peer_seq;
|
||||
|
||||
static struct pending_queue pending = {
|
||||
.head = NULL,
|
||||
@@ -66,7 +63,7 @@ static void pending_free()
|
||||
q->count = 0;
|
||||
}
|
||||
|
||||
static void pending_push(__u64 seq, __u32 len, const uint8_t *cmd)
|
||||
static void pending_push(__u32 len, const uint8_t *cmd)
|
||||
{
|
||||
struct cmd_node *node = malloc(sizeof(*node));
|
||||
if (!node)
|
||||
@@ -79,7 +76,6 @@ static void pending_push(__u64 seq, __u32 len, const uint8_t *cmd)
|
||||
}
|
||||
|
||||
memcpy(node->cmd, cmd, len);
|
||||
node->seq = seq;
|
||||
node->len = len;
|
||||
node->next = NULL;
|
||||
|
||||
@@ -93,72 +89,66 @@ static void pending_push(__u64 seq, __u32 len, const uint8_t *cmd)
|
||||
pending.count++;
|
||||
}
|
||||
|
||||
static void pending_gc(__u64 min_seq)
|
||||
{
|
||||
struct cmd_node *cur = pending.head;
|
||||
|
||||
int n = pending.count;
|
||||
while (cur && cur->seq < min_seq) {
|
||||
struct cmd_node *tmp = cur;
|
||||
cur = cur->next;
|
||||
|
||||
free(tmp->cmd);
|
||||
free(tmp);
|
||||
pending.count--;
|
||||
}
|
||||
|
||||
DEBUGLOG("gc:%d\n", n-pending.count);
|
||||
|
||||
pending.head = cur;
|
||||
if (!cur)
|
||||
pending.tail = NULL;
|
||||
}
|
||||
|
||||
static long long int sendn = 0;
|
||||
static void pending_send_all(void)
|
||||
{
|
||||
struct cmd_node *cur = pending.head;
|
||||
while (cur) {
|
||||
int rt = send(sockfd, cur->cmd, cur->len, 0);
|
||||
int need_out = 0;
|
||||
int sent_count = 0;
|
||||
const int MAX_BATCH = 100; // 批量发送上限,避免阻塞过久
|
||||
|
||||
if(rt == (int)cur->len){
|
||||
while (cur && sent_count < MAX_BATCH) {
|
||||
// 使用 MSG_MORE 合并多个小包
|
||||
int flags = (cur->next && sent_count < MAX_BATCH - 1) ? MSG_MORE : 0;
|
||||
int rt = send(sockfd, cur->cmd, cur->len, flags);
|
||||
|
||||
if (rt == (int)cur->len) {
|
||||
sendn += rt;
|
||||
printf("%s\n", cur->cmd);
|
||||
struct cmd_node *tmp = cur;
|
||||
cur = cur->next;
|
||||
|
||||
free(tmp->cmd);
|
||||
free(tmp);
|
||||
pending.count--;
|
||||
}else{
|
||||
DEBUGLOG("error\n");
|
||||
// 失败:不移动 cur,直接 break
|
||||
if (rt < 0) {
|
||||
pending.head = cur;
|
||||
sent_count++;
|
||||
} else if (rt > 0) {
|
||||
sendn += rt;
|
||||
memmove(cur->cmd, cur->cmd + rt, cur->len - rt);
|
||||
cur->len -= rt;
|
||||
need_out = 1;
|
||||
break;
|
||||
} else {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
need_out = 1;
|
||||
break;
|
||||
} else {
|
||||
perror("send failed");
|
||||
if (errno == ECONNRESET || errno == EPIPE) {
|
||||
state = OFFLINE;
|
||||
if (sockfd >= 0) {
|
||||
close(sockfd);
|
||||
sockfd = -1;
|
||||
DEBUGLOG("connect closed\n");
|
||||
}
|
||||
} else if (rt == 0) {
|
||||
fprintf(stderr, "send returned 0 (peer closed?)\n");
|
||||
} else {
|
||||
fprintf(stderr, "partial send: %d/%u\n", rt, cur->len);
|
||||
}
|
||||
|
||||
state = OFFLINE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
DEBUGLOG("sendn :%lld\n", sendn);
|
||||
|
||||
pending.head = cur;
|
||||
if(!cur)
|
||||
pending.tail = NULL;
|
||||
if (!cur) pending.tail = NULL;
|
||||
|
||||
if (sockfd >= 0 && state == ONLINE) {
|
||||
struct epoll_event ev = {0};
|
||||
ev.data.fd = sockfd;
|
||||
ev.events = EPOLLIN;
|
||||
if (need_out || pending.head) {
|
||||
ev.events |= EPOLLOUT;
|
||||
}
|
||||
epoll_ctl(epollfd, EPOLL_CTL_MOD, sockfd, &ev);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================= 网络逻辑 ================= */
|
||||
static void try_connect(void)
|
||||
{
|
||||
if(sockfd > 0){
|
||||
if (sockfd > 0) {
|
||||
close(sockfd);
|
||||
sockfd = -1;
|
||||
}
|
||||
@@ -170,14 +160,14 @@ static void try_connect(void)
|
||||
addr.sin_port = htons(peer_port);
|
||||
inet_pton(AF_INET, peer_ip, &addr.sin_addr);
|
||||
|
||||
for(i = 0;i < 10; ++ i){
|
||||
for (i = 0; i < 10; ++i) {
|
||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd < 0) {
|
||||
perror("socket");
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUGLOG("connect try %d...\n", i + 1);
|
||||
DEBUGLOG("connect try %d... %s:%d\n", i + 1, peer_ip, peer_port);
|
||||
if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
|
||||
DEBUGLOG("connect success: %s:%d\n", peer_ip, peer_port);
|
||||
|
||||
@@ -190,7 +180,10 @@ static void try_connect(void)
|
||||
epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev);
|
||||
|
||||
state = ONLINE;
|
||||
pending_send_all();
|
||||
if (pending.head) {
|
||||
ev.events = EPOLLIN | EPOLLOUT;
|
||||
epoll_ctl(epollfd, EPOLL_CTL_MOD, sockfd, &ev);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -234,17 +227,10 @@ static void handle_socket_readable(void)
|
||||
static void handle_socket_writable(void)
|
||||
{
|
||||
pending_send_all();
|
||||
if (!pending.head) {
|
||||
struct epoll_event ev;
|
||||
ev.events = EPOLLIN; // 只监听读
|
||||
ev.data.fd = sockfd;
|
||||
epoll_ctl(epollfd, EPOLL_CTL_MOD, sockfd, &ev);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* ================= perf buffer 回调 ================= */
|
||||
static void handle_event(void *ctx, int cpu, void *data, __u32 size)
|
||||
/* ================= ring buffer 回调 ================= */
|
||||
static int handle_event(void *ctx, void *data, size_t size)
|
||||
{
|
||||
struct replica_event *evt = data;
|
||||
switch (evt->type) {
|
||||
@@ -252,20 +238,18 @@ static void handle_event(void *ctx, int cpu, void *data, __u32 size)
|
||||
case EVENT_SSYNC:
|
||||
strncpy(peer_ip, evt->sync.ip, sizeof(peer_ip));
|
||||
peer_port = evt->sync.port;
|
||||
peer_seq = evt->sync.seq;
|
||||
DEBUGLOG("SSYNC [seq:%lld], [%s:%d]\n", peer_seq, peer_ip, peer_port);
|
||||
|
||||
DEBUGLOG("SSYNC [%s:%d]\n", peer_ip, peer_port);
|
||||
state = OFFLINE;
|
||||
pending_gc(peer_seq);
|
||||
break;
|
||||
|
||||
case EVENT_COMPLETED_CMD:
|
||||
// DEBUGLOG("CMD [seq:%lld], cmd:\n[\n%s]\n", evt->complete.seq, evt->complete.cmd);
|
||||
pending_push(evt->complete.seq,
|
||||
evt->complete.len,
|
||||
evt->complete.cmd);
|
||||
// 这里收到的可能是半个命令,或者是多个命令的粘包
|
||||
// 但对于转发器来说,只是字节流,直接 push 即可
|
||||
if (evt->complete.len > 0) {
|
||||
pending_push(evt->complete.len, evt->complete.cmd);
|
||||
}
|
||||
|
||||
if (state == ONLINE && sockfd >= 0) {
|
||||
if (state == ONLINE && sockfd >= 0 && pending.head) {
|
||||
struct epoll_event ev;
|
||||
ev.events = EPOLLIN | EPOLLOUT;
|
||||
ev.data.fd = sockfd;
|
||||
@@ -274,82 +258,80 @@ static void handle_event(void *ctx, int cpu, void *data, __u32 size)
|
||||
break;
|
||||
|
||||
case EVENT_SREADY:
|
||||
DEBUGLOG("SREADY \n");
|
||||
DEBUGLOG("SREADY\n");
|
||||
if (state == OFFLINE)
|
||||
try_connect();
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================= main ================= */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct replica_bpf *skel;
|
||||
struct perf_buffer *pb = NULL;
|
||||
int err;
|
||||
struct replica_bpf *skel;
|
||||
struct ring_buffer *rb = NULL;
|
||||
int err;
|
||||
|
||||
/* Open BPF application */
|
||||
skel = replica_bpf__open();
|
||||
if (!skel) {
|
||||
fprintf(stderr, "Failed to open BPF skeleton\n");
|
||||
return 1;
|
||||
}
|
||||
// 提高 rlimit 以允许加载 BPF
|
||||
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
|
||||
setrlimit(RLIMIT_MEMLOCK, &r);
|
||||
|
||||
/* Load & verify BPF programs */
|
||||
err = replica_bpf__load(skel);
|
||||
if (err) {
|
||||
fprintf(stderr, "Failed to load and verify BPF skeleton\n");
|
||||
goto cleanup;
|
||||
}
|
||||
skel = replica_bpf__open();
|
||||
if (!skel) {
|
||||
fprintf(stderr, "Failed to open BPF skeleton\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Attach tracepoint handler */
|
||||
err = replica_bpf__attach(skel);
|
||||
if (err) {
|
||||
fprintf(stderr, "Failed to attach BPF skeleton\n");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
printf("Successfully started! \n");
|
||||
|
||||
|
||||
pb = perf_buffer__new(bpf_map__fd(skel->maps.events), 8,
|
||||
handle_event, NULL, NULL, NULL);
|
||||
|
||||
if(!pb){
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
epollfd = epoll_create1(0);
|
||||
if (epollfd < 0) {
|
||||
fprintf(stderr, "epoll_create1 failed\n");
|
||||
err = replica_bpf__load(skel);
|
||||
if (err) {
|
||||
fprintf(stderr, "Failed to load BPF skeleton\n");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
struct epoll_event events[10];
|
||||
err = replica_bpf__attach(skel);
|
||||
if (err) {
|
||||
fprintf(stderr, "Failed to attach BPF skeleton\n");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
perf_buffer__poll(pb, 1000); // 处理事件
|
||||
printf("Successfully started! Monitoring TCP port 8888 (Kernel Side)...\n");
|
||||
|
||||
rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), handle_event, NULL, NULL);
|
||||
if (!rb) {
|
||||
fprintf(stderr, "Failed to create ring buffer\n");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
epollfd = epoll_create1(0);
|
||||
// ... (主循环保持不变) ...
|
||||
|
||||
// 主循环建议:
|
||||
while (1) {
|
||||
struct epoll_event events[10];
|
||||
|
||||
if(OFFLINE) continue;
|
||||
// 既然追求性能,Polling 依然是必要的
|
||||
// 10ms 的延迟对于 RingBuffer 消费是可以接受的
|
||||
int poll_timeout = (state == ONLINE) ? 10 : 100;
|
||||
|
||||
ring_buffer__poll(rb, poll_timeout);
|
||||
|
||||
if (state == OFFLINE) continue;
|
||||
|
||||
int nfds = epoll_wait(epollfd, events, 10, 0);
|
||||
for (int i = 0; i < nfds; i++) {
|
||||
if (events[i].data.fd == sockfd) {
|
||||
if (events[i].events & EPOLLIN) {
|
||||
handle_socket_readable(); // 快速消费接收数据
|
||||
}
|
||||
if (events[i].events & EPOLLOUT) {
|
||||
handle_socket_writable(); // 发送数据
|
||||
}
|
||||
if (events[i].events & EPOLLIN) handle_socket_readable();
|
||||
if (events[i].events & EPOLLOUT) handle_socket_writable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
perf_buffer__free(pb);
|
||||
|
||||
cleanup:
|
||||
pending_free();
|
||||
if (sockfd >= 0) close(sockfd);
|
||||
replica_bpf__destroy(skel);
|
||||
return -err;
|
||||
}
|
||||
// ... (清理代码保持不变) ...
|
||||
if (rb) ring_buffer__free(rb);
|
||||
pending_free();
|
||||
if (sockfd >= 0) close(sockfd);
|
||||
if (epollfd >= 0) close(epollfd);
|
||||
replica_bpf__destroy(skel);
|
||||
return -err;
|
||||
}
|
||||
Reference in New Issue
Block a user