Commit 44d9a1e3 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

add error event

parent 349affa4
use crate::openflow::ofp10::{
traiter::{MessageMarshal, OfpMsgEvent}, ErrorEvent, Msg, PacketInEvent
};
use std::{
io::{Read, Write},
net::TcpStream,
};
use crate::openflow::ofp10::{Msg, PacketInEvent, traiter::{MessageMarshal, OfpMsgEvent}};
use super::tcp_listener::tcp_listener_handler;
......@@ -31,6 +33,11 @@ pub trait ControllerFrame<OME: OfpMsgEvent> {
let message = self.get_ofp().msg_parse(message as u16);
match message {
Msg::Hello => self.send_msg(self.get_ofp().fetures_req(), xid, stream),
Msg::Error => {
let error =ErrorEvent::parse(&payload);
println!("Error {:?}", error.error_type);
()
},
Msg::FeaturesReq => (),
Msg::PacketIn => {
self.packet_in_handler(xid, PacketInEvent::parse(&payload), stream);
......
use std::{io::{BufRead, Cursor}, mem::size_of};
use crate::openflow::ofp10::{traiter::MessageMarshal, Msg};
use byteorder::{BigEndian, ReadBytesExt};
use super::error_type::ErrorType;
pub struct ErrorEvent {
pub error_type: ErrorType,
pub payload: Vec<u8>,
}
impl ErrorEvent {
pub fn new(error_type: ErrorType, payload: Vec<u8>) -> Self {
ErrorEvent {
error_type,
payload,
}
}
pub fn parse(buf: &Vec<u8>) -> ErrorEvent {
let mut bytes = Cursor::new(buf);
let error_type = bytes.read_u16::<BigEndian>().unwrap();
let error_code = bytes.read_u16::<BigEndian>().unwrap();
let code = ErrorType::new(error_type, error_code);
let payload = bytes.fill_buf().unwrap().to_vec();
ErrorEvent::new(code, payload)
}
}
impl MessageMarshal for ErrorEvent {
fn marshal(&self, _: &mut Vec<u8>) {}
fn msg_code(&self) -> crate::openflow::ofp10::Msg {
Msg::Error
}
fn msg_usize<OFP: crate::openflow::ofp10::traiter::OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp.msg_usize(Msg::Error)
}
fn size_of(&self) -> usize {
size_of::<(u16,u16)>() + self.payload.len()
}
}
#[derive(Debug)]
pub enum ErrorType {
HelloFailed(HelloFailed),
BadRequest(BadRequest),
BadAction(BadAction),
FlowModFailed(FlowModFailed),
PortModFailed(PortModFailed),
QueueOpFailed(QueueOpFailed),
}
impl ErrorType {
pub fn new(error_type: u16, error_code: u16) -> ErrorType {
match error_type {
0 => ErrorType::HelloFailed(HelloFailed::new(error_code)),
1 => ErrorType::BadRequest(BadRequest::new(error_code)),
2 => ErrorType::BadAction(BadAction::new(error_code)),
3 => ErrorType::FlowModFailed(FlowModFailed::new(error_code)),
4 => ErrorType::PortModFailed(PortModFailed::new(error_code)),
5 => ErrorType::QueueOpFailed(QueueOpFailed::new(error_code)),
_ => panic!("bad error_type in error {}", error_type),
}
}
}
#[derive(Debug)]
pub enum HelloFailed {
Incompatible,
EPerm,
}
impl HelloFailed {
pub fn new(error_code: u16) -> Self {
match error_code {
0 => Self::Incompatible,
_ => Self::EPerm,
}
}
}
#[derive(Debug)]
pub enum BadRequest {
BadVersion,
BadType,
BadStat,
BadVendor,
BadSubType,
EPerm,
BadLen,
BufferEmpty,
BufferUnkown,
}
impl BadRequest {
pub fn new(error_code: u16) -> Self {
match error_code {
0 => Self::BadVersion,
1 => Self::BadType,
2 => Self::BadStat,
3 => Self::BadVendor,
4 => Self::BadSubType,
5 => Self::EPerm,
6 => Self::BadLen,
7 => Self::BufferEmpty,
8 => Self::BufferUnkown,
_ => Self::BadVersion,
}
}
}
#[derive(Debug)]
pub enum BadAction {
BadType,
BadLen,
BadVendor,
BadVendotType,
BadOutPort,
BadArgument,
EPerm,
TooMany,
BadQueue,
}
impl BadAction {
pub fn new(error_code: u16) -> Self {
match error_code {
0 => Self::BadType,
1 => Self::BadLen,
2 => Self::BadVendor,
3 => Self::BadVendotType,
4 => Self::BadOutPort,
5 => Self::BadArgument,
6 => Self::EPerm,
7 => Self::TooMany,
_ => Self::BadQueue,
}
}
}
#[derive(Debug)]
pub enum FlowModFailed {
AllTablesFull,
Overlap,
EPerm,
BadEmergTimeout,
BadCommand,
Unsupported,
}
impl FlowModFailed {
pub fn new(error_code: u16) -> Self {
match error_code {
0 => Self::AllTablesFull,
1 => Self::Overlap,
2 => Self::EPerm,
3 => Self::BadEmergTimeout,
4 => Self::BadCommand,
_ => Self::Unsupported,
}
}
}
#[derive(Debug)]
pub enum PortModFailed {
BadPort,
BadHwAddr,
}
impl PortModFailed {
pub fn new(error_code: u16) -> Self {
match error_code {
0 => Self::BadPort,
_ => Self::BadHwAddr,
}
}
}
#[derive(Debug)]
pub enum QueueOpFailed {
BadPort,
BadQueue,
EPerm,
}
impl QueueOpFailed {
pub fn new(error_code: u16) -> Self {
match error_code {
0 => Self::BadPort,
1 => Self::BadQueue,
_ => Self::EPerm,
}
}
}
pub mod error_handler;
pub use error_handler::ErrorEvent;
pub mod error_type;
\ No newline at end of file
pub mod error;
pub use error::ErrorEvent;
pub mod packet_in;
pub use packet_in::{PacketInEvent, PacketInReason};
......
pub enum Msg {
Hello,
Error,
FeaturesReq,
PacketIn,
PacketOut,
......
......@@ -5,8 +5,8 @@ pub mod ofp_port;
pub use ofp_port::PseudoPort;
pub mod events;
pub use events::{FlowModEvent, HelloEvent, PacketInEvent, PacketOutEvent};
pub use events::{ErrorEvent, FlowModEvent, HelloEvent, PacketInEvent, PacketOutEvent};
pub mod traiter;
pub mod ofp_header;
pub mod ofp_v1_0;
pub mod traiter;
use super::{events::{Action, FeaturesReqEvent, Payload}, ofp_header::{OfpHeader, OfpHeader10, OpenflowHeader}, traiter::OfpMsgEvent, HelloEvent, Msg, PacketOutEvent};
use super::{
events::{Action, FeaturesReqEvent, Payload},
ofp_header::{OfpHeader, OfpHeader10, OpenflowHeader},
traiter::OfpMsgEvent,
HelloEvent, Msg, PacketOutEvent,
};
pub struct Openflow10 {}
......@@ -45,6 +49,7 @@ impl OfpMsgEvent for Openflow10 {
fn msg_parse(&self, msg: u16) -> Msg {
match msg {
0 => Msg::Hello,
1 => Msg::Error,
5 => Msg::FeaturesReq,
10 => Msg::PacketIn,
13 => Msg::PacketOut,
......@@ -56,6 +61,7 @@ impl OfpMsgEvent for Openflow10 {
fn msg_usize(&self, msg: Msg) -> usize {
match msg {
Msg::Hello => 0,
Msg::Error => 1,
Msg::FeaturesReq => 5,
Msg::PacketIn => 10,
Msg::PacketOut => 13,
......
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