本文整理汇总了Golang中github.com/vishvananda/netlink.LinkAdd函数的典型用法代码示例。如果您正苦于以下问题:Golang LinkAdd函数的具体用法?Golang LinkAdd怎么用?Golang LinkAdd使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LinkAdd函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: createInterfaces
func createInterfaces(netCfg *NetworkConfig) error {
for name, iface := range netCfg.Interfaces {
if iface.Bridge {
bridge := netlink.Bridge{}
bridge.LinkAttrs.Name = name
if err := netlink.LinkAdd(&bridge); err != nil {
log.Errorf("Failed to create bridge %s: %v", name, err)
}
} else if iface.Bond != "" {
bondIface, ok := netCfg.Interfaces[iface.Bond]
if !ok {
log.Errorf("Failed to find bond configuration for [%s]", iface.Bond)
continue
}
bond := Bond(iface.Bond)
if bond.Error() != nil {
log.Errorf("Failed to create bond [%s]: %v", iface.Bond, bond.Error())
continue
}
for k, v := range bondIface.BondOpts {
bond.Opt(k, v)
bond.Clear()
}
}
}
return nil
}
示例3: addUplink
// addUplink adds a dummy uplink to ofnet agent
func addUplink(ofa *OfnetAgent, linkName string, ofpPortNo uint32) (*netlink.Veth, error) {
link := &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{
Name: linkName,
TxQLen: 100,
MTU: 1400,
},
PeerName: linkName + "peer",
}
// delete old link if it exists.. and ignore error
netlink.LinkDel(link)
time.Sleep(100 * time.Millisecond)
if err := netlink.LinkAdd(link); err != nil {
return nil, err
}
// add it to ofnet
err := ofa.AddUplink(ofpPortNo, linkName)
if err != nil {
return nil, err
}
time.Sleep(time.Second)
// mark the link as up
if err := netlink.LinkSetUp(link); err != nil {
return nil, err
}
return link, nil
}
示例4: createDummyInterface
func createDummyInterface(t *testing.T) {
if testutils.RunningOnCircleCI() {
t.Skipf("Skipping as not supported on CIRCLE CI kernel")
}
dummy := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Name: "dummy",
},
}
err := netlink.LinkAdd(dummy)
require.NoError(t, err)
dummyLink, err := netlink.LinkByName("dummy")
require.NoError(t, err)
ip, ipNet, err := net.ParseCIDR("10.1.1.1/24")
require.NoError(t, err)
ipNet.IP = ip
ipAddr := &netlink.Addr{IPNet: ipNet, Label: ""}
err = netlink.AddrAdd(dummyLink, ipAddr)
require.NoError(t, err)
}
示例5: createInterface
func createInterface(t *testing.T, name string, nws ...string) {
// Add interface
link := &netlink.Bridge{
LinkAttrs: netlink.LinkAttrs{
Name: "test",
},
}
bips := []*net.IPNet{}
for _, nw := range nws {
bip, err := types.ParseCIDR(nw)
if err != nil {
t.Fatal(err)
}
bips = append(bips, bip)
}
if err := netlink.LinkAdd(link); err != nil {
t.Fatalf("Failed to create interface via netlink: %v", err)
}
for _, bip := range bips {
if err := netlink.AddrAdd(link, &netlink.Addr{IPNet: bip}); err != nil {
t.Fatal(err)
}
}
if err := netlink.LinkSetUp(link); err != nil {
t.Fatal(err)
}
}
示例6: 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
}
示例7: createEndpoint
// create endpoint call
func (driver *driver) createEndpoint(w http.ResponseWriter, r *http.Request) {
var create api.CreateEndpointRequest
if err := json.NewDecoder(r.Body).Decode(&create); err != nil {
sendError(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
return
}
Log.Infof("Create endpoint request %+v", &create)
endID := create.EndpointID
ip := create.Interface.Address
Log.Infof("Got IP from IPAM %s", ip)
local := vethPair(endID[:5])
if err := netlink.LinkAdd(local); err != nil {
Log.Error(err)
errorResponse(w, "could not create veth pair")
return
}
link, _ := netlink.LinkByName(local.PeerName)
mac := link.Attrs().HardwareAddr.String()
resp := &api.CreateEndpointResponse{
Interface: &api.EndpointInterface{
MacAddress: mac,
},
}
objectResponse(w, resp)
Log.Infof("Create endpoint %s %+v", endID, resp)
}
示例8: ensureBridge
func ensureBridge(brName string, mtu int) (*netlink.Bridge, error) {
br := &netlink.Bridge{
LinkAttrs: netlink.LinkAttrs{
Name: brName,
MTU: mtu,
},
}
if err := netlink.LinkAdd(br); err != nil {
if err != syscall.EEXIST {
return nil, fmt.Errorf("could not add %q: %v", brName, err)
}
// it's ok if the device already exists as long as config is similar
br, err = bridgeByName(brName)
if err != nil {
return nil, err
}
}
if err := netlink.LinkSetUp(br); err != nil {
return nil, err
}
return br, nil
}
示例9: Join
func (d *Driver) Join(r *dknet.JoinRequest) (*dknet.JoinResponse, error) {
// create and attach local name to the bridge
localVethPair := vethPair(truncateID(r.EndpointID))
if err := netlink.LinkAdd(localVethPair); err != nil {
log.Errorf("failed to create the veth pair named: [ %v ] error: [ %s ] ", localVethPair, err)
return nil, err
}
// Bring the veth pair up
err := netlink.LinkSetUp(localVethPair)
if err != nil {
log.Warnf("Error enabling Veth local iface: [ %v ]", localVethPair)
return nil, err
}
bridgeName := d.networks[r.NetworkID].BridgeName
err = d.addOvsVethPort(bridgeName, localVethPair.Name, 0)
if err != nil {
log.Errorf("error attaching veth [ %s ] to bridge [ %s ]", localVethPair.Name, bridgeName)
return nil, err
}
log.Infof("Attached veth [ %s ] to bridge [ %s ]", localVethPair.Name, bridgeName)
// SrcName gets renamed to DstPrefix + ID on the container iface
res := &dknet.JoinResponse{
InterfaceName: dknet.InterfaceName{
SrcName: localVethPair.PeerName,
DstPrefix: containerEthName,
},
Gateway: d.networks[r.NetworkID].Gateway,
}
log.Debugf("Join endpoint %s:%s to %s", r.NetworkID, r.EndpointID, r.SandboxKey)
return res, nil
}
示例10: createMacVlan
// Create the macvlan slave specifying the source name
func createMacVlan(containerIfName, parent, macvlanMode string) (string, error) {
// Set the macvlan mode. Default is bridge mode
mode, err := setMacVlanMode(macvlanMode)
if err != nil {
return "", fmt.Errorf("Unsupported %s macvlan mode: %v", macvlanMode, err)
}
// verify the Docker host interface acting as the macvlan parent iface exists
if !parentExists(parent) {
return "", fmt.Errorf("the requested parent interface %s was not found on the Docker host", parent)
}
// Get the link for the master index (Example: the docker host eth iface)
parentLink, err := netlink.LinkByName(parent)
if err != nil {
return "", fmt.Errorf("error occoured looking up the %s parent iface %s error: %s", macvlanType, parent, err)
}
// Create a macvlan link
macvlan := &netlink.Macvlan{
LinkAttrs: netlink.LinkAttrs{
Name: containerIfName,
ParentIndex: parentLink.Attrs().Index,
},
Mode: mode,
}
if err := netlink.LinkAdd(macvlan); err != nil {
// If a user creates a macvlan and ipvlan on same parent, only one slave iface can be active at a time.
return "", fmt.Errorf("failed to create the %s port: %v", macvlanType, err)
}
return macvlan.Attrs().Name, nil
}
示例11: ensureBridge
func ensureBridge(brName string, mtu int) (*netlink.Bridge, error) {
br := &netlink.Bridge{
LinkAttrs: netlink.LinkAttrs{
Name: brName,
MTU: mtu,
// Let kernel use default txqueuelen; leaving it unset
// means 0, and a zero-length TX queue messes up FIFO
// traffic shapers which use TX queue length as the
// default packet limit
TxQLen: -1,
},
}
if err := netlink.LinkAdd(br); err != nil {
if err != syscall.EEXIST {
return nil, fmt.Errorf("could not add %q: %v", brName, err)
}
// it's ok if the device already exists as long as config is similar
br, err = bridgeByName(brName)
if err != nil {
return nil, err
}
}
if err := netlink.LinkSetUp(br); err != nil {
return nil, err
}
return br, nil
}
示例12: createVethPair
func createVethPair() (string, string, error) {
defer osl.InitOSContext()()
// Generate a name for what will be the host side pipe interface
name1, err := netutils.GenerateIfaceName(vethPrefix, vethLen)
if err != nil {
return "", "", fmt.Errorf("error generating veth name1: %v", err)
}
// Generate a name for what will be the sandbox side pipe interface
name2, err := netutils.GenerateIfaceName(vethPrefix, vethLen)
if err != nil {
return "", "", fmt.Errorf("error generating veth name2: %v", err)
}
// Generate and add the interface pipe host <-> sandbox
veth := &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{Name: name1, TxQLen: 0},
PeerName: name2}
if err := netlink.LinkAdd(veth); err != nil {
return "", "", fmt.Errorf("error creating veth pair: %v", err)
}
return name1, name2, nil
}
示例13: Create
// Create creates a bridge device and returns the interface.
// If the device already exists, returns the existing interface.
func (Bridge) Create(name string, ip net.IP, subnet *net.IPNet) (intf *net.Interface, err error) {
netlinkMu.Lock()
defer netlinkMu.Unlock()
if intf, _ := net.InterfaceByName(name); intf != nil {
return intf, nil
}
link := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: name}}
if err := netlink.LinkAdd(link); err != nil && err.Error() != "file exists" {
return nil, fmt.Errorf("devices: create bridge: %v", err)
}
hAddr, _ := net.ParseMAC(randMacAddr())
err = netlink.LinkSetHardwareAddr(link, hAddr)
if err != nil {
return nil, fmt.Errorf("devices: set hardware address: %v", err)
}
if intf, err = net.InterfaceByName(name); err != nil {
return nil, fmt.Errorf("devices: look up created bridge interface: %v", err)
}
addr := &netlink.Addr{IPNet: &net.IPNet{IP: ip, Mask: subnet.Mask}}
if err = netlink.AddrAdd(link, addr); err != nil && err.Error() != "file exists" {
return nil, fmt.Errorf("devices: add IP to bridge: %v", err)
}
return intf, nil
}
示例14: 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)
}
}
示例15: makeIface
func (e *Endpoint) makeIface(network *Network, netns string) error {
if vethLink, _ := netlink.LinkByName("veth" + e.EndpointShortID); vethLink != nil {
log.Println("veth"+e.EndpointShortID, "already exist")
} else {
ethLink, err := netlink.LinkByName(network.Eth)
if err != nil {
fmt.Println("[Err] LinkByName:", err)
return err
}
attrs := netlink.NewLinkAttrs()
attrs.Name = "veth" + e.EndpointShortID
attrs.ParentIndex = ethLink.Attrs().Index
vlan := &netlink.Vlan{
attrs, network.VLanID,
}
if err := netlink.LinkAdd(vlan); err != nil {
log.Println("Err: ", err, "LinkAdd")
return err
}
}
if err := exec.Command("ip", "link", "set", "veth"+e.EndpointShortID, "netns", netns).Run(); err != nil {
log.Println("Err: ", err, "LinkSet NS")
return err
}
return nil
}