00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include <tclcl.h>
00054
00055 #include "node.h"
00056 #include "mpls/ldp.h"
00057 #include "mpls/mpls-module.h"
00058 #include "mpls/classifier-addr-mpls.h"
00059
00060 static class MPLSModuleClass : public TclClass {
00061 public:
00062 MPLSModuleClass() : TclClass("RtModule/MPLS") {}
00063 TclObject* create(int, const char*const*) {
00064 return (new MPLSModule);
00065 }
00066 } class_mpls_module;
00067
00068 MPLSModule::~MPLSModule()
00069 {
00070
00071 LDPListElem *e;
00072 for (LDPListElem *p = ldplist_.lh_first; p != NULL; ) {
00073 e = p;
00074 p = p->link.le_next;
00075 delete e;
00076 }
00077 }
00078
00079 void MPLSModule::detach_ldp(LDPAgent *a)
00080 {
00081 for (LDPListElem *p = ldplist_.lh_first; p != NULL;
00082 p = p->link.le_next)
00083 if (p->agt_ == a) {
00084 LIST_REMOVE(p, link);
00085 delete p;
00086 break;
00087 }
00088 }
00089
00090 LDPAgent* MPLSModule::exist_ldp(int nbr)
00091 {
00092 for (LDPListElem *p = ldplist_.lh_first; p != NULL;
00093 p = p->link.le_next)
00094 if (p->agt_->peer() == nbr)
00095 return p->agt_;
00096 return NULL;
00097 }
00098
00099 int MPLSModule::command(int argc, const char*const* argv)
00100 {
00101 Tcl& tcl = Tcl::instance();
00102 if (argc == 2) {
00103 if (strcmp(argv[1], "new-incoming-label") == 0) {
00104 tcl.resultf("%d", ++last_inlabel_);
00105 return (TCL_OK);
00106 } else if (strcmp(argv[1], "new-outgoing-label") == 0) {
00107 tcl.resultf("%d", --last_outlabel_);
00108 return (TCL_OK);
00109 } else if (strcmp(argv[1], "get-ldp-agents") == 0) {
00110 for (LDPListElem *e = ldplist_.lh_first; e != NULL;
00111 e = e->link.le_next)
00112 tcl.resultf("%s %s", tcl.result(),
00113 e->agt_->name());
00114 return (TCL_OK);
00115 } else if (strcmp(argv[1], "trace-ldp") == 0) {
00116 for (LDPListElem *e = ldplist_.lh_first; e != NULL;
00117 e = e->link.le_next)
00118 e->agt_->turn_on_trace();
00119 return (TCL_OK);
00120 } else if (strcmp(argv[1], "trace-msgtbl") == 0) {
00121 printf("%d : message-table\n", node()->nodeid());
00122 for (LDPListElem *e = ldplist_.lh_first; e != NULL;
00123 e = e->link.le_next)
00124 e->agt_->MSGTdump();
00125 return (TCL_OK);
00126 }
00127 } else if (argc == 3) {
00128 if (strcmp(argv[1], "attach-ldp") == 0) {
00129 LDPAgent *a = (LDPAgent*)TclObject::lookup(argv[2]);
00130 if (a == NULL) {
00131 fprintf(stderr,"Wrong object name %s",argv[2]);
00132 return (TCL_ERROR);
00133 }
00134 attach_ldp(a);
00135 return (TCL_OK);
00136 } else if (strcmp(argv[1], "detach-ldp") == 0) {
00137 LDPAgent *a = (LDPAgent*)TclObject::lookup(argv[2]);
00138 if (a == NULL) {
00139 fprintf(stderr,"Wrong object name %s",argv[2]);
00140 return (TCL_ERROR);
00141 }
00142 detach_ldp(a);
00143 return (TCL_OK);
00144 } else if (strcmp(argv[1], "exist-ldp-agent") == 0) {
00145 tcl.resultf("%d",
00146 exist_ldp(atoi(argv[2])) == NULL ? 0 : 1 );
00147 return (TCL_OK);
00148 } else if (strcmp(argv[1], "get-ldp-agent") == 0) {
00149 LDPAgent *a = exist_ldp(atoi(argv[2]));
00150 if (a == NULL)
00151 tcl.result("");
00152 else
00153 tcl.resultf("%s", a->name());
00154 return (TCL_OK);
00155 }
00156 else if (strcmp(argv[1] , "route-notify") == 0) {
00157 Node *node = (Node *)(TclObject::lookup(argv[2]));
00158 if (node == NULL) {
00159 tcl.add_errorf("Invalid node object %s", argv[2]);
00160 return TCL_ERROR;
00161 }
00162 if (node != n_) {
00163 tcl.add_errorf("Node object %s different from n_", argv[2]);
00164 return TCL_ERROR;
00165 }
00166 n_->route_notify(this);
00167 return TCL_OK;
00168 }
00169 if (strcmp(argv[1] , "unreg-route-notify") == 0) {
00170 Node *node = (Node *)(TclObject::lookup(argv[2]));
00171 if (node == NULL) {
00172 tcl.add_errorf("Invalid node object %s", argv[2]);
00173 return TCL_ERROR;
00174 }
00175 if (node != n_) {
00176 tcl.add_errorf("Node object %s different from n_", argv[2]);
00177 return TCL_ERROR;
00178 }
00179 n_->unreg_route_notify(this);
00180 return TCL_OK;
00181 }
00182 else if (strcmp(argv[1], "attach-classifier") == 0) {
00183 classifier_ = (MPLSAddressClassifier*)(TclObject::lookup(argv[2]));
00184 if (classifier_ == NULL) {
00185 tcl.add_errorf("Wrong object name %s",argv[2]);
00186 return TCL_ERROR;
00187 }
00188 return TCL_OK;
00189 }
00190 }
00191 return RoutingModule::command(argc, argv);
00192 }
00193
00194 void MPLSModule::add_route(char *dst, NsObject *target) {
00195 if (classifier_)
00196 ((MPLSAddressClassifier *)classifier_)->do_install(dst, target);
00197 if (next_rtm_ != NULL)
00198 next_rtm_->add_route(dst, target);
00199 }