blob: 8fcf3e6d9b507034033bb432dcdd88c145e84f4d [file] [log] [blame]
Darin Petkov41c0e0a2012-01-09 16:38:53 +01001// 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
Paul Stewartcba0f7f2012-02-29 16:33:05 -08005#include "shill/device_info.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -07006
Paul Stewart0af98bf2011-05-10 17:38:08 -07007#include <arpa/inet.h>
Paul Stewartcba0f7f2012-02-29 16:33:05 -08008#include <fcntl.h>
9#include <linux/if_tun.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070010#include <linux/netlink.h>
11#include <linux/rtnetlink.h>
Paul Stewartcba0f7f2012-02-29 16:33:05 -080012#include <net/if.h>
13#include <net/if_arp.h>
14#include <netinet/ether.h>
15#include <string.h>
16#include <sys/ioctl.h>
17#include <sys/socket.h>
18#include <time.h>
19#include <unistd.h>
20
Paul Stewart0af98bf2011-05-10 17:38:08 -070021#include <string>
22
Eric Shienbrood3e20a232012-02-16 11:35:56 -050023#include <base/bind.h>
Paul Stewartbf1861b2011-08-23 15:45:35 -070024#include <base/file_util.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070025#include <base/memory/scoped_ptr.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050026#include <base/stl_util.h>
Paul Stewart2001a422011-12-15 10:20:09 -080027#include <base/string_number_conversions.h>
Chris Masone877ff982011-09-21 16:18:24 -070028#include <base/string_util.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070029#include <base/stringprintf.h>
Paul Stewart0af98bf2011-05-10 17:38:08 -070030
31#include "shill/control_interface.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070032#include "shill/device.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070033#include "shill/device_stub.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070034#include "shill/ethernet.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070035#include "shill/logging.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070036#include "shill/manager.h"
Paul Stewart8c116a92012-05-02 18:30:03 -070037#include "shill/routing_table.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070038#include "shill/rtnl_handler.h"
39#include "shill/rtnl_listener.h"
Chris Masone2aa97072011-08-09 17:35:08 -070040#include "shill/rtnl_message.h"
Chris Masone487b8bf2011-05-13 16:27:57 -070041#include "shill/service.h"
Gary Morain41780232012-07-31 15:08:31 -070042#include "shill/sockets.h"
mukesh agrawal93a29ed2012-04-17 16:13:01 -070043#include "shill/virtio_ethernet.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070044#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070045
Eric Shienbrood3e20a232012-02-16 11:35:56 -050046using base::Bind;
Thieu Le8f1c8352012-04-16 11:02:12 -070047using base::StringPrintf;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050048using base::Unretained;
Darin Petkove6193c02011-08-11 12:42:40 -070049using std::map;
Paul Stewart050cfc02012-07-06 20:38:54 -070050using std::set;
Paul Stewart0af98bf2011-05-10 17:38:08 -070051using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070052using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070053
54namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070055
56// static
Jason Glasgowabc54032012-04-20 16:08:32 -040057const char DeviceInfo::kModemPseudoDeviceNamePrefix[] = "pseudomodem";
Christopher Wileye049cc52012-09-10 14:36:57 -070058const char DeviceInfo::kEthernetPseudoDeviceNamePrefix[] = "pseudoethernet";
Paul Stewartca876ee2012-04-21 08:55:58 -070059const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net";
Ben Chan4e64d2d2012-05-16 00:02:25 -070060const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
61const char DeviceInfo::kDriverGdmWiMax[] = "gdm_wimax";
mukesh agrawal93a29ed2012-04-17 16:13:01 -070062const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartca876ee2012-04-21 08:55:58 -070063const char DeviceInfo::kInterfaceUevent[] = "uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080064const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Paul Stewartca876ee2012-04-21 08:55:58 -070065const char DeviceInfo::kInterfaceDevice[] = "device";
66const char DeviceInfo::kInterfaceDriver[] = "device/driver";
67const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags";
68const char DeviceInfo::kInterfaceType[] = "type";
Paul Stewartb50f0b92011-05-16 16:31:42 -070069const char *DeviceInfo::kModemDrivers[] = {
70 "gobi",
71 "QCUSBNet2k",
Thieu Le8f1c8352012-04-16 11:02:12 -070072 "GobiNet"
Paul Stewartb50f0b92011-05-16 16:31:42 -070073};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080074const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewart050cfc02012-07-06 20:38:54 -070075const int DeviceInfo::kDelayedDeviceCreationSeconds = 5;
Paul Stewart1ac4e842012-07-10 12:58:12 -070076const int DeviceInfo::kRequestLinkStatisticsIntervalSeconds = 60;
Paul Stewartb50f0b92011-05-16 16:31:42 -070077
78DeviceInfo::DeviceInfo(ControlInterface *control_interface,
79 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080080 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070081 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070082 : control_interface_(control_interface),
83 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080084 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070085 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050086 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
87 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070088 link_listener_(NULL),
89 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070090 device_info_root_(kDeviceInfoRoot),
Paul Stewart8c116a92012-05-02 18:30:03 -070091 routing_table_(RoutingTable::GetInstance()),
Gary Morain41780232012-07-31 15:08:31 -070092 rtnl_handler_(RTNLHandler::GetInstance()),
93 sockets_(new Sockets()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070094}
95
Paul Stewarta3c56f92011-05-26 07:08:52 -070096DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070097
mukesh agrawal8f317b62011-07-15 11:53:23 -070098void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
99 black_list_.insert(device_name);
100}
101
Eric Shienbrood5e628a52012-03-21 16:56:59 -0400102bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
103 return ContainsKey(black_list_, device_name);
104}
105
Paul Stewarta3c56f92011-05-26 07:08:52 -0700106void DeviceInfo::Start() {
107 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500108 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700109 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500110 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700111 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
112 RTNLHandler::kRequestAddr);
Paul Stewart1ac4e842012-07-10 12:58:12 -0700113 request_link_statistics_callback_.Reset(
114 Bind(&DeviceInfo::RequestLinkStatistics, AsWeakPtr()));
115 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
116 kRequestLinkStatisticsIntervalSeconds * 1000);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700117}
118
Paul Stewarta3c56f92011-05-26 07:08:52 -0700119void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700120 link_listener_.reset();
121 address_listener_.reset();
Paul Stewart8c116a92012-05-02 18:30:03 -0700122 infos_.clear();
Paul Stewart1ac4e842012-07-10 12:58:12 -0700123 request_link_statistics_callback_.Cancel();
Paul Stewart050cfc02012-07-06 20:38:54 -0700124 delayed_devices_callback_.Cancel();
125 delayed_devices_.clear();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700126}
127
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700128void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700129 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
130 << device->interface_index() << ")";
Paul Stewart050cfc02012-07-06 20:38:54 -0700131 delayed_devices_.erase(device->interface_index());
Darin Petkove6193c02011-08-11 12:42:40 -0700132 CHECK(!GetDevice(device->interface_index()).get());
133 infos_[device->interface_index()].device = device;
Joshua Krollda798622012-06-05 12:30:48 -0700134 if ((device->technology() == Technology::kCellular) ||
135 (device->technology() == Technology::kEthernet) ||
136 (device->technology() == Technology::kWifi) ||
137 (device->technology() == Technology::kWiMax)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700138 manager_->RegisterDevice(device);
139 }
140}
141
Jason Glasgowe9089492012-02-23 17:57:37 -0500142void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
143 int interface_index = device->interface_index();
144
Ben Chanfad4a0b2012-04-18 15:49:59 -0700145 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
146 << interface_index << ")";
Joshua Krollda798622012-06-05 12:30:48 -0700147 CHECK((device->technology() == Technology::kCellular) ||
148 (device->technology() == Technology::kWiMax));
Jason Glasgowe9089492012-02-23 17:57:37 -0500149
150 // Release reference to the device
151 map<int, Info>::iterator iter = infos_.find(interface_index);
152 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700153 SLOG(Device, 2) << "Removing device from info for index: "
154 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500155 manager_->DeregisterDevice(device);
156 // Release the reference to the device, but maintain the mapping
157 // for the index. That will be cleaned up by an RTNL message.
158 iter->second.device = NULL;
159 }
160}
161
Paul Stewartca876ee2012-04-21 08:55:58 -0700162FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
163 const string &path_name) {
164 return device_info_root_.Append(iface_name).Append(path_name);
165}
166
167bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
168 const string &path_name,
169 string *contents_out) {
170 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
171 contents_out);
172}
173bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700174 const string &path_name,
175 FilePath *path_out) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700176 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
177 path_out);
178}
179
Paul Stewartfdd16072011-09-16 12:41:35 -0700180Technology::Identifier DeviceInfo::GetDeviceTechnology(
181 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700182 string type_string;
183 int arp_type = ARPHRD_VOID;;
184 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
185 TrimString(type_string, "\n", &type_string) &&
186 !base::StringToInt(type_string, &arp_type)) {
187 arp_type = ARPHRD_VOID;
188 }
189
Paul Stewart9364c4c2011-12-06 17:12:42 -0800190 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700191 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Paul Stewart050cfc02012-07-06 20:38:54 -0700192 LOG(INFO) << StringPrintf("%s: device %s has no uevent file",
193 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700194 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800195 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700196
Thieu Le8f1c8352012-04-16 11:02:12 -0700197 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
198 // start of the file or after a newline, we can safely assume this
199 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800200 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700201 SLOG(Device, 2)
202 << StringPrintf("%s: device %s has wifi signature in uevent file",
203 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700204 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700205 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
206 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800207 return Technology::kWiFiMonitor;
208 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700209 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700210 }
211
Jason Glasgow7904d142012-05-18 13:36:20 -0400212 // Special case for pseudo modems which are used for testing
213 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
214 SLOG(Device, 2) << StringPrintf(
215 "%s: device %s is a pseudo modem for testing",
216 __func__, iface_name.c_str());
217 return Technology::kCellular;
218 }
219
Christopher Wileye049cc52012-09-10 14:36:57 -0700220 // Special case for pseudo ethernet devices which are used for testing.
221 if (iface_name.find(kEthernetPseudoDeviceNamePrefix) == 0) {
222 SLOG(Device, 2) << StringPrintf(
223 "%s: device %s is a virtual ethernet device for testing",
224 __func__, iface_name.c_str());
225 return Technology::kEthernet;
226 }
227
Paul Stewart9364c4c2011-12-06 17:12:42 -0800228 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700229 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700230 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
231 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700232 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700233 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
234 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700235 return Technology::kLoopback;
236 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700237 if (arp_type == ARPHRD_PPP) {
238 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
239 __func__, iface_name.c_str());
240 return Technology::kPPP;
241 }
242 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800243 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700244 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
245 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
246 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800247 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700248 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
249 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800250 return Technology::kTunnel;
251 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700252 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800253 }
254
255 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700256 // See if driver for this interface is in a list of known modem driver names.
257 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
258 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800259 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700260 SLOG(Device, 2)
261 << StringPrintf("%s: device %s is matched with modem driver %s",
262 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800263 return Technology::kCellular;
264 }
265 }
266
Ben Chan4e64d2d2012-05-16 00:02:25 -0700267 if (driver_name == kDriverGdmWiMax) {
268 SLOG(Device, 2) << StringPrintf("%s: device %s is a WiMAX device",
269 __func__, iface_name.c_str());
270 return Technology::kWiMax;
271 }
272
Thieu Le8f1c8352012-04-16 11:02:12 -0700273 // For cdc_ether devices, make sure it's a modem because this driver
274 // can be used for other ethernet devices.
Paul Stewart050cfc02012-07-06 20:38:54 -0700275 if (driver_name == kDriverCdcEther) {
276 if (IsCdcEtherModemDevice(iface_name)) {
277 LOG(INFO) << StringPrintf("%s: device %s is a modem cdc_ether "
278 "device", __func__, iface_name.c_str());
279 return Technology::kCellular;
280 }
281 SLOG(Device, 2) << StringPrintf("%s: device %s is a cdc_ether "
282 "device", __func__, iface_name.c_str());
283 return Technology::kCDCEthernet;
Thieu Le8f1c8352012-04-16 11:02:12 -0700284 }
285
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700286 // Special case for the virtio driver, used when run under KVM. See also
287 // the comment in VirtioEthernet::Start.
288 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700289 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
290 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700291 return Technology::kVirtioEthernet;
292 }
293
Ben Chanfad4a0b2012-04-18 15:49:59 -0700294 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
295 "is defaulted to type ethernet",
296 __func__, iface_name.c_str(),
297 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700298 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700299}
300
Thieu Le8f1c8352012-04-16 11:02:12 -0700301bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
302 // A cdc_ether device is a modem device if it also exposes tty interfaces.
303 // To determine this, we look for the existence of the tty interface in the
304 // USB device sysfs tree.
305 //
306 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
307 // follows:
308 //
309 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
310 // 1-2:1.0
311 // tty
312 // ttyACM0
313 // 1-2:1.1
314 // net
315 // usb0
316 // 1-2:1.2
317 // tty
318 // ttyACM1
319 // ...
320 //
321 // /sys/class/net/usb0/device symlinks to
322 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700323 //
324 // Note that some modem devices have the tty directory one level deeper
325 // (eg. E362), so the device tree for the tty interface is:
326 // /sys/devices/pci0000:00/0000:00:1d.7/usb/1-2/1-2:1.0/ttyUSB0/tty/ttyUSB0
Thieu Le8f1c8352012-04-16 11:02:12 -0700327
Paul Stewartca876ee2012-04-21 08:55:58 -0700328 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700329 FilePath device_path;
330 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700331 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
332 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700333 return false;
334 }
335 if (!device_path.IsAbsolute()) {
336 device_path = device_file.DirName().Append(device_path);
337 file_util::AbsolutePath(&device_path);
338 }
339
340 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700341 // USB device and see if there's a subdirectory "tty" inside. In other
342 // words, using the example dir hierarchy above, find
343 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700344 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700345 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700346}
347
348// static
Thieu Leb27beee2012-04-20 09:19:06 -0700349bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
350 file_util::FileEnumerator::FileType type =
351 static_cast<file_util::FileEnumerator::FileType>(
352 file_util::FileEnumerator::DIRECTORIES |
353 file_util::FileEnumerator::SHOW_SYM_LINKS);
354 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700355 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
356 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700357 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700358 return true;
359 }
360 return false;
361}
362
Paul Stewart8c116a92012-05-02 18:30:03 -0700363DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name,
364 const string &address,
365 int interface_index,
366 Technology::Identifier technology) {
367 DeviceRefPtr device;
Paul Stewart050cfc02012-07-06 20:38:54 -0700368 delayed_devices_.erase(interface_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700369
370 switch (technology) {
371 case Technology::kCellular:
372 // Cellular devices are managed by ModemInfo.
373 SLOG(Device, 2) << "Cellular link " << link_name
374 << " at index " << interface_index
375 << " -- notifying ModemInfo.";
Gary Morainbf74a672012-07-30 16:27:19 -0700376
377 // The MAC address provided by RTNL is not reliable for Gobi 2K modems.
378 // Clear it here, and it will be fetched from the kernel is
379 // GetMacAddress().
380 infos_[interface_index].mac_address.Clear();
Paul Stewart8c116a92012-05-02 18:30:03 -0700381 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
382 break;
383 case Technology::kEthernet:
384 device = new Ethernet(control_interface_, dispatcher_, metrics_,
385 manager_, link_name, address, interface_index);
386 device->EnableIPv6Privacy();
387 break;
388 case Technology::kVirtioEthernet:
389 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
390 manager_, link_name, address,
391 interface_index);
392 device->EnableIPv6Privacy();
393 break;
394 case Technology::kWifi:
395 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
396 link_name, address, interface_index);
397 device->EnableIPv6Privacy();
398 break;
Ben Chan4e64d2d2012-05-16 00:02:25 -0700399 case Technology::kWiMax:
Darin Petkove4b27022012-05-16 13:28:50 +0200400 // WiMax devices are managed by WiMaxProvider.
401 SLOG(Device, 2) << "WiMax link " << link_name
402 << " at index " << interface_index
403 << " -- notifying WiMaxProvider.";
404 manager_->wimax_provider()->OnDeviceInfoAvailable(link_name);
Ben Chan4e64d2d2012-05-16 00:02:25 -0700405 break;
Paul Stewart8c116a92012-05-02 18:30:03 -0700406 case Technology::kPPP:
407 case Technology::kTunnel:
408 // Tunnel and PPP devices are managed by the VPN code (PPP for
409 // l2tpipsec). Notify the VPN Provider of the interface's presence.
410 // Since CreateDevice is only called once in the lifetime of an
411 // interface index, this notification will only occur the first
412 // time the device is seen.
413 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
414 << " at index " << interface_index
415 << " -- notifying VPNProvider.";
416 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
417 interface_index) &&
418 technology == Technology::kTunnel) {
419 // If VPN does not know anything about this tunnel, it is probably
420 // left over from a previous instance and should not exist.
421 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
422 DeleteInterface(interface_index);
423 }
424 break;
425 case Technology::kLoopback:
426 // Loopback devices are largely ignored, but we should make sure the
427 // link is enabled.
428 SLOG(Device, 2) << "Bringing up loopback device " << link_name
429 << " at index " << interface_index;
430 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
431 return NULL;
Paul Stewart050cfc02012-07-06 20:38:54 -0700432 case Technology::kCDCEthernet:
433 // CDCEnternet devices are of indeterminate type when they are
434 // initially created. Some time later, tty devices may or may
435 // not appear under the same USB device root, which will identify
436 // it as a modem. Alternatively, ModemManager may discover the
437 // device and create and register a Cellular device. In either
438 // case, we should delay creating a Device until we can make a
439 // better determination of what type this Device should be.
440 LOG(INFO) << "Delaying creation of device for " << link_name
441 << " at index " << interface_index;
442 DelayDeviceCreation(interface_index);
443 return NULL;
Paul Stewart8c116a92012-05-02 18:30:03 -0700444 default:
445 // We will not manage this device in shill. Do not create a device
446 // object or do anything to change its state. We create a stub object
447 // which is useful for testing.
448 return new DeviceStub(control_interface_, dispatcher_, metrics_,
449 manager_, link_name, address, interface_index,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700450 technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700451 }
452
453 // Reset the routing table and addresses.
454 routing_table_->FlushRoutes(interface_index);
455 FlushAddresses(interface_index);
456
457 return device;
458}
459
Chris Masone2aa97072011-08-09 17:35:08 -0700460void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700461 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
462 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700463 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700464 Technology::Identifier technology = Technology::kUnknown;
Darin Petkove6193c02011-08-11 12:42:40 -0700465 unsigned int flags = msg.link_status().flags;
466 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700467 bool new_device =
468 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700469 SLOG(Device, 2) << __func__ << "(index=" << dev_index
470 << std::showbase << std::hex
471 << ", flags=" << flags << ", change=" << change << ")"
472 << std::dec << std::noshowbase
473 << ", new_device=" << new_device;
Paul Stewart8c116a92012-05-02 18:30:03 -0700474 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700475 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700476
Paul Stewart1ac4e842012-07-10 12:58:12 -0700477 RetrieveLinkStatistics(dev_index, msg);
478
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700479 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700480 if (new_device) {
481 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700482 if (!msg.HasAttribute(IFLA_IFNAME)) {
483 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
484 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700485 }
Chris Masone2aa97072011-08-09 17:35:08 -0700486 ByteString b(msg.GetAttribute(IFLA_IFNAME));
487 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700488 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200489 infos_[dev_index].name = link_name;
490 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700491
Chris Masone2aa97072011-08-09 17:35:08 -0700492 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700493 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700494 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700495 } else {
496 technology = GetDeviceTechnology(link_name);
497 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700498 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800499 string address;
500 if (msg.HasAttribute(IFLA_ADDRESS)) {
501 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
502 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700503 SLOG(Device, 2) << "link index " << dev_index << " address "
504 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700505 } else if (technology != Technology::kTunnel &&
506 technology != Technology::kPPP) {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800507 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
508 return;
509 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700510 device = CreateDevice(link_name, address, dev_index, technology);
511 if (device) {
512 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700513 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700514 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700515 if (device) {
516 device->LinkEvent(flags, change);
517 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700518}
519
Chris Masone2aa97072011-08-09 17:35:08 -0700520void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700521 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700522
Paul Stewart9a908082011-08-31 12:18:48 -0700523 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
524 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700525 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
526 << std::showbase << std::hex
527 << ", flags=" << msg.link_status().flags
528 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700529 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700530}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700531
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700532DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
533 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700534 return info ? info->device : NULL;
535}
536
Darin Petkovf8046b82012-04-24 16:29:23 +0200537int DeviceInfo::GetIndex(const string &interface_name) const {
538 map<string, int>::const_iterator it = indices_.find(interface_name);
539 return it == indices_.end() ? -1 : it->second;
540}
541
Paul Stewart32852962011-08-30 14:06:53 -0700542bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700543 const Info *info = GetInfo(interface_index);
544 if (!info) {
545 return false;
546 }
Gary Morainbf74a672012-07-30 16:27:19 -0700547 // |mac_address| from RTNL is not used for some devices, in which case it will
548 // be empty here.
549 if (!info->mac_address.IsEmpty()) {
550 *address = info->mac_address;
551 return true;
552 }
553
554 // Ask the kernel for the MAC address.
555 *address = GetMACAddressFromKernel(interface_index);
556 return !address->IsEmpty();
557}
558
559ByteString DeviceInfo::GetMACAddressFromKernel(int interface_index) const {
Gary Morain41780232012-07-31 15:08:31 -0700560 const Info *info = GetInfo(interface_index);
561 if (!info) {
562 return ByteString();
563 }
564
565 const int fd = sockets_->Socket(PF_INET, SOCK_DGRAM, 0);
Gary Morainbf74a672012-07-30 16:27:19 -0700566 if (fd < 0) {
567 LOG(ERROR) << __func__ << ": Unable to open socket: " << fd;
Gary Morain41780232012-07-31 15:08:31 -0700568 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700569 }
Gary Morain41780232012-07-31 15:08:31 -0700570
571 ScopedSocketCloser socket_closer(sockets_.get(), fd);
Gary Morainbf74a672012-07-30 16:27:19 -0700572 struct ifreq ifr;
573 memset(&ifr, 0, sizeof(ifr));
574 ifr.ifr_ifindex = interface_index;
Gary Morain41780232012-07-31 15:08:31 -0700575 strcpy(ifr.ifr_ifrn.ifrn_name, info->name.c_str());
576 int err = sockets_->Ioctl(fd, SIOCGIFHWADDR, &ifr);
Gary Morainbf74a672012-07-30 16:27:19 -0700577 if (err < 0) {
578 LOG(ERROR) << __func__ << ": Unable to read MAC address: " << errno;
Gary Morain41780232012-07-31 15:08:31 -0700579 return ByteString();
Gary Morainbf74a672012-07-30 16:27:19 -0700580 }
Gary Morain41780232012-07-31 15:08:31 -0700581
582 return ByteString(ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700583}
584
Paul Stewart9a908082011-08-31 12:18:48 -0700585bool DeviceInfo::GetAddresses(int interface_index,
586 vector<AddressData> *addresses) const {
587 const Info *info = GetInfo(interface_index);
588 if (!info) {
589 return false;
590 }
591 *addresses = info->ip_addresses;
592 return true;
593}
594
595void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700596 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700597 const Info *info = GetInfo(interface_index);
598 if (!info) {
599 return;
600 }
601 const vector<AddressData> &addresses = info->ip_addresses;
602 vector<AddressData>::const_iterator iter;
603 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700604 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700605 (iter->scope == RT_SCOPE_UNIVERSE &&
606 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700607 SLOG(Device, 2) << __func__ << ": removing ip address "
608 << iter->address.ToString()
609 << " from interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700610 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
611 }
612 }
613}
614
Paul Stewart05a42c22012-08-02 16:47:21 -0700615bool DeviceInfo::HasOtherAddress(
616 int interface_index, const IPAddress &this_address) const {
617 SLOG(Device, 3) << __func__ << "(" << interface_index << ")";
618 const Info *info = GetInfo(interface_index);
619 if (!info) {
620 return false;
621 }
622 const vector<AddressData> &addresses = info->ip_addresses;
623 vector<AddressData>::const_iterator iter;
624 bool has_other_address = false;
625 bool has_this_address = false;
626 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
627 if (iter->address.family() != this_address.family()) {
628 continue;
629 }
630 if (iter->address.address().Equals(this_address.address())) {
631 has_this_address = true;
632 } else if (this_address.family() == IPAddress::kFamilyIPv4) {
633 has_other_address = true;
634 } else if ((iter->scope == RT_SCOPE_UNIVERSE &&
635 (iter->flags & IFA_F_TEMPORARY) == 0)) {
636 has_other_address = true;
637 }
638 }
639 return has_other_address && !has_this_address;
640}
641
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700642bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
643 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700644 if (!info) {
645 return false;
646 }
647 *flags = info->flags;
648 return true;
649}
650
Paul Stewart1ac4e842012-07-10 12:58:12 -0700651bool DeviceInfo::GetByteCounts(int interface_index,
652 uint64 *rx_bytes,
653 uint64 *tx_bytes) const {
654 const Info *info = GetInfo(interface_index);
655 if (!info) {
656 return false;
657 }
658 *rx_bytes = info->rx_bytes;
659 *tx_bytes = info->tx_bytes;
660 return true;
661}
662
Paul Stewartca6abd42012-03-01 15:45:29 -0800663bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800664 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
665 if (fd < 0) {
666 PLOG(ERROR) << "failed to open " << kTunDeviceName;
667 return false;
668 }
669 file_util::ScopedFD scoped_fd(&fd);
670
671 struct ifreq ifr;
672 memset(&ifr, 0, sizeof(ifr));
673 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
674 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
675 PLOG(ERROR) << "failed to create tunnel interface";
676 return false;
677 }
678
679 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
680 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
681 return false;
682 }
683
684 *interface_name = string(ifr.ifr_name);
685
686 return true;
687}
688
Paul Stewartca6abd42012-03-01 15:45:29 -0800689bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800690 return rtnl_handler_->RemoveInterface(interface_index);
691}
692
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700693const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
694 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700695 if (iter == infos_.end()) {
696 return NULL;
697 }
698 return &iter->second;
699}
700
701void DeviceInfo::RemoveInfo(int interface_index) {
702 map<int, Info>::iterator iter = infos_.find(interface_index);
703 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700704 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700705 if (iter->second.device.get()) {
706 manager_->DeregisterDevice(iter->second.device);
707 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200708 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700709 infos_.erase(iter);
Paul Stewart050cfc02012-07-06 20:38:54 -0700710 delayed_devices_.erase(interface_index);
mukesh agrawal47009f82011-08-25 14:07:35 -0700711 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700712 SLOG(Device, 2) << __func__ << ": Unknown device index: "
713 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700714 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700715}
716
Chris Masone2aa97072011-08-09 17:35:08 -0700717void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700718 DCHECK(msg.type() == RTNLMessage::kTypeLink);
719 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700720 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700721 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700722 DelLinkMsgHandler(msg);
723 } else {
724 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700725 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700726}
727
Paul Stewart9a908082011-08-31 12:18:48 -0700728void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700729 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700730 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
731 int interface_index = msg.interface_index();
732 if (!ContainsKey(infos_, interface_index)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700733 SLOG(Device, 2) << "Got advance address information for unknown index "
734 << interface_index;
735 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -0700736 }
737 const RTNLMessage::AddressStatus &status = msg.address_status();
738 IPAddress address(msg.family(),
Ben Chan682c5d42012-06-18 14:11:04 -0700739 msg.HasAttribute(IFA_LOCAL) ?
740 msg.GetAttribute(IFA_LOCAL) : msg.GetAttribute(IFA_ADDRESS),
Paul Stewart9a908082011-08-31 12:18:48 -0700741 status.prefix_len);
742
Ben Chan682c5d42012-06-18 14:11:04 -0700743 SLOG_IF(Device, 2, msg.HasAttribute(IFA_LOCAL))
744 << "Found local address attribute for interface " << interface_index;
745
Paul Stewart9a908082011-08-31 12:18:48 -0700746 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
747 vector<AddressData>::iterator iter;
748 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
749 if (address.Equals(iter->address)) {
750 break;
751 }
752 }
753 if (iter != address_list.end()) {
754 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700755 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700756 address_list.erase(iter);
757 } else {
758 iter->flags = status.flags;
759 iter->scope = status.scope;
760 }
761 } else if (msg.mode() == RTNLMessage::kModeAdd) {
762 address_list.push_back(AddressData(address, status.flags, status.scope));
Paul Stewart8c116a92012-05-02 18:30:03 -0700763 SLOG(Device, 2) << "Add address " << address.ToString()
764 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700765 }
766}
767
Paul Stewart050cfc02012-07-06 20:38:54 -0700768void DeviceInfo::DelayDeviceCreation(int interface_index) {
769 delayed_devices_.insert(interface_index);
770 delayed_devices_callback_.Reset(
771 Bind(&DeviceInfo::DelayedDeviceCreationTask, AsWeakPtr()));
772 dispatcher_->PostDelayedTask(delayed_devices_callback_.callback(),
773 kDelayedDeviceCreationSeconds * 1000);
774}
775
776// Re-evaluate the technology type for each delayed device.
777void DeviceInfo::DelayedDeviceCreationTask() {
778 while (!delayed_devices_.empty()) {
779 set<int>::iterator it = delayed_devices_.begin();
780 int dev_index = *it;
781 delayed_devices_.erase(it);
782
783 DCHECK(ContainsKey(infos_, dev_index));
784 DCHECK(!GetDevice(dev_index));
785
786 const string &link_name = infos_[dev_index].name;
787 Technology::Identifier technology = GetDeviceTechnology(link_name);
788
789 if (technology == Technology::kCDCEthernet) {
790 LOG(INFO) << "In " << __func__ << ": device " << link_name
791 << " is now assumed to be regular Ethernet.";
792 technology = Technology::kEthernet;
793 } else if (technology != Technology::kCellular) {
794 LOG(WARNING) << "In " << __func__ << ": device " << link_name
795 << " is unexpected technology "
796 << Technology::NameFromIdentifier(technology);
797 }
798 string address =
799 StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
800 DCHECK(!address.empty());
801
802 DeviceRefPtr device = CreateDevice(link_name, address, dev_index,
803 technology);
804 if (device) {
805 RegisterDevice(device);
806 }
807 }
808}
809
Paul Stewart1ac4e842012-07-10 12:58:12 -0700810void DeviceInfo::RetrieveLinkStatistics(int interface_index,
811 const RTNLMessage &msg) {
812 if (!msg.HasAttribute(IFLA_STATS64)) {
813 return;
814 }
815 ByteString stats_bytes(msg.GetAttribute(IFLA_STATS64));
816 struct rtnl_link_stats64 stats;
817 if (stats_bytes.GetLength() < sizeof(stats)) {
818 LOG(WARNING) << "Link statistics size is too small: "
819 << stats_bytes.GetLength() << " < " << sizeof(stats);
820 return;
821 }
822
823 memcpy(&stats, stats_bytes.GetConstData(), sizeof(stats));
824 SLOG(Device, 2) << "Link statistics for "
825 << " interface index " << interface_index << ": "
826 << "receive: " << stats.rx_bytes << "; "
827 << "transmit: " << stats.tx_bytes << ".";
828 infos_[interface_index].rx_bytes = stats.rx_bytes;
829 infos_[interface_index].tx_bytes = stats.tx_bytes;
830}
831
832void DeviceInfo::RequestLinkStatistics() {
833 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink);
834 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
835 kRequestLinkStatisticsIntervalSeconds * 1000);
836}
837
Paul Stewart0af98bf2011-05-10 17:38:08 -0700838} // namespace shill