cls_tcindex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 = kmalloc(sizeof(struct tcindex_data),GFP_KERNEL);
  119. if (!p)
  120. return -ENOMEM;
  121. memset(p, 0, sizeof(*p));
  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(u16))
  201. goto errout;
  202. cp.shift = *(u16 *) 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 = kmalloc(cp.hash * sizeof(*r), GFP_KERNEL);
  244. if (!cp.perfect)
  245. goto errout;
  246. memset(cp.perfect, 0, cp.hash * sizeof(*r));
  247. balloc = 1;
  248. } else {
  249. cp.h = kmalloc(cp.hash * sizeof(f), GFP_KERNEL);
  250. if (!cp.h)
  251. goto errout;
  252. memset(cp.h, 0, cp.hash * sizeof(f));
  253. balloc = 2;
  254. }
  255. }
  256. if (cp.perfect)
  257. r = cp.perfect + handle;
  258. else
  259. r = tcindex_lookup(&cp, handle) ? : &new_filter_result;
  260. if (r == &new_filter_result) {
  261. f = kmalloc(sizeof(*f), GFP_KERNEL);
  262. if (!f)
  263. goto errout_alloc;
  264. memset(f, 0, sizeof(*f));
  265. }
  266. if (tb[TCA_TCINDEX_CLASSID-1]) {
  267. cr.res.classid = *(u32 *) RTA_DATA(tb[TCA_TCINDEX_CLASSID-1]);
  268. tcf_bind_filter(tp, &cr.res, base);
  269. }
  270. tcf_exts_change(tp, &cr.exts, &e);
  271. tcf_tree_lock(tp);
  272. if (old_r && old_r != r)
  273. memset(old_r, 0, sizeof(*old_r));
  274. memcpy(p, &cp, sizeof(cp));
  275. memcpy(r, &cr, sizeof(cr));
  276. if (r == &new_filter_result) {
  277. struct tcindex_filter **fp;
  278. f->key = handle;
  279. f->result = new_filter_result;
  280. f->next = NULL;
  281. for (fp = p->h+(handle % p->hash); *fp; fp = &(*fp)->next)
  282. /* nothing */;
  283. *fp = f;
  284. }
  285. tcf_tree_unlock(tp);
  286. return 0;
  287. errout_alloc:
  288. if (balloc == 1)
  289. kfree(cp.perfect);
  290. else if (balloc == 2)
  291. kfree(cp.h);
  292. errout:
  293. tcf_exts_destroy(tp, &e);
  294. return err;
  295. }
  296. static int
  297. tcindex_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  298. struct rtattr **tca, unsigned long *arg)
  299. {
  300. struct rtattr *opt = tca[TCA_OPTIONS-1];
  301. struct rtattr *tb[TCA_TCINDEX_MAX];
  302. struct tcindex_data *p = PRIV(tp);
  303. struct tcindex_filter_result *r = (struct tcindex_filter_result *) *arg;
  304. DPRINTK("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
  305. "p %p,r %p,*arg 0x%lx\n",
  306. tp, handle, tca, arg, opt, p, r, arg ? *arg : 0L);
  307. if (!opt)
  308. return 0;
  309. if (rtattr_parse_nested(tb, TCA_TCINDEX_MAX, opt) < 0)
  310. return -EINVAL;
  311. return tcindex_set_parms(tp, base, handle, p, r, tb, tca[TCA_RATE-1]);
  312. }
  313. static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker)
  314. {
  315. struct tcindex_data *p = PRIV(tp);
  316. struct tcindex_filter *f,*next;
  317. int i;
  318. DPRINTK("tcindex_walk(tp %p,walker %p),p %p\n",tp,walker,p);
  319. if (p->perfect) {
  320. for (i = 0; i < p->hash; i++) {
  321. if (!p->perfect[i].res.class)
  322. continue;
  323. if (walker->count >= walker->skip) {
  324. if (walker->fn(tp,
  325. (unsigned long) (p->perfect+i), walker)
  326. < 0) {
  327. walker->stop = 1;
  328. return;
  329. }
  330. }
  331. walker->count++;
  332. }
  333. }
  334. if (!p->h)
  335. return;
  336. for (i = 0; i < p->hash; i++) {
  337. for (f = p->h[i]; f; f = next) {
  338. next = f->next;
  339. if (walker->count >= walker->skip) {
  340. if (walker->fn(tp,(unsigned long) &f->result,
  341. walker) < 0) {
  342. walker->stop = 1;
  343. return;
  344. }
  345. }
  346. walker->count++;
  347. }
  348. }
  349. }
  350. static int tcindex_destroy_element(struct tcf_proto *tp,
  351. unsigned long arg, struct tcf_walker *walker)
  352. {
  353. return __tcindex_delete(tp, arg, 0);
  354. }
  355. static void tcindex_destroy(struct tcf_proto *tp)
  356. {
  357. struct tcindex_data *p = PRIV(tp);
  358. struct tcf_walker walker;
  359. DPRINTK("tcindex_destroy(tp %p),p %p\n",tp,p);
  360. walker.count = 0;
  361. walker.skip = 0;
  362. walker.fn = &tcindex_destroy_element;
  363. tcindex_walk(tp,&walker);
  364. kfree(p->perfect);
  365. kfree(p->h);
  366. kfree(p);
  367. tp->root = NULL;
  368. }
  369. static int tcindex_dump(struct tcf_proto *tp, unsigned long fh,
  370. struct sk_buff *skb, struct tcmsg *t)
  371. {
  372. struct tcindex_data *p = PRIV(tp);
  373. struct tcindex_filter_result *r = (struct tcindex_filter_result *) fh;
  374. unsigned char *b = skb->tail;
  375. struct rtattr *rta;
  376. DPRINTK("tcindex_dump(tp %p,fh 0x%lx,skb %p,t %p),p %p,r %p,b %p\n",
  377. tp,fh,skb,t,p,r,b);
  378. DPRINTK("p->perfect %p p->h %p\n",p->perfect,p->h);
  379. rta = (struct rtattr *) b;
  380. RTA_PUT(skb,TCA_OPTIONS,0,NULL);
  381. if (!fh) {
  382. t->tcm_handle = ~0; /* whatever ... */
  383. RTA_PUT(skb,TCA_TCINDEX_HASH,sizeof(p->hash),&p->hash);
  384. RTA_PUT(skb,TCA_TCINDEX_MASK,sizeof(p->mask),&p->mask);
  385. RTA_PUT(skb,TCA_TCINDEX_SHIFT,sizeof(p->shift),&p->shift);
  386. RTA_PUT(skb,TCA_TCINDEX_FALL_THROUGH,sizeof(p->fall_through),
  387. &p->fall_through);
  388. rta->rta_len = skb->tail-b;
  389. } else {
  390. if (p->perfect) {
  391. t->tcm_handle = r-p->perfect;
  392. } else {
  393. struct tcindex_filter *f;
  394. int i;
  395. t->tcm_handle = 0;
  396. for (i = 0; !t->tcm_handle && i < p->hash; i++) {
  397. for (f = p->h[i]; !t->tcm_handle && f;
  398. f = f->next) {
  399. if (&f->result == r)
  400. t->tcm_handle = f->key;
  401. }
  402. }
  403. }
  404. DPRINTK("handle = %d\n",t->tcm_handle);
  405. if (r->res.class)
  406. RTA_PUT(skb, TCA_TCINDEX_CLASSID, 4, &r->res.classid);
  407. if (tcf_exts_dump(skb, &r->exts, &tcindex_ext_map) < 0)
  408. goto rtattr_failure;
  409. rta->rta_len = skb->tail-b;
  410. if (tcf_exts_dump_stats(skb, &r->exts, &tcindex_ext_map) < 0)
  411. goto rtattr_failure;
  412. }
  413. return skb->len;
  414. rtattr_failure:
  415. skb_trim(skb, b - skb->data);
  416. return -1;
  417. }
  418. static struct tcf_proto_ops cls_tcindex_ops = {
  419. .next = NULL,
  420. .kind = "tcindex",
  421. .classify = tcindex_classify,
  422. .init = tcindex_init,
  423. .destroy = tcindex_destroy,
  424. .get = tcindex_get,
  425. .put = tcindex_put,
  426. .change = tcindex_change,
  427. .delete = tcindex_delete,
  428. .walk = tcindex_walk,
  429. .dump = tcindex_dump,
  430. .owner = THIS_MODULE,
  431. };
  432. static int __init init_tcindex(void)
  433. {
  434. return register_tcf_proto_ops(&cls_tcindex_ops);
  435. }
  436. static void __exit exit_tcindex(void)
  437. {
  438. unregister_tcf_proto_ops(&cls_tcindex_ops);
  439. }
  440. module_init(init_tcindex)
  441. module_exit(exit_tcindex)
  442. MODULE_LICENSE("GPL");