Skip to content

Commit

Permalink
setup and send flow_mod
Browse files Browse the repository at this point in the history
  • Loading branch information
Arikato111 committed May 11, 2024
1 parent 6140181 commit 2e236af
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#![allow(unused)]
#![allow(unused_variables)]
use crate::openflow::events::flow_mod::{FlowModCommand, MatchFields};
/**
* Here is Controller you can modify and write the process or more you need.
* In production please remove allow unused.
*/
use crate::openflow::events::PacketInEvent;
use crate::openflow::events::{FlowAction, FlowModEvent, PacketInEvent};
use crate::openflow::PseudoPort;
use crate::openflow::{controller_frame::ControllerFrame, traiter::OfpMsgEvent};
use std::{collections::HashMap, net::TcpStream};

Expand All @@ -26,5 +28,42 @@ impl<OME: OfpMsgEvent> ControllerFrame<OME> for Controller<OME> {
/**
* Start here for handle packetIn message.
*/
fn packet_in_handler(&mut self, xid: u32, packetin: PacketInEvent, stream: &mut TcpStream) {}
fn packet_in_handler(&mut self, xid: u32, packetin: PacketInEvent, stream: &mut TcpStream) {
let mac_dst = packetin.payload.mac_des;
let mac_src = packetin.payload.mac_src;
let out_port = self.mac_to_port.get(&mac_dst);
match out_port {
Some(p) => {
let src_port = packetin.port;
let mut src_dst_match = MatchFields::match_all();
src_dst_match.mac_dest = Some(mac_dst);
src_dst_match.mac_src = Some(mac_src);

let mut dst_src_match = MatchFields::match_all();
dst_src_match.mac_dest = Some(mac_src);
dst_src_match.mac_src = Some(mac_dst);

let actions = vec![FlowAction::Oputput(PseudoPort::PhysicalPort(*p))];
self.add_flow(xid, src_dst_match, actions, stream);

let actions = vec![FlowAction::Oputput(PseudoPort::PhysicalPort(src_port))];
self.add_flow(xid, dst_src_match, actions, stream);

// let pkt_out =
}
None => todo!(),
}
}
}

impl<OME: OfpMsgEvent> Controller<OME> {
fn add_flow(
&self,
xid: u32,
flow: MatchFields,
actions: Vec<FlowAction>,
stream: &mut TcpStream,
) {
self.send_msg(FlowModEvent::add_flow(10, flow, actions), xid, stream)
}
}
15 changes: 15 additions & 0 deletions src/openflow/events/flow_mod/flow_mod_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ pub struct FlowModEvent {
}

impl FlowModEvent {
pub fn add_flow(priority: u16, match_fileds: MatchFields, actions: Vec<FlowAction>) -> Self {
Self {
command: FlowModCommand::Add,
match_fields: match_fileds,
priority,
actions,
cookie: 0,
idle_timeout: Timeout::Permanent,
hard_timeout: Timeout::Permanent,
notify_when_removed: false,
apply_to_packet: None,
out_port: None,
check_overlap: false,
}
}
pub fn get_controller_last(&self) -> Vec<FlowAction> {
let mut not_ctrl: Vec<FlowAction> = Vec::new();
let mut is_ctrl: Vec<FlowAction> = Vec::new();
Expand Down
16 changes: 16 additions & 0 deletions src/openflow/events/flow_mod/match_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ pub struct MatchFields {
}

impl MatchFields {
pub fn match_all() -> Self {
Self {
ethernet_type: None,
in_port: None,
ip_dest: None,
ip_src: None,
mac_dest: None,
mac_src: None,
protocol: None,
tos: None,
transport_dest: None,
transport_src: None,
vlan_pcp: None,
vlan_vid: None,
}
}
pub fn marshal(&self, bytes: &mut Vec<u8>) {
let mut match_f = 0u32;
match_f = set_bit(match_f, 0, self.in_port.is_some());
Expand Down

0 comments on commit 2e236af

Please sign in to comment.