📄 guessmanager.cc
字号:
#include <click/config.h>#include <click/confparse.hh>#include <click/error.hh>#include <click/glue.hh>#include <clicknet/ether.h>#include <click/etheraddress.hh>#include <click/ipaddress.hh>#include <elements/wifi/sr/ettstat.hh>#include "recpheader.hh"#include "guessmanager.hh"CLICK_DECLSGuessManager::GuessManager() : Element(1, 0), _enable_guessing(true), _threshold(0.8), _rate(2) //, _scrambleq(0){}GuessManager::~GuessManager(){}intGuessManager::configure(Vector<String> &conf, ErrorHandler *errh){ int res = cp_va_parse(conf, this, errh, cpKeywords, //"SCRAMBLEQUEUE", cpElement, "ScrambleQueue element", &_scrambleq, "RATE", cpUnsigned, "Bitrate", &_rate, cpEnd); click_chatter("guess manager done"); /* if (!_scrambleq) { return errh->error("SCRAMBLEQUEUE not specified"); } if (_scrambleq->cast("ScrambleQueue") == 0) { return errh->error("SCRAMBLEQUEUE element is not a ScrambleQueue"); } */ return res;}void*GuessManager::cast(const char *n){ if (strcmp(n, "GuessManager") == 0) { return (GuessManager *)this; } else { return NULL; }}voidGuessManager::push(int, Packet *p){ click_ether *eth = (click_ether *)p->data(); click_enc_recp *recph = (struct click_enc_recp *)(eth+1); // who is the neighbor who sent this reception report EtherAddress nbr = EtherAddress(eth->ether_shost); for (uint16_t i = 0; i < recph->nentries(); i++) { click_chatter("%s: push() looking for source index %u", id().cc(), i); IPAddress src = IPAddress(recph->get_entry_src(i)); uint16_t ipid = recph->get_entry_id(i); uint8_t bmap = recph->get_entry_bmap(i); StringAccum sa; register_recps(nbr, src, ipid, bmap); } p->kill();}voidGuessManager::register_recps(const EtherAddress& nbr, const IPAddress &src, uint16_t ipid, uint8_t bmap){ uint32_t now; now = Timestamp::now().msec1(); SrcMap *srcmap = _nbrstate.find(nbr); if (srcmap==NULL) { srcmap = new SrcMap; _nbrstate.insert(nbr, srcmap); } IPIDMap *ipidmap = srcmap->find(src); if (ipidmap==NULL) { ipidmap = new IPIDMap; srcmap->insert(src, ipidmap); } ipidmap->insert(ipid, 1.0); StringAccum sa; sa << "reception report received " << nbr << " " << src << " " << ipid << " " << now; click_chatter("%s: %s", id().cc(), sa.c_str()); for (uint32_t i = 1; i <=8; i++) { if (bmap & (0x01 << (i-1))) { StringAccum sa_t; sa_t << "reception report received " << nbr << " " << src << " " << (ipid+i) << " " << now; click_chatter("%s: %s", id().cc(), sa_t.c_str()); ipidmap->insert((ipid+i),1.0); } }}voidGuessManager::register_guess_single(const EtherAddress& nbr, const IPAddress &src, uint16_t ipid){ StringAccum sa_t; SrcMap *srcmap = _nbrstate.find(nbr); if (srcmap==NULL) { srcmap = new SrcMap; _nbrstate.insert(nbr, srcmap); } IPIDMap *ipidmap = srcmap->find(src); if (ipidmap==NULL) { ipidmap = new IPIDMap; srcmap->insert(src, ipidmap); } ipidmap->insert(ipid, (double)1.0); sa_t << " Inserted guess for " << src << " ipid " << ipid << " for nbr " << nbr << " with prob " << ipidmap->find(ipid); click_chatter("%u %s %s", Timestamp::now().msec1(), id().cc(), sa_t.c_str()); sa_t.clear();}voidGuessManager::register_guess(const EtherAddress& nbr, const IPAddress &src, uint16_t ipid){ StringAccum sa_t; SrcMap *srcmap = _nbrstate.find(nbr); if (srcmap==NULL) { srcmap = new SrcMap; _nbrstate.insert(nbr, srcmap); } IPIDMap *ipidmap = srcmap->find(src); if (ipidmap==NULL) { ipidmap = new IPIDMap; srcmap->insert(src, ipidmap); } ipidmap->insert(ipid, (double)1.0); sa_t << " Inserted guess for " << src << " ipid " << ipid << " for nbr " << nbr << " with prob " << ipidmap->find(ipid); click_chatter("%u %s %s", Timestamp::now().msec1(), id().cc(), sa_t.c_str()); sa_t.clear(); if (_enable_guessing) { ProbMap *probmap_t = _srcprobmap.find(nbr); if (probmap_t != NULL) { ProbMap::iterator x = probmap_t->begin(); while (x != probmap_t->end()) { srcmap = _nbrstate.find(x.key()); if (srcmap==NULL) { srcmap = new SrcMap; _nbrstate.insert(x.key(), srcmap); } ipidmap = srcmap->find(src); if (ipidmap==NULL) { ipidmap = new IPIDMap; srcmap->insert(src, ipidmap); } if ((!ipidmap->find(ipid)) || (ipidmap->find(ipid) < x.value())) { ipidmap->insert(ipid, x.value()); sa_t << "Inserted guess for " << src << " ipid " << ipid << " for nbr " << x.key() << " with prob " << ipidmap->find(ipid); click_chatter("%u %s %s", Timestamp::now().msec1(), id().cc(), sa_t.c_str()); sa_t.clear(); } x++; } } } click_chatter("guessing module called");}boolGuessManager::check_presence(PacketState *ps1, PacketState *ps2){ click_chatter("trying to find a packet to code"); StringAccum sa; sa << ps1->_nbr; click_chatter("looking up etheraddress %s", sa.c_str()); SrcMap *srcmap1 = _nbrstate.find(ps1->_nbr); if (srcmap1==NULL) { return false; } IPIDMap *ipidmap1 = srcmap1->find(ps2->_src); if (ipidmap1==NULL) { return false; } if (!ipidmap1->find(ps2->_ipid)) return false; double prob1 = ipidmap1->find(ps2->_ipid); if (prob1 < _threshold) { return false; } SrcMap *srcmap2 = _nbrstate.find(ps2->_nbr); if (srcmap2==NULL) { return false; } IPIDMap *ipidmap2 = srcmap2->find(ps1->_src); if (ipidmap2==NULL) { return false; } if (!ipidmap2->find(ps1->_ipid)) return false; double prob2 = ipidmap2->find(ps1->_ipid); if (prob2 < _threshold) { return false; } return true;}doubleGuessManager::get_prob(PacketState *nbr, PacketState *ovhd){ StringAccum sa_t; sa_t << "Checking prob for " << nbr->_nbr << " src " << ovhd->_src << " ipid " << ovhd->_ipid << "\n"; SrcMap *srcmap1 = _nbrstate.find(nbr->_nbr); if (srcmap1==NULL) { sa_t << "Didnt find the state for the neighbor itself"; click_chatter("%s %s", id().cc(), sa_t.c_str()); return 0.0; } IPIDMap *ipidmap1 = srcmap1->find(ovhd->_src); if (ipidmap1==NULL) { sa_t << "Didnt find the state for the IPaddress"; click_chatter("%s %s", id().cc(), sa_t.c_str()); return 0.0; } if (!ipidmap1->find(ovhd->_ipid)) { IPIDMap::iterator x = ipidmap1->begin(); while (x != ipidmap1->end()) { click_chatter("have ipid %u", x.key()); x++; } sa_t << "Didnt find the state for the IPID"; click_chatter("%u %s %s", Timestamp::now().msec1(), id().cc(), sa_t.c_str()); return 0.0; } sa_t << "Found probability " << ipidmap1->find(ovhd->_ipid); click_chatter("%s %s", id().cc(), sa_t.c_str()); return ipidmap1->find(ovhd->_ipid);} voidGuessManager::update_del_prob(EtherAddress src, EtherAddress dst, Vector<RateSize> rs, Vector<int> fwd, Vector<int> rev, uint32_t seq) { if (!src || !dst) { click_chatter("%{element}::update_link called with %s %s\n", this, src.s().cc(), dst.s().cc()); return; } ProbMap *probmap_src = _srcprobmap.find(src); if (probmap_src == NULL) { probmap_src = new ProbMap; _srcprobmap.insert(src, probmap_src); } ProbMap *probmap_dst = _srcprobmap.find(dst); if (probmap_dst == NULL) { probmap_dst = new ProbMap; _srcprobmap.insert(dst, probmap_dst); } for (int x = 0; x < rs.size(); x++) { if ((rs[x]._rate == _rate) && (rs[x]._size == 1500)) { probmap_src->insert(dst, (float)fwd[x]/100.0); probmap_dst->insert(src, (float)rev[x]/100.0); /* StringAccum sa_t; sa_t << "Inserted probabilities for src " << src << " dst " << dst << " probabilities " << (float)fwd[x]/100.0; click_chatter("%s", sa_t.c_str()); */ } }}StringGuessManager::read_del_prob(Element *e, void *){ StringAccum sa; GuessManager *me = static_cast<GuessManager *>(e); SrcProbMap::iterator i = me->_srcprobmap.begin(); while (i != me->_srcprobmap.end()) { ProbMap::iterator i_t = (i.value())->begin(); while (i_t != (i.value())->end()) { sa << i.key() << " " << i_t.key() << " " << (float)i_t.value() << "\n"; i_t++; } i++; } return sa.take_string();} intGuessManager::set_del_prob(const String &arg, Element *e, void *, ErrorHandler *errh) { Vector<String> args; GuessManager *me = static_cast<GuessManager *>(e); cp_spacevec(arg, args); EtherAddress src; EtherAddress dst; double prob; if (!cp_ethernet_address(args[0], &src)) { return errh->error("Couldn't read arg %d to src ethernet address: %s", 0, args[0].cc()); } if (!cp_ethernet_address(args[1], &dst)) { return errh->error("Couldn't read arg %d to dst ethernet address: %s", 1, args[1].cc()); } if (!cp_double(args[2], &prob)) { return errh->error("Couldn't read arg %d to probability %s", 2, args[2].cc()); } ProbMap *probmap_t = me->_srcprobmap.find(src); if (probmap_t == NULL) { probmap_t = new ProbMap; me->_srcprobmap.insert(src, probmap_t); } probmap_t->insert(dst, prob); return 0; // added by rahul to remove compiler warnings}intGuessManager::guess_write_handler(const String &arg, Element *e, void *, ErrorHandler *errh){ GuessManager *me = static_cast<GuessManager *>(e); Vector<String> args; cp_spacevec(arg, args); IPAddress saddr; unsigned int ipid; EtherAddress nhop; if (!cp_ip_address(args[0], &saddr)) { return errh->error("Couldn't read arg %d to src address: %s", 0, args[0].cc()); } if (!cp_unsigned(args[1], &ipid)) { return errh->error("Couldn't read arg %d to ipid %s", 1, args[1].cc()); } if (!cp_ethernet_address(args[2], &nhop)) { return errh->error("Couldn't read arg %d to nbr ethernet address %s", 2, args[2].cc()); } me->register_guess(nhop, saddr, (uint16_t)ipid); return 0; // added by rahul to remove compiler warnings}intGuessManager::enable_guessing(const String &arg, Element *e, void *, ErrorHandler *errh){ GuessManager *rm = static_cast<GuessManager *>(e); bool x; if (!cp_bool(arg, &x)) return errh->error("`enable_guessing' must be a boolean"); rm->set_enable_guessing(x); if (x) click_chatter("%{element}: guessing enabled", rm); else click_chatter("%{element}: guessing disabled", rm); return 0;}voidGuessManager::add_handlers(){ add_read_handler("apdelprobs", read_del_prob, (void*)0); add_write_handler("delprob", set_del_prob, 0); add_write_handler("guess", guess_write_handler, 0); add_write_handler("enable_guessing", enable_guessing, 0);} #include <click/bighashmap.cc>#if EXPLICIT_TEMPLATE_INSTANCEStemplate class HashMap<uint16_t, uint8_t>;template class HashMap<IPAddress, IPIDMap *>;template class HashMap<EtherAddress, SrcMap *>;#endifCLICK_ENDDECLS EXPORT_ELEMENT(GuessManager)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -