-
Notifications
You must be signed in to change notification settings - Fork 62
[internal-dns][sled agent] Assign IPs to DNS service from reserved subnet #965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
79fb8f3
Add internal-dns service
smklein 1a15d6c
fmt
smklein 4faef91
wip
smklein 5351d85
Merge branch 'main' into service-discovery
smklein 8f373bd
Added dnsadm
smklein 62c5778
Merge branch 'service-discovery' into service-discovery-in-a-zone
smklein a575e42
Add internal-dns SMF config, start it by RSS
smklein 39758c0
Merge branch 'main' into service-discovery
smklein f814759
Merge branch 'service-discovery' into service-discovery-in-a-zone
smklein 4ca5c80
Merge branch 'main' into service-discovery
smklein 789e274
Merge branch 'service-discovery' into service-discovery-in-a-zone
smklein a52e4b6
review feedback
smklein 73c0008
Merge branch 'main' into service-discovery-in-a-zone
smklein a9840d6
Patch addresses
smklein 437d699
Updated cfg path
smklein 4dc45ff
patch addresses
smklein 528204d
Add support for 'make GZ address', add DNS addrs
smklein bea8c7e
Add some tests
smklein fcbc0ab
Correctly passing addresses, GZ addresses to DNS service for setup
smklein f214fcf
Avoid specifying port when not necessary
smklein baea4a8
safer vec access, better errors
smklein 58744c4
fmt
smklein 77b8840
Merge branch 'main' into service-discovery-in-a-zone
smklein c1e2180
updated storage path
smklein e8f98ad
Merge branch 'service-discovery-in-a-zone' into use-service-discovery
smklein 39431c6
fix tests, clippy
smklein 22dfb79
Fix another test
smklein 9f7f55b
Bunyan formatted
smklein 802f4e5
Merge branch 'service-discovery-in-a-zone' into internal-dns-assigned…
smklein 981f744
Regenerate bindings
smklein 29a1a37
Merge branch 'main' into service-discovery-in-a-zone
smklein 432e368
Merge branch 'service-discovery-in-a-zone' into internal-dns-assigned…
smklein 638d99c
Merge branch 'main' into internal-dns-assigned-ips
smklein 857fe85
Const generic subnet prefix
smklein be3bc1b
Ipv6, comments
smklein 70fbc9f
Merge branch 'main' into internal-dns-assigned-ips
smklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| //! Common IP addressing functionality. | ||
| //! | ||
| //! This addressing functionality is shared by both initialization services | ||
| //! and Nexus, who need to agree upon addressing schemes. | ||
|
|
||
| use crate::api::external::Ipv6Net; | ||
| use ipnetwork::Ipv6Network; | ||
| use schemars::JsonSchema; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::net::{Ipv6Addr, SocketAddrV6}; | ||
|
|
||
| pub const AZ_PREFIX: u8 = 48; | ||
| pub const RACK_PREFIX: u8 = 56; | ||
| pub const SLED_PREFIX: u8 = 64; | ||
|
|
||
| /// The amount of redundancy for DNS servers. | ||
| /// | ||
| /// Must be less than MAX_DNS_REDUNDANCY. | ||
| pub const DNS_REDUNDANCY: usize = 1; | ||
| /// The maximum amount of redundancy for DNS servers. | ||
| /// | ||
| /// This determines the number of addresses which are | ||
| /// reserved for DNS servers. | ||
| pub const MAX_DNS_REDUNDANCY: usize = 5; | ||
|
|
||
| pub const DNS_PORT: u16 = 53; | ||
| pub const DNS_SERVER_PORT: u16 = 5353; | ||
| pub const SLED_AGENT_PORT: u16 = 12345; | ||
|
|
||
| // Anycast is a mechanism in which a single IP address is shared by multiple | ||
| // devices, and the destination is located based on routing distance. | ||
| // | ||
| // This is covered by RFC 4291 in much more detail: | ||
| // <https://datatracker.ietf.org/doc/html/rfc4291#section-2.6> | ||
| // | ||
| // Anycast addresses are always the "zeroeth" address within a subnet. We | ||
| // always explicitly skip these addresses within our network. | ||
| const _ANYCAST_ADDRESS_INDEX: usize = 0; | ||
| const DNS_ADDRESS_INDEX: usize = 1; | ||
| const GZ_ADDRESS_INDEX: usize = 2; | ||
|
|
||
| /// Wraps an [`Ipv6Network`] with a compile-time prefix length. | ||
| #[derive(Debug, Clone, Copy, JsonSchema, Serialize, Deserialize, PartialEq)] | ||
| pub struct Ipv6Subnet<const N: u8> { | ||
| net: Ipv6Net, | ||
| } | ||
|
|
||
| impl<const N: u8> Ipv6Subnet<N> { | ||
| pub fn new(addr: Ipv6Addr) -> Self { | ||
| // Create a network with the compile-time prefix length. | ||
| let net = Ipv6Network::new(addr, N).unwrap(); | ||
| // Ensure the address is set to within-prefix only components. | ||
| let net = Ipv6Network::new(net.network(), N).unwrap(); | ||
| Self { net: Ipv6Net(net) } | ||
| } | ||
|
|
||
| /// Returns the underlying network. | ||
| pub fn net(&self) -> Ipv6Network { | ||
| self.net.0 | ||
| } | ||
| } | ||
|
|
||
| /// Represents a subnet which may be used for contacting DNS services. | ||
| #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] | ||
| pub struct DnsSubnet { | ||
| subnet: Ipv6Subnet<SLED_PREFIX>, | ||
| } | ||
|
|
||
| impl DnsSubnet { | ||
| /// Returns the DNS server address within the subnet. | ||
| /// | ||
| /// This is the first address within the subnet. | ||
| pub fn dns_address(&self) -> Ipv6Network { | ||
| Ipv6Network::new( | ||
| self.subnet.net().iter().nth(DNS_ADDRESS_INDEX).unwrap(), | ||
| SLED_PREFIX, | ||
| ) | ||
| .unwrap() | ||
| } | ||
|
|
||
| /// Returns the address which the Global Zone should create | ||
| /// to be able to contact the DNS server. | ||
| /// | ||
| /// This is the second address within the subnet. | ||
| pub fn gz_address(&self) -> Ipv6Network { | ||
| Ipv6Network::new( | ||
| self.subnet.net().iter().nth(GZ_ADDRESS_INDEX).unwrap(), | ||
| SLED_PREFIX, | ||
| ) | ||
| .unwrap() | ||
| } | ||
| } | ||
|
|
||
| /// A wrapper around an IPv6 network, indicating it is a "reserved" rack | ||
| /// subnet which can be used for AZ-wide services. | ||
| #[derive(Debug, Clone)] | ||
| pub struct ReservedRackSubnet(pub Ipv6Subnet<RACK_PREFIX>); | ||
|
|
||
| impl ReservedRackSubnet { | ||
| /// Returns the subnet for the reserved rack subnet. | ||
| pub fn new(subnet: Ipv6Subnet<AZ_PREFIX>) -> Self { | ||
| ReservedRackSubnet(Ipv6Subnet::<RACK_PREFIX>::new(subnet.net().ip())) | ||
| } | ||
|
|
||
| /// Returns the DNS addresses from this reserved rack subnet. | ||
| /// | ||
| /// These addresses will come from the first [`DNS_REDUNDANCY`] `/64s` of the | ||
| /// [`RACK_PREFIX`] subnet. | ||
| pub fn get_dns_subnets(&self) -> Vec<DnsSubnet> { | ||
| (0..DNS_REDUNDANCY) | ||
| .map(|idx| { | ||
| let subnet = | ||
| get_64_subnet(self.0, u8::try_from(idx + 1).unwrap()); | ||
| DnsSubnet { subnet } | ||
| }) | ||
| .collect() | ||
| } | ||
| } | ||
|
|
||
| const SLED_AGENT_ADDRESS_INDEX: usize = 1; | ||
|
|
||
| /// Return the sled agent address for a subnet. | ||
| /// | ||
| /// This address will come from the first address of the [`SLED_PREFIX`] subnet. | ||
| pub fn get_sled_address(sled_subnet: Ipv6Subnet<SLED_PREFIX>) -> SocketAddrV6 { | ||
| let sled_agent_ip = | ||
| sled_subnet.net().iter().nth(SLED_AGENT_ADDRESS_INDEX).unwrap(); | ||
| SocketAddrV6::new(sled_agent_ip, SLED_AGENT_PORT, 0, 0) | ||
| } | ||
|
|
||
| /// Returns a sled subnet within a rack subnet. | ||
| /// | ||
| /// The subnet at index == 0 is used for rack-local services. | ||
| pub fn get_64_subnet( | ||
| rack_subnet: Ipv6Subnet<RACK_PREFIX>, | ||
| index: u8, | ||
| ) -> Ipv6Subnet<SLED_PREFIX> { | ||
| let mut rack_network = rack_subnet.net().network().octets(); | ||
|
|
||
| // To set bits distinguishing the /64 from the /56, we modify the 7th octet. | ||
| rack_network[7] = index; | ||
| Ipv6Subnet::<SLED_PREFIX>::new(Ipv6Addr::from(rack_network)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_dns_subnets() { | ||
| let subnet = Ipv6Subnet::<AZ_PREFIX>::new( | ||
| "fd00:1122:3344:0100::".parse::<Ipv6Addr>().unwrap(), | ||
| ); | ||
| let rack_subnet = ReservedRackSubnet::new(subnet); | ||
|
|
||
| assert_eq!( | ||
| // Note that these bits (indicating the rack) are zero. | ||
| // vv | ||
| "fd00:1122:3344:0000::/56".parse::<Ipv6Network>().unwrap(), | ||
| rack_subnet.0.net(), | ||
| ); | ||
|
|
||
| // Observe the first DNS subnet within this reserved rack subnet. | ||
| let dns_subnets = rack_subnet.get_dns_subnets(); | ||
| assert_eq!(DNS_REDUNDANCY, dns_subnets.len()); | ||
|
|
||
| // The DNS address and GZ address should be only differing by one. | ||
| assert_eq!( | ||
| "fd00:1122:3344:0001::1/64".parse::<Ipv6Network>().unwrap(), | ||
| dns_subnets[0].dns_address(), | ||
| ); | ||
| assert_eq!( | ||
| "fd00:1122:3344:0001::2/64".parse::<Ipv6Network>().unwrap(), | ||
| dns_subnets[0].gz_address(), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sled_address() { | ||
| let subnet = Ipv6Subnet::<SLED_PREFIX>::new( | ||
| "fd00:1122:3344:0101::".parse::<Ipv6Addr>().unwrap(), | ||
| ); | ||
| assert_eq!( | ||
| "[fd00:1122:3344:0101::1]:12345".parse::<SocketAddrV6>().unwrap(), | ||
| get_sled_address(subnet) | ||
| ); | ||
|
|
||
| let subnet = Ipv6Subnet::<SLED_PREFIX>::new( | ||
| "fd00:1122:3344:0308::".parse::<Ipv6Addr>().unwrap(), | ||
| ); | ||
| assert_eq!( | ||
| "[fd00:1122:3344:0308::1]:12345".parse::<SocketAddrV6>().unwrap(), | ||
| get_sled_address(subnet) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.