cls_u32.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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. if (n)
  302. kfree(n->pf);
  303. #endif
  304. kfree(n);
  305. return 0;
  306. }
  307. static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
  308. {
  309. struct tc_u_knode **kp;
  310. struct tc_u_hnode *ht = key->ht_up;
  311. if (ht) {
  312. for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
  313. if (*kp == key) {
  314. tcf_tree_lock(tp);
  315. *kp = key->next;
  316. tcf_tree_unlock(tp);
  317. u32_destroy_key(tp, key);
  318. return 0;
  319. }
  320. }
  321. }
  322. BUG_TRAP(0);
  323. return 0;
  324. }
  325. static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  326. {
  327. struct tc_u_knode *n;
  328. unsigned h;
  329. for (h=0; h<=ht->divisor; h++) {
  330. while ((n = ht->ht[h]) != NULL) {
  331. ht->ht[h] = n->next;
  332. u32_destroy_key(tp, n);
  333. }
  334. }
  335. }
  336. static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  337. {
  338. struct tc_u_common *tp_c = tp->data;
  339. struct tc_u_hnode **hn;
  340. BUG_TRAP(!ht->refcnt);
  341. u32_clear_hnode(tp, ht);
  342. for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
  343. if (*hn == ht) {
  344. *hn = ht->next;
  345. kfree(ht);
  346. return 0;
  347. }
  348. }
  349. BUG_TRAP(0);
  350. return -ENOENT;
  351. }
  352. static void u32_destroy(struct tcf_proto *tp)
  353. {
  354. struct tc_u_common *tp_c = tp->data;
  355. struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
  356. BUG_TRAP(root_ht != NULL);
  357. if (root_ht && --root_ht->refcnt == 0)
  358. u32_destroy_hnode(tp, root_ht);
  359. if (--tp_c->refcnt == 0) {
  360. struct tc_u_hnode *ht;
  361. struct tc_u_common **tp_cp;
  362. for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
  363. if (*tp_cp == tp_c) {
  364. *tp_cp = tp_c->next;
  365. break;
  366. }
  367. }
  368. for (ht=tp_c->hlist; ht; ht = ht->next)
  369. u32_clear_hnode(tp, ht);
  370. while ((ht = tp_c->hlist) != NULL) {
  371. tp_c->hlist = ht->next;
  372. BUG_TRAP(ht->refcnt == 0);
  373. kfree(ht);
  374. };
  375. kfree(tp_c);
  376. }
  377. tp->data = NULL;
  378. }
  379. static int u32_delete(struct tcf_proto *tp, unsigned long arg)
  380. {
  381. struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
  382. if (ht == NULL)
  383. return 0;
  384. if (TC_U32_KEY(ht->handle))
  385. return u32_delete_key(tp, (struct tc_u_knode*)ht);
  386. if (tp->root == ht)
  387. return -EINVAL;
  388. if (--ht->refcnt == 0)
  389. u32_destroy_hnode(tp, ht);
  390. return 0;
  391. }
  392. static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
  393. {
  394. struct tc_u_knode *n;
  395. unsigned i = 0x7FF;
  396. for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
  397. if (i < TC_U32_NODE(n->handle))
  398. i = TC_U32_NODE(n->handle);
  399. i++;
  400. return handle|(i>0xFFF ? 0xFFF : i);
  401. }
  402. static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
  403. struct tc_u_hnode *ht,
  404. struct tc_u_knode *n, struct rtattr **tb,
  405. struct rtattr *est)
  406. {
  407. int err;
  408. struct tcf_exts e;
  409. err = tcf_exts_validate(tp, tb, est, &e, &u32_ext_map);
  410. if (err < 0)
  411. return err;
  412. err = -EINVAL;
  413. if (tb[TCA_U32_LINK-1]) {
  414. u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
  415. struct tc_u_hnode *ht_down = NULL;
  416. if (TC_U32_KEY(handle))
  417. goto errout;
  418. if (handle) {
  419. ht_down = u32_lookup_ht(ht->tp_c, handle);
  420. if (ht_down == NULL)
  421. goto errout;
  422. ht_down->refcnt++;
  423. }
  424. tcf_tree_lock(tp);
  425. ht_down = xchg(&n->ht_down, ht_down);
  426. tcf_tree_unlock(tp);
  427. if (ht_down)
  428. ht_down->refcnt--;
  429. }
  430. if (tb[TCA_U32_CLASSID-1]) {
  431. n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
  432. tcf_bind_filter(tp, &n->res, base);
  433. }
  434. #ifdef CONFIG_NET_CLS_IND
  435. if (tb[TCA_U32_INDEV-1]) {
  436. int err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV-1]);
  437. if (err < 0)
  438. goto errout;
  439. }
  440. #endif
  441. tcf_exts_change(tp, &n->exts, &e);
  442. return 0;
  443. errout:
  444. tcf_exts_destroy(tp, &e);
  445. return err;
  446. }
  447. static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  448. struct rtattr **tca,
  449. unsigned long *arg)
  450. {
  451. struct tc_u_common *tp_c = tp->data;
  452. struct tc_u_hnode *ht;
  453. struct tc_u_knode *n;
  454. struct tc_u32_sel *s;
  455. struct rtattr *opt = tca[TCA_OPTIONS-1];
  456. struct rtattr *tb[TCA_U32_MAX];
  457. u32 htid;
  458. int err;
  459. if (opt == NULL)
  460. return handle ? -EINVAL : 0;
  461. if (rtattr_parse_nested(tb, TCA_U32_MAX, opt) < 0)
  462. return -EINVAL;
  463. if ((n = (struct tc_u_knode*)*arg) != NULL) {
  464. if (TC_U32_KEY(n->handle) == 0)
  465. return -EINVAL;
  466. return u32_set_parms(tp, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
  467. }
  468. if (tb[TCA_U32_DIVISOR-1]) {
  469. unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
  470. if (--divisor > 0x100)
  471. return -EINVAL;
  472. if (TC_U32_KEY(handle))
  473. return -EINVAL;
  474. if (handle == 0) {
  475. handle = gen_new_htid(tp->data);
  476. if (handle == 0)
  477. return -ENOMEM;
  478. }
  479. ht = kmalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
  480. if (ht == NULL)
  481. return -ENOBUFS;
  482. memset(ht, 0, sizeof(*ht) + divisor*sizeof(void*));
  483. ht->tp_c = tp_c;
  484. ht->refcnt = 0;
  485. ht->divisor = divisor;
  486. ht->handle = handle;
  487. ht->prio = tp->prio;
  488. ht->next = tp_c->hlist;
  489. tp_c->hlist = ht;
  490. *arg = (unsigned long)ht;
  491. return 0;
  492. }
  493. if (tb[TCA_U32_HASH-1]) {
  494. htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
  495. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  496. ht = tp->root;
  497. htid = ht->handle;
  498. } else {
  499. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  500. if (ht == NULL)
  501. return -EINVAL;
  502. }
  503. } else {
  504. ht = tp->root;
  505. htid = ht->handle;
  506. }
  507. if (ht->divisor < TC_U32_HASH(htid))
  508. return -EINVAL;
  509. if (handle) {
  510. if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
  511. return -EINVAL;
  512. handle = htid | TC_U32_NODE(handle);
  513. } else
  514. handle = gen_new_kid(ht, htid);
  515. if (tb[TCA_U32_SEL-1] == 0 ||
  516. RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
  517. return -EINVAL;
  518. s = RTA_DATA(tb[TCA_U32_SEL-1]);
  519. n = kmalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
  520. if (n == NULL)
  521. return -ENOBUFS;
  522. memset(n, 0, sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key));
  523. #ifdef CONFIG_CLS_U32_PERF
  524. n->pf = kmalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64), GFP_KERNEL);
  525. if (n->pf == NULL) {
  526. kfree(n);
  527. return -ENOBUFS;
  528. }
  529. memset(n->pf, 0, sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64));
  530. #endif
  531. memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  532. n->ht_up = ht;
  533. n->handle = handle;
  534. {
  535. u8 i = 0;
  536. u32 mask = s->hmask;
  537. if (mask) {
  538. while (!(mask & 1)) {
  539. i++;
  540. mask>>=1;
  541. }
  542. }
  543. n->fshift = i;
  544. }
  545. #ifdef CONFIG_CLS_U32_MARK
  546. if (tb[TCA_U32_MARK-1]) {
  547. struct tc_u32_mark *mark;
  548. if (RTA_PAYLOAD(tb[TCA_U32_MARK-1]) < sizeof(struct tc_u32_mark)) {
  549. #ifdef CONFIG_CLS_U32_PERF
  550. kfree(n->pf);
  551. #endif
  552. kfree(n);
  553. return -EINVAL;
  554. }
  555. mark = RTA_DATA(tb[TCA_U32_MARK-1]);
  556. memcpy(&n->mark, mark, sizeof(struct tc_u32_mark));
  557. n->mark.success = 0;
  558. }
  559. #endif
  560. err = u32_set_parms(tp, base, ht, n, tb, tca[TCA_RATE-1]);
  561. if (err == 0) {
  562. struct tc_u_knode **ins;
  563. for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
  564. if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
  565. break;
  566. n->next = *ins;
  567. wmb();
  568. *ins = n;
  569. *arg = (unsigned long)n;
  570. return 0;
  571. }
  572. #ifdef CONFIG_CLS_U32_PERF
  573. if (n)
  574. kfree(n->pf);
  575. #endif
  576. kfree(n);
  577. return err;
  578. }
  579. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  580. {
  581. struct tc_u_common *tp_c = tp->data;
  582. struct tc_u_hnode *ht;
  583. struct tc_u_knode *n;
  584. unsigned h;
  585. if (arg->stop)
  586. return;
  587. for (ht = tp_c->hlist; ht; ht = ht->next) {
  588. if (ht->prio != tp->prio)
  589. continue;
  590. if (arg->count >= arg->skip) {
  591. if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
  592. arg->stop = 1;
  593. return;
  594. }
  595. }
  596. arg->count++;
  597. for (h = 0; h <= ht->divisor; h++) {
  598. for (n = ht->ht[h]; n; n = n->next) {
  599. if (arg->count < arg->skip) {
  600. arg->count++;
  601. continue;
  602. }
  603. if (arg->fn(tp, (unsigned long)n, arg) < 0) {
  604. arg->stop = 1;
  605. return;
  606. }
  607. arg->count++;
  608. }
  609. }
  610. }
  611. }
  612. static int u32_dump(struct tcf_proto *tp, unsigned long fh,
  613. struct sk_buff *skb, struct tcmsg *t)
  614. {
  615. struct tc_u_knode *n = (struct tc_u_knode*)fh;
  616. unsigned char *b = skb->tail;
  617. struct rtattr *rta;
  618. if (n == NULL)
  619. return skb->len;
  620. t->tcm_handle = n->handle;
  621. rta = (struct rtattr*)b;
  622. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  623. if (TC_U32_KEY(n->handle) == 0) {
  624. struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
  625. u32 divisor = ht->divisor+1;
  626. RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
  627. } else {
  628. RTA_PUT(skb, TCA_U32_SEL,
  629. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  630. &n->sel);
  631. if (n->ht_up) {
  632. u32 htid = n->handle & 0xFFFFF000;
  633. RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
  634. }
  635. if (n->res.classid)
  636. RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
  637. if (n->ht_down)
  638. RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
  639. #ifdef CONFIG_CLS_U32_MARK
  640. if (n->mark.val || n->mark.mask)
  641. RTA_PUT(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark);
  642. #endif
  643. if (tcf_exts_dump(skb, &n->exts, &u32_ext_map) < 0)
  644. goto rtattr_failure;
  645. #ifdef CONFIG_NET_CLS_IND
  646. if(strlen(n->indev))
  647. RTA_PUT(skb, TCA_U32_INDEV, IFNAMSIZ, n->indev);
  648. #endif
  649. #ifdef CONFIG_CLS_U32_PERF
  650. RTA_PUT(skb, TCA_U32_PCNT,
  651. sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
  652. n->pf);
  653. #endif
  654. }
  655. rta->rta_len = skb->tail - b;
  656. if (TC_U32_KEY(n->handle))
  657. if (tcf_exts_dump_stats(skb, &n->exts, &u32_ext_map) < 0)
  658. goto rtattr_failure;
  659. return skb->len;
  660. rtattr_failure:
  661. skb_trim(skb, b - skb->data);
  662. return -1;
  663. }
  664. static struct tcf_proto_ops cls_u32_ops = {
  665. .next = NULL,
  666. .kind = "u32",
  667. .classify = u32_classify,
  668. .init = u32_init,
  669. .destroy = u32_destroy,
  670. .get = u32_get,
  671. .put = u32_put,
  672. .change = u32_change,
  673. .delete = u32_delete,
  674. .walk = u32_walk,
  675. .dump = u32_dump,
  676. .owner = THIS_MODULE,
  677. };
  678. static int __init init_u32(void)
  679. {
  680. printk("u32 classifier\n");
  681. #ifdef CONFIG_CLS_U32_PERF
  682. printk(" Perfomance counters on\n");
  683. #endif
  684. #ifdef CONFIG_NET_CLS_POLICE
  685. printk(" OLD policer on \n");
  686. #endif
  687. #ifdef CONFIG_NET_CLS_IND
  688. printk(" input device check on \n");
  689. #endif
  690. #ifdef CONFIG_NET_CLS_ACT
  691. printk(" Actions configured \n");
  692. #endif
  693. return register_tcf_proto_ops(&cls_u32_ops);
  694. }
  695. static void __exit exit_u32(void)
  696. {
  697. unregister_tcf_proto_ops(&cls_u32_ops);
  698. }
  699. module_init(init_u32)
  700. module_exit(exit_u32)
  701. MODULE_LICENSE("GPL");