-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new example
MultiLearningSwitch
.
- Loading branch information
Showing
7 changed files
with
154 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
# Change log | ||
|
||
## develop (unreleased) | ||
### New features | ||
* Add new example `MultiLearningSwitch`. | ||
|
||
|
||
## 0.1.0 (2/9/2015) | ||
|
||
### Misc | ||
* The initial release version of hello_trema that runs on [pure-Ruby Trema](https://github.com/trema/trema_ruby). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
Feature: "Multi Learning Switch" example | ||
Background: | ||
Given a file named ".trema/config" with: | ||
""" | ||
LOG_DIR: . | ||
PID_DIR: . | ||
SOCKET_DIR: . | ||
""" | ||
Given a file named "trema.conf" with: | ||
""" | ||
vswitch('lsw1') { datapath_id '0x1' } | ||
vswitch('lsw2') { datapath_id '0x2' } | ||
vswitch('lsw3') { datapath_id '0x3' } | ||
vswitch('lsw4') { datapath_id '0x4' } | ||
vhost('host1') | ||
vhost('host2') | ||
vhost('host3') | ||
vhost('host4') | ||
link 'lsw1', 'host1' | ||
link 'lsw2', 'host2' | ||
link 'lsw3', 'host3' | ||
link 'lsw4', 'host4' | ||
link 'lsw1', 'lsw2' | ||
link 'lsw2', 'lsw3' | ||
link 'lsw3', 'lsw4' | ||
""" | ||
|
||
Scenario: Run | ||
Given I successfully run `trema run ../../lib/multi_learning_switch.rb -c trema.conf -d` | ||
And I run `sleep 10` | ||
When I successfully run `trema send_packets --source host1 --dest host2 --n_pkts 2` | ||
Then the total number of tx packets should be: | ||
| host1 | host2 | host3 | host4 | | ||
| 2 | 0 | 0 | 0 | | ||
And the total number of rx packets should be: | ||
| host1 | host2 | host3 | host4 | | ||
| 0 | 2 | 0 | 0 | | ||
When I successfully run `trema send_packets --source host3 --dest host4 --n_pkts 3` | ||
Then the total number of tx packets should be: | ||
| host1 | host2 | host3 | host4 | | ||
| 2 | 0 | 3 | 0 | | ||
And the total number of rx packets should be: | ||
| host1 | host2 | host3 | host4 | | ||
| 0 | 2 | 0 | 3 | | ||
When I successfully run `trema send_packets --source host4 --dest host1 --n_pkts 2` | ||
Then the total number of tx packets should be: | ||
| host1 | host2 | host3 | host4 | | ||
| 2 | 0 | 3 | 2 | | ||
And the total number of rx packets should be: | ||
| host1 | host2 | host3 | host4 | | ||
| 2 | 2 | 0 | 3 | | ||
When I successfully run `trema send_packets --source host2 --dest host3 --n_pkts 4` | ||
Then the total number of tx packets should be: | ||
| host1 | host2 | host3 | host4 | | ||
| 2 | 4 | 3 | 2 | | ||
And the total number of rx packets should be: | ||
| host1 | host2 | host3 | host4 | | ||
| 2 | 2 | 4 | 3 | | ||
When I successfully run `trema send_packets --source host1 --dest host4 --n_pkts 1` | ||
Then the total number of tx packets should be: | ||
| host1 | host2 | host3 | host4 | | ||
| 3 | 4 | 3 | 2 | | ||
And the total number of rx packets should be: | ||
| host1 | host2 | host3 | host4 | | ||
| 2 | 2 | 4 | 4 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
$LOAD_PATH.unshift __dir__ | ||
|
||
require 'fdb' | ||
|
||
# An OpenFlow controller that emulates multiple layer-2 switches. | ||
class MultiLearningSwitch < Trema::Controller | ||
timer_event :age_fdbs, interval: 5.sec | ||
|
||
def start(_argv) | ||
@fdbs = {} | ||
end | ||
|
||
def switch_ready(datapath_id) | ||
@fdbs[datapath_id] = FDB.new | ||
end | ||
|
||
def packet_in(datapath_id, message) | ||
return if message.destination_mac.reserved? | ||
@fdbs.fetch(datapath_id).learn(message.source_mac, message.in_port) | ||
flow_mod_and_packet_out message | ||
end | ||
|
||
def age_fdbs | ||
@fdbs.each_value(&:age) | ||
end | ||
|
||
private | ||
|
||
def flow_mod_and_packet_out(message) | ||
port_no = @fdbs.fetch(message.dpid).lookup(message.destination_mac) | ||
flow_mod(message, port_no) if port_no | ||
packet_out(message, port_no || :flood) | ||
end | ||
|
||
def flow_mod(message, port_no) | ||
send_flow_mod_add( | ||
message.datapath_id, | ||
match: ExactMatch.new(message), | ||
actions: SendOutPort.new(port_no) | ||
) | ||
end | ||
|
||
def packet_out(message, port_no) | ||
send_packet_out( | ||
message.datapath_id, | ||
packet_in: message, | ||
actions: SendOutPort.new(port_no) | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
vswitch("lsw1") { datapath_id "0x1" } | ||
vswitch("lsw2") { datapath_id "0x2" } | ||
vswitch("lsw3") { datapath_id "0x3" } | ||
vswitch("lsw4") { datapath_id "0x4" } | ||
|
||
vhost("host1") | ||
vhost("host2") | ||
vhost("host3") | ||
vhost("host4") | ||
|
||
link "lsw1", "host1" | ||
link "lsw2", "host2" | ||
link "lsw3", "host3" | ||
link "lsw4", "host4" | ||
link "lsw1", "lsw2" | ||
link "lsw2", "lsw3" | ||
link "lsw3", "lsw4" |