bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: G <chapterk93@163.com>
To: "bpf@vger.kernel.org" <bpf@vger.kernel.org>
Subject: using "BPF_MAP_TYPE_DEVMAP" when pinning it to a global path
Date: Wed, 28 Jul 2021 10:50:26 +0800 (CST)	[thread overview]
Message-ID: <384aaee4.1b40.17aeb053a78.Coremail.chapterk93@163.com> (raw)

Hi BPF Experts

I'm having an issue with using "BPF_MAP_TYPE_DEVMAP" when pinning it to a global path for sharing with multiple programs.
I used iproute2 to load the program, the parameters for creating a map are as follows:
----------------------------------------------------------------------------------
    struct bpf_elf_map BPFM_SEC_MAPS dev_map_test = { 
        .type                = BPF_MAP_TYPE_DEVMAP,                  
        .size_key       = sizeof(int32),                  
        .size_value   = sizeof(int32),                  
        .max_elem   = 1,                  
        .flags               = 0,                 
        .pinning         = PIN_GLOBAL_NS,                    
    }; 
----------------------------------------------------------------------------------

Here I set the flags to 0, but the actual map is created with flags 0x80, so when the second program is loaded, map parameter check is incorrect.
When I tried to set the flags directly to 0x80, I failed with the parameter error `-EINVA`  when the first program was loaded
The general process for iproute2 to create a map with a pin tag is as follows:
-----------------------------------------------------------------------------------
bpf_map_attach
-> bpf_probe_pinned
    bpf_map_selfcheck_pinned
    -> bpf_derive_elf_map_from_fdinfo
         bpf_map_pin_report
         -> if (obj->flags != pin->flags)
	           fprintf(stderr, " - Flags:        %#x (obj) != %#x (pin)\n", obj->flags, pin->flags);
-----------------------------------------------------------------------------------

I have tried to read the relevant kernel code :
-----------------------------------------------------------------------------------
    // kernel/bpf/devmap.c
    #define DEV_CREATE_FLAG_MASK \
         (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)

    static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
    {
       ...
  	/* check sanity of attributes */
  	if (attr->max_entries == 0 || attr->key_size != 4 ||
      		attr->value_size != 4 || attr->map_flags & ~DEV_CREATE_FLAG_MASK)
    		return -EINVAL;

  	/* Lookup returns a pointer straight to dev->ifindex, so make sure the
   	 * verifier prevents writes from the BPF side
   	 */
  	attr->map_flags |= BPF_F_RDONLY_PROG;
        ...
    }
-----------------------------------------------------------------------------------

At the time of check , it does not allow 0x80 to be set (DEV_CREATE_FLAG_MASK) , but then the code sets the 0x80 flag itself.
So I'm not sure if we should allow 0x80 on the check here as well ? If anyone has suggestions please let me know, thanks.
-----------------------------------------------------------------------------------
    #define DEV_CREATE_FLAG_MASK \
         (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY | BPF_F_RDONLY_PROG)
-----------------------------------------------------------------------------------
 
Best regards
W.Gao

             reply	other threads:[~2021-07-28  3:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-28  2:50 G [this message]
2021-07-28 18:01 ` using "BPF_MAP_TYPE_DEVMAP" when pinning it to a global path Zvi Effron
2021-07-29  0:53   ` G

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=384aaee4.1b40.17aeb053a78.Coremail.chapterk93@163.com \
    --to=chapterk93@163.com \
    --cc=bpf@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).