cls_u32.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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. rcu_assign_pointer(tp->root, root_ht);
  347. tp->data = tp_c;
  348. return 0;
  349. }
  350. static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n,
  351. bool free_pf)
  352. {
  353. struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
  354. tcf_exts_destroy(&n->exts);
  355. tcf_exts_put_net(&n->exts);
  356. if (ht && --ht->refcnt == 0)
  357. kfree(ht);
  358. #ifdef CONFIG_CLS_U32_PERF
  359. if (free_pf)
  360. free_percpu(n->pf);
  361. #endif
  362. #ifdef CONFIG_CLS_U32_MARK
  363. if (free_pf)
  364. free_percpu(n->pcpu_success);
  365. #endif
  366. kfree(n);
  367. return 0;
  368. }
  369. /* u32_delete_key_rcu should be called when free'ing a copied
  370. * version of a tc_u_knode obtained from u32_init_knode(). When
  371. * copies are obtained from u32_init_knode() the statistics are
  372. * shared between the old and new copies to allow readers to
  373. * continue to update the statistics during the copy. To support
  374. * this the u32_delete_key_rcu variant does not free the percpu
  375. * statistics.
  376. */
  377. static void u32_delete_key_work(struct work_struct *work)
  378. {
  379. struct tc_u_knode *key = container_of(to_rcu_work(work),
  380. struct tc_u_knode,
  381. rwork);
  382. rtnl_lock();
  383. u32_destroy_key(key->tp, key, false);
  384. rtnl_unlock();
  385. }
  386. /* u32_delete_key_freepf_rcu is the rcu callback variant
  387. * that free's the entire structure including the statistics
  388. * percpu variables. Only use this if the key is not a copy
  389. * returned by u32_init_knode(). See u32_delete_key_rcu()
  390. * for the variant that should be used with keys return from
  391. * u32_init_knode()
  392. */
  393. static void u32_delete_key_freepf_work(struct work_struct *work)
  394. {
  395. struct tc_u_knode *key = container_of(to_rcu_work(work),
  396. struct tc_u_knode,
  397. rwork);
  398. rtnl_lock();
  399. u32_destroy_key(key->tp, key, true);
  400. rtnl_unlock();
  401. }
  402. static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
  403. {
  404. struct tc_u_knode __rcu **kp;
  405. struct tc_u_knode *pkp;
  406. struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
  407. if (ht) {
  408. kp = &ht->ht[TC_U32_HASH(key->handle)];
  409. for (pkp = rtnl_dereference(*kp); pkp;
  410. kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
  411. if (pkp == key) {
  412. RCU_INIT_POINTER(*kp, key->next);
  413. tcf_unbind_filter(tp, &key->res);
  414. idr_remove(&ht->handle_idr, key->handle);
  415. tcf_exts_get_net(&key->exts);
  416. tcf_queue_work(&key->rwork, u32_delete_key_freepf_work);
  417. return 0;
  418. }
  419. }
  420. }
  421. WARN_ON(1);
  422. return 0;
  423. }
  424. static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
  425. struct netlink_ext_ack *extack)
  426. {
  427. struct tcf_block *block = tp->chain->block;
  428. struct tc_cls_u32_offload cls_u32 = {};
  429. tc_cls_common_offload_init(&cls_u32.common, tp, h->flags, extack);
  430. cls_u32.command = TC_CLSU32_DELETE_HNODE;
  431. cls_u32.hnode.divisor = h->divisor;
  432. cls_u32.hnode.handle = h->handle;
  433. cls_u32.hnode.prio = h->prio;
  434. tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
  435. }
  436. static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
  437. u32 flags, struct netlink_ext_ack *extack)
  438. {
  439. struct tcf_block *block = tp->chain->block;
  440. struct tc_cls_u32_offload cls_u32 = {};
  441. bool skip_sw = tc_skip_sw(flags);
  442. bool offloaded = false;
  443. int err;
  444. tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
  445. cls_u32.command = TC_CLSU32_NEW_HNODE;
  446. cls_u32.hnode.divisor = h->divisor;
  447. cls_u32.hnode.handle = h->handle;
  448. cls_u32.hnode.prio = h->prio;
  449. err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
  450. if (err < 0) {
  451. u32_clear_hw_hnode(tp, h, NULL);
  452. return err;
  453. } else if (err > 0) {
  454. offloaded = true;
  455. }
  456. if (skip_sw && !offloaded)
  457. return -EINVAL;
  458. return 0;
  459. }
  460. static void u32_remove_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
  461. struct netlink_ext_ack *extack)
  462. {
  463. struct tcf_block *block = tp->chain->block;
  464. struct tc_cls_u32_offload cls_u32 = {};
  465. tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
  466. cls_u32.command = TC_CLSU32_DELETE_KNODE;
  467. cls_u32.knode.handle = n->handle;
  468. tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
  469. tcf_block_offload_dec(block, &n->flags);
  470. }
  471. static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
  472. u32 flags, struct netlink_ext_ack *extack)
  473. {
  474. struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
  475. struct tcf_block *block = tp->chain->block;
  476. struct tc_cls_u32_offload cls_u32 = {};
  477. bool skip_sw = tc_skip_sw(flags);
  478. int err;
  479. tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
  480. cls_u32.command = TC_CLSU32_REPLACE_KNODE;
  481. cls_u32.knode.handle = n->handle;
  482. cls_u32.knode.fshift = n->fshift;
  483. #ifdef CONFIG_CLS_U32_MARK
  484. cls_u32.knode.val = n->val;
  485. cls_u32.knode.mask = n->mask;
  486. #else
  487. cls_u32.knode.val = 0;
  488. cls_u32.knode.mask = 0;
  489. #endif
  490. cls_u32.knode.sel = &n->sel;
  491. cls_u32.knode.exts = &n->exts;
  492. if (n->ht_down)
  493. cls_u32.knode.link_handle = ht->handle;
  494. err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
  495. if (err < 0) {
  496. u32_remove_hw_knode(tp, n, NULL);
  497. return err;
  498. } else if (err > 0) {
  499. n->in_hw_count = err;
  500. tcf_block_offload_inc(block, &n->flags);
  501. }
  502. if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
  503. return -EINVAL;
  504. return 0;
  505. }
  506. static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
  507. struct netlink_ext_ack *extack)
  508. {
  509. struct tc_u_knode *n;
  510. unsigned int h;
  511. for (h = 0; h <= ht->divisor; h++) {
  512. while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
  513. RCU_INIT_POINTER(ht->ht[h],
  514. rtnl_dereference(n->next));
  515. tcf_unbind_filter(tp, &n->res);
  516. u32_remove_hw_knode(tp, n, extack);
  517. idr_remove(&ht->handle_idr, n->handle);
  518. if (tcf_exts_get_net(&n->exts))
  519. tcf_queue_work(&n->rwork, u32_delete_key_freepf_work);
  520. else
  521. u32_destroy_key(n->tp, n, true);
  522. }
  523. }
  524. }
  525. static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
  526. struct netlink_ext_ack *extack)
  527. {
  528. struct tc_u_common *tp_c = tp->data;
  529. struct tc_u_hnode __rcu **hn;
  530. struct tc_u_hnode *phn;
  531. WARN_ON(ht->refcnt);
  532. u32_clear_hnode(tp, ht, extack);
  533. hn = &tp_c->hlist;
  534. for (phn = rtnl_dereference(*hn);
  535. phn;
  536. hn = &phn->next, phn = rtnl_dereference(*hn)) {
  537. if (phn == ht) {
  538. u32_clear_hw_hnode(tp, ht, extack);
  539. idr_destroy(&ht->handle_idr);
  540. idr_remove(&tp_c->handle_idr, ht->handle);
  541. RCU_INIT_POINTER(*hn, ht->next);
  542. kfree_rcu(ht, rcu);
  543. return 0;
  544. }
  545. }
  546. return -ENOENT;
  547. }
  548. static bool ht_empty(struct tc_u_hnode *ht)
  549. {
  550. unsigned int h;
  551. for (h = 0; h <= ht->divisor; h++)
  552. if (rcu_access_pointer(ht->ht[h]))
  553. return false;
  554. return true;
  555. }
  556. static void u32_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
  557. {
  558. struct tc_u_common *tp_c = tp->data;
  559. struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
  560. WARN_ON(root_ht == NULL);
  561. if (root_ht && --root_ht->refcnt == 0)
  562. u32_destroy_hnode(tp, root_ht, extack);
  563. if (--tp_c->refcnt == 0) {
  564. struct tc_u_hnode *ht;
  565. hlist_del(&tp_c->hnode);
  566. while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
  567. u32_clear_hnode(tp, ht, extack);
  568. RCU_INIT_POINTER(tp_c->hlist, ht->next);
  569. /* u32_destroy_key() will later free ht for us, if it's
  570. * still referenced by some knode
  571. */
  572. if (--ht->refcnt == 0)
  573. kfree_rcu(ht, rcu);
  574. }
  575. idr_destroy(&tp_c->handle_idr);
  576. kfree(tp_c);
  577. }
  578. tp->data = NULL;
  579. }
  580. static int u32_delete(struct tcf_proto *tp, void *arg, bool *last,
  581. struct netlink_ext_ack *extack)
  582. {
  583. struct tc_u_hnode *ht = arg;
  584. struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
  585. struct tc_u_common *tp_c = tp->data;
  586. int ret = 0;
  587. if (ht == NULL)
  588. goto out;
  589. if (TC_U32_KEY(ht->handle)) {
  590. u32_remove_hw_knode(tp, (struct tc_u_knode *)ht, extack);
  591. ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
  592. goto out;
  593. }
  594. if (root_ht == ht) {
  595. NL_SET_ERR_MSG_MOD(extack, "Not allowed to delete root node");
  596. return -EINVAL;
  597. }
  598. if (ht->refcnt == 1) {
  599. ht->refcnt--;
  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 > 1) {
  609. *last = false;
  610. goto ret;
  611. }
  612. if (root_ht->refcnt == 1) {
  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. int err;
  782. #ifdef CONFIG_CLS_U32_PERF
  783. size_t size;
  784. #endif
  785. if (!opt) {
  786. if (handle) {
  787. NL_SET_ERR_MSG_MOD(extack, "Filter handle requires options");
  788. return -EINVAL;
  789. } else {
  790. return 0;
  791. }
  792. }
  793. err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, extack);
  794. if (err < 0)
  795. return err;
  796. if (tb[TCA_U32_FLAGS]) {
  797. flags = nla_get_u32(tb[TCA_U32_FLAGS]);
  798. if (!tc_flags_valid(flags)) {
  799. NL_SET_ERR_MSG_MOD(extack, "Invalid filter flags");
  800. return -EINVAL;
  801. }
  802. }
  803. n = *arg;
  804. if (n) {
  805. struct tc_u_knode *new;
  806. if (TC_U32_KEY(n->handle) == 0) {
  807. NL_SET_ERR_MSG_MOD(extack, "Key node id cannot be zero");
  808. return -EINVAL;
  809. }
  810. if ((n->flags ^ flags) &
  811. ~(TCA_CLS_FLAGS_IN_HW | TCA_CLS_FLAGS_NOT_IN_HW)) {
  812. NL_SET_ERR_MSG_MOD(extack, "Key node flags do not match passed flags");
  813. return -EINVAL;
  814. }
  815. new = u32_init_knode(tp, n);
  816. if (!new)
  817. return -ENOMEM;
  818. err = u32_set_parms(net, tp, base,
  819. rtnl_dereference(n->ht_up), new, tb,
  820. tca[TCA_RATE], ovr, extack);
  821. if (err) {
  822. u32_destroy_key(tp, new, false);
  823. return err;
  824. }
  825. err = u32_replace_hw_knode(tp, new, flags, extack);
  826. if (err) {
  827. u32_destroy_key(tp, new, false);
  828. return err;
  829. }
  830. if (!tc_in_hw(new->flags))
  831. new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  832. u32_replace_knode(tp, tp_c, new);
  833. tcf_unbind_filter(tp, &n->res);
  834. tcf_exts_get_net(&n->exts);
  835. tcf_queue_work(&n->rwork, u32_delete_key_work);
  836. return 0;
  837. }
  838. if (tb[TCA_U32_DIVISOR]) {
  839. unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
  840. if (--divisor > 0x100) {
  841. NL_SET_ERR_MSG_MOD(extack, "Exceeded maximum 256 hash buckets");
  842. return -EINVAL;
  843. }
  844. if (TC_U32_KEY(handle)) {
  845. NL_SET_ERR_MSG_MOD(extack, "Divisor can only be used on a hash table");
  846. return -EINVAL;
  847. }
  848. ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
  849. if (ht == NULL)
  850. return -ENOBUFS;
  851. if (handle == 0) {
  852. handle = gen_new_htid(tp->data, ht);
  853. if (handle == 0) {
  854. kfree(ht);
  855. return -ENOMEM;
  856. }
  857. } else {
  858. err = idr_alloc_u32(&tp_c->handle_idr, ht, &handle,
  859. handle, GFP_KERNEL);
  860. if (err) {
  861. kfree(ht);
  862. return err;
  863. }
  864. }
  865. ht->tp_c = tp_c;
  866. ht->refcnt = 1;
  867. ht->divisor = divisor;
  868. ht->handle = handle;
  869. ht->prio = tp->prio;
  870. idr_init(&ht->handle_idr);
  871. ht->flags = flags;
  872. err = u32_replace_hw_hnode(tp, ht, flags, extack);
  873. if (err) {
  874. idr_remove(&tp_c->handle_idr, handle);
  875. kfree(ht);
  876. return err;
  877. }
  878. RCU_INIT_POINTER(ht->next, tp_c->hlist);
  879. rcu_assign_pointer(tp_c->hlist, ht);
  880. *arg = ht;
  881. return 0;
  882. }
  883. if (tb[TCA_U32_HASH]) {
  884. htid = nla_get_u32(tb[TCA_U32_HASH]);
  885. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  886. ht = rtnl_dereference(tp->root);
  887. htid = ht->handle;
  888. } else {
  889. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  890. if (!ht) {
  891. NL_SET_ERR_MSG_MOD(extack, "Specified hash table not found");
  892. return -EINVAL;
  893. }
  894. }
  895. } else {
  896. ht = rtnl_dereference(tp->root);
  897. htid = ht->handle;
  898. }
  899. if (ht->divisor < TC_U32_HASH(htid)) {
  900. NL_SET_ERR_MSG_MOD(extack, "Specified hash table buckets exceed configured value");
  901. return -EINVAL;
  902. }
  903. if (handle) {
  904. if (TC_U32_HTID(handle) && TC_U32_HTID(handle ^ htid)) {
  905. NL_SET_ERR_MSG_MOD(extack, "Handle specified hash table address mismatch");
  906. return -EINVAL;
  907. }
  908. handle = htid | TC_U32_NODE(handle);
  909. err = idr_alloc_u32(&ht->handle_idr, NULL, &handle, handle,
  910. GFP_KERNEL);
  911. if (err)
  912. return err;
  913. } else
  914. handle = gen_new_kid(ht, htid);
  915. if (tb[TCA_U32_SEL] == NULL) {
  916. NL_SET_ERR_MSG_MOD(extack, "Selector not specified");
  917. err = -EINVAL;
  918. goto erridr;
  919. }
  920. s = nla_data(tb[TCA_U32_SEL]);
  921. n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
  922. if (n == NULL) {
  923. err = -ENOBUFS;
  924. goto erridr;
  925. }
  926. #ifdef CONFIG_CLS_U32_PERF
  927. size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
  928. n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
  929. if (!n->pf) {
  930. err = -ENOBUFS;
  931. goto errfree;
  932. }
  933. #endif
  934. memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  935. RCU_INIT_POINTER(n->ht_up, ht);
  936. n->handle = handle;
  937. n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
  938. n->flags = flags;
  939. n->tp = tp;
  940. err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
  941. if (err < 0)
  942. goto errout;
  943. #ifdef CONFIG_CLS_U32_MARK
  944. n->pcpu_success = alloc_percpu(u32);
  945. if (!n->pcpu_success) {
  946. err = -ENOMEM;
  947. goto errout;
  948. }
  949. if (tb[TCA_U32_MARK]) {
  950. struct tc_u32_mark *mark;
  951. mark = nla_data(tb[TCA_U32_MARK]);
  952. n->val = mark->val;
  953. n->mask = mark->mask;
  954. }
  955. #endif
  956. err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr,
  957. extack);
  958. if (err == 0) {
  959. struct tc_u_knode __rcu **ins;
  960. struct tc_u_knode *pins;
  961. err = u32_replace_hw_knode(tp, n, flags, extack);
  962. if (err)
  963. goto errhw;
  964. if (!tc_in_hw(n->flags))
  965. n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  966. ins = &ht->ht[TC_U32_HASH(handle)];
  967. for (pins = rtnl_dereference(*ins); pins;
  968. ins = &pins->next, pins = rtnl_dereference(*ins))
  969. if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
  970. break;
  971. RCU_INIT_POINTER(n->next, pins);
  972. rcu_assign_pointer(*ins, n);
  973. *arg = n;
  974. return 0;
  975. }
  976. errhw:
  977. #ifdef CONFIG_CLS_U32_MARK
  978. free_percpu(n->pcpu_success);
  979. #endif
  980. errout:
  981. tcf_exts_destroy(&n->exts);
  982. #ifdef CONFIG_CLS_U32_PERF
  983. errfree:
  984. free_percpu(n->pf);
  985. #endif
  986. kfree(n);
  987. erridr:
  988. idr_remove(&ht->handle_idr, handle);
  989. return err;
  990. }
  991. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  992. {
  993. struct tc_u_common *tp_c = tp->data;
  994. struct tc_u_hnode *ht;
  995. struct tc_u_knode *n;
  996. unsigned int h;
  997. if (arg->stop)
  998. return;
  999. for (ht = rtnl_dereference(tp_c->hlist);
  1000. ht;
  1001. ht = rtnl_dereference(ht->next)) {
  1002. if (ht->prio != tp->prio)
  1003. continue;
  1004. if (arg->count >= arg->skip) {
  1005. if (arg->fn(tp, ht, arg) < 0) {
  1006. arg->stop = 1;
  1007. return;
  1008. }
  1009. }
  1010. arg->count++;
  1011. for (h = 0; h <= ht->divisor; h++) {
  1012. for (n = rtnl_dereference(ht->ht[h]);
  1013. n;
  1014. n = rtnl_dereference(n->next)) {
  1015. if (arg->count < arg->skip) {
  1016. arg->count++;
  1017. continue;
  1018. }
  1019. if (arg->fn(tp, n, arg) < 0) {
  1020. arg->stop = 1;
  1021. return;
  1022. }
  1023. arg->count++;
  1024. }
  1025. }
  1026. }
  1027. }
  1028. static int u32_reoffload_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
  1029. bool add, tc_setup_cb_t *cb, void *cb_priv,
  1030. struct netlink_ext_ack *extack)
  1031. {
  1032. struct tc_cls_u32_offload cls_u32 = {};
  1033. int err;
  1034. tc_cls_common_offload_init(&cls_u32.common, tp, ht->flags, extack);
  1035. cls_u32.command = add ? TC_CLSU32_NEW_HNODE : TC_CLSU32_DELETE_HNODE;
  1036. cls_u32.hnode.divisor = ht->divisor;
  1037. cls_u32.hnode.handle = ht->handle;
  1038. cls_u32.hnode.prio = ht->prio;
  1039. err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
  1040. if (err && add && tc_skip_sw(ht->flags))
  1041. return err;
  1042. return 0;
  1043. }
  1044. static int u32_reoffload_knode(struct tcf_proto *tp, struct tc_u_knode *n,
  1045. bool add, tc_setup_cb_t *cb, void *cb_priv,
  1046. struct netlink_ext_ack *extack)
  1047. {
  1048. struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
  1049. struct tcf_block *block = tp->chain->block;
  1050. struct tc_cls_u32_offload cls_u32 = {};
  1051. int err;
  1052. tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
  1053. cls_u32.command = add ?
  1054. TC_CLSU32_REPLACE_KNODE : TC_CLSU32_DELETE_KNODE;
  1055. cls_u32.knode.handle = n->handle;
  1056. if (add) {
  1057. cls_u32.knode.fshift = n->fshift;
  1058. #ifdef CONFIG_CLS_U32_MARK
  1059. cls_u32.knode.val = n->val;
  1060. cls_u32.knode.mask = n->mask;
  1061. #else
  1062. cls_u32.knode.val = 0;
  1063. cls_u32.knode.mask = 0;
  1064. #endif
  1065. cls_u32.knode.sel = &n->sel;
  1066. cls_u32.knode.exts = &n->exts;
  1067. if (n->ht_down)
  1068. cls_u32.knode.link_handle = ht->handle;
  1069. }
  1070. err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
  1071. if (err) {
  1072. if (add && tc_skip_sw(n->flags))
  1073. return err;
  1074. return 0;
  1075. }
  1076. tc_cls_offload_cnt_update(block, &n->in_hw_count, &n->flags, add);
  1077. return 0;
  1078. }
  1079. static int u32_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
  1080. void *cb_priv, struct netlink_ext_ack *extack)
  1081. {
  1082. struct tc_u_common *tp_c = tp->data;
  1083. struct tc_u_hnode *ht;
  1084. struct tc_u_knode *n;
  1085. unsigned int h;
  1086. int err;
  1087. for (ht = rtnl_dereference(tp_c->hlist);
  1088. ht;
  1089. ht = rtnl_dereference(ht->next)) {
  1090. if (ht->prio != tp->prio)
  1091. continue;
  1092. /* When adding filters to a new dev, try to offload the
  1093. * hashtable first. When removing, do the filters before the
  1094. * hashtable.
  1095. */
  1096. if (add && !tc_skip_hw(ht->flags)) {
  1097. err = u32_reoffload_hnode(tp, ht, add, cb, cb_priv,
  1098. extack);
  1099. if (err)
  1100. return err;
  1101. }
  1102. for (h = 0; h <= ht->divisor; h++) {
  1103. for (n = rtnl_dereference(ht->ht[h]);
  1104. n;
  1105. n = rtnl_dereference(n->next)) {
  1106. if (tc_skip_hw(n->flags))
  1107. continue;
  1108. err = u32_reoffload_knode(tp, n, add, cb,
  1109. cb_priv, extack);
  1110. if (err)
  1111. return err;
  1112. }
  1113. }
  1114. if (!add && !tc_skip_hw(ht->flags))
  1115. u32_reoffload_hnode(tp, ht, add, cb, cb_priv, extack);
  1116. }
  1117. return 0;
  1118. }
  1119. static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
  1120. {
  1121. struct tc_u_knode *n = fh;
  1122. if (n && n->res.classid == classid)
  1123. n->res.class = cl;
  1124. }
  1125. static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
  1126. struct sk_buff *skb, struct tcmsg *t)
  1127. {
  1128. struct tc_u_knode *n = fh;
  1129. struct tc_u_hnode *ht_up, *ht_down;
  1130. struct nlattr *nest;
  1131. if (n == NULL)
  1132. return skb->len;
  1133. t->tcm_handle = n->handle;
  1134. nest = nla_nest_start(skb, TCA_OPTIONS);
  1135. if (nest == NULL)
  1136. goto nla_put_failure;
  1137. if (TC_U32_KEY(n->handle) == 0) {
  1138. struct tc_u_hnode *ht = fh;
  1139. u32 divisor = ht->divisor + 1;
  1140. if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
  1141. goto nla_put_failure;
  1142. } else {
  1143. #ifdef CONFIG_CLS_U32_PERF
  1144. struct tc_u32_pcnt *gpf;
  1145. int cpu;
  1146. #endif
  1147. if (nla_put(skb, TCA_U32_SEL,
  1148. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  1149. &n->sel))
  1150. goto nla_put_failure;
  1151. ht_up = rtnl_dereference(n->ht_up);
  1152. if (ht_up) {
  1153. u32 htid = n->handle & 0xFFFFF000;
  1154. if (nla_put_u32(skb, TCA_U32_HASH, htid))
  1155. goto nla_put_failure;
  1156. }
  1157. if (n->res.classid &&
  1158. nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
  1159. goto nla_put_failure;
  1160. ht_down = rtnl_dereference(n->ht_down);
  1161. if (ht_down &&
  1162. nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
  1163. goto nla_put_failure;
  1164. if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
  1165. goto nla_put_failure;
  1166. #ifdef CONFIG_CLS_U32_MARK
  1167. if ((n->val || n->mask)) {
  1168. struct tc_u32_mark mark = {.val = n->val,
  1169. .mask = n->mask,
  1170. .success = 0};
  1171. int cpum;
  1172. for_each_possible_cpu(cpum) {
  1173. __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
  1174. mark.success += cnt;
  1175. }
  1176. if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
  1177. goto nla_put_failure;
  1178. }
  1179. #endif
  1180. if (tcf_exts_dump(skb, &n->exts) < 0)
  1181. goto nla_put_failure;
  1182. #ifdef CONFIG_NET_CLS_IND
  1183. if (n->ifindex) {
  1184. struct net_device *dev;
  1185. dev = __dev_get_by_index(net, n->ifindex);
  1186. if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
  1187. goto nla_put_failure;
  1188. }
  1189. #endif
  1190. #ifdef CONFIG_CLS_U32_PERF
  1191. gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
  1192. n->sel.nkeys * sizeof(u64),
  1193. GFP_KERNEL);
  1194. if (!gpf)
  1195. goto nla_put_failure;
  1196. for_each_possible_cpu(cpu) {
  1197. int i;
  1198. struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
  1199. gpf->rcnt += pf->rcnt;
  1200. gpf->rhit += pf->rhit;
  1201. for (i = 0; i < n->sel.nkeys; i++)
  1202. gpf->kcnts[i] += pf->kcnts[i];
  1203. }
  1204. if (nla_put_64bit(skb, TCA_U32_PCNT,
  1205. sizeof(struct tc_u32_pcnt) +
  1206. n->sel.nkeys * sizeof(u64),
  1207. gpf, TCA_U32_PAD)) {
  1208. kfree(gpf);
  1209. goto nla_put_failure;
  1210. }
  1211. kfree(gpf);
  1212. #endif
  1213. }
  1214. nla_nest_end(skb, nest);
  1215. if (TC_U32_KEY(n->handle))
  1216. if (tcf_exts_dump_stats(skb, &n->exts) < 0)
  1217. goto nla_put_failure;
  1218. return skb->len;
  1219. nla_put_failure:
  1220. nla_nest_cancel(skb, nest);
  1221. return -1;
  1222. }
  1223. static struct tcf_proto_ops cls_u32_ops __read_mostly = {
  1224. .kind = "u32",
  1225. .classify = u32_classify,
  1226. .init = u32_init,
  1227. .destroy = u32_destroy,
  1228. .get = u32_get,
  1229. .change = u32_change,
  1230. .delete = u32_delete,
  1231. .walk = u32_walk,
  1232. .reoffload = u32_reoffload,
  1233. .dump = u32_dump,
  1234. .bind_class = u32_bind_class,
  1235. .owner = THIS_MODULE,
  1236. };
  1237. static int __init init_u32(void)
  1238. {
  1239. int i, ret;
  1240. pr_info("u32 classifier\n");
  1241. #ifdef CONFIG_CLS_U32_PERF
  1242. pr_info(" Performance counters on\n");
  1243. #endif
  1244. #ifdef CONFIG_NET_CLS_IND
  1245. pr_info(" input device check on\n");
  1246. #endif
  1247. #ifdef CONFIG_NET_CLS_ACT
  1248. pr_info(" Actions configured\n");
  1249. #endif
  1250. tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
  1251. sizeof(struct hlist_head),
  1252. GFP_KERNEL);
  1253. if (!tc_u_common_hash)
  1254. return -ENOMEM;
  1255. for (i = 0; i < U32_HASH_SIZE; i++)
  1256. INIT_HLIST_HEAD(&tc_u_common_hash[i]);
  1257. ret = register_tcf_proto_ops(&cls_u32_ops);
  1258. if (ret)
  1259. kvfree(tc_u_common_hash);
  1260. return ret;
  1261. }
  1262. static void __exit exit_u32(void)
  1263. {
  1264. unregister_tcf_proto_ops(&cls_u32_ops);
  1265. kvfree(tc_u_common_hash);
  1266. }
  1267. module_init(init_u32)
  1268. module_exit(exit_u32)
  1269. MODULE_LICENSE("GPL");