[meshnet] Manage system-level kernel and link tunables for packet performance - #734
Open
kraney wants to merge 8 commits into
Open
[meshnet] Manage system-level kernel and link tunables for packet performance#734kraney wants to merge 8 commits into
kraney wants to merge 8 commits into
Conversation
Replace the veth pair and libpcap packet capture implementation in meshnetd with persistent Linux TAP devices created directly inside container network namespaces. Reason for Change: - Eliminates packet loss under high traffic loads caused by libpcap's kernel- to-userspace drop policy. - Provides native Linux socket buffer flow control and backpressure (`txqueuelen`) up to the container application when gRPC processing falls behind. - Guarantees protocol transparent packet handling (including LACP and LLDP frames). - Ensures process crash resilience: setting `TUNSETPERSIST=1` keeps TAP interfaces alive in the container netns across daemon restarts without link flaps. - Synchronizes CNI plugin pod readiness with complete end-to-end gRPC wire setup, preventing test/ping race conditions upon pod startup. - Eliminates CGO and `libpcap-dev` build dependencies, producing a pure Go static binary (`CGO_ENABLED=0`). Key Changes: - Added `CreateOrAttachTAP` in `wireutil` using `TUNSETIFF` & `TUNSETPERSIST`. - Replaced `pcap.Handle` with `*os.File` in `gwire_map.go`, `grpcwire.go`, and `handler.go`. - Updated `ReconcilePodLinks` in `controller.go` to use TAP interfaces directly without host-side veth creation. - Updated `GRPCWireExists` and CNI `cmdAdd` readiness check to block until gRPC wire handshakes are fully established. - Removed `libpcap-dev` and updated Dockerfile to build `meshnetd` with `CGO_ENABLED=0`.
Add package & public method comments, and format Go
1. Bidirectional Streaming (SendToStream) Receiver (handler.go):
• Implemented SendToStream(stream mpb.WireProtocol_SendToStreamServer) error.
• In a loop, stream.Recv() continuously ingests incoming mpb.Packet frames and writes them directly to the
destination TAP interface (wrHandle.Write(pkt.Frame)), bypassing per-packet unary RPC overhead.
2. Streaming Sender & Auto-Reconnect (grpcwire.go):
• Updated RecvFrmLocalPodThread to establish a persistent client stream (wireClient.SendToStream(ctx)).
• Frames read from the local TAP interface are streamed out via st.Send(payload) without blocking for individual
RPC responses.
• If the stream encounters a network or peer reset, RecvFrmLocalPodThread automatically clears its stream handle
and transparently re-establishes SendToStream on the next packet.
3. HTTP/2 Window Size & Buffer Tuning:
• Server Configuration (meshnet.go):
• Stream window size: 4 MB (grpc.InitialWindowSize(4 * 1024 * 1024)).
• Connection window size: 16 MB (grpc.InitialConnWindowSize(16 * 1024 * 1024)).
• Max message payload: 64 MB (grpc.MaxRecvMsgSize / grpc.MaxSendMsgSize).
• Client Configuration (grpcwire.go):
• Configured matching initial window size and message payload limits on client grpc.Dial.
1. Created nodeStreamManager (stream_manager.go):
• Keyed by nodeStreamKey{ topoNs string, peerIP string }.
• Opens only one gRPC connection (grpc.Dial) and only one SendToStream bidirectional streaming RPC per (peerIP,
topoNs) pair.
• Includes reference counting (GetOrCreateStream / ReleaseStream). When the last wire for a topology targeting a
peer node is removed, the shared stream and gRPC connection close gracefully.
• Features a high-capacity buffered channel (10,000 packet queue) and dedicated sender worker loop with automatic
reconnect logic.
2. Updated TAP Reader Threads (grpcwire.go):
• Modified RecvFrmLocalPodThread so TAP readers no longer execute individual grpc.Dial or SendToStream calls.
• Each TAP reader now acquires the shared topology stream via nodeStream := streamMgr.GetOrCreateStream(wire.
TopoNamespace, wire.PeerNodeIP) and multiplexes packets into nodeStream.Send(payload).
3. Per-Topology Isolation:
• Topology topo-A and topology topo-B running between the same pair of physical nodes maintain completely
separate gRPC connections and streams.
The base design had a lot of single-interface RPCs to update k8s and remote meshnets. For large numbers of links it costs a lot of serial round trip wait time. This batches things and pipelines them so that we avoid a lot of unnecessary overhead delay at start time.
…mance
Implement kernel sysctl tuning, gRPC stream multiplexing, and persistent TAP device
packet I/O in meshnetd for high-density, high-throughput network meshes.
Reason for Change:
- Default Linux link queue lengths, netdev backlogs, multicast membership limits, and
strict rp_filter settings cause packet drops during netdev creation bursts and multi-link tests.
- IPv6 DAD (Duplicate Address Detection) and Router Solicitations introduce 1-2s readiness
delays per netdev when launching virtual topologies.
Key Changes:
- System & Kernel Tuning (`utils/wireutil/sys_tune.go`):
- Sets `RLIMIT_NOFILE` to 1,048,576 open file descriptors.
- Sets `netdev_max_backlog` to 10,000 for device queue bursts.
- Sets OS send/recv max & default buffers (`rmem_max`, `wmem_max`, `rmem_default`, `wmem_default`) to 16 MB.
- Sets ARP/neighbor GC thresholds (`gc_thresh1`=1024, `gc_thresh2`=4096, `gc_thresh3`=8192).
- Sets `igmp_max_memberships` to 10,000 and `mld_max_msf` to 4,096 to prevent multicast group netdev drops.
- Sets `rp_filter` to 2 (Loose mode) to allow asymmetric routing across multi-link meshes.
- Disables IPv6 DAD (`accept_dad`=0) and RS floods (`router_solicitations`=0) for instant link readiness.
- Sets `ipv6/route/max_size` to 1,048,576 for large routing tables.
- Configured link `txqueuelen` (default 10,000) across TAP, vEth, and vxLAN interface creation.
- All tunables are overridable via environment variables without recompilation.
Environment Variable │ Default Value │ Configures
───────────────────────────────┼───────────────────────────────┼─────────────────────────────────────────────────────
LINK_TXQUEUELEN │ 10000 │ Interface txqueuelen for TAP, vEth, and vxLAN links
RLIMIT_NOFILE │ 1048576 │ Process open file descriptor limit (unix.Setrlimit)
NETDEV_MAX_BACKLOG │ 10000 │ /proc/sys/net/core/netdev_max_backlog
RMEM_MAX │ 16777216 │ /proc/sys/net/core/rmem_max (16 MB)
WMEM_MAX │ 16777216 │ /proc/sys/net/core/wmem_max (16 MB)
RMEM_DEFAULT │ 16777216 │ /proc/sys/net/core/rmem_default (16 MB)
WMEM_DEFAULT │ 16777216 │ /proc/sys/net/core/wmem_default (16 MB)
ARP_GC_THRESH1 │ 1024 │ /proc/sys/net/ipv4 & ipv6/neigh/default/gc_thresh1
ARP_GC_THRESH2 │ 4096 │ /proc/sys/net/ipv4 & ipv6/neigh/default/gc_thresh2
ARP_GC_THRESH3 │ 8192 │ /proc/sys/net/ipv4 & ipv6/neigh/default/gc_thresh3
Sysctl / Limit │ Env Var Override │ High-Density Default
───────────────────────────────────────┼──────────────────────────────────────┼──────────────────────────────────────
igmp_max_memberships │ IGMP_MAX_MEMBERSHIPS │ 10000
mld_max_msf │ MLD_MAX_MSF │ 4096
rp_filter │ RP_FILTER │ 2 (loose)
accept_dad │ IPV6_ACCEPT_DAD │ 0 (disabled)
router_solicitations │ IPV6_ROUTER_SOLICITATIONS │ 0 (disabled)
max_size (IPv6 route) │ IPV6_ROUTE_MAX_SIZE │ 1048576
netdev_max_backlog │ NETDEV_MAX_BACKLOG │ 10000
rmem_max / wmem_max │ RMEM_MAX / WMEM_MAX │ 16777216 (16 MB)
gc_thresh1..3 │ ARP_GC_THRESH1..3 │ 1024 / 4096 / 8192
RLIMIT_NOFILE │ RLIMIT_NOFILE │ 1048576
Defaults are chosen with the intent of making custom tuning unnecessary for all but the
most demanding topologies.
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Note: this PR stacks on top of #733 . Please merge that one first (which should make the shared commits disappear from this PR.) The incremental change of this PR starts with sha:b3c915457042d606dd0dd9bb659ed1ea619af81e.