Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-szweda committed Feb 5, 2024
1 parent a00de03 commit 7e8fa77
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
20 changes: 10 additions & 10 deletions libebpfdiscoveryskel/src/Handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ __attribute__((always_inline)) inline static int sessionFillIP(
}

__attribute__((always_inline)) inline static void handleRead(
bool handleVector,
struct pt_regs* ctx,
struct DiscoveryGlobalState* globalStatePtr,
struct DiscoveryAllSessionState* allSessionStatePtr,
Expand All @@ -163,15 +162,7 @@ __attribute__((always_inline)) inline static void handleRead(
}

if (sessionPtr->bufferCount == 0) {
bool isBeginningOfHttpRequest;
if(handleVector) {
isBeginningOfHttpRequest = dataProbeIsBeginningOfHttpRequest(readArgsPtr->buf, bytesCount);
}
else {
isBeginningOfHttpRequest = dataProbeIsBeginningOfHttpRequest((char*)readArgsPtr->iov[0].iov_base, bytesCount);
}

if (!isBeginningOfHttpRequest) {
if (!dataProbeIsBeginningOfHttpRequest(readArgsPtr->buf, bytesCount)) {
deleteTrackedSession((struct DiscoveryTrackedSessionKey*)&event.dataKey, sessionPtr);
LOG_TRACE(
ctx,
Expand Down Expand Up @@ -219,6 +210,15 @@ __attribute__((always_inline)) inline static void handleRead(
sessionPtr->bufferCount++;
}

__attribute__((always_inline)) inline static void handleReadVector(
struct pt_regs* ctx,
struct DiscoveryGlobalState* globalStatePtr,
struct DiscoveryAllSessionState* allSessionStatePtr,
struct ReadVectorArgs* readVectorArgsPtr,
ssize_t bytesCount) {
return;
}

__attribute__((always_inline)) inline static void handleClose(
struct pt_regs* ctx, struct DiscoveryGlobalState* globalStatePtr, struct DiscoveryAllSessionState* allSessionStatePtr, int fd) {
struct DiscoveryTrackedSessionKey trackedSessionKey = {};
Expand Down
6 changes: 3 additions & 3 deletions libebpfdiscoveryskel/src/SysTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ struct AcceptArgs {

struct ReadArgs {
__u32 fd;

// For recv, read, recvfrom
char* buf;
};

// For recvmsg
struct ReadVectorArgs {
__u32 fd;
const struct iovec* iov;
size_t iovlen;
};
Expand Down
22 changes: 14 additions & 8 deletions libebpfdiscoveryskel/src/SyscallProbes.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ struct {
__uint(max_entries, DISCOVERY_MAX_SESSIONS);
} runningReadArgsMap SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, __u64); // pid_tgid
__type(value, struct ReadVectorArgs);
__uint(max_entries, DISCOVERY_MAX_SESSIONS);
} runningReadVectorArgsMap SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, __u64); // pid_tgid
Expand Down Expand Up @@ -177,7 +184,7 @@ __attribute__((always_inline)) inline static int handleSysReadExit(struct pt_reg
return 0;
}

handleRead(false, ctx, globalStatePtr, allSessionStatePtr, readArgsPtr, bytesCount);
handleRead(ctx, globalStatePtr, allSessionStatePtr, readArgsPtr, bytesCount);
bpf_map_delete_elem(&runningReadArgsMap, &pidTgid);

return 0;
Expand Down Expand Up @@ -261,13 +268,13 @@ __attribute__((always_inline)) inline static int handleSysRecvmsgEntry(struct pt
bpf_map_update_elem(&runningConnectArgsMap, &pidTgid, &connectArgs, BPF_ANY);
}

struct ReadArgs readArgs = {
struct ReadVectorArgs readVectorArgs = {
.fd = trackedSessionKey.fd,
.iov = msg->msg_iov,
.iovlen = msg->msg_iovlen,

};
bpf_map_update_elem(&runningReadArgsMap, &pidTgid, &readArgs, BPF_ANY);
bpf_map_update_elem(&runningReadVectorArgsMap, &pidTgid, &readVectorArgs, BPF_ANY);

return 0;
}
Expand All @@ -292,14 +299,13 @@ __attribute__((always_inline)) inline static int handleSysRecvmsgExit(struct pt_
}
bpf_map_delete_elem(&runningConnectArgsMap, &pidTgid);

struct ReadArgs* readArgsPtr = (struct ReadArgs*)bpf_map_lookup_elem(&runningReadArgsMap, &pidTgid);
if (readArgsPtr == NULL) {
struct ReadVectorArgs* readVectorArgsPtr = (struct ReadVectorArgs*)bpf_map_lookup_elem(&runningReadVectorArgsMap, &pidTgid);
if (readVectorArgsPtr == NULL) {
return 0;
}

// TODO: Extend handleRead with recvmsg handling or implement handleRecvmsg
handleRead(true, ctx, globalStatePtr, allSessionStatePtr, readArgsPtr, bytesCount);
bpf_map_delete_elem(&runningReadArgsMap, &pidTgid);
handleReadVector(ctx, globalStatePtr, allSessionStatePtr, readVectorArgsPtr, bytesCount);
bpf_map_delete_elem(&runningReadVectorArgsMap, &pidTgid);

return 0;
}
Expand Down

0 comments on commit 7e8fa77

Please sign in to comment.