iovisor / bcc

BCC - Tools for BPF-based Linux IO analysis, networking, monitoring, and more
Apache License 2.0
20.34k stars 3.86k forks source link

Can create maps from user-space dynamically? #3505

Open masibw opened 3 years ago

masibw commented 3 years ago

Hi team. As the title says, in C code we can create bpf maps by using the bpf_create_map helper. Can't the same thing be done with bcc? (Judging from reference_guide.md, we can't)

My usecase is to create maps dynamically and insert the map into BPF_*_OF_MAPS. Can I do it with the current bcc?

yonghong-song commented 3 years ago

Yes, you can create map in user space,

$ grep bcc_create_map *
grep: pyperf: Is a directory
UseExternalMap.cc: * created map by calling libbpf bcc_create_map.
UseExternalMap.cc:  // create a map through bcc_create_map, bcc knows nothing about this map.
UseExternalMap.cc:  ctrl_map_fd = bcc_create_map(BPF_MAP_TYPE_ARRAY, "control", sizeof(uint32_t),
UseExternalMap.cc:  CHECK(ctrl_map_fd < 0, "bcc_create_map failure");

and you can use bpf_map_update_elem to populate key/value pairs in the map,

$ egrep -r bpf_map_update_elem
test_map_in_map.cc:             bpf_map_update_elem(inner_map, &key, &data, 0);
test_map_in_map.cc:            bpf_map_update_elem(inner_map, &key, &data, 0);
test_map_in_map.cc:            bpf_map_update_elem(inner_map, &key, &data, 0);
test_map_in_map.cc:             bpf_map_update_elem(inner_map, &key, &data, 0);
masibw commented 3 years ago

@yonghong-song, Thanks.

I think writing it to reference_docs.md is useful. BTW how to call bcc_create_map from python? Is there any interface/method?

yonghong-song commented 3 years ago

@yonghong-song, Thanks.

I think writing it to reference_docs.md is useful.

The bcc APIs are available in installed header files. Some of them is documented and some of them are not.

Since bcc is built with libbpf as a submodule, the libbpf functions are also available to bcc, bpf_map_update_elem is an example. If one is really want, the libbpf bpf_create_map (or its variant) can also be used in bcc application. But in general, it is still preferred to have high level C++ construct for map manipulations.

Some paragraph in reference guide to specify what API functions are available and where to find them might be indeed useful.

BTW how to call bcc_create_map from python? Is there any interface/method?

For python, you may need to add an interface for bcc_create_map. Check libbcc.py for some examples. Current python MapInMapHash/Array should already provided an interface to update the map.

masibw commented 3 years ago

The bcc APIs are available in installed header files. Some of them are documented and some of them are not. Some paragraph in the reference guide to specify what API functions are available and where to find them might be indeed useful.

You mean... it is difficult to provide document completely. So writing the way to [catch up/figure out] from implementation is better?

For python, you may need to add an interface for bcc_create_map. Check libbcc.py for some examples. Current python MapInMapHash/Array should already provided an interface to update the map.

Thanks for providing the reference, in fact, I'm not interested in the python interface(because I'm using gobpf). So I'll try to implement in gobpf by taking a look at the libbcc.py and so on.

Thanks.

yonghong-song commented 3 years ago

You mean... it is difficult to provide document completely. So writing the way to [catch up/figure out] from implementation is better?

We do provided high level APIs in reference guide (e.g., src/cc/api/BPF.h src/cc/api/BPFTable.h, src/cc/usdt.h), but we have some low level APIs in the installed header file and we didn't explicitly document them (bcc_elf.h, bcc_syms.h, libbpf.h, etc.) In most cases, people shouldn't use them, but they are available for advanced users. Maybe we can have a separate section in reference guide to document them.

Thanks for providing the reference, in fact, I'm not interested in the python interface(because I'm using gobpf). So I'll try to implement in gobpf by taking a look at the libbcc.py and so on.

Sounds good. FYI, libbcc.py is the interface for selective global functions in

lib = ct.CDLL("libbcc.so.0", use_errno=True)

which should have most high-level/low-level api functions (including the those functions from libbpf submodule bpf.h) available to you.

masibw commented 3 years ago

Maybe we can have a separate section in reference guide to document them.

I see. It sounds a good idea to me.

Thank you very much for your kind attention. I'll try it next time I have some free time.

Thank you very much for your kind attention. I'll try it next time I have some free time.