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
21try:
22 from ns import ns
23except ModuleNotFoundError:
24 raise SystemExit(
25 "Error: ns3 Python module not found;"
26 " Python bindings may not be enabled"
27 " or your PYTHONPATH might not be properly configured"
28 )
29
30ns.LogComponentEnable("OpenFlowInterface", ns.LOG_LEVEL_ALL)
31ns.LogComponentEnable("OpenFlowSwitchNetDevice", ns.LOG_LEVEL_ALL)
32
33terminals = ns.NodeContainer()
34terminals.Create(4)
35
36csmaSwitch = ns.NodeContainer()
37csmaSwitch.Create(1)
38
39csma = ns.CsmaHelper()
40csma.SetChannelAttribute("DataRate", ns.DataRateValue(5000000))
41csma.SetChannelAttribute("Delay", ns.TimeValue(ns.MilliSeconds(2)))
42
43terminalDevices = ns.NetDeviceContainer()
44switchDevices = ns.NetDeviceContainer()
45for i in range(4):
46 container = ns.NodeContainer()
47 container.Add(terminals.Get(i))
48 container.Add(csmaSwitch)
49 link = csma.Install(container)
50 terminalDevices.Add(link.Get(0))
51 switchDevices.Add(link.Get(1))
52
53switchNode = csmaSwitch.Get(0)
54swtch = ns.OpenFlowSwitchHelper()
55controller = ns.ofi.DropController()
56# controller = ns.CreateObject("ns3::ofi::LearningController")
57swtch.Install(switchNode, switchDevices, controller)
58# controller->SetAttribute("ExpirationTime", TimeValue(timeout))
59
60
61internet = ns.InternetStackHelper()
62internet.Install(terminals)
63
64ipv4 = ns.Ipv4AddressHelper()
65ipv4.SetBase("10.1.1.0", "255.255.255.0")
66ipv4.Assign(terminalDevices)
67
68port = 9
69
70onoff = ns.OnOffHelper(
71 "ns3::UdpSocketFactory", ns.InetSocketAddress(ns.Ipv4Address("10.1.1.2"), port).ConvertTo()
72)
73onoff.SetConstantRate(ns.DataRate("500kb/s"))
74
75app = onoff.Install(terminals.Get(0))
76
77app.Start(ns.Seconds(1.0))
78app.Stop(ns.Seconds(10.0))
79
80sink = ns.PacketSinkHelper(
81 "ns3::UdpSocketFactory", ns.InetSocketAddress(ns.Ipv4Address.GetAny(), port).ConvertTo()
82)
83app = sink.Install(terminals.Get(1))
84app.Start(ns.Seconds(0.0))
85
86onoff.SetAttribute(
87 "Remote", ns.AddressValue(ns.InetSocketAddress(ns.Ipv4Address("10.1.1.1"), port).ConvertTo())
88)
89app = onoff.Install(terminals.Get(3))
90app.Start(ns.Seconds(1.1))
91app.Stop(ns.Seconds(10.0))
92
93app = sink.Install(terminals.Get(0))
94app.Start(ns.Seconds(0.0))
95
96ns.Simulator.Run()
97ns.Simulator.Destroy()