|
|
|
|
@ -17,7 +17,7 @@ import (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const conntrackAccountingPath = "/proc/sys/net/netfilter/nf_conntrack_acct"
|
|
|
|
|
const conntrackMaxPath = "/proc/sys/net/netfilter/nf_conntrack_max"
|
|
|
|
|
const conntrackCountPath = "/proc/sys/net/netfilter/nf_conntrack_count"
|
|
|
|
|
const defaultConntrackDumpTimeout = 10 * time.Second
|
|
|
|
|
|
|
|
|
|
type conntrackDumper interface {
|
|
|
|
|
@ -39,14 +39,14 @@ func (dump *activeConntrackDump) Close() error {
|
|
|
|
|
// NetlinkSource is intentionally read-only. The interface exposed here has no
|
|
|
|
|
// Flush, Delete, Update, Create or socket-option mutation method.
|
|
|
|
|
type NetlinkSource struct {
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
dial func() (conntrackDumper, error)
|
|
|
|
|
tableLimit func() (uint64, error)
|
|
|
|
|
dumpTimeout time.Duration
|
|
|
|
|
closed bool
|
|
|
|
|
nextDumpID uint64
|
|
|
|
|
active map[uint64]*activeConntrackDump
|
|
|
|
|
inflight sync.WaitGroup
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
dial func() (conntrackDumper, error)
|
|
|
|
|
currentEntries func() (uint64, error)
|
|
|
|
|
dumpTimeout time.Duration
|
|
|
|
|
closed bool
|
|
|
|
|
nextDumpID uint64
|
|
|
|
|
active map[uint64]*activeConntrackDump
|
|
|
|
|
inflight sync.WaitGroup
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func OpenNetlinkSource() (*NetlinkSource, error) {
|
|
|
|
|
@ -56,13 +56,13 @@ func OpenNetlinkSource() (*NetlinkSource, error) {
|
|
|
|
|
return nil, classifyNetlinkError(err)
|
|
|
|
|
}
|
|
|
|
|
return connection, nil
|
|
|
|
|
}, readConntrackTableLimit, defaultConntrackDumpTimeout), nil
|
|
|
|
|
}, readConntrackTableCount, defaultConntrackDumpTimeout), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newNetlinkSource(dial func() (conntrackDumper, error), tableLimit func() (uint64, error),
|
|
|
|
|
func newNetlinkSource(dial func() (conntrackDumper, error), currentEntries func() (uint64, error),
|
|
|
|
|
dumpTimeout time.Duration) *NetlinkSource {
|
|
|
|
|
return &NetlinkSource{
|
|
|
|
|
dial: dial, tableLimit: tableLimit, dumpTimeout: dumpTimeout,
|
|
|
|
|
dial: dial, currentEntries: currentEntries, dumpTimeout: dumpTimeout,
|
|
|
|
|
active: make(map[uint64]*activeConntrackDump),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -82,11 +82,11 @@ func (source *NetlinkSource) Snapshot(ctx context.Context) (KernelSnapshot, erro
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
return KernelSnapshot{}, err
|
|
|
|
|
}
|
|
|
|
|
if source.tableLimit == nil {
|
|
|
|
|
if source.currentEntries == nil {
|
|
|
|
|
return KernelSnapshot{}, ErrConntrackUnavailable
|
|
|
|
|
}
|
|
|
|
|
tableLimit, err := source.tableLimit()
|
|
|
|
|
if err != nil || tableLimit == 0 || tableLimit > HardMaxKernelFlows {
|
|
|
|
|
currentEntries, err := source.currentEntries()
|
|
|
|
|
if err != nil || currentEntries > HardMaxKernelFlows {
|
|
|
|
|
return KernelSnapshot{}, ErrConntrackUnavailable
|
|
|
|
|
}
|
|
|
|
|
if source.dial == nil {
|
|
|
|
|
@ -185,8 +185,8 @@ func (source *NetlinkSource) release(dumpID uint64, active *activeConntrackDump)
|
|
|
|
|
source.inflight.Done()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func readConntrackTableLimit() (uint64, error) {
|
|
|
|
|
value, err := os.ReadFile(conntrackMaxPath)
|
|
|
|
|
func readConntrackTableCount() (uint64, error) {
|
|
|
|
|
value, err := os.ReadFile(conntrackCountPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|