Commit 08809962 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

in controller, use openflow10 instead of impl

parent 6d8bb627
...@@ -2,24 +2,36 @@ ...@@ -2,24 +2,36 @@
#![allow(unused_variables)] #![allow(unused_variables)]
use std::{collections::HashMap, net::TcpStream}; 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}}}; use crate::{
etherparser::ether_type::EtherType,
openflow::{
controller_frame::ControllerFrame,
ofp10::{
self,
events::{flow_mod::MatchFields, Action},
ofp_v1_0::Openflow10,
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.
*/ */
pub struct Controller<OME: OfpMsgEvent> { pub struct Controller {
ofp: OME, ofp: Openflow10,
mac_to_port: HashMap<u64, u16>, mac_to_port: HashMap<u64, u16>,
} }
impl<OME: OfpMsgEvent> ControllerFrame<OME> for Controller<OME> { impl ControllerFrame<Openflow10> for Controller {
fn get_ofp(&self) -> &impl OfpMsgEvent { fn get_ofp(&self) -> &impl OfpMsgEvent {
&self.ofp &self.ofp
} }
fn new(ofp: OME) -> Self { fn new() -> Self {
Self { Self {
ofp, ofp: Openflow10::new(),
mac_to_port: HashMap::new(), mac_to_port: HashMap::new(),
} }
} }
...@@ -63,7 +75,7 @@ impl<OME: OfpMsgEvent> ControllerFrame<OME> for Controller<OME> { ...@@ -63,7 +75,7 @@ impl<OME: OfpMsgEvent> ControllerFrame<OME> for Controller<OME> {
} }
} }
impl<OME: OfpMsgEvent> Controller<OME> { impl Controller {
fn add_flow( fn add_flow(
&self, &self,
xid: u32, xid: u32,
......
use tenjin::{ use tenjin::{openflow::controller_frame::ControllerFrame, Controller};
openflow::{controller_frame::ControllerFrame, ofp10::ofp_v1_0::Openflow10},
Controller,
};
extern crate byteorder; extern crate byteorder;
fn main() -> Result<(), std::io::Error> { fn main() -> Result<(), std::io::Error> {
// Controller::listener("127.0.0.1:6633", Openflow10::new()); Controller::listener("127.0.0.1:6633");
// tcp_listener_handler(Controller::new(Openflow10), "127.0.0.1:6633");
Controller::listener("127.0.0.1:6633", Openflow10::new());
Ok(()) Ok(())
} }
use crate::openflow::ofp10::{ use crate::openflow::ofp10::{
traiter::{MessageMarshal, OfpMsgEvent}, ErrorEvent, Msg, PacketInEvent traiter::{MessageMarshal, OfpMsgEvent},
ErrorEvent, Msg, PacketInEvent,
}; };
use std::{ use std::{
io::{Read, Write}, io::{Read, Write},
...@@ -11,10 +12,10 @@ use super::tcp_listener::tcp_listener_handler; ...@@ -11,10 +12,10 @@ use super::tcp_listener::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;
fn packet_in_handler(&mut self, xid: u32, packetin: PacketInEvent, stream: &mut TcpStream); fn packet_in_handler(&mut self, xid: u32, packetin: PacketInEvent, stream: &mut TcpStream);
fn new(ofp: OME) -> Self; fn new() -> Self;
fn listener(address: &str, ofp: OME) { fn listener(address: &str) {
tcp_listener_handler::<OME>(address, ofp.version() as u8); tcp_listener_handler::<OME>(address);
} }
fn handle_header(&mut self, buf: &mut Vec<u8>) -> (u8, usize, u32) { fn handle_header(&mut self, buf: &mut Vec<u8>) -> (u8, usize, u32) {
...@@ -34,10 +35,10 @@ pub trait ControllerFrame<OME: OfpMsgEvent> { ...@@ -34,10 +35,10 @@ pub trait ControllerFrame<OME: OfpMsgEvent> {
match message { match message {
Msg::Hello => self.send_msg(self.get_ofp().fetures_req(), xid, stream), Msg::Hello => self.send_msg(self.get_ofp().fetures_req(), xid, stream),
Msg::Error => { Msg::Error => {
let error =ErrorEvent::parse(&payload); let error = ErrorEvent::parse(&payload);
println!("Error {:?}", error.error_type); println!("Error {:?}", error.error_type);
() ()
}, }
Msg::FeaturesReq => (), Msg::FeaturesReq => (),
Msg::PacketIn => { Msg::PacketIn => {
self.packet_in_handler(xid, PacketInEvent::parse(&payload), stream); self.packet_in_handler(xid, PacketInEvent::parse(&payload), stream);
......
/**
* this macro use for create Controller with version inside thread.
* Tt is used at 'tcp_listener/tcp_listener_handler.rs'
*/
#[macro_export]
macro_rules! ofp_from_version {
($ofp_version: expr) => {
match $ofp_version {
1 => Openflow10::new(),
_ => panic!("This version is not support")
}
};
}
\ No newline at end of file
pub mod controller_frame; pub mod controller_frame;
pub mod ofp10; pub mod ofp10;
pub mod tcp_listener; pub mod tcp_listener;
pub mod macro_selector;
\ No newline at end of file
use crate::{ofp_from_version, Controller}; use crate::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::ofp10::ofp_v1_0::Openflow10;
use crate::openflow::ofp10::{traiter::OfpMsgEvent, HelloEvent}; 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) {
let controller = Arc::new(Mutex::from(Controller::new(ofp_from_version!(ofp_version)))); let controller = Arc::new(Mutex::from(Controller::new()));
let listener = TcpListener::bind(address).unwrap(); let listener = TcpListener::bind(address).unwrap();
for stream in listener.incoming() { for stream in listener.incoming() {
match stream { match stream {
......
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