cls_u32.c 23 KB

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