diff --git a/examples/csma-multicast.cc b/examples/csma-multicast.cc index c22e4604c..504c4fc26 100644 --- a/examples/csma-multicast.cc +++ b/examples/csma-multicast.cc @@ -98,7 +98,6 @@ main (int argc, char *argv[]) DebugComponentEnable("Ipv4Interface"); DebugComponentEnable("ArpIpv4Interface"); DebugComponentEnable("Ipv4LoopbackInterface"); - // // Set up default values for the simulation. Use the DefaultValue::Bind() // technique to tell the system what subclass of Queue to use. The Bind @@ -200,10 +199,11 @@ main (int argc, char *argv[]) // earlier. We need to define a multicast group to send packets to. This // can be any multicast address from 224.0.0.0 through 239.255.255.255 // (avoiding the reserved routing protocol addresses). We just pick a -// convenient number. +// convenient number (225.0.0.0) and or in some bits to let us verify that +// correct Ethernet multicast addresses are constructed down in the system. // Ipv4Address multicastSource ("10.1.1.1"); - Ipv4Address multicastGroup ("225.0.0.0"); + Ipv4Address multicastGroup ("225.1.2.4"); // // We are going to manually configure multicast routing. This means telling // node two that it should expect multicast data coming from IP address diff --git a/src/devices/csma/csma-channel.cc b/src/devices/csma/csma-channel.cc index 9ba42d854..f9bb9fe48 100644 --- a/src/devices/csma/csma-channel.cc +++ b/src/devices/csma/csma-channel.cc @@ -250,7 +250,7 @@ CsmaChannel::TransmitEnd() } NS_DEBUG ("CsmaChannel::TransmitEnd (): Schedule event in " << - m_delay.GetSeconds () << "sec"); + m_delay.GetSeconds () << " sec"); Simulator::Schedule (m_delay, &CsmaChannel::PropagationCompleteEvent, diff --git a/src/devices/csma/csma-net-device.cc b/src/devices/csma/csma-net-device.cc index bb1dbbd08..ca6a693a5 100644 --- a/src/devices/csma/csma-net-device.cc +++ b/src/devices/csma/csma-net-device.cc @@ -526,20 +526,29 @@ CsmaNetDevice::Receive (const Packet& packet) NS_DEBUG ("CsmaNetDevice::Receive (): Pkt destination is " << header.GetDestination ()); // -// XXX BUGBUG -// // An IP host group address is mapped to an Ethernet multicast address // by placing the low-order 23-bits of the IP address into the low-order // 23 bits of the Ethernet multicast address 01-00-5E-00-00-00 (hex). // -// Need to mask off the appropriate multicast bits. +// We are going to receive all packets destined to any multicast address, +// which means clearing the low-order 23 bits the header destination // + Eui48Address mcDest; + uint8_t mcBuf[6]; + + header.GetDestination ().CopyTo (mcBuf); + mcBuf[3] &= 0x80; + mcBuf[4] = 0; + mcBuf[5] = 0; + mcDest.CopyFrom (mcBuf); + multicast = Eui48Address::ConvertFrom (GetMulticast ()); broadcast = Eui48Address::ConvertFrom (GetBroadcast ()); destination = Eui48Address::ConvertFrom (GetAddress ()); - if ((header.GetDestination() != broadcast) && - (header.GetDestination() != multicast) && - (header.GetDestination() != destination)) + + if ((header.GetDestination () != broadcast) && + (mcDest != multicast) && + (header.GetDestination () != destination)) { NS_DEBUG ("CsmaNetDevice::Receive (): Dropping pkt "); m_dropTrace (p); diff --git a/src/internet-node/arp-ipv4-interface.cc b/src/internet-node/arp-ipv4-interface.cc index e6012cde9..29ccb19f2 100644 --- a/src/internet-node/arp-ipv4-interface.cc +++ b/src/internet-node/arp-ipv4-interface.cc @@ -61,6 +61,74 @@ ArpIpv4Interface::DoCreateTraceResolver (TraceContext const &context) return resolver; } +// +// RFC 1112 says that an IP host group address is mapped to an Ethernet +// multicast address by placing the low-order 23-bits of the IP address into +// the low-order 23 bits of the Ethernet multicast address 01-00-5E-00-00-00 +// (hex). These are completely different animals and they're encapsulated +// very nicely. Translation: This isn't going to be very pretty. +// +Address +ArpIpv4Interface::MakeMulticastAddress(Ipv4Address multicastGroup) +{ + NS_DEBUG ("ArpIpv4Interface::MakeMulticastAddress (" << multicastGroup << + ")"); +// +// First, get the generic multicast address from the device. Since it is +// connected to this object, and this object is an IPV4 stack, we hope that +// it is really an Eui48Address. If it's not, then we don't know what to do. +// + Address hardwareDestination = GetDevice ()->GetMulticast (); + + NS_DEBUG ("ArpIpv4Interface::MakeMulticastAddress (): " + "Device multicast address: " << hardwareDestination); + + Eui48Address etherAddr = Eui48Address::ConvertFrom (hardwareDestination); +// +// We now have the multicast address in an abstract 48-bit container. We +// need to pull it out so we can play with it. When we're done, we have the +// high order bits in etherBuffer[0], etc. +// + uint8_t etherBuffer[6]; + etherAddr.CopyTo (etherBuffer); +// +// If the device is playing the game correctly, the low order 23 bits of the +// multicast base address will be zero. +// + NS_ASSERT_MSG((etherBuffer[4] & 0x7f) == 0, + "ArpIpv4Interface::SendTo (): Expected low order bits zeroed"); + NS_ASSERT_MSG(etherBuffer[5] == 0, + "ArpIpv4Interface::SendTo (): Expected low order bits zeroed"); + NS_ASSERT_MSG(etherBuffer[6] == 0, + "ArpIpv4Interface::SendTo (): Expected low order bits zeroed"); +// +// Now we need to pull the raw bits out of the Ipv4 destination address. +// + uint8_t ipBuffer[4]; + multicastGroup.Serialize (ipBuffer); +// +// We need to place the low order 23 bits of the IP address into the low order +// 23 bits of the ethernet address. +// + etherBuffer[3] |= ipBuffer[1] & 0x7f; + etherBuffer[4] = ipBuffer[2]; + etherBuffer[5] = ipBuffer[3]; +// +// Now, etherBuffer has the desired ethernet multicast address. We have to +// suck these bits back into the Eui48Address; and then suck those bits back +// into the abstract hardwareAddress. +// + etherAddr.CopyFrom (etherBuffer); +// +// Implicit conversion (operator Address ()) is defined for Eui48Address, so +// use it. +// + NS_DEBUG ("ArpIpv4Interface::MakeMulticastAddress (): " + "multicast address is " << etherAddr); + + return etherAddr; +} + void ArpIpv4Interface::SendTo (Packet p, Ipv4Address dest) { @@ -87,15 +155,8 @@ ArpIpv4Interface::SendTo (Packet p, Ipv4Address dest) NS_ASSERT_MSG(GetDevice ()->IsMulticast (), "ArpIpv4Interface::SendTo (): Sending multicast packet over " "non-multicast device"); -// XXX -// -// An IP host group address is mapped to an Ethernet multicast address -// by placing the low-order 23-bits of the IP address into the low-order -// 23 bits of the Ethernet multicast address 01-00-5E-00-00-00 (hex). -// -// Currently no easy way to or bit from Ipv4Address into Address -// - hardwareDestination = GetDevice ()->GetMulticast (); + + hardwareDestination = MakeMulticastAddress(dest); found = true; } else diff --git a/src/internet-node/arp-ipv4-interface.h b/src/internet-node/arp-ipv4-interface.h index 7df522e01..dd73ff2b0 100644 --- a/src/internet-node/arp-ipv4-interface.h +++ b/src/internet-node/arp-ipv4-interface.h @@ -24,6 +24,7 @@ #include "ipv4-interface.h" #include "ns3/ptr.h" +#include "ns3/eui48-address.h" namespace ns3 { @@ -46,6 +47,7 @@ class ArpIpv4Interface : public Ipv4Interface virtual void SendTo (Packet p, Ipv4Address dest); virtual TraceResolver *DoCreateTraceResolver (TraceContext const &context); Ptr m_node; + Address MakeMulticastAddress (Ipv4Address multicastGroup); }; }//namespace ns3 diff --git a/src/internet-node/ipv4-impl.cc b/src/internet-node/ipv4-impl.cc index 8c7f9ce4b..0b557eeaf 100644 --- a/src/internet-node/ipv4-impl.cc +++ b/src/internet-node/ipv4-impl.cc @@ -105,6 +105,16 @@ Ipv4Impl::AddMulticastRoute (Ipv4Address origin, m_ipv4->AddMulticastRoute (origin, group, inputInterface, outputInterfaces); } +void +Ipv4Impl::SetDefaultMulticastRoute (Ipv4Address origin, + Ipv4Address group, + uint32_t inputInterface, + std::vector outputInterfaces) +{ + m_ipv4->SetDefaultMulticastRoute (origin, group, inputInterface, + outputInterfaces); +} + uint32_t Ipv4Impl::GetNMulticastRoutes (void) const { @@ -208,6 +218,9 @@ Ipv4Impl::GetSourceAddress (Ipv4Address destination) const } else { +// +// If we can't find any address, just leave it 0.0.0.0 +// return Ipv4Address::GetAny (); } } diff --git a/src/internet-node/ipv4-impl.h b/src/internet-node/ipv4-impl.h index 2f267a392..8848c9f7e 100644 --- a/src/internet-node/ipv4-impl.h +++ b/src/internet-node/ipv4-impl.h @@ -62,6 +62,10 @@ public: uint32_t inputInterface, std::vector outputInterfaces); + virtual void SetDefaultMulticastRoute (Ipv4Address origin, + Ipv4Address group, uint32_t inputInterface, + std::vector outputInterfaces); + virtual uint32_t GetNMulticastRoutes (void) const; virtual Ipv4MulticastRoute GetMulticastRoute (uint32_t i) const; diff --git a/src/internet-node/ipv4-l3-protocol.cc b/src/internet-node/ipv4-l3-protocol.cc index 28a612ac8..b5faf663b 100644 --- a/src/internet-node/ipv4-l3-protocol.cc +++ b/src/internet-node/ipv4-l3-protocol.cc @@ -355,6 +355,20 @@ Ipv4L3Protocol::AddMulticastRoute (Ipv4Address origin, outputInterfaces); } +void +Ipv4L3Protocol::SetDefaultMulticastRoute ( + Ipv4Address origin, + Ipv4Address group, + uint32_t inputInterface, + std::vector outputInterfaces) +{ + NS_DEBUG("Ipv4L3Protocol::SetDefaultMulticastRoute (" << origin << ", " << + group << ", " << inputInterface << ", " << &outputInterfaces << ")"); + + m_staticRouting->SetDefaultMulticastRoute (origin, group, inputInterface, + outputInterfaces); +} + uint32_t Ipv4L3Protocol::GetNMulticastRoutes (void) const { @@ -768,7 +782,11 @@ Ipv4L3Protocol::GetIfIndexForDestination ( { NS_DEBUG("Ipv4L3Protocol::GetIfIndexForDestination (" << destination << ", " << &ifIndex << ")"); - +// +// The first thing we do in trying to determine a source address is to +// consult the routing protocols. These will also check for a default route +// if one has been set. +// for (Ipv4RoutingProtocolList::const_iterator i = m_routingProtocols.begin (); i != m_routingProtocols.end (); i++) @@ -785,13 +803,13 @@ Ipv4L3Protocol::GetIfIndexForDestination ( } } // -// If there's no routing table entry telling us what single interface will be -// used to send a packet to this destination, we'll have to just pick one. +// If there's no routing table entry telling us what *single* interface will +// be used to send a packet to this destination, we'll have to just pick one. // If there's only one interface on this node, a good answer isn't very hard // to come up with. Before jumping to any conclusions, remember that the // zeroth interface is the loopback interface, so what we actually want is // a situation where there are exactly two interfaces on the node, in which -// case interface one is the "single" interface. +// case interface one is the "single" interface connected to the outside world. // if (GetNInterfaces () == 2) { @@ -802,11 +820,21 @@ Ipv4L3Protocol::GetIfIndexForDestination ( } // // If we fall through to here, we have a node with multiple interfaces and -// no routes to guide us in determining what interface to choose. The last -// choice is to use the one set in the default route. -// +// no routes to guide us in determining what interface to choose. Either +// no default route was found (for unicast or multicast), or in the case of a +// multicast, the default route contained multiple outbound interfaces. +// +// The fallback position is to just get the unicast default route and use +// the outgoing interface specified there. We don't want to leave the source +// address unset, so we just assert here. +// +// N.B. that in the case of a multicast with a route containing multiple +// outgoing interfaces, the source address of packets from that node will be +// set to the IP address of the interface set in the default unicast route. +// Also, in the case of a broadcast, the same will be true. +// NS_DEBUG("Ipv4L3Protocol::GetIfIndexForDestination (): " - "Using default route"); + "Using default unicast route"); Ipv4Route *route = m_staticRouting->GetDefaultRoute (); NS_ASSERT_MSG(route, diff --git a/src/internet-node/ipv4-l3-protocol.h b/src/internet-node/ipv4-l3-protocol.h index 2114558b3..b93a2be5d 100644 --- a/src/internet-node/ipv4-l3-protocol.h +++ b/src/internet-node/ipv4-l3-protocol.h @@ -163,6 +163,11 @@ public: uint32_t inputInterface, std::vector outputInterfaces); + void SetDefaultMulticastRoute (Ipv4Address origin, + Ipv4Address group, + uint32_t inputInterface, + std::vector outputInterfaces); + uint32_t GetNMulticastRoutes (void) const; Ipv4MulticastRoute *GetMulticastRoute (uint32_t i) const; diff --git a/src/internet-node/ipv4-static-routing.cc b/src/internet-node/ipv4-static-routing.cc index cfd2a7046..37790a79a 100644 --- a/src/internet-node/ipv4-static-routing.cc +++ b/src/internet-node/ipv4-static-routing.cc @@ -27,6 +27,11 @@ NS_DEBUG_COMPONENT_DEFINE ("Ipv4StaticRouting"); namespace ns3 { +Ipv4StaticRouting::Ipv4StaticRouting () +: m_defaultRoute (0), m_defaultMulticastRoute (0) +{ +} + void Ipv4StaticRouting::AddHostRouteTo (Ipv4Address dest, Ipv4Address nextHop, @@ -95,10 +100,24 @@ Ipv4StaticRouting::AddMulticastRoute(Ipv4Address origin, m_multicastRoutes.push_back (route); } +void +Ipv4StaticRouting::SetDefaultMulticastRoute( + Ipv4Address origin, + Ipv4Address group, + uint32_t inputInterface, + std::vector outputInterfaces) +{ + Ipv4MulticastRoute *route = new Ipv4MulticastRoute (); + *route = Ipv4MulticastRoute::CreateMulticastRoute (origin, group, + inputInterface, outputInterfaces); + delete m_defaultMulticastRoute; + m_defaultMulticastRoute = route; +} + uint32_t Ipv4StaticRouting::GetNMulticastRoutes (void) const { - return m_multicastRoutes.size (); + return m_multicastRoutes.size () + m_defaultMulticastRoute ? 1 : 0; } Ipv4MulticastRoute * @@ -106,17 +125,54 @@ Ipv4StaticRouting::GetMulticastRoute (uint32_t index) const { NS_ASSERT_MSG(index < m_multicastRoutes.size (), "Ipv4StaticRouting::GetMulticastRoute (): Index out of range"); - - uint32_t tmp = 0; - for (MulticastRoutesCI i = m_multicastRoutes.begin (); - i != m_multicastRoutes.end (); - i++) +// +// From an external point of view the default route appears to be in slot 0 +// of the routing table. The implementation, however, puts it in a separate +// place. So, if a client asks for index 0 and we have a default multicast +// route, we have to return it from that different place +// (m_defaultMulticastRoute). +// + if (index == 0 && m_defaultMulticastRoute != 0) { - if (tmp == index) + return m_defaultMulticastRoute; + } +// +// If there is a default multicast route present, a client will just assume +// that it is in slot zero and there is one "extra" zeroth route in the table. +// To return the correct indexed entry in our list, we have to decrement the +// index to take into account the default route not being in the actual list. +// Since we fell through to here, we've taken care of the case where the +// index was zero. +// + if (m_defaultMulticastRoute != 0) + { + NS_ASSERT(index > 0); + index--; + } + + if (index < m_multicastRoutes.size ()) + { + uint32_t tmp = 0; + for (MulticastRoutesCI i = m_multicastRoutes.begin (); + i != m_multicastRoutes.end (); + i++) { - return *i; + if (tmp == index) + { + return *i; + } + tmp++; } - tmp++; + } + return 0; +} + +Ipv4MulticastRoute * +Ipv4StaticRouting::GetDefaultMulticastRoute () const +{ + if (m_defaultMulticastRoute != 0) + { + return m_defaultMulticastRoute; } return 0; } @@ -126,6 +182,9 @@ Ipv4StaticRouting::RemoveMulticastRoute(Ipv4Address origin, Ipv4Address group, uint32_t inputInterface) { +// +// This method does not attempt to delete the multicast route. +// for (MulticastRoutesI i = m_multicastRoutes.begin (); i != m_multicastRoutes.end (); i++) @@ -145,6 +204,32 @@ Ipv4StaticRouting::RemoveMulticastRoute(Ipv4Address origin, void Ipv4StaticRouting::RemoveMulticastRoute(uint32_t index) { +// +// From an external point of view the default route appears to be in slot 0 +// of the routing table. The implementation, however, puts it in a separate +// place. So, if a client asks to delete index 0 and we have a default +// multicast route set, we have to delete it from that different place +// (m_defaultMulticastRoute). +// + if (index == 0 && m_defaultMulticastRoute != 0) + { + delete m_defaultMulticastRoute; + m_defaultMulticastRoute = 0; + } +// +// If there is a default multicast route present, a client will just assume +// that it is in slot zero and there is one "extra" zeroth route in the table. +// To return the correct indexed entry in our list, we have to decrement the +// index to take into account the default route not being in the actual list. +// Since we fell through to here, we've taken care of the case where the +// index was zero. +// + if (m_defaultMulticastRoute != 0) + { + NS_ASSERT(index > 0); + index--; + } + uint32_t tmp = 0; for (MulticastRoutesI i = m_multicastRoutes.begin (); i != m_multicastRoutes.end (); @@ -215,6 +300,12 @@ Ipv4StaticRouting::LookupStatic ( } } } + + if (m_defaultMulticastRoute != 0) + { + return m_defaultMulticastRoute; + } + return 0; } @@ -492,6 +583,11 @@ Ipv4StaticRouting::DoDispose (void) { delete (*i); } + if (m_defaultMulticastRoute != 0) + { + delete m_defaultMulticastRoute; + m_defaultMulticastRoute = 0; + } Ipv4RoutingProtocol::DoDispose (); } diff --git a/src/internet-node/ipv4-static-routing.h b/src/internet-node/ipv4-static-routing.h index ac1351e1d..8b0420f27 100644 --- a/src/internet-node/ipv4-static-routing.h +++ b/src/internet-node/ipv4-static-routing.h @@ -49,7 +49,7 @@ class Ipv4StaticRouting : public Ipv4RoutingProtocol { public: - Ipv4StaticRouting () : m_defaultRoute (0) {} + Ipv4StaticRouting (); virtual bool RequestRoute (uint32_t ifIndex, Ipv4Header const &ipHeader, @@ -83,8 +83,14 @@ public: uint32_t inputInterface, std::vector outputInterfaces); + void SetDefaultMulticastRoute (Ipv4Address origin, + Ipv4Address group, + uint32_t inputInterface, + std::vector outputInterfaces); + uint32_t GetNMulticastRoutes (void) const; Ipv4MulticastRoute *GetMulticastRoute (uint32_t i) const; + Ipv4MulticastRoute *GetDefaultMulticastRoute (void) const; void RemoveMulticastRoute (Ipv4Address origin, Ipv4Address group, @@ -114,6 +120,7 @@ private: HostRoutes m_hostRoutes; NetworkRoutes m_networkRoutes; Ipv4Route *m_defaultRoute; + Ipv4MulticastRoute *m_defaultMulticastRoute; MulticastRoutes m_multicastRoutes; }; diff --git a/src/node/ipv4.h b/src/node/ipv4.h index b79a29d89..7d386a493 100644 --- a/src/node/ipv4.h +++ b/src/node/ipv4.h @@ -243,6 +243,19 @@ public: Ipv4Address group, uint32_t inputInterface) = 0; + /** + * \brief Set the default static multicast route for a given multicast + * source and group. + * + * \param origin The Ipv4 address of the multicast source. + * \param group The multicast group address. + * \param inputInterface The interface index over which the packet arrived. + * \param outputInterfaces The list of output interface indices over which + * the packet should be sent (excluding the inputInterface). + */ + virtual void SetDefaultMulticastRoute (Ipv4Address origin, + Ipv4Address group, uint32_t inputInterface, + std::vector outputInterfaces) = 0; /** * \returns the number of entries in the multicast routing table. */