blob: 1a269c62f8f9a3fee4ad26001a7663051ff23c2e [file] [log] [blame]
Paul Stewartb50f0b92011-05-16 16:31:42 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <time.h>
6#include <stdio.h>
Paul Stewartf1ce5d22011-05-19 13:10:20 -07007#include <netinet/ether.h>
8#include <linux/if.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -07009
10#include <string>
11
12#include <base/logging.h>
13
14#include "shill/control_interface.h"
15#include "shill/device.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070016#include "shill/device_info.h"
17#include "shill/manager.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070018#include "shill/shill_event.h"
19
20#include "shill/ethernet.h"
Paul Stewartf1ce5d22011-05-19 13:10:20 -070021#include "shill/ethernet_service.h"
22#include "shill/rtnl_handler.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070023
Chris Masonea82b7112011-05-25 15:16:29 -070024using std::string;
25
Paul Stewartb50f0b92011-05-16 16:31:42 -070026namespace shill {
Darin Petkovafa6fc42011-06-21 16:21:08 -070027
Paul Stewartb50f0b92011-05-16 16:31:42 -070028Ethernet::Ethernet(ControlInterface *control_interface,
29 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070030 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070031 const string &link_name,
Paul Stewartb50f0b92011-05-16 16:31:42 -070032 int interface_index)
Chris Masonea82b7112011-05-25 15:16:29 -070033 : Device(control_interface,
34 dispatcher,
35 manager,
36 link_name,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070037 interface_index),
mukesh agrawalf60e4062011-05-27 13:13:41 -070038 service_registered_(false),
Chris Masonea82b7112011-05-25 15:16:29 -070039 service_(new EthernetService(control_interface,
40 dispatcher,
41 this,
42 "service-" + link_name)),
mukesh agrawalf60e4062011-05-27 13:13:41 -070043 link_up_(false) {
Darin Petkovafa6fc42011-06-21 16:21:08 -070044 VLOG(2) << "Ethernet device " << link_name << " initialized.";
Paul Stewartb50f0b92011-05-16 16:31:42 -070045}
46
47Ethernet::~Ethernet() {
Paul Stewartf1ce5d22011-05-19 13:10:20 -070048 Stop();
49}
50
51void Ethernet::Start() {
52 Device::Start();
53 RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index_, IFF_UP,
54 IFF_UP);
55}
56
57void Ethernet::Stop() {
Darin Petkovafa6fc42011-06-21 16:21:08 -070058 manager_->DeregisterService(service_);
59 DestroyIPConfig();
Paul Stewartf1ce5d22011-05-19 13:10:20 -070060 Device::Stop();
61 RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index_, 0, IFF_UP);
Paul Stewartb50f0b92011-05-16 16:31:42 -070062}
63
64bool Ethernet::TechnologyIs(const Device::Technology type) {
65 return type == Device::kEthernet;
66}
67
Darin Petkovafa6fc42011-06-21 16:21:08 -070068void Ethernet::LinkEvent(unsigned int flags, unsigned int change) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -070069 Device::LinkEvent(flags, change);
70 if ((flags & IFF_LOWER_UP) != 0 && !link_up_) {
Darin Petkovafa6fc42011-06-21 16:21:08 -070071 LOG(INFO) << link_name() << " is up; should start L3!";
Paul Stewartf1ce5d22011-05-19 13:10:20 -070072 link_up_ = true;
Darin Petkovafa6fc42011-06-21 16:21:08 -070073 manager_->RegisterService(service_);
74 if (service_->auto_connect()) {
75 LOG_IF(ERROR, !AcquireDHCPConfig()) << "Unable to acquire DHCP config.";
76 }
Paul Stewartf1ce5d22011-05-19 13:10:20 -070077 } else if ((flags & IFF_LOWER_UP) == 0 && link_up_) {
78 link_up_ = false;
Darin Petkovafa6fc42011-06-21 16:21:08 -070079 manager_->DeregisterService(service_);
80 DestroyIPConfig();
Paul Stewartf1ce5d22011-05-19 13:10:20 -070081 }
82}
83
Paul Stewartb50f0b92011-05-16 16:31:42 -070084} // namespace shill