Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 16 | |
Ben Chan | 8760251 | 2014-11-07 20:50:05 -0800 | [diff] [blame] | 17 | #include "shill/ethernet/virtio_ethernet.h" |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 18 | |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <string> |
| 22 | |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 23 | #include "shill/control_interface.h" |
| 24 | #include "shill/event_dispatcher.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 25 | #include "shill/logging.h" |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 26 | #include "shill/manager.h" |
| 27 | |
| 28 | using std::string; |
| 29 | |
| 30 | namespace shill { |
| 31 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 32 | namespace Logging { |
| 33 | static auto kModuleLogScope = ScopeLogger::kEthernet; |
Paul Stewart | 7e779d8 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 34 | static string ObjectID(VirtioEthernet* v) { return v->GetRpcIdentifier(); } |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Paul Stewart | 7e779d8 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 37 | VirtioEthernet::VirtioEthernet(ControlInterface* control_interface, |
| 38 | EventDispatcher* dispatcher, |
| 39 | Metrics* metrics, |
| 40 | Manager* manager, |
| 41 | const string& link_name, |
| 42 | const string& address, |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 43 | int interface_index) |
| 44 | : Ethernet(control_interface, |
| 45 | dispatcher, |
| 46 | metrics, |
| 47 | manager, |
| 48 | link_name, |
| 49 | address, |
| 50 | interface_index) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 51 | SLOG(this, 2) << "VirtioEthernet device " << link_name << " initialized."; |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | VirtioEthernet::~VirtioEthernet() { |
| 55 | // Nothing to be done beyond what Ethernet dtor does. |
| 56 | } |
| 57 | |
Paul Stewart | 7e779d8 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 58 | void VirtioEthernet::Start(Error* error, |
| 59 | const EnabledStateChangedCallback& callback) { |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 60 | // We are sometimes instantiated (by DeviceInfo) before the Linux kernel |
| 61 | // has completed the setup function for the device (virtio_net:virtnet_probe). |
| 62 | // |
| 63 | // Furthermore, setting the IFF_UP flag on the device (as done in |
| 64 | // Ethernet::Start) may cause the kernel IPv6 code to send packets even |
| 65 | // though virtnet_probe has not completed. |
| 66 | // |
| 67 | // When that happens, the device gets stuck in a state where it cannot |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 68 | // transmit any frames. (See crbug.com/212041) |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 69 | // |
| 70 | // To avoid this, we sleep to let the device setup function complete. |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 71 | SLOG(this, 2) << "Sleeping to let virtio initialize."; |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 72 | sleep(2); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 73 | SLOG(this, 2) << "Starting virtio Ethernet."; |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 74 | Ethernet::Start(error, callback); |
| 75 | } |
| 76 | |
| 77 | } // namespace shill |