cls_u32.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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 const 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(__be32 key, struct tc_u32_sel *sel, u8 fshift)
  86. {
  87. unsigned h = ntohl(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 ((*(__be32*)(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(*(__be32*)(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 & *(__be16*)(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. ht->refcnt--;
  350. u32_clear_hnode(tp, ht);
  351. }
  352. while ((ht = tp_c->hlist) != NULL) {
  353. tp_c->hlist = ht->next;
  354. BUG_TRAP(ht->refcnt == 0);
  355. kfree(ht);
  356. }
  357. kfree(tp_c);
  358. }
  359. tp->data = NULL;
  360. }
  361. static int u32_delete(struct tcf_proto *tp, unsigned long arg)
  362. {
  363. struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
  364. if (ht == NULL)
  365. return 0;
  366. if (TC_U32_KEY(ht->handle))
  367. return u32_delete_key(tp, (struct tc_u_knode*)ht);
  368. if (tp->root == ht)
  369. return -EINVAL;
  370. if (ht->refcnt == 1) {
  371. ht->refcnt--;
  372. u32_destroy_hnode(tp, ht);
  373. } else {
  374. return -EBUSY;
  375. }
  376. return 0;
  377. }
  378. static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
  379. {
  380. struct tc_u_knode *n;
  381. unsigned i = 0x7FF;
  382. for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
  383. if (i < TC_U32_NODE(n->handle))
  384. i = TC_U32_NODE(n->handle);
  385. i++;
  386. return handle|(i>0xFFF ? 0xFFF : i);
  387. }
  388. static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
  389. [TCA_U32_CLASSID] = { .type = NLA_U32 },
  390. [TCA_U32_HASH] = { .type = NLA_U32 },
  391. [TCA_U32_LINK] = { .type = NLA_U32 },
  392. [TCA_U32_DIVISOR] = { .type = NLA_U32 },
  393. [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
  394. [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
  395. [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
  396. };
  397. static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
  398. struct tc_u_hnode *ht,
  399. struct tc_u_knode *n, struct nlattr **tb,
  400. struct nlattr *est)
  401. {
  402. int err;
  403. struct tcf_exts e;
  404. err = tcf_exts_validate(tp, tb, est, &e, &u32_ext_map);
  405. if (err < 0)
  406. return err;
  407. err = -EINVAL;
  408. if (tb[TCA_U32_LINK]) {
  409. u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
  410. struct tc_u_hnode *ht_down = NULL;
  411. if (TC_U32_KEY(handle))
  412. goto errout;
  413. if (handle) {
  414. ht_down = u32_lookup_ht(ht->tp_c, handle);
  415. if (ht_down == NULL)
  416. goto errout;
  417. ht_down->refcnt++;
  418. }
  419. tcf_tree_lock(tp);
  420. ht_down = xchg(&n->ht_down, ht_down);
  421. tcf_tree_unlock(tp);
  422. if (ht_down)
  423. ht_down->refcnt--;
  424. }
  425. if (tb[TCA_U32_CLASSID]) {
  426. n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
  427. tcf_bind_filter(tp, &n->res, base);
  428. }
  429. #ifdef CONFIG_NET_CLS_IND
  430. if (tb[TCA_U32_INDEV]) {
  431. err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV]);
  432. if (err < 0)
  433. goto errout;
  434. }
  435. #endif
  436. tcf_exts_change(tp, &n->exts, &e);
  437. return 0;
  438. errout:
  439. tcf_exts_destroy(tp, &e);
  440. return err;
  441. }
  442. static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  443. struct nlattr **tca,
  444. unsigned long *arg)
  445. {
  446. struct tc_u_common *tp_c = tp->data;
  447. struct tc_u_hnode *ht;
  448. struct tc_u_knode *n;
  449. struct tc_u32_sel *s;
  450. struct nlattr *opt = tca[TCA_OPTIONS];
  451. struct nlattr *tb[TCA_U32_MAX + 1];
  452. u32 htid;
  453. int err;
  454. if (opt == NULL)
  455. return handle ? -EINVAL : 0;
  456. err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy);
  457. if (err < 0)
  458. return err;
  459. if ((n = (struct tc_u_knode*)*arg) != NULL) {
  460. if (TC_U32_KEY(n->handle) == 0)
  461. return -EINVAL;
  462. return u32_set_parms(tp, base, n->ht_up, n, tb, tca[TCA_RATE]);
  463. }
  464. if (tb[TCA_U32_DIVISOR]) {
  465. unsigned divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
  466. if (--divisor > 0x100)
  467. return -EINVAL;
  468. if (TC_U32_KEY(handle))
  469. return -EINVAL;
  470. if (handle == 0) {
  471. handle = gen_new_htid(tp->data);
  472. if (handle == 0)
  473. return -ENOMEM;
  474. }
  475. ht = kzalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
  476. if (ht == NULL)
  477. return -ENOBUFS;
  478. ht->tp_c = tp_c;
  479. ht->refcnt = 1;
  480. ht->divisor = divisor;
  481. ht->handle = handle;
  482. ht->prio = tp->prio;
  483. ht->next = tp_c->hlist;
  484. tp_c->hlist = ht;
  485. *arg = (unsigned long)ht;
  486. return 0;
  487. }
  488. if (tb[TCA_U32_HASH]) {
  489. htid = nla_get_u32(tb[TCA_U32_HASH]);
  490. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  491. ht = tp->root;
  492. htid = ht->handle;
  493. } else {
  494. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  495. if (ht == NULL)
  496. return -EINVAL;
  497. }
  498. } else {
  499. ht = tp->root;
  500. htid = ht->handle;
  501. }
  502. if (ht->divisor < TC_U32_HASH(htid))
  503. return -EINVAL;
  504. if (handle) {
  505. if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
  506. return -EINVAL;
  507. handle = htid | TC_U32_NODE(handle);
  508. } else
  509. handle = gen_new_kid(ht, htid);
  510. if (tb[TCA_U32_SEL] == NULL)
  511. return -EINVAL;
  512. s = nla_data(tb[TCA_U32_SEL]);
  513. n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
  514. if (n == NULL)
  515. return -ENOBUFS;
  516. #ifdef CONFIG_CLS_U32_PERF
  517. n->pf = kzalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64), GFP_KERNEL);
  518. if (n->pf == NULL) {
  519. kfree(n);
  520. return -ENOBUFS;
  521. }
  522. #endif
  523. memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  524. n->ht_up = ht;
  525. n->handle = handle;
  526. n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
  527. #ifdef CONFIG_CLS_U32_MARK
  528. if (tb[TCA_U32_MARK]) {
  529. struct tc_u32_mark *mark;
  530. mark = nla_data(tb[TCA_U32_MARK]);
  531. memcpy(&n->mark, mark, sizeof(struct tc_u32_mark));
  532. n->mark.success = 0;
  533. }
  534. #endif
  535. err = u32_set_parms(tp, base, ht, n, tb, tca[TCA_RATE]);
  536. if (err == 0) {
  537. struct tc_u_knode **ins;
  538. for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
  539. if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
  540. break;
  541. n->next = *ins;
  542. wmb();
  543. *ins = n;
  544. *arg = (unsigned long)n;
  545. return 0;
  546. }
  547. #ifdef CONFIG_CLS_U32_PERF
  548. kfree(n->pf);
  549. #endif
  550. kfree(n);
  551. return err;
  552. }
  553. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  554. {
  555. struct tc_u_common *tp_c = tp->data;
  556. struct tc_u_hnode *ht;
  557. struct tc_u_knode *n;
  558. unsigned h;
  559. if (arg->stop)
  560. return;
  561. for (ht = tp_c->hlist; ht; ht = ht->next) {
  562. if (ht->prio != tp->prio)
  563. continue;
  564. if (arg->count >= arg->skip) {
  565. if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
  566. arg->stop = 1;
  567. return;
  568. }
  569. }
  570. arg->count++;
  571. for (h = 0; h <= ht->divisor; h++) {
  572. for (n = ht->ht[h]; n; n = n->next) {
  573. if (arg->count < arg->skip) {
  574. arg->count++;
  575. continue;
  576. }
  577. if (arg->fn(tp, (unsigned long)n, arg) < 0) {
  578. arg->stop = 1;
  579. return;
  580. }
  581. arg->count++;
  582. }
  583. }
  584. }
  585. }
  586. static int u32_dump(struct tcf_proto *tp, unsigned long fh,
  587. struct sk_buff *skb, struct tcmsg *t)
  588. {
  589. struct tc_u_knode *n = (struct tc_u_knode*)fh;
  590. struct nlattr *nest;
  591. if (n == NULL)
  592. return skb->len;
  593. t->tcm_handle = n->handle;
  594. nest = nla_nest_start(skb, TCA_OPTIONS);
  595. if (nest == NULL)
  596. goto nla_put_failure;
  597. if (TC_U32_KEY(n->handle) == 0) {
  598. struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
  599. u32 divisor = ht->divisor+1;
  600. NLA_PUT_U32(skb, TCA_U32_DIVISOR, divisor);
  601. } else {
  602. NLA_PUT(skb, TCA_U32_SEL,
  603. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  604. &n->sel);
  605. if (n->ht_up) {
  606. u32 htid = n->handle & 0xFFFFF000;
  607. NLA_PUT_U32(skb, TCA_U32_HASH, htid);
  608. }
  609. if (n->res.classid)
  610. NLA_PUT_U32(skb, TCA_U32_CLASSID, n->res.classid);
  611. if (n->ht_down)
  612. NLA_PUT_U32(skb, TCA_U32_LINK, n->ht_down->handle);
  613. #ifdef CONFIG_CLS_U32_MARK
  614. if (n->mark.val || n->mark.mask)
  615. NLA_PUT(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark);
  616. #endif
  617. if (tcf_exts_dump(skb, &n->exts, &u32_ext_map) < 0)
  618. goto nla_put_failure;
  619. #ifdef CONFIG_NET_CLS_IND
  620. if(strlen(n->indev))
  621. NLA_PUT_STRING(skb, TCA_U32_INDEV, n->indev);
  622. #endif
  623. #ifdef CONFIG_CLS_U32_PERF
  624. NLA_PUT(skb, TCA_U32_PCNT,
  625. sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
  626. n->pf);
  627. #endif
  628. }
  629. nla_nest_end(skb, nest);
  630. if (TC_U32_KEY(n->handle))
  631. if (tcf_exts_dump_stats(skb, &n->exts, &u32_ext_map) < 0)
  632. goto nla_put_failure;
  633. return skb->len;
  634. nla_put_failure:
  635. nla_nest_cancel(skb, nest);
  636. return -1;
  637. }
  638. static struct tcf_proto_ops cls_u32_ops __read_mostly = {
  639. .kind = "u32",
  640. .classify = u32_classify,
  641. .init = u32_init,
  642. .destroy = u32_destroy,
  643. .get = u32_get,
  644. .put = u32_put,
  645. .change = u32_change,
  646. .delete = u32_delete,
  647. .walk = u32_walk,
  648. .dump = u32_dump,
  649. .owner = THIS_MODULE,
  650. };
  651. static int __init init_u32(void)
  652. {
  653. printk("u32 classifier\n");
  654. #ifdef CONFIG_CLS_U32_PERF
  655. printk(" Performance counters on\n");
  656. #endif
  657. #ifdef CONFIG_NET_CLS_IND
  658. printk(" input device check on \n");
  659. #endif
  660. #ifdef CONFIG_NET_CLS_ACT
  661. printk(" Actions configured \n");
  662. #endif
  663. return register_tcf_proto_ops(&cls_u32_ops);
  664. }
  665. static void __exit exit_u32(void)
  666. {
  667. unregister_tcf_proto_ops(&cls_u32_ops);
  668. }
  669. module_init(init_u32)
  670. module_exit(exit_u32)
  671. MODULE_LICENSE("GPL");