cls_u32.c 16 KB

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