Commit 349affa4 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

rewrite structure

parent 2c52955e
#![allow(unused)] #![allow(unused)]
#![allow(unused_variables)] #![allow(unused_variables)]
use crate::etherparser::ether_type::EtherType; use std::{collections::HashMap, net::TcpStream};
use crate::openflow::events::flow_mod::{FlowModCommand, MatchFields};
use crate::{etherparser::ether_type::EtherType, openflow::{controller_frame::ControllerFrame, ofp10::{self, events::{flow_mod::MatchFields, Action}, traiter::OfpMsgEvent, FlowModEvent, PacketInEvent}}};
/** /**
* Here is Controller you can modify and write the process or more you need. * Here is Controller you can modify and write the process or more you need.
* In production please remove allow unused. * In production please remove allow unused.
*/ */
use crate::openflow::events::{FlowAction, FlowModEvent, PacketInEvent};
use crate::openflow::PseudoPort;
use crate::openflow::{controller_frame::ControllerFrame, traiter::OfpMsgEvent};
use std::{collections::HashMap, net::TcpStream};
pub struct Controller<OME: OfpMsgEvent> { pub struct Controller<OME: OfpMsgEvent> {
ofp: OME, ofp: OME,
...@@ -41,13 +38,13 @@ impl<OME: OfpMsgEvent> ControllerFrame<OME> for Controller<OME> { ...@@ -41,13 +38,13 @@ impl<OME: OfpMsgEvent> ControllerFrame<OME> for Controller<OME> {
} }
let out_port = match self.mac_to_port.get(&mac_dst) { let out_port = match self.mac_to_port.get(&mac_dst) {
Some(p) => PseudoPort::PhysicalPort(*p), Some(p) => ofp10::PseudoPort::PhysicalPort(*p),
None => PseudoPort::Flood, None => ofp10::PseudoPort::Flood,
}; };
let actions = vec![FlowAction::Oputput(out_port.clone())]; let actions = vec![Action::Oputput(out_port.clone())];
if let PseudoPort::PhysicalPort(_) = out_port { if let ofp10::PseudoPort::PhysicalPort(_) = out_port {
let mut match_fields = MatchFields::match_all(); let mut match_fields = MatchFields::match_all();
match_fields.in_port = Some(packetin.in_port); match_fields.in_port = Some(packetin.in_port);
match_fields.mac_dest = Some(mac_dst); match_fields.mac_dest = Some(mac_dst);
...@@ -72,7 +69,7 @@ impl<OME: OfpMsgEvent> Controller<OME> { ...@@ -72,7 +69,7 @@ impl<OME: OfpMsgEvent> Controller<OME> {
xid: u32, xid: u32,
priority: u16, priority: u16,
flow: MatchFields, flow: MatchFields,
actions: &Vec<FlowAction>, actions: &Vec<Action>,
buffer_id: Option<u32>, buffer_id: Option<u32>,
stream: &mut TcpStream, stream: &mut TcpStream,
) { ) {
......
use tenjin::{ use tenjin::{
openflow::{controller_frame::ControllerFrame, ofp_v1_0::Openflow10}, openflow::{controller_frame::ControllerFrame, ofp10::ofp_v1_0::Openflow10},
Controller, Controller,
}; };
......
...@@ -2,15 +2,9 @@ use std::{ ...@@ -2,15 +2,9 @@ use std::{
io::{Read, Write}, io::{Read, Write},
net::TcpStream, net::TcpStream,
}; };
use crate::openflow::ofp10::{Msg, PacketInEvent, traiter::{MessageMarshal, OfpMsgEvent}};
use super::{ use super::tcp_listener::tcp_listener_handler;
events::PacketInEvent,
ofp_manager::{
traiter::{MessageMarshal, OfpMsgEvent},
OfpMsg,
},
tcp_listener_handler,
};
pub trait ControllerFrame<OME: OfpMsgEvent> { pub trait ControllerFrame<OME: OfpMsgEvent> {
fn get_ofp(&self) -> &impl OfpMsgEvent; fn get_ofp(&self) -> &impl OfpMsgEvent;
...@@ -36,14 +30,14 @@ pub trait ControllerFrame<OME: OfpMsgEvent> { ...@@ -36,14 +30,14 @@ pub trait ControllerFrame<OME: OfpMsgEvent> {
let _ = stream.read(&mut payload); let _ = stream.read(&mut payload);
let message = self.get_ofp().msg_parse(message as u16); let message = self.get_ofp().msg_parse(message as u16);
match message { match message {
OfpMsg::Hello => self.send_msg(self.get_ofp().fetures_req(), xid, stream), Msg::Hello => self.send_msg(self.get_ofp().fetures_req(), xid, stream),
OfpMsg::FeaturesReq => (), Msg::FeaturesReq => (),
OfpMsg::PacketIn => { Msg::PacketIn => {
self.packet_in_handler(xid, PacketInEvent::parse(&payload), stream); self.packet_in_handler(xid, PacketInEvent::parse(&payload), stream);
} }
OfpMsg::PacketOut => (), Msg::PacketOut => (),
OfpMsg::FlowMod => (), Msg::FlowMod => (),
OfpMsg::NotFound => (), Msg::NotFound => (),
} }
} }
......
pub mod ofp_header;
pub use ofp_header::OfpHeader;
pub mod controller_frame; pub mod controller_frame;
pub mod events; pub mod ofp10;
pub mod ofp_port;
pub use ofp_port::{OfpPort, PseudoPort};
pub mod ofp_manager;
pub use ofp_manager::{ofp_v1_0, traiter};
pub mod tcp_listener; pub mod tcp_listener;
pub use tcp_listener::tcp_listener_handler;
\ No newline at end of file pub mod macro_selector;
\ No newline at end of file
...@@ -7,12 +7,25 @@ use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; ...@@ -7,12 +7,25 @@ use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use crate::{ use crate::{
etherparser::tools::bits::{bytes_to_mac, mac_to_bytes}, etherparser::tools::bits::{bytes_to_mac, mac_to_bytes},
openflow::PseudoPort, openflow::ofp10::PseudoPort,
}; };
use super::flow_actions_type::FlowActionType; pub enum ActionType {
Output = 0,
SetVlanId = 1,
SetVlanPCP = 2,
StripVlan = 3,
SetSrcMac = 4,
SetDstMac = 5,
SetIPv4Src = 6,
SetIPv4Des = 7,
SetTos = 8,
SetTpSrc = 9,
SetTpDst = 10,
Enqueue = 11,
}
pub enum FlowAction { pub enum Action {
Oputput(PseudoPort), Oputput(PseudoPort),
SetDlVlan(Option<u16>), SetDlVlan(Option<u16>),
SetDlVlanPcp(u8), SetDlVlanPcp(u8),
...@@ -27,38 +40,38 @@ pub enum FlowAction { ...@@ -27,38 +40,38 @@ pub enum FlowAction {
Unparsable, Unparsable,
} }
impl FlowAction { impl Action {
pub fn to_action_code(&self) -> FlowActionType { pub fn to_action_code(&self) -> ActionType {
match self { match self {
FlowAction::Oputput(_) => FlowActionType::Output, Action::Oputput(_) => ActionType::Output,
FlowAction::SetDlVlan(_) => FlowActionType::SetVlanId, Action::SetDlVlan(_) => ActionType::SetVlanId,
FlowAction::SetDlVlanPcp(_) => FlowActionType::SetVlanPCP, Action::SetDlVlanPcp(_) => ActionType::SetVlanPCP,
FlowAction::SetDlSrc(_) => FlowActionType::SetSrcMac, Action::SetDlSrc(_) => ActionType::SetSrcMac,
FlowAction::SetDlDest(_) => FlowActionType::SetDstMac, Action::SetDlDest(_) => ActionType::SetDstMac,
FlowAction::SetIpSrc(_) => FlowActionType::SetIPv4Src, Action::SetIpSrc(_) => ActionType::SetIPv4Src,
FlowAction::SetIpDes(_) => FlowActionType::SetIPv4Des, Action::SetIpDes(_) => ActionType::SetIPv4Des,
FlowAction::SetTos(_) => FlowActionType::SetTos, Action::SetTos(_) => ActionType::SetTos,
FlowAction::SetTpSrc(_) => FlowActionType::SetTpSrc, Action::SetTpSrc(_) => ActionType::SetTpSrc,
FlowAction::SetTpDest(_) => FlowActionType::SetTpDst, Action::SetTpDest(_) => ActionType::SetTpDst,
FlowAction::Enqueue(_, _) => FlowActionType::Enqueue, Action::Enqueue(_, _) => ActionType::Enqueue,
FlowAction::Unparsable => panic!("Unparse FlowAction to FlowActionType"), Action::Unparsable => panic!("Unparse Action to ActionType"),
} }
} }
pub fn length(&self) -> usize { pub fn length(&self) -> usize {
let header = size_of::<(u16, u16)>(); let header = size_of::<(u16, u16)>();
let body = match self { let body = match self {
FlowAction::Oputput(_) => size_of::<(u16, u16)>(), Action::Oputput(_) => size_of::<(u16, u16)>(),
FlowAction::SetDlVlan(_) => size_of::<(u16, u16)>(), Action::SetDlVlan(_) => size_of::<(u16, u16)>(),
FlowAction::SetDlVlanPcp(_) => size_of::<(u8, [u8; 3])>(), Action::SetDlVlanPcp(_) => size_of::<(u8, [u8; 3])>(),
FlowAction::SetDlSrc(_) => size_of::<([u8; 6], [u8; 6])>(), Action::SetDlSrc(_) => size_of::<([u8; 6], [u8; 6])>(),
FlowAction::SetDlDest(_) => size_of::<([u8; 6], [u8; 6])>(), Action::SetDlDest(_) => size_of::<([u8; 6], [u8; 6])>(),
FlowAction::SetIpSrc(_) => size_of::<u32>(), Action::SetIpSrc(_) => size_of::<u32>(),
FlowAction::SetIpDes(_) => size_of::<u32>(), Action::SetIpDes(_) => size_of::<u32>(),
FlowAction::SetTos(_) => size_of::<(u8, [u8; 3])>(), Action::SetTos(_) => size_of::<(u8, [u8; 3])>(),
FlowAction::SetTpSrc(_) => size_of::<(u16, u16)>(), Action::SetTpSrc(_) => size_of::<(u16, u16)>(),
FlowAction::SetTpDest(_) => size_of::<(u16, u16)>(), Action::SetTpDest(_) => size_of::<(u16, u16)>(),
FlowAction::Enqueue(_, _) => size_of::<(u16, [u8; 6], u32)>(), Action::Enqueue(_, _) => size_of::<(u16, [u8; 6], u32)>(),
FlowAction::Unparsable => 0, Action::Unparsable => 0,
}; };
header + body header + body
} }
...@@ -67,27 +80,27 @@ impl FlowAction { ...@@ -67,27 +80,27 @@ impl FlowAction {
let _ = bytes.write_u16::<BigEndian>(self.to_action_code() as u16); let _ = bytes.write_u16::<BigEndian>(self.to_action_code() as u16);
let _ = bytes.write_u16::<BigEndian>(self.length() as u16); let _ = bytes.write_u16::<BigEndian>(self.length() as u16);
match self { match self {
FlowAction::Oputput(pseudo) => { Action::Oputput(pseudo) => {
pseudo.marshal(bytes); pseudo.marshal(bytes);
let _ = bytes.write_u16::<BigEndian>(match pseudo { let _ = bytes.write_u16::<BigEndian>(match pseudo {
PseudoPort::Controller(w) => *w as u16, PseudoPort::Controller(w) => *w as u16,
_ => 0, _ => 0,
}); });
} }
FlowAction::SetDlVlan(None) => { Action::SetDlVlan(None) => {
let _ = bytes.write_u32::<BigEndian>(0xffff); let _ = bytes.write_u32::<BigEndian>(0xffff);
} }
FlowAction::SetDlVlan(Some(vid)) => { Action::SetDlVlan(Some(vid)) => {
let _ = bytes.write_u16::<BigEndian>(*vid); let _ = bytes.write_u16::<BigEndian>(*vid);
let _ = bytes.write_u16::<BigEndian>(0); let _ = bytes.write_u16::<BigEndian>(0);
} }
FlowAction::SetDlVlanPcp(pcp) => { Action::SetDlVlanPcp(pcp) => {
let _ = bytes.write_u8(*pcp); let _ = bytes.write_u8(*pcp);
for _ in 0..3 { for _ in 0..3 {
let _ = bytes.write_u8(0); let _ = bytes.write_u8(0);
} }
} }
FlowAction::SetDlSrc(mac) | FlowAction::SetDlDest(mac) => { Action::SetDlSrc(mac) | Action::SetDlDest(mac) => {
let mac = bytes_to_mac(*mac); let mac = bytes_to_mac(*mac);
for m in mac { for m in mac {
let _ = bytes.write_u8(m); let _ = bytes.write_u8(m);
...@@ -96,128 +109,128 @@ impl FlowAction { ...@@ -96,128 +109,128 @@ impl FlowAction {
let _ = bytes.write_u8(0); let _ = bytes.write_u8(0);
} }
} }
FlowAction::SetIpSrc(address) | FlowAction::SetIpDes(address) => { Action::SetIpSrc(address) | Action::SetIpDes(address) => {
let _ = bytes.write_u32::<BigEndian>(*address); let _ = bytes.write_u32::<BigEndian>(*address);
} }
FlowAction::SetTos(n) => { Action::SetTos(n) => {
let _ = bytes.write_u8(*n); let _ = bytes.write_u8(*n);
} }
FlowAction::SetTpSrc(pt) | FlowAction::SetTpDest(pt) => { Action::SetTpSrc(pt) | Action::SetTpDest(pt) => {
let _ = bytes.write_u16::<BigEndian>(*pt); let _ = bytes.write_u16::<BigEndian>(*pt);
let _ = bytes.write_u16::<BigEndian>(0); let _ = bytes.write_u16::<BigEndian>(0);
} }
FlowAction::Enqueue(pp, qid) => { Action::Enqueue(pp, qid) => {
pp.marshal(bytes); pp.marshal(bytes);
for _ in 0..6 { for _ in 0..6 {
let _ = bytes.write_u8(0); let _ = bytes.write_u8(0);
} }
let _ = bytes.write_u32::<BigEndian>(*qid); let _ = bytes.write_u32::<BigEndian>(*qid);
} }
FlowAction::Unparsable => (), Action::Unparsable => (),
} }
} }
pub fn parse_sequence(bytes: &mut Cursor<Vec<u8>>) -> Vec<FlowAction> { pub fn parse_sequence(bytes: &mut Cursor<Vec<u8>>) -> Vec<Action> {
if bytes.get_ref().is_empty() { if bytes.get_ref().is_empty() {
vec![] vec![]
} else { } else {
let action = FlowAction::parse(bytes); let action = Action::parse(bytes);
let mut v = vec![action]; let mut v = vec![action];
v.append(&mut FlowAction::parse_sequence(bytes)); v.append(&mut Action::parse_sequence(bytes));
v v
} }
} }
pub fn parse(bytes: &mut Cursor<Vec<u8>>) -> FlowAction { pub fn parse(bytes: &mut Cursor<Vec<u8>>) -> Action {
let action_code = bytes.read_u16::<BigEndian>().unwrap(); let action_code = bytes.read_u16::<BigEndian>().unwrap();
let _ = bytes.read_u16::<BigEndian>().unwrap(); let _ = bytes.read_u16::<BigEndian>().unwrap();
match action_code { match action_code {
t if t == (FlowActionType::Output as u16) => { t if t == (ActionType::Output as u16) => {
let port_code = bytes.read_u16::<BigEndian>().unwrap(); let port_code = bytes.read_u16::<BigEndian>().unwrap();
let len = bytes.read_u16::<BigEndian>().unwrap(); let len = bytes.read_u16::<BigEndian>().unwrap();
FlowAction::Oputput(PseudoPort::new(port_code, Some(len as u64))) Action::Oputput(PseudoPort::new(port_code, Some(len as u64)))
} }
t if t == (FlowActionType::SetVlanId as u16) => { t if t == (ActionType::SetVlanId as u16) => {
let vid = bytes.read_u16::<BigEndian>().unwrap(); let vid = bytes.read_u16::<BigEndian>().unwrap();
bytes.consume(2); bytes.consume(2);
if vid == 0xffff { if vid == 0xffff {
FlowAction::SetDlVlan(None) Action::SetDlVlan(None)
} else { } else {
FlowAction::SetDlVlan(Some(vid)) Action::SetDlVlan(Some(vid))
} }
} }
t if t == (FlowActionType::SetVlanPCP as u16) => { t if t == (ActionType::SetVlanPCP as u16) => {
let pcp = bytes.read_u8().unwrap(); let pcp = bytes.read_u8().unwrap();
bytes.consume(3); bytes.consume(3);
FlowAction::SetDlVlanPcp(pcp) Action::SetDlVlanPcp(pcp)
} }
t if t == (FlowActionType::StripVlan as u16) => { t if t == (ActionType::StripVlan as u16) => {
bytes.consume(4); bytes.consume(4);
FlowAction::SetDlVlan(None) Action::SetDlVlan(None)
} }
t if t == (FlowActionType::SetSrcMac as u16) => { t if t == (ActionType::SetSrcMac as u16) => {
let mut addr = [0u8; 6]; let mut addr = [0u8; 6];
for i in 0..6 { for i in 0..6 {
addr[i] = bytes.read_u8().unwrap(); addr[i] = bytes.read_u8().unwrap();
} }
bytes.consume(6); bytes.consume(6);
FlowAction::SetDlSrc(mac_to_bytes(addr)) Action::SetDlSrc(mac_to_bytes(addr))
} }
t if t == (FlowActionType::SetDstMac as u16) => { t if t == (ActionType::SetDstMac as u16) => {
let mut addr = [0u8; 6]; let mut addr = [0u8; 6];
for i in 0..6 { for i in 0..6 {
addr[i] = bytes.read_u8().unwrap(); addr[i] = bytes.read_u8().unwrap();
} }
bytes.consume(6); bytes.consume(6);
FlowAction::SetDlDest(mac_to_bytes(addr)) Action::SetDlDest(mac_to_bytes(addr))
} }
t if t == (FlowActionType::SetIPv4Src as u16) => { t if t == (ActionType::SetIPv4Src as u16) => {
FlowAction::SetIpSrc(bytes.read_u32::<BigEndian>().unwrap()) Action::SetIpSrc(bytes.read_u32::<BigEndian>().unwrap())
} }
t if t == (FlowActionType::SetIPv4Des as u16) => { t if t == (ActionType::SetIPv4Des as u16) => {
FlowAction::SetIpDes(bytes.read_u32::<BigEndian>().unwrap()) Action::SetIpDes(bytes.read_u32::<BigEndian>().unwrap())
} }
t if t == (FlowActionType::SetTos as u16) => { t if t == (ActionType::SetTos as u16) => {
let tos = bytes.read_u8().unwrap(); let tos = bytes.read_u8().unwrap();
bytes.consume(3); bytes.consume(3);
FlowAction::SetTos(tos) Action::SetTos(tos)
} }
t if t == (FlowActionType::SetTpSrc as u16) => { t if t == (ActionType::SetTpSrc as u16) => {
let pt = bytes.read_u16::<BigEndian>().unwrap(); let pt = bytes.read_u16::<BigEndian>().unwrap();
bytes.consume(2); bytes.consume(2);
FlowAction::SetTpSrc(pt) Action::SetTpSrc(pt)
} }
t if t == (FlowActionType::SetTpDst as u16) => { t if t == (ActionType::SetTpDst as u16) => {
let pt = bytes.read_u16::<BigEndian>().unwrap(); let pt = bytes.read_u16::<BigEndian>().unwrap();
bytes.consume(2); bytes.consume(2);
FlowAction::SetTpDest(pt) Action::SetTpDest(pt)
} }
t if t == (FlowActionType::Enqueue as u16) => { t if t == (ActionType::Enqueue as u16) => {
let pt = bytes.read_u16::<BigEndian>().unwrap(); let pt = bytes.read_u16::<BigEndian>().unwrap();
bytes.consume(6); bytes.consume(6);
let qid = bytes.read_u32::<BigEndian>().unwrap(); let qid = bytes.read_u32::<BigEndian>().unwrap();
FlowAction::Enqueue(PseudoPort::new(pt, Some(0)), qid) Action::Enqueue(PseudoPort::new(pt, Some(0)), qid)
} }
_ => FlowAction::Unparsable, _ => Action::Unparsable,
} }
} }
} }
pub trait SizeCheck { pub trait SizeCheck {
fn size_of_sequence(&self) -> usize; fn size_of_sequence(&self) -> usize;
fn move_controller_last(&self) -> Vec<FlowAction>; fn move_controller_last(&self) -> Vec<Action>;
} }
impl SizeCheck for Vec<FlowAction> { impl SizeCheck for Vec<Action> {
fn size_of_sequence(&self) -> usize { fn size_of_sequence(&self) -> usize {
self.iter().fold(0, |acc, x| x.length() + acc) self.iter().fold(0, |acc, x| x.length() + acc)
} }
fn move_controller_last(&self) -> Vec<FlowAction> { fn move_controller_last(&self) -> Vec<Action> {
let mut not_ctrl: Vec<FlowAction> = Vec::new(); let mut not_ctrl: Vec<Action> = Vec::new();
let mut is_ctrl: Vec<FlowAction> = Vec::new(); let mut is_ctrl: Vec<Action> = Vec::new();
for act in self { for act in self {
match act { match act {
FlowAction::Oputput(PseudoPort::Controller(_)) => { Action::Oputput(PseudoPort::Controller(_)) => {
is_ctrl.push(act.clone()); is_ctrl.push(act.clone());
} }
_ => not_ctrl.push(act.clone()), _ => not_ctrl.push(act.clone()),
...@@ -228,7 +241,7 @@ impl SizeCheck for Vec<FlowAction> { ...@@ -228,7 +241,7 @@ impl SizeCheck for Vec<FlowAction> {
} }
} }
impl Clone for FlowAction { impl Clone for Action {
fn clone(&self) -> Self { fn clone(&self) -> Self {
match self { match self {
Self::Oputput(v) => Self::Oputput(v.clone()), Self::Oputput(v) => Self::Oputput(v.clone()),
......
use crate::openflow::ofp_manager::{MessageMarshal, OfpMsg, OfpMsgEvent}; use crate::openflow::ofp10::{traiter::{MessageMarshal, OfpMsgEvent}, Msg};
pub struct FeaturesReq {}
impl FeaturesReq { pub struct FeaturesReqEvent {}
impl FeaturesReqEvent {
pub fn new() -> Self { pub fn new() -> Self {
FeaturesReq {} FeaturesReqEvent {}
} }
} }
impl MessageMarshal for FeaturesReq { impl MessageMarshal for FeaturesReqEvent {
fn marshal(&self, _: &mut Vec<u8>) {} fn marshal(&self, _: &mut Vec<u8>) {}
fn msg_code(&self) -> OfpMsg { fn msg_code(&self) -> Msg {
OfpMsg::FeaturesReq Msg::FeaturesReq
} }
fn size_of(&self) -> usize { fn size_of(&self) -> usize {
...@@ -20,6 +21,6 @@ impl MessageMarshal for FeaturesReq { ...@@ -20,6 +21,6 @@ impl MessageMarshal for FeaturesReq {
} }
fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize { fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp.msg_usize(OfpMsg::FeaturesReq) ofp.msg_usize(Msg::FeaturesReq)
} }
} }
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* it don't need to assign vlue. * it don't need to assign vlue.
* but I think I should assign for reading. * but I think I should assign for reading.
*/ */
pub enum FlowActionType { pub enum ActionType {
Output = 0, Output = 0,
SetVlanId = 1, SetVlanId = 1,
SetVlanPCP = 2, SetVlanPCP = 2,
......
...@@ -2,12 +2,10 @@ use std::io::Cursor; ...@@ -2,12 +2,10 @@ use std::io::Cursor;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use crate::openflow::{ use crate::openflow::ofp10::{events::{actions::SizeCheck, Action}, ofp_port::OfpPort, traiter::{MessageMarshal, OfpMsgEvent}, Msg, PseudoPort};
ofp_manager::{MessageMarshal, OfpMsg, OfpMsgEvent},
OfpPort, PseudoPort, use super::{FlowModCommand, MatchFields};
};
use super::{FlowAction, FlowModCommand, MatchFields, SizeCheck};
pub enum Timeout { pub enum Timeout {
Permanent, Permanent,
...@@ -32,7 +30,7 @@ pub struct FlowModEvent { ...@@ -32,7 +30,7 @@ pub struct FlowModEvent {
command: FlowModCommand, command: FlowModCommand,
match_fields: MatchFields, match_fields: MatchFields,
priority: u16, priority: u16,
actions: Vec<FlowAction>, actions: Vec<Action>,
cookie: u64, cookie: u64,
idle_timeout: Timeout, idle_timeout: Timeout,
hard_timeout: Timeout, hard_timeout: Timeout,
...@@ -46,7 +44,7 @@ impl FlowModEvent { ...@@ -46,7 +44,7 @@ impl FlowModEvent {
pub fn add_flow( pub fn add_flow(
priority: u16, priority: u16,
match_fileds: MatchFields, match_fileds: MatchFields,
actions: Vec<FlowAction>, actions: Vec<Action>,
buffer_id: Option<u32>, buffer_id: Option<u32>,
) -> Self { ) -> Self {
Self { Self {
...@@ -75,7 +73,7 @@ impl FlowModEvent { ...@@ -75,7 +73,7 @@ impl FlowModEvent {
let buffer_id = bytes.read_i32::<BigEndian>().unwrap(); let buffer_id = bytes.read_i32::<BigEndian>().unwrap();
let out_port = PseudoPort::parse(bytes.read_u16::<BigEndian>().unwrap()); let out_port = PseudoPort::parse(bytes.read_u16::<BigEndian>().unwrap());
let flags = bytes.read_u16::<BigEndian>().unwrap(); let flags = bytes.read_u16::<BigEndian>().unwrap();
let actions = FlowAction::parse_sequence(&mut bytes); let actions = Action::parse_sequence(&mut bytes);
FlowModEvent { FlowModEvent {
command, command,
match_fields, match_fields,
...@@ -99,13 +97,13 @@ impl FlowModEvent { ...@@ -99,13 +97,13 @@ impl FlowModEvent {
impl MessageMarshal for FlowModEvent { impl MessageMarshal for FlowModEvent {
fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize { fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp.msg_usize(OfpMsg::FlowMod) ofp.msg_usize(Msg::FlowMod)
} }
fn size_of(&self) -> usize { fn size_of(&self) -> usize {
24 24
} }
fn msg_code(&self) -> OfpMsg { fn msg_code(&self) -> Msg {
OfpMsg::FlowMod Msg::FlowMod
} }
fn marshal(&self, bytes: &mut Vec<u8>) { fn marshal(&self, bytes: &mut Vec<u8>) {
self.match_fields.marshal(bytes); self.match_fields.marshal(bytes);
...@@ -128,7 +126,7 @@ impl MessageMarshal for FlowModEvent { ...@@ -128,7 +126,7 @@ impl MessageMarshal for FlowModEvent {
); );
for act in self.actions.move_controller_last() { for act in self.actions.move_controller_last() {
match act { match act {
FlowAction::Oputput(PseudoPort::Table) => { Action::Oputput(PseudoPort::Table) => {
panic!("Openflow table not allowed") panic!("Openflow table not allowed")
} }
_ => (), _ => (),
......
...@@ -4,11 +4,7 @@ pub use flow_mod_handler::FlowModEvent; ...@@ -4,11 +4,7 @@ pub use flow_mod_handler::FlowModEvent;
pub mod command; pub mod command;
pub use command::FlowModCommand; pub use command::FlowModCommand;
pub mod flow_actions;
pub use flow_actions::{FlowAction, SizeCheck};
pub mod flow_actions_type;
pub use flow_actions_type::FlowActionType;
pub mod match_fields; pub mod match_fields;
pub use match_fields::{Mask, MatchFields}; pub use match_fields::{Mask, MatchFields};
use crate::openflow::ofp_manager::{MessageMarshal, OfpMsg}; use crate::openflow::ofp10::{traiter::{MessageMarshal, OfpMsgEvent}, Msg};
use crate::openflow::traiter::OfpMsgEvent;
pub struct HelloEvent {} pub struct HelloEvent {}
...@@ -12,8 +13,8 @@ impl HelloEvent { ...@@ -12,8 +13,8 @@ impl HelloEvent {
impl MessageMarshal for HelloEvent { impl MessageMarshal for HelloEvent {
fn marshal(&self, _: &mut Vec<u8>) {} fn marshal(&self, _: &mut Vec<u8>) {}
fn msg_code(&self) -> OfpMsg { fn msg_code(&self) -> Msg {
OfpMsg::Hello Msg::Hello
} }
fn size_of(&self) -> usize { fn size_of(&self) -> usize {
...@@ -21,6 +22,6 @@ impl MessageMarshal for HelloEvent { ...@@ -21,6 +22,6 @@ impl MessageMarshal for HelloEvent {
} }
fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize { fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp.msg_usize(OfpMsg::Hello) ofp.msg_usize(Msg::Hello)
} }
} }
...@@ -5,13 +5,16 @@ pub mod packet_out; ...@@ -5,13 +5,16 @@ pub mod packet_out;
pub use packet_out::PacketOutEvent; pub use packet_out::PacketOutEvent;
pub mod flow_mod; pub mod flow_mod;
pub use flow_mod::{FlowAction, FlowModEvent}; pub use flow_mod::FlowModEvent;
pub mod actions;
pub use actions::Action;
pub mod hello; pub mod hello;
pub use hello::HelloEvent; pub use hello::HelloEvent;
pub mod features_req; pub mod features_req;
pub use features_req::FeaturesReq; pub use features_req::FeaturesReqEvent;
pub mod payload; pub mod payload;
pub use payload::Payload; pub use payload::Payload;
...@@ -3,19 +3,19 @@ use std::{ ...@@ -3,19 +3,19 @@ use std::{
mem::size_of, mem::size_of,
}; };
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use crate::openflow::ofp10::{ofp_port::OfpPort, Msg};
use crate::openflow::ofp10::{
use crate::openflow::{ traiter::{MessageMarshal, OfpMsgEvent},
ofp_manager::{MessageMarshal, OfpMsg, OfpMsgEvent}, PseudoPort,
OfpPort, PseudoPort,
}; };
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use super::{flow_mod::SizeCheck, FlowAction, Payload}; use super::{actions::SizeCheck, Action, Payload};
pub struct PacketOutEvent { pub struct PacketOutEvent {
pub payload: Payload, pub payload: Payload,
pub in_port: Option<u16>, pub in_port: Option<u16>,
pub actions: Vec<FlowAction>, pub actions: Vec<Action>,
} }
impl MessageMarshal for PacketOutEvent { impl MessageMarshal for PacketOutEvent {
...@@ -39,12 +39,12 @@ impl MessageMarshal for PacketOutEvent { ...@@ -39,12 +39,12 @@ impl MessageMarshal for PacketOutEvent {
self.payload.marshal(bytes); self.payload.marshal(bytes);
} }
fn msg_code(&self) -> OfpMsg { fn msg_code(&self) -> Msg {
OfpMsg::PacketOut Msg::PacketOut
} }
fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize { fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp.msg_usize(OfpMsg::PacketOut) ofp.msg_usize(Msg::PacketOut)
} }
fn size_of(&self) -> usize { fn size_of(&self) -> usize {
...@@ -53,7 +53,7 @@ impl MessageMarshal for PacketOutEvent { ...@@ -53,7 +53,7 @@ impl MessageMarshal for PacketOutEvent {
} }
impl PacketOutEvent { impl PacketOutEvent {
pub fn new(in_port: Option<u16>, payload: Payload, actions: Vec<FlowAction>) -> Self { pub fn new(in_port: Option<u16>, payload: Payload, actions: Vec<Action>) -> Self {
Self { Self {
in_port, in_port,
payload, payload,
...@@ -74,7 +74,7 @@ impl PacketOutEvent { ...@@ -74,7 +74,7 @@ impl PacketOutEvent {
let mut actions_buf = vec![0; action_len as usize]; let mut actions_buf = vec![0; action_len as usize];
let _ = bytes.read_exact(&mut actions_buf); let _ = bytes.read_exact(&mut actions_buf);
let mut action_bytes = Cursor::new(actions_buf); let mut action_bytes = Cursor::new(actions_buf);
let actions = FlowAction::parse_sequence(&mut action_bytes); let actions = Action::parse_sequence(&mut action_bytes);
Self { Self {
payload: match buf_id { payload: match buf_id {
None => Payload::NoBuffered(bytes.fill_buf().unwrap().to_vec()), None => Payload::NoBuffered(bytes.fill_buf().unwrap().to_vec()),
......
pub enum OfpMsg { pub enum Msg {
Hello, Hello,
FeaturesReq, FeaturesReq,
PacketIn, PacketIn,
PacketOut, PacketOut,
FlowMod, FlowMod,
NotFound NotFound,
} }
pub mod message;
pub use message::Msg;
pub mod ofp_port;
pub use ofp_port::PseudoPort;
pub mod events;
pub use events::{FlowModEvent, HelloEvent, PacketInEvent, PacketOutEvent};
pub mod traiter;
pub mod ofp_header;
pub mod ofp_v1_0;
\ No newline at end of file
use crate::openflow::{ use super::{events::{Action, FeaturesReqEvent, Payload}, ofp_header::{OfpHeader, OfpHeader10, OpenflowHeader}, traiter::OfpMsgEvent, HelloEvent, Msg, PacketOutEvent};
events::{FeaturesReq, FlowAction, HelloEvent, PacketOutEvent, Payload},
ofp_header::{OfpHeader10, OpenflowHeader},
OfpHeader,
};
use super::traiter::OfpMsgEvent;
use super::OfpMsg;
pub struct Openflow10 {} pub struct Openflow10 {}
...@@ -26,14 +20,14 @@ impl OfpMsgEvent for Openflow10 { ...@@ -26,14 +20,14 @@ impl OfpMsgEvent for Openflow10 {
HelloEvent::new() HelloEvent::new()
} }
fn fetures_req(&self) -> FeaturesReq { fn fetures_req(&self) -> FeaturesReqEvent {
FeaturesReq::new() FeaturesReqEvent::new()
} }
fn packet_out( fn packet_out(
&self, &self,
port_id: Option<u16>, port_id: Option<u16>,
payload: Payload, payload: Payload,
actions: Vec<FlowAction>, actions: Vec<Action>,
) -> PacketOutEvent { ) -> PacketOutEvent {
PacketOutEvent::new(port_id, payload, actions) PacketOutEvent::new(port_id, payload, actions)
} }
...@@ -48,24 +42,24 @@ impl OfpMsgEvent for Openflow10 { ...@@ -48,24 +42,24 @@ impl OfpMsgEvent for Openflow10 {
OfpHeader::new(OfpHeader10::new(message, length as usize, xid as usize)) OfpHeader::new(OfpHeader10::new(message, length as usize, xid as usize))
} }
fn msg_parse(&self, msg: u16) -> OfpMsg { fn msg_parse(&self, msg: u16) -> Msg {
match msg { match msg {
0 => OfpMsg::Hello, 0 => Msg::Hello,
5 => OfpMsg::FeaturesReq, 5 => Msg::FeaturesReq,
10 => OfpMsg::PacketIn, 10 => Msg::PacketIn,
13 => OfpMsg::PacketOut, 13 => Msg::PacketOut,
14 => OfpMsg::FlowMod, 14 => Msg::FlowMod,
_ => OfpMsg::NotFound, _ => Msg::NotFound,
} }
} }
fn msg_usize(&self, msg: OfpMsg) -> usize { fn msg_usize(&self, msg: Msg) -> usize {
match msg { match msg {
OfpMsg::Hello => 0, Msg::Hello => 0,
OfpMsg::FeaturesReq => 5, Msg::FeaturesReq => 5,
OfpMsg::PacketIn => 10, Msg::PacketIn => 10,
OfpMsg::PacketOut => 13, Msg::PacketOut => 13,
OfpMsg::FlowMod => 14, Msg::FlowMod => 14,
_ => 1024, _ => 1024,
} }
} }
......
use crate::openflow::{ use crate::openflow::{
events::{FeaturesReq, FlowAction, HelloEvent, PacketOutEvent, Payload}, ofp10::events::{FeaturesReqEvent, Action, HelloEvent, PacketOutEvent, Payload},
ofp_header::OpenflowHeader,
OfpHeader,
}; };
use super::OfpMsg; use super::{ofp_header::{OfpHeader, OpenflowHeader}, Msg};
/** /**
* the trait for parse value to bytes. * the trait for parse value to bytes.
...@@ -12,7 +10,7 @@ use super::OfpMsg; ...@@ -12,7 +10,7 @@ use super::OfpMsg;
*/ */
pub trait MessageMarshal { pub trait MessageMarshal {
fn marshal(&self, bytes: &mut Vec<u8>); fn marshal(&self, bytes: &mut Vec<u8>);
fn msg_code(&self) -> OfpMsg; fn msg_code(&self) -> Msg;
fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize; fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize;
fn size_of(&self) -> usize; fn size_of(&self) -> usize;
} }
...@@ -27,9 +25,14 @@ pub trait OfpMsgEvent { ...@@ -27,9 +25,14 @@ pub trait OfpMsgEvent {
fn ofp_version() -> usize; fn ofp_version() -> usize;
fn header_size(&self) -> usize; fn header_size(&self) -> usize;
fn msg_usize(&self, msg: OfpMsg) -> usize; fn msg_usize(&self, msg: Msg) -> usize;
fn msg_parse(&self, msg: u16) -> OfpMsg; fn msg_parse(&self, msg: u16) -> Msg;
fn hello_event(&self) -> HelloEvent; fn hello_event(&self) -> HelloEvent;
fn fetures_req(&self) -> FeaturesReq; fn fetures_req(&self) -> FeaturesReqEvent;
fn packet_out(&self, port_id: Option<u16>, payload: Payload, actions: Vec<FlowAction>) -> PacketOutEvent; fn packet_out(
&self,
port_id: Option<u16>,
payload: Payload,
actions: Vec<Action>,
) -> PacketOutEvent;
} }
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;
pub mod macro_selector;
use crate::openflow::{ofp_manager::Openflow10, traiter::OfpMsgEvent};
use crate::{ofp_from_version, Controller}; use crate::{ofp_from_version, Controller};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::{io::Read, net::TcpListener, thread}; use std::{io::Read, net::TcpListener, thread};
use super::controller_frame::ControllerFrame; use super::controller_frame::ControllerFrame;
use super::events::HelloEvent; use super::ofp10::ofp_v1_0::Openflow10;
use crate::openflow::ofp10::{traiter::OfpMsgEvent, HelloEvent};
pub fn tcp_listener_handler<OME: OfpMsgEvent>(address: &str, ofp_version: u8) { pub fn tcp_listener_handler<OME: OfpMsgEvent>(address: &str, ofp_version: u8) {
let controller = Arc::new(Mutex::from(Controller::new(ofp_from_version!(ofp_version)))); let controller = Arc::new(Mutex::from(Controller::new(ofp_from_version!(ofp_version))));
......
...@@ -2,9 +2,8 @@ ...@@ -2,9 +2,8 @@
mod tests { mod tests {
use tenjin::{ use tenjin::{
openflow::{ openflow::{
controller_frame::ControllerFrame, controller_frame::ControllerFrame, ofp10::ofp_v1_0::Openflow10,
ofp_manager::{OfpMsg, OfpMsgEvent}, ofp10::traiter::OfpMsgEvent, ofp10::Msg,
ofp_v1_0::Openflow10,
}, },
Controller, Controller,
}; };
...@@ -28,7 +27,7 @@ mod tests { ...@@ -28,7 +27,7 @@ mod tests {
let controller = Controller::new(Openflow10::new()); let controller = Controller::new(Openflow10::new());
let ofp = controller.get_ofp(); let ofp = controller.get_ofp();
let ofp_header = ofp.header(ofp.msg_usize(OfpMsg::Hello) as u8, 0, 0); let ofp_header = ofp.header(ofp.msg_usize(Msg::Hello) as u8, 0, 0);
let mut bytes: Vec<u8> = Vec::new(); let mut bytes: Vec<u8> = Vec::new();
ofp_header.marshal(&mut bytes); ofp_header.marshal(&mut bytes);
......
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