Skip to content

[0/2] Refactor Logging Infrastructure#485

Open
yaakov-stein wants to merge 3 commits intofacebook:mainfrom
yaakov-stein:refactor-log-infrastructure
Open

[0/2] Refactor Logging Infrastructure#485
yaakov-stein wants to merge 3 commits intofacebook:mainfrom
yaakov-stein:refactor-log-infrastructure

Conversation

@yaakov-stein
Copy link
Copy Markdown
Contributor

@yaakov-stein yaakov-stein commented Mar 20, 2026

Stack:

Summary

This sets the stage for adding logging support for CGROUP_SOCK_ADDR. The first step is renaming the logging infrastructure in a way that isn't packet-specific (or making it clear that certain parts are packet-specific and naming accordingly), and adding a union that allows us to add the necessary fields to bf_log to support this change.

No functional change (besides the bf_log_type enum helper methods).

@meta-cla meta-cla bot added the cla signed label Mar 20, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 20, 2026

Claude review of PR #485 (12b8bcf)

Clean refactoring that renames bf_pkthdr to bf_log_opt, restructures bf_log as a tagged union, and renames the log ELF stub. The rename is applied consistently across all 15 changed files. Previous review comments about missing assert(str), include ordering, and silent early return remain open — see existing inline comments.

Must fix

  • Stale BF_PKTHDR_LINK in benchmarkstools/benchmarks/main.cpp:244,422BF_PKTHDR_LINK was not renamed to BF_LOG_OPT_LINK, causing a build failure when benchmarks are enabled

Suggestions

  • Struct naming conventionsrc/libbpfilter/include/bpfilter/runtime.h:97struct _bf_log_pkt and struct _bf_log_sock_addr use _bf_ prefix which has no precedent for struct types in this codebase; all existing structs use bf_ prefix

Nits

  • Missing "Can't be NULL" for str paramsrc/libbpfilter/include/bpfilter/rule.h:32 — Doxygen for bf_log_type_from_str and bf_log_opt_from_str documents "Can't be NULL" for the output param but not for str, unlike other _from_str declarations
  • Union missing Doxygensrc/libbpfilter/include/bpfilter/runtime.h:175 — Anonymous union in struct bf_log is the only field without a Doxygen comment
  • Doxygen multi-line format on union commentsrc/libbpfilter/include/bpfilter/runtime.h:170 — Multi-line Doxygen comment has text on first and last lines; style guide requires empty first/last lines for Doxygen
  • Copyright year droppedsrc/libbpfilter/bpf/pkt_log.bpf.c:3 — Year was removed from the copyright notice during the file rename; all other .bpf.c files retain their year

Workflow run

@yaakov-stein yaakov-stein changed the title [0/2] Refactor Log Infrastructure [0/2][CGROUP_SOCK_ADDR Logging] Refactor Log Infrastructure Mar 20, 2026
@yaakov-stein yaakov-stein force-pushed the refactor-log-infrastructure branch from c911410 to 7aeeab0 Compare March 27, 2026 17:06
/** Destination address (4 bytes for IPv4, 16 for IPv6). */
bf_aligned(8) __u8 daddr[sizeof(struct in6_addr)];
} sock_addr;
} payload;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd ideally have left this unnamed as there isn't much of a benefit of naming it and it now requires ->payload.pkt/sock_addr, but doxygen requires unions to be named :(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave it unnamed, we'll find a way to deal with the Doxygen issue differently.

@yaakov-stein yaakov-stein marked this pull request as ready for review March 27, 2026 19:07
@yaakov-stein yaakov-stein force-pushed the refactor-log-infrastructure branch from 7aeeab0 to 4a18e6d Compare March 27, 2026 19:45
@yaakov-stein yaakov-stein force-pushed the refactor-log-infrastructure branch from 4a18e6d to 4c82f48 Compare March 27, 2026 20:26
@yaakov-stein yaakov-stein force-pushed the refactor-log-infrastructure branch from 4c82f48 to fe77c33 Compare March 27, 2026 20:59
@yaakov-stein yaakov-stein marked this pull request as draft March 29, 2026 03:53
@yaakov-stein yaakov-stein force-pushed the refactor-log-infrastructure branch 2 times, most recently from 5c7c805 to 61e87f4 Compare March 29, 2026 19:25
@yaakov-stein yaakov-stein force-pushed the refactor-log-infrastructure branch from 61e87f4 to 3634fd4 Compare March 29, 2026 19:46
/**
* @brief Packet log payload fields (XDP, TC, NF, cgroup_skb).
*/
struct _bf_log_pkt
Copy link
Copy Markdown
Contributor Author

@yaakov-stein yaakov-stein Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This are only necessary internally but live in a public header file... I went with the approach of using the _bf prefix, but there weren't any examples to follow. Let me know if there's a better appraoch (maybe defining the structs directly inside the union?).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it only internal? This file can be included by clients reading from the ring buffer. I would suggest removing the _ prefix, as it's reserved in C. It's fine to use it in our own translation unit, as we control includes and naming, but it's not for public headers.

@yaakov-stein yaakov-stein marked this pull request as ready for review March 29, 2026 20:01
@yaakov-stein yaakov-stein changed the title [0/2][CGROUP_SOCK_ADDR Logging] Refactor Log Infrastructure [0/2] Refactor Logging Infrastructure Mar 29, 2026
Rename the packet logging ELF stub from `log` to `pkt_log` to
establish naming symmetry with the upcoming `sock_log` stub for
socket-based hooks.
Add a bf_log_type discriminator and restructure bf_log as a tagged
union with packet and socket variants. Packet-specific fields
(pkt_size, headers, l2hdr/l3hdr/l4hdr) move into the pkt variant.
The sock variant (pid, comm) is defined but not yet populated.

The struct sizes to the larger packet variant, so total size is
unchanged for ring buffer reservations.
@yaakov-stein yaakov-stein force-pushed the refactor-log-infrastructure branch from 3634fd4 to 12b8bcf Compare March 29, 2026 22:05
/** Destination address (4 bytes for IPv4, 16 for IPv6). */
bf_aligned(8) __u8 daddr[sizeof(struct in6_addr)];
} sock_addr;
} payload;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave it unnamed, we'll find a way to deal with the Doxygen issue differently.

/**
* @brief Packet log payload fields (XDP, TC, NF, cgroup_skb).
*/
struct _bf_log_pkt
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it only internal? This file can be included by clients reading from the ring buffer. I would suggest removing the _ prefix, as it's reserved in C. It's fine to use it in our own translation unit, as we control includes and naming, but it's not for public headers.

Comment on lines +107 to +111
/** User-requested headers, as defined in the rule. */
__u8 req_headers;

/** Logged headers, as not all hooks can access all headers. */
__u8 headers;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the :4 bitfield?


for (int i = 0; i < _BF_LOG_OPT_MAX; ++i) {
if (bf_streq_i(str, _bf_log_opt_strs[i])) {
*opt = (enum bf_log_opt)i;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need sock_addr logging to be configurable, there isn't that much data to log, and the only helper we'll use do current->tgid << 32 | current->pid. Hence, we can keep bf_pkthdr for packet logging options.

Comment on lines +22 to +48
static const char *_bf_log_type_strs[] = {
[BF_LOG_TYPE_PACKET] = "packet",
[BF_LOG_TYPE_SOCK_ADDR] = "sock_addr",
};
static_assert_enum_mapping(_bf_pkthdr_strs, _BF_PKTHDR_MAX);
static_assert_enum_mapping(_bf_log_type_strs, _BF_LOG_TYPE_MAX);

const char *bf_pkthdr_to_str(enum bf_pkthdr hdr)
const char *bf_log_type_to_str(enum bf_log_type log_type)
{
if (hdr < 0 || hdr >= _BF_PKTHDR_MAX)
return "<bf_pkthdr unknown>";
if (log_type < 0 || log_type >= _BF_LOG_TYPE_MAX)
return "<bf_log_type unknown>";

return _bf_pkthdr_strs[hdr];
return _bf_log_type_strs[log_type];
}

int bf_pkthdr_from_str(const char *str, enum bf_pkthdr *hdr)
int bf_log_type_from_str(const char *str, enum bf_log_type *log_type)
{
assert(hdr);
assert(log_type);

for (int i = 0; i < _BF_PKTHDR_MAX; ++i) {
if (bf_streq_i(str, _bf_pkthdr_strs[i])) {
*hdr = (enum bf_pkthdr)i;
for (int i = 0; i < _BF_LOG_TYPE_MAX; ++i) {
if (bf_streq_i(str, _bf_log_type_strs[i])) {
*log_type = (enum bf_log_type)i;
return 0;
}
}

return -EINVAL;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this as we don't use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants