cls_tcindex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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/config.h>
  7. #include <linux/module.h>
  8. #include <linux/types.h>
  9. #include <linux/kernel.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/errno.h>
  12. #include <linux/netdevice.h>
  13. #include <net/ip.h>
  14. #include <net/act_api.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 = kmalloc(sizeof(struct tcindex_data),GFP_KERNEL);
  120. if (!p)
  121. return -ENOMEM;
  122. memset(p, 0, sizeof(*p));
  123. p->mask = 0xffff;
  124. p->hash = DEFAULT_HASH_SIZE;
  125. p->fall_through = 1;
  126. tp->root = p;
  127. return 0;
  128. }
  129. static int
  130. __tcindex_delete(struct tcf_proto *tp, unsigned long arg, int lock)
  131. {
  132. struct tcindex_data *p = PRIV(tp);
  133. struct tcindex_filter_result *r = (struct tcindex_filter_result *) arg;
  134. struct tcindex_filter *f = NULL;
  135. DPRINTK("tcindex_delete(tp %p,arg 0x%lx),p %p,f %p\n",tp,arg,p,f);
  136. if (p->perfect) {
  137. if (!r->res.class)
  138. return -ENOENT;
  139. } else {
  140. int i;
  141. struct tcindex_filter **walk = NULL;
  142. for (i = 0; i < p->hash; i++)
  143. for (walk = p->h+i; *walk; walk = &(*walk)->next)
  144. if (&(*walk)->result == r)
  145. goto found;
  146. return -ENOENT;
  147. found:
  148. f = *walk;
  149. if (lock)
  150. tcf_tree_lock(tp);
  151. *walk = f->next;
  152. if (lock)
  153. tcf_tree_unlock(tp);
  154. }
  155. tcf_unbind_filter(tp, &r->res);
  156. tcf_exts_destroy(tp, &r->exts);
  157. if (f)
  158. kfree(f);
  159. return 0;
  160. }
  161. static int tcindex_delete(struct tcf_proto *tp, unsigned long arg)
  162. {
  163. return __tcindex_delete(tp, arg, 1);
  164. }
  165. static inline int
  166. valid_perfect_hash(struct tcindex_data *p)
  167. {
  168. return p->hash > (p->mask >> p->shift);
  169. }
  170. static int
  171. tcindex_set_parms(struct tcf_proto *tp, unsigned long base, u32 handle,
  172. struct tcindex_data *p, struct tcindex_filter_result *r,
  173. struct rtattr **tb, struct rtattr *est)
  174. {
  175. int err, balloc = 0;
  176. struct tcindex_filter_result new_filter_result, *old_r = r;
  177. struct tcindex_filter_result cr;
  178. struct tcindex_data cp;
  179. struct tcindex_filter *f = NULL; /* make gcc behave */
  180. struct tcf_exts e;
  181. err = tcf_exts_validate(tp, tb, est, &e, &tcindex_ext_map);
  182. if (err < 0)
  183. return err;
  184. memcpy(&cp, p, sizeof(cp));
  185. memset(&new_filter_result, 0, sizeof(new_filter_result));
  186. if (old_r)
  187. memcpy(&cr, r, sizeof(cr));
  188. else
  189. memset(&cr, 0, sizeof(cr));
  190. err = -EINVAL;
  191. if (tb[TCA_TCINDEX_HASH-1]) {
  192. if (RTA_PAYLOAD(tb[TCA_TCINDEX_HASH-1]) < sizeof(u32))
  193. goto errout;
  194. cp.hash = *(u32 *) RTA_DATA(tb[TCA_TCINDEX_HASH-1]);
  195. }
  196. if (tb[TCA_TCINDEX_MASK-1]) {
  197. if (RTA_PAYLOAD(tb[TCA_TCINDEX_MASK-1]) < sizeof(u16))
  198. goto errout;
  199. cp.mask = *(u16 *) RTA_DATA(tb[TCA_TCINDEX_MASK-1]);
  200. }
  201. if (tb[TCA_TCINDEX_SHIFT-1]) {
  202. if (RTA_PAYLOAD(tb[TCA_TCINDEX_SHIFT-1]) < sizeof(u16))
  203. goto errout;
  204. cp.shift = *(u16 *) RTA_DATA(tb[TCA_TCINDEX_SHIFT-1]);
  205. }
  206. err = -EBUSY;
  207. /* Hash already allocated, make sure that we still meet the
  208. * requirements for the allocated hash.
  209. */
  210. if (cp.perfect) {
  211. if (!valid_perfect_hash(&cp) ||
  212. cp.hash > cp.alloc_hash)
  213. goto errout;
  214. } else if (cp.h && cp.hash != cp.alloc_hash)
  215. goto errout;
  216. err = -EINVAL;
  217. if (tb[TCA_TCINDEX_FALL_THROUGH-1]) {
  218. if (RTA_PAYLOAD(tb[TCA_TCINDEX_FALL_THROUGH-1]) < sizeof(u32))
  219. goto errout;
  220. cp.fall_through =
  221. *(u32 *) RTA_DATA(tb[TCA_TCINDEX_FALL_THROUGH-1]);
  222. }
  223. if (!cp.hash) {
  224. /* Hash not specified, use perfect hash if the upper limit
  225. * of the hashing index is below the threshold.
  226. */
  227. if ((cp.mask >> cp.shift) < PERFECT_HASH_THRESHOLD)
  228. cp.hash = (cp.mask >> cp.shift)+1;
  229. else
  230. cp.hash = DEFAULT_HASH_SIZE;
  231. }
  232. if (!cp.perfect && !cp.h)
  233. cp.alloc_hash = cp.hash;
  234. /* Note: this could be as restrictive as if (handle & ~(mask >> shift))
  235. * but then, we'd fail handles that may become valid after some future
  236. * mask change. While this is extremely unlikely to ever matter,
  237. * the check below is safer (and also more backwards-compatible).
  238. */
  239. if (cp.perfect || valid_perfect_hash(&cp))
  240. if (handle >= cp.alloc_hash)
  241. goto errout;
  242. err = -ENOMEM;
  243. if (!cp.perfect && !cp.h) {
  244. if (valid_perfect_hash(&cp)) {
  245. cp.perfect = kmalloc(cp.hash * sizeof(*r), GFP_KERNEL);
  246. if (!cp.perfect)
  247. goto errout;
  248. memset(cp.perfect, 0, cp.hash * sizeof(*r));
  249. balloc = 1;
  250. } else {
  251. cp.h = kmalloc(cp.hash * sizeof(f), GFP_KERNEL);
  252. if (!cp.h)
  253. goto errout;
  254. memset(cp.h, 0, cp.hash * sizeof(f));
  255. balloc = 2;
  256. }
  257. }
  258. if (cp.perfect)
  259. r = cp.perfect + handle;
  260. else
  261. r = tcindex_lookup(&cp, handle) ? : &new_filter_result;
  262. if (r == &new_filter_result) {
  263. f = kmalloc(sizeof(*f), GFP_KERNEL);
  264. if (!f)
  265. goto errout_alloc;
  266. memset(f, 0, sizeof(*f));
  267. }
  268. if (tb[TCA_TCINDEX_CLASSID-1]) {
  269. cr.res.classid = *(u32 *) RTA_DATA(tb[TCA_TCINDEX_CLASSID-1]);
  270. tcf_bind_filter(tp, &cr.res, base);
  271. }
  272. tcf_exts_change(tp, &cr.exts, &e);
  273. tcf_tree_lock(tp);
  274. if (old_r && old_r != r)
  275. memset(old_r, 0, sizeof(*old_r));
  276. memcpy(p, &cp, sizeof(cp));
  277. memcpy(r, &cr, sizeof(cr));
  278. if (r == &new_filter_result) {
  279. struct tcindex_filter **fp;
  280. f->key = handle;
  281. f->result = new_filter_result;
  282. f->next = NULL;
  283. for (fp = p->h+(handle % p->hash); *fp; fp = &(*fp)->next)
  284. /* nothing */;
  285. *fp = f;
  286. }
  287. tcf_tree_unlock(tp);
  288. return 0;
  289. errout_alloc:
  290. if (balloc == 1)
  291. kfree(cp.perfect);
  292. else if (balloc == 2)
  293. kfree(cp.h);
  294. errout:
  295. tcf_exts_destroy(tp, &e);
  296. return err;
  297. }
  298. static int
  299. tcindex_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  300. struct rtattr **tca, unsigned long *arg)
  301. {
  302. struct rtattr *opt = tca[TCA_OPTIONS-1];
  303. struct rtattr *tb[TCA_TCINDEX_MAX];
  304. struct tcindex_data *p = PRIV(tp);
  305. struct tcindex_filter_result *r = (struct tcindex_filter_result *) *arg;
  306. DPRINTK("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
  307. "p %p,r %p,*arg 0x%lx\n",
  308. tp, handle, tca, arg, opt, p, r, arg ? *arg : 0L);
  309. if (!opt)
  310. return 0;
  311. if (rtattr_parse_nested(tb, TCA_TCINDEX_MAX, opt) < 0)
  312. return -EINVAL;
  313. return tcindex_set_parms(tp, base, handle, p, r, tb, tca[TCA_RATE-1]);
  314. }
  315. static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker)
  316. {
  317. struct tcindex_data *p = PRIV(tp);
  318. struct tcindex_filter *f,*next;
  319. int i;
  320. DPRINTK("tcindex_walk(tp %p,walker %p),p %p\n",tp,walker,p);
  321. if (p->perfect) {
  322. for (i = 0; i < p->hash; i++) {
  323. if (!p->perfect[i].res.class)
  324. continue;
  325. if (walker->count >= walker->skip) {
  326. if (walker->fn(tp,
  327. (unsigned long) (p->perfect+i), walker)
  328. < 0) {
  329. walker->stop = 1;
  330. return;
  331. }
  332. }
  333. walker->count++;
  334. }
  335. }
  336. if (!p->h)
  337. return;
  338. for (i = 0; i < p->hash; i++) {
  339. for (f = p->h[i]; f; f = next) {
  340. next = f->next;
  341. if (walker->count >= walker->skip) {
  342. if (walker->fn(tp,(unsigned long) &f->result,
  343. walker) < 0) {
  344. walker->stop = 1;
  345. return;
  346. }
  347. }
  348. walker->count++;
  349. }
  350. }
  351. }
  352. static int tcindex_destroy_element(struct tcf_proto *tp,
  353. unsigned long arg, struct tcf_walker *walker)
  354. {
  355. return __tcindex_delete(tp, arg, 0);
  356. }
  357. static void tcindex_destroy(struct tcf_proto *tp)
  358. {
  359. struct tcindex_data *p = PRIV(tp);
  360. struct tcf_walker walker;
  361. DPRINTK("tcindex_destroy(tp %p),p %p\n",tp,p);
  362. walker.count = 0;
  363. walker.skip = 0;
  364. walker.fn = &tcindex_destroy_element;
  365. tcindex_walk(tp,&walker);
  366. if (p->perfect)
  367. kfree(p->perfect);
  368. if (p->h)
  369. kfree(p->h);
  370. kfree(p);
  371. tp->root = NULL;
  372. }
  373. static int tcindex_dump(struct tcf_proto *tp, unsigned long fh,
  374. struct sk_buff *skb, struct tcmsg *t)
  375. {
  376. struct tcindex_data *p = PRIV(tp);
  377. struct tcindex_filter_result *r = (struct tcindex_filter_result *) fh;
  378. unsigned char *b = skb->tail;
  379. struct rtattr *rta;
  380. DPRINTK("tcindex_dump(tp %p,fh 0x%lx,skb %p,t %p),p %p,r %p,b %p\n",
  381. tp,fh,skb,t,p,r,b);
  382. DPRINTK("p->perfect %p p->h %p\n",p->perfect,p->h);
  383. rta = (struct rtattr *) b;
  384. RTA_PUT(skb,TCA_OPTIONS,0,NULL);
  385. if (!fh) {
  386. t->tcm_handle = ~0; /* whatever ... */
  387. RTA_PUT(skb,TCA_TCINDEX_HASH,sizeof(p->hash),&p->hash);
  388. RTA_PUT(skb,TCA_TCINDEX_MASK,sizeof(p->mask),&p->mask);
  389. RTA_PUT(skb,TCA_TCINDEX_SHIFT,sizeof(p->shift),&p->shift);
  390. RTA_PUT(skb,TCA_TCINDEX_FALL_THROUGH,sizeof(p->fall_through),
  391. &p->fall_through);
  392. rta->rta_len = skb->tail-b;
  393. } else {
  394. if (p->perfect) {
  395. t->tcm_handle = r-p->perfect;
  396. } else {
  397. struct tcindex_filter *f;
  398. int i;
  399. t->tcm_handle = 0;
  400. for (i = 0; !t->tcm_handle && i < p->hash; i++) {
  401. for (f = p->h[i]; !t->tcm_handle && f;
  402. f = f->next) {
  403. if (&f->result == r)
  404. t->tcm_handle = f->key;
  405. }
  406. }
  407. }
  408. DPRINTK("handle = %d\n",t->tcm_handle);
  409. if (r->res.class)
  410. RTA_PUT(skb, TCA_TCINDEX_CLASSID, 4, &r->res.classid);
  411. if (tcf_exts_dump(skb, &r->exts, &tcindex_ext_map) < 0)
  412. goto rtattr_failure;
  413. rta->rta_len = skb->tail-b;
  414. if (tcf_exts_dump_stats(skb, &r->exts, &tcindex_ext_map) < 0)
  415. goto rtattr_failure;
  416. }
  417. return skb->len;
  418. rtattr_failure:
  419. skb_trim(skb, b - skb->data);
  420. return -1;
  421. }
  422. static struct tcf_proto_ops cls_tcindex_ops = {
  423. .next = NULL,
  424. .kind = "tcindex",
  425. .classify = tcindex_classify,
  426. .init = tcindex_init,
  427. .destroy = tcindex_destroy,
  428. .get = tcindex_get,
  429. .put = tcindex_put,
  430. .change = tcindex_change,
  431. .delete = tcindex_delete,
  432. .walk = tcindex_walk,
  433. .dump = tcindex_dump,
  434. .owner = THIS_MODULE,
  435. };
  436. static int __init init_tcindex(void)
  437. {
  438. return register_tcf_proto_ops(&cls_tcindex_ops);
  439. }
  440. static void __exit exit_tcindex(void)
  441. {
  442. unregister_tcf_proto_ops(&cls_tcindex_ops);
  443. }
  444. module_init(init_tcindex)
  445. module_exit(exit_tcindex)
  446. MODULE_LICENSE("GPL");