vmware-archive / p4c-xdp

Backend for the P4 compiler targeting XDP
Apache License 2.0
171 stars 26 forks source link

Error compiling control-plane user_xdp5.h #106

Open kwjjyn opened 5 years ago

kwjjyn commented 5 years ago

Hi, When I run the below command in the /p4c/extensions/p4c-xdp/tests :

# gcc -I ../lib/   ../lib/libbpf.o user_xdp5.c -o xdp5

there's an error that

In file included from xdp5.h:6:0,
                 from user_xdp5.c:20:
ebpf_xdp.h:24:25: fatal error: ebpf_kernel.h: No such file or directory
 #include "ebpf_kernel.h"
                         ^
compilation terminated.

And then I found the header files to be included are in the /p4c/backends/ebpf/runtime/, so I move the ebpf_kernel.h and ebpf_common.h into the /tests :

root@debian:~/p4c/extensions/p4c-xdp/tests# cp /root/p4c/backends/ebpf/runtime/ebpf_common.h ./
root@debian:~/p4c/extensions/p4c-xdp/tests# cp /root/p4c/backends/ebpf/runtime/ebpf_kernel.h ./

however, when I compile again , some other errors occured :

In file included from ebpf_xdp.h:24:0,
                 from xdp5.h:6,
                 from user_xdp5.c:20:
ebpf_kernel.h:111:19: error: static declaration of ‘bpf_lookup_elem’ follows non-static declaration
 static inline int bpf_lookup_elem(int fd, void *key, void *value) {
                   ^~~~~~~~~~~~~~~
In file included from user_xdp5.c:17:0:
../lib/libbpf.h:10:5: note: previous declaration of ‘bpf_lookup_elem’ was here
 int bpf_lookup_elem(int fd, void *key, void *value);
     ^~~~~~~~~~~~~~~
In file included from ebpf_xdp.h:24:0,
                 from xdp5.h:6,
                 from user_xdp5.c:20:
ebpf_kernel.h:131:19: error: static declaration of ‘bpf_update_elem’ follows non-static declaration
 static inline int bpf_update_elem(int fd, void *key, void *value, u64 flags) {
                   ^~~~~~~~~~~~~~~
In file included from user_xdp5.c:17:0:
../lib/libbpf.h:9:5: note: previous declaration of ‘bpf_update_elem’ was here
 int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
     ^~~~~~~~~~~~~~~
In file included from ebpf_xdp.h:24:0,
                 from xdp5.h:6,
                 from user_xdp5.c:20:
ebpf_kernel.h:149:19: error: static declaration of ‘bpf_delete_elem’ follows non-static declaration
 static inline int bpf_delete_elem(int fd, void *key) {
                   ^~~~~~~~~~~~~~~
In file included from user_xdp5.c:17:0:
../lib/libbpf.h:11:5: note: previous declaration of ‘bpf_delete_elem’ was here
 int bpf_delete_elem(int fd, void *key);
     ^~~~~~~~~~~~~~~
In file included from ebpf_xdp.h:24:0,
                 from xdp5.h:6,
                 from user_xdp5.c:20:
ebpf_kernel.h:165:19: error: static declaration of ‘bpf_obj_pin’ follows non-static declaration
 static inline int bpf_obj_pin(int fd, const char *pathname) {
                   ^~~~~~~~~~~
In file included from user_xdp5.c:17:0:
../lib/libbpf.h:18:5: note: previous declaration of ‘bpf_obj_pin’ was here
 int bpf_obj_pin(int fd, const char *pathname);
     ^~~~~~~~~~~
In file included from ebpf_xdp.h:24:0,
                 from xdp5.h:6,
                 from user_xdp5.c:20:
ebpf_kernel.h:181:19: error: static declaration of ‘bpf_obj_get’ follows non-static declaration
 static inline int bpf_obj_get(const char *pathname) {
                   ^~~~~~~~~~~
In file included from user_xdp5.c:17:0:
../lib/libbpf.h:19:5: note: previous declaration of ‘bpf_obj_get’ was here
 int bpf_obj_get(const char *pathname);
     ^~~~~~~~~~~
user_xdp5.c: In function ‘main’:
user_xdp5.c:29:25: error: storage size of ‘key’ isn’t known
  struct dstmactable_key key;
                         ^~~
user_xdp5.c:30:27: error: storage size of ‘value’ isn’t known
  struct dstmactable_value value;
                           ^~~~~
user_xdp5.c:33:2: warning: implicit declaration of function ‘initialize_tables’ [-Wimplicit-function-declaration]
  initialize_tables();
  ^~~~~~~~~~~~~~~~~
user_xdp5.c:35:17: error: ‘Fallback_action’ undeclared (first use in this function)
  value.action = Fallback_action;
                 ^~~~~~~~~~~~~~~
user_xdp5.c:35:17: note: each undeclared identifier is reported only once for each function it appears in

could you give me some advice? Thanks.

williamtu commented 5 years ago

Hi, Thanks for testing this case. The user_xdp5.c is obsolete and not tested at all. I did quick fix to make it works by

diff --git a/lib/libbpf.h b/lib/libbpf.h
index 1c874b8..ada1e31 100644
--- a/lib/libbpf.h
+++ b/lib/libbpf.h
@@ -4,19 +4,19 @@

 struct bpf_insn;

-int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
+static int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
           int max_entries);
-int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
-int bpf_lookup_elem(int fd, void *key, void *value);
-int bpf_delete_elem(int fd, void *key);
-int bpf_get_next_key(int fd, void *key, void *next_key);
+static int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
+static int bpf_lookup_elem(int fd, void *key, void *value);
+static int bpf_delete_elem(int fd, void *key);
+static int bpf_get_next_key(int fd, void *key, void *next_key);

-int bpf_prog_load(enum bpf_prog_type prog_type,
+static int bpf_prog_load(enum bpf_prog_type prog_type,
          const struct bpf_insn *insns, int insn_len,
          const char *license, int kern_version);

-int bpf_obj_pin(int fd, const char *pathname);
-int bpf_obj_get(const char *pathname);
+static int bpf_obj_pin(int fd, const char *pathname);
+static int bpf_obj_get(const char *pathname);

 #define LOG_BUF_SIZE 262144
 extern char bpf_log_buf[LOG_BUF_SIZE];
diff --git a/tests/user_xdp5.c b/tests/user_xdp5.c
index ca13f9d..32f2875 100644
--- a/tests/user_xdp5.c
+++ b/tests/user_xdp5.c
@@ -26,13 +26,10 @@ int main(void)
 {
    int ret;    
     int fd;
-   struct dstmactable_key key;
-   struct dstmactable_value value;
+   struct Ingress_dstmactable_key key;
+   struct Ingress_dstmactable_value value;

-    /* populate the default table */
-   initialize_tables();
-
-   value.action = Fallback_action;
+   value.action = Ingress_Fallback_action;
    key.field0 = 0x800; // IP packet

     printf("=== Open BPF map: %s ===\n", MAP_PATH TABLE);

and compile it with

root@ba8b8adc32b6:/home/p4c/extensions/p4c-xdp/tests# gcc -I. -I../../../backends/ebpf/runtime/ -I ../lib/ ../lib/libbpf.o user_xdp5.c
mihaibudiu commented 5 years ago

@williamtu : do you want to push your changes to libbpf.h as a pull request?

kwjjyn commented 5 years ago

Actually when I update libbpf.h , I found that I can't run make in the /tests , because the load_and_verify.c file relies on the ../lib/bpf_load.c which alse include the libbpf.h . However, the functions which bpf_load,c calls are defined static functions in the libbpf.h . So it can't be compiled right.

But it's all right. The control plane code just use libbpf.h code . And I delete the bpf_load.c , .h and dont use the bpfloader anymore.

At the last . I think maybe you can maintain a copy of libbpf.h and libbpf.c with static in another folder like lib-control . And there is no effect on the /tests . And compile the control plane code with

gcc -I../../../backends/ebpf/runtime/ -I ../lib-control/ ../lib-control/libbpf.o user_xdp5.c -o user_xdp5
williamtu commented 5 years ago

Thanks for your help. I will work on a pull request.

chengchuntu commented 5 years ago

keep it open until fixed.