xref: /openbmc/linux/drivers/net/loopback.c (revision 57a7744e)
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 
431da177e4SLinus Torvalds #include <asm/uaccess.h>
441da177e4SLinus Torvalds #include <asm/io.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <linux/inet.h>
471da177e4SLinus Torvalds #include <linux/netdevice.h>
481da177e4SLinus Torvalds #include <linux/etherdevice.h>
491da177e4SLinus Torvalds #include <linux/skbuff.h>
501da177e4SLinus Torvalds #include <linux/ethtool.h>
511da177e4SLinus Torvalds #include <net/sock.h>
521da177e4SLinus Torvalds #include <net/checksum.h>
531da177e4SLinus Torvalds #include <linux/if_ether.h>	/* For the statistics structure. */
541da177e4SLinus Torvalds #include <linux/if_arp.h>	/* For ARPHRD_ETHER */
551da177e4SLinus Torvalds #include <linux/ip.h>
561da177e4SLinus Torvalds #include <linux/tcp.h>
571da177e4SLinus Torvalds #include <linux/percpu.h>
582774c7abSEric W. Biederman #include <net/net_namespace.h>
595eaa0bd8SEric Dumazet #include <linux/u64_stats_sync.h>
601da177e4SLinus Torvalds 
615175c378SEric Dumazet struct pcpu_lstats {
626b10de38SEric Dumazet 	u64			packets;
636b10de38SEric Dumazet 	u64			bytes;
645eaa0bd8SEric Dumazet 	struct u64_stats_sync	syncp;
655175c378SEric Dumazet };
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * The higher levels take care of making this non-reentrant (it's
691da177e4SLinus Torvalds  * called with bh's disabled).
701da177e4SLinus Torvalds  */
7161357325SStephen Hemminger static netdev_tx_t loopback_xmit(struct sk_buff *skb,
7261357325SStephen Hemminger 				 struct net_device *dev)
731da177e4SLinus Torvalds {
7447d74275STejun Heo 	struct pcpu_lstats *lb_stats;
757eebb0b2SEric Dumazet 	int len;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	skb_orphan(skb);
781da177e4SLinus Torvalds 
79794ed393SEric Dumazet 	/* Before queueing this packet to netif_rx(),
80794ed393SEric Dumazet 	 * make sure dst is refcounted.
81794ed393SEric Dumazet 	 */
82794ed393SEric Dumazet 	skb_dst_force(skb);
83794ed393SEric Dumazet 
841da177e4SLinus Torvalds 	skb->protocol = eth_type_trans(skb, dev);
851da177e4SLinus Torvalds 
869e0db4b1SEric W. Biederman 	/* it's OK to use per_cpu_ptr() because BHs are off */
87a7855c78SEric Dumazet 	lb_stats = this_cpu_ptr(dev->lstats);
881da177e4SLinus Torvalds 
897eebb0b2SEric Dumazet 	len = skb->len;
907eebb0b2SEric Dumazet 	if (likely(netif_rx(skb) == NET_RX_SUCCESS)) {
915eaa0bd8SEric Dumazet 		u64_stats_update_begin(&lb_stats->syncp);
927eebb0b2SEric Dumazet 		lb_stats->bytes += len;
937eebb0b2SEric Dumazet 		lb_stats->packets++;
945eaa0bd8SEric Dumazet 		u64_stats_update_end(&lb_stats->syncp);
95caf586e5SEric Dumazet 	}
961da177e4SLinus Torvalds 
976ed10654SPatrick McHardy 	return NETDEV_TX_OK;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
10028172739SEric Dumazet static struct rtnl_link_stats64 *loopback_get_stats64(struct net_device *dev,
10128172739SEric Dumazet 						      struct rtnl_link_stats64 *stats)
1021da177e4SLinus Torvalds {
1036b10de38SEric Dumazet 	u64 bytes = 0;
1046b10de38SEric Dumazet 	u64 packets = 0;
1051da177e4SLinus Torvalds 	int i;
1061da177e4SLinus Torvalds 
1070fed4846SKAMEZAWA Hiroyuki 	for_each_possible_cpu(i) {
1085175c378SEric Dumazet 		const struct pcpu_lstats *lb_stats;
1095eaa0bd8SEric Dumazet 		u64 tbytes, tpackets;
1105eaa0bd8SEric Dumazet 		unsigned int start;
1111da177e4SLinus Torvalds 
112a7855c78SEric Dumazet 		lb_stats = per_cpu_ptr(dev->lstats, i);
1135eaa0bd8SEric Dumazet 		do {
11457a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&lb_stats->syncp);
1155eaa0bd8SEric Dumazet 			tbytes = lb_stats->bytes;
1165eaa0bd8SEric Dumazet 			tpackets = lb_stats->packets;
11757a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&lb_stats->syncp, start));
1185eaa0bd8SEric Dumazet 		bytes   += tbytes;
1195eaa0bd8SEric Dumazet 		packets += tpackets;
1201da177e4SLinus Torvalds 	}
1215175c378SEric Dumazet 	stats->rx_packets = packets;
1225175c378SEric Dumazet 	stats->tx_packets = packets;
1235175c378SEric Dumazet 	stats->rx_bytes   = bytes;
1245175c378SEric Dumazet 	stats->tx_bytes   = bytes;
1251da177e4SLinus Torvalds 	return stats;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1287fa6b066SStephen Hemminger static u32 always_on(struct net_device *dev)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	return 1;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1337282d491SJeff Garzik static const struct ethtool_ops loopback_ethtool_ops = {
1347fa6b066SStephen Hemminger 	.get_link		= always_on,
1351da177e4SLinus Torvalds };
1361da177e4SLinus Torvalds 
1375f6d88b9SEric W. Biederman static int loopback_dev_init(struct net_device *dev)
1385f6d88b9SEric W. Biederman {
1391c213bd2SWANG Cong 	dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
140a7855c78SEric Dumazet 	if (!dev->lstats)
1415f6d88b9SEric W. Biederman 		return -ENOMEM;
1425f6d88b9SEric W. Biederman 	return 0;
1435f6d88b9SEric W. Biederman }
1445f6d88b9SEric W. Biederman 
1455f6d88b9SEric W. Biederman static void loopback_dev_free(struct net_device *dev)
1465f6d88b9SEric W. Biederman {
147e05e9070SEric W. Biederman 	dev_net(dev)->loopback_dev = NULL;
148a7855c78SEric Dumazet 	free_percpu(dev->lstats);
1495f6d88b9SEric W. Biederman 	free_netdev(dev);
1505f6d88b9SEric W. Biederman }
1515f6d88b9SEric W. Biederman 
152c02373bfSStephen Hemminger static const struct net_device_ops loopback_ops = {
153c02373bfSStephen Hemminger 	.ndo_init      = loopback_dev_init,
15400829823SStephen Hemminger 	.ndo_start_xmit= loopback_xmit,
1556b10de38SEric Dumazet 	.ndo_get_stats64 = loopback_get_stats64,
15625f929fbSWANG Cong 	.ndo_set_mac_address = eth_mac_addr,
157c02373bfSStephen Hemminger };
158c02373bfSStephen Hemminger 
1597fa6b066SStephen Hemminger /*
1609e0db4b1SEric W. Biederman  * The loopback device is special. There is only one instance
1619e0db4b1SEric W. Biederman  * per network namespace.
1627fa6b066SStephen Hemminger  */
163854d8363SDaniel Lezcano static void loopback_setup(struct net_device *dev)
164854d8363SDaniel Lezcano {
1650cf833aeSEric Dumazet 	dev->mtu		= 64 * 1024;
166854d8363SDaniel Lezcano 	dev->hard_header_len	= ETH_HLEN;	/* 14	*/
167854d8363SDaniel Lezcano 	dev->addr_len		= ETH_ALEN;	/* 6	*/
168854d8363SDaniel Lezcano 	dev->tx_queue_len	= 0;
169854d8363SDaniel Lezcano 	dev->type		= ARPHRD_LOOPBACK;	/* 0x0001*/
170854d8363SDaniel Lezcano 	dev->flags		= IFF_LOOPBACK;
17125f929fbSWANG Cong 	dev->priv_flags		|= IFF_LIVE_ADDR_CHANGE;
17293f154b5SEric Dumazet 	dev->priv_flags	       &= ~IFF_XMIT_DST_RELEASE;
173cf0bdefdSMichał Mirosław 	dev->hw_features	= NETIF_F_ALL_TSO | NETIF_F_UFO;
174854d8363SDaniel Lezcano 	dev->features 		= NETIF_F_SG | NETIF_F_FRAGLIST
175cf0bdefdSMichał Mirosław 		| NETIF_F_ALL_TSO
176cf0bdefdSMichał Mirosław 		| NETIF_F_UFO
17734324dc2SMichał Mirosław 		| NETIF_F_HW_CSUM
178cf0bdefdSMichał Mirosław 		| NETIF_F_RXCSUM
179b17c7069SDaniel Borkmann 		| NETIF_F_SCTP_CSUM
180854d8363SDaniel Lezcano 		| NETIF_F_HIGHDMA
181ce286d32SEric W. Biederman 		| NETIF_F_LLTX
1820553c891SKrishna Kumar 		| NETIF_F_NETNS_LOCAL
183eed2a12fSMahesh Bandewar 		| NETIF_F_VLAN_CHALLENGED
184eed2a12fSMahesh Bandewar 		| NETIF_F_LOOPBACK;
185854d8363SDaniel Lezcano 	dev->ethtool_ops	= &loopback_ethtool_ops;
1863b04dddeSStephen Hemminger 	dev->header_ops		= &eth_header_ops;
187c02373bfSStephen Hemminger 	dev->netdev_ops		= &loopback_ops;
1885f6d88b9SEric W. Biederman 	dev->destructor		= loopback_dev_free;
189854d8363SDaniel Lezcano }
190de3cb747SDaniel Lezcano 
19122783649SRalf Baechle /* Setup and register the loopback device. */
1924665079cSPavel Emelyanov static __net_init int loopback_net_init(struct net *net)
1931da177e4SLinus Torvalds {
194854d8363SDaniel Lezcano 	struct net_device *dev;
195854d8363SDaniel Lezcano 	int err;
196aeed9e82SHerbert Xu 
197854d8363SDaniel Lezcano 	err = -ENOMEM;
198854d8363SDaniel Lezcano 	dev = alloc_netdev(0, "lo", loopback_setup);
199854d8363SDaniel Lezcano 	if (!dev)
200854d8363SDaniel Lezcano 		goto out;
201854d8363SDaniel Lezcano 
202c346dca1SYOSHIFUJI Hideaki 	dev_net_set(dev, net);
203854d8363SDaniel Lezcano 	err = register_netdev(dev);
204854d8363SDaniel Lezcano 	if (err)
205854d8363SDaniel Lezcano 		goto out_free_netdev;
206854d8363SDaniel Lezcano 
2071fb9489bSPavel Emelyanov 	BUG_ON(dev->ifindex != LOOPBACK_IFINDEX);
2082774c7abSEric W. Biederman 	net->loopback_dev = dev;
2099d6dda32SPavel Emelyanov 	return 0;
210854d8363SDaniel Lezcano 
2111da177e4SLinus Torvalds 
212854d8363SDaniel Lezcano out_free_netdev:
213854d8363SDaniel Lezcano 	free_netdev(dev);
2149d6dda32SPavel Emelyanov out:
21509ad9bc7SOctavian Purdila 	if (net_eq(net, &init_net))
2169d6dda32SPavel Emelyanov 		panic("loopback: Failed to register netdevice: %d\n", err);
2179d6dda32SPavel Emelyanov 	return err;
218854d8363SDaniel Lezcano }
21960903f2cSAdrian Bunk 
220505d4f73SEric W. Biederman /* Registered in net/core/dev.c */
221505d4f73SEric W. Biederman struct pernet_operations __net_initdata loopback_net_ops = {
2222774c7abSEric W. Biederman        .init = loopback_net_init,
2232774c7abSEric W. Biederman };
224