xref: /openbmc/linux/drivers/net/loopback.c (revision 6b10de38f0ef4e921a1f6e5cba2b6c92d6b46ecd)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Pseudo-driver for the loopback interface.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Version:	@(#)loopback.c	1.0.4b	08/16/93
91da177e4SLinus Torvalds  *
1002c30a84SJesper Juhl  * Authors:	Ross Biro
111da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
121da177e4SLinus Torvalds  *		Donald Becker, <becker@scyld.com>
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *		Alan Cox	:	Fixed oddments for NET3.014
151da177e4SLinus Torvalds  *		Alan Cox	:	Rejig for NET3.029 snap #3
161da177e4SLinus Torvalds  *		Alan Cox	: 	Fixed NET3.029 bugs and sped up
171da177e4SLinus Torvalds  *		Larry McVoy	:	Tiny tweak to double performance
181da177e4SLinus Torvalds  *		Alan Cox	:	Backed out LMV's tweak - the linux mm
191da177e4SLinus Torvalds  *					can't take it...
201da177e4SLinus Torvalds  *              Michael Griffith:       Don't bother computing the checksums
211da177e4SLinus Torvalds  *                                      on packets received on the loopback
221da177e4SLinus Torvalds  *                                      interface.
231da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Potential hang under some extreme
241da177e4SLinus Torvalds  *					cases removed.
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
271da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
281da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
291da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds #include <linux/kernel.h>
321da177e4SLinus Torvalds #include <linux/jiffies.h>
331da177e4SLinus Torvalds #include <linux/module.h>
341da177e4SLinus Torvalds #include <linux/interrupt.h>
351da177e4SLinus Torvalds #include <linux/fs.h>
361da177e4SLinus Torvalds #include <linux/types.h>
371da177e4SLinus Torvalds #include <linux/string.h>
381da177e4SLinus Torvalds #include <linux/socket.h>
391da177e4SLinus Torvalds #include <linux/errno.h>
401da177e4SLinus Torvalds #include <linux/fcntl.h>
411da177e4SLinus Torvalds #include <linux/in.h>
421da177e4SLinus Torvalds #include <linux/init.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <asm/system.h>
451da177e4SLinus Torvalds #include <asm/uaccess.h>
461da177e4SLinus Torvalds #include <asm/io.h>
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #include <linux/inet.h>
491da177e4SLinus Torvalds #include <linux/netdevice.h>
501da177e4SLinus Torvalds #include <linux/etherdevice.h>
511da177e4SLinus Torvalds #include <linux/skbuff.h>
521da177e4SLinus Torvalds #include <linux/ethtool.h>
531da177e4SLinus Torvalds #include <net/sock.h>
541da177e4SLinus Torvalds #include <net/checksum.h>
551da177e4SLinus Torvalds #include <linux/if_ether.h>	/* For the statistics structure. */
561da177e4SLinus Torvalds #include <linux/if_arp.h>	/* For ARPHRD_ETHER */
571da177e4SLinus Torvalds #include <linux/ip.h>
581da177e4SLinus Torvalds #include <linux/tcp.h>
591da177e4SLinus Torvalds #include <linux/percpu.h>
602774c7abSEric W. Biederman #include <net/net_namespace.h>
611da177e4SLinus Torvalds 
625175c378SEric Dumazet struct pcpu_lstats {
63*6b10de38SEric Dumazet 	u64 packets;
64*6b10de38SEric Dumazet 	u64 bytes;
65*6b10de38SEric Dumazet #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
66*6b10de38SEric Dumazet 	seqcount_t seq;
67*6b10de38SEric Dumazet #endif
687eebb0b2SEric Dumazet 	unsigned long drops;
695175c378SEric Dumazet };
701da177e4SLinus Torvalds 
71*6b10de38SEric Dumazet #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
72*6b10de38SEric Dumazet static void inline lstats_update_begin(struct pcpu_lstats *lstats)
73*6b10de38SEric Dumazet {
74*6b10de38SEric Dumazet 	write_seqcount_begin(&lstats->seq);
75*6b10de38SEric Dumazet }
76*6b10de38SEric Dumazet static void inline lstats_update_end(struct pcpu_lstats *lstats)
77*6b10de38SEric Dumazet {
78*6b10de38SEric Dumazet 	write_seqcount_end(&lstats->seq);
79*6b10de38SEric Dumazet }
80*6b10de38SEric Dumazet static void inline lstats_fetch_and_add(u64 *packets, u64 *bytes, const struct pcpu_lstats *lstats)
81*6b10de38SEric Dumazet {
82*6b10de38SEric Dumazet 	u64 tpackets, tbytes;
83*6b10de38SEric Dumazet 	unsigned int seq;
84*6b10de38SEric Dumazet 
85*6b10de38SEric Dumazet 	do {
86*6b10de38SEric Dumazet 		seq = read_seqcount_begin(&lstats->seq);
87*6b10de38SEric Dumazet 		tpackets = lstats->packets;
88*6b10de38SEric Dumazet 		tbytes = lstats->bytes;
89*6b10de38SEric Dumazet 	} while (read_seqcount_retry(&lstats->seq, seq));
90*6b10de38SEric Dumazet 
91*6b10de38SEric Dumazet 	*packets += tpackets;
92*6b10de38SEric Dumazet 	*bytes += tbytes;
93*6b10de38SEric Dumazet }
94*6b10de38SEric Dumazet #else
95*6b10de38SEric Dumazet static void inline lstats_update_begin(struct pcpu_lstats *lstats)
96*6b10de38SEric Dumazet {
97*6b10de38SEric Dumazet }
98*6b10de38SEric Dumazet static void inline lstats_update_end(struct pcpu_lstats *lstats)
99*6b10de38SEric Dumazet {
100*6b10de38SEric Dumazet }
101*6b10de38SEric Dumazet static void inline lstats_fetch_and_add(u64 *packets, u64 *bytes, const struct pcpu_lstats *lstats)
102*6b10de38SEric Dumazet {
103*6b10de38SEric Dumazet 	*packets += lstats->packets;
104*6b10de38SEric Dumazet 	*bytes += lstats->bytes;
105*6b10de38SEric Dumazet }
106*6b10de38SEric Dumazet #endif
107*6b10de38SEric Dumazet 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * The higher levels take care of making this non-reentrant (it's
1101da177e4SLinus Torvalds  * called with bh's disabled).
1111da177e4SLinus Torvalds  */
11261357325SStephen Hemminger static netdev_tx_t loopback_xmit(struct sk_buff *skb,
11361357325SStephen Hemminger 				 struct net_device *dev)
1141da177e4SLinus Torvalds {
11547d74275STejun Heo 	struct pcpu_lstats __percpu *pcpu_lstats;
11647d74275STejun Heo 	struct pcpu_lstats *lb_stats;
1177eebb0b2SEric Dumazet 	int len;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	skb_orphan(skb);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	skb->protocol = eth_type_trans(skb, dev);
1221da177e4SLinus Torvalds 
1239e0db4b1SEric W. Biederman 	/* it's OK to use per_cpu_ptr() because BHs are off */
12447d74275STejun Heo 	pcpu_lstats = (void __percpu __force *)dev->ml_priv;
125ca0c9584SChristoph Lameter 	lb_stats = this_cpu_ptr(pcpu_lstats);
1261da177e4SLinus Torvalds 
1277eebb0b2SEric Dumazet 	len = skb->len;
1287eebb0b2SEric Dumazet 	if (likely(netif_rx(skb) == NET_RX_SUCCESS)) {
129*6b10de38SEric Dumazet 		lstats_update_begin(lb_stats);
1307eebb0b2SEric Dumazet 		lb_stats->bytes += len;
1317eebb0b2SEric Dumazet 		lb_stats->packets++;
132*6b10de38SEric Dumazet 		lstats_update_end(lb_stats);
1337eebb0b2SEric Dumazet 	} else
1347eebb0b2SEric Dumazet 		lb_stats->drops++;
1351da177e4SLinus Torvalds 
1366ed10654SPatrick McHardy 	return NETDEV_TX_OK;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
139*6b10de38SEric Dumazet static struct rtnl_link_stats64 *loopback_get_stats64(struct net_device *dev)
1401da177e4SLinus Torvalds {
14147d74275STejun Heo 	const struct pcpu_lstats __percpu *pcpu_lstats;
142*6b10de38SEric Dumazet 	struct rtnl_link_stats64 *stats = &dev->stats64;
143*6b10de38SEric Dumazet 	u64 bytes = 0;
144*6b10de38SEric Dumazet 	u64 packets = 0;
145*6b10de38SEric Dumazet 	u64 drops = 0;
1461da177e4SLinus Torvalds 	int i;
1471da177e4SLinus Torvalds 
14847d74275STejun Heo 	pcpu_lstats = (void __percpu __force *)dev->ml_priv;
1490fed4846SKAMEZAWA Hiroyuki 	for_each_possible_cpu(i) {
1505175c378SEric Dumazet 		const struct pcpu_lstats *lb_stats;
1511da177e4SLinus Torvalds 
1525f6d88b9SEric W. Biederman 		lb_stats = per_cpu_ptr(pcpu_lstats, i);
153*6b10de38SEric Dumazet 		lstats_fetch_and_add(&packets, &bytes, lb_stats);
1547eebb0b2SEric Dumazet 		drops   += lb_stats->drops;
1551da177e4SLinus Torvalds 	}
1565175c378SEric Dumazet 	stats->rx_packets = packets;
1575175c378SEric Dumazet 	stats->tx_packets = packets;
1587eebb0b2SEric Dumazet 	stats->rx_dropped = drops;
1597eebb0b2SEric Dumazet 	stats->rx_errors  = drops;
1605175c378SEric Dumazet 	stats->rx_bytes   = bytes;
1615175c378SEric Dumazet 	stats->tx_bytes   = bytes;
1621da177e4SLinus Torvalds 	return stats;
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1657fa6b066SStephen Hemminger static u32 always_on(struct net_device *dev)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	return 1;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
1707282d491SJeff Garzik static const struct ethtool_ops loopback_ethtool_ops = {
1717fa6b066SStephen Hemminger 	.get_link		= always_on,
1721da177e4SLinus Torvalds 	.set_tso		= ethtool_op_set_tso,
1737fa6b066SStephen Hemminger 	.get_tx_csum		= always_on,
1747fa6b066SStephen Hemminger 	.get_sg			= always_on,
1757fa6b066SStephen Hemminger 	.get_rx_csum		= always_on,
1761da177e4SLinus Torvalds };
1771da177e4SLinus Torvalds 
1785f6d88b9SEric W. Biederman static int loopback_dev_init(struct net_device *dev)
1795f6d88b9SEric W. Biederman {
18047d74275STejun Heo 	struct pcpu_lstats __percpu *lstats;
1815f6d88b9SEric W. Biederman 
1825f6d88b9SEric W. Biederman 	lstats = alloc_percpu(struct pcpu_lstats);
1835f6d88b9SEric W. Biederman 	if (!lstats)
1845f6d88b9SEric W. Biederman 		return -ENOMEM;
1855f6d88b9SEric W. Biederman 
18647d74275STejun Heo 	dev->ml_priv = (void __force *)lstats;
1875f6d88b9SEric W. Biederman 	return 0;
1885f6d88b9SEric W. Biederman }
1895f6d88b9SEric W. Biederman 
1905f6d88b9SEric W. Biederman static void loopback_dev_free(struct net_device *dev)
1915f6d88b9SEric W. Biederman {
19247d74275STejun Heo 	struct pcpu_lstats __percpu *lstats =
19347d74275STejun Heo 		(void __percpu __force *)dev->ml_priv;
1945f6d88b9SEric W. Biederman 
1955f6d88b9SEric W. Biederman 	free_percpu(lstats);
1965f6d88b9SEric W. Biederman 	free_netdev(dev);
1975f6d88b9SEric W. Biederman }
1985f6d88b9SEric W. Biederman 
199c02373bfSStephen Hemminger static const struct net_device_ops loopback_ops = {
200c02373bfSStephen Hemminger 	.ndo_init      = loopback_dev_init,
20100829823SStephen Hemminger 	.ndo_start_xmit= loopback_xmit,
202*6b10de38SEric Dumazet 	.ndo_get_stats64 = loopback_get_stats64,
203c02373bfSStephen Hemminger };
204c02373bfSStephen Hemminger 
2057fa6b066SStephen Hemminger /*
2069e0db4b1SEric W. Biederman  * The loopback device is special. There is only one instance
2079e0db4b1SEric W. Biederman  * per network namespace.
2087fa6b066SStephen Hemminger  */
209854d8363SDaniel Lezcano static void loopback_setup(struct net_device *dev)
210854d8363SDaniel Lezcano {
211854d8363SDaniel Lezcano 	dev->mtu		= (16 * 1024) + 20 + 20 + 12;
212854d8363SDaniel Lezcano 	dev->hard_header_len	= ETH_HLEN;	/* 14	*/
213854d8363SDaniel Lezcano 	dev->addr_len		= ETH_ALEN;	/* 6	*/
214854d8363SDaniel Lezcano 	dev->tx_queue_len	= 0;
215854d8363SDaniel Lezcano 	dev->type		= ARPHRD_LOOPBACK;	/* 0x0001*/
216854d8363SDaniel Lezcano 	dev->flags		= IFF_LOOPBACK;
21793f154b5SEric Dumazet 	dev->priv_flags	       &= ~IFF_XMIT_DST_RELEASE;
218854d8363SDaniel Lezcano 	dev->features 		= NETIF_F_SG | NETIF_F_FRAGLIST
219d2ae1d2fSChuck Ebbert 		| NETIF_F_TSO
220854d8363SDaniel Lezcano 		| NETIF_F_NO_CSUM
221854d8363SDaniel Lezcano 		| NETIF_F_HIGHDMA
222ce286d32SEric W. Biederman 		| NETIF_F_LLTX
2232d2c54e3SEmil Medve 		| NETIF_F_NETNS_LOCAL;
224854d8363SDaniel Lezcano 	dev->ethtool_ops	= &loopback_ethtool_ops;
2253b04dddeSStephen Hemminger 	dev->header_ops		= &eth_header_ops;
226c02373bfSStephen Hemminger 	dev->netdev_ops		= &loopback_ops;
2275f6d88b9SEric W. Biederman 	dev->destructor		= loopback_dev_free;
228854d8363SDaniel Lezcano }
229de3cb747SDaniel Lezcano 
23022783649SRalf Baechle /* Setup and register the loopback device. */
2314665079cSPavel Emelyanov static __net_init int loopback_net_init(struct net *net)
2321da177e4SLinus Torvalds {
233854d8363SDaniel Lezcano 	struct net_device *dev;
234854d8363SDaniel Lezcano 	int err;
235aeed9e82SHerbert Xu 
236854d8363SDaniel Lezcano 	err = -ENOMEM;
237854d8363SDaniel Lezcano 	dev = alloc_netdev(0, "lo", loopback_setup);
238854d8363SDaniel Lezcano 	if (!dev)
239854d8363SDaniel Lezcano 		goto out;
240854d8363SDaniel Lezcano 
241c346dca1SYOSHIFUJI Hideaki 	dev_net_set(dev, net);
242854d8363SDaniel Lezcano 	err = register_netdev(dev);
243854d8363SDaniel Lezcano 	if (err)
244854d8363SDaniel Lezcano 		goto out_free_netdev;
245854d8363SDaniel Lezcano 
2462774c7abSEric W. Biederman 	net->loopback_dev = dev;
2479d6dda32SPavel Emelyanov 	return 0;
248854d8363SDaniel Lezcano 
2491da177e4SLinus Torvalds 
250854d8363SDaniel Lezcano out_free_netdev:
251854d8363SDaniel Lezcano 	free_netdev(dev);
2529d6dda32SPavel Emelyanov out:
25309ad9bc7SOctavian Purdila 	if (net_eq(net, &init_net))
2549d6dda32SPavel Emelyanov 		panic("loopback: Failed to register netdevice: %d\n", err);
2559d6dda32SPavel Emelyanov 	return err;
256854d8363SDaniel Lezcano }
25760903f2cSAdrian Bunk 
258505d4f73SEric W. Biederman /* Registered in net/core/dev.c */
259505d4f73SEric W. Biederman struct pernet_operations __net_initdata loopback_net_ops = {
2602774c7abSEric W. Biederman        .init = loopback_net_init,
2612774c7abSEric W. Biederman };
262