Skip to content

Commit

Permalink
Merge pull request iovisor#519 from iovisor/bblanco_dev
Browse files Browse the repository at this point in the history
Fixes for clang frontend bugs and misc
  • Loading branch information
4ast committed May 2, 2016
2 parents e0f2cd1 + 0845567 commit c58cb19
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 23 deletions.
28 changes: 18 additions & 10 deletions examples/networking/distributed_bridge/tunnel_mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@ struct config {
};
BPF_TABLE("hash", int, struct config, conf, 1);

BPF_TABLE("hash", struct bpf_tunnel_key, int, tunkey2if, 1024);
struct tunnel_key {
u32 tunnel_id;
u32 remote_ipv4;
};
BPF_TABLE("hash", struct tunnel_key, int, tunkey2if, 1024);

BPF_TABLE("hash", int, struct bpf_tunnel_key, if2tunkey, 1024);
BPF_TABLE("hash", int, struct tunnel_key, if2tunkey, 1024);

// Handle packets from the encap device, demux into the dest tenant
int handle_ingress(struct __sk_buff *skb) {
struct bpf_tunnel_key tkey = {};
struct tunnel_key key;
bpf_skb_get_tunnel_key(skb, &tkey, sizeof(tkey), 0);

int *ifindex = tunkey2if.lookup(&tkey);
key.tunnel_id = tkey.tunnel_id;
key.remote_ipv4 = tkey.remote_ipv4;
int *ifindex = tunkey2if.lookup(&key);
if (ifindex) {
//bpf_trace_printk("ingress tunnel_id=%d remote_ip=%08x ifindex=%d\n",
// tkey.tunnel_id, tkey.remote_ipv4, *ifindex);
// key.tunnel_id, key.remote_ipv4, *ifindex);
// mark from external
skb->tc_index = 1;
bpf_clone_redirect(skb, *ifindex, 1/*ingress*/);
} else {
bpf_trace_printk("ingress invalid tunnel_id=%d\n", tkey.tunnel_id);
bpf_trace_printk("ingress invalid tunnel_id=%d\n", key.tunnel_id);
}

return 1;
Expand All @@ -33,7 +40,8 @@ int handle_ingress(struct __sk_buff *skb) {
// Handle packets from the tenant, mux into the encap device
int handle_egress(struct __sk_buff *skb) {
int ifindex = skb->ifindex;
struct bpf_tunnel_key *tkey_p, tkey = {};
struct bpf_tunnel_key tkey = {};
struct tunnel_key *key_p;
int one = 1;
struct config *cfg = conf.lookup(&one);

Expand All @@ -45,10 +53,10 @@ int handle_egress(struct __sk_buff *skb) {
return 1;
}

tkey_p = if2tunkey.lookup(&ifindex);
if (tkey_p) {
tkey.tunnel_id = tkey_p->tunnel_id;
tkey.remote_ipv4 = tkey_p->remote_ipv4;
key_p = if2tunkey.lookup(&ifindex);
if (key_p) {
tkey.tunnel_id = key_p->tunnel_id;
tkey.remote_ipv4 = key_p->remote_ipv4;
bpf_skb_set_tunnel_key(skb, &tkey, sizeof(tkey), 0);
bpf_clone_redirect(skb, cfg->tunnel_ifindex, 0/*egress*/);
}
Expand Down
10 changes: 6 additions & 4 deletions examples/networking/distributed_bridge/tunnel_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def run():
ifc_gc.append(vx.ifname)
else:
with ipdb.create(ifname="vxlan0", kind="vxlan", vxlan_id=0,
vxlan_link=ifc, vxlan_port=htons(4789),
vxlan_link=ifc, vxlan_port=4789,
vxlan_collect_metadata=True,
vxlan_learning=False) as vx:
vx.up()
Expand All @@ -66,12 +66,14 @@ def run():
if i != host_id:
v = ipdb.create(ifname="dummy%d%d" % (j , i), kind="dummy").up().commit()
ipaddr = "172.16.1.%d" % (100 + i)
tunkey2if_key = tunkey2if.Key(vni, IPAddress(ipaddr))
tunkey2if_key = tunkey2if.Key(vni)
tunkey2if_key.remote_ipv4 = IPAddress(ipaddr)
tunkey2if_leaf = tunkey2if.Leaf(v.index)
tunkey2if[tunkey2if_key] = tunkey2if_leaf

if2tunkey_key = if2tunkey.Key(v.index)
if2tunkey_leaf = if2tunkey.Leaf(vni, IPAddress(ipaddr))
if2tunkey_leaf = if2tunkey.Leaf(vni)
if2tunkey_leaf.remote_ipv4 = IPAddress(ipaddr)
if2tunkey[if2tunkey_key] = if2tunkey_leaf

ipr.tc("add", "sfq", v.index, "1:")
Expand Down Expand Up @@ -124,7 +126,7 @@ def run():
while retry < 0:
check = Popen(["ip", "addr", "show", "br%d" % j], stdout=PIPE, stderr=PIPE)
out = check.stdout.read()
checkip = "99.1.%d" % j
checkip = b"99.1.%d" % j
retry = out.find(checkip)

try:
Expand Down
2 changes: 1 addition & 1 deletion src/cc/bpf_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static void parse_type(IRBuilder<> &B, vector<Value *> *args, string *fmt,
*fmt += " ";
}
*fmt += "]";
} else if (PointerType *pt = dyn_cast<PointerType>(type)) {
} else if (isa<PointerType>(type)) {
*fmt += "0xl";
if (is_writer)
*fmt += "x";
Expand Down
6 changes: 6 additions & 0 deletions src/cc/frontends/clang/b_frontend_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ bool BMapDeclVisitor::VisitRecordDecl(RecordDecl *D) {
result_ += D->getName();
result_ += "\", [";
for (auto F : D->getDefinition()->fields()) {
if (F->isAnonymousStructOrUnion()) {
if (const RecordType *R = dyn_cast<RecordType>(F->getType()))
TraverseDecl(R->getDecl());
result_ += ", ";
continue;
}
result_ += "[";
TraverseDecl(F);
if (const ConstantArrayType *T = dyn_cast<ConstantArrayType>(F->getType()))
Expand Down
22 changes: 17 additions & 5 deletions src/python/bcc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def resolve(self, addr):
psym = ct.pointer(sym)
if lib.bcc_symcache_resolve(self.cache, addr, psym) < 0:
return "[unknown]", 0
return sym.name, sym.offset
return sym.name.decode(), sym.offset

def resolve_name(self, name):
addr = ct.c_ulonglong()
Expand Down Expand Up @@ -250,15 +250,25 @@ def dump_func(self, func_name):
def _decode_table_type(desc):
if isinstance(desc, basestring):
return BPF.str2ctype[desc]
anon = []
fields = []
for t in desc[1]:
if len(t) == 2:
fields.append((t[0], BPF._decode_table_type(t[1])))
elif len(t) == 3:
if isinstance(t[2], list):
fields.append((t[0], BPF._decode_table_type(t[1]) * t[2][0]))
else:
elif isinstance(t[2], int):
fields.append((t[0], BPF._decode_table_type(t[1]), t[2]))
elif isinstance(t[2], basestring) and (
t[2] == u"union" or t[2] == u"struct"):
name = t[0]
if name == "":
name = "__anon%d" % len(anon)
anon.append(name)
fields.append((name, BPF._decode_table_type(t)))
else:
raise Exception("Failed to decode type %s" % str(t))
else:
raise Exception("Failed to decode type %s" % str(t))
base = ct.Structure
Expand All @@ -267,7 +277,8 @@ def _decode_table_type(desc):
base = ct.Union
elif desc[2] == u"struct":
base = ct.Structure
cls = type(str(desc[0]), (base,), dict(_fields_=fields))
cls = type(str(desc[0]), (base,), dict(_anonymous_=anon,
_fields_=fields))
return cls

def get_table(self, name, keytype=None, leaftype=None, reducer=None):
Expand Down Expand Up @@ -426,11 +437,12 @@ def detach_kretprobe(event):
def _check_path_symbol(cls, module, symname, addr):
sym = bcc_symbol()
psym = ct.pointer(sym)
if lib.bcc_resolve_symname(module, symname, addr or 0x0, psym) < 0:
if lib.bcc_resolve_symname(module.encode("ascii"),
symname.encode("ascii"), addr or 0x0, psym) < 0:
if not sym.module:
raise Exception("could not find library %s" % module)
raise Exception("could not determine address of symbol %s" % symname)
return sym.module, sym.offset
return sym.module.decode(), sym.offset

@staticmethod
def find_library(libname):
Expand Down
8 changes: 8 additions & 0 deletions tests/python/test_clang.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,5 +320,13 @@ def test_syntax_error(self):
with self.assertRaises(Exception):
b = BPF(text="""int failure(void *ctx) { if (); return 0; }""")

def test_nested_union(self):
text = """
BPF_TABLE("hash", struct bpf_tunnel_key, int, t1, 1);
"""
b = BPF(text=text)
t1 = b["t1"]
print(t1.Key().remote_ipv4)

if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions tests/python/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestHistogram(TestCase):
def test_simple(self):
b = BPF(text="""
#include <uapi/linux/ptrace.h>
#include <linux/bpf.h>
struct bpf_map;
BPF_HISTOGRAM(hist1);
BPF_HASH(stub);
int kprobe__htab_map_delete_elem(struct pt_regs *ctx, struct bpf_map *map, u64 *k) {
Expand All @@ -35,7 +35,7 @@ def test_simple(self):
def test_struct(self):
b = BPF(text="""
#include <uapi/linux/ptrace.h>
#include <linux/bpf.h>
struct bpf_map;
typedef struct { void *map; u64 slot; } Key;
BPF_HISTOGRAM(hist1, Key, 1024);
BPF_HASH(stub1);
Expand Down
2 changes: 1 addition & 1 deletion tests/python/test_stackid.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_simple(self):
stackid = stack_entries[k]
self.assertIsNotNone(stackid)
stack = stack_traces[stackid].ip
self.assertEqual(b.ksym(stack[0]), "htab_map_lookup_elem")
self.assertEqual(b.ksym(stack[0]), b"htab_map_lookup_elem")


if __name__ == "__main__":
Expand Down

0 comments on commit c58cb19

Please sign in to comment.