一、引言:跨网段通信的烦恼
假设你有两台服务器,一台在北京,一台在上海,它们各自都有一个公网 IP,但内部分别在不同的私有网段中。北京的服务器在 192.168.10.0/24 网段,上海的服务器在 192.168.20.0/24 网段。现在有个需求:这两个私有网段要能直接通信,就像在同一局域网一样。
你可能会想到很多方案:申请专线成本太高;使用 VPN 配置复杂;修改公网路由不现实。有没有一种轻量级、简单易用的方案?
答案就是 IPIP 隧道。
┌─────────────────────────────────────────────────────────────────────────┐│ 跨网段通信需求 ││ ││ ┌─────────────────────┐ ┌─────────────────────┐ ││ │ 北京机房 │ │ 上海机房 │ ││ │ │ │ │ ││ │ 192.168.10.0/24 │ │ 192.168.20.0/24 │ ││ │ ┌──────────┐│ │ ┌──────────┐│ ││ │ │ Server A ││ │ │ Server B ││ ││ │ │ .10.10 ││ │ │ .20.20 ││ ││ │ └──────────┘│ │ └──────────┘│ ││ │ │ │ │ │ │ ││ └───────────┼─────────┘ └───────────┼─────────┘ ││ │ │ ││ 公网 IP: 203.0.113.10 公网 IP: 203.0.113.20 ││ │ │ ││ └──────────────── ✖ ──────────────────────┘ ││ 私有网段无法直接通信 │└───────────────────────────────────────────────────────────────────────────┘
图示说明:北京和上海机房的私有网段需要互通,但公网无法路由私有 IP
1.1 什么是隧道技术?
在解释 IPIP 之前,我们先理解隧道技术的本质。
想象一条河流分隔了两个城市,河的一边是城市 A,另一边是城市 B。汽车无法直接开过河(就像私有 IP 无法在公网上路由)。但是有一艘渡轮(隧道),可以把汽车从一边运到另一边。汽车开上渡轮(数据被封装),渡轮载着汽车过河(数据在公网上传输),到达对岸后汽车下船(数据被解封装)。
这就是隧道技术的核心思想:将一种协议的数据包封装在另一种协议中进行传输。
1.2 什么是 IPIP 隧道?
IPIP(IP in IP) 是一种简单的隧道协议,它将一个 IP 数据包完整地封装在另一个 IP 数据包中进行传输。IPIP 协议在 IP 头中的协议号是 4。
┌─────────────────────────────────────────────────────────────────┐│ IPIP 隧道原理 ││ ││ 发送端 接收端 ││ │ │ ││ │ 原始数据包 │ ││ │ ┌──────────────────────────┐ │ ││ │ │ 源IP: 192.168.10.10 │ │ ││ │ │ 目标IP: 192.168.20.20 │ │ ││ │ └──────────────────────────┘ │ ││ │ │ │ ││ │ ▼ │ ││ │ ┌──────────────────────────┐ │ ││ │ │ 原始 IP 头 (20字节) │ │ ││ │ │ 数据 │ │ ││ │ └──────────────────────────┘ │ ││ │ │ │ ││ │ ▼ │ ││ │ ┌─────────────────────────────────┐ │ ││ │ │ 外层 IP 头 (源: 203.0.113.10) │ │ ││ │ │ │ 原始 IP 头 (20字节) │ │ ││ │ │ │ 数据 │ │ ││ │ │ └──────────────────────────┘ │ ││ │ └─────────────────────────────────┘ │ ││ │ │ │ ││ │ ▼ │ ││ │ 通过公网传输 │ ││ │ ┌─────────────────────────────────┐ │ ││ │ │ 外层 IP 头 (目标: 203.0.113.20) │───►│ ││ │ │ │ 原始 IP 头 │ │ ││ │ │ │ 数据 │ │ ││ │ │ └──────────────────────────┘ │ ││ │ └─────────────────────────────────┘ │ ││ │ │ │ ││ │ ▼ │ ││ │ 解封装:剥除外层头 │ ││ │ ┌──────────────────────────┐ │ ││ │ │ 源IP: 192.168.10.10 │ │ ││ │ │ 目标IP: 192.168.20.20 │ │ ││ │ └──────────────────────────┘ │ ││ │ │ │└─────────────────────────────────────────────────────────────────┘
图示说明:IPIP 隧道将原始数据包封装在新的 IP 头中,外层 IP 包含公网地址,内层 IP 包含私有地址
二、IPIP 隧道核心原理
理解了基本概念后,我们深入了解 IPIP 的工作原理。
2.1 数据包封装过程
当数据通过 IPIP 隧道传输时,会经历以下步骤:
步骤一:原始数据包生成
应用程序发送数据包,目标地址是对方私有网段中的某个 IP:
# 例如,从 Server A (192.168.10.10) 访问 Server B (192.168.20.20)$ ping 192.168.20.20
步骤二:路由决策
内核查询路由表,发现目标地址 192.168.20.20 通过隧道接口 tunl0 转发:
# 查看路由表$ ip route show192.168.20.0/24 dev tunl0 proto kernel scope link src 192.168.20.1010.0.0.0/8 via 192.168.10.1 dev eth0192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.10
步骤三:封装
隧道驱动将原始 IP 数据包作为载荷,添加一个新的 IP 头:
┌─────────────────────────────────────────────────────────────────────┐│ IPIP 封装后的数据包 │├─────────────────────────────────────────────────────────────────────┤│ ││ ┌────────────────────────────────────────────────────────────────┐ ││ │ 外层 IP 头 (20 字节) │ ││ │ ┌────────┬────────┬─────────┬───────────────────────────────┐ │ ││ │ │ Version│ IHL │ TOS │ Total Length │ │ ││ │ ├────────┼────────┼─────────┼───────────────────────────────┤ │ ││ │ │ Identification │ Flags │ Fragment Offset │ │ ││ │ ├────────┼────────┼─────────┼───────────────────────────────┤ │ ││ │ │ TTL │ Proto=4│ Checksum│ Source IP: 203.0.113.10 │ │ ││ │ ├────────┼────────┼─────────┼───────────────────────────────┤ │ ││ │ │ Destination IP: 203.0.113.20 │ │ ││ │ └────────────────────────────────────────────────────────────┘ │ ││ └────────────────────────────────────────────────────────────────┘ ││ ││ ┌────────────────────────────────────────────────────────────────┐ ││ │ 内层 IP 头 (20 字节) │ ││ │ ┌────────┬────────┬─────────┬───────────────────────────────┐ │ ││ │ │ Version│ IHL │ TOS │ Total Length │ │ ││ │ ├────────┼────────┼─────────┼───────────────────────────────┤ │ ││ │ │ Identification │ Flags │ Fragment Offset │ │ ││ │ ├────────┴────────┴─────────┴───────────────────────────────┤ │ ││ │ │ TTL │ Proto=1│ Checksum│ Source IP: 192.168.10.10 │ │ ││ │ ├────────┼────────┼─────────┼───────────────────────────────┤ │ ││ │ │ Destination IP: 192.168.20.20 │ │ ││ │ └────────────────────────────────────────────────────────────┘ │ ││ └────────────────────────────────────────────────────────────────┘ ││ ││ ┌────────────────────────────────────────────────────────────────┐ ││ │ ICMP 数据 │ ││ └────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────┘
请求头信息:
Version | IP 版本,IPv4 为4 |
IHL | Internet Header Length,头部长度(以 32-bit 字为单位),通常为 5(即 20 字节) |
TOS / DSCP | 服务类型(Type of Service)或现在的 DiffServ 字段 |
Total Length | 整个 IP 包长度(包括头部和数据),单位:字节 |
Identification | 用于分片重组的唯一标识符 |
Flags | 分片标志(如 DF "Don't Fragment", MF "More Fragments") |
Fragment Offset | 分片偏移量(以 8 字节为单位) |
TTL | Time To Live,跳数限制 |
Protocol | 上层协议号(Proto=4表示上层是 IP-in-IP,注意区别 Version 的 4 表示 IPv4 包) IANA 协议号列表:https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml |
Header Checksum | 头部校验和(仅校验 IP 头) |
Source IP | 源 IP 地址,如203.0.113.10 (这是 RFC 5737 中的文档专用地址) |
2.2 协议号详解
IPIP 协议在 IP 头中的协议号(Protocol Field)是 4。这意味着当路由器看到一个 IP 数据包的协议号是 4 时,就知道这是一个 IPIP 封装的数据包,需要进行解封装处理。
# 查看 /etc/protocols 文件确认$ grep ipip /etc/protocolsipip 4 IP-in-IP encapsulation # IPIP 协议号 4
2.3 解封装过程
当数据包到达目标服务器的隧道接口后:
┌─────────────────────────────────────────────────────────────────────┐│ 解封装流程 ││ ││ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ││ │ 接收数据 │ ───► │ 检查协议 │ ───► │ 剥离外层 │ ───► │ 路由查找 │ ││ │ 包 │ │ 号 4 │ │ 头 │ │ │ ││ └─────────┘ └─────────┘ └─────────┘ └─────────┘ ││ ││ ┌─────────┐ ┌─────────┐ ┌─────────┐ ││ │ 交给应用 │ ◄─── │ 协议栈 │ ◄─── │ 本机地址 │ ││ │ │ │ 处理 │ │ 匹配 │ ││ └─────────┘ └─────────┘ └─────────┘ ││ │└─────────────────────────────────────────────────────────────────────┘
2.4 MTU 问题
这是 IPIP 隧道配置中最容易遇到的问题。
为什么需要调整 MTU?
标准以太网 MTU 是 1500 字节。IPIP 封装会额外增加 20 字节(外层 IP 头)。如果不调整,可能导致数据包被分片,影响性能甚至导致通信失败。
计算公式:
隧道 MTU = 物理接口 MTU - 外层 IP 头大小 - 内层 IP 头最小大小 = 1500 - 20 - 20 = 1460 字节
考虑到一些特殊情况,建议设置更保守的值:
# 推荐设置隧道 MTU 为 1400 或更小ip link set tunl0 mtu 1400
三、实战:搭建 IPIP 隧道
现在我们来动手搭建一个 IPIP 隧道。假设场景如下:
参数 | 北京服务器 (Server A) | 上海服务器 (Server B) |
公网 IP | 203.0.113.10 | 203.0.113.20 |
私网网段 | 192.168.10.0/24 | 192.168.20.0/24 |
隧道 IP | 10.0.0.1/30 | 10.0.0.2/30 |
┌─────────────────────────────────────────────────────────────────────────┐│ IPIP 隧道拓扑 ││ ││ 北京机房 (203.0.113.10) 上海机房 (203.0.113.20) ││ │ │ ││ │ 公网互联网 │ ││ │ ─────────────────────────────────────│─ ││ │ │ ││ ┌──────┴──────┐ ┌──────┴──────┐ ││ │ eth0 │ │ eth0 │ ││ │ 203.0.113.10│ │ 203.0.113.20│ ││ └──────┬──────┘ └──────┬──────┘ ││ │ │ ││ ┌──────┴──────┐ ┌──────┴──────┐ ││ │ tunl0 │◄────────────────────────►│ tunl0 │ ││ │ 10.0.0.1 │ IPIP 隧道 │ 10.0.0.2 │ ││ └──────┬──────┘ └──────┬──────┘ ││ │ │ ││ ┌──────┴──────┐ ┌──────┴──────┐ ││ │ 私网 eth1 │ │ 私网 eth1 │ ││ │192.168.10.10│ │192.168.20.20│ ││ └──────┬──────┘ └──────┬──────┘ ││ │ │ ││ 192.168.10.0/24 192.168.20.0/24 │└─────────────────────────────────────────────────────────────────────────┘
3.1 环境准备
首先确认系统支持 IPIP 隧道:
# 检查内核模块是否加载$ lsmod | grep ipipipip 24576 0 tunnel4 16384 1 ipipip_tunnel 28672 1 ipip# 如果没有加载,手动加载$ sudo modprobe ipip# 确认加载成功$ lsmod | grep ipipipip 24576 0 tunnel4 16384 1 ipipip_tunnel 28672 1 ipip
3.2 配置北京服务器 (Server A)
# 1. 创建隧道接口$ sudo ip tunnel add tunl0 mode ipip remote 203.0.113.20 local 203.0.113.10# 参数说明:# tunl0 - 隧道接口名称# mode ipip - 隧道模式为 IPIP# remote 203.0.113.20 - 远程公网 IP# local 203.0.113.10 - 本地公网 IP# 2. 启动隧道接口$ sudo ip link set tunl0 up# 3. 配置隧道 IP 地址$ sudo ip addr add 10.0.0.1/30 dev tunl0# 4. 设置 MTU(重要!)$ sudo ip link set tunl0 mtu 1400# 5. 添加路由,访问对方私网网段通过隧道# 这里的 via 10.0.0.2 仅用于标识隧道逻辑地址,不参与路由转发决策$ sudo ip route add 192.168.20.0/24 via 10.0.0.2 dev tunl0# 6. 验证配置$ ip addr show tunl0tunl0@NONE: <NOARP,UP,LOWER_UP> mtu 1400 qdisc noqueue state UNKNOWN group default qlen 1000 link/ipip 0.0.0.0 brd 0.0.0.0 inet 10.0.0.1/30 scope global tunl0 valid_lft forever preferred_lft forever$ ip route show10.0.0.0/30 dev tunl0 proto kernel scope link src 10.0.0.1 192.168.20.0/24 via 10.0.0.2 dev tunl0 src 10.0.0.1192.168.10.0/24 dev eth1 proto kernel scope link src 192.168.10.1010.0.0.0/8 via 192.168.10.1 dev eth1
3.3 配置上海服务器 (Server B)
# 1. 创建隧道接口$ sudo ip tunnel add tunl0 mode ipip remote 203.0.113.10 local 203.0.113.20# 2. 启动隧道接口$ sudo ip link set tunl0 up# 3. 配置隧道 IP 地址$ sudo ip addr add 10.0.0.2/30 dev tunl0# 4. 设置 MTU$ sudo ip link set tunl0 mtu 1400# 5. 添加路由,访问对方私网网段通过隧道$ sudo ip route add 192.168.10.0/24 via 10.0.0.1 dev tunl0
3.4 测试隧道连通性
在 北京服务器 上测试:
# 1. 测试隧道接口连通性$ ping -c 3 10.0.0.2PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=10.5 ms64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=10.3 ms64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=10.2 ms# 2. 测试上海私网连通性$ ping -c 3 192.168.20.20PING 192.168.20.20 (192.168.20.20) 56(84) bytes of data.64 bytes from 192.168.20.20: icmp_seq=1 ttl=62 time=11.2 ms64 bytes from 192.168.20.20: icmp_seq=2 ttl=62 time=11.0 ms64 bytes from 192.168.20.20: icmp_seq=3 ttl=62 time=10.8 ms
3.5 抓包验证封装
在任意一台服务器上使用 tcpdump 抓包,验证 IPIP 封装:
# 在北京服务器上抓取公网网卡$ sudo tcpdump -i eth0 -n -v | grep -E "IP|IPIP"# 正常会看到类似输出:# 10:05:23.123456 IP 203.0.113.10 > 203.0.113.20: IPIP 192.168.10.10 > 192.168.20.20: ICMP echo request# 10:05:23.124567 IP 203.0.113.20 > 203.0.113.10: IPIP 192.168.20.20 > 192.168.10.10: ICMP echo reply
从抓包结果可以清晰看到:
- 外层 IP:203.0.113.10 → 203.0.113.20
- 内层 IP:192.168.10.10 → 192.168.20.20
四、配置持久化
上面的配置在重启后会丢失。我们需要让配置持久化。
4.1 Ubuntu/Debian 配置
编辑 /etc/network/interfaces 文件:
# /etc/network/interfaces# 物理网卡配置(根据实际情况修改)auto eth0iface eth0 inet staticaddress 203.0.113.10netmask 255.255.255.0gateway 203.0.113.1# 私网网卡配置auto eth1iface eth1 inet staticaddress 192.168.10.10netmask 255.255.255.0# IPIP 隧道配置auto tunl0iface tunl0 inet staticaddress 10.0.0.1netmask 255.255.255.252# 隧道端点配置tunnel-remote 203.0.113.20tunnel-local 203.0.113.10# MTU 配置mtu 1400# 开机自动启动up ip route add 192.168.20.0/24 via 10.0.0.2 dev tunl0down ip route del 192.168.20.0/24 via 10.0.0.2 dev tunl0
应用配置:
$ sudo ifdown tunl0 && sudo ifup tunl0
4.2 RHEL 配置
创建网络脚本/etc/sysconfig/network-scripts/ifcfg-tunl0:
# /etc/sysconfig/network-scripts/ifcfg-tunl0DEVICE=tunl0NAME=tunl0BOOTPROTO=staticONBOOT=yesTYPE=GRETUNNEL_LOCAL=203.0.113.10TUNNEL_REMOTE=203.0.113.20MTU=1400IPADDR=10.0.0.1NETMASK=255.255.255.252
创建路由配置文件 /etc/sysconfig/network-scripts/route-tunl0:
# /etc/sysconfig/network-scripts/route-tunl0192.168.20.0/24 via 10.0.0.2 dev tunl0
启动接口:
$ sudo ifup tunl0$ sudo systemctl restart network
4.3 使用 Netplan(Ubuntu 18.04+)
编辑 /etc/netplan/01-netcfg.yaml:
network: version: 2 renderer: networkd ethernets: eth0: dhcp4: no addresses: - 203.0.113.10/24 routes: - to: default via: 203.0.113.1 eth1: dhcp4: no addresses: - 192.168.10.10/24 tunnels: tunl0: mode: ipip local: 203.0.113.10 remote: 203.0.113.20 addresses: - 10.0.0.1/30 mtu: 1400 routes: - to: 192.168.20.0/24 via: 10.0.0.2
应用配置:
五、高级应用场景
5.1 站点到站点(Site-to-Site)连接
将两个不同地理位置的私有网络连接成一个大的局域网:
┌─────────────────────────────────────────────────────────────────────────┐│ 站点到站点 IPIP 隧道 ││ ││ ┌─────────────────┐ ┌─────────────────┐ ││ │ 北京总部 │ │ 上海分公司 │ ││ │ │ │ │ ││ │ 10.0.0.0/16 │ │ 10.1.0.0/16 │ ││ │ │ │ │ │ │ ││ │ ▼ │ │ ▼ │ ││ │ ┌─────────┐ │ │ ┌─────────┐ │ ││ │ │ Gateway │ │ │ │ Gateway │ │ ││ │ │ .0.1 │ │ │ │ .1.1 │ │ ││ │ └────┬────┘ │ │ └────┬────┘ │ ││ │ │ │ │ │ │ ││ │ 公网 │ │ IPIP 隧道 │ │ 公网 │ ││ │ ◄────┴────► │◄────────────────────────────►◄────────┴────► │ ││ │ 203.0.113.10 │ │ 203.0.113.20 │ ││ └─────────────────┘ └─────────────────┘ ││ ││ 配置命令(在两端的 Gateway 上执行): ││ ││ # 北京端: ││ ip tunnel add tunl0 mode ipip remote 203.0.113.20 local 203.0.113.10 ││ ip link set tunl0 up ││ ip addr add 10.0.255.1/30 dev tunl0 ││ ip route add 10.1.0.0/16 via 10.0.255.2 dev tunl0 ││ ││ # 上海端: ││ ip tunnel add tunl0 mode ipip remote 203.0.113.10 local 203.0.113.20 ││ ip link set tunl0 up ││ ip addr add 10.0.255.2/30 dev tunl0 ││ ip route add 10.0.0.0/16 via 10.0.255.1 dev tunl0 ││ │└─────────────────────────────────────────────────────────────────────────┘
配置完成后,北京和上海的员工可以像在同一局域网一样互相访问。
5.2 跨云 VPC 互联
很多企业使用多云架构,IPIP 隧道可以帮助实现不同云 VPC 之间的互联:
# AWS VPC 配置(假设公网 IP 为 18.212.50.100)ip tunnel add cloud-tun mode ipip remote 203.0.113.20 local 18.212.50.100ip link set cloud-tun upip addr add 10.10.255.1/30 dev cloud-tunip route add 10.20.0.0/16 via 10.10.255.2 dev cloud-tun# 阿里云 VPC 配置(假设公网 IP 为 203.0.113.20)ip tunnel add cloud-tun mode ipip remote 18.212.50.100 local 203.0.113.20ip link set cloud-tun upip addr add 10.10.255.2/30 dev cloud-tunip route add 10.0.0.0/16 via 10.10.255.1 dev cloud-tun
5.3 多拨叠加带宽
在某些场景下,可以使用多条 IPIP 隧道实现带宽叠加:
# 创建多条隧道ip tunnel add tun0 mode ipip remote 203.0.113.20 local 203.0.113.10ip tunnel add tun1 mode ipip remote 203.0.113.21 local 203.0.113.11ip tunnel add tun2 mode ipip remote 203.0.113.22 local 203.0.113.12# 配置路由策略,实现负载均衡ip route add 192.168.20.0/24 \ nexthop via 10.0.0.2 dev tun0 \ nexthop via 10.0.1.2 dev tun1 \ nexthop via 10.0.2.2 dev tun2
六、与其他隧道技术的对比
IPIP 只是众多隧道技术中的一种。根据不同的场景,选择合适的隧道技术很重要。
特性 | IPIP | GRE | VXLAN | IPSec |
协议号 | 4 | 47 | 4789 | 50/51 |
层次 | L3 | L3 | L2 | L3 |
封装开销 | 20 字节 | 24 字节 | 50 字节 | 50+ 字节 |
加密 | ❌ | ❌ | ❌ | ✅ |
多播支持 | ❌ | ✅ | ✅ | ✅ |
非 IP 流量 | ❌ | ✅ | ✅ | ❌ |
配置复杂度 | 简单 | 中等 | 复杂 | 复杂 |
通用性 | 高 | 高 | 中等(容器网络) | 中等 |
6.1 选择建议
- 简单 L3 隧道
- 需要支持多播或非 IP
- 容器网络 Overlay
- 需要加密
七、常见问题与故障排除
7.1 隧道无法建立
症状:ping 不通对端隧道 IP
排查步骤:
# 1. 检查两端公网连通性ping 203.0.113.20# 2. 检查防火墙是否放行 Protocol 4sudo iptables -L -n -v | grep 4# 或sudo iptables -A INPUT -p 4 -j ACCEPTsudo iptables -A OUTPUT -p 4 -j ACCEPT# 3. 检查隧道接口状态ip tunnel showip link show tunl0# 4. 检查路由配置ip route show
7.2 MTU 导致的问题
症状:SSH 连接正常,但 scp 大文件失败;或某些网站能访问,某些不能访问
解决方案:
# 1. 调整隧道 MTUsudo ip link set tunl0 mtu 1400# 2. 同时调整服务器的整体 MTUsudo ip link set eth0 mtu 1400# 3. 在路由中设置 MTU(路径 MTU 发现可能失败)sudo ip route add 192.168.20.0/24 via 10.0.0.2 dev tunl0 mtu 1400# 4. 临时测试:降低客户端 MTUsudo ip link set eth0 mtu 1400
7.3 路由环路
症状:数据包在隧道中循环,无法到达目标
排查方法:
# 检查是否有错误的路由导致环路ip route showtraceroute 192.168.20.20# 常见原因:两台服务器都配置了默认路由通过隧道# 解决:只配置特定网段的路由,不要将默认路由指向隧道
7.4 防火墙配置
如果使用了 nftables 或 iptables,需要放行 IPIP 流量:
# iptables 放行 IPIPsudo iptables -I INPUT -p 4 -j ACCEPTsudo iptables -I OUTPUT -p 4 -j ACCEPT# nftables 放行 IPIPsudo nft add rule inet filter input ip protocol ipip acceptsudo nft add rule inet filter output ip protocol ipip accept
八、性能优化
8.1 启用硬件卸载
某些网卡支持隧道封装/解封装的硬件卸载:
# 检查是否支持ethtool -k eth0 | grep tunnel# 启用隧道卸载(如果支持)ethtool -K eth0 rx-tunnel offload on tx-tunnel offload on
8.2 调整缓冲区和队列
# 增加网络接口队列长度ip link set tunl0 txqueuelen 1000# 调整网卡的环形缓冲区ethtool -G eth0 rx 4096 tx 4096
8.3 使用多队列
在多核系统上,可以将隧道流量分配到不同的 CPU 核心:
# 查看当前 IRQ 亲和性cat /proc/interrupts | grep eth0# 将隧道相关的软中断绑定到特定 CPUecho 1 > /proc/irq/$(cat /proc/interrupts | grep eth0-rx-0 | awk '{print $1}')/smp_affinity
九、总结
IPIP 隧道是一种轻量级、易于配置的 L3 隧道技术,适用于以下场景:
- 跨地域私有网络互联
- 云 VPC 互联
- 简单 VPN 替代:快速建立点对点加密通道(注意:IPIP 本身不加密)
核心要点回顾
- 原理:将原始 IP 数据包封装在新的 IP 头中传输,协议号 4
- 配置:
ip tunnel add 创建隧道,ip route 添加路由 - 关键参数:local(本地公网)、remote(远端公网)、MTU
- 常见问题
- 选择场景:简单 L3 互联选 IPIP,需要加密选 IPSec,需要 L2 选 VXLAN