| 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 main |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "encoding/json" |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "net" |
| 23 | "os" |
| 24 | "path/filepath" |
| 25 | "runtime" |
| 26 | "strings" |
| 27 | |
| 28 | "github.com/vishvananda/netlink" |
| 29 | |
| 30 | "github.com/containernetworking/cni/pkg/skel" |
| 31 | "github.com/containernetworking/cni/pkg/types" |
| 32 | current "github.com/containernetworking/cni/pkg/types/100" |
| 33 | "github.com/containernetworking/cni/pkg/version" |
| 34 | "github.com/containernetworking/plugins/pkg/ip" |
| 35 | "github.com/containernetworking/plugins/pkg/ipam" |
| 36 | "github.com/containernetworking/plugins/pkg/netlinksafe" |
| 37 | "github.com/containernetworking/plugins/pkg/ns" |
| 38 | bv "github.com/containernetworking/plugins/pkg/utils/buildversion" |
| 39 | ) |
| 40 | |
| 41 | var ( |
| 42 | sysBusPCI = "/sys/bus/pci/devices" |
| 43 | sysBusAuxiliary = "/sys/bus/auxiliary/devices" |
| 44 | ) |
| 45 | |
| 46 | // Array of different linux drivers bound to network device needed for DPDK |
| 47 | var 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 NetConf struct { |
| 51 | types.NetConf |
| 52 | Device string `json:"device"` // Device-Name, something like eth0 or can0 etc. |
| 53 | HWAddr string `json:"hwaddr"` // MAC Address of target network interface |
| 54 | DPDKMode bool |
| 55 | KernelPath string `json:"kernelpath"` // Kernelpath of the device |
| 56 | PCIAddr string `json:"pciBusID"` // PCI Address of target network device |
| 57 | RuntimeConfig struct { |
| 58 | DeviceID string `json:"deviceID,omitempty"` |
| 59 | } `json:"runtimeConfig,omitempty"` |
| 60 | |
| 61 | // for internal use |
| 62 | auxDevice string `json:"-"` // Auxiliary device name as appears on Auxiliary bus (/sys/bus/auxiliary) |
| 63 | } |
| 64 | |
| 65 | func 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 | runtime.LockOSThread() |
| 70 | } |
| 71 | |
| 72 | // handleDeviceID updates netconf fields with DeviceID runtime config |
| 73 | func handleDeviceID(netconf *NetConf) error { |
| 74 | deviceID := netconf.RuntimeConfig.DeviceID |
| 75 | if deviceID == "" { |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | // Check if deviceID is a PCI device |
| 80 | pciPath := filepath.Join(sysBusPCI, deviceID) |
| 81 | if _, err := os.Stat(pciPath); err == nil { |
| 82 | netconf.PCIAddr = deviceID |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | // Check if deviceID is an Auxiliary device |
| 87 | auxPath := filepath.Join(sysBusAuxiliary, deviceID) |
| 88 | if _, err := os.Stat(auxPath); err == nil { |
| 89 | netconf.PCIAddr = "" |
| 90 | netconf.auxDevice = deviceID |
| 91 | return nil |
| 92 | } |
| 93 | |
| 94 | return fmt.Errorf("runtime config DeviceID %s not found or unsupported", deviceID) |
| 95 | } |
| 96 | |
| 97 | func loadConf(bytes []byte) (*NetConf, error) { |
| 98 | n := &NetConf{} |
| 99 | var err error |
| 100 | if err = json.Unmarshal(bytes, n); err != nil { |
| 101 | return nil, 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 := handleDeviceID(n); err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | |
| 109 | if n.Device == "" && n.HWAddr == "" && n.KernelPath == "" && n.PCIAddr == "" && n.auxDevice == "" { |
| 110 | return nil, fmt.Errorf(`specify either "device", "hwaddr", "kernelpath" or "pciBusID"`) |
| 111 | } |
| 112 | |
| 113 | if len(n.PCIAddr) > 0 { |
| 114 | n.DPDKMode, err = hasDpdkDriver(n.PCIAddr) |
| 115 | if err != nil { |
| 116 | return nil, fmt.Errorf("error with host device: %v", err) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return n, nil |
| 121 | } |
| 122 | |
| 123 | func cmdAdd(args *skel.CmdArgs) error { |
| 124 | cfg, err := loadConf(args.StdinData) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | containerNs, err := ns.GetNS(args.Netns) |
| 129 | if err != nil { |
| 130 | return fmt.Errorf("failed to open netns %q: %v", args.Netns, err) |
| 131 | } |
| 132 | defer containerNs.Close() |
| 133 | |
| 134 | result := ¤t.Result{} |
| 135 | result.Interfaces = []*current.Interface{{ |
| 136 | Name: args.IfName, |
| 137 | Sandbox: containerNs.Path(), |
| 138 | }} |
| 139 | |
| 140 | var contDev netlink.Link |
| 141 | if !cfg.DPDKMode { |
| 142 | hostDev, err := getLink(cfg.Device, cfg.HWAddr, cfg.KernelPath, cfg.PCIAddr, cfg.auxDevice) |
| 143 | if err != nil { |
| 144 | return fmt.Errorf("failed to find host device: %v", err) |
| 145 | } |
| 146 | |
| 147 | contDev, err = moveLinkIn(hostDev, containerNs, args.IfName) |
| 148 | if err != nil { |
| 149 | return fmt.Errorf("failed to move link %v", err) |
| 150 | } |
| 151 | |
| 152 | // Override the device name with the name in the container namespace |
| 153 | result.Interfaces[0].Name = contDev.Attrs().Name |
| 154 | // Set the MAC address of the interface |
| 155 | result.Interfaces[0].Mac = contDev.Attrs().HardwareAddr.String() |
| 156 | } |
| 157 | |
| 158 | if cfg.IPAM.Type == "" { |
| 159 | if cfg.DPDKMode { |
| 160 | return types.PrintResult(result, cfg.CNIVersion) |
| 161 | } |
| 162 | return printLink(contDev, cfg.CNIVersion, containerNs) |
| 163 | } |
| 164 | |
| 165 | // run the IPAM plugin and get back the config to apply |
| 166 | r, err := ipam.ExecAdd(cfg.IPAM.Type, args.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 | ipam.ExecDel(cfg.IPAM.Type, args.StdinData) |
| 175 | } |
| 176 | }() |
| 177 | |
| 178 | // Convert whatever the IPAM result was into the current Result type |
| 179 | newResult, err := current.NewResultFromResult(r) |
| 180 | if err != nil { |
| 181 | return err |
| 182 | } |
| 183 | |
| 184 | if len(newResult.IPs) == 0 { |
| 185 | return errors.New("IPAM plugin returned missing IP config") |
| 186 | } |
| 187 | |
| 188 | for _, ipc := range newResult.IPs { |
| 189 | // All addresses apply to the container interface (move from host) |
| 190 | ipc.Interface = current.Int(0) |
| 191 | } |
| 192 | |
| 193 | newResult.Interfaces = result.Interfaces |
| 194 | |
| 195 | if !cfg.DPDKMode { |
| 196 | err = containerNs.Do(func(_ ns.NetNS) error { |
| 197 | return ipam.ConfigureIface(args.IfName, newResult) |
| 198 | }) |
| 199 | if err != nil { |
| 200 | return err |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | newResult.DNS = cfg.DNS |
| 205 | |
| 206 | return types.PrintResult(newResult, cfg.CNIVersion) |
| 207 | } |
| 208 | |
| 209 | func cmdDel(args *skel.CmdArgs) error { |
| 210 | cfg, err := loadConf(args.StdinData) |
| 211 | if err != nil { |
| 212 | return err |
| 213 | } |
| 214 | if args.Netns == "" { |
| 215 | return nil |
| 216 | } |
| 217 | containerNs, err := ns.GetNS(args.Netns) |
| 218 | if err != nil { |
| 219 | return fmt.Errorf("failed to open netns %q: %v", args.Netns, err) |
| 220 | } |
| 221 | defer containerNs.Close() |
| 222 | |
| 223 | if cfg.IPAM.Type != "" { |
| 224 | if err := ipam.ExecDel(cfg.IPAM.Type, args.StdinData); err != nil { |
| 225 | return err |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if !cfg.DPDKMode { |
| 230 | if err := moveLinkOut(containerNs, args.IfName); err != nil { |
| 231 | return err |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return nil |
| 236 | } |
| 237 | |
| 238 | func moveLinkIn(hostDev netlink.Link, containerNs ns.NetNS, containerIfName string) (netlink.Link, error) { |
| 239 | hostDevName := hostDev.Attrs().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 := ns.TempNetNS() |
| 249 | if err != nil { |
| 250 | return nil, fmt.Errorf("failed to create tempNS: %v", err) |
| 251 | } |
| 252 | defer tempNS.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.Attrs().Flags&net.FlagUp == net.FlagUp { |
| 258 | defer func() { |
| 259 | if err != nil { |
| 260 | // lookup the device again (index might have changed) |
| 261 | if hostDev, err := netlinksafe.LinkByName(hostDevName); err == nil { |
| 262 | _ = netlink.LinkSetUp(hostDev) |
| 263 | } |
| 264 | } |
| 265 | }() |
| 266 | } |
| 267 | |
| 268 | // Move the host device into tempNS |
| 269 | if err = netlink.LinkSetNsFd(hostDev, int(tempNS.Fd())); err != nil { |
| 270 | return nil, fmt.Errorf("failed to move %q to tempNS: %v", hostDevName, err) |
| 271 | } |
| 272 | |
| 273 | var contDev 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.Do(func(hostNS ns.NetNS) error { |
| 278 | // lookup the device in tempNS (index might have changed) |
| 279 | tempNSDev, err := netlinksafe.LinkByName(hostDevName) |
| 280 | if err != nil { |
| 281 | return 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 | _ = netlink.LinkSetNsFd(tempNSDev, int(hostNS.Fd())) |
| 290 | } |
| 291 | }() |
| 292 | |
| 293 | // Rename the device to the wanted name |
| 294 | if err = netlink.LinkSetName(tempNSDev, containerIfName); err != nil { |
| 295 | return 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 | _ = netlink.LinkSetName(tempNSDev, hostDevName) |
| 302 | } |
| 303 | }() |
| 304 | |
| 305 | // Save host device name into the container device's alias property |
| 306 | if err = netlink.LinkSetAlias(tempNSDev, hostDevName); err != nil { |
| 307 | return 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 | _ = netlink.LinkSetAlias(tempNSDev, "") |
| 314 | } |
| 315 | }() |
| 316 | |
| 317 | // Move the device to the containerNS |
| 318 | if err = netlink.LinkSetNsFd(tempNSDev, int(containerNs.Fd())); err != nil { |
| 319 | return 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, _ = netlinksafe.LinkByName(containerIfName) |
| 326 | } |
| 327 | }() |
| 328 | |
| 329 | err = containerNs.Do(func(_ ns.NetNS) error { |
| 330 | var err error |
| 331 | contDev, err = netlinksafe.LinkByName(containerIfName) |
| 332 | if err != nil { |
| 333 | return 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 | _ = netlink.LinkSetNsFd(contDev, int(tempNS.Fd())) |
| 340 | } |
| 341 | }() |
| 342 | |
| 343 | // Bring the device up |
| 344 | // This must be done in the containerNS |
| 345 | if err = netlink.LinkSetUp(contDev); err != nil { |
| 346 | return 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 moveLinkOut(containerNs 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 := ns.TempNetNS() |
| 365 | if err != nil { |
| 366 | return fmt.Errorf("failed to create tempNS: %v", err) |
| 367 | } |
| 368 | defer tempNS.Close() |
| 369 | |
| 370 | var contDev 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.Attrs().Flags&net.FlagUp == net.FlagUp { |
| 377 | containerNs.Do(func(_ ns.NetNS) error { |
| 378 | // lookup the device again (index might have changed) |
| 379 | if contDev, err := netlinksafe.LinkByName(containerIfName); err == nil { |
| 380 | _ = netlink.LinkSetUp(contDev) |
| 381 | } |
| 382 | return nil |
| 383 | }) |
| 384 | } |
| 385 | }() |
| 386 | |
| 387 | err = containerNs.Do(func(_ ns.NetNS) error { |
| 388 | var err error |
| 389 | // Lookup the device in the containerNS |
| 390 | contDev, err = netlinksafe.LinkByName(containerIfName) |
| 391 | if err != nil { |
| 392 | return fmt.Errorf("failed to find %q in containerNS: %v", containerIfName, err) |
| 393 | } |
| 394 | |
| 395 | // Verify we have the original name |
| 396 | if contDev.Attrs().Alias == "" { |
| 397 | return 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 = netlink.LinkSetNsFd(contDev, int(tempNS.Fd())); err != nil { |
| 402 | return 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.Do(func(hostNS ns.NetNS) error { |
| 411 | // Lookup the device in tempNS (index might have changed) |
| 412 | tempNSDev, err := netlinksafe.LinkByName(containerIfName) |
| 413 | if err != nil { |
| 414 | return 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 | _ = netlink.LinkSetNsFd(tempNSDev, int(containerNs.Fd())) |
| 421 | } |
| 422 | }() |
| 423 | |
| 424 | hostDevName := tempNSDev.Attrs().Alias |
| 425 | |
| 426 | // Rename container device to hostDevName |
| 427 | if err = netlink.LinkSetName(tempNSDev, hostDevName); err != nil { |
| 428 | return 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 | _ = netlink.LinkSetName(tempNSDev, containerIfName) |
| 435 | } |
| 436 | }() |
| 437 | |
| 438 | // Unset device's alias property |
| 439 | if err = netlink.LinkSetAlias(tempNSDev, ""); err != nil { |
| 440 | return 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 | _ = netlink.LinkSetAlias(tempNSDev, hostDevName) |
| 447 | } |
| 448 | }() |
| 449 | |
| 450 | // Finally move the device to the hostNS |
| 451 | if err = netlink.LinkSetNsFd(tempNSDev, int(hostNS.Fd())); err != nil { |
| 452 | return 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 hasDpdkDriver(pciaddr string) (bool, error) { |
| 467 | driverLink := filepath.Join(sysBusPCI, pciaddr, "driver") |
| 468 | driverPath, err := filepath.EvalSymlinks(driverLink) |
| 469 | if err != nil { |
| 470 | return false, err |
| 471 | } |
| 472 | driverStat, err := os.Stat(driverPath) |
| 473 | if err != nil { |
| 474 | return false, err |
| 475 | } |
| 476 | driverName := driverStat.Name() |
| 477 | for _, drv := range userspaceDrivers { |
| 478 | if driverName == drv { |
| 479 | return true, nil |
| 480 | } |
| 481 | } |
| 482 | return false, nil |
| 483 | } |
| 484 | |
| 485 | func printLink(dev netlink.Link, cniVersion string, containerNs ns.NetNS) error { |
| 486 | result := current.Result{ |
| 487 | CNIVersion: current.ImplementedSpecVersion, |
| 488 | Interfaces: []*current.Interface{ |
| 489 | { |
| 490 | Name: dev.Attrs().Name, |
| 491 | Mac: dev.Attrs().HardwareAddr.String(), |
| 492 | Sandbox: containerNs.Path(), |
| 493 | }, |
| 494 | }, |
| 495 | } |
| 496 | return types.PrintResult(&result, cniVersion) |
| 497 | } |
| 498 | |
| 499 | func linkFromPath(path string) (netlink.Link, error) { |
| 500 | entries, err := os.ReadDir(path) |
| 501 | if err != nil { |
| 502 | return nil, fmt.Errorf("failed to read directory %s: %q", path, err) |
| 503 | } |
| 504 | if len(entries) > 0 { |
| 505 | // grab the first net device |
| 506 | return netlinksafe.LinkByName(entries[0].Name()) |
| 507 | } |
| 508 | return nil, fmt.Errorf("failed to find network device in path %s", path) |
| 509 | } |
| 510 | |
| 511 | func getLink(devname, hwaddr, kernelpath, pciaddr string, auxDev string) (netlink.Link, error) { |
| 512 | switch { |
| 513 | |
| 514 | case len(devname) > 0: |
| 515 | return netlinksafe.LinkByName(devname) |
| 516 | case len(hwaddr) > 0: |
| 517 | hwAddr, err := net.ParseMAC(hwaddr) |
| 518 | if err != nil { |
| 519 | return nil, fmt.Errorf("failed to parse MAC address %q: %v", hwaddr, err) |
| 520 | } |
| 521 | |
| 522 | links, err := netlinksafe.LinkList() |
| 523 | if err != nil { |
| 524 | return nil, fmt.Errorf("failed to list node links: %v", err) |
| 525 | } |
| 526 | |
| 527 | for _, link := range links { |
| 528 | if bytes.Equal(link.Attrs().HardwareAddr, hwAddr) { |
| 529 | return link, nil |
| 530 | } |
| 531 | } |
| 532 | case len(kernelpath) > 0: |
| 533 | if !filepath.IsAbs(kernelpath) || !strings.HasPrefix(kernelpath, "/sys/devices/") { |
| 534 | return nil, fmt.Errorf("kernel device path %q must be absolute and begin with /sys/devices/", kernelpath) |
| 535 | } |
| 536 | netDir := filepath.Join(kernelpath, "net") |
| 537 | return linkFromPath(netDir) |
| 538 | case len(pciaddr) > 0: |
| 539 | netDir := filepath.Join(sysBusPCI, pciaddr, "net") |
| 540 | if _, err := os.Lstat(netDir); err != nil { |
| 541 | virtioNetDir := filepath.Join(sysBusPCI, pciaddr, "virtio*", "net") |
| 542 | matches, err := filepath.Glob(virtioNetDir) |
| 543 | if matches == nil || err != nil { |
| 544 | return nil, fmt.Errorf("no net directory under pci device %s", pciaddr) |
| 545 | } |
| 546 | netDir = matches[0] |
| 547 | } |
| 548 | return linkFromPath(netDir) |
| 549 | case len(auxDev) > 0: |
| 550 | netDir := filepath.Join(sysBusAuxiliary, auxDev, "net") |
| 551 | return linkFromPath(netDir) |
| 552 | } |
| 553 | |
| 554 | return nil, fmt.Errorf("failed to find physical interface") |
| 555 | } |
| 556 | |
| 557 | func main() { |
| 558 | skel.PluginMainFuncs(skel.CNIFuncs{ |
| 559 | Add: cmdAdd, |
| 560 | Check: cmdCheck, |
| 561 | Del: cmdDel, |
| 562 | Status: cmdStatus, |
| 563 | /* FIXME GC */ |
| 564 | }, version.All, bv.BuildString("host-device")) |
| 565 | } |
| 566 | |
| 567 | func cmdCheck(args *skel.CmdArgs) error { |
| 568 | cfg, err := loadConf(args.StdinData) |
| 569 | if err != nil { |
| 570 | return err |
| 571 | } |
| 572 | netns, err := ns.GetNS(args.Netns) |
| 573 | if err != nil { |
| 574 | return fmt.Errorf("failed to open netns %q: %v", args.Netns, err) |
| 575 | } |
| 576 | defer netns.Close() |
| 577 | |
| 578 | // run the IPAM plugin and get back the config to apply |
| 579 | if cfg.IPAM.Type != "" { |
| 580 | err = ipam.ExecCheck(cfg.IPAM.Type, args.StdinData) |
| 581 | if err != nil { |
| 582 | return err |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // Parse previous result. |
| 587 | if cfg.NetConf.RawPrevResult == nil { |
| 588 | return fmt.Errorf("Required prevResult missing") |
| 589 | } |
| 590 | |
| 591 | if err := version.ParsePrevResult(&cfg.NetConf); err != nil { |
| 592 | return err |
| 593 | } |
| 594 | |
| 595 | result, err := current.NewResultFromResult(cfg.PrevResult) |
| 596 | if err != nil { |
| 597 | return err |
| 598 | } |
| 599 | |
| 600 | if cfg.DPDKMode { |
| 601 | return nil |
| 602 | } |
| 603 | |
| 604 | var contMap current.Interface |
| 605 | // Find interfaces for name we know, that of host-device inside container |
| 606 | for _, intf := range result.Interfaces { |
| 607 | if args.IfName == intf.Name { |
| 608 | if args.Netns == intf.Sandbox { |
| 609 | contMap = *intf |
| 610 | continue |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // The namespace must be the same as what was configured |
| 616 | if args.Netns != contMap.Sandbox { |
| 617 | return fmt.Errorf("Sandbox in prevResult %s doesn't match configured netns: %s", |
| 618 | contMap.Sandbox, args.Netns) |
| 619 | } |
| 620 | |
| 621 | // |
| 622 | // Check prevResults for ips, routes and dns against values found in the container |
| 623 | if err := netns.Do(func(_ ns.NetNS) error { |
| 624 | // Check interface against values found in the container |
| 625 | err := validateCniContainerInterface(contMap) |
| 626 | if err != nil { |
| 627 | return err |
| 628 | } |
| 629 | |
| 630 | err = ip.ValidateExpectedInterfaceIPs(args.IfName, result.IPs) |
| 631 | if err != nil { |
| 632 | return err |
| 633 | } |
| 634 | |
| 635 | err = ip.ValidateExpectedRoute(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 validateCniContainerInterface(intf current.Interface) error { |
| 649 | var link netlink.Link |
| 650 | var err error |
| 651 | |
| 652 | if intf.Name == "" { |
| 653 | return fmt.Errorf("Container interface name missing in prevResult: %v", intf.Name) |
| 654 | } |
| 655 | link, err = netlinksafe.LinkByName(intf.Name) |
| 656 | if err != nil { |
| 657 | return fmt.Errorf("Container Interface name in prevResult: %s not found", intf.Name) |
| 658 | } |
| 659 | if intf.Sandbox == "" { |
| 660 | return fmt.Errorf("Error: Container interface %s should not be in host namespace", link.Attrs().Name) |
| 661 | } |
| 662 | |
| 663 | if intf.Mac != "" { |
| 664 | if intf.Mac != link.Attrs().HardwareAddr.String() { |
| 665 | return fmt.Errorf("Interface %s Mac %s doesn't match container Mac: %s", intf.Name, intf.Mac, link.Attrs().HardwareAddr) |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | return nil |
| 670 | } |
| 671 | |
| 672 | func cmdStatus(args *skel.CmdArgs) error { |
| 673 | conf := NetConf{} |
| 674 | if err := json.Unmarshal(args.StdinData, &conf); err != nil { |
| 675 | return fmt.Errorf("failed to load netconf: %w", err) |
| 676 | } |
| 677 | |
| 678 | if conf.IPAM.Type != "" { |
| 679 | if err := ipam.ExecStatus(conf.IPAM.Type, args.StdinData); err != nil { |
| 680 | return err |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | // TODO: Check if host device exists. |
| 685 | |
| 686 | return nil |
| 687 | } |