Skip to content

Commit 375251b

Browse files
committed
fix: reduce the frequency of log printing
1 parent 6ee39d0 commit 375251b

File tree

10 files changed

+284
-88
lines changed

10 files changed

+284
-88
lines changed

agent/benches/labeler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn bench_labeler(c: &mut Criterion) {
7070

7171
cidr_list.push(Arc::new(cidr));
7272
}
73-
labeler.update_cidr_table(&cidr_list);
73+
labeler.update_cidr_table(&cidr_list, false, &mut false);
7474
labeler.update_interface_table(&iface_list);
7575

7676
let key: LookupKey = LookupKey {
@@ -118,7 +118,7 @@ fn bench_labeler(c: &mut Criterion) {
118118

119119
cidr_list.push(Arc::new(cidr));
120120
}
121-
labeler.update_cidr_table(&cidr_list);
121+
labeler.update_cidr_table(&cidr_list, false, &mut false);
122122
labeler.update_interface_table(&iface_list);
123123

124124
let key: LookupKey = LookupKey {
@@ -163,7 +163,7 @@ fn bench_policy(c: &mut Criterion) {
163163
Arc::new(IpGroupData::new(10, 2, "192.168.2.1/32")),
164164
Arc::new(IpGroupData::new(20, 20, "192.168.2.5/31")),
165165
]);
166-
let _ = first.update_acl(&vec![Arc::new(acl)], true);
166+
let _ = first.update_acl(&vec![Arc::new(acl)], true, false, &mut false);
167167

168168
first
169169
}

agent/src/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ pub trait FlowAclListener: Send + Sync {
100100
peers: &Vec<Arc<PeerConnection>>,
101101
cidrs: &Vec<Arc<Cidr>>,
102102
acls: &Vec<Arc<Acl>>,
103+
enabled_invalid_log: bool,
104+
has_invalid_log: &mut bool,
103105
) -> Result<(), String>;
104106
fn containers_change(&mut self, _: &Vec<Arc<Container>>) {}
105107
fn id(&self) -> usize;

agent/src/debug/rpc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl RpcDebugger {
156156
}
157157

158158
let mut sg = self.status.write();
159-
sg.get_platform_data(&resp);
159+
sg.get_platform_data(&resp, false);
160160
let mut res = sg
161161
.cidrs
162162
.iter()
@@ -181,7 +181,7 @@ impl RpcDebugger {
181181
}
182182

183183
let mut sg = self.status.write();
184-
sg.get_platform_data(&resp);
184+
sg.get_platform_data(&resp, false);
185185
let mut res = sg
186186
.interfaces
187187
.iter()
@@ -211,7 +211,7 @@ impl RpcDebugger {
211211
}
212212

213213
let mut sg = self.status.write();
214-
sg.get_ip_groups(&resp);
214+
sg.get_ip_groups(&resp, false);
215215
let mut res = sg
216216
.ip_groups
217217
.iter()
@@ -236,7 +236,7 @@ impl RpcDebugger {
236236
}
237237

238238
let mut sg = self.status.write();
239-
sg.get_flow_acls(&resp);
239+
sg.get_flow_acls(&resp, false);
240240
let mut res = sg
241241
.acls
242242
.iter()

agent/src/dispatcher/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ impl FlowAclListener for DispatcherListener {
255255
_: &Vec<Arc<crate::common::policy::PeerConnection>>,
256256
_: &Vec<Arc<crate::_Cidr>>,
257257
_: &Vec<Arc<crate::_Acl>>,
258+
_: bool,
259+
_: &mut bool,
258260
) -> Result<(), String> {
259261
match self {
260262
DispatcherListener::Local(a) => a.flow_acl_change(),

agent/src/ebpf_dispatcher.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ impl FlowAclListener for SyncEbpfDispatcher {
496496
_: &Vec<Arc<crate::common::policy::PeerConnection>>,
497497
_: &Vec<Arc<crate::_Cidr>>,
498498
_: &Vec<Arc<crate::_Acl>>,
499+
_: bool,
500+
_: &mut bool,
499501
) -> Result<(), String> {
500502
self.pause.store(false, Ordering::Relaxed);
501503
Ok(())

agent/src/policy/first_path.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ impl FirstPath {
340340
}
341341

342342
if self.group_ip_map.is_none() {
343-
warn!("IpGroup is nil, invalid acl: {}", acl);
344343
return false;
345344
}
346345

@@ -352,7 +351,6 @@ impl FirstPath {
352351
.get(&(*group as u16))
353352
.is_none()
354353
{
355-
warn!("Invalid acl by src group({}): {}", group, acl);
356354
return true;
357355
}
358356
}
@@ -365,7 +363,6 @@ impl FirstPath {
365363
.get(&(*group as u16))
366364
.is_none()
367365
{
368-
warn!("Invalid acl by dst group({}): {}", group, acl);
369366
return true;
370367
}
371368
}
@@ -593,19 +590,36 @@ impl FirstPath {
593590
Ok(())
594591
}
595592

596-
pub fn update_acl(&mut self, acls: &Vec<Arc<Acl>>, check: bool) -> PResult<()> {
593+
pub fn update_acl(
594+
&mut self,
595+
acls: &Vec<Arc<Acl>>,
596+
check: bool,
597+
enabled_invalid_log: bool,
598+
has_invalid_log: &mut bool,
599+
) -> PResult<()> {
597600
if !NOT_SUPPORT {
598601
let mut valid_acls = Vec::new();
602+
let mut invalid_acls = vec![];
599603

600604
for acl in acls {
601605
if self.is_invalid_acl(acl, check) {
606+
invalid_acls.push(acl.id);
602607
continue;
603608
}
604609
let mut valid_acl = (**acl).clone();
605610

606611
valid_acl.reset();
607612
valid_acls.push(valid_acl);
608613
}
614+
615+
if enabled_invalid_log && !invalid_acls.is_empty() {
616+
warn!(
617+
"Invalid acls: {:?}, maybe the IP resource group doesn't have an IP address.",
618+
invalid_acls
619+
);
620+
*has_invalid_log = true;
621+
}
622+
609623
self.generate_first_table(&mut valid_acls)?;
610624
}
611625

agent/src/policy/labeler.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,16 @@ impl Labeler {
259259
return (0, 0);
260260
}
261261

262-
pub fn update_cidr_table(&mut self, cidrs: &Vec<Arc<Cidr>>) {
262+
pub fn update_cidr_table(
263+
&mut self,
264+
cidrs: &Vec<Arc<Cidr>>,
265+
enabled_invalid_log: bool,
266+
has_invalid_log: &mut bool,
267+
) {
263268
let mut masklen_table: AHashMap<i32, (u8, u8)> = AHashMap::new();
264269
let mut epc_table: AHashMap<EpcNetIpKey, Arc<Cidr>> = AHashMap::new();
265270
let mut tunnel_table: AHashMap<u32, Vec<Arc<Cidr>>> = AHashMap::new();
271+
let mut invalid_cidr = Vec::new();
266272

267273
for item in cidrs {
268274
let mut epc_id = item.epc_id;
@@ -275,10 +281,7 @@ impl Labeler {
275281
if (item.cidr_type == CidrType::Wan && item.epc_id != old.epc_id)
276282
|| item.is_vip != old.is_vip
277283
{
278-
warn!(
279-
"Found the same cidr, please check {:?} and {:?}.",
280-
item, old
281-
);
284+
invalid_cidr.push(item.ip)
282285
}
283286
}
284287
masklen_table
@@ -301,6 +304,11 @@ impl Labeler {
301304
}
302305
}
303306

307+
if enabled_invalid_log && !invalid_cidr.is_empty() {
308+
warn!("Invalid same cidr: {:?}", invalid_cidr);
309+
*has_invalid_log = true;
310+
}
311+
304312
// 排序使用降序是为了CIDR的最长前缀匹配
305313
for (_k, v) in &mut tunnel_table.iter_mut() {
306314
v.sort_by(|a, b| {
@@ -1095,7 +1103,7 @@ mod tests {
10951103
let cidrs = vec![Arc::new(cidr1), Arc::new(cidr2), Arc::new(cidr3)];
10961104
let mut endpoint: EndpointInfo = Default::default();
10971105

1098-
labeler.update_cidr_table(&cidrs);
1106+
labeler.update_cidr_table(&cidrs, false, &mut false);
10991107

11001108
labeler.set_epc_by_cidr("192.168.10.100".parse().unwrap(), 10, &mut endpoint);
11011109
assert_eq!(endpoint.is_vip, true);
@@ -1117,7 +1125,7 @@ mod tests {
11171125
..Default::default()
11181126
};
11191127
let cidrs = vec![Arc::new(cidr1), Arc::new(cidr2)];
1120-
labeler.update_cidr_table(&cidrs);
1128+
labeler.update_cidr_table(&cidrs, false, &mut false);
11211129

11221130
let mut endpoint: EndpointInfo = Default::default();
11231131
labeler.set_epc_by_cidr("10.1.2.3".parse().unwrap(), 10, &mut endpoint);
@@ -1136,7 +1144,7 @@ mod tests {
11361144

11371145
let mut endpoint: EndpointInfo = Default::default();
11381146

1139-
labeler.update_cidr_table(&vec![Arc::new(cidr1)]);
1147+
labeler.update_cidr_table(&vec![Arc::new(cidr1)], false, &mut false);
11401148
labeler.set_epc_by_cidr("192.168.10.100".parse().unwrap(), 10, &mut endpoint);
11411149
assert_eq!(endpoint.l3_epc_id, 0);
11421150

@@ -1160,7 +1168,7 @@ mod tests {
11601168

11611169
let mut endpoint: EndpointInfo = Default::default();
11621170

1163-
labeler.update_cidr_table(&vec![Arc::new(cidr1)]);
1171+
labeler.update_cidr_table(&vec![Arc::new(cidr1)], false, &mut false);
11641172

11651173
labeler.set_epc_vip_by_tunnel("192.168.10.100".parse().unwrap(), 10, &mut endpoint);
11661174
assert_eq!(endpoint.l3_epc_id, 10);
@@ -1178,7 +1186,7 @@ mod tests {
11781186

11791187
let mut endpoint: EndpointInfo = Default::default();
11801188

1181-
labeler.update_cidr_table(&vec![Arc::new(cidr1)]);
1189+
labeler.update_cidr_table(&vec![Arc::new(cidr1)], false, &mut false);
11821190

11831191
labeler.set_vip_by_cidr("192.168.10.100".parse().unwrap(), 10, &mut endpoint);
11841192
assert_eq!(endpoint.is_vip, true);
@@ -1220,7 +1228,7 @@ mod tests {
12201228

12211229
labeler.update_mac_table(&list);
12221230
labeler.update_epc_ip_table(&list);
1223-
labeler.update_cidr_table(&vec![Arc::new(cidr1)]);
1231+
labeler.update_cidr_table(&vec![Arc::new(cidr1)], false, &mut false);
12241232

12251233
let key: LookupKey = LookupKey {
12261234
src_mac: MacAddr::from_str("11:22:33:44:55:66").unwrap(),
@@ -1260,7 +1268,7 @@ mod tests {
12601268
..Default::default()
12611269
};
12621270
labeler.update_mac_table(&vec![Arc::new(interface)]);
1263-
labeler.update_cidr_table(&vec![Arc::new(cidr)]);
1271+
labeler.update_cidr_table(&vec![Arc::new(cidr)], false, &mut false);
12641272
let mut endpoints: EndpointData = Default::default();
12651273
endpoints.src_info.l3_epc_id = 1;
12661274

@@ -1315,7 +1323,7 @@ mod tests {
13151323
is_vip: true,
13161324
..Default::default()
13171325
};
1318-
labeler.update_cidr_table(&vec![Arc::new(cidr)]);
1326+
labeler.update_cidr_table(&vec![Arc::new(cidr)], false, &mut false);
13191327

13201328
let mut endpoints: EndpointData = Default::default();
13211329
endpoints.dst_info.l3_epc_id = 1;

agent/src/policy/policy.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::sync::{
2121
};
2222

2323
use ahash::AHashMap;
24-
use log::{debug, info, warn};
24+
use log::{debug, info};
2525
use pnet::datalink;
2626
use public::enums::IpProtocol;
2727

@@ -476,17 +476,30 @@ impl Policy {
476476
self.labeler.update_peer_table(peers);
477477
}
478478

479-
pub fn update_cidr(&mut self, cidrs: &Vec<Arc<Cidr>>) {
479+
pub fn update_cidr(
480+
&mut self,
481+
cidrs: &Vec<Arc<Cidr>>,
482+
enabled_invalid_log: bool,
483+
has_invalid_log: &mut bool,
484+
) {
480485
self.table.update_cidr(cidrs);
481-
self.labeler.update_cidr_table(cidrs);
486+
self.labeler
487+
.update_cidr_table(cidrs, enabled_invalid_log, has_invalid_log);
482488
}
483489

484490
pub fn update_container(&mut self, cidrs: &Vec<Arc<Container>>) {
485491
self.labeler.update_container(cidrs);
486492
}
487493

488-
pub fn update_acl(&mut self, acls: &Vec<Arc<Acl>>, check: bool) -> PResult<()> {
489-
self.table.update_acl(acls, check)?;
494+
pub fn update_acl(
495+
&mut self,
496+
acls: &Vec<Arc<Acl>>,
497+
check: bool,
498+
enabled_invalid_log: bool,
499+
has_invalid_log: &mut bool,
500+
) -> PResult<()> {
501+
self.table
502+
.update_acl(acls, check, enabled_invalid_log, has_invalid_log)?;
490503

491504
self.acls = acls.clone();
492505

@@ -553,7 +566,7 @@ impl Policy {
553566
for gpid_entry in gpid_entries.iter() {
554567
let protocol = u8::from(gpid_entry.protocol) as usize;
555568
if protocol >= table.len() {
556-
warn!("Invalid protocol {:?} in {:?}", protocol, &gpid_entry);
569+
debug!("Invalid protocol {:?} in {:?}", protocol, &gpid_entry);
557570
continue;
558571
}
559572

@@ -678,6 +691,8 @@ impl FlowAclListener for PolicySetter {
678691
peers: &Vec<Arc<PeerConnection>>,
679692
cidrs: &Vec<Arc<Cidr>>,
680693
acls: &Vec<Arc<Acl>>,
694+
enabled_invalid_log: bool,
695+
has_invalid_log: &mut bool,
681696
) -> Result<(), String> {
682697
self.update_local_epc(
683698
local_epc,
@@ -686,8 +701,8 @@ impl FlowAclListener for PolicySetter {
686701
self.update_interfaces(agent_type, platform_data);
687702
self.update_ip_group(ip_groups);
688703
self.update_peer_connections(peers);
689-
self.update_cidr(cidrs);
690-
if let Err(e) = self.update_acl(acls, true) {
704+
self.update_cidr(cidrs, enabled_invalid_log, has_invalid_log);
705+
if let Err(e) = self.update_acl(acls, true, enabled_invalid_log, has_invalid_log) {
691706
return Err(format!("{}", e));
692707
}
693708

@@ -727,16 +742,29 @@ impl PolicySetter {
727742
self.policy().update_peer_connections(peers);
728743
}
729744

730-
pub fn update_cidr(&mut self, cidrs: &Vec<Arc<Cidr>>) {
731-
self.policy().update_cidr(cidrs);
745+
pub fn update_cidr(
746+
&mut self,
747+
cidrs: &Vec<Arc<Cidr>>,
748+
enabled_invalid_log: bool,
749+
has_invalid_log: &mut bool,
750+
) {
751+
self.policy()
752+
.update_cidr(cidrs, enabled_invalid_log, has_invalid_log);
732753
}
733754

734755
pub fn update_container(&mut self, containers: &Vec<Arc<Container>>) {
735756
self.policy().update_container(containers);
736757
}
737758

738-
pub fn update_acl(&mut self, acls: &Vec<Arc<Acl>>, check: bool) -> PResult<()> {
739-
self.policy().update_acl(acls, check)?;
759+
pub fn update_acl(
760+
&mut self,
761+
acls: &Vec<Arc<Acl>>,
762+
check: bool,
763+
enabled_invalid_log: bool,
764+
has_invalid_log: &mut bool,
765+
) -> PResult<()> {
766+
self.policy()
767+
.update_acl(acls, check, enabled_invalid_log, has_invalid_log)?;
740768

741769
Ok(())
742770
}
@@ -805,7 +833,7 @@ mod test {
805833
..Default::default()
806834
};
807835
setter.update_interfaces(AgentType::TtHostPod, &vec![Arc::new(interface)]);
808-
setter.update_cidr(&vec![Arc::new(cidr)]);
836+
setter.update_cidr(&vec![Arc::new(cidr)], false, &mut false);
809837
setter.flush();
810838

811839
let mut key = LookupKey {

0 commit comments

Comments
 (0)