cls_u32.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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 <asm/uaccess.h>
  33. #include <asm/system.h>
  34. #include <linux/bitops.h>
  35. #include <linux/module.h>
  36. #include <linux/types.h>
  37. #include <linux/kernel.h>
  38. #include <linux/sched.h>
  39. #include <linux/string.h>
  40. #include <linux/mm.h>
  41. #include <linux/socket.h>
  42. #include <linux/sockios.h>
  43. #include <linux/in.h>
  44. #include <linux/errno.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/if_ether.h>
  47. #include <linux/inet.h>
  48. #include <linux/netdevice.h>
  49. #include <linux/etherdevice.h>
  50. #include <linux/notifier.h>
  51. #include <linux/rtnetlink.h>
  52. #include <net/ip.h>
  53. #include <net/route.h>
  54. #include <linux/skbuff.h>
  55. #include <net/sock.h>
  56. #include <net/act_api.h>
  57. #include <net/pkt_cls.h>
  58. struct tc_u_knode
  59. {
  60. struct tc_u_knode *next;
  61. u32 handle;
  62. struct tc_u_hnode *ht_up;
  63. struct tcf_exts exts;
  64. #ifdef CONFIG_NET_CLS_IND
  65. char indev[IFNAMSIZ];
  66. #endif
  67. u8 fshift;
  68. struct tcf_result res;
  69. struct tc_u_hnode *ht_down;
  70. #ifdef CONFIG_CLS_U32_PERF
  71. struct tc_u32_pcnt *pf;
  72. #endif
  73. #ifdef CONFIG_CLS_U32_MARK
  74. struct tc_u32_mark mark;
  75. #endif
  76. struct tc_u32_sel sel;
  77. };
  78. struct tc_u_hnode
  79. {
  80. struct tc_u_hnode *next;
  81. u32 handle;
  82. u32 prio;
  83. struct tc_u_common *tp_c;
  84. int refcnt;
  85. unsigned divisor;
  86. struct tc_u_knode *ht[1];
  87. };
  88. struct tc_u_common
  89. {
  90. struct tc_u_common *next;
  91. struct tc_u_hnode *hlist;
  92. struct Qdisc *q;
  93. int refcnt;
  94. u32 hgenerator;
  95. };
  96. static struct tcf_ext_map u32_ext_map = {
  97. .action = TCA_U32_ACT,
  98. .police = TCA_U32_POLICE
  99. };
  100. static struct tc_u_common *u32_list;
  101. static __inline__ unsigned u32_hash_fold(u32 key, struct tc_u32_sel *sel, u8 fshift)
  102. {
  103. unsigned h = (key & sel->hmask)>>fshift;
  104. return h;
  105. }
  106. static int u32_classify(struct sk_buff *skb, struct tcf_proto *tp, struct tcf_result *res)
  107. {
  108. struct {
  109. struct tc_u_knode *knode;
  110. u8 *ptr;
  111. } stack[TC_U32_MAXDEPTH];
  112. struct tc_u_hnode *ht = (struct tc_u_hnode*)tp->root;
  113. u8 *ptr = skb->nh.raw;
  114. struct tc_u_knode *n;
  115. int sdepth = 0;
  116. int off2 = 0;
  117. int sel = 0;
  118. #ifdef CONFIG_CLS_U32_PERF
  119. int j;
  120. #endif
  121. int i, r;
  122. next_ht:
  123. n = ht->ht[sel];
  124. next_knode:
  125. if (n) {
  126. struct tc_u32_key *key = n->sel.keys;
  127. #ifdef CONFIG_CLS_U32_PERF
  128. n->pf->rcnt +=1;
  129. j = 0;
  130. #endif
  131. #ifdef CONFIG_CLS_U32_MARK
  132. if ((skb->nfmark & n->mark.mask) != n->mark.val) {
  133. n = n->next;
  134. goto next_knode;
  135. } else {
  136. n->mark.success++;
  137. }
  138. #endif
  139. for (i = n->sel.nkeys; i>0; i--, key++) {
  140. if ((*(u32*)(ptr+key->off+(off2&key->offmask))^key->val)&key->mask) {
  141. n = n->next;
  142. goto next_knode;
  143. }
  144. #ifdef CONFIG_CLS_U32_PERF
  145. n->pf->kcnts[j] +=1;
  146. j++;
  147. #endif
  148. }
  149. if (n->ht_down == NULL) {
  150. check_terminal:
  151. if (n->sel.flags&TC_U32_TERMINAL) {
  152. *res = n->res;
  153. #ifdef CONFIG_NET_CLS_IND
  154. if (!tcf_match_indev(skb, n->indev)) {
  155. n = n->next;
  156. goto next_knode;
  157. }
  158. #endif
  159. #ifdef CONFIG_CLS_U32_PERF
  160. n->pf->rhit +=1;
  161. #endif
  162. r = tcf_exts_exec(skb, &n->exts, res);
  163. if (r < 0) {
  164. n = n->next;
  165. goto next_knode;
  166. }
  167. return r;
  168. }
  169. n = n->next;
  170. goto next_knode;
  171. }
  172. /* PUSH */
  173. if (sdepth >= TC_U32_MAXDEPTH)
  174. goto deadloop;
  175. stack[sdepth].knode = n;
  176. stack[sdepth].ptr = ptr;
  177. sdepth++;
  178. ht = n->ht_down;
  179. sel = 0;
  180. if (ht->divisor)
  181. sel = ht->divisor&u32_hash_fold(*(u32*)(ptr+n->sel.hoff), &n->sel,n->fshift);
  182. if (!(n->sel.flags&(TC_U32_VAROFFSET|TC_U32_OFFSET|TC_U32_EAT)))
  183. goto next_ht;
  184. if (n->sel.flags&(TC_U32_OFFSET|TC_U32_VAROFFSET)) {
  185. off2 = n->sel.off + 3;
  186. if (n->sel.flags&TC_U32_VAROFFSET)
  187. off2 += ntohs(n->sel.offmask & *(u16*)(ptr+n->sel.offoff)) >>n->sel.offshift;
  188. off2 &= ~3;
  189. }
  190. if (n->sel.flags&TC_U32_EAT) {
  191. ptr += off2;
  192. off2 = 0;
  193. }
  194. if (ptr < skb->tail)
  195. goto next_ht;
  196. }
  197. /* POP */
  198. if (sdepth--) {
  199. n = stack[sdepth].knode;
  200. ht = n->ht_up;
  201. ptr = stack[sdepth].ptr;
  202. goto check_terminal;
  203. }
  204. return -1;
  205. deadloop:
  206. if (net_ratelimit())
  207. printk("cls_u32: dead loop\n");
  208. return -1;
  209. }
  210. static __inline__ 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 __inline__ struct tc_u_knode *
  220. u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
  221. {
  222. unsigned 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. for (tp_c = u32_list; tp_c; tp_c = tp_c->next)
  264. if (tp_c->q == tp->q)
  265. break;
  266. root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
  267. if (root_ht == NULL)
  268. return -ENOBUFS;
  269. root_ht->divisor = 0;
  270. root_ht->refcnt++;
  271. root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
  272. root_ht->prio = tp->prio;
  273. if (tp_c == NULL) {
  274. tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
  275. if (tp_c == NULL) {
  276. kfree(root_ht);
  277. return -ENOBUFS;
  278. }
  279. tp_c->q = tp->q;
  280. tp_c->next = u32_list;
  281. u32_list = tp_c;
  282. }
  283. tp_c->refcnt++;
  284. root_ht->next = tp_c->hlist;
  285. tp_c->hlist = root_ht;
  286. root_ht->tp_c = tp_c;
  287. tp->root = root_ht;
  288. tp->data = tp_c;
  289. return 0;
  290. }
  291. static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
  292. {
  293. tcf_unbind_filter(tp, &n->res);
  294. tcf_exts_destroy(tp, &n->exts);
  295. if (n->ht_down)
  296. n->ht_down->refcnt--;
  297. #ifdef CONFIG_CLS_U32_PERF
  298. kfree(n->pf);
  299. #endif
  300. kfree(n);
  301. return 0;
  302. }
  303. static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
  304. {
  305. struct tc_u_knode **kp;
  306. struct tc_u_hnode *ht = key->ht_up;
  307. if (ht) {
  308. for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
  309. if (*kp == key) {
  310. tcf_tree_lock(tp);
  311. *kp = key->next;
  312. tcf_tree_unlock(tp);
  313. u32_destroy_key(tp, key);
  314. return 0;
  315. }
  316. }
  317. }
  318. BUG_TRAP(0);
  319. return 0;
  320. }
  321. static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  322. {
  323. struct tc_u_knode *n;
  324. unsigned h;
  325. for (h=0; h<=ht->divisor; h++) {
  326. while ((n = ht->ht[h]) != NULL) {
  327. ht->ht[h] = n->next;
  328. u32_destroy_key(tp, n);
  329. }
  330. }
  331. }
  332. static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  333. {
  334. struct tc_u_common *tp_c = tp->data;
  335. struct tc_u_hnode **hn;
  336. BUG_TRAP(!ht->refcnt);
  337. u32_clear_hnode(tp, ht);
  338. for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
  339. if (*hn == ht) {
  340. *hn = ht->next;
  341. kfree(ht);
  342. return 0;
  343. }
  344. }
  345. BUG_TRAP(0);
  346. return -ENOENT;
  347. }
  348. static void u32_destroy(struct tcf_proto *tp)
  349. {
  350. struct tc_u_common *tp_c = tp->data;
  351. struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
  352. BUG_TRAP(root_ht != NULL);
  353. if (root_ht && --root_ht->refcnt == 0)
  354. u32_destroy_hnode(tp, root_ht);
  355. if (--tp_c->refcnt == 0) {
  356. struct tc_u_hnode *ht;
  357. struct tc_u_common **tp_cp;
  358. for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
  359. if (*tp_cp == tp_c) {
  360. *tp_cp = tp_c->next;
  361. break;
  362. }
  363. }
  364. for (ht=tp_c->hlist; ht; ht = ht->next)
  365. u32_clear_hnode(tp, ht);
  366. while ((ht = tp_c->hlist) != NULL) {
  367. tp_c->hlist = ht->next;
  368. BUG_TRAP(ht->refcnt == 0);
  369. kfree(ht);
  370. };
  371. kfree(tp_c);
  372. }
  373. tp->data = NULL;
  374. }
  375. static int u32_delete(struct tcf_proto *tp, unsigned long arg)
  376. {
  377. struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
  378. if (ht == NULL)
  379. return 0;
  380. if (TC_U32_KEY(ht->handle))
  381. return u32_delete_key(tp, (struct tc_u_knode*)ht);
  382. if (tp->root == ht)
  383. return -EINVAL;
  384. if (--ht->refcnt == 0)
  385. u32_destroy_hnode(tp, ht);
  386. return 0;
  387. }
  388. static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
  389. {
  390. struct tc_u_knode *n;
  391. unsigned i = 0x7FF;
  392. for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
  393. if (i < TC_U32_NODE(n->handle))
  394. i = TC_U32_NODE(n->handle);
  395. i++;
  396. return handle|(i>0xFFF ? 0xFFF : i);
  397. }
  398. static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
  399. struct tc_u_hnode *ht,
  400. struct tc_u_knode *n, struct rtattr **tb,
  401. struct rtattr *est)
  402. {
  403. int err;
  404. struct tcf_exts e;
  405. err = tcf_exts_validate(tp, tb, est, &e, &u32_ext_map);
  406. if (err < 0)
  407. return err;
  408. err = -EINVAL;
  409. if (tb[TCA_U32_LINK-1]) {
  410. u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
  411. struct tc_u_hnode *ht_down = NULL;
  412. if (TC_U32_KEY(handle))
  413. goto errout;
  414. if (handle) {
  415. ht_down = u32_lookup_ht(ht->tp_c, handle);
  416. if (ht_down == NULL)
  417. goto errout;
  418. ht_down->refcnt++;
  419. }
  420. tcf_tree_lock(tp);
  421. ht_down = xchg(&n->ht_down, ht_down);
  422. tcf_tree_unlock(tp);
  423. if (ht_down)
  424. ht_down->refcnt--;
  425. }
  426. if (tb[TCA_U32_CLASSID-1]) {
  427. n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
  428. tcf_bind_filter(tp, &n->res, base);
  429. }
  430. #ifdef CONFIG_NET_CLS_IND
  431. if (tb[TCA_U32_INDEV-1]) {
  432. int err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV-1]);
  433. if (err < 0)
  434. goto errout;
  435. }
  436. #endif
  437. tcf_exts_change(tp, &n->exts, &e);
  438. return 0;
  439. errout:
  440. tcf_exts_destroy(tp, &e);
  441. return err;
  442. }
  443. static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  444. struct rtattr **tca,
  445. unsigned long *arg)
  446. {
  447. struct tc_u_common *tp_c = tp->data;
  448. struct tc_u_hnode *ht;
  449. struct tc_u_knode *n;
  450. struct tc_u32_sel *s;
  451. struct rtattr *opt = tca[TCA_OPTIONS-1];
  452. struct rtattr *tb[TCA_U32_MAX];
  453. u32 htid;
  454. int err;
  455. if (opt == NULL)
  456. return handle ? -EINVAL : 0;
  457. if (rtattr_parse_nested(tb, TCA_U32_MAX, opt) < 0)
  458. return -EINVAL;
  459. if ((n = (struct tc_u_knode*)*arg) != NULL) {
  460. if (TC_U32_KEY(n->handle) == 0)
  461. return -EINVAL;
  462. return u32_set_parms(tp, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
  463. }
  464. if (tb[TCA_U32_DIVISOR-1]) {
  465. unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
  466. if (--divisor > 0x100)
  467. return -EINVAL;
  468. if (TC_U32_KEY(handle))
  469. return -EINVAL;
  470. if (handle == 0) {
  471. handle = gen_new_htid(tp->data);
  472. if (handle == 0)
  473. return -ENOMEM;
  474. }
  475. ht = kzalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
  476. if (ht == NULL)
  477. return -ENOBUFS;
  478. ht->tp_c = tp_c;
  479. ht->refcnt = 0;
  480. ht->divisor = divisor;
  481. ht->handle = handle;
  482. ht->prio = tp->prio;
  483. ht->next = tp_c->hlist;
  484. tp_c->hlist = ht;
  485. *arg = (unsigned long)ht;
  486. return 0;
  487. }
  488. if (tb[TCA_U32_HASH-1]) {
  489. htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
  490. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  491. ht = tp->root;
  492. htid = ht->handle;
  493. } else {
  494. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  495. if (ht == NULL)
  496. return -EINVAL;
  497. }
  498. } else {
  499. ht = tp->root;
  500. htid = ht->handle;
  501. }
  502. if (ht->divisor < TC_U32_HASH(htid))
  503. return -EINVAL;
  504. if (handle) {
  505. if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
  506. return -EINVAL;
  507. handle = htid | TC_U32_NODE(handle);
  508. } else
  509. handle = gen_new_kid(ht, htid);
  510. if (tb[TCA_U32_SEL-1] == 0 ||
  511. RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
  512. return -EINVAL;
  513. s = RTA_DATA(tb[TCA_U32_SEL-1]);
  514. n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
  515. if (n == NULL)
  516. return -ENOBUFS;
  517. #ifdef CONFIG_CLS_U32_PERF
  518. n->pf = kzalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64), GFP_KERNEL);
  519. if (n->pf == NULL) {
  520. kfree(n);
  521. return -ENOBUFS;
  522. }
  523. #endif
  524. memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  525. n->ht_up = ht;
  526. n->handle = handle;
  527. {
  528. u8 i = 0;
  529. u32 mask = s->hmask;
  530. if (mask) {
  531. while (!(mask & 1)) {
  532. i++;
  533. mask>>=1;
  534. }
  535. }
  536. n->fshift = i;
  537. }
  538. #ifdef CONFIG_CLS_U32_MARK
  539. if (tb[TCA_U32_MARK-1]) {
  540. struct tc_u32_mark *mark;
  541. if (RTA_PAYLOAD(tb[TCA_U32_MARK-1]) < sizeof(struct tc_u32_mark)) {
  542. #ifdef CONFIG_CLS_U32_PERF
  543. kfree(n->pf);
  544. #endif
  545. kfree(n);
  546. return -EINVAL;
  547. }
  548. mark = RTA_DATA(tb[TCA_U32_MARK-1]);
  549. memcpy(&n->mark, mark, sizeof(struct tc_u32_mark));
  550. n->mark.success = 0;
  551. }
  552. #endif
  553. err = u32_set_parms(tp, base, ht, n, tb, tca[TCA_RATE-1]);
  554. if (err == 0) {
  555. struct tc_u_knode **ins;
  556. for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
  557. if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
  558. break;
  559. n->next = *ins;
  560. wmb();
  561. *ins = n;
  562. *arg = (unsigned long)n;
  563. return 0;
  564. }
  565. #ifdef CONFIG_CLS_U32_PERF
  566. kfree(n->pf);
  567. #endif
  568. kfree(n);
  569. return err;
  570. }
  571. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  572. {
  573. struct tc_u_common *tp_c = tp->data;
  574. struct tc_u_hnode *ht;
  575. struct tc_u_knode *n;
  576. unsigned h;
  577. if (arg->stop)
  578. return;
  579. for (ht = tp_c->hlist; ht; ht = ht->next) {
  580. if (ht->prio != tp->prio)
  581. continue;
  582. if (arg->count >= arg->skip) {
  583. if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
  584. arg->stop = 1;
  585. return;
  586. }
  587. }
  588. arg->count++;
  589. for (h = 0; h <= ht->divisor; h++) {
  590. for (n = ht->ht[h]; n; n = n->next) {
  591. if (arg->count < arg->skip) {
  592. arg->count++;
  593. continue;
  594. }
  595. if (arg->fn(tp, (unsigned long)n, arg) < 0) {
  596. arg->stop = 1;
  597. return;
  598. }
  599. arg->count++;
  600. }
  601. }
  602. }
  603. }
  604. static int u32_dump(struct tcf_proto *tp, unsigned long fh,
  605. struct sk_buff *skb, struct tcmsg *t)
  606. {
  607. struct tc_u_knode *n = (struct tc_u_knode*)fh;
  608. unsigned char *b = skb->tail;
  609. struct rtattr *rta;
  610. if (n == NULL)
  611. return skb->len;
  612. t->tcm_handle = n->handle;
  613. rta = (struct rtattr*)b;
  614. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  615. if (TC_U32_KEY(n->handle) == 0) {
  616. struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
  617. u32 divisor = ht->divisor+1;
  618. RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
  619. } else {
  620. RTA_PUT(skb, TCA_U32_SEL,
  621. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  622. &n->sel);
  623. if (n->ht_up) {
  624. u32 htid = n->handle & 0xFFFFF000;
  625. RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
  626. }
  627. if (n->res.classid)
  628. RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
  629. if (n->ht_down)
  630. RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
  631. #ifdef CONFIG_CLS_U32_MARK
  632. if (n->mark.val || n->mark.mask)
  633. RTA_PUT(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark);
  634. #endif
  635. if (tcf_exts_dump(skb, &n->exts, &u32_ext_map) < 0)
  636. goto rtattr_failure;
  637. #ifdef CONFIG_NET_CLS_IND
  638. if(strlen(n->indev))
  639. RTA_PUT(skb, TCA_U32_INDEV, IFNAMSIZ, n->indev);
  640. #endif
  641. #ifdef CONFIG_CLS_U32_PERF
  642. RTA_PUT(skb, TCA_U32_PCNT,
  643. sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
  644. n->pf);
  645. #endif
  646. }
  647. rta->rta_len = skb->tail - b;
  648. if (TC_U32_KEY(n->handle))
  649. if (tcf_exts_dump_stats(skb, &n->exts, &u32_ext_map) < 0)
  650. goto rtattr_failure;
  651. return skb->len;
  652. rtattr_failure:
  653. skb_trim(skb, b - skb->data);
  654. return -1;
  655. }
  656. static struct tcf_proto_ops cls_u32_ops = {
  657. .next = NULL,
  658. .kind = "u32",
  659. .classify = u32_classify,
  660. .init = u32_init,
  661. .destroy = u32_destroy,
  662. .get = u32_get,
  663. .put = u32_put,
  664. .change = u32_change,
  665. .delete = u32_delete,
  666. .walk = u32_walk,
  667. .dump = u32_dump,
  668. .owner = THIS_MODULE,
  669. };
  670. static int __init init_u32(void)
  671. {
  672. printk("u32 classifier\n");
  673. #ifdef CONFIG_CLS_U32_PERF
  674. printk(" Performance counters on\n");
  675. #endif
  676. #ifdef CONFIG_NET_CLS_POLICE
  677. printk(" OLD policer on \n");
  678. #endif
  679. #ifdef CONFIG_NET_CLS_IND
  680. printk(" input device check on \n");
  681. #endif
  682. #ifdef CONFIG_NET_CLS_ACT
  683. printk(" Actions configured \n");
  684. #endif
  685. return register_tcf_proto_ops(&cls_u32_ops);
  686. }
  687. static void __exit exit_u32(void)
  688. {
  689. unregister_tcf_proto_ops(&cls_u32_ops);
  690. }
  691. module_init(init_u32)
  692. module_exit(exit_u32)
  693. MODULE_LICENSE("GPL");