cls_u32.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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->divisor = 0;
  292. root_ht->refcnt++;
  293. root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
  294. root_ht->prio = tp->prio;
  295. if (tp_c == NULL) {
  296. tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
  297. if (tp_c == NULL) {
  298. kfree(root_ht);
  299. return -ENOBUFS;
  300. }
  301. tp_c->q = tp->q;
  302. tp->q->u32_node = tp_c;
  303. }
  304. tp_c->refcnt++;
  305. RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
  306. rcu_assign_pointer(tp_c->hlist, root_ht);
  307. root_ht->tp_c = tp_c;
  308. rcu_assign_pointer(tp->root, root_ht);
  309. tp->data = tp_c;
  310. return 0;
  311. }
  312. static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n,
  313. bool free_pf)
  314. {
  315. tcf_exts_destroy(&n->exts);
  316. if (n->ht_down)
  317. n->ht_down->refcnt--;
  318. #ifdef CONFIG_CLS_U32_PERF
  319. if (free_pf)
  320. free_percpu(n->pf);
  321. #endif
  322. #ifdef CONFIG_CLS_U32_MARK
  323. if (free_pf)
  324. free_percpu(n->pcpu_success);
  325. #endif
  326. kfree(n);
  327. return 0;
  328. }
  329. /* u32_delete_key_rcu should be called when free'ing a copied
  330. * version of a tc_u_knode obtained from u32_init_knode(). When
  331. * copies are obtained from u32_init_knode() the statistics are
  332. * shared between the old and new copies to allow readers to
  333. * continue to update the statistics during the copy. To support
  334. * this the u32_delete_key_rcu variant does not free the percpu
  335. * statistics.
  336. */
  337. static void u32_delete_key_rcu(struct rcu_head *rcu)
  338. {
  339. struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
  340. u32_destroy_key(key->tp, key, false);
  341. }
  342. /* u32_delete_key_freepf_rcu is the rcu callback variant
  343. * that free's the entire structure including the statistics
  344. * percpu variables. Only use this if the key is not a copy
  345. * returned by u32_init_knode(). See u32_delete_key_rcu()
  346. * for the variant that should be used with keys return from
  347. * u32_init_knode()
  348. */
  349. static void u32_delete_key_freepf_rcu(struct rcu_head *rcu)
  350. {
  351. struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
  352. u32_destroy_key(key->tp, key, true);
  353. }
  354. static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
  355. {
  356. struct tc_u_knode __rcu **kp;
  357. struct tc_u_knode *pkp;
  358. struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
  359. if (ht) {
  360. kp = &ht->ht[TC_U32_HASH(key->handle)];
  361. for (pkp = rtnl_dereference(*kp); pkp;
  362. kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
  363. if (pkp == key) {
  364. RCU_INIT_POINTER(*kp, key->next);
  365. tcf_unbind_filter(tp, &key->res);
  366. call_rcu(&key->rcu, u32_delete_key_freepf_rcu);
  367. return 0;
  368. }
  369. }
  370. }
  371. WARN_ON(1);
  372. return 0;
  373. }
  374. static void u32_remove_hw_knode(struct tcf_proto *tp, u32 handle)
  375. {
  376. struct net_device *dev = tp->q->dev_queue->dev;
  377. struct tc_cls_u32_offload u32_offload = {0};
  378. struct tc_to_netdev offload;
  379. offload.type = TC_SETUP_CLSU32;
  380. offload.cls_u32 = &u32_offload;
  381. if (tc_should_offload(dev, tp, 0)) {
  382. offload.cls_u32->command = TC_CLSU32_DELETE_KNODE;
  383. offload.cls_u32->knode.handle = handle;
  384. dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
  385. tp->protocol, &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->protocol, &offload);
  405. if (tc_skip_sw(flags))
  406. return err;
  407. return 0;
  408. }
  409. static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h)
  410. {
  411. struct net_device *dev = tp->q->dev_queue->dev;
  412. struct tc_cls_u32_offload u32_offload = {0};
  413. struct tc_to_netdev offload;
  414. offload.type = TC_SETUP_CLSU32;
  415. offload.cls_u32 = &u32_offload;
  416. if (tc_should_offload(dev, tp, 0)) {
  417. offload.cls_u32->command = TC_CLSU32_DELETE_HNODE;
  418. offload.cls_u32->hnode.divisor = h->divisor;
  419. offload.cls_u32->hnode.handle = h->handle;
  420. offload.cls_u32->hnode.prio = h->prio;
  421. dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
  422. tp->protocol, &offload);
  423. }
  424. }
  425. static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
  426. u32 flags)
  427. {
  428. struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
  429. struct net_device *dev = tp->q->dev_queue->dev;
  430. struct tc_cls_u32_offload u32_offload = {0};
  431. struct tc_to_netdev offload;
  432. int err;
  433. offload.type = TC_SETUP_CLSU32;
  434. offload.cls_u32 = &u32_offload;
  435. if (!tc_should_offload(dev, tp, flags))
  436. return tc_skip_sw(flags) ? -EINVAL : 0;
  437. offload.cls_u32->command = TC_CLSU32_REPLACE_KNODE;
  438. offload.cls_u32->knode.handle = n->handle;
  439. offload.cls_u32->knode.fshift = n->fshift;
  440. #ifdef CONFIG_CLS_U32_MARK
  441. offload.cls_u32->knode.val = n->val;
  442. offload.cls_u32->knode.mask = n->mask;
  443. #else
  444. offload.cls_u32->knode.val = 0;
  445. offload.cls_u32->knode.mask = 0;
  446. #endif
  447. offload.cls_u32->knode.sel = &n->sel;
  448. offload.cls_u32->knode.exts = &n->exts;
  449. if (n->ht_down)
  450. offload.cls_u32->knode.link_handle = ht->handle;
  451. err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
  452. tp->protocol, &offload);
  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_hnode *ht = rtnl_dereference(n->ht_down);
  667. struct tc_u32_sel *s = &n->sel;
  668. struct tc_u_knode *new;
  669. new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
  670. GFP_KERNEL);
  671. if (!new)
  672. return NULL;
  673. RCU_INIT_POINTER(new->next, n->next);
  674. new->handle = n->handle;
  675. RCU_INIT_POINTER(new->ht_up, n->ht_up);
  676. #ifdef CONFIG_NET_CLS_IND
  677. new->ifindex = n->ifindex;
  678. #endif
  679. new->fshift = n->fshift;
  680. new->res = n->res;
  681. new->flags = n->flags;
  682. RCU_INIT_POINTER(new->ht_down, ht);
  683. /* bump reference count as long as we hold pointer to structure */
  684. if (ht)
  685. ht->refcnt++;
  686. #ifdef CONFIG_CLS_U32_PERF
  687. /* Statistics may be incremented by readers during update
  688. * so we must keep them in tact. When the node is later destroyed
  689. * a special destroy call must be made to not free the pf memory.
  690. */
  691. new->pf = n->pf;
  692. #endif
  693. #ifdef CONFIG_CLS_U32_MARK
  694. new->val = n->val;
  695. new->mask = n->mask;
  696. /* Similarly success statistics must be moved as pointers */
  697. new->pcpu_success = n->pcpu_success;
  698. #endif
  699. new->tp = tp;
  700. memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  701. if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
  702. kfree(new);
  703. return NULL;
  704. }
  705. return new;
  706. }
  707. static int u32_change(struct net *net, struct sk_buff *in_skb,
  708. struct tcf_proto *tp, unsigned long base, u32 handle,
  709. struct nlattr **tca, unsigned long *arg, bool ovr)
  710. {
  711. struct tc_u_common *tp_c = tp->data;
  712. struct tc_u_hnode *ht;
  713. struct tc_u_knode *n;
  714. struct tc_u32_sel *s;
  715. struct nlattr *opt = tca[TCA_OPTIONS];
  716. struct nlattr *tb[TCA_U32_MAX + 1];
  717. u32 htid, flags = 0;
  718. int err;
  719. #ifdef CONFIG_CLS_U32_PERF
  720. size_t size;
  721. #endif
  722. if (opt == NULL)
  723. return handle ? -EINVAL : 0;
  724. err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy);
  725. if (err < 0)
  726. return err;
  727. if (tb[TCA_U32_FLAGS]) {
  728. flags = nla_get_u32(tb[TCA_U32_FLAGS]);
  729. if (!tc_flags_valid(flags))
  730. return -EINVAL;
  731. }
  732. n = (struct tc_u_knode *)*arg;
  733. if (n) {
  734. struct tc_u_knode *new;
  735. if (TC_U32_KEY(n->handle) == 0)
  736. return -EINVAL;
  737. if (n->flags != flags)
  738. return -EINVAL;
  739. new = u32_init_knode(tp, n);
  740. if (!new)
  741. return -ENOMEM;
  742. err = u32_set_parms(net, tp, base,
  743. rtnl_dereference(n->ht_up), new, tb,
  744. tca[TCA_RATE], ovr);
  745. if (err) {
  746. u32_destroy_key(tp, new, false);
  747. return err;
  748. }
  749. err = u32_replace_hw_knode(tp, new, flags);
  750. if (err) {
  751. u32_destroy_key(tp, new, false);
  752. return err;
  753. }
  754. u32_replace_knode(tp, tp_c, new);
  755. tcf_unbind_filter(tp, &n->res);
  756. call_rcu(&n->rcu, u32_delete_key_rcu);
  757. return 0;
  758. }
  759. if (tb[TCA_U32_DIVISOR]) {
  760. unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
  761. if (--divisor > 0x100)
  762. return -EINVAL;
  763. if (TC_U32_KEY(handle))
  764. return -EINVAL;
  765. if (handle == 0) {
  766. handle = gen_new_htid(tp->data);
  767. if (handle == 0)
  768. return -ENOMEM;
  769. }
  770. ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
  771. if (ht == NULL)
  772. return -ENOBUFS;
  773. ht->tp_c = tp_c;
  774. ht->refcnt = 1;
  775. ht->divisor = divisor;
  776. ht->handle = handle;
  777. ht->prio = tp->prio;
  778. err = u32_replace_hw_hnode(tp, ht, flags);
  779. if (err) {
  780. kfree(ht);
  781. return err;
  782. }
  783. RCU_INIT_POINTER(ht->next, tp_c->hlist);
  784. rcu_assign_pointer(tp_c->hlist, ht);
  785. *arg = (unsigned long)ht;
  786. return 0;
  787. }
  788. if (tb[TCA_U32_HASH]) {
  789. htid = nla_get_u32(tb[TCA_U32_HASH]);
  790. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  791. ht = rtnl_dereference(tp->root);
  792. htid = ht->handle;
  793. } else {
  794. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  795. if (ht == NULL)
  796. return -EINVAL;
  797. }
  798. } else {
  799. ht = rtnl_dereference(tp->root);
  800. htid = ht->handle;
  801. }
  802. if (ht->divisor < TC_U32_HASH(htid))
  803. return -EINVAL;
  804. if (handle) {
  805. if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
  806. return -EINVAL;
  807. handle = htid | TC_U32_NODE(handle);
  808. } else
  809. handle = gen_new_kid(ht, htid);
  810. if (tb[TCA_U32_SEL] == NULL)
  811. return -EINVAL;
  812. s = nla_data(tb[TCA_U32_SEL]);
  813. n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
  814. if (n == NULL)
  815. return -ENOBUFS;
  816. #ifdef CONFIG_CLS_U32_PERF
  817. size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
  818. n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
  819. if (!n->pf) {
  820. kfree(n);
  821. return -ENOBUFS;
  822. }
  823. #endif
  824. memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  825. RCU_INIT_POINTER(n->ht_up, ht);
  826. n->handle = handle;
  827. n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
  828. n->flags = flags;
  829. n->tp = tp;
  830. err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
  831. if (err < 0)
  832. goto errout;
  833. #ifdef CONFIG_CLS_U32_MARK
  834. n->pcpu_success = alloc_percpu(u32);
  835. if (!n->pcpu_success) {
  836. err = -ENOMEM;
  837. goto errout;
  838. }
  839. if (tb[TCA_U32_MARK]) {
  840. struct tc_u32_mark *mark;
  841. mark = nla_data(tb[TCA_U32_MARK]);
  842. n->val = mark->val;
  843. n->mask = mark->mask;
  844. }
  845. #endif
  846. err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr);
  847. if (err == 0) {
  848. struct tc_u_knode __rcu **ins;
  849. struct tc_u_knode *pins;
  850. err = u32_replace_hw_knode(tp, n, flags);
  851. if (err)
  852. goto errhw;
  853. ins = &ht->ht[TC_U32_HASH(handle)];
  854. for (pins = rtnl_dereference(*ins); pins;
  855. ins = &pins->next, pins = rtnl_dereference(*ins))
  856. if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
  857. break;
  858. RCU_INIT_POINTER(n->next, pins);
  859. rcu_assign_pointer(*ins, n);
  860. *arg = (unsigned long)n;
  861. return 0;
  862. }
  863. errhw:
  864. #ifdef CONFIG_CLS_U32_MARK
  865. free_percpu(n->pcpu_success);
  866. #endif
  867. errout:
  868. tcf_exts_destroy(&n->exts);
  869. #ifdef CONFIG_CLS_U32_PERF
  870. free_percpu(n->pf);
  871. #endif
  872. kfree(n);
  873. return err;
  874. }
  875. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  876. {
  877. struct tc_u_common *tp_c = tp->data;
  878. struct tc_u_hnode *ht;
  879. struct tc_u_knode *n;
  880. unsigned int h;
  881. if (arg->stop)
  882. return;
  883. for (ht = rtnl_dereference(tp_c->hlist);
  884. ht;
  885. ht = rtnl_dereference(ht->next)) {
  886. if (ht->prio != tp->prio)
  887. continue;
  888. if (arg->count >= arg->skip) {
  889. if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
  890. arg->stop = 1;
  891. return;
  892. }
  893. }
  894. arg->count++;
  895. for (h = 0; h <= ht->divisor; h++) {
  896. for (n = rtnl_dereference(ht->ht[h]);
  897. n;
  898. n = rtnl_dereference(n->next)) {
  899. if (arg->count < arg->skip) {
  900. arg->count++;
  901. continue;
  902. }
  903. if (arg->fn(tp, (unsigned long)n, arg) < 0) {
  904. arg->stop = 1;
  905. return;
  906. }
  907. arg->count++;
  908. }
  909. }
  910. }
  911. }
  912. static int u32_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  913. struct sk_buff *skb, struct tcmsg *t)
  914. {
  915. struct tc_u_knode *n = (struct tc_u_knode *)fh;
  916. struct tc_u_hnode *ht_up, *ht_down;
  917. struct nlattr *nest;
  918. if (n == NULL)
  919. return skb->len;
  920. t->tcm_handle = n->handle;
  921. nest = nla_nest_start(skb, TCA_OPTIONS);
  922. if (nest == NULL)
  923. goto nla_put_failure;
  924. if (TC_U32_KEY(n->handle) == 0) {
  925. struct tc_u_hnode *ht = (struct tc_u_hnode *)fh;
  926. u32 divisor = ht->divisor + 1;
  927. if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
  928. goto nla_put_failure;
  929. } else {
  930. #ifdef CONFIG_CLS_U32_PERF
  931. struct tc_u32_pcnt *gpf;
  932. int cpu;
  933. #endif
  934. if (nla_put(skb, TCA_U32_SEL,
  935. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  936. &n->sel))
  937. goto nla_put_failure;
  938. ht_up = rtnl_dereference(n->ht_up);
  939. if (ht_up) {
  940. u32 htid = n->handle & 0xFFFFF000;
  941. if (nla_put_u32(skb, TCA_U32_HASH, htid))
  942. goto nla_put_failure;
  943. }
  944. if (n->res.classid &&
  945. nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
  946. goto nla_put_failure;
  947. ht_down = rtnl_dereference(n->ht_down);
  948. if (ht_down &&
  949. nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
  950. goto nla_put_failure;
  951. if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
  952. goto nla_put_failure;
  953. #ifdef CONFIG_CLS_U32_MARK
  954. if ((n->val || n->mask)) {
  955. struct tc_u32_mark mark = {.val = n->val,
  956. .mask = n->mask,
  957. .success = 0};
  958. int cpum;
  959. for_each_possible_cpu(cpum) {
  960. __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
  961. mark.success += cnt;
  962. }
  963. if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
  964. goto nla_put_failure;
  965. }
  966. #endif
  967. if (tcf_exts_dump(skb, &n->exts) < 0)
  968. goto nla_put_failure;
  969. #ifdef CONFIG_NET_CLS_IND
  970. if (n->ifindex) {
  971. struct net_device *dev;
  972. dev = __dev_get_by_index(net, n->ifindex);
  973. if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
  974. goto nla_put_failure;
  975. }
  976. #endif
  977. #ifdef CONFIG_CLS_U32_PERF
  978. gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
  979. n->sel.nkeys * sizeof(u64),
  980. GFP_KERNEL);
  981. if (!gpf)
  982. goto nla_put_failure;
  983. for_each_possible_cpu(cpu) {
  984. int i;
  985. struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
  986. gpf->rcnt += pf->rcnt;
  987. gpf->rhit += pf->rhit;
  988. for (i = 0; i < n->sel.nkeys; i++)
  989. gpf->kcnts[i] += pf->kcnts[i];
  990. }
  991. if (nla_put_64bit(skb, TCA_U32_PCNT,
  992. sizeof(struct tc_u32_pcnt) +
  993. n->sel.nkeys * sizeof(u64),
  994. gpf, TCA_U32_PAD)) {
  995. kfree(gpf);
  996. goto nla_put_failure;
  997. }
  998. kfree(gpf);
  999. #endif
  1000. }
  1001. nla_nest_end(skb, nest);
  1002. if (TC_U32_KEY(n->handle))
  1003. if (tcf_exts_dump_stats(skb, &n->exts) < 0)
  1004. goto nla_put_failure;
  1005. return skb->len;
  1006. nla_put_failure:
  1007. nla_nest_cancel(skb, nest);
  1008. return -1;
  1009. }
  1010. static struct tcf_proto_ops cls_u32_ops __read_mostly = {
  1011. .kind = "u32",
  1012. .classify = u32_classify,
  1013. .init = u32_init,
  1014. .destroy = u32_destroy,
  1015. .get = u32_get,
  1016. .change = u32_change,
  1017. .delete = u32_delete,
  1018. .walk = u32_walk,
  1019. .dump = u32_dump,
  1020. .owner = THIS_MODULE,
  1021. };
  1022. static int __init init_u32(void)
  1023. {
  1024. pr_info("u32 classifier\n");
  1025. #ifdef CONFIG_CLS_U32_PERF
  1026. pr_info(" Performance counters on\n");
  1027. #endif
  1028. #ifdef CONFIG_NET_CLS_IND
  1029. pr_info(" input device check on\n");
  1030. #endif
  1031. #ifdef CONFIG_NET_CLS_ACT
  1032. pr_info(" Actions configured\n");
  1033. #endif
  1034. return register_tcf_proto_ops(&cls_u32_ops);
  1035. }
  1036. static void __exit exit_u32(void)
  1037. {
  1038. unregister_tcf_proto_ops(&cls_u32_ops);
  1039. }
  1040. module_init(init_u32)
  1041. module_exit(exit_u32)
  1042. MODULE_LICENSE("GPL");