cls_tcindex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * net/sched/cls_tcindex.c Packet classifier for skb->tc_index
  3. *
  4. * Written 1998,1999 by Werner Almesberger, EPFL ICA
  5. */
  6. #include <linux/module.h>
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/errno.h>
  11. #include <linux/slab.h>
  12. #include <net/act_api.h>
  13. #include <net/netlink.h>
  14. #include <net/pkt_cls.h>
  15. /*
  16. * Passing parameters to the root seems to be done more awkwardly than really
  17. * necessary. At least, u32 doesn't seem to use such dirty hacks. To be
  18. * verified. FIXME.
  19. */
  20. #define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */
  21. #define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */
  22. #define PRIV(tp) ((struct tcindex_data *) (tp)->root)
  23. struct tcindex_filter_result {
  24. struct tcf_exts exts;
  25. struct tcf_result res;
  26. };
  27. struct tcindex_filter {
  28. u16 key;
  29. struct tcindex_filter_result result;
  30. struct tcindex_filter *next;
  31. };
  32. struct tcindex_data {
  33. struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
  34. struct tcindex_filter **h; /* imperfect hash; only used if !perfect;
  35. NULL if unused */
  36. u16 mask; /* AND key with mask */
  37. int shift; /* shift ANDed key to the right */
  38. int hash; /* hash table size; 0 if undefined */
  39. int alloc_hash; /* allocated size */
  40. int fall_through; /* 0: only classify if explicit match */
  41. };
  42. static const struct tcf_ext_map tcindex_ext_map = {
  43. .police = TCA_TCINDEX_POLICE,
  44. .action = TCA_TCINDEX_ACT
  45. };
  46. static inline int
  47. tcindex_filter_is_set(struct tcindex_filter_result *r)
  48. {
  49. return tcf_exts_is_predicative(&r->exts) || r->res.classid;
  50. }
  51. static struct tcindex_filter_result *
  52. tcindex_lookup(struct tcindex_data *p, u16 key)
  53. {
  54. struct tcindex_filter *f;
  55. if (p->perfect)
  56. return tcindex_filter_is_set(p->perfect + key) ?
  57. p->perfect + key : NULL;
  58. else if (p->h) {
  59. for (f = p->h[key % p->hash]; f; f = f->next)
  60. if (f->key == key)
  61. return &f->result;
  62. }
  63. return NULL;
  64. }
  65. static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  66. struct tcf_result *res)
  67. {
  68. struct tcindex_data *p = PRIV(tp);
  69. struct tcindex_filter_result *f;
  70. int key = (skb->tc_index & p->mask) >> p->shift;
  71. pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
  72. skb, tp, res, p);
  73. f = tcindex_lookup(p, key);
  74. if (!f) {
  75. if (!p->fall_through)
  76. return -1;
  77. res->classid = TC_H_MAKE(TC_H_MAJ(tp->q->handle), key);
  78. res->class = 0;
  79. pr_debug("alg 0x%x\n", res->classid);
  80. return 0;
  81. }
  82. *res = f->res;
  83. pr_debug("map 0x%x\n", res->classid);
  84. return tcf_exts_exec(skb, &f->exts, res);
  85. }
  86. static unsigned long tcindex_get(struct tcf_proto *tp, u32 handle)
  87. {
  88. struct tcindex_data *p = PRIV(tp);
  89. struct tcindex_filter_result *r;
  90. pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle);
  91. if (p->perfect && handle >= p->alloc_hash)
  92. return 0;
  93. r = tcindex_lookup(p, handle);
  94. return r && tcindex_filter_is_set(r) ? (unsigned long) r : 0UL;
  95. }
  96. static void tcindex_put(struct tcf_proto *tp, unsigned long f)
  97. {
  98. pr_debug("tcindex_put(tp %p,f 0x%lx)\n", tp, f);
  99. }
  100. static int tcindex_init(struct tcf_proto *tp)
  101. {
  102. struct tcindex_data *p;
  103. pr_debug("tcindex_init(tp %p)\n", tp);
  104. p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL);
  105. if (!p)
  106. return -ENOMEM;
  107. p->mask = 0xffff;
  108. p->hash = DEFAULT_HASH_SIZE;
  109. p->fall_through = 1;
  110. tp->root = p;
  111. return 0;
  112. }
  113. static int
  114. __tcindex_delete(struct tcf_proto *tp, unsigned long arg, int lock)
  115. {
  116. struct tcindex_data *p = PRIV(tp);
  117. struct tcindex_filter_result *r = (struct tcindex_filter_result *) arg;
  118. struct tcindex_filter *f = NULL;
  119. pr_debug("tcindex_delete(tp %p,arg 0x%lx),p %p,f %p\n", tp, arg, p, f);
  120. if (p->perfect) {
  121. if (!r->res.class)
  122. return -ENOENT;
  123. } else {
  124. int i;
  125. struct tcindex_filter **walk = NULL;
  126. for (i = 0; i < p->hash; i++)
  127. for (walk = p->h+i; *walk; walk = &(*walk)->next)
  128. if (&(*walk)->result == r)
  129. goto found;
  130. return -ENOENT;
  131. found:
  132. f = *walk;
  133. if (lock)
  134. tcf_tree_lock(tp);
  135. *walk = f->next;
  136. if (lock)
  137. tcf_tree_unlock(tp);
  138. }
  139. tcf_unbind_filter(tp, &r->res);
  140. tcf_exts_destroy(tp, &r->exts);
  141. kfree(f);
  142. return 0;
  143. }
  144. static int tcindex_delete(struct tcf_proto *tp, unsigned long arg)
  145. {
  146. return __tcindex_delete(tp, arg, 1);
  147. }
  148. static inline int
  149. valid_perfect_hash(struct tcindex_data *p)
  150. {
  151. return p->hash > (p->mask >> p->shift);
  152. }
  153. static const struct nla_policy tcindex_policy[TCA_TCINDEX_MAX + 1] = {
  154. [TCA_TCINDEX_HASH] = { .type = NLA_U32 },
  155. [TCA_TCINDEX_MASK] = { .type = NLA_U16 },
  156. [TCA_TCINDEX_SHIFT] = { .type = NLA_U32 },
  157. [TCA_TCINDEX_FALL_THROUGH] = { .type = NLA_U32 },
  158. [TCA_TCINDEX_CLASSID] = { .type = NLA_U32 },
  159. };
  160. static int
  161. tcindex_set_parms(struct tcf_proto *tp, unsigned long base, u32 handle,
  162. struct tcindex_data *p, struct tcindex_filter_result *r,
  163. struct nlattr **tb, struct nlattr *est)
  164. {
  165. int err, balloc = 0;
  166. struct tcindex_filter_result new_filter_result, *old_r = r;
  167. struct tcindex_filter_result cr;
  168. struct tcindex_data cp;
  169. struct tcindex_filter *f = NULL; /* make gcc behave */
  170. struct tcf_exts e;
  171. err = tcf_exts_validate(tp, tb, est, &e, &tcindex_ext_map);
  172. if (err < 0)
  173. return err;
  174. memcpy(&cp, p, sizeof(cp));
  175. memset(&new_filter_result, 0, sizeof(new_filter_result));
  176. if (old_r)
  177. memcpy(&cr, r, sizeof(cr));
  178. else
  179. memset(&cr, 0, sizeof(cr));
  180. if (tb[TCA_TCINDEX_HASH])
  181. cp.hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
  182. if (tb[TCA_TCINDEX_MASK])
  183. cp.mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
  184. if (tb[TCA_TCINDEX_SHIFT])
  185. cp.shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
  186. err = -EBUSY;
  187. /* Hash already allocated, make sure that we still meet the
  188. * requirements for the allocated hash.
  189. */
  190. if (cp.perfect) {
  191. if (!valid_perfect_hash(&cp) ||
  192. cp.hash > cp.alloc_hash)
  193. goto errout;
  194. } else if (cp.h && cp.hash != cp.alloc_hash)
  195. goto errout;
  196. err = -EINVAL;
  197. if (tb[TCA_TCINDEX_FALL_THROUGH])
  198. cp.fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
  199. if (!cp.hash) {
  200. /* Hash not specified, use perfect hash if the upper limit
  201. * of the hashing index is below the threshold.
  202. */
  203. if ((cp.mask >> cp.shift) < PERFECT_HASH_THRESHOLD)
  204. cp.hash = (cp.mask >> cp.shift) + 1;
  205. else
  206. cp.hash = DEFAULT_HASH_SIZE;
  207. }
  208. if (!cp.perfect && !cp.h)
  209. cp.alloc_hash = cp.hash;
  210. /* Note: this could be as restrictive as if (handle & ~(mask >> shift))
  211. * but then, we'd fail handles that may become valid after some future
  212. * mask change. While this is extremely unlikely to ever matter,
  213. * the check below is safer (and also more backwards-compatible).
  214. */
  215. if (cp.perfect || valid_perfect_hash(&cp))
  216. if (handle >= cp.alloc_hash)
  217. goto errout;
  218. err = -ENOMEM;
  219. if (!cp.perfect && !cp.h) {
  220. if (valid_perfect_hash(&cp)) {
  221. cp.perfect = kcalloc(cp.hash, sizeof(*r), GFP_KERNEL);
  222. if (!cp.perfect)
  223. goto errout;
  224. balloc = 1;
  225. } else {
  226. cp.h = kcalloc(cp.hash, sizeof(f), GFP_KERNEL);
  227. if (!cp.h)
  228. goto errout;
  229. balloc = 2;
  230. }
  231. }
  232. if (cp.perfect)
  233. r = cp.perfect + handle;
  234. else
  235. r = tcindex_lookup(&cp, handle) ? : &new_filter_result;
  236. if (r == &new_filter_result) {
  237. f = kzalloc(sizeof(*f), GFP_KERNEL);
  238. if (!f)
  239. goto errout_alloc;
  240. }
  241. if (tb[TCA_TCINDEX_CLASSID]) {
  242. cr.res.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
  243. tcf_bind_filter(tp, &cr.res, base);
  244. }
  245. tcf_exts_change(tp, &cr.exts, &e);
  246. tcf_tree_lock(tp);
  247. if (old_r && old_r != r)
  248. memset(old_r, 0, sizeof(*old_r));
  249. memcpy(p, &cp, sizeof(cp));
  250. memcpy(r, &cr, sizeof(cr));
  251. if (r == &new_filter_result) {
  252. struct tcindex_filter **fp;
  253. f->key = handle;
  254. f->result = new_filter_result;
  255. f->next = NULL;
  256. for (fp = p->h+(handle % p->hash); *fp; fp = &(*fp)->next)
  257. /* nothing */;
  258. *fp = f;
  259. }
  260. tcf_tree_unlock(tp);
  261. return 0;
  262. errout_alloc:
  263. if (balloc == 1)
  264. kfree(cp.perfect);
  265. else if (balloc == 2)
  266. kfree(cp.h);
  267. errout:
  268. tcf_exts_destroy(tp, &e);
  269. return err;
  270. }
  271. static int
  272. tcindex_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  273. struct nlattr **tca, unsigned long *arg)
  274. {
  275. struct nlattr *opt = tca[TCA_OPTIONS];
  276. struct nlattr *tb[TCA_TCINDEX_MAX + 1];
  277. struct tcindex_data *p = PRIV(tp);
  278. struct tcindex_filter_result *r = (struct tcindex_filter_result *) *arg;
  279. int err;
  280. pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
  281. "p %p,r %p,*arg 0x%lx\n",
  282. tp, handle, tca, arg, opt, p, r, arg ? *arg : 0L);
  283. if (!opt)
  284. return 0;
  285. err = nla_parse_nested(tb, TCA_TCINDEX_MAX, opt, tcindex_policy);
  286. if (err < 0)
  287. return err;
  288. return tcindex_set_parms(tp, base, handle, p, r, tb, tca[TCA_RATE]);
  289. }
  290. static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker)
  291. {
  292. struct tcindex_data *p = PRIV(tp);
  293. struct tcindex_filter *f, *next;
  294. int i;
  295. pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p);
  296. if (p->perfect) {
  297. for (i = 0; i < p->hash; i++) {
  298. if (!p->perfect[i].res.class)
  299. continue;
  300. if (walker->count >= walker->skip) {
  301. if (walker->fn(tp,
  302. (unsigned long) (p->perfect+i), walker)
  303. < 0) {
  304. walker->stop = 1;
  305. return;
  306. }
  307. }
  308. walker->count++;
  309. }
  310. }
  311. if (!p->h)
  312. return;
  313. for (i = 0; i < p->hash; i++) {
  314. for (f = p->h[i]; f; f = next) {
  315. next = f->next;
  316. if (walker->count >= walker->skip) {
  317. if (walker->fn(tp, (unsigned long) &f->result,
  318. walker) < 0) {
  319. walker->stop = 1;
  320. return;
  321. }
  322. }
  323. walker->count++;
  324. }
  325. }
  326. }
  327. static int tcindex_destroy_element(struct tcf_proto *tp,
  328. unsigned long arg, struct tcf_walker *walker)
  329. {
  330. return __tcindex_delete(tp, arg, 0);
  331. }
  332. static void tcindex_destroy(struct tcf_proto *tp)
  333. {
  334. struct tcindex_data *p = PRIV(tp);
  335. struct tcf_walker walker;
  336. pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
  337. walker.count = 0;
  338. walker.skip = 0;
  339. walker.fn = &tcindex_destroy_element;
  340. tcindex_walk(tp, &walker);
  341. kfree(p->perfect);
  342. kfree(p->h);
  343. kfree(p);
  344. tp->root = NULL;
  345. }
  346. static int tcindex_dump(struct tcf_proto *tp, unsigned long fh,
  347. struct sk_buff *skb, struct tcmsg *t)
  348. {
  349. struct tcindex_data *p = PRIV(tp);
  350. struct tcindex_filter_result *r = (struct tcindex_filter_result *) fh;
  351. unsigned char *b = skb_tail_pointer(skb);
  352. struct nlattr *nest;
  353. pr_debug("tcindex_dump(tp %p,fh 0x%lx,skb %p,t %p),p %p,r %p,b %p\n",
  354. tp, fh, skb, t, p, r, b);
  355. pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
  356. nest = nla_nest_start(skb, TCA_OPTIONS);
  357. if (nest == NULL)
  358. goto nla_put_failure;
  359. if (!fh) {
  360. t->tcm_handle = ~0; /* whatever ... */
  361. NLA_PUT_U32(skb, TCA_TCINDEX_HASH, p->hash);
  362. NLA_PUT_U16(skb, TCA_TCINDEX_MASK, p->mask);
  363. NLA_PUT_U32(skb, TCA_TCINDEX_SHIFT, p->shift);
  364. NLA_PUT_U32(skb, TCA_TCINDEX_FALL_THROUGH, p->fall_through);
  365. nla_nest_end(skb, nest);
  366. } else {
  367. if (p->perfect) {
  368. t->tcm_handle = r-p->perfect;
  369. } else {
  370. struct tcindex_filter *f;
  371. int i;
  372. t->tcm_handle = 0;
  373. for (i = 0; !t->tcm_handle && i < p->hash; i++) {
  374. for (f = p->h[i]; !t->tcm_handle && f;
  375. f = f->next) {
  376. if (&f->result == r)
  377. t->tcm_handle = f->key;
  378. }
  379. }
  380. }
  381. pr_debug("handle = %d\n", t->tcm_handle);
  382. if (r->res.class)
  383. NLA_PUT_U32(skb, TCA_TCINDEX_CLASSID, r->res.classid);
  384. if (tcf_exts_dump(skb, &r->exts, &tcindex_ext_map) < 0)
  385. goto nla_put_failure;
  386. nla_nest_end(skb, nest);
  387. if (tcf_exts_dump_stats(skb, &r->exts, &tcindex_ext_map) < 0)
  388. goto nla_put_failure;
  389. }
  390. return skb->len;
  391. nla_put_failure:
  392. nlmsg_trim(skb, b);
  393. return -1;
  394. }
  395. static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
  396. .kind = "tcindex",
  397. .classify = tcindex_classify,
  398. .init = tcindex_init,
  399. .destroy = tcindex_destroy,
  400. .get = tcindex_get,
  401. .put = tcindex_put,
  402. .change = tcindex_change,
  403. .delete = tcindex_delete,
  404. .walk = tcindex_walk,
  405. .dump = tcindex_dump,
  406. .owner = THIS_MODULE,
  407. };
  408. static int __init init_tcindex(void)
  409. {
  410. return register_tcf_proto_ops(&cls_tcindex_ops);
  411. }
  412. static void __exit exit_tcindex(void)
  413. {
  414. unregister_tcf_proto_ops(&cls_tcindex_ops);
  415. }
  416. module_init(init_tcindex)
  417. module_exit(exit_tcindex)
  418. MODULE_LICENSE("GPL");