コード例 #1
0
ファイル: csma.py プロジェクト: Sofoca/marvis
    def __init__(self, network, nodes, delay="0ms", speed="100Mbps"):
        super().__init__(network, 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.speed = speed

        #: A helper for connecting nodes via CSMA.
        self.csma_helper = csma.CsmaHelper()

        logger.info('Install connection between nodes')
        self.csma_helper.SetChannelAttribute("DataRate",
                                             core.StringValue(self.speed))
        self.csma_helper.SetChannelAttribute("Delay",
                                             core.StringValue(self.delay))

        #: All ns-3 devices on this channel.
        self.devices_container = self.csma_helper.Install(
            self.ns3_nodes_container)

        logger.info('Set IP addresses on nodes')
        stack_helper = internet.InternetStackHelper()

        for i, node in enumerate(nodes):
            ns3_device = self.devices_container.Get(i)

            address = 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)
                device_container = ns_net.NetDeviceContainer(ns3_device)
                ip_address = self.network.address_helper.Assign(
                    device_container).GetAddress(0)
                netmask = network.network.prefixlen
                address = ipaddress.ip_interface(f'{ip_address}/{netmask}')

            interface = Interface(node=node,
                                  ns3_device=ns3_device,
                                  address=address)
            ns3_device.SetAddress(ns_net.Mac48Address(interface.mac_address))
            node.add_interface(interface)
            self.interfaces.append(interface)
コード例 #2
0
ファイル: wifi.py プロジェクト: osmhpi/cohydra
    def __init__(self, network, 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):
        super().__init__(network, 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

        logger.debug("Setting up physical layer of WiFi.")
        self.wifi_phy_helper = wifi.YansWifiPhyHelper.Default()
        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()
        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, node in enumerate(nodes):
            ns3_device = self.devices_container.Get(i)

            address = 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)
                device_container = ns_net.NetDeviceContainer(ns3_device)
                ip_address = self.network.address_helper.Assign(device_container).GetAddress(0)
                netmask = network.network.prefixlen
                address = ipaddress.ip_interface(f'{ip_address}/{netmask}')

            interface = Interface(node=node, ns3_device=ns3_device, address=address)
            ns3_device.GetMac().SetAddress(ns_net.Mac48Address(interface.mac_address))
            node.add_interface(interface)
            self.interfaces.append(interface)
コード例 #3
0
 def __init__(self, name):
     super().__init__(name)
     bridge_helper = bridge.BridgeHelper()
     #: The ns-3 internal device to route packages.
     self.bridge_device = bridge_helper.Install(self.name, network.NetDeviceContainer()).Get(0)
コード例 #4
0
    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)
コード例 #5
0
    def __init__(self,
                 network,
                 nodes,
                 frequency=None,
                 channel=1,
                 channel_width=40,
                 antennas=1,
                 tx_power=20.0,
                 standard: WiFiStandard = WiFiStandard.WIFI_802_11a,
                 data_rate: WiFiDataRate = WiFiDataRate.OFDM_RATE_6Mbps):
        super().__init__(network, 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

        logger.debug("Setting up physical layer of WiFi.")
        self.wifi_phy_helper = wifi.YansWifiPhyHelper.Default()
        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_helper = wifi.YansWifiChannelHelper()
        wifi_channel_helper.SetPropagationDelay(
            "ns3::ConstantSpeedPropagationDelayModel")
        wifi_channel_helper.AddPropagationLoss(
            "ns3::LogDistancePropagationLossModel")
        # wifi_channel_helper.AddPropagationLoss("ns3::RangePropagationLossModel")

        self.wifi_phy_helper.SetChannel(wifi_channel_helper.Create())

        #: Helper for creating the WiFi channel
        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)

        wifi_mac_helper = wifi.WifiMacHelper()

        # Adhoc network between multiple nodes (no access point).
        wifi_mac_helper.SetType("ns3::AdhocWifiMac")

        # 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,
                                                   wifi_mac_helper,
                                                   self.ns3_nodes_container)

        logger.info('Setting IP addresses on nodes.')
        stack_helper = internet.InternetStackHelper()

        for i, node in enumerate(nodes):
            ns3_device = self.devices_container.Get(i)

            address = 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)
                device_container = ns_net.NetDeviceContainer(ns3_device)
                ip_address = self.network.address_helper.Assign(
                    device_container).GetAddress(0)
                netmask = network.network.prefixlen
                address = ipaddress.ip_interface(f'{ip_address}/{netmask}')

            interface = Interface(node=node,
                                  ns3_device=ns3_device,
                                  address=address)
            ns3_device.GetMac().SetAddress(
                ns_net.Mac48Address(interface.mac_address))
            node.add_interface(interface)
            self.interfaces.append(interface)