cls_tcindex.c 12 KB

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