Skip to content

Commit

Permalink
Remove underscore prefix from macros
Browse files Browse the repository at this point in the history
  • Loading branch information
hack3ric committed Nov 5, 2024
1 parent eb1f214 commit d851ddf
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 65 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ endif # KERNEL_VMLINUX

# Specify whether to use system vmlinux
ifneq ($(BPF_USE_SYSTEM_VMLINUX),)
BPF_CFLAGS += -D_MIMIC_BPF_USE_SYSTEM_VMLINUX
BPF_CFLAGS += -DMIMIC_BPF_USE_SYSTEM_VMLINUX
use_system_vmlinux_req := bpf/vmlinux/system.h
else
BPF_CFLAGS += -D_MIMIC_BPF_TARGET_ARCH_$(shell $(CC) -dumpmachine | sed 's/-.*//')
BPF_CFLAGS += -DMIMIC_BPF_TARGET_ARCH_$(shell $(CC) -dumpmachine | sed 's/-.*//')
endif # BPF_USE_SYSTEM_VMLINUX

# Mimic runtime directory, where the lock files are stored
Expand Down Expand Up @@ -174,7 +174,7 @@ else
endif

$(mimic_bpf_obj): bpf/%.o: bpf/%.c $(mimic_bpf_headers) $(use_system_vmlinux_req) $(check_options)
$(BPF_CC) $(BPF_CFLAGS) -D_MIMIC_BPF -c -o $@ $<
$(BPF_CC) $(BPF_CFLAGS) -DMIMIC_BPF -c -o $@ $<

out/mimic.bpf.o: $(mimic_bpf_obj)
$(mkdir_p)
Expand Down
6 changes: 3 additions & 3 deletions bpf/main.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _MIMIC_BPF_MIMIC_H
#define _MIMIC_BPF_MIMIC_H
#ifndef MIMIC_BPF_MIMIC_H
#define MIMIC_BPF_MIMIC_H

#include "vmlinux.h"

Expand Down Expand Up @@ -172,4 +172,4 @@ static inline bool ipv6_is_ext(__u8 nexthdr) {
(val) = 2; \
})

#endif // _MIMIC_BPF_MIMIC_H
#endif // MIMIC_BPF_MIMIC_H
30 changes: 15 additions & 15 deletions bpf/vmlinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@
#ifndef _BPF_VMLINUX_H
#define _BPF_VMLINUX_H

#if defined(_MIMIC_BPF_USE_SYSTEM_VMLINUX)
#if defined(MIMIC_BPF_USE_SYSTEM_VMLINUX)
#include "vmlinux/system.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_i386) || defined(_MIMIC_BPF_TARGET_ARCH_i486) || defined(_MIMIC_BPF_TARGET_ARCH_i586) || defined(_MIMIC_BPF_TARGET_ARCH_i686)
#elif defined(MIMIC_BPF_TARGET_ARCH_i386) || defined(MIMIC_BPF_TARGET_ARCH_i486) || defined(MIMIC_BPF_TARGET_ARCH_i586) || defined(MIMIC_BPF_TARGET_ARCH_i686)
#include "vmlinux/i386.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_x86_64)
#elif defined(MIMIC_BPF_TARGET_ARCH_x86_64)
#include "vmlinux/x86_64.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_arm)
#elif defined(MIMIC_BPF_TARGET_ARCH_arm)
#include "vmlinux/arm.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_aarch64)
#elif defined(MIMIC_BPF_TARGET_ARCH_aarch64)
#include "vmlinux/aarch64.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_riscv64)
#elif defined(MIMIC_BPF_TARGET_ARCH_riscv64)
#include "vmlinux/riscv64.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_loongarch64) || defined(_MIMIC_BPF_TARGET_ARCH_loong64)
#elif defined(MIMIC_BPF_TARGET_ARCH_loongarch64) || defined(MIMIC_BPF_TARGET_ARCH_loong64)
#include "vmlinux/loongarch64.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_ppc) || defined(_MIMIC_BPF_TARGET_ARCH_powerpc)
#elif defined(MIMIC_BPF_TARGET_ARCH_ppc) || defined(MIMIC_BPF_TARGET_ARCH_powerpc)
#include "vmlinux/ppc.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_ppc64) || defined(_MIMIC_BPF_TARGET_ARCH_powerpc64)
#elif defined(MIMIC_BPF_TARGET_ARCH_ppc64) || defined(MIMIC_BPF_TARGET_ARCH_powerpc64)
#include "vmlinux/ppc64.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_ppc64le) || defined(_MIMIC_BPF_TARGET_ARCH_powerpc64le)
#elif defined(MIMIC_BPF_TARGET_ARCH_ppc64le) || defined(MIMIC_BPF_TARGET_ARCH_powerpc64le)
#include "vmlinux/ppc64le.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_mips)
#elif defined(MIMIC_BPF_TARGET_ARCH_mips)
#include "vmlinux/mips.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_mipsel)
#elif defined(MIMIC_BPF_TARGET_ARCH_mipsel)
#include "vmlinux/mipsel.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_mips64)
#elif defined(MIMIC_BPF_TARGET_ARCH_mips64)
#include "vmlinux/mips64.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_mips64el)
#elif defined(MIMIC_BPF_TARGET_ARCH_mips64el)
#include "vmlinux/mips64el.h" // IWYU pragma: export
#elif defined(_MIMIC_BPF_TARGET_ARCH_s390x)
#elif defined(MIMIC_BPF_TARGET_ARCH_s390x)
#include "vmlinux/s390x.h" // IWYU pragma: export
#else
#include <asm/types.h> // IWYU pragma: export
Expand Down
12 changes: 6 additions & 6 deletions common/checksum.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _MIMIC_COMMON_CHECKSUM_H
#define _MIMIC_COMMON_CHECKSUM_H
#ifndef MIMIC_COMMON_CHECKSUM_H
#define MIMIC_COMMON_CHECKSUM_H

#ifdef _MIMIC_BPF
#ifdef MIMIC_BPF
// clang-format off
#include "bpf/vmlinux.h"
#include <bpf/bpf_helpers.h>
Expand All @@ -16,7 +16,7 @@
static inline __u32 u32_fold(__u32 num) { return (num & 0xffff) + (num >> 16); }
static inline __u16 csum_fold(__u32 csum) { return ~u32_fold(u32_fold(csum)); }

#ifdef _MIMIC_BPF
#ifdef MIMIC_BPF

static inline __u32 calc_ctx_csum(__u32 data, __u32 data_end, __u32 off) {
__u32 csum = 0;
Expand Down Expand Up @@ -45,6 +45,6 @@ static inline __u32 calc_csum(void* data, size_t data_len) {
return result;
}

#endif // _MIMIC_BPF
#endif // MIMIC_BPF

#endif // _MIMIC_COMMON_CHECKSUM_H
#endif // MIMIC_COMMON_CHECKSUM_H
14 changes: 7 additions & 7 deletions common/defs.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _MIMIC_COMMON_DEFS_H
#define _MIMIC_COMMON_DEFS_H
#ifndef MIMIC_COMMON_DEFS_H
#define MIMIC_COMMON_DEFS_H

#ifdef _MIMIC_BPF
#ifdef MIMIC_BPF
// clang-format off
#include "bpf/vmlinux.h"
#include <bpf/bpf_helpers.h>
Expand Down Expand Up @@ -64,7 +64,7 @@
#define br_likely
#endif

#ifdef _MIMIC_BPF
#ifdef MIMIC_BPF

// Some missing declaration of vmlinux.h

Expand Down Expand Up @@ -134,7 +134,7 @@ static inline void cleanup_malloc_str(char** ptr) { cleanup_malloc((void*)ptr);
#define _cleanup_malloc __attribute__((__cleanup__(cleanup_malloc)))
#define _cleanup_malloc_str __attribute__((__cleanup__(cleanup_malloc_str)))

#endif // _MIMIC_BPF
#endif // MIMIC_BPF

// max: "[%pI6]:%d\0"
#define IP_PORT_MAX_LEN (INET6_ADDRSTRLEN + 2 + 5 + 1)
Expand All @@ -160,7 +160,7 @@ static inline void cleanup_malloc_str(char** ptr) { cleanup_malloc((void*)ptr);
// On eBPF, these markers are just for convenience, so that I can get a comprehensive list of texts.
// In the future, logging should be rewritten so that eBPF should only send structurized information
// and let userspace call gettext.
#ifndef _MIMIC_BPF
#ifndef MIMIC_BPF
// #define _(text) text
static inline __attribute__((__format_arg__(1))) const char* _(const char* text) { return text; }
#define gettext(text) _(text)
Expand Down Expand Up @@ -397,4 +397,4 @@ struct rb_item {
// additional buffer follows
};

#endif // _MIMIC_COMMON_DEFS_H
#endif // MIMIC_COMMON_DEFS_H
6 changes: 3 additions & 3 deletions common/log.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _COMMON_LOG_H
#define _COMMON_LOG_H
#ifndef MIMIC_COMMON_LOG_H
#define MIMIC_COMMON_LOG_H

#include "defs.h" // IWYU pragma: keep

Expand All @@ -22,4 +22,4 @@ void log_any(int level, const char* fmt, ...);
#define log_debug(fmt, ...) log_any(LOG_DEBUG, fmt, ##__VA_ARGS__)
#define log_trace(fmt, ...) log_any(LOG_TRACE, fmt, ##__VA_ARGS__)

#endif
#endif // MIMIC_COMMON_LOG_H
6 changes: 3 additions & 3 deletions common/log_impl.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _COMMON_LOG_IMPL_H
#define _COMMON_LOG_IMPL_H
#ifndef MIMIC_COMMON_LOG_IMPL_H
#define MIMIC_COMMON_LOG_IMPL_H

#include <stdarg.h>
#include <stdio.h>
Expand Down Expand Up @@ -27,4 +27,4 @@ void log_any(int level, const char* fmt, ...) {
va_end(ap);
}

#endif
#endif // MIMIC_COMMON_LOG_IMPL_H
12 changes: 6 additions & 6 deletions common/try.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _MIMIC_COMMON_TRY_H
#define _MIMIC_COMMON_TRY_H
#ifndef MIMIC_COMMON_TRY_H
#define MIMIC_COMMON_TRY_H

#ifdef _MIMIC_BPF
#ifdef MIMIC_BPF
#else
#include <errno.h>
#include <string.h> // IWYU pragma: keep
Expand Down Expand Up @@ -93,7 +93,7 @@
})

// `errno` is not available in BPF
#ifndef _MIMIC_BPF
#ifndef MIMIC_BPF

// Same as `try`, but returns -errno
#define try_e(expr, ...) \
Expand Down Expand Up @@ -139,7 +139,7 @@
_ptr; \
})

#endif // _MIMIC_BPF
#endif // MIMIC_BPF

// Tests int return value from a function, but return a different value when failed.
#define try_ret(expr, ret) \
Expand Down Expand Up @@ -169,4 +169,4 @@

#define strret strerror(-_ret)

#endif // _MIMIC_COMMON_TRY_H
#endif // MIMIC_COMMON_TRY_H
2 changes: 1 addition & 1 deletion kmod/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
obj-m += mimic.o
mimic-objs := main.o
ccflags-y := -D_MIMIC_KMOD -std=gnu99 -Wno-error=declaration-after-statement
ccflags-y := -DMIMIC_KMOD -std=gnu99 -Wno-error=declaration-after-statement

KERNEL_UNAME ?= $(shell uname -r)
SYSTEM_BUILD_DIR := /lib/modules/$(KERNEL_UNAME)/build
Expand Down
18 changes: 9 additions & 9 deletions kmod/csum-hack.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#ifndef _MIMIC_KMOD_CSUM_HACK_H
#define _MIMIC_KMOD_CSUM_HACK_H
#ifndef MIMIC_KMOD_CSUM_HACK_H
#define MIMIC_KMOD_CSUM_HACK_H

#if defined(_MIMIC_KMOD)
#if defined(MIMIC_KMOD)
#include <linux/stddef.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#elif defined(_MIMIC_BPF)
#elif defined(MIMIC_BPF)
// clang-format off
#include "bpf/vmlinux.h"
#include <bpf/bpf_helpers.h>
// clang-format on
#endif

#ifdef _MIMIC_KMOD
#ifdef MIMIC_KMOD
int csum_hack_init(void);
void csum_hack_exit(void);
#endif

#define MAGIC_FLAG1 0xfc9e39d5
#define MAGIC_FLAG2 0x4eb37b03751ff785

#ifdef _MIMIC_KMOD
#ifdef MIMIC_KMOD
static inline int change_csum_offset(struct sk_buff* skb, __u16 proto) {
if (skb->ip_summed != CHECKSUM_PARTIAL) return -1;
switch (proto) {
Expand All @@ -37,7 +37,7 @@ static inline int change_csum_offset(struct sk_buff* skb, __u16 proto) {
}
#endif

#ifdef _MIMIC_BPF
#ifdef MIMIC_BPF
#if defined(MIMIC_CHECKSUM_HACK_kfunc)
static inline int mimic_skb_ip_summed(struct __sk_buff* skb) {
struct sk_buff* mimic_inspect_skb(struct __sk_buff * skb) __ksym;
Expand All @@ -54,5 +54,5 @@ static inline int mimic_change_csum_offset(struct __sk_buff* skb, __u16 protocol
}

#endif // MIMIC_CHECKSUM_HACK_*
#endif // _MIMIC_BPF
#endif // _MIMIC_KMOD_CSUM_HACK_H
#endif // MIMIC_BPF
#endif // MIMIC_KMOD_CSUM_HACK_H
6 changes: 3 additions & 3 deletions src/log.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _MIMIC_LOG_H
#define _MIMIC_LOG_H
#ifndef MIMIC_LOG_H
#define MIMIC_LOG_H

#include <bpf/libbpf.h>
#include <linux/tcp.h>
Expand All @@ -15,4 +15,4 @@ void log_destroy(enum log_level level, struct conn_tuple* conn, enum destroy_typ
int libbpf_print_fn(enum libbpf_print_level level, const char* format, va_list args);
int handle_log_event(struct log_event* e);

#endif // _MIMIC_LOG_H
#endif // MIMIC_LOG_H
6 changes: 3 additions & 3 deletions src/main.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _MIMIC_MIMIC_H
#define _MIMIC_MIMIC_H
#ifndef MIMIC_MAIN_H
#define MIMIC_MAIN_H

#include <bpf/bpf.h>
#include <errno.h>
Expand Down Expand Up @@ -119,4 +119,4 @@ static inline int bpf_map_iter_next(struct bpf_map_iter* iter, void* key) {
}
}

#endif // _MIMIC_MIMIC_H
#endif // MIMIC_MAIN_H
6 changes: 3 additions & 3 deletions tools/vendor-vmlinux-h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ EOF
EOF
/usr/sbin/bpftool btf dump file $build_path/vmlinux format c >bpf/vmlinux/$arch.h
echo -n "#elif defined(_MIMIC_BPF_TARGET_ARCH_${arch[0]})" >>bpf/vmlinux.h
echo -n "#elif defined(MIMIC_BPF_TARGET_ARCH_${arch[0]})" >>bpf/vmlinux.h
for _arch in ${arch[@]:1}; do
echo -n " || defined(_MIMIC_BPF_TARGET_ARCH_$_arch)" >>bpf/vmlinux.h
echo -n " || defined(MIMIC_BPF_TARGET_ARCH_$_arch)" >>bpf/vmlinux.h
done
echo >>bpf/vmlinux.h
echo "#include \"vmlinux/$arch.h\" // IWYU pragma: export" >>bpf/vmlinux.h
Expand All @@ -56,7 +56,7 @@ cat >bpf/vmlinux.h <<EOF
#ifndef _BPF_VMLINUX_H
#define _BPF_VMLINUX_H
#if defined(_MIMIC_BPF_USE_SYSTEM_VMLINUX)
#if defined(MIMIC_BPF_USE_SYSTEM_VMLINUX)
#include "vmlinux/system.h" // IWYU pragma: export
EOF

Expand Down

0 comments on commit d851ddf

Please sign in to comment.