// 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
}
Exemple #2
0
func (b *Bridge) setupBridge(externalPort string) error {

	la := netlink.NewLinkAttrs()
	la.Name = b.bridgeName
	bridge, _ := netlink.LinkByName(b.bridgeName)

	if bridge == nil {
		log.Debugf("Bridge %s does not exist ", b.bridgeName)
		out, err := exec.Command("ovs-vsctl", "add-br", b.bridgeName).CombinedOutput()
		if err != nil {
			log.Fatalf("Bridge %s creation failed been created.  Resp: %s, err: %s", b.bridgeName, out, err)
		}
		log.Infof("Bridge %s has been created.  Resp: %s", b.bridgeName, out)

		out, err = exec.Command("ovs-vsctl", "add-port", b.bridgeName, externalPort).CombinedOutput()
		if err != nil {
			log.Fatalf("Failed to add external port %s.  Resp: %s, err: %s", externalPort, out, err)
		}
		log.Infof("External port %s has been added to %s. Resp: %s", externalPort, b.bridgeName, out)

		out, err = exec.Command("ifconfig", externalPort, "0.0.0.0").CombinedOutput()
		if err != nil {
			log.Fatalf("Failed to ip address of port %s. Resp: %s, err: %s", externalPort, out, err)
		}
		log.Infof("Ip address of port  %s has been cleaned. Resp: %s", externalPort, out)

		return err
	} else {
		log.Debugf("Bridge %s already exsist", b.bridgeName)
	}

	return nil
}
Exemple #3
0
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)
	}
}
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
}
Exemple #5
0
func Add(name string) {
	if iface.Exists(name) {
		log.Printf("reusing existing bridge %s", name)
	} else {
		log.Printf("adding bridge %s", name)

		br := netlink.NewLinkAttrs()
		br.Name = name
		if err := netlink.LinkAdd(&netlink.Bridge{br}); err != nil {
			log.Fatalf("failed to add bridge %s: %s", name, err)
		}
	}
}
Exemple #6
0
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
}
Exemple #7
0
// vethPair creates a veth pair. Peername is renamed to eth0 in the container.
func vethPair(pid int, bridgeName string) (*netlink.Veth, error) {
	br, err := netlink.LinkByName(bridgeName)
	if err != nil {
		return nil, err
	}

	la := netlink.NewLinkAttrs()
	la.Name = fmt.Sprintf("%s-%d", defaultPortPrefix, pid)
	la.MasterIndex = br.Attrs().Index

	return &netlink.Veth{
		LinkAttrs: la,
		PeerName:  fmt.Sprintf("ethc%d", pid),
	}, nil
}
Exemple #8
0
// initBridge creates a bridge if it does not exist
func initBridge() error {
	_, err := net.InterfaceByName(bridgeName)
	if err == nil {
		return nil
	}
	if !strings.Contains(err.Error(), "no such network interface") {
		return err
	}

	// Create *netlink.Bridge object
	la := netlink.NewLinkAttrs()
	la.Name = bridgeName
	la.MTU = mtu
	br := &netlink.Bridge{LinkAttrs: la}
	if err := netlink.LinkAdd(br); err != nil {
		return fmt.Errorf("Bridge creation failed: %v", err)
	}

	// Setup ip address for bridge
	addr, err := netlink.ParseAddr(ipAddr)
	if err != nil {
		return fmt.Errorf("Parsing address %s: %v", ipAddr, err)
	}
	if err := netlink.AddrAdd(br, addr); err != nil {
		return fmt.Errorf("Adding address %v to bridge %s failed: %v", addr, bridgeName, err)
	}

	// Validate that the IPAddress is there!
	_, err = getIfaceAddr(bridgeName)
	if err != nil {
		return fmt.Errorf("No IP address found on bridge %s", bridgeName)
	}

	// Add NAT rules for iptables
	if err = natOut(ipAddr, iptables.Insert); err != nil {
		return fmt.Errorf("Could not set NAT rules for bridge %s", bridgeName)
	}

	// Bring the bridge up
	if err := netlink.LinkSetUp(br); err != nil {
		return err
	}

	return nil
}
Exemple #9
0
/*   setup()
 */
func setup(pid int, brName string) error {
	br, e := netlink.LinkByName(brName)
	if e != nil {
		return fmt.Errorf("Host bridge not found: %v", e)
	}

	// Network interface names.
	hsIfName := fmt.Sprintf("veth%d", pid) // Host side interface
	nsIfName := "veth0"

	// Assign host side interface to bridge.
	linkAttrs := netlink.NewLinkAttrs()
	linkAttrs.Name = hsIfName
	linkAttrs.MasterIndex = br.Attrs().Index

	// Create interface pair.
	hsIf := &netlink.Veth{LinkAttrs: linkAttrs, PeerName: nsIfName}
	if e := netlink.LinkAdd(hsIf); e != nil {
		return fmt.Errorf("Failed to create veth pair: %v", e)
	}

	// Get namespace side interface handle.
	nsIf, e := netlink.LinkByName(nsIfName)
	if e != nil {
		netlink.LinkDel(hsIf)
		return fmt.Errorf("Failed to get namespace iface: %v", e)
	}

	// Attach network interface to namespace.
	if e := netlink.LinkSetNsPid(nsIf, pid); e != nil {
		netlink.LinkDel(hsIf)
		return fmt.Errorf("Failed to attach namespace iface: %v", e)
	}

	// Bring up host side interface.
	if e := netlink.LinkSetUp(hsIf); e != nil {
		netlink.LinkDel(hsIf)
		return fmt.Errorf("Failed to bring up host iface: %v", e)
	}

	return nil
}
Exemple #10
0
func bridgeCreate() {
	la := netlink.NewLinkAttrs()
	la.Name = "mycbridge"
	mybridge := &netlink.Bridge{la}
	err := netlink.LinkAdd(mybridge)
	if err != nil {
		panic(err)
	}

	addr, _ := netlink.ParseAddr("172.19.80.1/24")
	err = netlink.AddrAdd(mybridge, addr)
	if err != nil {
		panic(err)
	}

	err = netlink.LinkSetUp(mybridge)
	if err != nil {
		panic(err)
	}
}
Exemple #11
0
func NewBridgex() error {
	bridgeName := "dhcpbr0"
	la := netlink.NewLinkAttrs()
	la.Name = bridgeName
	bridge, _ := netlink.LinkByName(bridgeName)

	if bridge == nil {
		log.Debugf("Bridge %s does not exist ", bridgeName)
		bridge = &netlink.Bridge{la}
		err := netlink.LinkAdd(bridge)
		if err == nil {
			err = netlink.LinkSetUp(bridge)
			log.Debugf("Bridge %s has been added and enabled ", bridgeName)
		}
		return err
	} else {
		log.Debugf("Bridge %s already exsist", bridgeName)
	}

	return nil
}
Exemple #12
0
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.Debugf("Create endpoint request: %+v", create)
	log.Debugf("Create endpoint request interface: %+v", create.Interface)

	// create and attach local name to the bridge
	local, remote := veth(create.EndpointID)

	la := netlink.NewLinkAttrs()
	la.Name = local
	veth := &netlink.Veth{la, remote}

	if err := netlink.LinkAdd(veth); err != nil {
		log.Errorf("could not create veth pair: %s", err)
	}

	remoteTap, _ := netlink.LinkByName(remote)

	log.Debugf("remoteTap: %+v", remoteTap)
	mac := remoteTap.Attrs().HardwareAddr.String()
	log.Debugf("converted MacAddress: %s", mac)

	ifResult := &api.EndpointInterface{
		MacAddress: mac,
	}

	// IP addrs comes from libnetwork ipam via user 'docker network' parameters
	resp := &api.CreateEndpointResponse{
		Interface: ifResult,
	}
	log.Debugf("Create endpoint response: %+v", resp)
	log.Debugf("Create endpoint response interface: %+v", resp.Interface)
	objectResponse(w, resp)
}
Exemple #13
0
func (driver *driver) deleteEndpoint(w http.ResponseWriter, r *http.Request) {
	var delete api.DeleteEndpointRequest
	if err := json.NewDecoder(r.Body).Decode(&delete); err != nil {
		sendError(w, "Could not decode JSON encode payload", http.StatusBadRequest)
		return
	}

	// create and attach local name to the bridge
	local, remote := veth(delete.EndpointID)

	la := netlink.NewLinkAttrs()
	la.Name = local
	veth := &netlink.Veth{la, remote}

	if err := netlink.LinkDel(veth); err != nil {
		log.Errorf("could not create veth pair: %s", err)
	}

	log.Debugf("Delete endpoint request: %+v", &delete)
	emptyResponse(w)

	log.Debugf("Delete endpoint %s", delete.EndpointID)

}
Exemple #14
0
func (s *NetSetup) SetUpTest(c *C) {
	la := netlink.NewLinkAttrs()
	s.ifName = "testlinkfoo"
	la.Name = s.ifName
	runtime.LockOSThread()
	locked := true
	defer func() {
		if locked {
			runtime.UnlockOSThread()
		}
	}()

	globalNS, err := netns.Get()
	if err != nil {
		c.Fatalf("failed to get the global network namespace: %v", err)
	}
	s.globalNS = globalNS
	defer func() {
		netns.Set(globalNS)
	}()

	newNS, err := netns.New()
	if err != nil {
		c.Fatal("failed to create new network namespace")
	}
	s.newNS = newNS

	cmd := exec.Command("sleep", "infinity")
	if err = cmd.Start(); err != nil {
		c.Fatalf("failed to start the 'sleep 9999' process: %v", err)
	}
	s.cmd = cmd

	s.pid = cmd.Process.Pid

	if err = netns.Set(globalNS); err != nil {
		c.Fatalf("failed to return to the global netns: %v", err)
	}

	// the rest of the code should run without a locked thread
	if locked {
		runtime.UnlockOSThread()
		locked = false
	}

	dummy := &netlink.Dummy{LinkAttrs: la}
	if err := netlink.LinkAdd(dummy); err != nil {
		c.Fatalf("failed to add dummy interface: %v", err)
	}

	link, err := netlink.LinkByName(la.Name)
	if err != nil {
		c.Fatalf("failed to get interface by name: %v", err)
	}
	s.link = link

	netIf, err := net.InterfaceByName(la.Name)
	if err != nil {
		c.Fatalf("InterfaceByName failed: %v", err)
	}

	if netIf.Flags&net.FlagUp != 0 {
		c.Fatalf("expected interface to be down, but it's up")
	}
}