cls_u32.c 34 KB

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