Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
846 lines (746 loc) · 27.4 KB
/
Copy pathmain.rs
File metadata and controls
846 lines (746 loc) · 27.4 KB
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
// 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/
//
// Copyright 2026 Oxide Computer Company
//! Main application entry point for `dpd`, the Dendrite switch management API
//! server.
use std::collections::BTreeMap;
use std::collections::VecDeque;
use std::hash::Hash;
use std::net::SocketAddr;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;
use anyhow::Context;
use clap::{Parser, Subcommand};
use dpd_types::link::LinkCreate;
use dpd_types::link::LinkId;
use dpd_types::oxstats::OximeterMetadata;
use futures::stream::StreamExt;
use libc::c_int;
use signal_hook::consts::SIGHUP;
use signal_hook::consts::SIGINT;
use signal_hook::consts::SIGQUIT;
use signal_hook::consts::SIGTERM;
use signal_hook::consts::SIGUSR1;
use signal_hook_tokio::Signals;
use slog::debug;
use slog::error;
use slog::info;
use tokio::sync::Mutex as TokioMutex;
use tokio::time::Duration;
use tokio::time::sleep;
use crate::macaddrs::BaseMac;
use crate::port_map::SidecarRevision;
use crate::rpw::WorkflowServer;
use crate::switch_port::SwitchPorts;
use aal::{ActionParse, AsicError, MatchParse};
use common::counters::CounterId;
use common::network::MacAddr;
use common::ports::PortId;
use common::table::TableType;
use dpd_types::switch_identifiers::SwitchIdentifiers;
use table::Table;
use types::*;
cfg_if::cfg_if! {
if #[cfg(feature = "tofino_asic")] {
use crate::transceivers::TransceiverState;
}
}
mod api_server;
mod arp;
mod attached_subnet;
mod config;
mod counters;
mod dhcpv6;
mod fault;
mod freemap;
mod link;
mod loopback;
mod macaddrs;
#[cfg(feature = "multicast")]
mod mcast;
mod nat;
mod oxstats;
mod port_map;
mod port_settings;
mod ports;
mod route;
mod rpw;
#[cfg(feature = "tofino_asic")]
mod snapshot;
mod switch_identifiers;
mod switch_port;
mod table;
mod transceivers;
mod types;
mod version;
/// dataplane controller for oxide switch
#[derive(Debug, Parser)]
pub struct Cli {
#[command(subcommand)]
args: Args,
}
#[derive(Debug, Subcommand)]
#[clap(name = "dpd")]
pub(crate) enum Args {
/// Run the Dendrite API server.
Run(Box<Opt>),
}
/// dataplane controller for oxide switch
#[derive(Debug, Default, Parser)]
#[clap(name = "dpd")]
pub(crate) struct Opt {
/// send log data to the named file rather than stdout
#[clap(long)]
log_file: Option<String>,
/// log format
///
/// format logs for 'human' or 'json' consumption
#[clap(long, short = 'l')]
log_format: Option<common::logging::LogFormat>,
// TODO-correctness: This argument may need to change or go away. The
// control plane ultimately will set the addresses for each switch port
// independently, but it's not clear whether that makes sense in the SoftNPU
// and Intel simulator implementations.
/// set the base mac address for the switch
#[clap(long)]
mac_base: Option<MacAddr>,
/// file defining the ports to configure at startup
#[clap(long)]
port_config: Option<String>,
/// file describing alternate settings for some transceivers
#[clap(long)]
xcvr_defaults: Option<String>,
// TODO-completeness: This will ultimately go away in the product, or be
// ignored, as the value will ideally be determined from the FRUID data in
// the Sidecar itself.
/// Revision of the Sidecar which Dendrite will manage
#[cfg_attr(not(feature = "tofino_asic"), allow(dead_code))]
#[clap(long)]
sidecar_revision: Option<SidecarRevision>,
/// IP addresses and ports on which to expose the API server
#[clap(long)]
listen_addresses: Option<Vec<SocketAddr>>,
/// path to the tofino device
#[cfg(feature = "tofino_asic")]
#[clap(long)]
device_path: Option<String>,
/// path to the the chaos testing configuration
#[cfg(feature = "chaos")]
#[clap(long)]
chaos_config: Option<String>,
// NOTE: This should never be set to something other than the default
// `sidecar0` in the product.
/// IP interface over which to communicate with
/// the Hubris transceivers task for controlling
/// QSFP modules
#[cfg_attr(not(feature = "tofino_asic"), allow(dead_code))]
#[clap(long)]
transceiver_interface: Option<String>,
/// Mechanism for controlling SoftNPU emulated switch
#[cfg(feature = "softnpu")]
#[clap(long)]
softnpu_management: Option<asic::softnpu::mgmt::SoftnpuManagement>,
/// Path to UNIX domain socket to use for communicating with asic
#[cfg(feature = "softnpu")]
#[clap(long)]
uds_path: Option<String>,
/// Enable RPW services
#[clap(long)]
enable_rpw: bool,
/// IP address and port of nexus server
#[clap(long)]
nexus_address: Option<SocketAddr>,
/// IP address and port of MGS server.
#[structopt(long)]
mgs_address: Option<SocketAddr>,
}
/// The main context object for running all of `dpd`.
pub struct Switch {
// Time this object was created.
start_time: chrono::DateTime<chrono::Utc>,
pub config: Mutex<config::Config>,
pub log: slog::Logger,
pub asic_hdl: asic::Handle,
pub tables: BTreeMap<TableType, Mutex<Table>>,
pub counters: BTreeMap<CounterId, Mutex<counters::Counter>>,
pub links: Mutex<link::LinkMap>,
pub routes: TokioMutex<route::RouteData>,
pub arp: Mutex<arp::ArpData>,
pub nat: nat::Nat,
pub attached_subnet: Mutex<attached_subnet::AttachedSubnetData>,
pub loopback: Mutex<loopback::LoopbackData>,
pub identifiers: Mutex<Option<SwitchIdentifiers>>,
pub oximeter_producer: Mutex<Option<oximeter_producer::Server>>,
pub oximeter_meta: Mutex<Option<OximeterMetadata>>,
pub reconciler: link::LinkReconciler,
#[cfg(feature = "multicast")]
pub mcast: Mutex<mcast::MulticastGroupData>,
mac_mgmt: Mutex<macaddrs::MacManagement>,
// record of events for each asic_id-indexed port
port_history: Mutex<BTreeMap<u16, VecDeque<ports::EventRecord>>>,
// Information about the physical switch ports and the contained links.
switch_ports: SwitchPorts,
// State used to manage transceivers.
#[cfg(feature = "tofino_asic")]
transceivers: TransceiverState,
// Reliable Persistent Workflow server
pub workflow_server: WorkflowServer,
}
// The sidecar revision only _exists_ in the ASIC build of Dendrite.
// However, we're using it to track all the switch ports and contained
// links. This is an unfortunate side effect of the ways in which the
// stub / softnpu backends aren't quite emulating the real system.
#[cfg(feature = "tofino_asic")]
fn get_sidecar_revision(
config: &config::Config,
) -> anyhow::Result<SidecarRevision> {
config
.asic_config
.board_rev
.parse()
.map_err(|e| anyhow::anyhow!("Invalid Sidecar revision: {e}"))
}
#[cfg(feature = "tofino_stub")]
fn get_sidecar_revision(
_config: &config::Config,
) -> anyhow::Result<SidecarRevision> {
Ok(SidecarRevision::B)
}
#[cfg(feature = "softnpu")]
fn get_sidecar_revision(
config: &config::Config,
) -> anyhow::Result<SidecarRevision> {
Ok(SidecarRevision::Soft {
front: config.asic_config.front_ports,
rear: config.asic_config.rear_ports,
})
}
#[cfg(feature = "chaos")]
fn get_sidecar_revision(
_config: &config::Config,
) -> anyhow::Result<SidecarRevision> {
Ok(SidecarRevision::Chaos)
}
impl Switch {
fn new(
log: slog::Logger,
_p4_name: &str,
config: config::Config,
) -> anyhow::Result<Self> {
let start_time = chrono::Utc::now();
let asic_hdl = match asic::Handle::new(&log, &config.asic_config) {
Ok(h) => h,
Err(AsicError::AsicMissing) => {
panic!("Unable to find the network switch ASIC")
}
Err(e) => panic!("unable to initialize bf: {e:?}"),
};
let switch_ports = SwitchPorts::new(
get_sidecar_revision(&config)?,
&config.xcvr_defaults,
)?;
let counters = counters::init(&asic_hdl)
.context("failed to initialize counters")?;
#[cfg(feature = "tofino_asic")]
let transceivers = TransceiverState::new(
&log,
config.asic_config.xcvr_iface.as_deref(),
);
let route_data = route::init(&log);
let mac_mgmt = Mutex::new(macaddrs::MacManagement::new(&log));
#[cfg(feature = "tofino_asic")]
if !asic_hdl.is_model() {
#[cfg(feature = "interrupts")]
run_interrupt_monitor(log.clone());
}
let ws_log = log.new(slog::o!("unit" => "workflow_server"));
let workflow_server = rpw::WorkflowServer::new(ws_log);
let nat = nat::Nat::new(&log);
Ok(Switch {
start_time,
config: Mutex::new(config),
log,
asic_hdl,
tables: BTreeMap::new(),
links: Mutex::new(link::LinkMap::new()),
counters,
routes: TokioMutex::new(route_data),
arp: Mutex::new(arp::init()),
nat,
attached_subnet: Mutex::new(attached_subnet::init()),
loopback: Mutex::new(loopback::init()),
switch_ports,
identifiers: Mutex::new(None),
oximeter_producer: Mutex::new(None),
oximeter_meta: Mutex::new(None),
reconciler: link::LinkReconciler::default(),
#[cfg(feature = "multicast")]
mcast: Mutex::new(mcast::MulticastGroupData::new()),
mac_mgmt,
port_history: Mutex::new(BTreeMap::new()),
#[cfg(feature = "tofino_asic")]
transceivers,
workflow_server,
})
}
/// Get exclusive access to a table of the requested type, if it exists.
pub fn table_get(&self, id: TableType) -> DpdResult<MutexGuard<'_, Table>> {
match self.tables.get(&id) {
Some(table) => Ok(table.lock().unwrap()),
None => Err("no such table".into()),
}
}
// Add a new table to the internal list of tables.
fn table_add(&mut self, id: TableType) -> anyhow::Result<()> {
let t = table::Table::new(&self.asic_hdl, id)
.with_context(|| format!("creating {id:?} table"))?;
self.tables.insert(id, Mutex::new(t));
Ok(())
}
/// Returns the number of entries the table can hold
pub fn table_size(&self, table_type: TableType) -> DpdResult<u32> {
let t = self.table_get(table_type)?;
Ok(t.size())
}
/// Add a single entry to the requested table.
pub fn table_entry_add<M: MatchParse + Hash, A: ActionParse>(
&self,
table_type: TableType,
key: &M,
data: &A,
) -> DpdResult<()> {
let mut t = self.table_get(table_type)?;
t.entry_add(&self.asic_hdl, key, data).map_err(|e| {
debug!(
self.log,
"failed to add entry to {:?}: {:?}", table_type, e
);
e
})
}
/// Update a single table entry.
pub fn table_entry_update<M: MatchParse + Hash, A: ActionParse>(
&self,
table_type: TableType,
key: &M,
data: &A,
) -> DpdResult<()> {
let mut t = self.table_get(table_type)?;
t.entry_update(&self.asic_hdl, key, data).map_err(|e| {
debug!(
self.log,
"failed to update entry in {:?}: {:?}", table_type, e
);
e
})
}
/// Delete a single table entry.
pub fn table_entry_del<M: MatchParse + Hash>(
&self,
table_type: TableType,
key: &M,
) -> DpdResult<()> {
let mut t = self.table_get(table_type)?;
t.entry_del(&self.asic_hdl, key).map_err(|e| {
debug!(
self.log,
"failed to delete entry from {:?}: {:?}", table_type, e
);
e
})
}
/// Fetch all of the entries in a P4 table and return them
pub fn table_dump<M: MatchParse, A: ActionParse>(
&self,
t: TableType,
from_hardware: bool,
) -> DpdResult<dpd_types::table::Table> {
let t = self.table_get(t)?;
Ok(dpd_types::table::Table {
name: t.type_.to_string(),
size: t.usage.size as usize,
entries: t
.get_entries::<M, A>(&self.asic_hdl, from_hardware)
.map_err(|e| {
error!(
self.log, "failed to get table contents";
"table" => t.type_.to_string(),
"error" => %e
);
e
})
.map(|vec| {
vec.into_iter()
.map(|(key, action): (M, A)| {
dpd_types::table::TableEntry::new(key, action)
})
.collect()
})?,
})
}
/// Fetch all of the counter data in a P4 table and return it
pub fn counter_fetch<M: MatchParse>(
&self,
force_sync: bool,
t: TableType,
) -> DpdResult<Vec<dpd_types::table::TableCounterEntry>> {
let t = self.table_get(t)?;
t.get_counters::<M>(&self.asic_hdl, force_sync)
.map_err(|e| {
error!(
self.log, "failed to get counter data";
"table" => t.type_.to_string(),
"error" => %e
);
e
})
.map(|vec| {
vec.into_iter()
.map(|(key, data): (M, aal::CounterData)| {
dpd_types::table::TableCounterEntry::new(key, data)
})
.collect()
})
}
/// Completely clear the requested table.
pub fn table_clear(&self, t: TableType) -> DpdResult<()> {
let mut t = self.table_get(t)?;
t.clear(&self.asic_hdl)
}
pub fn allocate_mac_address(
&self,
port_id: PortId,
link_id: LinkId,
) -> DpdResult<MacAddr> {
let mut mgr = self.mac_mgmt.lock().unwrap();
mgr.allocate_mac_address(port_id, link_id)
.ok_or(DpdError::NoMacAddrsAvailable)
}
pub fn reclaim_mac_address(&self, mac: MacAddr) {
let mut mgr = self.mac_mgmt.lock().unwrap();
mgr.reclaim_mac_address(mac)
}
pub fn free_mac_address(&self, mac: MacAddr) {
let mut mgr = self.mac_mgmt.lock().unwrap();
mgr.free_mac_address(mac)
}
}
/// Handle a SMF refresh event on the `Switch` configuration.
fn handle_smf_refresh(
switch: &Switch,
smf_tx: &tokio::sync::watch::Sender<()>,
) {
if !common::is_smf_active() {
return;
}
let mut new_config = config::Config::default();
match config::update_from_smf(&mut new_config) {
Ok(_) => {
// We explicitly keep any learned MAC address -- new SMF
// refreshes do not override the prior value for this
// property.
let mut curr_config = switch.config.lock().unwrap();
let mac_base = curr_config.mac_base.or(new_config.mac_base);
*curr_config = config::Config { mac_base, ..new_config };
info!(switch.log, "refreshed config: {:#?}", curr_config);
}
Err(e) => {
error!(switch.log, "failed to refresh smf: {e:?}");
return;
}
}
// Let the config consumers know that something has changed
if let Err(e) = smf_tx.send(()) {
error!(switch.log, "failed to send smf update: {e:}");
}
}
async fn handle_signals(
switch: &Switch,
mut signals: Signals,
smf_tx: tokio::sync::watch::Sender<()>,
) {
let log = switch.log.new(slog::o!("unit" => "signal_handler"));
let handle = signals.handle();
info!(log, "refreshing SMF config prior to waiting on signals");
handle_smf_refresh(switch, &smf_tx);
while let Some(signal) = signals.next().await {
match signal {
SIGTERM | SIGQUIT | SIGINT | SIGHUP => {
info!(log, "received signal"; "sig" => signal);
handle.close();
return;
}
SIGUSR1 => {
info!(log, "handling SMF refresh");
handle_smf_refresh(switch, &smf_tx)
}
_ => unreachable!(),
}
}
}
async fn update_switch_identifiers(switch: Arc<Switch>) {
let identifiers =
switch_identifiers::fetch_switch_identifiers_loop(switch.clone()).await;
// Update the switch identifiers if we were able to fetch them.
//
// We do this nearer to `Switch` instantiation since we are updating
// the `identifiers` field directly.
if let Ok(idents) = identifiers {
let mut identifiers = switch.identifiers.lock().unwrap();
assert!(identifiers.is_none());
identifiers.replace(idents);
info!(switch.log, "updated switch identifiers");
}
}
async fn stub_main(switch: Switch) -> anyhow::Result<()> {
let maybe_config_file = switch.config.lock().unwrap().port_config.clone();
if let Some(file) = &maybe_config_file {
debug!(switch.log, "reading autoconfigured ports"; "file" => file);
let config = load_auto_configuration(file).await?;
ports::auto_config(&switch, config.iter());
}
let switch = Arc::new(switch);
let (smf_tx, _smf_rx) = tokio::sync::watch::channel(());
const SIGNALS: &[c_int] = &[SIGTERM, SIGQUIT, SIGINT, SIGHUP, SIGUSR1];
let signals = Signals::new(SIGNALS).unwrap();
handle_signals(&switch, signals, smf_tx).await;
info!(switch.log, "shutting down switch driver");
switch.asic_hdl.fini();
info!(switch.log, "done");
Ok(())
}
// Initialize the QSFP platform subsystem, for the BF SDE to manage the
// front IO ports.
//
// This requires both the CPU port to be configured at the ASIC, but also
// for `tfportd` to create the datalink and IPv6 link-local address we need
// to talk to the SP through that port. _That_ can only happen after the API
// server is running, since `tfportd` makes requests to the `dpd` server for
// the list of configured ports which need links / interfaces.
#[cfg(feature = "tofino_asic")]
async fn qsfp_init(switch: Arc<Switch>) {
if switch.asic_hdl.is_model() {
return;
}
debug!(switch.log, "beginning qsfp initialization");
let sw = Arc::clone(&switch);
tokio::spawn(sw.sp_transceiver_request_handler());
let sw = Arc::clone(&switch);
tokio::spawn(sw.sde_transceiver_request_handler());
let sw = Arc::clone(&switch);
tokio::spawn(sw.transceiver_monitor());
let sw = Arc::clone(&switch);
tokio::spawn(sw.led_launch_sequence());
debug!(switch.log, "completed qsfp initialization");
}
pub type AutoconfiguredLinks = BTreeMap<PortId, LinkCreate>;
async fn load_auto_configuration<P>(path: P) -> DpdResult<AutoconfiguredLinks>
where
P: AsRef<std::path::Path>,
{
let contents = tokio::fs::read_to_string(path).await?;
toml::from_str(&contents)
.map_err(|e| DpdError::Other(format!("failed to parse TOML: {e:?}")))
}
async fn sidecar_main(mut switch: Switch) -> anyhow::Result<()> {
#[cfg(feature = "tokio-console")]
console_subscriber::init();
table::init(&mut switch).context("failed to initialize tables")?;
route::init_freemaps(&switch)
.await
.context("failed to initialize route freemaps")?;
// Load the links to be auto-created.
//
// We need this for correctly configuring the link on the CPU port ahead of
// the other links.
let maybe_config_file = switch.config.lock().unwrap().port_config.clone();
let autoconfig_links = if let Some(config_file) = &maybe_config_file {
debug!(switch.log, "reading autoconfigured ports"; "file" => config_file);
Some(load_auto_configuration(config_file).await?)
} else {
None
};
let switch = Arc::new(switch);
// Start the link reconcilation task
switch.reconciler.run(switch.clone());
let (smf_tx, smf_rx) = tokio::sync::watch::channel(());
// Start the Workflow Server
// This is a background process that facilitates the Reliable Persistent
// Workflow logic needed for reconciling state upon cold boot and avoiding
// issues from out-of-order requests
let wf_switch = switch.clone();
let wf_rx = smf_rx.clone();
tokio::spawn(async move {
if wf_switch.config.lock().unwrap().enable_rpw {
// if starting workflow server fails, wait for 5s then retry
while let Err(e) = wf_switch
.workflow_server
.run(wf_switch.clone(), wf_rx.clone())
.await
{
error!(wf_switch.log, "failed to start workflow server"; "error" => ?e);
sleep(Duration::from_millis(5000)).await;
}
}
});
table::port_ip::ipv4_table_clear(&switch)?;
table::port_ip::ipv6_table_clear(&switch)?;
// Launch the API servers.
//
// It is critical that this happen _before_ the section below acquiring a
// base MAC address. That communication occurs over a VLAN interface created
// on top of the CPU port. That interface is only created by `tfportd`, and
// only when it fetches a matching link from the `dpd` API server. Thus we
// need to start the servers, and _then_ possibly create the CPU link used
// to fetch MAC addresses.
//
// Note that we're starting the API server before we can properly create
// links or really do much of anything. We need a way to fail such requests.
// That is done by failing until we have Some(_) base MAC address.
let api_server_manager = tokio::task::spawn(
api_server::api_server_manager(switch.clone(), smf_rx.clone()),
);
info!(switch.log, "spawning fetching of switch identifiers from MGS");
tokio::task::spawn(update_switch_identifiers(switch.clone()));
info!(switch.log, "spawning oximeter register");
tokio::task::spawn(oxstats::oximeter_register(
switch.clone(),
smf_rx.clone(),
));
// Initialize the task watching for changes in link state from the SDE.
link::init_update_handler(&switch)
.await
.context("failed to launch link update handler")?;
// We define our signal handler here, but do not invoke it until after the
// mac address initialation has completed. We will receive a SIGUSR1
// if/when our SMF config is refreshed by sled-agent. The default
// disposition for SIGUSR1 is to kill the process. By setting up the signal
// handler here, we ensure that an early refresh signal will be queued for
// us, rather than taking out the process.
const SIGNALS: &[c_int] = &[SIGTERM, SIGQUIT, SIGINT, SIGHUP, SIGUSR1];
let signals = Signals::new(SIGNALS).unwrap();
// Get the configured base mac address, if any.
let config_base_mac = switch.config.lock().unwrap().mac_base;
// If there has been no base mac address configured via SMF or the command
// line, then we need to fetch it from the SP (for real sidecars) or just
// make one up (everywhere else).
let (skip_cpu_link, base_mac) = match config_base_mac {
Some(config_mac) => {
let base_mac = BaseMac::Permanent(config_mac);
debug!(
switch.log,
"permanent base MAC address already set, it will be kept";
"mac" => %base_mac
);
let mut mgr = switch.mac_mgmt.lock().unwrap();
assert_eq!(mgr.set_base_mac(base_mac)?, None);
(false, config_mac)
}
None => {
let base_mac =
switch.set_base_mac_address(&autoconfig_links).await?;
(true, base_mac)
}
};
// Start the task managing DHCPv6 addresses on the technician ports.
let dhcpv6_log = switch.log.new(slog::o!("unit" => "dhcpv6-task"));
let dhcpv6_task =
tokio::task::spawn(dhcpv6::ensure_dhcpv6_agent(dhcpv6_log, base_mac));
if let Some(auto_conf) = &autoconfig_links {
// If we've created the link on the CPU port above, to fetch the MAC
// addresses from the Sidecar, then we skip that particular link in this
// autoconfiguration step.
if skip_cpu_link {
let links = auto_conf.iter().filter(|(port_id, _params)| {
!matches!(port_id, PortId::Internal(_))
});
ports::auto_config(&switch, links);
} else {
ports::auto_config(&switch, auto_conf.iter());
};
}
// Ensure links exist for all rear ports. This is sometimes hard coded in a
// package via the file pointed to by the SMF property config/port_config.
// However, for virtual testing across a bunch of different topologies, we
// don't want to create a package per radix config, so we just make sure
// that all rear ports have links here.
#[cfg(feature = "softnpu")]
{
let rear_links: BTreeMap<PortId, LinkCreate> = switch
.switch_ports
.ports
.iter()
.filter_map(|(port_id, _)| {
if matches!(port_id, PortId::Rear(_)) {
let create = LinkCreate {
speed: common::ports::PortSpeed::Speed100G,
fec: Some(common::ports::PortFec::RS),
autoneg: true,
kr: true,
lane: Some(dpd_types::link::LinkId(0)),
tx_eq: None,
};
Some((*port_id, create))
} else {
None
}
})
.collect();
ports::auto_config(&switch, rear_links.iter());
}
// qsfp_init() may block for an arbitrarily long time waiting for the
// tfports to be constructed. If our SMF properties are refreshed while
// we're blocked, the associated SIGUSR1 will crash the daemon. Spawning a
// new task for qsfp_init() lets us stand up our signal handler to catch
// those events.
#[cfg(feature = "tofino_asic")]
let qsfp_hdlr = tokio::task::spawn(qsfp_init(switch.clone()));
// Wait for a signal to exit.
handle_signals(&switch, signals, smf_tx).await;
switch.reconciler.quit();
// Wait for spawned tasks.
#[cfg(feature = "tofino_asic")]
qsfp_hdlr.await?;
api_server_manager
.await
.expect("while shutting down the api_server_manager");
dhcpv6_task.await?;
info!(switch.log, "shutting down switch driver");
switch.asic_hdl.fini();
info!(switch.log, "done");
Ok(())
}
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
match cli.args {
Args::Run(opt) => oxide_tokio_rt::run(run_dpd(*opt)),
}
}
async fn run_dpd(opt: Opt) -> anyhow::Result<()> {
let config = config::build_config(&opt)?;
let log =
common::logging::init("dpd", &config.log_file, config.log_format)?;
info!(
log, "starting dpd";
"config" => ?config,
"build_info" => ?api_server::build_info(),
);
let p4_name =
std::env::var("P4_NAME").unwrap_or_else(|_| String::from("sidecar"));
let switch = Switch::new(log, &p4_name, config)?;
if p4_name == "sidecar" {
sidecar_main(switch).await
} else {
info!(switch.log, "running as stub to support p4 program: {p4_name}");
stub_main(switch).await
}
}
#[cfg(all(feature = "tofino_asic", feature = "interrupts"))]
fn run_interrupt_monitor(log: slog::Logger) {
std::thread::spawn(move || {
asic::tofino_asic::interrupt_monitor::monitor_interrupts(log);
});
}