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

rewrite structure

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