Alexander Afanasyev | 3ba44e5 | 2011-11-10 16:38:10 -0800 | [diff] [blame] | 1 | // -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- |
| 2 | // |
| 3 | // Copyright (c) 2008 University of Washington |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License version 2 as |
| 7 | // published by the Free Software Foundation; |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | // |
| 18 | |
| 19 | #include "ns3/names.h" |
| 20 | #include "ns3/node.h" |
| 21 | #include "ns3/log.h" |
| 22 | #include "ns3/simulator.h" |
| 23 | #include "ns3/object.h" |
| 24 | #include "ns3/packet.h" |
| 25 | #include "ns3/net-device.h" |
| 26 | #include "ns3/ipv4-route.h" |
| 27 | #include "ipv4-global-routing-unordered-nexthops.h" |
| 28 | |
| 29 | #include <iomanip> |
| 30 | |
Alexander Afanasyev | 918839c | 2011-11-10 17:02:06 -0800 | [diff] [blame] | 31 | #ifndef UINT32_MAX |
| 32 | # define UINT32_MAX (4294967295U) |
| 33 | #endif |
| 34 | |
Alexander Afanasyev | 3ba44e5 | 2011-11-10 16:38:10 -0800 | [diff] [blame] | 35 | NS_LOG_COMPONENT_DEFINE ("Ipv4GlobalRoutingUnorderedNexthops"); |
| 36 | |
| 37 | namespace ns3 { |
| 38 | |
| 39 | NS_OBJECT_ENSURE_REGISTERED (Ipv4GlobalRoutingUnorderedNexthops); |
| 40 | |
| 41 | TypeId |
| 42 | Ipv4GlobalRoutingUnorderedNexthops::GetTypeId (void) |
| 43 | { |
| 44 | static TypeId tid = TypeId ("ns3::Ipv4GlobalRoutingUnorderedNexthops") |
| 45 | .SetParent<Ipv4GlobalRouting> () |
| 46 | .AddConstructor<Ipv4GlobalRoutingUnorderedNexthops> () |
| 47 | ; |
| 48 | return tid; |
| 49 | } |
| 50 | |
| 51 | Ipv4GlobalRoutingUnorderedNexthops::Ipv4GlobalRoutingUnorderedNexthops () |
| 52 | : m_numLogicalEntries (1) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | Ipv4GlobalRoutingUnorderedNexthops::FixRoutes () |
| 58 | { |
| 59 | for (Ipv4AddressTrieMap::iterator route = m_routes.begin (); |
| 60 | route != m_routes.end (); |
| 61 | route ++) |
| 62 | { |
| 63 | if (route->second->size () < m_numLogicalEntries) |
| 64 | { |
| 65 | NS_LOG_WARN ("Not all entries got a new routing entry. Making one based on previous set"); |
| 66 | route->second->push_back (route->second->back ()); |
| 67 | route->second->back ().SetMetric (UINT32_MAX); // just to ensure we have consistency across the sets |
| 68 | } |
| 69 | } |
| 70 | m_numLogicalEntries ++; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | Ipv4GlobalRoutingUnorderedNexthops::AddRouteTo (Ipv4Address dest, |
| 75 | Ipv4Mask destMask, |
| 76 | Ipv4Address nextHop, |
| 77 | uint32_t interface, |
| 78 | uint32_t metric/*=0*/) |
| 79 | { |
| 80 | NS_LOG_FUNCTION (dest << destMask << nextHop << interface << metric); |
| 81 | |
| 82 | // First, make sure we don't try to add route to ourselves |
| 83 | int32_t iface = m_ipv4->GetInterfaceForPrefix (dest, destMask); |
| 84 | NS_LOG_LOGIC ("Iface " << iface << " for " << dest); |
| 85 | if (destMask != Ipv4Mask::GetZero () && iface >= 0) |
| 86 | { |
| 87 | NS_LOG_LOGIC ("Do not add route to ourselves"); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | // Second, there is no reason to add p2p route that equals to the next hop |
| 92 | if (destMask == Ipv4Mask::GetOnes () && dest == nextHop) |
| 93 | { |
| 94 | NS_LOG_LOGIC ("Ignore route to nexthop via nexhop"); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | Ptr<EntryContainer> nextHops = 0; |
| 99 | |
| 100 | Ipv4AddressTrieMap::iterator route = |
| 101 | m_routes.find (dest.CombineMask (destMask)); |
| 102 | if (route == m_routes.end ()) |
| 103 | { |
| 104 | nextHops = Create<EntryContainer> (); |
| 105 | m_routes[dest.CombineMask (destMask)] = nextHops; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | nextHops = route->second; |
| 110 | } |
| 111 | |
| 112 | // NS_LOG_ERROR ("numLogicalEntries: " << m_numLogicalEntries << ", nextHops->size: " << nextHops->size ()); |
| 113 | NS_ASSERT_MSG (nextHops->size ()==m_numLogicalEntries || |
| 114 | nextHops->size ()==m_numLogicalEntries-1, |
| 115 | "Number of entries in nextHops should be either equal to number of logical entries, or one smaller"); |
| 116 | |
| 117 | if (nextHops->size ()==m_numLogicalEntries-1) |
| 118 | { |
| 119 | // new entry |
| 120 | nextHops->push_back (Ipv4RoutingTableEntry::CreateNetworkRouteTo (dest, destMask, nextHop, interface, metric)); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | if (nextHops->back ().GetMetric () > metric) |
| 125 | { |
| 126 | // update entry if new metric is smaller |
| 127 | nextHops->back () = Ipv4RoutingTableEntry::CreateNetworkRouteTo (dest, destMask, nextHop, interface, metric); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | Ptr<Ipv4Route> |
| 133 | Ipv4GlobalRoutingUnorderedNexthops::LookupGlobal (uint32_t entryNum, Ipv4Address dest, Ptr<NetDevice> oif) |
| 134 | { |
| 135 | NS_LOG_FUNCTION_NOARGS (); |
| 136 | NS_LOG_LOGIC ("Looking for route for destination " << dest); |
| 137 | |
| 138 | NS_ASSERT_MSG (m_numLogicalEntries > 0, |
| 139 | "Number of logical entries should be at least one"); |
| 140 | |
| 141 | NS_ASSERT_MSG (entryNum < m_numLogicalEntries, |
| 142 | "Number of requested logical entry should be smaller than total number of logical entries"); |
| 143 | |
| 144 | Ipv4AddressTrieMap::const_iterator longest_prefix_map = m_routes.longest_prefix_match (dest); |
| 145 | if (longest_prefix_map == m_routes.end ()) |
| 146 | { |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | const Ipv4RoutingTableEntry & entry = (*longest_prefix_map->second)[entryNum]; |
| 151 | |
| 152 | if (oif != 0 && oif == m_ipv4->GetNetDevice (entry.GetInterface ())) |
| 153 | { |
| 154 | NS_LOG_LOGIC ("Route points to the incoming interface. Return empty route"); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | // create a Ipv4Route object from the selected routing table entry |
| 159 | Ptr<Ipv4Route> rtentry = Create<Ipv4Route> (); |
| 160 | rtentry->SetDestination (entry.GetDest ()); |
| 161 | rtentry->SetSource (m_ipv4->GetAddress (entry.GetInterface (), 0).GetLocal ()); |
| 162 | rtentry->SetGateway (entry.GetGateway ()); |
| 163 | rtentry->SetOutputDevice (m_ipv4->GetNetDevice (entry.GetInterface ())); |
| 164 | return rtentry; |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | Ipv4GlobalRoutingUnorderedNexthops::DeleteRoutes () |
| 169 | { |
| 170 | m_routes.clear (); |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | Ipv4GlobalRoutingUnorderedNexthops::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const |
| 175 | { |
| 176 | std::ostream* os = stream->GetStream (); |
| 177 | if (m_routes.size () > 0) |
| 178 | { |
| 179 | *os << "Destination Iface(Metric),...,Iface(Metric)" << std::endl; |
| 180 | for (Ipv4AddressTrieMap::const_iterator i=m_routes.begin (); i != m_routes.end (); i++) |
| 181 | { |
| 182 | if (i->second->size ()==0) continue; |
| 183 | const Ipv4RoutingTableEntry &route = i->second->front (); |
| 184 | |
| 185 | std::ostringstream dest; |
| 186 | dest << route.GetDest () << "/" << route.GetDestNetworkMask().GetPrefixLength (); |
| 187 | *os << std::setiosflags (std::ios::left) << std::setw (15) << dest.str (); |
| 188 | |
| 189 | for (EntryContainer::iterator entry = i->second->begin (); |
| 190 | entry != i->second->end (); |
| 191 | entry ++) |
| 192 | { |
| 193 | if (entry != i->second->begin ()) |
| 194 | *os << ","; |
| 195 | *os << entry->GetInterface () << "(" << entry->GetMetric () << ")"; |
| 196 | } |
| 197 | |
| 198 | *os << std::endl; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | Ptr<Ipv4Route> |
| 204 | Ipv4GlobalRoutingUnorderedNexthops::RouteOutput (Ptr<Packet> p, const Ipv4Header &header, |
| 205 | Ptr<NetDevice> oif, Socket::SocketErrno &sockerr) |
| 206 | { |
| 207 | // |
| 208 | // First, see if this is a multicast packet we have a route for. If we |
| 209 | // have a route, then send the packet down each of the specified interfaces. |
| 210 | // |
| 211 | if (header.GetDestination ().IsMulticast ()) |
| 212 | { |
| 213 | NS_LOG_LOGIC ("Multicast destination-- returning false"); |
| 214 | return 0; // Let other routing protocols try to handle this |
| 215 | } |
| 216 | // |
| 217 | // See if this is a unicast packet we have a route for. |
| 218 | // |
| 219 | NS_LOG_LOGIC ("Unicast destination- looking up"); |
| 220 | Ptr<Ipv4Route> rtentry = LookupGlobal (0, header.GetDestination (), oif); |
| 221 | if (rtentry) |
| 222 | { |
| 223 | sockerr = Socket::ERROR_NOTERROR; |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | sockerr = Socket::ERROR_NOROUTETOHOST; |
| 228 | } |
| 229 | return rtentry; |
| 230 | } |
| 231 | |
| 232 | bool |
| 233 | Ipv4GlobalRoutingUnorderedNexthops::RouteInput (Ptr<const Packet> p, const Ipv4Header &header, |
| 234 | Ptr<const NetDevice> idev, |
| 235 | UnicastForwardCallback ucb, MulticastForwardCallback mcb, |
| 236 | LocalDeliverCallback lcb, ErrorCallback ecb) |
| 237 | { |
| 238 | |
| 239 | NS_LOG_FUNCTION (this << p << header << header.GetSource () << header.GetDestination () << idev); |
| 240 | // Check if input device supports IP |
| 241 | NS_ASSERT (m_ipv4->GetInterfaceForDevice (idev) >= 0); |
| 242 | uint32_t iif = m_ipv4->GetInterfaceForDevice (idev); |
| 243 | |
| 244 | if (header.GetDestination ().IsMulticast ()) |
| 245 | { |
| 246 | NS_LOG_LOGIC ("Multicast destination-- returning false"); |
| 247 | return false; // Let other routing protocols try to handle this |
| 248 | } |
| 249 | |
| 250 | if (header.GetDestination ().IsBroadcast ()) |
| 251 | { |
| 252 | NS_LOG_LOGIC ("For me (Ipv4Addr broadcast address)"); |
| 253 | // TODO: Local Deliver for broadcast |
| 254 | // TODO: Forward broadcast |
| 255 | } |
| 256 | |
| 257 | // TODO: Configurable option to enable RFC 1222 Strong End System Model |
| 258 | // Right now, we will be permissive and allow a source to send us |
| 259 | // a packet to one of our other interface addresses; that is, the |
| 260 | // destination unicast address does not match one of the iif addresses, |
| 261 | // but we check our other interfaces. This could be an option |
| 262 | // (to remove the outer loop immediately below and just check iif). |
| 263 | for (uint32_t j = 0; j < m_ipv4->GetNInterfaces (); j++) |
| 264 | { |
| 265 | for (uint32_t i = 0; i < m_ipv4->GetNAddresses (j); i++) |
| 266 | { |
| 267 | Ipv4InterfaceAddress iaddr = m_ipv4->GetAddress (j, i); |
| 268 | Ipv4Address addr = iaddr.GetLocal (); |
| 269 | if (addr.IsEqual (header.GetDestination ())) |
| 270 | { |
| 271 | if (j == iif) |
| 272 | { |
| 273 | NS_LOG_LOGIC ("For me (destination " << addr << " match)"); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | NS_LOG_LOGIC ("For me (destination " << addr << " match) on another interface " << header.GetDestination ()); |
| 278 | } |
| 279 | lcb (p, header, iif); |
| 280 | return true; |
| 281 | } |
| 282 | if (header.GetDestination ().IsEqual (iaddr.GetBroadcast ())) |
| 283 | { |
| 284 | NS_LOG_LOGIC ("For me (interface broadcast address)"); |
| 285 | lcb (p, header, iif); |
| 286 | return true; |
| 287 | } |
| 288 | NS_LOG_LOGIC ("Address "<< addr << " not a match"); |
| 289 | } |
| 290 | } |
| 291 | // Check if input device supports IP forwarding |
| 292 | if (m_ipv4->IsForwarding (iif) == false) |
| 293 | { |
| 294 | NS_LOG_LOGIC ("Forwarding disabled for this interface"); |
| 295 | ecb (p, header, Socket::ERROR_NOROUTETOHOST); |
| 296 | return false; |
| 297 | } |
| 298 | // Next, try to find a route |
| 299 | NS_LOG_LOGIC ("Unicast destination- looking up global route"); |
| 300 | Ptr<Ipv4Route> rtentry = LookupGlobal (0, header.GetDestination ()); |
| 301 | if (rtentry != 0) |
| 302 | { |
| 303 | NS_LOG_LOGIC ("Found unicast destination- calling unicast callback"); |
| 304 | ucb (rtentry, p, header); |
| 305 | return true; |
| 306 | } |
| 307 | else |
| 308 | { |
| 309 | NS_LOG_LOGIC ("Did not find unicast destination- returning false"); |
| 310 | return false; // Let other routing protocols try to handle this |
| 311 | // route request. |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | } // namespace ns3 |