On Thu, Jul 2, 2020 at 2:24 AM Jakub Sitnicki <[email protected]> wrote:
>
> Extend the BPF netns link callbacks to rebuild (grow/shrink) or update the
> prog_array at given position when link gets attached/updated/released.
>
> This let's us lift the limit of having just one link attached for the new
> attach type introduced by subsequent patch.
>
> No functional changes intended.
>
> Signed-off-by: Jakub Sitnicki <[email protected]>
> ---
>
> Notes:
> v3:
> - New in v3 to support multi-prog attachments. (Alexei)
>
> include/linux/bpf.h | 4 ++
> kernel/bpf/core.c | 22 ++++++++++
> kernel/bpf/net_namespace.c | 88 +++++++++++++++++++++++++++++++++++---
> 3 files changed, 107 insertions(+), 7 deletions(-)
>
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 3d2ade703a35..26bc70533db0 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -928,6 +928,10 @@ int bpf_prog_array_copy_to_user(struct bpf_prog_array
> *progs,
>
> void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
> struct bpf_prog *old_prog);
> +void bpf_prog_array_delete_safe_at(struct bpf_prog_array *array,
> + unsigned int index);
> +void bpf_prog_array_update_at(struct bpf_prog_array *array, unsigned int
> index,
> + struct bpf_prog *prog);
> int bpf_prog_array_copy_info(struct bpf_prog_array *array,
> u32 *prog_ids, u32 request_cnt,
> u32 *prog_cnt);
> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> index 9df4cc9a2907..d4b3b9ee6bf1 100644
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
> @@ -1958,6 +1958,28 @@ void bpf_prog_array_delete_safe(struct bpf_prog_array
> *array,
> }
> }
>
> +void bpf_prog_array_delete_safe_at(struct bpf_prog_array *array,
> + unsigned int index)
> +{
> + bpf_prog_array_update_at(array, index, &dummy_bpf_prog.prog);
> +}
> +
> +void bpf_prog_array_update_at(struct bpf_prog_array *array, unsigned int
> index,
> + struct bpf_prog *prog)
it's a good idea to mention it in a comment for both delete_safe_at
and update_at that slots with dummy entries are ignored.
Also, given that index can be out of bounds, should these functions
actually return error if the slot is not found?
> +{
> + struct bpf_prog_array_item *item;
> +
> + for (item = array->items; item->prog; item++) {
> + if (item->prog == &dummy_bpf_prog.prog)
> + continue;
> + if (!index) {
> + WRITE_ONCE(item->prog, prog);
> + break;
> + }
> + index--;
> + }
> +}
> +
> int bpf_prog_array_copy(struct bpf_prog_array *old_array,
> struct bpf_prog *exclude_prog,
> struct bpf_prog *include_prog,
> diff --git a/kernel/bpf/net_namespace.c b/kernel/bpf/net_namespace.c
> index 247543380fa6..6011122c35b6 100644
> --- a/kernel/bpf/net_namespace.c
> +++ b/kernel/bpf/net_namespace.c
> @@ -36,11 +36,51 @@ static void netns_bpf_run_array_detach(struct net *net,
> bpf_prog_array_free(run_array);
> }
>
> +static unsigned int link_index(struct net *net,
> + enum netns_bpf_attach_type type,
> + struct bpf_netns_link *link)
> +{
> + struct bpf_netns_link *pos;
> + unsigned int i = 0;
> +
> + list_for_each_entry(pos, &net->bpf.links[type], node) {
> + if (pos == link)
> + return i;
> + i++;
> + }
> + return UINT_MAX;
Why not return a negative error, if the slot is not found? Feels a bit
unusual as far as error reporting goes.
> +}
> +
[...]