cls_u32.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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 = kmalloc(sizeof(*root_ht), GFP_KERNEL);
  267. if (root_ht == NULL)
  268. return -ENOBUFS;
  269. memset(root_ht, 0, sizeof(*root_ht));
  270. root_ht->divisor = 0;
  271. root_ht->refcnt++;
  272. root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
  273. root_ht->prio = tp->prio;
  274. if (tp_c == NULL) {
  275. tp_c = kmalloc(sizeof(*tp_c), GFP_KERNEL);
  276. if (tp_c == NULL) {
  277. kfree(root_ht);
  278. return -ENOBUFS;
  279. }
  280. memset(tp_c, 0, sizeof(*tp_c));
  281. tp_c->q = tp->q;
  282. tp_c->next = u32_list;
  283. u32_list = 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. BUG_TRAP(0);
  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 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. BUG_TRAP(!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. BUG_TRAP(0);
  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 = xchg(&tp->root, NULL);
  354. BUG_TRAP(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. struct tc_u_common **tp_cp;
  360. for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
  361. if (*tp_cp == tp_c) {
  362. *tp_cp = tp_c->next;
  363. break;
  364. }
  365. }
  366. for (ht=tp_c->hlist; ht; ht = ht->next)
  367. u32_clear_hnode(tp, ht);
  368. while ((ht = tp_c->hlist) != NULL) {
  369. tp_c->hlist = ht->next;
  370. BUG_TRAP(ht->refcnt == 0);
  371. kfree(ht);
  372. };
  373. kfree(tp_c);
  374. }
  375. tp->data = NULL;
  376. }
  377. static int u32_delete(struct tcf_proto *tp, unsigned long arg)
  378. {
  379. struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
  380. if (ht == NULL)
  381. return 0;
  382. if (TC_U32_KEY(ht->handle))
  383. return u32_delete_key(tp, (struct tc_u_knode*)ht);
  384. if (tp->root == ht)
  385. return -EINVAL;
  386. if (--ht->refcnt == 0)
  387. u32_destroy_hnode(tp, ht);
  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 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 int u32_set_parms(struct tcf_proto *tp, unsigned long base,
  401. struct tc_u_hnode *ht,
  402. struct tc_u_knode *n, struct rtattr **tb,
  403. struct rtattr *est)
  404. {
  405. int err;
  406. struct tcf_exts e;
  407. err = tcf_exts_validate(tp, tb, est, &e, &u32_ext_map);
  408. if (err < 0)
  409. return err;
  410. err = -EINVAL;
  411. if (tb[TCA_U32_LINK-1]) {
  412. u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
  413. struct tc_u_hnode *ht_down = NULL;
  414. if (TC_U32_KEY(handle))
  415. goto errout;
  416. if (handle) {
  417. ht_down = u32_lookup_ht(ht->tp_c, handle);
  418. if (ht_down == NULL)
  419. goto errout;
  420. ht_down->refcnt++;
  421. }
  422. tcf_tree_lock(tp);
  423. ht_down = xchg(&n->ht_down, ht_down);
  424. tcf_tree_unlock(tp);
  425. if (ht_down)
  426. ht_down->refcnt--;
  427. }
  428. if (tb[TCA_U32_CLASSID-1]) {
  429. n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
  430. tcf_bind_filter(tp, &n->res, base);
  431. }
  432. #ifdef CONFIG_NET_CLS_IND
  433. if (tb[TCA_U32_INDEV-1]) {
  434. int err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV-1]);
  435. if (err < 0)
  436. goto errout;
  437. }
  438. #endif
  439. tcf_exts_change(tp, &n->exts, &e);
  440. return 0;
  441. errout:
  442. tcf_exts_destroy(tp, &e);
  443. return err;
  444. }
  445. static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  446. struct rtattr **tca,
  447. unsigned long *arg)
  448. {
  449. struct tc_u_common *tp_c = tp->data;
  450. struct tc_u_hnode *ht;
  451. struct tc_u_knode *n;
  452. struct tc_u32_sel *s;
  453. struct rtattr *opt = tca[TCA_OPTIONS-1];
  454. struct rtattr *tb[TCA_U32_MAX];
  455. u32 htid;
  456. int err;
  457. if (opt == NULL)
  458. return handle ? -EINVAL : 0;
  459. if (rtattr_parse_nested(tb, TCA_U32_MAX, opt) < 0)
  460. return -EINVAL;
  461. if ((n = (struct tc_u_knode*)*arg) != NULL) {
  462. if (TC_U32_KEY(n->handle) == 0)
  463. return -EINVAL;
  464. return u32_set_parms(tp, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
  465. }
  466. if (tb[TCA_U32_DIVISOR-1]) {
  467. unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
  468. if (--divisor > 0x100)
  469. return -EINVAL;
  470. if (TC_U32_KEY(handle))
  471. return -EINVAL;
  472. if (handle == 0) {
  473. handle = gen_new_htid(tp->data);
  474. if (handle == 0)
  475. return -ENOMEM;
  476. }
  477. ht = kmalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
  478. if (ht == NULL)
  479. return -ENOBUFS;
  480. memset(ht, 0, sizeof(*ht) + divisor*sizeof(void*));
  481. ht->tp_c = tp_c;
  482. ht->refcnt = 0;
  483. ht->divisor = divisor;
  484. ht->handle = handle;
  485. ht->prio = tp->prio;
  486. ht->next = tp_c->hlist;
  487. tp_c->hlist = ht;
  488. *arg = (unsigned long)ht;
  489. return 0;
  490. }
  491. if (tb[TCA_U32_HASH-1]) {
  492. htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
  493. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  494. ht = tp->root;
  495. htid = ht->handle;
  496. } else {
  497. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  498. if (ht == NULL)
  499. return -EINVAL;
  500. }
  501. } else {
  502. ht = tp->root;
  503. htid = ht->handle;
  504. }
  505. if (ht->divisor < TC_U32_HASH(htid))
  506. return -EINVAL;
  507. if (handle) {
  508. if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
  509. return -EINVAL;
  510. handle = htid | TC_U32_NODE(handle);
  511. } else
  512. handle = gen_new_kid(ht, htid);
  513. if (tb[TCA_U32_SEL-1] == 0 ||
  514. RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
  515. return -EINVAL;
  516. s = RTA_DATA(tb[TCA_U32_SEL-1]);
  517. n = kmalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
  518. if (n == NULL)
  519. return -ENOBUFS;
  520. memset(n, 0, sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key));
  521. #ifdef CONFIG_CLS_U32_PERF
  522. n->pf = kmalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64), GFP_KERNEL);
  523. if (n->pf == NULL) {
  524. kfree(n);
  525. return -ENOBUFS;
  526. }
  527. memset(n->pf, 0, sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64));
  528. #endif
  529. memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  530. n->ht_up = ht;
  531. n->handle = handle;
  532. {
  533. u8 i = 0;
  534. u32 mask = s->hmask;
  535. if (mask) {
  536. while (!(mask & 1)) {
  537. i++;
  538. mask>>=1;
  539. }
  540. }
  541. n->fshift = i;
  542. }
  543. #ifdef CONFIG_CLS_U32_MARK
  544. if (tb[TCA_U32_MARK-1]) {
  545. struct tc_u32_mark *mark;
  546. if (RTA_PAYLOAD(tb[TCA_U32_MARK-1]) < sizeof(struct tc_u32_mark)) {
  547. #ifdef CONFIG_CLS_U32_PERF
  548. kfree(n->pf);
  549. #endif
  550. kfree(n);
  551. return -EINVAL;
  552. }
  553. mark = RTA_DATA(tb[TCA_U32_MARK-1]);
  554. memcpy(&n->mark, mark, sizeof(struct tc_u32_mark));
  555. n->mark.success = 0;
  556. }
  557. #endif
  558. err = u32_set_parms(tp, base, ht, n, tb, tca[TCA_RATE-1]);
  559. if (err == 0) {
  560. struct tc_u_knode **ins;
  561. for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
  562. if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
  563. break;
  564. n->next = *ins;
  565. wmb();
  566. *ins = n;
  567. *arg = (unsigned long)n;
  568. return 0;
  569. }
  570. #ifdef CONFIG_CLS_U32_PERF
  571. kfree(n->pf);
  572. #endif
  573. kfree(n);
  574. return err;
  575. }
  576. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  577. {
  578. struct tc_u_common *tp_c = tp->data;
  579. struct tc_u_hnode *ht;
  580. struct tc_u_knode *n;
  581. unsigned h;
  582. if (arg->stop)
  583. return;
  584. for (ht = tp_c->hlist; ht; ht = ht->next) {
  585. if (ht->prio != tp->prio)
  586. continue;
  587. if (arg->count >= arg->skip) {
  588. if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
  589. arg->stop = 1;
  590. return;
  591. }
  592. }
  593. arg->count++;
  594. for (h = 0; h <= ht->divisor; h++) {
  595. for (n = ht->ht[h]; n; n = n->next) {
  596. if (arg->count < arg->skip) {
  597. arg->count++;
  598. continue;
  599. }
  600. if (arg->fn(tp, (unsigned long)n, arg) < 0) {
  601. arg->stop = 1;
  602. return;
  603. }
  604. arg->count++;
  605. }
  606. }
  607. }
  608. }
  609. static int u32_dump(struct tcf_proto *tp, unsigned long fh,
  610. struct sk_buff *skb, struct tcmsg *t)
  611. {
  612. struct tc_u_knode *n = (struct tc_u_knode*)fh;
  613. unsigned char *b = skb->tail;
  614. struct rtattr *rta;
  615. if (n == NULL)
  616. return skb->len;
  617. t->tcm_handle = n->handle;
  618. rta = (struct rtattr*)b;
  619. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  620. if (TC_U32_KEY(n->handle) == 0) {
  621. struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
  622. u32 divisor = ht->divisor+1;
  623. RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
  624. } else {
  625. RTA_PUT(skb, TCA_U32_SEL,
  626. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  627. &n->sel);
  628. if (n->ht_up) {
  629. u32 htid = n->handle & 0xFFFFF000;
  630. RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
  631. }
  632. if (n->res.classid)
  633. RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
  634. if (n->ht_down)
  635. RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
  636. #ifdef CONFIG_CLS_U32_MARK
  637. if (n->mark.val || n->mark.mask)
  638. RTA_PUT(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark);
  639. #endif
  640. if (tcf_exts_dump(skb, &n->exts, &u32_ext_map) < 0)
  641. goto rtattr_failure;
  642. #ifdef CONFIG_NET_CLS_IND
  643. if(strlen(n->indev))
  644. RTA_PUT(skb, TCA_U32_INDEV, IFNAMSIZ, n->indev);
  645. #endif
  646. #ifdef CONFIG_CLS_U32_PERF
  647. RTA_PUT(skb, TCA_U32_PCNT,
  648. sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
  649. n->pf);
  650. #endif
  651. }
  652. rta->rta_len = skb->tail - b;
  653. if (TC_U32_KEY(n->handle))
  654. if (tcf_exts_dump_stats(skb, &n->exts, &u32_ext_map) < 0)
  655. goto rtattr_failure;
  656. return skb->len;
  657. rtattr_failure:
  658. skb_trim(skb, b - skb->data);
  659. return -1;
  660. }
  661. static struct tcf_proto_ops cls_u32_ops = {
  662. .next = NULL,
  663. .kind = "u32",
  664. .classify = u32_classify,
  665. .init = u32_init,
  666. .destroy = u32_destroy,
  667. .get = u32_get,
  668. .put = u32_put,
  669. .change = u32_change,
  670. .delete = u32_delete,
  671. .walk = u32_walk,
  672. .dump = u32_dump,
  673. .owner = THIS_MODULE,
  674. };
  675. static int __init init_u32(void)
  676. {
  677. printk("u32 classifier\n");
  678. #ifdef CONFIG_CLS_U32_PERF
  679. printk(" Perfomance counters on\n");
  680. #endif
  681. #ifdef CONFIG_NET_CLS_POLICE
  682. printk(" OLD policer on \n");
  683. #endif
  684. #ifdef CONFIG_NET_CLS_IND
  685. printk(" input device check on \n");
  686. #endif
  687. #ifdef CONFIG_NET_CLS_ACT
  688. printk(" 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");