From patchwork Fri Nov 10 05:49:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yan Zhai X-Patchwork-Id: 163967 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp1347504vqs; Fri, 10 Nov 2023 11:52:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IHTHvrjr4BtZ8Yjg2sTGdpezp3p47EMN4ZI+cmNW5rhm2b7QzYaPSikcBS8CsQpDerRgu14 X-Received: by 2002:a05:6300:808c:b0:185:6cb6:57dd with SMTP id ap12-20020a056300808c00b001856cb657ddmr133127pzc.25.1699645964556; Fri, 10 Nov 2023 11:52:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699645964; cv=none; d=google.com; s=arc-20160816; b=CL7CPDRTJqMQtsyzDlPyJvDNJjq7xbTO9W1J3ZBmJHnYeo1A6ZqgSlGeC5X1zMAsRm 5lwG8IS5llf/qW/+R+L0EeUa6X5VaTvjGYahdrrxFd1F5iL45cnVwzcK4L3ac5bx+Zzr QEgLOTh9wxLsajRbulfvlXBPIEas5kpbfUyKjPFtfjqqJePmy85xUqM+Ew/SJdTkFL8S aW16sLdC2Tejy6kHztKCiIutbq6yH0LZLQNwMk700znqvHl2MCw8qQsknp1mI+f/J2f4 XJjYJ/waOG63GzQA7HJNgWLI9PWlnM8DWWGbxkdRyw6lxnOG0wKg2bQmxBKqKKEs0n0v Hbnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=/UY2V4KB781XJBY0gcAuxuyVl/cjuGIjQ/iARGJ/tjI=; fh=Dhpxd9MC5r7g7pQr9B4C+ZBw+ayE0n/nAwdR+OuvsD0=; b=DFQx0af0y9ZwshdfRSlESkLjv1opAqFF37P63lRRwZCSz9QCtTWCLdyTUaQMtRGeTi tPs1K6wTQX2Z9zr9U6kJG7Mv5Bishpu9RNQEd4mgXfwMbOSyFJCxLcuVoSmklFKwJhPU Z4bG9IaJ1u46pTTTeGfcCgDy1h8WBElGRm6mEXOp85mD968QjVwqViY8YNdAsWboNy5p w+qjPDzO3OSFLNbzFKmOjX+K1QulN6gxdX9Lalb1gfQGDweN2+8QMEOe2Bd2kIxgB/cR shi9DFUnuJRXc1rPK4ch7Yed7zPZBq5OQN7rRZm6KM9LIBRSUBJXQr0VuUBkfkKAfG5a wocA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google09082023 header.b=WB9W4QtN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id k20-20020a6568d4000000b005859c81f1e9si104639pgt.229.2023.11.10.11.52.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Nov 2023 11:52:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google09082023 header.b=WB9W4QtN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id DE302807933B; Fri, 10 Nov 2023 11:52:41 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236333AbjKJTw3 (ORCPT + 29 others); Fri, 10 Nov 2023 14:52:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346618AbjKJTwN (ORCPT ); Fri, 10 Nov 2023 14:52:13 -0500 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F393959FF for ; Thu, 9 Nov 2023 22:16:16 -0800 (PST) Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-6be1bc5aa1cso1746843b3a.3 for ; Thu, 09 Nov 2023 22:16:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google09082023; t=1699596976; x=1700201776; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=/UY2V4KB781XJBY0gcAuxuyVl/cjuGIjQ/iARGJ/tjI=; b=WB9W4QtN24qKq6I6EQamSBCq/cmDFN9aSGBJJjEiNO6o5/qSU7PGJ/K/TXJW+RxYz5 fx7MdjB17tbvPKdXYgPM1t4fBWuMrjvWa18+w9iscQMKJiU+kwsrYO1gDl04UKdUhi+s Kr58ljBmQCAeLZZ/iv1RHMkoQbrr6ln7VAP3zTGQPZxGFnRNutEL1pT6sACwVLaghD1g 6u/nyRBb2SsFJWi8WcW+hBoh/5FMKi8Nu0yuf55z43V/Xr3KU9Qe8MiQ7BvVO+Mvkvg7 P0ewV90uBk3WMcc/pzOH4MJPIc5PmK5SN1dkluYQr8w6UYjymoZL1UMNheYfMhaQDi+5 qrww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699596976; x=1700201776; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/UY2V4KB781XJBY0gcAuxuyVl/cjuGIjQ/iARGJ/tjI=; b=QzLkaBb/TPES1SNy+fXONCNekacgF7fMMld3YtOBP3R9FyKikwudWf2Gniqm1tL8Ou DoUxuMnGvkN5rIvFHt4g0ug8EldaQM4FmlXGOdHig5wKAHbOMR5eNH+GASjlEJmXHqEw MloX3Vu//DwYdquXwdIp2gp1mLTfVjZZuHgpkt+6o/AVXeccwy6TWIWoaeq74kyy8Pyv IKlWz8YbU0Ydo3sQUQVpe/BPDnx7v2i6lYWhnwBCEwcoZu29c2xQhNfsDYwZKX6Ph0u0 gYTW3k1nUxAyHmEyTdGewiDVTxRBxu0clyEDUHfL1Ikp8hHq6iPVgcxczDPCeOh3I7z+ N16A== X-Gm-Message-State: AOJu0YzLCcklZGV58DywAHSgFtMLKSBcaDLHbK0C5i7l+DAAzWyfd2PE 8fLo8CwEG1i31RW/4A/AvTHOqFXx27hd9ZU1czBWCg== X-Received: by 2002:ad4:4ea1:0:b0:66d:6af7:4571 with SMTP id ed1-20020ad44ea1000000b0066d6af74571mr8513666qvb.17.1699595366906; Thu, 09 Nov 2023 21:49:26 -0800 (PST) Received: from debian.debian ([140.141.197.139]) by smtp.gmail.com with ESMTPSA id w9-20020ae9e509000000b00765ab6d3e81sm468753qkf.122.2023.11.09.21.49.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 21:49:26 -0800 (PST) Date: Thu, 9 Nov 2023 21:49:24 -0800 From: Yan Zhai To: netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Weongyo Jeong , Ivan Babrou , David Ahern , Jesper Brouer , linux-kernel@vger.kernel.org, kernel-team@cloudflare.com Subject: [PATCH net-next] packet: add a generic drop reason for receive Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Fri, 10 Nov 2023 11:52:41 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782200958522337978 X-GMAIL-MSGID: 1782207966981687867 Commit da37845fdce2 ("packet: uses kfree_skb() for errors.") switches from consume_skb to kfree_skb to improve error handling. However, this could bring a lot of noises when we monitor real packet drops in kfree_skb[1], because in tpacket_rcv or packet_rcv only packet clones can be freed, not actual packets. Adding a generic drop reason to allow distinguish these "clone drops". [1]: https://lore.kernel.org/netdev/CABWYdi00L+O30Q=Zah28QwZ_5RU-xcxLFUK2Zj08A8MrLk9jzg@mail.gmail.com/ Fixes: da37845fdce2 ("packet: uses kfree_skb() for errors.") Signed-off-by: Yan Zhai --- include/net/dropreason-core.h | 6 ++++++ net/packet/af_packet.c | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 845dce805de7..6ff543fe8a8b 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -81,6 +81,7 @@ FN(IPV6_NDISC_NS_OTHERHOST) \ FN(QUEUE_PURGE) \ FN(TC_ERROR) \ + FN(PACKET_SOCK_ERROR) \ FNe(MAX) /** @@ -348,6 +349,11 @@ enum skb_drop_reason { SKB_DROP_REASON_QUEUE_PURGE, /** @SKB_DROP_REASON_TC_ERROR: generic internal tc error. */ SKB_DROP_REASON_TC_ERROR, + /** + * @SKB_DROP_REASON_PACKET_SOCK_ERROR: generic packet socket errors + * after its filter matches an incoming packet. + */ + SKB_DROP_REASON_PACKET_SOCK_ERROR, /** * @SKB_DROP_REASON_MAX: the maximum of core drop reasons, which * shouldn't be used as a real 'reason' - only for tracing code gen diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index a84e00b5904b..94b8a9d8e038 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -2128,6 +2128,7 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev, int skb_len = skb->len; unsigned int snaplen, res; bool is_drop_n_account = false; + enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; if (skb->pkt_type == PACKET_LOOPBACK) goto drop; @@ -2161,6 +2162,10 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev, res = run_filter(skb, sk, snaplen); if (!res) goto drop_n_restore; + + /* skb will only be "consumed" not "dropped" before this */ + drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR; + if (snaplen > res) snaplen = res; @@ -2230,7 +2235,7 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev, if (!is_drop_n_account) consume_skb(skb); else - kfree_skb(skb); + kfree_skb_reason(skb, drop_reason); return 0; } @@ -2253,6 +2258,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, bool is_drop_n_account = false; unsigned int slot_id = 0; int vnet_hdr_sz = 0; + enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT. * We may add members to them until current aligned size without forcing @@ -2355,6 +2361,10 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, vnet_hdr_sz = 0; } } + + /* skb will only be "consumed" not "dropped" before this */ + drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR; + spin_lock(&sk->sk_receive_queue.lock); h.raw = packet_current_rx_frame(po, skb, TP_STATUS_KERNEL, (macoff+snaplen)); @@ -2501,7 +2511,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, if (!is_drop_n_account) consume_skb(skb); else - kfree_skb(skb); + kfree_skb_reason(skb, drop_reason); return 0; drop_n_account: @@ -2510,7 +2520,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, is_drop_n_account = true; sk->sk_data_ready(sk); - kfree_skb(copy_skb); + kfree_skb_reason(copy_skb, drop_reason); goto drop_n_restore; }