cls_u32.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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/percpu.h>
  39. #include <linux/rtnetlink.h>
  40. #include <linux/skbuff.h>
  41. #include <linux/bitmap.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/hash.h>
  44. #include <net/netlink.h>
  45. #include <net/act_api.h>
  46. #include <net/pkt_cls.h>
  47. #include <linux/idr.h>
  48. struct tc_u_knode {
  49. struct tc_u_knode __rcu *next;
  50. u32 handle;
  51. struct tc_u_hnode __rcu *ht_up;
  52. struct tcf_exts exts;
  53. #ifdef CONFIG_NET_CLS_IND
  54. int ifindex;
  55. #endif
  56. u8 fshift;
  57. struct tcf_result res;
  58. struct tc_u_hnode __rcu *ht_down;
  59. #ifdef CONFIG_CLS_U32_PERF
  60. struct tc_u32_pcnt __percpu *pf;
  61. #endif
  62. u32 flags;
  63. #ifdef CONFIG_CLS_U32_MARK
  64. u32 val;
  65. u32 mask;
  66. u32 __percpu *pcpu_success;
  67. #endif
  68. struct tcf_proto *tp;
  69. union {
  70. struct work_struct work;
  71. struct rcu_head rcu;
  72. };
  73. /* The 'sel' field MUST be the last field in structure to allow for
  74. * tc_u32_keys allocated at end of structure.
  75. */
  76. struct tc_u32_sel sel;
  77. };
  78. struct tc_u_hnode {
  79. struct tc_u_hnode __rcu *next;
  80. u32 handle;
  81. u32 prio;
  82. struct tc_u_common *tp_c;
  83. int refcnt;
  84. unsigned int divisor;
  85. struct idr handle_idr;
  86. struct rcu_head rcu;
  87. /* The 'ht' field MUST be the last field in structure to allow for
  88. * more entries allocated at end of structure.
  89. */
  90. struct tc_u_knode __rcu *ht[1];
  91. };
  92. struct tc_u_common {
  93. struct tc_u_hnode __rcu *hlist;
  94. struct tcf_block *block;
  95. int refcnt;
  96. struct idr handle_idr;
  97. struct hlist_node hnode;
  98. struct rcu_head rcu;
  99. };
  100. static inline unsigned int u32_hash_fold(__be32 key,
  101. const struct tc_u32_sel *sel,
  102. u8 fshift)
  103. {
  104. unsigned int h = ntohl(key & sel->hmask) >> fshift;
  105. return h;
  106. }
  107. static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  108. struct tcf_result *res)
  109. {
  110. struct {
  111. struct tc_u_knode *knode;
  112. unsigned int off;
  113. } stack[TC_U32_MAXDEPTH];
  114. struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
  115. unsigned int off = skb_network_offset(skb);
  116. struct tc_u_knode *n;
  117. int sdepth = 0;
  118. int off2 = 0;
  119. int sel = 0;
  120. #ifdef CONFIG_CLS_U32_PERF
  121. int j;
  122. #endif
  123. int i, r;
  124. next_ht:
  125. n = rcu_dereference_bh(ht->ht[sel]);
  126. next_knode:
  127. if (n) {
  128. struct tc_u32_key *key = n->sel.keys;
  129. #ifdef CONFIG_CLS_U32_PERF
  130. __this_cpu_inc(n->pf->rcnt);
  131. j = 0;
  132. #endif
  133. if (tc_skip_sw(n->flags)) {
  134. n = rcu_dereference_bh(n->next);
  135. goto next_knode;
  136. }
  137. #ifdef CONFIG_CLS_U32_MARK
  138. if ((skb->mark & n->mask) != n->val) {
  139. n = rcu_dereference_bh(n->next);
  140. goto next_knode;
  141. } else {
  142. __this_cpu_inc(*n->pcpu_success);
  143. }
  144. #endif
  145. for (i = n->sel.nkeys; i > 0; i--, key++) {
  146. int toff = off + key->off + (off2 & key->offmask);
  147. __be32 *data, hdata;
  148. if (skb_headroom(skb) + toff > INT_MAX)
  149. goto out;
  150. data = skb_header_pointer(skb, toff, 4, &hdata);
  151. if (!data)
  152. goto out;
  153. if ((*data ^ key->val) & key->mask) {
  154. n = rcu_dereference_bh(n->next);
  155. goto next_knode;
  156. }
  157. #ifdef CONFIG_CLS_U32_PERF
  158. __this_cpu_inc(n->pf->kcnts[j]);
  159. j++;
  160. #endif
  161. }
  162. ht = rcu_dereference_bh(n->ht_down);
  163. if (!ht) {
  164. check_terminal:
  165. if (n->sel.flags & TC_U32_TERMINAL) {
  166. *res = n->res;
  167. #ifdef CONFIG_NET_CLS_IND
  168. if (!tcf_match_indev(skb, n->ifindex)) {
  169. n = rcu_dereference_bh(n->next);
  170. goto next_knode;
  171. }
  172. #endif
  173. #ifdef CONFIG_CLS_U32_PERF
  174. __this_cpu_inc(n->pf->rhit);
  175. #endif
  176. r = tcf_exts_exec(skb, &n->exts, res);
  177. if (r < 0) {
  178. n = rcu_dereference_bh(n->next);
  179. goto next_knode;
  180. }
  181. return r;
  182. }
  183. n = rcu_dereference_bh(n->next);
  184. goto next_knode;
  185. }
  186. /* PUSH */
  187. if (sdepth >= TC_U32_MAXDEPTH)
  188. goto deadloop;
  189. stack[sdepth].knode = n;
  190. stack[sdepth].off = off;
  191. sdepth++;
  192. ht = rcu_dereference_bh(n->ht_down);
  193. sel = 0;
  194. if (ht->divisor) {
  195. __be32 *data, hdata;
  196. data = skb_header_pointer(skb, off + n->sel.hoff, 4,
  197. &hdata);
  198. if (!data)
  199. goto out;
  200. sel = ht->divisor & u32_hash_fold(*data, &n->sel,
  201. n->fshift);
  202. }
  203. if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
  204. goto next_ht;
  205. if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
  206. off2 = n->sel.off + 3;
  207. if (n->sel.flags & TC_U32_VAROFFSET) {
  208. __be16 *data, hdata;
  209. data = skb_header_pointer(skb,
  210. off + n->sel.offoff,
  211. 2, &hdata);
  212. if (!data)
  213. goto out;
  214. off2 += ntohs(n->sel.offmask & *data) >>
  215. n->sel.offshift;
  216. }
  217. off2 &= ~3;
  218. }
  219. if (n->sel.flags & TC_U32_EAT) {
  220. off += off2;
  221. off2 = 0;
  222. }
  223. if (off < skb->len)
  224. goto next_ht;
  225. }
  226. /* POP */
  227. if (sdepth--) {
  228. n = stack[sdepth].knode;
  229. ht = rcu_dereference_bh(n->ht_up);
  230. off = stack[sdepth].off;
  231. goto check_terminal;
  232. }
  233. out:
  234. return -1;
  235. deadloop:
  236. net_warn_ratelimited("cls_u32: dead loop\n");
  237. return -1;
  238. }
  239. static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
  240. {
  241. struct tc_u_hnode *ht;
  242. for (ht = rtnl_dereference(tp_c->hlist);
  243. ht;
  244. ht = rtnl_dereference(ht->next))
  245. if (ht->handle == handle)
  246. break;
  247. return ht;
  248. }
  249. static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
  250. {
  251. unsigned int sel;
  252. struct tc_u_knode *n = NULL;
  253. sel = TC_U32_HASH(handle);
  254. if (sel > ht->divisor)
  255. goto out;
  256. for (n = rtnl_dereference(ht->ht[sel]);
  257. n;
  258. n = rtnl_dereference(n->next))
  259. if (n->handle == handle)
  260. break;
  261. out:
  262. return n;
  263. }
  264. static void *u32_get(struct tcf_proto *tp, u32 handle)
  265. {
  266. struct tc_u_hnode *ht;
  267. struct tc_u_common *tp_c = tp->data;
  268. if (TC_U32_HTID(handle) == TC_U32_ROOT)
  269. ht = rtnl_dereference(tp->root);
  270. else
  271. ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
  272. if (!ht)
  273. return NULL;
  274. if (TC_U32_KEY(handle) == 0)
  275. return ht;
  276. return u32_lookup_key(ht, handle);
  277. }
  278. static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
  279. {
  280. unsigned long idr_index;
  281. int err;
  282. /* This is only used inside rtnl lock it is safe to increment
  283. * without read _copy_ update semantics
  284. */
  285. err = idr_alloc_ext(&tp_c->handle_idr, ptr, &idr_index,
  286. 1, 0x7FF, GFP_KERNEL);
  287. if (err)
  288. return 0;
  289. return (u32)(idr_index | 0x800) << 20;
  290. }
  291. static struct hlist_head *tc_u_common_hash;
  292. #define U32_HASH_SHIFT 10
  293. #define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
  294. static unsigned int tc_u_hash(const struct tcf_proto *tp)
  295. {
  296. return hash_ptr(tp->chain->block, U32_HASH_SHIFT);
  297. }
  298. static struct tc_u_common *tc_u_common_find(const struct tcf_proto *tp)
  299. {
  300. struct tc_u_common *tc;
  301. unsigned int h;
  302. h = tc_u_hash(tp);
  303. hlist_for_each_entry(tc, &tc_u_common_hash[h], hnode) {
  304. if (tc->block == tp->chain->block)
  305. return tc;
  306. }
  307. return NULL;
  308. }
  309. static int u32_init(struct tcf_proto *tp)
  310. {
  311. struct tc_u_hnode *root_ht;
  312. struct tc_u_common *tp_c;
  313. unsigned int h;
  314. tp_c = tc_u_common_find(tp);
  315. root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
  316. if (root_ht == NULL)
  317. return -ENOBUFS;
  318. root_ht->refcnt++;
  319. root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000;
  320. root_ht->prio = tp->prio;
  321. idr_init(&root_ht->handle_idr);
  322. if (tp_c == NULL) {
  323. tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
  324. if (tp_c == NULL) {
  325. kfree(root_ht);
  326. return -ENOBUFS;
  327. }
  328. tp_c->block = tp->chain->block;
  329. INIT_HLIST_NODE(&tp_c->hnode);
  330. idr_init(&tp_c->handle_idr);
  331. h = tc_u_hash(tp);
  332. hlist_add_head(&tp_c->hnode, &tc_u_common_hash[h]);
  333. }
  334. tp_c->refcnt++;
  335. RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
  336. rcu_assign_pointer(tp_c->hlist, root_ht);
  337. root_ht->tp_c = tp_c;
  338. rcu_assign_pointer(tp->root, root_ht);
  339. tp->data = tp_c;
  340. return 0;
  341. }
  342. static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n,
  343. bool free_pf)
  344. {
  345. tcf_exts_destroy(&n->exts);
  346. tcf_exts_put_net(&n->exts);
  347. if (n->ht_down)
  348. n->ht_down->refcnt--;
  349. #ifdef CONFIG_CLS_U32_PERF
  350. if (free_pf)
  351. free_percpu(n->pf);
  352. #endif
  353. #ifdef CONFIG_CLS_U32_MARK
  354. if (free_pf)
  355. free_percpu(n->pcpu_success);
  356. #endif
  357. kfree(n);
  358. return 0;
  359. }
  360. /* u32_delete_key_rcu should be called when free'ing a copied
  361. * version of a tc_u_knode obtained from u32_init_knode(). When
  362. * copies are obtained from u32_init_knode() the statistics are
  363. * shared between the old and new copies to allow readers to
  364. * continue to update the statistics during the copy. To support
  365. * this the u32_delete_key_rcu variant does not free the percpu
  366. * statistics.
  367. */
  368. static void u32_delete_key_work(struct work_struct *work)
  369. {
  370. struct tc_u_knode *key = container_of(work, struct tc_u_knode, work);
  371. rtnl_lock();
  372. u32_destroy_key(key->tp, key, false);
  373. rtnl_unlock();
  374. }
  375. static void u32_delete_key_rcu(struct rcu_head *rcu)
  376. {
  377. struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
  378. INIT_WORK(&key->work, u32_delete_key_work);
  379. tcf_queue_work(&key->work);
  380. }
  381. /* u32_delete_key_freepf_rcu is the rcu callback variant
  382. * that free's the entire structure including the statistics
  383. * percpu variables. Only use this if the key is not a copy
  384. * returned by u32_init_knode(). See u32_delete_key_rcu()
  385. * for the variant that should be used with keys return from
  386. * u32_init_knode()
  387. */
  388. static void u32_delete_key_freepf_work(struct work_struct *work)
  389. {
  390. struct tc_u_knode *key = container_of(work, struct tc_u_knode, work);
  391. rtnl_lock();
  392. u32_destroy_key(key->tp, key, true);
  393. rtnl_unlock();
  394. }
  395. static void u32_delete_key_freepf_rcu(struct rcu_head *rcu)
  396. {
  397. struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
  398. INIT_WORK(&key->work, u32_delete_key_freepf_work);
  399. tcf_queue_work(&key->work);
  400. }
  401. static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
  402. {
  403. struct tc_u_knode __rcu **kp;
  404. struct tc_u_knode *pkp;
  405. struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
  406. if (ht) {
  407. kp = &ht->ht[TC_U32_HASH(key->handle)];
  408. for (pkp = rtnl_dereference(*kp); pkp;
  409. kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
  410. if (pkp == key) {
  411. RCU_INIT_POINTER(*kp, key->next);
  412. tcf_unbind_filter(tp, &key->res);
  413. tcf_exts_get_net(&key->exts);
  414. call_rcu(&key->rcu, u32_delete_key_freepf_rcu);
  415. return 0;
  416. }
  417. }
  418. }
  419. WARN_ON(1);
  420. return 0;
  421. }
  422. static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h)
  423. {
  424. struct tcf_block *block = tp->chain->block;
  425. struct tc_cls_u32_offload cls_u32 = {};
  426. tc_cls_common_offload_init(&cls_u32.common, tp);
  427. cls_u32.command = TC_CLSU32_DELETE_HNODE;
  428. cls_u32.hnode.divisor = h->divisor;
  429. cls_u32.hnode.handle = h->handle;
  430. cls_u32.hnode.prio = h->prio;
  431. tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
  432. }
  433. static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
  434. u32 flags)
  435. {
  436. struct tcf_block *block = tp->chain->block;
  437. struct tc_cls_u32_offload cls_u32 = {};
  438. bool skip_sw = tc_skip_sw(flags);
  439. bool offloaded = false;
  440. int err;
  441. tc_cls_common_offload_init(&cls_u32.common, tp);
  442. cls_u32.command = TC_CLSU32_NEW_HNODE;
  443. cls_u32.hnode.divisor = h->divisor;
  444. cls_u32.hnode.handle = h->handle;
  445. cls_u32.hnode.prio = h->prio;
  446. err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
  447. if (err < 0) {
  448. u32_clear_hw_hnode(tp, h);
  449. return err;
  450. } else if (err > 0) {
  451. offloaded = true;
  452. }
  453. if (skip_sw && !offloaded)
  454. return -EINVAL;
  455. return 0;
  456. }
  457. static void u32_remove_hw_knode(struct tcf_proto *tp, u32 handle)
  458. {
  459. struct tcf_block *block = tp->chain->block;
  460. struct tc_cls_u32_offload cls_u32 = {};
  461. tc_cls_common_offload_init(&cls_u32.common, tp);
  462. cls_u32.command = TC_CLSU32_DELETE_KNODE;
  463. cls_u32.knode.handle = handle;
  464. tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
  465. }
  466. static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
  467. u32 flags)
  468. {
  469. struct tcf_block *block = tp->chain->block;
  470. struct tc_cls_u32_offload cls_u32 = {};
  471. bool skip_sw = tc_skip_sw(flags);
  472. int err;
  473. tc_cls_common_offload_init(&cls_u32.common, tp);
  474. cls_u32.command = TC_CLSU32_REPLACE_KNODE;
  475. cls_u32.knode.handle = n->handle;
  476. cls_u32.knode.fshift = n->fshift;
  477. #ifdef CONFIG_CLS_U32_MARK
  478. cls_u32.knode.val = n->val;
  479. cls_u32.knode.mask = n->mask;
  480. #else
  481. cls_u32.knode.val = 0;
  482. cls_u32.knode.mask = 0;
  483. #endif
  484. cls_u32.knode.sel = &n->sel;
  485. cls_u32.knode.exts = &n->exts;
  486. if (n->ht_down)
  487. cls_u32.knode.link_handle = n->ht_down->handle;
  488. err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
  489. if (err < 0) {
  490. u32_remove_hw_knode(tp, n->handle);
  491. return err;
  492. } else if (err > 0) {
  493. n->flags |= TCA_CLS_FLAGS_IN_HW;
  494. }
  495. if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
  496. return -EINVAL;
  497. return 0;
  498. }
  499. static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  500. {
  501. struct tc_u_knode *n;
  502. unsigned int h;
  503. for (h = 0; h <= ht->divisor; h++) {
  504. while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
  505. RCU_INIT_POINTER(ht->ht[h],
  506. rtnl_dereference(n->next));
  507. tcf_unbind_filter(tp, &n->res);
  508. u32_remove_hw_knode(tp, n->handle);
  509. idr_remove_ext(&ht->handle_idr, n->handle);
  510. if (tcf_exts_get_net(&n->exts))
  511. call_rcu(&n->rcu, u32_delete_key_freepf_rcu);
  512. else
  513. u32_destroy_key(n->tp, n, true);
  514. }
  515. }
  516. }
  517. static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  518. {
  519. struct tc_u_common *tp_c = tp->data;
  520. struct tc_u_hnode __rcu **hn;
  521. struct tc_u_hnode *phn;
  522. WARN_ON(ht->refcnt);
  523. u32_clear_hnode(tp, ht);
  524. hn = &tp_c->hlist;
  525. for (phn = rtnl_dereference(*hn);
  526. phn;
  527. hn = &phn->next, phn = rtnl_dereference(*hn)) {
  528. if (phn == ht) {
  529. u32_clear_hw_hnode(tp, ht);
  530. idr_destroy(&ht->handle_idr);
  531. idr_remove_ext(&tp_c->handle_idr, ht->handle);
  532. RCU_INIT_POINTER(*hn, ht->next);
  533. kfree_rcu(ht, rcu);
  534. return 0;
  535. }
  536. }
  537. return -ENOENT;
  538. }
  539. static bool ht_empty(struct tc_u_hnode *ht)
  540. {
  541. unsigned int h;
  542. for (h = 0; h <= ht->divisor; h++)
  543. if (rcu_access_pointer(ht->ht[h]))
  544. return false;
  545. return true;
  546. }
  547. static void u32_destroy(struct tcf_proto *tp)
  548. {
  549. struct tc_u_common *tp_c = tp->data;
  550. struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
  551. WARN_ON(root_ht == NULL);
  552. if (root_ht && --root_ht->refcnt == 0)
  553. u32_destroy_hnode(tp, root_ht);
  554. if (--tp_c->refcnt == 0) {
  555. struct tc_u_hnode *ht;
  556. hlist_del(&tp_c->hnode);
  557. for (ht = rtnl_dereference(tp_c->hlist);
  558. ht;
  559. ht = rtnl_dereference(ht->next)) {
  560. ht->refcnt--;
  561. u32_clear_hnode(tp, ht);
  562. }
  563. while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
  564. RCU_INIT_POINTER(tp_c->hlist, ht->next);
  565. kfree_rcu(ht, rcu);
  566. }
  567. idr_destroy(&tp_c->handle_idr);
  568. kfree(tp_c);
  569. }
  570. tp->data = NULL;
  571. }
  572. static int u32_delete(struct tcf_proto *tp, void *arg, bool *last)
  573. {
  574. struct tc_u_hnode *ht = arg;
  575. struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
  576. struct tc_u_common *tp_c = tp->data;
  577. int ret = 0;
  578. if (ht == NULL)
  579. goto out;
  580. if (TC_U32_KEY(ht->handle)) {
  581. u32_remove_hw_knode(tp, ht->handle);
  582. ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
  583. goto out;
  584. }
  585. if (root_ht == ht)
  586. return -EINVAL;
  587. if (ht->refcnt == 1) {
  588. ht->refcnt--;
  589. u32_destroy_hnode(tp, ht);
  590. } else {
  591. return -EBUSY;
  592. }
  593. out:
  594. *last = true;
  595. if (root_ht) {
  596. if (root_ht->refcnt > 1) {
  597. *last = false;
  598. goto ret;
  599. }
  600. if (root_ht->refcnt == 1) {
  601. if (!ht_empty(root_ht)) {
  602. *last = false;
  603. goto ret;
  604. }
  605. }
  606. }
  607. if (tp_c->refcnt > 1) {
  608. *last = false;
  609. goto ret;
  610. }
  611. if (tp_c->refcnt == 1) {
  612. struct tc_u_hnode *ht;
  613. for (ht = rtnl_dereference(tp_c->hlist);
  614. ht;
  615. ht = rtnl_dereference(ht->next))
  616. if (!ht_empty(ht)) {
  617. *last = false;
  618. break;
  619. }
  620. }
  621. ret:
  622. return ret;
  623. }
  624. static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
  625. {
  626. unsigned long idr_index;
  627. u32 start = htid | 0x800;
  628. u32 max = htid | 0xFFF;
  629. u32 min = htid;
  630. if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
  631. start, max + 1, GFP_KERNEL)) {
  632. if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
  633. min + 1, max + 1, GFP_KERNEL))
  634. return max;
  635. }
  636. return (u32)idr_index;
  637. }
  638. static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
  639. [TCA_U32_CLASSID] = { .type = NLA_U32 },
  640. [TCA_U32_HASH] = { .type = NLA_U32 },
  641. [TCA_U32_LINK] = { .type = NLA_U32 },
  642. [TCA_U32_DIVISOR] = { .type = NLA_U32 },
  643. [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
  644. [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
  645. [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
  646. [TCA_U32_FLAGS] = { .type = NLA_U32 },
  647. };
  648. static int u32_set_parms(struct net *net, struct tcf_proto *tp,
  649. unsigned long base, struct tc_u_hnode *ht,
  650. struct tc_u_knode *n, struct nlattr **tb,
  651. struct nlattr *est, bool ovr)
  652. {
  653. int err;
  654. err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr);
  655. if (err < 0)
  656. return err;
  657. if (tb[TCA_U32_LINK]) {
  658. u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
  659. struct tc_u_hnode *ht_down = NULL, *ht_old;
  660. if (TC_U32_KEY(handle))
  661. return -EINVAL;
  662. if (handle) {
  663. ht_down = u32_lookup_ht(ht->tp_c, handle);
  664. if (ht_down == NULL)
  665. return -EINVAL;
  666. ht_down->refcnt++;
  667. }
  668. ht_old = rtnl_dereference(n->ht_down);
  669. rcu_assign_pointer(n->ht_down, ht_down);
  670. if (ht_old)
  671. ht_old->refcnt--;
  672. }
  673. if (tb[TCA_U32_CLASSID]) {
  674. n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
  675. tcf_bind_filter(tp, &n->res, base);
  676. }
  677. #ifdef CONFIG_NET_CLS_IND
  678. if (tb[TCA_U32_INDEV]) {
  679. int ret;
  680. ret = tcf_change_indev(net, tb[TCA_U32_INDEV]);
  681. if (ret < 0)
  682. return -EINVAL;
  683. n->ifindex = ret;
  684. }
  685. #endif
  686. return 0;
  687. }
  688. static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
  689. struct tc_u_knode *n)
  690. {
  691. struct tc_u_knode __rcu **ins;
  692. struct tc_u_knode *pins;
  693. struct tc_u_hnode *ht;
  694. if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
  695. ht = rtnl_dereference(tp->root);
  696. else
  697. ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
  698. ins = &ht->ht[TC_U32_HASH(n->handle)];
  699. /* The node must always exist for it to be replaced if this is not the
  700. * case then something went very wrong elsewhere.
  701. */
  702. for (pins = rtnl_dereference(*ins); ;
  703. ins = &pins->next, pins = rtnl_dereference(*ins))
  704. if (pins->handle == n->handle)
  705. break;
  706. idr_replace_ext(&ht->handle_idr, n, n->handle);
  707. RCU_INIT_POINTER(n->next, pins->next);
  708. rcu_assign_pointer(*ins, n);
  709. }
  710. static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
  711. struct tc_u_knode *n)
  712. {
  713. struct tc_u_knode *new;
  714. struct tc_u32_sel *s = &n->sel;
  715. new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
  716. GFP_KERNEL);
  717. if (!new)
  718. return NULL;
  719. RCU_INIT_POINTER(new->next, n->next);
  720. new->handle = n->handle;
  721. RCU_INIT_POINTER(new->ht_up, n->ht_up);
  722. #ifdef CONFIG_NET_CLS_IND
  723. new->ifindex = n->ifindex;
  724. #endif
  725. new->fshift = n->fshift;
  726. new->res = n->res;
  727. new->flags = n->flags;
  728. RCU_INIT_POINTER(new->ht_down, n->ht_down);
  729. /* bump reference count as long as we hold pointer to structure */
  730. if (new->ht_down)
  731. new->ht_down->refcnt++;
  732. #ifdef CONFIG_CLS_U32_PERF
  733. /* Statistics may be incremented by readers during update
  734. * so we must keep them in tact. When the node is later destroyed
  735. * a special destroy call must be made to not free the pf memory.
  736. */
  737. new->pf = n->pf;
  738. #endif
  739. #ifdef CONFIG_CLS_U32_MARK
  740. new->val = n->val;
  741. new->mask = n->mask;
  742. /* Similarly success statistics must be moved as pointers */
  743. new->pcpu_success = n->pcpu_success;
  744. #endif
  745. new->tp = tp;
  746. memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  747. if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
  748. kfree(new);
  749. return NULL;
  750. }
  751. return new;
  752. }
  753. static int u32_change(struct net *net, struct sk_buff *in_skb,
  754. struct tcf_proto *tp, unsigned long base, u32 handle,
  755. struct nlattr **tca, void **arg, bool ovr)
  756. {
  757. struct tc_u_common *tp_c = tp->data;
  758. struct tc_u_hnode *ht;
  759. struct tc_u_knode *n;
  760. struct tc_u32_sel *s;
  761. struct nlattr *opt = tca[TCA_OPTIONS];
  762. struct nlattr *tb[TCA_U32_MAX + 1];
  763. u32 htid, flags = 0;
  764. int err;
  765. #ifdef CONFIG_CLS_U32_PERF
  766. size_t size;
  767. #endif
  768. if (opt == NULL)
  769. return handle ? -EINVAL : 0;
  770. err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, NULL);
  771. if (err < 0)
  772. return err;
  773. if (tb[TCA_U32_FLAGS]) {
  774. flags = nla_get_u32(tb[TCA_U32_FLAGS]);
  775. if (!tc_flags_valid(flags))
  776. return -EINVAL;
  777. }
  778. n = *arg;
  779. if (n) {
  780. struct tc_u_knode *new;
  781. if (TC_U32_KEY(n->handle) == 0)
  782. return -EINVAL;
  783. if (n->flags != flags)
  784. return -EINVAL;
  785. new = u32_init_knode(tp, n);
  786. if (!new)
  787. return -ENOMEM;
  788. err = u32_set_parms(net, tp, base,
  789. rtnl_dereference(n->ht_up), new, tb,
  790. tca[TCA_RATE], ovr);
  791. if (err) {
  792. u32_destroy_key(tp, new, false);
  793. return err;
  794. }
  795. err = u32_replace_hw_knode(tp, new, flags);
  796. if (err) {
  797. u32_destroy_key(tp, new, false);
  798. return err;
  799. }
  800. if (!tc_in_hw(new->flags))
  801. new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  802. u32_replace_knode(tp, tp_c, new);
  803. tcf_unbind_filter(tp, &n->res);
  804. tcf_exts_get_net(&n->exts);
  805. call_rcu(&n->rcu, u32_delete_key_rcu);
  806. return 0;
  807. }
  808. if (tb[TCA_U32_DIVISOR]) {
  809. unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
  810. if (--divisor > 0x100)
  811. return -EINVAL;
  812. if (TC_U32_KEY(handle))
  813. return -EINVAL;
  814. ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
  815. if (ht == NULL)
  816. return -ENOBUFS;
  817. if (handle == 0) {
  818. handle = gen_new_htid(tp->data, ht);
  819. if (handle == 0) {
  820. kfree(ht);
  821. return -ENOMEM;
  822. }
  823. } else {
  824. err = idr_alloc_ext(&tp_c->handle_idr, ht, NULL,
  825. handle, handle + 1, GFP_KERNEL);
  826. if (err) {
  827. kfree(ht);
  828. return err;
  829. }
  830. }
  831. ht->tp_c = tp_c;
  832. ht->refcnt = 1;
  833. ht->divisor = divisor;
  834. ht->handle = handle;
  835. ht->prio = tp->prio;
  836. idr_init(&ht->handle_idr);
  837. err = u32_replace_hw_hnode(tp, ht, flags);
  838. if (err) {
  839. idr_remove_ext(&tp_c->handle_idr, handle);
  840. kfree(ht);
  841. return err;
  842. }
  843. RCU_INIT_POINTER(ht->next, tp_c->hlist);
  844. rcu_assign_pointer(tp_c->hlist, ht);
  845. *arg = ht;
  846. return 0;
  847. }
  848. if (tb[TCA_U32_HASH]) {
  849. htid = nla_get_u32(tb[TCA_U32_HASH]);
  850. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  851. ht = rtnl_dereference(tp->root);
  852. htid = ht->handle;
  853. } else {
  854. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  855. if (ht == NULL)
  856. return -EINVAL;
  857. }
  858. } else {
  859. ht = rtnl_dereference(tp->root);
  860. htid = ht->handle;
  861. }
  862. if (ht->divisor < TC_U32_HASH(htid))
  863. return -EINVAL;
  864. if (handle) {
  865. if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
  866. return -EINVAL;
  867. handle = htid | TC_U32_NODE(handle);
  868. err = idr_alloc_ext(&ht->handle_idr, NULL, NULL,
  869. handle, handle + 1,
  870. GFP_KERNEL);
  871. if (err)
  872. return err;
  873. } else
  874. handle = gen_new_kid(ht, htid);
  875. if (tb[TCA_U32_SEL] == NULL) {
  876. err = -EINVAL;
  877. goto erridr;
  878. }
  879. s = nla_data(tb[TCA_U32_SEL]);
  880. n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
  881. if (n == NULL) {
  882. err = -ENOBUFS;
  883. goto erridr;
  884. }
  885. #ifdef CONFIG_CLS_U32_PERF
  886. size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
  887. n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
  888. if (!n->pf) {
  889. err = -ENOBUFS;
  890. goto errfree;
  891. }
  892. #endif
  893. memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  894. RCU_INIT_POINTER(n->ht_up, ht);
  895. n->handle = handle;
  896. n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
  897. n->flags = flags;
  898. n->tp = tp;
  899. err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
  900. if (err < 0)
  901. goto errout;
  902. #ifdef CONFIG_CLS_U32_MARK
  903. n->pcpu_success = alloc_percpu(u32);
  904. if (!n->pcpu_success) {
  905. err = -ENOMEM;
  906. goto errout;
  907. }
  908. if (tb[TCA_U32_MARK]) {
  909. struct tc_u32_mark *mark;
  910. mark = nla_data(tb[TCA_U32_MARK]);
  911. n->val = mark->val;
  912. n->mask = mark->mask;
  913. }
  914. #endif
  915. err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr);
  916. if (err == 0) {
  917. struct tc_u_knode __rcu **ins;
  918. struct tc_u_knode *pins;
  919. err = u32_replace_hw_knode(tp, n, flags);
  920. if (err)
  921. goto errhw;
  922. if (!tc_in_hw(n->flags))
  923. n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  924. ins = &ht->ht[TC_U32_HASH(handle)];
  925. for (pins = rtnl_dereference(*ins); pins;
  926. ins = &pins->next, pins = rtnl_dereference(*ins))
  927. if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
  928. break;
  929. RCU_INIT_POINTER(n->next, pins);
  930. rcu_assign_pointer(*ins, n);
  931. *arg = n;
  932. return 0;
  933. }
  934. errhw:
  935. #ifdef CONFIG_CLS_U32_MARK
  936. free_percpu(n->pcpu_success);
  937. #endif
  938. errout:
  939. tcf_exts_destroy(&n->exts);
  940. #ifdef CONFIG_CLS_U32_PERF
  941. errfree:
  942. free_percpu(n->pf);
  943. #endif
  944. kfree(n);
  945. erridr:
  946. idr_remove_ext(&ht->handle_idr, handle);
  947. return err;
  948. }
  949. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  950. {
  951. struct tc_u_common *tp_c = tp->data;
  952. struct tc_u_hnode *ht;
  953. struct tc_u_knode *n;
  954. unsigned int h;
  955. if (arg->stop)
  956. return;
  957. for (ht = rtnl_dereference(tp_c->hlist);
  958. ht;
  959. ht = rtnl_dereference(ht->next)) {
  960. if (ht->prio != tp->prio)
  961. continue;
  962. if (arg->count >= arg->skip) {
  963. if (arg->fn(tp, ht, arg) < 0) {
  964. arg->stop = 1;
  965. return;
  966. }
  967. }
  968. arg->count++;
  969. for (h = 0; h <= ht->divisor; h++) {
  970. for (n = rtnl_dereference(ht->ht[h]);
  971. n;
  972. n = rtnl_dereference(n->next)) {
  973. if (arg->count < arg->skip) {
  974. arg->count++;
  975. continue;
  976. }
  977. if (arg->fn(tp, n, arg) < 0) {
  978. arg->stop = 1;
  979. return;
  980. }
  981. arg->count++;
  982. }
  983. }
  984. }
  985. }
  986. static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
  987. {
  988. struct tc_u_knode *n = fh;
  989. if (n && n->res.classid == classid)
  990. n->res.class = cl;
  991. }
  992. static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
  993. struct sk_buff *skb, struct tcmsg *t)
  994. {
  995. struct tc_u_knode *n = fh;
  996. struct tc_u_hnode *ht_up, *ht_down;
  997. struct nlattr *nest;
  998. if (n == NULL)
  999. return skb->len;
  1000. t->tcm_handle = n->handle;
  1001. nest = nla_nest_start(skb, TCA_OPTIONS);
  1002. if (nest == NULL)
  1003. goto nla_put_failure;
  1004. if (TC_U32_KEY(n->handle) == 0) {
  1005. struct tc_u_hnode *ht = fh;
  1006. u32 divisor = ht->divisor + 1;
  1007. if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
  1008. goto nla_put_failure;
  1009. } else {
  1010. #ifdef CONFIG_CLS_U32_PERF
  1011. struct tc_u32_pcnt *gpf;
  1012. int cpu;
  1013. #endif
  1014. if (nla_put(skb, TCA_U32_SEL,
  1015. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  1016. &n->sel))
  1017. goto nla_put_failure;
  1018. ht_up = rtnl_dereference(n->ht_up);
  1019. if (ht_up) {
  1020. u32 htid = n->handle & 0xFFFFF000;
  1021. if (nla_put_u32(skb, TCA_U32_HASH, htid))
  1022. goto nla_put_failure;
  1023. }
  1024. if (n->res.classid &&
  1025. nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
  1026. goto nla_put_failure;
  1027. ht_down = rtnl_dereference(n->ht_down);
  1028. if (ht_down &&
  1029. nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
  1030. goto nla_put_failure;
  1031. if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
  1032. goto nla_put_failure;
  1033. #ifdef CONFIG_CLS_U32_MARK
  1034. if ((n->val || n->mask)) {
  1035. struct tc_u32_mark mark = {.val = n->val,
  1036. .mask = n->mask,
  1037. .success = 0};
  1038. int cpum;
  1039. for_each_possible_cpu(cpum) {
  1040. __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
  1041. mark.success += cnt;
  1042. }
  1043. if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
  1044. goto nla_put_failure;
  1045. }
  1046. #endif
  1047. if (tcf_exts_dump(skb, &n->exts) < 0)
  1048. goto nla_put_failure;
  1049. #ifdef CONFIG_NET_CLS_IND
  1050. if (n->ifindex) {
  1051. struct net_device *dev;
  1052. dev = __dev_get_by_index(net, n->ifindex);
  1053. if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
  1054. goto nla_put_failure;
  1055. }
  1056. #endif
  1057. #ifdef CONFIG_CLS_U32_PERF
  1058. gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
  1059. n->sel.nkeys * sizeof(u64),
  1060. GFP_KERNEL);
  1061. if (!gpf)
  1062. goto nla_put_failure;
  1063. for_each_possible_cpu(cpu) {
  1064. int i;
  1065. struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
  1066. gpf->rcnt += pf->rcnt;
  1067. gpf->rhit += pf->rhit;
  1068. for (i = 0; i < n->sel.nkeys; i++)
  1069. gpf->kcnts[i] += pf->kcnts[i];
  1070. }
  1071. if (nla_put_64bit(skb, TCA_U32_PCNT,
  1072. sizeof(struct tc_u32_pcnt) +
  1073. n->sel.nkeys * sizeof(u64),
  1074. gpf, TCA_U32_PAD)) {
  1075. kfree(gpf);
  1076. goto nla_put_failure;
  1077. }
  1078. kfree(gpf);
  1079. #endif
  1080. }
  1081. nla_nest_end(skb, nest);
  1082. if (TC_U32_KEY(n->handle))
  1083. if (tcf_exts_dump_stats(skb, &n->exts) < 0)
  1084. goto nla_put_failure;
  1085. return skb->len;
  1086. nla_put_failure:
  1087. nla_nest_cancel(skb, nest);
  1088. return -1;
  1089. }
  1090. static struct tcf_proto_ops cls_u32_ops __read_mostly = {
  1091. .kind = "u32",
  1092. .classify = u32_classify,
  1093. .init = u32_init,
  1094. .destroy = u32_destroy,
  1095. .get = u32_get,
  1096. .change = u32_change,
  1097. .delete = u32_delete,
  1098. .walk = u32_walk,
  1099. .dump = u32_dump,
  1100. .bind_class = u32_bind_class,
  1101. .owner = THIS_MODULE,
  1102. };
  1103. static int __init init_u32(void)
  1104. {
  1105. int i, ret;
  1106. pr_info("u32 classifier\n");
  1107. #ifdef CONFIG_CLS_U32_PERF
  1108. pr_info(" Performance counters on\n");
  1109. #endif
  1110. #ifdef CONFIG_NET_CLS_IND
  1111. pr_info(" input device check on\n");
  1112. #endif
  1113. #ifdef CONFIG_NET_CLS_ACT
  1114. pr_info(" Actions configured\n");
  1115. #endif
  1116. tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
  1117. sizeof(struct hlist_head),
  1118. GFP_KERNEL);
  1119. if (!tc_u_common_hash)
  1120. return -ENOMEM;
  1121. for (i = 0; i < U32_HASH_SIZE; i++)
  1122. INIT_HLIST_HEAD(&tc_u_common_hash[i]);
  1123. ret = register_tcf_proto_ops(&cls_u32_ops);
  1124. if (ret)
  1125. kvfree(tc_u_common_hash);
  1126. return ret;
  1127. }
  1128. static void __exit exit_u32(void)
  1129. {
  1130. unregister_tcf_proto_ops(&cls_u32_ops);
  1131. kvfree(tc_u_common_hash);
  1132. }
  1133. module_init(init_u32)
  1134. module_exit(exit_u32)
  1135. MODULE_LICENSE("GPL");