cls_u32.c 27 KB

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