blob: af2a8d67b500c729bb4231dbe32ac643e5f6a094 [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
51 // 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
Thieu Lecaef8932012-02-28 16:06:59 -080056 // Remove routes associated with interface.
Thieu Lefb46caf2012-03-08 11:57:15 -080057 // Route entries are immediately purged from our copy of the routing table.
58 virtual void FlushRoutes(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070059
Paul Stewartc8f4bef2011-12-13 09:45:51 -080060 // Flush the routing cache for all interfaces.
61 virtual bool FlushCache();
62
63 // Reset local state for this interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070064 virtual void ResetTable(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070065
Paul Stewartc8f4bef2011-12-13 09:45:51 -080066 // Set the metric (priority) on existing default routes for an interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070067 virtual void SetDefaultMetric(int interface_index, uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070068
Paul Stewartf748a362012-03-07 12:01:20 -080069 // Get the default route to |destination| through |interface_index| and
70 // create a host route to that destination.
71 virtual bool RequestRouteToHost(const IPAddress &destination,
72 int interface_index);
73
Paul Stewart0d2ada32011-08-09 17:01:57 -070074 protected:
Paul Stewart75e89d22011-08-01 10:00:02 -070075 RoutingTable();
Paul Stewart0d2ada32011-08-09 17:01:57 -070076
77 private:
78 friend struct base::DefaultLazyInstanceTraits<RoutingTable>;
79 friend class RoutingTableTest;
Paul Stewart75e89d22011-08-01 10:00:02 -070080
Paul Stewartf748a362012-03-07 12:01:20 -080081
82 static bool ParseRoutingTableMessage(const RTNLMessage &message,
83 int *interface_index,
84 RoutingTableEntry *entry);
Chris Masone2aa97072011-08-09 17:35:08 -070085 void RouteMsgHandler(const RTNLMessage &msg);
Paul Stewart75e89d22011-08-01 10:00:02 -070086 bool ApplyRoute(uint32 interface_index,
87 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -070088 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -070089 unsigned int flags);
mukesh agrawald4ef6772012-02-21 16:28:04 -080090 // Get the default route associated with an interface of a given addr family.
91 // A pointer to the route is placed in |*entry|.
92 virtual bool GetDefaultRouteInternal(int interface_index,
93 IPAddress::Family family,
94 RoutingTableEntry **entry);
95
Paul Stewartc1dec4d2011-12-08 15:25:28 -080096 void ReplaceMetric(uint32 interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -080097 RoutingTableEntry *entry,
Paul Stewartc1dec4d2011-12-08 15:25:28 -080098 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070099
100 static const char kRouteFlushPath4[];
101 static const char kRouteFlushPath6[];
102
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800103 base::hash_map<int, std::vector<RoutingTableEntry> > tables_; // NOLINT
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800104 // NOLINT above: hash_map from base, no need to #include <hash_map>.
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800105
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500106 base::Callback<void(const RTNLMessage &)> route_callback_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700107 scoped_ptr<RTNLListener> route_listener_;
Paul Stewartf748a362012-03-07 12:01:20 -0800108 std::queue<uint32> route_query_sequences_;
109
110 // Cache singleton pointer for performance and test purposes.
111 RTNLHandler *rtnl_handler_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700112
113 DISALLOW_COPY_AND_ASSIGN(RoutingTable);
114};
115
116} // namespace shill
117
118#endif // SHILL_ROUTING_TABLE_