Skip to content

Commit

Permalink
Remove the BPF_EXPORT macro, use rewriter instead
Browse files Browse the repository at this point in the history
* Add a BFrontentAction to recognize any external linkage function as
  being available for export, and internally set the section attribute.
* Change bpf helpers to be static inline

Signed-off-by: Brenden Blanco <[email protected]>
  • Loading branch information
Brenden Blanco committed Jun 11, 2015
1 parent 2e657fe commit 004f005
Show file tree
Hide file tree
Showing 13 changed files with 14 additions and 26 deletions.
1 change: 0 additions & 1 deletion examples/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from subprocess import call

prog = """
BPF_EXPORT(hello)
int hello(void *ctx) {
char fmt[] = "Hello, World!\\n";
bpf_trace_printk(fmt, sizeof(fmt));
Expand Down
2 changes: 0 additions & 2 deletions examples/vlan_learning.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ BPF_TABLE("hash", u64, struct ifindex_leaf_t, egress, 4096);
// redirect based on mac -> out_ifindex (config-driven)
BPF_TABLE("hash", u64, struct ifindex_leaf_t, ingress, 4096);

BPF_EXPORT(handle_phys2virt)
int handle_phys2virt(struct __sk_buff *skb) {
BEGIN(ethernet);
PROTO(ethernet) {
Expand All @@ -37,7 +36,6 @@ int handle_phys2virt(struct __sk_buff *skb) {
return 1;
}

BPF_EXPORT(handle_virt2phys)
int handle_virt2phys(struct __sk_buff *skb) {
BEGIN(ethernet);
PROTO(ethernet) {
Expand Down
2 changes: 1 addition & 1 deletion examples/vlan_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ifindex_leaf_t(Structure):
("tx_bytes", c_ulonglong)]

# load the bpf program
b = BPF(src_file="examples/vlan_learning.c", debug=0)
b = BPF(src_file="examples/vlan_learning.c", debug=1)
phys_fn = b.load_func("handle_phys2virt", BPF.SCHED_CLS)
virt_fn = b.load_func("handle_virt2phys", BPF.SCHED_CLS)

Expand Down
6 changes: 6 additions & 0 deletions src/cc/b_frontend_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ BTypeVisitor::BTypeVisitor(ASTContext &C, Rewriter &rewriter, map<string, BPFTab
}

bool BTypeVisitor::VisitFunctionDecl(FunctionDecl *D) {
// put each non-static non-inline function decl in its own section, to be
// extracted by the MemoryManager
if (D->isExternallyVisible() && D->hasBody()) {
string attr = string("__attribute__((section(\".") + D->getName().str() + "\")))";
rewriter_.InsertText(D->getLocStart(), attr);
}
return true;
}

Expand Down
15 changes: 7 additions & 8 deletions src/cc/export/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ __attribute__((section("maps/" _table_type))) \
struct _name##_table_t _name

// packet parsing state machine helpers
#define STATE_MACHINE(name) \
BPF_EXPORT(name) int _##name(struct __sk_buff *skb)
#define BEGIN(next) \
u64 _parse_cursor = 0; \
goto next
Expand All @@ -54,10 +52,6 @@ name: ; \
struct name##_t *name __attribute__((deprecated("packet"))) = (void *)_parse_cursor; \
_parse_cursor += sizeof(*name);

// export this function to llvm by putting it into a specially named section
//#define BPF_EXPORT(_ret, _name, ...) SEC("." #_name) _ret _name(__VA_ARGS__)
#define BPF_EXPORT(_name) __attribute__((section("." #_name)))

char _license[4] SEC("license") = "GPL";

unsigned _version SEC("version") = LINUX_VERSION_CODE;
Expand Down Expand Up @@ -163,7 +157,7 @@ static inline void bpf_store_dword(void *skb, u64 off, u64 val) {

struct bpf_context;

//static inline __attribute__((always_inline))
static inline __attribute__((always_inline))
SEC("helpers")
u64 bpf_dext_pkt(void *pkt, u64 off, u64 bofs, u64 bsz) {
if (bofs == 0 && bsz == 8) {
Expand All @@ -186,7 +180,7 @@ u64 bpf_dext_pkt(void *pkt, u64 off, u64 bofs, u64 bsz) {
return 0;
}

//static inline __attribute__((always_inline))
static inline __attribute__((always_inline))
SEC("helpers")
void bpf_dins_pkt(void *pkt, u64 off, u64 bofs, u64 bsz, u64 val) {
// The load_xxx function does a bswap before returning the short/word/dword,
Expand Down Expand Up @@ -229,21 +223,25 @@ void bpf_dins_pkt(void *pkt, u64 off, u64 bofs, u64 bsz, u64 val) {
}
}

static inline __attribute__((always_inline))
SEC("helpers")
void * bpf_map_lookup_elem_(uintptr_t map, void *key) {
return bpf_map_lookup_elem((void *)map, key);
}

static inline __attribute__((always_inline))
SEC("helpers")
int bpf_map_update_elem_(uintptr_t map, void *key, void *value, u64 flags) {
return bpf_map_update_elem((void *)map, key, value, flags);
}

static inline __attribute__((always_inline))
SEC("helpers")
int bpf_map_delete_elem_(uintptr_t map, void *key) {
return bpf_map_delete_elem((void *)map, key);
}

static inline __attribute__((always_inline))
SEC("helpers")
int bpf_l3_csum_replace_(void *ctx, u64 off, u64 from, u64 to, u64 flags) {
switch (flags & 0xf) {
Expand All @@ -259,6 +257,7 @@ int bpf_l3_csum_replace_(void *ctx, u64 off, u64 from, u64 to, u64 flags) {
return bpf_l3_csum_replace(ctx, off, from, to, flags);
}

static inline __attribute__((always_inline))
SEC("helpers")
int bpf_l4_csum_replace_(void *ctx, u64 off, u64 from, u64 to, u64 flags) {
switch (flags & 0xf) {
Expand Down
3 changes: 0 additions & 3 deletions tests/cc/test_brb.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ BPF_TABLE("array", u32, u32, br2_rtr, 1);
// <mac, ifindex>
BPF_TABLE("hash", eth_addr_t, u32, br2_mac_ifindex, 1);

BPF_EXPORT(pem)
int pem(struct __sk_buff *skb) {
bpf_metadata_t meta = {};
u32 ifindex;
Expand Down Expand Up @@ -217,12 +216,10 @@ static int br_common(struct __sk_buff *skb, int which_br) {
return 0;
}

BPF_EXPORT(br1)
int br1(struct __sk_buff *skb) {
return br_common(skb, 1);
}

BPF_EXPORT(br2)
int br2(struct __sk_buff *skb) {
return br_common(skb, 2);
}
1 change: 0 additions & 1 deletion tests/cc/test_brb2.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ BPF_TABLE("hash", u32, u32, pem_dest, 256);
// <0, tx_pkts>
BPF_TABLE("array", u32, u32, pem_stats, 1);

BPF_EXPORT(pem)
int pem(struct __sk_buff *skb) {
u32 ifindex_in, *ifindex_p;

Expand Down
4 changes: 0 additions & 4 deletions tests/cc/test_call1.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ enum states {
S_IP
};

BPF_EXPORT(parse_ether)
int parse_ether(struct __sk_buff *skb) {
size_t cur = 0;
size_t next = cur + 14;
Expand All @@ -28,7 +27,6 @@ int parse_ether(struct __sk_buff *skb) {
return 1;
}

BPF_EXPORT(parse_arp)
int parse_arp(struct __sk_buff *skb) {
size_t cur = 14; // TODO: get from ctx
size_t next = cur + 28;
Expand All @@ -41,7 +39,6 @@ int parse_arp(struct __sk_buff *skb) {
return 1;
}

BPF_EXPORT(parse_ip)
int parse_ip(struct __sk_buff *skb) {
size_t cur = 14; // TODO: get from ctx
size_t next = cur + 20;
Expand All @@ -54,7 +51,6 @@ int parse_ip(struct __sk_buff *skb) {
return 1;
}

BPF_EXPORT(eop)
int eop(struct __sk_buff *skb) {
int key = S_EOP;
u64 *leaf = stats.lookup(&key);
Expand Down
1 change: 0 additions & 1 deletion tests/cc/test_stat1.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ struct IPLeaf {

BPF_TABLE("hash", struct IPKey, struct IPLeaf, stats, 256);

BPF_EXPORT(on_packet)
int on_packet(struct __sk_buff *skb) {
BEGIN(ethernet);

Expand Down
1 change: 0 additions & 1 deletion tests/cc/test_trace2.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ struct Ptr { u64 ptr; };
struct Counters { u64 stat1; };
BPF_TABLE("hash", struct Ptr, struct Counters, stats, 1024);

BPF_EXPORT(count_sched)
int count_sched(struct pt_regs *ctx) {
struct Ptr key = {.ptr=ctx->bx};
struct Counters zleaf = {0};
Expand Down
1 change: 0 additions & 1 deletion tests/cc/test_trace2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
struct Counters { u64 stat1; };
BPF_TABLE("hash", struct Ptr, struct Counters, stats, 1024);
BPF_EXPORT(count_sched)
int count_sched(struct pt_regs *ctx) {
struct Ptr key = {.ptr=ctx->bx};
stats.data[(u64)&key].stat1++;
Expand Down
2 changes: 0 additions & 2 deletions tests/cc/test_trace3.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ static u32 log2l(u64 v) {
return log2(v);
}

BPF_EXPORT(probe_blk_start_request)
int probe_blk_start_request(struct pt_regs *ctx) {
struct Request rq = {.rq = ctx->di};
struct Time tm = {.start = bpf_ktime_get_ns()};
requests.update(&rq, &tm);
return 0;
}

BPF_EXPORT(probe_blk_update_request)
int probe_blk_update_request(struct pt_regs *ctx) {
struct Request rq = {.rq = ctx->di};
struct Time *tm = requests.lookup(&rq);
Expand Down
1 change: 0 additions & 1 deletion tests/cc/test_xlate1.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ struct IPLeaf {
};
BPF_TABLE("hash", struct IPKey, struct IPLeaf, xlate, 1024);

BPF_EXPORT(on_packet)
int on_packet(struct __sk_buff *skb) {

u32 orig_dip = 0;
Expand Down

0 comments on commit 004f005

Please sign in to comment.