golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.07k stars 17.36k forks source link

proposal: x/sys/unix: add support for linux' TCP_CC_INFO #68232

Open costela opened 5 days ago

costela commented 5 days ago

Proposal Details

The TCP_INFO support in x/sys/unix should be expanded to also cover TCP_CC_INFO.

There is at least one external package providing this functionality, but since it requires unsafe conversions of kernel structs, it is prone to subtle errors (e.g. "converted pointer straddles multiple allocations").

Therefore this functionality would profit from using the existing code-generation in x/sys/unix to keep it less error-prone.

gopherbot commented 5 days ago

Change https://go.dev/cl/595676 mentions this issue: linux: add tcp_cc_info and its related types

ianlancetaylor commented 4 days ago

This API is unusual for x/sys/unix, so sending it through the proposal process.

Proposal is

// GetsockoptTCPCCInfo returns algorithm specific congestion control information for the socket.
// It requires a type parameter to specify the type of congestion control algorithm being used. E.g. for BBR:
//
//  info, err := unix.GetsockoptTCPCCInfo[TCPBBRInfo](fd, unix.IPPROTO_TCP, unix.TCP_CC_INFO)
//
// The socket's congestion control algorighm can be retrieved via [GetsockoptString] with the [TCP_CONGESTION] option.
func GetsockoptTCPCCInfo[T TCPVegasInfo | TCPDCTCPInfo | TCPBBRInfo](fd, level, opt int) (*T, error) {
    var value tcpCCInfo
    vallen := _Socklen(SizeofTCPCCInfo)
    err := getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
    out := (*T)(unsafe.Pointer(&value[0]))
    return out, err
}

type TCPVegasInfo struct {
    ...
}

type TCPDCTCPInfo struct {
    ... 
}

type TCPBBRInfo struct {
    ... 
}