xref: /openbmc/linux/drivers/net/loopback.c (revision 47d74275)
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 {
635175c378SEric Dumazet 	unsigned long packets;
645175c378SEric Dumazet 	unsigned long bytes;
657eebb0b2SEric Dumazet 	unsigned long drops;
665175c378SEric Dumazet };
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds  * The higher levels take care of making this non-reentrant (it's
701da177e4SLinus Torvalds  * called with bh's disabled).
711da177e4SLinus Torvalds  */
7261357325SStephen Hemminger static netdev_tx_t loopback_xmit(struct sk_buff *skb,
7361357325SStephen Hemminger 				 struct net_device *dev)
741da177e4SLinus Torvalds {
7547d74275STejun Heo 	struct pcpu_lstats __percpu *pcpu_lstats;
7647d74275STejun Heo 	struct pcpu_lstats *lb_stats;
777eebb0b2SEric Dumazet 	int len;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	skb_orphan(skb);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	skb->protocol = eth_type_trans(skb, dev);
821da177e4SLinus Torvalds 
839e0db4b1SEric W. Biederman 	/* it's OK to use per_cpu_ptr() because BHs are off */
8447d74275STejun Heo 	pcpu_lstats = (void __percpu __force *)dev->ml_priv;
85ca0c9584SChristoph Lameter 	lb_stats = this_cpu_ptr(pcpu_lstats);
861da177e4SLinus Torvalds 
877eebb0b2SEric Dumazet 	len = skb->len;
887eebb0b2SEric Dumazet 	if (likely(netif_rx(skb) == NET_RX_SUCCESS)) {
897eebb0b2SEric Dumazet 		lb_stats->bytes += len;
907eebb0b2SEric Dumazet 		lb_stats->packets++;
917eebb0b2SEric Dumazet 	} else
927eebb0b2SEric Dumazet 		lb_stats->drops++;
931da177e4SLinus Torvalds 
946ed10654SPatrick McHardy 	return NETDEV_TX_OK;
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
97c02373bfSStephen Hemminger static struct net_device_stats *loopback_get_stats(struct net_device *dev)
981da177e4SLinus Torvalds {
9947d74275STejun Heo 	const struct pcpu_lstats __percpu *pcpu_lstats;
10033036807SEric Dumazet 	struct net_device_stats *stats = &dev->stats;
1015175c378SEric Dumazet 	unsigned long bytes = 0;
1025175c378SEric Dumazet 	unsigned long packets = 0;
1037eebb0b2SEric Dumazet 	unsigned long drops = 0;
1041da177e4SLinus Torvalds 	int i;
1051da177e4SLinus Torvalds 
10647d74275STejun Heo 	pcpu_lstats = (void __percpu __force *)dev->ml_priv;
1070fed4846SKAMEZAWA Hiroyuki 	for_each_possible_cpu(i) {
1085175c378SEric Dumazet 		const struct pcpu_lstats *lb_stats;
1091da177e4SLinus Torvalds 
1105f6d88b9SEric W. Biederman 		lb_stats = per_cpu_ptr(pcpu_lstats, i);
1115175c378SEric Dumazet 		bytes   += lb_stats->bytes;
1125175c378SEric Dumazet 		packets += lb_stats->packets;
1137eebb0b2SEric Dumazet 		drops   += lb_stats->drops;
1141da177e4SLinus Torvalds 	}
1155175c378SEric Dumazet 	stats->rx_packets = packets;
1165175c378SEric Dumazet 	stats->tx_packets = packets;
1177eebb0b2SEric Dumazet 	stats->rx_dropped = drops;
1187eebb0b2SEric Dumazet 	stats->rx_errors  = drops;
1195175c378SEric Dumazet 	stats->rx_bytes   = bytes;
1205175c378SEric Dumazet 	stats->tx_bytes   = bytes;
1211da177e4SLinus Torvalds 	return stats;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1247fa6b066SStephen Hemminger static u32 always_on(struct net_device *dev)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	return 1;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1297282d491SJeff Garzik static const struct ethtool_ops loopback_ethtool_ops = {
1307fa6b066SStephen Hemminger 	.get_link		= always_on,
1311da177e4SLinus Torvalds 	.set_tso		= ethtool_op_set_tso,
1327fa6b066SStephen Hemminger 	.get_tx_csum		= always_on,
1337fa6b066SStephen Hemminger 	.get_sg			= always_on,
1347fa6b066SStephen Hemminger 	.get_rx_csum		= always_on,
1351da177e4SLinus Torvalds };
1361da177e4SLinus Torvalds 
1375f6d88b9SEric W. Biederman static int loopback_dev_init(struct net_device *dev)
1385f6d88b9SEric W. Biederman {
13947d74275STejun Heo 	struct pcpu_lstats __percpu *lstats;
1405f6d88b9SEric W. Biederman 
1415f6d88b9SEric W. Biederman 	lstats = alloc_percpu(struct pcpu_lstats);
1425f6d88b9SEric W. Biederman 	if (!lstats)
1435f6d88b9SEric W. Biederman 		return -ENOMEM;
1445f6d88b9SEric W. Biederman 
14547d74275STejun Heo 	dev->ml_priv = (void __force *)lstats;
1465f6d88b9SEric W. Biederman 	return 0;
1475f6d88b9SEric W. Biederman }
1485f6d88b9SEric W. Biederman 
1495f6d88b9SEric W. Biederman static void loopback_dev_free(struct net_device *dev)
1505f6d88b9SEric W. Biederman {
15147d74275STejun Heo 	struct pcpu_lstats __percpu *lstats =
15247d74275STejun Heo 		(void __percpu __force *)dev->ml_priv;
1535f6d88b9SEric W. Biederman 
1545f6d88b9SEric W. Biederman 	free_percpu(lstats);
1555f6d88b9SEric W. Biederman 	free_netdev(dev);
1565f6d88b9SEric W. Biederman }
1575f6d88b9SEric W. Biederman 
158c02373bfSStephen Hemminger static const struct net_device_ops loopback_ops = {
159c02373bfSStephen Hemminger 	.ndo_init      = loopback_dev_init,
16000829823SStephen Hemminger 	.ndo_start_xmit= loopback_xmit,
161c02373bfSStephen Hemminger 	.ndo_get_stats = loopback_get_stats,
162c02373bfSStephen Hemminger };
163c02373bfSStephen Hemminger 
1647fa6b066SStephen Hemminger /*
1659e0db4b1SEric W. Biederman  * The loopback device is special. There is only one instance
1669e0db4b1SEric W. Biederman  * per network namespace.
1677fa6b066SStephen Hemminger  */
168854d8363SDaniel Lezcano static void loopback_setup(struct net_device *dev)
169854d8363SDaniel Lezcano {
170854d8363SDaniel Lezcano 	dev->mtu		= (16 * 1024) + 20 + 20 + 12;
171854d8363SDaniel Lezcano 	dev->hard_header_len	= ETH_HLEN;	/* 14	*/
172854d8363SDaniel Lezcano 	dev->addr_len		= ETH_ALEN;	/* 6	*/
173854d8363SDaniel Lezcano 	dev->tx_queue_len	= 0;
174854d8363SDaniel Lezcano 	dev->type		= ARPHRD_LOOPBACK;	/* 0x0001*/
175854d8363SDaniel Lezcano 	dev->flags		= IFF_LOOPBACK;
17693f154b5SEric Dumazet 	dev->priv_flags	       &= ~IFF_XMIT_DST_RELEASE;
177854d8363SDaniel Lezcano 	dev->features 		= NETIF_F_SG | NETIF_F_FRAGLIST
178d2ae1d2fSChuck Ebbert 		| NETIF_F_TSO
179854d8363SDaniel Lezcano 		| NETIF_F_NO_CSUM
180854d8363SDaniel Lezcano 		| NETIF_F_HIGHDMA
181ce286d32SEric W. Biederman 		| NETIF_F_LLTX
1822d2c54e3SEmil Medve 		| NETIF_F_NETNS_LOCAL;
183854d8363SDaniel Lezcano 	dev->ethtool_ops	= &loopback_ethtool_ops;
1843b04dddeSStephen Hemminger 	dev->header_ops		= &eth_header_ops;
185c02373bfSStephen Hemminger 	dev->netdev_ops		= &loopback_ops;
1865f6d88b9SEric W. Biederman 	dev->destructor		= loopback_dev_free;
187854d8363SDaniel Lezcano }
188de3cb747SDaniel Lezcano 
18922783649SRalf Baechle /* Setup and register the loopback device. */
1904665079cSPavel Emelyanov static __net_init int loopback_net_init(struct net *net)
1911da177e4SLinus Torvalds {
192854d8363SDaniel Lezcano 	struct net_device *dev;
193854d8363SDaniel Lezcano 	int err;
194aeed9e82SHerbert Xu 
195854d8363SDaniel Lezcano 	err = -ENOMEM;
196854d8363SDaniel Lezcano 	dev = alloc_netdev(0, "lo", loopback_setup);
197854d8363SDaniel Lezcano 	if (!dev)
198854d8363SDaniel Lezcano 		goto out;
199854d8363SDaniel Lezcano 
200c346dca1SYOSHIFUJI Hideaki 	dev_net_set(dev, net);
201854d8363SDaniel Lezcano 	err = register_netdev(dev);
202854d8363SDaniel Lezcano 	if (err)
203854d8363SDaniel Lezcano 		goto out_free_netdev;
204854d8363SDaniel Lezcano 
2052774c7abSEric W. Biederman 	net->loopback_dev = dev;
2069d6dda32SPavel Emelyanov 	return 0;
207854d8363SDaniel Lezcano 
2081da177e4SLinus Torvalds 
209854d8363SDaniel Lezcano out_free_netdev:
210854d8363SDaniel Lezcano 	free_netdev(dev);
2119d6dda32SPavel Emelyanov out:
21209ad9bc7SOctavian Purdila 	if (net_eq(net, &init_net))
2139d6dda32SPavel Emelyanov 		panic("loopback: Failed to register netdevice: %d\n", err);
2149d6dda32SPavel Emelyanov 	return err;
215854d8363SDaniel Lezcano }
21660903f2cSAdrian Bunk 
217505d4f73SEric W. Biederman /* Registered in net/core/dev.c */
218505d4f73SEric W. Biederman struct pernet_operations __net_initdata loopback_net_ops = {
2192774c7abSEric W. Biederman        .init = loopback_net_init,
2202774c7abSEric W. Biederman };
221