cls_tcindex.c 12 KB

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