cgroup.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * lib/route/cls/cgroup.c Control Groups Classifier
  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) 2009-2013 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup cls
  13. * @defgroup cls_cgroup Control Groups Classifier
  14. *
  15. * @{
  16. */
  17. #include <netlink-private/netlink.h>
  18. #include <netlink-private/tc.h>
  19. #include <netlink/netlink.h>
  20. #include <netlink/attr.h>
  21. #include <netlink/utils.h>
  22. #include <netlink-private/route/tc-api.h>
  23. #include <netlink/route/classifier.h>
  24. #include <netlink/route/cls/cgroup.h>
  25. #include <netlink/route/cls/ematch.h>
  26. /** @cond SKIP */
  27. #define CGROUP_ATTR_EMATCH 0x001
  28. /** @endcond */
  29. static struct nla_policy cgroup_policy[TCA_CGROUP_MAX+1] = {
  30. [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
  31. };
  32. static int cgroup_clone(void *dst, void *src)
  33. {
  34. return -NLE_OPNOTSUPP;
  35. }
  36. static void cgroup_free_data(struct rtnl_tc *tc, void *data)
  37. {
  38. struct rtnl_cgroup *c = data;
  39. if (!c)
  40. return;
  41. rtnl_ematch_tree_free(c->cg_ematch);
  42. }
  43. static int cgroup_msg_parser(struct rtnl_tc *tc, void *data)
  44. {
  45. struct nlattr *tb[TCA_CGROUP_MAX + 1];
  46. struct rtnl_cgroup *c = data;
  47. int err;
  48. err = tca_parse(tb, TCA_CGROUP_MAX, tc, cgroup_policy);
  49. if (err < 0)
  50. return err;
  51. if (tb[TCA_CGROUP_EMATCHES]) {
  52. if ((err = rtnl_ematch_parse_attr(tb[TCA_CGROUP_EMATCHES],
  53. &c->cg_ematch)) < 0)
  54. return err;
  55. c->cg_mask |= CGROUP_ATTR_EMATCH;
  56. }
  57. #if 0
  58. TODO:
  59. TCA_CGROUP_ACT,
  60. TCA_CGROUP_POLICE,
  61. #endif
  62. return 0;
  63. }
  64. static void cgroup_dump_line(struct rtnl_tc *tc, void *data,
  65. struct nl_dump_params *p)
  66. {
  67. struct rtnl_cgroup *c = data;
  68. if (!c)
  69. return;
  70. if (c->cg_mask & CGROUP_ATTR_EMATCH)
  71. nl_dump(p, " ematch");
  72. else
  73. nl_dump(p, " match-all");
  74. }
  75. static void cgroup_dump_details(struct rtnl_tc *tc, void *data,
  76. struct nl_dump_params *p)
  77. {
  78. struct rtnl_cgroup *c = data;
  79. if (!c)
  80. return;
  81. if (c->cg_mask & CGROUP_ATTR_EMATCH) {
  82. nl_dump_line(p, " ematch ");
  83. if (c->cg_ematch)
  84. rtnl_ematch_tree_dump(c->cg_ematch, p);
  85. else
  86. nl_dump(p, "<no tree>");
  87. } else
  88. nl_dump(p, "no options");
  89. }
  90. static int cgroup_fill_msg(struct rtnl_tc *tc, void *data,
  91. struct nl_msg *msg)
  92. {
  93. struct rtnl_cgroup *c = data;
  94. if (!c)
  95. BUG();
  96. if (!(tc->ce_mask & TCA_ATTR_HANDLE))
  97. return -NLE_MISSING_ATTR;
  98. if (c->cg_mask & CGROUP_ATTR_EMATCH)
  99. return rtnl_ematch_fill_attr(msg, TCA_CGROUP_EMATCHES,
  100. c->cg_ematch);
  101. return 0;
  102. }
  103. /**
  104. * @name Attribute Modifications
  105. * @{
  106. */
  107. void rtnl_cgroup_set_ematch(struct rtnl_cls *cls, struct rtnl_ematch_tree *tree)
  108. {
  109. struct rtnl_cgroup *c;
  110. if (!(c = rtnl_tc_data(TC_CAST(cls))))
  111. BUG();
  112. if (c->cg_ematch) {
  113. rtnl_ematch_tree_free(c->cg_ematch);
  114. c->cg_mask &= ~CGROUP_ATTR_EMATCH;
  115. }
  116. c->cg_ematch = tree;
  117. if (tree)
  118. c->cg_mask |= CGROUP_ATTR_EMATCH;
  119. }
  120. struct rtnl_ematch_tree *rtnl_cgroup_get_ematch(struct rtnl_cls *cls)
  121. {
  122. struct rtnl_cgroup *c;
  123. if (!(c = rtnl_tc_data(TC_CAST(cls))))
  124. BUG();
  125. return c->cg_ematch;
  126. }
  127. /** @} */
  128. static struct rtnl_tc_ops cgroup_ops = {
  129. .to_kind = "cgroup",
  130. .to_type = RTNL_TC_TYPE_CLS,
  131. .to_size = sizeof(struct rtnl_cgroup),
  132. .to_clone = cgroup_clone,
  133. .to_msg_parser = cgroup_msg_parser,
  134. .to_free_data = cgroup_free_data,
  135. .to_msg_fill = cgroup_fill_msg,
  136. .to_dump = {
  137. [NL_DUMP_LINE] = cgroup_dump_line,
  138. [NL_DUMP_DETAILS] = cgroup_dump_details,
  139. },
  140. };
  141. static void __init cgroup_init(void)
  142. {
  143. rtnl_tc_register(&cgroup_ops);
  144. }
  145. static void __exit cgroup_exit(void)
  146. {
  147. rtnl_tc_unregister(&cgroup_ops);
  148. }
  149. /** @} */