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

packet_out

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