Commit 35d61ff6 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

rewrite header to split version to support others version in future, and create…

rewrite header to split version to support others version in future, and create header struct to be interface for using
parent 3a1c73b2
...@@ -21,17 +21,25 @@ pub trait ControllerFrame<OME: OfpMsgEvent> { ...@@ -21,17 +21,25 @@ pub trait ControllerFrame<OME: OfpMsgEvent> {
tcp_listener_handler::<OME>(address, ofp.version() as u8); tcp_listener_handler::<OME>(address, ofp.version() as u8);
} }
fn handle_header(&mut self, buf: &mut Vec<u8>) -> (u8, usize, u32) {
let ofp_header = self.get_ofp().header_parse(&buf);
(
ofp_header.message(),
ofp_header.pkt_size(),
ofp_header.xid(),
)
}
fn request_handler(&mut self, buf: &mut Vec<u8>, stream: &mut TcpStream) { fn request_handler(&mut self, buf: &mut Vec<u8>, stream: &mut TcpStream) {
let ofp = self.get_ofp(); let (message, pkt_size, xid) = self.handle_header(buf);
let ofp_header = ofp.header_parse(&buf); let mut payload = vec![0u8; pkt_size];
let mut payload = vec![0u8; ofp_header.pkt_size()];
let _ = stream.read(&mut payload); let _ = stream.read(&mut payload);
let message = ofp.msg_parse(ofp_header.message as u16); let message = self.get_ofp().msg_parse(message as u16);
match message { match message {
OfpMsg::Hello => self.send_msg(ofp.fetures_req(), ofp_header.xid, stream), OfpMsg::Hello => self.send_msg(self.get_ofp().fetures_req(), xid, stream),
OfpMsg::FeaturesReq => todo!(), OfpMsg::FeaturesReq => todo!(),
OfpMsg::PacketIn => { OfpMsg::PacketIn => {
self.packet_in_handler(ofp_header.xid, PacketInEvent::parse(&payload), stream); self.packet_in_handler(xid, PacketInEvent::parse(&payload), stream);
} }
OfpMsg::FlowMod => todo!(), OfpMsg::FlowMod => todo!(),
OfpMsg::NotFound => todo!(), OfpMsg::NotFound => todo!(),
......
pub mod header; pub mod ofp_header;
pub use header::OfpHeader; pub use ofp_header::OfpHeader;
pub mod controller_frame; pub mod controller_frame;
......
pub trait OpenflowHeader {
fn version(&self) -> usize;
fn message(&self) -> u8;
fn length(&self) -> usize;
fn xid(&self) -> u32;
fn pkt_size(&self) -> usize;
fn new(message: u8, length: usize, xid: usize) -> Self;
fn header_size(&self) -> usize;
fn parse(buf: &Vec<u8>) -> Self;
fn marshal(&self, bytes: &mut Vec<u8>);
}
pub mod header_traiter;
pub use header_traiter::OpenflowHeader;
pub mod ofp_header;
pub use ofp_header::OfpHeader;
pub mod ofp_header_v1_0;
pub use ofp_header_v1_0::OfpHeader10;
use super::OpenflowHeader;
pub struct OfpHeader<OFH: OpenflowHeader> {
ofp_header: OFH,
}
impl<OFH: OpenflowHeader> OfpHeader<OFH> {
pub fn new(ofp_header: OFH) -> Self {
Self { ofp_header }
}
pub fn version(&self) -> usize {
self.ofp_header.version()
}
pub fn message(&self) -> u8 {
self.ofp_header.message()
}
pub fn length(&self) -> usize {
self.ofp_header.length()
}
pub fn xid(&self) -> u32 {
self.ofp_header.xid()
}
pub fn header_size(&self) -> usize {
self.ofp_header.header_size()
}
pub fn pkt_size(&self) -> usize {
self.ofp_header.pkt_size()
}
pub fn marshal(&self, bytes: &mut Vec<u8>) {
self.ofp_header.marshal(bytes);
}
}
use std::{io::Cursor, mem::size_of}; use super::OpenflowHeader;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use std::{io::Cursor, mem::size_of};
pub struct OfpHeader { pub struct OfpHeader10 {
pub version: u8, pub version: u8,
pub message: u8, pub message: u8,
pub length: u16, pub length: u16,
pub xid: u32, pub xid: u32,
} }
impl OfpHeader { impl OpenflowHeader for OfpHeader10 {
pub fn header_size(&self) -> usize { fn new(message: u8, length: usize, xid: usize) -> Self {
size_of::<OfpHeader>() Self {
} version: 1,
pub fn pkt_size(&self) -> usize { message,
return self.length as usize - size_of::<OfpHeader>(); length: (size_of::<OfpHeader10>() + length) as u16,
} xid: xid as u32,
pub fn new(version: u8, message: u8, length: u16, xid: u32) -> Self { }
Self { }
version, fn version(&self) -> usize {
message, 1
length: length + 8, }
xid, fn message(&self) -> u8 {
} self.message
} }
pub fn parse(buf: &Vec<u8>) -> Self { fn length(&self) -> usize {
let mut buf_cursor = Cursor::new(buf); self.length as usize
// split data from header }
let version = buf_cursor.read_u8().unwrap(); fn xid(&self) -> u32 {
let message = buf_cursor.read_u8().unwrap(); self.xid
// size is size of packet }
let length = buf_cursor.read_u16::<BigEndian>().unwrap(); fn header_size(&self) -> usize {
let xid = buf_cursor.read_u32::<BigEndian>().unwrap(); size_of::<OfpHeader10>()
Self { }
version, fn pkt_size(&self) -> usize {
message, return self.length as usize - size_of::<OfpHeader10>();
length, }
xid,
} fn parse(buf: &Vec<u8>) -> Self {
} let mut buf_cursor = Cursor::new(buf);
pub fn marshal(&self, bytes: &mut Vec<u8>) { let version = buf_cursor.read_u8().unwrap();
bytes.write_u8(self.version).unwrap(); let message = buf_cursor.read_u8().unwrap();
bytes.write_u8(self.message).unwrap(); let length = buf_cursor.read_u16::<BigEndian>().unwrap();
bytes.write_u16::<BigEndian>(self.length).unwrap(); let xid = buf_cursor.read_u32::<BigEndian>().unwrap();
bytes.write_u32::<BigEndian>(self.xid).unwrap(); Self {
} version,
} message,
length,
xid,
}
}
fn marshal(&self, bytes: &mut Vec<u8>) {
bytes.write_u8(self.version).unwrap();
bytes.write_u8(self.message).unwrap();
bytes.write_u16::<BigEndian>(self.length).unwrap();
bytes.write_u32::<BigEndian>(self.xid).unwrap();
}
}
use crate::openflow::{ use crate::openflow::{
events::{FeaturesReq, HelloEvent}, events::{FeaturesReq, HelloEvent},
ofp_header::{OfpHeader10, OpenflowHeader},
OfpHeader, OfpHeader,
}; };
...@@ -15,8 +16,8 @@ impl Openflow10 { ...@@ -15,8 +16,8 @@ impl Openflow10 {
} }
impl OfpMsgEvent for Openflow10 { impl OfpMsgEvent for Openflow10 {
fn header_parse(&self, bytes:&Vec<u8>) -> OfpHeader { fn header_parse(&self, bytes: &Vec<u8>) -> OfpHeader<impl OpenflowHeader> {
OfpHeader::parse(bytes) OfpHeader::new(OfpHeader10::parse(bytes))
} }
fn header_size(&self) -> usize { fn header_size(&self) -> usize {
8 8
...@@ -35,13 +36,8 @@ impl OfpMsgEvent for Openflow10 { ...@@ -35,13 +36,8 @@ impl OfpMsgEvent for Openflow10 {
1 1
} }
fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader { fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader<impl OpenflowHeader> {
OfpHeader { OfpHeader::new(OfpHeader10::new(message, length as usize, xid as usize))
version: 1,
message,
length,
xid,
}
} }
fn msg_parse(&self, msg: u16) -> OfpMsg { fn msg_parse(&self, msg: u16) -> OfpMsg {
......
use crate::openflow::{ use crate::openflow::{
events::{FeaturesReq, HelloEvent}, events::{FeaturesReq, HelloEvent},
ofp_header::OpenflowHeader,
OfpHeader, OfpHeader,
}; };
...@@ -20,11 +21,11 @@ pub trait MessageMarshal { ...@@ -20,11 +21,11 @@ pub trait MessageMarshal {
* for works with controller to create OfpMsgEvent * for works with controller to create OfpMsgEvent
*/ */
pub trait OfpMsgEvent { pub trait OfpMsgEvent {
fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader; fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader<impl OpenflowHeader>;
fn header_parse(&self, bytes: &Vec<u8>) -> OfpHeader<impl OpenflowHeader>;
fn version(&self) -> usize; fn version(&self) -> usize;
fn ofp_version() -> usize; fn ofp_version() -> usize;
fn header_size(&self) -> usize; fn header_size(&self) -> usize;
fn header_parse(&self, bytes:&Vec<u8>) -> OfpHeader;
fn msg_usize(&self, msg: OfpMsg) -> usize; fn msg_usize(&self, msg: OfpMsg) -> usize;
fn msg_parse(&self, msg: u16) -> OfpMsg; fn msg_parse(&self, msg: u16) -> OfpMsg;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment