| 1 | // Copyright 2015 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/ |
| 16 | |
| 17 | import ( |
| 18 | "bytesscip-go gomod github.com/golang/go/src go1.24.2 bytes/" |
| 19 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/" |
| 20 | "errorsscip-go gomod github.com/golang/go/src go1.24.2 errors/" |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/" |
| 22 | "netscip-go gomod github.com/golang/go/src go1.24.2 net/" |
| 23 | "osscip-go gomod github.com/golang/go/src go1.24.2 os/" |
| 24 | "path/filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/" |
| 25 | "runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/" |
| 26 | "stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/" |
| 27 | |
| 28 | "github.com/vishvananda/netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/" |
| 29 | |
| 30 | "github.com/containernetworking/cni/pkg/skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/" |
| 31 | "github.com/containernetworking/cni/pkg/typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/" |
| 32 | currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/ "github.com/containernetworking/cni/pkg/types/100scip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/" |
| 33 | "github.com/containernetworking/cni/pkg/versionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/" |
| 34 | "github.com/containernetworking/plugins/pkg/ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/" |
| 35 | "github.com/containernetworking/plugins/pkg/ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/" |
| 36 | "github.com/containernetworking/plugins/pkg/netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/" |
| 37 | "github.com/containernetworking/plugins/pkg/nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/" |
| 38 | bvscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/buildversion`/ "github.com/containernetworking/plugins/pkg/utils/buildversionscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/buildversion`/" |
| 39 | ) |
| 40 | |
| 41 | var ( |
| 42 | sysBusPCIscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/sysBusPCI. = "/sys/bus/pci/devices" |
| 43 | sysBusAuxiliaryscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/sysBusAuxiliary. = "/sys/bus/auxiliary/devices" |
| 44 | ) |
| 45 | |
| 46 | // Array of different linux drivers bound to network device needed for DPDK |
| 47 | var userspaceDriversscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/userspaceDrivers. = []string{"vfio-pci", "uio_pci_generic", "igb_uio"} |
| 48 | |
| 49 | // NetConf for host-device config, look the README to learn how to use those parameters |
| 50 | type NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf# struct { |
| 51 | typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#NetConf. |
| 52 | Devicescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#Device. string `json:"device"` // Device-Name, something like eth0 or can0 etc. |
| 53 | HWAddrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#HWAddr. string `json:"hwaddr"` // MAC Address of target network interface |
| 54 | DPDKModescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#DPDKMode. bool |
| 55 | KernelPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#KernelPath. string `json:"kernelpath"` // Kernelpath of the device |
| 56 | PCIAddrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#PCIAddr. string `json:"pciBusID"` // PCI Address of target network device |
| 57 | RuntimeConfigscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#RuntimeConfig. struct { |
| 58 | DeviceIDscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#$anon_24ccfef1da1456c2#DeviceID. string `json:"deviceID,omitempty"` |
| 59 | } `json:"runtimeConfig,omitempty"` |
| 60 | |
| 61 | // for internal use |
| 62 | auxDevicescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#auxDevice. string `json:"-"` // Auxiliary device name as appears on Auxiliary bus (/sys/bus/auxiliary) |
| 63 | } |
| 64 | |
| 65 | func initscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/init().() { |
| 66 | // this ensures that main runs only on main thread (thread group leader). |
| 67 | // since namespace ops (unshare, setns) are done for a single thread, we |
| 68 | // must ensure that the goroutine does not jump from OS thread to thread |
| 69 | runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/.LockOSThreadscip-go gomod github.com/golang/go/src go1.24.2 runtime/LockOSThread().() |
| 70 | } |
| 71 | |
| 72 | // handleDeviceID updates netconf fields with DeviceID runtime config |
| 73 | func handleDeviceIDscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/handleDeviceID().(netconf *NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#) error { |
| 74 | deviceID := netconf.RuntimeConfigscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#RuntimeConfig..DeviceIDscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#$anon_24ccfef1da1456c2#DeviceID. |
| 75 | if deviceID == "" { |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | // Check if deviceID is a PCI device |
| 80 | pciPath := filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/Join().(sysBusPCIscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/sysBusPCI., deviceID) |
| 81 | if _, err := osscip-go gomod github.com/golang/go/src go1.24.2 os/.Statscip-go gomod github.com/golang/go/src go1.24.2 os/Stat().(pciPath); err == nil { |
| 82 | netconf.PCIAddrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#PCIAddr. = deviceID |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | // Check if deviceID is an Auxiliary device |
| 87 | auxPath := filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/Join().(sysBusAuxiliaryscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/sysBusAuxiliary., deviceID) |
| 88 | if _, err := osscip-go gomod github.com/golang/go/src go1.24.2 os/.Statscip-go gomod github.com/golang/go/src go1.24.2 os/Stat().(auxPath); err == nil { |
| 89 | netconf.PCIAddrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#PCIAddr. = "" |
| 90 | netconf.auxDevicescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#auxDevice. = deviceID |
| 91 | return nil |
| 92 | } |
| 93 | |
| 94 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("runtime config DeviceID %s not found or unsupported", deviceID) |
| 95 | } |
| 96 | |
| 97 | func loadConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/loadConf().(bytes []byte) (*NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#, error) { |
| 98 | n := &NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#{} |
| 99 | var err error |
| 100 | if err = jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/Unmarshal().(bytes, n); err != nil { |
| 101 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to load netconf: %v", err) |
| 102 | } |
| 103 | |
| 104 | // Override device with the standardized DeviceID if provided in Runtime Config. |
| 105 | if err := handleDeviceIDscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/handleDeviceID().(n); err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | |
| 109 | if n.Devicescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#Device. == "" && n.HWAddrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#HWAddr. == "" && n.KernelPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#KernelPath. == "" && n.PCIAddrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#PCIAddr. == "" && n.auxDevicescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#auxDevice. == "" { |
| 110 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().(`specify either "device", "hwaddr", "kernelpath" or "pciBusID"`) |
| 111 | } |
| 112 | |
| 113 | if len(n.PCIAddrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#PCIAddr.) > 0 { |
| 114 | n.DPDKModescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#DPDKMode., err = hasDpdkDriverscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/hasDpdkDriver().(n.PCIAddrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#PCIAddr.) |
| 115 | if err != nil { |
| 116 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("error with host device: %v", err) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return n, nil |
| 121 | } |
| 122 | |
| 123 | func cmdAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/cmdAdd().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error { |
| 124 | cfg, err := loadConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/loadConf().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | containerNs, err := nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.GetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/GetNS().(args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns.) |
| 129 | if err != nil { |
| 130 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to open netns %q: %v", args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns., err) |
| 131 | } |
| 132 | defer containerNs.Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.() |
| 133 | |
| 134 | result := ¤tscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#{} |
| 135 | result.Interfacesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Interfaces. = []*currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Interfacescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#{{ |
| 136 | Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name.: args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName., |
| 137 | Sandboxscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Sandbox.: containerNs.Pathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Path.(), |
| 138 | }} |
| 139 | |
| 140 | var contDev netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link# |
| 141 | if !cfg.DPDKModescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#DPDKMode. { |
| 142 | hostDev, err := getLinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/getLink().(cfg.Devicescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#Device., cfg.HWAddrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#HWAddr., cfg.KernelPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#KernelPath., cfg.PCIAddrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#PCIAddr., cfg.auxDevicescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#auxDevice.) |
| 143 | if err != nil { |
| 144 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to find host device: %v", err) |
| 145 | } |
| 146 | |
| 147 | contDev, err = moveLinkInscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/moveLinkIn().(hostDev, containerNs, args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.) |
| 148 | if err != nil { |
| 149 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to move link %v", err) |
| 150 | } |
| 151 | |
| 152 | // Override the device name with the name in the container namespace |
| 153 | result.Interfacesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Interfaces.[0].Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name. = contDev.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name. |
| 154 | // Set the MAC address of the interface |
| 155 | result.Interfacesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Interfaces.[0].Macscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Mac. = contDev.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().HardwareAddrscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#HardwareAddr..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/HardwareAddr#String().() |
| 156 | } |
| 157 | |
| 158 | if cfg.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type. == "" { |
| 159 | if cfg.DPDKModescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#DPDKMode. { |
| 160 | return typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.PrintResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PrintResult().(result, cfg.CNIVersionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#CNIVersion.) |
| 161 | } |
| 162 | return printLinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/printLink().(contDev, cfg.CNIVersionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#CNIVersion., containerNs) |
| 163 | } |
| 164 | |
| 165 | // run the IPAM plugin and get back the config to apply |
| 166 | r, err := ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ExecAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ExecAdd().(cfg.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type., args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.) |
| 167 | if err != nil { |
| 168 | return err |
| 169 | } |
| 170 | |
| 171 | // Invoke ipam del if err to avoid ip leak |
| 172 | defer func() { |
| 173 | if err != nil { |
| 174 | ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ExecDelscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ExecDel().(cfg.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type., args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.) |
| 175 | } |
| 176 | }() |
| 177 | |
| 178 | // Convert whatever the IPAM result was into the current Result type |
| 179 | newResult, err := currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.NewResultFromResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/NewResultFromResult().(r) |
| 180 | if err != nil { |
| 181 | return err |
| 182 | } |
| 183 | |
| 184 | if len(newResult.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs.) == 0 { |
| 185 | return errorsscip-go gomod github.com/golang/go/src go1.24.2 errors/.Newscip-go gomod github.com/golang/go/src go1.24.2 errors/New().("IPAM plugin returned missing IP config") |
| 186 | } |
| 187 | |
| 188 | for _, ipc := range newResult.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs. { |
| 189 | // All addresses apply to the container interface (move from host) |
| 190 | ipc.Interfacescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#Interface. = currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Intscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Int().(0) |
| 191 | } |
| 192 | |
| 193 | newResult.Interfacesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Interfaces. = result.Interfacesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Interfaces. |
| 194 | |
| 195 | if !cfg.DPDKModescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#DPDKMode. { |
| 196 | err = containerNs.Doscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Do.(func(_ nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error { |
| 197 | return ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ConfigureIfacescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ConfigureIface().(args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName., newResult) |
| 198 | }) |
| 199 | if err != nil { |
| 200 | return err |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | newResult.DNSscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#DNS. = cfg.DNSscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#DNS. |
| 205 | |
| 206 | return typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.PrintResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PrintResult().(newResult, cfg.CNIVersionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#CNIVersion.) |
| 207 | } |
| 208 | |
| 209 | func cmdDelscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/cmdDel().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error { |
| 210 | cfg, err := loadConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/loadConf().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.) |
| 211 | if err != nil { |
| 212 | return err |
| 213 | } |
| 214 | if args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns. == "" { |
| 215 | return nil |
| 216 | } |
| 217 | containerNs, err := nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.GetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/GetNS().(args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns.) |
| 218 | if err != nil { |
| 219 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to open netns %q: %v", args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns., err) |
| 220 | } |
| 221 | defer containerNs.Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.() |
| 222 | |
| 223 | if cfg.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type. != "" { |
| 224 | if err := ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ExecDelscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ExecDel().(cfg.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type., args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.); err != nil { |
| 225 | return err |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if !cfg.DPDKModescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#DPDKMode. { |
| 230 | if err := moveLinkOutscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/moveLinkOut().(containerNs, args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.); err != nil { |
| 231 | return err |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return nil |
| 236 | } |
| 237 | |
| 238 | func moveLinkInscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/moveLinkIn().(hostDev netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#, containerNs nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#, containerIfName string) (netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#, error) { |
| 239 | hostDevName := hostDev.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name. |
| 240 | |
| 241 | // With recent kernels we could do all changes in a single netlink call, |
| 242 | // but on failure the device is left in a partially modified state. |
| 243 | // Doing changes one by one allow us to (try to) rollback to the initial state. |
| 244 | |
| 245 | // Create a temporary namespace to rename (and modify) the device in. |
| 246 | // We were previously using a temporary name, but rapid rename leads to |
| 247 | // race condition with udev and NetworkManager. |
| 248 | tempNS, err := nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.TempNetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/TempNetNS().() |
| 249 | if err != nil { |
| 250 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to create tempNS: %v", err) |
| 251 | } |
| 252 | defer tempNS.Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.() |
| 253 | |
| 254 | // Restore original up state in case of error |
| 255 | // This must be done in the hostNS as moving |
| 256 | // device between namespaces sets the link down |
| 257 | if hostDev.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Flagsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Flags.&netscip-go gomod github.com/golang/go/src go1.24.2 net/.FlagUpscip-go gomod github.com/golang/go/src go1.24.2 net/FlagUp. == netscip-go gomod github.com/golang/go/src go1.24.2 net/.FlagUpscip-go gomod github.com/golang/go/src go1.24.2 net/FlagUp. { |
| 258 | defer func() { |
| 259 | if err != nil { |
| 260 | // lookup the device again (index might have changed) |
| 261 | if hostDev, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(hostDevName); err == nil { |
| 262 | _ = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetUpscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetUp().(hostDev) |
| 263 | } |
| 264 | } |
| 265 | }() |
| 266 | } |
| 267 | |
| 268 | // Move the host device into tempNS |
| 269 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNsFdscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetNsFd().(hostDev, int(tempNS.Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Fd.())); err != nil { |
| 270 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to move %q to tempNS: %v", hostDevName, err) |
| 271 | } |
| 272 | |
| 273 | var contDev netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link# |
| 274 | |
| 275 | // In a container in container scenario, hostNS is not the initial net namespace, |
| 276 | // but host / container naming is easier to follow. |
| 277 | if err = tempNS.Doscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Do.(func(hostNS nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error { |
| 278 | // lookup the device in tempNS (index might have changed) |
| 279 | tempNSDev, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(hostDevName) |
| 280 | if err != nil { |
| 281 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to find %q in tempNS: %v", hostDevName, err) |
| 282 | } |
| 283 | |
| 284 | // detroying a non empty tempNS would move physical devices back to the initial net namespace, |
| 285 | // not the namespace of the "parent" process, and virtual devices would be destroyed, |
| 286 | // so we need to actively move the device back to hostNS on error |
| 287 | defer func() { |
| 288 | if err != nil && tempNSDev != nil { |
| 289 | _ = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNsFdscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetNsFd().(tempNSDev, int(hostNS.Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Fd.())) |
| 290 | } |
| 291 | }() |
| 292 | |
| 293 | // Rename the device to the wanted name |
| 294 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNamescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetName().(tempNSDev, containerIfName); err != nil { |
| 295 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to rename host device %q to %q: %v", hostDevName, containerIfName, err) |
| 296 | } |
| 297 | |
| 298 | // Restore the original device name in case of error |
| 299 | defer func() { |
| 300 | if err != nil && tempNSDev != nil { |
| 301 | _ = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNamescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetName().(tempNSDev, hostDevName) |
| 302 | } |
| 303 | }() |
| 304 | |
| 305 | // Save host device name into the container device's alias property |
| 306 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetAliasscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetAlias().(tempNSDev, hostDevName); err != nil { |
| 307 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to set alias to %q: %v", hostDevName, err) |
| 308 | } |
| 309 | |
| 310 | // Remove the alias on error |
| 311 | defer func() { |
| 312 | if err != nil && tempNSDev != nil { |
| 313 | _ = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetAliasscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetAlias().(tempNSDev, "") |
| 314 | } |
| 315 | }() |
| 316 | |
| 317 | // Move the device to the containerNS |
| 318 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNsFdscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetNsFd().(tempNSDev, int(containerNs.Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Fd.())); err != nil { |
| 319 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to move %q (host: %q) to container NS: %v", containerIfName, hostDevName, err) |
| 320 | } |
| 321 | |
| 322 | // Lookup the device again on error, the index might have changed |
| 323 | defer func() { |
| 324 | if err != nil { |
| 325 | tempNSDev, _ = netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(containerIfName) |
| 326 | } |
| 327 | }() |
| 328 | |
| 329 | err = containerNs.Doscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Do.(func(_ nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error { |
| 330 | var err error |
| 331 | contDev, err = netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(containerIfName) |
| 332 | if err != nil { |
| 333 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to find %q in container NS: %v", containerIfName, err) |
| 334 | } |
| 335 | |
| 336 | // Move the interface back to tempNS on error |
| 337 | defer func() { |
| 338 | if err != nil { |
| 339 | _ = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNsFdscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetNsFd().(contDev, int(tempNS.Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Fd.())) |
| 340 | } |
| 341 | }() |
| 342 | |
| 343 | // Bring the device up |
| 344 | // This must be done in the containerNS |
| 345 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetUpscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetUp().(contDev); err != nil { |
| 346 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to set %q up: %v", containerIfName, err) |
| 347 | } |
| 348 | |
| 349 | return nil |
| 350 | }) |
| 351 | |
| 352 | return err |
| 353 | }); err != nil { |
| 354 | return nil, err |
| 355 | } |
| 356 | |
| 357 | return contDev, nil |
| 358 | } |
| 359 | |
| 360 | func moveLinkOutscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/moveLinkOut().(containerNs nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#, containerIfName string) error { |
| 361 | // Create a temporary namespace to rename (and modify) the device in. |
| 362 | // We were previously using a temporary name, but multiple rapid renames |
| 363 | // leads to race condition with udev and NetworkManager. |
| 364 | tempNS, err := nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.TempNetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/TempNetNS().() |
| 365 | if err != nil { |
| 366 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to create tempNS: %v", err) |
| 367 | } |
| 368 | defer tempNS.Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.() |
| 369 | |
| 370 | var contDev netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link# |
| 371 | |
| 372 | // Restore original up state in case of error |
| 373 | // This must be done in the containerNS as moving |
| 374 | // device between namespaces sets the link down |
| 375 | defer func() { |
| 376 | if err != nil && contDev != nil && contDev.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Flagsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Flags.&netscip-go gomod github.com/golang/go/src go1.24.2 net/.FlagUpscip-go gomod github.com/golang/go/src go1.24.2 net/FlagUp. == netscip-go gomod github.com/golang/go/src go1.24.2 net/.FlagUpscip-go gomod github.com/golang/go/src go1.24.2 net/FlagUp. { |
| 377 | containerNs.Doscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Do.(func(_ nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error { |
| 378 | // lookup the device again (index might have changed) |
| 379 | if contDev, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(containerIfName); err == nil { |
| 380 | _ = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetUpscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetUp().(contDev) |
| 381 | } |
| 382 | return nil |
| 383 | }) |
| 384 | } |
| 385 | }() |
| 386 | |
| 387 | err = containerNs.Doscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Do.(func(_ nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error { |
| 388 | var err error |
| 389 | // Lookup the device in the containerNS |
| 390 | contDev, err = netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(containerIfName) |
| 391 | if err != nil { |
| 392 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to find %q in containerNS: %v", containerIfName, err) |
| 393 | } |
| 394 | |
| 395 | // Verify we have the original name |
| 396 | if contDev.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Aliasscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Alias. == "" { |
| 397 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to find original ifname for %q (alias is not set)", containerIfName) |
| 398 | } |
| 399 | |
| 400 | // Move the device to the tempNS |
| 401 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNsFdscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetNsFd().(contDev, int(tempNS.Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Fd.())); err != nil { |
| 402 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to move %q to tempNS: %v", containerIfName, err) |
| 403 | } |
| 404 | return nil |
| 405 | }) |
| 406 | if err != nil { |
| 407 | return err |
| 408 | } |
| 409 | |
| 410 | err = tempNS.Doscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Do.(func(hostNS nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error { |
| 411 | // Lookup the device in tempNS (index might have changed) |
| 412 | tempNSDev, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(containerIfName) |
| 413 | if err != nil { |
| 414 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to find %q in tempNS: %v", containerIfName, err) |
| 415 | } |
| 416 | |
| 417 | // Move the device back to containerNS on error |
| 418 | defer func() { |
| 419 | if err != nil { |
| 420 | _ = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNsFdscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetNsFd().(tempNSDev, int(containerNs.Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Fd.())) |
| 421 | } |
| 422 | }() |
| 423 | |
| 424 | hostDevName := tempNSDev.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Aliasscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Alias. |
| 425 | |
| 426 | // Rename container device to hostDevName |
| 427 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNamescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetName().(tempNSDev, hostDevName); err != nil { |
| 428 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to rename device %q to %q: %v", containerIfName, hostDevName, err) |
| 429 | } |
| 430 | |
| 431 | // Rename the device back to containerIfName on error |
| 432 | defer func() { |
| 433 | if err != nil { |
| 434 | _ = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNamescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetName().(tempNSDev, containerIfName) |
| 435 | } |
| 436 | }() |
| 437 | |
| 438 | // Unset device's alias property |
| 439 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetAliasscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetAlias().(tempNSDev, ""); err != nil { |
| 440 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to unset alias of %q: %v", hostDevName, err) |
| 441 | } |
| 442 | |
| 443 | // Set back the device alias to hostDevName on error |
| 444 | defer func() { |
| 445 | if err != nil { |
| 446 | _ = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetAliasscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetAlias().(tempNSDev, hostDevName) |
| 447 | } |
| 448 | }() |
| 449 | |
| 450 | // Finally move the device to the hostNS |
| 451 | if err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkSetNsFdscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkSetNsFd().(tempNSDev, int(hostNS.Fdscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Fd.())); err != nil { |
| 452 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to move %q to hostNS: %v", hostDevName, err) |
| 453 | } |
| 454 | |
| 455 | // As we don't know the previous state, leave the link down |
| 456 | |
| 457 | return nil |
| 458 | }) |
| 459 | if err != nil { |
| 460 | return err |
| 461 | } |
| 462 | |
| 463 | return nil |
| 464 | } |
| 465 | |
| 466 | func hasDpdkDriverscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/hasDpdkDriver().(pciaddr string) (bool, error) { |
| 467 | driverLink := filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/Join().(sysBusPCIscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/sysBusPCI., pciaddr, "driver") |
| 468 | driverPath, err := filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.EvalSymlinksscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/EvalSymlinks().(driverLink) |
| 469 | if err != nil { |
| 470 | return false, err |
| 471 | } |
| 472 | driverStat, err := osscip-go gomod github.com/golang/go/src go1.24.2 os/.Statscip-go gomod github.com/golang/go/src go1.24.2 os/Stat().(driverPath) |
| 473 | if err != nil { |
| 474 | return false, err |
| 475 | } |
| 476 | driverName := driverStat.Namescip-go gomod github.com/golang/go/src go1.24.2 `io/fs`/FileInfo#Name().() |
| 477 | for _, drv := range userspaceDriversscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/userspaceDrivers. { |
| 478 | if driverName == drv { |
| 479 | return true, nil |
| 480 | } |
| 481 | } |
| 482 | return false, nil |
| 483 | } |
| 484 | |
| 485 | func printLinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/printLink().(dev netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#, cniVersion string, containerNs nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error { |
| 486 | result := currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#{ |
| 487 | CNIVersionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#CNIVersion.: currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.ImplementedSpecVersionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/ImplementedSpecVersion., |
| 488 | Interfacesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Interfaces.: []*currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Interfacescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#{ |
| 489 | { |
| 490 | Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name.: dev.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name., |
| 491 | Macscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Mac.: dev.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().HardwareAddrscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#HardwareAddr..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/HardwareAddr#String().(), |
| 492 | Sandboxscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Sandbox.: containerNs.Pathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Path.(), |
| 493 | }, |
| 494 | }, |
| 495 | } |
| 496 | return typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.PrintResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PrintResult().(&result, cniVersion) |
| 497 | } |
| 498 | |
| 499 | func linkFromPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/linkFromPath().(path string) (netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#, error) { |
| 500 | entries, err := osscip-go gomod github.com/golang/go/src go1.24.2 os/.ReadDirscip-go gomod github.com/golang/go/src go1.24.2 os/ReadDir().(path) |
| 501 | if err != nil { |
| 502 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to read directory %s: %q", path, err) |
| 503 | } |
| 504 | if len(entries) > 0 { |
| 505 | // grab the first net device |
| 506 | return netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(entries[0].Namescip-go gomod github.com/golang/go/src go1.24.2 `io/fs`/DirEntry#Name().()) |
| 507 | } |
| 508 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to find network device in path %s", path) |
| 509 | } |
| 510 | |
| 511 | func getLinkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/getLink().(devname, hwaddr, kernelpath, pciaddr string, auxDev string) (netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#, error) { |
| 512 | switch { |
| 513 | |
| 514 | case len(devname) > 0: |
| 515 | return netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(devname) |
| 516 | case len(hwaddr) > 0: |
| 517 | hwAddr, err := netscip-go gomod github.com/golang/go/src go1.24.2 net/.ParseMACscip-go gomod github.com/golang/go/src go1.24.2 net/ParseMAC().(hwaddr) |
| 518 | if err != nil { |
| 519 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to parse MAC address %q: %v", hwaddr, err) |
| 520 | } |
| 521 | |
| 522 | links, err := netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkListscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkList().() |
| 523 | if err != nil { |
| 524 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to list node links: %v", err) |
| 525 | } |
| 526 | |
| 527 | for _, link := range links { |
| 528 | if bytesscip-go gomod github.com/golang/go/src go1.24.2 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.2 bytes/Equal().(link.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().HardwareAddrscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#HardwareAddr., hwAddr) { |
| 529 | return link, nil |
| 530 | } |
| 531 | } |
| 532 | case len(kernelpath) > 0: |
| 533 | if !filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.IsAbsscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/IsAbs().(kernelpath) || !stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.24.2 strings/HasPrefix().(kernelpath, "/sys/devices/") { |
| 534 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("kernel device path %q must be absolute and begin with /sys/devices/", kernelpath) |
| 535 | } |
| 536 | netDir := filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/Join().(kernelpath, "net") |
| 537 | return linkFromPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/linkFromPath().(netDir) |
| 538 | case len(pciaddr) > 0: |
| 539 | netDir := filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/Join().(sysBusPCIscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/sysBusPCI., pciaddr, "net") |
| 540 | if _, err := osscip-go gomod github.com/golang/go/src go1.24.2 os/.Lstatscip-go gomod github.com/golang/go/src go1.24.2 os/Lstat().(netDir); err != nil { |
| 541 | virtioNetDir := filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/Join().(sysBusPCIscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/sysBusPCI., pciaddr, "virtio*", "net") |
| 542 | matches, err := filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.Globscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/Glob().(virtioNetDir) |
| 543 | if matches == nil || err != nil { |
| 544 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("no net directory under pci device %s", pciaddr) |
| 545 | } |
| 546 | netDir = matches[0] |
| 547 | } |
| 548 | return linkFromPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/linkFromPath().(netDir) |
| 549 | case len(auxDev) > 0: |
| 550 | netDir := filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/Join().(sysBusAuxiliaryscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/sysBusAuxiliary., auxDev, "net") |
| 551 | return linkFromPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/linkFromPath().(netDir) |
| 552 | } |
| 553 | |
| 554 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to find physical interface") |
| 555 | } |
| 556 | |
| 557 | func mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/main().() { |
| 558 | skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.PluginMainFuncsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/PluginMainFuncs().(skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CNIFuncsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#{ |
| 559 | Addscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Add.: cmdAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/cmdAdd()., |
| 560 | Checkscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Check.: cmdCheckscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/cmdCheck()., |
| 561 | Delscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Del.: cmdDelscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/cmdDel()., |
| 562 | Statusscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Status.: cmdStatusscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/cmdStatus()., |
| 563 | /* FIXME GC */ |
| 564 | }, versionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/.Allscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/All., bvscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/buildversion`/.BuildStringscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/buildversion`/BuildString().("host-device")) |
| 565 | } |
| 566 | |
| 567 | func cmdCheckscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/cmdCheck().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error { |
| 568 | cfg, err := loadConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/loadConf().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.) |
| 569 | if err != nil { |
| 570 | return err |
| 571 | } |
| 572 | netns, err := nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.GetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/GetNS().(args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns.) |
| 573 | if err != nil { |
| 574 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to open netns %q: %v", args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns., err) |
| 575 | } |
| 576 | defer netns.Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.() |
| 577 | |
| 578 | // run the IPAM plugin and get back the config to apply |
| 579 | if cfg.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type. != "" { |
| 580 | err = ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ExecCheckscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ExecCheck().(cfg.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type., args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.) |
| 581 | if err != nil { |
| 582 | return err |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // Parse previous result. |
| 587 | if cfg.NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#NetConf..RawPrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#RawPrevResult. == nil { |
| 588 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Required prevResult missing") |
| 589 | } |
| 590 | |
| 591 | if err := versionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/.ParsePrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/ParsePrevResult().(&cfg.NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#NetConf.); err != nil { |
| 592 | return err |
| 593 | } |
| 594 | |
| 595 | result, err := currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.NewResultFromResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/NewResultFromResult().(cfg.PrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#PrevResult.) |
| 596 | if err != nil { |
| 597 | return err |
| 598 | } |
| 599 | |
| 600 | if cfg.DPDKModescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#DPDKMode. { |
| 601 | return nil |
| 602 | } |
| 603 | |
| 604 | var contMap currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Interfacescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface# |
| 605 | // Find interfaces for name we know, that of host-device inside container |
| 606 | for _, intf := range result.Interfacesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Interfaces. { |
| 607 | if args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName. == intf.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name. { |
| 608 | if args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns. == intf.Sandboxscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Sandbox. { |
| 609 | contMap = *intf |
| 610 | continue |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // The namespace must be the same as what was configured |
| 616 | if args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns. != contMap.Sandboxscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Sandbox. { |
| 617 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Sandbox in prevResult %s doesn't match configured netns: %s", |
| 618 | contMap.Sandboxscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Sandbox., args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns.) |
| 619 | } |
| 620 | |
| 621 | // |
| 622 | // Check prevResults for ips, routes and dns against values found in the container |
| 623 | if err := netns.Doscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Do.(func(_ nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error { |
| 624 | // Check interface against values found in the container |
| 625 | err := validateCniContainerInterfacescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/validateCniContainerInterface().(contMap) |
| 626 | if err != nil { |
| 627 | return err |
| 628 | } |
| 629 | |
| 630 | err = ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.ValidateExpectedInterfaceIPsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ValidateExpectedInterfaceIPs().(args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName., result.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs.) |
| 631 | if err != nil { |
| 632 | return err |
| 633 | } |
| 634 | |
| 635 | err = ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.ValidateExpectedRoutescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ValidateExpectedRoute().(result.Routesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Routes.) |
| 636 | if err != nil { |
| 637 | return err |
| 638 | } |
| 639 | return nil |
| 640 | }); err != nil { |
| 641 | return err |
| 642 | } |
| 643 | |
| 644 | // |
| 645 | return nil |
| 646 | } |
| 647 | |
| 648 | func validateCniContainerInterfacescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/validateCniContainerInterface().(intf currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Interfacescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#) error { |
| 649 | var link netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.Linkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link# |
| 650 | var err error |
| 651 | |
| 652 | if intf.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name. == "" { |
| 653 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Container interface name missing in prevResult: %v", intf.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name.) |
| 654 | } |
| 655 | link, err = netlinksafescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/.LinkByNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/netlinksafe`/LinkByName().(intf.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name.) |
| 656 | if err != nil { |
| 657 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Container Interface name in prevResult: %s not found", intf.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name.) |
| 658 | } |
| 659 | if intf.Sandboxscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Sandbox. == "" { |
| 660 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Error: Container interface %s should not be in host namespace", link.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name.) |
| 661 | } |
| 662 | |
| 663 | if intf.Macscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Mac. != "" { |
| 664 | if intf.Macscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Mac. != link.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().HardwareAddrscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#HardwareAddr..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/HardwareAddr#String().() { |
| 665 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Interface %s Mac %s doesn't match container Mac: %s", intf.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Name., intf.Macscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Interface#Mac., link.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().HardwareAddrscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#HardwareAddr.) |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | return nil |
| 670 | } |
| 671 | |
| 672 | func cmdStatusscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/cmdStatus().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error { |
| 673 | conf := NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/main/host-device`/NetConf#{} |
| 674 | if err := jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/Unmarshal().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData., &conf); err != nil { |
| 675 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to load netconf: %w", err) |
| 676 | } |
| 677 | |
| 678 | if conf.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type. != "" { |
| 679 | if err := ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ExecStatusscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ExecStatus().(conf.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type., args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.); err != nil { |
| 680 | return err |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | // TODO: Check if host device exists. |
| 685 | |
| 686 | return nil |
| 687 | } |