A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
openflow-switch.py
Go to the documentation of this file.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License version 2 as
5# published by the Free Software Foundation;
6#
7# This program is distributed in the hope that it will be useful,
8# but WITHOUT ANY WARRANTY; without even the implied warranty of
9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10# GNU General Public License for more details.
11#
12# You should have received a copy of the GNU General Public License
13# along with this program; if not, write to the Free Software
14# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15#
16# Author: Blake Hurd <naimorai@gmail.com>
17# Modified by: Josh Pelkey <joshpelkey@gmail.com>
18# Gabriel Ferreira <gabrielcarvfer@gmail.com>
19#
20
21from ns import ns
22
23ns.LogComponentEnable("OpenFlowInterface", ns.LOG_LEVEL_ALL)
24ns.LogComponentEnable("OpenFlowSwitchNetDevice", ns.LOG_LEVEL_ALL)
25
26terminals = ns.NodeContainer()
27terminals.Create(4)
28
29csmaSwitch = ns.NodeContainer()
30csmaSwitch.Create(1)
31
32csma = ns.CsmaHelper()
33csma.SetChannelAttribute("DataRate", ns.DataRateValue(5000000))
34csma.SetChannelAttribute("Delay", ns.TimeValue(ns.MilliSeconds(2)))
35
36terminalDevices = ns.NetDeviceContainer()
37switchDevices = ns.NetDeviceContainer()
38for i in range(4):
39 container = ns.NodeContainer()
40 container.Add(terminals.Get(i))
41 container.Add(csmaSwitch)
42 link = csma.Install(container)
43 terminalDevices.Add(link.Get(0))
44 switchDevices.Add(link.Get(1))
45
46switchNode = csmaSwitch.Get(0)
47swtch = ns.OpenFlowSwitchHelper()
48controller = ns.ofi.DropController()
49# controller = ns.CreateObject("ns3::ofi::LearningController")
50swtch.Install(switchNode, switchDevices, controller)
51# controller->SetAttribute("ExpirationTime", TimeValue(timeout))
52
53
54internet = ns.InternetStackHelper()
55internet.Install(terminals)
56
57ipv4 = ns.Ipv4AddressHelper()
58ipv4.SetBase("10.1.1.0", "255.255.255.0")
59ipv4.Assign(terminalDevices)
60
61port = 9
62
63onoff = ns.OnOffHelper("ns3::UdpSocketFactory", ns.InetSocketAddress(ns.Ipv4Address("10.1.1.2"), port).ConvertTo())
64onoff.SetConstantRate(ns.DataRate("500kb/s"))
65
66app = onoff.Install(terminals.Get(0))
67
68app.Start(ns.Seconds(1.0))
69app.Stop(ns.Seconds(10.0))
70
71sink = ns.PacketSinkHelper("ns3::UdpSocketFactory",
72 ns.InetSocketAddress(ns.Ipv4Address.GetAny(), port).ConvertTo())
73app = sink.Install(terminals.Get(1))
74app.Start(ns.Seconds(0.0))
75
76onoff.SetAttribute("Remote", ns.AddressValue(ns.InetSocketAddress(ns.Ipv4Address("10.1.1.1"), port).ConvertTo()))
77app = onoff.Install(terminals.Get(3))
78app.Start(ns.Seconds(1.1))
79app.Stop(ns.Seconds(10.0))
80
81app = sink.Install(terminals.Get(0))
82app.Start(ns.Seconds(0.0))
83
84ns.Simulator.Run()
85ns.Simulator.Destroy()