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

Add macro for BPF_HASH #135

Closed
drzaeus77 opened this issue Aug 17, 2015 · 3 comments
Closed

Add macro for BPF_HASH #135

drzaeus77 opened this issue Aug 17, 2015 · 3 comments

Comments

@drzaeus77
Copy link
Collaborator

It's nice to have a lot of control over BPF maps, but I wonder if we
could have some simplified macros for the really common cases, like: hash
of one key -> u64.

Before:

struct key_t {
  struct request *req;
};
BPF_TABLE("hash", struct key_t, u64, start, 10240);

After:

BPF_HASH(start, struct request *);

Where the name is "start", the single key is "struct request *", and it
assumes the value is u64 (common case), and there is a global default for
number of entries (10240). If people want to customize, then do BPF_TABLE.

@brendangregg
Copy link
Member

Or even supporting both explicit or implicit value types (3rd argument optional):

BPF_HASH(start, struct request *, u64);
BPF_HASH(start, struct request *);

drzaeus77 pushed a commit that referenced this issue Sep 6, 2015
* Usage: BPF_HASH(tablename, key_type=u64, leaf_type=u64)
  2nd and 3rd arguments are optional in the C++ default argument style

Fixes: #135
Signed-off-by: Brenden Blanco <bblanco@plumgrid.com>
@brendangregg
Copy link
Member

This has been working, so the ticket can be closed.

It works for hashes that exist only for the C program; eg, temporarily storing timestamps.

I hit issues accessing a BPF_HASH() key (when a key_t is not in use, like in the example above) from Python, complaining about it not being c_int, but that can be a separate issue.

@drzaeus77
Copy link
Collaborator Author

For the python case you mentioned, what I tend to do is wrap things that should be a ctype with a Table.Key() or Table.Leaf() type.

For instance, if Key is a complex type like struct { int a; int b; }:

b["stats"][b["stats"].Key(0, 1)] = b["stats"].Leaf(42)

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

No branches or pull requests

2 participants