cls_u32.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. if (tp_c->refcnt > 1)
  425. return false;
  426. if (tp_c->refcnt == 1) {
  427. struct tc_u_hnode *ht;
  428. for (ht = rtnl_dereference(tp_c->hlist);
  429. ht;
  430. ht = rtnl_dereference(ht->next))
  431. if (!ht_empty(ht))
  432. return false;
  433. }
  434. }
  435. if (root_ht && --root_ht->refcnt == 0)
  436. u32_destroy_hnode(tp, root_ht);
  437. if (--tp_c->refcnt == 0) {
  438. struct tc_u_hnode *ht;
  439. tp->q->u32_node = NULL;
  440. for (ht = rtnl_dereference(tp_c->hlist);
  441. ht;
  442. ht = rtnl_dereference(ht->next)) {
  443. ht->refcnt--;
  444. u32_clear_hnode(tp, ht);
  445. }
  446. while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
  447. RCU_INIT_POINTER(tp_c->hlist, ht->next);
  448. kfree_rcu(ht, rcu);
  449. }
  450. kfree(tp_c);
  451. }
  452. tp->data = NULL;
  453. return true;
  454. }
  455. static int u32_delete(struct tcf_proto *tp, unsigned long arg)
  456. {
  457. struct tc_u_hnode *ht = (struct tc_u_hnode *)arg;
  458. struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
  459. if (ht == NULL)
  460. return 0;
  461. if (TC_U32_KEY(ht->handle))
  462. return u32_delete_key(tp, (struct tc_u_knode *)ht);
  463. if (root_ht == ht)
  464. return -EINVAL;
  465. if (ht->refcnt == 1) {
  466. ht->refcnt--;
  467. u32_destroy_hnode(tp, ht);
  468. } else {
  469. return -EBUSY;
  470. }
  471. return 0;
  472. }
  473. #define NR_U32_NODE (1<<12)
  474. static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
  475. {
  476. struct tc_u_knode *n;
  477. unsigned long i;
  478. unsigned long *bitmap = kzalloc(BITS_TO_LONGS(NR_U32_NODE) * sizeof(unsigned long),
  479. GFP_KERNEL);
  480. if (!bitmap)
  481. return handle | 0xFFF;
  482. for (n = rtnl_dereference(ht->ht[TC_U32_HASH(handle)]);
  483. n;
  484. n = rtnl_dereference(n->next))
  485. set_bit(TC_U32_NODE(n->handle), bitmap);
  486. i = find_next_zero_bit(bitmap, NR_U32_NODE, 0x800);
  487. if (i >= NR_U32_NODE)
  488. i = find_next_zero_bit(bitmap, NR_U32_NODE, 1);
  489. kfree(bitmap);
  490. return handle | (i >= NR_U32_NODE ? 0xFFF : i);
  491. }
  492. static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
  493. [TCA_U32_CLASSID] = { .type = NLA_U32 },
  494. [TCA_U32_HASH] = { .type = NLA_U32 },
  495. [TCA_U32_LINK] = { .type = NLA_U32 },
  496. [TCA_U32_DIVISOR] = { .type = NLA_U32 },
  497. [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
  498. [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
  499. [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
  500. };
  501. static int u32_set_parms(struct net *net, struct tcf_proto *tp,
  502. unsigned long base, struct tc_u_hnode *ht,
  503. struct tc_u_knode *n, struct nlattr **tb,
  504. struct nlattr *est, bool ovr)
  505. {
  506. int err;
  507. struct tcf_exts e;
  508. tcf_exts_init(&e, TCA_U32_ACT, TCA_U32_POLICE);
  509. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  510. if (err < 0)
  511. return err;
  512. err = -EINVAL;
  513. if (tb[TCA_U32_LINK]) {
  514. u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
  515. struct tc_u_hnode *ht_down = NULL, *ht_old;
  516. if (TC_U32_KEY(handle))
  517. goto errout;
  518. if (handle) {
  519. ht_down = u32_lookup_ht(ht->tp_c, handle);
  520. if (ht_down == NULL)
  521. goto errout;
  522. ht_down->refcnt++;
  523. }
  524. ht_old = rtnl_dereference(n->ht_down);
  525. rcu_assign_pointer(n->ht_down, ht_down);
  526. if (ht_old)
  527. ht_old->refcnt--;
  528. }
  529. if (tb[TCA_U32_CLASSID]) {
  530. n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
  531. tcf_bind_filter(tp, &n->res, base);
  532. }
  533. #ifdef CONFIG_NET_CLS_IND
  534. if (tb[TCA_U32_INDEV]) {
  535. int ret;
  536. ret = tcf_change_indev(net, tb[TCA_U32_INDEV]);
  537. if (ret < 0)
  538. goto errout;
  539. n->ifindex = ret;
  540. }
  541. #endif
  542. tcf_exts_change(tp, &n->exts, &e);
  543. return 0;
  544. errout:
  545. tcf_exts_destroy(&e);
  546. return err;
  547. }
  548. static void u32_replace_knode(struct tcf_proto *tp,
  549. struct tc_u_common *tp_c,
  550. struct tc_u_knode *n)
  551. {
  552. struct tc_u_knode __rcu **ins;
  553. struct tc_u_knode *pins;
  554. struct tc_u_hnode *ht;
  555. if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
  556. ht = rtnl_dereference(tp->root);
  557. else
  558. ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
  559. ins = &ht->ht[TC_U32_HASH(n->handle)];
  560. /* The node must always exist for it to be replaced if this is not the
  561. * case then something went very wrong elsewhere.
  562. */
  563. for (pins = rtnl_dereference(*ins); ;
  564. ins = &pins->next, pins = rtnl_dereference(*ins))
  565. if (pins->handle == n->handle)
  566. break;
  567. RCU_INIT_POINTER(n->next, pins->next);
  568. rcu_assign_pointer(*ins, n);
  569. }
  570. static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
  571. struct tc_u_knode *n)
  572. {
  573. struct tc_u_knode *new;
  574. struct tc_u32_sel *s = &n->sel;
  575. new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
  576. GFP_KERNEL);
  577. if (!new)
  578. return NULL;
  579. RCU_INIT_POINTER(new->next, n->next);
  580. new->handle = n->handle;
  581. RCU_INIT_POINTER(new->ht_up, n->ht_up);
  582. #ifdef CONFIG_NET_CLS_IND
  583. new->ifindex = n->ifindex;
  584. #endif
  585. new->fshift = n->fshift;
  586. new->res = n->res;
  587. RCU_INIT_POINTER(new->ht_down, n->ht_down);
  588. /* bump reference count as long as we hold pointer to structure */
  589. if (new->ht_down)
  590. new->ht_down->refcnt++;
  591. #ifdef CONFIG_CLS_U32_PERF
  592. /* Statistics may be incremented by readers during update
  593. * so we must keep them in tact. When the node is later destroyed
  594. * a special destroy call must be made to not free the pf memory.
  595. */
  596. new->pf = n->pf;
  597. #endif
  598. #ifdef CONFIG_CLS_U32_MARK
  599. new->val = n->val;
  600. new->mask = n->mask;
  601. /* Similarly success statistics must be moved as pointers */
  602. new->pcpu_success = n->pcpu_success;
  603. #endif
  604. new->tp = tp;
  605. memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  606. tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE);
  607. return new;
  608. }
  609. static int u32_change(struct net *net, struct sk_buff *in_skb,
  610. struct tcf_proto *tp, unsigned long base, u32 handle,
  611. struct nlattr **tca,
  612. unsigned long *arg, bool ovr)
  613. {
  614. struct tc_u_common *tp_c = tp->data;
  615. struct tc_u_hnode *ht;
  616. struct tc_u_knode *n;
  617. struct tc_u32_sel *s;
  618. struct nlattr *opt = tca[TCA_OPTIONS];
  619. struct nlattr *tb[TCA_U32_MAX + 1];
  620. u32 htid;
  621. int err;
  622. #ifdef CONFIG_CLS_U32_PERF
  623. size_t size;
  624. #endif
  625. if (opt == NULL)
  626. return handle ? -EINVAL : 0;
  627. err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy);
  628. if (err < 0)
  629. return err;
  630. n = (struct tc_u_knode *)*arg;
  631. if (n) {
  632. struct tc_u_knode *new;
  633. if (TC_U32_KEY(n->handle) == 0)
  634. return -EINVAL;
  635. new = u32_init_knode(tp, n);
  636. if (!new)
  637. return -ENOMEM;
  638. err = u32_set_parms(net, tp, base,
  639. rtnl_dereference(n->ht_up), new, tb,
  640. tca[TCA_RATE], ovr);
  641. if (err) {
  642. u32_destroy_key(tp, new, false);
  643. return err;
  644. }
  645. u32_replace_knode(tp, tp_c, new);
  646. tcf_unbind_filter(tp, &n->res);
  647. call_rcu(&n->rcu, u32_delete_key_rcu);
  648. return 0;
  649. }
  650. if (tb[TCA_U32_DIVISOR]) {
  651. unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
  652. if (--divisor > 0x100)
  653. return -EINVAL;
  654. if (TC_U32_KEY(handle))
  655. return -EINVAL;
  656. if (handle == 0) {
  657. handle = gen_new_htid(tp->data);
  658. if (handle == 0)
  659. return -ENOMEM;
  660. }
  661. ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
  662. if (ht == NULL)
  663. return -ENOBUFS;
  664. ht->tp_c = tp_c;
  665. ht->refcnt = 1;
  666. ht->divisor = divisor;
  667. ht->handle = handle;
  668. ht->prio = tp->prio;
  669. RCU_INIT_POINTER(ht->next, tp_c->hlist);
  670. rcu_assign_pointer(tp_c->hlist, ht);
  671. *arg = (unsigned long)ht;
  672. return 0;
  673. }
  674. if (tb[TCA_U32_HASH]) {
  675. htid = nla_get_u32(tb[TCA_U32_HASH]);
  676. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  677. ht = rtnl_dereference(tp->root);
  678. htid = ht->handle;
  679. } else {
  680. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  681. if (ht == NULL)
  682. return -EINVAL;
  683. }
  684. } else {
  685. ht = rtnl_dereference(tp->root);
  686. htid = ht->handle;
  687. }
  688. if (ht->divisor < TC_U32_HASH(htid))
  689. return -EINVAL;
  690. if (handle) {
  691. if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
  692. return -EINVAL;
  693. handle = htid | TC_U32_NODE(handle);
  694. } else
  695. handle = gen_new_kid(ht, htid);
  696. if (tb[TCA_U32_SEL] == NULL)
  697. return -EINVAL;
  698. s = nla_data(tb[TCA_U32_SEL]);
  699. n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
  700. if (n == NULL)
  701. return -ENOBUFS;
  702. #ifdef CONFIG_CLS_U32_PERF
  703. size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
  704. n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
  705. if (!n->pf) {
  706. kfree(n);
  707. return -ENOBUFS;
  708. }
  709. #endif
  710. memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
  711. RCU_INIT_POINTER(n->ht_up, ht);
  712. n->handle = handle;
  713. n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
  714. tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
  715. n->tp = tp;
  716. #ifdef CONFIG_CLS_U32_MARK
  717. n->pcpu_success = alloc_percpu(u32);
  718. if (!n->pcpu_success) {
  719. err = -ENOMEM;
  720. goto errout;
  721. }
  722. if (tb[TCA_U32_MARK]) {
  723. struct tc_u32_mark *mark;
  724. mark = nla_data(tb[TCA_U32_MARK]);
  725. n->val = mark->val;
  726. n->mask = mark->mask;
  727. }
  728. #endif
  729. err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr);
  730. if (err == 0) {
  731. struct tc_u_knode __rcu **ins;
  732. struct tc_u_knode *pins;
  733. ins = &ht->ht[TC_U32_HASH(handle)];
  734. for (pins = rtnl_dereference(*ins); pins;
  735. ins = &pins->next, pins = rtnl_dereference(*ins))
  736. if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
  737. break;
  738. RCU_INIT_POINTER(n->next, pins);
  739. rcu_assign_pointer(*ins, n);
  740. *arg = (unsigned long)n;
  741. return 0;
  742. }
  743. #ifdef CONFIG_CLS_U32_MARK
  744. free_percpu(n->pcpu_success);
  745. errout:
  746. #endif
  747. #ifdef CONFIG_CLS_U32_PERF
  748. free_percpu(n->pf);
  749. #endif
  750. kfree(n);
  751. return err;
  752. }
  753. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  754. {
  755. struct tc_u_common *tp_c = tp->data;
  756. struct tc_u_hnode *ht;
  757. struct tc_u_knode *n;
  758. unsigned int h;
  759. if (arg->stop)
  760. return;
  761. for (ht = rtnl_dereference(tp_c->hlist);
  762. ht;
  763. ht = rtnl_dereference(ht->next)) {
  764. if (ht->prio != tp->prio)
  765. continue;
  766. if (arg->count >= arg->skip) {
  767. if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
  768. arg->stop = 1;
  769. return;
  770. }
  771. }
  772. arg->count++;
  773. for (h = 0; h <= ht->divisor; h++) {
  774. for (n = rtnl_dereference(ht->ht[h]);
  775. n;
  776. n = rtnl_dereference(n->next)) {
  777. if (arg->count < arg->skip) {
  778. arg->count++;
  779. continue;
  780. }
  781. if (arg->fn(tp, (unsigned long)n, arg) < 0) {
  782. arg->stop = 1;
  783. return;
  784. }
  785. arg->count++;
  786. }
  787. }
  788. }
  789. }
  790. static int u32_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  791. struct sk_buff *skb, struct tcmsg *t)
  792. {
  793. struct tc_u_knode *n = (struct tc_u_knode *)fh;
  794. struct tc_u_hnode *ht_up, *ht_down;
  795. struct nlattr *nest;
  796. if (n == NULL)
  797. return skb->len;
  798. t->tcm_handle = n->handle;
  799. nest = nla_nest_start(skb, TCA_OPTIONS);
  800. if (nest == NULL)
  801. goto nla_put_failure;
  802. if (TC_U32_KEY(n->handle) == 0) {
  803. struct tc_u_hnode *ht = (struct tc_u_hnode *)fh;
  804. u32 divisor = ht->divisor + 1;
  805. if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
  806. goto nla_put_failure;
  807. } else {
  808. #ifdef CONFIG_CLS_U32_PERF
  809. struct tc_u32_pcnt *gpf;
  810. int cpu;
  811. #endif
  812. if (nla_put(skb, TCA_U32_SEL,
  813. sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
  814. &n->sel))
  815. goto nla_put_failure;
  816. ht_up = rtnl_dereference(n->ht_up);
  817. if (ht_up) {
  818. u32 htid = n->handle & 0xFFFFF000;
  819. if (nla_put_u32(skb, TCA_U32_HASH, htid))
  820. goto nla_put_failure;
  821. }
  822. if (n->res.classid &&
  823. nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
  824. goto nla_put_failure;
  825. ht_down = rtnl_dereference(n->ht_down);
  826. if (ht_down &&
  827. nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
  828. goto nla_put_failure;
  829. #ifdef CONFIG_CLS_U32_MARK
  830. if ((n->val || n->mask)) {
  831. struct tc_u32_mark mark = {.val = n->val,
  832. .mask = n->mask,
  833. .success = 0};
  834. int cpum;
  835. for_each_possible_cpu(cpum) {
  836. __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
  837. mark.success += cnt;
  838. }
  839. if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
  840. goto nla_put_failure;
  841. }
  842. #endif
  843. if (tcf_exts_dump(skb, &n->exts) < 0)
  844. goto nla_put_failure;
  845. #ifdef CONFIG_NET_CLS_IND
  846. if (n->ifindex) {
  847. struct net_device *dev;
  848. dev = __dev_get_by_index(net, n->ifindex);
  849. if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
  850. goto nla_put_failure;
  851. }
  852. #endif
  853. #ifdef CONFIG_CLS_U32_PERF
  854. gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
  855. n->sel.nkeys * sizeof(u64),
  856. GFP_KERNEL);
  857. if (!gpf)
  858. goto nla_put_failure;
  859. for_each_possible_cpu(cpu) {
  860. int i;
  861. struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
  862. gpf->rcnt += pf->rcnt;
  863. gpf->rhit += pf->rhit;
  864. for (i = 0; i < n->sel.nkeys; i++)
  865. gpf->kcnts[i] += pf->kcnts[i];
  866. }
  867. if (nla_put(skb, TCA_U32_PCNT,
  868. sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
  869. gpf)) {
  870. kfree(gpf);
  871. goto nla_put_failure;
  872. }
  873. kfree(gpf);
  874. #endif
  875. }
  876. nla_nest_end(skb, nest);
  877. if (TC_U32_KEY(n->handle))
  878. if (tcf_exts_dump_stats(skb, &n->exts) < 0)
  879. goto nla_put_failure;
  880. return skb->len;
  881. nla_put_failure:
  882. nla_nest_cancel(skb, nest);
  883. return -1;
  884. }
  885. static struct tcf_proto_ops cls_u32_ops __read_mostly = {
  886. .kind = "u32",
  887. .classify = u32_classify,
  888. .init = u32_init,
  889. .destroy = u32_destroy,
  890. .get = u32_get,
  891. .change = u32_change,
  892. .delete = u32_delete,
  893. .walk = u32_walk,
  894. .dump = u32_dump,
  895. .owner = THIS_MODULE,
  896. };
  897. static int __init init_u32(void)
  898. {
  899. pr_info("u32 classifier\n");
  900. #ifdef CONFIG_CLS_U32_PERF
  901. pr_info(" Performance counters on\n");
  902. #endif
  903. #ifdef CONFIG_NET_CLS_IND
  904. pr_info(" input device check on\n");
  905. #endif
  906. #ifdef CONFIG_NET_CLS_ACT
  907. pr_info(" Actions configured\n");
  908. #endif
  909. return register_tcf_proto_ops(&cls_u32_ops);
  910. }
  911. static void __exit exit_u32(void)
  912. {
  913. unregister_tcf_proto_ops(&cls_u32_ops);
  914. }
  915. module_init(init_u32)
  916. module_exit(exit_u32)
  917. MODULE_LICENSE("GPL");