func (v *veth) create(n *network, nspid int) (err error) { tmpName, err := v.generateTempPeerName() if err != nil { return err } n.TempVethPeerName = tmpName defer func() { if err != nil { netlink.NetworkLinkDel(n.HostInterfaceName) netlink.NetworkLinkDel(n.TempVethPeerName) } }() if n.Bridge == "" { return fmt.Errorf("bridge is not specified") } if err := netlink.NetworkCreateVethPair(n.HostInterfaceName, n.TempVethPeerName, n.TxQueueLen); err != nil { return err } if err := v.attach(&n.Network); err != nil { return err } child, err := net.InterfaceByName(n.TempVethPeerName) if err != nil { return err } return netlink.NetworkSetNsPid(child, nspid) }
// SetPeerLinkNsToDocker sends peer link into Docker func (veth *VethPair) SetPeerLinkNsToDocker(name string, dockerHost string) error { pid, err := DockerPidByName(name, dockerHost) if err != nil { return fmt.Errorf("Failed to find docker %s : %s", name, err) } return netlink.NetworkSetNsPid(veth.peerIfc, pid) }
// setLinkOptions validates and sets link's various options passed in as LinkOptions. func setLinkOptions(ifc *net.Interface, opts LinkOptions) error { macaddr, mtu, flags, ns := opts.MacAddr, opts.MTU, opts.Flags, opts.Ns // if MTU is passed in LinkOptions if mtu != 0 { if err := validMtu(mtu); err != nil { return err } if err := netlink.NetworkSetMTU(ifc, mtu); err != nil { return fmt.Errorf("Unable to set MTU: %s", err) } } // if MacAddress is passed in LinkOptions if macaddr != "" { if err := validMacAddress(macaddr); err != nil { return err } if err := netlink.NetworkSetMacAddress(ifc, macaddr); err != nil { return fmt.Errorf("Unable to set MAC Address: %s", err) } } // if ns is passed in LinkOptions if ns != 0 { if err := validNs(ns); err != nil { return err } if err := netlink.NetworkSetNsPid(ifc, ns); err != nil { return fmt.Errorf("Unable to set Network namespace: %s", err) } } // if flags is passed in LinkOptions if flags != 0 { if err := validFlags(flags); err != nil { return err } if ns != 0 && (ns != 1 || ns != os.Getpid()) { if (flags & syscall.IFF_UP) == syscall.IFF_UP { origNs, _ := NetNsHandle(os.Getpid()) defer syscall.Close(int(origNs)) defer system.Setns(origNs, syscall.CLONE_NEWNET) if err := SetNetNsToPid(ns); err != nil { return fmt.Errorf("Switching to %d network namespace failed: %s", ns, err) } if err := netlink.NetworkLinkUp(ifc); err != nil { return fmt.Errorf("Unable to bring %s interface UP: %s", ifc.Name, ns) } } } else { if err := netlink.NetworkLinkUp(ifc); err != nil { return fmt.Errorf("Could not bring up network link %s: %s", ifc.Name, err) } } } return nil }
// SetLinkNetNsPid moves the link to Network namespace specified by PID. func (l *Link) SetLinkNetNsPid(nspid int) error { return netlink.NetworkSetNsPid(l.NetInterface(), nspid) }
// SetPeerLinkNsPid sends peer link into container specified by PID func (veth *VethPair) SetPeerLinkNsPid(nspid int) error { return netlink.NetworkSetNsPid(veth.peerIfc, nspid) }