Skip to content

fix: reduce the frequency of log printing #9909

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

Merged
merged 2 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions agent/benches/labeler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn bench_labeler(c: &mut Criterion) {

cidr_list.push(Arc::new(cidr));
}
labeler.update_cidr_table(&cidr_list);
labeler.update_cidr_table(&cidr_list, false, &mut false);
labeler.update_interface_table(&iface_list);

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

cidr_list.push(Arc::new(cidr));
}
labeler.update_cidr_table(&cidr_list);
labeler.update_cidr_table(&cidr_list, false, &mut false);
labeler.update_interface_table(&iface_list);

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

first
}
Expand Down
2 changes: 2 additions & 0 deletions agent/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub trait FlowAclListener: Send + Sync {
peers: &Vec<Arc<PeerConnection>>,
cidrs: &Vec<Arc<Cidr>>,
acls: &Vec<Arc<Acl>>,
enabled_invalid_log: bool,
has_invalid_log: &mut bool,
) -> Result<(), String>;
fn containers_change(&mut self, _: &Vec<Arc<Container>>) {}
fn id(&self) -> usize;
Expand Down
8 changes: 4 additions & 4 deletions agent/src/debug/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl RpcDebugger {
}

let mut sg = self.status.write();
sg.get_platform_data(&resp);
sg.get_platform_data(&resp, false);
let mut res = sg
.cidrs
.iter()
Expand All @@ -181,7 +181,7 @@ impl RpcDebugger {
}

let mut sg = self.status.write();
sg.get_platform_data(&resp);
sg.get_platform_data(&resp, false);
let mut res = sg
.interfaces
.iter()
Expand Down Expand Up @@ -211,7 +211,7 @@ impl RpcDebugger {
}

let mut sg = self.status.write();
sg.get_ip_groups(&resp);
sg.get_ip_groups(&resp, false);
let mut res = sg
.ip_groups
.iter()
Expand All @@ -236,7 +236,7 @@ impl RpcDebugger {
}

let mut sg = self.status.write();
sg.get_flow_acls(&resp);
sg.get_flow_acls(&resp, false);
let mut res = sg
.acls
.iter()
Expand Down
2 changes: 2 additions & 0 deletions agent/src/dispatcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ impl FlowAclListener for DispatcherListener {
_: &Vec<Arc<crate::common::policy::PeerConnection>>,
_: &Vec<Arc<crate::_Cidr>>,
_: &Vec<Arc<crate::_Acl>>,
_: bool,
_: &mut bool,
) -> Result<(), String> {
match self {
DispatcherListener::Local(a) => a.flow_acl_change(),
Expand Down
2 changes: 2 additions & 0 deletions agent/src/ebpf_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ impl FlowAclListener for SyncEbpfDispatcher {
_: &Vec<Arc<crate::common::policy::PeerConnection>>,
_: &Vec<Arc<crate::_Cidr>>,
_: &Vec<Arc<crate::_Acl>>,
_: bool,
_: &mut bool,
) -> Result<(), String> {
self.pause.store(false, Ordering::Relaxed);
Ok(())
Expand Down
24 changes: 20 additions & 4 deletions agent/src/policy/first_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ impl FirstPath {
}

if self.group_ip_map.is_none() {
warn!("IpGroup is nil, invalid acl: {}", acl);
return false;
}

Expand All @@ -352,7 +351,6 @@ impl FirstPath {
.get(&(*group as u16))
.is_none()
{
warn!("Invalid acl by src group({}): {}", group, acl);
return true;
}
}
Expand All @@ -365,7 +363,6 @@ impl FirstPath {
.get(&(*group as u16))
.is_none()
{
warn!("Invalid acl by dst group({}): {}", group, acl);
return true;
}
}
Expand Down Expand Up @@ -593,19 +590,38 @@ impl FirstPath {
Ok(())
}

pub fn update_acl(&mut self, acls: &Vec<Arc<Acl>>, check: bool) -> PResult<()> {
pub fn update_acl(
&mut self,
acls: &Vec<Arc<Acl>>,
check: bool,
enabled_invalid_log: bool,
has_invalid_log: &mut bool,
) -> PResult<()> {
if !NOT_SUPPORT {
let mut valid_acls = Vec::new();
let mut invalid_acls = vec![];

for acl in acls {
if self.is_invalid_acl(acl, check) {
if enabled_invalid_log {
invalid_acls.push(acl.id);
}
continue;
}
let mut valid_acl = (**acl).clone();

valid_acl.reset();
valid_acls.push(valid_acl);
}

if enabled_invalid_log && !invalid_acls.is_empty() {
warn!(
"Invalid acls: {:?}, maybe the IP resource group doesn't have an IP address.",
invalid_acls
);
*has_invalid_log = true;
}

self.generate_first_table(&mut valid_acls)?;
}

Expand Down
39 changes: 24 additions & 15 deletions agent/src/policy/labeler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,16 @@ impl Labeler {
return (0, 0);
}

pub fn update_cidr_table(&mut self, cidrs: &Vec<Arc<Cidr>>) {
pub fn update_cidr_table(
&mut self,
cidrs: &Vec<Arc<Cidr>>,
enabled_invalid_log: bool,
has_invalid_log: &mut bool,
) {
let mut masklen_table: AHashMap<i32, (u8, u8)> = AHashMap::new();
let mut epc_table: AHashMap<EpcNetIpKey, Arc<Cidr>> = AHashMap::new();
let mut tunnel_table: AHashMap<u32, Vec<Arc<Cidr>>> = AHashMap::new();
let mut invalid_cidr = Vec::new();

for item in cidrs {
let mut epc_id = item.epc_id;
Expand All @@ -272,13 +278,11 @@ impl Labeler {
let key = EpcNetIpKey::new(&item.ip.network(), item.ip.prefix_len(), epc_id);

if let Some(old) = epc_table.insert(key, item.clone()) {
if (item.cidr_type == CidrType::Wan && item.epc_id != old.epc_id)
|| item.is_vip != old.is_vip
if enabled_invalid_log
&& ((item.cidr_type == CidrType::Wan && item.epc_id != old.epc_id)
|| item.is_vip != old.is_vip)
{
warn!(
"Found the same cidr, please check {:?} and {:?}.",
item, old
);
invalid_cidr.push(item.ip)
}
}
masklen_table
Expand All @@ -301,6 +305,11 @@ impl Labeler {
}
}

if enabled_invalid_log && !invalid_cidr.is_empty() {
warn!("Invalid same cidr: {:?}", invalid_cidr);
*has_invalid_log = true;
}

// 排序使用降序是为了CIDR的最长前缀匹配
for (_k, v) in &mut tunnel_table.iter_mut() {
v.sort_by(|a, b| {
Expand Down Expand Up @@ -1095,7 +1104,7 @@ mod tests {
let cidrs = vec![Arc::new(cidr1), Arc::new(cidr2), Arc::new(cidr3)];
let mut endpoint: EndpointInfo = Default::default();

labeler.update_cidr_table(&cidrs);
labeler.update_cidr_table(&cidrs, false, &mut false);

labeler.set_epc_by_cidr("192.168.10.100".parse().unwrap(), 10, &mut endpoint);
assert_eq!(endpoint.is_vip, true);
Expand All @@ -1117,7 +1126,7 @@ mod tests {
..Default::default()
};
let cidrs = vec![Arc::new(cidr1), Arc::new(cidr2)];
labeler.update_cidr_table(&cidrs);
labeler.update_cidr_table(&cidrs, false, &mut false);

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

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

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

Expand All @@ -1160,7 +1169,7 @@ mod tests {

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

labeler.update_cidr_table(&vec![Arc::new(cidr1)]);
labeler.update_cidr_table(&vec![Arc::new(cidr1)], false, &mut false);

labeler.set_epc_vip_by_tunnel("192.168.10.100".parse().unwrap(), 10, &mut endpoint);
assert_eq!(endpoint.l3_epc_id, 10);
Expand All @@ -1178,7 +1187,7 @@ mod tests {

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

labeler.update_cidr_table(&vec![Arc::new(cidr1)]);
labeler.update_cidr_table(&vec![Arc::new(cidr1)], false, &mut false);

labeler.set_vip_by_cidr("192.168.10.100".parse().unwrap(), 10, &mut endpoint);
assert_eq!(endpoint.is_vip, true);
Expand Down Expand Up @@ -1220,7 +1229,7 @@ mod tests {

labeler.update_mac_table(&list);
labeler.update_epc_ip_table(&list);
labeler.update_cidr_table(&vec![Arc::new(cidr1)]);
labeler.update_cidr_table(&vec![Arc::new(cidr1)], false, &mut false);

let key: LookupKey = LookupKey {
src_mac: MacAddr::from_str("11:22:33:44:55:66").unwrap(),
Expand Down Expand Up @@ -1260,7 +1269,7 @@ mod tests {
..Default::default()
};
labeler.update_mac_table(&vec![Arc::new(interface)]);
labeler.update_cidr_table(&vec![Arc::new(cidr)]);
labeler.update_cidr_table(&vec![Arc::new(cidr)], false, &mut false);
let mut endpoints: EndpointData = Default::default();
endpoints.src_info.l3_epc_id = 1;

Expand Down Expand Up @@ -1315,7 +1324,7 @@ mod tests {
is_vip: true,
..Default::default()
};
labeler.update_cidr_table(&vec![Arc::new(cidr)]);
labeler.update_cidr_table(&vec![Arc::new(cidr)], false, &mut false);

let mut endpoints: EndpointData = Default::default();
endpoints.dst_info.l3_epc_id = 1;
Expand Down
54 changes: 41 additions & 13 deletions agent/src/policy/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::sync::{
};

use ahash::AHashMap;
use log::{debug, info, warn};
use log::{debug, info};
use pnet::datalink;
use public::enums::IpProtocol;

Expand Down Expand Up @@ -476,17 +476,30 @@ impl Policy {
self.labeler.update_peer_table(peers);
}

pub fn update_cidr(&mut self, cidrs: &Vec<Arc<Cidr>>) {
pub fn update_cidr(
&mut self,
cidrs: &Vec<Arc<Cidr>>,
enabled_invalid_log: bool,
has_invalid_log: &mut bool,
) {
self.table.update_cidr(cidrs);
self.labeler.update_cidr_table(cidrs);
self.labeler
.update_cidr_table(cidrs, enabled_invalid_log, has_invalid_log);
}

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

pub fn update_acl(&mut self, acls: &Vec<Arc<Acl>>, check: bool) -> PResult<()> {
self.table.update_acl(acls, check)?;
pub fn update_acl(
&mut self,
acls: &Vec<Arc<Acl>>,
check: bool,
enabled_invalid_log: bool,
has_invalid_log: &mut bool,
) -> PResult<()> {
self.table
.update_acl(acls, check, enabled_invalid_log, has_invalid_log)?;

self.acls = acls.clone();

Expand Down Expand Up @@ -553,7 +566,7 @@ impl Policy {
for gpid_entry in gpid_entries.iter() {
let protocol = u8::from(gpid_entry.protocol) as usize;
if protocol >= table.len() {
warn!("Invalid protocol {:?} in {:?}", protocol, &gpid_entry);
debug!("Invalid protocol {:?} in {:?}", protocol, &gpid_entry);
continue;
}

Expand Down Expand Up @@ -678,6 +691,8 @@ impl FlowAclListener for PolicySetter {
peers: &Vec<Arc<PeerConnection>>,
cidrs: &Vec<Arc<Cidr>>,
acls: &Vec<Arc<Acl>>,
enabled_invalid_log: bool,
has_invalid_log: &mut bool,
) -> Result<(), String> {
self.update_local_epc(
local_epc,
Expand All @@ -686,8 +701,8 @@ impl FlowAclListener for PolicySetter {
self.update_interfaces(agent_type, platform_data);
self.update_ip_group(ip_groups);
self.update_peer_connections(peers);
self.update_cidr(cidrs);
if let Err(e) = self.update_acl(acls, true) {
self.update_cidr(cidrs, enabled_invalid_log, has_invalid_log);
if let Err(e) = self.update_acl(acls, true, enabled_invalid_log, has_invalid_log) {
return Err(format!("{}", e));
}

Expand Down Expand Up @@ -727,16 +742,29 @@ impl PolicySetter {
self.policy().update_peer_connections(peers);
}

pub fn update_cidr(&mut self, cidrs: &Vec<Arc<Cidr>>) {
self.policy().update_cidr(cidrs);
pub fn update_cidr(
&mut self,
cidrs: &Vec<Arc<Cidr>>,
enabled_invalid_log: bool,
has_invalid_log: &mut bool,
) {
self.policy()
.update_cidr(cidrs, enabled_invalid_log, has_invalid_log);
}

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

pub fn update_acl(&mut self, acls: &Vec<Arc<Acl>>, check: bool) -> PResult<()> {
self.policy().update_acl(acls, check)?;
pub fn update_acl(
&mut self,
acls: &Vec<Arc<Acl>>,
check: bool,
enabled_invalid_log: bool,
has_invalid_log: &mut bool,
) -> PResult<()> {
self.policy()
.update_acl(acls, check, enabled_invalid_log, has_invalid_log)?;

Ok(())
}
Expand Down Expand Up @@ -805,7 +833,7 @@ mod test {
..Default::default()
};
setter.update_interfaces(AgentType::TtHostPod, &vec![Arc::new(interface)]);
setter.update_cidr(&vec![Arc::new(cidr)]);
setter.update_cidr(&vec![Arc::new(cidr)], false, &mut false);
setter.flush();

let mut key = LookupKey {
Expand Down
Loading