cls_tcindex.c 12 KB

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