📄 click-fastclassifier.cc
字号:
errh->fatal("could not run '%s'", compile_command.cc()); else if (compile_retval < 0) errh->fatal("could not run '%s': %s", compile_command.cc(), strerror(errno)); else if (compile_retval != 0) errh->fatal("'%s' failed", compile_command.cc()); } // compile userlevel if (compile_user) { String compile_command = click_compile_prog + " --directory=" + tmpdir + " --driver=user --package=" + package_name + ".uo " + cxx_filename; int compile_retval = system(compile_command.cc()); if (compile_retval == 127) errh->fatal("could not run '%s'", compile_command.cc()); else if (compile_retval < 0) errh->fatal("could not run '%s': %s", compile_command.cc(), strerror(errno)); else if (compile_retval != 0) errh->fatal("'%s' failed", compile_command.cc()); } } // add .cc, .hh and .?o files to archive { ArchiveElement ae = init_archive_element(package_name + ".cc", 0600); ae.data = source.take_string(); r->add_archive(ae); ae.name = package_name + ".hh"; ae.data = header.take_string(); r->add_archive(ae); if (compile_kernel) { ae.name = package_name + ".ko"; ae.data = file_string(tmpdir + ae.name, errh); r->add_archive(ae); } if (compile_user) { ae.name = package_name + ".uo"; ae.data = file_string(tmpdir + ae.name, errh); r->add_archive(ae); } } // add elementmap to archive { if (r->archive_index("elementmap-fastclassifier.xml") < 0) r->add_archive(init_archive_element("elementmap-fastclassifier.xml", 0600)); ArchiveElement &ae = r->archive("elementmap-fastclassifier.xml"); ElementMap em(ae.data); ElementTraits t; t.header_file = package_name + ".hh"; t.source_file = package_name + ".cc"; t.processing_code = "h/h"; t.flow_code = "x/x"; for (int i = 0; i < gen_eclass_names.size(); i++) { t.name = gen_eclass_names[i]; t.cxx = gen_cxxclass_names[i]; em.add(t); } ae.data = em.unparse("fastclassifier"); } // add classifier configurations to archive { if (r->archive_index("fastclassifier_info") < 0) r->add_archive(init_archive_element("fastclassifier_info", 0600)); ArchiveElement &ae = r->archive("fastclassifier_info"); StringAccum sa; for (int i = 0; i < gen_eclass_names.size(); i++) { sa << gen_eclass_names[i] << '\t' << cids[all_programs[i].type]->name << '\t' << cp_quote(old_configurations[i]) << '\n'; } ae.data += sa.take_string(); }}static voidreverse_transformation(RouterT *r, ErrorHandler *){ // parse fastclassifier_config if (r->archive_index("fastclassifier_info") < 0) return; ArchiveElement &fc_ae = r->archive("fastclassifier_info"); Vector<String> click_names, old_type_names, configurations; parse_tabbed_lines(fc_ae.data, &click_names, &old_type_names, &configurations, (void *)0); // prepare type_map : type -> configuration # HashMap<ElementClassT *, int> type_map(-1); for (int i = 0; i < click_names.size(); i++) type_map.insert(ElementClassT::base_type(click_names[i]), i); // change configuration for (int i = 0; i < r->nelements(); i++) { ElementT *e = r->element(i); int x = type_map[e->type()]; if (x >= 0) { e->set_configuration(configurations[x]); e->set_type(ElementClassT::base_type(old_type_names[x])); } } // remove requirements { Vector<String> requirements = r->requirements(); for (int i = 0; i < requirements.size(); i++) if (requirements[i].substring(0, 14) == "fastclassifier") r->remove_requirement(requirements[i]); } // remove archive elements for (int i = 0; i < r->narchive(); i++) { ArchiveElement &ae = r->archive(i); if (ae.name.substring(0, 14) == "fastclassifier" || ae.name == "elementmap-fastclassifier.xml") ae.name = String(); }}extern "C" {void add_fast_classifiers_1();void add_fast_classifiers_2();} intmain(int argc, char **argv){ click_static_initialize(); CLICK_DEFAULT_PROVIDES; ErrorHandler *errh = new PrefixErrorHandler(ErrorHandler::default_handler(), "click-fastclassifier: "); // read command line arguments Clp_Parser *clp = Clp_NewParser(argc, argv, sizeof(options) / sizeof(options[0]), options); Clp_SetOptionChar(clp, '+', Clp_ShortNegated); program_name = Clp_ProgramName(clp); const char *router_file = 0; const char *output_file = 0; bool compile_kernel = false; bool compile_user = false; bool combine_classifiers = true; bool do_compile = true; bool source_only = false; bool config_only = false; bool reverse = false; bool quiet = false; bool file_is_expr = false; while (1) { int opt = Clp_Next(clp); switch (opt) { case HELP_OPT: usage(); exit(0); break; case VERSION_OPT: printf("click-fastclassifier (Click) %s\n", CLICK_VERSION); printf("Copyright (c) 1999-2000 Massachusetts Institute of Technology\n\Copyright (c) 2000-2001 Mazu Networks, Inc.\n\Copyright (c) 2001 International Computer Science Institute\n\This is free software; see the source for copying conditions.\n\There is NO warranty, not even for merchantability or fitness for a\n\particular purpose.\n"); exit(0); break; case CLICKPATH_OPT: set_clickpath(clp->arg); break; case ROUTER_OPT: case EXPRESSION_OPT: case Clp_NotOption: if (router_file) { errh->error("router configuration specified twice"); goto bad_option; } router_file = clp->arg; file_is_expr = (opt == EXPRESSION_OPT); break; case OUTPUT_OPT: if (output_file) { errh->error("output file specified twice"); goto bad_option; } output_file = clp->arg; break; case COMBINE_OPT: combine_classifiers = !clp->negated; break; case COMPILE_OPT: do_compile = !clp->negated; break; case REVERSE_OPT: reverse = !clp->negated; break; case SOURCE_OPT: source_only = !clp->negated; break; case CONFIG_OPT: config_only = !clp->negated; break; case KERNEL_OPT: compile_kernel = !clp->negated; break; case USERLEVEL_OPT: compile_user = !clp->negated; break; case QUIET_OPT: quiet = !clp->negated; break; case VERBOSE_OPT: verbose = !clp->negated; break; bad_option: case Clp_BadOption: short_usage(); exit(1); break; case Clp_Done: goto done; } } done: RouterT *r = read_router(router_file, file_is_expr, errh); if (r) r->flatten(errh); if (!r || errh->nerrors() > 0) exit(1); if (source_only || config_only) compile_user = compile_kernel = false; // open output file FILE *outf = stdout; if (output_file && strcmp(output_file, "-") != 0) { outf = fopen(output_file, "w"); if (!outf) errh->fatal("%s: %s", output_file, strerror(errno)); } // handle reverse case if (reverse) { reverse_transformation(r, errh); write_router_file(r, outf, errh); exit(0); } // install classifier handlers add_interesting_handler("program"); add_fast_classifiers_1(); add_fast_classifiers_2(); // find Click binaries runclick_prog = clickpath_find_file("click", "bin", CLICK_BINDIR, errh); click_buildtool_prog = clickpath_find_file("click-buildtool", "bin", CLICK_BINDIR, errh); click_compile_prog = clickpath_find_file("click-compile", "bin", CLICK_BINDIR, errh); if (quiet) click_compile_prog += " -q"; // find Classifiers Vector<ElementT *> classifiers; for (RouterT::iterator x = r->begin_elements(); x; x++) if (cid_name_map[x->type_name()] >= 0) classifiers.push_back(x); // quit early if no Classifiers if (classifiers.size() == 0) { if (source_only) errh->message("no Classifiers in router"); else write_router_file(r, outf, errh); exit(0); } // find name of package String package_name = "fastclassifier"; int uniqueifier = 1; while (1) { if (r->archive_index(package_name + ".cc") < 0) break; uniqueifier++; package_name = "fastclassifier" + String(uniqueifier); } // try combining classifiers if (combine_classifiers) { bool any_combined = false; for (int i = 0; i < classifiers.size(); i++) any_combined |= try_combine_classifiers(r, classifiers[i]); if (any_combined) try_remove_classifiers(r, classifiers); } if (do_compile) compile_classifiers(r, package_name, classifiers, compile_kernel, compile_user, errh); // write output if (source_only) { if (r->archive_index(package_name + ".hh") < 0) { errh->error("no source code generated"); exit(1); } const ArchiveElement &aeh = r->archive(package_name + ".hh"); const ArchiveElement &aec = r->archive(package_name + ".cc"); fwrite(aeh.data.data(), 1, aeh.data.length(), outf); fwrite(aec.data.data(), 1, aec.data.length(), outf); } else if (config_only) { String config = r->configuration_string(); fwrite(config.data(), 1, config.length(), outf); } else write_router_file(r, outf, errh); exit(0);}// generate Vector template instance#include <click/vector.cc>template class Vector<Classifier_Insn>;template class Vector<Classifier_Program>;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -