From patchwork Sat Jun 22 00:45:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?TWFoZXNoIEJhbmRld2FyICjgpK7gpLngpYfgpLYg4KSs4KSC4KSh4KWH4KS14KS+4KSwKQ==?= X-Patchwork-Id: 1120590 X-Patchwork-Delegate: davem@davemloft.net 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=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="In2BiNBn"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45Vxg46Qqxz9s4Y for ; Sat, 22 Jun 2019 10:45:36 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726485AbfFVApf (ORCPT ); Fri, 21 Jun 2019 20:45:35 -0400 Received: from mail-qk1-f201.google.com ([209.85.222.201]:51615 "EHLO mail-qk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726058AbfFVApe (ORCPT ); Fri, 21 Jun 2019 20:45:34 -0400 Received: by mail-qk1-f201.google.com with SMTP id s25so9417464qkj.18 for ; Fri, 21 Jun 2019 17:45:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=sTDk3S9JzZt0ASSy5OGsWTZeBwZz9FXjYYt9px6OwsA=; b=In2BiNBn0kkUH3/cIsIxlIUSCoQS4wGJcOnpCTsp+/CxpQuTBB1LBMjOo9aDNa2dQR Ezfk3QR+b6qiSkmCXtcKTeNZ/HHXknTEk6surqMjsJKPkkkAVTKU5ZS6WNGQ/sf4KbGt QjLiuVs46xwYsx2Nu9pBmkXBfkc79532/ld0oVuEO2zmGhCAIm/jvfGiKwzkyA1geJUJ mG+BQYJxmcCdewZSGHwjrrMXM68JhCNbTmBT+a3mBZciluGvcuG1z+FrXmJvXPuVY0jI wIRtqLFo32B3xZuCpOCJUHqaaEBx8rZV+JLSgNJrTIu4B61RW1ezTbDnfRgMPV3oPMek 4WEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=sTDk3S9JzZt0ASSy5OGsWTZeBwZz9FXjYYt9px6OwsA=; b=k3ppavTsMIDEt31YIdEGc0SIRQnSv/tPYGRrAiK7cpOtZRteUmqS8hfHrqeF9l6Zj1 ggCuQ04DsRi9Tkv+R7v4+gV9kUtI8H8sZSeafRD6EeQQ6Xg9+SsFUXz14pTnwnqLnF6E xvu1IN2sqymKnm+Gk7lFHmq094KCaVOqiTWQvaWhxNwGukOggSRMjXj3qujALWszvI08 GIwRuevYSurEVbQtkbnk7WrBpL4raljk6wuIsi1e/1QAkOq1KKKSwIFgQ00EosB0b+kf bqXPy1w9uFaNaG4mD4XLnuHgTSqdcaZjELLfsw8PophOhA5OrTaKJvqBqNo4UBs7bkgl NYkw== X-Gm-Message-State: APjAAAUsty9wJrd1BcjoKLJ7/ffizTgdJqmPKH9qdhj3bQPzmXJtctZL dG1nfClY5XCaMY4mNT7hWMbnakC3YAnE4AWxO9ea7jRumChbU6ujBm+Ygkjerr2vSlmqgGlS6l6 gCfMYa4zfS7l6mKjPkAapMuXa6Px/bJ9umXiTl3IBOoGJxLlt/HL6IXyDF0yvmAk3 X-Google-Smtp-Source: APXvYqzKs4gL37RTdNM2W5KQoaFNTr0P4J9IsIwDa/jE7PcCZRweNuy1FGVIwWq9viZ3O+Oj3GF9DgEc1JxG X-Received: by 2002:a37:9cf:: with SMTP id 198mr93231559qkj.351.1561164333335; Fri, 21 Jun 2019 17:45:33 -0700 (PDT) Date: Fri, 21 Jun 2019 17:45:25 -0700 Message-Id: <20190622004525.90270-1-maheshb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH next 1/3] loopback: create blackhole net device similar to loopack. From: Mahesh Bandewar To: Netdev Cc: Eric Dumazet , David Miller , Michael Chan , Daniel Axtens , Mahesh Bandewar , Mahesh Bandewar Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Create a blackhole net device that can be used for "dead" dst entries instead of loopback device. This blackhole device differs from loopback in few aspects: (a) It's not per-ns. (b) MTU on this device is ETH_MIN_MTU (c) The xmit function is essentially kfree_skb(). and (d) since it's not registered it won't have ifindex. Lower MTU effectively make the device not pass the MTU check during the route check when a dst associated with the skb is dead. Signed-off-by: Mahesh Bandewar --- drivers/net/loopback.c | 76 ++++++++++++++++++++++++++++++++++----- include/linux/netdevice.h | 2 ++ 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/drivers/net/loopback.c b/drivers/net/loopback.c index 87d361666cdd..3b39def5471e 100644 --- a/drivers/net/loopback.c +++ b/drivers/net/loopback.c @@ -55,6 +55,13 @@ #include #include +/* blackhole_netdev - a device used for dsts that are marked expired! + * This is global device (instead of per-net-ns) since it's not needed + * to be per-ns and gets initialized at boot time. + */ +struct net_device *blackhole_netdev; +EXPORT_SYMBOL(blackhole_netdev); + /* The higher levels take care of making this non-reentrant (it's * called with bh's disabled). */ @@ -150,12 +157,14 @@ static const struct net_device_ops loopback_ops = { .ndo_set_mac_address = eth_mac_addr, }; -/* The loopback device is special. There is only one instance - * per network namespace. - */ -static void loopback_setup(struct net_device *dev) +static void gen_lo_setup(struct net_device *dev, + unsigned int mtu, + const struct ethtool_ops *eth_ops, + const struct header_ops *hdr_ops, + const struct net_device_ops *dev_ops, + void (*dev_destructor)(struct net_device *dev)) { - dev->mtu = 64 * 1024; + dev->mtu = mtu; dev->hard_header_len = ETH_HLEN; /* 14 */ dev->min_header_len = ETH_HLEN; /* 14 */ dev->addr_len = ETH_ALEN; /* 6 */ @@ -174,11 +183,20 @@ static void loopback_setup(struct net_device *dev) | NETIF_F_NETNS_LOCAL | NETIF_F_VLAN_CHALLENGED | NETIF_F_LOOPBACK; - dev->ethtool_ops = &loopback_ethtool_ops; - dev->header_ops = ð_header_ops; - dev->netdev_ops = &loopback_ops; + dev->ethtool_ops = eth_ops; + dev->header_ops = hdr_ops; + dev->netdev_ops = dev_ops; dev->needs_free_netdev = true; - dev->priv_destructor = loopback_dev_free; + dev->priv_destructor = dev_destructor; +} + +/* The loopback device is special. There is only one instance + * per network namespace. + */ +static void loopback_setup(struct net_device *dev) +{ + gen_lo_setup(dev, (64 * 1024), &loopback_ethtool_ops, ð_header_ops, + &loopback_ops, loopback_dev_free); } /* Setup and register the loopback device. */ @@ -213,3 +231,43 @@ static __net_init int loopback_net_init(struct net *net) struct pernet_operations __net_initdata loopback_net_ops = { .init = loopback_net_init, }; + +/* blackhole netdevice */ +static netdev_tx_t blackhole_netdev_xmit(struct sk_buff *skb, + struct net_device *dev) +{ + kfree_skb(skb); + net_warn_ratelimited("%s(): Dropping skb.\n", __func__); + return NETDEV_TX_OK; +} + +static const struct net_device_ops blackhole_netdev_ops = { + .ndo_start_xmit = blackhole_netdev_xmit, +}; + +/* This is a dst-dummy device used specifically for invalidated + * DSTs and unlike loopback, this is not per-ns. + */ +static void blackhole_netdev_setup(struct net_device *dev) +{ + gen_lo_setup(dev, ETH_MIN_MTU, NULL, NULL, &blackhole_netdev_ops, NULL); +} + +/* Setup and register the blackhole_netdev. */ +static int __init blackhole_netdev_init(void) +{ + blackhole_netdev = alloc_netdev(0, "blackhole_dev", NET_NAME_UNKNOWN, + blackhole_netdev_setup); + if (!blackhole_netdev) + return -ENOMEM; + + dev_init_scheduler(blackhole_netdev); + dev_activate(blackhole_netdev); + + blackhole_netdev->flags |= IFF_UP | IFF_RUNNING; + dev_net_set(blackhole_netdev, &init_net); + + return 0; +} + +device_initcall(blackhole_netdev_init); diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index eeacebd7debb..88292953aa6f 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -4870,4 +4870,6 @@ do { \ #define PTYPE_HASH_SIZE (16) #define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1) +extern struct net_device *blackhole_netdev; + #endif /* _LINUX_NETDEVICE_H */ From patchwork Sat Jun 22 00:45:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?TWFoZXNoIEJhbmRld2FyICjgpK7gpLngpYfgpLYg4KSs4KSC4KSh4KWH4KS14KS+4KSwKQ==?= X-Patchwork-Id: 1120592 X-Patchwork-Delegate: davem@davemloft.net 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=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="vEOAzobq"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45VxgG6rXcz9s4Y for ; Sat, 22 Jun 2019 10:45:46 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726423AbfFVApo (ORCPT ); Fri, 21 Jun 2019 20:45:44 -0400 Received: from mail-vk1-f201.google.com ([209.85.221.201]:38545 "EHLO mail-vk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726058AbfFVApo (ORCPT ); Fri, 21 Jun 2019 20:45:44 -0400 Received: by mail-vk1-f201.google.com with SMTP id u202so3100800vku.5 for ; Fri, 21 Jun 2019 17:45:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=mmP+fV5cy5/NoNcNYiiJaN4iM05M45CWe+WxnFdBezM=; b=vEOAzobq3y+X9MlSV5eP2oe7+N/g/Mm5mbcvStpzbrIpiAu1+QMLLrmBwChpivj9k3 C5IYWaoWIeye1hV6xBD3nB1eZeIh0f4Q3nq3m5pVFWn5yydfezPDohwVzwA0iKPUc9qX amYKb+ItYz7jdAcUB4mxW7oB34fN+NlHtoVLuOWH0JBT9cMHIJkuBk+PPb7yevfLDp7i kgeacW9SJsU6fQcxtxGeiSemr7Npv3uhnKk6bVgLp7EsExud4KEMEOOqEsWy+f7l46M7 E5vebr6d2NW//CZgLPAnTy+jBrUlAQXCE+4XS+VpLAV32Wy6wLmur8UvZWZhcXXDxtIM E/qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=mmP+fV5cy5/NoNcNYiiJaN4iM05M45CWe+WxnFdBezM=; b=WC03sktIBJICuj8gZc0+zyJl2cqUC/qhPYuhANdMH86qqx1Qtugr1FbFS85VMetboj 3WdIr5wqQF1ze4dfTY1S2yVgyUI1wE9uECky+okguSjjZLBqkYSdS2+uRFUZUmPTIR2j 1vaKfbDOdvATPRaAhMEx/bvbNi1Dv9PWkxbqancBaMgP/RapgNq2+FMIHrJvXFV2eE6G jmyKH5KDyzOKAoJeJJFmoBLS8pw7QRE0knqSY8eXfVnDJInTWFuaBW+Lq6zYk6JeqeUz UO2G1wT3ATel0MbsBaiVPKvkC77FU6v/1F0VJkbi7sJ6+U7c5CfRKKQXdFu4rkph8kGU qFTQ== X-Gm-Message-State: APjAAAWsw26SaLGCy7j90dUt0CpBNxOyHoXUvI9OAPVh7EFjSocniwea Vf0xVbMTdcKVgurZwV/hXTpcw4Wx+bAu0whG9TWcnYXmglJraRdUhLs2/Tex4Sh4DHVCJ2EAmFT uq66Fw936PozpjlpHPSoFCRUqq+ubau3mOZL9E4qlK5GXHvdmorIB1C4hdFSHj2S5 X-Google-Smtp-Source: APXvYqyi+EaWy1QpXiWZ0XOHMgKgC0UDooCGjJLuZj7kFLt5A1gQcQz7cEZseuTzcuf6vfsJhGLn5gZggM9r X-Received: by 2002:ab0:30c7:: with SMTP id c7mr23676228uam.143.1561164338200; Fri, 21 Jun 2019 17:45:38 -0700 (PDT) Date: Fri, 21 Jun 2019 17:45:34 -0700 Message-Id: <20190622004534.91201-1-maheshb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH next 2/3] blackhole_netdev: use blackhole_netdev to invalidate dst entries From: Mahesh Bandewar To: Netdev Cc: Eric Dumazet , David Miller , Michael Chan , Daniel Axtens , Mahesh Bandewar , Mahesh Bandewar Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Use blackhole_netdev instead of 'lo' device with lower MTU when marking dst "dead". Signed-off-by: Mahesh Bandewar --- net/core/dst.c | 2 +- net/ipv4/route.c | 3 +-- net/ipv6/route.c | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/net/core/dst.c b/net/core/dst.c index e46366228eaf..1325316d9eab 100644 --- a/net/core/dst.c +++ b/net/core/dst.c @@ -160,7 +160,7 @@ void dst_dev_put(struct dst_entry *dst) dst->ops->ifdown(dst, dev, true); dst->input = dst_discard; dst->output = dst_discard_out; - dst->dev = dev_net(dst->dev)->loopback_dev; + dst->dev = blackhole_netdev; dev_hold(dst->dev); dev_put(dev); } diff --git a/net/ipv4/route.c b/net/ipv4/route.c index 66cbe8a7a168..049af0f88307 100644 --- a/net/ipv4/route.c +++ b/net/ipv4/route.c @@ -1532,7 +1532,6 @@ static void ipv4_dst_destroy(struct dst_entry *dst) void rt_flush_dev(struct net_device *dev) { - struct net *net = dev_net(dev); struct rtable *rt; int cpu; @@ -1543,7 +1542,7 @@ void rt_flush_dev(struct net_device *dev) list_for_each_entry(rt, &ul->head, rt_uncached) { if (rt->dst.dev != dev) continue; - rt->dst.dev = net->loopback_dev; + rt->dst.dev = blackhole_netdev; dev_hold(rt->dst.dev); dev_put(dev); } diff --git a/net/ipv6/route.c b/net/ipv6/route.c index c4d285fe0adc..335bcf812b25 100644 --- a/net/ipv6/route.c +++ b/net/ipv6/route.c @@ -176,7 +176,7 @@ static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) } if (rt_dev == dev) { - rt->dst.dev = loopback_dev; + rt->dst.dev = blackhole_netdev; dev_hold(rt->dst.dev); dev_put(rt_dev); } From patchwork Sat Jun 22 00:45:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?TWFoZXNoIEJhbmRld2FyICjgpK7gpLngpYfgpLYg4KSs4KSC4KSh4KWH4KS14KS+4KSwKQ==?= X-Patchwork-Id: 1120591 X-Patchwork-Delegate: davem@davemloft.net 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=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="jS2mt9gR"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45VxgF3TDQz9s4Y for ; Sat, 22 Jun 2019 10:45:45 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726497AbfFVApo (ORCPT ); Fri, 21 Jun 2019 20:45:44 -0400 Received: from mail-vk1-f201.google.com ([209.85.221.201]:55662 "EHLO mail-vk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726137AbfFVApo (ORCPT ); Fri, 21 Jun 2019 20:45:44 -0400 Received: by mail-vk1-f201.google.com with SMTP id b85so3056911vke.22 for ; Fri, 21 Jun 2019 17:45:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=kP76Lb5ppqhKfq4Tg1hR23zjC7FzxWzxkzO08aA94QA=; b=jS2mt9gRycnO/DWZpHUF3j3YCgufUO4v0d1SxqArsHRD2qBdVC3SoQxhQFsIUFlMHu 6+wsyT1wY+4G5Ayx71dYz2cYFbQCzs22KblveMTNn9qICLrpe7SUoCFbHn8lGEl8rO9T 6UiYAA41RXzMPBdi1jXZ9JjfCb/bQRGWpuhmSZfgZu9MDFr4LlXZRDYnw9quGibjUWtO c5dU3nesRue87AhvZ+Jtmf1W+pYJ5ljNv88Fnk+OXdaiC2exvmyoM+TT0SU9wjleSGgw sJM0Qf8w+eGj+2SxXgnEczktcJaiwSLnul0AIHOZseje/lAZoTKV4H7IorgyRgkOvDgi 1wAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=kP76Lb5ppqhKfq4Tg1hR23zjC7FzxWzxkzO08aA94QA=; b=svxiDfVebAcg3nkfuceaYdaWfdlxwjjYVRm/5TwDdy487+fkSooxKXK1m6Gxzj572L MFL4Ib8Ydm8PFW/RBfXhVYUMxwu+jneXSrCUbgczZXPFGyTWmrzYivNjtjyA+YhWwhXk YdgnUyckLLGXf7omiCOMzpM9/Yx+Df2QF2bZ+81B7+5DjCrNIN/tvUdEN22Sn+yxTeZI 1JsX5iCjOZTvgTrR9qYjLZtj0xf56OoG8hjqTgg8fqu9LLcR+dMGWahhlFEg1ZhQVCRA QSYecvwYZw1xJUwTAZiOPZiBxkwrPSQKAu60HafV3mswQIzu7AX8E1c9oLYl4e+GHwfc kbEA== X-Gm-Message-State: APjAAAVv0uXpGA+Zdkb9YgPDIuaP46v0Dt7FrLrxThl1zKJRiTMRQhS5 EHKz3TrxRKLwahEWop4vGqE90LBECp/8M8lXWjioQvEfPv+TtOl4Dp/HjOiNDlwILNXoAcE91op xYDNS4ESoiY/M48OX/Fj2lWiTBLff74dnZQSBSYw77z3Rix3WzGebwUFdoOmR8LGL X-Google-Smtp-Source: APXvYqymLPzxIC4XS/8MexDja+7XkqrkYGR8pMY0/9x4iu8mNAPuHZcqQCE4GgLFtv2mRiqcX+/Jh7glP1c2 X-Received: by 2002:ab0:2756:: with SMTP id c22mr30186226uap.22.1561164343258; Fri, 21 Jun 2019 17:45:43 -0700 (PDT) Date: Fri, 21 Jun 2019 17:45:39 -0700 Message-Id: <20190622004539.92199-1-maheshb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH next 3/3] blackhole_dev: add a selftest From: Mahesh Bandewar To: Netdev Cc: Eric Dumazet , David Miller , Michael Chan , Daniel Axtens , Mahesh Bandewar , Mahesh Bandewar Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Since this is not really a device with all capabilities, this test ensures that it has *enough* to make it through the data path without causing unwanted side-effects (read crash!). Signed-off-by: Mahesh Bandewar --- lib/Kconfig.debug | 9 ++ lib/Makefile | 1 + lib/test_blackhole_dev.c | 100 ++++++++++++++++++ tools/testing/selftests/net/Makefile | 3 +- tools/testing/selftests/net/config | 1 + .../selftests/net/test_blackhole_dev.sh | 11 ++ 6 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 lib/test_blackhole_dev.c create mode 100755 tools/testing/selftests/net/test_blackhole_dev.sh diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index cbdfae379896..79d96e1614f5 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1909,6 +1909,15 @@ config TEST_BPF If unsure, say N. +config TEST_BLACKHOLE_DEV + tristate "Test BPF filter functionality" + depends on m && NET + help + This builds the "test_blackhole_dev" module that validates the + data path through this blackhole netdev. + + If unsure, say N. + config FIND_BIT_BENCHMARK tristate "Test find_bit functions" help diff --git a/lib/Makefile b/lib/Makefile index fb7697031a79..c293624d3664 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -91,6 +91,7 @@ obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o obj-$(CONFIG_TEST_STACKINIT) += test_stackinit.o +obj-$(CONFIG_TEST_BLACKHOLE_DEV) += test_blackhole_dev.o obj-$(CONFIG_TEST_LIVEPATCH) += livepatch/ diff --git a/lib/test_blackhole_dev.c b/lib/test_blackhole_dev.c new file mode 100644 index 000000000000..4c40580a99a3 --- /dev/null +++ b/lib/test_blackhole_dev.c @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This module tests the blackhole_dev that is created during the + * net subsystem initialization. The test this module performs is + * by injecting an skb into the stack with skb->dev as the + * blackhole_dev and expects kernel to behave in a sane manner + * (in other words, *not crash*)! + * + * Copyright (c) 2018, Mahesh Bandewar + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +#define SKB_SIZE 256 +#define HEAD_SIZE (14+40+8) /* Ether + IPv6 + UDP */ +#define TAIL_SIZE 32 /* random tail-room */ + +#define UDP_PORT 1234 + +static int __init test_blackholedev_init(void) +{ + struct ipv6hdr *ip6h; + struct sk_buff *skb; + struct ethhdr *ethh; + struct udphdr *uh; + int data_len; + int ret; + + skb = alloc_skb(SKB_SIZE, GFP_KERNEL); + if (!skb) + return -ENOMEM; + + /* Reserve head-room for the headers */ + skb_reserve(skb, HEAD_SIZE); + + /* Add data to the skb */ + data_len = SKB_SIZE - (HEAD_SIZE + TAIL_SIZE); + memset(__skb_put(skb, data_len), 0xf, data_len); + + /* Add protocol data */ + /* (Transport) UDP */ + uh = (struct udphdr *)skb_push(skb, sizeof(struct udphdr)); + skb_set_transport_header(skb, 0); + uh->source = uh->dest = htons(UDP_PORT); + uh->len = htons(data_len); + uh->check = 0; + /* (Network) IPv6 */ + ip6h = (struct ipv6hdr *)skb_push(skb, sizeof(struct ipv6hdr)); + skb_set_network_header(skb, 0); + ip6h->hop_limit = 32; + ip6h->payload_len = data_len + sizeof(struct udphdr); + ip6h->nexthdr = IPPROTO_UDP; + ip6h->saddr = in6addr_loopback; + ip6h->daddr = in6addr_loopback; + /* Ether */ + ethh = (struct ethhdr *)skb_push(skb, sizeof(struct ethhdr)); + skb_set_mac_header(skb, 0); + + skb->protocol = htons(ETH_P_IPV6); + skb->pkt_type = PACKET_HOST; + skb->dev = blackhole_netdev; + + /* Now attempt to send the packet */ + ret = dev_queue_xmit(skb); + + switch (ret) { + case NET_XMIT_SUCCESS: + pr_warn("dev_queue_xmit() returned NET_XMIT_SUCCESS\n"); + break; + case NET_XMIT_DROP: + pr_warn("dev_queue_xmit() returned NET_XMIT_DROP\n"); + break; + case NET_XMIT_CN: + pr_warn("dev_queue_xmit() returned NET_XMIT_CN\n"); + break; + default: + pr_err("dev_queue_xmit() returned UNKNOWN(%d)\n", ret); + } + + return 0; +} + +static void __exit test_blackholedev_exit(void) +{ + pr_warn("test_blackholedev module terminating.\n"); +} + +module_init(test_blackholedev_init); +module_exit(test_blackholedev_exit); + +MODULE_AUTHOR("Mahesh Bandewar "); +MODULE_LICENSE("GPL"); diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index 9a275d932fd5..fac7e9eca034 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -4,8 +4,9 @@ CFLAGS = -Wall -Wl,--no-as-needed -O2 -g CFLAGS += -I../../../../usr/include/ +<<<<<<< HEAD TEST_PROGS := run_netsocktests run_afpackettests test_bpf.sh netdevice.sh \ - rtnetlink.sh xfrm_policy.sh + rtnetlink.sh xfrm_policy.sh test_blackhole_dev.sh TEST_PROGS += fib_tests.sh fib-onlink-tests.sh pmtu.sh udpgso.sh ip_defrag.sh TEST_PROGS += udpgso_bench.sh fib_rule_tests.sh msg_zerocopy.sh psock_snd.sh TEST_PROGS += udpgro_bench.sh udpgro.sh test_vxlan_under_vrf.sh reuseport_addr_any.sh diff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config index 89f84b5118bf..e4b878d95ba0 100644 --- a/tools/testing/selftests/net/config +++ b/tools/testing/selftests/net/config @@ -27,3 +27,4 @@ CONFIG_NFT_CHAIN_NAT_IPV6=m CONFIG_NFT_CHAIN_NAT_IPV4=m CONFIG_NET_SCH_FQ=m CONFIG_NET_SCH_ETF=m +CONFIG_TEST_BLACKHOLE_DEV=m diff --git a/tools/testing/selftests/net/test_blackhole_dev.sh b/tools/testing/selftests/net/test_blackhole_dev.sh new file mode 100755 index 000000000000..3119b80e711f --- /dev/null +++ b/tools/testing/selftests/net/test_blackhole_dev.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# Runs blackhole-dev test using blackhole-dev kernel module + +if /sbin/modprobe -q test_blackhole_dev ; then + /sbin/modprobe -q -r test_blackhole_dev; + echo "test_blackhole_dev: ok"; +else + echo "test_blackhole_dev: [FAIL]"; + exit 1; +fi