Commit ecb47c2e authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

add event of message -> packetIn

parent 3baedfb4
use tenjin::etherparser::ethernet::EthernetFrame;
use tenjin::openflow::{Controller, Msg, OfpHeader}; use tenjin::openflow::{Controller, Msg, OfpHeader};
use std::io::Read; use std::io::Read;
use std::net::TcpListener; use std::net::TcpListener;
...@@ -40,7 +41,7 @@ fn main() -> Result<(), std::io::Error> { ...@@ -40,7 +41,7 @@ fn main() -> Result<(), std::io::Error> {
println!("Hello event"); println!("Hello event");
} }
Msg::PacketIn(b) => { Msg::PacketIn(b) => {
controller.packetIn(packet.xid, &payload, &mut stream); controller.packetIn(packet.xid, EthernetFrame::parse(&payload), &mut stream);
println!("PacketIn event"); println!("PacketIn event");
} }
_ => { _ => {
......
use std::{io::Write, mem::size_of, net::TcpStream}; use std::{collections::HashMap, io::Write, mem::size_of, net::TcpStream};
use byteorder::{BigEndian, WriteBytesExt}; use byteorder::{BigEndian, WriteBytesExt};
use crate::etherparser::ethernet::EthernetFrame;
use super::OfpHeader; use super::OfpHeader;
pub struct Controller { pub struct Controller {
version: u8, version: u8,
mac_to_port: HashMap<u64, u16>,
} }
impl Controller { impl Controller {
pub const OFP_1_0: u8 = 1; pub const OFP_1_0: u8 = 1;
pub fn new(version: u8) -> Self { pub fn new(version: u8) -> Self {
Self { version } Self {
version,
mac_to_port: HashMap::new(),
}
} }
pub fn hello(&self, stream: &mut TcpStream) { pub fn hello(&self, stream: &mut TcpStream) {
let header = OfpHeader::new(self.version, 0, size_of::<OfpHeader>() as u16, 0); let header = OfpHeader::new(self.version, 0, size_of::<OfpHeader>() as u16, 0);
...@@ -27,9 +33,7 @@ impl Controller { ...@@ -27,9 +33,7 @@ impl Controller {
stream.write_all(&bytes).unwrap(); stream.write_all(&bytes).unwrap();
} }
pub fn packetIn(&self, xid: u32, payload: &Vec<u8>, stream: &mut TcpStream) { pub fn packetIn(&self, xid: u32, payload: EthernetFrame, stream: &mut TcpStream) {}
// pass
}
pub fn send(&self, xid: u32, message: u8, payload: &Vec<u8>, stream: &mut TcpStream) { pub fn send(&self, xid: u32, message: u8, payload: &Vec<u8>, stream: &mut TcpStream) {
let length = size_of::<OfpHeader>() + payload.len(); let length = size_of::<OfpHeader>() + payload.len();
......
use std::io::{BufRead, Cursor};
use byteorder::{BigEndian, ReadBytesExt};
use crate::etherparser::ethernet::EthernetFrame;
pub enum PacketInReason {
NoMatch,
Action,
Unknown,
}
pub struct PacketInEvent {
pub buf_id: Option<i32>,
pub total_len: u16,
pub port: u16,
pub reason: PacketInReason,
pub table_id: u8,
pub payload: EthernetFrame,
}
impl PacketInEvent {
pub fn parse(payload: &Vec<u8>) -> PacketInEvent {
let mut bytes = Cursor::new(payload.to_vec());
let buf_id = match bytes.read_i32::<BigEndian>().unwrap() {
-1 => None,
n => Some(n),
};
let total_len = bytes.read_u16::<BigEndian>().unwrap();
let port = bytes.read_u16::<BigEndian>().unwrap();
let reason = match bytes.read_u8().unwrap() {
1 => PacketInReason::NoMatch,
2 => PacketInReason::Action,
_ => PacketInReason::Unknown,
};
let table_id = bytes.read_u8().unwrap();
let packet = bytes.fill_buf().unwrap().to_vec();
let payload = EthernetFrame::parse(&packet);
PacketInEvent {
buf_id,
total_len,
port,
reason,
table_id,
payload,
}
}
}
...@@ -6,3 +6,5 @@ pub use message::Msg; ...@@ -6,3 +6,5 @@ pub use message::Msg;
pub mod controller; pub mod controller;
pub use controller::Controller; pub use controller::Controller;
pub mod events;
\ No newline at end of file
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