| 1 |
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 1997 Regents of the University of California. |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* 3. All advertising materials mentioning features or use of this software |
| 15 |
* must display the following acknowledgement: |
| 16 |
* This product includes software developed by the Computer Systems |
| 17 |
* Engineering Group at Lawrence Berkeley Laboratory. |
| 18 |
* 4. Neither the name of the University nor of the Laboratory may be used |
| 19 |
* to endorse or promote products derived from this software without |
| 20 |
* specific prior written permission. |
| 21 |
* |
| 22 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 23 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 26 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 28 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 31 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 |
* SUCH DAMAGE. |
| 33 |
*/ |
| 34 |
/* Ported from CMU/Monarch's code, nov'98 -Padma.*/ |
| 35 |
|
| 36 |
/* -*- c++ -*- |
| 37 |
priqueue.cc |
| 38 |
|
| 39 |
A simple priority queue with a remove packet function |
| 40 |
$Id: priqueue.cc,v 1.9 2010/05/09 22:28:41 tom_henderson Exp $ |
| 41 |
*/ |
| 42 |
|
| 43 |
#include <object.h> |
| 44 |
#include <queue.h> |
| 45 |
#include <drop-tail.h> |
| 46 |
#include <packet.h> |
| 47 |
#include <cmu-trace.h> |
| 48 |
|
| 49 |
#include "priqueue.h" |
| 50 |
|
| 51 |
typedef int (*PacketFilter)(Packet *, void *); |
| 52 |
|
| 53 |
PriQueue_List PriQueue::prhead = { 0 }; |
| 54 |
|
| 55 |
static class PriQueueClass : public TclClass { |
| 56 |
public: |
| 57 |
PriQueueClass() : TclClass("Queue/DropTail/PriQueue") {} |
| 58 |
TclObject* create(int, const char*const*) { |
| 59 |
return (new PriQueue); |
| 60 |
} |
| 61 |
} class_PriQueue; |
| 62 |
|
| 63 |
|
| 64 |
PriQueue::PriQueue() : DropTail() |
| 65 |
{ |
| 66 |
bind("Prefer_Routing_Protocols", &Prefer_Routing_Protocols); |
| 67 |
LIST_INSERT_HEAD(&prhead, this, link); |
| 68 |
} |
| 69 |
|
| 70 |
int |
| 71 |
PriQueue::command(int argc, const char*const* argv) |
| 72 |
{ |
| 73 |
if (argc == 2 && strcasecmp(argv[1], "reset") == 0) |
| 74 |
{ |
| 75 |
Terminate(); |
| 76 |
//FALL-THROUGH to give parents a chance to reset |
| 77 |
} |
| 78 |
return DropTail::command(argc, argv); |
| 79 |
} |
| 80 |
|
| 81 |
void |
| 82 |
PriQueue::recv(Packet *p, Handler *h) |
| 83 |
{ |
| 84 |
struct hdr_cmn *ch = HDR_CMN(p); |
| 85 |
|
| 86 |
if(Prefer_Routing_Protocols) { |
| 87 |
|
| 88 |
switch(ch->ptype()) { |
| 89 |
case PT_DSR: |
| 90 |
case PT_MESSAGE: |
| 91 |
case PT_TORA: |
| 92 |
case PT_AODV: |
| 93 |
case PT_PROTOLIBMK: |
| 94 |
case PT_AOMDV: |
| 95 |
case PT_MDART: |
| 96 |
recvHighPriority(p, h); |
| 97 |
break; |
| 98 |
|
| 99 |
default: |
| 100 |
Queue::recv(p, h); |
| 101 |
} |
| 102 |
} |
| 103 |
else { |
| 104 |
Queue::recv(p, h); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
void |
| 110 |
PriQueue::recvHighPriority(Packet *p, Handler *) |
| 111 |
// insert packet at front of queue |
| 112 |
{ |
| 113 |
q_->enqueHead(p); |
| 114 |
if (q_->length() >= qlim_) |
| 115 |
{ |
| 116 |
Packet *to_drop = q_->lookup(q_->length()-1); |
| 117 |
q_->remove(to_drop); |
| 118 |
drop(to_drop); |
| 119 |
} |
| 120 |
|
| 121 |
if (!blocked_) { |
| 122 |
/* |
| 123 |
* We're not blocked. Get a packet and send it on. |
| 124 |
* We perform an extra check because the queue |
| 125 |
* might drop the packet even if it was |
| 126 |
* previously empty! (e.g., RED can do this.) |
| 127 |
*/ |
| 128 |
p = deque(); |
| 129 |
if (p != 0) { |
| 130 |
blocked_ = 1; |
| 131 |
target_->recv(p, &qh_); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
void |
| 137 |
PriQueue::filter(PacketFilter filter, void * data) |
| 138 |
// apply filter to each packet in queue, |
| 139 |
// - if filter returns 0 leave packet in queue |
| 140 |
// - if filter returns 1 remove packet from queue |
| 141 |
{ |
| 142 |
int i = 0; |
| 143 |
while (i < q_->length()) |
| 144 |
{ |
| 145 |
Packet *p = q_->lookup(i); |
| 146 |
if (filter(p,data)) |
| 147 |
{ |
| 148 |
q_->remove(p); // decrements q len |
| 149 |
} |
| 150 |
else i++; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
Packet* |
| 155 |
PriQueue::filter(nsaddr_t id) |
| 156 |
{ |
| 157 |
Packet *p = 0; |
| 158 |
Packet *pp = 0; |
| 159 |
struct hdr_cmn *ch; |
| 160 |
|
| 161 |
for(p = q_->head(); p; p = p->next_) { |
| 162 |
ch = HDR_CMN(p); |
| 163 |
if(ch->next_hop() == id) |
| 164 |
break; |
| 165 |
pp = p; |
| 166 |
} |
| 167 |
|
| 168 |
/* |
| 169 |
* Deque Packet |
| 170 |
*/ |
| 171 |
if(p) { |
| 172 |
if(pp == 0) |
| 173 |
q_->remove(p); |
| 174 |
else |
| 175 |
q_->remove(p, pp); |
| 176 |
} |
| 177 |
return p; |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* Called at the end of the simulation to purge the IFQ. |
| 182 |
*/ |
| 183 |
void |
| 184 |
PriQueue::Terminate() |
| 185 |
{ |
| 186 |
Packet *p; |
| 187 |
while((p = deque())) { |
| 188 |
drop(p, DROP_END_OF_SIMULATION); |
| 189 |
//drop(p); |
| 190 |
|
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|