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

📄 classifier-addr-mpls.cc

📁 MPLS中 基于地址分类的 源代码
💻 CC
📖 第 1 页 / 共 3 页
字号:
// -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*-/* * classifier-addr-mpls.cc * Copyright (C) 2000 by the University of Southern California * $Id: classifier-addr-mpls.cc,v 1.7 2005/08/25 18:58:09 johnh Exp $ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License, * version 2, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * * The copyright of this module includes the following * linking-with-specific-other-licenses addition: * * In addition, as a special exception, the copyright holders of * this module give you permission to combine (via static or * dynamic linking) this module with free software programs or * libraries that are released under the GNU LGPL and with code * included in the standard release of ns-2 under the Apache 2.0 * license or under otherwise-compatible licenses with advertising * requirements (or modified versions of such code, with unchanged * license).  You may copy and distribute such a system following the * terms of the GNU GPL for this module and the licenses of the * other code concerned, provided that you include the source code of * that other code when and as the GNU GPL requires distribution of * source code. * * Note that people who make modified versions of this module * are not obligated to grant this special exception for their * modified versions; it is their choice whether to do so.  The GNU * General Public License gives permission to release a modified * version without this exception; this exception also makes it * possible to release a modified version which carries forward this * exception. * *///// Original source contributed by Gaeil Ahn. See below.//// $Header: /cvsroot/nsnam/ns-2/mpls/classifier-addr-mpls.cc,v 1.7 2005/08/25 18:58:09 johnh Exp $// XXX//// - Because MPLS header contains pointers, it cannot be used WITH multicast //   routing which replicates packets// - Currently it works only with flat routing./************************************************************************** * Copyright (c) 2000 by Gaeil Ahn                                   	  * * Everyone is permitted to copy and distribute this software.		  * * Please send mail to fog1@ce.cnu.ac.kr when you modify or distribute    * * this sources.							  * **************************************************************************//************************************************************ *                                                          * *    File: File for packet switching in MPLS node          * *    Author: Gaeil Ahn (fog1@ce.cnu.ac.kr), Dec. 1999      * *                                                          * ************************************************************/#include "packet.h"#include "trace.h"#include "classifier-addr-mpls.h"int hdr_mpls::offset_;static class shimhdreaderClass : public PacketHeaderClass {public:	shimhdreaderClass() : PacketHeaderClass("PacketHeader/MPLS", 						sizeof(hdr_mpls)) {		bind_offset(&hdr_mpls::offset_);	}} class_shimhdreader;static class MPLSAddrClassifierClass : public TclClass {public:	MPLSAddrClassifierClass() : TclClass("Classifier/Addr/MPLS") {}	virtual TclObject* create(int, const char*const*) {		return (new MPLSAddressClassifier());	}	virtual void bind();	virtual int method(int argc, const char*const* argv);} class_mpls_addr_classifier;void MPLSAddrClassifierClass::bind(){	TclClass::bind();	add_method("minimum-lspid");	add_method("dont-care");	add_method("ordered-control?");	add_method("on-demand?");	add_method("enable-ordered-control");	add_method("enable-on-demand");}int MPLSAddrClassifierClass::method(int ac, const char*const* av){	Tcl& tcl = Tcl::instance();	int argc = ac - 2;	const char*const* argv = av + 2;	if (argc == 2) {		if (strcmp(argv[1], "minimum-lspid") == 0) {			tcl.resultf("%d", MPLS_MINIMUM_LSPID);			return (TCL_OK);		} else if (strcmp(argv[1], "dont-care") == 0) {			tcl.resultf("%d", MPLS_DONTCARE);			return (TCL_OK);		} if (strcmp(argv[1], "ordered-control?") == 0) {			tcl.resultf("%d", 				    MPLSAddressClassifier::ordered_control_);			return (TCL_OK);		} else if (strcmp(argv[1], "on-demand?") == 0) {			tcl.resultf("%d", MPLSAddressClassifier::on_demand_);			return (TCL_OK);		} else if (strcmp(argv[1], "enable-ordered-control") == 0) {			MPLSAddressClassifier::ordered_control_ = 1;			return (TCL_OK);		} else if (strcmp(argv[1], "enable-on-demand") == 0) {			MPLSAddressClassifier::on_demand_ = 1;			return (TCL_OK);		}	}	return TclClass::method(ac, av);}int MPLSAddressClassifier::on_demand_ = 0;int MPLSAddressClassifier::ordered_control_ = 0;MPLSAddressClassifier::MPLSAddressClassifier() : 	data_driven_(0), control_driven_(0){	PFT_.NB_ = 0;	ERB_.NB_ = 0;	LIB_.NB_ = 0;	ttl_ = 32;}void MPLSAddressClassifier::delay_bind_init_all(){	delay_bind_init_one("ttl_");	delay_bind_init_one("trace_mpls_");   	delay_bind_init_one("label_");	delay_bind_init_one("enable_reroute_");	delay_bind_init_one("reroute_option_");	delay_bind_init_one("data_driven_");	delay_bind_init_one("control_driven_");	AddressClassifier::delay_bind_init_all();}// Arguments: varName, localName, tracerint MPLSAddressClassifier::delay_bind_dispatch(const char *vn, 					       const char* ln, TclObject *t){	if (delay_bind(vn, ln, "ttl_", &ttl_, t))		return TCL_OK;	if (delay_bind(vn, ln, "trace_mpls_", &trace_mpls_, t)) 		return TCL_OK;	if (delay_bind(vn, ln, "label_", &label_, t)) 		return TCL_OK;	if (delay_bind(vn, ln, "enable_reroute_", &enable_reroute_, t))		return TCL_OK;	if (delay_bind(vn, ln, "reroute_option_", &reroute_option_, t))		return TCL_OK;	if (delay_bind(vn, ln, "data_driven_", &data_driven_, t))		return TCL_OK;	if (delay_bind(vn, ln, "control_driven_", &control_driven_, t))		return TCL_OK;	return AddressClassifier::delay_bind_dispatch(vn, ln, t);}int MPLSAddressClassifier::classify(Packet* p){	int nexthop = MPLSclassify(p);	if ((enable_reroute_ == 1) && (size_ > 0) && 	    (is_link_down(nexthop)))		// Use alternative path if it exist		nexthop = do_reroute(p);	if (nexthop == MPLS_GOTO_L3)		return AddressClassifier::classify(p);	// XXX Do NOT return -1, which lets the classifier to process this 	// packet twice!!	return (nexthop == -1) ? Classifier::ONCE : nexthop;}int MPLSAddressClassifier::is_link_down(int node){	Tcl& tcl = Tcl::instance();   	tcl.evalf("[%s set mpls_mod_] get-link-status %d", name(), node);	return (strcmp(tcl.result(), "down") == 0) ? 1 : 0;}int MPLSAddressClassifier::do_reroute(Packet* p){	int oIface, oLabel, LIBptr;	PI_.shimhdr_ = GetShimHeader(p);	int iLabel = PI_.shimhdr_->label_;	if (aPathLookup(PI_.dst_.addr_, PI_.phb_, oIface, oLabel, LIBptr) == 0)		return convertL2toL2(iLabel, oIface, oLabel, LIBptr);	else {		PI_.shimhdr_ = DelAllShimHeader(PI_.shimhdr_);		trace("L", size_, iLabel, "Drop(linkFail)", -1, -1, -1);		switch (reroute_option_) {		case MPLS_DROPPACKET:			return -1;		case MPLS_L3FORWARDING:			return MPLS_GOTO_L3;		case MPLS_MAKENEWLSP:			Tcl& tcl = Tcl::instance();   			if (!control_driven_)				tcl.evalf("%s ldp-trigger-by-switch %d", 					  name(), PI_.dst_.addr_);			return -1;		}		return -1;	}}int MPLSAddressClassifier::convertL3toL2(int oIface, int oLabel, int LIBptr){	int iLabel= -1;	int ptr;              	while (oLabel >= 0) {		/* penultimate hop */		if (oLabel == 0)  {			/* no operation */			trace("U",size_, iLabel, "Push(penultimate)", 			      oIface, oLabel, ttl_);		} else {			/* push operation in ingerss LSR */			PI_.shimhdr_ = push(PI_.shimhdr_,oLabel);			trace("U",size_, iLabel, "Push(ingress)", 			      oIface, oLabel, ttl_);		}		if (LIBptr >= 0) {			/* stack operation */			iLabel = oLabel;			ptr = LIBptr;			LIBlookup(ptr, oIface, oLabel, LIBptr);		} else			break;	}	if (oLabel < 0) {		if (size_ > 0)			PI_.shimhdr_ = DelAllShimHeader(PI_.shimhdr_);		trace("U",size_, iLabel , "L3(errorLabel)", -1,-1, -1);		return MPLS_GOTO_L3;	}	if (oIface < 0) {  		PI_.shimhdr_ = DelAllShimHeader(PI_.shimhdr_);		trace("U", size_, iLabel, "L3(errorOIF)", -1 , -1, -1);		return MPLS_GOTO_L3;	} else		// Guaranteed returned oIface is >= 0 or MPLS_GOTO_L3		return oIface;}int MPLSAddressClassifier::convertL2toL2(int iLabel, int oIface, 					 int oLabel, int LIBptr){	int  ttl = PI_.shimhdr_->ttl_;	int  ptr;	// push(stack) operation after swap or pop	if (oLabel == 0) {		// in penultimate hop		PI_.shimhdr_ = pop(PI_.shimhdr_);		trace("L", size_, iLabel, "Pop(penultimate)", 		      oIface, oLabel, ttl);	} else if (oLabel > 0) {		// swap operation 		swap(PI_.shimhdr_,oLabel);		trace("L", size_, iLabel, "Swap", oIface, oLabel, ttl);	} else {		// Errored Label		PI_.shimhdr_ = DelAllShimHeader(PI_.shimhdr_);		trace("L", size_, iLabel, "L3(errorLabel)", -1, -1, -1);		return MPLS_GOTO_L3;	}	while (LIBptr >= 0) {		// stack operation		iLabel= oLabel;		ptr = LIBptr;		LIBlookup(ptr, oIface, oLabel, LIBptr);		PI_.shimhdr_ = push(PI_.shimhdr_,oLabel);		trace("L", size_, iLabel, "Push(tunnel)", 		      oIface, oLabel, ttl_);	}   	if (oIface < 0) {  		if (size_ > 0)			PI_.shimhdr_ = DelAllShimHeader(PI_.shimhdr_);		trace("L",size_, iLabel , "L3(errorOIf)", -1 , -1, -1);		return MPLS_GOTO_L3;	}	// Guaranteed returned oIface >= 0	return oIface;}// Process unlabeled packetint MPLSAddressClassifier::processIP(){	int oIface,oLabel,LIBptr;	int iLabel = -1;	// 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 packetint MPLSAddressClassifier::processLabelP(){	int oIface,oLabel,LIBptr;	int iLabel = PI_.shimhdr_->label_;	PI_.shimhdr_ = checkTTL(PI_.shimhdr_);	if (size_ == 0)		// TTL check		return MPLS_GOTO_L3;	// 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){	GetIPInfo(p, PI_.dst_, PI_.phb_, PI_.srcnode_);	PI_.shimhdr_ = GetShimHeader(p);   	// 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){

⌨️ 快捷键说明

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