mirror of
https://github.com/jsimonetti/rtnetlink.git
synced 2026-03-27 06:51:07 +01:00
* netns: remove iproute2 dependency This commit introduces a breaking change to rtnetlink.NetNS. The existing netns implementation had a few problems. It assumed that network namespaces have names, that they would always be pinned to /var/run/netns, and that numeric/integer references are pid references. This made the NetNS type unusable for referring to existing netns by fd, such as ones created by other libraries, or by opening procfs entries directly as demonstrated in the new testutils.NetNS() function. The forced dependency on the `ip` CLI tool also wasn't reasonable for a pure-Go library. Using the old implementation in a scratch/distroless container would quickly run into roadblocks. This commit also removes the functionality of creating and pinning new netns. There are plenty of options out in the Go ecosystem for that, and providing your own is only a few lines of code. Signed-off-by: Timo Beckers <timo@incline.eu> * test: remove calls to unix.Setrlimit() in favor of rlimit.RemoveMemlock() ebpf-go provides this out of the box and skips setting the rlimit on kernels that support bpf memory cgroup accounting. Signed-off-by: Timo Beckers <timo@incline.eu> * neigh: fix flaky tests, add State field to Neigh entry The flaky tests that were documented in the code are expected. Use the State field to discard entries that can't reasonably be considered in tests. Signed-off-by: Timo Beckers <timo@incline.eu> * neigh: fix race in Conn.Neighbours When running tests locally, I would frequently hit "too many/little matches, expected 1, actual 0" due to other tests creating and deleting interfaces in the common host netns used by all tests. Neigh entries that fail the interface lookup can't have their Interface fields populated and should be dropped from the result since the interface is no longer there to begin with. Signed-off-by: Timo Beckers <timo@incline.eu> * xdp: refactor test suite to use test helpers and netns-driven tests While running the test suite for testing netns-related changes, I noticed some of the xdp tests started failing because they wanted to create a dummy interface in the host network namespace. Avoid the complexity of managing dummy interfaces altogether by running all tests within their own netns and use the existing lo device that's present by default. Signed-off-by: Timo Beckers <timo@incline.eu> * xdp,netkit: remove duplicate kernelMinReq in favor of testutils.SkipOnOldKernel There were two implementations of this, so move them to common testutils. Signed-off-by: Timo Beckers <timo@incline.eu> --------- Signed-off-by: Timo Beckers <timo@incline.eu>
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package rtnetlink
|
|
|
|
import (
|
|
"github.com/jsimonetti/rtnetlink/v2/internal/unix"
|
|
)
|
|
|
|
// NetNS represents a Linux network namespace handle to specify in
|
|
// [LinkAttributes].
|
|
//
|
|
// Use [NetNSForPID] to create a handle to the network namespace of an existing
|
|
// PID, or [NetNSForFD] for a handle to an existing network namespace created by
|
|
// another library.
|
|
type NetNS struct {
|
|
fd *uint32
|
|
pid *uint32
|
|
}
|
|
|
|
// NetNSForPID returns a handle to the network namespace of an existing process
|
|
// given its pid. The process must be alive when the NetNS is used in any API
|
|
// calls.
|
|
//
|
|
// The resulting NetNS doesn't hold a hard reference to the netns (it doesn't
|
|
// increase its refcount) and becomes invalid when the process it points to
|
|
// dies.
|
|
func NetNSForPID(pid uint32) *NetNS {
|
|
return &NetNS{pid: &pid}
|
|
}
|
|
|
|
// NetNSForFD returns a handle to an existing network namespace created by
|
|
// another library. It does not clone fd or manage its lifecycle in any way.
|
|
// The caller is responsible for making sure the underlying fd stays alive
|
|
// for the duration of any API calls using the NetNS.
|
|
func NetNSForFD(fd uint32) *NetNS {
|
|
return &NetNS{fd: &fd}
|
|
}
|
|
|
|
// value returns the type and value of the NetNS for use in netlink attributes.
|
|
func (ns *NetNS) value() (uint16, uint32) {
|
|
if ns.fd != nil {
|
|
return unix.IFLA_NET_NS_FD, *ns.fd
|
|
}
|
|
if ns.pid != nil {
|
|
return unix.IFLA_NET_NS_PID, *ns.pid
|
|
}
|
|
return 0, 0
|
|
}
|