def network_address_helper(network): if network.version == 4: address = ns_net.Ipv4Address(network.network_address) prefix = ns_net.Ipv4Mask(network.netmask) return internet.Ipv4AddressHelper(address, prefix) if network.version == 6: address = ns_net.Ipv6Address(network.network_address) prefix = ns_net.Ipv6Prefix(network.prefixlen) return internet.Ipv6AddressHelper(address, prefix) raise 'Network version is not IPv4 or IPv6'
def network_address_helper(network, base): if network.version == 4: if base is None: base = "0.0.0.1" address = ns_net.Ipv4Address(network.network_address) prefix = ns_net.Ipv4Mask(network.netmask) return internet.Ipv4AddressHelper(address, prefix, base=ns_net.Ipv4Address(base)) if network.version == 6: if base is None: base = "::1" address = ns_net.Ipv6Address(network.network_address) prefix = ns_net.Ipv6Prefix(network.prefixlen) return internet.Ipv6AddressHelper(address, prefix, base=ns_net.Ipv6Address(base)) raise 'Network version is not IPv4 or IPv6'
def __init__(self, network, channel_name, nodes, frequency=None, channel=1, channel_width=40, antennas=1, tx_power=20.0, standard: WiFiStandard = WiFiStandard.WIFI_802_11b, data_rate: WiFiDataRate = WiFiDataRate.DSSS_RATE_11Mbps, delay="0ms"): super().__init__(network, channel_name, nodes) #: The channel to use. self.channel = channel #: The frequency to use. #: #: This could collide with other WiFi channels. self.frequency = frequency #: The width of the channel in MHz. self.channel_width = channel_width #: The number of antennas to use. self.antennas = antennas #: The sending power in dBm. self.tx_power = tx_power #: The WiFi standard to use. self.standard = standard #: The data rate to use. self.data_rate = data_rate #: The delay for the channel self.delay = delay logger.debug("Setting up physical layer of WiFi.") self.wifi_phy_helper = wifi.YansWifiPhyHelper() self.wifi_phy_helper.Set("ChannelWidth", core.UintegerValue(self.channel_width)) if self.frequency: self.wifi_phy_helper.Set("Frequency", core.UintegerValue(self.frequency)) else: self.wifi_phy_helper.Set("ChannelNumber", core.UintegerValue(self.channel)) self.wifi_phy_helper.Set("Antennas", core.UintegerValue(self.antennas)) self.wifi_phy_helper.Set("MaxSupportedTxSpatialStreams", core.UintegerValue(self.antennas)) self.wifi_phy_helper.Set("MaxSupportedRxSpatialStreams", core.UintegerValue(self.antennas)) self.wifi_phy_helper.Set("TxPowerStart", core.DoubleValue(self.tx_power)) self.wifi_phy_helper.Set("TxPowerEnd", core.DoubleValue(self.tx_power)) # Enable monitoring of radio headers. self.wifi_phy_helper.SetPcapDataLinkType( wifi.WifiPhyHelper.DLT_IEEE802_11_RADIO) wifi_channel = wifi.YansWifiChannel() self.propagation_delay_model = propagation.ConstantSpeedPropagationDelayModel( ) self.set_delay(self.delay) wifi_channel.SetAttribute( "PropagationDelayModel", core.PointerValue(self.propagation_delay_model)) if self.standard == WiFiChannel.WiFiStandard.WIFI_802_11p: # Loss Model, parameter values from Boockmeyer, A. (2020): # "Hatebefi: Hybrid Application Testbed for Fault Injection" loss_model = propagation.ThreeLogDistancePropagationLossModel() loss_model.SetAttribute("Distance0", core.DoubleValue(27.3)) loss_model.SetAttribute("Distance1", core.DoubleValue(68.4)) loss_model.SetAttribute("Distance2", core.DoubleValue(80.7)) loss_model.SetAttribute("Exponent0", core.DoubleValue(1.332671627050236)) loss_model.SetAttribute("Exponent1", core.DoubleValue(2.6812446718062612)) loss_model.SetAttribute("Exponent2", core.DoubleValue(3.5145944762444183)) loss_model.SetAttribute("ReferenceLoss", core.DoubleValue(83.54330702928374)) wifi_channel.SetAttribute("PropagationLossModel", core.PointerValue(loss_model)) else: loss_model = propagation.LogDistancePropagationLossModel() wifi_channel.SetAttribute("PropagationLossModel", core.PointerValue(loss_model)) self.wifi_phy_helper.SetChannel(wifi_channel) #: Helper for creating the WiFi channel. self.wifi = None #: All ns-3 devices on this channel. self.devices_container = None #: Helper for creating MAC layers. self.wifi_mac_helper = None if self.standard != WiFiChannel.WiFiStandard.WIFI_802_11p: self.wifi = wifi.WifiHelper() self.wifi.SetRemoteStationManager( "ns3::ConstantRateWifiManager", "DataMode", core.StringValue(self.data_rate.value), "ControlMode", core.StringValue(self.data_rate.value)) self.wifi.SetStandard(self.standard.value) self.wifi_mac_helper = wifi.WifiMacHelper() # Adhoc network between multiple nodes (no access point). self.wifi_mac_helper.SetType("ns3::AdhocWifiMac") else: self.wifi = wave.Wifi80211pHelper.Default() self.wifi.SetRemoteStationManager( "ns3::ConstantRateWifiManager", "DataMode", core.StringValue(self.data_rate.value), "ControlMode", core.StringValue(self.data_rate.value), "NonUnicastMode", core.StringValue(self.data_rate.value)) self.wifi_mac_helper = wave.NqosWaveMacHelper.Default() # Install on all connected nodes. logger.debug( "Installing the WiFi channel to %d nodes. Mode is %s (data) / %s (control).", len(nodes), self.standard, self.data_rate) #: All ns-3 devices on this channel. self.devices_container = self.wifi.Install(self.wifi_phy_helper, self.wifi_mac_helper, self.ns3_nodes_container) logger.info('Setting IP addresses on nodes.') stack_helper = internet.InternetStackHelper() for i, connected_node in enumerate(nodes): ns3_device = self.devices_container.Get(i) node = connected_node.node address = None interface = None if node.wants_ip_stack(): if node.ns3_node.GetObject(internet.Ipv4.GetTypeId()) is None: logger.info('Installing IP stack on %s', node.name) stack_helper.Install(node.ns3_node) address = connected_node.address if address is None: address = self.network.get_free_ip_address() network_address = ipaddress.ip_network( f'{str(address)}/{network.netmask}', strict=False) ns3_network_address = ns_net.Ipv4Address( network_address.network_address) ns3_network_prefix = ns_net.Ipv4Mask(network_address.netmask) base = ipaddress.ip_address( int(address) - int(network_address.network_address)) helper = internet.Ipv4AddressHelper(ns3_network_address, ns3_network_prefix, base=ns_net.Ipv4Address( str(base))) device_container = ns_net.NetDeviceContainer(ns3_device) helper.Assign(device_container) interface = Interface(node=node, ns3_device=ns3_device, address=ipaddress.ip_interface( f'{str(address)}/{network.netmask}')) else: interface = Interface(node=node, ns3_device=ns3_device, address=connected_node.address) ns3_device.SetAddress(ns_net.Mac48Address(interface.mac_address)) node.add_interface(interface, name=connected_node.ifname) self.interfaces.append(interface)
def __init__(self, network, channel_name, nodes, delay="0ms", data_rate="100Mbps"): super().__init__(network, channel_name, nodes) #: The channel's delay. self.delay = delay #: The channel's speed for transmitting and receiving. #: #: Valid values e.g. are :code:`'100Mbps'` or :code:`'64kbps'`. self.data_rate = data_rate #: A helper for connecting nodes via CSMA. self.csma_helper = csma.CsmaHelper() #: The channel for connecting the nodes self.csma_channel = csma.CsmaChannel() self.set_delay(self.delay) self.set_data_rate(self.data_rate) #: All ns-3 devices on this channel. logger.info('Install connection between nodes') self.devices_container = self.csma_helper.Install( self.ns3_nodes_container, self.csma_channel) logger.info('Set IP addresses on nodes') stack_helper = internet.InternetStackHelper() for i, connected_node in enumerate(nodes): ns3_device = self.devices_container.Get(i) node = connected_node.node if node.wants_ip_stack(): if node.ns3_node.GetObject(internet.Ipv4.GetTypeId()) is None: logger.info('Installing IP stack on %s', node.name) stack_helper.Install(node.ns3_node) address = connected_node.address if address is None: address = self.network.get_free_ip_address() network_address = ipaddress.ip_network( f'{str(address)}/{network.netmask}', strict=False) ns3_network_address = ns_net.Ipv4Address( network_address.network_address) ns3_network_prefix = ns_net.Ipv4Mask(network_address.netmask) base = ipaddress.ip_address( int(address) - int(network_address.network_address)) helper = internet.Ipv4AddressHelper(ns3_network_address, ns3_network_prefix, base=ns_net.Ipv4Address( str(base))) device_container = ns_net.NetDeviceContainer(ns3_device) helper.Assign(device_container) interface = Interface(node=node, ns3_device=ns3_device, address=ipaddress.ip_interface( f'{str(address)}/{network.netmask}')) else: interface = Interface(node=node, ns3_device=ns3_device, address=connected_node.address) ns3_device.SetAddress(ns_net.Mac48Address(interface.mac_address)) node.add_interface(interface) self.interfaces.append(interface)
def main(li): p2pNodes = network.NodeContainer() p2pNodes.Create(2) pointToPoint = point_to_point.PointToPointHelper() pointToPoint.SetDeviceAttribute( "DataRate", core.StringValue(str(int(sum([i[0] for i in li]))) + "Mbps")) pointToPoint.SetChannelAttribute("Delay", core.StringValue("20ms")) p2pDevices = pointToPoint.Install(p2pNodes) csmaNodes = network.NodeContainer() csmaNodes.Add(p2pNodes.Get(1)) csmaNodes.Create(3) csma = ccc.CsmaHelper() csma.SetChannelAttribute("DataRate", core.StringValue("100Mbps")) csma.SetChannelAttribute("Delay", core.TimeValue(core.NanoSeconds(6560))) csmaDevices = csma.Install(csmaNodes) wifiStaNodes = network.NodeContainer() wifiStaNodes.Create(3) wifiApNode = p2pNodes.Get(0) channel = www.YansWifiChannelHelper.Default() phy = www.YansWifiPhyHelper.Default() phy.SetChannel(channel.Create()) wifi = www.WifiHelper() # wifi.SetRemoteStationManager("ns3::AarfWifiManager") wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode", core.StringValue("OfdmRate54Mbps")) mac = www.WifiMacHelper() ssid = www.Ssid("ns-3-ssid") mac.SetType("ns3::StaWifiMac", "Ssid", www.SsidValue(ssid), "ActiveProbing", core.BooleanValue(False)) staDevices = wifi.Install(phy, mac, wifiStaNodes) mac.SetType("ns3::ApWifiMac", "Ssid", www.SsidValue(ssid)) apDevices = wifi.Install(phy, mac, wifiApNode) mobility = mob.MobilityHelper() mobility.SetPositionAllocator("ns3::GridPositionAllocator", "MinX", core.DoubleValue(0.0), "MinY", core.DoubleValue(0.0), "DeltaX", core.DoubleValue(5.0), "DeltaY", core.DoubleValue(10.0), "GridWidth", core.UintegerValue(3), "LayoutType", core.StringValue("RowFirst")) mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel") mobility.Install(wifiApNode) mobility.Install(wifiStaNodes) stack = internet.InternetStackHelper() stack.Install(csmaNodes) stack.Install(wifiApNode) stack.Install(wifiStaNodes) address = internet.Ipv4AddressHelper() address.SetBase(network.Ipv4Address("10.1.1.0"), network.Ipv4Mask("255.255.255.0")) address.Assign(p2pDevices) address.SetBase(network.Ipv4Address("10.1.2.0"), network.Ipv4Mask("255.255.255.0")) address.Assign(csmaDevices) address.SetBase(network.Ipv4Address("10.1.3.0"), network.Ipv4Mask("255.255.255.0")) address.Assign(staDevices) address.Assign(apDevices) client = csmaNodes.Get(0) soc = network.Socket.CreateSocket(client, internet.UdpSocketFactory.GetTypeId()) pre = li[0] client.AddApplication(send(soc, pre[0], pre[1], False)) for i in range(1, 4): client = csmaNodes.Get(i) soc = network.Socket.CreateSocket( client, internet.UdpSocketFactory.GetTypeId()) pre = li[i] client.AddApplication(send(soc, pre[0], pre[1])) client = wifiStaNodes.Get(0) soc = network.Socket.CreateSocket(client, internet.UdpSocketFactory.GetTypeId()) client.AddApplication(recv(soc)) client = wifiStaNodes.Get(1) soc = network.Socket.CreateSocket(client, internet.UdpSocketFactory.GetTypeId()) client.AddApplication(recv(soc, False)) internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables() core.Simulator.Stop(core.Seconds(3.0)) # pointToPoint.EnablePcapAll("third") # phy.EnablePcapAll("csma") # csma.EnablePcapAll("third", True) # stack.EnablePcapIpv4All('ics') core.Simulator.Run() core.Simulator.Destroy()
def main(): # core.LogComponentEnable("UdpEchoClientApplication", core.LOG_LEVEL_INFO) wifihelper = wifi.WifiHelper.Default() wifihelper.SetStandard(wifi.WIFI_PHY_STANDARD_80211a) wifiphy = wifi.YansWifiPhyHelper.Default() wifichannel = wifi.YansWifiChannelHelper.Default() wifiphy.SetChannel(wifichannel.Create()) wifimac = wifi.WifiMacHelper() wifimac.SetType("ns3::AdhocWifiMac") wifihelper.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode", core.StringValue("OfdmRate54Mbps")) p2pmac = p2p.PointToPointHelper() p2pmac.SetChannelAttribute("Delay", core.TimeValue(core.NanoSeconds(6560))) p2pmac.SetDeviceAttribute("DataRate", core.StringValue("2Mbps")) stas = network.NodeContainer() stas.Create(3) p2ps = network.NodeContainer() p2ps.Create(1) mob = mobility.MobilityHelper() mob.Install(stas) stack = internet.InternetStackHelper() stack.Install(stas) stack.Install(p2ps) dev = wifihelper.Install(wifiphy, wifimac, stas) p2ps.Add(stas.Get(0)) dev2 = p2pmac.Install(p2ps) ip = internet.Ipv4AddressHelper() ip.SetBase(network.Ipv4Address('192.168.0.0'), network.Ipv4Mask('255.255.255.0')) ip.Assign(dev) ip.SetBase(network.Ipv4Address('192.168.1.0'), network.Ipv4Mask('255.255.255.0')) ip.Assign(dev2) client = p2ps.Get(0) client.AddApplication( aaa( network.Socket.CreateSocket(client, internet.UdpSocketFactory.GetTypeId()), 0)) for i in range(3): client = stas.Get(i) client.AddApplication( aaa( network.Socket.CreateSocket( client, internet.UdpSocketFactory.GetTypeId()), i)) internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables() core.Simulator.Stop(core.Seconds(10.0)) wifiphy.EnablePcapAll('adhoc', True) stack.EnablePcapIpv4All('ipv4') core.Simulator.Run() core.Simulator.Destroy() sys.exit(0)