Skip to content

Commit c0accdb

Browse files
Fredi-raspallmvachhar
authored andcommitted
feat(routing): cleanup, improve logs & tests
Signed-off-by: Fredi Raspall <[email protected]>
1 parent 7d5e956 commit c0accdb

File tree

4 files changed

+86
-34
lines changed

4 files changed

+86
-34
lines changed

routing/src/fib/fibtable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl FibTable {
4343
}
4444
/// Delete a `Fib`, by unregistering a `FibReaderFactory` for it
4545
fn del_fib(&mut self, id: &FibKey) {
46-
info!("Unregistering Fib id {id} from the FibTable");
46+
info!("Unregistering Fib with id {id} from the FibTable");
4747
self.entries.remove(id);
4848
}
4949
/// Register an existing `Fib` with a given [`Vni`].
@@ -52,15 +52,15 @@ impl FibTable {
5252
if let Some(entry) = self.get_entry(id) {
5353
self.entries
5454
.insert(FibKey::from_vni(vni), Arc::clone(entry));
55-
info!("Registering Fib with id {id} with new vni {vni} in FibTable");
55+
info!("Registering Fib with id {id} in the FibTable with vni {vni}");
5656
} else {
5757
error!("Failed to register Fib {id} with vni {vni}: no fib with id {id} found");
5858
}
5959
}
6060
/// Remove any entry keyed by a [`Vni`]
6161
fn unregister_vni(&mut self, vni: Vni) {
6262
let key = FibKey::from_vni(vni);
63-
info!("Unregistered Fib with vni {vni} from the FibTable");
63+
info!("Unregistered key = {key} from the FibTable");
6464
self.entries.remove(&key);
6565
}
6666

routing/src/interfaces/iftablerw.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use left_right::ReadHandleFactory;
1414
use left_right::{Absorb, ReadGuard, ReadHandle, WriteHandle};
1515
use net::interface::InterfaceIndex;
1616

17+
use tracing::debug;
18+
1719
enum IfTableChange {
1820
Add(RouterInterfaceConfig),
1921
Mod(RouterInterfaceConfig),
@@ -169,6 +171,7 @@ impl IfTableWriter {
169171
self.0.publish();
170172
}
171173
pub fn detach_interfaces_from_vrf(&mut self, vrfid: VrfId) {
174+
debug!("Scheduling detach of interfaces from vrf {vrfid}");
172175
self.0
173176
.append(IfTableChange::DetachFromVrf(FibKey::Id(vrfid)));
174177
self.0.publish();

routing/src/rib/vrf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl Vrf {
267267
/////////////////////////////////////////////////////////////////////////
268268
pub fn set_vni(&mut self, vni: Vni) {
269269
self.vni = Some(vni);
270-
debug!("Associated vni {vni} to Vrf '{}'", self.name);
270+
debug!("Set vni {vni} to Vrf {} ({})", self.vrfid, self.name);
271271
}
272272

273273
/////////////////////////////////////////////////////////////////////////

routing/src/rib/vrftable.rs

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,20 @@ impl VrfTable {
9595
return Ok(()); /* vrf already has that vni */
9696
}
9797
// No vrf has the requested vni, including the vrf with id vrfId.
98-
// However the vrf with id VrfId may have another vni associated,
99-
100-
/* remove vni from vrf if it has one */
98+
// However the vrf with id VrfId may have another vni associated.
99+
// So, unset any vni that the vrf may have associated first.
101100
self.unset_vni(vrfid)?;
102101

103-
/* set the vni to the vrf */
102+
/* lookup vrf */
104103
let vrf = self.get_vrf_mut(vrfid)?;
104+
105+
/* set the vni to the vrf */
105106
vrf.set_vni(vni);
106107

107-
/* register vni */
108+
/* register vni as key for vrf vrfid in the vrf table */
108109
self.by_vni.insert(vni, vrfid);
109110

110-
/* make fib accessible from vni */
111+
/* make fib accessible from vni in the fib table */
111112
self.fibtablew.register_fib_by_vni(vrfid, vni);
112113
Ok(())
113114
}
@@ -117,13 +118,18 @@ impl VrfTable {
117118
/// removes it from the `by_vni` map. It also unindexes the vrf's FIB by the vni.
118119
///////////////////////////////////////////////////////////////////////////////////
119120
pub fn unset_vni(&mut self, vrfid: VrfId) -> Result<(), RouterError> {
121+
debug!("Unsetting any vni configuration for vrf {vrfid}..");
120122
let vrf = self.get_vrf_mut(vrfid)?;
123+
124+
// check if vrf has vni configured
121125
if let Some(vni) = vrf.vni {
122-
debug!("Removing vni {vni} from vrf {vrfid}...");
126+
debug!("Vrf {vrfid} has vni {vni} associated. Removing...");
123127
vrf.vni.take();
124128
self.by_vni.remove(&vni);
125129
self.fibtablew.unregister_vni(vni);
126-
debug!("Vrf with Id {vrfid} no longer has a VNI associated");
130+
debug!("Vrf with Id {vrfid} no longer has a vni {vni} associated");
131+
} else {
132+
debug!("Vrf {vrfid} has no vni configured");
127133
}
128134
Ok(())
129135
}
@@ -173,14 +179,15 @@ impl VrfTable {
173179
vrfid: VrfId,
174180
iftablew: &mut IfTableWriter,
175181
) -> Result<(), RouterError> {
176-
debug!("Removing VRF with vrfid {vrfid}...");
182+
// remove the vrf from the vrf table
183+
debug!("Removing VRF {vrfid}...");
177184
let Some(vrf) = self.by_id.remove(&vrfid) else {
178185
error!("No vrf with id {vrfid} exists");
179186
return Err(RouterError::NoSuchVrf);
180187
};
181188
// delete the corresponding fib
182189
if vrf.fibw.is_some() {
183-
debug!("Requesting deletion of vrf {vrfid} FIB");
190+
debug!("Deleting Fib for vrf {vrfid} from the FibTable");
184191
self.fibtablew.del_fib(vrfid, vrf.vni);
185192
iftablew.detach_interfaces_from_vrf(vrfid);
186193
}
@@ -190,6 +197,7 @@ impl VrfTable {
190197
debug!("Unregistering vni {vni}");
191198
self.by_vni.remove(&vni);
192199
}
200+
debug!("Vrf {vrfid} has been removed");
193201
Ok(())
194202
}
195203

@@ -216,13 +224,15 @@ impl VrfTable {
216224
/// Remove all of the VRFs with status `Deleted`
217225
//////////////////////////////////////////////////////////////////
218226
pub fn remove_deleted_vrfs(&mut self, iftablew: &mut IfTableWriter) {
227+
debug!("Removing deletable vrfs...");
219228
self.remove_vrfs(Vrf::can_be_deleted, iftablew);
220229
}
221230

222231
//////////////////////////////////////////////////////////////////
223232
/// Remove all of the VRFs with status `Deleting`
224233
//////////////////////////////////////////////////////////////////
225234
pub fn remove_deleting_vrfs(&mut self, iftablew: &mut IfTableWriter) {
235+
debug!("Removing vrfs with deleting status...");
226236
self.remove_vrfs(Vrf::is_deleting, iftablew);
227237
}
228238

@@ -657,11 +667,10 @@ mod tests {
657667
.expect("Should find vrfid by vni");
658668
assert_eq!(id, vrfid);
659669
debug!("\n{vrftable}");
670+
660671
if let Some(fibtable) = fibtr.enter() {
661-
let fib = fibtable.get_fib(&FibKey::from_vrfid(vrfid));
662-
assert!(fib.is_some());
663-
let fib = fibtable.get_fib(&FibKey::from_vni(vni));
664-
assert!(fib.is_some());
672+
assert!(fibtable.get_fib(&FibKey::from_vrfid(vrfid)).is_some());
673+
assert!(fibtable.get_fib(&FibKey::from_vni(vni)).is_some());
665674
}
666675

667676
debug!("━━━━Test: Unset vni {vni} from the vrf");
@@ -674,36 +683,72 @@ mod tests {
674683
assert!((id.is_err_and(|e| e == RouterError::NoSuchVrf)));
675684
debug!("\n{vrftable}");
676685
if let Some(fibtable) = fibtr.enter() {
677-
let fib = fibtable.get_fib(&FibKey::from_vrfid(vrfid));
678-
assert!(fib.is_some());
679-
let fib = fibtable.get_fib(&FibKey::from_vni(vni));
680-
assert!(fib.is_none());
686+
assert!(fibtable.get_fib(&FibKey::from_vrfid(vrfid)).is_some());
687+
assert!(fibtable.get_fib(&FibKey::from_vni(vni)).is_none());
681688
}
682689
}
683690

684691
#[traced_test]
685692
#[test]
686693
fn vrf_table_deletions() {
694+
debug!("━━━━Test: Create testing interface table");
695+
let (mut iftw, iftr) = build_test_iftable_left_right();
696+
687697
debug!("━━━━Test: Create vrf table");
688698
let (fibtw, fibtr) = FibTableWriter::new();
689-
let (mut iftw, iftr) = build_test_iftable_left_right();
690699
let mut vrftable = VrfTable::new(fibtw);
691700

701+
debug!("━━━━Test: Check fib access for vrf default");
702+
if let Some(fibtable) = fibtr.enter() {
703+
let fibkey = FibKey::from_vrfid(0);
704+
let fibr = fibtable.get_fib(&fibkey).unwrap();
705+
let fib = fibr.enter().unwrap();
706+
assert_eq!(fib.as_ref().get_id(), fibkey);
707+
}
708+
692709
let vrfid = 999;
693710
let vni = mk_vni(3000);
694711

695-
debug!("━━━━Test: Add a VRF without Vni");
712+
debug!("━━━━Test: Add a VRF with id {vrfid} but no Vni");
696713
let vrf_cfg = RouterVrfConfig::new(vrfid, "VPC-1");
697714
vrftable.add_vrf(&vrf_cfg).expect("Should be created");
715+
assert_eq!(vrftable.len(), 2); // default is always there
716+
debug!("\n{vrftable}");
717+
718+
debug!("━━━━Test: Check fib access for vrf {vrfid}");
719+
if let Some(fibtable) = fibtr.enter() {
720+
// check that fib is accessible from vrfid
721+
let fibkey = FibKey::from_vrfid(vrfid);
722+
let fibr = fibtable.get_fib(&fibkey).unwrap();
723+
let fib = fibr.enter().unwrap();
724+
assert_eq!(fib.as_ref().get_id(), fibkey);
725+
assert_eq!(fibtable.as_ref().len(), 2);
726+
}
698727

699-
debug!("━━━━Test: Associate VNI {vni}");
728+
debug!("━━━━Test: Associate vni {vni} to vrf {vrfid}");
700729
vrftable.set_vni(vrfid, vni).expect("Should succeed");
701-
assert_eq!(vrftable.len(), 2); // default is always there
702730
debug!("\n{vrftable}");
703731

732+
debug!("━━━━Test: Check fib access for vrf {vrfid} and vni {vni}");
733+
if let Some(fibtable) = fibtr.enter() {
734+
// check that fib continues to be accessible via vrfid
735+
let fibkey = FibKey::from_vrfid(vrfid);
736+
let fibr = fibtable.get_fib(&fibkey).unwrap();
737+
let fib = fibr.enter().unwrap();
738+
assert_eq!(fib.as_ref().get_id(), fibkey);
739+
740+
// check that fib is accessible from vni
741+
let fibkey = FibKey::from_vni(vni);
742+
let fibr = fibtable.get_fib(&fibkey).unwrap();
743+
let fib = fibr.enter().unwrap();
744+
assert_eq!(fib.as_ref().get_id(), FibKey::from_vrfid(vrfid));
745+
}
746+
704747
debug!("━━━━Test: deleting removed VRFs: nothing should be removed");
705748
vrftable.remove_deleted_vrfs(&mut iftw);
706749
assert_eq!(vrftable.len(), 2); // default is always there
750+
assert_eq!(fibtr.enter().unwrap().len(), 3);
751+
debug!("\n{vrftable}");
707752

708753
debug!("━━━━Test: Get interface from iftable");
709754
let idx = InterfaceIndex::try_new(2).unwrap();
@@ -713,7 +758,7 @@ mod tests {
713758
debug!("\n{}", *iftable);
714759
}
715760

716-
debug!("━━━━Test: Attach interface to vrf");
761+
debug!("━━━━Test: Attach interface to vrf {vrfid}");
717762
iftw.attach_interface_to_vrf(idx, vrfid, &vrftable)
718763
.expect("Should succeed");
719764
if let Some(iftable) = iftr.enter() {
@@ -727,18 +772,22 @@ mod tests {
727772
vrf.set_status(VrfStatus::Deleted);
728773
debug!("\n{vrftable}");
729774

730-
debug!("━━━━Test: remove vrfs marked as deleted again - VPC-1 vrf should be gone");
775+
debug!("━━━━Test: remove vrfs marked as deleted: VPC-1 vrf should be gone");
731776
vrftable.remove_deleted_vrfs(&mut iftw);
732777
assert_eq!(vrftable.len(), 1, "should be gone");
778+
assert_eq!(
779+
fibtr.enter().unwrap().len(),
780+
1,
781+
"only default fib should remain"
782+
);
733783

734-
// check fib table
784+
debug!("━━━━Test: Check that no fib is accessible vrfid:{vrfid} nor vni:{vni}");
735785
if let Some(fibtable) = fibtr.enter() {
736-
let fib = fibtable.get_fib(&FibKey::from_vrfid(vrfid));
737-
assert!(fib.is_none());
738-
let fib = fibtable.get_fib(&FibKey::from_vni(vni));
739-
assert!(fib.is_none());
740-
assert_eq!(fibtable.len(), 1);
786+
assert!(fibtable.get_fib(&FibKey::from_vrfid(vrfid)).is_none());
787+
assert!(fibtable.get_fib(&FibKey::from_vni(vni)).is_none());
788+
assert!(fibtable.get_fib(&FibKey::from_vrfid(0)).is_some());
741789
}
790+
debug!("━━━━Test: Interface {idx} should no longer be attached");
742791
if let Some(iftable) = iftr.enter() {
743792
let iface = iftable.get_interface(idx).expect("Should be there");
744793
assert!(iface.attachment.is_none(), "Should have been detached");

0 commit comments

Comments
 (0)