[Recent commit 23499442c319 ("bpf: libbpf: retry map creation without
the name") fixed this issue for maps, let's do the same for programs.]

Since commit 88cda1c9da02 ("bpf: libbpf: Provide basic API support
to specify BPF obj name"), libbpf unconditionally sets bpf_attr->name
for programs. Pre v4.14 kernels don't know about programs names and
return an error about unexpected non-zero data. Retry
bpf_load_program_xattr without a program name to cover older kernels.

Signed-off-by: Stanislav Fomichev <s...@google.com>
---
 tools/lib/bpf/libbpf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index cb6565d79603..2ea14dfa28fc 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1401,6 +1401,18 @@ load_program(struct bpf_program *prog, struct bpf_insn 
*insns, int insns_cnt,
 
        ret = bpf_load_program_xattr(&load_attr, log_buf, BPF_LOG_BUF_SIZE);
 
+       if (ret < 0 && errno == E2BIG && load_attr.name) {
+               /* Retry the same operation, but without the name.
+                * Pre v4.14 kernels don't support prog names.
+                */
+               cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
+               pr_warning("Error in bpf_load_program_xattr(%s):%s(%d). 
Retrying without name.\n",
+                          prog->name, cp, errno);
+               load_attr.name = NULL;
+               ret = bpf_load_program_xattr(&load_attr, log_buf,
+                                            BPF_LOG_BUF_SIZE);
+       }
+
        if (ret >= 0) {
                *pfd = ret;
                ret = 0;
-- 
2.19.1.1215.g8438c0b245-goog

Reply via email to