-
Notifications
You must be signed in to change notification settings - Fork 40
/
agent.rs
451 lines (401 loc) · 15.1 KB
/
agent.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// 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/.
//! Bootstrap-related APIs.
use super::config::{Config, BOOTSTRAP_AGENT_PORT};
use super::discovery;
use super::params::SledAgentRequest;
use super::rss_handle::RssHandle;
use super::trust_quorum::{
self, RackSecret, ShareDistribution, TrustQuorumError,
};
use super::views::{ShareResponse, SledAgentResponse};
use crate::config::Config as SledConfig;
use crate::illumos::dladm::{self, Dladm, PhysicalLink};
use crate::illumos::zone::Zones;
use crate::server::Server as SledServer;
use omicron_common::address::get_sled_address;
use omicron_common::api::external::{Error as ExternalError, MacAddr};
use omicron_common::backoff::{
internal_service_policy, retry_notify, BackoffError,
};
use slog::Logger;
use std::io;
use std::net::{Ipv6Addr, SocketAddrV6};
use std::path::{Path, PathBuf};
use thiserror::Error;
use tokio::sync::Mutex;
/// Describes errors which may occur while operating the bootstrap service.
#[derive(Error, Debug)]
pub enum BootstrapError {
#[error("IO error: {message}: {err}")]
Io {
message: String,
#[source]
err: std::io::Error,
},
#[error("Error starting sled agent: {0}")]
SledError(String),
#[error("Error deserializing toml from {path}: {err}")]
Toml { path: PathBuf, err: toml::de::Error },
#[error(transparent)]
TrustQuorum(#[from] TrustQuorumError),
#[error("Failed to initialize bootstrap address: {err}")]
BootstrapAddress { err: crate::illumos::zone::EnsureGzAddressError },
}
impl From<BootstrapError> for ExternalError {
fn from(err: BootstrapError) -> Self {
Self::internal_error(&err.to_string())
}
}
// Attempt to read a key share file. If the file does not exist, we return
// `Ok(None)`, indicating the sled is operating in a single node cluster. If
// the file exists, we parse it and return Ok(ShareDistribution). For any
// other error, we return the error.
//
// TODO: Remove after dynamic key generation. See #513.
fn read_key_share() -> Result<Option<ShareDistribution>, BootstrapError> {
let key_share_dir = Path::new("/opt/oxide/sled-agent/pkg");
match ShareDistribution::read(&key_share_dir) {
Ok(share) => Ok(Some(share)),
Err(TrustQuorumError::Io { message, err }) => {
if err.kind() == io::ErrorKind::NotFound {
Ok(None)
} else {
Err(BootstrapError::Io { message, err })
}
}
Err(e) => Err(e.into()),
}
}
/// The entity responsible for bootstrapping an Oxide rack.
pub(crate) struct Agent {
/// Debug log
log: Logger,
/// Store the parent log - without "component = BootstrapAgent" - so
/// other launched components can set their own value.
parent_log: Logger,
peer_monitor: discovery::PeerMonitor,
share: Option<ShareDistribution>,
rss: Mutex<Option<RssHandle>>,
sled_agent: Mutex<Option<SledServer>>,
sled_config: SledConfig,
}
fn get_sled_agent_request_path() -> PathBuf {
Path::new(omicron_common::OMICRON_CONFIG_PATH)
.join("sled-agent-request.toml")
}
fn mac_to_socket_addr(mac: MacAddr) -> SocketAddrV6 {
let mac_bytes = mac.into_array();
assert_eq!(6, mac_bytes.len());
let address = Ipv6Addr::new(
0xfdb0,
((mac_bytes[0] as u16) << 8) | mac_bytes[1] as u16,
((mac_bytes[2] as u16) << 8) | mac_bytes[3] as u16,
((mac_bytes[4] as u16) << 8) | mac_bytes[5] as u16,
0,
0,
0,
1,
);
SocketAddrV6::new(address, BOOTSTRAP_AGENT_PORT, 0, 0)
}
// TODO(https://github.com/oxidecomputer/omicron/issues/945): This address
// could be randomly generated when it no longer needs to be durable.
pub fn bootstrap_address(
link: PhysicalLink,
) -> Result<SocketAddrV6, dladm::GetMacError> {
let mac = Dladm::get_mac(link)?;
Ok(mac_to_socket_addr(mac))
}
impl Agent {
pub async fn new(
log: Logger,
sled_config: SledConfig,
address: Ipv6Addr,
) -> Result<Self, BootstrapError> {
let ba_log = log.new(o!(
"component" => "BootstrapAgent",
"server" => sled_config.id.to_string(),
));
// We expect this directory to exist - ensure that it does, before any
// subsequent operations which may write configs here.
info!(
log, "Ensuring config directory exists";
"path" => omicron_common::OMICRON_CONFIG_PATH,
);
tokio::fs::create_dir_all(omicron_common::OMICRON_CONFIG_PATH)
.await
.map_err(|err| BootstrapError::Io {
message: format!(
"Creating config directory {}",
omicron_common::OMICRON_CONFIG_PATH
),
err,
})?;
let etherstub = Dladm::create_etherstub().map_err(|e| {
BootstrapError::SledError(format!(
"Can't access etherstub device: {}",
e
))
})?;
let etherstub_vnic =
Dladm::create_etherstub_vnic(ðerstub).map_err(|e| {
BootstrapError::SledError(format!(
"Can't access etherstub VNIC device: {}",
e
))
})?;
Zones::ensure_has_global_zone_v6_address(
etherstub_vnic,
address,
"bootstrap6",
)
.map_err(|err| BootstrapError::BootstrapAddress { err })?;
let peer_monitor = discovery::PeerMonitor::new(&ba_log, address)
.map_err(|err| BootstrapError::Io {
message: format!("Monitoring for peers from {address}"),
err,
})?;
let share = read_key_share()?;
let agent = Agent {
log: ba_log,
parent_log: log,
peer_monitor,
share,
rss: Mutex::new(None),
sled_agent: Mutex::new(None),
sled_config,
};
let request_path = get_sled_agent_request_path();
if request_path.exists() {
info!(agent.log, "Sled already configured, loading sled agent");
let sled_request: SledAgentRequest = toml::from_str(
&tokio::fs::read_to_string(&request_path).await.map_err(
|err| BootstrapError::Io {
message: format!(
"Reading subnet path from {request_path:?}"
),
err,
},
)?,
)
.map_err(|err| BootstrapError::Toml { path: request_path, err })?;
agent.request_agent(sled_request).await?;
}
Ok(agent)
}
/// Implements the "request share" API.
pub async fn request_share(
&self,
identity: Vec<u8>,
) -> Result<ShareResponse, BootstrapError> {
// TODO-correctness: Validate identity, return whatever
// information is necessary to establish trust quorum.
//
// This current implementation is a placeholder.
info!(&self.log, "request_share, received identity: {:x?}", identity);
Ok(ShareResponse { shared_secret: vec![] })
}
/// Initializes the Sled Agent on behalf of the RSS, if one has not already
/// been initialized.
pub async fn request_agent(
&self,
request: SledAgentRequest,
) -> Result<SledAgentResponse, BootstrapError> {
info!(&self.log, "Loading Sled Agent: {:?}", request);
let sled_address = get_sled_address(request.subnet);
let mut maybe_agent = self.sled_agent.lock().await;
if let Some(server) = &*maybe_agent {
// Server already exists, return it.
info!(&self.log, "Sled Agent already loaded");
if &server.address().ip() != sled_address.ip() {
let err_str = format!(
"Sled Agent already running on address {}, but {} was requested",
server.address().ip(),
sled_address.ip(),
);
return Err(BootstrapError::SledError(err_str));
}
return Ok(SledAgentResponse { id: server.id() });
}
// Server does not exist, initialize it.
let server = SledServer::start(
&self.sled_config,
self.parent_log.clone(),
sled_address,
)
.await
.map_err(|e| {
BootstrapError::SledError(format!(
"Could not start sled agent server: {e}"
))
})?;
maybe_agent.replace(server);
info!(&self.log, "Sled Agent loaded; recording configuration");
// Record this request so the sled agent can be automatically
// initialized on the next boot.
let path = get_sled_agent_request_path();
tokio::fs::write(
&path,
&toml::to_string(
&toml::Value::try_from(&request)
.expect("Cannot serialize request"),
)
.expect("Cannot convert toml to string"),
)
.await
.map_err(|err| BootstrapError::Io {
message: format!("Recording Sled Agent request to {path:?}"),
err,
})?;
Ok(SledAgentResponse { id: self.sled_config.id })
}
/// Communicates with peers, sharing secrets, until the rack has been
/// sufficiently unlocked.
async fn establish_sled_quorum(
&self,
) -> Result<RackSecret, BootstrapError> {
let rack_secret = retry_notify(
internal_service_policy(),
|| async {
let other_agents = self.peer_monitor.peer_addrs().await;
info!(
&self.log,
"Bootstrap: Communicating with peers: {:?}", other_agents
);
let share = self.share.as_ref().unwrap();
// "-1" to account for ourselves.
if other_agents.len() < share.threshold - 1 {
warn!(
&self.log,
"Not enough peers to start establishing quorum"
);
return Err(BackoffError::transient(
TrustQuorumError::NotEnoughPeers,
));
}
info!(
&self.log,
"Bootstrap: Enough peers to start share transfer"
);
// Retrieve verified rack_secret shares from a quorum of agents
let other_agents: Vec<trust_quorum::Client> = other_agents
.into_iter()
.map(|addr| {
let addr = SocketAddrV6::new(
addr,
trust_quorum::PORT,
0,
0,
);
trust_quorum::Client::new(
&self.log,
share.verifier.clone(),
addr,
)
})
.collect();
// TODO: Parallelize this and keep track of whose shares we've already retrieved and
// don't resend. See https://github.com/oxidecomputer/omicron/issues/514
let mut shares = vec![share.share.clone()];
for agent in &other_agents {
let share = agent.get_share().await
.map_err(|e| {
info!(&self.log, "Bootstrap: failed to retreive share from peer: {:?}", e);
BackoffError::transient(e)
})?;
info!(
&self.log,
"Bootstrap: retreived share from peer: {}",
agent.addr()
);
shares.push(share);
}
let rack_secret = RackSecret::combine_shares(
share.threshold,
share.total_shares,
&shares,
)
.map_err(|e| {
warn!(
&self.log,
"Bootstrap: failed to construct rack secret: {:?}", e
);
// TODO: We probably need to actually write an error
// handling routine that gives up in some cases based on
// the error returned from `RackSecret::combine_shares`.
// See https://github.com/oxidecomputer/omicron/issues/516
BackoffError::transient(
TrustQuorumError::RackSecretConstructionFailed(e),
)
})?;
info!(self.log, "RackSecret computed from shares.");
Ok(rack_secret)
},
|error, duration| {
warn!(
self.log,
"Failed to unlock sleds (will retry after {:?}: {:#}",
duration,
error,
)
},
)
.await?;
Ok(rack_secret)
}
async fn run_trust_quorum_server(&self) -> Result<(), BootstrapError> {
let my_share = self.share.as_ref().unwrap().share.clone();
let mut server = trust_quorum::Server::new(&self.log, my_share)
.map_err(|err| BootstrapError::Io {
message: "Cannot run trust quorum server".to_string(),
err,
})?;
tokio::spawn(async move { server.run().await });
Ok(())
}
// Initializes the Rack Setup Service.
async fn start_rss(&self, config: &Config) -> Result<(), BootstrapError> {
if let Some(rss_config) = &config.rss_config {
let rss = RssHandle::start_rss(
&self.parent_log,
rss_config.clone(),
self.peer_monitor.observer().await,
);
self.rss.lock().await.replace(rss);
}
Ok(())
}
/// Performs device initialization:
///
/// - Communicates with other sled agents to establish a trust quorum if a
/// ShareDistribution file exists on the host. Otherwise, the sled operates
/// as a single node cluster.
/// - Verifies, unpacks, and launches other services.
pub async fn initialize(
&self,
config: &Config,
) -> Result<(), BootstrapError> {
info!(&self.log, "bootstrap service initializing");
if self.share.is_some() {
self.run_trust_quorum_server().await?;
self.establish_sled_quorum().await?;
}
self.start_rss(config).await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use macaddr::MacAddr6;
#[test]
fn test_mac_to_socket_addr() {
let mac = MacAddr("a8:40:25:10:00:01".parse::<MacAddr6>().unwrap());
assert_eq!(
mac_to_socket_addr(mac).ip(),
&"fdb0:a840:2510:1::1".parse::<Ipv6Addr>().unwrap(),
);
}
}