host-device: add deviceID property

Fixes #253
This commit is contained in:
Zenghui Shi 2019-04-18 12:18:39 +08:00
parent 7df5acee0f
commit 79b1c402c4
3 changed files with 47 additions and 9 deletions

View File

@ -1,17 +1,25 @@
# host-device # host-device
Move an already-existing device into a container. Move an already-existing device into a container.
This simple plugin will move the requested device from the host's network namespace ## Overview
to the container's. Nothing else will be done - no IPAM, no addresses.
The device can be specified with any one of three properties: This simple plugin will move the requested device from the host's network namespace
to the container's. IPAM configuration can be used for this plugin.
## Network configuration reference
The device can be specified with any one of four properties:
* `device`: The device name, e.g. `eth0`, `can0` * `device`: The device name, e.g. `eth0`, `can0`
* `hwaddr`: A MAC address * `hwaddr`: A MAC address
* `kernelpath`: The kernel device kobj, e.g. `/sys/devices/pci0000:00/0000:00:1f.6` * `kernelpath`: The kernel device kobj, e.g. `/sys/devices/pci0000:00/0000:00:1f.6`
* `pciBusID`: A PCI address of network device, e.g `0000:00:1f.6`
For this plugin, `CNI_IFNAME` will be ignored. Upon DEL, the device will be moved back. For this plugin, `CNI_IFNAME` will be ignored. Upon DEL, the device will be moved back.
A sample configuration might look like: ## Example configuration
A sample configuration with `device` property looks like:
```json ```json
{ {
@ -20,3 +28,13 @@ A sample configuration might look like:
"device": "enp0s1" "device": "enp0s1"
} }
``` ```
A sample configuration with `pciBusID` property looks like:
```json
{
"cniVersion": "0.3.1",
"type": "host-device",
"pciBusID": "0000:3d:00.1"
}
```

View File

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -38,12 +39,17 @@ import (
bv "github.com/containernetworking/plugins/pkg/utils/buildversion" bv "github.com/containernetworking/plugins/pkg/utils/buildversion"
) )
const (
sysBusPCI = "/sys/bus/pci/devices"
)
//NetConf for host-device config, look the README to learn how to use those parameters //NetConf for host-device config, look the README to learn how to use those parameters
type NetConf struct { type NetConf struct {
types.NetConf types.NetConf
Device string `json:"device"` // Device-Name, something like eth0 or can0 etc. Device string `json:"device"` // Device-Name, something like eth0 or can0 etc.
HWAddr string `json:"hwaddr"` // MAC Address of target network interface HWAddr string `json:"hwaddr"` // MAC Address of target network interface
KernelPath string `json:"kernelpath"` // Kernelpath of the device KernelPath string `json:"kernelpath"` // Kernelpath of the device
PCIAddr string `json:"pciBusID"` // PCI Address of target network device
} }
func init() { func init() {
@ -58,8 +64,8 @@ func loadConf(bytes []byte) (*NetConf, error) {
if err := json.Unmarshal(bytes, n); err != nil { if err := json.Unmarshal(bytes, n); err != nil {
return nil, fmt.Errorf("failed to load netconf: %v", err) return nil, fmt.Errorf("failed to load netconf: %v", err)
} }
if n.Device == "" && n.HWAddr == "" && n.KernelPath == "" { if n.Device == "" && n.HWAddr == "" && n.KernelPath == "" && n.PCIAddr == "" {
return nil, fmt.Errorf(`specify either "device", "hwaddr" or "kernelpath"`) return nil, fmt.Errorf(`specify either "device", "hwaddr", "kernelpath" or "pciBusID"`)
} }
return n, nil return n, nil
} }
@ -75,7 +81,7 @@ func cmdAdd(args *skel.CmdArgs) error {
} }
defer containerNs.Close() defer containerNs.Close()
hostDev, err := getLink(cfg.Device, cfg.HWAddr, cfg.KernelPath) hostDev, err := getLink(cfg.Device, cfg.HWAddr, cfg.KernelPath, cfg.PCIAddr)
if err != nil { if err != nil {
return fmt.Errorf("failed to find host device: %v", err) return fmt.Errorf("failed to find host device: %v", err)
} }
@ -240,7 +246,7 @@ func printLink(dev netlink.Link, cniVersion string, containerNs ns.NetNS) error
return types.PrintResult(&result, cniVersion) return types.PrintResult(&result, cniVersion)
} }
func getLink(devname, hwaddr, kernelpath string) (netlink.Link, error) { func getLink(devname, hwaddr, kernelpath, pciaddr string) (netlink.Link, error) {
links, err := netlink.LinkList() links, err := netlink.LinkList()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to list node links: %v", err) return nil, fmt.Errorf("failed to list node links: %v", err)
@ -278,6 +284,19 @@ func getLink(devname, hwaddr, kernelpath string) (netlink.Link, error) {
} }
} }
} }
} else if len(pciaddr) > 0 {
netDir := filepath.Join(sysBusPCI, pciaddr, "net")
if _, err := os.Lstat(netDir); err != nil {
return nil, fmt.Errorf("no net directory under pci device %s: %q", pciaddr, err)
}
fInfo, err := ioutil.ReadDir(netDir)
if err != nil {
return nil, fmt.Errorf("failed to read net directory %s: %q", netDir, err)
}
if len(fInfo) > 0 {
return netlink.LinkByName(fInfo[0].Name())
}
return nil, fmt.Errorf("failed to find device name for pci address %s", pciaddr)
} }
return nil, fmt.Errorf("failed to find physical interface") return nil, fmt.Errorf("failed to find physical interface")

View File

@ -40,6 +40,7 @@ type Net struct {
Device string `json:"device"` // Device-Name, something like eth0 or can0 etc. Device string `json:"device"` // Device-Name, something like eth0 or can0 etc.
HWAddr string `json:"hwaddr"` // MAC Address of target network interface HWAddr string `json:"hwaddr"` // MAC Address of target network interface
KernelPath string `json:"kernelpath"` // Kernelpath of the device KernelPath string `json:"kernelpath"` // Kernelpath of the device
PCIAddr string `json:"pciBusID"` // PCI Address of target network device
IPAM *IPAMConfig `json:"ipam,omitempty"` IPAM *IPAMConfig `json:"ipam,omitempty"`
DNS types.DNS `json:"dns"` DNS types.DNS `json:"dns"`
RawPrevResult map[string]interface{} `json:"prevResult,omitempty"` RawPrevResult map[string]interface{} `json:"prevResult,omitempty"`
@ -449,7 +450,7 @@ var _ = Describe("base functionality", func() {
StdinData: []byte(conf), StdinData: []byte(conf),
} }
_, _, err := testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) }) _, _, err := testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) })
Expect(err).To(MatchError(`specify either "device", "hwaddr" or "kernelpath"`)) Expect(err).To(MatchError(`specify either "device", "hwaddr", "kernelpath" or "pciBusID"`))
}) })