⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classifier-addr-mpls.cc

📁 c++编写应用于ns环境下的关于MPLS网络中的多播路由算法
💻 CC
📖 第 1 页 / 共 4 页
字号:
// Process unlabeled packetint MPLSAddressClassifier::processIP(){	int oIface,oLabel,LIBptr,iIface;	int iLabel = -1;			// at first, look up mft to afirm if it is a packet of multicast, if not ,look up PFT as unicast		if (LSGSGlookup(iIface, iLabel , PI_.srcnode_, PI_.dst_.addr_) == 0){	  if (LIBlookup(-1, iLabel, oIface, oLabel, LIBptr) == 0)	    // Push operation		  return convertL3toL2(oIface,oLabel,LIBptr);  //atteion, if this need to be change to return oIface derectly?	}	// Insert code to manipulate PHB	if (PFTlookup(PI_.dst_.addr_, PI_.phb_, oIface, oLabel, LIBptr) == 0)		// Push operation		return convertL3toL2(oIface,oLabel,LIBptr);	// L3 forwarding	// Traffic-driven, triggered by MPLS switch	if (data_driven_) 		Tcl::instance().evalf("%s ldp-trigger-by-switch %d", 				      name(), PI_.dst_.addr_);	trace("U", size_, iLabel, "L3", -1, -1, -1);               	return MPLS_GOTO_L3;}// Process labeled packet
int MPLSAddressClassifier::processLabelP()
{if (DnNM_ > 1)  printf("i am in processLabelP functionn, and this node is a branch now\n");
	int oIface,oLabel,LIBptr;
	int iLabel = PI_.shimhdr_->label_;

	PI_.shimhdr_ = checkTTL(PI_.shimhdr_);

	if (size_ == 0)
		// TTL check
		return MPLS_GOTO_L3;
if (DnNM_ > 1){  printf("i will do liblookup\n");  printf("iLabel=%d\n",iLabel);}
	// Label swapping operation 
	if (LIBlookup(-1, iLabel, oIface, oLabel, LIBptr) == 0)
		return convertL2toL2(iLabel,oIface,oLabel,LIBptr);	PI_.shimhdr_ = DelAllShimHeader(PI_.shimhdr_);
	trace("L",size_, iLabel,"L3(errorLabel)", -1, -1, -1);
	return ( MPLS_GOTO_L3 );
}

int MPLSAddressClassifier::MPLSclassify(Packet* p)
{if (DnNM_ > 1)  printf("i am in MPLSclassify functionn, and this node is a branch now\n");//printf("the prev PI_.shimhdr_->label_ = %d\n",PI_.shimhdr_->label_);	GetIPInfo(p, PI_.dst_, PI_.phb_, PI_.srcnode_);
	PI_.shimhdr_ = GetShimHeader(p);
//printf("the next PI_.shimhdr_->label_ = %d\n",PI_.shimhdr_->label_);	// XXX Using header size to determine if this is a MPLS-labeled packet
	// is a very bad method. We should have some explicit flag that labels
	// every packet; this flag will only be set on for every MPLS-labeled
	// packet. This can be done by a bitmap field in the common header.
	if (size_ == 0) 
		// Unlabeled packet
		return processIP();

	// Labeled packet 
	int ret = processLabelP();
	if (ret == MPLS_GOTO_L3) {  
		PI_.shimhdr_ = GetShimHeader(p);
		return processIP();
	}
	return ret;   
}
hdr_mpls *MPLSAddressClassifier::checkTTL(hdr_mpls *shimhdr){	shimhdr->ttl_--;	int ttl   = shimhdr->ttl_;	int iLabel= shimhdr->label_;   	if (ttl == 0) {		shimhdr = DelAllShimHeader(shimhdr);		trace("L", size_, iLabel, "L3(TTL=0)", -1, -1, ttl);	}	return shimhdr;}void MPLSAddressClassifier::GetIPInfo(Packet* p, ns_addr_t &dst,				      int &phb, int &srcnode){	hdr_ip* iphdr = hdr_ip::access(p);	dst = iphdr->dst_;	srcnode = iphdr->src_.addr_;	phb = MPLS_DEFAULT_PHB;}hdr_mpls *MPLSAddressClassifier::GetShimHeader(Packet* p){	hdr_mpls *shimhdr = hdr_mpls::access(p);if (DnNM_ > 1) {printf("i am in GetShimHeader\n");printf("before judge, p->top_->label = %d\n",shimhdr->top_->label_);printf("before judge, p->label = %d\n",shimhdr->label_);}	size_ = 0;	if ((shimhdr->label_ == 0) && (shimhdr->bflag_ == 0) && 	    (shimhdr->ttl_ == 0)) {		shimhdr->bflag_ = -1;		shimhdr->label_ = -1;		shimhdr->ttl_ = -1;		shimhdr->top_    = shimhdr;		shimhdr->nexthdr_= shimhdr;	} else {		while (shimhdr->top_ != shimhdr) { printf("before swap top_, p->label = %d\n",shimhdr->top_->label_);printf("before swap, p->label = %d\n",shimhdr->label_);			shimhdr = shimhdr->top_;                                                 //Sflag_ +=4;                        			size_ += 4;		}	} printf("after judge, p->label = %d\n",shimhdr->label_);	return shimhdr;}hdr_mpls *MPLSAddressClassifier::DelAllShimHeader(hdr_mpls *shimhdr){	while (shimhdr->bflag_ != -1)		shimhdr = pop(shimhdr);	return(shimhdr);}hdr_mpls *MPLSAddressClassifier::push(hdr_mpls *shimhdr, int oLabel){	hdr_mpls *newhdr;	newhdr = (hdr_mpls *) malloc( sizeof(hdr_mpls) );	newhdr->label_ = oLabel;	newhdr->bflag_ = 1;	newhdr->ttl_ = ttl_;	newhdr->top_  = newhdr;	newhdr->nexthdr_ = shimhdr;	shimhdr->top_ = newhdr;	size_ += 4;    	return newhdr;}hdr_mpls *MPLSAddressClassifier::pop(hdr_mpls *shimhdr){	shimhdr = shimhdr->nexthdr_;	free(shimhdr->top_);	shimhdr->top_ = shimhdr;	size_ -= 4;	return shimhdr;}void MPLSAddressClassifier::install(int slot, NsObject *target) {	Tcl& tcl = Tcl::instance();	if ((slot >= 0) && (slot < nslot_) && 	    (slot_[slot] != NULL)) {		if (strcmp(slot_[slot]->name(), target->name()) != 0)			tcl.evalf("%s routing-update %d %.15g",				  name(), slot,				  Scheduler::instance().clock());		else			tcl.evalf("%s routing-nochange %d %.15g",				  name(), slot,				  Scheduler::instance().clock());	} else		tcl.evalf("%s routing-new %d %.15g",			  name(), slot,			  Scheduler::instance().clock());	Classifier::install(slot,target);}int MPLSAddressClassifier::command(int argc, const char*const* argv){	Tcl& tcl = Tcl::instance();	if (argc == 2) {		if (strcmp(argv[1], "control-driven?") == 0) {			tcl.resultf("%d", control_driven_);			return (TCL_OK);		} else if (strcmp(argv[1], "data-driven?") == 0) {			tcl.resultf("%d", data_driven_);			return (TCL_OK);		} else if (strcmp(argv[1], "enable-control-driven") == 0) {			// XXX data-driven and control-driven triggers are			// exclusive 			control_driven_ = 1;			data_driven_ = 0;			return (TCL_OK);		} else if (strcmp(argv[1], "enable-data-driven") == 0) {			control_driven_ = 0;			data_driven_ = 1;			return (TCL_OK);		} 		  else if (strcmp(argv[1], "testpause") == 0) {			sleep(2);		} 	} else if (argc == 3) {      		if (strcmp(argv[1], "PFTdump") == 0) {			// <classifier> PFTdump nodeid*/			PFTdump(argv[2]);			return (TCL_OK);		} else if (strcmp(argv[1], "ERBdump") == 0) {			ERBdump(argv[2]);			return (TCL_OK);		} else if (strcmp(argv[1], "LIBdump") == 0) {			LIBdump(argv[2]);			return (TCL_OK);									//*********************************************************for multicast		}	else if (strcmp(argv[1], "LSGdump") == 0) {			LSGdump(argv[2]);			return (TCL_OK);				} 						else if (strcmp(argv[1], "get-fec-for-lspid") == 0) {			// <classifier get-fec-for-lspid LSPid			int LSPid  = atoi(argv[2]);			int LIBptr = -1;			int ERBnb;			ERBnb = ERBlocate(LSPid, -1, LIBptr);			if (ERBnb >= 0)				tcl.resultf("%d", ERB_.Entry_[ERBnb].FEC_);			else   				tcl.result("-1");			return (TCL_OK);		} 	} else if (argc == 4) {		//*****************************LSG command********************		if (strcmp(argv[1], "checkSG") == 0) {
			int Source = atoi(argv[2]);
			int Group = atoi(argv[3]);
			int iLabel , iIface , check ;
			// <classifier> checkSG Source Group */
			
			check = LSGSGlookup(iLabel, iIface, Source, Group);
			tcl.resultf("%d", check);
			return (TCL_OK);
		} else if (strcmp(argv[1], "LIBdelete") == 0) {
			int iLabel = atoi(argv[2]);
			int oIface = atoi(argv[3]);
			int check;
			// <classifier> LIBdelete iLabel oIface*/
			check = LIBdelete(iLabel , oIface);
			tcl.resultf("%d", check);
			return (TCL_OK);
		} else if (strcmp(argv[1], "LSGdelete") == 0) {
			int Source = atoi(argv[2]);
			int Group = atoi(argv[3]);
			int check;
			// <classifier> LSGdelete Source Group*/
			check = LSGdelete(Source , Group);
			tcl.resultf("%d", check);
			return (TCL_OK);
		} else if (strcmp(argv[1], "is-branching") == 0) {
			int Source = atoi(argv[2]);
			int Group = atoi(argv[3]);
			// <classifier> is-branching Source Group */
			int check;
			check = LIBisbranching(Source, Group);
			tcl.resultf("%d", Source);
			return (TCL_OK);
		} else if (strcmp(argv[1], "get-Source-for-iIface-iLabel") == 0) {
			int iIface = atoi(argv[2]);
			int iLabel = atoi(argv[3]);
			int Source;
			int Group ;
			// <classifier> get-Source-for-iIface-iLabel iIface iLabel */
			LSGlabellookup(iIface, iLabel, Source, Group);
			tcl.resultf("%d", Source);
			return (TCL_OK);
		} else if (strcmp(argv[1], "get-Group-for-iIface-iLabel") == 0) {
			int iIface = atoi(argv[2]);
			int iLabel = atoi(argv[3]);
			int Source ;
			int Group ;
			// <classifier> get-Group-for-iIface-iLabel iIface iLabel */
			LSGlabellookup(iIface, iLabel, Source, Group);
			tcl.resultf("%d", Group);
			return (TCL_OK);
		} else if (strcmp(argv[1], "get-iLabel-for-SG") == 0) {
			int Source = atoi(argv[2]);
			int Group = atoi(argv[3]);
			int iLabel, iIface ;
			// <classifier> get-iLabel-for-SG Source Group */
			LSGSGlookup(iIface, iLabel, Source, Group);
			tcl.resultf("%d", iLabel);
			return (TCL_OK);
		} else if (strcmp(argv[1], "get-oLabel-for-SG") == 0) {
			int Source = atoi(argv[2]);
			int Group = atoi(argv[3]);
			int oLabel, oIface, iLabel , iIface , x ;
			// <classifier> get-oLabel-for-SG Source Group */
			LSGSGlookup(iIface, iLabel, Source, Group);
			LIBlookup(iIface, iLabel, oIface, oLabel, x);
			tcl.resultf("%d", oLabel);
			return (TCL_OK);
		} else  if (strcmp(argv[1], "get-oIface-for-SG") == 0) {
			int Source = atoi(argv[2]);
			int Group = atoi(argv[3]);
			int oLabel, oIface, iLabel , iIface , x ;
			// <classifier> get-oIface-for-SG Source Group */
			LSGSGlookup(iIface, iLabel, Source, Group);
			LIBlookup(iIface, iLabel, oIface, oLabel, x);
			tcl.resultf("%d", oIface);
			return (TCL_OK);
		} else if (strcmp(argv[1], "get-iIface-for-SG") == 0) {
			int Source = atoi(argv[2]);
			int Group = atoi(argv[3]);
			int iLabel, iIface ;
			// <classifier> get-iIface-for-SG Source Group */
			LSGSGlookup(iIface, iLabel, Source, Group);
			tcl.resultf("%d", iIface);
			return (TCL_OK);
		}//************************************************************							  else if (strcmp(argv[1], "exist-fec") == 0) {			// <classifier> exist-fec <fec> <phb>			int LIBptr;			int PFTnb = PFTlocate(atoi(argv[2]), atoi(argv[3]),					      LIBptr);         			if (PFTnb > -1)				tcl.result("1");			else				tcl.result("-1");			return (TCL_OK);		}		int PFTnb = -1;		int LIBptr = -1;		int iLabel, oLabel, iIface, oIface;		int fec   = atoi(argv[2]);		int LSPid = atoi(argv[3]);		int PHB   = LSPid;		if (LSPid < 0)     // topology-based LSP			PFTnb = PFTlocate(fec,PHB, LIBptr);		else               // ER-LSP			ERBlocate(LSPid,fec, LIBptr);		if (strcmp(argv[1], "GetInIface") == 0) {			// <classifier> GetInIface <FEC> <LSPid>			if (LIBptr > -1) {				LIBgetIncoming(LIBptr,iIface,iLabel);				tcl.resultf("%d", iIface);			} else				tcl.result("-1");			return (TCL_OK);		} else if (strcmp(argv[1], "GetInLabel") == 0) {			// <classifier> GetInLabel <FEC> <LSPid>			if (LIBptr > -1) {				LIBgetIncoming(LIBptr,iIface,iLabel);				tcl.resultf("%d", iLabel);			} else 				tcl.result("-1");			return (TCL_OK);		} else if (strcmp(argv[1], "GetOutIface") == 0) {			// <classifier> GetOutIface <FEC> <phb/LSPid>			if (LIBptr > -1) {				LIBlookup(LIBptr, oIface, oLabel, LIBptr);				tcl.resultf("%d", oIface);			} else				tcl.result("-1");			return (TCL_OK);		} else if (strcmp(argv[1], "GetOutLabel") == 0) {			// <classifier> GetOutLabel <FEC> <phb/LSPid>			if (LIBptr > -1) {				LIBlookup(LIBptr, oIface, oLabel, LIBptr);				tcl.resultf("%d", oLabel);			} else				tcl.result("-1");			return (TCL_OK);		} else if (strcmp(argv[1], "install") == 0) { 			int slot = atoi(argv[2]);			//if ((slot >= 0) && (slot < nslot_) && 			//  (slot_[slot] != NULL)) {			//	if (strcmp(slot_[slot]->name(),argv[3]) != 0)			//		tcl.evalf("%s routing-update %s %.15g",			//			  name(), argv[2],			//			Scheduler::instance().clock());			//	else			//	      tcl.evalf("%s routing-nochange %s %.15g",			//			name(), argv[2],			//			Scheduler::instance().clock());			//} else			//	tcl.evalf("%s routing-new %s %.15g",			//		  name(), argv[2],			//		  Scheduler::instance().clock());			// Then the control is passed on the the base			// address classifier!			//return AddressClassifier::command(argc, argv);			//not a good idea since it would start an infinite loop incase MPLSAddressClassifier::install is defined; 			//so call Classifier::install explicitly.			NsObject* target = (NsObject*)TclObject::lookup(argv[3]);			//Classifier::install(slot, target);			install(slot,target);			return (TCL_OK);		}	} else if (argc == 5) {      		if (strcmp(argv[1], "ErLspBinding") == 0) {			// <classifier> ErLspBinding <FEC> <PHB> <lspid>			int addr   = atoi(argv[2]);			int PHB    = atoi(argv[3]);			int LSPid  = atoi(argv[4]);			if ( !ErLspBinding(addr, PHB,-1, LSPid) )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -