blob: a01c044d0ab121aaa51f8eb5c49a7148eaf4d948 [file] [log] [blame]
Alexander Afanasyev3875a4b2011-11-10 11:55:53 -08001// -*- 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-ordered-nexthops.h"
28
29#include <boost/lambda/lambda.hpp>
30#include <iomanip>
31
32NS_LOG_COMPONENT_DEFINE ("Ipv4GlobalRoutingOrderedNexthops");
33
34namespace ns3 {
35
36NS_OBJECT_ENSURE_REGISTERED (Ipv4GlobalRoutingOrderedNexthops);
37
38TypeId
39Ipv4GlobalRoutingOrderedNexthops::GetTypeId (void)
40{
41 static TypeId tid = TypeId ("ns3::Ipv4GlobalRoutingOrderedNexthops")
42 .SetParent<Ipv4GlobalRouting> ()
43 .AddConstructor<Ipv4GlobalRoutingOrderedNexthops> ()
44 ;
45 return tid;
46}
47
48Ipv4GlobalRoutingOrderedNexthops::Ipv4GlobalRoutingOrderedNexthops ()
49{
50}
51
52void
53Ipv4GlobalRoutingOrderedNexthops::AddRouteTo (Ipv4Address dest,
54 Ipv4Mask destMask,
55 Ipv4Address nextHop,
56 uint32_t interface,
57 uint32_t metric/*=0*/)
58{
59 // if (m_ipv4->GetObject<Node> ()->GetId ()!=3) return;
60 NS_LOG_FUNCTION (dest << destMask << nextHop << interface << metric);
61
62 // First, make sure we don't try to add route to ourselves
63 int32_t iface = m_ipv4->GetInterfaceForPrefix (dest, destMask);
64 NS_LOG_LOGIC ("Iface " << iface << " for " << dest);
65 if (destMask != Ipv4Mask::GetZero () && iface >= 0)
66 {
67 NS_LOG_LOGIC ("Do not add route to ourselves");
68 return;
69 }
70
71 // Second, there is no reason to add p2p route that equals to the next hop
72 if (destMask == Ipv4Mask::GetOnes () && dest == nextHop)
73 {
74 NS_LOG_LOGIC ("Ignore route to nexthop via nexhop");
75 return;
76 }
77
78 Ptr<EntryContainer> nextHops = 0;
79
80 Ipv4AddressTrieMap::iterator route =
81 m_routes.find (dest.CombineMask (destMask));
82 if (route == m_routes.end ())
83 {
84 nextHops = Create<EntryContainer> ();
85 m_routes[dest.CombineMask (destMask)] = nextHops;
86 }
87 else
88 {
89 nextHops = route->second;
90 }
91
92 std::pair<EntryContainer::iterator,bool> result =
93 nextHops->insert (Ipv4RoutingTableEntry::CreateNetworkRouteTo (dest, destMask, nextHop, interface, metric));
94 if (!result.second)
95 {
96 NS_LOG_LOGIC ("Entry for the interface already exists");
97 if (result.first->GetMetric () > metric)
98 {
99 NS_LOG_LOGIC ("Update metric");
100 nextHops->modify (result.first,
101 boost::bind(&Ipv4RoutingTableEntry::SetMetric, boost::lambda::_1, metric));
102 }
103 }
104
105 nextHops->get<i_index> ().rearrange (nextHops->get<i_metric> ().begin ());
106}
107
108Ptr<Ipv4Route>
109Ipv4GlobalRoutingOrderedNexthops::LookupGlobal (Ipv4Address dest, Ptr<NetDevice> oif)
110{
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800111 NS_LOG_FUNCTION (this << dest << oif);
Alexander Afanasyev3875a4b2011-11-10 11:55:53 -0800112
113 Ipv4AddressTrieMap::const_iterator longest_prefix_map = m_routes.longest_prefix_match (dest);
114 if (longest_prefix_map == m_routes.end ())
115 {
116 return 0;
117 }
118
119 const Ipv4RoutingTableEntry & entry = longest_prefix_map->second->get<i_index> ()[0];
120
121 if (oif != 0 && oif == m_ipv4->GetNetDevice (entry.GetInterface ()))
122 {
123 NS_LOG_LOGIC ("Route points to the incoming interface. Return empty route");
124 return 0;
125 }
126
127 // create a Ipv4Route object from the selected routing table entry
128 Ptr<Ipv4Route> rtentry = Create<Ipv4Route> ();
129 rtentry->SetDestination (entry.GetDest ());
130 rtentry->SetSource (m_ipv4->GetAddress (entry.GetInterface (), 0).GetLocal ());
131 rtentry->SetGateway (entry.GetGateway ());
132 rtentry->SetOutputDevice (m_ipv4->GetNetDevice (entry.GetInterface ()));
133 return rtentry;
134}
135
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800136const Ptr<Ipv4GlobalRoutingOrderedNexthops::EntryContainer>
137Ipv4GlobalRoutingOrderedNexthops::Lookup (Ipv4Address dest)
138{
139 NS_LOG_FUNCTION (this << dest);
140
141 Ipv4AddressTrieMap::const_iterator longest_prefix_map = m_routes.longest_prefix_match (dest);
142 if (longest_prefix_map == m_routes.end ())
143 {
144 return 0;
145 }
146
147 return longest_prefix_map->second;
148}
149
150
Alexander Afanasyev3875a4b2011-11-10 11:55:53 -0800151void
152Ipv4GlobalRoutingOrderedNexthops::DeleteRoutes ()
153{
154 m_routes.clear ();
155}
156
157void
158Ipv4GlobalRoutingOrderedNexthops::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const
159{
160 std::ostream* os = stream->GetStream ();
161 if (m_routes.size () > 0)
162 {
163 *os << "Destination Iface(Metric),...,Iface(Metric)" << std::endl;
164 for (Ipv4AddressTrieMap::const_iterator i=m_routes.begin (); i != m_routes.end (); i++)
165 {
166 std::ostringstream dest, gw;
167 const Ipv4RoutingTableEntry &route = i->second->get<i_index> ()[0];
168 dest << route.GetDest () << "/" << route.GetDestNetworkMask().GetPrefixLength ();
169 gw << route.GetGateway ();
170
171 *os << std::setiosflags (std::ios::left) << std::setw (15) << dest.str ();
172
173 for (EntryContainer::index<i_metric>::type::iterator metric = i->second->get<i_metric> ().begin ();
174 metric != i->second->get<i_metric> ().end ();
175 metric ++)
176 {
177 if (metric != i->second->get<i_metric> ().begin ())
178 *os << ",";
179 *os << metric->GetInterface () << "(" << metric->GetMetric () << ")";
180 }
181
182 // for (EntryContainer::iterator metric = i->second->begin ();
183 // metric != i->second->end ();
184 // metric ++)
185 // {
186 // if (metric != i->second->begin ())
187 // *os << ",";
188 // *os << metric->GetInterface () << "(" << metric->GetMetric () << ")";
189 // }
190
191 *os << std::endl;
192 }
193 }
194}
195
196Ptr<Ipv4Route>
197Ipv4GlobalRoutingOrderedNexthops::RouteOutput (Ptr<Packet> p, const Ipv4Header &header,
198 Ptr<NetDevice> oif, Socket::SocketErrno &sockerr)
199{
200//
201// First, see if this is a multicast packet we have a route for. If we
202// have a route, then send the packet down each of the specified interfaces.
203//
204 if (header.GetDestination ().IsMulticast ())
205 {
206 NS_LOG_LOGIC ("Multicast destination-- returning false");
207 return 0; // Let other routing protocols try to handle this
208 }
209//
210// See if this is a unicast packet we have a route for.
211//
212 NS_LOG_LOGIC ("Unicast destination- looking up");
213 Ptr<Ipv4Route> rtentry = LookupGlobal (header.GetDestination (), oif);
214 if (rtentry)
215 {
216 sockerr = Socket::ERROR_NOTERROR;
217 }
218 else
219 {
220 sockerr = Socket::ERROR_NOROUTETOHOST;
221 }
222 return rtentry;
223}
224
225bool
226Ipv4GlobalRoutingOrderedNexthops::RouteInput (Ptr<const Packet> p, const Ipv4Header &header,
227 Ptr<const NetDevice> idev,
228 UnicastForwardCallback ucb, MulticastForwardCallback mcb,
229 LocalDeliverCallback lcb, ErrorCallback ecb)
230{
231
232 NS_LOG_FUNCTION (this << p << header << header.GetSource () << header.GetDestination () << idev);
233 // Check if input device supports IP
234 NS_ASSERT (m_ipv4->GetInterfaceForDevice (idev) >= 0);
235 uint32_t iif = m_ipv4->GetInterfaceForDevice (idev);
236
237 if (header.GetDestination ().IsMulticast ())
238 {
239 NS_LOG_LOGIC ("Multicast destination-- returning false");
240 return false; // Let other routing protocols try to handle this
241 }
242
243 if (header.GetDestination ().IsBroadcast ())
244 {
245 NS_LOG_LOGIC ("For me (Ipv4Addr broadcast address)");
246 // TODO: Local Deliver for broadcast
247 // TODO: Forward broadcast
248 }
249
250 // TODO: Configurable option to enable RFC 1222 Strong End System Model
251 // Right now, we will be permissive and allow a source to send us
252 // a packet to one of our other interface addresses; that is, the
253 // destination unicast address does not match one of the iif addresses,
254 // but we check our other interfaces. This could be an option
255 // (to remove the outer loop immediately below and just check iif).
256 for (uint32_t j = 0; j < m_ipv4->GetNInterfaces (); j++)
257 {
258 for (uint32_t i = 0; i < m_ipv4->GetNAddresses (j); i++)
259 {
260 Ipv4InterfaceAddress iaddr = m_ipv4->GetAddress (j, i);
261 Ipv4Address addr = iaddr.GetLocal ();
262 if (addr.IsEqual (header.GetDestination ()))
263 {
264 if (j == iif)
265 {
266 NS_LOG_LOGIC ("For me (destination " << addr << " match)");
267 }
268 else
269 {
270 NS_LOG_LOGIC ("For me (destination " << addr << " match) on another interface " << header.GetDestination ());
271 }
272 lcb (p, header, iif);
273 return true;
274 }
275 if (header.GetDestination ().IsEqual (iaddr.GetBroadcast ()))
276 {
277 NS_LOG_LOGIC ("For me (interface broadcast address)");
278 lcb (p, header, iif);
279 return true;
280 }
281 NS_LOG_LOGIC ("Address "<< addr << " not a match");
282 }
283 }
284 // Check if input device supports IP forwarding
285 if (m_ipv4->IsForwarding (iif) == false)
286 {
287 NS_LOG_LOGIC ("Forwarding disabled for this interface");
288 ecb (p, header, Socket::ERROR_NOROUTETOHOST);
289 return false;
290 }
291 // Next, try to find a route
292 NS_LOG_LOGIC ("Unicast destination- looking up global route");
293 Ptr<Ipv4Route> rtentry = LookupGlobal (header.GetDestination ());
294 if (rtentry != 0)
295 {
296 NS_LOG_LOGIC ("Found unicast destination- calling unicast callback");
297 ucb (rtentry, p, header);
298 return true;
299 }
300 else
301 {
302 NS_LOG_LOGIC ("Did not find unicast destination- returning false");
303 return false; // Let other routing protocols try to handle this
304 // route request.
305 }
306}
307
308} // namespace ns3