blob: bdb7635b7ba77cf8c97d6f71978f91d6f7f30bf5 [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartb50f0b92011-05-16 16:31:42 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Chris Masoneb2e326b2011-07-12 13:28:51 -07005#include "shill/ethernet.h"
6
Paul Stewartb50f0b92011-05-16 16:31:42 -07007#include <time.h>
8#include <stdio.h>
Paul Stewartf1ce5d22011-05-19 13:10:20 -07009#include <netinet/ether.h>
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070010#include <linux/if.h> // Needs definitions from netinet/ether.h
Paul Stewartb50f0b92011-05-16 16:31:42 -070011
12#include <string>
13
14#include <base/logging.h>
15
Eric Shienbrood9a245532012-03-07 14:20:39 -050016#include "shill/adaptor_interfaces.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070017#include "shill/control_interface.h"
18#include "shill/device.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070019#include "shill/device_info.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070020#include "shill/ethernet_service.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070021#include "shill/event_dispatcher.h"
22#include "shill/manager.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070023#include "shill/profile.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070024#include "shill/rtnl_handler.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070025#include "shill/scope_logger.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070026
Chris Masonea82b7112011-05-25 15:16:29 -070027using std::string;
28
Paul Stewartb50f0b92011-05-16 16:31:42 -070029namespace shill {
Darin Petkovafa6fc42011-06-21 16:21:08 -070030
Paul Stewartb50f0b92011-05-16 16:31:42 -070031Ethernet::Ethernet(ControlInterface *control_interface,
32 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080033 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070034 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070035 const string &link_name,
mukesh agrawal93a29ed2012-04-17 16:13:01 -070036 const string &address,
Paul Stewartb50f0b92011-05-16 16:31:42 -070037 int interface_index)
Chris Masonea82b7112011-05-25 15:16:29 -070038 : Device(control_interface,
39 dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080040 metrics,
Chris Masonea82b7112011-05-25 15:16:29 -070041 manager,
42 link_name,
Chris Masone626719f2011-08-18 16:58:48 -070043 address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080044 interface_index,
45 Technology::kEthernet),
mukesh agrawalf60e4062011-05-27 13:13:41 -070046 service_registered_(false),
mukesh agrawalf60e4062011-05-27 13:13:41 -070047 link_up_(false) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070048 SLOG(Ethernet, 2) << "Ethernet device " << link_name << " initialized.";
Paul Stewartb50f0b92011-05-16 16:31:42 -070049}
50
51Ethernet::~Ethernet() {
Eric Shienbrood9a245532012-03-07 14:20:39 -050052 Stop(NULL, EnabledStateChangedCallback());
Paul Stewartf1ce5d22011-05-19 13:10:20 -070053}
54
Eric Shienbrood9a245532012-03-07 14:20:39 -050055void Ethernet::Start(Error *error,
56 const EnabledStateChangedCallback &callback) {
Paul Stewart2713d6c2011-08-25 15:38:15 -070057 service_ = new EthernetService(control_interface(),
58 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -080059 metrics(),
Paul Stewart2713d6c2011-08-25 15:38:15 -070060 manager(),
61 this);
Paul Stewartac4ac002011-08-26 12:04:26 -070062 RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index(), IFF_UP,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070063 IFF_UP);
Eric Shienbrood9a245532012-03-07 14:20:39 -050064 OnEnabledStateChanged(EnabledStateChangedCallback(), Error());
65 if (error)
66 error->Reset(); // indicate immediate completion
Paul Stewartf1ce5d22011-05-19 13:10:20 -070067}
68
Eric Shienbrood9a245532012-03-07 14:20:39 -050069void Ethernet::Stop(Error *error, const EnabledStateChangedCallback &callback) {
Paul Stewartf2792e82011-12-12 10:23:11 -080070 if (service_) {
71 manager()->DeregisterService(service_);
72 service_ = NULL;
73 }
Eric Shienbrood9a245532012-03-07 14:20:39 -050074 OnEnabledStateChanged(EnabledStateChangedCallback(), Error());
75 if (error)
76 error->Reset(); // indicate immediate completion
Paul Stewartb50f0b92011-05-16 16:31:42 -070077}
78
Paul Stewartfdd16072011-09-16 12:41:35 -070079bool Ethernet::TechnologyIs(const Technology::Identifier type) const {
80 return type == Technology::kEthernet;
Paul Stewartb50f0b92011-05-16 16:31:42 -070081}
82
Darin Petkovafa6fc42011-06-21 16:21:08 -070083void Ethernet::LinkEvent(unsigned int flags, unsigned int change) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -070084 Device::LinkEvent(flags, change);
85 if ((flags & IFF_LOWER_UP) != 0 && !link_up_) {
Paul Stewartac4ac002011-08-26 12:04:26 -070086 LOG(INFO) << link_name() << " is up; should start L3!";
Paul Stewartf1ce5d22011-05-19 13:10:20 -070087 link_up_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -050088 if (service_) {
89 manager()->RegisterService(service_);
90 if (service_->auto_connect()) {
Paul Stewartd408fdf2012-05-07 17:15:57 -070091 if (AcquireIPConfigWithLeaseName(service_->GetStorageIdentifier())) {
Eric Shienbrood9a245532012-03-07 14:20:39 -050092 SelectService(service_);
93 SetServiceState(Service::kStateConfiguring);
94 } else {
95 LOG(ERROR) << "Unable to acquire DHCP config.";
96 }
Paul Stewart03dba0b2011-08-22 16:32:45 -070097 }
Darin Petkovafa6fc42011-06-21 16:21:08 -070098 }
Paul Stewartf1ce5d22011-05-19 13:10:20 -070099 } else if ((flags & IFF_LOWER_UP) == 0 && link_up_) {
100 link_up_ = false;
Paul Stewart63c93932012-01-06 11:24:12 -0800101 DestroyIPConfig();
Paul Stewartac4ac002011-08-26 12:04:26 -0700102 manager()->DeregisterService(service_);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700103 SelectService(NULL);
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700104 }
105}
106
Paul Stewartb50f0b92011-05-16 16:31:42 -0700107} // namespace shill