From patchwork Thu May 30 16:43:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin 'ldir' Darbyshire-Bryant X-Patchwork-Id: 1107829 Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@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; dmarc=pass (p=none dis=none) header.from=darbyshire-bryant.me.uk Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=darbyshire-bryant.me.uk header.i=@darbyshire-bryant.me.uk header.b="uldnrM20"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45FD0z2Dlvz9s1c for ; Fri, 31 May 2019 02:43:31 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727180AbfE3Qna (ORCPT ); Thu, 30 May 2019 12:43:30 -0400 Received: from mail-eopbgr70083.outbound.protection.outlook.com ([40.107.7.83]:6069 "EHLO EUR04-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725961AbfE3Qna (ORCPT ); Thu, 30 May 2019 12:43:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=darbyshire-bryant.me.uk; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GZQqlMVLg5+fBjZnQ49u3dqRyd1I5yp73LowRnlhyDY=; b=uldnrM209yBA4p62/RGFa/MRRanGK+xgkyq+n8da+CSxbjdk8oPNbNqwZpAlG/uON0b8nw4aATaV5XXIQvtOoNXC28HoT5dgHYSehR7LR7+jPyQR4jczgLAQK0yUMNnp6761OLNfVVRqSshS2PZq0b1IBzmPeGX/TRZSVvd9LA0= Received: from VI1PR0302MB2750.eurprd03.prod.outlook.com (10.171.106.21) by VI1PR0302MB2589.eurprd03.prod.outlook.com (10.171.104.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1943.17; Thu, 30 May 2019 16:43:20 +0000 Received: from VI1PR0302MB2750.eurprd03.prod.outlook.com ([fe80::603a:6eb9:2073:bde4]) by VI1PR0302MB2750.eurprd03.prod.outlook.com ([fe80::603a:6eb9:2073:bde4%5]) with mapi id 15.20.1922.021; Thu, 30 May 2019 16:43:20 +0000 From: Kevin 'ldir' Darbyshire-Bryant To: "netdev@vger.kernel.org" , "stephen@networkplumber.org" CC: Kevin 'ldir' Darbyshire-Bryant Subject: [RFC PATCH iproute2-next 1/1] tc: add support for act ctinfo Thread-Topic: [RFC PATCH iproute2-next 1/1] tc: add support for act ctinfo Thread-Index: AQHVFwbJ/p/rOXTiw0SyKMktH1aCdA== Date: Thu, 30 May 2019 16:43:20 +0000 Message-ID: <20190530164246.17955-2-ldir@darbyshire-bryant.me.uk> References: <20190530164246.17955-1-ldir@darbyshire-bryant.me.uk> In-Reply-To: <20190530164246.17955-1-ldir@darbyshire-bryant.me.uk> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: AM6PR03CA0003.eurprd03.prod.outlook.com (2603:10a6:20b::16) To VI1PR0302MB2750.eurprd03.prod.outlook.com (2603:10a6:800:e3::21) authentication-results: spf=none (sender IP is ) smtp.mailfrom=ldir@darbyshire-bryant.me.uk; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.20.1 (Apple Git-117) x-originating-ip: [2a02:c7f:1268:6500::dc83] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: ef4a1b4f-df38-41f9-040c-08d6e51dec26 x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(7021145)(8989299)(4534185)(7022145)(4603075)(4627221)(201702281549075)(8990200)(7048125)(7024125)(7027125)(7023125)(5600148)(711020)(4605104)(1401327)(2017052603328)(7193020); SRVR:VI1PR0302MB2589; x-ms-traffictypediagnostic: VI1PR0302MB2589: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:1468; x-forefront-prvs: 00531FAC2C x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(396003)(39830400003)(346002)(366004)(136003)(376002)(199004)(189003)(486006)(36756003)(8936002)(66946007)(53936002)(66446008)(64756008)(66556008)(66476007)(68736007)(46003)(30864003)(50226002)(73956011)(476003)(14454004)(2616005)(2501003)(5660300002)(110136005)(186003)(71190400001)(2906002)(446003)(71200400001)(74482002)(508600001)(4326008)(25786009)(256004)(11346002)(14444005)(107886003)(305945005)(99286004)(6436002)(6506007)(52116002)(1076003)(6116002)(76176011)(386003)(316002)(86362001)(81156014)(6486002)(81166006)(7736002)(8676002)(6512007)(102836004); DIR:OUT; SFP:1101; SCL:1; SRVR:VI1PR0302MB2589; H:VI1PR0302MB2750.eurprd03.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; received-spf: None (protection.outlook.com: darbyshire-bryant.me.uk does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: va3uwWR4gZb6VeJj5rnMq62oKX+fZOwqeOx6Kjowqa4Xv8lVWBFo6qJ2bnFkTh++dWk3y02d5svfSGA9ojOGVte5Ie/9pB8xk7lekoFk55CmQiXS6uqmI0EPDYAvo79DJJF+6eUtiXQ8S9pjDqtqkOioaI0YtUw4fMtD1C1y6OkmxJ3QiSIRv+v/F4PXWS8Xds8EFqUMwKP/+Qd7fvWha57Ilt0fzgCaOKjWoLYL6q0xIq8295Uk9soXrziitp4pbakY85KxZnjYvwKtxowgz0xHeTErflR/LDwAdANqt3hJpGe26XMNYpKH6rjLdN+NwoxvHjv2JyWQvs1SW8u6HrnxwDOFu97ReyKJqTMvmszsyU2qlEfhe2oZ8WiXYupxEt0qLbcBxshsa7VGP+XsCu/WF/lkaCItBDQLzjYDWBU= MIME-Version: 1.0 X-OriginatorOrg: darbyshire-bryant.me.uk X-MS-Exchange-CrossTenant-Network-Message-Id: ef4a1b4f-df38-41f9-040c-08d6e51dec26 X-MS-Exchange-CrossTenant-originalarrivaltime: 30 May 2019 16:43:20.1653 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 9151708b-c553-406f-8e56-694f435154a4 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: kevin@darbyshire-bryant.me.uk X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0302MB2589 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org ctinfo is an action restoring data stored in conntrack marks to various fields. At present it has two independent modes of operation, restoration of DSCP into IPv4/v6 diffserv and restoration of conntrack marks into packet skb marks. It understands a number of parameters specific to this action in additional to the usual action syntax. Each operating mode is independent of the other so all options are err, optional, however not specifying at least one mode is a bit pointless. Usage: ... ctinfo [dscp mask[/statemask]] [cpmark [mask]] [zone ZONE] [CONTROL] [index ]\n" DSCP mode dscp enables copying of a DSCP store in the conntrack mark into the ipv4/v6 diffserv field. The mask is a 32bit field and specifies where in the conntrack mark the DSCP value is stored. It must be 6 contiguous bits long, e.g. 0xfc000000 would restore the DSCP from the upper 6 bits of the conntrack mark. The DSCP copying may be optionally controlled by a statemask. The statemask is a 32bit field, usually with a single bit set and must not overlap the dscp mask. The DSCP restore operation will only take place if the corresponding bit/s in conntrack mark yield a non zero result. eg. dscp 0xfc000000/0x01000000 would retrieve the DSCP from the top 6 bits, whilst using bit 25 as a flag to do so. Bit 26 is unused in this example. CPMARK mode cpmark enables copying of the conntrack mark to the packet skb mark. In this mode it is completely equivalent to the existing act_connmark. Additional functionality is provided by the optional mask parameter, whereby the stored conntrack mark is logically anded with the cpmark mask before being stored into skb mark. This allows shared usage of the conntrack mark between applications. eg. cpmark 0x00ffffff would restore only the lower 24 bits of the conntrack mark, thus may be useful in the event that the upper 8 bits are used by the DSCP function. Usage: ... ctinfo [dscp mask[/statemask]] [cpmark [mask]] [zone ZONE] [CONTROL] [index ] where : dscp MASK is the bitmask to restore DSCP STATEMASK is the bitmask to determine conditional restoring cpmark MASK mask applied to restored packet mark ZONE is the conntrack zone CONTROL := reclassify | pipe | drop | continue | ok | goto chain Signed-off-by: Kevin Darbyshire-Bryant --- include/uapi/linux/pkt_cls.h | 3 +- include/uapi/linux/tc_act/tc_ctinfo.h | 34 ++++ tc/Makefile | 1 + tc/m_ctinfo.c | 244 ++++++++++++++++++++++++++ 4 files changed, 281 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/tc_act/tc_ctinfo.h create mode 100644 tc/m_ctinfo.c diff --git a/include/uapi/linux/pkt_cls.h b/include/uapi/linux/pkt_cls.h index 51a0496f..b0c6a49a 100644 --- a/include/uapi/linux/pkt_cls.h +++ b/include/uapi/linux/pkt_cls.h @@ -105,7 +105,8 @@ enum tca_id { TCA_ID_IFE = TCA_ACT_IFE, TCA_ID_SAMPLE = TCA_ACT_SAMPLE, /* other actions go here */ - __TCA_ID_MAX = 255 + TCA_ID_CTINFO, + __TCA_ID_MAX=255 }; #define TCA_ID_MAX __TCA_ID_MAX diff --git a/include/uapi/linux/tc_act/tc_ctinfo.h b/include/uapi/linux/tc_act/tc_ctinfo.h new file mode 100644 index 00000000..da803e05 --- /dev/null +++ b/include/uapi/linux/tc_act/tc_ctinfo.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef __UAPI_TC_CTINFO_H +#define __UAPI_TC_CTINFO_H + +#include +#include + +struct tc_ctinfo { + tc_gen; +}; + +enum { + TCA_CTINFO_UNSPEC, + TCA_CTINFO_PAD, + TCA_CTINFO_TM, + TCA_CTINFO_ACT, + TCA_CTINFO_ZONE, + TCA_CTINFO_PARMS_DSCP_MASK, + TCA_CTINFO_PARMS_DSCP_STATEMASK, + TCA_CTINFO_PARMS_CPMARK_MASK, + TCA_CTINFO_STATS_DSCP_SET, + TCA_CTINFO_STATS_DSCP_ERROR, + TCA_CTINFO_STATS_CPMARK_SET, + __TCA_CTINFO_MAX +}; + +#define TCA_CTINFO_MAX (__TCA_CTINFO_MAX - 1) + +enum { + CTINFO_MODE_DSCP = BIT(0), + CTINFO_MODE_CPMARK = BIT(1) +}; + +#endif diff --git a/tc/Makefile b/tc/Makefile index 1a305cf4..60abddee 100644 --- a/tc/Makefile +++ b/tc/Makefile @@ -48,6 +48,7 @@ TCMODULES += m_csum.o TCMODULES += m_simple.o TCMODULES += m_vlan.o TCMODULES += m_connmark.o +TCMODULES += m_ctinfo.o TCMODULES += m_bpf.o TCMODULES += m_tunnel_key.o TCMODULES += m_sample.o diff --git a/tc/m_ctinfo.c b/tc/m_ctinfo.c new file mode 100644 index 00000000..8bd0e817 --- /dev/null +++ b/tc/m_ctinfo.c @@ -0,0 +1,244 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * m_ctinfo.c netfilter ctinfo mark action + * + * Copyright (c) 2019 Kevin Darbyshire-Bryant + */ + +#include +#include +#include +#include +#include "utils.h" +#include "tc_util.h" +#include + +static void +explain(void) +{ + fprintf(stderr, + "Usage: ... ctinfo [dscp mask[/statemask]] [cpmark [mask]] [zone ZONE] [CONTROL] [index ]\n" + "where :\n" + "\tdscp MASK bitmask location of stored DSCP\n" + "\t STATEMASK bitmask to determine conditional restoring\n" + "\tcpmark MASK mask applied to mark on restoration\n" + "\tZONE is the conntrack zone\n" + "\tCONTROL := reclassify | pipe | drop | continue | ok |\n" + "\t goto chain \n"); +} + +static void +usage(void) +{ + explain(); + exit(-1); +} + +static int +parse_ctinfo(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, + struct nlmsghdr *n) +{ + unsigned int cpmarkmask = 0, dscpmask = 0, dscpstatemask = 0; + struct tc_ctinfo sel = {}; + unsigned short zone = 0; + char **argv = *argv_p; + struct rtattr *tail; + int argc = *argc_p; + int ok = 0; + + while (argc > 0) { + if (matches(*argv, "ctinfo") == 0) { + ok = 1; + argc--; + argv++; + } else if (matches(*argv, "help") == 0) { + usage(); + } else { + break; + } + + } + + if (!ok) { + explain(); + return -1; + } + + if (argc) { + if (matches(*argv, "dscp") == 0) { + NEXT_ARG(); + char *slash; + if ((slash = strchr(*argv, '/'))) + *slash = '\0'; + if (get_u32(&dscpmask, *argv, 0)) { + fprintf(stderr, "ctinfo: Illegal dscp \"mask\"\n"); + return -1; + } + if (slash) { + if (get_u32(&dscpstatemask, slash + 1, 0)) { + fprintf(stderr, "ctinfo: Illegal dscp \"statemask\"\n"); + return -1; + } + } + argc--; + argv++; + } + } + + /* cpmark has optional mask parameter, so the next arg might not exist, */ + /* or it might be the next option, or it may actually be a 32bit mask */ + if (argc) { + if (matches(*argv, "cpmark") == 0) { + if (NEXT_ARG_OK()) { + NEXT_ARG_FWD(); + if (!get_u32(&cpmarkmask, *argv, 0)) { + NEXT_ARG_FWD(); /* was a mask */ + } + else { /* not a mask, use default */ + cpmarkmask = ~0; + } + } else { + NEXT_ARG_FWD(); + } + } + } + + if (argc) { + if (matches(*argv, "zone") == 0) { + NEXT_ARG(); + if (get_u16(&zone, *argv, 10)) { + fprintf(stderr, "ctinfo: Illegal \"zone\"\n"); + return -1; + } + argc--; + argv++; + } + } + + parse_action_control_dflt(&argc, &argv, &sel.action, false, TC_ACT_PIPE); + + if (argc) { + if (matches(*argv, "index") == 0) { + NEXT_ARG(); + if (get_u32(&sel.index, *argv, 10)) { + fprintf(stderr, "ctinfo: Illegal \"index\"\n"); + return -1; + } + argc--; + argv++; + } + } + + tail = addattr_nest(n, MAX_MSG, tca_id); + addattr_l(n, MAX_MSG, TCA_CTINFO_ACT, &sel, sizeof(sel)); + if (zone) + addattr16(n, MAX_MSG, TCA_CTINFO_ZONE, zone); + + if (dscpmask) + addattr32(n, MAX_MSG, TCA_CTINFO_PARMS_DSCP_MASK, dscpmask); + + if (dscpstatemask) + addattr32(n, MAX_MSG, TCA_CTINFO_PARMS_DSCP_STATEMASK, dscpstatemask); + + if (cpmarkmask) + addattr32(n, MAX_MSG, TCA_CTINFO_PARMS_CPMARK_MASK, cpmarkmask); + + addattr_nest_end(n, tail); + + *argc_p = argc; + *argv_p = argv; + return 0; +} + +static int print_ctinfo(struct action_util *au, FILE *f, struct rtattr *arg) +{ + unsigned int cpmarkmask = ~0, dscpmask = 0, dscpstatemask = 0; + struct rtattr *tb[TCA_CTINFO_MAX + 1]; + unsigned short zone = 0; + struct tc_ctinfo *ci; + + if (arg == NULL) + return -1; + + parse_rtattr_nested(tb, TCA_CTINFO_MAX, arg); + if (!tb[TCA_CTINFO_ACT]) { + print_string(PRINT_FP, NULL, "%s", "[NULL ctinfo action parameters]"); + return -1; + } + + ci = RTA_DATA(tb[TCA_CTINFO_ACT]); + + if (tb[TCA_CTINFO_PARMS_DSCP_MASK]) { + if (RTA_PAYLOAD(tb[TCA_CTINFO_PARMS_DSCP_MASK]) >= sizeof(__u32)) { + dscpmask = rta_getattr_u32(tb[TCA_CTINFO_PARMS_DSCP_MASK]); + } else { + print_string(PRINT_FP, NULL, "%s", "[invalid dscp mask parameter]"); + } + } + + if (tb[TCA_CTINFO_PARMS_DSCP_STATEMASK]) { + if (RTA_PAYLOAD(tb[TCA_CTINFO_PARMS_DSCP_STATEMASK]) >= sizeof(__u32)) { + dscpstatemask = rta_getattr_u32(tb[TCA_CTINFO_PARMS_DSCP_STATEMASK]); + } else { + print_string(PRINT_FP, NULL, "%s", "[invalid dscp statemask parameter]"); + } + } + + if (tb[TCA_CTINFO_PARMS_CPMARK_MASK]) { + if (RTA_PAYLOAD(tb[TCA_CTINFO_PARMS_CPMARK_MASK]) >= sizeof(__u32)) { + cpmarkmask = rta_getattr_u32(tb[TCA_CTINFO_PARMS_CPMARK_MASK]); + } else { + print_string(PRINT_FP, NULL, "%s", "[invalid cpmark mask parameter]"); + } + } + + if (tb[TCA_CTINFO_ZONE] && + RTA_PAYLOAD(tb[TCA_CTINFO_ZONE]) >= sizeof(__u16)) + zone = rta_getattr_u16(tb[TCA_CTINFO_ZONE]); + + print_string(PRINT_ANY, "kind", "%s ", "ctinfo"); + print_hu(PRINT_ANY, "zone", "zone %u", zone); + print_action_control(f, " ", ci->action, ""); + + print_string(PRINT_FP, NULL, "%s", _SL_); + print_uint(PRINT_ANY, "index", "\t index %u", ci->index); + print_int(PRINT_ANY, "ref", " ref %d", ci->refcnt); + print_int(PRINT_ANY, "bind", " bind %d", ci->bindcnt); + + if (tb[TCA_CTINFO_PARMS_DSCP_MASK]) { + print_0xhex(PRINT_ANY, "dscpmask", " dscp %#010llx", dscpmask); + print_0xhex(PRINT_ANY, "dscpstatemask", "/%#010llx", dscpstatemask); + } + + if (tb[TCA_CTINFO_PARMS_CPMARK_MASK]) { + print_0xhex(PRINT_ANY, "mark", " mark %#010llx", cpmarkmask); + } + + if (show_stats) { + if (tb[TCA_CTINFO_TM]) { + struct tcf_t *tm = RTA_DATA(tb[TCA_CTINFO_TM]); + + print_tm(f, tm); + } + + if (tb[TCA_CTINFO_STATS_DSCP_SET]) + print_lluint(PRINT_ANY, "dscpset", " DSCP set %llu", + rta_getattr_u64(tb[TCA_CTINFO_STATS_DSCP_SET])); + if (tb[TCA_CTINFO_STATS_DSCP_ERROR]) + print_lluint(PRINT_ANY, "dscperror", " error %llu", + rta_getattr_u64(tb[TCA_CTINFO_STATS_DSCP_ERROR])); + + if (tb[TCA_CTINFO_STATS_CPMARK_SET]) + print_lluint(PRINT_ANY, "cpmarkset", " CPMARK set %llu", + rta_getattr_u64(tb[TCA_CTINFO_STATS_CPMARK_SET])); + } + print_string(PRINT_FP, NULL, "%s", _SL_); + + return 0; +} + +struct action_util ctinfo_action_util = { + .id = "ctinfo", + .parse_aopt = parse_ctinfo, + .print_aopt = print_ctinfo, +};