bpf: Make bpf_helper_defs.h c++ friendly
PATCH v2 1/1 bpf: Make bpf_helper_defs.h c++ friendly
From: Peng Wei <[email protected]>
Compiling C++ BPF programs with existing bpf_helper_defs.h is not possible due to stricter C++ type conversions. C++ complains about (void *) type conversions:
$ clang++ --include linux/types.h ./tools/lib/bpf/bpf_helper_defs.h
bpf_helper_defs.h:57:67: error: invalid conversion from ‘void*’ to ‘void* (*)(void*, const void*)’ [-fpermissive]
57 | static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
| ^~~~~~~~~~
| |
| void*Extend bpf_doc.py to use proper function type instead of void.
Before:
static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
After:
static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *(*)(void *map, const void *key)) 1;
v2:
- add clang++ invocation example (Yonghong)
--- scripts/bpf_doc.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
scripts/bpf_doc.py +6 -1
@@ -827,6 +827,9 @@ COMMANDS print(' *{}{}'.format(' \t' if line else '', line)) print(' */') + fptr_type = '%s%s(*)(' % ( + self.map_type(proto['ret_type']), + ((' ' + proto['ret_star']) if proto['ret_star'] else '')) print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']), proto['ret_star'], proto['name']), end='') comma = ''
@@ -845,8 +848,10 @@ COMMANDS one_arg += '{}'.format(n) comma = ', ' print(one_arg, end='') + fptr_type += one_arg - print(') = (void *) %d;' % helper.enum_val) + fptr_type += ')' + print(') = (%s) %d;' % (fptr_type, helper.enum_val)) print('') ###############################################################################
Signature · 1 line
2.40.1.495.gc816e09b53d-goog
Quoted 22 lines · Stanislav Fomichev
From: Peng Wei <[email protected]>
Compiling C++ BPF programs with existing
bpf_helper_defs.his not possible due to stricter C++ type conversions. C++ complains about (void *) type conversions:$ clang++ --include linux/types.h ./tools/lib/bpf/bpf_helper_defs.hbpf_helper_defs.h:57:67: error: invalid conversion from ‘void*’ to ‘void* (*)(void*, const void*)’ [-fpermissive]
57 | static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1; | ^~~~~~~~~~ | | | void*Extend
bpf_doc.pyto use proper function type instead of void.Before:
static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;After:
static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *(*)(void *map, const void *key)) 1;
Quoted 6 lines
v2:
- add clang++ invocation example (Yonghong)Cc Yonghong Song <[email protected]>Signed-off-by Peng Wei <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>
Quoted 10 lines · Stanislav Fomichev
From: Peng Wei <[email protected]>
Compiling C++ BPF programs with existing
bpf_helper_defs.his not possible due to stricter C++ type conversions. C++ complains about (void *) type conversions:$ clang++ --include linux/types.h ./tools/lib/bpf/bpf_helper_defs.hbpf_helper_defs.h:57:67: error: invalid conversion from ‘void*’ to ‘void* (*)(void*, const void*)’ [-fpermissive]
Can you use -fpermissive instead? As Yonghong said, C++ is not really supported, so pretending we do will just cause more confusion and issues down the line.
BTW, can you elaborate more on v4 vs v6 code reuse (or what was it)? I wonder if there is something that would stay within C domain that could be done?
Quoted 52 lines
57 | static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1; | ^~~~~~~~~~ | | | void*Extend
bpf_doc.pyto use proper function type instead of void.Before:
static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;After:
static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *(*)(void *map, const void *key)) 1;v2:
- add clang++ invocation example (Yonghong)Cc Yonghong Song <[email protected]>Signed-off-by Peng Wei <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- scripts/bpf_doc.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)scripts/bpf_doc.py +6
-2@@ -827,6 +827,9 @@ COMMANDS print(' *{}{}'.format(' \t' if line else '', line)) print(' */') + fptr_type = '%s%s(*)(' % ( + self.map_type(proto['ret_type']), + ((' ' + proto['ret_star']) if proto['ret_star'] else '')) print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']), proto['ret_star'], proto['name']), end='') comma = ''@@ -845,8 +848,10 @@ COMMANDS one_arg += '{}'.format(n) comma = ', ' print(one_arg, end='') + fptr_type += one_arg - print(') = (void *) %d;' % helper.enum_val) + fptr_type += ')' + print(') = (%s) %d;' % (fptr_type, helper.enum_val)) print('') ############################################################################### --2.40.1.495.gc816e09b53d-goog
Quoted 9 lines · Andrii Nakryiko
Quoted 7 lines · Stanislav Fomichev
From: Peng Wei <[email protected]>
Compiling C++ BPF programs with existing
bpf_helper_defs.his not possible due to stricter C++ type conversions. C++ complains about (void *) type conversions:
Quoted 7 lines
Quoted 3 lines
$ clang++ --include linux/types.h ./tools/lib/bpf/bpf_helper_defs.hbpf_helper_defs.h:57:67: error: invalid conversion from ‘void*’ to ‘void* (*)(void*, const void*)’ [-fpermissive]
Can you use -fpermissive instead? As Yonghong said, C++ is not really supported, so pretending we do will just cause more confusion and issues down the line.
I get the same errors with -fpermissive :-(
RE unsupported C++: we are not really subscribing to support it here, right? Just making it easier for the folks who want to experiment with it to try it out.
Maybe I should stick something like this into the header? #ifdef __cplusplus
#warning "C++ is not supported, use at your own risk!" #endif
But we should be able to carry this patch internally if you see no value. I wanted to share it in case somebody else is gonna try the same eventually..
Quoted 3 lines
BTW, can you elaborate more on v4 vs v6 code reuse (or what was it)? I wonder if there is something that would stay within C domain that could be done?
We have a program which handles both v4 and v6 packets. The logic is mostly the same for v4 and v6: parse out the addresses, ports, lookup some maps and do some action on match. Right now, both cases are handled explicitly with a bunch of copy-paste. The idea of this c++ experiment is to leverage templates to write this logic once and parametrize on the address sizes, packet field offsets, etc. I'm pretty certain we can do similar things in C, but it feels like it will be less expressive and a bit more ugly (assuming we'd use a preprocessor to do it).
Quoted 52 lines
Quoted 52 lines
57 | static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1; | ^~~~~~~~~~ | | | void*Extend
bpf_doc.pyto use proper function type instead of void.Before:
static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;After:
static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *(*)(void *map, const void *key)) 1;v2:
- add clang++ invocation example (Yonghong)Cc Yonghong Song <[email protected]>Signed-off-by Peng Wei <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- scripts/bpf_doc.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)scripts/bpf_doc.py +6
-2@@ -827,6 +827,9 @@ COMMANDS print(' *{}{}'.format(' \t' if line else '', line)) print(' */') + fptr_type = '%s%s(*)(' % ( + self.map_type(proto['ret_type']), + ((' ' + proto['ret_star']) if proto['ret_star'] else '')) print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']), proto['ret_star'], proto['name']), end='') comma = ''@@ -845,8 +848,10 @@ COMMANDS one_arg += '{}'.format(n) comma = ', ' print(one_arg, end='') + fptr_type += one_arg - print(') = (void *) %d;' % helper.enum_val) + fptr_type += ')' + print(') = (%s) %d;' % (fptr_type, helper.enum_val)) print('') ############################################################################### --2.40.1.495.gc816e09b53d-goog
Quoted 4 lines · Stanislav Fomichev
RE unsupported C++: we are not really subscribing to support it here, right? Just making it easier for the folks who want to experiment with it to try it out.
I think short term experiments should be out of tree.
The experiments that lead to a long term goal can certainly be in tree. In that sense if your team is really going to invest into making c++ a supported front-end for bpf than this is a good first step. Such c++ flavor will have restrictions (like no virtual calls and exceptions) and it's ok. The basic things like CO-RE has to work though. In other words if there is a draft patch for llvm to enable existing attrs for c++ then this patch would be fine.
This patch alone with no effort made on llvm side should stay out of tree.
Quoted 7 lines · Alexei Starovoitov
Quoted 4 lines · Stanislav Fomichev
RE unsupported C++: we are not really subscribing to support it here, right? Just making it easier for the folks who want to experiment with it to try it out.
Quoted 9 lines
I think short term experiments should be out of tree.
The experiments that lead to a long term goal can certainly be in tree. In that sense if your team is really going to invest into making c++ a supported front-end for bpf than this is a good first step. Such c++ flavor will have restrictions (like no virtual calls and exceptions) and it's ok. The basic things like CO-RE has to work though. In other words if there is a draft patch for llvm to enable existing attrs for c++ then this patch would be fine.
This patch alone with no effort made on llvm side should stay out of tree.
SG, thanks! I'll relay the message internally.