cls_u32.c 16 KB

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