xdp-project / xdp-tutorial

XDP tutorial
2.48k stars 577 forks source link

Compilation error: unused function <func> [-Werror,-Wunused-function] #142

Open imgrass opened 4 years ago

imgrass commented 4 years ago

In commit "dcdc6836f237c8a1a36b2d699c171475a7c4d07e" with branch "origin/master", the following compilation error would occur like that:

In file included from xdp_prog_kern.c:8: ./../common/parsing_helpers.h:156:28: error: unused function 'parse_icmp6hdr' [-Werror,-Wunused-function] static always_inline int parse_icmp6hdr(struct hdr_cursor nh,
./../common/parsing_helpers.h:171:28: error: unused function 'parse_icmphdr' [-Werror,-Wunused-function] static __always_inline int parse_icmphdr(struct hdr_cursor
nh,
./../common/parsing_helpers.h:204:28: error: unused function 'parse_udphdr' [-Werror,-Wunused-function] static
always_inline int parse_udphdr(struct hdr_cursor nh,
./../common/parsing_helpers.h:227:28: error: unused function 'parse_tcphdr' [-Werror,-Wunused-function] static __always_inline int parse_tcphdr(struct hdr_cursor
nh,
4 errors generated.

I added "__maybe_unused" marco to these function definitions shown as below, then these errors disappeared

diff --git a/common/parsing_helpers.h b/common/parsing_helpers.h index c129ea5..5397f86 100644 --- a/common/parsing_helpers.h +++ b/common/parsing_helpers.h @@ -29,6 +29,9 @@

include <linux/udp.h>

include <linux/tcp.h>

+#define __maybeunused __attribute__((__unused_\)) + + / Header cursor to keep track of current parsing position / struct hdr_cursor { void *pos; @@ -59,7 +62,7 @@ struct icmphdr_common {

define VLAN_MAX_DEPTH 4

endif

-static always_inline int proto_is_vlan(__u16 h_proto) +static always_inline __maybe_unused int proto_is_vlan(__u16 h_proto) ...

This is my system info:

xdp-tutorial(14:33>master uname -a Linux docker 4.4.0-179-generic #209-Ubuntu SMP Fri Apr 24 17:48:44 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux xdp-tutorial(14:33>master gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.12' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12)

Is this a bug? Or there are other steps need to be done firstly?

ljluestc commented 2 months ago
#include <linux/if_vlan.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/udp.h>
#include <linux/tcp.h>

#define __maybe_unused __attribute__((__unused__))

/* Header cursor to keep track of current parsing position */
struct hdr_cursor {
    void *pos;
};

struct icmphdr_common {
    __u8 type;
    __u8 code;
};

/* Function prototypes with __maybe_unused attribute */
static __always_inline __maybe_unused int proto_is_vlan(__u16 h_proto);
static __always_inline __maybe_unused int parse_icmp6hdr(struct hdr_cursor *nh, void *data_end, struct icmphdr_common *icmphdr);
static __always_inline __maybe_unused int parse_icmphdr(struct hdr_cursor *nh, void *data_end, struct icmphdr_common *icmphdr);
static __always_inline __maybe_unused int parse_udphdr(struct hdr_cursor *nh, void *data_end, struct udphdr **udp);
static __always_inline __maybe_unused int parse_tcphdr(struct hdr_cursor *nh, void *data_end, struct tcphdr **tcp);

static __always_inline __maybe_unused int proto_is_vlan(__u16 h_proto) {
    return h_proto == htons(ETH_P_8021Q) || h_proto == htons(ETH_P_8021AD);
}

static __always_inline __maybe_unused int parse_icmp6hdr(struct hdr_cursor *nh, void *data_end, struct icmphdr_common *icmphdr) {
    struct icmp6hdr *hdr = nh->pos;
    if (hdr + 1 > data_end)
        return -1;
    icmphdr->type = hdr->icmp6_type;
    icmphdr->code = hdr->icmp6_code;
    nh->pos = hdr + 1;
    return 0;
}

static __always_inline __maybe_unused int parse_icmphdr(struct hdr_cursor *nh, void *data_end, struct icmphdr_common *icmphdr) {
    struct icmphdr *hdr = nh->pos;
    if (hdr + 1 > data_end)
        return -1;
    icmphdr->type = hdr->type;
    icmphdr->code = hdr->code;
    nh->pos = hdr + 1;
    return 0;
}

static __always_inline __maybe_unused int parse_udphdr(struct hdr_cursor *nh, void *data_end, struct udphdr **udp) {
    struct udphdr *hdr = nh->pos;
    if (hdr + 1 > data_end)
        return -1;
    *udp = hdr;
    nh->pos = hdr + 1;
    return 0;
}

static __always_inline __maybe_unused int parse_tcphdr(struct hdr_cursor *nh, void *data_end, struct tcphdr **tcp) {
    struct tcphdr *hdr = nh->pos;
    if (hdr + 1 > data_end)
        return -1;
    *tcp = hdr;
    nh->pos = hdr + 1;
    return 0;
}