cls_u32.c 23 KB

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