Skip to content

Commit 29e5734

Browse files
Add loop logging macros
1 parent 31b5333 commit 29e5734

File tree

3 files changed

+64
-70
lines changed

3 files changed

+64
-70
lines changed

src/logging.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,29 @@ macro_rules! log_debug_dir {
7777
}
7878
};
7979
}
80+
81+
#[macro_export]
82+
macro_rules! option_or_log_continue {
83+
($opt:expr, $log_macro:ident, $($args:tt)*) => {
84+
match $opt {
85+
Some(v) => v,
86+
None => {
87+
$log_macro!($($args)*);
88+
continue;
89+
}
90+
}
91+
};
92+
}
93+
94+
#[macro_export]
95+
macro_rules! result_or_log_continue {
96+
($res:expr, $log_macro:ident, $($args:tt)*) => {
97+
match $res {
98+
Ok(v) => v,
99+
Err(e) => {
100+
$log_macro!($($args)*, e);
101+
continue;
102+
}
103+
}
104+
};
105+
}

src/net/payload.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const fn be16_16(b0: u8, b1: u8) -> u16 {
3030
}
3131

3232
#[inline]
33-
fn validate_payload<'a>(
33+
pub(crate) fn validate_payload<'a>(
3434
c2u: bool,
3535
cfg: &Config,
3636
stats: &Stats,
@@ -156,29 +156,6 @@ pub(crate) fn send_payload(
156156
}
157157
}
158158

159-
pub(crate) fn validate_payload_or_log<'a>(
160-
c2u: bool,
161-
worker_id: usize,
162-
cfg: &Config,
163-
stats: &Stats,
164-
buf: &'a [u8],
165-
recv_port_id: u16,
166-
) -> Option<ValidatedPayload<'a>> {
167-
match validate_payload(c2u, cfg, stats, buf, recv_port_id) {
168-
Ok(v) => Some(v),
169-
Err(e) => {
170-
log_debug_dir!(
171-
cfg.debug_log_drops,
172-
worker_id,
173-
c2u,
174-
"validate_payload error: {}",
175-
e
176-
);
177-
None
178-
}
179-
}
180-
}
181-
182159
pub(crate) fn handle_payload_result(
183160
c2u: bool,
184161
worker_id: usize,

src/worker.rs

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cli::{Config, TimeoutAction};
22
use crate::net::params::MAX_WIRE_PAYLOAD;
3-
use crate::net::payload::{handle_payload_result, send_payload, validate_payload_or_log};
3+
use crate::net::payload::{handle_payload_result, send_payload, validate_payload};
44
use crate::net::sock_mgr::{SocketHandles, SocketManager};
55
use crate::stats::Stats;
66
use socket2::{SockAddr, Type};
@@ -236,16 +236,14 @@ pub fn run_upstream_to_client_thread(
236236
cache.refresh_handles_and_cache(sock_mgr, &mut handles);
237237

238238
if locked.load(AtomOrdering::Relaxed) {
239-
let Some(validated) = validate_payload_or_log(
240-
C2U,
239+
let validated = result_or_log_continue!(
240+
validate_payload(C2U, cfg, stats, &buf.data[..len], cache.recv_port_id),
241+
log_debug_dir,
242+
cfg.debug_log_drops,
241243
worker_id,
242-
cfg,
243-
stats,
244-
&buf.data[..len],
245-
cache.recv_port_id,
246-
) else {
247-
continue;
248-
};
244+
C2U,
245+
"validate_payload error: {}"
246+
);
249247
let send_res = send_payload(
250248
C2U,
251249
&validated,
@@ -314,16 +312,14 @@ pub fn run_client_to_upstream_thread(
314312
let t_recv = Instant::now();
315313

316314
if locked.load(AtomOrdering::Relaxed) {
317-
let Some(validated) = validate_payload_or_log(
318-
C2U,
315+
let validated = result_or_log_continue!(
316+
validate_payload(C2U, cfg, stats, &buf.data[..len], cache.recv_port_id),
317+
log_debug_dir,
318+
cfg.debug_log_drops,
319319
worker_id,
320-
cfg,
321-
stats,
322-
&buf.data[..len],
323-
cache.recv_port_id,
324-
) else {
325-
continue;
326-
};
320+
C2U,
321+
"validate_payload error: {}"
322+
);
327323
let send_res = send_payload(
328324
C2U,
329325
&validated,
@@ -365,26 +361,23 @@ pub fn run_client_to_upstream_thread(
365361

366362
// First lock: publish client and connect the socket for fast path
367363
if !locked.load(AtomOrdering::Relaxed) {
368-
let Some(src) = src_sa.as_socket() else {
369-
log_warn_dir!(
370-
worker_id,
371-
C2U,
372-
"recv_from client non-IP address family (ignored): {:?}",
373-
src_sa
374-
);
375-
continue;
376-
};
377-
378-
let Some(validated) = validate_payload_or_log(
364+
let src = option_or_log_continue!(
365+
src_sa.as_socket(),
366+
log_warn_dir,
367+
worker_id,
379368
C2U,
369+
"recv_from client non-IP address family (ignored): {:?}",
370+
src_sa
371+
);
372+
373+
let validated = result_or_log_continue!(
374+
validate_payload(C2U, cfg, stats, &buf.data[..len], cache.recv_port_id),
375+
log_debug_dir,
376+
cfg.debug_log_drops,
380377
worker_id,
381-
cfg,
382-
stats,
383-
&buf.data[..len],
384-
cache.recv_port_id,
385-
) else {
386-
continue;
387-
};
378+
C2U,
379+
"validate_payload error: {}"
380+
);
388381

389382
// Signal to other threads that a client is currently being locked
390383
locked.store(true, AtomOrdering::Relaxed);
@@ -468,16 +461,14 @@ pub fn run_client_to_upstream_thread(
468461
);
469462
} else if Some(src_sa) == cache.client_sa {
470463
// Only forward packets from the locked client (recv_from may still deliver before connect succeeds)
471-
let Some(validated) = validate_payload_or_log(
472-
C2U,
464+
let validated = result_or_log_continue!(
465+
validate_payload(C2U, cfg, stats, &buf.data[..len], cache.recv_port_id),
466+
log_debug_dir,
467+
cfg.debug_log_drops,
473468
worker_id,
474-
cfg,
475-
stats,
476-
&buf.data[..len],
477-
cache.recv_port_id,
478-
) else {
479-
continue;
480-
};
469+
C2U,
470+
"validate_payload error: {}"
471+
);
481472
let send_res = send_payload(
482473
C2U,
483474
&validated,

0 commit comments

Comments
 (0)