本文整理汇总了Golang中github.com/vishvananda/netlink.LinkSetMaster函数的典型用法代码示例。如果您正苦于以下问题:Golang LinkSetMaster函数的具体用法?Golang LinkSetMaster怎么用?Golang LinkSetMaster使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LinkSetMaster函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: testBridgeSimpleSetup
func testBridgeSimpleSetup(t *testing.T, nsPrefix string, br *netlink.Bridge) (
*netlink.Bridge, []*netlink.Veth, []netns.NsHandle, func()) {
links, nets, cleanup := testNetnsPair(t, nsPrefix)
if br == nil {
br = &netlink.Bridge{
LinkAttrs: netlink.LinkAttrs{
Name: "br0",
},
}
if err := netlink.LinkAdd(br); err != nil {
cleanup()
t.Fatal(err)
}
}
cleanup2 := func() {
netlink.LinkDel(br)
cleanup()
}
if err := netlink.LinkSetMaster(links[0], br); err != nil {
cleanup2()
t.Fatal(err)
}
if err := netlink.LinkSetMaster(links[1], br); err != nil {
cleanup2()
t.Fatal(err)
}
if err := netlink.LinkSetUp(br); err != nil {
cleanup2()
t.Fatal(err)
}
return br, links, nets, cleanup2
}
示例2: wireTapInterface
// Wire up a tap interface for communicating with the guest. Returns the name
// of the created tap interface.
func wireTapInterface(config *netConfig) string {
// Drop link on eth0 before configuring anything
eth0, err := netlink.LinkByName("eth0")
if err != nil {
log.Fatalf("LinkByName(eth0): %v", err)
}
if err := netlink.LinkSetDown(eth0); err != nil {
log.Fatalf("LinkSetDown(eth0): %v", err)
}
// Flush any L3 addresses on eth0
if err := flushAddresses(eth0); err != nil {
log.Fatalf("flushAddresses(eth0): %v", err)
}
// Generate and set random MAC address for eth0
eth0Addr := generateHardwareAddr()
if err := netlink.LinkSetHardwareAddr(eth0, eth0Addr); err != nil {
log.Fatalf("LinkSetHardwareAddr(eth0): %v", err)
}
// Create "tap0" (interface to guest)
tap0Attrs := netlink.NewLinkAttrs()
tap0Attrs.Name = "tap0"
tap0 := &netlink.Tuntap{tap0Attrs, netlink.TUNTAP_MODE_TAP}
if err := netlink.LinkAdd(tap0); err != nil {
log.Fatalf("LinkAdd(tap0): %v", err)
}
// Create a new bridge, br0 and add eth0 and tap0 to it
br0Attrs := netlink.NewLinkAttrs()
br0Attrs.Name = "br0"
br0 := &netlink.Bridge{br0Attrs}
if err := netlink.LinkAdd(br0); err != nil {
log.Fatalf("LinkAdd(br0): %v", err)
}
if err := netlink.LinkSetMaster(eth0, br0); err != nil {
log.Fatalf("LinkSetMaster(eth0, br0): %v", err)
}
if err := netlink.LinkSetMaster(tap0, br0); err != nil {
log.Fatalf("LinkSetMaster(tap0, br0): %v", err)
}
// Set all links up
if err := netlink.LinkSetUp(tap0); err != nil {
log.Fatalf("LinkSetUp(tap0): %v", err)
}
if err := netlink.LinkSetUp(eth0); err != nil {
log.Fatalf("LinkSetUp(eth0): %v", err)
}
if err := netlink.LinkSetUp(br0); err != nil {
log.Fatalf("LinkSetUp(br0): %v", err)
}
return tap0Attrs.Name
}
示例3: attach
// attach a container network interface to an external network
func (v *veth) attach(n *configs.Network) (err error) {
brl, err := netlink.LinkByName(n.Bridge)
if err != nil {
return err
}
br, ok := brl.(*netlink.Bridge)
if !ok {
return fmt.Errorf("Wrong device type %T", brl)
}
host, err := netlink.LinkByName(n.HostInterfaceName)
if err != nil {
return err
}
if err := netlink.LinkSetMaster(host, br); err != nil {
return err
}
if err := netlink.LinkSetMTU(host, n.Mtu); err != nil {
return err
}
if n.HairpinMode {
if err := netlink.LinkSetHairpin(host, true); err != nil {
return err
}
}
if err := netlink.LinkSetUp(host); err != nil {
return err
}
return nil
}
示例4: endpointCreate
func endpointCreate() {
hostIfName := "myveth0"
containerIfName := "myveth1"
veth, err := netlink.LinkByName("myveth0")
if err == nil && veth != nil {
return
}
veth = &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{Name: hostIfName, TxQLen: 0},
PeerName: containerIfName}
if err := netlink.LinkAdd(veth); err != nil {
panic(err)
}
la := netlink.NewLinkAttrs()
la.Name = "mycbridge"
mybridge := &netlink.Bridge{la}
err = netlink.LinkSetMaster(veth, mybridge)
if err != nil {
panic(err)
}
err = netlink.LinkSetUp(veth)
if err != nil {
panic(err)
}
}
示例5: setupVeth
func setupVeth(netns string, br *netlink.Bridge, ifName string, mtu int, hairpinMode bool) error {
var hostVethName string
err := ns.WithNetNSPath(netns, false, func(hostNS *os.File) error {
// create the veth pair in the container and move host end into host netns
hostVeth, _, err := ip.SetupVeth(ifName, mtu, hostNS)
if 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)
}
// set hairpin mode
if err = netlink.LinkSetHairpin(hostVeth, hairpinMode); err != nil {
return fmt.Errorf("failed to setup hairpin mode for %v: %v", hostVethName, err)
}
return nil
}
示例6: setInterfaceMaster
func setInterfaceMaster(iface netlink.Link, i *nwIface) error {
if i.DstMaster() == "" {
return nil
}
return netlink.LinkSetMaster(iface, &netlink.Bridge{
LinkAttrs: netlink.LinkAttrs{Name: i.DstMaster()}})
}
示例7: joinEndpoint
func (driver *driver) joinEndpoint(w http.ResponseWriter, r *http.Request) {
var j join
if err := json.NewDecoder(r.Body).Decode(&j); err != nil {
sendError(w, "Could not decode JSON encode payload", http.StatusBadRequest)
return
}
Log.Debugf("Join request: %+v", &j)
endID := j.EndpointID
// create and attach local name to the bridge
local := vethPair(endID[:5])
if err := netlink.LinkAdd(local); err != nil {
Log.Error(err)
errorResponsef(w, "could not create veth pair")
return
}
var bridge *netlink.Bridge
if maybeBridge, err := netlink.LinkByName(WeaveBridge); err != nil {
Log.Error(err)
errorResponsef(w, `bridge "%s" not present`, WeaveBridge)
return
} else {
var ok bool
if bridge, ok = maybeBridge.(*netlink.Bridge); !ok {
Log.Errorf("%s is %+v", WeaveBridge, maybeBridge)
errorResponsef(w, `device "%s" not a bridge`, WeaveBridge)
return
}
}
if netlink.LinkSetMaster(local, bridge) != nil || netlink.LinkSetUp(local) != nil {
errorResponsef(w, `unable to bring veth up`)
return
}
ifname := &iface{
SrcName: local.PeerName,
DstPrefix: "ethwe",
ID: 0,
}
res := &joinResponse{
InterfaceNames: []*iface{ifname},
}
if driver.nameserver != "" {
routeToDNS := &staticRoute{
Destination: driver.nameserver + "/32",
RouteType: types.CONNECTED,
NextHop: "",
InterfaceID: 0,
}
res.StaticRoutes = []*staticRoute{routeToDNS}
}
objectResponse(w, res)
Log.Infof("Join endpoint %s:%s to %s", j.NetworkID, j.EndpointID, j.SandboxKey)
}
示例8: Add
func (Bridge) Add(bridge, slaveIf *net.Interface) error {
netlinkMu.Lock()
defer netlinkMu.Unlock()
master, err := netlink.LinkByName(bridge.Name)
if err != nil {
return err
}
slave, err := netlink.LinkByName(slaveIf.Name)
if err != nil {
return err
}
return netlink.LinkSetMaster(slave, master.(*netlink.Bridge))
}
示例9: AddLink
func (b *Bridge) AddLink(link netlink.Link) error {
existing, err := netlink.LinkByName(b.name)
if err != nil {
return err
}
if bridge, ok := existing.(*netlink.Bridge); ok {
if link.Attrs().MasterIndex != bridge.Index {
return netlink.LinkSetMaster(link, bridge)
}
} else {
return fmt.Errorf("%s is not a bridge", b.name)
}
return nil
}
示例10: JoinEndpoint
func (driver *driver) JoinEndpoint(j *api.JoinRequest) (response *api.JoinResponse, error error) {
endID := j.EndpointID
// create and attach local name to the bridge
local := vethPair(endID[:5])
if err := netlink.LinkAdd(local); err != nil {
error = fmt.Errorf("could not create veth pair: %s", err)
return
}
var bridge *netlink.Bridge
if maybeBridge, err := netlink.LinkByName(WeaveBridge); err != nil {
err = fmt.Errorf(`bridge "%s" not present`, WeaveBridge)
return
} else {
var ok bool
if bridge, ok = maybeBridge.(*netlink.Bridge); !ok {
Log.Errorf("%s is %+v", WeaveBridge, maybeBridge)
err = fmt.Errorf(`device "%s" not a bridge`, WeaveBridge)
return
}
}
if netlink.LinkSetMaster(local, bridge) != nil || netlink.LinkSetUp(local) != nil {
error = fmt.Errorf(`unable to bring veth up`)
return
}
ifname := &api.InterfaceName{
SrcName: local.PeerName,
DstPrefix: "ethwe",
}
response = &api.JoinResponse{
InterfaceName: ifname,
}
if driver.nameserver != "" {
routeToDNS := api.StaticRoute{
Destination: driver.nameserver + "/32",
RouteType: types.CONNECTED,
NextHop: "",
}
response.StaticRoutes = []api.StaticRoute{routeToDNS}
}
Log.Infof("Join endpoint %s:%s to %s", j.NetworkID, j.EndpointID, j.SandboxKey)
return
}
示例11: addToBridge
func addToBridge(ifaceName, bridgeName string) error {
link, err := netlink.LinkByName(ifaceName)
if err != nil {
return fmt.Errorf("could not find interface %s: %v", ifaceName, err)
}
if err = netlink.LinkSetMaster(link,
&netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: bridgeName}}); err != nil {
logrus.Debugf("Failed to add %s to bridge via netlink.Trying ioctl: %v", ifaceName, err)
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return fmt.Errorf("could not find network interface %s: %v", ifaceName, err)
}
master, err := net.InterfaceByName(bridgeName)
if err != nil {
return fmt.Errorf("could not find bridge %s: %v", bridgeName, err)
}
return ioctlAddToBridge(iface, master)
}
return nil
}
示例12: createLink
func (n *networks) createLink(config networkConfig) error {
//Link creation starts from checking if current vlan interface exists
if _, err := netlink.LinkByName(config.LinkName); err != nil {
//Try creating the link
la := netlink.NewLinkAttrs()
la.Name = config.LinkName
la.ParentIndex = n.parent.Attrs().Index
vl := &netlink.Vlan{la, config.Vlan}
if err := netlink.LinkAdd(vl); err != nil {
return ErrNetlinkError{"create vlan iface", err}
}
if err := netlink.LinkSetUp(vl); err != nil {
return ErrNetlinkError{"bring vlan iface up", err}
}
}
//Now check if bridge exists
if _, err := netlink.LinkByName(config.BridgeName); err != nil {
//Try creating the bridge
la := netlink.NewLinkAttrs()
la.Name = config.BridgeName
br := &netlink.Bridge{la}
if err := netlink.LinkAdd(br); err != nil {
return ErrNetlinkError{"create bridge", err}
}
//Link bridge to new interface
if li, err := netlink.LinkByName(config.LinkName); err != nil {
netlink.LinkDel(br)
return ErrNetlinkError{"find iface by name (" + config.LinkName + ")", err}
} else if err := netlink.LinkSetMaster(li, br); err != nil {
netlink.LinkDel(br)
return ErrNetlinkError{"set bridge master", err}
}
if err := netlink.LinkSetUp(br); err != nil {
return ErrNetlinkError{"bring bridge up", err}
}
}
return nil
}
示例13: LinkSetMaster
func (*nl) LinkSetMaster(slave netlink.Link, master *netlink.Bridge) error {
return netlink.LinkSetMaster(slave, master)
}
示例14: detach
func (v *veth) detach(n *configs.Network) (err error) {
return netlink.LinkSetMaster(&netlink.Device{netlink.LinkAttrs{Name: n.HostInterfaceName}}, nil)
}
示例15: kvmSetup
// 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, errwrap.Wrap(errors.New("error loading network definitions"), e)
}
for i, n := range network.nets {
if n.conf.Type == "flannel" {
if err := kvmTransformFlannelNetwork(&n); err != nil {
return nil, errwrap.Wrap(errors.New("cannot transform flannel network into basic network"), 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, errwrap.Wrap(fmt.Errorf("cannot add address to host tap device %q", ifName), err)
}
if err := removeAllRoutesOnLink(link); err != nil {
return nil, errwrap.Wrap(fmt.Errorf("cannot remove route on host tap device %q", ifName), err)
}
if err := addRoute(link, n.runtime.IP); err != nil {
return nil, errwrap.Wrap(errors.New("cannot add on host direct route to pod"), err)
}
case "bridge":
config := BridgeNetConf{
NetConf: NetConf{
MTU: defaultMTU,
},
BrName: defaultBrName,
}
if err := json.Unmarshal(n.confBytes, &config); err != nil {
return nil, errwrap.Wrap(fmt.Errorf("error parsing %q result", n.conf.Name), err)
}
br, err := ensureBridgeIsUp(config.BrName, config.MTU)
if err != nil {
return nil, errwrap.Wrap(errors.New("error in time of bridge setup"), err)
}
link, err := setupTapDevice(podID)
if err != nil {
return nil, errwrap.Wrap(errors.New("can not setup tap device"), err)
}
err = netlink.LinkSetMaster(link, br)
if err != nil {
rErr := tuntap.RemovePersistentIface(n.runtime.IfName, tuntap.Tap)
if rErr != nil {
stderr.PrintE("warning: could not cleanup tap interface", rErr)
}
return nil, errwrap.Wrap(errors.New("can not add tap interface to bridge"), 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),
},
//.........这里部分代码省略.........