Commit 117ebae2 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

packet_in: change payload -> dont auto parse to ethernet frame and create method to parse payload

parent 8e30497f
...@@ -29,8 +29,9 @@ impl<OME: OfpMsgEvent> ControllerFrame<OME> for Controller<OME> { ...@@ -29,8 +29,9 @@ impl<OME: OfpMsgEvent> ControllerFrame<OME> for Controller<OME> {
* Start here for handle packetIn message. * Start here for handle packetIn message.
*/ */
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) {
let mac_dst = packetin.payload.mac_des; let pkt = packetin.ether_parse();
let mac_src = packetin.payload.mac_src; let mac_dst = pkt.mac_des;
let mac_src = pkt.mac_src;
let out_port = self.mac_to_port.get(&mac_dst); let out_port = self.mac_to_port.get(&mac_dst);
match out_port { match out_port {
Some(p) => { Some(p) => {
...@@ -49,7 +50,7 @@ impl<OME: OfpMsgEvent> ControllerFrame<OME> for Controller<OME> { ...@@ -49,7 +50,7 @@ impl<OME: OfpMsgEvent> ControllerFrame<OME> for Controller<OME> {
let actions = vec![FlowAction::Oputput(PseudoPort::PhysicalPort(src_port))]; let actions = vec![FlowAction::Oputput(PseudoPort::PhysicalPort(src_port))];
self.add_flow(xid, dst_src_match, actions, stream); self.add_flow(xid, dst_src_match, actions, stream);
// let pkt_out = // let pkt_out =
} }
None => todo!(), None => todo!(),
} }
......
pub mod packet_in; pub mod packet_in;
pub use packet_in::{PacketInEvent, PacketInReason}; pub use packet_in::{PacketInEvent, PacketInReason};
pub mod packet_out;
pub mod flow_mod; pub mod flow_mod;
pub use flow_mod::{FlowAction, FlowModEvent}; pub use flow_mod::{FlowAction, FlowModEvent};
...@@ -8,4 +10,7 @@ pub mod hello; ...@@ -8,4 +10,7 @@ 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::FeaturesReq;
\ No newline at end of file
pub mod payload;
pub use payload::Payload;
\ No newline at end of file
use std::io::{BufRead, Cursor}; use crate::etherparser::ethernet::EthernetFrame;
use super::Payload;
use byteorder::{BigEndian, ReadBytesExt}; use byteorder::{BigEndian, ReadBytesExt};
use std::io::{BufRead, Cursor};
use crate::etherparser::ethernet::EthernetFrame;
pub enum PacketInReason { pub enum PacketInReason {
NoMatch, NoMatch,
...@@ -16,10 +16,15 @@ pub struct PacketInEvent { ...@@ -16,10 +16,15 @@ pub struct PacketInEvent {
pub port: u16, pub port: u16,
pub reason: PacketInReason, pub reason: PacketInReason,
pub table_id: u8, pub table_id: u8,
pub payload: EthernetFrame, pub payload: Payload,
} }
impl PacketInEvent { impl PacketInEvent {
pub fn ether_parse(&self) -> EthernetFrame {
match &self.payload {
Payload::Buffered(_, p) | Payload::NoBuffered(p) => EthernetFrame::parse(&p),
}
}
pub fn parse(payload: &Vec<u8>) -> PacketInEvent { pub fn parse(payload: &Vec<u8>) -> PacketInEvent {
let mut bytes = Cursor::new(payload.to_vec()); let mut bytes = Cursor::new(payload.to_vec());
let buf_id = match bytes.read_i32::<BigEndian>().unwrap() { let buf_id = match bytes.read_i32::<BigEndian>().unwrap() {
...@@ -35,7 +40,10 @@ impl PacketInEvent { ...@@ -35,7 +40,10 @@ impl PacketInEvent {
}; };
let table_id = bytes.read_u8().unwrap(); let table_id = bytes.read_u8().unwrap();
let packet = bytes.fill_buf().unwrap().to_vec(); let packet = bytes.fill_buf().unwrap().to_vec();
let payload = EthernetFrame::parse(&packet); let payload = match buf_id {
Some(n) => Payload::Buffered(n as u32, packet),
None => Payload::NoBuffered(packet),
};
PacketInEvent { PacketInEvent {
buf_id, buf_id,
total_len, total_len,
......
use super::Payload;
pub struct PacketOutEvent {
pub payload: Payload
}
\ No newline at end of file
pub enum Payload {
Buffered(u32, Vec<u8>),
NoBuffered(Vec<u8>),
}
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