cls_u32.c 24 KB

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