Exemplo n.º 1
0
func (lc *LocalCluster) NewTap(bridge string) (*TunTap, error) {
	nsExit, err := NsEnter(lc.nshandle)
	if err != nil {
		return nil, err
	}
	defer nsExit()

	tap, err := AddLinkTap("")
	if err != nil {
		return nil, fmt.Errorf("tap failed: %v", err)
	}

	err = netlink.LinkSetUp(tap)
	if err != nil {
		return nil, fmt.Errorf("tap up failed: %v", err)
	}

	br, err := netlink.LinkByName(bridge)
	if err != nil {
		return nil, fmt.Errorf("bridge failed: %v", err)
	}

	err = netlink.LinkSetMaster(tap, br.(*netlink.Bridge))
	if err != nil {
		return nil, fmt.Errorf("set master failed: %v", err)
	}

	return tap, nil
}
Exemplo n.º 2
0
func NewDnsmasq() (*Dnsmasq, error) {
	dm := &Dnsmasq{}
	for s := byte(0); s < numSegments; s++ {
		seg, err := newSegment(s)
		if err != nil {
			return nil, fmt.Errorf("Network setup failed: %v", err)
		}
		dm.Segments = append(dm.Segments, seg)
	}

	// setup lo
	lo, err := netlink.LinkByName("lo")
	if err != nil {
		return nil, fmt.Errorf("Network loopback setup failed: %v", err)
	}
	err = netlink.LinkSetUp(lo)
	if err != nil {
		return nil, fmt.Errorf("Network loopback setup failed: %v", err)
	}

	dm.dnsmasq = exec.Command("dnsmasq", "--conf-file=-")
	cfg, err := dm.dnsmasq.StdinPipe()
	if err != nil {
		return nil, err
	}
	out, err := dm.dnsmasq.StdoutPipe()
	if err != nil {
		return nil, err
	}
	dm.dnsmasq.Stderr = dm.dnsmasq.Stdout
	go util.LogFrom(capnslog.INFO, out)

	if err = dm.dnsmasq.Start(); err != nil {
		cfg.Close()
		return nil, err
	}

	if err = configTemplate.Execute(cfg, dm); err != nil {
		cfg.Close()
		dm.Destroy()
		return nil, err
	}
	cfg.Close()

	return dm, nil
}
Exemplo n.º 3
0
func newSegment(s byte) (*Segment, error) {
	seg := &Segment{
		BridgeName: fmt.Sprintf("br%d", s),
		BridgeIf:   newInterface(s, 1),
	}

	for i := byte(2); i < 2+numInterfaces; i++ {
		seg.Interfaces = append(seg.Interfaces, newInterface(s, i))
	}

	br := netlink.Bridge{
		LinkAttrs: netlink.LinkAttrs{
			Name:         seg.BridgeName,
			HardwareAddr: seg.BridgeIf.HardwareAddr,
		},
	}

	if err := netlink.LinkAdd(&br); err != nil {
		return nil, err
	}

	for _, addr := range seg.BridgeIf.DHCPv4 {
		nladdr := netlink.Addr{IPNet: &addr}
		if err := netlink.AddrAdd(&br, &nladdr); err != nil {
			return nil, err
		}
	}

	for _, addr := range seg.BridgeIf.DHCPv6 {
		nladdr := netlink.Addr{IPNet: &addr}
		if err := netlink.AddrAdd(&br, &nladdr); err != nil {
			return nil, err
		}
	}

	if err := netlink.LinkSetUp(&br); err != nil {
		return nil, err
	}

	return seg, nil
}