2024-06-03 20:42:06 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-08-09 15:03:58 +00:00
|
|
|
"net/netip"
|
2024-08-12 20:39:11 +00:00
|
|
|
"syscall"
|
2024-06-03 20:42:06 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mdlayher/socket"
|
|
|
|
"golang.org/x/sys/unix"
|
2024-06-04 14:26:10 +00:00
|
|
|
"tailscale.com/net/stun"
|
2024-06-03 20:42:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
flags = unix.SOF_TIMESTAMPING_TX_SOFTWARE | // tx timestamp generation in device driver
|
|
|
|
unix.SOF_TIMESTAMPING_RX_SOFTWARE | // rx timestamp generation in the kernel
|
|
|
|
unix.SOF_TIMESTAMPING_SOFTWARE // report software timestamps
|
|
|
|
)
|
|
|
|
|
2024-08-09 15:03:58 +00:00
|
|
|
func getUDPConnKernelTimestamp() (io.ReadWriteCloser, error) {
|
2024-06-03 20:42:06 +00:00
|
|
|
sconn, err := socket.Socket(unix.AF_INET6, unix.SOCK_DGRAM, unix.IPPROTO_UDP, "udp", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sa := unix.SockaddrInet6{}
|
|
|
|
err = sconn.Bind(&sa)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = sconn.SetsockoptInt(unix.SOL_SOCKET, unix.SO_TIMESTAMPING_NEW, flags)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sconn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTimestampFromCmsgs(oob []byte) (time.Time, error) {
|
|
|
|
msgs, err := unix.ParseSocketControlMessage(oob)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, fmt.Errorf("error parsing oob as cmsgs: %w", err)
|
|
|
|
}
|
|
|
|
for _, msg := range msgs {
|
|
|
|
if msg.Header.Level == unix.SOL_SOCKET && msg.Header.Type == unix.SO_TIMESTAMPING_NEW && len(msg.Data) >= 16 {
|
|
|
|
sec := int64(binary.NativeEndian.Uint64(msg.Data[:8]))
|
|
|
|
ns := int64(binary.NativeEndian.Uint64(msg.Data[8:16]))
|
|
|
|
return time.Unix(sec, ns), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Time{}, errors.New("failed to parse timestamp from cmsgs")
|
|
|
|
}
|
|
|
|
|
2024-08-12 20:39:11 +00:00
|
|
|
func measureSTUNRTTKernel(conn io.ReadWriteCloser, hostname string, dst netip.AddrPort) (rtt time.Duration, err error) {
|
2024-06-03 20:42:06 +00:00
|
|
|
sconn, ok := conn.(*socket.Conn)
|
|
|
|
if !ok {
|
2024-06-04 14:26:10 +00:00
|
|
|
return 0, fmt.Errorf("conn of unexpected type: %T", conn)
|
2024-06-03 20:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var to unix.Sockaddr
|
2024-08-09 15:03:58 +00:00
|
|
|
if dst.Addr().Is4() {
|
2024-06-03 20:42:06 +00:00
|
|
|
to = &unix.SockaddrInet4{
|
2024-08-09 15:03:58 +00:00
|
|
|
Port: int(dst.Port()),
|
2024-06-03 20:42:06 +00:00
|
|
|
}
|
2024-08-09 15:03:58 +00:00
|
|
|
copy(to.(*unix.SockaddrInet4).Addr[:], dst.Addr().AsSlice())
|
2024-06-03 20:42:06 +00:00
|
|
|
} else {
|
|
|
|
to = &unix.SockaddrInet6{
|
2024-08-09 15:03:58 +00:00
|
|
|
Port: int(dst.Port()),
|
2024-06-03 20:42:06 +00:00
|
|
|
}
|
2024-08-09 15:03:58 +00:00
|
|
|
copy(to.(*unix.SockaddrInet6).Addr[:], dst.Addr().AsSlice())
|
2024-06-03 20:42:06 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 14:26:10 +00:00
|
|
|
txID := stun.NewTxID()
|
|
|
|
req := stun.Request(txID)
|
|
|
|
|
2024-06-03 20:42:06 +00:00
|
|
|
err = sconn.Sendto(context.Background(), req, 0, to)
|
|
|
|
if err != nil {
|
2024-06-04 14:26:10 +00:00
|
|
|
return 0, fmt.Errorf("sendto error: %v", err) // don't wrap
|
2024-06-03 20:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txCtx, txCancel := context.WithTimeout(context.Background(), time.Second*2)
|
|
|
|
defer txCancel()
|
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
oob := make([]byte, 1024)
|
|
|
|
var txAt time.Time
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, oobn, _, _, err := sconn.Recvmsg(txCtx, buf, oob, unix.MSG_ERRQUEUE)
|
|
|
|
if err != nil {
|
2024-06-04 14:26:10 +00:00
|
|
|
return 0, fmt.Errorf("recvmsg (MSG_ERRQUEUE) error: %v", err) // don't wrap
|
2024-06-03 20:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf = buf[:n]
|
|
|
|
if n < len(req) || !bytes.Equal(req, buf[len(buf)-len(req):]) {
|
|
|
|
// Spin until we find the message we sent. We get the full packet
|
|
|
|
// looped including eth header so match against the tail.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
txAt, err = parseTimestampFromCmsgs(oob[:oobn])
|
|
|
|
if err != nil {
|
2024-06-04 14:26:10 +00:00
|
|
|
return 0, fmt.Errorf("failed to get tx timestamp: %v", err) // don't wrap
|
2024-06-03 20:42:06 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
rxCtx, rxCancel := context.WithTimeout(context.Background(), time.Second*2)
|
|
|
|
defer rxCancel()
|
|
|
|
|
2024-06-04 14:26:10 +00:00
|
|
|
for {
|
|
|
|
n, oobn, _, _, err := sconn.Recvmsg(rxCtx, buf, oob, 0)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("recvmsg error: %w", err) // wrap for timeout-related error unwrapping
|
|
|
|
}
|
|
|
|
|
|
|
|
gotTxID, _, err := stun.ParseResponse(buf[:n])
|
|
|
|
if err != nil || gotTxID != txID {
|
|
|
|
// Spin until we find the txID we sent. We may end up reading
|
|
|
|
// extremely late arriving responses from previous intervals. As
|
|
|
|
// such, we can't be certain if we're parsing the "current"
|
|
|
|
// response, so spin for parse errors too.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rxAt, err := parseTimestampFromCmsgs(oob[:oobn])
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to get rx timestamp: %v", err) // don't wrap
|
|
|
|
}
|
|
|
|
|
|
|
|
return rxAt.Sub(txAt), nil
|
2024-06-03 20:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-12 21:09:45 +00:00
|
|
|
func getProtocolSupportInfo(p protocol) protocolSupportInfo {
|
|
|
|
switch p {
|
|
|
|
case protocolSTUN:
|
|
|
|
return protocolSupportInfo{
|
|
|
|
kernelTS: true,
|
|
|
|
userspaceTS: true,
|
|
|
|
stableConn: true,
|
|
|
|
}
|
|
|
|
case protocolHTTPS:
|
|
|
|
return protocolSupportInfo{
|
|
|
|
kernelTS: false,
|
|
|
|
userspaceTS: true,
|
|
|
|
stableConn: true,
|
|
|
|
}
|
|
|
|
case protocolTCP:
|
|
|
|
return protocolSupportInfo{
|
|
|
|
kernelTS: true,
|
|
|
|
userspaceTS: false,
|
|
|
|
stableConn: true,
|
|
|
|
}
|
|
|
|
// TODO(jwhited): add ICMP
|
2024-08-09 15:03:58 +00:00
|
|
|
}
|
2024-08-12 21:09:45 +00:00
|
|
|
return protocolSupportInfo{}
|
2024-06-03 20:42:06 +00:00
|
|
|
}
|
2024-08-12 20:39:11 +00:00
|
|
|
|
|
|
|
func setSOReuseAddr(fd uintptr) error {
|
|
|
|
// we may restart faster than TIME_WAIT can clear
|
|
|
|
return syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
|
|
|
|
}
|