PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/net/sched/cls_u32.c

https://github.com/mstsirkin/kvm
C | 817 lines | 653 code | 131 blank | 33 comment | 150 complexity | 21c57ad388cc868e7d99a1f01eae0420 MD5 | raw file
  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. if (net_ratelimit())
  212. pr_warning("cls_u32: dead loop\n");
  213. return -1;
  214. }
  215. static struct tc_u_hnode *
  216. u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
  217. {
  218. struct tc_u_hnode *ht;
  219. for (ht = tp_c->hlist; ht; ht = ht->next)
  220. if (ht->handle == handle)
  221. break;
  222. return ht;
  223. }
  224. static struct tc_u_knode *
  225. u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
  226. {
  227. unsigned int sel;
  228. struct tc_u_knode *n = NULL;
  229. sel = TC_U32_HASH(handle);
  230. if (sel > ht->divisor)
  231. goto out;
  232. for (n = ht->ht[sel]; n; n = n->next)
  233. if (n->handle == handle)
  234. break;
  235. out:
  236. return n;
  237. }
  238. static unsigned long u32_get(struct tcf_proto *tp, u32 handle)
  239. {
  240. struct tc_u_hnode *ht;
  241. struct tc_u_common *tp_c = tp->data;
  242. if (TC_U32_HTID(handle) == TC_U32_ROOT)
  243. ht = tp->root;
  244. else
  245. ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
  246. if (!ht)
  247. return 0;
  248. if (TC_U32_KEY(handle) == 0)
  249. return (unsigned long)ht;
  250. return (unsigned long)u32_lookup_key(ht, handle);
  251. }
  252. static void u32_put(struct tcf_proto *tp, unsigned long f)
  253. {
  254. }
  255. static u32 gen_new_htid(struct tc_u_common *tp_c)
  256. {
  257. int i = 0x800;
  258. do {
  259. if (++tp_c->hgenerator == 0x7FF)
  260. tp_c->hgenerator = 1;
  261. } while (--i > 0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
  262. return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
  263. }
  264. static int u32_init(struct tcf_proto *tp)
  265. {
  266. struct tc_u_hnode *root_ht;
  267. struct tc_u_common *tp_c;
  268. tp_c = tp->q->u32_node;
  269. root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
  270. if (root_ht == NULL)
  271. return -ENOBUFS;
  272. root_ht->divisor = 0;
  273. root_ht->refcnt++;
  274. root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
  275. root_ht->prio = tp->prio;
  276. if (tp_c == NULL) {
  277. tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
  278. if (tp_c == NULL) {
  279. kfree(root_ht);
  280. return -ENOBUFS;
  281. }
  282. tp_c->q = tp->q;
  283. tp->q->u32_node = tp_c;
  284. }
  285. tp_c->refcnt++;
  286. root_ht->next = tp_c->hlist;
  287. tp_c->hlist = root_ht;
  288. root_ht->tp_c = tp_c;
  289. tp->root = root_ht;
  290. tp->data = tp_c;
  291. return 0;
  292. }
  293. static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
  294. {
  295. tcf_unbind_filter(tp, &n->res);
  296. tcf_exts_destroy(tp, &n->exts);
  297. if (n->ht_down)
  298. n->ht_down->refcnt--;
  299. #ifdef CONFIG_CLS_U32_PERF
  300. kfree(n->pf);
  301. #endif
  302. kfree(n);
  303. return 0;
  304. }
  305. static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
  306. {
  307. struct tc_u_knode **kp;
  308. struct tc_u_hnode *ht = key->ht_up;
  309. if (ht) {
  310. for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
  311. if (*kp == key) {
  312. tcf_tree_lock(tp);
  313. *kp = key->next;
  314. tcf_tree_unlock(tp);
  315. u32_destroy_key(tp, key);
  316. return 0;
  317. }
  318. }
  319. }
  320. WARN_ON(1);
  321. return 0;
  322. }
  323. static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  324. {
  325. struct tc_u_knode *n;
  326. unsigned int h;
  327. for (h = 0; h <= ht->divisor; h++) {
  328. while ((n = ht->ht[h]) != NULL) {
  329. ht->ht[h] = n->next;
  330. u32_destroy_key(tp, n);
  331. }
  332. }
  333. }
  334. static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  335. {
  336. struct tc_u_common *tp_c = tp->data;
  337. struct tc_u_hnode **hn;
  338. WARN_ON(ht->refcnt);
  339. u32_clear_hnode(tp, ht);
  340. for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
  341. if (*hn == ht) {
  342. *hn = ht->next;
  343. kfree(ht);
  344. return 0;
  345. }
  346. }
  347. WARN_ON(1);
  348. return -ENOENT;
  349. }
  350. static void u32_destroy(struct tcf_proto *tp)
  351. {
  352. struct tc_u_common *tp_c = tp->data;
  353. struct tc_u_hnode *root_ht = tp->root;
  354. WARN_ON(root_ht == NULL);
  355. if (root_ht && --root_ht->refcnt == 0)
  356. u32_destroy_hnode(tp, root_ht);
  357. if (--tp_c->refcnt == 0) {
  358. struct tc_u_hnode *ht;
  359. tp->q->u32_node = NULL;
  360. for (ht = tp_c->hlist; ht; ht = ht->next) {
  361. ht->refcnt--;
  362. u32_clear_hnode(tp, ht);
  363. }
  364. while ((ht = tp_c->hlist) != NULL) {
  365. tp_c->hlist = ht->next;
  366. WARN_ON(ht->refcnt != 0);
  367. kfree(ht);
  368. }
  369. kfree(tp_c);
  370. }
  371. tp->data = NULL;
  372. }
  373. static int u32_delete(struct tcf_proto *tp, unsigned long arg)
  374. {
  375. struct tc_u_hnode *ht = (struct tc_u_hnode *)arg;
  376. if (ht == NULL)
  377. return 0;
  378. if (TC_U32_KEY(ht->handle))
  379. return u32_delete_key(tp, (struct tc_u_knode *)ht);
  380. if (tp->root == ht)
  381. return -EINVAL;
  382. if (ht->refcnt == 1) {
  383. ht->refcnt--;
  384. u32_destroy_hnode(tp, ht);
  385. } else {
  386. return -EBUSY;
  387. }
  388. return 0;
  389. }
  390. static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
  391. {
  392. struct tc_u_knode *n;
  393. unsigned int i = 0x7FF;
  394. for (n = ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
  395. if (i < TC_U32_NODE(n->handle))
  396. i = TC_U32_NODE(n->handle);
  397. i++;
  398. return handle | (i > 0xFFF ? 0xFFF : i);
  399. }
  400. static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
  401. [TCA_U32_CLASSID] = { .type = NLA_U32 },
  402. [TCA_U32_HASH] = { .type = NLA_U32 },
  403. [TCA_U32_LINK] = { .type = NLA_U32 },
  404. [TCA_U32_DIVISOR] = { .type = NLA_U32 },
  405. [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
  406. [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
  407. [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
  408. };
  409. static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
  410. struct tc_u_hnode *ht,
  411. struct tc_u_knode *n, struct nlattr **tb,
  412. struct nlattr *est)
  413. {
  414. int err;
  415. struct tcf_exts e;
  416. err = tcf_exts_validate(tp, tb, est, &e, &u32_ext_map);
  417. if (err < 0)
  418. return err;
  419. err = -EINVAL;
  420. if (tb[TCA_U32_LINK]) {
  421. u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
  422. struct tc_u_hnode *ht_down = NULL, *ht_old;
  423. if (TC_U32_KEY(handle))
  424. goto errout;
  425. if (handle) {
  426. ht_down = u32_lookup_ht(ht->tp_c, handle);
  427. if (ht_down == NULL)
  428. goto errout;
  429. ht_down->refcnt++;
  430. }
  431. tcf_tree_lock(tp);
  432. ht_old = n->ht_down;
  433. n->ht_down = ht_down;
  434. tcf_tree_unlock(tp);
  435. if (ht_old)
  436. ht_old->refcnt--;
  437. }
  438. if (tb[TCA_U32_CLASSID]) {
  439. n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
  440. tcf_bind_filter(tp, &n->res, base);
  441. }
  442. #ifdef CONFIG_NET_CLS_IND
  443. if (tb[TCA_U32_INDEV]) {
  444. err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV]);
  445. if (err < 0)
  446. goto errout;
  447. }
  448. #endif
  449. tcf_exts_change(tp, &n->exts, &e);
  450. return 0;
  451. errout:
  452. tcf_exts_destroy(tp, &e);
  453. return err;
  454. }
  455. static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  456. struct nlattr **tca,
  457. unsigned long *arg)
  458. {
  459. struct tc_u_common *tp_c = tp->data;
  460. struct tc_u_hnode *ht;
  461. struct tc_u_knode *n;
  462. struct tc_u32_sel *s;
  463. struct nlattr *opt = tca[TCA_OPTIONS];
  464. struct nlattr *tb[TCA_U32_MAX + 1];
  465. u32 htid;
  466. int err;
  467. if (opt == NULL)
  468. return handle ? -EINVAL : 0;
  469. err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy);
  470. if (err < 0)
  471. return err;
  472. n = (struct tc_u_knode *)*arg;
  473. if (n) {
  474. if (TC_U32_KEY(n->handle) == 0)
  475. return -EINVAL;
  476. return u32_set_parms(tp, base, n->ht_up, n, tb, 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. #ifdef CONFIG_CLS_U32_MARK
  542. if (tb[TCA_U32_MARK]) {
  543. struct tc_u32_mark *mark;
  544. mark = nla_data(tb[TCA_U32_MARK]);
  545. memcpy(&n->mark, mark, sizeof(struct tc_u32_mark));
  546. n->mark.success = 0;
  547. }
  548. #endif
  549. err = u32_set_parms(tp, base, ht, n, tb, tca[TCA_RATE]);
  550. if (err == 0) {
  551. struct tc_u_knode **ins;
  552. for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
  553. if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
  554. break;
  555. n->next = *ins;
  556. tcf_tree_lock(tp);
  557. *ins = n;
  558. tcf_tree_unlock(tp);
  559. *arg = (unsigned long)n;
  560. return 0;
  561. }
  562. #ifdef CONFIG_CLS_U32_PERF
  563. kfree(n->pf);
  564. #endif
  565. kfree(n);
  566. return err;
  567. }
  568. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  569. {
  570. struct tc_u_common *tp_c = tp->data;
  571. struct tc_u_hnode *ht;
  572. struct tc_u_knode *n;
  573. unsigned int h;
  574. if (arg->stop)
  575. return;
  576. for (ht = tp_c->hlist; ht; ht = ht->next) {
  577. if (ht->prio != tp->prio)
  578. continue;
  579. if (arg->count >= arg->skip) {
  580. if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
  581. arg->stop = 1;
  582. return;
  583. }
  584. }
  585. arg->count++;
  586. for (h = 0; h <= ht->divisor; h++) {
  587. for (n = ht->ht[h]; n; n = n->next) {
  588. if (arg->count < arg->skip) {
  589. arg->count++;
  590. continue;
  591. }
  592. if (arg->fn(tp, (unsigned long)n, arg) < 0) {
  593. arg->stop = 1;
  594. return;
  595. }
  596. arg->count++;
  597. }
  598. }
  599. }
  600. }
  601. static int u32_dump(struct tcf_proto *tp, unsigned long fh,
  602. struct sk_buff *skb, struct tcmsg *t)
  603. {
  604. struct tc_u_knode *n = (struct tc_u_knode *)fh;
  605. struct nlattr *nest;
  606. if (n == NULL)
  607. return skb->len;
  608. t->tcm_handle = n->handle;
  609. nest = nla_nest_start(skb, TCA_OPTIONS);
  610. if (nest == NULL)
  611. goto nla_put_failure;
  612. if (TC_U32_KEY(n->handle) == 0) {
  613. struct tc_u_hnode *ht = (struct tc_u_hnode *)fh;
  614. u32 divisor = ht->divisor + 1;
  615. NLA_PUT_U32(skb, TCA_U32_DIVISOR, divisor);
  616. } else {
  617. NLA_PUT(skb, TCA_U32_SEL,
  618. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  619. &n->sel);
  620. if (n->ht_up) {
  621. u32 htid = n->handle & 0xFFFFF000;
  622. NLA_PUT_U32(skb, TCA_U32_HASH, htid);
  623. }
  624. if (n->res.classid)
  625. NLA_PUT_U32(skb, TCA_U32_CLASSID, n->res.classid);
  626. if (n->ht_down)
  627. NLA_PUT_U32(skb, TCA_U32_LINK, n->ht_down->handle);
  628. #ifdef CONFIG_CLS_U32_MARK
  629. if (n->mark.val || n->mark.mask)
  630. NLA_PUT(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark);
  631. #endif
  632. if (tcf_exts_dump(skb, &n->exts, &u32_ext_map) < 0)
  633. goto nla_put_failure;
  634. #ifdef CONFIG_NET_CLS_IND
  635. if (strlen(n->indev))
  636. NLA_PUT_STRING(skb, TCA_U32_INDEV, n->indev);
  637. #endif
  638. #ifdef CONFIG_CLS_U32_PERF
  639. NLA_PUT(skb, TCA_U32_PCNT,
  640. sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
  641. n->pf);
  642. #endif
  643. }
  644. nla_nest_end(skb, nest);
  645. if (TC_U32_KEY(n->handle))
  646. if (tcf_exts_dump_stats(skb, &n->exts, &u32_ext_map) < 0)
  647. goto nla_put_failure;
  648. return skb->len;
  649. nla_put_failure:
  650. nla_nest_cancel(skb, nest);
  651. return -1;
  652. }
  653. static struct tcf_proto_ops cls_u32_ops __read_mostly = {
  654. .kind = "u32",
  655. .classify = u32_classify,
  656. .init = u32_init,
  657. .destroy = u32_destroy,
  658. .get = u32_get,
  659. .put = u32_put,
  660. .change = u32_change,
  661. .delete = u32_delete,
  662. .walk = u32_walk,
  663. .dump = u32_dump,
  664. .owner = THIS_MODULE,
  665. };
  666. static int __init init_u32(void)
  667. {
  668. pr_info("u32 classifier\n");
  669. #ifdef CONFIG_CLS_U32_PERF
  670. pr_info(" Performance counters on\n");
  671. #endif
  672. #ifdef CONFIG_NET_CLS_IND
  673. pr_info(" input device check on\n");
  674. #endif
  675. #ifdef CONFIG_NET_CLS_ACT
  676. pr_info(" Actions configured\n");
  677. #endif
  678. return register_tcf_proto_ops(&cls_u32_ops);
  679. }
  680. static void __exit exit_u32(void)
  681. {
  682. unregister_tcf_proto_ops(&cls_u32_ops);
  683. }
  684. module_init(init_u32)
  685. module_exit(exit_u32)
  686. MODULE_LICENSE("GPL");