cls_u32.c 23 KB

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