cls_tcindex.c 12 KB

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