cls_tcindex.c 12 KB

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