Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the use of custom keys in BPF_HASH_OF_MAPS #3500

Merged
merged 5 commits into from
Jun 22, 2021

Conversation

masibw
Copy link
Contributor

@masibw masibw commented Jun 20, 2021

close #3496
Hi team. I wrote a macro that can use custom key for BPF_HASH_OF_MAPS.

But in c++, get_map_in_map_table is shared to BPF_ARRAY_OF_MAPS.
So, when using BPF_ARRAY_OF_MAPS, we have to declare like this.

auto t = bpf.get_map_in_map_table<int>("maps_array");

Is it okay?

-- solved --
Maybe I could wrote about BPF_HASH_OF_MAPS macro. But .cc files have errors. I've never write c++ programs, Can anyone help me?

In file included from /home/*/bcc/tests/cc/test_map_in_map.cc:21:
In file included from /home/*/bcc/src/cc/api/BPF.h:25:
/home/*/bcc/src/cc/api/BPFTable.h:487:15: error: function 'ebpf::BPFMapInMapTable<custom_key>::update_value' is used but not defined in this translation unit, and cannot be defined in any other translation unit because its type does not have linkage
  StatusTuple update_value(const KeyType& key, const int& inner_map_fd);
              ^
/home/*/bcc/tests/cc/test_map_in_map.cc:182:13: note: used here
    res = t.update_value(hash_key, ex1_fd);

This is probably due to the fact that the definition and implementation are in separate files, but if I write it in the header file, the following error occurs. I don't know which one is closer to the solution, so I pushed the one where the definition and implementation are in separate files.

In file included from /home/*/bcc/tests/cc/test_map_in_map.cc:21:
In file included from /home/*/bcc/src/cc/api/BPF.h:25:
/home/*/bcc/src/cc/api/BPFTable.h:499:23: error: const_cast from 'char *(*)(const char *, int) throw()' to 'custom_key *' is not allowed
    if (!this->remove(const_cast<KeyType*>(&index)))

@chenhengqi
Copy link
Collaborator

The implementation of a template class should be in header file.

@masibw
Copy link
Contributor Author

masibw commented Jun 20, 2021

Thanks for replying @chenhengqi, like this code?

but I have this error.

In file included from /home/*/bcc/tests/cc/test_map_in_map.cc:21:
In file included from /home/*/bcc/src/cc/api/BPF.h:25:
/home/*/bcc/src/cc/api/BPFTable.h:499:23: error: const_cast from 'char *(*)(const char *, int) throw()' to 'custom_key *' is not allowed
    if (!this->remove(const_cast<KeyType*>(&index)))

@chenhengqi
Copy link
Collaborator

remove(&key) not remove(&index)

src/cc/api/BPF.h Outdated
Comment on lines 215 to 220
BPFMapInMapTable<KeyType> get_map_in_map_table(const std::string& name){
TableStorage::iterator it;
if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}),it))
return BPFMapInMapTable<KeyType>(it->second);
return BPFMapInMapTable<KeyType>({});
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

style: 2 space for indention, 1 space after comma.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed

@masibw masibw changed the title [WIP]Allow the use of custom keys in BPF_HASH_OF_MAPS Allow the use of custom keys in BPF_HASH_OF_MAPS Jun 21, 2021
@masibw
Copy link
Contributor Author

masibw commented Jun 21, 2021

remove(&key) not remove(&index)

Oh, I'm sorry. I made a terrible mistake. 😢
Thanks to you, it's working now.

@yonghong-song
Copy link
Collaborator

[buildbot, test this please]

@masibw
Copy link
Contributor Author

masibw commented Jun 22, 2021

Sorry, I made a mistake with the test_map_in_map.py file name.

@yonghong-song
Copy link
Collaborator

[buildbot, test this please]

@yonghong-song
Copy link
Collaborator

test_map_in_map.py failed, probably due to python3 compatibility.

42: Traceback (most recent call last):
42:   File "/home/fedora/jenkins/workspace/bcc-pr/label/fc28/tests/python/test_map_in_map.py", line 160, in test_hash_table_custom_key
42:     assert(ex1_map[ct.c_int(0)] >= 1)
42: TypeError: '>=' not supported between instances of 'c_int' and 'int'

@yonghong-song
Copy link
Collaborator

[buildbot, ok to test]

@masibw
Copy link
Contributor Author

masibw commented Jun 22, 2021

test_map_in_map.py failed, probably due to python3 compatibility.

OK, probably fixed.

@yonghong-song
Copy link
Collaborator

LGTM. Thanks!

@yonghong-song yonghong-song merged commit 722cf83 into iovisor:master Jun 22, 2021
brho pushed a commit to brho/bcc that referenced this pull request Nov 3, 2021
 - Allow the use of custom keys in BPF_HASH_OF_MAPS
 - Add both python and C++ tests
CrackerCat pushed a commit to CrackerCat/bcc that referenced this pull request Jul 31, 2024
 - Allow the use of custom keys in BPF_HASH_OF_MAPS
 - Add both python and C++ tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add macro HASH_OF_MAPS other types of the key
3 participants