Fundamentals 12 min read

Deep Dive into Linux Localhost Network I/O: Kernel-Level Packet Transmission and Reception on 127.0.0.1

This article provides a comprehensive analysis of the Linux kernel's handling of localhost network I/O, detailing how packets destined for 127.0.0.1 bypass physical network interfaces, traverse the routing table, and utilize the loopback virtual device and soft interrupts for efficient intra-machine communication.

Refining Core Development Skills
Refining Core Development Skills
Refining Core Development Skills
Deep Dive into Linux Localhost Network I/O: Kernel-Level Packet Transmission and Reception on 127.0.0.1

This article explores the Linux kernel's internal mechanisms for handling localhost network I/O, specifically addressing how communication via 127.0.0.1 differs from cross-machine networking. Unlike external traffic, local packets bypass physical network interface cards and hardware interrupts, relying instead on the loopback virtual device and software interrupts.

During the sending process, the kernel routes packets through the local routing table, which immediately identifies the destination as local and assigns the loopback device. The network device subsystem skips complex queuing mechanisms and directly invokes the loopback driver's transmit function. The relevant kernel code demonstrates this routing lookup:

//file: net/ipv4/ip_output.c
int ip_queue_xmit(struct sk_buff *skb, struct flowi *fl)
{
rt = (struct rtable *)__sk_dst_check(sk, 0);
if (rt == NULL) {
rt = ip_route_output_ports(...);
sk_setup_caps(sk, &rt->dst);
}
}

The loopback transmit function strips the original socket association and enqueues the packet for reception, triggering a soft interrupt instead of a hardware interrupt. The code snippet below illustrates how the packet is queued and the soft interrupt is raised:

//file: net/core/dev.c
static int enqueue_to_backlog(struct sk_buff *skb, int cpu, unsigned int *qtail)
{
sd = &per_cpu(softnet_data, cpu);
__skb_queue_tail(&sd->input_pkt_queue, skb);
____napi_schedule(sd, &sd->backlog);
}

On the receiving side, the soft interrupt handler processes the queued packets. The default poll function for the loopback interface, process_backlog, moves packets from the input queue to the processing queue and passes them up the network protocol stack. This bypasses the physical NIC's ring buffer and hardware interrupt overhead, though the packet still traverses the full kernel protocol stack.

In summary, localhost network I/O does not require physical network hardware. While it saves overhead by avoiding hardware interrupts and physical queuing, it still incurs costs from system calls, protocol stack traversal, and soft interrupt handling. The article also notes that modern architectures like Istio leverage eBPF to further optimize sidecar-to-local-process communication by bypassing the kernel network stack entirely.

system programmingeBPFnetwork I/OKernel RoutingLoopback InterfaceOS internalsSoft Interrupts
Refining Core Development Skills
Written by

Refining Core Development Skills

Fei has over 10 years of development experience at Tencent and Sogou. Through this account, he shares his deep insights on performance.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.