Skip to content

Commit b338524

Browse files
committed
loopback: create blackhole net device similar to loopack.
jira LE-3201 Rebuild_History Non-Buildable kernel-rt-4.18.0-553.27.1.rt7.368.el8_10 commit-author Mahesh Bandewar <maheshb@google.com> commit 4de83b8 Empty-Commit: Cherry-Pick Conflicts during history rebuild. Will be included in final tarball splat. Ref for failed cherry-pick at: ciq/ciq_backports/kernel-rt-4.18.0-553.27.1.rt7.368.el8_10/4de83b88.failed 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 <maheshb@google.com> Signed-off-by: David S. Miller <davem@davemloft.net> (cherry picked from commit 4de83b8) Signed-off-by: Jonathan Maple <jmaple@ciq.com> # Conflicts: # include/linux/netdevice.h
1 parent 91eb418 commit b338524

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
loopback: create blackhole net device similar to loopack.
2+
3+
jira LE-3201
4+
Rebuild_History Non-Buildable kernel-rt-4.18.0-553.27.1.rt7.368.el8_10
5+
commit-author Mahesh Bandewar <maheshb@google.com>
6+
commit 4de83b88c66a1e4dba426b29766fb68e61d93792
7+
Empty-Commit: Cherry-Pick Conflicts during history rebuild.
8+
Will be included in final tarball splat. Ref for failed cherry-pick at:
9+
ciq/ciq_backports/kernel-rt-4.18.0-553.27.1.rt7.368.el8_10/4de83b88.failed
10+
11+
Create a blackhole net device that can be used for "dead"
12+
dst entries instead of loopback device. This blackhole device differs
13+
from loopback in few aspects: (a) It's not per-ns. (b) MTU on this
14+
device is ETH_MIN_MTU (c) The xmit function is essentially kfree_skb().
15+
and (d) since it's not registered it won't have ifindex.
16+
17+
Lower MTU effectively make the device not pass the MTU check during
18+
the route check when a dst associated with the skb is dead.
19+
20+
Signed-off-by: Mahesh Bandewar <maheshb@google.com>
21+
Signed-off-by: David S. Miller <davem@davemloft.net>
22+
(cherry picked from commit 4de83b88c66a1e4dba426b29766fb68e61d93792)
23+
Signed-off-by: Jonathan Maple <jmaple@ciq.com>
24+
25+
# Conflicts:
26+
# include/linux/netdevice.h
27+
diff --cc include/linux/netdevice.h
28+
index 41a5e560a32a,88292953aa6f..000000000000
29+
--- a/include/linux/netdevice.h
30+
+++ b/include/linux/netdevice.h
31+
@@@ -5414,9 -4870,6 +5414,13 @@@ do {
32+
#define PTYPE_HASH_SIZE (16)
33+
#define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1)
34+
35+
++<<<<<<< HEAD
36+
+/* Note: Avoid these macros in fast path, prefer per-cpu or per-queue counters. */
37+
+#define DEV_STATS_INC(DEV, FIELD) atomic_long_inc(&(DEV)->stats.__##FIELD)
38+
+#define DEV_STATS_ADD(DEV, FIELD, VAL) \
39+
+ atomic_long_add((VAL), &(DEV)->stats.__##FIELD)
40+
++=======
41+
+ extern struct net_device *blackhole_netdev;
42+
++>>>>>>> 4de83b88c66a (loopback: create blackhole net device similar to loopack.)
43+
44+
#endif /* _LINUX_NETDEVICE_H */
45+
diff --git a/drivers/net/loopback.c b/drivers/net/loopback.c
46+
index 2df7f60fe052..48d7842d5f9c 100644
47+
--- a/drivers/net/loopback.c
48+
+++ b/drivers/net/loopback.c
49+
@@ -59,6 +59,13 @@
50+
#include <net/net_namespace.h>
51+
#include <linux/u64_stats_sync.h>
52+
53+
+/* blackhole_netdev - a device used for dsts that are marked expired!
54+
+ * This is global device (instead of per-net-ns) since it's not needed
55+
+ * to be per-ns and gets initialized at boot time.
56+
+ */
57+
+struct net_device *blackhole_netdev;
58+
+EXPORT_SYMBOL(blackhole_netdev);
59+
+
60+
/* The higher levels take care of making this non-reentrant (it's
61+
* called with bh's disabled).
62+
*/
63+
@@ -166,12 +173,14 @@ static const struct net_device_ops loopback_ops = {
64+
.ndo_set_mac_address = eth_mac_addr,
65+
};
66+
67+
-/* The loopback device is special. There is only one instance
68+
- * per network namespace.
69+
- */
70+
-static void loopback_setup(struct net_device *dev)
71+
+static void gen_lo_setup(struct net_device *dev,
72+
+ unsigned int mtu,
73+
+ const struct ethtool_ops *eth_ops,
74+
+ const struct header_ops *hdr_ops,
75+
+ const struct net_device_ops *dev_ops,
76+
+ void (*dev_destructor)(struct net_device *dev))
77+
{
78+
- dev->mtu = 64 * 1024;
79+
+ dev->mtu = mtu;
80+
dev->hard_header_len = ETH_HLEN; /* 14 */
81+
dev->min_header_len = ETH_HLEN; /* 14 */
82+
dev->addr_len = ETH_ALEN; /* 6 */
83+
@@ -190,11 +199,20 @@ static void loopback_setup(struct net_device *dev)
84+
| NETIF_F_NETNS_LOCAL
85+
| NETIF_F_VLAN_CHALLENGED
86+
| NETIF_F_LOOPBACK;
87+
- dev->ethtool_ops = &loopback_ethtool_ops;
88+
- dev->header_ops = &eth_header_ops;
89+
- dev->netdev_ops = &loopback_ops;
90+
+ dev->ethtool_ops = eth_ops;
91+
+ dev->header_ops = hdr_ops;
92+
+ dev->netdev_ops = dev_ops;
93+
dev->needs_free_netdev = true;
94+
- dev->priv_destructor = loopback_dev_free;
95+
+ dev->priv_destructor = dev_destructor;
96+
+}
97+
+
98+
+/* The loopback device is special. There is only one instance
99+
+ * per network namespace.
100+
+ */
101+
+static void loopback_setup(struct net_device *dev)
102+
+{
103+
+ gen_lo_setup(dev, (64 * 1024), &loopback_ethtool_ops, &eth_header_ops,
104+
+ &loopback_ops, loopback_dev_free);
105+
}
106+
107+
/* Setup and register the loopback device. */
108+
@@ -229,3 +247,43 @@ static __net_init int loopback_net_init(struct net *net)
109+
struct pernet_operations __net_initdata loopback_net_ops = {
110+
.init = loopback_net_init,
111+
};
112+
+
113+
+/* blackhole netdevice */
114+
+static netdev_tx_t blackhole_netdev_xmit(struct sk_buff *skb,
115+
+ struct net_device *dev)
116+
+{
117+
+ kfree_skb(skb);
118+
+ net_warn_ratelimited("%s(): Dropping skb.\n", __func__);
119+
+ return NETDEV_TX_OK;
120+
+}
121+
+
122+
+static const struct net_device_ops blackhole_netdev_ops = {
123+
+ .ndo_start_xmit = blackhole_netdev_xmit,
124+
+};
125+
+
126+
+/* This is a dst-dummy device used specifically for invalidated
127+
+ * DSTs and unlike loopback, this is not per-ns.
128+
+ */
129+
+static void blackhole_netdev_setup(struct net_device *dev)
130+
+{
131+
+ gen_lo_setup(dev, ETH_MIN_MTU, NULL, NULL, &blackhole_netdev_ops, NULL);
132+
+}
133+
+
134+
+/* Setup and register the blackhole_netdev. */
135+
+static int __init blackhole_netdev_init(void)
136+
+{
137+
+ blackhole_netdev = alloc_netdev(0, "blackhole_dev", NET_NAME_UNKNOWN,
138+
+ blackhole_netdev_setup);
139+
+ if (!blackhole_netdev)
140+
+ return -ENOMEM;
141+
+
142+
+ dev_init_scheduler(blackhole_netdev);
143+
+ dev_activate(blackhole_netdev);
144+
+
145+
+ blackhole_netdev->flags |= IFF_UP | IFF_RUNNING;
146+
+ dev_net_set(blackhole_netdev, &init_net);
147+
+
148+
+ return 0;
149+
+}
150+
+
151+
+device_initcall(blackhole_netdev_init);
152+
* Unmerged path include/linux/netdevice.h

0 commit comments

Comments
 (0)