basic.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * lib/cli/cls/basic.c basic classifier module for CLI lib
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2010-2011 Thomas Graf <tgraf@suug.ch>
  10. */
  11. #include <netlink/cli/utils.h>
  12. #include <netlink/cli/tc.h>
  13. #include <netlink/cli/cls.h>
  14. #include <netlink/route/cls/basic.h>
  15. static void print_usage(void)
  16. {
  17. printf(
  18. "Usage: nl-cls-add [...] basic [OPTIONS]...\n"
  19. "\n"
  20. "OPTIONS\n"
  21. " -h, --help Show this help text.\n"
  22. " -t, --target=ID Target class to send matching packets to\n"
  23. " -e, --ematch=EXPR Ematch expression\n"
  24. "\n"
  25. "EXAMPLE"
  26. " # Create a \"catch-all\" classifier, attached to \"q_root\", classyfing\n"
  27. " # all not yet classified packets to class \"c_default\"\n"
  28. " nl-cls-add --dev=eth0 --parent=q_root basic --target=c_default\n");
  29. }
  30. static void parse_argv(struct rtnl_tc *tc, int argc, char **argv)
  31. {
  32. struct rtnl_cls *cls = (struct rtnl_cls *) tc;
  33. struct rtnl_ematch_tree *tree;
  34. uint32_t target;
  35. int err;
  36. for (;;) {
  37. int c, optidx = 0;
  38. enum {
  39. ARG_TARGET = 257,
  40. ARG_DEFAULT = 258,
  41. };
  42. static struct option long_opts[] = {
  43. { "help", 0, 0, 'h' },
  44. { "target", 1, 0, 't' },
  45. { "ematch", 1, 0, 'e' },
  46. { 0, 0, 0, 0 }
  47. };
  48. c = getopt_long(argc, argv, "ht:e:", long_opts, &optidx);
  49. if (c == -1)
  50. break;
  51. switch (c) {
  52. case 'h':
  53. print_usage();
  54. exit(0);
  55. case 't':
  56. if ((err = rtnl_tc_str2handle(optarg, &target)) < 0)
  57. nl_cli_fatal(err, "Unable to parse target \"%s\":",
  58. optarg, nl_geterror(err));
  59. rtnl_basic_set_target(cls, target);
  60. break;
  61. case 'e':
  62. tree = nl_cli_cls_parse_ematch(cls, optarg);
  63. rtnl_basic_set_ematch(cls, tree);
  64. break;
  65. }
  66. }
  67. }
  68. static struct nl_cli_tc_module basic_module =
  69. {
  70. .tm_name = "basic",
  71. .tm_type = RTNL_TC_TYPE_CLS,
  72. .tm_parse_argv = parse_argv,
  73. };
  74. static void __init basic_init(void)
  75. {
  76. nl_cli_tc_register(&basic_module);
  77. }
  78. static void __exit basic_exit(void)
  79. {
  80. nl_cli_tc_unregister(&basic_module);
  81. }