|
|
@@ -6,6 +6,7 @@
|
|
|
#include "mongo.c"
|
|
|
#include "kafka.c"
|
|
|
#include "cassandra.c"
|
|
|
+#include "rabbitmq.c"
|
|
|
|
|
|
|
|
|
#define PROTOCOL_UNKNOWN 0
|
|
|
@@ -17,6 +18,11 @@
|
|
|
#define PROTOCOL_MONGO 6
|
|
|
#define PROTOCOL_KAFKA 7
|
|
|
#define PROTOCOL_CASSANDRA 8
|
|
|
+#define PROTOCOL_RABBITMQ 9
|
|
|
+
|
|
|
+#define METHOD_UNKNOWN 0
|
|
|
+#define METHOD_PRODUCE 1
|
|
|
+#define METHOD_CONSUME 2
|
|
|
|
|
|
struct l7_event {
|
|
|
__u64 fd;
|
|
|
@@ -25,6 +31,7 @@ struct l7_event {
|
|
|
__u32 status;
|
|
|
__u64 duration;
|
|
|
__u8 protocol;
|
|
|
+ __u8 method;
|
|
|
};
|
|
|
|
|
|
struct {
|
|
|
@@ -36,6 +43,7 @@ struct {
|
|
|
struct rw_args_t {
|
|
|
__u64 fd;
|
|
|
char* buf;
|
|
|
+ __u64 size;
|
|
|
};
|
|
|
|
|
|
struct {
|
|
|
@@ -85,7 +93,19 @@ struct iov {
|
|
|
};
|
|
|
|
|
|
static inline __attribute__((__always_inline__))
|
|
|
-int trace_enter_write(__u64 fd, char *buf, __u64 size) {
|
|
|
+__u64 get_connection_timestamp(__u32 pid, __u64 fd) {
|
|
|
+ struct sk_info sk = {};
|
|
|
+ sk.pid = pid;
|
|
|
+ sk.fd = fd;
|
|
|
+ __u64 *timestamp = bpf_map_lookup_elem(&connection_timestamps, &sk);
|
|
|
+ if (timestamp) {
|
|
|
+ return *timestamp;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static inline __attribute__((__always_inline__))
|
|
|
+int trace_enter_write(struct trace_event_raw_sys_enter_rw__stub* ctx, __u64 fd, char *buf, __u64 size) {
|
|
|
__u64 id = bpf_get_current_pid_tgid();
|
|
|
struct l7_request req = {};
|
|
|
req.protocol = PROTOCOL_UNKNOWN;
|
|
|
@@ -108,6 +128,16 @@ int trace_enter_write(__u64 fd, char *buf, __u64 size) {
|
|
|
req.protocol = PROTOCOL_MYSQL;
|
|
|
} else if (is_mongo_query(buf, size)) {
|
|
|
req.protocol = PROTOCOL_MONGO;
|
|
|
+ } else if (is_rabbitmq_produce(buf, size)) {
|
|
|
+ struct l7_event e = {};
|
|
|
+ e.protocol = PROTOCOL_RABBITMQ;
|
|
|
+ e.fd = k.fd;
|
|
|
+ e.pid = k.pid;
|
|
|
+ e.status = 200;
|
|
|
+ e.method = METHOD_PRODUCE;
|
|
|
+ e.connection_timestamp = get_connection_timestamp(k.pid, k.fd);
|
|
|
+ bpf_perf_event_output(ctx, &l7_events, BPF_F_CURRENT_CPU, &e, sizeof(e));
|
|
|
+ return 0;
|
|
|
} else {
|
|
|
__s32 request_id = is_kafka_request(buf, size);
|
|
|
if (request_id > 0) {
|
|
|
@@ -141,6 +171,7 @@ int trace_enter_read(struct trace_event_raw_sys_enter_rw__stub* ctx) {
|
|
|
struct rw_args_t args = {};
|
|
|
args.fd = ctx->fd;
|
|
|
args.buf = ctx->buf;
|
|
|
+ args.size = ctx->size;
|
|
|
bpf_map_update_elem(&active_reads, &id, &args, BPF_ANY);
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -153,20 +184,37 @@ int trace_exit_read(struct trace_event_raw_sys_exit_rw__stub* ctx) {
|
|
|
if (!args) {
|
|
|
return 0;
|
|
|
}
|
|
|
- char *buf;
|
|
|
struct socket_key k = {};
|
|
|
k.pid = id >> 32;
|
|
|
k.fd = args->fd;
|
|
|
k.stream_id = -1;
|
|
|
- buf = args->buf;
|
|
|
+ char *buf = args->buf;
|
|
|
+ __u64 size = args->size;
|
|
|
|
|
|
bpf_map_delete_elem(&active_reads, &id);
|
|
|
+
|
|
|
if (ctx->ret <= 0) {
|
|
|
return 0;
|
|
|
}
|
|
|
+
|
|
|
+ struct l7_event e = {};
|
|
|
+ e.fd = k.fd;
|
|
|
+ e.pid = k.pid;
|
|
|
+ e.connection_timestamp = 0;
|
|
|
+ e.status = 0;
|
|
|
+ e.method = METHOD_UNKNOWN;
|
|
|
+
|
|
|
+ if (is_rabbitmq_consume(buf, size)) {
|
|
|
+ e.protocol = PROTOCOL_RABBITMQ;
|
|
|
+ e.status = 200;
|
|
|
+ e.method = METHOD_CONSUME;
|
|
|
+ e.connection_timestamp = get_connection_timestamp(k.pid, k.fd);
|
|
|
+ bpf_perf_event_output(ctx, &l7_events, BPF_F_CURRENT_CPU, &e, sizeof(e));
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
struct cassandra_header cassandra_response = {};
|
|
|
cassandra_response.stream_id = -1;
|
|
|
-
|
|
|
struct l7_request *req = bpf_map_lookup_elem(&active_l7_requests, &k);
|
|
|
if (!req) {
|
|
|
if (bpf_probe_read(&cassandra_response, sizeof(cassandra_response), (void *)(buf)) < 0) {
|
|
|
@@ -179,11 +227,7 @@ int trace_exit_read(struct trace_event_raw_sys_exit_rw__stub* ctx) {
|
|
|
}
|
|
|
}
|
|
|
__s32 request_id = req->request_id;
|
|
|
- struct l7_event e = {};
|
|
|
e.protocol = req->protocol;
|
|
|
- e.fd = k.fd;
|
|
|
- e.pid = k.pid;
|
|
|
- e.connection_timestamp = 0;
|
|
|
__u64 ns = req->ns;
|
|
|
__u8 partial = req->partial;
|
|
|
bpf_map_delete_elem(&active_l7_requests, &k);
|
|
|
@@ -216,13 +260,7 @@ int trace_exit_read(struct trace_event_raw_sys_exit_rw__stub* ctx) {
|
|
|
return 0;
|
|
|
}
|
|
|
e.duration = bpf_ktime_get_ns() - ns;
|
|
|
- struct sk_info sk = {};
|
|
|
- sk.pid = k.pid;
|
|
|
- sk.fd = k.fd;
|
|
|
- __u64 *timestamp = bpf_map_lookup_elem(&connection_timestamps, &sk);
|
|
|
- if (timestamp) {
|
|
|
- e.connection_timestamp = *timestamp;
|
|
|
- }
|
|
|
+ e.connection_timestamp = get_connection_timestamp(k.pid, k.fd);
|
|
|
bpf_perf_event_output(ctx, &l7_events, BPF_F_CURRENT_CPU, &e, sizeof(e));
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -233,17 +271,17 @@ int sys_enter_writev(struct trace_event_raw_sys_enter_rw__stub* ctx) {
|
|
|
if (bpf_probe_read(&iov0, sizeof(struct iov), (void *)ctx->buf) < 0) {
|
|
|
return 0;
|
|
|
}
|
|
|
- return trace_enter_write(ctx->fd, iov0.buf, iov0.size);
|
|
|
+ return trace_enter_write(ctx, ctx->fd, iov0.buf, iov0.size);
|
|
|
}
|
|
|
|
|
|
SEC("tracepoint/syscalls/sys_enter_write")
|
|
|
int sys_enter_write(struct trace_event_raw_sys_enter_rw__stub* ctx) {
|
|
|
- return trace_enter_write(ctx->fd, ctx->buf, ctx->size);
|
|
|
+ return trace_enter_write(ctx, ctx->fd, ctx->buf, ctx->size);
|
|
|
}
|
|
|
|
|
|
SEC("tracepoint/syscalls/sys_enter_sendto")
|
|
|
int sys_enter_sendto(struct trace_event_raw_sys_enter_rw__stub* ctx) {
|
|
|
- return trace_enter_write(ctx->fd, ctx->buf, ctx->size);
|
|
|
+ return trace_enter_write(ctx, ctx->fd, ctx->buf, ctx->size);
|
|
|
}
|
|
|
|
|
|
SEC("tracepoint/syscalls/sys_enter_read")
|