cls_u32.c 34 KB

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