cls_u32.c 28 KB

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