From patchwork Mon Jul 1 21:38:49 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: 1125627 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="oL9Fv6Z1"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45d1364gJXz9s8m for ; Tue, 2 Jul 2019 07:38:58 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726895AbfGAVi5 (ORCPT ); Mon, 1 Jul 2019 17:38:57 -0400 Received: from mail-pl1-f201.google.com ([209.85.214.201]:40277 "EHLO mail-pl1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726586AbfGAVi5 (ORCPT ); Mon, 1 Jul 2019 17:38:57 -0400 Received: by mail-pl1-f201.google.com with SMTP id 91so7876331pla.7 for ; Mon, 01 Jul 2019 14:38:56 -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=Nyh8E1HY6vj9p85/o0XGEaEVkuOIdVvLnsWWM9nvnG0=; b=oL9Fv6Z1qscU/HjXtwNvCYTcmBw3AyEBjCR2WdQMAuP4/LKPY/VtMoU7KIJZBRK6N5 gX8cy69wZbnKzhkS4oVfn3bzmQaCObsVhYRMBCkExl9wEIT8ASJbgNacel9dA7wifzhq y2wUgxNkQiaCX1iMoiCFDYV35USo0zQc5+KrYVnRs2mB0riTcpvesUXZmDpROiZkPPy6 C+BJjj7QeIqmBRUUmCkjkBL4l3q9zKTtd+hYifoMCqAfIARhPfHKZGiCiKsIbjUvASCg +MrNDcKNj9WfXGoiKSeRmmrmJyqJKAEPi6SjO2p7Z3NXzOhQe9MHWZVSn0shZ3SlXs1G +nSA== 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=Nyh8E1HY6vj9p85/o0XGEaEVkuOIdVvLnsWWM9nvnG0=; b=XTP+BG0Y93hO1rCLYaEeAZ0OqNid7/sL2OcD+GZXov6OdrK8hF4uv/HakN1gaGbJ93 qUWyWJzPF5C9Rla6xH2wKuO8lq2feF6nk3uBDwjRxox11J3hdiiCr++UplKVAxj5FtPQ mB+CpB5KUNepl5ZGdZVprKIef4FJ1je5zKzhvnbkbBVDrG8SvMXp5NZ5Bd3MdJ9Vet// shGBl6kNVIrLT0pY5e76diY+C3Ds4q8AAdQ75B6e5L+S3P8tFsKJ4KVYf4/Ddir30Q++ f5xItTNALZSZ8ijF0T2HJlaNhJxr6ugdH9sqshZl4ecaWXoyLJ0CROWPaM1ZMJg+QXhb yvPA== X-Gm-Message-State: APjAAAWFODCSQERwPaN1/Wc0RTizRY1T9D7fdFvVf2V97JYkQq8yvSzG J+vp8tLcrFMAoMSuRabZ67nJB2n7pGOzXdKUcBfVP/hyRnRahB6CBtC73y/TjntxWF1RjqW0SyR j9PfjQN48xi59qHfTNv4pJjsUKHlUrUCzPrnY8YdhpDFybU7TJwNdW0kSGZiRwGRU X-Google-Smtp-Source: APXvYqzKI5XfiIUj6rSIGpSkl0jCDLKLg8peZ5bBmxq0i8oxDwbaDxoBNeUi/7yEk9rWwgIP2Jc2yw39T29x X-Received: by 2002:a63:62c5:: with SMTP id w188mr15326770pgb.129.1562017135982; Mon, 01 Jul 2019 14:38:55 -0700 (PDT) Date: Mon, 1 Jul 2019 14:38:49 -0700 Message-Id: <20190701213849.102759-1-maheshb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCHv3 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 --- v1->v2->v3 no change 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 Mon Jul 1 21:38:57 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: 1125628 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="oLfJ/Vc4"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45d13C2wClz9sBp for ; Tue, 2 Jul 2019 07:39:03 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726960AbfGAVjC (ORCPT ); Mon, 1 Jul 2019 17:39:02 -0400 Received: from mail-qt1-f202.google.com ([209.85.160.202]:35931 "EHLO mail-qt1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726586AbfGAVjC (ORCPT ); Mon, 1 Jul 2019 17:39:02 -0400 Received: by mail-qt1-f202.google.com with SMTP id q26so14650518qtr.3 for ; Mon, 01 Jul 2019 14:39:01 -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=x4FJeAaIKyxT+9SBDURR60/ynQyNGLeRgihMlyWM7KI=; b=oLfJ/Vc4aQS4gtdl6QwyNNHEQkC6jx541ZwWX+2/t9xX7Y1HOw3ekFQ2nnE48bZ8YK cKpOcC+/0abnEpyHwGd1HT/ABHdlK53Eyg+qcYrMUNVn6oITxwTDXNPJ5BuulKAOCxPj PQJE9enXS49acYXIQ1QbPcl8vzOeWUo33xEtINjCtRR7tdkAXZ0Z6mxaiSKbPQ2L2fus dbGd/+w+DyuQGANYQTP1Y9IiUfYhDOpHoXkIIgx3B0nN5l3iStd+B9F3g27BhdzE+rlv 8YKC7sTyTfPn56oZsGNhkWY5l7QdIckWLQRElOJOOiy2y5w14mUcZjMhwfPDztKkKbgN aW8w== 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=x4FJeAaIKyxT+9SBDURR60/ynQyNGLeRgihMlyWM7KI=; b=Zw7+4DM62dcM8kmhEmrypx8oO0TD1U5coy9CohmIlwqnTqW0fF2DwmAErwBHZdlWOK TukXj81FwQ4/FMvvIKsTLTY69xX7Bk9j8E8ZkUb+iXoBTi/RLjFYcgJLAav6Ze1xAAr0 wOSsd0TavyGmVhP8xzWWiukUzodQkRn6qPLXdXZxUedCbhB9gcMSZYpp3aiidwtuoRmp 9SN6Ogl8GWvjUxXCWu2fTbW5L0sRsgQbP37K2bPXeChKIC1OTXANHkxnE9o06GODtHea 1hMeWiU8Nif2z7Bvf0EOffFSfq4JF4UdDoV1kvf30LKbqfbaQ9QAA/RhRwP4Kwn+C2iA 7UPA== X-Gm-Message-State: APjAAAXSYefqxNvrJqI9OUveV3kQUreWepUa0QJwgq0bAVuehZC8GUB5 TDRYMZQjG3kj1KjPImQKOh+v388pPXlKhZIYYoMNERAKVQWO7GAFCd15aNtYaD/a7hczUJ8FnoY TrwSC10/nBE7N9bNEIE/NimCmuM0j7CiX4e+LnHXxqDdEfdfImYxxGKsQEx243n8k X-Google-Smtp-Source: APXvYqxOqwsPyXfiHsPs8OFnA32uGQJN/0wbcP4X/tn32LprM/fRp6Uz3LKGKfSH6h2nEbZM/E6jlqLYcx+/ X-Received: by 2002:a0c:d941:: with SMTP id t1mr23449055qvj.176.1562017140946; Mon, 01 Jul 2019 14:39:00 -0700 (PDT) Date: Mon, 1 Jul 2019 14:38:57 -0700 Message-Id: <20190701213857.103511-1-maheshb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCHv3 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 Tested-by: Michael Chan --- v1->v2->v3 no change 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 bbd55c7f6b2e..dc1f510a7c81 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 7556275b1cef..39361f57351a 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 Mon Jul 1 21:39:01 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: 1125629 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="buuvMhMa"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45d13J03pLz9s8m for ; Tue, 2 Jul 2019 07:39:08 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726967AbfGAVjH (ORCPT ); Mon, 1 Jul 2019 17:39:07 -0400 Received: from mail-qk1-f201.google.com ([209.85.222.201]:48855 "EHLO mail-qk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726586AbfGAVjG (ORCPT ); Mon, 1 Jul 2019 17:39:06 -0400 Received: by mail-qk1-f201.google.com with SMTP id z13so14845421qka.15 for ; Mon, 01 Jul 2019 14:39:06 -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=XND7BWwKXkBnyW1GhGYJ6OdDkEnaIktIR9DB/vHtWb0=; b=buuvMhMab+zoZnFRscYKPoZEwsh1sFXw4HUCIYh+4fssdSdR1ymWQpEvqam6haSyG+ ZPgrrZg0fASW6XGUb7zedW6drOzlQnBdJ1X7nYP/956BD7wSnrTj6wgmKIe9U6PNQ4EJ FqMn39DEAZOovBcYq5fMjx/3lYo1bHHz8jv/q2RNSMzV8P7HWxbTu6/B+TPtmKB4whkw jJJ+oMlpiCsJ8hymwxOVIrtaRayLe2NF0HQCh3b4Srd/jShm5ps7/DMTU0Y4ZBm4vz+/ OUWrksWQvnK639wzJkEyPoec1T9CakAh+XihdZUpMxvdZGFhF94+z4tgSCU6RxWOeu8Y C7fQ== 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=XND7BWwKXkBnyW1GhGYJ6OdDkEnaIktIR9DB/vHtWb0=; b=ZupS58viT6cxPBcCXrhojX0/j6epfoCYLNE83m9Zg/YXjvX60tUnqlUinhxd+POx2i 4tobPdNKKLOfycWGU1zYWh+hiQGlh8624wts78B2MK4dpmNwAnUqoLAbDDLv8hXLUbzH betJDnuSEhvyUXjwlxpxiftCl/K1DD7bDwBbKDJPkuGQvrdRGYPRKUSIESyIDwPXKpvQ 8kCduCQdsII7wu3nztziAc1+4wnwLdDg2QwdqmUFzWDowaZc2T73pX1Ya7gMB/2JxWSL 04YTHFJMtMVopeYHQ5EC/+vJp7WKe/GGqOPLBiijqZdg79TrfpjiQ3uXtC1m73Pwt0zy OK9w== X-Gm-Message-State: APjAAAULGDWo3Uphsn9yW/QquwZ3Q6/fa7e0alj5HFGyw1cgyzrh+nwr bzwg/i8JbrV4AOg7xVQDL352Mr9jvB0YL2dx/ZaG/rujPx8KsNBbcvWYxm1q+ZMcP7LAEPTTZ2J iulptFrkajvNYc9qf85q+c5n2gwWcVmSK4ntfHcYuYMOxDfbsAH4iwf8UddiJGfJN X-Google-Smtp-Source: APXvYqysxIv9CJX92gMfAJ0Jw4SDhcmvvE/gy45Wy38s/qKOUqMkGYzb+xsRnO1J4Pdd4nK1HzeGRcw/VvVI X-Received: by 2002:ac8:303c:: with SMTP id f57mr22558649qte.294.1562017145523; Mon, 01 Jul 2019 14:39:05 -0700 (PDT) Date: Mon, 1 Jul 2019 14:39:01 -0700 Message-Id: <20190701213901.104160-1-maheshb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCHv3 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 --- v1 -> v2 fixed the conflict resolution in selftests Makefile v2 -> v3 fixed lib/Kconfig.debug tristate text / string. lib/Kconfig.debug | 9 ++ lib/Makefile | 1 + lib/test_blackhole_dev.c | 100 ++++++++++++++++++ tools/testing/selftests/net/Makefile | 2 +- tools/testing/selftests/net/config | 1 + .../selftests/net/test_blackhole_dev.sh | 11 ++ 6 files changed, 123 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..99272b5dd980 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 blackhole netdev 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 dcb558c7554d..6ac44fe2a37f 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..1b24e36b4047 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -5,7 +5,7 @@ CFLAGS = -Wall -Wl,--no-as-needed -O2 -g CFLAGS += -I../../../../usr/include/ 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