Просмотр исходного кода

Merge pull request #12 from eyakubovich/master

Config attributes renaming. Use a new port UDP 8285.
Eugene Yakubovich 10 лет назад
Родитель
Сommit
0a42a413b5
6 измененных файлов с 36 добавлено и 35 удалено
  1. 7 7
      README.md
  2. 1 1
      main.go
  3. 20 16
      subnet/config.go
  4. 4 4
      subnet/subnet.go
  5. 2 5
      udp/cproxy.go
  6. 2 2
      udp/proxy.go

+ 7 - 7
README.md

@@ -25,14 +25,14 @@ The value of the config should be a JSON dictionary with the following keys:
 * ```Network``` (string): IPv4 network in CIDR format to use for the entire overlay network. This
 is the only mandatory key.
 
-* ```HostSubnet``` (number): The size of the subnet allocated to each host. Defaults to 24 (i.e. /24) unless
-the Network was configured to be less than a /24 in which case it is one less than the network.
+* ```SubnetLen``` (number): The size of the subnet allocated to each host. Defaults to 24 (i.e. /24) unless
+the Network was configured to be smaller than a /24 in which case it is one less than the network.
 
-* ```FirstIP``` (string): The beginning of IP range which the subnet allocation should start with. Defaults
-to the value of Network.
+* ```SubnetMin``` (string): The beginning of IP range which the subnet allocation should start with. Defaults
+to the first subnet of Network.
 
-* ```LastIP``` (string): The end of the IP range at which the subnet allocation should end with. Defaults to
-one host-sized subnet prior to end of Network range.
+* ```SubnetMax``` (string): The end of the IP range at which the subnet allocation should end with. Defaults to
+the last subnet of Network.
 
 ## Running
 
@@ -52,7 +52,7 @@ MTU that it supports.
 -etcd-endpoint="http://127.0.0.1:4001": etcd endpoint
 -etcd-prefix="/coreos.com/network": etcd prefix
 -iface="": interface to use (IP or name) for inter-host communication. Defaults to the interface for the default route on the machine.
--port=4242: port to use for inter-node communications
+-port=8285: UDP port to use for inter-node communications
 -subnet-file="/run/kolach/subnet.env": filename where env variables (subnet and MTU values) will be written to
 -v=0: log level for V logs. Set to 1 to see messages related to data path
 ```

+ 1 - 1
main.go

@@ -18,7 +18,7 @@ import (
 )
 
 const (
-	defaultPort = 4242
+	defaultPort = 8285
 )
 
 type CmdLineOpts struct {

+ 20 - 16
subnet/config.go

@@ -9,9 +9,9 @@ import (
 
 type Config struct {
 	Network    pkg.IP4Net
-	FirstIP    pkg.IP4
-	LastIP     pkg.IP4
-	HostSubnet uint
+	SubnetMin  pkg.IP4
+	SubnetMax  pkg.IP4
+	SubnetLen  uint
 }
 
 func ParseConfig(s string) (*Config, error) {
@@ -21,31 +21,35 @@ func ParseConfig(s string) (*Config, error) {
 		return nil, err
 	}
 
-	if cfg.HostSubnet > 0 {
-		if cfg.HostSubnet < cfg.Network.PrefixLen {
+	if cfg.SubnetLen > 0 {
+		if cfg.SubnetLen < cfg.Network.PrefixLen {
 			return nil, errors.New("HostSubnet is larger network than Network")
 		}
 	} else {
 		// try to give each host a /24 but if the whole network
 		// is /24 or smaller, half the network
 		if cfg.Network.PrefixLen < 24 {
-			cfg.HostSubnet = 24
+			cfg.SubnetLen = 24
 		} else {
-			cfg.HostSubnet = cfg.Network.PrefixLen + 1
+			cfg.SubnetLen = cfg.Network.PrefixLen + 1
 		}
 	}
 
-	if cfg.FirstIP == pkg.IP4(0) {
-		cfg.FirstIP = cfg.Network.IP
-	} else if !cfg.Network.Contains(cfg.FirstIP) {
-		return nil, errors.New("FirstIP is not in the range of the Network")
+	subnetSize := pkg.IP4(1 << (32 - cfg.SubnetLen))
+
+	if cfg.SubnetMin == pkg.IP4(0) {
+		// skip over the first subnet otherwise it causes problems. e.g.
+		// if Network is 10.100.0.0/16, having an interface with 10.0.0.0
+		// makes ping think it's a broadcast address (not sure why)
+		cfg.SubnetMin = cfg.Network.IP + subnetSize
+	} else if !cfg.Network.Contains(cfg.SubnetMin) {
+		return nil, errors.New("SubnetMin is not in the range of the Network")
 	}
 
-	if cfg.LastIP == pkg.IP4(0) {
-		cfg.LastIP = cfg.Network.Next().IP
-		cfg.LastIP -= (1 << (32 - cfg.HostSubnet))
-	} else if !cfg.Network.Contains(cfg.LastIP) {
-		return nil, errors.New("LastIP is not in the range of the Network")
+	if cfg.SubnetMax == pkg.IP4(0) {
+		cfg.SubnetMax = cfg.Network.Next().IP - subnetSize
+	} else if !cfg.Network.Contains(cfg.SubnetMax) {
+		return nil, errors.New("SubnetMax is not in the range of the Network")
 	}
 
 	return cfg, nil

+ 4 - 4
subnet/subnet.go

@@ -305,13 +305,13 @@ type BaseAttrs struct {
 }
 
 func (sm *SubnetManager) allocateSubnet() (pkg.IP4Net, error) {
-	log.Infof("Picking subnet in range %s ... %s", sm.config.FirstIP, sm.config.LastIP)
+	log.Infof("Picking subnet in range %s ... %s", sm.config.SubnetMin, sm.config.SubnetMax)
 
 	var bag []pkg.IP4
-	sn := pkg.IP4Net{sm.config.FirstIP, sm.config.HostSubnet}
+	sn := pkg.IP4Net{sm.config.SubnetMin, sm.config.SubnetLen}
 
 OuterLoop:
-	for ; sn.IP <= sm.config.LastIP && len(bag) < 100; sn = sn.Next() {
+	for ; sn.IP <= sm.config.SubnetMax && len(bag) < 100; sn = sn.Next() {
 		for _, l := range sm.leases {
 			if sn.Overlaps(l.Network) {
 				continue OuterLoop
@@ -324,7 +324,7 @@ OuterLoop:
 		return pkg.IP4Net{}, errors.New("out of subnets")
 	} else {
 		i := pkg.RandInt(0, len(bag))
-		return pkg.IP4Net{bag[i], sm.config.HostSubnet}, nil
+		return pkg.IP4Net{bag[i], sm.config.SubnetLen}, nil
 	}
 }
 

+ 2 - 5
udp/cproxy.go

@@ -22,7 +22,6 @@ func runCProxy(tun *os.File, conn *os.File, ctl *os.File, tunIP pkg.IP4) {
 	if log.V(1) {
 		log_errors = 1
 	}
-	log.Info("C.run_proxy: log_errors: ", log_errors)
 
 	C.run_proxy(
 		C.int(tun.Fd()),
@@ -31,8 +30,6 @@ func runCProxy(tun *os.File, conn *os.File, ctl *os.File, tunIP pkg.IP4) {
 		C.in_addr_t(tunIP.NetworkOrder()),
 		C.int(log_errors),
 	)
-
-	log.Info("C.run_proxy exited")
 }
 
 func writeCommand(f *os.File, cmd *C.command) {
@@ -102,7 +99,7 @@ func fastProxy(sm *subnet.SubnetManager, tun *os.File, conn *net.UDPConn, tunIP
 				writeCommand(ctl, &cmd)
 
 			} else if evt.Type == subnet.SubnetRemoved {
-				log.Info("Subnet removed: %v", evt.Lease.Network)
+				log.Info("Subnet removed: ", evt.Lease.Network)
 
 				cmd := C.command{
 					cmd:          C.CMD_DEL_ROUTE,
@@ -113,7 +110,7 @@ func fastProxy(sm *subnet.SubnetManager, tun *os.File, conn *net.UDPConn, tunIP
 				writeCommand(ctl, &cmd)
 
 			} else {
-				log.Errorf("Internal error: unknown event type: %d", int(evt.Type))
+				log.Error("Internal error: unknown event type: ", int(evt.Type))
 			}
 		}
 	}

+ 2 - 2
udp/proxy.go

@@ -127,11 +127,11 @@ func proxy(sm *subnet.SubnetManager, tun *os.File, conn *net.UDPConn, port int)
 				rtr.SetRoute(evt.Lease.Network, attrs.PublicIP)
 
 			} else if evt.Type == subnet.SubnetRemoved {
-				log.Info("Subnet removed: %v", evt.Lease.Network)
+				log.Info("Subnet removed: ", evt.Lease.Network)
 				rtr.DelRoute(evt.Lease.Network)
 
 			} else {
-				log.Errorf("Internal error: unknown event type: %d", int(evt.Type))
+				log.Error("Internal error: unknown event type: ", int(evt.Type))
 			}
 		}
 	}