blob: 90eccbe05a54708672057e9350e1dd692c4bb75b [file] [log] [blame]
mukesh agrawald4ef6772012-02-21 16:28:04 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75e89d22011-08-01 10:00:02 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_ROUTING_TABLE_
6#define SHILL_ROUTING_TABLE_
7
8#include <string>
Hristo Stefanoved2c28c2011-11-29 15:37:30 -08009#include <vector>
Paul Stewart75e89d22011-08-01 10:00:02 -070010
11#include <base/callback_old.h>
12#include <base/hash_tables.h>
Paul Stewart0d2ada32011-08-09 17:01:57 -070013#include <base/lazy_instance.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070014#include <base/memory/ref_counted.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070015#include <base/memory/scoped_ptr.h>
16
17#include "shill/ip_address.h"
18#include "shill/refptr_types.h"
19#include "shill/rtnl_message.h"
20
Paul Stewart75e89d22011-08-01 10:00:02 -070021namespace shill {
22
23class RoutingTableEntry;
24class RTNLListener;
25
26// This singleton maintains an in-process copy of the routing table on
27// a per-interface basis. It offers the ability for other modules to
28// make modifications to the routing table, centered around setting the
29// default route for an interface or modifying its metric (priority).
30class RoutingTable {
31 public:
Paul Stewart0d2ada32011-08-09 17:01:57 -070032 virtual ~RoutingTable();
33
Paul Stewart75e89d22011-08-01 10:00:02 -070034 static RoutingTable *GetInstance();
35
Paul Stewartdd60e452011-08-08 11:38:36 -070036 virtual void Start();
37 virtual void Stop();
Paul Stewart75e89d22011-08-01 10:00:02 -070038
Paul Stewartc8f4bef2011-12-13 09:45:51 -080039 // Add an entry to the routing table.
Paul Stewartdd60e452011-08-08 11:38:36 -070040 virtual bool AddRoute(int interface_index, const RoutingTableEntry &entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070041
Paul Stewartc8f4bef2011-12-13 09:45:51 -080042 // Get the default route associated with an interface of a given addr family.
mukesh agrawald4ef6772012-02-21 16:28:04 -080043 // The route is copied into |*entry|.
Paul Stewartdd60e452011-08-08 11:38:36 -070044 virtual bool GetDefaultRoute(int interface_index,
45 IPAddress::Family family,
46 RoutingTableEntry *entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070047
48 // Set the default route for an interface, given an ipconfig entry
Paul Stewartdd60e452011-08-08 11:38:36 -070049 virtual bool SetDefaultRoute(int interface_index,
50 const IPConfigRefPtr &ipconfig,
51 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070052
Paul Stewartc8f4bef2011-12-13 09:45:51 -080053 // Remove all routes associated with interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070054 virtual void FlushRoutes(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070055
Paul Stewartc8f4bef2011-12-13 09:45:51 -080056 // Flush the routing cache for all interfaces.
57 virtual bool FlushCache();
58
59 // Reset local state for this interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070060 virtual void ResetTable(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070061
Paul Stewartc8f4bef2011-12-13 09:45:51 -080062 // Set the metric (priority) on existing default routes for an interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070063 virtual void SetDefaultMetric(int interface_index, uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070064
Paul Stewart0d2ada32011-08-09 17:01:57 -070065 protected:
Paul Stewart75e89d22011-08-01 10:00:02 -070066 RoutingTable();
Paul Stewart0d2ada32011-08-09 17:01:57 -070067
68 private:
69 friend struct base::DefaultLazyInstanceTraits<RoutingTable>;
70 friend class RoutingTableTest;
Paul Stewart75e89d22011-08-01 10:00:02 -070071
Chris Masone2aa97072011-08-09 17:35:08 -070072 void RouteMsgHandler(const RTNLMessage &msg);
Paul Stewart75e89d22011-08-01 10:00:02 -070073 bool ApplyRoute(uint32 interface_index,
74 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -070075 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -070076 unsigned int flags);
mukesh agrawald4ef6772012-02-21 16:28:04 -080077 // Get the default route associated with an interface of a given addr family.
78 // A pointer to the route is placed in |*entry|.
79 virtual bool GetDefaultRouteInternal(int interface_index,
80 IPAddress::Family family,
81 RoutingTableEntry **entry);
82
Paul Stewartc1dec4d2011-12-08 15:25:28 -080083 void ReplaceMetric(uint32 interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -080084 RoutingTableEntry *entry,
Paul Stewartc1dec4d2011-12-08 15:25:28 -080085 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070086
87 static const char kRouteFlushPath4[];
88 static const char kRouteFlushPath6[];
89
Hristo Stefanoved2c28c2011-11-29 15:37:30 -080090 base::hash_map<int, std::vector<RoutingTableEntry> > tables_; // NOLINT
Paul Stewartc8f4bef2011-12-13 09:45:51 -080091 // NOLINT above: hash_map from base, no need to #include <hash_map>.
Hristo Stefanoved2c28c2011-11-29 15:37:30 -080092
Chris Masone2aa97072011-08-09 17:35:08 -070093 scoped_ptr<Callback1<const RTNLMessage &>::Type> route_callback_;
Paul Stewart75e89d22011-08-01 10:00:02 -070094 scoped_ptr<RTNLListener> route_listener_;
95
96 DISALLOW_COPY_AND_ASSIGN(RoutingTable);
97};
98
99} // namespace shill
100
101#endif // SHILL_ROUTING_TABLE_