cls_u32.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/cls_u32.c Ugly (or Universal) 32bit key Packet Classifier.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. *
  7. * The filters are packed to hash tables of key nodes
  8. * with a set of 32bit key/mask pairs at every node.
  9. * Nodes reference next level hash tables etc.
  10. *
  11. * This scheme is the best universal classifier I managed to
  12. * invent; it is not super-fast, but it is not slow (provided you
  13. * program it correctly), and general enough. And its relative
  14. * speed grows as the number of rules becomes larger.
  15. *
  16. * It seems that it represents the best middle point between
  17. * speed and manageability both by human and by machine.
  18. *
  19. * It is especially useful for link sharing combined with QoS;
  20. * pure RSVP doesn't need such a general approach and can use
  21. * much simpler (and faster) schemes, sort of cls_rsvp.c.
  22. *
  23. * nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro>
  24. */
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/types.h>
  28. #include <linux/kernel.h>
  29. #include <linux/string.h>
  30. #include <linux/errno.h>
  31. #include <linux/percpu.h>
  32. #include <linux/rtnetlink.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/bitmap.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/hash.h>
  37. #include <net/netlink.h>
  38. #include <net/act_api.h>
  39. #include <net/pkt_cls.h>
  40. #include <linux/idr.h>
  41. #include <net/tc_wrapper.h>
  42. struct tc_u_knode {
  43. struct tc_u_knode __rcu *next;
  44. u32 handle;
  45. struct tc_u_hnode __rcu *ht_up;
  46. struct tcf_exts exts;
  47. int ifindex;
  48. u8 fshift;
  49. struct tcf_result res;
  50. struct tc_u_hnode __rcu *ht_down;
  51. #ifdef CONFIG_CLS_U32_PERF
  52. struct tc_u32_pcnt __percpu *pf;
  53. #endif
  54. u32 flags;
  55. unsigned int in_hw_count;
  56. #ifdef CONFIG_CLS_U32_MARK
  57. u32 val;
  58. u32 mask;
  59. u32 __percpu *pcpu_success;
  60. #endif
  61. struct rcu_work rwork;
  62. /* The 'sel' field MUST be the last field in structure to allow for
  63. * tc_u32_keys allocated at end of structure.
  64. */
  65. struct tc_u32_sel sel;
  66. };
  67. struct tc_u_hnode {
  68. struct tc_u_hnode __rcu *next;
  69. u32 handle;
  70. u32 prio;
  71. refcount_t refcnt;
  72. unsigned int divisor;
  73. struct idr handle_idr;
  74. bool is_root;
  75. struct rcu_head rcu;
  76. u32 flags;
  77. /* The 'ht' field MUST be the last field in structure to allow for
  78. * more entries allocated at end of structure.
  79. */
  80. struct tc_u_knode __rcu *ht[];
  81. };
  82. struct tc_u_common {
  83. struct tc_u_hnode __rcu *hlist;
  84. void *ptr;
  85. refcount_t refcnt;
  86. struct idr handle_idr;
  87. struct hlist_node hnode;
  88. long knodes;
  89. };
  90. static u32 handle2id(u32 h)
  91. {
  92. return ((h & 0x80000000) ? ((h >> 20) & 0x7FF) : h);
  93. }
  94. static u32 id2handle(u32 id)
  95. {
  96. return (id | 0x800U) << 20;
  97. }
  98. static inline unsigned int u32_hash_fold(__be32 key,
  99. const struct tc_u32_sel *sel,
  100. u8 fshift)
  101. {
  102. unsigned int h = ntohl(key & sel->hmask) >> fshift;
  103. return h;
  104. }
  105. TC_INDIRECT_SCOPE int u32_classify(struct sk_buff *skb,
  106. const struct tcf_proto *tp,
  107. struct tcf_result *res)
  108. {
  109. struct {
  110. struct tc_u_knode *knode;
  111. unsigned int off;
  112. } stack[TC_U32_MAXDEPTH];
  113. struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
  114. unsigned int off = skb_network_offset(skb);
  115. struct tc_u_knode *n;
  116. int sdepth = 0;
  117. int off2 = 0;
  118. int sel = 0;
  119. #ifdef CONFIG_CLS_U32_PERF
  120. int j;
  121. #endif
  122. int i, r;
  123. next_ht:
  124. n = rcu_dereference_bh(ht->ht[sel]);
  125. next_knode:
  126. if (n) {
  127. struct tc_u32_key *key = n->sel.keys;
  128. #ifdef CONFIG_CLS_U32_PERF
  129. __this_cpu_inc(n->pf->rcnt);
  130. j = 0;
  131. #endif
  132. if (tc_skip_sw(n->flags)) {
  133. n = rcu_dereference_bh(n->next);
  134. goto next_knode;
  135. }
  136. #ifdef CONFIG_CLS_U32_MARK
  137. if ((skb->mark & n->mask) != n->val) {
  138. n = rcu_dereference_bh(n->next);
  139. goto next_knode;
  140. } else {
  141. __this_cpu_inc(*n->pcpu_success);
  142. }
  143. #endif
  144. for (i = n->sel.nkeys; i > 0; i--, key++) {
  145. int toff = off + key->off + (off2 & key->offmask);
  146. __be32 *data, hdata;
  147. if (skb_headroom(skb) + toff > INT_MAX)
  148. goto out;
  149. data = skb_header_pointer(skb, toff, 4, &hdata);
  150. if (!data)
  151. goto out;
  152. if ((*data ^ key->val) & key->mask) {
  153. n = rcu_dereference_bh(n->next);
  154. goto next_knode;
  155. }
  156. #ifdef CONFIG_CLS_U32_PERF
  157. __this_cpu_inc(n->pf->kcnts[j]);
  158. j++;
  159. #endif
  160. }
  161. ht = rcu_dereference_bh(n->ht_down);
  162. if (!ht) {
  163. check_terminal:
  164. if (n->sel.flags & TC_U32_TERMINAL) {
  165. *res = n->res;
  166. if (!tcf_match_indev(skb, n->ifindex)) {
  167. n = rcu_dereference_bh(n->next);
  168. goto next_knode;
  169. }
  170. #ifdef CONFIG_CLS_U32_PERF
  171. __this_cpu_inc(n->pf->rhit);
  172. #endif
  173. r = tcf_exts_exec(skb, &n->exts, res);
  174. if (r < 0) {
  175. n = rcu_dereference_bh(n->next);
  176. goto next_knode;
  177. }
  178. return r;
  179. }
  180. n = rcu_dereference_bh(n->next);
  181. goto next_knode;
  182. }
  183. /* PUSH */
  184. if (sdepth >= TC_U32_MAXDEPTH)
  185. goto deadloop;
  186. stack[sdepth].knode = n;
  187. stack[sdepth].off = off;
  188. sdepth++;
  189. ht = rcu_dereference_bh(n->ht_down);
  190. sel = 0;
  191. if (ht->divisor) {
  192. __be32 *data, hdata;
  193. data = skb_header_pointer(skb, off + n->sel.hoff, 4,
  194. &hdata);
  195. if (!data)
  196. goto out;
  197. sel = ht->divisor & u32_hash_fold(*data, &n->sel,
  198. n->fshift);
  199. }
  200. if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
  201. goto next_ht;
  202. if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
  203. off2 = n->sel.off + 3;
  204. if (n->sel.flags & TC_U32_VAROFFSET) {
  205. __be16 *data, hdata;
  206. data = skb_header_pointer(skb,
  207. off + n->sel.offoff,
  208. 2, &hdata);
  209. if (!data)
  210. goto out;
  211. off2 += ntohs(n->sel.offmask & *data) >>
  212. n->sel.offshift;
  213. }
  214. off2 &= ~3;
  215. }
  216. if (n->sel.flags & TC_U32_EAT) {
  217. off += off2;
  218. off2 = 0;
  219. }
  220. if (off < skb->len)
  221. goto next_ht;
  222. }
  223. /* POP */
  224. if (sdepth--) {
  225. n = stack[sdepth].knode;
  226. ht = rcu_dereference_bh(n->ht_up);
  227. off = stack[sdepth].off;
  228. goto check_terminal;
  229. }
  230. out:
  231. return -1;
  232. deadloop:
  233. net_warn_ratelimited("cls_u32: dead loop\n");
  234. return -1;
  235. }
  236. static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
  237. {
  238. struct tc_u_hnode *ht;
  239. for (ht = rtnl_dereference(tp_c->hlist);
  240. ht;
  241. ht = rtnl_dereference(ht->next))
  242. if (ht->handle == handle)
  243. break;
  244. return ht;
  245. }
  246. static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
  247. {
  248. unsigned int sel;
  249. struct tc_u_knode *n = NULL;
  250. sel = TC_U32_HASH(handle);
  251. if (sel > ht->divisor)
  252. goto out;
  253. for (n = rtnl_dereference(ht->ht[sel]);
  254. n;
  255. n = rtnl_dereference(n->next))
  256. if (n->handle == handle)
  257. break;
  258. out:
  259. return n;
  260. }
  261. static void *u32_get(struct tcf_proto *tp, u32 handle)
  262. {
  263. struct tc_u_hnode *ht;
  264. struct tc_u_common *tp_c = tp->data;
  265. if (TC_U32_HTID(handle) == TC_U32_ROOT)
  266. ht = rtnl_dereference(tp->root);
  267. else
  268. ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
  269. if (!ht)
  270. return NULL;
  271. if (TC_U32_KEY(handle) == 0)
  272. return ht;
  273. return u32_lookup_key(ht, handle);
  274. }
  275. /* Protected by rtnl lock */
  276. static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
  277. {
  278. int id = idr_alloc_cyclic(&tp_c->handle_idr, ptr, 1, 0x7FF, GFP_KERNEL);
  279. if (id < 0)
  280. return 0;
  281. return id2handle(id);
  282. }
  283. static struct hlist_head *tc_u_common_hash;
  284. #define U32_HASH_SHIFT 10
  285. #define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
  286. static void *tc_u_common_ptr(const struct tcf_proto *tp)
  287. {
  288. struct tcf_block *block = tp->chain->block;
  289. /* The block sharing is currently supported only
  290. * for classless qdiscs. In that case we use block
  291. * for tc_u_common identification. In case the
  292. * block is not shared, block->q is a valid pointer
  293. * and we can use that. That works for classful qdiscs.
  294. */
  295. if (tcf_block_shared(block))
  296. return block;
  297. else
  298. return block->q;
  299. }
  300. static struct hlist_head *tc_u_hash(void *key)
  301. {
  302. return tc_u_common_hash + hash_ptr(key, U32_HASH_SHIFT);
  303. }
  304. static struct tc_u_common *tc_u_common_find(void *key)
  305. {
  306. struct tc_u_common *tc;
  307. hlist_for_each_entry(tc, tc_u_hash(key), hnode) {
  308. if (tc->ptr == key)
  309. return tc;
  310. }
  311. return NULL;
  312. }
  313. static int u32_init(struct tcf_proto *tp)
  314. {
  315. struct tc_u_hnode *root_ht;
  316. void *key = tc_u_common_ptr(tp);
  317. struct tc_u_common *tp_c = tc_u_common_find(key);
  318. root_ht = kzalloc(struct_size(root_ht, ht, 1), GFP_KERNEL);
  319. if (root_ht == NULL)
  320. return -ENOBUFS;
  321. refcount_set(&root_ht->refcnt, 1);
  322. root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : id2handle(0);
  323. root_ht->prio = tp->prio;
  324. root_ht->is_root = true;
  325. idr_init(&root_ht->handle_idr);
  326. if (tp_c == NULL) {
  327. tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
  328. if (tp_c == NULL) {
  329. kfree(root_ht);
  330. return -ENOBUFS;
  331. }
  332. refcount_set(&tp_c->refcnt, 1);
  333. tp_c->ptr = key;
  334. INIT_HLIST_NODE(&tp_c->hnode);
  335. idr_init(&tp_c->handle_idr);
  336. hlist_add_head(&tp_c->hnode, tc_u_hash(key));
  337. } else {
  338. refcount_inc(&tp_c->refcnt);
  339. }
  340. RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
  341. rcu_assign_pointer(tp_c->hlist, root_ht);
  342. /* root_ht must be destroyed when tcf_proto is destroyed */
  343. rcu_assign_pointer(tp->root, root_ht);
  344. tp->data = tp_c;
  345. return 0;
  346. }
  347. static void __u32_destroy_key(struct tc_u_knode *n)
  348. {
  349. struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
  350. tcf_exts_destroy(&n->exts);
  351. if (ht && refcount_dec_and_test(&ht->refcnt))
  352. kfree(ht);
  353. kfree(n);
  354. }
  355. static void u32_destroy_key(struct tc_u_knode *n, bool free_pf)
  356. {
  357. tcf_exts_put_net(&n->exts);
  358. #ifdef CONFIG_CLS_U32_PERF
  359. if (free_pf)
  360. free_percpu(n->pf);
  361. #endif
  362. #ifdef CONFIG_CLS_U32_MARK
  363. if (free_pf)
  364. free_percpu(n->pcpu_success);
  365. #endif
  366. __u32_destroy_key(n);
  367. }
  368. /* u32_delete_key_rcu should be called when free'ing a copied
  369. * version of a tc_u_knode obtained from u32_init_knode(). When
  370. * copies are obtained from u32_init_knode() the statistics are
  371. * shared between the old and new copies to allow readers to
  372. * continue to update the statistics during the copy. To support
  373. * this the u32_delete_key_rcu variant does not free the percpu
  374. * statistics.
  375. */
  376. static void u32_delete_key_work(struct work_struct *work)
  377. {
  378. struct tc_u_knode *key = container_of(to_rcu_work(work),
  379. struct tc_u_knode,
  380. rwork);
  381. rtnl_lock();
  382. u32_destroy_key(key, false);
  383. rtnl_unlock();
  384. }
  385. /* u32_delete_key_freepf_rcu is the rcu callback variant
  386. * that free's the entire structure including the statistics
  387. * percpu variables. Only use this if the key is not a copy
  388. * returned by u32_init_knode(). See u32_delete_key_rcu()
  389. * for the variant that should be used with keys return from
  390. * u32_init_knode()
  391. */
  392. static void u32_delete_key_freepf_work(struct work_struct *work)
  393. {
  394. struct tc_u_knode *key = container_of(to_rcu_work(work),
  395. struct tc_u_knode,
  396. rwork);
  397. rtnl_lock();
  398. u32_destroy_key(key, true);
  399. rtnl_unlock();
  400. }
  401. static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
  402. {
  403. struct tc_u_common *tp_c = tp->data;
  404. struct tc_u_knode __rcu **kp;
  405. struct tc_u_knode *pkp;
  406. struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
  407. if (ht) {
  408. kp = &ht->ht[TC_U32_HASH(key->handle)];
  409. for (pkp = rtnl_dereference(*kp); pkp;
  410. kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
  411. if (pkp == key) {
  412. RCU_INIT_POINTER(*kp, key->next);
  413. tp_c->knodes--;
  414. tcf_unbind_filter(tp, &key->res);
  415. idr_remove(&ht->handle_idr, key->handle);
  416. tcf_exts_get_net(&key->exts);
  417. tcf_queue_work(&key->rwork, u32_delete_key_freepf_work);
  418. return 0;
  419. }
  420. }
  421. }
  422. WARN_ON(1);
  423. return 0;
  424. }
  425. static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
  426. struct netlink_ext_ack *extack)
  427. {
  428. struct tcf_block *block = tp->chain->block;
  429. struct tc_cls_u32_offload cls_u32 = {};
  430. tc_cls_common_offload_init(&cls_u32.common, tp, h->flags, extack);
  431. cls_u32.command = TC_CLSU32_DELETE_HNODE;
  432. cls_u32.hnode.divisor = h->divisor;
  433. cls_u32.hnode.handle = h->handle;
  434. cls_u32.hnode.prio = h->prio;
  435. tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, false, true);
  436. }
  437. static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
  438. u32 flags, struct netlink_ext_ack *extack)
  439. {
  440. struct tcf_block *block = tp->chain->block;
  441. struct tc_cls_u32_offload cls_u32 = {};
  442. bool skip_sw = tc_skip_sw(flags);
  443. bool offloaded = false;
  444. int err;
  445. tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
  446. cls_u32.command = TC_CLSU32_NEW_HNODE;
  447. cls_u32.hnode.divisor = h->divisor;
  448. cls_u32.hnode.handle = h->handle;
  449. cls_u32.hnode.prio = h->prio;
  450. err = tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, skip_sw, true);
  451. if (err < 0) {
  452. u32_clear_hw_hnode(tp, h, NULL);
  453. return err;
  454. } else if (err > 0) {
  455. offloaded = true;
  456. }
  457. if (skip_sw && !offloaded)
  458. return -EINVAL;
  459. return 0;
  460. }
  461. static void u32_remove_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
  462. struct netlink_ext_ack *extack)
  463. {
  464. struct tcf_block *block = tp->chain->block;
  465. struct tc_cls_u32_offload cls_u32 = {};
  466. tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
  467. cls_u32.command = TC_CLSU32_DELETE_KNODE;
  468. cls_u32.knode.handle = n->handle;
  469. tc_setup_cb_destroy(block, tp, TC_SETUP_CLSU32, &cls_u32, false,
  470. &n->flags, &n->in_hw_count, true);
  471. }
  472. static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
  473. u32 flags, struct netlink_ext_ack *extack)
  474. {
  475. struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
  476. struct tcf_block *block = tp->chain->block;
  477. struct tc_cls_u32_offload cls_u32 = {};
  478. bool skip_sw = tc_skip_sw(flags);
  479. int err;
  480. tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
  481. cls_u32.command = TC_CLSU32_REPLACE_KNODE;
  482. cls_u32.knode.handle = n->handle;
  483. cls_u32.knode.fshift = n->fshift;
  484. #ifdef CONFIG_CLS_U32_MARK
  485. cls_u32.knode.val = n->val;
  486. cls_u32.knode.mask = n->mask;
  487. #else
  488. cls_u32.knode.val = 0;
  489. cls_u32.knode.mask = 0;
  490. #endif
  491. cls_u32.knode.sel = &n->sel;
  492. cls_u32.knode.res = &n->res;
  493. cls_u32.knode.exts = &n->exts;
  494. if (n->ht_down)
  495. cls_u32.knode.link_handle = ht->handle;
  496. err = tc_setup_cb_add(block, tp, TC_SETUP_CLSU32, &cls_u32, skip_sw,
  497. &n->flags, &n->in_hw_count, true);
  498. if (err) {
  499. u32_remove_hw_knode(tp, n, NULL);
  500. return err;
  501. }
  502. if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
  503. return -EINVAL;
  504. return 0;
  505. }
  506. static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
  507. struct netlink_ext_ack *extack)
  508. {
  509. struct tc_u_common *tp_c = tp->data;
  510. struct tc_u_knode *n;
  511. unsigned int h;
  512. for (h = 0; h <= ht->divisor; h++) {
  513. while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
  514. RCU_INIT_POINTER(ht->ht[h],
  515. rtnl_dereference(n->next));
  516. tp_c->knodes--;
  517. tcf_unbind_filter(tp, &n->res);
  518. u32_remove_hw_knode(tp, n, extack);
  519. idr_remove(&ht->handle_idr, n->handle);
  520. if (tcf_exts_get_net(&n->exts))
  521. tcf_queue_work(&n->rwork, u32_delete_key_freepf_work);
  522. else
  523. u32_destroy_key(n, true);
  524. }
  525. }
  526. }
  527. static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
  528. struct netlink_ext_ack *extack)
  529. {
  530. struct tc_u_common *tp_c = tp->data;
  531. struct tc_u_hnode __rcu **hn;
  532. struct tc_u_hnode *phn;
  533. u32_clear_hnode(tp, ht, extack);
  534. hn = &tp_c->hlist;
  535. for (phn = rtnl_dereference(*hn);
  536. phn;
  537. hn = &phn->next, phn = rtnl_dereference(*hn)) {
  538. if (phn == ht) {
  539. u32_clear_hw_hnode(tp, ht, extack);
  540. idr_destroy(&ht->handle_idr);
  541. idr_remove(&tp_c->handle_idr, handle2id(ht->handle));
  542. RCU_INIT_POINTER(*hn, ht->next);
  543. kfree_rcu(ht, rcu);
  544. return 0;
  545. }
  546. }
  547. return -ENOENT;
  548. }
  549. static void u32_destroy(struct tcf_proto *tp, bool rtnl_held,
  550. struct netlink_ext_ack *extack)
  551. {
  552. struct tc_u_common *tp_c = tp->data;
  553. struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
  554. WARN_ON(root_ht == NULL);
  555. if (root_ht && refcount_dec_and_test(&root_ht->refcnt))
  556. u32_destroy_hnode(tp, root_ht, extack);
  557. if (refcount_dec_and_test(&tp_c->refcnt)) {
  558. struct tc_u_hnode *ht;
  559. hlist_del(&tp_c->hnode);
  560. while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
  561. u32_clear_hnode(tp, ht, extack);
  562. RCU_INIT_POINTER(tp_c->hlist, ht->next);
  563. /* u32_destroy_key() will later free ht for us, if it's
  564. * still referenced by some knode
  565. */
  566. if (refcount_dec_and_test(&ht->refcnt))
  567. kfree_rcu(ht, rcu);
  568. }
  569. idr_destroy(&tp_c->handle_idr);
  570. kfree(tp_c);
  571. }
  572. tp->data = NULL;
  573. }
  574. static int u32_delete(struct tcf_proto *tp, void *arg, bool *last,
  575. bool rtnl_held, struct netlink_ext_ack *extack)
  576. {
  577. struct tc_u_hnode *ht = arg;
  578. struct tc_u_common *tp_c = tp->data;
  579. int ret = 0;
  580. if (TC_U32_KEY(ht->handle)) {
  581. u32_remove_hw_knode(tp, (struct tc_u_knode *)ht, extack);
  582. ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
  583. goto out;
  584. }
  585. if (ht->is_root) {
  586. NL_SET_ERR_MSG_MOD(extack, "Not allowed to delete root node");
  587. return -EINVAL;
  588. }
  589. if (refcount_dec_if_one(&ht->refcnt)) {
  590. u32_destroy_hnode(tp, ht, extack);
  591. } else {
  592. NL_SET_ERR_MSG_MOD(extack, "Can not delete in-use filter");
  593. return -EBUSY;
  594. }
  595. out:
  596. *last = refcount_read(&tp_c->refcnt) == 1 && tp_c->knodes == 0;
  597. return ret;
  598. }
  599. static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
  600. {
  601. u32 index = htid | 0x800;
  602. u32 max = htid | 0xFFF;
  603. if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max, GFP_KERNEL)) {
  604. index = htid + 1;
  605. if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max,
  606. GFP_KERNEL))
  607. index = max;
  608. }
  609. return index;
  610. }
  611. static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
  612. [TCA_U32_CLASSID] = { .type = NLA_U32 },
  613. [TCA_U32_HASH] = { .type = NLA_U32 },
  614. [TCA_U32_LINK] = { .type = NLA_U32 },
  615. [TCA_U32_DIVISOR] = { .type = NLA_U32 },
  616. [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
  617. [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
  618. [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
  619. [TCA_U32_FLAGS] = { .type = NLA_U32 },
  620. };
  621. static void u32_unbind_filter(struct tcf_proto *tp, struct tc_u_knode *n,
  622. struct nlattr **tb)
  623. {
  624. if (tb[TCA_U32_CLASSID])
  625. tcf_unbind_filter(tp, &n->res);
  626. }
  627. static void u32_bind_filter(struct tcf_proto *tp, struct tc_u_knode *n,
  628. unsigned long base, struct nlattr **tb)
  629. {
  630. if (tb[TCA_U32_CLASSID]) {
  631. n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
  632. tcf_bind_filter(tp, &n->res, base);
  633. }
  634. }
  635. static int u32_set_parms(struct net *net, struct tcf_proto *tp,
  636. struct tc_u_knode *n, struct nlattr **tb,
  637. struct nlattr *est, u32 flags, u32 fl_flags,
  638. struct netlink_ext_ack *extack)
  639. {
  640. int err, ifindex = -1;
  641. err = tcf_exts_validate_ex(net, tp, tb, est, &n->exts, flags,
  642. fl_flags, extack);
  643. if (err < 0)
  644. return err;
  645. if (tb[TCA_U32_INDEV]) {
  646. ifindex = tcf_change_indev(net, tb[TCA_U32_INDEV], extack);
  647. if (ifindex < 0)
  648. return -EINVAL;
  649. }
  650. if (tb[TCA_U32_LINK]) {
  651. u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
  652. struct tc_u_hnode *ht_down = NULL, *ht_old;
  653. if (TC_U32_KEY(handle)) {
  654. NL_SET_ERR_MSG_MOD(extack, "u32 Link handle must be a hash table");
  655. return -EINVAL;
  656. }
  657. if (handle) {
  658. ht_down = u32_lookup_ht(tp->data, handle);
  659. if (!ht_down) {
  660. NL_SET_ERR_MSG_MOD(extack, "Link hash table not found");
  661. return -EINVAL;
  662. }
  663. if (ht_down->is_root) {
  664. NL_SET_ERR_MSG_MOD(extack, "Not linking to root node");
  665. return -EINVAL;
  666. }
  667. refcount_inc(&ht_down->refcnt);
  668. }
  669. ht_old = rtnl_dereference(n->ht_down);
  670. rcu_assign_pointer(n->ht_down, ht_down);
  671. if (ht_old)
  672. refcount_dec(&ht_old->refcnt);
  673. }
  674. if (ifindex >= 0)
  675. n->ifindex = ifindex;
  676. return 0;
  677. }
  678. static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
  679. struct tc_u_knode *n)
  680. {
  681. struct tc_u_knode __rcu **ins;
  682. struct tc_u_knode *pins;
  683. struct tc_u_hnode *ht;
  684. if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
  685. ht = rtnl_dereference(tp->root);
  686. else
  687. ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
  688. ins = &ht->ht[TC_U32_HASH(n->handle)];
  689. /* The node must always exist for it to be replaced if this is not the
  690. * case then something went very wrong elsewhere.
  691. */
  692. for (pins = rtnl_dereference(*ins); ;
  693. ins = &pins->next, pins = rtnl_dereference(*ins))
  694. if (pins->handle == n->handle)
  695. break;
  696. idr_replace(&ht->handle_idr, n, n->handle);
  697. RCU_INIT_POINTER(n->next, pins->next);
  698. rcu_assign_pointer(*ins, n);
  699. }
  700. static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp,
  701. struct tc_u_knode *n)
  702. {
  703. struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
  704. struct tc_u32_sel *s = &n->sel;
  705. struct tc_u_knode *new;
  706. new = kzalloc(struct_size(new, sel.keys, s->nkeys), GFP_KERNEL);
  707. if (!new)
  708. return NULL;
  709. RCU_INIT_POINTER(new->next, n->next);
  710. new->handle = n->handle;
  711. RCU_INIT_POINTER(new->ht_up, n->ht_up);
  712. new->ifindex = n->ifindex;
  713. new->fshift = n->fshift;
  714. new->flags = n->flags;
  715. RCU_INIT_POINTER(new->ht_down, ht);
  716. #ifdef CONFIG_CLS_U32_PERF
  717. /* Statistics may be incremented by readers during update
  718. * so we must keep them in tact. When the node is later destroyed
  719. * a special destroy call must be made to not free the pf memory.
  720. */
  721. new->pf = n->pf;
  722. #endif
  723. #ifdef CONFIG_CLS_U32_MARK
  724. new->val = n->val;
  725. new->mask = n->mask;
  726. /* Similarly success statistics must be moved as pointers */
  727. new->pcpu_success = n->pcpu_success;
  728. #endif
  729. memcpy(&new->sel, s, struct_size(s, keys, s->nkeys));
  730. if (tcf_exts_init(&new->exts, net, TCA_U32_ACT, TCA_U32_POLICE)) {
  731. kfree(new);
  732. return NULL;
  733. }
  734. /* bump reference count as long as we hold pointer to structure */
  735. if (ht)
  736. refcount_inc(&ht->refcnt);
  737. return new;
  738. }
  739. static int u32_change(struct net *net, struct sk_buff *in_skb,
  740. struct tcf_proto *tp, unsigned long base, u32 handle,
  741. struct nlattr **tca, void **arg, u32 flags,
  742. struct netlink_ext_ack *extack)
  743. {
  744. struct tc_u_common *tp_c = tp->data;
  745. struct tc_u_hnode *ht;
  746. struct tc_u_knode *n;
  747. struct tc_u32_sel *s;
  748. struct nlattr *opt = tca[TCA_OPTIONS];
  749. struct nlattr *tb[TCA_U32_MAX + 1];
  750. u32 htid, userflags = 0;
  751. size_t sel_size;
  752. int err;
  753. if (!opt) {
  754. if (handle) {
  755. NL_SET_ERR_MSG_MOD(extack, "Filter handle requires options");
  756. return -EINVAL;
  757. } else {
  758. return 0;
  759. }
  760. }
  761. err = nla_parse_nested_deprecated(tb, TCA_U32_MAX, opt, u32_policy,
  762. extack);
  763. if (err < 0)
  764. return err;
  765. if (tb[TCA_U32_FLAGS]) {
  766. userflags = nla_get_u32(tb[TCA_U32_FLAGS]);
  767. if (!tc_flags_valid(userflags)) {
  768. NL_SET_ERR_MSG_MOD(extack, "Invalid filter flags");
  769. return -EINVAL;
  770. }
  771. }
  772. n = *arg;
  773. if (n) {
  774. struct tc_u_knode *new;
  775. if (TC_U32_KEY(n->handle) == 0) {
  776. NL_SET_ERR_MSG_MOD(extack, "Key node id cannot be zero");
  777. return -EINVAL;
  778. }
  779. if ((n->flags ^ userflags) &
  780. ~(TCA_CLS_FLAGS_IN_HW | TCA_CLS_FLAGS_NOT_IN_HW)) {
  781. NL_SET_ERR_MSG_MOD(extack, "Key node flags do not match passed flags");
  782. return -EINVAL;
  783. }
  784. new = u32_init_knode(net, tp, n);
  785. if (!new)
  786. return -ENOMEM;
  787. err = u32_set_parms(net, tp, new, tb, tca[TCA_RATE],
  788. flags, new->flags, extack);
  789. if (err) {
  790. __u32_destroy_key(new);
  791. return err;
  792. }
  793. u32_bind_filter(tp, new, base, tb);
  794. err = u32_replace_hw_knode(tp, new, flags, extack);
  795. if (err) {
  796. u32_unbind_filter(tp, new, tb);
  797. if (tb[TCA_U32_LINK]) {
  798. struct tc_u_hnode *ht_old;
  799. ht_old = rtnl_dereference(n->ht_down);
  800. if (ht_old)
  801. refcount_inc(&ht_old->refcnt);
  802. }
  803. __u32_destroy_key(new);
  804. return err;
  805. }
  806. if (!tc_in_hw(new->flags))
  807. new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  808. tcf_proto_update_usesw(tp, new->flags);
  809. u32_replace_knode(tp, tp_c, new);
  810. tcf_unbind_filter(tp, &n->res);
  811. tcf_exts_get_net(&n->exts);
  812. tcf_queue_work(&n->rwork, u32_delete_key_work);
  813. return 0;
  814. }
  815. if (tb[TCA_U32_DIVISOR]) {
  816. unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
  817. if (!is_power_of_2(divisor)) {
  818. NL_SET_ERR_MSG_MOD(extack, "Divisor is not a power of 2");
  819. return -EINVAL;
  820. }
  821. if (divisor-- > 0x100) {
  822. NL_SET_ERR_MSG_MOD(extack, "Exceeded maximum 256 hash buckets");
  823. return -EINVAL;
  824. }
  825. if (TC_U32_KEY(handle)) {
  826. NL_SET_ERR_MSG_MOD(extack, "Divisor can only be used on a hash table");
  827. return -EINVAL;
  828. }
  829. ht = kzalloc(struct_size(ht, ht, divisor + 1), GFP_KERNEL);
  830. if (ht == NULL)
  831. return -ENOBUFS;
  832. if (handle == 0) {
  833. handle = gen_new_htid(tp->data, ht);
  834. if (handle == 0) {
  835. kfree(ht);
  836. return -ENOMEM;
  837. }
  838. } else {
  839. err = idr_alloc_u32(&tp_c->handle_idr, ht, &handle,
  840. handle, GFP_KERNEL);
  841. if (err) {
  842. kfree(ht);
  843. return err;
  844. }
  845. }
  846. refcount_set(&ht->refcnt, 1);
  847. ht->divisor = divisor;
  848. ht->handle = handle;
  849. ht->prio = tp->prio;
  850. idr_init(&ht->handle_idr);
  851. ht->flags = userflags;
  852. err = u32_replace_hw_hnode(tp, ht, userflags, extack);
  853. if (err) {
  854. idr_remove(&tp_c->handle_idr, handle2id(handle));
  855. kfree(ht);
  856. return err;
  857. }
  858. RCU_INIT_POINTER(ht->next, tp_c->hlist);
  859. rcu_assign_pointer(tp_c->hlist, ht);
  860. *arg = ht;
  861. return 0;
  862. }
  863. if (tb[TCA_U32_HASH]) {
  864. htid = nla_get_u32(tb[TCA_U32_HASH]);
  865. if (TC_U32_HTID(htid) == TC_U32_ROOT) {
  866. ht = rtnl_dereference(tp->root);
  867. htid = ht->handle;
  868. } else {
  869. ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
  870. if (!ht) {
  871. NL_SET_ERR_MSG_MOD(extack, "Specified hash table not found");
  872. return -EINVAL;
  873. }
  874. }
  875. } else {
  876. ht = rtnl_dereference(tp->root);
  877. htid = ht->handle;
  878. }
  879. if (ht->divisor < TC_U32_HASH(htid)) {
  880. NL_SET_ERR_MSG_MOD(extack, "Specified hash table buckets exceed configured value");
  881. return -EINVAL;
  882. }
  883. /* At this point, we need to derive the new handle that will be used to
  884. * uniquely map the identity of this table match entry. The
  885. * identity of the entry that we need to construct is 32 bits made of:
  886. * htid(12b):bucketid(8b):node/entryid(12b)
  887. *
  888. * At this point _we have the table(ht)_ in which we will insert this
  889. * entry. We carry the table's id in variable "htid".
  890. * Note that earlier code picked the ht selection either by a) the user
  891. * providing the htid specified via TCA_U32_HASH attribute or b) when
  892. * no such attribute is passed then the root ht, is default to at ID
  893. * 0x[800][00][000]. Rule: the root table has a single bucket with ID 0.
  894. * If OTOH the user passed us the htid, they may also pass a bucketid of
  895. * choice. 0 is fine. For example a user htid is 0x[600][01][000] it is
  896. * indicating hash bucketid of 1. Rule: the entry/node ID _cannot_ be
  897. * passed via the htid, so even if it was non-zero it will be ignored.
  898. *
  899. * We may also have a handle, if the user passed one. The handle also
  900. * carries the same addressing of htid(12b):bucketid(8b):node/entryid(12b).
  901. * Rule: the bucketid on the handle is ignored even if one was passed;
  902. * rather the value on "htid" is always assumed to be the bucketid.
  903. */
  904. if (handle) {
  905. /* Rule: The htid from handle and tableid from htid must match */
  906. if (TC_U32_HTID(handle) && TC_U32_HTID(handle ^ htid)) {
  907. NL_SET_ERR_MSG_MOD(extack, "Handle specified hash table address mismatch");
  908. return -EINVAL;
  909. }
  910. /* Ok, so far we have a valid htid(12b):bucketid(8b) but we
  911. * need to finalize the table entry identification with the last
  912. * part - the node/entryid(12b)). Rule: Nodeid _cannot be 0_ for
  913. * entries. Rule: nodeid of 0 is reserved only for tables(see
  914. * earlier code which processes TC_U32_DIVISOR attribute).
  915. * Rule: The nodeid can only be derived from the handle (and not
  916. * htid).
  917. * Rule: if the handle specified zero for the node id example
  918. * 0x60000000, then pick a new nodeid from the pool of IDs
  919. * this hash table has been allocating from.
  920. * If OTOH it is specified (i.e for example the user passed a
  921. * handle such as 0x60000123), then we use it generate our final
  922. * handle which is used to uniquely identify the match entry.
  923. */
  924. if (!TC_U32_NODE(handle)) {
  925. handle = gen_new_kid(ht, htid);
  926. } else {
  927. handle = htid | TC_U32_NODE(handle);
  928. err = idr_alloc_u32(&ht->handle_idr, NULL, &handle,
  929. handle, GFP_KERNEL);
  930. if (err)
  931. return err;
  932. }
  933. } else {
  934. /* The user did not give us a handle; lets just generate one
  935. * from the table's pool of nodeids.
  936. */
  937. handle = gen_new_kid(ht, htid);
  938. }
  939. if (tb[TCA_U32_SEL] == NULL) {
  940. NL_SET_ERR_MSG_MOD(extack, "Selector not specified");
  941. err = -EINVAL;
  942. goto erridr;
  943. }
  944. s = nla_data(tb[TCA_U32_SEL]);
  945. sel_size = struct_size(s, keys, s->nkeys);
  946. if (nla_len(tb[TCA_U32_SEL]) < sel_size) {
  947. err = -EINVAL;
  948. goto erridr;
  949. }
  950. n = kzalloc(struct_size(n, sel.keys, s->nkeys), GFP_KERNEL);
  951. if (n == NULL) {
  952. err = -ENOBUFS;
  953. goto erridr;
  954. }
  955. #ifdef CONFIG_CLS_U32_PERF
  956. n->pf = __alloc_percpu(struct_size(n->pf, kcnts, s->nkeys),
  957. __alignof__(struct tc_u32_pcnt));
  958. if (!n->pf) {
  959. err = -ENOBUFS;
  960. goto errfree;
  961. }
  962. #endif
  963. unsafe_memcpy(&n->sel, s, sel_size,
  964. /* A composite flex-array structure destination,
  965. * which was correctly sized with struct_size(),
  966. * bounds-checked against nla_len(), and allocated
  967. * above. */);
  968. RCU_INIT_POINTER(n->ht_up, ht);
  969. n->handle = handle;
  970. n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
  971. n->flags = userflags;
  972. err = tcf_exts_init(&n->exts, net, TCA_U32_ACT, TCA_U32_POLICE);
  973. if (err < 0)
  974. goto errout;
  975. #ifdef CONFIG_CLS_U32_MARK
  976. n->pcpu_success = alloc_percpu(u32);
  977. if (!n->pcpu_success) {
  978. err = -ENOMEM;
  979. goto errout;
  980. }
  981. if (tb[TCA_U32_MARK]) {
  982. struct tc_u32_mark *mark;
  983. mark = nla_data(tb[TCA_U32_MARK]);
  984. n->val = mark->val;
  985. n->mask = mark->mask;
  986. }
  987. #endif
  988. err = u32_set_parms(net, tp, n, tb, tca[TCA_RATE],
  989. flags, n->flags, extack);
  990. u32_bind_filter(tp, n, base, tb);
  991. if (err == 0) {
  992. struct tc_u_knode __rcu **ins;
  993. struct tc_u_knode *pins;
  994. err = u32_replace_hw_knode(tp, n, flags, extack);
  995. if (err)
  996. goto errunbind;
  997. if (!tc_in_hw(n->flags))
  998. n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  999. tcf_proto_update_usesw(tp, n->flags);
  1000. ins = &ht->ht[TC_U32_HASH(handle)];
  1001. for (pins = rtnl_dereference(*ins); pins;
  1002. ins = &pins->next, pins = rtnl_dereference(*ins))
  1003. if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
  1004. break;
  1005. RCU_INIT_POINTER(n->next, pins);
  1006. rcu_assign_pointer(*ins, n);
  1007. tp_c->knodes++;
  1008. *arg = n;
  1009. return 0;
  1010. }
  1011. errunbind:
  1012. u32_unbind_filter(tp, n, tb);
  1013. #ifdef CONFIG_CLS_U32_MARK
  1014. free_percpu(n->pcpu_success);
  1015. #endif
  1016. errout:
  1017. tcf_exts_destroy(&n->exts);
  1018. #ifdef CONFIG_CLS_U32_PERF
  1019. errfree:
  1020. free_percpu(n->pf);
  1021. #endif
  1022. kfree(n);
  1023. erridr:
  1024. idr_remove(&ht->handle_idr, handle);
  1025. return err;
  1026. }
  1027. static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg,
  1028. bool rtnl_held)
  1029. {
  1030. struct tc_u_common *tp_c = tp->data;
  1031. struct tc_u_hnode *ht;
  1032. struct tc_u_knode *n;
  1033. unsigned int h;
  1034. if (arg->stop)
  1035. return;
  1036. for (ht = rtnl_dereference(tp_c->hlist);
  1037. ht;
  1038. ht = rtnl_dereference(ht->next)) {
  1039. if (ht->prio != tp->prio)
  1040. continue;
  1041. if (!tc_cls_stats_dump(tp, arg, ht))
  1042. return;
  1043. for (h = 0; h <= ht->divisor; h++) {
  1044. for (n = rtnl_dereference(ht->ht[h]);
  1045. n;
  1046. n = rtnl_dereference(n->next)) {
  1047. if (!tc_cls_stats_dump(tp, arg, n))
  1048. return;
  1049. }
  1050. }
  1051. }
  1052. }
  1053. static int u32_reoffload_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
  1054. bool add, flow_setup_cb_t *cb, void *cb_priv,
  1055. struct netlink_ext_ack *extack)
  1056. {
  1057. struct tc_cls_u32_offload cls_u32 = {};
  1058. int err;
  1059. tc_cls_common_offload_init(&cls_u32.common, tp, ht->flags, extack);
  1060. cls_u32.command = add ? TC_CLSU32_NEW_HNODE : TC_CLSU32_DELETE_HNODE;
  1061. cls_u32.hnode.divisor = ht->divisor;
  1062. cls_u32.hnode.handle = ht->handle;
  1063. cls_u32.hnode.prio = ht->prio;
  1064. err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
  1065. if (err && add && tc_skip_sw(ht->flags))
  1066. return err;
  1067. return 0;
  1068. }
  1069. static int u32_reoffload_knode(struct tcf_proto *tp, struct tc_u_knode *n,
  1070. bool add, flow_setup_cb_t *cb, void *cb_priv,
  1071. struct netlink_ext_ack *extack)
  1072. {
  1073. struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
  1074. struct tcf_block *block = tp->chain->block;
  1075. struct tc_cls_u32_offload cls_u32 = {};
  1076. tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
  1077. cls_u32.command = add ?
  1078. TC_CLSU32_REPLACE_KNODE : TC_CLSU32_DELETE_KNODE;
  1079. cls_u32.knode.handle = n->handle;
  1080. if (add) {
  1081. cls_u32.knode.fshift = n->fshift;
  1082. #ifdef CONFIG_CLS_U32_MARK
  1083. cls_u32.knode.val = n->val;
  1084. cls_u32.knode.mask = n->mask;
  1085. #else
  1086. cls_u32.knode.val = 0;
  1087. cls_u32.knode.mask = 0;
  1088. #endif
  1089. cls_u32.knode.sel = &n->sel;
  1090. cls_u32.knode.res = &n->res;
  1091. cls_u32.knode.exts = &n->exts;
  1092. if (n->ht_down)
  1093. cls_u32.knode.link_handle = ht->handle;
  1094. }
  1095. return tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSU32,
  1096. &cls_u32, cb_priv, &n->flags,
  1097. &n->in_hw_count);
  1098. }
  1099. static int u32_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
  1100. void *cb_priv, struct netlink_ext_ack *extack)
  1101. {
  1102. struct tc_u_common *tp_c = tp->data;
  1103. struct tc_u_hnode *ht;
  1104. struct tc_u_knode *n;
  1105. unsigned int h;
  1106. int err;
  1107. for (ht = rtnl_dereference(tp_c->hlist);
  1108. ht;
  1109. ht = rtnl_dereference(ht->next)) {
  1110. if (ht->prio != tp->prio)
  1111. continue;
  1112. /* When adding filters to a new dev, try to offload the
  1113. * hashtable first. When removing, do the filters before the
  1114. * hashtable.
  1115. */
  1116. if (add && !tc_skip_hw(ht->flags)) {
  1117. err = u32_reoffload_hnode(tp, ht, add, cb, cb_priv,
  1118. extack);
  1119. if (err)
  1120. return err;
  1121. }
  1122. for (h = 0; h <= ht->divisor; h++) {
  1123. for (n = rtnl_dereference(ht->ht[h]);
  1124. n;
  1125. n = rtnl_dereference(n->next)) {
  1126. if (tc_skip_hw(n->flags))
  1127. continue;
  1128. err = u32_reoffload_knode(tp, n, add, cb,
  1129. cb_priv, extack);
  1130. if (err)
  1131. return err;
  1132. }
  1133. }
  1134. if (!add && !tc_skip_hw(ht->flags))
  1135. u32_reoffload_hnode(tp, ht, add, cb, cb_priv, extack);
  1136. }
  1137. return 0;
  1138. }
  1139. static void u32_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
  1140. unsigned long base)
  1141. {
  1142. struct tc_u_knode *n = fh;
  1143. tc_cls_bind_class(classid, cl, q, &n->res, base);
  1144. }
  1145. static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
  1146. struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
  1147. {
  1148. struct tc_u_knode *n = fh;
  1149. struct tc_u_hnode *ht_up, *ht_down;
  1150. struct nlattr *nest;
  1151. if (n == NULL)
  1152. return skb->len;
  1153. t->tcm_handle = n->handle;
  1154. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  1155. if (nest == NULL)
  1156. goto nla_put_failure;
  1157. if (TC_U32_KEY(n->handle) == 0) {
  1158. struct tc_u_hnode *ht = fh;
  1159. u32 divisor = ht->divisor + 1;
  1160. if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
  1161. goto nla_put_failure;
  1162. } else {
  1163. #ifdef CONFIG_CLS_U32_PERF
  1164. struct tc_u32_pcnt *gpf;
  1165. int cpu;
  1166. #endif
  1167. if (nla_put(skb, TCA_U32_SEL, struct_size(&n->sel, keys, n->sel.nkeys),
  1168. &n->sel))
  1169. goto nla_put_failure;
  1170. ht_up = rtnl_dereference(n->ht_up);
  1171. if (ht_up) {
  1172. u32 htid = n->handle & 0xFFFFF000;
  1173. if (nla_put_u32(skb, TCA_U32_HASH, htid))
  1174. goto nla_put_failure;
  1175. }
  1176. if (n->res.classid &&
  1177. nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
  1178. goto nla_put_failure;
  1179. ht_down = rtnl_dereference(n->ht_down);
  1180. if (ht_down &&
  1181. nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
  1182. goto nla_put_failure;
  1183. if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
  1184. goto nla_put_failure;
  1185. #ifdef CONFIG_CLS_U32_MARK
  1186. if ((n->val || n->mask)) {
  1187. struct tc_u32_mark mark = {.val = n->val,
  1188. .mask = n->mask,
  1189. .success = 0};
  1190. int cpum;
  1191. for_each_possible_cpu(cpum) {
  1192. __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
  1193. mark.success += cnt;
  1194. }
  1195. if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
  1196. goto nla_put_failure;
  1197. }
  1198. #endif
  1199. if (tcf_exts_dump(skb, &n->exts) < 0)
  1200. goto nla_put_failure;
  1201. if (n->ifindex) {
  1202. struct net_device *dev;
  1203. dev = __dev_get_by_index(net, n->ifindex);
  1204. if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
  1205. goto nla_put_failure;
  1206. }
  1207. #ifdef CONFIG_CLS_U32_PERF
  1208. gpf = kzalloc(struct_size(gpf, kcnts, n->sel.nkeys), GFP_KERNEL);
  1209. if (!gpf)
  1210. goto nla_put_failure;
  1211. for_each_possible_cpu(cpu) {
  1212. int i;
  1213. struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
  1214. gpf->rcnt += pf->rcnt;
  1215. gpf->rhit += pf->rhit;
  1216. for (i = 0; i < n->sel.nkeys; i++)
  1217. gpf->kcnts[i] += pf->kcnts[i];
  1218. }
  1219. if (nla_put_64bit(skb, TCA_U32_PCNT, struct_size(gpf, kcnts, n->sel.nkeys),
  1220. gpf, TCA_U32_PAD)) {
  1221. kfree(gpf);
  1222. goto nla_put_failure;
  1223. }
  1224. kfree(gpf);
  1225. #endif
  1226. }
  1227. nla_nest_end(skb, nest);
  1228. if (TC_U32_KEY(n->handle))
  1229. if (tcf_exts_dump_stats(skb, &n->exts) < 0)
  1230. goto nla_put_failure;
  1231. return skb->len;
  1232. nla_put_failure:
  1233. nla_nest_cancel(skb, nest);
  1234. return -1;
  1235. }
  1236. static struct tcf_proto_ops cls_u32_ops __read_mostly = {
  1237. .kind = "u32",
  1238. .classify = u32_classify,
  1239. .init = u32_init,
  1240. .destroy = u32_destroy,
  1241. .get = u32_get,
  1242. .change = u32_change,
  1243. .delete = u32_delete,
  1244. .walk = u32_walk,
  1245. .reoffload = u32_reoffload,
  1246. .dump = u32_dump,
  1247. .bind_class = u32_bind_class,
  1248. .owner = THIS_MODULE,
  1249. };
  1250. MODULE_ALIAS_NET_CLS("u32");
  1251. static int __init init_u32(void)
  1252. {
  1253. int i, ret;
  1254. pr_info("u32 classifier\n");
  1255. #ifdef CONFIG_CLS_U32_PERF
  1256. pr_info(" Performance counters on\n");
  1257. #endif
  1258. pr_info(" input device check on\n");
  1259. #ifdef CONFIG_NET_CLS_ACT
  1260. pr_info(" Actions configured\n");
  1261. #endif
  1262. tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
  1263. sizeof(struct hlist_head),
  1264. GFP_KERNEL);
  1265. if (!tc_u_common_hash)
  1266. return -ENOMEM;
  1267. for (i = 0; i < U32_HASH_SIZE; i++)
  1268. INIT_HLIST_HEAD(&tc_u_common_hash[i]);
  1269. ret = register_tcf_proto_ops(&cls_u32_ops);
  1270. if (ret)
  1271. kvfree(tc_u_common_hash);
  1272. return ret;
  1273. }
  1274. static void __exit exit_u32(void)
  1275. {
  1276. unregister_tcf_proto_ops(&cls_u32_ops);
  1277. kvfree(tc_u_common_hash);
  1278. }
  1279. module_init(init_u32)
  1280. module_exit(exit_u32)
  1281. MODULE_DESCRIPTION("Universal 32bit based TC Classifier");
  1282. MODULE_LICENSE("GPL");