func setupVeth(netns string, br *netlink.Bridge, ifName string, mtu int, pr *plugin.Result) error { var hostVethName string err := ns.WithNetNSPath(netns, func(hostNS *os.File) error { // create the veth pair in the container and move host end into host netns hostVeth, _, err := ip.SetupVeth(netns, ifName, mtu, hostNS) if err != nil { return err } if err = plugin.ConfigureIface(ifName, pr); err != nil { return err } hostVethName = hostVeth.Attrs().Name return nil }) if err != nil { return err } // need to lookup hostVeth again as its index has changed during ns move hostVeth, err := netlink.LinkByName(hostVethName) if err != nil { return fmt.Errorf("failed to lookup %q: %v", hostVethName, err) } // connect host veth end to the bridge if err = netlink.LinkSetMaster(hostVeth, br); err != nil { return fmt.Errorf("failed to connect %q to bridge %v: %v", hostVethName, br.Attrs().Name, err) } return nil }
// kvmSetup prepare new Networking to be used in kvm environment based on tuntap pair interfaces // to allow communication with virtual machine created by lkvm tool func kvmSetup(podRoot string, podID types.UUID, fps []ForwardedPort, netList common.NetList, localConfig string) (*Networking, error) { network := Networking{ podEnv: podEnv{ podRoot: podRoot, podID: podID, netsLoadList: netList, localConfig: localConfig, }, } var e error network.nets, e = network.loadNets() if e != nil { return nil, fmt.Errorf("error loading network definitions: %v", e) } for i, n := range network.nets { if n.conf.Type == "flannel" { if err := kvmTransformFlannelNetwork(&n); err != nil { return nil, fmt.Errorf("cannot transform flannel network into basic network: %v", err) } } switch n.conf.Type { case "ptp": link, err := setupTapDevice(podID) if err != nil { return nil, err } ifName := link.Attrs().Name n.runtime.IfName = ifName err = kvmSetupNetAddressing(&network, n, ifName) if err != nil { return nil, err } // add address to host tap device err = ensureHasAddr( link, &net.IPNet{ IP: n.runtime.IP4.Gateway, Mask: net.IPMask(n.runtime.Mask), }, ) if err != nil { return nil, fmt.Errorf("cannot add address to host tap device %q: %v", ifName, err) } if err := removeAllRoutesOnLink(link); err != nil { return nil, fmt.Errorf("cannot remove route on host tap device %q: %v", ifName, err) } if err := addRoute(link, n.runtime.IP); err != nil { return nil, fmt.Errorf("cannot add on host direct route to pod: %v", err) } case "bridge": config := BridgeNetConf{ NetConf: NetConf{ MTU: defaultMTU, }, BrName: defaultBrName, } if err := json.Unmarshal(n.confBytes, &config); err != nil { return nil, fmt.Errorf("error parsing %q result: %v", n.conf.Name, err) } br, err := ensureBridgeIsUp(config.BrName, config.MTU) if err != nil { return nil, fmt.Errorf("error in time of bridge setup: %v", err) } link, err := setupTapDevice(podID) if err != nil { return nil, fmt.Errorf("can not setup tap device: %v", err) } err = netlink.LinkSetMaster(link, br) if err != nil { rErr := tuntap.RemovePersistentIface(n.runtime.IfName, tuntap.Tap) if rErr != nil { log.Printf("Warning: could not cleanup tap interface: %v", rErr) } return nil, fmt.Errorf("can not add tap interface to bridge: %v", err) } ifName := link.Attrs().Name n.runtime.IfName = ifName err = kvmSetupNetAddressing(&network, n, ifName) if err != nil { return nil, err } if config.IsGw { err = ensureHasAddr( br, &net.IPNet{ IP: n.runtime.IP4.Gateway, Mask: net.IPMask(n.runtime.Mask), }, ) if err != nil { return nil, fmt.Errorf("cannot add address to host bridge device %q: %v", br.Name, err) } } case "macvlan": config := MacVTapNetConf{} if err := json.Unmarshal(n.confBytes, &config); err != nil { return nil, fmt.Errorf("error parsing %q result: %v", n.conf.Name, err) } link, err := setupMacVTapDevice(podID, config) if err != nil { return nil, err } ifName := link.Attrs().Name n.runtime.IfName = ifName err = kvmSetupNetAddressing(&network, n, ifName) if err != nil { return nil, err } default: return nil, fmt.Errorf("network %q have unsupported type: %q", n.conf.Name, n.conf.Type) } if n.conf.IPMasq { chain := getChainName(podID.String(), n.conf.Name) if err := ip.SetupIPMasq(&net.IPNet{ IP: n.runtime.IP, Mask: net.IPMask(n.runtime.Mask), }, chain); err != nil { return nil, err } } network.nets[i] = n } err := network.forwardPorts(fps, network.GetDefaultIP()) if err != nil { return nil, err } return &network, nil }