Commit 1291acdc authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

packet_out

parent 8bb532bc
...@@ -161,12 +161,11 @@ impl SetField { ...@@ -161,12 +161,11 @@ impl SetField {
} }
} }
pub type Buffer = u16; pub type Buffer = u16;
#[derive(Clone)] #[derive(Clone)]
#[repr(u8)] #[repr(u8)]
pub enum Action { pub enum Action {
Oputput(PseudoPort, Option<Buffer>), Oputput(PseudoPort),
CopyTtlOut, // Copy TTL "outwards" -- from next-to-outermost to outermost CopyTtlOut, // Copy TTL "outwards" -- from next-to-outermost to outermost
CopyTtlIn, // Copy TTL "inwards" -- from outermost to next-to-outermost CopyTtlIn, // Copy TTL "inwards" -- from outermost to next-to-outermost
SetMplsTtl(u8), // MPLS TTL SetMplsTtl(u8), // MPLS TTL
...@@ -191,7 +190,7 @@ pub enum Action { ...@@ -191,7 +190,7 @@ pub enum Action {
impl Action { impl Action {
pub fn action_type(&self) -> ActionType { pub fn action_type(&self) -> ActionType {
match &self { match &self {
Action::Oputput(_, _) => ActionType::Output, Action::Oputput(_) => ActionType::Output,
Action::CopyTtlOut => ActionType::CopyTtlOut, Action::CopyTtlOut => ActionType::CopyTtlOut,
Action::CopyTtlIn => ActionType::CopyTtlIn, Action::CopyTtlIn => ActionType::CopyTtlIn,
Action::SetMplsTtl(_) => ActionType::SetMplsTtl, Action::SetMplsTtl(_) => ActionType::SetMplsTtl,
...@@ -212,15 +211,11 @@ impl Action { ...@@ -212,15 +211,11 @@ impl Action {
} }
pub fn marshal(&self, bytes: &mut Vec<u8>) { pub fn marshal(&self, bytes: &mut Vec<u8>) {
match &self { match &self {
Action::Oputput(port, buffer) => { Action::Oputput(port) => {
self.action_type().marshal(bytes); self.action_type().marshal(bytes);
bytes.write_u16::<BigEndian>(16); // len bytes.write_u16::<BigEndian>(16); // len
port.marshal(bytes); port.marshal(bytes);
if let Some(buf_id) = buffer { bytes.write_u16::<BigEndian>(ControllerMaxLen::NoBuffer.into());
bytes.write_u16::<BigEndian>(*buf_id);
} else {
bytes.write_u16::<BigEndian>(ControllerMaxLen::NoBuffer.into());
}
// padding 48bit // padding 48bit
bytes.write_u32::<BigEndian>(0); bytes.write_u32::<BigEndian>(0);
bytes.write_u16::<BigEndian>(0); bytes.write_u16::<BigEndian>(0);
......
use std::{ use crate::openflow::ofp13::PseudoPort;
io::{BufRead, Cursor, Error, Read}, use crate::openflow::ofp13::{ofp_port::OfpPort, MessageMarshal, Msg};
mem::size_of, use byteorder::{BigEndian, WriteBytesExt};
};
use crate::openflow::ofp10::PseudoPort; use super::{Action, Payload};
use crate::openflow::ofp10::{ofp_port::OfpPort, MessageMarshal, Msg};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use super::{actions::SizeCheck, Action, Payload};
pub struct PacketOutEvent { pub struct PacketOutEvent {
pub payload: Payload, // buffer_id is in Payload
pub in_port: Option<u16>, pub in_port: Option<u32>,
pub actions: Vec<Action>, pub actions: Vec<Action>,
pub payload: Payload,
} }
impl MessageMarshal for PacketOutEvent { impl MessageMarshal for PacketOutEvent {
fn marshal(&self, bytes: &mut Vec<u8>) { fn marshal(&self, bytes: &mut Vec<u8>) {
// buffer id
let _ = bytes.write_i32::<BigEndian>(match self.payload { let _ = bytes.write_i32::<BigEndian>(match self.payload {
Payload::Buffered(n, _) => n as i32, Payload::Buffered(n, _) => n as i32,
Payload::NoBuffered(_) => -1, Payload::NoBuffered(_) => -1,
}); });
// in_port
match self.in_port { match self.in_port {
Some(id) => { Some(id) => {
PseudoPort::PhysicalPort(id).marshal(bytes); PseudoPort::PhysicalPort(id).marshal(bytes);
} }
None => { None => {
let _ = bytes.write_u16::<BigEndian>(OfpPort::None as u16); let _ = bytes.write_u32::<BigEndian>(OfpPort::Any as u32);
} }
} }
let _ = bytes.write_u16::<BigEndian>(self.actions.size_of_sequence() as u16); let mut action_byte: Vec<u8> = Vec::new();
for act in self.actions.move_controller_last() { for act in self.actions.iter() {
act.marshal(bytes); act.marshal(&mut action_byte);
} }
let _ = bytes.write_u16::<BigEndian>(action_byte.len() as u16);
// padding 48 bit
bytes.write_u32::<BigEndian>(0);
bytes.write_u16::<BigEndian>(0);
bytes.append(&mut action_byte);
self.payload.marshal(bytes); self.payload.marshal(bytes);
} }
...@@ -45,28 +49,26 @@ impl MessageMarshal for PacketOutEvent { ...@@ -45,28 +49,26 @@ impl MessageMarshal for PacketOutEvent {
} }
fn size_of(&self) -> usize { fn size_of(&self) -> usize {
size_of::<(u32, u16, u16)>() + self.actions.size_of_sequence() + self.payload.length() 24
} }
} }
impl PacketOutEvent { impl PacketOutEvent {
pub fn new(in_port: Option<u16>, payload: Payload, actions: Vec<Action>) -> Self { pub fn new(in_port: Option<u32>, payload: Payload, actions: Vec<Action>) -> Self {
Self { Self {
in_port, in_port,
payload, payload,
actions, actions,
} }
} }
/* TODO
pub fn parse(buf: &Vec<u8>) -> Result<Self, Error> { pub fn parse(buf: &Vec<u8>) -> Result<Self, Error> {
let mut bytes = Cursor::new(buf); let mut bytes = Cursor::new(buf);
let buf_id = match bytes let buf_id = match bytes.read_i32::<BigEndian>()? {
.read_i32::<BigEndian>()
.expect("cannot parse buf id in packetout")
{
-1 => None, -1 => None,
n => Some(n), n => Some(n),
}; };
let in_port = bytes.read_u16::<BigEndian>()?; let in_port = bytes.read_u32::<BigEndian>()?;
let action_len = bytes.read_u16::<BigEndian>()?; let action_len = bytes.read_u16::<BigEndian>()?;
let mut actions_buf = vec![0; action_len as usize]; let mut actions_buf = vec![0; action_len as usize];
let _ = bytes.read_exact(&mut actions_buf); let _ = bytes.read_exact(&mut actions_buf);
...@@ -86,5 +88,5 @@ impl PacketOutEvent { ...@@ -86,5 +88,5 @@ impl PacketOutEvent {
}, },
actions, actions,
}) })
} } */
} }
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