Skip to content

Commit

Permalink
[API break] nsid: Reorganize the code base
Browse files Browse the repository at this point in the history
 * This patch is first preparation on stable release 1.0 for submodule
   nsid. The detailed checklist for developer is stated in
   README.md file.
 * Stored constants into where it been used. Removed `constants.rs`
   as this is the last submodule for refactor.
 * Replace `pub use self::*` to explicit expose.
 * Renamed `nsid::Nla` to `NsidAttribute` and all its entry
   changed to detailed data structure instead of `Vec<u8>`
 * The `RtnlMessasge` changed to `RouteNetlinkMessage`.
 * Unit test cases included.

Signed-off-by: Gris Ge <[email protected]>
  • Loading branch information
cathay4t committed Dec 1, 2023
1 parent c490423 commit 4e35a89
Show file tree
Hide file tree
Showing 23 changed files with 1,060 additions and 2,094 deletions.
8 changes: 4 additions & 4 deletions examples/dump_neighbour_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use netlink_packet_core::{
NetlinkHeader, NetlinkMessage, NetlinkPayload, NLM_F_DUMP, NLM_F_REQUEST,
};
use netlink_packet_route::{
neighbour_table::NeighbourTableMessage, RtnlMessage,
neighbour_table::NeighbourTableMessage, RouteNetlinkMessage,
};
use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};

Expand All @@ -18,7 +18,7 @@ fn main() {

let mut req = NetlinkMessage::new(
nl_hdr,
NetlinkPayload::from(RtnlMessage::GetNeighbourTable(
NetlinkPayload::from(RouteNetlinkMessage::GetNeighbourTable(
NeighbourTableMessage::default(),
)),
);
Expand All @@ -42,13 +42,13 @@ fn main() {
loop {
let bytes = &receive_buffer[offset..];
// Parse the message
let msg: NetlinkMessage<RtnlMessage> =
let msg: NetlinkMessage<RouteNetlinkMessage> =
NetlinkMessage::deserialize(bytes).unwrap();

match msg.payload {
NetlinkPayload::Done(_) => break 'outer,
NetlinkPayload::InnerMessage(
RtnlMessage::NewNeighbourTable(entry),
RouteNetlinkMessage::NewNeighbourTable(entry),
) => {
println!("HAHA {:?}", entry);
}
Expand Down
12 changes: 6 additions & 6 deletions examples/dump_neighbours.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use netlink_packet_core::{
};
use netlink_packet_route::{
neighbour::{NeighbourAddress, NeighbourAttribute, NeighbourMessage},
AddressFamily, RtnlMessage,
AddressFamily, RouteNetlinkMessage,
};
use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};

Expand All @@ -21,7 +21,7 @@ fn main() {

let mut req = NetlinkMessage::new(
nl_hdr,
NetlinkPayload::from(RtnlMessage::GetNeighbour(
NetlinkPayload::from(RouteNetlinkMessage::GetNeighbour(
NeighbourMessage::default(),
)),
);
Expand All @@ -45,14 +45,14 @@ fn main() {
loop {
let bytes = &receive_buffer[offset..];
// Parse the message
let msg: NetlinkMessage<RtnlMessage> =
let msg: NetlinkMessage<RouteNetlinkMessage> =
NetlinkMessage::deserialize(bytes).unwrap();

match msg.payload {
NetlinkPayload::Done(_) => break 'outer,
NetlinkPayload::InnerMessage(RtnlMessage::NewNeighbour(
entry,
)) => {
NetlinkPayload::InnerMessage(
RouteNetlinkMessage::NewNeighbour(entry),
) => {
let address_family = entry.header.family;
if address_family == AddressFamily::Inet
|| address_family == AddressFamily::Inet6
Expand Down
8 changes: 5 additions & 3 deletions examples/dump_packet_link_bridge_vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use netlink_packet_core::{
};
use netlink_packet_route::{
link::{LinkAttribute, LinkMessage},
AddressFamily, RtnlMessage, RTEXT_FILTER_BRVLAN_COMPRESSED,
AddressFamily, RouteNetlinkMessage,
};
use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};

const RTEXT_FILTER_BRVLAN_COMPRESSED: u32 = 1 << 2;

fn main() {
let mut socket = Socket::new(NETLINK_ROUTE).unwrap();
let _port_number = socket.bind_auto().unwrap().port_number();
Expand All @@ -21,7 +23,7 @@ fn main() {
.push(LinkAttribute::ExtMask(RTEXT_FILTER_BRVLAN_COMPRESSED));
let mut packet = NetlinkMessage::new(
NetlinkHeader::default(),
NetlinkPayload::from(RtnlMessage::GetLink(message)),
NetlinkPayload::from(RouteNetlinkMessage::GetLink(message)),
);
packet.header.flags = NLM_F_DUMP | NLM_F_REQUEST;
packet.header.sequence_number = 1;
Expand Down Expand Up @@ -62,7 +64,7 @@ fn main() {
// Parseable<NetlinkMessage>>::parse(NetlinkBuffer::new(&bytes))
// .unwrap();
//
let rx_packet: NetlinkMessage<RtnlMessage> =
let rx_packet: NetlinkMessage<RouteNetlinkMessage> =
NetlinkMessage::deserialize(bytes).unwrap();

println!("<<< {rx_packet:?}");
Expand Down
8 changes: 5 additions & 3 deletions examples/dump_packet_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use netlink_packet_core::{
NetlinkHeader, NetlinkMessage, NetlinkPayload, NLM_F_DUMP, NLM_F_REQUEST,
};
use netlink_packet_route::{link::LinkMessage, RtnlMessage};
use netlink_packet_route::{link::LinkMessage, RouteNetlinkMessage};
use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};

fn main() {
Expand All @@ -13,7 +13,9 @@ fn main() {

let mut packet = NetlinkMessage::new(
NetlinkHeader::default(),
NetlinkPayload::from(RtnlMessage::GetLink(LinkMessage::default())),
NetlinkPayload::from(RouteNetlinkMessage::GetLink(
LinkMessage::default(),
)),
);
packet.header.flags = NLM_F_DUMP | NLM_F_REQUEST;
packet.header.sequence_number = 1;
Expand Down Expand Up @@ -54,7 +56,7 @@ fn main() {
// Parseable<NetlinkMessage>>::parse(NetlinkBuffer::new(&bytes))
// .unwrap();
//
let rx_packet: NetlinkMessage<RtnlMessage> =
let rx_packet: NetlinkMessage<RouteNetlinkMessage> =
NetlinkMessage::deserialize(bytes).unwrap();

println!("<<< {rx_packet:?}");
Expand Down
9 changes: 6 additions & 3 deletions examples/dump_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use netlink_packet_core::{
NetlinkHeader, NetlinkMessage, NetlinkPayload, NLM_F_DUMP, NLM_F_REQUEST,
};
use netlink_packet_route::{rule::RuleMessage, RtnlMessage};
use netlink_packet_route::{rule::RuleMessage, RouteNetlinkMessage};
use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};

fn main() {
Expand All @@ -15,7 +15,9 @@ fn main() {
nl_hdr.flags = NLM_F_REQUEST | NLM_F_DUMP;
let mut packet = NetlinkMessage::new(
nl_hdr,
NetlinkPayload::from(RtnlMessage::GetRule(RuleMessage::default())),
NetlinkPayload::from(RouteNetlinkMessage::GetRule(
RuleMessage::default(),
)),
);

packet.finalize();
Expand Down Expand Up @@ -44,7 +46,8 @@ fn main() {
loop {
let bytes = &receive_buffer[offset..];
let rx_packet =
<NetlinkMessage<RtnlMessage>>::deserialize(bytes).unwrap();
<NetlinkMessage<RouteNetlinkMessage>>::deserialize(bytes)
.unwrap();
println!("<<< {rx_packet:?}");

if matches!(rx_packet.payload, NetlinkPayload::Done(_)) {
Expand Down
7 changes: 4 additions & 3 deletions examples/new_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use netlink_packet_core::{
use netlink_packet_route::{
route::RouteProtocol,
rule::{RuleAction, RuleAttribute, RuleHeader, RuleMessage},
AddressFamily, RtnlMessage,
AddressFamily, RouteNetlinkMessage,
};
use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};

Expand Down Expand Up @@ -36,7 +36,7 @@ fn main() {

let mut msg = NetlinkMessage::new(
nl_hdr,
NetlinkPayload::from(RtnlMessage::NewRule(rule_msg)),
NetlinkPayload::from(RouteNetlinkMessage::NewRule(rule_msg)),
);

msg.finalize();
Expand All @@ -54,7 +54,8 @@ fn main() {

while let Ok(_size) = socket.recv(&mut receive_buffer, 0) {
let bytes = &receive_buffer[..];
let rx_packet = <NetlinkMessage<RtnlMessage>>::deserialize(bytes);
let rx_packet =
<NetlinkMessage<RouteNetlinkMessage>>::deserialize(bytes);
println!("<<< {rx_packet:?}");
if let Ok(rx_packet) = rx_packet {
if let NetlinkPayload::Error(e) = rx_packet.payload {
Expand Down
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// SPDX-License-Identifier: MIT

pub mod rtnl;
pub use self::rtnl::*;

pub mod address;
pub mod link;
pub mod neighbour;
pub mod neighbour_table;
pub mod nsid;
pub mod route;
pub mod rule;
pub mod tc;

mod message;
#[cfg(test)]
mod tests;

pub(crate) mod ip;

#[cfg(any(target_os = "linux", target_os = "fuchsia"))]
Expand All @@ -37,6 +39,7 @@ mod address_family_fallback;
pub use self::address_family_fallback::AddressFamily;

pub use self::ip::IpProtocol;
pub use self::message::{RouteNetlinkMessage, RouteNetlinkMessageBuffer};

/// The `netlink-packet-route` crate is designed to abstract Netlink route
/// protocol(`rtnetlink`) packet into Rust data types. The goal of this crate is
Expand Down
Loading

0 comments on commit 4e35a89

Please sign in to comment.