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