From patchwork Sat Jan 20 10:00:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 863942 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) 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; dkim=pass (2048-bit key; unprotected) header.d=resnulli-us.20150623.gappssmtp.com header.i=@resnulli-us.20150623.gappssmtp.com header.b="QTeEdSVT"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zNtVk2bK3z9s82 for ; Sat, 20 Jan 2018 21:00:46 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755008AbeATKAk (ORCPT ); Sat, 20 Jan 2018 05:00:40 -0500 Received: from mail-wr0-f194.google.com ([209.85.128.194]:33287 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751334AbeATKAd (ORCPT ); Sat, 20 Jan 2018 05:00:33 -0500 Received: by mail-wr0-f194.google.com with SMTP id p6so3725350wrd.0 for ; Sat, 20 Jan 2018 02:00:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=X72UmLsVOLwmway5yN4Q8gYRt3UaIC9mpwd2f9ogikM=; b=QTeEdSVTJVdUzDxdzhPP9/2jxRdHRQfKEUlKjZkorTpGaYAmsbmvVYVwcImqi0oYmb hGnQVsSgpecbI45JEozHByFiI7JTkSXdGIwhDaYGTaZba+lcW+GSRYCxTFvDBwsco7wX nHFaZPZlqfqs5NbgxGZQj3JTAn8H6nfkHR8EIN6B+aB4RaYB4Qsw54sOvBnye5rQYdXs 8BVTA58qV+KBDOpT+nPIo8qhDzRQaUeJxN84Nb45RU4JF2TyVbkb9D5SMlqiFl4xEQEY IWiPHKwO0UW4w0JyVw+fohiMs9cXQ8cF1heFqELJWbm4AUqfLyUFpqfSg5SfdcecVeuJ RWjA== 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; bh=X72UmLsVOLwmway5yN4Q8gYRt3UaIC9mpwd2f9ogikM=; b=VgCZwKy3EtQMDxKVmrm5Tviq5azS7bOjdAaQhtG4UFjp4LhTg23IFgPY0eCyvwtlan eIHKMa0X/5JnfnsX+CyiJm2DDcJzfVnQLTqk1giYIA2yFOwom8hI1Q9L7HdVN0N9McFu IEfSkx1CyIrg4hn0zCalYIPCKULr+w2tfG1QkgjNqWtQ2AGqixWA4lw9kYuWU1EnioSg gcVG9UYAqS9QDzOQdn1uitjlZmoTyuUxTnP86q07kb5gAkw2YNBNaH+aSl4Mkui4lGUe VS5Igs8lZTEd9WDgDoFc70bgp/tDtNd9KUlOrzTcFascJUEf/G9oZQsIGFkgzuIPu3B0 QvyA== X-Gm-Message-State: AKwxyte4WtDCA0/p7b0ixJGRCCYOkkKqKX4KCURwxGCEy0+9Gj70jYMi HJsERsos4FipVjBScX0qws8DS6s/ X-Google-Smtp-Source: AH8x225r+tc9zweNzEWPFidh7KBgySAxLhLAQVEQzLMTD3h4ZQgwU3DM+vMw4fsDaNsINc3cXOmuHA== X-Received: by 10.223.192.139 with SMTP id d11mr870528wrf.260.1516442432148; Sat, 20 Jan 2018 02:00:32 -0800 (PST) Received: from localhost (jirka.pirko.cz. [84.16.102.26]) by smtp.gmail.com with ESMTPSA id o22sm26563571wra.51.2018.01.20.02.00.31 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 20 Jan 2018 02:00:31 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: mlxsw@mellanox.com, dsahern@gmail.com Subject: [patch iproute2 net-next v12 1/3] tc: introduce tc_qdisc_block_exists helper Date: Sat, 20 Jan 2018 11:00:27 +0100 Message-Id: <20180120100029.886-2-jiri@resnulli.us> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180120100029.886-1-jiri@resnulli.us> References: <20180120100029.886-1-jiri@resnulli.us> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Jiri Pirko This hepler used qdisc dump to list all qdisc and find if block index in question is used by any of them. That means the block with specified index exists. Signed-off-by: Jiri Pirko --- v11->v12: - fixed return type of tc_qdisc_block_exists - removed 0 checks for block in tc_qdisc_block_exists - rever xmas tree variables in tc_qdisc_block_exists --- tc/tc_qdisc.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tc/tc_util.h | 2 ++ 2 files changed, 63 insertions(+) diff --git a/tc/tc_qdisc.c b/tc/tc_qdisc.c index 70279b9d..54701c26 100644 --- a/tc/tc_qdisc.c +++ b/tc/tc_qdisc.c @@ -412,3 +412,64 @@ int do_qdisc(int argc, char **argv) fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv); return -1; } + +struct tc_qdisc_block_exists_ctx { + __u32 block_index; + bool found; +}; + +static int tc_qdisc_block_exists_cb(const struct sockaddr_nl *who, + struct nlmsghdr *n, void *arg) +{ + struct tc_qdisc_block_exists_ctx *ctx = arg; + struct tcmsg *t = NLMSG_DATA(n); + struct rtattr *tb[TCA_MAX+1]; + int len = n->nlmsg_len; + + if (n->nlmsg_type != RTM_NEWQDISC) + return 0; + + len -= NLMSG_LENGTH(sizeof(*t)); + if (len < 0) + return -1; + + parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len); + + if (tb[TCA_KIND] == NULL) + return -1; + + if (tb[TCA_INGRESS_BLOCK] && + RTA_PAYLOAD(tb[TCA_INGRESS_BLOCK]) >= sizeof(__u32)) { + __u32 block = rta_getattr_u32(tb[TCA_INGRESS_BLOCK]); + + if (block == ctx->block_index) + ctx->found = true; + } + + if (tb[TCA_EGRESS_BLOCK] && + RTA_PAYLOAD(tb[TCA_EGRESS_BLOCK]) >= sizeof(__u32)) { + __u32 block = rta_getattr_u32(tb[TCA_EGRESS_BLOCK]); + + if (block == ctx->block_index) + ctx->found = true; + } + return 0; +} + +bool tc_qdisc_block_exists(__u32 block_index) +{ + struct tc_qdisc_block_exists_ctx ctx = { .block_index = block_index }; + struct tcmsg t = { .tcm_family = AF_UNSPEC }; + + if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) { + perror("Cannot send dump request"); + return false; + } + + if (rtnl_dump_filter(&rth, tc_qdisc_block_exists_cb, &ctx) < 0) { + perror("Dump terminated\n"); + return false; + } + + return ctx.found; +} diff --git a/tc/tc_util.h b/tc/tc_util.h index 1218610d..682dd4fd 100644 --- a/tc/tc_util.h +++ b/tc/tc_util.h @@ -132,4 +132,6 @@ int prio_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt); int cls_names_init(char *path); void cls_names_uninit(void); +bool tc_qdisc_block_exists(__u32 block_index); + #endif