mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 2 | // 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 Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 8 | #include <queue> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 9 | #include <string> |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 10 | #include <vector> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 11 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 12 | #include <base/callback.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 13 | #include <base/hash_tables.h> |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 14 | #include <base/lazy_instance.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 15 | #include <base/memory/ref_counted.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 16 | #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 Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 22 | namespace shill { |
| 23 | |
| 24 | class RoutingTableEntry; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 25 | class RTNLHandler; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 26 | class RTNLListener; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 27 | class RTNLMessage; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 28 | |
| 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). |
| 33 | class RoutingTable { |
| 34 | public: |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 35 | virtual ~RoutingTable(); |
| 36 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 37 | static RoutingTable *GetInstance(); |
| 38 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 39 | virtual void Start(); |
| 40 | virtual void Stop(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 41 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 42 | // Add an entry to the routing table. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 43 | virtual bool AddRoute(int interface_index, const RoutingTableEntry &entry); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 44 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 45 | // Get the default route associated with an interface of a given addr family. |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 46 | // The route is copied into |*entry|. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 47 | virtual bool GetDefaultRoute(int interface_index, |
| 48 | IPAddress::Family family, |
| 49 | RoutingTableEntry *entry); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 50 | |
| 51 | // Set the default route for an interface, given an ipconfig entry |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 52 | virtual bool SetDefaultRoute(int interface_index, |
| 53 | const IPConfigRefPtr &ipconfig, |
| 54 | uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 55 | |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 56 | // Remove routes associated with interface. |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 57 | // Route entries are immediately purged from our copy of the routing table. |
| 58 | virtual void FlushRoutes(int interface_index); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 59 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 60 | // Flush the routing cache for all interfaces. |
| 61 | virtual bool FlushCache(); |
| 62 | |
| 63 | // Reset local state for this interface. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 64 | virtual void ResetTable(int interface_index); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 65 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 66 | // Set the metric (priority) on existing default routes for an interface. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 67 | virtual void SetDefaultMetric(int interface_index, uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 68 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 69 | // 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 Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 74 | protected: |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 75 | RoutingTable(); |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 76 | |
| 77 | private: |
| 78 | friend struct base::DefaultLazyInstanceTraits<RoutingTable>; |
| 79 | friend class RoutingTableTest; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 80 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 81 | |
| 82 | static bool ParseRoutingTableMessage(const RTNLMessage &message, |
| 83 | int *interface_index, |
| 84 | RoutingTableEntry *entry); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 85 | void RouteMsgHandler(const RTNLMessage &msg); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 86 | bool ApplyRoute(uint32 interface_index, |
| 87 | const RoutingTableEntry &entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 88 | RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 89 | unsigned int flags); |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 90 | // 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 Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 96 | void ReplaceMetric(uint32 interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 97 | RoutingTableEntry *entry, |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 98 | uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 99 | |
| 100 | static const char kRouteFlushPath4[]; |
| 101 | static const char kRouteFlushPath6[]; |
| 102 | |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 103 | base::hash_map<int, std::vector<RoutingTableEntry> > tables_; // NOLINT |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 104 | // NOLINT above: hash_map from base, no need to #include <hash_map>. |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 105 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame^] | 106 | base::Callback<void(const RTNLMessage &)> route_callback_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 107 | scoped_ptr<RTNLListener> route_listener_; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 108 | std::queue<uint32> route_query_sequences_; |
| 109 | |
| 110 | // Cache singleton pointer for performance and test purposes. |
| 111 | RTNLHandler *rtnl_handler_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 112 | |
| 113 | DISALLOW_COPY_AND_ASSIGN(RoutingTable); |
| 114 | }; |
| 115 | |
| 116 | } // namespace shill |
| 117 | |
| 118 | #endif // SHILL_ROUTING_TABLE_ |