Commit 8cb453e2 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

use MacAddr instread of u64;

parent b9221574
...@@ -5,13 +5,13 @@ use byteorder::{BigEndian, ReadBytesExt}; ...@@ -5,13 +5,13 @@ use byteorder::{BigEndian, ReadBytesExt};
use super::{ use super::{
packet::{ARP, IP}, packet::{ARP, IP},
tools::bits::mac_to_bytes, MacAddr,
}; };
pub struct EthernetFrame { pub struct EthernetFrame {
pub ether_type: EtherType, pub ether_type: EtherType,
pub mac_dst: u64, pub mac_dst: MacAddr,
pub mac_src: u64, pub mac_src: MacAddr,
pub vlan_pcp: u8, pub vlan_pcp: u8,
pub vlan_dei: bool, pub vlan_dei: bool,
pub vlan_vid: Option<u16>, pub vlan_vid: Option<u16>,
...@@ -20,10 +20,10 @@ pub struct EthernetFrame { ...@@ -20,10 +20,10 @@ pub struct EthernetFrame {
impl EthernetFrame { impl EthernetFrame {
pub fn mac_dst_string(&self) -> String { pub fn mac_dst_string(&self) -> String {
Self::mac_str(self.mac_dst) self.mac_dst.to_string()
} }
pub fn mac_src_string(&self) -> String { pub fn mac_src_string(&self) -> String {
Self::mac_str(self.mac_src) self.mac_src.to_string()
} }
pub fn mac_str(mac: u64) -> String { pub fn mac_str(mac: u64) -> String {
let mut mac_string = String::new(); let mut mac_string = String::new();
...@@ -76,8 +76,8 @@ impl EthernetFrame { ...@@ -76,8 +76,8 @@ impl EthernetFrame {
}; };
Ok(EthernetFrame { Ok(EthernetFrame {
ether_type: EtherType::parse(typ), ether_type: EtherType::parse(typ),
mac_dst: mac_to_bytes(mac_dst), mac_dst: MacAddr::new(mac_dst),
mac_src: mac_to_bytes(mac_src), mac_src: MacAddr::new(mac_src),
vlan_pcp, vlan_pcp,
vlan_dei, vlan_dei,
vlan_vid, vlan_vid,
......
use byteorder::WriteBytesExt; use byteorder::WriteBytesExt;
#[derive(Clone)] #[derive(Clone, Copy)]
pub struct MacAddr { pub struct MacAddr {
mac: [u8; 6], mac: [u8; 6],
} }
...@@ -12,12 +12,18 @@ impl MacAddr { ...@@ -12,12 +12,18 @@ impl MacAddr {
} }
impl MacAddr { impl MacAddr {
pub fn to_string(&self) -> String {
let mut mac_string = String::new();
for m in self.mac.iter() {
mac_string = format!("{}:{:02x}", mac_string, *m);
}
mac_string.pop();
mac_string
}
pub fn marshal(&self, bytes: &mut Vec<u8>) { pub fn marshal(&self, bytes: &mut Vec<u8>) {
for i in 0..6 { for m in self.mac.iter() {
let _ = bytes.write_u8(match self.mac.get(5 - i) { let _ = bytes.write_u8(*m);
Some(v) => *v,
None => 0,
});
} }
} }
} }
...@@ -25,9 +31,9 @@ impl MacAddr { ...@@ -25,9 +31,9 @@ impl MacAddr {
impl From<MacAddr> for u64 { 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 m in value.mac.iter() {
// byte = byte << 8; byte = byte << 8;
byte += (value.mac[i] as u64) << i * 8; byte += *m as u64;
} }
byte byte
} }
...@@ -39,6 +45,7 @@ impl From<u64> for MacAddr { ...@@ -39,6 +45,7 @@ impl From<u64> for MacAddr {
for i in 0..6 { for i in 0..6 {
mac[i] = (value >> (i * 8)) as u8; mac[i] = (value >> (i * 8)) as u8;
} }
mac.reverse();
Self { mac } Self { mac }
} }
} }
......
...@@ -24,20 +24,3 @@ pub fn set_bit(byte: u32, position: u32, set: bool) -> u32 { ...@@ -24,20 +24,3 @@ pub fn set_bit(byte: u32, position: u32, set: bool) -> u32 {
byte & !(1 << position) byte & !(1 << position)
} }
} }
pub fn mac_to_bytes(byte: [u8; 6]) -> u64 {
let mut addr = [0u8; 8];
for i in 2..8 {
addr[i] = byte[i - 2];
}
u64::from_be_bytes(addr)
}
pub fn bytes_to_mac(bytes: u64) -> [u8; 6] {
let mut address = [0; 6];
for i in 0..6 {
address[i] = ((bytes >> (8 * i)) & 0xff) as u8;
}
address.reverse();
address
}
...@@ -16,7 +16,7 @@ use crate::{ ...@@ -16,7 +16,7 @@ use crate::{
* In production please remove allow unused. * In production please remove allow unused.
*/ */
#[derive(Clone)] #[derive(Clone)]
pub struct Controller10 { pub struct Controller10 {
mac_to_port: HashMap<u64, u16>, mac_to_port: HashMap<u64, u16>,
} }
...@@ -42,7 +42,8 @@ impl ControllerFrame10 for Controller10 { ...@@ -42,7 +42,8 @@ impl ControllerFrame10 for Controller10 {
packetin.in_port packetin.in_port
); );
self.mac_to_port.insert(pkt.mac_src, packetin.in_port); self.mac_to_port
.insert(pkt.mac_src.into(), packetin.in_port);
let mac_dst = pkt.mac_dst; let mac_dst = pkt.mac_dst;
let mac_src = pkt.mac_src; let mac_src = pkt.mac_src;
...@@ -51,7 +52,7 @@ impl ControllerFrame10 for Controller10 { ...@@ -51,7 +52,7 @@ impl ControllerFrame10 for Controller10 {
return; return;
} }
let out_port = match self.mac_to_port.get(&mac_dst) { let out_port = match self.mac_to_port.get(&mac_dst.into()) {
Some(p) => ofp10::PseudoPort::PhysicalPort(*p), Some(p) => ofp10::PseudoPort::PhysicalPort(*p),
None => ofp10::PseudoPort::Flood, None => ofp10::PseudoPort::Flood,
}; };
...@@ -93,4 +94,4 @@ impl Controller10 { ...@@ -93,4 +94,4 @@ impl Controller10 {
stream, stream,
) )
} }
} }
\ No newline at end of file
...@@ -56,7 +56,7 @@ impl ControllerFrame13 for Controller13 { ...@@ -56,7 +56,7 @@ impl ControllerFrame13 for Controller13 {
in_port in_port
); );
self.mac_to_port.insert(pkt.mac_src, in_port); self.mac_to_port.insert(pkt.mac_src.into(), in_port);
let mac_dst = pkt.mac_dst; let mac_dst = pkt.mac_dst;
let mac_src = pkt.mac_src; let mac_src = pkt.mac_src;
...@@ -65,7 +65,7 @@ impl ControllerFrame13 for Controller13 { ...@@ -65,7 +65,7 @@ impl ControllerFrame13 for Controller13 {
return; return;
} }
let out_port = match self.mac_to_port.get(&mac_dst) { let out_port = match self.mac_to_port.get(&mac_dst.into()) {
Some(p) => ofp13::PseudoPort::PhysicalPort(*p), Some(p) => ofp13::PseudoPort::PhysicalPort(*p),
None => ofp13::PseudoPort::Flood, None => ofp13::PseudoPort::Flood,
}; };
......
...@@ -5,10 +5,7 @@ use std::{ ...@@ -5,10 +5,7 @@ use std::{
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use crate::{ use crate::{etherparser::MacAddr, openflow::ofp10::PseudoPort};
etherparser::tools::bits::{bytes_to_mac, mac_to_bytes},
openflow::ofp10::PseudoPort,
};
pub enum ActionType { pub enum ActionType {
Output = 0, Output = 0,
...@@ -30,8 +27,8 @@ pub enum Action { ...@@ -30,8 +27,8 @@ pub enum Action {
Oputput(PseudoPort), Oputput(PseudoPort),
SetDlVlan(Option<u16>), SetDlVlan(Option<u16>),
SetDlVlanPcp(u8), SetDlVlanPcp(u8),
SetDlSrc(u64), SetDlSrc(MacAddr),
SetDlDest(u64), SetDlDest(MacAddr),
SetIpSrc(u32), SetIpSrc(u32),
SetIpDes(u32), SetIpDes(u32),
SetTos(u8), SetTos(u8),
...@@ -102,13 +99,8 @@ impl Action { ...@@ -102,13 +99,8 @@ impl Action {
} }
} }
Action::SetDlSrc(mac) | Action::SetDlDest(mac) => { Action::SetDlSrc(mac) | Action::SetDlDest(mac) => {
let mac = bytes_to_mac(*mac); mac.marshal(bytes);
for m in mac { MacAddr::from(0).marshal(bytes);
let _ = bytes.write_u8(m);
}
for _ in 0..6 {
let _ = bytes.write_u8(0);
}
} }
Action::SetIpSrc(address) | Action::SetIpDes(address) => { Action::SetIpSrc(address) | Action::SetIpDes(address) => {
let _ = bytes.write_u32::<BigEndian>(*address); let _ = bytes.write_u32::<BigEndian>(*address);
...@@ -180,7 +172,7 @@ impl Action { ...@@ -180,7 +172,7 @@ impl Action {
addr[i] = bytes.read_u8()?; addr[i] = bytes.read_u8()?;
} }
bytes.consume(6); bytes.consume(6);
Ok(Action::SetDlSrc(mac_to_bytes(addr))) Ok(Action::SetDlSrc(MacAddr::new(addr)))
} }
t if t == (ActionType::SetDstMac as u16) => { t if t == (ActionType::SetDstMac as u16) => {
let mut addr = [0u8; 6]; let mut addr = [0u8; 6];
...@@ -188,7 +180,7 @@ impl Action { ...@@ -188,7 +180,7 @@ impl Action {
addr[i] = bytes.read_u8()?; addr[i] = bytes.read_u8()?;
} }
bytes.consume(6); bytes.consume(6);
Ok(Action::SetDlDest(mac_to_bytes(addr))) Ok(Action::SetDlDest(MacAddr::new(addr)))
} }
t if t == (ActionType::SetIPv4Src as u16) => { t if t == (ActionType::SetIPv4Src as u16) => {
Ok(Action::SetIpSrc(bytes.read_u32::<BigEndian>()?)) Ok(Action::SetIpSrc(bytes.read_u32::<BigEndian>()?))
......
...@@ -2,7 +2,10 @@ use std::io::{BufRead, Cursor, Error}; ...@@ -2,7 +2,10 @@ use std::io::{BufRead, Cursor, Error};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use crate::etherparser::tools::bits::{bit_bool, bytes_to_mac, mac_to_bytes, set_bit}; use crate::etherparser::{
tools::bits::{bit_bool, set_bit},
MacAddr,
};
pub struct Mask<T> { pub struct Mask<T> {
pub ip: T, pub ip: T,
...@@ -104,8 +107,8 @@ impl Wildcards { ...@@ -104,8 +107,8 @@ impl Wildcards {
pub struct MatchFields { pub struct MatchFields {
pub in_port: Option<u16>, pub in_port: Option<u16>,
pub mac_dest: Option<u64>, pub mac_dest: Option<MacAddr>,
pub mac_src: Option<u64>, pub mac_src: Option<MacAddr>,
pub ethernet_type: Option<u16>, pub ethernet_type: Option<u16>,
pub vlan_vid: Option<u16>, // vlan type pub vlan_vid: Option<u16>, // vlan type
...@@ -143,19 +146,13 @@ impl MatchFields { ...@@ -143,19 +146,13 @@ impl MatchFields {
Some(p) => p, Some(p) => p,
None => 0, None => 0,
}); });
let mac_src = match self.mac_src { match &self.mac_src {
Some(mac) => bytes_to_mac(mac), Some(mac) => mac.marshal(bytes),
None => bytes_to_mac(0), None => MacAddr::from(0).marshal(bytes),
};
for m in mac_src {
let _ = bytes.write_u8(m);
} }
let mac_dest = match self.mac_dest { match &self.mac_dest {
Some(mac) => bytes_to_mac(mac), Some(mac) => mac.marshal(bytes),
None => bytes_to_mac(0), None => MacAddr::from(0).marshal(bytes),
};
for m in mac_dest {
let _ = bytes.write_u8(m);
} }
let vlan = match self.vlan_vid { let vlan = match self.vlan_vid {
Some(v) => v, Some(v) => v,
...@@ -213,7 +210,7 @@ impl MatchFields { ...@@ -213,7 +210,7 @@ impl MatchFields {
for i in 0..6 { for i in 0..6 {
arr[i] = bytes.read_u8()?; arr[i] = bytes.read_u8()?;
} }
Some(mac_to_bytes(arr)) Some(MacAddr::new(arr))
}; };
let mac_dest = if wildcards.mac_dest { let mac_dest = if wildcards.mac_dest {
None None
...@@ -222,7 +219,7 @@ impl MatchFields { ...@@ -222,7 +219,7 @@ impl MatchFields {
for i in 0..6 { for i in 0..6 {
arr[i] = bytes.read_u8()?; arr[i] = bytes.read_u8()?;
} }
Some(mac_to_bytes(arr)) Some(MacAddr::new(arr))
}; };
let vlan_vid = if wildcards.vlan_vid { let vlan_vid = if wildcards.vlan_vid {
None None
...@@ -307,4 +304,4 @@ impl MatchFields { ...@@ -307,4 +304,4 @@ impl MatchFields {
transport_dest, transport_dest,
}) })
} }
} }
\ 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