rtnetlink/netns.go
Birol Bilgin bd79d59a97
Add LinkDriver interface and Driver package (#221)
* Add attr validation

This commit removes unix.IFLA_UNSPEC and introduces checks for the interface name,
the link type and the queue disc fields.
Interface name validation is necessary to prevent an 'invalid argument' error
when creating a link with an empty name. Other checks were added to be consistent with the ip tools.

Signed-off-by: Birol Bilgin <birolbilgin@gmail.com>

* Add network namespace type

This commit introduces the NetNS struct and integrates network namespace capabilities into link attributes.
This enhancement facilitates the creation of links, such as veth pairs, across different network namespaces
without the need to execute directly within those namespaces.

Signed-off-by: Birol Bilgin <birolbilgin@gmail.com>

* Add LinkDriver interface

This commit introduces a Driver interface and changes Data and SlaveData fields within the LinkInfo struct
as LinkDriver to accommodate driver-specific data encoding, addressing the limitation
where LinkInfo.Data and SlaveData fields were merely byte slices without support for specific data encoding.

Drivers are registered globally with the RegisterDriver function.
For un-registered drivers, the default LinkData driver is used.

Signed-off-by: Birol Bilgin <birolbilgin@gmail.com>

* Add Driver Package

This commit introduces the 'driver' package, which contains specific implementations of the LinkDriver interface.
It also includes the implementation of the Linux bond driver as LinkDriver and the bond slave driver as LinkSlaveDriver.

Signed-off-by: Birol Bilgin <birolbilgin@gmail.com>

* Add Netkit and Veth drivers

This commit adds Netkit and Veth drivers.

Signed-off-by: Birol Bilgin <birolbilgin@gmail.com>

---------

Signed-off-by: Birol Bilgin <birolbilgin@gmail.com>
2024-05-10 08:51:09 +02:00

55 lines
1.2 KiB
Go
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package rtnetlink
import (
"os"
"path/filepath"
"github.com/jsimonetti/rtnetlink/internal/unix"
)
// NetNS represents a Linux network namespace
type NetNS struct {
file *os.File
pid uint32
}
// NewNetNS returns a new NetNS from the given type
// When an uint32 is given simply the pid value is set
// When a string is given a namespace file is opened with the name and the file descriptor is set
// The file descriptor should be closed after use with the Close() method
func NewNetNS[T string | uint32](t T) (*NetNS, error) {
if name, ok := any(t).(string); ok {
file, err := os.Open(filepath.Join("/var/run/netns", name))
if err != nil {
return nil, err
}
return &NetNS{file: file}, nil
}
return &NetNS{pid: any(t).(uint32)}, nil
}
// Type returns either unix.IFLA_NET_NS_FD or unix.IFLA_NET_NS_PID according ns data type
func (n *NetNS) Type() uint16 {
if n.file != nil {
return unix.IFLA_NET_NS_FD
}
return unix.IFLA_NET_NS_PID
}
// Value returns either a file descriptor value or the pid value of the ns
func (n *NetNS) Value() uint32 {
if n.file != nil {
return uint32(n.file.Fd())
}
return n.pid
}
// Close closes the file descriptor
func (n *NetNS) Close() error {
if n.file != nil {
return n.file.Close()
}
return nil
}