cls_u32.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * net/sched/cls_u32.c Ugly (or Universal) 32bit key Packet Classifier.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. *
  11. * The filters are packed to hash tables of key nodes
  12. * with a set of 32bit key/mask pairs at every node.
  13. * Nodes reference next level hash tables etc.
  14. *
  15. * This scheme is the best universal classifier I managed to
  16. * invent; it is not super-fast, but it is not slow (provided you
  17. * program it correctly), and general enough. And its relative
  18. * speed grows as the number of rules becomes larger.
  19. *
  20. * It seems that it represents the best middle point between
  21. * speed and manageability both by human and by machine.
  22. *
  23. * It is especially useful for link sharing combined with QoS;
  24. * pure RSVP doesn't need such a general approach and can use
  25. * much simpler (and faster) schemes, sort of cls_rsvp.c.
  26. *
  27. * JHS: We should remove the CONFIG_NET_CLS_IND from here
  28. * eventually when the meta match extension is made available
  29. *
  30. * nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro>
  31. */
  32. #include <asm/uaccess.h>
  33. #include <asm/system.h>
  34. #include <linux/bitops.h>
  35. #include <linux/config.h>
  36. #include <linux/module.h>
  37. #include <linux/types.h>
  38. #include <linux/kernel.h>
  39. #include <linux/sched.h>
  40. #include <linux/string.h>
  41. #include <linux/mm.h>
  42. #include <linux/socket.h>
  43. #include <linux/sockios.h>
  44. #include <linux/in.h>
  45. #include <linux/errno.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/if_ether.h>
  48. #include <linux/inet.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/etherdevice.h>
  51. #include <linux/notifier.h>
  52. #include <linux/rtnetlink.h>
  53. #include <net/ip.h>
  54. #include <net/route.h>
  55. #include <linux/skbuff.h>
  56. #include <net/sock.h>
  57. #include <net/act_api.h>
  58. #include <net/pkt_cls.h>
  59. struct tc_u_knode
  60. {
  61. struct tc_u_knode *next;
  62. u32 handle;
  63. struct tc_u_hnode *ht_up;
  64. struct tcf_exts exts;
  65. #ifdef CONFIG_NET_CLS_IND
  66. char indev[IFNAMSIZ];
  67. #endif
  68. u8 fshift;
  69. struct tcf_result res;
  70. struct tc_u_hnode *ht_down;
  71. #ifdef CONFIG_CLS_U32_PERF
  72. struct tc_u32_pcnt *pf;
  73. #endif
  74. #ifdef CONFIG_CLS_U32_MARK
  75. struct tc_u32_mark mark;
  76. #endif
  77. struct tc_u32_sel sel;
  78. };
  79. struct tc_u_hnode
  80. {
  81. struct tc_u_hnode *next;
  82. u32 handle;
  83. u32 prio;
  84. struct tc_u_common *tp_c;
  85. int refcnt;
  86. unsigned divisor;
  87. struct tc_u_knode *ht[1];
  88. };
  89. struct tc_u_common
  90. {
  91. struct tc_u_common *next;
  92. struct tc_u_hnode *hlist;
  93. struct Qdisc *q;
  94. int refcnt;
  95. u32 hgenerator;
  96. };
  97. static struct tcf_ext_map u32_ext_map = {
  98. .action = TCA_U32_ACT,
  99. .police = TCA_U32_POLICE
  100. };
  101. static struct tc_u_common *u32_list;
  102. static __inline__ unsigned u32_hash_fold(u32 key, struct tc_u32_sel *sel, u8 fshift)
  103. {
  104. unsigned h = (key & sel->hmask)>>fshift;
  105. return h;
  106. }
  107. static int u32_classify(struct sk_buff *skb, struct tcf_proto *tp, struct tcf_result *res)
  108. {
  109. struct {
  110. struct tc_u_knode *knode;
  111. u8 *ptr;
  112. } stack[TC_U32_MAXDEPTH];
  113. struct tc_u_hnode *ht = (struct tc_u_hnode*)tp->root;
  114. u8 *ptr = skb->nh.raw;
  115. struct tc_u_knode *n;
  116. int sdepth = 0;
  117. int off2 = 0;
  118. int sel = 0;
  119. #ifdef CONFIG_CLS_U32_PERF
  120. int j;
  121. #endif
  122. int i, r;
  123. next_ht:
  124. n = ht->ht[sel];
  125. next_knode:
  126. if (n) {
  127. struct tc_u32_key *key = n->sel.keys;
  128. #ifdef CONFIG_CLS_U32_PERF
  129. n->pf->rcnt +=1;
  130. j = 0;
  131. #endif
  132. #ifdef CONFIG_CLS_U32_MARK
  133. if ((skb->nfmark & n->mark.mask) != n->mark.val) {
  134. n = n->next;
  135. goto next_knode;
  136. } else {
  137. n->mark.success++;
  138. }
  139. #endif
  140. for (i = n->sel.nkeys; i>0; i--, key++) {
  141. if ((*(u32*)(ptr+key->off+(off2&key->offmask))^key->val)&key->mask) {
  142. n = n->next;
  143. goto next_knode;
  144. }
  145. #ifdef CONFIG_CLS_U32_PERF
  146. n->pf->kcnts[j] +=1;
  147. j++;
  148. #endif
  149. }
  150. if (n->ht_down == NULL) {
  151. check_terminal:
  152. if (n->sel.flags&TC_U32_TERMINAL) {
  153. *res = n->res;
  154. #ifdef CONFIG_NET_CLS_IND
  155. if (!tcf_match_indev(skb, n->indev)) {
  156. n = n->next;
  157. goto next_knode;
  158. }
  159. #endif
  160. #ifdef CONFIG_CLS_U32_PERF
  161. n->pf->rhit +=1;
  162. #endif
  163. r = tcf_exts_exec(skb, &n->exts, res);
  164. if (r < 0) {
  165. n = n->next;
  166. goto next_knode;
  167. }
  168. return r;
  169. }
  170. n = n->next;
  171. goto next_knode;
  172. }
  173. /* PUSH */
  174. if (sdepth >= TC_U32_MAXDEPTH)
  175. goto deadloop;
  176. stack[sdepth].knode = n;
  177. stack[sdepth].ptr = ptr;
  178. sdepth++;
  179. ht = n->ht_down;
  180. sel = 0;
  181. if (ht->divisor)
  182. sel = ht->divisor&u32_hash_fold(*(u32*)(ptr+n->sel.hoff), &n->sel,n->fshift);
  183. if (!(n->sel.flags&(TC_U32_VAROFFSET|TC_U32_OFFSET|TC_U32_EAT)))
  184. goto next_ht;
  185. if (n->sel.flags&(TC_U32_OFFSET|TC_U32_VAROFFSET)) {
  186. off2 = n->sel.off + 3;
  187. if (n->sel.flags&TC_U32_VAROFFSET)
  188. off2 += ntohs(n->sel.offmask & *(u16*)(ptr+n->sel.offoff)) >>n->sel.offshift;
  189. off2 &= ~3;
  190. }
  191. if (n->sel.flags&TC_U32_EAT) {
  192. ptr += off2;
  193. off2 = 0;
  194. }
  195. if (ptr < skb->tail)
  196. goto next_ht;
  197. }
  198. /* POP */
  199. if (sdepth--) {
  200. n = stack[sdepth].knode;
  201. ht = n->ht_up;
  202. ptr = stack[sdepth].ptr;
  203. goto check_terminal;
  204. }
  205. return -1;
  206. deadloop:
  207. if (net_ratelimit())
  208. printk("cls_u32: dead loop\n");
  209. return -1;
  210. }
  211. static __inline__ struct tc_u_hnode *
  212. u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
  213. {
  214. struct tc_u_hnode *ht;
  215. for (ht = tp_c->hlist; ht; ht = ht->next)
  216. if (ht->handle == handle)
  217. break;
  218. return ht;
  219. }
  220. static __inline__ struct tc_u_knode *
  221. u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
  222. {
  223. unsigned sel;
  224. struct tc_u_knode *n = NULL;
  225. sel = TC_U32_HASH(handle);
  226. if (sel > ht->divisor)
  227. goto out;
  228. for (n = ht->ht[sel]; n; n = n->next)
  229. if (n->handle == handle)
  230. break;
  231. out:
  232. return n;
  233. }
  234. static unsigned long u32_get(struct tcf_proto *tp, u32 handle)
  235. {
  236. struct tc_u_hnode *ht;
  237. struct tc_u_common *tp_c = tp->data;
  238. if (TC_U32_HTID(handle) == TC_U32_ROOT)
  239. ht = tp->root;
  240. else
  241. ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
  242. if (!ht)
  243. return 0;
  244. if (TC_U32_KEY(handle) == 0)
  245. return (unsigned long)ht;
  246. return (unsigned long)u32_lookup_key(ht, handle);
  247. }
  248. static void u32_put(struct tcf_proto *tp, unsigned long f)
  249. {
  250. }
  251. static u32 gen_new_htid(struct tc_u_common *tp_c)
  252. {
  253. int i = 0x800;
  254. do {
  255. if (++tp_c->hgenerator == 0x7FF)
  256. tp_c->hgenerator = 1;
  257. } while (--i>0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
  258. return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
  259. }
  260. static int u32_init(struct tcf_proto *tp)
  261. {
  262. struct tc_u_hnode *root_ht;
  263. struct tc_u_common *tp_c;
  264. for (tp_c = u32_list; tp_c; tp_c = tp_c->next)
  265. if (tp_c->q == tp->q)
  266. break;
  267. root_ht = kmalloc(sizeof(*root_ht), GFP_KERNEL);
  268. if (root_ht == NULL)
  269. return -ENOBUFS;
  270. memset(root_ht, 0, sizeof(*root_ht));
  271. root_ht->divisor = 0;
  272. root_ht->refcnt++;
  273. root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
  274. root_ht->prio = tp->prio;
  275. if (tp_c == NULL) {
  276. tp_c = kmalloc(sizeof(*tp_c), GFP_KERNEL);
  277. if (tp_c == NULL) {
  278. kfree(root_ht);
  279. return -ENOBUFS;
  280. }
  281. memset(tp_c, 0, sizeof(*tp_c));
  282. tp_c->q = tp->q;
  283. tp_c->next = u32_list;
  284. u32_list = tp_c;
  285. }
  286. tp_c->refcnt++;
  287. root_ht->next = tp_c->hlist;
  288. tp_c->hlist = root_ht;
  289. root_ht->tp_c = tp_c;
  290. tp->root = root_ht;
  291. tp->data = tp_c;
  292. return 0;
  293. }
  294. static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
  295. {
  296. tcf_unbind_filter(tp, &n->res);
  297. tcf_exts_destroy(tp, &n->exts);
  298. if (n->ht_down)
  299. n->ht_down->refcnt--;
  300. #ifdef CONFIG_CLS_U32_PERF
  301. kfree(n->pf);
  302. #endif
  303. kfree(n);
  304. return 0;
  305. }
  306. static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
  307. {
  308. struct tc_u_knode **kp;
  309. struct tc_u_hnode *ht = key->ht_up;
  310. if (ht) {
  311. for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
  312. if (*kp == key) {
  313. tcf_tree_lock(tp);
  314. *kp = key->next;
  315. tcf_tree_unlock(tp);
  316. u32_destroy_key(tp, key);
  317. return 0;
  318. }
  319. }
  320. }
  321. BUG_TRAP(0);
  322. return 0;
  323. }
  324. static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  325. {
  326. struct tc_u_knode *n;
  327. unsigned h;
  328. for (h=0; h<=ht->divisor; h++) {
  329. while ((n = ht->ht[h]) != NULL) {
  330. ht->ht[h] = n->next;
  331. u32_destroy_key(tp, n);
  332. }
  333. }
  334. }
  335. static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  336. {
  337. struct tc_u_common *tp_c = tp->data;
  338. struct tc_u_hnode **hn;
  339. BUG_TRAP(!ht->refcnt);
  340. u32_clear_hnode(tp, ht);
  341. for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
  342. if (*hn == ht) {
  343. *hn = ht->next;
  344. kfree(ht);
  345. return 0;
  346. }
  347. }
  348. BUG_TRAP(0);
  349. return -ENOENT;
  350. }
  351. static void u32_destroy(struct tcf_proto *tp)
  352. {
  353. struct tc_u_common *tp_c = tp->data;
  354. struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
  355. BUG_TRAP(root_ht != NULL);
  356. if (root_ht && --root_ht->refcnt == 0)
  357. u32_destroy_hnode(tp, root_ht);
  358. if (--tp_c->refcnt == 0) {
  359. struct tc_u_hnode *ht;
  360. struct tc_u_common **tp_cp;
  361. for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
  362. if (*tp_cp == tp_c) {
  363. *tp_cp = tp_c->next;
  364. break;
  365. }
  366. }
  367. for (ht=tp_c->hlist; ht; ht = ht->next)
  368. u32_clear_hnode(tp, ht);
  369. while ((ht = tp_c->hlist) != NULL) {
  370. tp_c->hlist = ht->next;
  371. BUG_TRAP(ht->refcnt == 0);
  372. kfree(ht);
  373. };
  374. kfree(tp_c);
  375. }
  376. tp->data = NULL;
  377. }
  378. static int u32_delete(struct tcf_proto *tp, unsigned long arg)
  379. {
  380. struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
  381. if (ht == NULL)
  382. return 0;
  383. if (TC_U32_KEY(ht->handle))
  384. return u32_delete_key(tp, (struct tc_u_knode*)ht);
  385. if (tp->root == ht)
  386. return -EINVAL;
  387. if (--ht->refcnt == 0)
  388. u32_destroy_hnode(tp, ht);
  389. return 0;
  390. }
  391. static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
  392. {
  393. struct tc_u_knode *n;
  394. unsigned i = 0x7FF;
  395. for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
  396. if (i < TC_U32_NODE(n->handle))
  397. i = TC_U32_NODE(n->handle);
  398. i++;
  399. return handle|(i>0xFFF ? 0xFFF : i);
  400. }
  401. static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
  402. struct tc_u_hnode *ht,
  403. struct tc_u_knode *n, struct rtattr **tb,
  404. struct rtattr *est)
  405. {
  406. int err;
  407. struct tcf_exts e;
  408. err = tcf_exts_validate(tp, tb, est, &e, &u32_ext_map);
  409. if (err < 0)
  410. return err;
  411. err = -EINVAL;
  412. if (tb[TCA_U32_LINK-1]) {
  413. u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
  414. struct tc_u_hnode *ht_down = NULL;
  415. if (TC_U32_KEY(handle))
  416. goto errout;
  417. if (handle) {
  418. ht_down = u32_lookup_ht(ht->tp_c, handle);
  419. if (ht_down == NULL)
  420. goto errout;
  421. ht_down->refcnt++;
  422. }
  423. tcf_tree_lock(tp);
  424. ht_down = xchg(&n->ht_down, ht_down);
  425. tcf_tree_unlock(tp);
  426. if (ht_down)
  427. ht_down->refcnt--;
  428. }
  429. if (tb[TCA_U32_CLASSID-1]) {
  430. n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
  431. tcf_bind_filter(tp, &n->res, base);
  432. }
  433. #ifdef CONFIG_NET_CLS_IND
  434. if (tb[TCA_U32_INDEV-1]) {
  435. int err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV-1]);
  436. if (err < 0)
  437. goto errout;
  438. }
  439. #endif
  440. tcf_exts_change(tp, &n->exts, &e);
  441. return 0;
  442. errout:
  443. tcf_exts_destroy(tp, &e);
  444. return err;
  445. }
  446. static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  447. struct rtattr **tca,
  448. unsigned long *arg)
  449. {
  450. struct tc_u_common *tp_c = tp->data;
  451. struct tc_u_hnode *ht;
  452. struct tc_u_knode *n;
  453. struct tc_u32_sel *s;
  454. struct rtattr *opt = tca[TCA_OPTIONS-1];
  455. struct rtattr *tb[TCA_U32_MAX];
  456. u32 htid;
  457. int err;
  458. if (opt == NULL)
  459. return handle ? -EINVAL : 0;
  460. if (rtattr_parse_nested(tb, TCA_U32_MAX, opt) < 0)
  461. return -EINVAL;
  462. if ((n = (struct tc_u_knode*)*arg) != NULL) {
  463. if (TC_U32_KEY(n->handle) == 0)
  464. return -EINVAL;
  465. return u32_set_parms(tp, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
  466. }
  467. if (tb[TCA_U32_DIVISOR-1]) {
  468. unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
  469. if (--divisor > 0x100)
  470. return -EINVAL;
  471. if (TC_U32_KEY(handle))
  472. return -EINVAL;
  473. if (handle == 0) {
  474. handle = gen_new_htid(tp->data);
  475. if (handle == 0)
  476. return -ENOMEM;
  477. }
  478. ht = kmalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
  479. if (ht == NULL)
  480. return -ENOBUFS;
  481. memset(ht, 0, sizeof(*ht) + divisor*sizeof(void*));
  482. ht->tp_c = tp_c;
  483. ht->refcnt = 0;
  484. ht->divisor = divisor;
  485. ht->handle = handle;
  486. ht->prio = tp->prio;
  487. ht->next = tp_c->hlist;
  488. tp_c->hlist = ht;
  489. *arg = (unsigned long)ht;
  490. return 0;
  491. }
  492. if (tb[TCA_U32_HASH-1]) {
  493. htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
  494. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  495. ht = tp->root;
  496. htid = ht->handle;
  497. } else {
  498. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  499. if (ht == NULL)
  500. return -EINVAL;
  501. }
  502. } else {
  503. ht = tp->root;
  504. htid = ht->handle;
  505. }
  506. if (ht->divisor < TC_U32_HASH(htid))
  507. return -EINVAL;
  508. if (handle) {
  509. if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
  510. return -EINVAL;
  511. handle = htid | TC_U32_NODE(handle);
  512. } else
  513. handle = gen_new_kid(ht, htid);
  514. if (tb[TCA_U32_SEL-1] == 0 ||
  515. RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
  516. return -EINVAL;
  517. s = RTA_DATA(tb[TCA_U32_SEL-1]);
  518. n = kmalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
  519. if (n == NULL)
  520. return -ENOBUFS;
  521. memset(n, 0, sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key));
  522. #ifdef CONFIG_CLS_U32_PERF
  523. n->pf = kmalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64), GFP_KERNEL);
  524. if (n->pf == NULL) {
  525. kfree(n);
  526. return -ENOBUFS;
  527. }
  528. memset(n->pf, 0, sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64));
  529. #endif
  530. memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  531. n->ht_up = ht;
  532. n->handle = handle;
  533. {
  534. u8 i = 0;
  535. u32 mask = s->hmask;
  536. if (mask) {
  537. while (!(mask & 1)) {
  538. i++;
  539. mask>>=1;
  540. }
  541. }
  542. n->fshift = i;
  543. }
  544. #ifdef CONFIG_CLS_U32_MARK
  545. if (tb[TCA_U32_MARK-1]) {
  546. struct tc_u32_mark *mark;
  547. if (RTA_PAYLOAD(tb[TCA_U32_MARK-1]) < sizeof(struct tc_u32_mark)) {
  548. #ifdef CONFIG_CLS_U32_PERF
  549. kfree(n->pf);
  550. #endif
  551. kfree(n);
  552. return -EINVAL;
  553. }
  554. mark = RTA_DATA(tb[TCA_U32_MARK-1]);
  555. memcpy(&n->mark, mark, sizeof(struct tc_u32_mark));
  556. n->mark.success = 0;
  557. }
  558. #endif
  559. err = u32_set_parms(tp, base, ht, n, tb, tca[TCA_RATE-1]);
  560. if (err == 0) {
  561. struct tc_u_knode **ins;
  562. for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
  563. if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
  564. break;
  565. n->next = *ins;
  566. wmb();
  567. *ins = n;
  568. *arg = (unsigned long)n;
  569. return 0;
  570. }
  571. #ifdef CONFIG_CLS_U32_PERF
  572. kfree(n->pf);
  573. #endif
  574. kfree(n);
  575. return err;
  576. }
  577. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  578. {
  579. struct tc_u_common *tp_c = tp->data;
  580. struct tc_u_hnode *ht;
  581. struct tc_u_knode *n;
  582. unsigned h;
  583. if (arg->stop)
  584. return;
  585. for (ht = tp_c->hlist; ht; ht = ht->next) {
  586. if (ht->prio != tp->prio)
  587. continue;
  588. if (arg->count >= arg->skip) {
  589. if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
  590. arg->stop = 1;
  591. return;
  592. }
  593. }
  594. arg->count++;
  595. for (h = 0; h <= ht->divisor; h++) {
  596. for (n = ht->ht[h]; n; n = n->next) {
  597. if (arg->count < arg->skip) {
  598. arg->count++;
  599. continue;
  600. }
  601. if (arg->fn(tp, (unsigned long)n, arg) < 0) {
  602. arg->stop = 1;
  603. return;
  604. }
  605. arg->count++;
  606. }
  607. }
  608. }
  609. }
  610. static int u32_dump(struct tcf_proto *tp, unsigned long fh,
  611. struct sk_buff *skb, struct tcmsg *t)
  612. {
  613. struct tc_u_knode *n = (struct tc_u_knode*)fh;
  614. unsigned char *b = skb->tail;
  615. struct rtattr *rta;
  616. if (n == NULL)
  617. return skb->len;
  618. t->tcm_handle = n->handle;
  619. rta = (struct rtattr*)b;
  620. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  621. if (TC_U32_KEY(n->handle) == 0) {
  622. struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
  623. u32 divisor = ht->divisor+1;
  624. RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
  625. } else {
  626. RTA_PUT(skb, TCA_U32_SEL,
  627. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  628. &n->sel);
  629. if (n->ht_up) {
  630. u32 htid = n->handle & 0xFFFFF000;
  631. RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
  632. }
  633. if (n->res.classid)
  634. RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
  635. if (n->ht_down)
  636. RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
  637. #ifdef CONFIG_CLS_U32_MARK
  638. if (n->mark.val || n->mark.mask)
  639. RTA_PUT(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark);
  640. #endif
  641. if (tcf_exts_dump(skb, &n->exts, &u32_ext_map) < 0)
  642. goto rtattr_failure;
  643. #ifdef CONFIG_NET_CLS_IND
  644. if(strlen(n->indev))
  645. RTA_PUT(skb, TCA_U32_INDEV, IFNAMSIZ, n->indev);
  646. #endif
  647. #ifdef CONFIG_CLS_U32_PERF
  648. RTA_PUT(skb, TCA_U32_PCNT,
  649. sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
  650. n->pf);
  651. #endif
  652. }
  653. rta->rta_len = skb->tail - b;
  654. if (TC_U32_KEY(n->handle))
  655. if (tcf_exts_dump_stats(skb, &n->exts, &u32_ext_map) < 0)
  656. goto rtattr_failure;
  657. return skb->len;
  658. rtattr_failure:
  659. skb_trim(skb, b - skb->data);
  660. return -1;
  661. }
  662. static struct tcf_proto_ops cls_u32_ops = {
  663. .next = NULL,
  664. .kind = "u32",
  665. .classify = u32_classify,
  666. .init = u32_init,
  667. .destroy = u32_destroy,
  668. .get = u32_get,
  669. .put = u32_put,
  670. .change = u32_change,
  671. .delete = u32_delete,
  672. .walk = u32_walk,
  673. .dump = u32_dump,
  674. .owner = THIS_MODULE,
  675. };
  676. static int __init init_u32(void)
  677. {
  678. printk("u32 classifier\n");
  679. #ifdef CONFIG_CLS_U32_PERF
  680. printk(" Perfomance counters on\n");
  681. #endif
  682. #ifdef CONFIG_NET_CLS_POLICE
  683. printk(" OLD policer on \n");
  684. #endif
  685. #ifdef CONFIG_NET_CLS_IND
  686. printk(" input device check on \n");
  687. #endif
  688. #ifdef CONFIG_NET_CLS_ACT
  689. printk(" Actions configured \n");
  690. #endif
  691. return register_tcf_proto_ops(&cls_u32_ops);
  692. }
  693. static void __exit exit_u32(void)
  694. {
  695. unregister_tcf_proto_ops(&cls_u32_ops);
  696. }
  697. module_init(init_u32)
  698. module_exit(exit_u32)
  699. MODULE_LICENSE("GPL");