Skip to content

Commit

Permalink
[WIP] ibg integration
Browse files Browse the repository at this point in the history
  • Loading branch information
ccanel committed Jan 30, 2025
1 parent 282cb14 commit 60b6362
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 46 deletions.
42 changes: 21 additions & 21 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
version: 0.1
cli:
version: 1.22.8
version: 1.22.9
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
plugins:
sources:
- id: trunk
ref: v1.6.5
ref: v1.6.7
uri: https://github.com/trunk-io/plugins
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
runtimes:
enabled:
- [email protected]
- node@18.12.1
- node@18.20.5
- [email protected]
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
Expand All @@ -34,36 +34,36 @@ lint:
enabled:
- [email protected]
- [email protected]
- codespell@2.3.0
- codespell@2.4.1
- [email protected]
- gitleaks@8.21.2
- gitleaks@8.23.3
- [email protected]
- [email protected]
- [email protected]
- pragma-once
- [email protected]
- ruff@0.7.3
- semgrep@1.96.0
- ruff@0.9.3
- semgrep@1.104.0
- [email protected]
- [email protected]
- trivy@0.56.2
- trufflehog-git@3.83.7
- [email protected].3
- [email protected].0
- [email protected].4
- bandit@1.7.10
- black@24.10.0
- [email protected].296
- trivy@0.58.2
- trufflehog-git@3.88.2
- [email protected].4
- [email protected].4
- [email protected].7
- bandit@1.8.2
- black@25.1.0
- [email protected].358
- git-diff-check
- [email protected]
- isort@5.13.2
- markdownlint@0.42.0
- [email protected].1
- prettier@3.3.3
- ruff@0.7.3
- isort@6.0.0
- markdownlint@0.44.0
- [email protected].2
- prettier@3.4.2
- ruff@0.9.3
- [email protected]
- [email protected]
- trufflehog@3.83.7
- trufflehog@3.88.2
- [email protected]
actions:
disabled:
Expand Down
7 changes: 7 additions & 0 deletions ratemon/runtime/c/compile_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"directory": "/home/ccanel/src/ratemon/ratemon/runtime/c",
"command": "/usr/bin/g++ -g -std=c++20 -Wall -Wextra -shared -fPIC $< -ldl -L${BOOST_LIB} -lboost_thread -lbpf",
"file": "libratemon_interp.cpp.cpp"
}
]
87 changes: 62 additions & 25 deletions ratemon/runtime/c/libratemon_interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,46 +608,32 @@ void initial_scheduling(int fd) {
}
}

// For some reason, C++ function name mangling does not prevent us from
// overriding accept(), so we do not need 'extern "C"'.
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
static int (*real_accept)(int, struct sockaddr *, socklen_t *) =
(int (*)(int, struct sockaddr *, socklen_t *))dlsym(RTLD_NEXT, "accept");
if (real_accept == NULL) {
RM_PRINTF("ERROR: failed to query dlsym for 'accept': %s\n", dlerror());
return -1;
}
int fd = real_accept(sockfd, addr, addrlen);
if (fd == -1) {
RM_PRINTF("ERROR: real 'accept' failed\n");
return fd;
}
int check_family(struct sockaddr *addr) {
if (addr != NULL && addr->sa_family != AF_INET) {
RM_PRINTF("WARNING: got 'accept' for non-AF_INET sa_family=%u\n",
addr->sa_family);
RM_PRINTF("WARNING: got non-AF_INET sa_family=%u\n", addr->sa_family);
if (addr->sa_family == AF_INET6) {
RM_PRINTF("WARNING: (continued) got 'accept' for AF_INET6\n");
RM_PRINTF("WARNING: (continued) got AF_INET6\n");
}
return fd;
return -1;
}
return 0;
}

// If we have been signalled to quit, then do nothing more.
if (!run)
return fd;
void register_fd_for_monitoring(int fd) {
// One-time setup.
lock_setup.lock();
if (!setup_done) {
if (!setup()) {
lock_setup.unlock();
return fd;
return;
}
setup_done = true;
}
lock_setup.unlock();
// Look up the four-tuple.
struct rm_flow flow;
if (!get_flow(fd, &flow))
return fd;
return;
RM_PRINTF("flow: %u:%u->%u:%u\n", flow.remote_addr, flow.remote_port,
flow.local_addr, flow.local_port);
// Ignore flows that are not in the monitor port range.
Expand All @@ -657,17 +643,68 @@ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
"INFO: ignoring flow on remote port %u, not in monitor port range: "
"[%u, %u]\n",
flow.remote_port, monitor_port_start, monitor_port_end);
return fd;
return;
}
fd_to_flow[fd] = flow;
// Change the CCA to BPF_CUBIC.
if (!set_cca(fd, RM_BPF_CUBIC))
return fd;
return;
// Initial scheduling for this flow.
lock_scheduler.lock();
initial_scheduling(fd);
lock_scheduler.unlock();
RM_PRINTF("INFO: successful 'accept' for FD=%d, got FD=%d\n", sockfd, fd);
}

// For some reason, C++ function name mangling does not prevent us from
// overriding accept(), so we do not need 'extern "C"'.
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
static int (*real_accept)(int, struct sockaddr *, socklen_t *) =
(int (*)(int, struct sockaddr *, socklen_t *))dlsym(RTLD_NEXT, "accept");
if (real_accept == NULL) {
RM_PRINTF("ERROR: failed to query dlsym for 'accept': %s\n", dlerror());
return -1;
}
int fd = real_accept(sockfd, addr, addrlen);
if (fd == -1) {
RM_PRINTF("ERROR: real 'accept' failed\n");
return fd;
}
if (check_family(addr) != 0)
return fd;

// If we have been signalled to quit, then do nothing more.
if (!run)
return fd;

register_fd_for_monitoring(fd);
return fd;
}

// TODO: With iperf, the receiver (where we want to run this) is the listener
// and calls accept(). In ibg, the receiver is the initiator and calls
// connect(). Therefore, we need to support monitoring a flow from both accept()
// and connect().
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
static int (*real_connect)(int, struct sockaddr *, socklen_t *) =
(int (*)(int, struct sockaddr *, socklen_t *))dlsym(RTLD_NEXT, "connect");
if (real_connect == NULL) {
RM_PRINTF("ERROR: failed to query dlsym for 'connect': %s\n", dlerror());
return -1;
}
int fd = real_connect(sockfd, addr, addrlen);
if (fd == -1) {
RM_PRINTF("ERROR: real 'connect' failed\n");
return fd;
}
if (check_family(addr) != 0)
return fd;

// If we have been signalled to quit, then do nothing more.
if (!run)
return fd;

register_fd_for_monitoring(fd);
return fd;
}

Expand Down

0 comments on commit 60b6362

Please sign in to comment.