Ejemplo n.º 1
0
func (v *veth) initialize(config *network) error {
	peer := config.TempVethPeerName
	if peer == "" {
		return fmt.Errorf("peer is not specified")
	}
	child, err := net.InterfaceByName(peer)
	if err != nil {
		return err
	}
	if err := netlink.NetworkLinkDown(child); err != nil {
		return err
	}
	if err := netlink.NetworkChangeName(child, config.Name); err != nil {
		return err
	}
	// get the interface again after we changed the name as the index also changes.
	if child, err = net.InterfaceByName(config.Name); err != nil {
		return err
	}
	if config.MacAddress != "" {
		if err := netlink.NetworkSetMacAddress(child, config.MacAddress); err != nil {
			return err
		}
	}
	ip, ipNet, err := net.ParseCIDR(config.Address)
	if err != nil {
		return err
	}
	if err := netlink.NetworkLinkAddIp(child, ip, ipNet); err != nil {
		return err
	}
	if config.IPv6Address != "" {
		if ip, ipNet, err = net.ParseCIDR(config.IPv6Address); err != nil {
			return err
		}
		if err := netlink.NetworkLinkAddIp(child, ip, ipNet); err != nil {
			return err
		}
	}
	if err := netlink.NetworkSetMTU(child, config.Mtu); err != nil {
		return err
	}
	if err := netlink.NetworkLinkUp(child); err != nil {
		return err
	}
	if config.Gateway != "" {
		if err := netlink.AddDefaultGw(config.Gateway, config.Name); err != nil {
			return err
		}
	}
	if config.IPv6Gateway != "" {
		if err := netlink.AddDefaultGw(config.IPv6Gateway, config.Name); err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 2
0
// SetPeerLinkNetInNs configures peer link's IP network in network namespace specified by PID
func (veth *VethPair) SetPeerLinkNetInNs(nspid int, ip net.IP, network *net.IPNet, gw *net.IP) error {
	origNs, _ := NetNsHandle(os.Getpid())
	defer syscall.Close(int(origNs))
	defer system.Setns(origNs, syscall.CLONE_NEWNET)

	if err := SetNetNsToPid(nspid); err != nil {
		return fmt.Errorf("Setting network namespace failed: %s", err)
	}

	if err := netlink.NetworkLinkAddIp(veth.peerIfc, ip, network); err != nil {
		return fmt.Errorf("Unable to set IP: %s in pid: %d network namespace", ip.String(), nspid)
	}

	if err := netlink.NetworkLinkUp(veth.peerIfc); err != nil {
		return fmt.Errorf("Unable to bring %s interface UP: %s", veth.peerIfc.Name, nspid)
	}

	if gw != nil {
		if err := netlink.AddDefaultGw(gw.String(), veth.peerIfc.Name); err != nil {
			return fmt.Errorf("Unable to set Default gateway: %s in pid: %d network namespace", gw.String(), nspid)
		}
	}

	return nil
}
Ejemplo n.º 3
0
func main() {
	log.SetFlags(log.Lshortfile)

	iface, err := net.InterfaceByName("eth0")
	if err != nil {
		log.Fatal(err)
	}
	ip, ipNet, _ := net.ParseCIDR("192.168.122.128/24")
	if err := netlink.NetworkLinkAddIp(iface, ip, ipNet); err != nil {
		log.Fatal(err)
	}
	if err := netlink.AddDefaultGw("192.168.122.1", "eth0"); err != nil {
		log.Fatal(err)
	}
	if err := netlink.NetworkLinkUp(iface); err != nil {
		log.Fatal(err)
	}
	start := time.Now()
	arping.SetTimeout(100 * time.Millisecond)
	for {
		addr, t, err := arping.PingOverIface(net.ParseIP("192.168.122.1"), *iface)
		if err != nil {
			log.Println("ARP error:", err)
			continue
		}
		log.Printf("ARP success after %s: %v %v", time.Now().Sub(start), addr, t)
		return
	}
}
Ejemplo n.º 4
0
Archivo: init.go Proyecto: NERSC/docker
// Setup networking
func setupNetworking(args *InitArgs) error {
	if args.Ip != "" {
		// eth0
		iface, err := net.InterfaceByName("eth0")
		if err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}
		ip, ipNet, err := net.ParseCIDR(args.Ip)
		if err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}
		if err := netlink.NetworkLinkAddIp(iface, ip, ipNet); err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}
		if err := netlink.NetworkSetMTU(iface, args.Mtu); err != nil {
			return fmt.Errorf("Unable to set MTU: %v", err)
		}
		if err := netlink.NetworkLinkUp(iface); err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}

		// loopback
		iface, err = net.InterfaceByName("lo")
		if err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}
		if err := netlink.NetworkLinkUp(iface); err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}
	}
	if args.Gateway != "" {
		gw := net.ParseIP(args.Gateway)
		if gw == nil {
			return fmt.Errorf("Unable to set up networking, %s is not a valid gateway IP", args.Gateway)
		}

		if err := netlink.AddDefaultGw(gw.String(), "eth0"); err != nil {
			return fmt.Errorf("Unable to set up networking: %v", err)
		}
	}

	return nil
}
Ejemplo n.º 5
0
func SetDefaultGateway(ip, ifaceName string) error {
	return netlink.AddDefaultGw(ip, ifaceName)
}
Ejemplo n.º 6
0
func SetDefaultGateway(ip string) error {
	return netlink.AddDefaultGw(net.ParseIP(ip))
}
Ejemplo n.º 7
0
func (Link) AddDefaultGW(intf *net.Interface, ip net.IP) error {
	netlinkMu.Lock()
	defer netlinkMu.Unlock()

	return errF(netlink.AddDefaultGw(ip.String(), intf.Name))
}
Ejemplo n.º 8
0
// SetLinkDefaultGw configures the link's default Gateway.
// It is equivalent of running: ip route add default via ${ip address}
func (l *Link) SetLinkDefaultGw(gw *net.IP) error {
	return netlink.AddDefaultGw(gw.String(), l.NetInterface().Name)
}