cls_u32.c 36 KB

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