blob: af2543aaca2d309f04441d01f8f57c8930bfef99 [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart0af98bf2011-05-10 17:38:08 -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_DEVICE_INFO_
6#define SHILL_DEVICE_INFO_
7
Darin Petkove6193c02011-08-11 12:42:40 -07008#include <map>
mukesh agrawal8f317b62011-07-15 11:53:23 -07009#include <set>
10#include <string>
Paul Stewart9a908082011-08-31 12:18:48 -070011#include <vector>
mukesh agrawal8f317b62011-07-15 11:53:23 -070012
Eric Shienbrood3e20a232012-02-16 11:35:56 -050013#include <base/callback.h>
Paul Stewart050cfc02012-07-06 20:38:54 -070014#include <base/cancelable_callback.h>
Paul Stewartca876ee2012-04-21 08:55:58 -070015#include <base/file_path.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070016#include <base/memory/ref_counted.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070017#include <base/memory/scoped_ptr.h>
Darin Petkov5a850472012-06-06 15:44:24 +020018#include <base/memory/weak_ptr.h>
Darin Petkov0828f5f2011-08-11 10:18:52 -070019#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewart0af98bf2011-05-10 17:38:08 -070020
Darin Petkove3e1cfa2011-08-11 13:41:17 -070021#include "shill/byte_string.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070022#include "shill/device.h"
Paul Stewart9a908082011-08-31 12:18:48 -070023#include "shill/ip_address.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070024#include "shill/rtnl_listener.h"
Paul Stewartcba0f7f2012-02-29 16:33:05 -080025#include "shill/technology.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070026
27namespace shill {
28
Paul Stewartb50f0b92011-05-16 16:31:42 -070029class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080030class Metrics;
Paul Stewart2ddf2c62013-04-16 09:47:34 -070031class NetlinkManager;
Wade Guthrie7347bf22013-04-30 11:21:51 -070032class Nl80211Message;
Paul Stewart8c116a92012-05-02 18:30:03 -070033class RoutingTable;
Paul Stewart9a908082011-08-31 12:18:48 -070034class RTNLHandler;
Chris Masone2aa97072011-08-09 17:35:08 -070035class RTNLMessage;
Gary Morain41780232012-07-31 15:08:31 -070036class Sockets;
Paul Stewartb50f0b92011-05-16 16:31:42 -070037
Darin Petkov5a850472012-06-06 15:44:24 +020038class DeviceInfo : public base::SupportsWeakPtr<DeviceInfo> {
Paul Stewart0af98bf2011-05-10 17:38:08 -070039 public:
Paul Stewart9a908082011-08-31 12:18:48 -070040 struct AddressData {
41 AddressData()
Paul Stewart7355ce12011-09-02 10:47:01 -070042 : address(IPAddress::kFamilyUnknown), flags(0), scope(0) {}
Paul Stewart9a908082011-08-31 12:18:48 -070043 AddressData(const IPAddress &address_in,
44 unsigned char flags_in,
45 unsigned char scope_in)
46 : address(address_in), flags(flags_in), scope(scope_in) {}
47 IPAddress address;
48 unsigned char flags;
49 unsigned char scope;
50 };
51
Jason Glasgowabc54032012-04-20 16:08:32 -040052 // Device name prefix for modem pseudo devices used in testing.
53 static const char kModemPseudoDeviceNamePrefix[];
Christopher Wileye049cc52012-09-10 14:36:57 -070054 // Device name prefix for virtual ethernet devices used in testing.
55 static const char kEthernetPseudoDeviceNamePrefix[];
Ben Chanb061f892013-02-27 17:46:55 -080056 // Time interval for polling for link statistics.
57 static const int kRequestLinkStatisticsIntervalMilliseconds;
Jason Glasgowabc54032012-04-20 16:08:32 -040058
Paul Stewartb50f0b92011-05-16 16:31:42 -070059 DeviceInfo(ControlInterface *control_interface,
60 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080061 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070062 Manager *manager);
Darin Petkov5a850472012-06-06 15:44:24 +020063 virtual ~DeviceInfo();
Darin Petkov887f2982011-07-14 16:10:17 -070064
mukesh agrawal8f317b62011-07-15 11:53:23 -070065 void AddDeviceToBlackList(const std::string &device_name);
Eric Shienbrood5e628a52012-03-21 16:56:59 -040066 bool IsDeviceBlackListed(const std::string &device_name);
Paul Stewart0af98bf2011-05-10 17:38:08 -070067 void Start();
68 void Stop();
Darin Petkov887f2982011-07-14 16:10:17 -070069
Ben Chan5086b972013-01-15 21:51:38 -080070 std::vector<std::string> GetUninitializedTechnologies() const;
71
Darin Petkov6f9eaa32011-08-09 15:26:44 -070072 // Adds |device| to this DeviceInfo instance so that we can handle its link
73 // messages, and registers it with the manager.
David Rochbergfa1d31d2012-03-20 10:38:07 -040074 virtual void RegisterDevice(const DeviceRefPtr &device);
Darin Petkov6f9eaa32011-08-09 15:26:44 -070075
Jason Glasgowe9089492012-02-23 17:57:37 -050076 // Remove |device| from this DeviceInfo. This function should only
77 // be called for cellular devices because the lifetime of the
78 // cellular devices is controlled by the Modem object and its
79 // communication to modem manager, rather than by RTNL messages.
David Rochbergfa1d31d2012-03-20 10:38:07 -040080 virtual void DeregisterDevice(const DeviceRefPtr &device);
Jason Glasgowe9089492012-02-23 17:57:37 -050081
Paul Stewartc8f4bef2011-12-13 09:45:51 -080082 virtual DeviceRefPtr GetDevice(int interface_index) const;
Paul Stewart32852962011-08-30 14:06:53 -070083 virtual bool GetMACAddress(int interface_index, ByteString *address) const;
Gary Morainbf74a672012-07-30 16:27:19 -070084
85 // Queries the kernel for a MAC address for |interface_index|. Returns an
86 // empty ByteString on failure.
87 virtual ByteString GetMACAddressFromKernel(int interface_index) const;
Gary Morain41780232012-07-31 15:08:31 -070088
Chris Masone626719f2011-08-18 16:58:48 -070089 virtual bool GetFlags(int interface_index, unsigned int *flags) const;
Paul Stewart1ac4e842012-07-10 12:58:12 -070090 virtual bool GetByteCounts(int interface_index,
91 uint64 *rx_bytes, uint64 *tx_bytes) const;
Paul Stewart9a908082011-08-31 12:18:48 -070092 virtual bool GetAddresses(int interface_index,
93 std::vector<AddressData> *addresses) const;
Paul Stewart05a42c22012-08-02 16:47:21 -070094 // Flush all addresses associated with |interface_index|.
Paul Stewart9a908082011-08-31 12:18:48 -070095 virtual void FlushAddresses(int interface_index) const;
Paul Stewart05a42c22012-08-02 16:47:21 -070096 // Returns whether this interface does not have |this_address|
97 // but has another non-temporary address of the same family.
98 virtual bool HasOtherAddress(
99 int interface_index, const IPAddress &this_address) const;
Paul Stewartca6abd42012-03-01 15:45:29 -0800100 virtual bool CreateTunnelInterface(std::string *interface_name) const;
101 virtual bool DeleteInterface(int interface_index) const;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700102
Darin Petkovf8046b82012-04-24 16:29:23 +0200103 // Returns the interface index for |interface_name| or -1 if unknown.
104 virtual int GetIndex(const std::string &interface_name) const;
105
Paul Stewart0af98bf2011-05-10 17:38:08 -0700106 private:
Paul Stewart050cfc02012-07-06 20:38:54 -0700107 friend class DeviceInfoDelayedCreationTest;
Paul Stewartca876ee2012-04-21 08:55:58 -0700108 friend class DeviceInfoTechnologyTest;
Darin Petkov887f2982011-07-14 16:10:17 -0700109 friend class DeviceInfoTest;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700110 FRIEND_TEST(CellularTest, StartLinked);
Ben Chan4b285862012-10-10 22:52:16 -0700111 FRIEND_TEST(DeviceInfoTest, CreateDeviceWiMax);
Ben Chan5086b972013-01-15 21:51:38 -0800112 FRIEND_TEST(DeviceInfoTest, GetUninitializedTechnologies);
Thieu Leb27beee2012-04-20 09:19:06 -0700113 FRIEND_TEST(DeviceInfoTest, HasSubdir); // For HasSubdir.
Paul Stewart1ac4e842012-07-10 12:58:12 -0700114 FRIEND_TEST(DeviceInfoTest, RequestLinkStatistics);
Paul Stewart8c116a92012-05-02 18:30:03 -0700115 FRIEND_TEST(DeviceInfoTest, StartStop);
Darin Petkov887f2982011-07-14 16:10:17 -0700116
Darin Petkove6193c02011-08-11 12:42:40 -0700117 struct Info {
Ben Chan5086b972013-01-15 21:51:38 -0800118 Info()
119 : flags(0),
120 rx_bytes(0),
121 tx_bytes(0),
122 has_addresses_only(false),
123 technology(Technology::kUnknown)
124 {}
Darin Petkove6193c02011-08-11 12:42:40 -0700125
126 DeviceRefPtr device;
Darin Petkovf8046b82012-04-24 16:29:23 +0200127 std::string name;
Paul Stewart32852962011-08-30 14:06:53 -0700128 ByteString mac_address;
Paul Stewart9a908082011-08-31 12:18:48 -0700129 std::vector<AddressData> ip_addresses;
Darin Petkove6193c02011-08-11 12:42:40 -0700130 unsigned int flags;
Paul Stewart1ac4e842012-07-10 12:58:12 -0700131 uint64 rx_bytes;
132 uint64 tx_bytes;
133
Paul Stewart8c116a92012-05-02 18:30:03 -0700134 // This flag indicates that link information has not been retrieved yet;
135 // only the ip_addresses field is valid.
136 bool has_addresses_only;
Ben Chan5086b972013-01-15 21:51:38 -0800137
138 Technology::Identifier technology;
Darin Petkove6193c02011-08-11 12:42:40 -0700139 };
140
Paul Stewartca876ee2012-04-21 08:55:58 -0700141 // Root of the kernel sysfs directory holding network device info.
142 static const char kDeviceInfoRoot[];
Ben Chan4e64d2d2012-05-16 00:02:25 -0700143 // Name of the "cdc_ether" driver. This driver is not included in the
144 // kModemDrivers list because we need to do additional checking.
145 static const char kDriverCdcEther[];
146 // Name of the GDM WiMAX driver.
147 static const char kDriverGdmWiMax[];
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700148 // Name of the virtio network driver.
149 static const char kDriverVirtioNet[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700150 // Sysfs path to a device uevent file.
Paul Stewartbf1861b2011-08-23 15:45:35 -0700151 static const char kInterfaceUevent[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700152 // Content of a device uevent file that indicates it is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800153 static const char kInterfaceUeventWifiSignature[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700154 // Sysfs path to a device via its interface name.
155 static const char kInterfaceDevice[];
156 // Sysfs path to the driver of a device via its interface name.
Paul Stewartbf1861b2011-08-23 15:45:35 -0700157 static const char kInterfaceDriver[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700158 // Sysfs path to the file that is used to determine if this is tun device.
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800159 static const char kInterfaceTunFlags[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700160 // Sysfs path to the file that is used to determine if a wifi device is
161 // operating in monitor mode.
Paul Stewart2001a422011-12-15 10:20:09 -0800162 static const char kInterfaceType[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700163 // Modem drivers that we support.
Paul Stewartb50f0b92011-05-16 16:31:42 -0700164 static const char *kModemDrivers[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700165 // Path to the tun device.
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800166 static const char kTunDeviceName[];
Paul Stewart050cfc02012-07-06 20:38:54 -0700167 // Time to wait before registering devices which need extra time to detect.
168 static const int kDelayedDeviceCreationSeconds;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700169
Paul Stewart8c116a92012-05-02 18:30:03 -0700170 // Create a Device object for the interface named |linkname|, with a
171 // string-form MAC address |address|, whose kernel interface index
172 // is |interface_index| and detected technology is |technology|.
Paul Stewart050cfc02012-07-06 20:38:54 -0700173 virtual DeviceRefPtr CreateDevice(const std::string &link_name,
174 const std::string &address,
175 int interface_index,
176 Technology::Identifier technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700177
Paul Stewartca876ee2012-04-21 08:55:58 -0700178 // Return the FilePath for a given |path_name| in the device sysinfo for
179 // a specific interface |iface_name|.
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800180 base::FilePath GetDeviceInfoPath(const std::string &iface_name,
Paul Stewartca876ee2012-04-21 08:55:58 -0700181 const std::string &path_name);
182 // Return the contents of the device info file |path_name| for interface
183 // |iface_name| in output parameter |contents_out|. Returns true if file
184 // read succeeded, false otherwise.
185 bool GetDeviceInfoContents(const std::string &iface_name,
186 const std::string &path_name,
187 std::string *contents_out);
188
189 // Return the filepath for the target of the device info symbolic link
190 // |path_name| for interface |iface_name| in output parameter |path_out|.
191 // Returns true if symbolic link read succeeded, false otherwise.
192 bool GetDeviceInfoSymbolicLink(const std::string &iface_name,
193 const std::string &path_name,
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800194 base::FilePath *path_out);
Paul Stewartca876ee2012-04-21 08:55:58 -0700195 // Classify the device named |iface_name|, and return an identifier
196 // indicating its type.
Paul Stewart050cfc02012-07-06 20:38:54 -0700197 virtual Technology::Identifier GetDeviceTechnology(
198 const std::string &iface_name);
Thieu Le8f1c8352012-04-16 11:02:12 -0700199 // Checks the device specified by |iface_name| to see if it's a modem device.
200 // This method assumes that |iface_name| has already been determined to be
201 // using the cdc_ether driver.
Paul Stewartca876ee2012-04-21 08:55:58 -0700202 bool IsCdcEtherModemDevice(const std::string &iface_name);
Thieu Leb27beee2012-04-20 09:19:06 -0700203 // Returns true if |base_dir| has a subdirectory named |subdir|.
204 // |subdir| can be an immediate subdirectory of |base_dir| or can be
205 // several levels deep.
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800206 static bool HasSubdir(const base::FilePath &base_dir,
207 const base::FilePath &subdir);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700208
Chris Masone2aa97072011-08-09 17:35:08 -0700209 void AddLinkMsgHandler(const RTNLMessage &msg);
210 void DelLinkMsgHandler(const RTNLMessage &msg);
211 void LinkMsgHandler(const RTNLMessage &msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700212 void AddressMsgHandler(const RTNLMessage &msg);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700213
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700214 const Info *GetInfo(int interface_index) const;
Darin Petkove6193c02011-08-11 12:42:40 -0700215 void RemoveInfo(int interface_index);
Paul Stewart050cfc02012-07-06 20:38:54 -0700216 void DelayDeviceCreation(int interface_index);
217 void DelayedDeviceCreationTask();
Paul Stewart1ac4e842012-07-10 12:58:12 -0700218 void RetrieveLinkStatistics(int interface_index, const RTNLMessage &msg);
219 void RequestLinkStatistics();
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700220
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700221 // Use nl80211 to get information on |interface_index|.
222 void GetWiFiInterfaceInfo(int interface_index);
Wade Guthrie7347bf22013-04-30 11:21:51 -0700223 void OnWiFiInterfaceInfoReceived(const Nl80211Message &message);
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700224
Gary Morain41780232012-07-31 15:08:31 -0700225 void set_sockets(Sockets* sockets) { sockets_.reset(sockets); }
226
Paul Stewartb50f0b92011-05-16 16:31:42 -0700227 ControlInterface *control_interface_;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700228 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800229 Metrics *metrics_;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700230 Manager *manager_;
Darin Petkovf8046b82012-04-24 16:29:23 +0200231
232 std::map<int, Info> infos_; // Maps interface index to Info.
233 std::map<std::string, int> indices_; // Maps interface name to index.
234
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500235 base::Callback<void(const RTNLMessage &)> link_callback_;
236 base::Callback<void(const RTNLMessage &)> address_callback_;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700237 scoped_ptr<RTNLListener> link_listener_;
Paul Stewart9a908082011-08-31 12:18:48 -0700238 scoped_ptr<RTNLListener> address_listener_;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700239 std::set<std::string> black_list_;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800240 base::FilePath device_info_root_;
Darin Petkove6193c02011-08-11 12:42:40 -0700241
Paul Stewart1ac4e842012-07-10 12:58:12 -0700242 // Keep track of devices that require a delayed call to CreateDevice().
Paul Stewart050cfc02012-07-06 20:38:54 -0700243 base::CancelableClosure delayed_devices_callback_;
244 std::set<int> delayed_devices_;
245
Paul Stewart1ac4e842012-07-10 12:58:12 -0700246 // Maintain a callback for the periodic link statistics poll task.
247 base::CancelableClosure request_link_statistics_callback_;
248
Paul Stewart8c116a92012-05-02 18:30:03 -0700249 // Cache copy of singleton pointers.
250 RoutingTable *routing_table_;
Paul Stewart9a908082011-08-31 12:18:48 -0700251 RTNLHandler *rtnl_handler_;
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700252 NetlinkManager *netlink_manager_;
Paul Stewart9a908082011-08-31 12:18:48 -0700253
Gary Morain41780232012-07-31 15:08:31 -0700254 // A member of the class so that a mock can be injected for testing.
255 scoped_ptr<Sockets> sockets_;
256
Darin Petkove6193c02011-08-11 12:42:40 -0700257 DISALLOW_COPY_AND_ASSIGN(DeviceInfo);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700258};
259
Paul Stewart0af98bf2011-05-10 17:38:08 -0700260} // namespace shill
261
262#endif // SHILL_DEVICE_INFO_