blob: 9ed2b60496a066c278ec3a43250893771cd53673 [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{
111 NS_LOG_FUNCTION_NOARGS ();
112 NS_LOG_LOGIC ("Looking for route for destination " << dest);
113
114 Ipv4AddressTrieMap::const_iterator longest_prefix_map = m_routes.longest_prefix_match (dest);
115 if (longest_prefix_map == m_routes.end ())
116 {
117 return 0;
118 }
119
120 const Ipv4RoutingTableEntry & entry = longest_prefix_map->second->get<i_index> ()[0];
121
122 if (oif != 0 && oif == m_ipv4->GetNetDevice (entry.GetInterface ()))
123 {
124 NS_LOG_LOGIC ("Route points to the incoming interface. Return empty route");
125 return 0;
126 }
127
128 // create a Ipv4Route object from the selected routing table entry
129 Ptr<Ipv4Route> rtentry = Create<Ipv4Route> ();
130 rtentry->SetDestination (entry.GetDest ());
131 rtentry->SetSource (m_ipv4->GetAddress (entry.GetInterface (), 0).GetLocal ());
132 rtentry->SetGateway (entry.GetGateway ());
133 rtentry->SetOutputDevice (m_ipv4->GetNetDevice (entry.GetInterface ()));
134 return rtentry;
135}
136
137void
138Ipv4GlobalRoutingOrderedNexthops::DeleteRoutes ()
139{
140 m_routes.clear ();
141}
142
143void
144Ipv4GlobalRoutingOrderedNexthops::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const
145{
146 std::ostream* os = stream->GetStream ();
147 if (m_routes.size () > 0)
148 {
149 *os << "Destination Iface(Metric),...,Iface(Metric)" << std::endl;
150 for (Ipv4AddressTrieMap::const_iterator i=m_routes.begin (); i != m_routes.end (); i++)
151 {
152 std::ostringstream dest, gw;
153 const Ipv4RoutingTableEntry &route = i->second->get<i_index> ()[0];
154 dest << route.GetDest () << "/" << route.GetDestNetworkMask().GetPrefixLength ();
155 gw << route.GetGateway ();
156
157 *os << std::setiosflags (std::ios::left) << std::setw (15) << dest.str ();
158
159 for (EntryContainer::index<i_metric>::type::iterator metric = i->second->get<i_metric> ().begin ();
160 metric != i->second->get<i_metric> ().end ();
161 metric ++)
162 {
163 if (metric != i->second->get<i_metric> ().begin ())
164 *os << ",";
165 *os << metric->GetInterface () << "(" << metric->GetMetric () << ")";
166 }
167
168 // for (EntryContainer::iterator metric = i->second->begin ();
169 // metric != i->second->end ();
170 // metric ++)
171 // {
172 // if (metric != i->second->begin ())
173 // *os << ",";
174 // *os << metric->GetInterface () << "(" << metric->GetMetric () << ")";
175 // }
176
177 *os << std::endl;
178 }
179 }
180}
181
182Ptr<Ipv4Route>
183Ipv4GlobalRoutingOrderedNexthops::RouteOutput (Ptr<Packet> p, const Ipv4Header &header,
184 Ptr<NetDevice> oif, Socket::SocketErrno &sockerr)
185{
186//
187// First, see if this is a multicast packet we have a route for. If we
188// have a route, then send the packet down each of the specified interfaces.
189//
190 if (header.GetDestination ().IsMulticast ())
191 {
192 NS_LOG_LOGIC ("Multicast destination-- returning false");
193 return 0; // Let other routing protocols try to handle this
194 }
195//
196// See if this is a unicast packet we have a route for.
197//
198 NS_LOG_LOGIC ("Unicast destination- looking up");
199 Ptr<Ipv4Route> rtentry = LookupGlobal (header.GetDestination (), oif);
200 if (rtentry)
201 {
202 sockerr = Socket::ERROR_NOTERROR;
203 }
204 else
205 {
206 sockerr = Socket::ERROR_NOROUTETOHOST;
207 }
208 return rtentry;
209}
210
211bool
212Ipv4GlobalRoutingOrderedNexthops::RouteInput (Ptr<const Packet> p, const Ipv4Header &header,
213 Ptr<const NetDevice> idev,
214 UnicastForwardCallback ucb, MulticastForwardCallback mcb,
215 LocalDeliverCallback lcb, ErrorCallback ecb)
216{
217
218 NS_LOG_FUNCTION (this << p << header << header.GetSource () << header.GetDestination () << idev);
219 // Check if input device supports IP
220 NS_ASSERT (m_ipv4->GetInterfaceForDevice (idev) >= 0);
221 uint32_t iif = m_ipv4->GetInterfaceForDevice (idev);
222
223 if (header.GetDestination ().IsMulticast ())
224 {
225 NS_LOG_LOGIC ("Multicast destination-- returning false");
226 return false; // Let other routing protocols try to handle this
227 }
228
229 if (header.GetDestination ().IsBroadcast ())
230 {
231 NS_LOG_LOGIC ("For me (Ipv4Addr broadcast address)");
232 // TODO: Local Deliver for broadcast
233 // TODO: Forward broadcast
234 }
235
236 // TODO: Configurable option to enable RFC 1222 Strong End System Model
237 // Right now, we will be permissive and allow a source to send us
238 // a packet to one of our other interface addresses; that is, the
239 // destination unicast address does not match one of the iif addresses,
240 // but we check our other interfaces. This could be an option
241 // (to remove the outer loop immediately below and just check iif).
242 for (uint32_t j = 0; j < m_ipv4->GetNInterfaces (); j++)
243 {
244 for (uint32_t i = 0; i < m_ipv4->GetNAddresses (j); i++)
245 {
246 Ipv4InterfaceAddress iaddr = m_ipv4->GetAddress (j, i);
247 Ipv4Address addr = iaddr.GetLocal ();
248 if (addr.IsEqual (header.GetDestination ()))
249 {
250 if (j == iif)
251 {
252 NS_LOG_LOGIC ("For me (destination " << addr << " match)");
253 }
254 else
255 {
256 NS_LOG_LOGIC ("For me (destination " << addr << " match) on another interface " << header.GetDestination ());
257 }
258 lcb (p, header, iif);
259 return true;
260 }
261 if (header.GetDestination ().IsEqual (iaddr.GetBroadcast ()))
262 {
263 NS_LOG_LOGIC ("For me (interface broadcast address)");
264 lcb (p, header, iif);
265 return true;
266 }
267 NS_LOG_LOGIC ("Address "<< addr << " not a match");
268 }
269 }
270 // Check if input device supports IP forwarding
271 if (m_ipv4->IsForwarding (iif) == false)
272 {
273 NS_LOG_LOGIC ("Forwarding disabled for this interface");
274 ecb (p, header, Socket::ERROR_NOROUTETOHOST);
275 return false;
276 }
277 // Next, try to find a route
278 NS_LOG_LOGIC ("Unicast destination- looking up global route");
279 Ptr<Ipv4Route> rtentry = LookupGlobal (header.GetDestination ());
280 if (rtentry != 0)
281 {
282 NS_LOG_LOGIC ("Found unicast destination- calling unicast callback");
283 ucb (rtentry, p, header);
284 return true;
285 }
286 else
287 {
288 NS_LOG_LOGIC ("Did not find unicast destination- returning false");
289 return false; // Let other routing protocols try to handle this
290 // route request.
291 }
292}
293
294} // namespace ns3