package rings import "net/netip" // RingZeroPrefix represents the backbone that connects gateways // of the different Ring 1 networks. // // The ring 0 network corresponds to what would be ring 2 for region_id 0. // 10.0.0.0-10.0.255.255 func RingZeroPrefix(region RegionID, zone ZoneID) (cidr netip.Prefix, err error) { switch { case !region.Valid(): err = ErrOutOfRange(region, "region") case !zone.Valid(): err = ErrOutOfRange(zone, "zone") default: addr := UnsafeRingZeroAddress(region, zone, 0) cidr = netip.PrefixFrom(addr, RingZeroBits) } return cidr, err } // RingZeroAddress returns a Ring 0 address for a particular node. // // A ring 0 address looks like 10.0.(region_id << 4 + zone_id).(node_id)/20 func RingZeroAddress(region RegionID, zone ZoneID, node NodeID) (addr netip.Addr, err error) { switch { case !region.Valid(): err = ErrOutOfRange(region, "region") case !zone.Valid(): err = ErrOutOfRange(zone, "zone") case !node.ValidZero(): err = ErrOutOfRange(node, "node") default: addr = UnsafeRingZeroAddress(region, zone, node) } return addr, err } // RingOnePrefix represents a (virtual) local network of a zone. // // Ring 1 is `10.(region_id).(zone_id << 4).(node_id)/20` network // grouped under what would be Ring 2 for region_id 0. // There are 12 bits worth of nodes but nodes under 255 are special // as they also get a slot on Ring 0. func RingOnePrefix(region RegionID, zone ZoneID) (cidr netip.Prefix, err error) { switch { case !region.Valid(): err = ErrOutOfRange(region, "region") case !zone.Valid(): err = ErrOutOfRange(zone, "zone") default: addr := UnsafeRingOneAddress(region, zone, 0) cidr = netip.PrefixFrom(addr, RingOneBits) } return cidr, err } // RingOneAddress returns a Ring 1 address for a particular node. // // A ring 1 address is `10.(region_id).(zone_id << 4).(node_id)/20` // but the node_id can take up to 12 bits. func RingOneAddress(region RegionID, zone ZoneID, node NodeID) (addr netip.Addr, err error) { switch { case !region.Valid(): err = ErrOutOfRange(region, "region") case !zone.Valid(): err = ErrOutOfRange(zone, "zone") case !node.Valid(): err = ErrOutOfRange(node, "node") default: addr = UnsafeRingOneAddress(region, zone, node) } return addr, err } // RingTwoPrefix represents the services of a cluster // // Ring 2 subnets are of the form `10.(region_id).0.0/20`, // using the address space that would belong to the ring 3 // region_id 0. func RingTwoPrefix(region RegionID) (cidr netip.Prefix, err error) { switch { case !region.Valid(): err = ErrOutOfRange(region, "region") default: addr := UnsafeRingTwoAddress(region, 0) cidr = netip.PrefixFrom(addr, RingTwoBits) } return cidr, err } // RingThreePrefix returns the subnet corresponding to // the pods of a cluster. // // Ring 3 is a `10.(region_id << 4).0.0/12` network func RingThreePrefix(region RegionID) (subnet netip.Prefix, err error) { switch { case !region.Valid(): err = ErrOutOfRange(region, "region") default: addr := UnsafeRingThreeAddress(region, 0) subnet = netip.PrefixFrom(addr, RingThreeBits) } return subnet, err } // UnsafeRingZeroAddress is equivalent ot RingZeroAddress but without validating // the input. func UnsafeRingZeroAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr { r := uint(region) z := uint(zone) n := uint(node) return AddrFrom4(10, 0, r<<4+z, n) } // UnsafeRingOneAddress is equivalent ot RingOneAddress but without validating // the input. func UnsafeRingOneAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr { r := uint(region) z := uint(zone) n := uint(node) n1 := n >> 8 n0 := n >> 0 return AddrFrom4(10, r, z<<4+n1, n0) } // UnsafeRingTwoAddress is equivalent ot RingTwoAddress but without validating // the input. func UnsafeRingTwoAddress(region RegionID, n uint) netip.Addr { r := uint(region) n1 := n >> 8 n0 := n >> 0 return AddrFrom4(10, r, n1, n0) } // UnsafeRingThreeAddress is equivalent ot RingThreeAddress but without validating // the input. func UnsafeRingThreeAddress(region RegionID, n uint) netip.Addr { r := uint(region) n2 := n >> 16 n1 := n >> 8 n0 := n >> 0 return AddrFrom4(10, r<<4+n2, n1, n0) }