📄 aodv.cc
字号:
/* aodv.cc $Id: aodv.cc,v 1.7 2000/03/10 00:57:29 yaxu Exp $ *//* The AODV code developed by the CMU/MONARCH group was optimized * and tuned by Samir Das (UTSA) and Mahesh Marina (UTSA). The * work was partially done in Sun Microsystems. * * The original CMU copyright is below. *//*Copyright (c) 1997, 1998 Carnegie Mellon University. All RightsReserved. Permission to use, copy, modify, and distribute thissoftware and its documentation is hereby granted (including forcommercial or for-profit use), provided that both the copyright noticeand this permission notice appear in all copies of the software,derivative works, or modified versions, and any portions thereof, andthat both notices appear in supporting documentation, and that creditis given to Carnegie Mellon University in all publications reportingon direct or indirect use of this code or its derivatives.ALL CODE, SOFTWARE, PROTOCOLS, AND ARCHITECTURES DEVELOPED BY THE CMUMONARCH PROJECT ARE EXPERIMENTAL AND ARE KNOWN TO HAVE BUGS, SOME OFWHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON PROVIDES THISSOFTWARE OR OTHER INTELLECTUAL PROPERTY IN ITS ``AS IS'' CONDITION,AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULARPURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITYBE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, ORCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OFSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; ORBUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCEOR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE ORINTELLECTUAL PROPERTY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCHDAMAGE.Carnegie Mellon encourages (but does not require) users of thissoftware or intellectual property to return any improvements orextensions that they make, and to grant Carnegie Mellon the rights toredistribute these changes without encumbrance.*/#include <aodv/aodv.h>#include <aodv/aodv_packet.h>#include <ip.h>#include <random.h>#include <cmu-trace.h>#define max(a,b) a > b ? a : b#define CURRENT_TIME Scheduler::instance().clock()//#define DEBUG#ifdef DEBUGstatic int extra_route_reply = 0;static int limit_route_request = 0;static int route_request = 0;#endif/* =================================================================== TCL Hooks ================================================================= */static class AODVHeaderClass : public PacketHeaderClass {public: AODVHeaderClass() : PacketHeaderClass("PacketHeader/AODV", AODV_HDR_LEN) { } } class_rtProtoAODV_hdr;static class AODVclass : public TclClass {public: AODVclass() : TclClass("Agent/AODV") {} TclObject* create(int argc, const char*const* argv) { assert(argc == 5); return (new AODV((nsaddr_t) atoi(argv[4]))); }} class_rtProtoAODV;/* ================================================================ Timers ============================================================= */voidBroadcastTimer::handle(Event*){ agent->id_purge(); Scheduler::instance().schedule(this, &intr, BCAST_ID_SAVE);}voidHelloTimer::handle(Event*){ agent->sendHello(); double interval = MinHelloInterval + ((MaxHelloInterval - MinHelloInterval) * Random::uniform()); assert(interval >= 0); Scheduler::instance().schedule(this, &intr, interval);}voidNeighborTimer::handle(Event*){ agent->nb_purge(); Scheduler::instance().schedule(this, &intr, HELLO_INTERVAL);}voidRouteCacheTimer::handle(Event*){ agent->rt_purge();#define FREQUENCY 0.5 // sec Scheduler::instance().schedule(this, &intr, FREQUENCY);}voidLocalRepairTimer::handle(Event* p) // SRD: 5/4/99{ rt_entry *rt; /* you get here after the timeout in a local repair attempt */ /* fprintf(stderr, "%s\n", __FUNCTION__); */ struct hdr_ip *ih = HDR_IP( (Packet *)p); // rt = agent->rtable.rt_lookup(ih->dst_); rt = agent->rtable.rt_lookup(ih->daddr()); if (rt && rt->rt_flags != RTF_UP) { // route is yet to be repaired // I will be conservative and bring down the route // and send triggered replies upstream. /* The following assert fails, not sure why */ /* assert (rt->rt_flags == RTF_IN_REPAIR); */ agent->rt_down(rt);/* printf("Node %d: Dst - %d, failed local repair\n",index, rt->rt_dst);*/ } Packet::free((Packet *)p);}/*================================================================== */AODV::AODV(nsaddr_t id) : Agent(PT_AODV), btimer(this), htimer(this), ntimer(this), rtimer(this), lrtimer(this), rqueue(){ bind("off_AODV_", &off_AODV_); index = id; seqno = 1; bid = 1; LIST_INIT(&nbhead); LIST_INIT(&bihead); logtarget = 0; ifqueue = 0;}intAODV::command(int argc, const char*const* argv){ if(argc == 2) { Tcl& tcl = Tcl::instance(); if(strncasecmp(argv[1], "id", 2) == 0) { tcl.resultf("%d", index); return TCL_OK; } if(strncasecmp(argv[1], "start", 2) == 0) { btimer.handle((Event*) 0);#ifndef AODV_LINK_LAYER_DETECTION htimer.handle((Event*) 0); ntimer.handle((Event*) 0);#endif rtimer.handle((Event*) 0); return TCL_OK; } } else if(argc == 3) { if(strcmp(argv[1], "index") == 0) { index = atoi(argv[2]); return TCL_OK; } else if(strcmp(argv[1], "log-target") == 0 || strcmp(argv[1], "tracetarget") == 0 ) { logtarget = (Trace*) TclObject::lookup(argv[2]); if(logtarget == 0) return TCL_ERROR; return TCL_OK; } else if(strcmp(argv[1], "drop-target") == 0) { int stat = rqueue.command(argc,argv); if (stat != TCL_OK) return stat; return Agent::command(argc, argv); } else if(strcmp(argv[1], "if-queue") == 0) { ifqueue = (PriQueue*) TclObject::lookup(argv[2]); if(ifqueue == 0) return TCL_ERROR; return TCL_OK; } } return Agent::command(argc, argv);}/* ===================================================================== Neighbor Management Functions ===================================================================== */voidAODV::nb_insert(nsaddr_t id){ Neighbor *nb = new Neighbor(id); assert(nb); nb->nb_expire = CURRENT_TIME + (1.5 * ALLOWED_HELLO_LOSS * HELLO_INTERVAL); LIST_INSERT_HEAD(&nbhead, nb, nb_link); seqno += 1; // set of neighbors changed}Neighbor*AODV::nb_lookup(nsaddr_t id){ Neighbor *nb = nbhead.lh_first; for(; nb; nb = nb->nb_link.le_next) { if(nb->nb_addr == id) break; } return nb;}/* * Called when we receive *explicit* notification that a Neighbor * is no longer reachable. */voidAODV::nb_delete(nsaddr_t id){ Neighbor *nb = nbhead.lh_first; rt_entry *rt; log_link_del(id); seqno += 1; // Set of neighbors changed for(; nb; nb = nb->nb_link.le_next) { if(nb->nb_addr == id) { LIST_REMOVE(nb,nb_link); delete nb; break; } } for(rt = rtable.head(); rt; rt = rt->rt_link.le_next) { if(rt->rt_nexthop == id) { rt_down(rt); } }}/* * Purges all timed-out Neighbor Entries - runs every * HELLO_INTERVAL * 1.5 seconds. */voidAODV::nb_purge(){ Neighbor *nb = nbhead.lh_first; Neighbor *nbn; double now = CURRENT_TIME; for(; nb; nb = nbn) { nbn = nb->nb_link.le_next; if(nb->nb_expire <= now) { nb_delete(nb->nb_addr); } }}/* ===================================================================== Broadcast ID Management Functions ===================================================================== */voidAODV::id_insert(nsaddr_t id, u_int32_t bid){ BroadcastID *b = new BroadcastID(id, bid); assert(b); b->expire = CURRENT_TIME + BCAST_ID_SAVE; LIST_INSERT_HEAD(&bihead, b, link);}/* I changed this, SRD */u_int32_tAODV::id_lookup(nsaddr_t id, u_int32_t bid){ BroadcastID *b = bihead.lh_first; // Search the list for a match of source and bid for( ; b; b = b->link.le_next) { if ((b->src == id) && (b->id == bid)) return ID_FOUND; } return ID_NOT_FOUND;}voidAODV::id_purge(){ BroadcastID *b = bihead.lh_first; BroadcastID *bn; double now = CURRENT_TIME; for(; b; b = bn) { bn = b->link.le_next; if(b->expire <= now) { LIST_REMOVE(b,link); delete b; } }}/* ================================================================= */static voidaodv_rt_failed_callback(Packet *p, void *arg){ ((AODV*) arg)->rt_ll_failed(p);}/* * This routine is invoked when the link-layer reports a route failed. */voidAODV::rt_ll_failed(Packet *p){#ifndef AODV_LINK_LAYER_DETECTION drop(p, DROP_RTR_MAC_CALLBACK);#else struct hdr_cmn *ch = HDR_CMN(p); struct hdr_ip *ih = HDR_IP(p); rt_entry *rt; /* * Non-data packets and Broadcast Packets can be dropped. */ if(! DATA_PACKET(ch->ptype()) || // (u_int32_t) ih->dst_ == IP_BROADCAST) { ih->daddr() == (nsaddr_t)IP_BROADCAST) { drop(p, DROP_RTR_MAC_CALLBACK); return; } log_link_broke(p); // if((rt = rtable.rt_lookup(ih->dst_)) == 0) { if((rt = rtable.rt_lookup(ih->daddr())) == 0) { drop(p, DROP_RTR_MAC_CALLBACK); return; } log_link_del(ch->next_hop_); /* if the broken link is closer to the dest than source, attempt a local repair. Otherwise, bring down the route. */#ifdef AODV_LOCAL_REPAIR if (ch->num_forwards() > rt->rt_hops) { local_rt_repair(rt, p); // local repair // Mahesh 09/11/99 // retrieve all the packets in the ifq using this link, // queue the packets for which local repair is done, // drop the rest of the packets and send triggered replies return; } else #endif { /* Increment the sequence no. and bring down the route */ rt->rt_seqno++; rt_down(rt); // if (index == ih->src_) { if (index == ih->saddr()) { // If I am the source, // queue the packet since rt_down tries to send a request // Mahesh 09/11/99 rqueue.enque(p); } else { drop(p,DROP_RTR_NO_ROUTE); } }#endif /* AODV_LINK_LAYER_DETECTION */}voidAODV::local_rt_repair(rt_entry *rt, Packet *p){ /* fprintf(stderr,"%s: Dst - %d\n", __FUNCTION__, rt->rt_dst); */ /* Buffer the packet */ rqueue.enque(p); /* mark the route as under repair */ rt->rt_flags = RTF_IN_REPAIR; /* start a route discovery */ /* Mahesh 09/11/99 Note that the following does not ensure that route request is actually sent. */ sendRequest(rt->rt_dst); /* set up a timer interrupt */ Scheduler::instance().schedule(&lrtimer, p->copy(), rt->rt_req_timeout);}voidAODV::rt_down(rt_entry *rt){ /* * Make sure that you don't "down" a route more than once. */ if(rt->rt_flags == RTF_DOWN) { return; } /* // Mahesh 09/11/99 // This function has changed considerably from the last version. // Need to check whether the next hop for the destination // is changed before bringing down the route, for the time being // we ignore this case. */ rt->rt_flags = RTF_DOWN; rt->rt_expire = 0;#ifndef ERROR_BROADCAST { Neighbor *nb = rt->rt_nblist.lh_first; Neighbor *nbn; for( ; nb; nb = nbn) { nbn = nb->nb_link.le_next; if (nb->nb_expire > CURRENT_TIME) // If this neighbor is still considered active sendTriggeredReply(nb->nb_addr, rt->rt_dst, rt->rt_seqno); LIST_REMOVE(nb, nb_link); delete nb; } }#else // Broadcast an unsolicited route reply to the upstream neighbors sendTriggeredReply(rt->rt_dst, rt->rt_seqno);#endif /* * Now purge the Network Interface queues that * may have packets destined for this broken * neighbor. */ { Packet *p; while((p = ifqueue->filter(rt->rt_nexthop))) { // struct hdr_cmn *ch = HDR_CMN(p); struct hdr_ip *ih = HDR_IP(p); // if (index == ih->src_) { if (index == ih->saddr()) { // If I am the source of the packet, // queue this packet and send route request. rqueue.enque(p); // sendRequest(ih->dst_);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -