cls_u32.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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->protocol, &offload);
  385. }
  386. }
  387. static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
  388. u32 flags)
  389. {
  390. struct net_device *dev = tp->q->dev_queue->dev;
  391. struct tc_cls_u32_offload u32_offload = {0};
  392. struct tc_to_netdev offload;
  393. int err;
  394. if (!tc_should_offload(dev, tp, flags))
  395. return tc_skip_sw(flags) ? -EINVAL : 0;
  396. offload.type = TC_SETUP_CLSU32;
  397. offload.cls_u32 = &u32_offload;
  398. offload.cls_u32->command = TC_CLSU32_NEW_HNODE;
  399. offload.cls_u32->hnode.divisor = h->divisor;
  400. offload.cls_u32->hnode.handle = h->handle;
  401. offload.cls_u32->hnode.prio = h->prio;
  402. err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
  403. tp->protocol, &offload);
  404. if (tc_skip_sw(flags))
  405. return err;
  406. return 0;
  407. }
  408. static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h)
  409. {
  410. struct net_device *dev = tp->q->dev_queue->dev;
  411. struct tc_cls_u32_offload u32_offload = {0};
  412. struct tc_to_netdev offload;
  413. offload.type = TC_SETUP_CLSU32;
  414. offload.cls_u32 = &u32_offload;
  415. if (tc_should_offload(dev, tp, 0)) {
  416. offload.cls_u32->command = TC_CLSU32_DELETE_HNODE;
  417. offload.cls_u32->hnode.divisor = h->divisor;
  418. offload.cls_u32->hnode.handle = h->handle;
  419. offload.cls_u32->hnode.prio = h->prio;
  420. dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
  421. tp->protocol, &offload);
  422. }
  423. }
  424. static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
  425. u32 flags)
  426. {
  427. struct net_device *dev = tp->q->dev_queue->dev;
  428. struct tc_cls_u32_offload u32_offload = {0};
  429. struct tc_to_netdev offload;
  430. int err;
  431. offload.type = TC_SETUP_CLSU32;
  432. offload.cls_u32 = &u32_offload;
  433. if (!tc_should_offload(dev, tp, flags))
  434. return tc_skip_sw(flags) ? -EINVAL : 0;
  435. offload.cls_u32->command = TC_CLSU32_REPLACE_KNODE;
  436. offload.cls_u32->knode.handle = n->handle;
  437. offload.cls_u32->knode.fshift = n->fshift;
  438. #ifdef CONFIG_CLS_U32_MARK
  439. offload.cls_u32->knode.val = n->val;
  440. offload.cls_u32->knode.mask = n->mask;
  441. #else
  442. offload.cls_u32->knode.val = 0;
  443. offload.cls_u32->knode.mask = 0;
  444. #endif
  445. offload.cls_u32->knode.sel = &n->sel;
  446. offload.cls_u32->knode.exts = &n->exts;
  447. if (n->ht_down)
  448. offload.cls_u32->knode.link_handle = n->ht_down->handle;
  449. err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
  450. tp->protocol, &offload);
  451. if (!err)
  452. n->flags |= TCA_CLS_FLAGS_IN_HW;
  453. if (tc_skip_sw(flags))
  454. return err;
  455. return 0;
  456. }
  457. static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  458. {
  459. struct tc_u_knode *n;
  460. unsigned int h;
  461. for (h = 0; h <= ht->divisor; h++) {
  462. while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
  463. RCU_INIT_POINTER(ht->ht[h],
  464. rtnl_dereference(n->next));
  465. tcf_unbind_filter(tp, &n->res);
  466. u32_remove_hw_knode(tp, n->handle);
  467. call_rcu(&n->rcu, u32_delete_key_freepf_rcu);
  468. }
  469. }
  470. }
  471. static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
  472. {
  473. struct tc_u_common *tp_c = tp->data;
  474. struct tc_u_hnode __rcu **hn;
  475. struct tc_u_hnode *phn;
  476. WARN_ON(ht->refcnt);
  477. u32_clear_hnode(tp, ht);
  478. hn = &tp_c->hlist;
  479. for (phn = rtnl_dereference(*hn);
  480. phn;
  481. hn = &phn->next, phn = rtnl_dereference(*hn)) {
  482. if (phn == ht) {
  483. u32_clear_hw_hnode(tp, ht);
  484. RCU_INIT_POINTER(*hn, ht->next);
  485. kfree_rcu(ht, rcu);
  486. return 0;
  487. }
  488. }
  489. return -ENOENT;
  490. }
  491. static bool ht_empty(struct tc_u_hnode *ht)
  492. {
  493. unsigned int h;
  494. for (h = 0; h <= ht->divisor; h++)
  495. if (rcu_access_pointer(ht->ht[h]))
  496. return false;
  497. return true;
  498. }
  499. static bool u32_destroy(struct tcf_proto *tp, bool force)
  500. {
  501. struct tc_u_common *tp_c = tp->data;
  502. struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
  503. WARN_ON(root_ht == NULL);
  504. if (!force) {
  505. if (root_ht) {
  506. if (root_ht->refcnt > 1)
  507. return false;
  508. if (root_ht->refcnt == 1) {
  509. if (!ht_empty(root_ht))
  510. return false;
  511. }
  512. }
  513. if (tp_c->refcnt > 1)
  514. return false;
  515. if (tp_c->refcnt == 1) {
  516. struct tc_u_hnode *ht;
  517. for (ht = rtnl_dereference(tp_c->hlist);
  518. ht;
  519. ht = rtnl_dereference(ht->next))
  520. if (!ht_empty(ht))
  521. return false;
  522. }
  523. }
  524. if (root_ht && --root_ht->refcnt == 0)
  525. u32_destroy_hnode(tp, root_ht);
  526. if (--tp_c->refcnt == 0) {
  527. struct tc_u_hnode *ht;
  528. tp->q->u32_node = NULL;
  529. for (ht = rtnl_dereference(tp_c->hlist);
  530. ht;
  531. ht = rtnl_dereference(ht->next)) {
  532. ht->refcnt--;
  533. u32_clear_hnode(tp, ht);
  534. }
  535. while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
  536. RCU_INIT_POINTER(tp_c->hlist, ht->next);
  537. kfree_rcu(ht, rcu);
  538. }
  539. kfree(tp_c);
  540. }
  541. tp->data = NULL;
  542. return true;
  543. }
  544. static int u32_delete(struct tcf_proto *tp, unsigned long arg)
  545. {
  546. struct tc_u_hnode *ht = (struct tc_u_hnode *)arg;
  547. struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
  548. if (ht == NULL)
  549. return 0;
  550. if (TC_U32_KEY(ht->handle)) {
  551. u32_remove_hw_knode(tp, ht->handle);
  552. return u32_delete_key(tp, (struct tc_u_knode *)ht);
  553. }
  554. if (root_ht == ht)
  555. return -EINVAL;
  556. if (ht->refcnt == 1) {
  557. ht->refcnt--;
  558. u32_destroy_hnode(tp, ht);
  559. } else {
  560. return -EBUSY;
  561. }
  562. return 0;
  563. }
  564. #define NR_U32_NODE (1<<12)
  565. static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
  566. {
  567. struct tc_u_knode *n;
  568. unsigned long i;
  569. unsigned long *bitmap = kzalloc(BITS_TO_LONGS(NR_U32_NODE) * sizeof(unsigned long),
  570. GFP_KERNEL);
  571. if (!bitmap)
  572. return handle | 0xFFF;
  573. for (n = rtnl_dereference(ht->ht[TC_U32_HASH(handle)]);
  574. n;
  575. n = rtnl_dereference(n->next))
  576. set_bit(TC_U32_NODE(n->handle), bitmap);
  577. i = find_next_zero_bit(bitmap, NR_U32_NODE, 0x800);
  578. if (i >= NR_U32_NODE)
  579. i = find_next_zero_bit(bitmap, NR_U32_NODE, 1);
  580. kfree(bitmap);
  581. return handle | (i >= NR_U32_NODE ? 0xFFF : i);
  582. }
  583. static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
  584. [TCA_U32_CLASSID] = { .type = NLA_U32 },
  585. [TCA_U32_HASH] = { .type = NLA_U32 },
  586. [TCA_U32_LINK] = { .type = NLA_U32 },
  587. [TCA_U32_DIVISOR] = { .type = NLA_U32 },
  588. [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
  589. [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
  590. [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
  591. [TCA_U32_FLAGS] = { .type = NLA_U32 },
  592. };
  593. static int u32_set_parms(struct net *net, struct tcf_proto *tp,
  594. unsigned long base, struct tc_u_hnode *ht,
  595. struct tc_u_knode *n, struct nlattr **tb,
  596. struct nlattr *est, bool ovr)
  597. {
  598. struct tcf_exts e;
  599. int err;
  600. err = tcf_exts_init(&e, TCA_U32_ACT, TCA_U32_POLICE);
  601. if (err < 0)
  602. return err;
  603. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  604. if (err < 0)
  605. goto errout;
  606. err = -EINVAL;
  607. if (tb[TCA_U32_LINK]) {
  608. u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
  609. struct tc_u_hnode *ht_down = NULL, *ht_old;
  610. if (TC_U32_KEY(handle))
  611. goto errout;
  612. if (handle) {
  613. ht_down = u32_lookup_ht(ht->tp_c, handle);
  614. if (ht_down == NULL)
  615. goto errout;
  616. ht_down->refcnt++;
  617. }
  618. ht_old = rtnl_dereference(n->ht_down);
  619. rcu_assign_pointer(n->ht_down, ht_down);
  620. if (ht_old)
  621. ht_old->refcnt--;
  622. }
  623. if (tb[TCA_U32_CLASSID]) {
  624. n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
  625. tcf_bind_filter(tp, &n->res, base);
  626. }
  627. #ifdef CONFIG_NET_CLS_IND
  628. if (tb[TCA_U32_INDEV]) {
  629. int ret;
  630. ret = tcf_change_indev(net, tb[TCA_U32_INDEV]);
  631. if (ret < 0)
  632. goto errout;
  633. n->ifindex = ret;
  634. }
  635. #endif
  636. tcf_exts_change(tp, &n->exts, &e);
  637. return 0;
  638. errout:
  639. tcf_exts_destroy(&e);
  640. return err;
  641. }
  642. static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
  643. struct tc_u_knode *n)
  644. {
  645. struct tc_u_knode __rcu **ins;
  646. struct tc_u_knode *pins;
  647. struct tc_u_hnode *ht;
  648. if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
  649. ht = rtnl_dereference(tp->root);
  650. else
  651. ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
  652. ins = &ht->ht[TC_U32_HASH(n->handle)];
  653. /* The node must always exist for it to be replaced if this is not the
  654. * case then something went very wrong elsewhere.
  655. */
  656. for (pins = rtnl_dereference(*ins); ;
  657. ins = &pins->next, pins = rtnl_dereference(*ins))
  658. if (pins->handle == n->handle)
  659. break;
  660. RCU_INIT_POINTER(n->next, pins->next);
  661. rcu_assign_pointer(*ins, n);
  662. }
  663. static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
  664. struct tc_u_knode *n)
  665. {
  666. struct tc_u_knode *new;
  667. struct tc_u32_sel *s = &n->sel;
  668. new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
  669. GFP_KERNEL);
  670. if (!new)
  671. return NULL;
  672. RCU_INIT_POINTER(new->next, n->next);
  673. new->handle = n->handle;
  674. RCU_INIT_POINTER(new->ht_up, n->ht_up);
  675. #ifdef CONFIG_NET_CLS_IND
  676. new->ifindex = n->ifindex;
  677. #endif
  678. new->fshift = n->fshift;
  679. new->res = n->res;
  680. new->flags = n->flags;
  681. RCU_INIT_POINTER(new->ht_down, n->ht_down);
  682. /* bump reference count as long as we hold pointer to structure */
  683. if (new->ht_down)
  684. new->ht_down->refcnt++;
  685. #ifdef CONFIG_CLS_U32_PERF
  686. /* Statistics may be incremented by readers during update
  687. * so we must keep them in tact. When the node is later destroyed
  688. * a special destroy call must be made to not free the pf memory.
  689. */
  690. new->pf = n->pf;
  691. #endif
  692. #ifdef CONFIG_CLS_U32_MARK
  693. new->val = n->val;
  694. new->mask = n->mask;
  695. /* Similarly success statistics must be moved as pointers */
  696. new->pcpu_success = n->pcpu_success;
  697. #endif
  698. new->tp = tp;
  699. memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  700. if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
  701. kfree(new);
  702. return NULL;
  703. }
  704. return new;
  705. }
  706. static int u32_change(struct net *net, struct sk_buff *in_skb,
  707. struct tcf_proto *tp, unsigned long base, u32 handle,
  708. struct nlattr **tca, 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. if (!tc_in_hw(new->flags))
  754. new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  755. u32_replace_knode(tp, tp_c, new);
  756. tcf_unbind_filter(tp, &n->res);
  757. call_rcu(&n->rcu, u32_delete_key_rcu);
  758. return 0;
  759. }
  760. if (tb[TCA_U32_DIVISOR]) {
  761. unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
  762. if (--divisor > 0x100)
  763. return -EINVAL;
  764. if (TC_U32_KEY(handle))
  765. return -EINVAL;
  766. if (handle == 0) {
  767. handle = gen_new_htid(tp->data);
  768. if (handle == 0)
  769. return -ENOMEM;
  770. }
  771. ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
  772. if (ht == NULL)
  773. return -ENOBUFS;
  774. ht->tp_c = tp_c;
  775. ht->refcnt = 1;
  776. ht->divisor = divisor;
  777. ht->handle = handle;
  778. ht->prio = tp->prio;
  779. err = u32_replace_hw_hnode(tp, ht, flags);
  780. if (err) {
  781. kfree(ht);
  782. return err;
  783. }
  784. RCU_INIT_POINTER(ht->next, tp_c->hlist);
  785. rcu_assign_pointer(tp_c->hlist, ht);
  786. *arg = (unsigned long)ht;
  787. return 0;
  788. }
  789. if (tb[TCA_U32_HASH]) {
  790. htid = nla_get_u32(tb[TCA_U32_HASH]);
  791. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  792. ht = rtnl_dereference(tp->root);
  793. htid = ht->handle;
  794. } else {
  795. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  796. if (ht == NULL)
  797. return -EINVAL;
  798. }
  799. } else {
  800. ht = rtnl_dereference(tp->root);
  801. htid = ht->handle;
  802. }
  803. if (ht->divisor < TC_U32_HASH(htid))
  804. return -EINVAL;
  805. if (handle) {
  806. if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
  807. return -EINVAL;
  808. handle = htid | TC_U32_NODE(handle);
  809. } else
  810. handle = gen_new_kid(ht, htid);
  811. if (tb[TCA_U32_SEL] == NULL)
  812. return -EINVAL;
  813. s = nla_data(tb[TCA_U32_SEL]);
  814. n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
  815. if (n == NULL)
  816. return -ENOBUFS;
  817. #ifdef CONFIG_CLS_U32_PERF
  818. size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
  819. n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
  820. if (!n->pf) {
  821. kfree(n);
  822. return -ENOBUFS;
  823. }
  824. #endif
  825. memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  826. RCU_INIT_POINTER(n->ht_up, ht);
  827. n->handle = handle;
  828. n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
  829. n->flags = flags;
  830. n->tp = tp;
  831. err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
  832. if (err < 0)
  833. goto errout;
  834. #ifdef CONFIG_CLS_U32_MARK
  835. n->pcpu_success = alloc_percpu(u32);
  836. if (!n->pcpu_success) {
  837. err = -ENOMEM;
  838. goto errout;
  839. }
  840. if (tb[TCA_U32_MARK]) {
  841. struct tc_u32_mark *mark;
  842. mark = nla_data(tb[TCA_U32_MARK]);
  843. n->val = mark->val;
  844. n->mask = mark->mask;
  845. }
  846. #endif
  847. err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr);
  848. if (err == 0) {
  849. struct tc_u_knode __rcu **ins;
  850. struct tc_u_knode *pins;
  851. err = u32_replace_hw_knode(tp, n, flags);
  852. if (err)
  853. goto errhw;
  854. if (!tc_in_hw(n->flags))
  855. n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  856. ins = &ht->ht[TC_U32_HASH(handle)];
  857. for (pins = rtnl_dereference(*ins); pins;
  858. ins = &pins->next, pins = rtnl_dereference(*ins))
  859. if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
  860. break;
  861. RCU_INIT_POINTER(n->next, pins);
  862. rcu_assign_pointer(*ins, n);
  863. *arg = (unsigned long)n;
  864. return 0;
  865. }
  866. errhw:
  867. #ifdef CONFIG_CLS_U32_MARK
  868. free_percpu(n->pcpu_success);
  869. #endif
  870. errout:
  871. tcf_exts_destroy(&n->exts);
  872. #ifdef CONFIG_CLS_U32_PERF
  873. free_percpu(n->pf);
  874. #endif
  875. kfree(n);
  876. return err;
  877. }
  878. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  879. {
  880. struct tc_u_common *tp_c = tp->data;
  881. struct tc_u_hnode *ht;
  882. struct tc_u_knode *n;
  883. unsigned int h;
  884. if (arg->stop)
  885. return;
  886. for (ht = rtnl_dereference(tp_c->hlist);
  887. ht;
  888. ht = rtnl_dereference(ht->next)) {
  889. if (ht->prio != tp->prio)
  890. continue;
  891. if (arg->count >= arg->skip) {
  892. if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
  893. arg->stop = 1;
  894. return;
  895. }
  896. }
  897. arg->count++;
  898. for (h = 0; h <= ht->divisor; h++) {
  899. for (n = rtnl_dereference(ht->ht[h]);
  900. n;
  901. n = rtnl_dereference(n->next)) {
  902. if (arg->count < arg->skip) {
  903. arg->count++;
  904. continue;
  905. }
  906. if (arg->fn(tp, (unsigned long)n, arg) < 0) {
  907. arg->stop = 1;
  908. return;
  909. }
  910. arg->count++;
  911. }
  912. }
  913. }
  914. }
  915. static int u32_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  916. struct sk_buff *skb, struct tcmsg *t)
  917. {
  918. struct tc_u_knode *n = (struct tc_u_knode *)fh;
  919. struct tc_u_hnode *ht_up, *ht_down;
  920. struct nlattr *nest;
  921. if (n == NULL)
  922. return skb->len;
  923. t->tcm_handle = n->handle;
  924. nest = nla_nest_start(skb, TCA_OPTIONS);
  925. if (nest == NULL)
  926. goto nla_put_failure;
  927. if (TC_U32_KEY(n->handle) == 0) {
  928. struct tc_u_hnode *ht = (struct tc_u_hnode *)fh;
  929. u32 divisor = ht->divisor + 1;
  930. if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
  931. goto nla_put_failure;
  932. } else {
  933. #ifdef CONFIG_CLS_U32_PERF
  934. struct tc_u32_pcnt *gpf;
  935. int cpu;
  936. #endif
  937. if (nla_put(skb, TCA_U32_SEL,
  938. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  939. &n->sel))
  940. goto nla_put_failure;
  941. ht_up = rtnl_dereference(n->ht_up);
  942. if (ht_up) {
  943. u32 htid = n->handle & 0xFFFFF000;
  944. if (nla_put_u32(skb, TCA_U32_HASH, htid))
  945. goto nla_put_failure;
  946. }
  947. if (n->res.classid &&
  948. nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
  949. goto nla_put_failure;
  950. ht_down = rtnl_dereference(n->ht_down);
  951. if (ht_down &&
  952. nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
  953. goto nla_put_failure;
  954. if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
  955. goto nla_put_failure;
  956. #ifdef CONFIG_CLS_U32_MARK
  957. if ((n->val || n->mask)) {
  958. struct tc_u32_mark mark = {.val = n->val,
  959. .mask = n->mask,
  960. .success = 0};
  961. int cpum;
  962. for_each_possible_cpu(cpum) {
  963. __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
  964. mark.success += cnt;
  965. }
  966. if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
  967. goto nla_put_failure;
  968. }
  969. #endif
  970. if (tcf_exts_dump(skb, &n->exts) < 0)
  971. goto nla_put_failure;
  972. #ifdef CONFIG_NET_CLS_IND
  973. if (n->ifindex) {
  974. struct net_device *dev;
  975. dev = __dev_get_by_index(net, n->ifindex);
  976. if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
  977. goto nla_put_failure;
  978. }
  979. #endif
  980. #ifdef CONFIG_CLS_U32_PERF
  981. gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
  982. n->sel.nkeys * sizeof(u64),
  983. GFP_KERNEL);
  984. if (!gpf)
  985. goto nla_put_failure;
  986. for_each_possible_cpu(cpu) {
  987. int i;
  988. struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
  989. gpf->rcnt += pf->rcnt;
  990. gpf->rhit += pf->rhit;
  991. for (i = 0; i < n->sel.nkeys; i++)
  992. gpf->kcnts[i] += pf->kcnts[i];
  993. }
  994. if (nla_put_64bit(skb, TCA_U32_PCNT,
  995. sizeof(struct tc_u32_pcnt) +
  996. n->sel.nkeys * sizeof(u64),
  997. gpf, TCA_U32_PAD)) {
  998. kfree(gpf);
  999. goto nla_put_failure;
  1000. }
  1001. kfree(gpf);
  1002. #endif
  1003. }
  1004. nla_nest_end(skb, nest);
  1005. if (TC_U32_KEY(n->handle))
  1006. if (tcf_exts_dump_stats(skb, &n->exts) < 0)
  1007. goto nla_put_failure;
  1008. return skb->len;
  1009. nla_put_failure:
  1010. nla_nest_cancel(skb, nest);
  1011. return -1;
  1012. }
  1013. static struct tcf_proto_ops cls_u32_ops __read_mostly = {
  1014. .kind = "u32",
  1015. .classify = u32_classify,
  1016. .init = u32_init,
  1017. .destroy = u32_destroy,
  1018. .get = u32_get,
  1019. .change = u32_change,
  1020. .delete = u32_delete,
  1021. .walk = u32_walk,
  1022. .dump = u32_dump,
  1023. .owner = THIS_MODULE,
  1024. };
  1025. static int __init init_u32(void)
  1026. {
  1027. pr_info("u32 classifier\n");
  1028. #ifdef CONFIG_CLS_U32_PERF
  1029. pr_info(" Performance counters on\n");
  1030. #endif
  1031. #ifdef CONFIG_NET_CLS_IND
  1032. pr_info(" input device check on\n");
  1033. #endif
  1034. #ifdef CONFIG_NET_CLS_ACT
  1035. pr_info(" Actions configured\n");
  1036. #endif
  1037. return register_tcf_proto_ops(&cls_u32_ops);
  1038. }
  1039. static void __exit exit_u32(void)
  1040. {
  1041. unregister_tcf_proto_ops(&cls_u32_ops);
  1042. }
  1043. module_init(init_u32)
  1044. module_exit(exit_u32)
  1045. MODULE_LICENSE("GPL");