drivers/net/loopback.c
Source file repositories/reference/linux-study-clean/drivers/net/loopback.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/net/loopback.c- Extension
.c- Size
- 7572 bytes
- Lines
- 293
- Domain
- Driver Families
- Bucket
- drivers/net
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel.hlinux/jiffies.hlinux/module.hlinux/interrupt.hlinux/fs.hlinux/types.hlinux/string.hlinux/socket.hlinux/errno.hlinux/fcntl.hlinux/in.hlinux/uaccess.hlinux/io.hlinux/inet.hlinux/netdevice.hlinux/etherdevice.hlinux/skbuff.hlinux/ethtool.hnet/sch_generic.hnet/sock.hnet/checksum.hlinux/if_ether.hlinux/if_arp.hlinux/ip.hlinux/tcp.hlinux/percpu.hlinux/net_tstamp.hnet/net_namespace.hnet/netdev_lock.hlinux/u64_stats_sync.h
Detected Declarations
function loopback_xmitfunction dev_lstats_readfunction for_each_possible_cpufunction loopback_get_stats64function always_onfunction loopback_dev_initfunction loopback_dev_freefunction gen_lo_setupfunction loopback_setupfunction loopback_net_initfunction blackhole_netdev_xmitfunction blackhole_neigh_outputfunction blackhole_neigh_constructfunction blackhole_netdev_setupfunction blackhole_netdev_initmodule init blackhole_netdev_initexport blackhole_netdevexport dev_lstats_read
Annotated Snippet
static const struct net_device_ops loopback_ops = {
.ndo_init = loopback_dev_init,
.ndo_start_xmit = loopback_xmit,
.ndo_get_stats64 = loopback_get_stats64,
.ndo_set_mac_address = eth_mac_addr,
};
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 = mtu;
dev->hard_header_len = ETH_HLEN; /* 14 */
dev->min_header_len = ETH_HLEN; /* 14 */
dev->addr_len = ETH_ALEN; /* 6 */
dev->type = ARPHRD_LOOPBACK; /* 0x0001*/
dev->flags = IFF_LOOPBACK;
dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
dev->lltx = true;
dev->netns_immutable = true;
netif_keep_dst(dev);
dev->hw_features = NETIF_F_GSO_SOFTWARE;
dev->features = NETIF_F_SG | NETIF_F_FRAGLIST
| NETIF_F_GSO_SOFTWARE
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
| NETIF_F_SCTP_CRC
| NETIF_F_HIGHDMA
| NETIF_F_VLAN_CHALLENGED
| NETIF_F_LOOPBACK;
dev->ethtool_ops = eth_ops;
dev->header_ops = hdr_ops;
dev->netdev_ops = dev_ops;
dev->needs_free_netdev = true;
dev->pcpu_stat_type = NETDEV_PCPU_STAT_LSTATS;
dev->priv_destructor = dev_destructor;
netif_set_tso_max_size(dev, GSO_MAX_SIZE);
}
/* 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. */
static __net_init int loopback_net_init(struct net *net)
{
struct net_device *dev;
int err;
err = -ENOMEM;
dev = alloc_netdev(0, "lo", NET_NAME_PREDICTABLE, loopback_setup);
if (!dev)
goto out;
dev_net_set(dev, net);
err = register_netdev(dev);
if (err)
goto out_free_netdev;
BUG_ON(dev->ifindex != LOOPBACK_IFINDEX);
net->loopback_dev = dev;
return 0;
out_free_netdev:
free_netdev(dev);
out:
if (net_eq(net, &init_net))
panic("loopback: Failed to register netdevice: %d\n", err);
return err;
}
/* Registered in net/core/dev.c */
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);
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/jiffies.h`, `linux/module.h`, `linux/interrupt.h`, `linux/fs.h`, `linux/types.h`, `linux/string.h`, `linux/socket.h`.
- Detected declarations: `function loopback_xmit`, `function dev_lstats_read`, `function for_each_possible_cpu`, `function loopback_get_stats64`, `function always_on`, `function loopback_dev_init`, `function loopback_dev_free`, `function gen_lo_setup`, `function loopback_setup`, `function loopback_net_init`.
- Atlas domain: Driver Families / drivers/net.
- Implementation status: pattern implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.