cls_u32.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 <linux/module.h>
  33. #include <linux/types.h>
  34. #include <linux/kernel.h>
  35. #include <linux/string.h>
  36. #include <linux/errno.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/skbuff.h>
  39. #include <net/netlink.h>
  40. #include <net/act_api.h>
  41. #include <net/pkt_cls.h>
  42. struct tc_u_knode
  43. {
  44. struct tc_u_knode *next;
  45. u32 handle;
  46. struct tc_u_hnode *ht_up;
  47. struct tcf_exts exts;
  48. #ifdef CONFIG_NET_CLS_IND
  49. char indev[IFNAMSIZ];
  50. #endif
  51. u8 fshift;
  52. struct tcf_result res;
  53. struct tc_u_hnode *ht_down;
  54. #ifdef CONFIG_CLS_U32_PERF
  55. struct tc_u32_pcnt *pf;
  56. #endif
  57. #ifdef CONFIG_CLS_U32_MARK
  58. struct tc_u32_mark mark;
  59. #endif
  60. struct tc_u32_sel sel;
  61. };
  62. struct tc_u_hnode
  63. {
  64. struct tc_u_hnode *next;
  65. u32 handle;
  66. u32 prio;
  67. struct tc_u_common *tp_c;
  68. int refcnt;
  69. unsigned divisor;
  70. struct tc_u_knode *ht[1];
  71. };
  72. struct tc_u_common
  73. {
  74. struct tc_u_common *next;
  75. struct tc_u_hnode *hlist;
  76. struct Qdisc *q;
  77. int refcnt;
  78. u32 hgenerator;
  79. };
  80. static struct tcf_ext_map u32_ext_map = {
  81. .action = TCA_U32_ACT,
  82. .police = TCA_U32_POLICE
  83. };
  84. static struct tc_u_common *u32_list;
  85. static __inline__ unsigned u32_hash_fold(u32 key, struct tc_u32_sel *sel, u8 fshift)
  86. {
  87. unsigned h = (key & sel->hmask)>>fshift;
  88. return h;
  89. }
  90. static int u32_classify(struct sk_buff *skb, struct tcf_proto *tp, struct tcf_result *res)
  91. {
  92. struct {
  93. struct tc_u_knode *knode;
  94. u8 *ptr;
  95. } stack[TC_U32_MAXDEPTH];
  96. struct tc_u_hnode *ht = (struct tc_u_hnode*)tp->root;
  97. u8 *ptr = skb_network_header(skb);
  98. struct tc_u_knode *n;
  99. int sdepth = 0;
  100. int off2 = 0;
  101. int sel = 0;
  102. #ifdef CONFIG_CLS_U32_PERF
  103. int j;
  104. #endif
  105. int i, r;
  106. next_ht:
  107. n = ht->ht[sel];
  108. next_knode:
  109. if (n) {
  110. struct tc_u32_key *key = n->sel.keys;
  111. #ifdef CONFIG_CLS_U32_PERF
  112. n->pf->rcnt +=1;
  113. j = 0;
  114. #endif
  115. #ifdef CONFIG_CLS_U32_MARK
  116. if ((skb->mark & n->mark.mask) != n->mark.val) {
  117. n = n->next;
  118. goto next_knode;
  119. } else {
  120. n->mark.success++;
  121. }
  122. #endif
  123. for (i = n->sel.nkeys; i>0; i--, key++) {
  124. if ((*(u32*)(ptr+key->off+(off2&key->offmask))^key->val)&key->mask) {
  125. n = n->next;
  126. goto next_knode;
  127. }
  128. #ifdef CONFIG_CLS_U32_PERF
  129. n->pf->kcnts[j] +=1;
  130. j++;
  131. #endif
  132. }
  133. if (n->ht_down == NULL) {
  134. check_terminal:
  135. if (n->sel.flags&TC_U32_TERMINAL) {
  136. *res = n->res;
  137. #ifdef CONFIG_NET_CLS_IND
  138. if (!tcf_match_indev(skb, n->indev)) {
  139. n = n->next;
  140. goto next_knode;
  141. }
  142. #endif
  143. #ifdef CONFIG_CLS_U32_PERF
  144. n->pf->rhit +=1;
  145. #endif
  146. r = tcf_exts_exec(skb, &n->exts, res);
  147. if (r < 0) {
  148. n = n->next;
  149. goto next_knode;
  150. }
  151. return r;
  152. }
  153. n = n->next;
  154. goto next_knode;
  155. }
  156. /* PUSH */
  157. if (sdepth >= TC_U32_MAXDEPTH)
  158. goto deadloop;
  159. stack[sdepth].knode = n;
  160. stack[sdepth].ptr = ptr;
  161. sdepth++;
  162. ht = n->ht_down;
  163. sel = 0;
  164. if (ht->divisor)
  165. sel = ht->divisor&u32_hash_fold(*(u32*)(ptr+n->sel.hoff), &n->sel,n->fshift);
  166. if (!(n->sel.flags&(TC_U32_VAROFFSET|TC_U32_OFFSET|TC_U32_EAT)))
  167. goto next_ht;
  168. if (n->sel.flags&(TC_U32_OFFSET|TC_U32_VAROFFSET)) {
  169. off2 = n->sel.off + 3;
  170. if (n->sel.flags&TC_U32_VAROFFSET)
  171. off2 += ntohs(n->sel.offmask & *(u16*)(ptr+n->sel.offoff)) >>n->sel.offshift;
  172. off2 &= ~3;
  173. }
  174. if (n->sel.flags&TC_U32_EAT) {
  175. ptr += off2;
  176. off2 = 0;
  177. }
  178. if (ptr < skb_tail_pointer(skb))
  179. goto next_ht;
  180. }
  181. /* POP */
  182. if (sdepth--) {
  183. n = stack[sdepth].knode;
  184. ht = n->ht_up;
  185. ptr = stack[sdepth].ptr;
  186. goto check_terminal;
  187. }
  188. return -1;
  189. deadloop:
  190. if (net_ratelimit())
  191. printk("cls_u32: dead loop\n");
  192. return -1;
  193. }
  194. static __inline__ struct tc_u_hnode *
  195. u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
  196. {
  197. struct tc_u_hnode *ht;
  198. for (ht = tp_c->hlist; ht; ht = ht->next)
  199. if (ht->handle == handle)
  200. break;
  201. return ht;
  202. }
  203. static __inline__ struct tc_u_knode *
  204. u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
  205. {
  206. unsigned sel;
  207. struct tc_u_knode *n = NULL;
  208. sel = TC_U32_HASH(handle);
  209. if (sel > ht->divisor)
  210. goto out;
  211. for (n = ht->ht[sel]; n; n = n->next)
  212. if (n->handle == handle)
  213. break;
  214. out:
  215. return n;
  216. }
  217. static unsigned long u32_get(struct tcf_proto *tp, u32 handle)
  218. {
  219. struct tc_u_hnode *ht;
  220. struct tc_u_common *tp_c = tp->data;
  221. if (TC_U32_HTID(handle) == TC_U32_ROOT)
  222. ht = tp->root;
  223. else
  224. ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
  225. if (!ht)
  226. return 0;
  227. if (TC_U32_KEY(handle) == 0)
  228. return (unsigned long)ht;
  229. return (unsigned long)u32_lookup_key(ht, handle);
  230. }
  231. static void u32_put(struct tcf_proto *tp, unsigned long f)
  232. {
  233. }
  234. static u32 gen_new_htid(struct tc_u_common *tp_c)
  235. {
  236. int i = 0x800;
  237. do {
  238. if (++tp_c->hgenerator == 0x7FF)
  239. tp_c->hgenerator = 1;
  240. } while (--i>0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
  241. return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
  242. }
  243. static int u32_init(struct tcf_proto *tp)
  244. {
  245. struct tc_u_hnode *root_ht;
  246. struct tc_u_common *tp_c;
  247. for (tp_c = u32_list; tp_c; tp_c = tp_c->next)
  248. if (tp_c->q == tp->q)
  249. break;
  250. root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
  251. if (root_ht == NULL)
  252. return -ENOBUFS;
  253. root_ht->divisor = 0;
  254. root_ht->refcnt++;
  255. root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
  256. root_ht->prio = tp->prio;
  257. if (tp_c == NULL) {
  258. tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
  259. if (tp_c == NULL) {
  260. kfree(root_ht);
  261. return -ENOBUFS;
  262. }
  263. tp_c->q = tp->q;
  264. tp_c->next = u32_list;
  265. u32_list = tp_c;
  266. }
  267. tp_c->refcnt++;
  268. root_ht->next = tp_c->hlist;
  269. tp_c->hlist = root_ht;
  270. root_ht->tp_c = tp_c;
  271. tp->root = root_ht;
  272. tp->data = tp_c;
  273. return 0;
  274. }
  275. static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
  276. {
  277. tcf_unbind_filter(tp, &n->res);
  278. tcf_exts_destroy(tp, &n->exts);
  279. if (n->ht_down)
  280. n->ht_down->refcnt--;
  281. #ifdef CONFIG_CLS_U32_PERF
  282. kfree(n->pf);
  283. #endif
  284. kfree(n);
  285. return 0;
  286. }
  287. static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
  288. {
  289. struct tc_u_knode **kp;
  290. struct tc_u_hnode *ht = key->ht_up;
  291. if (ht) {
  292. for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
  293. if (*kp == key) {
  294. tcf_tree_lock(tp);
  295. *kp = key->next;
  296. tcf_tree_unlock(tp);
  297. u32_destroy_key(tp, key);
  298. return 0;
  299. }
  300. }
  301. }
  302. BUG_TRAP(0);
  303. return 0;
  304. }
  305. static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  306. {
  307. struct tc_u_knode *n;
  308. unsigned h;
  309. for (h=0; h<=ht->divisor; h++) {
  310. while ((n = ht->ht[h]) != NULL) {
  311. ht->ht[h] = n->next;
  312. u32_destroy_key(tp, n);
  313. }
  314. }
  315. }
  316. static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  317. {
  318. struct tc_u_common *tp_c = tp->data;
  319. struct tc_u_hnode **hn;
  320. BUG_TRAP(!ht->refcnt);
  321. u32_clear_hnode(tp, ht);
  322. for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
  323. if (*hn == ht) {
  324. *hn = ht->next;
  325. kfree(ht);
  326. return 0;
  327. }
  328. }
  329. BUG_TRAP(0);
  330. return -ENOENT;
  331. }
  332. static void u32_destroy(struct tcf_proto *tp)
  333. {
  334. struct tc_u_common *tp_c = tp->data;
  335. struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
  336. BUG_TRAP(root_ht != NULL);
  337. if (root_ht && --root_ht->refcnt == 0)
  338. u32_destroy_hnode(tp, root_ht);
  339. if (--tp_c->refcnt == 0) {
  340. struct tc_u_hnode *ht;
  341. struct tc_u_common **tp_cp;
  342. for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
  343. if (*tp_cp == tp_c) {
  344. *tp_cp = tp_c->next;
  345. break;
  346. }
  347. }
  348. for (ht=tp_c->hlist; ht; ht = ht->next)
  349. u32_clear_hnode(tp, ht);
  350. while ((ht = tp_c->hlist) != NULL) {
  351. tp_c->hlist = ht->next;
  352. BUG_TRAP(ht->refcnt == 0);
  353. kfree(ht);
  354. }
  355. kfree(tp_c);
  356. }
  357. tp->data = NULL;
  358. }
  359. static int u32_delete(struct tcf_proto *tp, unsigned long arg)
  360. {
  361. struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
  362. if (ht == NULL)
  363. return 0;
  364. if (TC_U32_KEY(ht->handle))
  365. return u32_delete_key(tp, (struct tc_u_knode*)ht);
  366. if (tp->root == ht)
  367. return -EINVAL;
  368. if (--ht->refcnt == 0)
  369. u32_destroy_hnode(tp, ht);
  370. return 0;
  371. }
  372. static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
  373. {
  374. struct tc_u_knode *n;
  375. unsigned i = 0x7FF;
  376. for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
  377. if (i < TC_U32_NODE(n->handle))
  378. i = TC_U32_NODE(n->handle);
  379. i++;
  380. return handle|(i>0xFFF ? 0xFFF : i);
  381. }
  382. static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
  383. struct tc_u_hnode *ht,
  384. struct tc_u_knode *n, struct rtattr **tb,
  385. struct rtattr *est)
  386. {
  387. int err;
  388. struct tcf_exts e;
  389. err = tcf_exts_validate(tp, tb, est, &e, &u32_ext_map);
  390. if (err < 0)
  391. return err;
  392. err = -EINVAL;
  393. if (tb[TCA_U32_LINK-1]) {
  394. u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
  395. struct tc_u_hnode *ht_down = NULL;
  396. if (TC_U32_KEY(handle))
  397. goto errout;
  398. if (handle) {
  399. ht_down = u32_lookup_ht(ht->tp_c, handle);
  400. if (ht_down == NULL)
  401. goto errout;
  402. ht_down->refcnt++;
  403. }
  404. tcf_tree_lock(tp);
  405. ht_down = xchg(&n->ht_down, ht_down);
  406. tcf_tree_unlock(tp);
  407. if (ht_down)
  408. ht_down->refcnt--;
  409. }
  410. if (tb[TCA_U32_CLASSID-1]) {
  411. n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
  412. tcf_bind_filter(tp, &n->res, base);
  413. }
  414. #ifdef CONFIG_NET_CLS_IND
  415. if (tb[TCA_U32_INDEV-1]) {
  416. err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV-1]);
  417. if (err < 0)
  418. goto errout;
  419. }
  420. #endif
  421. tcf_exts_change(tp, &n->exts, &e);
  422. return 0;
  423. errout:
  424. tcf_exts_destroy(tp, &e);
  425. return err;
  426. }
  427. static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  428. struct rtattr **tca,
  429. unsigned long *arg)
  430. {
  431. struct tc_u_common *tp_c = tp->data;
  432. struct tc_u_hnode *ht;
  433. struct tc_u_knode *n;
  434. struct tc_u32_sel *s;
  435. struct rtattr *opt = tca[TCA_OPTIONS-1];
  436. struct rtattr *tb[TCA_U32_MAX];
  437. u32 htid;
  438. int err;
  439. if (opt == NULL)
  440. return handle ? -EINVAL : 0;
  441. if (rtattr_parse_nested(tb, TCA_U32_MAX, opt) < 0)
  442. return -EINVAL;
  443. if ((n = (struct tc_u_knode*)*arg) != NULL) {
  444. if (TC_U32_KEY(n->handle) == 0)
  445. return -EINVAL;
  446. return u32_set_parms(tp, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
  447. }
  448. if (tb[TCA_U32_DIVISOR-1]) {
  449. unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
  450. if (--divisor > 0x100)
  451. return -EINVAL;
  452. if (TC_U32_KEY(handle))
  453. return -EINVAL;
  454. if (handle == 0) {
  455. handle = gen_new_htid(tp->data);
  456. if (handle == 0)
  457. return -ENOMEM;
  458. }
  459. ht = kzalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
  460. if (ht == NULL)
  461. return -ENOBUFS;
  462. ht->tp_c = tp_c;
  463. ht->refcnt = 0;
  464. ht->divisor = divisor;
  465. ht->handle = handle;
  466. ht->prio = tp->prio;
  467. ht->next = tp_c->hlist;
  468. tp_c->hlist = ht;
  469. *arg = (unsigned long)ht;
  470. return 0;
  471. }
  472. if (tb[TCA_U32_HASH-1]) {
  473. htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
  474. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  475. ht = tp->root;
  476. htid = ht->handle;
  477. } else {
  478. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  479. if (ht == NULL)
  480. return -EINVAL;
  481. }
  482. } else {
  483. ht = tp->root;
  484. htid = ht->handle;
  485. }
  486. if (ht->divisor < TC_U32_HASH(htid))
  487. return -EINVAL;
  488. if (handle) {
  489. if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
  490. return -EINVAL;
  491. handle = htid | TC_U32_NODE(handle);
  492. } else
  493. handle = gen_new_kid(ht, htid);
  494. if (tb[TCA_U32_SEL-1] == NULL ||
  495. RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
  496. return -EINVAL;
  497. s = RTA_DATA(tb[TCA_U32_SEL-1]);
  498. n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
  499. if (n == NULL)
  500. return -ENOBUFS;
  501. #ifdef CONFIG_CLS_U32_PERF
  502. n->pf = kzalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64), GFP_KERNEL);
  503. if (n->pf == NULL) {
  504. kfree(n);
  505. return -ENOBUFS;
  506. }
  507. #endif
  508. memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  509. n->ht_up = ht;
  510. n->handle = handle;
  511. {
  512. u8 i = 0;
  513. u32 mask = s->hmask;
  514. if (mask) {
  515. while (!(mask & 1)) {
  516. i++;
  517. mask>>=1;
  518. }
  519. }
  520. n->fshift = i;
  521. }
  522. #ifdef CONFIG_CLS_U32_MARK
  523. if (tb[TCA_U32_MARK-1]) {
  524. struct tc_u32_mark *mark;
  525. if (RTA_PAYLOAD(tb[TCA_U32_MARK-1]) < sizeof(struct tc_u32_mark)) {
  526. #ifdef CONFIG_CLS_U32_PERF
  527. kfree(n->pf);
  528. #endif
  529. kfree(n);
  530. return -EINVAL;
  531. }
  532. mark = RTA_DATA(tb[TCA_U32_MARK-1]);
  533. memcpy(&n->mark, mark, sizeof(struct tc_u32_mark));
  534. n->mark.success = 0;
  535. }
  536. #endif
  537. err = u32_set_parms(tp, base, ht, n, tb, tca[TCA_RATE-1]);
  538. if (err == 0) {
  539. struct tc_u_knode **ins;
  540. for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
  541. if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
  542. break;
  543. n->next = *ins;
  544. wmb();
  545. *ins = n;
  546. *arg = (unsigned long)n;
  547. return 0;
  548. }
  549. #ifdef CONFIG_CLS_U32_PERF
  550. kfree(n->pf);
  551. #endif
  552. kfree(n);
  553. return err;
  554. }
  555. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  556. {
  557. struct tc_u_common *tp_c = tp->data;
  558. struct tc_u_hnode *ht;
  559. struct tc_u_knode *n;
  560. unsigned h;
  561. if (arg->stop)
  562. return;
  563. for (ht = tp_c->hlist; ht; ht = ht->next) {
  564. if (ht->prio != tp->prio)
  565. continue;
  566. if (arg->count >= arg->skip) {
  567. if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
  568. arg->stop = 1;
  569. return;
  570. }
  571. }
  572. arg->count++;
  573. for (h = 0; h <= ht->divisor; h++) {
  574. for (n = ht->ht[h]; n; n = n->next) {
  575. if (arg->count < arg->skip) {
  576. arg->count++;
  577. continue;
  578. }
  579. if (arg->fn(tp, (unsigned long)n, arg) < 0) {
  580. arg->stop = 1;
  581. return;
  582. }
  583. arg->count++;
  584. }
  585. }
  586. }
  587. }
  588. static int u32_dump(struct tcf_proto *tp, unsigned long fh,
  589. struct sk_buff *skb, struct tcmsg *t)
  590. {
  591. struct tc_u_knode *n = (struct tc_u_knode*)fh;
  592. unsigned char *b = skb_tail_pointer(skb);
  593. struct rtattr *rta;
  594. if (n == NULL)
  595. return skb->len;
  596. t->tcm_handle = n->handle;
  597. rta = (struct rtattr*)b;
  598. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  599. if (TC_U32_KEY(n->handle) == 0) {
  600. struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
  601. u32 divisor = ht->divisor+1;
  602. RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
  603. } else {
  604. RTA_PUT(skb, TCA_U32_SEL,
  605. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  606. &n->sel);
  607. if (n->ht_up) {
  608. u32 htid = n->handle & 0xFFFFF000;
  609. RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
  610. }
  611. if (n->res.classid)
  612. RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
  613. if (n->ht_down)
  614. RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
  615. #ifdef CONFIG_CLS_U32_MARK
  616. if (n->mark.val || n->mark.mask)
  617. RTA_PUT(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark);
  618. #endif
  619. if (tcf_exts_dump(skb, &n->exts, &u32_ext_map) < 0)
  620. goto rtattr_failure;
  621. #ifdef CONFIG_NET_CLS_IND
  622. if(strlen(n->indev))
  623. RTA_PUT(skb, TCA_U32_INDEV, IFNAMSIZ, n->indev);
  624. #endif
  625. #ifdef CONFIG_CLS_U32_PERF
  626. RTA_PUT(skb, TCA_U32_PCNT,
  627. sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
  628. n->pf);
  629. #endif
  630. }
  631. rta->rta_len = skb_tail_pointer(skb) - b;
  632. if (TC_U32_KEY(n->handle))
  633. if (tcf_exts_dump_stats(skb, &n->exts, &u32_ext_map) < 0)
  634. goto rtattr_failure;
  635. return skb->len;
  636. rtattr_failure:
  637. nlmsg_trim(skb, b);
  638. return -1;
  639. }
  640. static struct tcf_proto_ops cls_u32_ops = {
  641. .next = NULL,
  642. .kind = "u32",
  643. .classify = u32_classify,
  644. .init = u32_init,
  645. .destroy = u32_destroy,
  646. .get = u32_get,
  647. .put = u32_put,
  648. .change = u32_change,
  649. .delete = u32_delete,
  650. .walk = u32_walk,
  651. .dump = u32_dump,
  652. .owner = THIS_MODULE,
  653. };
  654. static int __init init_u32(void)
  655. {
  656. printk("u32 classifier\n");
  657. #ifdef CONFIG_CLS_U32_PERF
  658. printk(" Performance counters on\n");
  659. #endif
  660. #ifdef CONFIG_NET_CLS_IND
  661. printk(" input device check on \n");
  662. #endif
  663. #ifdef CONFIG_NET_CLS_ACT
  664. printk(" Actions configured \n");
  665. #endif
  666. return register_tcf_proto_ops(&cls_u32_ops);
  667. }
  668. static void __exit exit_u32(void)
  669. {
  670. unregister_tcf_proto_ops(&cls_u32_ops);
  671. }
  672. module_init(init_u32)
  673. module_exit(exit_u32)
  674. MODULE_LICENSE("GPL");