1c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 2c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Copyright (C) 2015 The Android Open Source Project 3c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 4c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Licensed under the Apache License, Version 2.0 (the "License"); 5c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// you may not use this file except in compliance with the License. 6c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// You may obtain a copy of the License at 7c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 8c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// http://www.apache.org/licenses/LICENSE-2.0 9c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 10c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Unless required by applicable law or agreed to in writing, software 11c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// distributed under the License is distributed on an "AS IS" BASIS, 12c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// See the License for the specific language governing permissions and 14c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// limitations under the License. 15c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 16fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 17fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu#ifndef SHILL_PASSIVE_LINK_MONITOR_H_ 18fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu#define SHILL_PASSIVE_LINK_MONITOR_H_ 19fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 20fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu#include <base/callback.h> 21fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu#include <base/cancelable_callback.h> 22fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 23fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu#include "shill/refptr_types.h" 24fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 25fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiunamespace shill { 26fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 27fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiuclass ArpClient; 28fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiuclass EventDispatcher; 29fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiuclass IOHandler; 30fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 31fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu// PassiveLinkMonitor tracks the status of a connection by monitoring ARP 32fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu// requests received on the given interface. Each cycle consist of 25 seconds, 33fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu// with at lease 5 ARP requests expected in a cycle, a callback indicating 34fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu// failure will be invoke if that expectation is not met. Caller can specify 35fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu// number of cycles to monitor, once that number is reached without any 36fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu// failures, a callback indicating success will be invoked. Monitor will 37fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu// automatically stop when the monitor results in either failure or success. 38fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiuclass PassiveLinkMonitor { 39fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu public: 40fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu typedef base::Callback<void(bool)> ResultCallback; 41fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 42fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // The default number of cycles to monitor for. 43fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu static const int kDefaultMonitorCycles; 44fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 451a212a6b98b22ad1d69652bb26a9e94138635476Paul Stewart PassiveLinkMonitor(const ConnectionRefPtr& connection, 461a212a6b98b22ad1d69652bb26a9e94138635476Paul Stewart EventDispatcher* dispatcher, 471a212a6b98b22ad1d69652bb26a9e94138635476Paul Stewart const ResultCallback& result_callback); 48fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu virtual ~PassiveLinkMonitor(); 49fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 50fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // Starts passive link-monitoring for the specified number of cycles. 51fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu virtual bool Start(int num_cycles); 52fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // Stop passive link-monitoring. Clears any accumulated statistics. 53fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu virtual void Stop(); 54fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 55fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu private: 56fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu friend class PassiveLinkMonitorTest; 57fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 58fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // The number of milliseconds per cycle. 59fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu static const int kCyclePeriodMilliseconds; 60fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 61fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // Minimum number of ARP requests expected per cycle. 62fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu static const int kMinArpRequestsPerCycle; 63fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 64fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu bool StartArpClient(); 65fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu void StopArpClient(); 66fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 67fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // Callback to be invoked whenever the ARP reception socket has data 68fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // available to be received. 69fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu void ReceiveRequest(int fd); 70fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // Callback to be invoked when cycle period is reached without receiving 71fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // the expected number of ARP requests. 72fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu void CycleTimeoutHandler(); 73fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // Method to be called when the monitor is completed. 74fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu void MonitorCompleted(bool status); 75fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 76fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // The connection on which to perform passive link monitoring. 77fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu ConnectionRefPtr connection_; 78fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // The dispatcher on which to create delayed tasks. 791a212a6b98b22ad1d69652bb26a9e94138635476Paul Stewart EventDispatcher* dispatcher_; 80fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // ArpClient instance for monitoring ARP requests. 81fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu std::unique_ptr<ArpClient> arp_client_; 82fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // Callback to be invoked when monitor is completed, either failure or 83fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // success. 84fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu ResultCallback result_callback_; 85fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 86fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // Number of cycles to monitor for. 87fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu int num_cycles_to_monitor_; 88fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // Number of ARP requests received in current cycle. 89fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu int num_requests_received_; 90fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // Number of cycles passed so far. 91fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu int num_cycles_passed_; 92fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 93fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // IOCallback that fires when the socket associated with our ArpClient 94fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // has a packet to be received. Calls ReceiveRequest(). 95fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu std::unique_ptr<IOHandler> receive_request_handler_; 96fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu // Callback for handling cycle timeout. 97fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu base::CancelableClosure monitor_cycle_timeout_callback_; 98bf767f27ac66de32c6ce025b34a01029e3cf698aPeter Qiu // Callback for handling monitor completed event. 99bf767f27ac66de32c6ce025b34a01029e3cf698aPeter Qiu base::CancelableClosure monitor_completed_callback_; 100fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 101fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu DISALLOW_COPY_AND_ASSIGN(PassiveLinkMonitor); 102fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu}; 103fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 104fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu} // namespace shill 105fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu 106fcc00ee06a68504b6c26977942c11e26ff6080d9Peter Qiu#endif // SHILL_PASSIVE_LINK_MONITOR_H_ 107