Commit 35dbe4ad authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

fixed: match_field, OfpMatch, MacAddr;

MacAddr wrong convert;
match_fields hasmask for match, and length;
OfpMatch: padding was wrong position;
oxm_header: wrong size of payload, and I fogot to write length to bytes;
parent 54a476c1
...@@ -13,8 +13,11 @@ impl MacAddr { ...@@ -13,8 +13,11 @@ impl MacAddr {
impl MacAddr { impl MacAddr {
pub fn marshal(&self, bytes: &mut Vec<u8>) { pub fn marshal(&self, bytes: &mut Vec<u8>) {
for m in self.mac.iter() { for i in 0..6 {
bytes.write_u8(*m); let _ = bytes.write_u8(match self.mac.get(5 - i) {
Some(v) => *v,
None => 0,
});
} }
} }
} }
...@@ -23,8 +26,8 @@ impl From<MacAddr> for u64 { ...@@ -23,8 +26,8 @@ impl From<MacAddr> for u64 {
fn from(value: MacAddr) -> Self { fn from(value: MacAddr) -> Self {
let mut byte: u64 = 0; let mut byte: u64 = 0;
for i in 0..6 { for i in 0..6 {
byte = byte << 8; // byte = byte << 8;
byte += value.mac[i] as u64; byte += (value.mac[i] as u64) << i * 8;
} }
byte byte
} }
......
...@@ -87,7 +87,7 @@ where ...@@ -87,7 +87,7 @@ where
self.send_msg(self.ofp().fetures_req(), xid, stream); self.send_msg(self.ofp().fetures_req(), xid, stream);
} }
fn error_handler(&self, error: ErrorEvent) { fn error_handler(&self, error: ErrorEvent) {
println!("Error {:?}", error.error_type); println!("Error {:?} payload: {:x?}", error.error_type, error.payload);
} }
fn echo_request_handler(&self, xid: u32, echo: EchoRequestEvent, stream: &mut TcpStream) { fn echo_request_handler(&self, xid: u32, echo: EchoRequestEvent, stream: &mut TcpStream) {
self.send_msg(EchoReplyEvent::new(echo.payload), xid, stream); self.send_msg(EchoReplyEvent::new(echo.payload), xid, stream);
......
...@@ -101,55 +101,61 @@ impl SetField { ...@@ -101,55 +101,61 @@ impl SetField {
pub fn marshal(&self, bytes: &mut Vec<u8>) { pub fn marshal(&self, bytes: &mut Vec<u8>) {
match &self { match &self {
SetField::InPort(port) => { SetField::InPort(port) => {
OxmHeader::new(OxmMatchFields::InPort, 4).marshal(bytes); OxmHeader::new(OxmMatchFields::InPort, 4, false).marshal(bytes);
port.marshal(bytes); port.marshal(bytes);
} }
SetField::EthDst(mac) => { SetField::EthDst(mac) => {
OxmHeader::new(OxmMatchFields::EthDst, 6).marshal(bytes); OxmHeader::new(OxmMatchFields::EthDst, 12, true).marshal(bytes);
mac.marshal(bytes); mac.marshal(bytes);
MacAddr::from(!0).marshal(bytes);
} }
SetField::EthSrc(mac) => { SetField::EthSrc(mac) => {
OxmHeader::new(OxmMatchFields::EthSrc, 6); OxmHeader::new(OxmMatchFields::EthSrc, 12, true).marshal(bytes);
mac.marshal(bytes); mac.marshal(bytes);
MacAddr::from(!0).marshal(bytes);
} }
SetField::EthTyp(eth) => { SetField::EthTyp(eth) => {
OxmHeader::new(OxmMatchFields::EthType, 2).marshal(bytes); OxmHeader::new(OxmMatchFields::EthType, 2, false).marshal(bytes);
bytes.write_u16::<BigEndian>(*eth); bytes.write_u16::<BigEndian>(*eth);
} }
SetField::IpProto(proto) => { SetField::IpProto(proto) => {
OxmHeader::new(OxmMatchFields::IpProto, 1).marshal(bytes); OxmHeader::new(OxmMatchFields::IpProto, 1, false).marshal(bytes);
bytes.write_u8(*proto); bytes.write_u8(*proto);
} }
SetField::Ipv4Src(ipv4) => { SetField::Ipv4Src(ipv4) => {
OxmHeader::new(OxmMatchFields::Ipv4Src, 4).marshal(bytes); OxmHeader::new(OxmMatchFields::Ipv4Src, 8, true).marshal(bytes);
bytes.write_u32::<BigEndian>(ipv4.clone().into()); bytes.write_u32::<BigEndian>(ipv4.clone().into());
bytes.write_u32::<BigEndian>(!0);
} }
SetField::Ipv4Dst(ipv4) => { SetField::Ipv4Dst(ipv4) => {
OxmHeader::new(OxmMatchFields::Ipv4Dst, 4).marshal(bytes); OxmHeader::new(OxmMatchFields::Ipv4Dst, 8, true).marshal(bytes);
bytes.write_u32::<BigEndian>(ipv4.clone().into()); bytes.write_u32::<BigEndian>(ipv4.clone().into());
bytes.write_u32::<BigEndian>(!0);
} }
SetField::Ipv6Src(ipv6) => { SetField::Ipv6Src(ipv6) => {
OxmHeader::new(OxmMatchFields::Ipv6Src, 16).marshal(bytes); OxmHeader::new(OxmMatchFields::Ipv6Src, 32, true).marshal(bytes);
bytes.write_u128::<BigEndian>(ipv6.clone().into()); bytes.write_u128::<BigEndian>(ipv6.clone().into());
bytes.write_u128::<BigEndian>(!0);
} }
SetField::Ipv6Dst(ipv6) => { SetField::Ipv6Dst(ipv6) => {
OxmHeader::new(OxmMatchFields::Ipv6Dst, 16).marshal(bytes); OxmHeader::new(OxmMatchFields::Ipv6Dst, 32, true).marshal(bytes);
bytes.write_u128::<BigEndian>(ipv6.clone().into()); bytes.write_u128::<BigEndian>(ipv6.clone().into());
bytes.write_u128::<BigEndian>(!0);
} }
SetField::TcpSrc(tcp) => { SetField::TcpSrc(tcp) => {
OxmHeader::new(OxmMatchFields::TcpSrc, 2).marshal(bytes); OxmHeader::new(OxmMatchFields::TcpSrc, 2, false).marshal(bytes);
bytes.write_u16::<BigEndian>(*tcp); bytes.write_u16::<BigEndian>(*tcp);
} }
SetField::TcpDst(tcp) => { SetField::TcpDst(tcp) => {
OxmHeader::new(OxmMatchFields::TcpDst, 2).marshal(bytes); OxmHeader::new(OxmMatchFields::TcpDst, 2, false).marshal(bytes);
bytes.write_u16::<BigEndian>(*tcp); bytes.write_u16::<BigEndian>(*tcp);
} }
SetField::UdpSrc(udp) => { SetField::UdpSrc(udp) => {
OxmHeader::new(OxmMatchFields::UdpSrc, 2).marshal(bytes); OxmHeader::new(OxmMatchFields::UdpSrc, 2, false).marshal(bytes);
bytes.write_u16::<BigEndian>(*udp); bytes.write_u16::<BigEndian>(*udp);
} }
SetField::UdpDst(udp) => { SetField::UdpDst(udp) => {
OxmHeader::new(OxmMatchFields::UdpDst, 2).marshal(bytes); OxmHeader::new(OxmMatchFields::UdpDst, 2, false).marshal(bytes);
bytes.write_u16::<BigEndian>(*udp); bytes.write_u16::<BigEndian>(*udp);
} }
} }
......
...@@ -33,8 +33,8 @@ impl OfpMatch { ...@@ -33,8 +33,8 @@ impl OfpMatch {
pub fn marshal(&self, bytes: &mut Vec<u8>) { pub fn marshal(&self, bytes: &mut Vec<u8>) {
bytes.write_u16::<BigEndian>(self.typ.clone().into()); bytes.write_u16::<BigEndian>(self.typ.clone().into());
bytes.write_u16::<BigEndian>(self.length + (self.oxm_fields.len() as u16)); bytes.write_u16::<BigEndian>(self.length + (self.oxm_fields.len() as u16));
bytes.write_u32::<BigEndian>(0);
bytes.append(&mut self.oxm_fields.clone()); bytes.append(&mut self.oxm_fields.clone());
bytes.write_u32::<BigEndian>(0);
} }
} }
...@@ -82,12 +82,12 @@ pub struct OxmHeader { ...@@ -82,12 +82,12 @@ pub struct OxmHeader {
} }
impl OxmHeader { impl OxmHeader {
pub fn new(field: OxmMatchFields, size: u8) -> Self { pub fn new(field: OxmMatchFields, size: u8, hasmask: bool) -> Self {
Self { Self {
class: OxmClass::OpenflowBasic, class: OxmClass::OpenflowBasic,
field, field,
hasmask: false, hasmask: hasmask,
length: 4 + size, length: size,
experimenter: None, experimenter: None,
} }
} }
...@@ -95,6 +95,10 @@ impl OxmHeader { ...@@ -95,6 +95,10 @@ impl OxmHeader {
bytes.write_u16::<BigEndian>(self.class.clone().into()); bytes.write_u16::<BigEndian>(self.class.clone().into());
let field: u8 = self.field.clone().into(); let field: u8 = self.field.clone().into();
bytes.write_u8(field << 1 | if self.hasmask { 1 } else { 0 }); bytes.write_u8(field << 1 | if self.hasmask { 1 } else { 0 });
bytes.write_u8(self.length);
// if let Some(exp) = self.experimenter {
// bytes.write_u32::<BigEndian>(exp);
// }
} }
} }
...@@ -222,58 +226,66 @@ impl MatchFields { ...@@ -222,58 +226,66 @@ impl MatchFields {
let ofp_byte = ofp_match.oxm_fields.as_mut(); let ofp_byte = ofp_match.oxm_fields.as_mut();
if let Some(in_port) = &self.in_port { if let Some(in_port) = &self.in_port {
let header = OxmHeader::new(OxmMatchFields::InPort, 4); let header = OxmHeader::new(OxmMatchFields::InPort, 4, false);
header.marshal(ofp_byte); header.marshal(ofp_byte);
ofp_byte.write_u32::<BigEndian>(*in_port); ofp_byte.write_u32::<BigEndian>(*in_port);
} }
if let Some(eth_dst) = &self.eth_dst { if let Some(eth_dst) = &self.eth_dst {
let header = OxmHeader::new(OxmMatchFields::EthDst, 6); let header = OxmHeader::new(OxmMatchFields::EthDst, 12, true);
header.marshal(ofp_byte); header.marshal(ofp_byte);
eth_dst.marshal(ofp_byte); eth_dst.marshal(ofp_byte);
// mac mask
MacAddr::from(!0).marshal(ofp_byte);
} }
if let Some(eth_src) = &self.eth_src { if let Some(eth_src) = &self.eth_src {
let header = OxmHeader::new(OxmMatchFields::EthSrc, 6); let header = OxmHeader::new(OxmMatchFields::EthSrc, 12, true);
header.marshal(ofp_byte); header.marshal(ofp_byte);
eth_src.marshal(ofp_byte); eth_src.marshal(ofp_byte);
// mac mask
MacAddr::from(!0).marshal(ofp_byte);
} }
if let Some(eth_typ) = &self.eth_typ { if let Some(eth_typ) = &self.eth_typ {
OxmHeader::new(OxmMatchFields::EthType, 2).marshal(ofp_byte); OxmHeader::new(OxmMatchFields::EthType, 2, false).marshal(ofp_byte);
ofp_byte.write_u16::<BigEndian>(*eth_typ); ofp_byte.write_u16::<BigEndian>(*eth_typ);
} }
if let Some(ip_proto) = &self.ip_proto { if let Some(ip_proto) = &self.ip_proto {
OxmHeader::new(OxmMatchFields::IpProto, 1); OxmHeader::new(OxmMatchFields::IpProto, 1, false).marshal(ofp_byte);
ofp_byte.write_u8(*ip_proto); ofp_byte.write_u8(*ip_proto);
} }
if let Some(ipv4_src) = &self.ipv4_src { if let Some(ipv4_src) = &self.ipv4_src {
OxmHeader::new(OxmMatchFields::Ipv4Src, 4).marshal(ofp_byte); OxmHeader::new(OxmMatchFields::Ipv4Src, 8, true).marshal(ofp_byte);
bytes.write_u32::<BigEndian>(ipv4_src.clone().into()); bytes.write_u32::<BigEndian>(ipv4_src.clone().into());
bytes.write_u32::<BigEndian>(!0);
} }
if let Some(ipv4_dst) = &self.ipv4_dst { if let Some(ipv4_dst) = &self.ipv4_dst {
OxmHeader::new(OxmMatchFields::Ipv4Dst, 4).marshal(ofp_byte); OxmHeader::new(OxmMatchFields::Ipv4Dst, 8, true).marshal(ofp_byte);
bytes.write_u32::<BigEndian>(ipv4_dst.clone().into()); bytes.write_u32::<BigEndian>(ipv4_dst.clone().into());
bytes.write_u32::<BigEndian>(!0);
} }
if let Some(ipv6_src) = &self.ipv6_src { if let Some(ipv6_src) = &self.ipv6_src {
OxmHeader::new(OxmMatchFields::Ipv6Src, 16); OxmHeader::new(OxmMatchFields::Ipv6Src, 32, true).marshal(ofp_byte);
ofp_byte.write_u128::<BigEndian>(ipv6_src.clone().into()); ofp_byte.write_u128::<BigEndian>(ipv6_src.clone().into());
ofp_byte.write_u128::<BigEndian>(!0);
} }
if let Some(ipv6_dst) = &self.ipv6_dst { if let Some(ipv6_dst) = &self.ipv6_dst {
OxmHeader::new(OxmMatchFields::Ipv6Dst, 16); OxmHeader::new(OxmMatchFields::Ipv6Dst, 32, true).marshal(ofp_byte);
ofp_byte.write_u128::<BigEndian>(ipv6_dst.clone().into()); ofp_byte.write_u128::<BigEndian>(ipv6_dst.clone().into());
ofp_byte.write_u128::<BigEndian>(!0);
} }
if let Some(tcp_src) = &self.tcp_src { if let Some(tcp_src) = &self.tcp_src {
OxmHeader::new(OxmMatchFields::TcpSrc, 2); OxmHeader::new(OxmMatchFields::TcpSrc, 2, false);
ofp_byte.write_u16::<BigEndian>(*tcp_src); ofp_byte.write_u16::<BigEndian>(*tcp_src);
} }
if let Some(tcp_dst) = &self.tcp_dst { if let Some(tcp_dst) = &self.tcp_dst {
OxmHeader::new(OxmMatchFields::TcpDst, 2); OxmHeader::new(OxmMatchFields::TcpDst, 2, false);
ofp_byte.write_u16::<BigEndian>(*tcp_dst); ofp_byte.write_u16::<BigEndian>(*tcp_dst);
} }
if let Some(udp_src) = &self.udp_src { if let Some(udp_src) = &self.udp_src {
OxmHeader::new(OxmMatchFields::UdpSrc, 2); OxmHeader::new(OxmMatchFields::UdpSrc, 2, false);
ofp_byte.write_u16::<BigEndian>(*udp_src); ofp_byte.write_u16::<BigEndian>(*udp_src);
} }
if let Some(udp_dst) = &self.udp_dst { if let Some(udp_dst) = &self.udp_dst {
OxmHeader::new(OxmMatchFields::UdpDst, 2); OxmHeader::new(OxmMatchFields::UdpDst, 2, false);
ofp_byte.write_u16::<BigEndian>(*udp_dst); ofp_byte.write_u16::<BigEndian>(*udp_dst);
} }
ofp_match.marshal(bytes); ofp_match.marshal(bytes);
......
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