blob: 228b9660b82ce6a67efb6b7fff26e6f9cbdb96f4 [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
Paul Stewartf748a362012-03-07 12:01:20 -08008#include <queue>
Paul Stewart75e89d22011-08-01 10:00:02 -07009#include <string>
Hristo Stefanoved2c28c2011-11-29 15:37:30 -080010#include <vector>
Paul Stewart75e89d22011-08-01 10:00:02 -070011
Eric Shienbrood3e20a232012-02-16 11:35:56 -050012#include <base/callback.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070013#include <base/hash_tables.h>
Paul Stewart0d2ada32011-08-09 17:01:57 -070014#include <base/lazy_instance.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070015#include <base/memory/ref_counted.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070016#include <base/memory/scoped_ptr.h>
17
18#include "shill/ip_address.h"
19#include "shill/refptr_types.h"
20#include "shill/rtnl_message.h"
21
Paul Stewart75e89d22011-08-01 10:00:02 -070022namespace shill {
23
24class RoutingTableEntry;
Paul Stewartf748a362012-03-07 12:01:20 -080025class RTNLHandler;
Paul Stewart75e89d22011-08-01 10:00:02 -070026class RTNLListener;
Paul Stewartf748a362012-03-07 12:01:20 -080027class RTNLMessage;
Paul Stewart75e89d22011-08-01 10:00:02 -070028
29// This singleton maintains an in-process copy of the routing table on
30// a per-interface basis. It offers the ability for other modules to
31// make modifications to the routing table, centered around setting the
32// default route for an interface or modifying its metric (priority).
33class RoutingTable {
34 public:
Paul Stewart0d2ada32011-08-09 17:01:57 -070035 virtual ~RoutingTable();
36
Paul Stewart75e89d22011-08-01 10:00:02 -070037 static RoutingTable *GetInstance();
38
Paul Stewartdd60e452011-08-08 11:38:36 -070039 virtual void Start();
40 virtual void Stop();
Paul Stewart75e89d22011-08-01 10:00:02 -070041
Paul Stewartc8f4bef2011-12-13 09:45:51 -080042 // Add an entry to the routing table.
Paul Stewartdd60e452011-08-08 11:38:36 -070043 virtual bool AddRoute(int interface_index, const RoutingTableEntry &entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070044
Paul Stewartc8f4bef2011-12-13 09:45:51 -080045 // Get the default route associated with an interface of a given addr family.
mukesh agrawald4ef6772012-02-21 16:28:04 -080046 // The route is copied into |*entry|.
Paul Stewartdd60e452011-08-08 11:38:36 -070047 virtual bool GetDefaultRoute(int interface_index,
48 IPAddress::Family family,
49 RoutingTableEntry *entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070050
Paul Stewart3f68bb12012-03-15 13:33:10 -070051 // Set the default route for an interface, given an ipconfig entry.
Paul Stewartdd60e452011-08-08 11:38:36 -070052 virtual bool SetDefaultRoute(int interface_index,
53 const IPConfigRefPtr &ipconfig,
54 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070055
Paul Stewart3f68bb12012-03-15 13:33:10 -070056 // Configure routing table entries from the "routes" portion of |ipconifg|.
57 // Returns true if all routes were installed successfully, false otherwise.
58 virtual bool ConfigureRoutes(int interface_index,
59 const IPConfigRefPtr &ipconfig,
60 uint32 metric);
61
Thieu Lecaef8932012-02-28 16:06:59 -080062 // Remove routes associated with interface.
Thieu Lefb46caf2012-03-08 11:57:15 -080063 // Route entries are immediately purged from our copy of the routing table.
64 virtual void FlushRoutes(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070065
Paul Stewartc8f4bef2011-12-13 09:45:51 -080066 // Flush the routing cache for all interfaces.
67 virtual bool FlushCache();
68
69 // Reset local state for this interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070070 virtual void ResetTable(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070071
Paul Stewartc8f4bef2011-12-13 09:45:51 -080072 // Set the metric (priority) on existing default routes for an interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070073 virtual void SetDefaultMetric(int interface_index, uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070074
Paul Stewartf748a362012-03-07 12:01:20 -080075 // Get the default route to |destination| through |interface_index| and
76 // create a host route to that destination.
77 virtual bool RequestRouteToHost(const IPAddress &destination,
78 int interface_index);
79
Paul Stewart0d2ada32011-08-09 17:01:57 -070080 protected:
Paul Stewart75e89d22011-08-01 10:00:02 -070081 RoutingTable();
Paul Stewart0d2ada32011-08-09 17:01:57 -070082
83 private:
84 friend struct base::DefaultLazyInstanceTraits<RoutingTable>;
85 friend class RoutingTableTest;
Paul Stewart75e89d22011-08-01 10:00:02 -070086
Paul Stewartf748a362012-03-07 12:01:20 -080087
88 static bool ParseRoutingTableMessage(const RTNLMessage &message,
89 int *interface_index,
90 RoutingTableEntry *entry);
Chris Masone2aa97072011-08-09 17:35:08 -070091 void RouteMsgHandler(const RTNLMessage &msg);
Paul Stewart75e89d22011-08-01 10:00:02 -070092 bool ApplyRoute(uint32 interface_index,
93 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -070094 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -070095 unsigned int flags);
mukesh agrawald4ef6772012-02-21 16:28:04 -080096 // Get the default route associated with an interface of a given addr family.
97 // A pointer to the route is placed in |*entry|.
98 virtual bool GetDefaultRouteInternal(int interface_index,
99 IPAddress::Family family,
100 RoutingTableEntry **entry);
101
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800102 void ReplaceMetric(uint32 interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800103 RoutingTableEntry *entry,
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800104 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700105
106 static const char kRouteFlushPath4[];
107 static const char kRouteFlushPath6[];
108
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800109 base::hash_map<int, std::vector<RoutingTableEntry> > tables_; // NOLINT
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800110 // NOLINT above: hash_map from base, no need to #include <hash_map>.
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800111
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500112 base::Callback<void(const RTNLMessage &)> route_callback_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700113 scoped_ptr<RTNLListener> route_listener_;
Paul Stewartf748a362012-03-07 12:01:20 -0800114 std::queue<uint32> route_query_sequences_;
115
116 // Cache singleton pointer for performance and test purposes.
117 RTNLHandler *rtnl_handler_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700118
119 DISALLOW_COPY_AND_ASSIGN(RoutingTable);
120};
121
122} // namespace shill
123
124#endif // SHILL_ROUTING_TABLE_