cls_u32.c 17 KB

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