cls_u32.c 17 KB

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