A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
openflow-switch.py
Go to the documentation of this file.
2#
3# SPDX-License-Identifier: GPL-2.0-only
4#
5# Author: Blake Hurd <naimorai@gmail.com>
6# Modified by: Josh Pelkey <joshpelkey@gmail.com>
7# Gabriel Ferreira <gabrielcarvfer@gmail.com>
8#
9
10try:
11 from ns import ns
12except ModuleNotFoundError:
13 raise SystemExit(
14 "Error: ns3 Python module not found;"
15 " Python bindings may not be enabled"
16 " or your PYTHONPATH might not be properly configured"
17 )
18
19ns.LogComponentEnable("OpenFlowInterface", ns.LOG_LEVEL_ALL)
20ns.LogComponentEnable("OpenFlowSwitchNetDevice", ns.LOG_LEVEL_ALL)
21
22terminals = ns.NodeContainer()
23terminals.Create(4)
24
25csmaSwitch = ns.NodeContainer()
26csmaSwitch.Create(1)
27
28csma = ns.CsmaHelper()
29csma.SetChannelAttribute("DataRate", ns.DataRateValue(5000000))
30csma.SetChannelAttribute("Delay", ns.TimeValue(ns.MilliSeconds(2)))
31
32terminalDevices = ns.NetDeviceContainer()
33switchDevices = ns.NetDeviceContainer()
34for i in range(4):
35 container = ns.NodeContainer()
36 container.Add(terminals.Get(i))
37 container.Add(csmaSwitch)
38 link = csma.Install(container)
39 terminalDevices.Add(link.Get(0))
40 switchDevices.Add(link.Get(1))
41
42switchNode = csmaSwitch.Get(0)
43swtch = ns.OpenFlowSwitchHelper()
44controller = ns.ofi.DropController()
45# controller = ns.CreateObject[ns.ofi.LearningController]()
46swtch.Install(switchNode, switchDevices, controller)
47# controller->SetAttribute("ExpirationTime", TimeValue(timeout))
48
49
50internet = ns.InternetStackHelper()
51internet.Install(terminals)
52
53ipv4 = ns.Ipv4AddressHelper()
54ipv4.SetBase("10.1.1.0", "255.255.255.0")
55ipv4.Assign(terminalDevices)
56
57port = 9
58
59onoff = ns.OnOffHelper(
60 "ns3::UdpSocketFactory", ns.InetSocketAddress(ns.Ipv4Address("10.1.1.2"), port).ConvertTo()
61)
62onoff.SetConstantRate(ns.DataRate("500kb/s"))
63
64app = onoff.Install(terminals.Get(0))
65
66app.Start(ns.Seconds(1.0))
67app.Stop(ns.Seconds(10.0))
68
69sink = ns.PacketSinkHelper(
70 "ns3::UdpSocketFactory", ns.InetSocketAddress(ns.Ipv4Address.GetAny(), port).ConvertTo()
71)
72app = sink.Install(terminals.Get(1))
73app.Start(ns.Seconds(0.0))
74
75onoff.SetAttribute(
76 "Remote", ns.AddressValue(ns.InetSocketAddress(ns.Ipv4Address("10.1.1.1"), port).ConvertTo())
77)
78app = onoff.Install(terminals.Get(3))
79app.Start(ns.Seconds(1.1))
80app.Stop(ns.Seconds(10.0))
81
82app = sink.Install(terminals.Get(0))
83app.Start(ns.Seconds(0.0))
84
85ns.Simulator.Run()
86ns.Simulator.Destroy()