-
Notifications
You must be signed in to change notification settings - Fork 40
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
6750 Add support for post-install LLDP configuration #7132
Open
Nieuwejaar
wants to merge
1
commit into
main
Choose a base branch
from
lldp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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,178 @@ | ||
use super::DataStore; | ||
use crate::context::OpContext; | ||
use crate::db; | ||
use crate::db::error::public_error_from_diesel; | ||
use crate::db::error::ErrorHandler; | ||
use crate::db::model::LldpLinkConfig; | ||
use async_bb8_diesel::AsyncRunQueryDsl; | ||
use chrono::Utc; | ||
use diesel::ExpressionMethods; | ||
use diesel::QueryDsl; | ||
use diesel::SelectableHelper; | ||
use ipnetwork::IpNetwork; | ||
use omicron_common::api::external; | ||
use omicron_common::api::external::Error; | ||
use omicron_common::api::external::LookupResult; | ||
use omicron_common::api::external::Name; | ||
use omicron_common::api::external::ResourceType; | ||
use omicron_common::api::external::UpdateResult; | ||
use uuid::Uuid; | ||
|
||
// The LLDP configuration has been defined as a leaf of the switch-port-settings | ||
// tree, and is identified in the database with a UUID stored in that tree. | ||
// Using the uuid as the target argument for the config operations would be | ||
// reasonable, and similar in spirit to the link configuration operations. | ||
// | ||
// On the other hand, the neighbors are discovered on a configured link, but the | ||
// data is otherwise completely independent of the configuration. Furthermore, | ||
// the questions answered by the neighbor information have to do with the | ||
// physical connections between the Oxide rack and the upstream, datacenter | ||
// switch. Accordingly, it seems more appropriate to use the physical | ||
// rack/switch/port triple to identify the port of interest for the neighbors | ||
// query. | ||
// | ||
// For consistency across the lldp operations, all use rack/switch/port rather | ||
// than the uuid. | ||
// XXX: Is this the right call? The other options being: uuid for all | ||
// operations, or uuid for config and r/s/p for neighbors. | ||
impl DataStore { | ||
/// Look up the settings id for this port in the switch_port table by its | ||
/// rack/switch/port triple, and then use that id to look up the lldp | ||
/// config id in the switch_port_settings_link_config table. | ||
async fn lldp_config_id_get( | ||
&self, | ||
opctx: &OpContext, | ||
rack_id: Uuid, | ||
switch_location: Name, | ||
port_name: Name, | ||
) -> LookupResult<Uuid> { | ||
use db::schema::switch_port; | ||
use db::schema::switch_port::dsl as switch_port_dsl; | ||
use db::schema::switch_port_settings_link_config; | ||
use db::schema::switch_port_settings_link_config::dsl as config_dsl; | ||
|
||
let conn = self.pool_connection_authorized(opctx).await?; | ||
|
||
let port_settings_id: Uuid = switch_port_dsl::switch_port | ||
.filter(switch_port::rack_id.eq(rack_id)) | ||
.filter( | ||
switch_port::switch_location.eq(switch_location.to_string()), | ||
) | ||
.filter(switch_port::port_name.eq(port_name.to_string())) | ||
.select(switch_port::port_settings_id) | ||
.limit(1) | ||
.first_async::<Option<Uuid>>(&*conn) | ||
.await | ||
.map_err(|_| { | ||
Error::not_found_by_name(ResourceType::SwitchPort, &port_name) | ||
})? | ||
.ok_or(Error::invalid_value( | ||
"settings", | ||
"switch port not yet configured".to_string(), | ||
))?; | ||
|
||
let lldp_id: Uuid = config_dsl::switch_port_settings_link_config | ||
.filter( | ||
switch_port_settings_link_config::port_settings_id | ||
.eq(port_settings_id), | ||
) | ||
.select(switch_port_settings_link_config::lldp_link_config_id) | ||
.limit(1) | ||
.first_async::<Option<Uuid>>(&*conn) | ||
.await | ||
.map_err(|_| { | ||
Error::not_found_by_id( | ||
ResourceType::SwitchPortSettings, | ||
&port_settings_id, | ||
) | ||
})? | ||
.ok_or(Error::invalid_value( | ||
"settings", | ||
"lldp not configured for this port".to_string(), | ||
))?; | ||
Ok(lldp_id) | ||
} | ||
|
||
/// Fetch the current LLDP configuration settings for the link identified | ||
/// using the rack/switch/port triple. | ||
pub async fn lldp_config_get( | ||
&self, | ||
opctx: &OpContext, | ||
rack_id: Uuid, | ||
switch_location: Name, | ||
port_name: Name, | ||
) -> LookupResult<external::LldpLinkConfig> { | ||
use db::schema::lldp_link_config; | ||
use db::schema::lldp_link_config::dsl; | ||
|
||
let id = self | ||
.lldp_config_id_get(opctx, rack_id, switch_location, port_name) | ||
.await?; | ||
|
||
let conn = self.pool_connection_authorized(opctx).await?; | ||
dsl::lldp_link_config | ||
.filter(lldp_link_config::id.eq(id)) | ||
.select(LldpLinkConfig::as_select()) | ||
.limit(1) | ||
.first_async::<LldpLinkConfig>(&*conn) | ||
.await | ||
.map(|config| config.into()) | ||
.map_err(|e| { | ||
let msg = "failed to lookup lldp config by id"; | ||
error!(opctx.log, "{msg}"; "error" => ?e); | ||
|
||
match e { | ||
diesel::result::Error::NotFound => Error::not_found_by_id( | ||
ResourceType::LldpLinkConfig, | ||
&id, | ||
), | ||
_ => Error::internal_error(msg), | ||
} | ||
}) | ||
} | ||
|
||
/// Update the current LLDP configuration settings for the link identified | ||
/// using the rack/switch/port triple. n.b.: each link is given an empty | ||
/// configuration structure at link creation time, so there are no | ||
/// lldp config create/delete operations. | ||
pub async fn lldp_config_update( | ||
&self, | ||
opctx: &OpContext, | ||
rack_id: Uuid, | ||
switch_location: Name, | ||
port_name: Name, | ||
config: external::LldpLinkConfig, | ||
) -> UpdateResult<()> { | ||
use db::schema::lldp_link_config::dsl; | ||
|
||
let id = self | ||
.lldp_config_id_get(opctx, rack_id, switch_location, port_name) | ||
.await?; | ||
if id != config.id { | ||
return Err(external::Error::invalid_request(&format!( | ||
"id ({}) doesn't match provided config ({})", | ||
id, config.id | ||
))); | ||
} | ||
|
||
diesel::update(dsl::lldp_link_config) | ||
.filter(dsl::time_deleted.is_null()) | ||
.filter(dsl::id.eq(id)) | ||
.set(( | ||
dsl::time_modified.eq(Utc::now()), | ||
dsl::enabled.eq( config.enabled), | ||
dsl::link_name.eq( config.link_name.clone()), | ||
dsl::link_description.eq( config.link_description.clone()), | ||
dsl::chassis_id.eq( config.chassis_id.clone()), | ||
dsl::system_name.eq( config.system_name.clone()), | ||
dsl::system_description.eq( config.system_description.clone()), | ||
dsl::management_ip.eq( config.management_ip.map(|a| IpNetwork::from(a))))) | ||
.execute_async(&*self.pool_connection_authorized(opctx).await?) | ||
.await | ||
.map_err(|err| { | ||
error!(opctx.log, "lldp link config update failed"; "error" => ?err); | ||
public_error_from_diesel(err, ErrorHandler::Server) | ||
})?; | ||
Ok(()) | ||
} | ||
} |
This file contains 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 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason LLDP configuration should be a part of the
switch-port-settings
object, aside from everything else also being there? I think it can be argued that we could migrate it out and make theCREATE
operation consistent with the other paths that have been defined for theGET
andUPDATE
methods.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could go back and look at the git history to be sure, but I think they've been there for as long as we've have switch port settings in the database. These are settings that belong to a specific port on the switch, so I'm not sure where else they would go? The settings themselves have their own table - the switch-port-settings just has a link to that table.
If you have something specific in mind, I'd be happy to tackle that as follow-on work. This is already a pretty chunky PR and doesn't include any database changes, so I'd rather not weigh it down with a db restructure as well.