Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 1 | // 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 Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 7 | #include <netinet/ether.h> |
| 8 | #include <linux/if.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 9 | |
| 10 | #include <string> |
| 11 | |
| 12 | #include <base/logging.h> |
| 13 | |
| 14 | #include "shill/control_interface.h" |
| 15 | #include "shill/device.h" |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 16 | #include "shill/device_info.h" |
| 17 | #include "shill/manager.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 18 | #include "shill/shill_event.h" |
| 19 | |
| 20 | #include "shill/ethernet.h" |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 21 | #include "shill/ethernet_service.h" |
| 22 | #include "shill/rtnl_handler.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 23 | |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 24 | using std::string; |
| 25 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 26 | namespace shill { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 27 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 28 | Ethernet::Ethernet(ControlInterface *control_interface, |
| 29 | EventDispatcher *dispatcher, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 30 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 31 | const string &link_name, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 32 | int interface_index) |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 33 | : Device(control_interface, |
| 34 | dispatcher, |
| 35 | manager, |
| 36 | link_name, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 37 | interface_index), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 38 | service_registered_(false), |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 39 | service_(new EthernetService(control_interface, |
| 40 | dispatcher, |
| 41 | this, |
| 42 | "service-" + link_name)), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 43 | link_up_(false) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 44 | VLOG(2) << "Ethernet device " << link_name << " initialized."; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | Ethernet::~Ethernet() { |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 48 | Stop(); |
| 49 | } |
| 50 | |
| 51 | void Ethernet::Start() { |
| 52 | Device::Start(); |
| 53 | RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index_, IFF_UP, |
| 54 | IFF_UP); |
| 55 | } |
| 56 | |
| 57 | void Ethernet::Stop() { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 58 | manager_->DeregisterService(service_); |
| 59 | DestroyIPConfig(); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 60 | Device::Stop(); |
| 61 | RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index_, 0, IFF_UP); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | bool Ethernet::TechnologyIs(const Device::Technology type) { |
| 65 | return type == Device::kEthernet; |
| 66 | } |
| 67 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 68 | void Ethernet::LinkEvent(unsigned int flags, unsigned int change) { |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 69 | Device::LinkEvent(flags, change); |
| 70 | if ((flags & IFF_LOWER_UP) != 0 && !link_up_) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 71 | LOG(INFO) << link_name() << " is up; should start L3!"; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 72 | link_up_ = true; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 73 | manager_->RegisterService(service_); |
| 74 | if (service_->auto_connect()) { |
| 75 | LOG_IF(ERROR, !AcquireDHCPConfig()) << "Unable to acquire DHCP config."; |
| 76 | } |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 77 | } else if ((flags & IFF_LOWER_UP) == 0 && link_up_) { |
| 78 | link_up_ = false; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 79 | manager_->DeregisterService(service_); |
| 80 | DestroyIPConfig(); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 84 | } // namespace shill |