?? show_interfaces.cc
字號:
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-// Copyright (c) 2001-2003 International Computer Science Institute//// Permission is hereby granted, free of charge, to any person obtaining a// copy of this software and associated documentation files (the "Software")// to deal in the Software without restriction, subject to the conditions// listed in the XORP LICENSE file. These conditions include: you must// preserve this copyright notice, and you cannot mention the copyright// holders in advertising related to the Software without their permission.// The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This// notice is a summary of the XORP LICENSE file; the license in that file is// legally binding.#ident "$XORP: xorp/rtrmgr/tools/show_interfaces.cc,v 1.10 2003/10/24 20:48:52 pavlin Exp $"#include "rtrmgr/rtrmgr_module.h"#include "config.h"#include "libxorp/xlog.h"#include "libxorp/debug.h"#include "show_interfaces.hh"InterfaceMonitor::InterfaceMonitor(XrlRouter& xrl_rtr, EventLoop& eventloop) : _xrl_rtr(xrl_rtr), _eventloop(eventloop), _ifmgr_client(&xrl_rtr){ _state = INITIALIZING; _register_retry_counter = 0; _interfaces_remaining = 0; _vifs_remaining = 0; _addrs_remaining = 0; _flags_remaining = 0;}InterfaceMonitor::~InterfaceMonitor(){ map <string, Vif*>::iterator i; for (i = _vifs_by_name.begin(); i != _vifs_by_name.end(); i++) { delete i->second; } for (i = _vifs_by_name.begin(); i != _vifs_by_name.end(); i++) { _vifs_by_name.erase(i); }}voidInterfaceMonitor::start(){ clean_out_old_state();}voidInterfaceMonitor::clean_out_old_state(){ // we call unregister_client first, to cause the FEA to remove any // registrations left over from previous incarnations of the RIB XorpCallback1<void, const XrlError&>::RefPtr cb; cb = callback(this, &InterfaceMonitor::clean_out_old_state_done); if (_ifmgr_client.send_unregister_client("fea", _xrl_rtr.name(), cb) == false) { XLOG_ERROR("Failed to unregister fea client"); _state = FAILED; }}voidInterfaceMonitor::clean_out_old_state_done(const XrlError& e){ UNUSED(e); // We really don't care here if the request succeeded or failed. // It's normal to fail with COMMAND_FAILED if there was no state // left behind from a previous incarnation. Any other errors would // also show up in register_if_spy, so we'll let that deal with // them. register_if_spy();}voidInterfaceMonitor::register_if_spy(){ XorpCallback1<void, const XrlError&>::RefPtr cb; cb = callback(this, &InterfaceMonitor::register_if_spy_done); if (_ifmgr_client.send_register_client("fea", _xrl_rtr.name(), cb) == false) { XLOG_ERROR("Failed to register fea client"); _state = FAILED; }}voidInterfaceMonitor::register_if_spy_done(const XrlError& e){ if (e == XrlError::OKAY()) { // The registration was successful. Now we need to query the // entries that are already there. First, find out the set of // configured interfaces. XorpCallback2<void, const XrlError&, const XrlAtomList*>::RefPtr cb; cb = callback(this, &InterfaceMonitor::interface_names_done); if (_ifmgr_client.send_get_configured_interface_names("fea", cb) == false) { XLOG_ERROR("Failed to request interface names"); _state = FAILED; } return; } // if the resolve failed, it could be that we got going too quickly // for the FEA. Retry every two seconds. If after ten seconds we // still can't register, give up. It's a higher level issue as to // whether failing to register is a fatal error. if (e == XrlError::RESOLVE_FAILED() && (_register_retry_counter < 5)) { XLOG_WARNING("Register Interface Spy: RESOLVE_FAILED"); _register_retry_counter++; OneoffTimerCallback cb; cb = callback(this, &InterfaceMonitor::register_if_spy); _register_retry_timer = _eventloop.new_oneoff_after_ms(2000, cb); return; } _state = FAILED; XLOG_ERROR("Register Interface Spy: Permanent Error");}voidInterfaceMonitor::interface_names_done(const XrlError& e, const XrlAtomList* alist){ debug_msg("interface_names_done\n"); if (e == XrlError::OKAY()) { debug_msg("OK\n"); for (u_int i = 0; i < alist->size(); i++) { // Spin through the list of interfaces, and fire off // requests in parallel for all the Vifs on each interface XrlAtom atom = alist->get(i); string ifname = atom.text(); XorpCallback2<void, const XrlError&, const XrlAtomList*>::RefPtr cb; debug_msg("got interface name: %s\n", ifname.c_str()); cb = callback(this, &InterfaceMonitor::vif_names_done, ifname); if (_ifmgr_client.send_get_configured_vif_names("fea", ifname, cb) == false) { XLOG_ERROR("Failed to request vif names"); _state = FAILED; } _interfaces_remaining++; } return; } _state = FAILED; XLOG_ERROR("Get Interface Names: Permanent Error");}voidInterfaceMonitor::vif_names_done(const XrlError& e, const XrlAtomList* alist, string ifname){ debug_msg("vif_names_done\n"); UNUSED(ifname); if (e == XrlError::OKAY()) { for (u_int i = 0; i < alist->size(); i++) { // Spin through all the Vifs on this interface, and fire // off requests in parallel for all the addresses on each // Vif. XrlAtom atom = alist->get(i); string vifname = atom.text(); vif_created(ifname, vifname); XorpCallback2<void, const XrlError&, const XrlAtomList*>::RefPtr cb; cb = callback(this, &InterfaceMonitor::get_vifaddr4_done, ifname, vifname); if (_ifmgr_client.send_get_configured_vif_addresses4("fea", ifname, vifname, cb) == false) { XLOG_ERROR("Failed to request IPv4 addresses"); _state = FAILED; } _vifs_remaining++; } _interfaces_remaining--; } else if (e == XrlError::COMMAND_FAILED()) { // perhaps the interface went away. _interfaces_remaining--; } else { XLOG_ERROR("Get VIF Names: Permanent Error"); _state = FAILED; return; } if (_interfaces_remaining == 0 && _vifs_remaining == 0 && _addrs_remaining == 0 && _flags_remaining == 0) { _state = READY; }}voidInterfaceMonitor::get_vifaddr4_done(const XrlError& e, const XrlAtomList* alist, string ifname, string vifname){ if (e == XrlError::OKAY()) { for (u_int i = 0; i < alist->size(); i++) { XrlAtom atom = alist->get(i); IPv4 addr = atom.ipv4(); vifaddr4_created(ifname, vifname, addr); XorpCallback6<void, const XrlError&, const bool*, const bool*, const bool*, const bool*, const bool*>::RefPtr cb; cb = callback(this, &InterfaceMonitor::get_flags4_done, ifname, vifname, addr); if (_ifmgr_client.send_get_configured_address_flags4("fea", ifname, vifname, addr, cb) == false) { XLOG_ERROR("Failed to request interface flags"); _state = FAILED; return; } _flags_remaining++; } _vifs_remaining--; } else if (e == XrlError::COMMAND_FAILED()) { // perhaps the vif went away? _vifs_remaining--; } else { XLOG_ERROR("Get VIF Names: Permanent Error"); _state = FAILED; return; } if (_interfaces_remaining == 0 && _vifs_remaining == 0 && _addrs_remaining == 0 && _flags_remaining == 0) { _state = READY; }}voidInterfaceMonitor::get_flags4_done(const XrlError& e, const bool* enabled, const bool* broadcast, const bool* loopback, const bool* point_to_point, const bool* multicast, string ifname, string vifname, IPv4 addr){ UNUSED(addr); UNUSED(ifname); if (e == XrlError::OKAY()) { if (_vifs_by_name.find(vifname) == _vifs_by_name.end()) { // silently ignore - the vif could have been deleted while we // were waiting for the answer. return; } Vif* vif = _vifs_by_name[vifname]; // XXX this should be per-addr, not per VIF! vif->set_multicast_capable(*multicast); vif->set_p2p(*point_to_point); vif->set_broadcast_capable(*broadcast); vif->set_underlying_vif_up(*enabled); vif->set_loopback(*loopback); _flags_remaining--; } else if (e == XrlError::COMMAND_FAILED()) { // perhaps the vif went away? _flags_remaining--; } else { _state = FAILED; XLOG_ERROR("Get VIF Flags: Permanent Error"); return; } if (_interfaces_remaining == 0 && _vifs_remaining == 0 && _addrs_remaining == 0 && _flags_remaining == 0) { _state = READY; }}voidInterfaceMonitor::interface_update(const string& ifname, const uint32_t& event){ switch (event) { case IF_EVENT_CREATED: // doesn't directly affect vifs - we'll get a vif_update for // any new vifs break; case IF_EVENT_DELETED: interface_deleted(ifname); break; case IF_EVENT_CHANGED: // doesn't directly affect vifs break; }}voidInterfaceMonitor::vif_update(const string& ifname, const string& vifname, const uint32_t& event){ switch (event) { case IF_EVENT_CREATED: vif_created(ifname, vifname); break; case IF_EVENT_DELETED: vif_deleted(ifname, vifname); break; case IF_EVENT_CHANGED: // doesn't directly affect us break; }}voidInterfaceMonitor::vifaddr4_update(const string& ifname, const string& vifname, const IPv4& addr, const uint32_t& event){ switch (event) { case IF_EVENT_CREATED: vifaddr4_created(ifname, vifname, addr); break; case IF_EVENT_DELETED:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -