POX Python Codes
POX Python Codes
py
#!/usr/bin/python
# Mininet Example Copyright 2012 William Yu
# [email protected]
from mininet.net import Mininet
from mininet.cli import CLI
net = Mininet()
# Creating nodes in the network.
c0 = net.addController()
h0 = net.addHost('h0')
s0 = net.addSwitch('s0')
h1 = net.addHost('h1')
# Creating links between nodes in network
net.addLink(h0, s0)
net.addLink(h1, s0)
# Configuration of IP addresses in interfaces
h0.setIP('192.168.1.1', 24)
h1.setIP('192.168.1.2', 24)
net.start()
net.pingAll()
CLI(net)
net.stop()
Fanout.py
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/python
# Mininet Example Copyright 2012 William Yu
# [email protected]
from mininet.net import Mininet
from mininet.topolib import TreeTopo
Tree22 = TreeTopo(depth=2, fanout=2)
net = Mininet(topo=Tree22)
net.start()
net.pingAll()
net.stop()
Linear.py
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/python
# Mininet Example Copyright 2012 William Yu
# [email protected]
from mininet.net import Mininet
from mininet.topo import LinearTopo
Linear4 = LinearTopo(k=4)
net = Mininet(topo=Linear4)
net.start()
net.pingAll()
net.stop()
Perf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/python
# Mininet Example Copyright 2012 William Yu
# [email protected]
from mininet.net import Mininet
from mininet.node import CPULimitedHost
from mininet.link import TCLink
from mininet.cli import CLI
net = Mininet(host=CPULimitedHost, link=TCLink)
c0
s0
h0
h1
h2
= net.addController()
= net.addSwitch('s0')
= net.addHost('h0', cpu=0.5)
= net.addHost('h1')
= net.addHost('h2')
Single.py
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/python
# Mininet Example Copyright 2012 William Yu
# [email protected]
from mininet.net import Mininet
from mininet.topo import SingleSwitchTopo
Single3 = SingleSwitchTopo(k=3)
net = Mininet(topo=Single3)
net.start()
net.pingAll()
net.stop()
Flowstats.py
1 #!/usr/bin/python
2 # Copyright 2012 William Yu
3 # [email protected]
4 #
5 # This file is part of POX.
6 #
7 # POX is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# POX is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with POX. If not, see <http://www.gnu.org/licenses/>.
19#
20
21"""
22This is a demonstration file created to show how to obtain flow
23and port statistics from OpenFlow 1.0-enabled switches. The flow
24statistics handler contains a summary of web-only traffic.
25"""
26
27# standard includes
28from pox.core import core
29from pox.lib.util import dpidToStr
30import pox.openflow.libopenflow_01 as of
31
32# include as part of the betta branch
33from pox.openflow.of_json import *
34
35log = core.getLogger()
36
37# handler for timer function that sends the requests to all the
38# switches connected to the controller.
39def _timer_func ():
40 for connection in core.openflow._connections.values():
connection.send(of.ofp_stats_request(body=of.ofp_flow_stats_request()))
41
connection.send(of.ofp_stats_request(body=of.ofp_port_stats_request()))
42
43 log.debug("Sent %i flow/port stats request(s)", len(core.openflow._connections))
44
45# handler to display flow statistics received in JSON format
46# structure of event.stats is defined by ofp_flow_stats()
47def _handle_flowstats_received (event):
48 stats = flow_stats_to_list(event.stats)
49 log.debug("FlowStatsReceived from %s: %s",
dpidToStr(event.connection.dpid), stats)
50
51
52 # Get number of bytes/packets in flows for web traffic only
53 web_bytes = 0
54 web_flows = 0
55 web_packet = 0
56 for f in event.stats:
if f.match.tp_dst == 80 or f.match.tp_src == 80:
57
web_bytes += f.byte_count
58
web_packet += f.packet_count
59
web_flows += 1
60
61 log.info("Web traffic from %s: %s bytes (%s packets) over %s flows",
dpidToStr(event.connection.dpid), web_bytes, web_packet, web_flows)
62
63
Of_double_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
# Copyright 2012 William Yu
# [email protected]
#
from mininet.net import Mininet
from mininet.node import Controller, OVSKernelSwitch, RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.util import createLink
def createDoubleControllerNetwork():
info( '*** Creating network for Double Controller Example\n' )
# Create an empty network.
net = Mininet(switch=OVSKernelSwitch)
c0 = net.addController('c0', controller=RemoteController,
defaultIP="127.0.0.1", port=6633)
c1 = net.addController('c1', controller=RemoteController,
defaultIP="127.0.0.1", port=6644)
# Creating nodes in the network.
h0 = net.addHost('h0')
h1 = net.addHost('h1')
s0 = net.addSwitch('s0')
h2 = net.addHost('h2')
h3 = net.addHost('h3')
s1 = net.addSwitch('s1')
# Creating links between nodes in network.
h0int, s0int = createLink(h0, s0)
h1int, s0int = createLink(h1, s0)
h2int, s1int = createLink(h2, s1)
h3int, s1int = createLink(h3, s1)
s0int, s1int = createLink(s0, s1)
# Configuration
h0.setIP(h0int,
h1.setIP(h1int,
h2.setIP(h2int,
h3.setIP(h3int,
of IP addresses in interfaces
'192.168.1.2', 26)
'192.168.1.3', 26)
'192.168.1.66', 26)
'192.168.1.67', 26)
# Start network
net.build()
# Attaching Controllers to Switches
s0.start([c0])
s1.start([c1])
# Setting interface only routes and not default routes
h0.cmd("route del -net 0.0.0.0")
h1.cmd("route del -net 0.0.0.0")
h2.cmd("route del -net 0.0.0.0")
h3.cmd("route del -net 0.0.0.0")
h0.cmd("route add -net 192.168.1.0 netmask 255.255.255.192 " + h0int)
h1.cmd("route add -net 192.168.1.0 netmask 255.255.255.192 " + h1int)
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Of_firewall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
# Copyright 2012 William Yu
# [email protected]
#
# This file is part of POX.
#
# POX is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# POX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with POX. If not, see <http://www.gnu.org/licenses/>.
#
# NOISY FIREWALL. This is a simple firewall implementation for demonstration
# purposes. This firewall is bad because only flows for valid packets are
# installed. Non-matches always trigger a PacketIn().
#
# This is a demonstration file aims to build a firewall. In this demo,
# firewall rules are applied to specific ports in the switch using the
# following commands:
#
AddRule (event, dl_type=0x800, nw_proto=1, port=0, src_port=of.OFPP_ALL)
#
DeleteRule (event, dl_type=0x800, nw_proto=1, port=0, src_port=of.OFPP_ALL):
#
ShowRule ()
#
# Mininet Command Line: sudo mn --topo single,3 --mac --switch ovsk --controller remote
# Command Line: ./pox.py py log.level --DEBUG samples.of_firewall
#
# THIS VERSION SUPPORT resend() functionality in the betta branch POX.
#
# These next two imports are common POX convention
from pox.core import core
from pox.lib.util import dpidToStr
import pox.openflow.libopenflow_01 as of
from pox.lib.packet.ethernet import ethernet
# Even a simple usage of the logger is much nicer than print!
log = core.getLogger()
# This table maps (switch,MAC-addr) pairs to the port on 'switch' at
# which we last saw a packet *from* 'MAC-addr'.
# (In this case, we use a Connection object for the switch.)
table = {}
# This table contains the firewall rules:
# firewall[(switch, dl_type, nw_proto, port, src_port)] = TRUE/FALSE
#
# Our firewall only supports inbound rule enforcement per port only.
# By default, this is empty.
#
Sample dl_type(s): IP (0x800)
#
Sample nw_proto(s): ICMP (1), TCP (6), UDP (17)
#
firewall = {}
# function that allows adding firewall rules into the firewall table
def AddRule (event, dl_type=0x800, nw_proto=1, port=0, src_port=of.OFPP_ALL):
firewall[(event.connection,dl_type,nw_proto,port,src_port)]=True
log.debug("Adding firewall rule to %s: %s %s %s %s" %
dpidToStr(event.connection.dpid), dl_type, nw_proto, port, src_port)
# function that allows deleting firewall rules from the firewall table
def DeleteRule (event, dl_type=0x800, nw_proto=1, port=0, src_port=of.OFPP_ALL):
try:
del firewall[(event.connection,dl_type,nw_proto,port,src_port)]
log.debug("Deleting firewall rule in %s: %s %s %s %s" %
dpidToStr(event.connection.dpid), dl_type, nw_proto, port, src_port)
except KeyError:
log.error("Cannot find in %s: %s %s %s %s" %
dpidToStr(event.connection.dpid), dl_type, nw_proto, port, src_port)
# function to display firewall rules
def ShowRules ():
for key in firewall:
log.info("Rule %s defined" % key)
# function to handle all housekeeping items when firewall starts
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
Of_router_topo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python
# Copyright 2012 William Yu
# [email protected]
#
# Sample network for creating an OpenFlow Static Router.
#
# This is a demonstration file aims to build a simple static router.
# Network A (192.168.1.0/26)
# <--> Router A (192.168.1.1, 192.168.1.129)
# <--> Router B (192.168.1.65, 192.168.1.130)
# <--> Network B (192.168.1.64/26)
#
from mininet.net import Mininet
from mininet.node import Controller, OVSKernelSwitch, RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.util import createLink
def createStaticRouterNetwork():
info( '*** Creating network for Static Router Example\n' )
# Create an empty network.
net = Mininet(controller=RemoteController, switch=OVSKernelSwitch)
net.addController('c0')
# Creating nodes in the network.
h0 = net.addHost('h0')
s0 = net.addSwitch('s0')
h1 = net.addHost('h1')
s1 = net.addSwitch('s1')
# Creating links between nodes in network.
h0int, s0int = createLink(h0, s0)
h1int, s1int = createLink(h1, s1)
s0pint, s1pint = createLink(s0, s1)
# Configuration of IP addresses in interfaces
s0.setIP(s0int, '192.168.1.1', 26)
h0.setIP(h0int, '192.168.1.2', 26)
s1.setIP(s1int, '192.168.1.65', 26)
h1.setIP(h1int, '192.168.1.66', 26)
s0.setIP(s0pint, '192.168.1.129', 26)
s1.setIP(s1pint, '192.168.1.130', 26)
info( '*** Network state:\n' )
for node in s0, s1, h0, h1:
info( str( node ) + '\n' )
# Start command line
51
52
53
54
55
56
57
58
net.start()
CLI(net)
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
createStaticRouterNetwork()
Of_simple_switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/python
# Copyright 2012 William Yu
# [email protected]
#
# Sample network for creating an OpenFlow Static Router.
#
# This is a demonstration file aims to build a simple static router.
# Network A (192.168.1.0/26)
# <--> Switch
# <--> Network B (192.168.1.64/26)
#
from
from
from
from
from
def createStaticRouterNetwork():
info( '*** Creating network for Static Router Example\n' )
# Create an empty network.
net = Mininet(controller=RemoteController, switch=OVSKernelSwitch)
net.addController('c0')
# Creating nodes in the network.
h0 = net.addHost('h0')
s0 = net.addSwitch('s0')
h1 = net.addHost('h1')
# Creating links between nodes in network.
h0int, s0int = createLink(h0, s0)
h1int, s0int = createLink(h1, s0)
# Configuration of IP addresses in interfaces
h0.setIP(h0int, '192.168.1.2', 26)
h1.setIP(h1int, '192.168.1.66', 26)
info( '*** Network state:\n' )
for node in s0, h0, h1:
info( str( node ) + '\n' )
# Start command line
net.start()
CLI(net)
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
createStaticRouterNetwork()
46
Of_switch_flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
"""
This component is for use with the OpenFlow tutorial.
It acts as a simple hub, but can be modified to act like an L2
learning switch.
It's quite similar to the one for NOX.
"""
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#
#< Set other fields of flow_mod (timeouts? buffer_id?) >
#
#< Add an output action, and send -- similar to send_packet() >
#msg.in_port = packet_in.in_port
#if packet_in.buffer_id != -1 and packet_in.buffer_id is not None:
# We got a buffer ID from the switch; use that
# msg.buffer_id = packet_in.buffer_id
#else:
# No buffer ID from switch -- we got the raw data
# if packet_in.data is None:
# No raw_data specified -- nothing to send!
#
return
# msg.data = packet_in.data
# Add an action to send to the specified port
#action = of.ofp_action_output(port = self.mac_to_port[str(packet.dst)])
#msg.actions.append(action)
else:
# Flood the packet out everything but the input port
# This part looks familiar, right?
self.send_packet(packet_in.buffer_id, packet_in.data,
of.OFPP_FLOOD, packet_in.in_port)
def _handle_PortStatus (self, event):
"""
Returns on change of port status
"""
if event.added:
log.debug("HW Address %s to port %d added (%d).",
event.ofp.desc.hw_addr, event.port, event.dpid)
elif event.deleted:
log.debug("Port %d deleted (%d).",
event.port, event.dpid)
def _handle_PacketIn (self, event):
"""
Handles packet in messages from the switch.
"""
packet = event.parsed # This is the parsed packet data.
if not packet.parsed:
log.warning("Ignoring incomplete packet")
return
packet_in = event.ofp # The actual ofp_packet_in message.
# Comment out the following line and uncomment the one after
# when starting the exercise.
#self.act_like_hub(packet, packet_in)
self.act_like_switch(packet, packet_in)
197
198
199
200
Of_switch_tutorial1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python
# Copyright 2012 James McCauley, William Yu
# [email protected]
#
# This file is part of POX.
#
# POX is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# POX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with POX. If not, see <http://www.gnu.org/licenses/>.
#
"""
This is a demonstration file that has various switch implementations.
The first example is a basic "all match" switch followed by a
destination match, pair match then finally a more ideal pair match
switch.
Mininet: sudo mn --topo single,3 --mac --switch ovsk --controller remote
Command Line: ./pox.py log.level --DEBUG samples.of_sw_tutorial
"""
# These next two imports are common POX convention
from pox.core import core
import pox.openflow.libopenflow_01 as of
# Even a simple usage of the logger is much nicer than print!
log = core.getLogger()
# This table maps (switch,MAC-addr) pairs to the port on 'switch' at
# which we last saw a packet *from* 'MAC-addr'.
# (In this case, we use a Connection object for the switch.)
table = {}
# Method for just sending a packet to any port (broadcast by default)
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
msg.actions.append(of.ofp_action_output(port = dst_port))
event.connection.send(msg)
log.debug("Installing %s.%i -> %s.%i AND %s.%i -> %s.%i" %
(packet.dst, dst_port, packet.src, event.ofp.in_port,
packet.src, event.ofp.in_port, packet.dst, dst_port))
# function that is invoked upon load to ensure that listeners are
# registered appropriately. Uncomment the hub/switch you would like
# to test. Only one at a time please.
def launch ():
#core.openflow.addListenerByName("PacketIn", _handle_dumbhub_packetin)
#core.openflow.addListenerByName("PacketIn", _handle_pairhub_packetin)
#core.openflow.addListenerByName("PacketIn", _handle_lazyhub_packetin)
#core.openflow.addListenerByName("PacketIn", _handle_badswitch_packetin)
#core.openflow.addListenerByName("PacketIn", _handle_pairswitch_packetin)
core.openflow.addListenerByName("PacketIn",
_handle_idealpairswitch_packetin)
log.info("Switch Tutorial is running.")
Switch_tu
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/python
# Copyright 2012 James McCauley, William Yu
# [email protected]
#
# This file is part of POX.
#
# POX is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# POX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
# You should have received a copy of the GNU General Public License
# along with POX. If not, see <http://www.gnu.org/licenses/>.
"""
This is a demonstration file that has various switch implementations.
The first example is a basic "all match" switch followed by a
destination match, pair match then finally a more ideal pair match
switch.
Mininet Command: sudo mn --topo single,3 --mac
--switch ovsk
--controller remote
Command Line: ./pox.py py --completion
log.level --DEBUG
samples.of_sw_tutorial_oo
THIS VERSION SUPPORT resend() functionality in the betta branch POX.
Object-oriented version that allows user to switch switches via the
command line interface.
"""
# These next two imports are common POX convention
from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.lib.util import dpidToStr
# Even a simple usage of the logger is much nicer than print!
log = core.getLogger()
# Create the class to hold the switch tutorial implementations
class SwitchTutorial (object):
# This table maps (switch,MAC-addr) pairs to the port on 'switch' at
# which we last saw a packet *from* 'MAC-addr'.
# (In this case, we use a Connection object for the switch.)
table = {}
# Holds the object with the default switch
handlerName = 'SW_IDEALPAIRSWITCH'
# Holds the current active PacketIn listener object
listeners = None
# Constructor and sets default handler to Ideal Pair Switch
def __init__(self, handlerName = 'SW_IDEALPAIRSWITCH'):
log.debug("Initializing switch %s." % handlerName)
# Method for just sending a packet to any port (broadcast by default)
def send_packet(self, event, dst_port = of.OFPP_ALL):
msg = of.ofp_packet_out(in_port=event.ofp.in_port)
if event.ofp.buffer_id != -1 and event.ofp.buffer_id is not None:
msg.buffer_id = event.ofp.buffer_id
else:
if event.ofp.data:
return
msg.data = event.ofp.data
msg.actions.append(of.ofp_action_output(port = dst_port))
event.connection.send(msg)
# Optimal method for resending a packet
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
msg = of.ofp_flow_mod()
msg.idle_timeout = 10
msg.hard_timeout = 30
msg.match.dl_dst = packet.src
msg.actions.append(of.ofp_action_output(port = event.port))
event.connection.send(msg)
log.debug("Installing %s.%i -> %s.%i" %
("ff:ff:ff:ff:ff:ff", event.ofp.in_port, packet.src, event.port))
# determine if appropriate destination route is available
dst_port = self.table.get((event.connection,packet.dst))
if dst_port is None:
# We don't know where the destination is yet. So, we'll just
# send the packet out all ports (except the one it came in on!)
# and hope the destination is out there somewhere. :)
# To send out all ports, we can use either of the special ports
# OFPP_FLOOD or OFPP_ALL. We'd like to just use OFPP_FLOOD,
# but it's not clear if all switches support this. :(
self.resend_packet(event, of.OFPP_ALL)
log.debug("Broadcasting %s.%i -> %s.%i" %
(packet.src, event.ofp.in_port, packet.dst, of.OFPP_ALL))
else:
# This is the packet that just came in -- we want send the packet
# if we know the destination.
self.resend_packet(event, dst_port)
log.debug("Sending %s.%i -> %s.%i" %
(packet.src, event.ofp.in_port, packet.dst, dst_port))
# PAIR-WISE MATCH SWITCH Implementation
# This is an implementation of an pair match switch. This only matches
# source and destination MAC addresses. Whenever a new source
# destination MAC address is detected it then add a new flow
# identifying the source destination pair. The routing table is updated
# using the detected destination MAC address to the destination port.
def _handle_pairswitch_packetin (self, event):
packet = event.parsed
# Learn the source and fill up routing table
self.table[(event.connection,packet.src)] = event.port
dst_port = self.table.get((event.connection,packet.dst))
if dst_port is None:
# We don't know where the destination is yet. So, we'll just
# send the packet out all ports (except the one it came in on!)
# and hope the destination is out there somewhere. :)
# To send out all ports, we can use either of the special ports
# OFPP_FLOOD or OFPP_ALL. We'd like to just use OFPP_FLOOD,
# but it's not clear if all switches support this. :(
self.resend_packet(event, of.OFPP_ALL)
log.debug("Broadcasting %s.%i -> %s.%i" %
(packet.src, event.ofp.in_port, packet.dst, of.OFPP_ALL))
else:
# This is the packet that just came in -- we want to
# install the rule and also resend the packet.
msg = of.ofp_flow_mod()
msg.data = event.ofp
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
msg.idle_timeout = 10
msg.hard_timeout = 30
msg.match.dl_src = packet.src
msg.match.dl_dst = packet.dst
msg.actions.append(of.ofp_action_output(port = dst_port))
event.connection.send(msg)
log.debug("Installing %s.%i -> %s.%i" %
(packet.src, event.ofp.in_port, packet.dst, dst_port))
# SMARTER PAIR-WISE MATCH SWITCH Implementation
# This is an implementation of an ideal pair switch. This optimizes the
# previous example by adding both direction in one entry.
def _handle_idealpairswitch_packetin(self, event):
packet = event.parsed
# Learn the source and fill up routing table
self.table[(event.connection,packet.src)] = event.port
dst_port = self.table.get((event.connection,packet.dst))
if dst_port is None:
# We don't know where the destination is yet. So, we'll just
# send the packet out all ports (except the one it came in on!)
# and hope the destination is out there somewhere. :)
# To send out all ports, we can use either of the special ports
# OFPP_FLOOD or OFPP_ALL. We'd like to just use OFPP_FLOOD,
# but it's not clear if all switches support this. :(
self.resend_packet(event, of.OFPP_ALL)
log.debug("Broadcasting %s.%i -> %s.%i" %
(packet.src, event.ofp.in_port, packet.dst, of.OFPP_ALL))
else:
# Since we know the switch ports for both the source and dest
# MACs, we can install rules for both directions.
msg = of.ofp_flow_mod()
msg.idle_timeout = 10
msg.hard_timeout = 30
msg.match.dl_dst = packet.src
msg.match.dl_src = packet.dst
msg.actions.append(of.ofp_action_output(port = event.port))
event.connection.send(msg)
# This is the packet that just came in -- we want to
# install the rule and also resend the packet.
msg = of.ofp_flow_mod()
msg.data = event.ofp
msg.idle_timeout = 10
msg.hard_timeout = 30
msg.match.dl_src = packet.src
msg.match.dl_dst = packet.dst
msg.actions.append(of.ofp_action_output(port = dst_port))
event.connection.send(msg)
log.debug("Installing %s.%i -> %s.%i AND %s.%i -> %s.%i" %
(packet.dst, dst_port, packet.src, event.ofp.in_port,
packet.src, event.ofp.in_port, packet.dst, dst_port))
# Define the proper handler
def _set_handler_name (self, handlerName = 'SW_IDEALPAIRSWITCH'):
self.handlerName = handlerName
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
Refference:
http://nullege.com/codes/show/src@p@o@poxstuff-HEAD@of_sw_tutorial_oo.py