From patchwork Thu Jun 27 19:43:00 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: 1123586 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="gkbqhG07"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45ZVgH31tHz9s3l for ; Fri, 28 Jun 2019 05:43:07 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726508AbfF0TnG (ORCPT ); Thu, 27 Jun 2019 15:43:06 -0400 Received: from mail-qk1-f201.google.com ([209.85.222.201]:34121 "EHLO mail-qk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726441AbfF0TnF (ORCPT ); Thu, 27 Jun 2019 15:43:05 -0400 Received: by mail-qk1-f201.google.com with SMTP id h198so3688989qke.1 for ; Thu, 27 Jun 2019 12:43:04 -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=L4qaAnZe4Ztf1cznBBEJGB9/EFFCE71pnMap9910TSE=; b=gkbqhG07O0Q4zmI01GDNaJVJ86oizZwselU1siybeml2dXn9iLkaTBNrD5PYHQuf1n aHk9ZWkNtl9fCI0g5LcDvQ/QqxKK9Zal3BnlYNI7vKiybcr59qyqEPJjyoQFe/EXoqgt +WgXziCKOyTsnisieU8KHUFBB8QWDoix5pUWRtb4NC0Py1tm27nlskI7K9H+SpO6B9Kq NRuCIsy89HFpjiRsR3L0qXfdIH75vK7GTu213XY+W7QRO/M2YMXf2WllM15LegTupiEU 8VqmcHNRBrPQzFGD0Q2j89VoFP9oQxuP1K7B9i6AZIL8S83qK31kICAEVrIBj5u0fzPl 5I1w== 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=L4qaAnZe4Ztf1cznBBEJGB9/EFFCE71pnMap9910TSE=; b=WLKtC74OyCQhXr6cToYr9vhnbzQtn4PiqxqBscofun2w0gW2jq9GhETurANRNILf93 jCeXx5UqWsequYTvS/Plb1YJnbNYS7lvrVMzbQzLagibOsdtWeqGaENhbPm/k/dFpo2w AapV6Abls2/XZ+tKev/u9zbAaAg/Gp0BfNteXcEfAO5vvvOclPkUKyFxO0E9x+C4xQVG XpJDsLlC306WdK6YticWySKocLizURt+lOKF6c4Bn+K8CjcLsf6ZOncpEbBC3/Ge5Esr 7AdK1vw7d4l9qpnopH1UIuofphJEIQabrHDsk0z9HDBDMgg4RyBufaZNgFnmBoJsXQSr BWLQ== X-Gm-Message-State: APjAAAVvE0bXa7CV3nqvG0JPqp1fLTIcLoTLrGAT311v3twcP3AinEKZ vkhYigiXcElUKzZ/GT+iHyZnqWUSs1pdGZMHHdTwNjuQXlPBZAJg7pyma+oEMgZWe8FltHXm/DK 7ap/UjLLAIV0EuEEqRb27qaEoWwG9GaA1a/6LsSJGQYSqVrmDw9AB8uGstSCE3Zt5 X-Google-Smtp-Source: APXvYqyJG2c7UcnCyFuwJtGG6DYGIFQxBFh4MHlmXFUVqH+YmqhjdUvhs/ZYgeDcGQi1Grb81M79rC7YyHp6 X-Received: by 2002:a37:47d6:: with SMTP id u205mr5112007qka.214.1561664584420; Thu, 27 Jun 2019 12:43:04 -0700 (PDT) Date: Thu, 27 Jun 2019 12:43:00 -0700 Message-Id: <20190627194300.92202-1-maheshb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCHv2 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 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 Thu Jun 27 19:43:05 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: 1123587 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="ODrfRPFd"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45ZVgL4b9kz9s3l for ; Fri, 28 Jun 2019 05:43:10 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726525AbfF0TnJ (ORCPT ); Thu, 27 Jun 2019 15:43:09 -0400 Received: from mail-ua1-f74.google.com ([209.85.222.74]:48825 "EHLO mail-ua1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726441AbfF0TnJ (ORCPT ); Thu, 27 Jun 2019 15:43:09 -0400 Received: by mail-ua1-f74.google.com with SMTP id s14so446386uap.15 for ; Thu, 27 Jun 2019 12:43:08 -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=bwxL9AOfJq1TYcqJgvHADeWXFMs7jT1tkEM8zoDSfPs=; b=ODrfRPFdwVk/nOWy3+njM94t2CicsQlKJaUSN4txJSRl0h4zbpq+UFWSw7qNb61tm9 c/iz7eyCIFJuFWHsJeZXV0xOxwK5WlRzGeXSVbEGb8PSIYuqyz9fbNZJOJADoVZtU4UH Y8dSYeWzzK7goyeAYESg1Cqq91oIGk1puPI3pkLY4wExbGLhflju+TiiOSdEept6icJk 8MqN/LfTwTG6LBWt5xPI88Fb8syWgXUHsddrFPzzIV9kqo9DhTw4ZCIhnhUq8rSjfzK1 4DR57j70pflHGIUbclydlSfbwyK+m9xbaznMkslR+2Pn8uZ7umEtazpO4crNLvMs1J/E F8SQ== 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=bwxL9AOfJq1TYcqJgvHADeWXFMs7jT1tkEM8zoDSfPs=; b=Qhj5trJHqx2gu6RRVAwYxuSf6jtMgVLdP/xUEBCUkGh1n5Dx+wNswOxDUtj+qPBH81 j0qNX/wsVXCUVnYy7rtnwBzVi5ULy/EFQ6RGGeYg1cjALDXmzVSZTW+CDWyuWo/6w7DD K0nDXMufWfZyilvPKx33GFDwdTn9bwmZU7scQd/ZOp6+zqcKN2LsRgSRIUh6Lmky7fmK wvdH/MV5THaRx6JSbCQMCAjq0z6DQLAdcAQy+ai7z1KOb0t5vu7GSepUKbkeuAHp+sTR OaP2J3GRZEdgLCt2qqmDxykKeg8YcHLWxqjwz8oYaPZy9e4FC1WAAhgct2K2rR7jkwfQ mI9A== X-Gm-Message-State: APjAAAVCVGSP0aUxQcMiM166e4uN/ttRI0S1Jg9/QxO/U3jEQ+sK/vwO +vsV3+XoJd85cjfE5ZAU39NqoSj0lOLSF+K5YgJSyGdrln2AinWtSPrZ3RTNBM1EE/onlts4p3w AcgVGP8YAJ+3lKbasDuIj02lnLZrTeOEx/xVBtrAIeV4oQU715ztZPQKsEqRsMeBZ X-Google-Smtp-Source: APXvYqzIukIgSgSY6fPid0W2d2msA8EHiwaJZ2+8IlNU4inLRzreROlEwhkLCQUce+zDELq0kZD/IdokMuBD X-Received: by 2002:a67:b919:: with SMTP id q25mr3948740vsn.18.1561664588434; Thu, 27 Jun 2019 12:43:08 -0700 (PDT) Date: Thu, 27 Jun 2019 12:43:05 -0700 Message-Id: <20190627194305.93241-1-maheshb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCHv2 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 --- v1 -> v2 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 59670fafcd26..d61f43feb7fa 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 e7c2824435c6..ff44c6287633 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 Thu Jun 27 19:43:09 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: 1123588 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="r07q6Qjg"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 45ZVgR4mC7z9s3l for ; Fri, 28 Jun 2019 05:43:15 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726540AbfF0TnO (ORCPT ); Thu, 27 Jun 2019 15:43:14 -0400 Received: from mail-pg1-f201.google.com ([209.85.215.201]:52740 "EHLO mail-pg1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726441AbfF0TnO (ORCPT ); Thu, 27 Jun 2019 15:43:14 -0400 Received: by mail-pg1-f201.google.com with SMTP id a13so1824242pgw.19 for ; Thu, 27 Jun 2019 12:43:13 -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=bOfc1i4cJ+0OyQJgUqhzaYp4v4An8v8V80psElK1Qt4=; b=r07q6QjgH5h2sh4dXF3sH5WvSqyg+2ZlaB5XBLQBxKWqbMGNA2w2MZnlgbGu/V8WAi kj8fvxVHzcU3ZcISgF+vwhLydqJRbeqCQ/AffPNTa1CQGNQatL9VhIt3IhqOBT9+Vikd QMU+Xi3Ckz5L5VZjvrQoTId1fE2O6QINdGPZPEirSD/++Xhqg9Bo1eURUEx/O1NxAxwc 4G8fjkPZzsPH5mwVGnRHcuNRfdmJfcZ4C4SzyEvWsSB5Y3+6xO+EE0CHtjfWLkbGqSFA nSknNk1TvQsoIqE+bJlkh7v3SeqZ0A8gPEFHfE5lSmmLSr7yYMXW2WBRDHawr+yEK1zO sDPA== 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=bOfc1i4cJ+0OyQJgUqhzaYp4v4An8v8V80psElK1Qt4=; b=ESCXVwMEG6OG7wg1ig5hBnXvNG6posbOOXEWJ8kEZG3Kj5+z2LXF7bINcUY2+5Gvq0 aIMajLGqbIDW5hAv0tfib00LGm6rYXv7fflDDw517LN1h156fvxm5Ijx9VgCZFg0OK8G JO2EY3eUsSPSDXv+sshWYtSvWPpo5AbDGSGsQPfZY79krUfpXMHLfLCJWAZQCOAT5W+k yoo2H+kvptz96d8LA/PTikO01nDg8SASHFmkbQNZinJ6qKrpSMWq9rnrD8wVeGZmJXBY GyRJacvXSq+AYCUdnGv2tSisQGaYJPgu9ojaoYo300Kgo4pZS9ZiVPbEtPBcLsQANvCK YGTw== X-Gm-Message-State: APjAAAU6WGlBkEQ+7kmARGiXpWUSKwotsazdX9B3HklkoUxEzWLhiGJl 7nQ61tBpMaqh1wJS47+gq2b1omrScRHeRdt/HyDD5WZJE6HfizCbbZO+MrwRX6Sqv3KpsRfZGYq Jcy9zSsmnKd7LH7lRBX94Bi9EvoJjGeg+8Ah/6RaIuLCjM+uCd7MeWYT/wN8bFe4u X-Google-Smtp-Source: APXvYqyVXahd8rY2KEozodT1tVnA223EdRuiS9C3eomDz7AYA+AshQiqO6pxGDUzs44o8wOUaBQX+ic/u+SP X-Received: by 2002:a63:c302:: with SMTP id c2mr5107327pgd.300.1561664592684; Thu, 27 Jun 2019 12:43:12 -0700 (PDT) Date: Thu, 27 Jun 2019 12:43:09 -0700 Message-Id: <20190627194309.94291-1-maheshb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCHv2 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 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..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..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