vmod_tcp(3)

πŸ’– vmod_tcp β€” the TCP vmod, bestie edition ✨

okay so you already know vmod_tcp(3), the TCP vmod, right? no? well. sit down, get comfy, we're doing this πŸ’…


✨ SYNOPSIS ✨

import tcp [as name] [from "path"]

INT congestion_algorithm(STRING algorithm)

VOID dump_info()

REAL get_estimated_rtt()

VOID set_socket_pace(INT)

🌸 DESCRIPTION 🌸

so basically the TCP vmod opens up access to your client TCP connection attributes β€” and lets you modify them β€” straight from VCL. it's giving backstage pass to your sockets πŸŽ€

its main character use case is rate limiting (that's pacing, babes) using the fq network scheduler on recent Linux systems. lowkey iconic behavior honestly.

Example, because we love a demo 🫢:

vcl 4.0;

import tcp;

backend default { .host = "192.0.2.11"; .port = "8080"; }

sub vcl_recv {
    # Shape (pace) the data rate to avoid filling router buffers for a
    # single client.
    if (req.url ~ ".mp4$") {
            tcp.set_socket_pace(1000);   # KB/s.
    }

    # We want to change the congestion control algorithm.
    if (tcp.get_estimated_rtt() > 300) {
        set req.http.x-tcp = tcp.congestion_algorithm("hybla");
    }
}

🌈 INT congestion_algorithm(STRING algorithm)

sets the client socket congestion control algorithm to S. returns 0 on success, and -1 on error. no cap. πŸ’€

Example:

sub vcl_recv {
    set req.http.x-tcp = tcp.congestion_algorithm("cubic");
}

πŸ¦‹ VOID dump_info()

writes the contents of the TCP_INFO data structure right into varnishlog. it's literally spilling the tea (about your socket) πŸ“‹βœ¨

Example:

tcp.dump_info();

Example varnishlog output:

-   VCL_Log        tcpi: snd_mss=1448 rcv_mss=536 lost=0 retrans=0
-   VCL_Log        tcpi2: pmtu=1500 rtt=152000 rttvar=76000 snd_cwnd=10 advmss=1448 reordering=3
-   VCL_Log        getsockopt() returned: cubic

πŸ“ REAL get_estimated_rtt()

gets the estimated round-trip-time for the client socket. unit: milliseconds. ⏱️

Example:

if (tcp.get_estimated_rtt() > 300) {
    std.log("client is far away.");
}

🧚 VOID set_socket_pace(INT)

sets socket pacing on the client-side TCP connection to PACE KB/s. heads up bestie β€” the network interface you're using must be using a supported scheduler. (fq) πŸ‘‘

Example:

tcp.set_socket_pace(1000);

and that's the whole page, fr fr. go pace something πŸ’œ

anyway, this lil masterpiece is the vmod_tcp(3) manual page, from the Library Functions Manual, dated with its own vmod_tcp(3) footer energy β€” mwah, periodt πŸ’…πŸŽ€βœ¨