cls_u32.c 16 KB

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