cls_tcindex.c 14 KB

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