From patchwork Thu Mar 12 18:25:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 1253845 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=isovalent.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=isovalent-com.20150623.gappssmtp.com header.i=@isovalent-com.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=n4pS0PRX; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 48dchw0p3dz9sQx for ; Fri, 13 Mar 2020 05:26:08 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726545AbgCLS0G (ORCPT ); Thu, 12 Mar 2020 14:26:06 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:46801 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726526AbgCLS0G (ORCPT ); Thu, 12 Mar 2020 14:26:06 -0400 Received: by mail-wr1-f66.google.com with SMTP id n15so8743057wrw.13 for ; Thu, 12 Mar 2020 11:26:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KIhZ0tVM9yZO9gvswOz7Wk6QPkMWOulH19S5SPkuvfw=; b=n4pS0PRXdQdpMIexENXZcbAGcpVBpTAEgPxlHvaATYeh8ffKmEVMkttW6OIcTFNVSP 84hoKxARrHMyCkEZrbauqKHa7GBFcCpAsWgoxHO0LZzOUOJ9gyrzXfGUUfu6lYxEwPZC P3ufVo8i/OCpPUw0IF4Po91UoJzEkxs1yEyqUW06L9qfY+TrESIy5Ap+E6JeRH/V3Dz2 CvYDY2kOmPDw04pU77XJqBiEgjyJaHvhvZHKYlfz9uKHgFMH65k8+L8WZOqMQTmtHSWb Fcwgxks/YGOadS/P9VPF6CXJRxDUrw4qYVdUdDCoMIHccTs4pvORSJuAa47siPkzZW90 JWFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KIhZ0tVM9yZO9gvswOz7Wk6QPkMWOulH19S5SPkuvfw=; b=gRY7b03Rzr5ZWNqsStPOPGh41T9JIOkV0aNTjRlTg1Fs/EWLsln9iJxRb2JYYTaNo3 mipj9uCnzkdTCQsI92IsymwOLa+oQqFTxFsY25APFVf0GSns+jgQwAK7D+MgZ7K9kyTg QDtdg74oRo5wo8gMH3oJZrIsMWevXMTW4esSVSpfIfU09X/LStO20NcwA/Yh+DXbFw/r 4vo0/R8/YOICXkJbBVm/hMbt4CtNgYuT2pAAHgA2r2K/ainOw2chknv67sPyfc2wnhPK NhhMUJcWrMARVZYUx5SvRGX6K5gT2tl9R+b6kdsjGdu9/No/C9fSPrNMJhyG5H0sY9QZ egMQ== X-Gm-Message-State: ANhLgQ0dISFXM8091oopz1ocVHXMhcsmploeemsDIUTyZoIlO+L3SKwJ is/a16A/sbRiT+8mZnoACgZNXA== X-Google-Smtp-Source: ADFU+vu12LETaaO5MyFn9/1C4zI9XB72gB2ln1fGcttGD2OyAJP31lVhcOxQCnD92HgJXVjKRvgcLA== X-Received: by 2002:adf:ea02:: with SMTP id q2mr12180374wrm.222.1584037565216; Thu, 12 Mar 2020 11:26:05 -0700 (PDT) Received: from localhost.localdomain ([194.35.118.177]) by smtp.gmail.com with ESMTPSA id r9sm6379134wma.47.2020.03.12.11.26.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Mar 2020 11:26:04 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Quentin Monnet Subject: [PATCH bpf-next 1/2] tools: bpftool: allow all prog/map handles for pinning objects Date: Thu, 12 Mar 2020 18:25:54 +0000 Message-Id: <20200312182555.945-2-quentin@isovalent.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200312182555.945-1-quentin@isovalent.com> References: <20200312182555.945-1-quentin@isovalent.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Documentation and interactive help for bpftool have always explained that the regular handles for programs (id|name|tag|pinned) and maps (id|name|pinned) can be passed to the utility when attempting to pin objects (bpftool prog pin PROG / bpftool map pin MAP). THIS IS A LIE!! The tool actually accepts only ids, as the parsing is done in do_pin_any() in common.c instead of reusing the parsing functions that have long been generic for program and map handles. Instead of fixing the doc, fix the code. It is trivial to reuse the generic parsing, and to simplify do_pin_any() in the process. Do not accept to pin multiple objects at the same time with prog_parse_fds() or map_parse_fds() (this would require a more complex syntax for passing multiple sysfs paths and validating that they correspond to the number of e.g. programs we find for a given name or tag). Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/common.c | 39 +++++--------------------------------- tools/bpf/bpftool/main.h | 2 +- tools/bpf/bpftool/map.c | 2 +- tools/bpf/bpftool/prog.c | 2 +- 4 files changed, 8 insertions(+), 37 deletions(-) diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index b75b8ec5469c..92e51a62bd72 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -211,44 +211,15 @@ int do_pin_fd(int fd, const char *name) return err; } -int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)) +int do_pin_any(int argc, char **argv, int (*get_fd)(int *, char ***)) { - unsigned int id; - char *endptr; - int err; int fd; - if (argc < 3) { - p_err("too few arguments, id ID and FILE path is required"); - return -1; - } else if (argc > 3) { - p_err("too many arguments"); - return -1; - } - - if (!is_prefix(*argv, "id")) { - p_err("expected 'id' got %s", *argv); - return -1; - } - NEXT_ARG(); - - id = strtoul(*argv, &endptr, 0); - if (*endptr) { - p_err("can't parse %s as ID", *argv); - return -1; - } - NEXT_ARG(); - - fd = get_fd_by_id(id); - if (fd < 0) { - p_err("can't open object by id (%u): %s", id, strerror(errno)); - return -1; - } - - err = do_pin_fd(fd, *argv); + fd = get_fd(&argc, &argv); + if (fd < 0) + return fd; - close(fd); - return err; + return do_pin_fd(fd, *argv); } const char *get_fd_type_name(enum bpf_obj_type type) diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 724ef9d941d3..d57972dd0f2b 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -146,7 +146,7 @@ char *get_fdinfo(int fd, const char *key); int open_obj_pinned(char *path, bool quiet); int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type); int mount_bpffs_for_pin(const char *name); -int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)); +int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(int *, char ***)); int do_pin_fd(int fd, const char *name); int do_prog(int argc, char **arg); diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index e6c85680b34d..693a632f6813 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -1384,7 +1384,7 @@ static int do_pin(int argc, char **argv) { int err; - err = do_pin_any(argc, argv, bpf_map_get_fd_by_id); + err = do_pin_any(argc, argv, map_parse_fd); if (!err && json_output) jsonw_null(json_wtr); return err; diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 576ddd82bc96..6e3d69f06b60 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -813,7 +813,7 @@ static int do_pin(int argc, char **argv) { int err; - err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id); + err = do_pin_any(argc, argv, prog_parse_fd); if (!err && json_output) jsonw_null(json_wtr); return err; From patchwork Thu Mar 12 18:25:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 1253847 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=isovalent.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=isovalent-com.20150623.gappssmtp.com header.i=@isovalent-com.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=L9bfE9GC; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 48dchy28M4z9sQx for ; Fri, 13 Mar 2020 05:26:10 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726776AbgCLS0I (ORCPT ); Thu, 12 Mar 2020 14:26:08 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:43257 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726526AbgCLS0I (ORCPT ); Thu, 12 Mar 2020 14:26:08 -0400 Received: by mail-wr1-f66.google.com with SMTP id b2so2546866wrj.10 for ; Thu, 12 Mar 2020 11:26:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Gc4XGbEnsdtIWPY34ern2lcaBJ5zVKAE3vmO2e4SFfQ=; b=L9bfE9GCb6NjbSuBecBwDG3/L2SzwVf7WX6omWK5LB9sYYGRx2OxozR7+GM8SpzI4k KwEWOWnq9Xwf6aUE0zK293OtBlktg2iiRYyePAloIJlMOmufl+2JOfEXIhL4Fek3e7PY rdnL4PPZRtvIHozsX1MEB60fKIVdTLA08R28Krn6HwoKk10+V/wVu3DQtbfNovL125Bp Yx3ST8DRBpl1uNwPZKi7Kc1R9gvnhvFVtOnvwlTdBUEdLLOiv9va4rIR5DZid2ZjOj0C tRwMdz5odi/iQghF9mMXSQITm8MJYezmzMsAyTe9a66rVOPkRIdae71VfrhDC6i0Xnc1 TW7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Gc4XGbEnsdtIWPY34ern2lcaBJ5zVKAE3vmO2e4SFfQ=; b=fcdmiPtWY4E9HHZd7Uv8uo0VRx1cb5AZlPEFv1o7r6BOsGERXBGfX2M+jd8oBWWhfY gN1giZexB+gBnC6YVQuiRY2/pZsZt+cImduiNpLAKmj82e4SN/na8syk/6+PeIsWf3H8 QYuSxtlKI9w0Y3mR4zGopSHq1AG7niyVpjemDB/vsWGZ7Fxykudn4CkIVVrcpBGRXj0o 9jLycB3gQj6cuJmsy02qec6jJCcgZmglVrs7vr9zpb1IaXzBQc7E7Yg8cvl/ZSAt3PnU MUUyVbPsU7m1W28rrq+LBtR1cdq5Pzn5/rPvoerfhibNc286OWMMpyAYXqLkc8qk0G1w B5hg== X-Gm-Message-State: ANhLgQ2sV1txQ6ga+TcphJzzJgfYphNKpVExvIJo9VL8jF3hjawrLn4G /cLQf6nqxtSz2scQjUQG0NavG4e2QmQ= X-Google-Smtp-Source: ADFU+vuh+0RDXEwnDckgZwLvjvJuPOv4mibsESf5PLhgeyLJfmwidcZ5J2frTLxelsaYxug/cDGCeg== X-Received: by 2002:adf:f708:: with SMTP id r8mr12259839wrp.221.1584037566162; Thu, 12 Mar 2020 11:26:06 -0700 (PDT) Received: from localhost.localdomain ([194.35.118.177]) by smtp.gmail.com with ESMTPSA id r9sm6379134wma.47.2020.03.12.11.26.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Mar 2020 11:26:05 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Quentin Monnet Subject: [PATCH bpf-next 2/2] tools: bpftool: fix minor bash completion mistakes Date: Thu, 12 Mar 2020 18:25:55 +0000 Message-Id: <20200312182555.945-3-quentin@isovalent.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200312182555.945-1-quentin@isovalent.com> References: <20200312182555.945-1-quentin@isovalent.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Minor fixes for bash completion: addition of program name completion for two subcommands, and correction for program test-runs and map pinning. The completion for the following commands is fixed or improved: # bpftool prog run [TAB] # bpftool prog pin [TAB] # bpftool map pin [TAB] # bpftool net attach xdp name [TAB] Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/bash-completion/bpftool | 29 ++++++++++++++++------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool index a9cce9d3745a..9b0534f558f1 100644 --- a/tools/bpf/bpftool/bash-completion/bpftool +++ b/tools/bpf/bpftool/bash-completion/bpftool @@ -542,8 +542,8 @@ _bpftool() esac ;; run) - if [[ ${#words[@]} -lt 5 ]]; then - _filedir + if [[ ${#words[@]} -eq 4 ]]; then + COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) ) return 0 fi case $prev in @@ -551,6 +551,10 @@ _bpftool() _bpftool_get_prog_ids return 0 ;; + name) + _bpftool_get_prog_names + return 0 + ;; data_in|data_out|ctx_in|ctx_out) _filedir return 0 @@ -756,11 +760,17 @@ _bpftool() esac ;; pin) - if [[ $prev == "$command" ]]; then - COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) ) - else - _filedir - fi + case $prev in + $command) + COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) ) + ;; + id) + _bpftool_get_map_ids + ;; + name) + _bpftool_get_map_names + ;; + esac return 0 ;; event_pipe) @@ -887,7 +897,7 @@ _bpftool() case $command in skeleton) _filedir - ;; + ;; *) [[ $prev == $object ]] && \ COMPREPLY=( $( compgen -W 'skeleton help' -- "$cur" ) ) @@ -987,6 +997,9 @@ _bpftool() id) _bpftool_get_prog_ids ;; + name) + _bpftool_get_prog_names + ;; pinned) _filedir ;;