|
| 1 | +// |
| 2 | +// Copyright (c) 2025 ZettaScale Technology |
| 3 | +// |
| 4 | +// This program and the accompanying materials are made available under the |
| 5 | +// terms of the Eclipse Public License 2.0 which is available at |
| 6 | +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +// |
| 9 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | +// |
| 11 | +// Contributors: |
| 12 | +// ZettaScale Zenoh Team, <[email protected]> |
| 13 | +// |
| 14 | +use std::{net::SocketAddr, num::ParseIntError}; |
| 15 | + |
| 16 | +use tracing::warn; |
| 17 | +use zenoh_protocol::core::Config; |
| 18 | +use zenoh_result::{zerror, ZResult}; |
| 19 | + |
| 20 | +use crate::DSCP; |
| 21 | + |
| 22 | +fn parse_int(s: &str) -> Result<u32, ParseIntError> { |
| 23 | + if let Some(s) = ["0x", "0X"].iter().find_map(|pfx| s.strip_prefix(pfx)) { |
| 24 | + u32::from_str_radix(s, 16) |
| 25 | + } else if let Some(s) = ["0xb", "0B"].iter().find_map(|pfx| s.strip_prefix(pfx)) { |
| 26 | + u32::from_str_radix(s, 2) |
| 27 | + } else { |
| 28 | + s.parse::<u32>() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// Parse DSCP config. |
| 33 | +/// |
| 34 | +/// It supports hexa/binary prefixes, as well as `|` operator, e.g. `dscp=0x04|0x10`. |
| 35 | +pub fn parse_dscp(config: &Config) -> ZResult<Option<u32>> { |
| 36 | + let Some(dscp) = config.get(DSCP) else { |
| 37 | + return Ok(None); |
| 38 | + }; |
| 39 | + Ok(Some( |
| 40 | + dscp.split('|') |
| 41 | + .map(parse_int) |
| 42 | + .map(Result::ok) |
| 43 | + .reduce(|a, b| Some(a? | b?)) |
| 44 | + .flatten() |
| 45 | + .ok_or_else(|| zerror!("Unknown DSCP argument: {dscp}"))?, |
| 46 | + )) |
| 47 | +} |
| 48 | + |
| 49 | +/// Set DSCP option to the socket if supported by the target. |
| 50 | +/// |
| 51 | +/// If the target doesn't support it, a warning is emitted. |
| 52 | +/// IPv4 uses IP_TOS, while IPv6 uses IPV6_TCLASS |
| 53 | +pub fn set_dscp<'a>( |
| 54 | + socket: impl Into<socket2::SockRef<'a>>, |
| 55 | + addr: SocketAddr, |
| 56 | + dscp: u32, |
| 57 | +) -> std::io::Result<()> { |
| 58 | + match addr { |
| 59 | + #[cfg(not(any( |
| 60 | + target_os = "fuchsia", |
| 61 | + target_os = "redox", |
| 62 | + target_os = "solaris", |
| 63 | + target_os = "illumos", |
| 64 | + )))] |
| 65 | + SocketAddr::V4(_) => socket.into().set_tos(dscp)?, |
| 66 | + #[cfg(any( |
| 67 | + target_os = "android", |
| 68 | + target_os = "dragonfly", |
| 69 | + target_os = "freebsd", |
| 70 | + target_os = "fuchsia", |
| 71 | + target_os = "linux", |
| 72 | + target_os = "macos", |
| 73 | + target_os = "netbsd", |
| 74 | + target_os = "openbsd" |
| 75 | + ))] |
| 76 | + SocketAddr::V6(_) => socket.into().set_tclass_v6(dscp)?, |
| 77 | + #[allow(unreachable_patterns)] |
| 78 | + SocketAddr::V4(_) => warn!( |
| 79 | + "IPv4 DSCP is unsupported on platform {}", |
| 80 | + std::env::consts::OS |
| 81 | + ), |
| 82 | + #[allow(unreachable_patterns)] |
| 83 | + SocketAddr::V6(_) => warn!( |
| 84 | + "IPv6 DSCP is unsupported on platform {}", |
| 85 | + std::env::consts::OS |
| 86 | + ), |
| 87 | + } |
| 88 | + Ok(()) |
| 89 | +} |
0 commit comments