From f8cf01096e2d701832a6ecbfa08ed11740140622 Mon Sep 17 00:00:00 2001 From: Nawasan Wisitsingkhon Date: Sun, 5 May 2024 16:07:41 +0700 Subject: [PATCH] refactor and plan for others message --- src/main.rs | 7 ++- src/openflow/controller.rs | 18 +++--- src/openflow/events/features_req.rs | 10 +++- .../events/flow_mod/flow_mod_handler.rs | 8 ++- src/openflow/events/hello.rs | 11 +++- src/openflow/message.rs | 47 --------------- src/openflow/messages/mod.rs | 9 +++ src/openflow/messages/ofp_message.rs | 7 +++ src/openflow/messages/ofp_v1_0.rs | 58 +++++++++++++++++++ src/openflow/messages/traiter.rs | 29 ++++++++++ src/openflow/mod.rs | 6 +- src/openflow/trait_marshal.rs | 11 ---- 12 files changed, 141 insertions(+), 80 deletions(-) delete mode 100644 src/openflow/message.rs create mode 100644 src/openflow/messages/mod.rs create mode 100644 src/openflow/messages/ofp_message.rs create mode 100644 src/openflow/messages/ofp_v1_0.rs create mode 100644 src/openflow/messages/traiter.rs delete mode 100644 src/openflow/trait_marshal.rs diff --git a/src/main.rs b/src/main.rs index 66aa315..dfa2694 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,9 @@ use std::io::Read; use std::net::TcpListener; use tenjin::openflow::events::packet_in::PacketInEvent; -use tenjin::openflow::message::Openflow10; -use tenjin::openflow::{Controller, OfpHeader, OfpMsg}; +use tenjin::openflow::messages::{OfpMsg, Openflow10}; +use tenjin::openflow::traiter::OfpMsgEvent; +use tenjin::openflow::{Controller, OfpHeader}; extern crate byteorder; @@ -32,7 +33,7 @@ fn main() -> Result<(), std::io::Error> { let length_payload = packet.size(); let mut payload = vec![0u8; length_payload]; stream.read(&mut payload)?; - let message = OfpMsg::parse(packet.message); + let message = controller.ofp.msg_parse(packet.message as u16); match message { // 0 is Hello message diff --git a/src/openflow/controller.rs b/src/openflow/controller.rs index e0e4b08..a03626e 100644 --- a/src/openflow/controller.rs +++ b/src/openflow/controller.rs @@ -1,11 +1,16 @@ use std::{collections::HashMap, io::Write, net::TcpStream}; use super::{ - events::PacketInEvent, message::OfpMsgEvent, trait_marshal::MessageMarshal, OfpHeader, + events::PacketInEvent, + messages::traiter::{MessageMarshal, OfpMsgEvent}, }; pub struct Controller { - ofp: OME, + /* + * pub is temporary, remove soon; + * for test in main func + */ + pub ofp: OME, mac_to_port: HashMap, } @@ -22,12 +27,9 @@ impl Controller { let mut header_bytes: Vec = Vec::new(); let mut body_bytes: Vec = Vec::new(); msg.marshal(&mut body_bytes); - let ofpheader = OfpHeader::new( - self.ofp.version() as u8, - msg.msg_code() as u8, - body_bytes.len() as u16, - xid, - ); + let ofpheader = + self.ofp + .header(msg.msg_usize(&self.ofp) as u8, body_bytes.len() as u16, xid); ofpheader.marshal(&mut header_bytes); header_bytes.append(&mut body_bytes); let _ = stream.write_all(&header_bytes); diff --git a/src/openflow/events/features_req.rs b/src/openflow/events/features_req.rs index aac99c0..91814ed 100644 --- a/src/openflow/events/features_req.rs +++ b/src/openflow/events/features_req.rs @@ -1,4 +1,4 @@ -use crate::openflow::trait_marshal::MessageMarshal; +use crate::openflow::messages::{MessageMarshal, OfpMsg, OfpMsgEvent}; pub struct FeaturesReq {} @@ -11,11 +11,15 @@ impl FeaturesReq { impl MessageMarshal for FeaturesReq { fn marshal(&self, _: &mut Vec) {} - fn msg_code(&self) -> crate::openflow::OfpMsg { - crate::openflow::OfpMsg::FeaturesReq + fn msg_code(&self) -> OfpMsg { + OfpMsg::FeaturesReq } fn size_of(&self) -> usize { 0 } + + fn msg_usize(&self, ofp: &OFP) -> usize { + ofp.msg_usize(OfpMsg::FeaturesReq) + } } diff --git a/src/openflow/events/flow_mod/flow_mod_handler.rs b/src/openflow/events/flow_mod/flow_mod_handler.rs index 65afc95..2c45fa8 100644 --- a/src/openflow/events/flow_mod/flow_mod_handler.rs +++ b/src/openflow/events/flow_mod/flow_mod_handler.rs @@ -2,7 +2,10 @@ use std::io::Cursor; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; -use crate::openflow::{trait_marshal::MessageMarshal, OfpMsg, OfpPort, PseudoPort}; +use crate::openflow::{ + messages::{MessageMarshal, OfpMsg, OfpMsgEvent}, + OfpPort, PseudoPort, +}; use super::{FlowAction, FlowModCommand, MatchFields}; @@ -89,6 +92,9 @@ impl FlowModEvent { } impl MessageMarshal for FlowModEvent { + fn msg_usize(&self, ofp: &OFP) -> usize { + ofp.msg_usize(OfpMsg::FlowMod) + } fn size_of(&self) -> usize { 24 } diff --git a/src/openflow/events/hello.rs b/src/openflow/events/hello.rs index aed816b..d8aed79 100644 --- a/src/openflow/events/hello.rs +++ b/src/openflow/events/hello.rs @@ -1,9 +1,10 @@ -use crate::openflow::{trait_marshal::MessageMarshal, OfpMsg}; +use crate::openflow::messages::{MessageMarshal, OfpMsg}; +use crate::openflow::traiter::OfpMsgEvent; pub struct HelloEvent {} impl HelloEvent { - pub fn new() -> Self { + pub fn new() -> Self { HelloEvent {} } } @@ -11,11 +12,15 @@ impl HelloEvent { impl MessageMarshal for HelloEvent { fn marshal(&self, _: &mut Vec) {} - fn msg_code(&self) -> crate::openflow::OfpMsg { + fn msg_code(&self) -> OfpMsg { OfpMsg::Hello } fn size_of(&self) -> usize { 0 } + + fn msg_usize(&self, ofp: &OFP) -> usize { + ofp.msg_usize(OfpMsg::Hello) + } } diff --git a/src/openflow/message.rs b/src/openflow/message.rs deleted file mode 100644 index 73a1927..0000000 --- a/src/openflow/message.rs +++ /dev/null @@ -1,47 +0,0 @@ -use super::events::{FeaturesReq, HelloEvent}; - -pub trait OfpMsgEvent { - fn hello_event(&self) -> HelloEvent; - fn fetures_req(&self) -> FeaturesReq; - fn version(&self) -> usize; -} - -pub enum OfpMsg { - Hello = 0, - FeaturesReq = 5, - PacketIn = 8, - FlowMod = 14, - NotFound = -1, -} - -pub struct Openflow10 {} - -impl Openflow10 { - pub fn new() -> Self { - Openflow10 {} - } -} - -impl OfpMsgEvent for Openflow10 { - fn hello_event(&self) -> HelloEvent { - HelloEvent::new() - } - - fn fetures_req(&self) -> FeaturesReq { - FeaturesReq::new() - } - - fn version(&self) -> usize { - 1 - } -} - -impl OfpMsg { - pub fn parse(message_code: u8) -> Self { - match message_code { - 0 => OfpMsg::Hello, - 8 => OfpMsg::PacketIn, - _ => OfpMsg::NotFound, - } - } -} diff --git a/src/openflow/messages/mod.rs b/src/openflow/messages/mod.rs new file mode 100644 index 0000000..903a271 --- /dev/null +++ b/src/openflow/messages/mod.rs @@ -0,0 +1,9 @@ +pub mod ofp_message; +pub use ofp_message::OfpMsg; + +pub mod traiter; +pub use traiter::MessageMarshal; +pub use traiter::OfpMsgEvent; + +pub mod ofp_v1_0; +pub use ofp_v1_0::Openflow10; diff --git a/src/openflow/messages/ofp_message.rs b/src/openflow/messages/ofp_message.rs new file mode 100644 index 0000000..4268ad1 --- /dev/null +++ b/src/openflow/messages/ofp_message.rs @@ -0,0 +1,7 @@ +pub enum OfpMsg { + Hello = 0, + FeaturesReq = 5, + PacketIn = 8, + FlowMod = 14, + NotFound = -1, +} diff --git a/src/openflow/messages/ofp_v1_0.rs b/src/openflow/messages/ofp_v1_0.rs new file mode 100644 index 0000000..87269f3 --- /dev/null +++ b/src/openflow/messages/ofp_v1_0.rs @@ -0,0 +1,58 @@ +use crate::openflow::{ + events::{FeaturesReq, HelloEvent}, + OfpHeader, +}; + +use super::traiter::OfpMsgEvent; +use super::OfpMsg; + +pub struct Openflow10 {} + +impl Openflow10 { + pub fn new() -> Self { + Openflow10 {} + } +} + +impl OfpMsgEvent for Openflow10 { + fn hello_event(&self) -> HelloEvent { + HelloEvent::new() + } + + fn fetures_req(&self) -> FeaturesReq { + FeaturesReq::new() + } + + fn version(&self) -> usize { + 1 + } + + fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader { + OfpHeader { + version: 1, + message, + length, + xid, + } + } + + fn msg_parse(&self, msg: u16) -> OfpMsg { + match msg { + 0 => OfpMsg::Hello, + 5 => OfpMsg::FeaturesReq, + 8 => OfpMsg::PacketIn, + 14 => OfpMsg::FlowMod, + _ => OfpMsg::NotFound, + } + } + + fn msg_usize(&self, msg: OfpMsg) -> usize { + match msg { + OfpMsg::Hello => 0, + OfpMsg::FeaturesReq => 5, + OfpMsg::PacketIn => 8, + OfpMsg::FlowMod => 14, + _ => 1024, + } + } +} diff --git a/src/openflow/messages/traiter.rs b/src/openflow/messages/traiter.rs new file mode 100644 index 0000000..a64d840 --- /dev/null +++ b/src/openflow/messages/traiter.rs @@ -0,0 +1,29 @@ +use crate::openflow::{ + events::{FeaturesReq, HelloEvent}, + OfpHeader, +}; + +use super::OfpMsg; + +/** + * the trait for parse value to bytes. + * for use with Controller's send_msg. + */ +pub trait MessageMarshal { + fn marshal(&self, bytes: &mut Vec); + fn msg_code(&self) -> OfpMsg; + fn msg_usize(&self, ofp: &OFP) -> usize; + fn size_of(&self) -> usize; +} + +/** + * for works with controller to create OfpMsgEvent + */ +pub trait OfpMsgEvent { + fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader; + fn hello_event(&self) -> HelloEvent; + fn fetures_req(&self) -> FeaturesReq; + fn version(&self) -> usize; + fn msg_parse(&self, msg: u16) -> OfpMsg; + fn msg_usize(&self, msg: OfpMsg) -> usize; +} diff --git a/src/openflow/mod.rs b/src/openflow/mod.rs index 3d0d3eb..775448b 100644 --- a/src/openflow/mod.rs +++ b/src/openflow/mod.rs @@ -1,9 +1,6 @@ pub mod header; pub use header::OfpHeader; -pub mod message; -pub use message::OfpMsg; - pub mod controller; pub use controller::Controller; @@ -12,4 +9,5 @@ pub mod events; pub mod ofp_port; pub use ofp_port::{OfpPort, PseudoPort}; -pub mod trait_marshal; \ No newline at end of file +pub mod messages; +pub use messages::{ofp_v1_0, traiter}; diff --git a/src/openflow/trait_marshal.rs b/src/openflow/trait_marshal.rs deleted file mode 100644 index 0ded602..0000000 --- a/src/openflow/trait_marshal.rs +++ /dev/null @@ -1,11 +0,0 @@ -use super::OfpMsg; - -/** - * the trait for parse value to bytes. - * for use with Controller's send_msg. - */ -pub trait MessageMarshal { - fn marshal(&self, bytes: &mut Vec); - fn msg_code(&self) -> OfpMsg; - fn size_of(&self) -> usize; -}