cls_tcindex.c 15 KB

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