Linux / OS Fundamentals Series

🧱 User Space, Kernel Space, and TUN/TAP Devices, Understood from a "Top 1%" Perspective

Introduction

This article is part of the Top 1% Series’ full article guide.

Prerequisites

Getting the Big Picture

In a Nutshell

Kernel space is the privileged region where the OS’s core processing — hardware control, memory management, process scheduling, and so on — runs, while user space is the region, with restricted direct access to hardware, where ordinary applications run. The whole point of strictly separating the two is to prevent a bug or crash in an ordinary application from directly damaging the OS as a whole or the hardware underneath it.

graph TB
    subgraph User["User space (Ring 3 / unprivileged)"]
        U1["Ordinary applications"]
        U2["Daemons (sshd, OpenVPN, etc.)"]
        U3["Libraries (libc, OpenSSL, etc.)"]
    end
    subgraph Kernel["Kernel space (Ring 0 / privileged)"]
        K1["Process scheduler"]
        K2["Memory management"]
        K3["Device drivers"]
        K4["Network stack"]
    end
    User -->|"System call<br/>(the only legitimate entry point)"| Kernel
    Kernel -->|"Directly controls hardware"| HW["Physical hardware (CPU / memory / NIC / disk)"]

An ordinary application isn’t allowed to directly control hardware such as a disk or a network card. If it needs anything done that touches hardware, it must ask the kernel to do it on its behalf — and the mechanism for making that request is the system call, described next.

The Fundamentals, Explained Thoroughly

Why the Separation Exists: The CPU’s Privilege Levels

This separation isn’t just a software convention — it’s enforced by a privilege-level mechanism built into the CPU hardware itself (on x86, this is the notion of “rings,” Ring 0 through Ring 3).

If code running in user mode attempts to execute a privileged instruction, the CPU raises a fault and forcibly interrupts it. In other words, “user-space programs can’t directly control hardware” isn’t merely an agreed-upon convention or good manners — it’s a constraint enforced at the CPU hardware level. The reason a buggy application can’t directly corrupt disk contents or peek into another process’s memory comes down to this privilege-level separation.

System Calls: The One Legitimate Route Across the Boundary

When a user-space program wants to use a kernel feature — reading or writing a file, network communication, allocating memory — it invokes a system call.

sequenceDiagram
    participant App as Application (user space)
    participant CPU as CPU
    participant Kernel as Kernel (kernel space)

    App->>CPU: Executes a system call instruction (e.g., write())
    Note over CPU: Switches privilege level from Ring 3 to Ring 0
    CPU->>Kernel: Jumps to the corresponding kernel-side handler
    Kernel->>Kernel: Performs the requested work (e.g., writing to disk)
    Kernel-->>CPU: Returns the result
    Note over CPU: Switches privilege level back from Ring 0 to Ring 3
    CPU-->>App: Receives the system call's return value

Writing to a file with write(), creating a network socket with socket(), spawning a new process with fork() — most of the standard library functions application developers use every day ultimately invoke a system call and go through this privilege-level switch to ask the kernel to do the work. This switching itself costs CPU cycles (the overhead of a context switch), so programs that call system calls frequently are more exposed to the cost of that switching.

Aside: What is a context switch?

A context switch is when the CPU saves the state of whatever it’s currently executing (register values, etc.) and loads the state of something else to switch execution to it. The switch between user space and kernel space is one kind of context switch; another occurs when the OS switches between processes to give each one a fair share of CPU time. Either way, saving and restoring registers is overhead, and doing it frequently affects overall CPU throughput.

The TUN/TAP Device: A “Virtual Network Card” Visible from User Space

Given the principles above, controlling a network interface (NIC) is fundamentally kernel-space work, and a user-space program shouldn’t be able to send or receive packets directly. Yet user-space VPN software like OpenVPN clearly does read and write packets freely. What makes this possible is the TUN/TAP device.

TUN/TAP is a virtual network interface mechanism provided by the Linux kernel that creates a two-way channel for exchanging packets between the kernel’s network stack and a user-space program.

graph LR
    subgraph Kernel["Kernel space"]
        Stack["Network stack<br/>(IP routing, etc.)"]
        TunDev["/dev/net/tun<br/>(virtual device)"]
    end
    subgraph User["User space"]
        App["VPN software<br/>(e.g., OpenVPN)"]
    end

    Stack <-->|"Looks like a physical NIC<br/>from the OS's perspective"| TunDev
    TunDev <-->|"Sends/receives packets directly<br/>via read()/write()"| App

Here’s how it works, step by step:

  1. The VPN software opens /dev/net/tun and creates a virtual network interface such as tun0.
  2. The OS’s routing table registers this tun0 as a network interface, exactly like a physical NIC.
  3. When the kernel’s routing decision determines that a packet an application sent is destined for tun0, the kernel hands that packet straight to the user-space VPN software (readable via read()).
  4. The VPN software encrypts the packet it received and sends it to the remote VPN server over the actual physical NIC.
  5. Once it decrypts an encrypted packet arriving from the remote side, the VPN software writes it back to tun0 via write(), handing it to the kernel’s network stack “as if it had arrived from tun0” directly.

A TUN (TUNnel) device works with raw IP packets (layer 3), while a TAP (network TAP) device works with Ethernet frames (layer 2, including MAC addresses). A TUN device is enough for most remote-access VPNs, which only need routing, while configurations that need bridging (joining the same network segment) use a TAP device instead.

This mechanism is a concrete example of the very topic of this article: bridging the boundary between a user-space program and the kernel-space network stack through a legitimate, kernel-provided interface. The fact that a VPN’s software must cross this read()/write() system-call boundary between user space and kernel space for every single packet is the direct source of the overhead that user-space VPN implementations carry, compared to implementations that complete entirely inside kernel space.

What the Top 1% Sees

Why Kernel-Space-Only Implementations Are Faster

Communication through TUN/TAP involves, at minimum, a round trip per packet: “copy from kernel to user space plus a context switch,” “processing in user space,” and “copy back from user space to kernel plus another context switch.” By contrast, an implementation that handles networking entirely within kernel space (IPsec’s XFRM framework, or WireGuard, built directly into the Linux kernel) never makes that round trip at all, so under equivalent conditions it tends to achieve higher throughput and lower latency. Whether an implementation lives in user space or kernel space isn’t merely a detail of where the code happens to run — it’s a design decision that determines the length of the actual path a packet has to travel. Keeping that framing in mind is useful whenever you’re evaluating the performance characteristics of a VPN protocol or a piece of networking equipment.

Why Bugs in Kernel Space Are So Much More Dangerous

If a user-space program crashes, the damage is contained to that one process — the OS reclaims its memory and keeps running. But when code running in kernel space (the kernel itself, or a device driver) crashes due to a bug, the failure happens in the very place where the privilege-level separation mechanism is supposed to be protecting things, and the entire OS goes down (a kernel panic). The fact that most device drivers run in kernel space is what gives them high performance, but it also means driver quality is directly tied to the stability of the entire system — a real trade-off.

Common Misconceptions and Pitfalls

A Troubleshooting Perspective

Issues at the user-space/kernel-space boundary are best triaged by first determining which side of the boundary the problem is actually on.

  1. tun0 isn’t created when establishing a VPN connection: A typical cause is a permissions issue on the /dev/net/tun device file, or in a container environment, a missing capability grant such as --cap-add=NET_ADMIN. Creating a TUN/TAP device requires an operation roughly equivalent to administrator-level privileges over the kernel.
  2. Traffic flows over the VPN, but throughput won’t scale: Check the CPU usage of the relevant process with top, in particular the system-time percentage (%sy). A high system-time share suggests system-call/context-switch overhead is the bottleneck.
  3. A kernel panic or full system freeze occurs: Check dmesg or a kernel crash dump to identify which kernel module (such as a device driver) was involved right before the crash. Application logs in user space alone can’t explain this — checking kernel logs is essential.

Preventive Measures and Long-Term Fixes

Summary

Things to Keep in Mind Starting Today

  1. When you hit a throughput problem in a user-space implementation, suspect system-call/context-switch overhead first, and check the CPU’s system-time percentage (%sy).
  2. When running TUN/TAP-based software in a container, identify the necessary permissions ahead of time.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).