act_police.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/act_police.c Input police filter
  4. *
  5. * Authors: Alexey Kuznetsov, <[email protected]>
  6. * J Hadi Salim (action changes)
  7. */
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/rtnetlink.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <net/act_api.h>
  18. #include <net/netlink.h>
  19. #include <net/pkt_cls.h>
  20. #include <net/tc_act/tc_police.h>
  21. /* Each policer is serialized by its individual spinlock */
  22. static struct tc_action_ops act_police_ops;
  23. static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
  24. [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
  25. [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
  26. [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
  27. [TCA_POLICE_RESULT] = { .type = NLA_U32 },
  28. [TCA_POLICE_RATE64] = { .type = NLA_U64 },
  29. [TCA_POLICE_PEAKRATE64] = { .type = NLA_U64 },
  30. [TCA_POLICE_PKTRATE64] = { .type = NLA_U64, .min = 1 },
  31. [TCA_POLICE_PKTBURST64] = { .type = NLA_U64, .min = 1 },
  32. };
  33. static int tcf_police_init(struct net *net, struct nlattr *nla,
  34. struct nlattr *est, struct tc_action **a,
  35. struct tcf_proto *tp, u32 flags,
  36. struct netlink_ext_ack *extack)
  37. {
  38. int ret = 0, tcfp_result = TC_ACT_OK, err, size;
  39. bool bind = flags & TCA_ACT_FLAGS_BIND;
  40. struct nlattr *tb[TCA_POLICE_MAX + 1];
  41. struct tcf_chain *goto_ch = NULL;
  42. struct tc_police *parm;
  43. struct tcf_police *police;
  44. struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
  45. struct tc_action_net *tn = net_generic(net, act_police_ops.net_id);
  46. struct tcf_police_params *new;
  47. bool exists = false;
  48. u32 index;
  49. u64 rate64, prate64;
  50. u64 pps, ppsburst;
  51. if (nla == NULL)
  52. return -EINVAL;
  53. err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
  54. police_policy, NULL);
  55. if (err < 0)
  56. return err;
  57. if (tb[TCA_POLICE_TBF] == NULL)
  58. return -EINVAL;
  59. size = nla_len(tb[TCA_POLICE_TBF]);
  60. if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
  61. return -EINVAL;
  62. parm = nla_data(tb[TCA_POLICE_TBF]);
  63. index = parm->index;
  64. err = tcf_idr_check_alloc(tn, &index, a, bind);
  65. if (err < 0)
  66. return err;
  67. exists = err;
  68. if (exists && bind)
  69. return 0;
  70. if (!exists) {
  71. ret = tcf_idr_create(tn, index, NULL, a,
  72. &act_police_ops, bind, true, flags);
  73. if (ret) {
  74. tcf_idr_cleanup(tn, index);
  75. return ret;
  76. }
  77. ret = ACT_P_CREATED;
  78. spin_lock_init(&(to_police(*a)->tcfp_lock));
  79. } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  80. tcf_idr_release(*a, bind);
  81. return -EEXIST;
  82. }
  83. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  84. if (err < 0)
  85. goto release_idr;
  86. police = to_police(*a);
  87. if (parm->rate.rate) {
  88. err = -ENOMEM;
  89. R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
  90. if (R_tab == NULL)
  91. goto failure;
  92. if (parm->peakrate.rate) {
  93. P_tab = qdisc_get_rtab(&parm->peakrate,
  94. tb[TCA_POLICE_PEAKRATE], NULL);
  95. if (P_tab == NULL)
  96. goto failure;
  97. }
  98. }
  99. if (est) {
  100. err = gen_replace_estimator(&police->tcf_bstats,
  101. police->common.cpu_bstats,
  102. &police->tcf_rate_est,
  103. &police->tcf_lock,
  104. false, est);
  105. if (err)
  106. goto failure;
  107. } else if (tb[TCA_POLICE_AVRATE] &&
  108. (ret == ACT_P_CREATED ||
  109. !gen_estimator_active(&police->tcf_rate_est))) {
  110. err = -EINVAL;
  111. goto failure;
  112. }
  113. if (tb[TCA_POLICE_RESULT]) {
  114. tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
  115. if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
  116. NL_SET_ERR_MSG(extack,
  117. "goto chain not allowed on fallback");
  118. err = -EINVAL;
  119. goto failure;
  120. }
  121. }
  122. if ((tb[TCA_POLICE_PKTRATE64] && !tb[TCA_POLICE_PKTBURST64]) ||
  123. (!tb[TCA_POLICE_PKTRATE64] && tb[TCA_POLICE_PKTBURST64])) {
  124. NL_SET_ERR_MSG(extack,
  125. "Both or neither packet-per-second burst and rate must be provided");
  126. err = -EINVAL;
  127. goto failure;
  128. }
  129. if (tb[TCA_POLICE_PKTRATE64] && R_tab) {
  130. NL_SET_ERR_MSG(extack,
  131. "packet-per-second and byte-per-second rate limits not allowed in same action");
  132. err = -EINVAL;
  133. goto failure;
  134. }
  135. new = kzalloc(sizeof(*new), GFP_KERNEL);
  136. if (unlikely(!new)) {
  137. err = -ENOMEM;
  138. goto failure;
  139. }
  140. /* No failure allowed after this point */
  141. new->tcfp_result = tcfp_result;
  142. new->tcfp_mtu = parm->mtu;
  143. if (!new->tcfp_mtu) {
  144. new->tcfp_mtu = ~0;
  145. if (R_tab)
  146. new->tcfp_mtu = 255 << R_tab->rate.cell_log;
  147. }
  148. if (R_tab) {
  149. new->rate_present = true;
  150. rate64 = tb[TCA_POLICE_RATE64] ?
  151. nla_get_u64(tb[TCA_POLICE_RATE64]) : 0;
  152. psched_ratecfg_precompute(&new->rate, &R_tab->rate, rate64);
  153. qdisc_put_rtab(R_tab);
  154. } else {
  155. new->rate_present = false;
  156. }
  157. if (P_tab) {
  158. new->peak_present = true;
  159. prate64 = tb[TCA_POLICE_PEAKRATE64] ?
  160. nla_get_u64(tb[TCA_POLICE_PEAKRATE64]) : 0;
  161. psched_ratecfg_precompute(&new->peak, &P_tab->rate, prate64);
  162. qdisc_put_rtab(P_tab);
  163. } else {
  164. new->peak_present = false;
  165. }
  166. new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
  167. if (new->peak_present)
  168. new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
  169. new->tcfp_mtu);
  170. if (tb[TCA_POLICE_AVRATE])
  171. new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
  172. if (tb[TCA_POLICE_PKTRATE64]) {
  173. pps = nla_get_u64(tb[TCA_POLICE_PKTRATE64]);
  174. ppsburst = nla_get_u64(tb[TCA_POLICE_PKTBURST64]);
  175. new->pps_present = true;
  176. new->tcfp_pkt_burst = PSCHED_TICKS2NS(ppsburst);
  177. psched_ppscfg_precompute(&new->ppsrate, pps);
  178. }
  179. spin_lock_bh(&police->tcf_lock);
  180. spin_lock_bh(&police->tcfp_lock);
  181. police->tcfp_t_c = ktime_get_ns();
  182. police->tcfp_toks = new->tcfp_burst;
  183. if (new->peak_present)
  184. police->tcfp_ptoks = new->tcfp_mtu_ptoks;
  185. spin_unlock_bh(&police->tcfp_lock);
  186. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  187. new = rcu_replace_pointer(police->params,
  188. new,
  189. lockdep_is_held(&police->tcf_lock));
  190. spin_unlock_bh(&police->tcf_lock);
  191. if (goto_ch)
  192. tcf_chain_put_by_act(goto_ch);
  193. if (new)
  194. kfree_rcu(new, rcu);
  195. return ret;
  196. failure:
  197. qdisc_put_rtab(P_tab);
  198. qdisc_put_rtab(R_tab);
  199. if (goto_ch)
  200. tcf_chain_put_by_act(goto_ch);
  201. release_idr:
  202. tcf_idr_release(*a, bind);
  203. return err;
  204. }
  205. static bool tcf_police_mtu_check(struct sk_buff *skb, u32 limit)
  206. {
  207. u32 len;
  208. if (skb_is_gso(skb))
  209. return skb_gso_validate_mac_len(skb, limit);
  210. len = qdisc_pkt_len(skb);
  211. if (skb_at_tc_ingress(skb))
  212. len += skb->mac_len;
  213. return len <= limit;
  214. }
  215. static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
  216. struct tcf_result *res)
  217. {
  218. struct tcf_police *police = to_police(a);
  219. s64 now, toks, ppstoks = 0, ptoks = 0;
  220. struct tcf_police_params *p;
  221. int ret;
  222. tcf_lastuse_update(&police->tcf_tm);
  223. bstats_update(this_cpu_ptr(police->common.cpu_bstats), skb);
  224. ret = READ_ONCE(police->tcf_action);
  225. p = rcu_dereference_bh(police->params);
  226. if (p->tcfp_ewma_rate) {
  227. struct gnet_stats_rate_est64 sample;
  228. if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
  229. sample.bps >= p->tcfp_ewma_rate)
  230. goto inc_overlimits;
  231. }
  232. if (tcf_police_mtu_check(skb, p->tcfp_mtu)) {
  233. if (!p->rate_present && !p->pps_present) {
  234. ret = p->tcfp_result;
  235. goto end;
  236. }
  237. now = ktime_get_ns();
  238. spin_lock_bh(&police->tcfp_lock);
  239. toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
  240. if (p->peak_present) {
  241. ptoks = toks + police->tcfp_ptoks;
  242. if (ptoks > p->tcfp_mtu_ptoks)
  243. ptoks = p->tcfp_mtu_ptoks;
  244. ptoks -= (s64)psched_l2t_ns(&p->peak,
  245. qdisc_pkt_len(skb));
  246. }
  247. if (p->rate_present) {
  248. toks += police->tcfp_toks;
  249. if (toks > p->tcfp_burst)
  250. toks = p->tcfp_burst;
  251. toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
  252. } else if (p->pps_present) {
  253. ppstoks = min_t(s64, now - police->tcfp_t_c, p->tcfp_pkt_burst);
  254. ppstoks += police->tcfp_pkttoks;
  255. if (ppstoks > p->tcfp_pkt_burst)
  256. ppstoks = p->tcfp_pkt_burst;
  257. ppstoks -= (s64)psched_pkt2t_ns(&p->ppsrate, 1);
  258. }
  259. if ((toks | ptoks | ppstoks) >= 0) {
  260. police->tcfp_t_c = now;
  261. police->tcfp_toks = toks;
  262. police->tcfp_ptoks = ptoks;
  263. police->tcfp_pkttoks = ppstoks;
  264. spin_unlock_bh(&police->tcfp_lock);
  265. ret = p->tcfp_result;
  266. goto inc_drops;
  267. }
  268. spin_unlock_bh(&police->tcfp_lock);
  269. }
  270. inc_overlimits:
  271. qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
  272. inc_drops:
  273. if (ret == TC_ACT_SHOT)
  274. qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
  275. end:
  276. return ret;
  277. }
  278. static void tcf_police_cleanup(struct tc_action *a)
  279. {
  280. struct tcf_police *police = to_police(a);
  281. struct tcf_police_params *p;
  282. p = rcu_dereference_protected(police->params, 1);
  283. if (p)
  284. kfree_rcu(p, rcu);
  285. }
  286. static void tcf_police_stats_update(struct tc_action *a,
  287. u64 bytes, u64 packets, u64 drops,
  288. u64 lastuse, bool hw)
  289. {
  290. struct tcf_police *police = to_police(a);
  291. struct tcf_t *tm = &police->tcf_tm;
  292. tcf_action_update_stats(a, bytes, packets, drops, hw);
  293. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  294. }
  295. static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
  296. int bind, int ref)
  297. {
  298. unsigned char *b = skb_tail_pointer(skb);
  299. struct tcf_police *police = to_police(a);
  300. struct tcf_police_params *p;
  301. struct tc_police opt = {
  302. .index = police->tcf_index,
  303. .refcnt = refcount_read(&police->tcf_refcnt) - ref,
  304. .bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
  305. };
  306. struct tcf_t t;
  307. spin_lock_bh(&police->tcf_lock);
  308. opt.action = police->tcf_action;
  309. p = rcu_dereference_protected(police->params,
  310. lockdep_is_held(&police->tcf_lock));
  311. opt.mtu = p->tcfp_mtu;
  312. opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
  313. if (p->rate_present) {
  314. psched_ratecfg_getrate(&opt.rate, &p->rate);
  315. if ((p->rate.rate_bytes_ps >= (1ULL << 32)) &&
  316. nla_put_u64_64bit(skb, TCA_POLICE_RATE64,
  317. p->rate.rate_bytes_ps,
  318. TCA_POLICE_PAD))
  319. goto nla_put_failure;
  320. }
  321. if (p->peak_present) {
  322. psched_ratecfg_getrate(&opt.peakrate, &p->peak);
  323. if ((p->peak.rate_bytes_ps >= (1ULL << 32)) &&
  324. nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64,
  325. p->peak.rate_bytes_ps,
  326. TCA_POLICE_PAD))
  327. goto nla_put_failure;
  328. }
  329. if (p->pps_present) {
  330. if (nla_put_u64_64bit(skb, TCA_POLICE_PKTRATE64,
  331. p->ppsrate.rate_pkts_ps,
  332. TCA_POLICE_PAD))
  333. goto nla_put_failure;
  334. if (nla_put_u64_64bit(skb, TCA_POLICE_PKTBURST64,
  335. PSCHED_NS2TICKS(p->tcfp_pkt_burst),
  336. TCA_POLICE_PAD))
  337. goto nla_put_failure;
  338. }
  339. if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
  340. goto nla_put_failure;
  341. if (p->tcfp_result &&
  342. nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
  343. goto nla_put_failure;
  344. if (p->tcfp_ewma_rate &&
  345. nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
  346. goto nla_put_failure;
  347. tcf_tm_dump(&t, &police->tcf_tm);
  348. if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
  349. goto nla_put_failure;
  350. spin_unlock_bh(&police->tcf_lock);
  351. return skb->len;
  352. nla_put_failure:
  353. spin_unlock_bh(&police->tcf_lock);
  354. nlmsg_trim(skb, b);
  355. return -1;
  356. }
  357. static int tcf_police_act_to_flow_act(int tc_act, u32 *extval,
  358. struct netlink_ext_ack *extack)
  359. {
  360. int act_id = -EOPNOTSUPP;
  361. if (!TC_ACT_EXT_OPCODE(tc_act)) {
  362. if (tc_act == TC_ACT_OK)
  363. act_id = FLOW_ACTION_ACCEPT;
  364. else if (tc_act == TC_ACT_SHOT)
  365. act_id = FLOW_ACTION_DROP;
  366. else if (tc_act == TC_ACT_PIPE)
  367. act_id = FLOW_ACTION_PIPE;
  368. else if (tc_act == TC_ACT_RECLASSIFY)
  369. NL_SET_ERR_MSG_MOD(extack, "Offload not supported when conform/exceed action is \"reclassify\"");
  370. else
  371. NL_SET_ERR_MSG_MOD(extack, "Unsupported conform/exceed action offload");
  372. } else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_GOTO_CHAIN)) {
  373. act_id = FLOW_ACTION_GOTO;
  374. *extval = tc_act & TC_ACT_EXT_VAL_MASK;
  375. } else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_JUMP)) {
  376. act_id = FLOW_ACTION_JUMP;
  377. *extval = tc_act & TC_ACT_EXT_VAL_MASK;
  378. } else if (tc_act == TC_ACT_UNSPEC) {
  379. act_id = FLOW_ACTION_CONTINUE;
  380. } else {
  381. NL_SET_ERR_MSG_MOD(extack, "Unsupported conform/exceed action offload");
  382. }
  383. return act_id;
  384. }
  385. static int tcf_police_offload_act_setup(struct tc_action *act, void *entry_data,
  386. u32 *index_inc, bool bind,
  387. struct netlink_ext_ack *extack)
  388. {
  389. if (bind) {
  390. struct flow_action_entry *entry = entry_data;
  391. struct tcf_police *police = to_police(act);
  392. struct tcf_police_params *p;
  393. int act_id;
  394. p = rcu_dereference_protected(police->params,
  395. lockdep_is_held(&police->tcf_lock));
  396. entry->id = FLOW_ACTION_POLICE;
  397. entry->police.burst = tcf_police_burst(act);
  398. entry->police.rate_bytes_ps =
  399. tcf_police_rate_bytes_ps(act);
  400. entry->police.peakrate_bytes_ps = tcf_police_peakrate_bytes_ps(act);
  401. entry->police.avrate = tcf_police_tcfp_ewma_rate(act);
  402. entry->police.overhead = tcf_police_rate_overhead(act);
  403. entry->police.burst_pkt = tcf_police_burst_pkt(act);
  404. entry->police.rate_pkt_ps =
  405. tcf_police_rate_pkt_ps(act);
  406. entry->police.mtu = tcf_police_tcfp_mtu(act);
  407. act_id = tcf_police_act_to_flow_act(police->tcf_action,
  408. &entry->police.exceed.extval,
  409. extack);
  410. if (act_id < 0)
  411. return act_id;
  412. entry->police.exceed.act_id = act_id;
  413. act_id = tcf_police_act_to_flow_act(p->tcfp_result,
  414. &entry->police.notexceed.extval,
  415. extack);
  416. if (act_id < 0)
  417. return act_id;
  418. entry->police.notexceed.act_id = act_id;
  419. *index_inc = 1;
  420. } else {
  421. struct flow_offload_action *fl_action = entry_data;
  422. fl_action->id = FLOW_ACTION_POLICE;
  423. }
  424. return 0;
  425. }
  426. MODULE_AUTHOR("Alexey Kuznetsov");
  427. MODULE_DESCRIPTION("Policing actions");
  428. MODULE_LICENSE("GPL");
  429. static struct tc_action_ops act_police_ops = {
  430. .kind = "police",
  431. .id = TCA_ID_POLICE,
  432. .owner = THIS_MODULE,
  433. .stats_update = tcf_police_stats_update,
  434. .act = tcf_police_act,
  435. .dump = tcf_police_dump,
  436. .init = tcf_police_init,
  437. .cleanup = tcf_police_cleanup,
  438. .offload_act_setup = tcf_police_offload_act_setup,
  439. .size = sizeof(struct tcf_police),
  440. };
  441. static __net_init int police_init_net(struct net *net)
  442. {
  443. struct tc_action_net *tn = net_generic(net, act_police_ops.net_id);
  444. return tc_action_net_init(net, tn, &act_police_ops);
  445. }
  446. static void __net_exit police_exit_net(struct list_head *net_list)
  447. {
  448. tc_action_net_exit(net_list, act_police_ops.net_id);
  449. }
  450. static struct pernet_operations police_net_ops = {
  451. .init = police_init_net,
  452. .exit_batch = police_exit_net,
  453. .id = &act_police_ops.net_id,
  454. .size = sizeof(struct tc_action_net),
  455. };
  456. static int __init police_init_module(void)
  457. {
  458. return tcf_register_action(&act_police_ops, &police_net_ops);
  459. }
  460. static void __exit police_cleanup_module(void)
  461. {
  462. tcf_unregister_action(&act_police_ops, &police_net_ops);
  463. }
  464. module_init(police_init_module);
  465. module_exit(police_cleanup_module);