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