Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -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_RTNL_HANDLER_ |
| 6 | #define SHILL_RTNL_HANDLER_ |
| 7 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 8 | #include <string> |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 11 | #include <base/callback.h> |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 12 | #include <base/hash_tables.h> |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 13 | #include <base/lazy_instance.h> |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 14 | #include <base/memory/ref_counted.h> |
| 15 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 16 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 17 | |
| 18 | #include "shill/device.h" |
| 19 | |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 20 | #include "shill/event_dispatcher.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 21 | #include "shill/io_handler.h" |
| 22 | #include "shill/rtnl_listener.h" |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 23 | #include "shill/rtnl_message.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 24 | |
| 25 | struct nlmsghdr; |
| 26 | |
| 27 | namespace shill { |
| 28 | |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 29 | class Error; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 30 | class IPConfig; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 31 | class Sockets; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 32 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 33 | // This singleton class is responsible for interacting with the RTNL subsystem. |
| 34 | // RTNL provides (among other things) access to interface discovery (add/remove |
| 35 | // events), interface state monitoring and the ability to change interace flags. |
| 36 | // Similar functionality also exists for IP address configuration for interfaces |
| 37 | // and IP routing tables. |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 38 | // |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 39 | // RTNLHandler provides access to these events through a callback system and |
| 40 | // provides utility functions to make changes to interface, address and routing |
| 41 | // state. |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 42 | class RTNLHandler { |
| 43 | public: |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 44 | static const int kRequestLink = 1; |
| 45 | static const int kRequestAddr = 2; |
| 46 | static const int kRequestRoute = 4; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 47 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 48 | virtual ~RTNLHandler(); |
| 49 | |
mukesh agrawal | f407d59 | 2013-07-31 11:37:57 -0700 | [diff] [blame] | 50 | // Since this is a singleton, use RTNHandler::GetInstance()->Foo(). |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 51 | static RTNLHandler *GetInstance(); |
| 52 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 53 | // This starts the event-monitoring function of the RTNL handler. This |
| 54 | // function requires an EventDispatcher pointer so it can add itself to the |
| 55 | // event loop. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 56 | virtual void Start(EventDispatcher *dispatcher, Sockets *sockets); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 57 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 58 | // Add an RTNL event listener to the list of entities that will |
| 59 | // be notified of RTNL events. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 60 | virtual void AddListener(RTNLListener *to_add); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 61 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 62 | // Remove a previously added RTNL event listener |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 63 | virtual void RemoveListener(RTNLListener *to_remove); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 64 | |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 65 | // Set flags on a network interface that has a kernel index of |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 66 | // 'interface_index'. Only the flags bits set in 'change' will |
| 67 | // be set, and they will be set to the corresponding bit in 'flags'. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 68 | virtual void SetInterfaceFlags(int interface_index, |
| 69 | unsigned int flags, |
| 70 | unsigned int change); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 71 | |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 72 | // Set address of a network interface that has a kernel index of |
| 73 | // 'interface_index'. |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 74 | virtual bool AddInterfaceAddress(int interface_index, |
| 75 | const IPAddress &local, |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 76 | const IPAddress &gateway, |
| 77 | const IPAddress &peer); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 78 | |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 79 | // Remove address from a network interface that has a kernel index of |
| 80 | // 'interface_index'. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 81 | virtual bool RemoveInterfaceAddress(int interface_index, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 82 | const IPAddress &local); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 83 | |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 84 | // Remove a network interface from the kernel. |
| 85 | virtual bool RemoveInterface(int interface_index); |
| 86 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 87 | // Request that various tables (link, address, routing) tables be |
| 88 | // exhaustively dumped via RTNL. As results arrive from the kernel |
| 89 | // they will be broadcast to all listeners. The possible values |
| 90 | // (multiple can be ORred together) are below. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 91 | virtual void RequestDump(int request_flags); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 92 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 93 | // Returns the index of interface |interface_name|, or -1 if unable to |
| 94 | // determine the index. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 95 | virtual int GetInterfaceIndex(const std::string &interface_name); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 96 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 97 | // Send a formatted RTNL message. The sequence number in the message is set. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 98 | virtual bool SendMessage(RTNLMessage *message); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 99 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 100 | protected: |
| 101 | RTNLHandler(); |
| 102 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 103 | private: |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 104 | friend struct base::DefaultLazyInstanceTraits<RTNLHandler>; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 105 | friend class CellularTest; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 106 | friend class DeviceInfoTest; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 107 | friend class ModemTest; |
| 108 | friend class RTNLHandlerTest; |
Han Shen | 4afba20 | 2012-12-17 08:48:35 -0800 | [diff] [blame] | 109 | friend class RTNLListenerTest; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 110 | friend class RoutingTableTest; |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 111 | |
Darin Petkov | ca432fc | 2011-07-08 15:56:57 -0700 | [diff] [blame] | 112 | FRIEND_TEST(RTNLListenerTest, NoRun); |
| 113 | FRIEND_TEST(RTNLListenerTest, Run); |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 114 | FRIEND_TEST(RoutingTableTest, RouteDeleteForeign); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 115 | |
Julius Werner | f253c84 | 2012-12-20 14:52:30 -0800 | [diff] [blame] | 116 | static const int kReceiveBufferSize; |
| 117 | |
Paul Stewart | 65c40f5 | 2011-08-08 07:27:46 -0700 | [diff] [blame] | 118 | // This stops the event-monitoring function of the RTNL handler -- it is |
| 119 | // private since it will never happen in normal running, but is useful for |
| 120 | // tests. |
| 121 | void Stop(); |
| 122 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 123 | // Dispatches an rtnl message to all listeners |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 124 | void DispatchEvent(int type, const RTNLMessage &msg); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 125 | // Send the next table-dump request to the kernel |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 126 | void NextRequest(uint32 seq); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 127 | // Parse an incoming rtnl message from the kernel |
| 128 | void ParseRTNL(InputData *data); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 129 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 130 | bool AddressRequest(int interface_index, |
| 131 | RTNLMessage::Mode mode, |
| 132 | int flags, |
| 133 | const IPAddress &local, |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 134 | const IPAddress &gateway, |
| 135 | const IPAddress &peer); |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 136 | |
| 137 | // Called by the RTNL read handler on exceptional events. |
| 138 | void OnReadError(const Error &error); |
| 139 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 140 | Sockets *sockets_; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 141 | bool in_request_; |
| 142 | |
| 143 | int rtnl_socket_; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 144 | uint32 request_flags_; |
| 145 | uint32 request_sequence_; |
| 146 | uint32 last_dump_sequence_; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 147 | |
| 148 | std::vector<RTNLListener *> listeners_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 149 | base::Callback<void(InputData *)> rtnl_callback_; |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 150 | scoped_ptr<IOHandler> rtnl_handler_; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 151 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 152 | DISALLOW_COPY_AND_ASSIGN(RTNLHandler); |
| 153 | }; |
| 154 | |
| 155 | } // namespace shill |
| 156 | |
| 157 | #endif // SHILL_RTNL_HANDLER_ |