cls_u32.c 31 KB

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