Skip to content
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

Clean up routes module slightly #244

Closed
wants to merge 3 commits into from
Closed

Conversation

jD91mZM2
Copy link
Contributor

@jD91mZM2 jD91mZM2 commented Jun 19, 2018

  • Replace update with get_ref/get_mut
  • Rename ipv4 add_default_ipv4_route to set since it can only have one gateway
  • Add get_default_ipv4_route and get_default_ipv6_routes
  • Add remove_default_ipv4_route and clear_default_ipv6_routes

@whitequark
Copy link
Contributor

The update method is there so that any invariants on the internal ManagedMap can be enforced. But maybe we should not expose it at all, rather have an add_route method and such.

@jD91mZM2
Copy link
Contributor Author

I think update is fine, but there's no reason to have it take a closure :)

@jD91mZM2 jD91mZM2 changed the title Clean up routes module Clean up routes module slightly Jun 19, 2018
@whitequark
Copy link
Contributor

There is--if you just give away a &mut there is no way to maintain any internal invariants. There aren't any right now but that is likely wrong.

@jD91mZM2
Copy link
Contributor Author

What exactly do you mean by internal invariants?

@whitequark
Copy link
Contributor

Not every route is legal. For example, no multicast addresses should be added to the route table. This isn't checked currently, but should be, and it can't be checked with get_mut.

@jD91mZM2
Copy link
Contributor Author

Aaaaaah okay, that explains it. I'll implement your add_route idea

@progval
Copy link
Contributor

progval commented Jun 21, 2018

The update method is inspired from the one in ethernet.rs, so you might want to change it as well.

@jD91mZM2
Copy link
Contributor Author

There wasn't much to do in ethernet.rs unfortunately since a ManagedSlice cannot be pushed/removed to.

f(&mut self.inner.ip_addrs);
InterfaceInner::check_ip_addrs(&self.inner.ip_addrs)
pub fn set_ip_addrs<S>(&mut self, ips: S)
where S: Into<ManagedSlice<'c, IpCidr>>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this method is to be able to update IP addresses without assigning a different ManagedSlice. It's for memory-constrained devices without an allocator.

Copy link
Contributor Author

@jD91mZM2 jD91mZM2 Jun 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I make a way to set an ip based on index? Or revert the change to that file?

@@ -195,9 +248,7 @@ mod test {
via_router: ADDR_1A.into(),
preferred_until: None, expires_at: None,
};
routes.update(|storage| {
storage.insert(cidr_1().into(), route).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what does this achieve? One line less in route setup code? I'm not convinced it's worth adding so many functions in the public API (and you don't even validate input in some of them).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, imagine you want to fetch the default gateway with the current code:

let mut gateway = None;
routes.update(|storage| {
     let cidr = IpCidr::new(IpAddress::v4(0, 0, 0, 0), 0);
    gateway = Some(storage.get(&cidr).clone());
});
let gateway = gateway.unwrap();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the redox netstack code after this PR.

(Your existing code doesn't validate input either btw)

Err((_cidr, _route)) => Err(Error::Exhausted)
}
}
/// Returns a route registered on cidr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring isn't informative at all. What does "registered on cidr" mean? It's not clear from the documentation whether this function would only return a route that is an exact match or any route matching the argument.

Also, "CIDR" should be capitalized, it's an acronym.

Also, there's a missing newline.

pub fn get_route(&mut self, cidr: &IpCidr) -> Option<&Route> {
self.storage.get(cidr)
}
/// Returns a mutable reference to the route registered by cidr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

}
/// Delete a route.
///
/// On success, returns the value it deleted, if any.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returns the route it deleted, if any

/// Delete a route.
///
/// On success, returns the value it deleted, if any.
pub fn del_route(&mut self, cidr: &IpCidr) -> Option<Route> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be called delete_route (or remove_route; not del_route).

/// Add a new route.
///
/// On success, return the previous route, if any.
pub fn add_route(&mut self, cidr: IpCidr, route: Route) -> Result<Option<Route>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming of this function is inconsistent with set_*_default_route. Either this should be called set_route, or those functions should be called add_*_default_route. They fundamentally do the same thing.

@@ -105,6 +141,23 @@ impl<'a> Routes<'a> {
}
}

/// Get the current default ipv6 gateways, if any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPv6 gateways

#[cfg(feature = "proto-ipv6")]
pub fn get_default_ipv6_routes(&mut self) -> impl Iterator<Item = &Route> {
let cidr = IpCidr::new(IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 0), 0);
// TODO: Support multiple default ipv6 gateways
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a TODO if you already return an iterator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I return an iterator over one thing, but I heard that in the future you might support multiple

self.storage.get(&cidr).into_iter()
}

/// Clear default ipv6 gateways.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPv6 gateways


/// Clear default ipv6 gateways.
///
/// On success, returns the cleared routes, if any.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function returns exactly one route.

#[cfg(feature = "proto-ipv6")]
pub fn clear_default_ipv6_routes(&mut self) -> Option<Route> {
let cidr = IpCidr::new(IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 0), 0);
self.storage.remove(&cidr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should use self.remove_route.

@jD91mZM2
Copy link
Contributor Author

Since you previously said you don't think this is needed, I'll just close this because I'm lazy 😛. Better now than later when I put in more time into something that isn't even desired.

@jD91mZM2 jD91mZM2 closed this Jul 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants