cls_u32.c 28 KB

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