cls_tcindex.c 12 KB

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