A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
openflow-switch.py
Go to the documentation of this file.
1 #
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 
21 try:
22  from ns import ns
23 except 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 
30 ns.LogComponentEnable("OpenFlowInterface", ns.LOG_LEVEL_ALL)
31 ns.LogComponentEnable("OpenFlowSwitchNetDevice", ns.LOG_LEVEL_ALL)
32 
33 terminals = ns.NodeContainer()
34 terminals.Create(4)
35 
36 csmaSwitch = ns.NodeContainer()
37 csmaSwitch.Create(1)
38 
39 csma = ns.CsmaHelper()
40 csma.SetChannelAttribute("DataRate", ns.DataRateValue(5000000))
41 csma.SetChannelAttribute("Delay", ns.TimeValue(ns.MilliSeconds(2)))
42 
43 terminalDevices = ns.NetDeviceContainer()
44 switchDevices = ns.NetDeviceContainer()
45 for 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 
53 switchNode = csmaSwitch.Get(0)
54 swtch = ns.OpenFlowSwitchHelper()
55 controller = ns.ofi.DropController()
56 # controller = ns.CreateObject("ns3::ofi::LearningController")
57 swtch.Install(switchNode, switchDevices, controller)
58 # controller->SetAttribute("ExpirationTime", TimeValue(timeout))
59 
60 
61 internet = ns.InternetStackHelper()
62 internet.Install(terminals)
63 
64 ipv4 = ns.Ipv4AddressHelper()
65 ipv4.SetBase("10.1.1.0", "255.255.255.0")
66 ipv4.Assign(terminalDevices)
67 
68 port = 9
69 
70 onoff = ns.OnOffHelper(
71  "ns3::UdpSocketFactory", ns.InetSocketAddress(ns.Ipv4Address("10.1.1.2"), port).ConvertTo()
72 )
73 onoff.SetConstantRate(ns.DataRate("500kb/s"))
74 
75 app = onoff.Install(terminals.Get(0))
76 
77 app.Start(ns.Seconds(1.0))
78 app.Stop(ns.Seconds(10.0))
79 
80 sink = ns.PacketSinkHelper(
81  "ns3::UdpSocketFactory", ns.InetSocketAddress(ns.Ipv4Address.GetAny(), port).ConvertTo()
82 )
83 app = sink.Install(terminals.Get(1))
84 app.Start(ns.Seconds(0.0))
85 
86 onoff.SetAttribute(
87  "Remote", ns.AddressValue(ns.InetSocketAddress(ns.Ipv4Address("10.1.1.1"), port).ConvertTo())
88 )
89 app = onoff.Install(terminals.Get(3))
90 app.Start(ns.Seconds(1.1))
91 app.Stop(ns.Seconds(10.0))
92 
93 app = sink.Install(terminals.Get(0))
94 app.Start(ns.Seconds(0.0))
95 
96 ns.Simulator.Run()
97 ns.Simulator.Destroy()