cls_u32.c 17 KB

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