11 using namespace forest;
13 RouterControl::RouterControl(
Router *rtr1,
int thx,
15 : rtr(rtr1), myThx(thx), inQ(inQ1), outQ(outQ1) {
18 void RouterControl::start(
RouterControl *
self) {
self->run(); }
20 void RouterControl::run() {
23 Packet& p = rtr->ps->getPacket(px);
37 Packet& p = rtr->ps->getPacket(px);
42 case CtlPkt::ADD_IFACE:
addIface(cp,rcp);
break;
43 case CtlPkt::DROP_IFACE:
dropIface(cp,rcp);
break;
44 case CtlPkt::GET_IFACE:
getIface(cp,rcp);
break;
45 case CtlPkt::MOD_IFACE:
modIface(cp,rcp);
break;
46 case CtlPkt::GET_IFACE_SET:
getIfaceSet(cp,rcp);
break;
49 case CtlPkt::ADD_LINK: addLink(cp,rcp);
break;
50 case CtlPkt::DROP_LINK:
dropLink(cp,rcp);
break;
51 case CtlPkt::GET_LINK: getLink(cp,rcp);
break;
52 case CtlPkt::MOD_LINK: modLink(cp,rcp);
break;
53 case CtlPkt::GET_LINK_SET:
getLinkSet(cp,rcp);
break;
56 case CtlPkt::ADD_COMTREE: addComtree(cp,rcp);
break;
57 case CtlPkt::DROP_COMTREE: dropComtree(cp,rcp);
break;
58 case CtlPkt::GET_COMTREE: getComtree(cp,rcp);
break;
59 case CtlPkt::MOD_COMTREE: modComtree(cp,rcp);
break;
62 case CtlPkt::ADD_COMTREE_LINK: addComtreeLink(cp,rcp);
break;
63 case CtlPkt::DROP_COMTREE_LINK: dropComtreeLink(cp,rcp);
break;
64 case CtlPkt::GET_COMTREE_LINK: getComtreeLink(cp,rcp);
break;
65 case CtlPkt::MOD_COMTREE_LINK: modComtreeLink(cp,rcp);
break;
68 case CtlPkt::ADD_ROUTE: addRoute(cp,rcp);
break;
69 case CtlPkt::DROP_ROUTE: dropRoute(cp,rcp);
break;
70 case CtlPkt::GET_ROUTE: getRoute(cp,rcp);
break;
71 case CtlPkt::MOD_ROUTE: modRoute(cp,rcp);
break;
72 case CtlPkt::GET_ROUTE_SET:
getRouteSet(cp,rcp);
break;
75 case CtlPkt::ADD_FILTER:
addFilter(cp,rcp);
break;
76 case CtlPkt::DROP_FILTER: dropFilter(cp,rcp);
break;
77 case CtlPkt::GET_FILTER: getFilter(cp,rcp);
break;
78 case CtlPkt::MOD_FILTER: modFilter(cp,rcp);
break;
79 case CtlPkt::GET_FILTER_SET:
getFilterSet(cp,rcp);
break;
81 case CtlPkt::ENABLE_PACKET_LOG: enablePacketLog(cp,rcp);
break;
84 case CtlPkt::SET_LEAF_RANGE: setLeafRange(cp,rcp);
break;
89 case CtlPkt::ADD_BRANCH:
addBranch(cp);
break
90 case CtlPkt::PRUNE:
prune(cp);
break
91 case CtlPkt::CONFIRM:
confirm(cp);
break
92 case CtlPkt::ABORT: abort(cp);
break
95 cerr <<
"unrecognized control packet type " << cp.
type
97 rcp.errMsg =
"invalid control packet for router";
98 rcp.mode = CtlPkt::NEG_REPLY;
111 Packet& p = rtr->ps->getPacket(px);
127 int iface; ipa_t ip;
RateSpec rates;
129 cp.
fmtError(
"unable to unpack control packet");
return;
140 unique_lock iftLock(rtr->iftMtx);
141 if (rtr->ift->valid(iface)) {
142 cp.
fmtError(
"addIface: requested interface "
143 "conflicts with existing interface");
145 }
else if (!rtr->ift->addEntry(iface, cp.ip1, 0, rs)) {
146 cp.
fmtError(
"addIface: cannot add interface");
148 }
else if (!rtr->setupIface(iface)) {
149 cp.
fmtError(
"addIface: could not setup interface");
163 cp.
fmtError(
"unable to unpack control packet");
return;
165 unique_lock iftLock(rtr->iftMtx);
166 rtr->ift->removeEntry(iface);
177 cp.
fmtError(
"unable to unpack control packet");
return;
179 unique_lock iftLock(rtr->iftMtx);
180 if (rtr->ift->valid(iface)) {
186 rcp.fmtError(
"get iface: invalid interface");
198 cp.
fmtError(
"unable to unpack control packet");
return;
200 unique_lock iftLock(rtr->iftMtx);
201 if (rtr->ift->valid(iface)) {
206 cp.
fmtError(
"mod iface: invalid interface");
221 cp.
fmtError(
"unable to unpack control packet");
return;
223 unique_lock iftLock(rtr->iftMtx);
224 if (iface == 0) iface = rtr->ift->firstIface();
225 else if (!rtr->ift->valid(iface)) {
226 cp.
fmtError(
"get iface set: invalid iface number");
229 count = min(10, count);
231 while (i < count && iface != 0) {
232 s.append(to_string(ifIndex) +
" "
233 + rtr->ift->entry2string(ifIndex));
234 if (s.length() > 1300) {
235 cp.
fmtError(
"getIfaceSet: reply string too long");
238 i++; iface = rtr->ift->nextIface(iface);
243 void RouterControl::addLink(
CtlPkt& cp) {
244 Forest::ntyp_t ntyp;
int iface, lnk; ipa_t peerIp; ipp_t peerPort;
245 fAdr_t peerAdr; uint64_t nonce;
246 if (!cp.
xtrAddLink(ntyp, iface, lnk, peerIp, peerPort, peerAdr, nonce)) {
247 cp.
fmtError(
"unable to unpack control packet");
return;
250 cp.
fmtError(
"add link: adding link to router, but no peer "
256 unique_lock iftLock(rtr->iftMtx,defer_lock);
257 unique_lock ltLock( rtr->ltMtx,defer_lock);
258 lock(iftLock, ltLock);
260 if (rtr->lt->lookup(peerIp, peerPort) != 0 ||
261 (lnk != 0 && rtr->lt->valid(lnk))) {
262 cp.
fmtError(
"add link: new link conflicts with existing link");
273 cp.
fmtError(
"add link: requested link "
274 "exceeds interface capacity");
291 lnk = rtr->lt->addEntry(lnk,peerIp,peerPort,nonce);
293 cp.
fmtError(
"add link: cannot add requested link");
302 if (peerAdr == 0) lte.
peerAdr = rtr->allocLeafAdr();
303 else if (rtr->allocLeafAdr(peerAdr)) lte.
peerAdr = peerAdr;
305 rtr->lt->removeEntry(lnk);
306 cp.
fmtError(
"add link: cannot add link using "
316 sm->clearLnkStats(lnk);
319 pktx px = rtr->ps->alloc();
320 Packet& p = rtr->ps->getPacket(px);
326 int64_t seqNum = rtr->nextSeqNum();
327 (p.payload())[0] = htonl((int32_t) ((seqNum>>32) & 0xffffffff));
328 (p.payload())[1] = htonl((int32_t) (seqNum & 0xffffffff));
329 p.payload()[2] = htonl((uint32_t) (lte.
nonce >> 32));
330 p.payload()[3] = htonl((uint32_t) (lte.
nonce & 0xffffffff));
350 cp.
fmtError(
"unable to unpack control packet");
return;
353 unique_lock iftLock(rtr->iftMtx,defer_lock);
354 unique_lock ltLock( rtr->ltMtx,defer_lock);
355 unique_lock cttLock(rtr->cttMtx,defer_lock);
356 unique_lock rtLock( rtr->rtMtx,defer_lock);
357 lock(iftLock, ltLock, cttLock, rtrLock);
359 if (lnk == 0) lnk = rtr->lt->lookup(peerAdr);
362 Dlist comtList = rtr->ctt->getComtList(lnk);
363 for (
int ctx = comtList.first(); ctx != 0; ctx = comtList.next()) {
364 rtr->lt->purge(rtr->ctt->getComtree(ctx),
365 rtr->ctt->getClnkNum(ctx,lnk));
369 rtr->ctt->purgeLink(lnk);
378 rtr->lt->removeEntry(lnk);
382 void RouterControl::getLink(
CtlPkt& cp) {
385 cp.
fmtError(
"unable to unpack control packet");
return;
387 unique_lock ltLock(rtr->ltMtx);
388 if (rtr->lt->valid(lnk)) {
394 cp.
fmtError(
"get link: invalid link number";
410 xtrGetLinkSet(lnk,count);
412 unique_lock ltLock(rtr->ltMtx);
413 if (lnk == 0) lnk = rtr->lt->firstLink();
414 else if (!rtr->lt->valid(lnk)) {
415 cp.
fmtError(
"get link set: invalid link number";
418 count = min(10,count);
421 while (i < count && lnk != 0) {
422 rcp.stringData.append(to_string(lnk) +
" ");
423 rcp.stringData.append(rtr->lt->link2string(lnk) +
"\n");
424 if (rcp.stringData.length() > 1300) {
425 cp.
fmtError(
"get link set: error while formatting "
429 i++; lnk = rtr->lt->nextLink(lnk);
435 void RouterControl::modLink(
CtlPkt& cp) {
438 cp.
fmtError(
"unable to unpack control packet");
return;
441 unique_lock iftLock(rtr->iftMtx,defer_lock);
442 unique_lock ltLock(rtr->ltMtx,defer_lock);
443 lock(iftLock, ltLock);
445 if (!rtr->lt->valid(lnk)) {
446 cp.
fmtError(
"get link: invalid link number";
456 "exceeds interface capacity";
462 rtr->qm->setLinkRates(lnk,rates);
467 void RouterControl::addComtree(
CtlPkt& cp) {
470 cp.
fmtError(
"unable to unpack control packet");
return;
473 unique_lock cttLock(rtr->cttMtx);
474 if(rtr->ctt->validComtree(comt) || rtr->ctt->addEntry(comt) != 0) {
478 cp.
fmtError(
"add comtree: cannot add comtree";
482 void RouterControl::dropComtree(
CtlPkt& cp) {
485 cp.
fmtError(
"unable to unpack control packet");
return;
488 unique_lock ltLock(rtr->ltMtx,defer_lock);
489 unique_lock cttLock(rtr->cttMtx,defer_lock);
490 unique_lock rtLock(rtr->rtMtx,defer_lock);
491 lock(ltLock, cttLock, rtLock);
493 int ctx = rtr->ctt->getComtIndex(comt);
495 cp.
fmtError(
"dropComtree: no such comtree";
499 int plink = cte.
pLnk;
501 int cLnk = rtr->ctt->firstComtLink(ctx);
505 rtr->lt->purge(comt,cLnk);
508 int lnk = rtr->ctt->getLink(ctx,cLnk);
509 RouteTable::entry& lte = rtr->lt->getEntry(lnk);
513 if (lnk == cte.
pLnk) pRates = lte.availRates;
516 qm->freeQ(rtr->ctt->getLinkQ(cLnk));
518 rtr->ctt->removeLink(ctx,cLnk);
519 cLnk = rtr->ctt->firstComtLink(ctx);
521 rtr->ctt->removeEntry(ctx);
526 void RouterControl::getComtree(
CtlPkt& cp) {
529 cp.
fmtError(
"unable to unpack control packet");
return;
532 unique_lock cttLock(rtr->cttMtx);
533 int ctx = rtr->ctt->getComtIndex(comt);
535 cp.
fmtError(
"get comtree: invalid comtree";
540 rtr->ctt->getLinkCount(ctx));
544 void RouterControl::modComtree(
CtlPkt& cp) {
545 comt_t comt,
int coreFlag,
int plnk;
547 cp.
fmtError(
"unable to unpack control packet");
return;
549 unique_lock cttLock(rtr->cttMtx);
551 int ctx = rtr->ctt->getComtIndex(comt);
557 if (plnk != 0 && !rtr->ctt->isLink(ctx,plnk)) {
559 "not belong to comtree";
562 if (plnk != 0 && !rtr->ctt->isRtrLink(ctx,plnk)) {
564 "not connect to a router";
568 cte.
pClnk = rtr->ctt->getClnkNum(comt,plnk);
573 cp.
fmtError(
"modify comtree: invalid comtree";
590 cp.
fmtError(
"unable to unpack control packet");
return;
593 unique_lock cttLock(rtr->cttMtx);
596 ctx = rtr->ctt->firstComtIndex();
598 ctx = rtr->ctt->getComtIndex(comt);
600 cp.
fmtError(
"get comtree set: invalid comtree number";
603 count = min(10,count);
605 while (i < count && ctx != 0) {
606 rcp.stringData.append(rtr->ctt->entry2string(ctx));
607 if (rcp.stringData.length() > 1300) {
608 cp.
fmtError(
"get comtee set: error while formatting "
612 i++; ctx = rtr->ctt->nextComtIndex(ctx);
618 void RouterControl::addComtreeLink(
CtlPkt& cp) {
619 comt_t comt;
int lnk, coreFlag; ipa_t peerIp; ipp_t peerPort;
621 xtrAddComtreeLink(comt,lnk,coreFlag,peerIp,peerPort,peerAdr);
623 unique_lock ltLock(rtr->ltMtx,defer_lock);
624 unique_lock cttLock(rtr->cttMtx,defer_lock);
625 unique_lock rtLock(rtr->rtMtx,defer_lock);
626 lock(ltLock, cttLock, rtLock);
627 int ctx = rtr->ctt->getComtIndex(comt);
629 cp.
fmtError(
"add comtree link: invalid comtree";
632 if (lnk == 0 && cp.ip1 != 0 && cp.port1 != 0) {
633 lnk = rtr->lt->lookup(cp.ip1, cp.port1);
634 }
else if (lnk == 0 && peerAdr != 0) {
635 lnk = rtr->lt->lookup(peerAdr);
637 if (!rtr->lt->valid(lnk)) {
638 cp.
fmtError(
"add comtree link: invalid link or "
648 cp.
fmtError(
"add comtree link: must specify "
649 "core flag on links to routers";
653 int cLnk = rtr->ctt->getClnkNum(comt,lnk);
655 cp.
fmtError(
"addComtreeLink: specified "
656 "link already in comtree";
660 if (!rtr->ctt->addLink(ctx,lnk,isRtr,coreFlag)) {
661 cp.
fmtError(
"add comtree link: cannot add "
662 "requested comtree link";
665 cLnk = rtr->ctt->getClnkNum(comt,lnk);
672 int rtx = rtr->rt->getRtx(comt,peerAdr);
673 if (rtx == 0) rtr->rt->addEntry(comt,lte.peerAdr,cLnk);
678 int rtx = rtr->rt->getRtx(comt,dest);
679 if (rtx == 0) rtr->rt->addEntry(comt,dest,cLnk);
684 int qid = qm->allocQ(lnk);
686 rtr->ctt->removeLink(ctx,cLnk);
687 cp.
fmtError(
"add comtree link: no queues "
688 "available for link";
696 if (!minRates.leq(lte.availRates)) {
697 cp.
fmtError(
"add comtree link: request "
698 "exceeds link capacity";
701 lte.availRates.subtract(minRates);
702 cli.rates = minRates;
704 qm->setQRates(qid,minRates);
705 if (isRtr) qm->setQLimits(qid,500,1000000);
706 else qm->setQLimits(qid,500,1000000);
707 sm->clearQuStats(qid);
712 void RouterControl::dropComtreeLink(
CtlPkt& cp) {
713 comt_t comt;
int lnk; ipa_t peerIp; ipp_t peerPort;
fAdr_t peerAdr;
714 xtrDropComtreeLink(comt, lnk, peerIp, peerPort, peerAdr);
716 unique_lock ltLock(rtr->ltMtx,defer_lock);
717 unique_lock cttLock(rtr->cttMtx,defer_lock);
718 unique_lock rtLock(rtr->rtMtx,defer_lock);
719 lock(ltLock, cttLock, rtLock);
720 int ctx = rtr->ctt->getComtIndex(comt);
722 cp.
fmtError(
"drop comtree link: invalid comtree";
725 if (lnk == 0 && cp.ip1 != 0 && cp.port1 != 0) {
726 lnk = rtr->lt->lookup(cp.ip1, cp.port1);
727 }
else if (lnk == 0 && peerAdr != 0) {
728 lnk = rtr->lt->lookup(cp.adr1);
730 if (!rtr->lt->valid(lnk)) {
731 cp.
fmtError(
"drop comtree link: invalid link "
732 "or peer IP and port";
736 int cLnk = rtr->ctt->getComtLink(comt,lnk);
739 rtr->lt->purge(comt,cLnk);
748 rtr->ctt->removeLink(ctx,cLnk);
754 void RouterControl::modComtreeLink(
CtlPkt& cp) {
757 cp.
fmtError(
"unable to unpack control packet");
return;
760 unique_lock ltLock(rtr->ltMtx,defer_lock);
761 unique_lock cttLock(rtr->cttMtx,defer_lock);
762 lock(ltLock, cttLock);
763 int ctx = rtr->ctt->getComtIndex(comt);
765 cp.
fmtError(
"modify comtree link: invalid comtree";
768 if (!rtr->lt->valid(lnk)) {
769 cp.
fmtError(
"modify comtree link: invalid link number";
772 int cLnk = rtr->ctt->getComtLink(comt,lnk);
774 cp.
fmtError(
"modify comtree link: specified link "
775 "not defined in specified comtree";
785 cp.
fmtError(
"modify comtree link: new rate spec "
786 "exceeds available link capacity";
795 void RouterControl::getComtreeLink(
CtlPkt& cp) {
798 cp.
fmtError(
"unable to unpack control packet");
return;
801 unique_lock ltLock(rtr->ltMtx,defer_lock);
802 unique_lock cttLock(rtr->cttMtx,defer_lock);
803 lock(ltLock, cttLock);
804 int ctx = rtr->ctt->getComtIndex(comt);
806 cp.
fmtError(
"get comtree link: invalid comtree";
809 if (!rtr->lt->valid(lnk)) {
810 cp.
fmtError(
"get comtree link: invalid link number";
813 int cLnk = rtr->ctt->getClnkNum(comt,lnk);
815 cp.
fmtError(
"getComtreeLink: specified link "
816 "not defined in specified comtree";
824 void RouterControl::addRoute(
CtlPkt& cp) {
827 cp.
fmtError(
"unable to unpack control packet");
return;
830 unique_lock cttLock(rtr->cttMtx,defer_lock);
831 unique_lock rtLock(rtr->rtMtx,defer_lock);
832 lock(cttLock, rtLock);
833 if (!rtr->ctt->validComtree(comt)) {
834 cp.
fmtError(
"comtree not defined at this router\n";
841 int cLnk = rtr->ctt->getComtLink(comt,lnk);
842 int rtx = rtr->rt->getRteIndex(comt,destAdr);
844 cp.
fmtError(
"add route: requested route "
845 "conflicts with existing route";
847 }
else if (rtr->rt->addEntry(comt, destAdr, lnk)) {
851 cp.
fmtError(
"add route: cannot add route";
855 void RouterControl::dropRoute(
CtlPkt& cp) {
858 cp.
fmtError(
"unable to unpack control packet");
return;
861 unique_lock cttLock(rtr->cttMtx,defer_lock);
862 unique_lock rtLock(rtr->rtMtx,defer_lock);
863 lock(cttLock, rtLock);
864 if (!rtr->ctt->validComtree(comt)) {
865 cp.
fmtError(
"comtree not defined at this router\n";
872 rtr->rt->removeEntry(rtr->rt->getRtx(comt,destAdr));
877 void RouterControl::getRoute(
CtlPkt& cp) {
880 cp.
fmtError(
"unable to unpack control packet");
return;
883 unique_lock cttLock(rtr->cttMtx,defer_lock);
884 unique_lock rtLock(rtr->rtMtx,defer_lock);
885 lock(cttLock, rtLock);
886 if (!rtr->ctt->validComtree(comt)) {
887 cp.
fmtError(
"comtree not defined at this router\n";
894 int rtx = rtr->rt->getRtx(comt,destAdr);
897 rtr->ctt->getLink(rtr->rt->firstComtLink(rtx)) : 0);
898 cp.
fmtGetRoute(comt,destAdr,lnk,rtr->rt->getLinkCount(rtx));
901 cp.
fmtError(
"get route: no route for specified address";
905 void RouterControl::modRoute(
CtlPkt& cp) {
908 cp.
fmtError(
"unable to unpack control packet");
return;
911 unique_lock cttLock(rtr->cttMtx,defer_lock);
912 unique_lock rtLock(rtr->rtMtx,defer_lock);
913 lock(cttLock, rtLock);
914 if (!rtr->ctt->validComtree(comt)) {
915 cp.
fmtError(
"comtree not defined at this router\n";
922 int rtx = rtr->rt->getRtx(comt,destAdr);
927 "set link in multicast route";
930 rtr->rt->setLink(rtx,cp.
link);
935 cp.
fmtError(
"modify route: invalid route";
951 xtrGetRouteSet(rtx, count);
953 unique_lock cttLock(rtr->cttMtx,defer_lock);
954 unique_lock rtLock(rtr->rtMtx,defer_lock);
955 lock(cttLock, rtLock);
958 rtx = rtr->rt->firstRtx();
959 }
else if (!rtr->rt->validRtx(rtx)) {
960 cp.
fmtError(
"get route set: invalid route number";
963 count = min(10,count);
965 while (i < count && rtx != 0) {
966 rcp.stringData.append(rtr->rt->entry2string(rtx));
967 if (rcp.stringData.length() > 1300) {
968 cp.
fmtError(
"get route set: error while formatting "
972 i++; rtx = rtr->rt->nextRtx(rIndex);
990 cp.
fmtError(
"unable to unpack control packet");
return;
993 fltx fx = pktLog->addFilter();
995 cp.
fmtError(
"add filter: cannot add filter";
1002 void RouterControl::dropFilter(
CtlPkt& cp) {
1005 cp.
fmtError(
"unable to unpack control packet");
return;
1007 pktLog->dropFilter(fx);
1012 void RouterControl::getFilter(
CtlPkt& cp) {
1015 cp.
fmtError(
"unable to unpack control packet");
return;
1018 if (!pktLog->validFilter(fx)) {
1019 cp.
fmtError(
"get filter: invalid filter index";
1027 void RouterControl::modFilter(
CtlPkt& cp) {
1030 cp.
fmtError(
"unable to unpack control packet");
return;
1032 if (!pktLog->validFilter(fx)) {
1033 cp.
fmtError(
"mod filter: invalid filter index";
1055 cp.
fmtError(
"unable to unpack control packet");
return;
1059 fx = pktLog->firstFilter();
1060 }
else if (!pktLog->validFilter(fx)) {
1061 cp.
fmtError(
"get filter set: invalid filter index";
1064 count = min(10,count);
1067 while (i < count && fx != 0) {
1069 ss << fx <<
" " << f.
toString() <<
"\n";
1071 if (ss.str().length() > 1300) {
1072 cp.
fmtError(
"get filter set: error while "
1076 i++; fx = pktLog->nextFilter(fx);
1090 cp.
fmtError(
"unable to unpack control packet");
return;
1093 int count = pktLog->extract(1300, s);
1158 void RouterControl::abort(
CtlPkt& cp) {