Skip to content

Commit

Permalink
refactor and plan for others message
Browse files Browse the repository at this point in the history
  • Loading branch information
Arikato111 committed May 5, 2024
1 parent 2a1ef30 commit f8cf010
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 80 deletions.
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions src/openflow/controller.rs
Original file line number Diff line number Diff line change
@@ -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<OME: OfpMsgEvent> {
ofp: OME,
/*
* pub is temporary, remove soon;
* for test in main func
*/
pub ofp: OME,
mac_to_port: HashMap<u64, u16>,
}

Expand All @@ -22,12 +27,9 @@ impl<OME: OfpMsgEvent> Controller<OME> {
let mut header_bytes: Vec<u8> = Vec::new();
let mut body_bytes: Vec<u8> = 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);
Expand Down
10 changes: 7 additions & 3 deletions src/openflow/events/features_req.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::openflow::trait_marshal::MessageMarshal;
use crate::openflow::messages::{MessageMarshal, OfpMsg, OfpMsgEvent};

pub struct FeaturesReq {}

Expand All @@ -11,11 +11,15 @@ impl FeaturesReq {
impl MessageMarshal for FeaturesReq {
fn marshal(&self, _: &mut Vec<u8>) {}

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<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp.msg_usize(OfpMsg::FeaturesReq)
}
}
8 changes: 7 additions & 1 deletion src/openflow/events/flow_mod/flow_mod_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -89,6 +92,9 @@ impl FlowModEvent {
}

impl MessageMarshal for FlowModEvent {
fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp.msg_usize(OfpMsg::FlowMod)
}
fn size_of(&self) -> usize {
24
}
Expand Down
11 changes: 8 additions & 3 deletions src/openflow/events/hello.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
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 {}
}
}

impl MessageMarshal for HelloEvent {
fn marshal(&self, _: &mut Vec<u8>) {}

fn msg_code(&self) -> crate::openflow::OfpMsg {
fn msg_code(&self) -> OfpMsg {
OfpMsg::Hello
}

fn size_of(&self) -> usize {
0
}

fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp.msg_usize(OfpMsg::Hello)
}
}
47 changes: 0 additions & 47 deletions src/openflow/message.rs

This file was deleted.

9 changes: 9 additions & 0 deletions src/openflow/messages/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions src/openflow/messages/ofp_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub enum OfpMsg {
Hello = 0,
FeaturesReq = 5,
PacketIn = 8,
FlowMod = 14,
NotFound = -1,
}
58 changes: 58 additions & 0 deletions src/openflow/messages/ofp_v1_0.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
}
29 changes: 29 additions & 0 deletions src/openflow/messages/traiter.rs
Original file line number Diff line number Diff line change
@@ -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<u8>);
fn msg_code(&self) -> OfpMsg;
fn msg_usize<OFP: OfpMsgEvent>(&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;
}
6 changes: 2 additions & 4 deletions src/openflow/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,4 +9,5 @@ pub mod events;
pub mod ofp_port;
pub use ofp_port::{OfpPort, PseudoPort};

pub mod trait_marshal;
pub mod messages;
pub use messages::{ofp_v1_0, traiter};
11 changes: 0 additions & 11 deletions src/openflow/trait_marshal.rs

This file was deleted.

0 comments on commit f8cf010

Please sign in to comment.