cls_tcindex.c 12 KB

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