blob: 76f479aeb77fa787bdbb9f6ed09aa9e2d6f1c184 [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"
mukesh agrawal93a29ed2012-04-17 16:13:01 -070042#include "shill/virtio_ethernet.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070043#include "shill/wifi.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070044
Eric Shienbrood3e20a232012-02-16 11:35:56 -050045using base::Bind;
Thieu Le8f1c8352012-04-16 11:02:12 -070046using base::StringPrintf;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050047using base::Unretained;
Darin Petkove6193c02011-08-11 12:42:40 -070048using std::map;
Paul Stewart050cfc02012-07-06 20:38:54 -070049using std::set;
Paul Stewart0af98bf2011-05-10 17:38:08 -070050using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070051using std::vector;
Paul Stewart0af98bf2011-05-10 17:38:08 -070052
53namespace shill {
Paul Stewartb50f0b92011-05-16 16:31:42 -070054
55// static
Jason Glasgowabc54032012-04-20 16:08:32 -040056const char DeviceInfo::kModemPseudoDeviceNamePrefix[] = "pseudomodem";
Paul Stewartca876ee2012-04-21 08:55:58 -070057const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net";
Ben Chan4e64d2d2012-05-16 00:02:25 -070058const char DeviceInfo::kDriverCdcEther[] = "cdc_ether";
59const char DeviceInfo::kDriverGdmWiMax[] = "gdm_wimax";
mukesh agrawal93a29ed2012-04-17 16:13:01 -070060const char DeviceInfo::kDriverVirtioNet[] = "virtio_net";
Paul Stewartca876ee2012-04-21 08:55:58 -070061const char DeviceInfo::kInterfaceUevent[] = "uevent";
Paul Stewart9364c4c2011-12-06 17:12:42 -080062const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n";
Paul Stewartca876ee2012-04-21 08:55:58 -070063const char DeviceInfo::kInterfaceDevice[] = "device";
64const char DeviceInfo::kInterfaceDriver[] = "device/driver";
65const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags";
66const char DeviceInfo::kInterfaceType[] = "type";
Paul Stewartb50f0b92011-05-16 16:31:42 -070067const char *DeviceInfo::kModemDrivers[] = {
68 "gobi",
69 "QCUSBNet2k",
Thieu Le8f1c8352012-04-16 11:02:12 -070070 "GobiNet"
Paul Stewartb50f0b92011-05-16 16:31:42 -070071};
Paul Stewartcba0f7f2012-02-29 16:33:05 -080072const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun";
Paul Stewart050cfc02012-07-06 20:38:54 -070073const int DeviceInfo::kDelayedDeviceCreationSeconds = 5;
Paul Stewart1ac4e842012-07-10 12:58:12 -070074const int DeviceInfo::kRequestLinkStatisticsIntervalSeconds = 60;
Paul Stewartb50f0b92011-05-16 16:31:42 -070075
76DeviceInfo::DeviceInfo(ControlInterface *control_interface,
77 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080078 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070079 Manager *manager)
Paul Stewarta3c56f92011-05-26 07:08:52 -070080 : control_interface_(control_interface),
81 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080082 metrics_(metrics),
Paul Stewarta3c56f92011-05-26 07:08:52 -070083 manager_(manager),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050084 link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))),
85 address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))),
Paul Stewart9a908082011-08-31 12:18:48 -070086 link_listener_(NULL),
87 address_listener_(NULL),
Paul Stewartca876ee2012-04-21 08:55:58 -070088 device_info_root_(kDeviceInfoRoot),
Paul Stewart8c116a92012-05-02 18:30:03 -070089 routing_table_(RoutingTable::GetInstance()),
Paul Stewart9a908082011-08-31 12:18:48 -070090 rtnl_handler_(RTNLHandler::GetInstance()) {
Paul Stewart0af98bf2011-05-10 17:38:08 -070091}
92
Paul Stewarta3c56f92011-05-26 07:08:52 -070093DeviceInfo::~DeviceInfo() {}
Paul Stewart0af98bf2011-05-10 17:38:08 -070094
mukesh agrawal8f317b62011-07-15 11:53:23 -070095void DeviceInfo::AddDeviceToBlackList(const string &device_name) {
96 black_list_.insert(device_name);
97}
98
Eric Shienbrood5e628a52012-03-21 16:56:59 -040099bool DeviceInfo::IsDeviceBlackListed(const string &device_name) {
100 return ContainsKey(black_list_, device_name);
101}
102
Paul Stewarta3c56f92011-05-26 07:08:52 -0700103void DeviceInfo::Start() {
104 link_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500105 new RTNLListener(RTNLHandler::kRequestLink, link_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700106 address_listener_.reset(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500107 new RTNLListener(RTNLHandler::kRequestAddr, address_callback_));
Paul Stewart9a908082011-08-31 12:18:48 -0700108 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink |
109 RTNLHandler::kRequestAddr);
Paul Stewart1ac4e842012-07-10 12:58:12 -0700110 request_link_statistics_callback_.Reset(
111 Bind(&DeviceInfo::RequestLinkStatistics, AsWeakPtr()));
112 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
113 kRequestLinkStatisticsIntervalSeconds * 1000);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700114}
115
Paul Stewarta3c56f92011-05-26 07:08:52 -0700116void DeviceInfo::Stop() {
Paul Stewart9a908082011-08-31 12:18:48 -0700117 link_listener_.reset();
118 address_listener_.reset();
Paul Stewart8c116a92012-05-02 18:30:03 -0700119 infos_.clear();
Paul Stewart1ac4e842012-07-10 12:58:12 -0700120 request_link_statistics_callback_.Cancel();
Paul Stewart050cfc02012-07-06 20:38:54 -0700121 delayed_devices_callback_.Cancel();
122 delayed_devices_.clear();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700123}
124
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700125void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700126 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
127 << device->interface_index() << ")";
Paul Stewart050cfc02012-07-06 20:38:54 -0700128 delayed_devices_.erase(device->interface_index());
Darin Petkove6193c02011-08-11 12:42:40 -0700129 CHECK(!GetDevice(device->interface_index()).get());
130 infos_[device->interface_index()].device = device;
Joshua Krollda798622012-06-05 12:30:48 -0700131 if ((device->technology() == Technology::kCellular) ||
132 (device->technology() == Technology::kEthernet) ||
133 (device->technology() == Technology::kWifi) ||
134 (device->technology() == Technology::kWiMax)) {
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700135 manager_->RegisterDevice(device);
136 }
137}
138
Jason Glasgowe9089492012-02-23 17:57:37 -0500139void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) {
140 int interface_index = device->interface_index();
141
Ben Chanfad4a0b2012-04-18 15:49:59 -0700142 SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", "
143 << interface_index << ")";
Joshua Krollda798622012-06-05 12:30:48 -0700144 CHECK((device->technology() == Technology::kCellular) ||
145 (device->technology() == Technology::kWiMax));
Jason Glasgowe9089492012-02-23 17:57:37 -0500146
147 // Release reference to the device
148 map<int, Info>::iterator iter = infos_.find(interface_index);
149 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700150 SLOG(Device, 2) << "Removing device from info for index: "
151 << interface_index;
Jason Glasgowe9089492012-02-23 17:57:37 -0500152 manager_->DeregisterDevice(device);
153 // Release the reference to the device, but maintain the mapping
154 // for the index. That will be cleaned up by an RTNL message.
155 iter->second.device = NULL;
156 }
157}
158
Paul Stewartca876ee2012-04-21 08:55:58 -0700159FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name,
160 const string &path_name) {
161 return device_info_root_.Append(iface_name).Append(path_name);
162}
163
164bool DeviceInfo::GetDeviceInfoContents(const string &iface_name,
165 const string &path_name,
166 string *contents_out) {
167 return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name),
168 contents_out);
169}
170bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700171 const string &path_name,
172 FilePath *path_out) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700173 return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name),
174 path_out);
175}
176
Paul Stewartfdd16072011-09-16 12:41:35 -0700177Technology::Identifier DeviceInfo::GetDeviceTechnology(
178 const string &iface_name) {
Paul Stewartca876ee2012-04-21 08:55:58 -0700179 string type_string;
180 int arp_type = ARPHRD_VOID;;
181 if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) &&
182 TrimString(type_string, "\n", &type_string) &&
183 !base::StringToInt(type_string, &arp_type)) {
184 arp_type = ARPHRD_VOID;
185 }
186
Paul Stewart9364c4c2011-12-06 17:12:42 -0800187 string contents;
Paul Stewartca876ee2012-04-21 08:55:58 -0700188 if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) {
Paul Stewart050cfc02012-07-06 20:38:54 -0700189 LOG(INFO) << StringPrintf("%s: device %s has no uevent file",
190 __func__, iface_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700191 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800192 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700193
Thieu Le8f1c8352012-04-16 11:02:12 -0700194 // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the
195 // start of the file or after a newline, we can safely assume this
196 // is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800197 if (contents.find(kInterfaceUeventWifiSignature) != string::npos) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700198 SLOG(Device, 2)
199 << StringPrintf("%s: device %s has wifi signature in uevent file",
200 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700201 if (arp_type == ARPHRD_IEEE80211_RADIOTAP) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700202 SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode",
203 __func__, iface_name.c_str());
Paul Stewart2001a422011-12-15 10:20:09 -0800204 return Technology::kWiFiMonitor;
205 }
Paul Stewartfdd16072011-09-16 12:41:35 -0700206 return Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700207 }
208
Jason Glasgow7904d142012-05-18 13:36:20 -0400209 // Special case for pseudo modems which are used for testing
210 if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) {
211 SLOG(Device, 2) << StringPrintf(
212 "%s: device %s is a pseudo modem for testing",
213 __func__, iface_name.c_str());
214 return Technology::kCellular;
215 }
216
Paul Stewart9364c4c2011-12-06 17:12:42 -0800217 FilePath driver_path;
Paul Stewartca876ee2012-04-21 08:55:58 -0700218 if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700219 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
220 __func__, iface_name.c_str());
Paul Stewartca876ee2012-04-21 08:55:58 -0700221 if (arp_type == ARPHRD_LOOPBACK) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700222 SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device",
223 __func__, iface_name.c_str());
Paul Stewarte81eb702012-04-11 15:04:53 -0700224 return Technology::kLoopback;
225 }
Paul Stewartca876ee2012-04-21 08:55:58 -0700226 if (arp_type == ARPHRD_PPP) {
227 SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device",
228 __func__, iface_name.c_str());
229 return Technology::kPPP;
230 }
231 string tun_flags_str;
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800232 int tun_flags = 0;
Paul Stewartca876ee2012-04-21 08:55:58 -0700233 if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) &&
234 TrimString(tun_flags_str, "\n", &tun_flags_str) &&
235 base::HexStringToInt(tun_flags_str, &tun_flags) &&
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800236 (tun_flags & IFF_TUN)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700237 SLOG(Device, 2) << StringPrintf("%s: device %s is tun device",
238 __func__, iface_name.c_str());
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800239 return Technology::kTunnel;
240 }
Paul Stewarte81eb702012-04-11 15:04:53 -0700241 return Technology::kUnknown;
Paul Stewart9364c4c2011-12-06 17:12:42 -0800242 }
243
244 string driver_name(driver_path.BaseName().value());
Thieu Le8f1c8352012-04-16 11:02:12 -0700245 // See if driver for this interface is in a list of known modem driver names.
246 for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers);
247 ++modem_idx) {
Paul Stewart9364c4c2011-12-06 17:12:42 -0800248 if (driver_name == kModemDrivers[modem_idx]) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700249 SLOG(Device, 2)
250 << StringPrintf("%s: device %s is matched with modem driver %s",
251 __func__, iface_name.c_str(), driver_name.c_str());
Paul Stewart9364c4c2011-12-06 17:12:42 -0800252 return Technology::kCellular;
253 }
254 }
255
Ben Chan4e64d2d2012-05-16 00:02:25 -0700256 if (driver_name == kDriverGdmWiMax) {
257 SLOG(Device, 2) << StringPrintf("%s: device %s is a WiMAX device",
258 __func__, iface_name.c_str());
259 return Technology::kWiMax;
260 }
261
Thieu Le8f1c8352012-04-16 11:02:12 -0700262 // For cdc_ether devices, make sure it's a modem because this driver
263 // can be used for other ethernet devices.
Paul Stewart050cfc02012-07-06 20:38:54 -0700264 if (driver_name == kDriverCdcEther) {
265 if (IsCdcEtherModemDevice(iface_name)) {
266 LOG(INFO) << StringPrintf("%s: device %s is a modem cdc_ether "
267 "device", __func__, iface_name.c_str());
268 return Technology::kCellular;
269 }
270 SLOG(Device, 2) << StringPrintf("%s: device %s is a cdc_ether "
271 "device", __func__, iface_name.c_str());
272 return Technology::kCDCEthernet;
Thieu Le8f1c8352012-04-16 11:02:12 -0700273 }
274
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700275 // Special case for the virtio driver, used when run under KVM. See also
276 // the comment in VirtioEthernet::Start.
277 if (driver_name == kDriverVirtioNet) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700278 SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet",
279 __func__, iface_name.c_str());
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700280 return Technology::kVirtioEthernet;
281 }
282
Ben Chanfad4a0b2012-04-18 15:49:59 -0700283 SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, "
284 "is defaulted to type ethernet",
285 __func__, iface_name.c_str(),
286 driver_name.c_str());
Paul Stewartfdd16072011-09-16 12:41:35 -0700287 return Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700288}
289
Thieu Le8f1c8352012-04-16 11:02:12 -0700290bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) {
291 // A cdc_ether device is a modem device if it also exposes tty interfaces.
292 // To determine this, we look for the existence of the tty interface in the
293 // USB device sysfs tree.
294 //
295 // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as
296 // follows:
297 //
298 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2
299 // 1-2:1.0
300 // tty
301 // ttyACM0
302 // 1-2:1.1
303 // net
304 // usb0
305 // 1-2:1.2
306 // tty
307 // ttyACM1
308 // ...
309 //
310 // /sys/class/net/usb0/device symlinks to
311 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1
Thieu Leb27beee2012-04-20 09:19:06 -0700312 //
313 // Note that some modem devices have the tty directory one level deeper
314 // (eg. E362), so the device tree for the tty interface is:
315 // /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 -0700316
Paul Stewartca876ee2012-04-21 08:55:58 -0700317 FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice);
Thieu Le8f1c8352012-04-16 11:02:12 -0700318 FilePath device_path;
319 if (!file_util::ReadSymbolicLink(device_file, &device_path)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700320 SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink",
321 __func__, iface_name.c_str());
Thieu Le8f1c8352012-04-16 11:02:12 -0700322 return false;
323 }
324 if (!device_path.IsAbsolute()) {
325 device_path = device_file.DirName().Append(device_path);
326 file_util::AbsolutePath(&device_path);
327 }
328
329 // Look for tty interface by enumerating all directories under the parent
Thieu Leb27beee2012-04-20 09:19:06 -0700330 // USB device and see if there's a subdirectory "tty" inside. In other
331 // words, using the example dir hierarchy above, find
332 // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty.
Thieu Le8f1c8352012-04-16 11:02:12 -0700333 // If this exists, then this is a modem device.
Thieu Leb27beee2012-04-20 09:19:06 -0700334 return HasSubdir(device_path.DirName(), FilePath("tty"));
Thieu Le8f1c8352012-04-16 11:02:12 -0700335}
336
337// static
Thieu Leb27beee2012-04-20 09:19:06 -0700338bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) {
339 file_util::FileEnumerator::FileType type =
340 static_cast<file_util::FileEnumerator::FileType>(
341 file_util::FileEnumerator::DIRECTORIES |
342 file_util::FileEnumerator::SHOW_SYM_LINKS);
343 file_util::FileEnumerator dir_enum(base_dir, true, type);
Thieu Le8f1c8352012-04-16 11:02:12 -0700344 for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty();
345 curr_dir = dir_enum.Next()) {
Thieu Leb27beee2012-04-20 09:19:06 -0700346 if (curr_dir.BaseName() == subdir)
Thieu Le8f1c8352012-04-16 11:02:12 -0700347 return true;
348 }
349 return false;
350}
351
Paul Stewart8c116a92012-05-02 18:30:03 -0700352DeviceRefPtr DeviceInfo::CreateDevice(const string &link_name,
353 const string &address,
354 int interface_index,
355 Technology::Identifier technology) {
356 DeviceRefPtr device;
Paul Stewart050cfc02012-07-06 20:38:54 -0700357 delayed_devices_.erase(interface_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700358
359 switch (technology) {
360 case Technology::kCellular:
361 // Cellular devices are managed by ModemInfo.
362 SLOG(Device, 2) << "Cellular link " << link_name
363 << " at index " << interface_index
364 << " -- notifying ModemInfo.";
Gary Morainbf74a672012-07-30 16:27:19 -0700365
366 // The MAC address provided by RTNL is not reliable for Gobi 2K modems.
367 // Clear it here, and it will be fetched from the kernel is
368 // GetMacAddress().
369 infos_[interface_index].mac_address.Clear();
Paul Stewart8c116a92012-05-02 18:30:03 -0700370 manager_->modem_info()->OnDeviceInfoAvailable(link_name);
371 break;
372 case Technology::kEthernet:
373 device = new Ethernet(control_interface_, dispatcher_, metrics_,
374 manager_, link_name, address, interface_index);
375 device->EnableIPv6Privacy();
376 break;
377 case Technology::kVirtioEthernet:
378 device = new VirtioEthernet(control_interface_, dispatcher_, metrics_,
379 manager_, link_name, address,
380 interface_index);
381 device->EnableIPv6Privacy();
382 break;
383 case Technology::kWifi:
384 device = new WiFi(control_interface_, dispatcher_, metrics_, manager_,
385 link_name, address, interface_index);
386 device->EnableIPv6Privacy();
387 break;
Ben Chan4e64d2d2012-05-16 00:02:25 -0700388 case Technology::kWiMax:
Darin Petkove4b27022012-05-16 13:28:50 +0200389 // WiMax devices are managed by WiMaxProvider.
390 SLOG(Device, 2) << "WiMax link " << link_name
391 << " at index " << interface_index
392 << " -- notifying WiMaxProvider.";
393 manager_->wimax_provider()->OnDeviceInfoAvailable(link_name);
Ben Chan4e64d2d2012-05-16 00:02:25 -0700394 break;
Paul Stewart8c116a92012-05-02 18:30:03 -0700395 case Technology::kPPP:
396 case Technology::kTunnel:
397 // Tunnel and PPP devices are managed by the VPN code (PPP for
398 // l2tpipsec). Notify the VPN Provider of the interface's presence.
399 // Since CreateDevice is only called once in the lifetime of an
400 // interface index, this notification will only occur the first
401 // time the device is seen.
402 SLOG(Device, 2) << "Tunnel / PPP link " << link_name
403 << " at index " << interface_index
404 << " -- notifying VPNProvider.";
405 if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name,
406 interface_index) &&
407 technology == Technology::kTunnel) {
408 // If VPN does not know anything about this tunnel, it is probably
409 // left over from a previous instance and should not exist.
410 SLOG(Device, 2) << "Tunnel link is unused. Deleting.";
411 DeleteInterface(interface_index);
412 }
413 break;
414 case Technology::kLoopback:
415 // Loopback devices are largely ignored, but we should make sure the
416 // link is enabled.
417 SLOG(Device, 2) << "Bringing up loopback device " << link_name
418 << " at index " << interface_index;
419 rtnl_handler_->SetInterfaceFlags(interface_index, IFF_UP, IFF_UP);
420 return NULL;
Paul Stewart050cfc02012-07-06 20:38:54 -0700421 case Technology::kCDCEthernet:
422 // CDCEnternet devices are of indeterminate type when they are
423 // initially created. Some time later, tty devices may or may
424 // not appear under the same USB device root, which will identify
425 // it as a modem. Alternatively, ModemManager may discover the
426 // device and create and register a Cellular device. In either
427 // case, we should delay creating a Device until we can make a
428 // better determination of what type this Device should be.
429 LOG(INFO) << "Delaying creation of device for " << link_name
430 << " at index " << interface_index;
431 DelayDeviceCreation(interface_index);
432 return NULL;
Paul Stewart8c116a92012-05-02 18:30:03 -0700433 default:
434 // We will not manage this device in shill. Do not create a device
435 // object or do anything to change its state. We create a stub object
436 // which is useful for testing.
437 return new DeviceStub(control_interface_, dispatcher_, metrics_,
438 manager_, link_name, address, interface_index,
Ben Chan4e64d2d2012-05-16 00:02:25 -0700439 technology);
Paul Stewart8c116a92012-05-02 18:30:03 -0700440 }
441
442 // Reset the routing table and addresses.
443 routing_table_->FlushRoutes(interface_index);
444 FlushAddresses(interface_index);
445
446 return device;
447}
448
Chris Masone2aa97072011-08-09 17:35:08 -0700449void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700450 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
451 msg.mode() == RTNLMessage::kModeAdd);
Chris Masone2aa97072011-08-09 17:35:08 -0700452 int dev_index = msg.interface_index();
Paul Stewartfdd16072011-09-16 12:41:35 -0700453 Technology::Identifier technology = Technology::kUnknown;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700454
Darin Petkove6193c02011-08-11 12:42:40 -0700455 unsigned int flags = msg.link_status().flags;
456 unsigned int change = msg.link_status().change;
Paul Stewart8c116a92012-05-02 18:30:03 -0700457 bool new_device =
458 !ContainsKey(infos_, dev_index) || infos_[dev_index].has_addresses_only;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700459 SLOG(Device, 2) << __func__ << "(index=" << dev_index
460 << std::showbase << std::hex
461 << ", flags=" << flags << ", change=" << change << ")"
462 << std::dec << std::noshowbase
463 << ", new_device=" << new_device;
Paul Stewart8c116a92012-05-02 18:30:03 -0700464 infos_[dev_index].has_addresses_only = false;
Darin Petkove6193c02011-08-11 12:42:40 -0700465 infos_[dev_index].flags = flags;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700466
Paul Stewart1ac4e842012-07-10 12:58:12 -0700467 RetrieveLinkStatistics(dev_index, msg);
468
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700469 DeviceRefPtr device = GetDevice(dev_index);
Paul Stewart8c116a92012-05-02 18:30:03 -0700470 if (new_device) {
471 CHECK(!device);
Chris Masone2aa97072011-08-09 17:35:08 -0700472 if (!msg.HasAttribute(IFLA_IFNAME)) {
473 LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!";
474 return;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700475 }
Chris Masone2aa97072011-08-09 17:35:08 -0700476 ByteString b(msg.GetAttribute(IFLA_IFNAME));
477 string link_name(reinterpret_cast<const char*>(b.GetConstData()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700478 SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name;
Darin Petkovf8046b82012-04-24 16:29:23 +0200479 infos_[dev_index].name = link_name;
480 indices_[link_name] = dev_index;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700481
Chris Masone2aa97072011-08-09 17:35:08 -0700482 if (!link_name.empty()) {
mukesh agrawal8f317b62011-07-15 11:53:23 -0700483 if (ContainsKey(black_list_, link_name)) {
Paul Stewartfdd16072011-09-16 12:41:35 -0700484 technology = Technology::kBlacklisted;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700485 } else {
486 technology = GetDeviceTechnology(link_name);
487 }
Darin Petkov633ac6f2011-07-08 13:56:13 -0700488 }
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800489 string address;
490 if (msg.HasAttribute(IFLA_ADDRESS)) {
491 infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS);
492 address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
Ben Chanfad4a0b2012-04-18 15:49:59 -0700493 SLOG(Device, 2) << "link index " << dev_index << " address "
494 << infos_[dev_index].mac_address.HexEncode();
Paul Stewartca876ee2012-04-21 08:55:58 -0700495 } else if (technology != Technology::kTunnel &&
496 technology != Technology::kPPP) {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800497 LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!";
498 return;
499 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700500 device = CreateDevice(link_name, address, dev_index, technology);
501 if (device) {
502 RegisterDevice(device);
Paul Stewartb50f0b92011-05-16 16:31:42 -0700503 }
Paul Stewartb50f0b92011-05-16 16:31:42 -0700504 }
Paul Stewart8c116a92012-05-02 18:30:03 -0700505 if (device) {
506 device->LinkEvent(flags, change);
507 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700508}
509
Chris Masone2aa97072011-08-09 17:35:08 -0700510void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700511 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")";
mukesh agrawal47009f82011-08-25 14:07:35 -0700512
Paul Stewart9a908082011-08-31 12:18:48 -0700513 DCHECK(msg.type() == RTNLMessage::kTypeLink &&
514 msg.mode() == RTNLMessage::kModeDelete);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700515 SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index()
516 << std::showbase << std::hex
517 << ", flags=" << msg.link_status().flags
518 << ", change=" << msg.link_status().change << ")";
Darin Petkove6193c02011-08-11 12:42:40 -0700519 RemoveInfo(msg.interface_index());
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700520}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700521
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700522DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const {
523 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700524 return info ? info->device : NULL;
525}
526
Darin Petkovf8046b82012-04-24 16:29:23 +0200527int DeviceInfo::GetIndex(const string &interface_name) const {
528 map<string, int>::const_iterator it = indices_.find(interface_name);
529 return it == indices_.end() ? -1 : it->second;
530}
531
Paul Stewart32852962011-08-30 14:06:53 -0700532bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const {
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700533 const Info *info = GetInfo(interface_index);
534 if (!info) {
535 return false;
536 }
Gary Morainbf74a672012-07-30 16:27:19 -0700537 // |mac_address| from RTNL is not used for some devices, in which case it will
538 // be empty here.
539 if (!info->mac_address.IsEmpty()) {
540 *address = info->mac_address;
541 return true;
542 }
543
544 // Ask the kernel for the MAC address.
545 *address = GetMACAddressFromKernel(interface_index);
546 return !address->IsEmpty();
547}
548
549ByteString DeviceInfo::GetMACAddressFromKernel(int interface_index) const {
550 ByteString mac_address;
551 mac_address.Clear();
552 // TODO(gmorain): Use ScopedSocketCloser instead.
553 const int fd = socket(PF_INET, SOCK_DGRAM, 0);
554 if (fd < 0) {
555 LOG(ERROR) << __func__ << ": Unable to open socket: " << fd;
556 return mac_address;
557 }
558 struct ifreq ifr;
559 memset(&ifr, 0, sizeof(ifr));
560 ifr.ifr_ifindex = interface_index;
561 int err = HANDLE_EINTR(ioctl(fd, SIOCGIFNAME, &ifr));
562 if (err < 0) {
563 LOG(ERROR) << __func__ << ": Unable to read ifname: " << errno;
564 close(fd);
565 return mac_address;
566 }
567 err = HANDLE_EINTR(ioctl(fd, SIOCGIFHWADDR, &ifr));
568 close(fd);
569 if (err < 0) {
570 LOG(ERROR) << __func__ << ": Unable to read MAC address: " << errno;
571 return mac_address;
572 }
573 mac_address.Resize(IFHWADDRLEN);
574 memcpy(mac_address.GetData(), ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
575 return mac_address;
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700576}
577
Paul Stewart9a908082011-08-31 12:18:48 -0700578bool DeviceInfo::GetAddresses(int interface_index,
579 vector<AddressData> *addresses) const {
580 const Info *info = GetInfo(interface_index);
581 if (!info) {
582 return false;
583 }
584 *addresses = info->ip_addresses;
585 return true;
586}
587
588void DeviceInfo::FlushAddresses(int interface_index) const {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700589 SLOG(Device, 2) << __func__ << "(" << interface_index << ")";
Paul Stewart9a908082011-08-31 12:18:48 -0700590 const Info *info = GetInfo(interface_index);
591 if (!info) {
592 return;
593 }
594 const vector<AddressData> &addresses = info->ip_addresses;
595 vector<AddressData>::const_iterator iter;
596 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
Paul Stewart7355ce12011-09-02 10:47:01 -0700597 if (iter->address.family() == IPAddress::kFamilyIPv4 ||
Paul Stewart9a908082011-08-31 12:18:48 -0700598 (iter->scope == RT_SCOPE_UNIVERSE &&
599 (iter->flags & ~IFA_F_TEMPORARY) == 0)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700600 SLOG(Device, 2) << __func__ << ": removing ip address "
601 << iter->address.ToString()
602 << " from interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700603 rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address);
604 }
605 }
606}
607
Paul Stewart05a42c22012-08-02 16:47:21 -0700608bool DeviceInfo::HasOtherAddress(
609 int interface_index, const IPAddress &this_address) const {
610 SLOG(Device, 3) << __func__ << "(" << interface_index << ")";
611 const Info *info = GetInfo(interface_index);
612 if (!info) {
613 return false;
614 }
615 const vector<AddressData> &addresses = info->ip_addresses;
616 vector<AddressData>::const_iterator iter;
617 bool has_other_address = false;
618 bool has_this_address = false;
619 for (iter = addresses.begin(); iter != addresses.end(); ++iter) {
620 if (iter->address.family() != this_address.family()) {
621 continue;
622 }
623 if (iter->address.address().Equals(this_address.address())) {
624 has_this_address = true;
625 } else if (this_address.family() == IPAddress::kFamilyIPv4) {
626 has_other_address = true;
627 } else if ((iter->scope == RT_SCOPE_UNIVERSE &&
628 (iter->flags & IFA_F_TEMPORARY) == 0)) {
629 has_other_address = true;
630 }
631 }
632 return has_other_address && !has_this_address;
633}
634
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700635bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const {
636 const Info *info = GetInfo(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700637 if (!info) {
638 return false;
639 }
640 *flags = info->flags;
641 return true;
642}
643
Paul Stewart1ac4e842012-07-10 12:58:12 -0700644bool DeviceInfo::GetByteCounts(int interface_index,
645 uint64 *rx_bytes,
646 uint64 *tx_bytes) const {
647 const Info *info = GetInfo(interface_index);
648 if (!info) {
649 return false;
650 }
651 *rx_bytes = info->rx_bytes;
652 *tx_bytes = info->tx_bytes;
653 return true;
654}
655
Paul Stewartca6abd42012-03-01 15:45:29 -0800656bool DeviceInfo::CreateTunnelInterface(string *interface_name) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800657 int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR));
658 if (fd < 0) {
659 PLOG(ERROR) << "failed to open " << kTunDeviceName;
660 return false;
661 }
662 file_util::ScopedFD scoped_fd(&fd);
663
664 struct ifreq ifr;
665 memset(&ifr, 0, sizeof(ifr));
666 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
667 if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) {
668 PLOG(ERROR) << "failed to create tunnel interface";
669 return false;
670 }
671
672 if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) {
673 PLOG(ERROR) << "failed to set tunnel interface to be persistent";
674 return false;
675 }
676
677 *interface_name = string(ifr.ifr_name);
678
679 return true;
680}
681
Paul Stewartca6abd42012-03-01 15:45:29 -0800682bool DeviceInfo::DeleteInterface(int interface_index) const {
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800683 return rtnl_handler_->RemoveInterface(interface_index);
684}
685
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700686const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const {
687 map<int, Info>::const_iterator iter = infos_.find(interface_index);
Darin Petkove6193c02011-08-11 12:42:40 -0700688 if (iter == infos_.end()) {
689 return NULL;
690 }
691 return &iter->second;
692}
693
694void DeviceInfo::RemoveInfo(int interface_index) {
695 map<int, Info>::iterator iter = infos_.find(interface_index);
696 if (iter != infos_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700697 SLOG(Device, 2) << "Removing info for device index: " << interface_index;
Darin Petkove6193c02011-08-11 12:42:40 -0700698 if (iter->second.device.get()) {
699 manager_->DeregisterDevice(iter->second.device);
700 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200701 indices_.erase(iter->second.name);
Darin Petkove6193c02011-08-11 12:42:40 -0700702 infos_.erase(iter);
Paul Stewart050cfc02012-07-06 20:38:54 -0700703 delayed_devices_.erase(interface_index);
mukesh agrawal47009f82011-08-25 14:07:35 -0700704 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700705 SLOG(Device, 2) << __func__ << ": Unknown device index: "
706 << interface_index;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700707 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700708}
709
Chris Masone2aa97072011-08-09 17:35:08 -0700710void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) {
Paul Stewart9a908082011-08-31 12:18:48 -0700711 DCHECK(msg.type() == RTNLMessage::kTypeLink);
712 if (msg.mode() == RTNLMessage::kModeAdd) {
Chris Masone2aa97072011-08-09 17:35:08 -0700713 AddLinkMsgHandler(msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700714 } else if (msg.mode() == RTNLMessage::kModeDelete) {
Chris Masone2aa97072011-08-09 17:35:08 -0700715 DelLinkMsgHandler(msg);
716 } else {
717 NOTREACHED();
Paul Stewartb50f0b92011-05-16 16:31:42 -0700718 }
Paul Stewart0af98bf2011-05-10 17:38:08 -0700719}
720
Paul Stewart9a908082011-08-31 12:18:48 -0700721void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700722 SLOG(Device, 2) << __func__;
Paul Stewart9a908082011-08-31 12:18:48 -0700723 DCHECK(msg.type() == RTNLMessage::kTypeAddress);
724 int interface_index = msg.interface_index();
725 if (!ContainsKey(infos_, interface_index)) {
Paul Stewart8c116a92012-05-02 18:30:03 -0700726 SLOG(Device, 2) << "Got advance address information for unknown index "
727 << interface_index;
728 infos_[interface_index].has_addresses_only = true;
Paul Stewart9a908082011-08-31 12:18:48 -0700729 }
730 const RTNLMessage::AddressStatus &status = msg.address_status();
731 IPAddress address(msg.family(),
Ben Chan682c5d42012-06-18 14:11:04 -0700732 msg.HasAttribute(IFA_LOCAL) ?
733 msg.GetAttribute(IFA_LOCAL) : msg.GetAttribute(IFA_ADDRESS),
Paul Stewart9a908082011-08-31 12:18:48 -0700734 status.prefix_len);
735
Ben Chan682c5d42012-06-18 14:11:04 -0700736 SLOG_IF(Device, 2, msg.HasAttribute(IFA_LOCAL))
737 << "Found local address attribute for interface " << interface_index;
738
Paul Stewart9a908082011-08-31 12:18:48 -0700739 vector<AddressData> &address_list = infos_[interface_index].ip_addresses;
740 vector<AddressData>::iterator iter;
741 for (iter = address_list.begin(); iter != address_list.end(); ++iter) {
742 if (address.Equals(iter->address)) {
743 break;
744 }
745 }
746 if (iter != address_list.end()) {
747 if (msg.mode() == RTNLMessage::kModeDelete) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700748 SLOG(Device, 2) << "Delete address for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700749 address_list.erase(iter);
750 } else {
751 iter->flags = status.flags;
752 iter->scope = status.scope;
753 }
754 } else if (msg.mode() == RTNLMessage::kModeAdd) {
755 address_list.push_back(AddressData(address, status.flags, status.scope));
Paul Stewart8c116a92012-05-02 18:30:03 -0700756 SLOG(Device, 2) << "Add address " << address.ToString()
757 << " for interface " << interface_index;
Paul Stewart9a908082011-08-31 12:18:48 -0700758 }
759}
760
Paul Stewart050cfc02012-07-06 20:38:54 -0700761void DeviceInfo::DelayDeviceCreation(int interface_index) {
762 delayed_devices_.insert(interface_index);
763 delayed_devices_callback_.Reset(
764 Bind(&DeviceInfo::DelayedDeviceCreationTask, AsWeakPtr()));
765 dispatcher_->PostDelayedTask(delayed_devices_callback_.callback(),
766 kDelayedDeviceCreationSeconds * 1000);
767}
768
769// Re-evaluate the technology type for each delayed device.
770void DeviceInfo::DelayedDeviceCreationTask() {
771 while (!delayed_devices_.empty()) {
772 set<int>::iterator it = delayed_devices_.begin();
773 int dev_index = *it;
774 delayed_devices_.erase(it);
775
776 DCHECK(ContainsKey(infos_, dev_index));
777 DCHECK(!GetDevice(dev_index));
778
779 const string &link_name = infos_[dev_index].name;
780 Technology::Identifier technology = GetDeviceTechnology(link_name);
781
782 if (technology == Technology::kCDCEthernet) {
783 LOG(INFO) << "In " << __func__ << ": device " << link_name
784 << " is now assumed to be regular Ethernet.";
785 technology = Technology::kEthernet;
786 } else if (technology != Technology::kCellular) {
787 LOG(WARNING) << "In " << __func__ << ": device " << link_name
788 << " is unexpected technology "
789 << Technology::NameFromIdentifier(technology);
790 }
791 string address =
792 StringToLowerASCII(infos_[dev_index].mac_address.HexEncode());
793 DCHECK(!address.empty());
794
795 DeviceRefPtr device = CreateDevice(link_name, address, dev_index,
796 technology);
797 if (device) {
798 RegisterDevice(device);
799 }
800 }
801}
802
Paul Stewart1ac4e842012-07-10 12:58:12 -0700803void DeviceInfo::RetrieveLinkStatistics(int interface_index,
804 const RTNLMessage &msg) {
805 if (!msg.HasAttribute(IFLA_STATS64)) {
806 return;
807 }
808 ByteString stats_bytes(msg.GetAttribute(IFLA_STATS64));
809 struct rtnl_link_stats64 stats;
810 if (stats_bytes.GetLength() < sizeof(stats)) {
811 LOG(WARNING) << "Link statistics size is too small: "
812 << stats_bytes.GetLength() << " < " << sizeof(stats);
813 return;
814 }
815
816 memcpy(&stats, stats_bytes.GetConstData(), sizeof(stats));
817 SLOG(Device, 2) << "Link statistics for "
818 << " interface index " << interface_index << ": "
819 << "receive: " << stats.rx_bytes << "; "
820 << "transmit: " << stats.tx_bytes << ".";
821 infos_[interface_index].rx_bytes = stats.rx_bytes;
822 infos_[interface_index].tx_bytes = stats.tx_bytes;
823}
824
825void DeviceInfo::RequestLinkStatistics() {
826 rtnl_handler_->RequestDump(RTNLHandler::kRequestLink);
827 dispatcher_->PostDelayedTask(request_link_statistics_callback_.callback(),
828 kRequestLinkStatisticsIntervalSeconds * 1000);
829}
830
Paul Stewart0af98bf2011-05-10 17:38:08 -0700831} // namespace shill