Commit 3ac60692 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

improve controller13, match_fields to public

parent 1291acdc
......@@ -3,7 +3,7 @@
use std::{collections::HashMap, net::TcpStream};
use crate::{
etherparser::ether_type::EtherType,
etherparser::{ether_type::EtherType, MacAddr},
openflow::ofp13::{
self,
events::{flow_mod::MatchFields, Action},
......@@ -16,12 +16,12 @@ use crate::{
* In production please remove allow unused.
*/
#[derive(Clone)]
pub struct Controller {
mac_to_port: HashMap<u64, u16>,
#[derive(Clone)]
pub struct Controller13 {
mac_to_port: HashMap<u64, u32>,
}
impl ControllerFrame13 for Controller {
impl ControllerFrame13 for Controller13 {
fn new() -> Self {
Self {
mac_to_port: HashMap::new(),
......@@ -35,14 +35,18 @@ impl ControllerFrame13 for Controller {
Ok(pkt) => pkt,
Err(_) => return,
};
let in_port = match packetin.matchs.in_port {
Some(p) => p,
None => return,
};
println!(
"packet in {} {} {}",
pkt.mac_src_string(),
pkt.mac_dst_string(),
packetin.in_port
in_port
);
self.mac_to_port.insert(pkt.mac_src, packetin.in_port);
self.mac_to_port.insert(pkt.mac_src, in_port);
let mac_dst = pkt.mac_dst;
let mac_src = pkt.mac_src;
......@@ -60,35 +64,52 @@ impl ControllerFrame13 for Controller {
if let ofp13::PseudoPort::PhysicalPort(_) = out_port {
let mut match_fields = MatchFields::match_all();
match_fields.in_port = Some(packetin.in_port);
match_fields.mac_dest = Some(mac_dst);
match_fields.mac_src = Some(mac_src);
match_fields.in_port = Some(in_port);
match_fields.eth_dst = Some(MacAddr::from(mac_dst));
match_fields.eth_src = Some(MacAddr::from(mac_src));
if let Some(buf_id) = packetin.buf_id {
self.add_flow(xid, 1, match_fields, &actions, Some(buf_id as u32), stream);
self.add_flow(
xid,
1,
match_fields,
&actions,
packetin.table_id,
Some(buf_id as u32),
stream,
);
return;
} else {
self.add_flow(xid, 1, match_fields, &actions, None, stream);
self.add_flow(
xid,
1,
match_fields,
&actions,
packetin.table_id,
None,
stream,
);
}
}
let packet_out = self
.ofp()
.packet_out(Some(packetin.in_port), packetin.payload, actions);
.packet_out(Some(in_port), packetin.payload, actions);
self.send_msg(packet_out, xid, stream);
}
}
impl Controller {
impl Controller13 {
fn add_flow(
&self,
xid: u32,
priority: u16,
flow: MatchFields,
actions: &Vec<Action>,
table_id: u8,
buffer_id: Option<u32>,
stream: &mut TcpStream,
) {
self.send_msg(
FlowModEvent::add_flow(10, flow, actions.clone(), buffer_id),
FlowModEvent::add_flow(10, flow, actions.clone(), table_id, buffer_id),
xid,
stream,
)
......
pub mod controller13;
pub use controller13::Controller13;
\ No newline at end of file
......@@ -2,5 +2,4 @@ pub mod openflow;
pub mod etherparser;
pub mod controller;
pub use controller::Controller;
\ No newline at end of file
pub mod example;
\ No newline at end of file
use tenjin::{openflow::ofp13::ControllerFrame13, Controller};
use tenjin::example;
use tenjin::openflow::ofp13::ControllerFrame13;
extern crate byteorder;
fn main() -> Result<(), std::io::Error> {
let controller = Controller::new();
let controller = example::Controller13::new();
controller.listener("127.0.0.1:6633");
Ok(())
}
......@@ -5,7 +5,7 @@ pub enum FlowModCommand {
ModifyStrict = 2,
Delete = 3,
DeleteStrict = 4,
Unparsable = 0xffff,
Unparsable = 0xff,
}
impl FlowModCommand {
......
......@@ -179,19 +179,19 @@ impl From<OxmMatchFields> for u8 {
// Required match fields.
pub struct MatchFields {
in_port: Option<u32>, // Ingress port. This may be a physical or switch-defined logical port.
eth_dst: Option<MacAddr>, // Ethernet source address. Can use arbitrary bitmask
eth_src: Option<MacAddr>, // Ethernet destination address. Can use arbitrary bitmask
eth_typ: Option<u16>, // Ethernet type of the OpenFlow packet payload, after VLAN tags.
ip_proto: Option<u8>, // IPv4 or IPv6 protocol number
ipv4_src: Option<Ipv4Addr>, // IPv4 source address. Can use subnet mask or arbitrary bitmask
ipv4_dst: Option<Ipv4Addr>, // IPv4 destination address. Can use subnet mask or arbitrary bitmask
ipv6_src: Option<Ipv6Addr>, // IPv6 source address. Can use subnet mask or arbitrary bitmask
ipv6_dst: Option<Ipv6Addr>, // IPv6 destination address. Can use subnet mask or arbitrary bitmask
tcp_src: Option<u16>, // TCP source port
tcp_dst: Option<u16>, // TCP destination port
udp_src: Option<u16>, // UDP source port
udp_dst: Option<u16>, // UDP destination port
pub in_port: Option<u32>, // Ingress port. This may be a physical or switch-defined logical port.
pub eth_dst: Option<MacAddr>, // Ethernet source address. Can use arbitrary bitmask
pub eth_src: Option<MacAddr>, // Ethernet destination address. Can use arbitrary bitmask
pub eth_typ: Option<u16>, // Ethernet type of the OpenFlow packet payload, after VLAN tags.
pub ip_proto: Option<u8>, // IPv4 or IPv6 protocol number
pub ipv4_src: Option<Ipv4Addr>, // IPv4 source address. Can use subnet mask or arbitrary bitmask
pub ipv4_dst: Option<Ipv4Addr>, // IPv4 destination address. Can use subnet mask or arbitrary bitmask
pub ipv6_src: Option<Ipv6Addr>, // IPv6 source address. Can use subnet mask or arbitrary bitmask
pub ipv6_dst: Option<Ipv6Addr>, // IPv6 destination address. Can use subnet mask or arbitrary bitmask
pub tcp_src: Option<u16>, // TCP source port
pub tcp_dst: Option<u16>, // TCP destination port
pub udp_src: Option<u16>, // UDP source port
pub udp_dst: Option<u16>, // UDP destination port
}
impl MatchFields {
......
......@@ -28,7 +28,7 @@ impl OfpMsgEvent for Openflow13 {
}
fn packet_out(
&self,
port_id: Option<u16>,
port_id: Option<u32>,
payload: Payload,
actions: Vec<Action>,
) -> PacketOutEvent {
......
......@@ -33,7 +33,7 @@ pub trait OfpMsgEvent {
fn fetures_req(&self) -> FeaturesReqEvent;
fn packet_out(
&self,
port_id: Option<u16>,
port_id: Option<u32>,
payload: Payload,
actions: Vec<Action>,
) -> PacketOutEvent;
......
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