act_gate.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Copyright 2020 NXP */
  3. #include <linux/module.h>
  4. #include <linux/types.h>
  5. #include <linux/kernel.h>
  6. #include <linux/string.h>
  7. #include <linux/errno.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/rtnetlink.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <net/act_api.h>
  13. #include <net/netlink.h>
  14. #include <net/pkt_cls.h>
  15. #include <net/tc_act/tc_gate.h>
  16. #include <net/tc_wrapper.h>
  17. static struct tc_action_ops act_gate_ops;
  18. static ktime_t gate_get_time(struct tcf_gate *gact)
  19. {
  20. ktime_t mono = ktime_get();
  21. switch (gact->tk_offset) {
  22. case TK_OFFS_MAX:
  23. return mono;
  24. default:
  25. return ktime_mono_to_any(mono, gact->tk_offset);
  26. }
  27. return KTIME_MAX;
  28. }
  29. static void tcf_gate_params_free_rcu(struct rcu_head *head);
  30. static void gate_get_start_time(struct tcf_gate *gact,
  31. const struct tcf_gate_params *param,
  32. ktime_t *start)
  33. {
  34. ktime_t now, base, cycle;
  35. u64 n;
  36. base = ns_to_ktime(param->tcfg_basetime);
  37. now = gate_get_time(gact);
  38. if (ktime_after(base, now)) {
  39. *start = base;
  40. return;
  41. }
  42. cycle = param->tcfg_cycletime;
  43. n = div64_u64(ktime_sub_ns(now, base), cycle);
  44. *start = ktime_add_ns(base, (n + 1) * cycle);
  45. }
  46. static void gate_start_timer(struct tcf_gate *gact, ktime_t start)
  47. {
  48. ktime_t expires;
  49. expires = hrtimer_get_expires(&gact->hitimer);
  50. if (expires == 0)
  51. expires = KTIME_MAX;
  52. start = min_t(ktime_t, start, expires);
  53. hrtimer_start(&gact->hitimer, start, HRTIMER_MODE_ABS_SOFT);
  54. }
  55. static enum hrtimer_restart gate_timer_func(struct hrtimer *timer)
  56. {
  57. struct tcf_gate *gact = container_of(timer, struct tcf_gate,
  58. hitimer);
  59. struct tcfg_gate_entry *next;
  60. struct tcf_gate_params *p;
  61. ktime_t close_time, now;
  62. spin_lock(&gact->tcf_lock);
  63. p = rcu_dereference_protected(gact->param,
  64. lockdep_is_held(&gact->tcf_lock));
  65. next = gact->next_entry;
  66. /* cycle start, clear pending bit, clear total octets */
  67. gact->current_gate_status = next->gate_state ? GATE_ACT_GATE_OPEN : 0;
  68. gact->current_entry_octets = 0;
  69. gact->current_max_octets = next->maxoctets;
  70. gact->current_close_time = ktime_add_ns(gact->current_close_time,
  71. next->interval);
  72. close_time = gact->current_close_time;
  73. if (list_is_last(&next->list, &p->entries))
  74. next = list_first_entry(&p->entries,
  75. struct tcfg_gate_entry, list);
  76. else
  77. next = list_next_entry(next, list);
  78. now = gate_get_time(gact);
  79. if (ktime_after(now, close_time)) {
  80. ktime_t cycle, base;
  81. u64 n;
  82. cycle = p->tcfg_cycletime;
  83. base = ns_to_ktime(p->tcfg_basetime);
  84. n = div64_u64(ktime_sub_ns(now, base), cycle);
  85. close_time = ktime_add_ns(base, (n + 1) * cycle);
  86. }
  87. gact->next_entry = next;
  88. hrtimer_set_expires(&gact->hitimer, close_time);
  89. spin_unlock(&gact->tcf_lock);
  90. return HRTIMER_RESTART;
  91. }
  92. TC_INDIRECT_SCOPE int tcf_gate_act(struct sk_buff *skb,
  93. const struct tc_action *a,
  94. struct tcf_result *res)
  95. {
  96. struct tcf_gate *gact = to_gate(a);
  97. int action = READ_ONCE(gact->tcf_action);
  98. tcf_lastuse_update(&gact->tcf_tm);
  99. tcf_action_update_bstats(&gact->common, skb);
  100. spin_lock(&gact->tcf_lock);
  101. if (unlikely(gact->current_gate_status & GATE_ACT_PENDING)) {
  102. spin_unlock(&gact->tcf_lock);
  103. return action;
  104. }
  105. if (!(gact->current_gate_status & GATE_ACT_GATE_OPEN)) {
  106. spin_unlock(&gact->tcf_lock);
  107. goto drop;
  108. }
  109. if (gact->current_max_octets >= 0) {
  110. gact->current_entry_octets += qdisc_pkt_len(skb);
  111. if (gact->current_entry_octets > gact->current_max_octets) {
  112. spin_unlock(&gact->tcf_lock);
  113. goto overlimit;
  114. }
  115. }
  116. spin_unlock(&gact->tcf_lock);
  117. return action;
  118. overlimit:
  119. tcf_action_inc_overlimit_qstats(&gact->common);
  120. drop:
  121. tcf_action_inc_drop_qstats(&gact->common);
  122. return TC_ACT_SHOT;
  123. }
  124. static const struct nla_policy entry_policy[TCA_GATE_ENTRY_MAX + 1] = {
  125. [TCA_GATE_ENTRY_INDEX] = { .type = NLA_U32 },
  126. [TCA_GATE_ENTRY_GATE] = { .type = NLA_FLAG },
  127. [TCA_GATE_ENTRY_INTERVAL] = { .type = NLA_U32 },
  128. [TCA_GATE_ENTRY_IPV] = { .type = NLA_S32 },
  129. [TCA_GATE_ENTRY_MAX_OCTETS] = { .type = NLA_S32 },
  130. };
  131. static const struct nla_policy gate_policy[TCA_GATE_MAX + 1] = {
  132. [TCA_GATE_PARMS] =
  133. NLA_POLICY_EXACT_LEN(sizeof(struct tc_gate)),
  134. [TCA_GATE_PRIORITY] = { .type = NLA_S32 },
  135. [TCA_GATE_ENTRY_LIST] = { .type = NLA_NESTED },
  136. [TCA_GATE_BASE_TIME] = { .type = NLA_U64 },
  137. [TCA_GATE_CYCLE_TIME] = { .type = NLA_U64 },
  138. [TCA_GATE_CYCLE_TIME_EXT] = { .type = NLA_U64 },
  139. [TCA_GATE_FLAGS] = { .type = NLA_U32 },
  140. [TCA_GATE_CLOCKID] = { .type = NLA_S32 },
  141. };
  142. static int fill_gate_entry(struct nlattr **tb, struct tcfg_gate_entry *entry,
  143. struct netlink_ext_ack *extack)
  144. {
  145. u32 interval = 0;
  146. entry->gate_state = nla_get_flag(tb[TCA_GATE_ENTRY_GATE]);
  147. if (tb[TCA_GATE_ENTRY_INTERVAL])
  148. interval = nla_get_u32(tb[TCA_GATE_ENTRY_INTERVAL]);
  149. if (interval == 0) {
  150. NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
  151. return -EINVAL;
  152. }
  153. entry->interval = interval;
  154. entry->ipv = nla_get_s32_default(tb[TCA_GATE_ENTRY_IPV], -1);
  155. entry->maxoctets = nla_get_s32_default(tb[TCA_GATE_ENTRY_MAX_OCTETS],
  156. -1);
  157. return 0;
  158. }
  159. static int parse_gate_entry(struct nlattr *n, struct tcfg_gate_entry *entry,
  160. int index, struct netlink_ext_ack *extack)
  161. {
  162. struct nlattr *tb[TCA_GATE_ENTRY_MAX + 1] = { };
  163. int err;
  164. err = nla_parse_nested(tb, TCA_GATE_ENTRY_MAX, n, entry_policy, extack);
  165. if (err < 0) {
  166. NL_SET_ERR_MSG(extack, "Could not parse nested entry");
  167. return -EINVAL;
  168. }
  169. entry->index = index;
  170. return fill_gate_entry(tb, entry, extack);
  171. }
  172. static void release_entry_list(struct list_head *entries)
  173. {
  174. struct tcfg_gate_entry *entry, *e;
  175. list_for_each_entry_safe(entry, e, entries, list) {
  176. list_del(&entry->list);
  177. kfree(entry);
  178. }
  179. }
  180. static int tcf_gate_copy_entries(struct tcf_gate_params *dst,
  181. const struct tcf_gate_params *src,
  182. struct netlink_ext_ack *extack)
  183. {
  184. struct tcfg_gate_entry *entry;
  185. int i = 0;
  186. list_for_each_entry(entry, &src->entries, list) {
  187. struct tcfg_gate_entry *new;
  188. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  189. if (!new) {
  190. NL_SET_ERR_MSG(extack, "Not enough memory for entry");
  191. return -ENOMEM;
  192. }
  193. new->index = entry->index;
  194. new->gate_state = entry->gate_state;
  195. new->interval = entry->interval;
  196. new->ipv = entry->ipv;
  197. new->maxoctets = entry->maxoctets;
  198. list_add_tail(&new->list, &dst->entries);
  199. i++;
  200. }
  201. dst->num_entries = i;
  202. return 0;
  203. }
  204. static int parse_gate_list(struct nlattr *list_attr,
  205. struct tcf_gate_params *sched,
  206. struct netlink_ext_ack *extack)
  207. {
  208. struct tcfg_gate_entry *entry;
  209. struct nlattr *n;
  210. int err, rem;
  211. int i = 0;
  212. if (!list_attr)
  213. return -EINVAL;
  214. nla_for_each_nested(n, list_attr, rem) {
  215. if (nla_type(n) != TCA_GATE_ONE_ENTRY) {
  216. NL_SET_ERR_MSG(extack, "Attribute isn't type 'entry'");
  217. continue;
  218. }
  219. entry = kzalloc_obj(*entry, GFP_ATOMIC);
  220. if (!entry) {
  221. NL_SET_ERR_MSG(extack, "Not enough memory for entry");
  222. err = -ENOMEM;
  223. goto release_list;
  224. }
  225. err = parse_gate_entry(n, entry, i, extack);
  226. if (err < 0) {
  227. kfree(entry);
  228. goto release_list;
  229. }
  230. list_add_tail(&entry->list, &sched->entries);
  231. i++;
  232. }
  233. sched->num_entries = i;
  234. return i;
  235. release_list:
  236. release_entry_list(&sched->entries);
  237. return err;
  238. }
  239. static bool gate_timer_needs_cancel(u64 basetime, u64 old_basetime,
  240. enum tk_offsets tko,
  241. enum tk_offsets old_tko,
  242. s32 clockid, s32 old_clockid)
  243. {
  244. return basetime != old_basetime ||
  245. clockid != old_clockid ||
  246. tko != old_tko;
  247. }
  248. static int gate_clock_resolve(s32 clockid, enum tk_offsets *tko,
  249. struct netlink_ext_ack *extack)
  250. {
  251. switch (clockid) {
  252. case CLOCK_REALTIME:
  253. *tko = TK_OFFS_REAL;
  254. return 0;
  255. case CLOCK_MONOTONIC:
  256. *tko = TK_OFFS_MAX;
  257. return 0;
  258. case CLOCK_BOOTTIME:
  259. *tko = TK_OFFS_BOOT;
  260. return 0;
  261. case CLOCK_TAI:
  262. *tko = TK_OFFS_TAI;
  263. return 0;
  264. default:
  265. NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
  266. return -EINVAL;
  267. }
  268. }
  269. static void gate_setup_timer(struct tcf_gate *gact, s32 clockid,
  270. enum tk_offsets tko)
  271. {
  272. WRITE_ONCE(gact->tk_offset, tko);
  273. hrtimer_setup(&gact->hitimer, gate_timer_func, clockid,
  274. HRTIMER_MODE_ABS_SOFT);
  275. }
  276. static int tcf_gate_init(struct net *net, struct nlattr *nla,
  277. struct nlattr *est, struct tc_action **a,
  278. struct tcf_proto *tp, u32 flags,
  279. struct netlink_ext_ack *extack)
  280. {
  281. struct tc_action_net *tn = net_generic(net, act_gate_ops.net_id);
  282. u64 cycletime = 0, basetime = 0, cycletime_ext = 0;
  283. struct tcf_gate_params *p = NULL, *old_p = NULL;
  284. enum tk_offsets old_tk_offset = TK_OFFS_TAI;
  285. const struct tcf_gate_params *cur_p = NULL;
  286. bool bind = flags & TCA_ACT_FLAGS_BIND;
  287. struct nlattr *tb[TCA_GATE_MAX + 1];
  288. enum tk_offsets tko = TK_OFFS_TAI;
  289. struct tcf_chain *goto_ch = NULL;
  290. s32 timer_clockid = CLOCK_TAI;
  291. bool use_old_entries = false;
  292. s32 old_clockid = CLOCK_TAI;
  293. bool need_cancel = false;
  294. s32 clockid = CLOCK_TAI;
  295. struct tcf_gate *gact;
  296. struct tc_gate *parm;
  297. u64 old_basetime = 0;
  298. int ret = 0, err;
  299. u32 gflags = 0;
  300. s32 prio = -1;
  301. ktime_t start;
  302. u32 index;
  303. if (!nla)
  304. return -EINVAL;
  305. err = nla_parse_nested(tb, TCA_GATE_MAX, nla, gate_policy, extack);
  306. if (err < 0)
  307. return err;
  308. if (!tb[TCA_GATE_PARMS])
  309. return -EINVAL;
  310. if (tb[TCA_GATE_CLOCKID])
  311. clockid = nla_get_s32(tb[TCA_GATE_CLOCKID]);
  312. parm = nla_data(tb[TCA_GATE_PARMS]);
  313. index = parm->index;
  314. err = tcf_idr_check_alloc(tn, &index, a, bind);
  315. if (err < 0)
  316. return err;
  317. if (err && bind)
  318. return ACT_P_BOUND;
  319. if (!err) {
  320. ret = tcf_idr_create_from_flags(tn, index, est, a,
  321. &act_gate_ops, bind, flags);
  322. if (ret) {
  323. tcf_idr_cleanup(tn, index);
  324. return ret;
  325. }
  326. ret = ACT_P_CREATED;
  327. } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  328. tcf_idr_release(*a, bind);
  329. return -EEXIST;
  330. }
  331. gact = to_gate(*a);
  332. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  333. if (err < 0)
  334. goto release_idr;
  335. p = kzalloc(sizeof(*p), GFP_KERNEL);
  336. if (!p) {
  337. err = -ENOMEM;
  338. goto chain_put;
  339. }
  340. INIT_LIST_HEAD(&p->entries);
  341. use_old_entries = !tb[TCA_GATE_ENTRY_LIST];
  342. if (!use_old_entries) {
  343. err = parse_gate_list(tb[TCA_GATE_ENTRY_LIST], p, extack);
  344. if (err < 0)
  345. goto err_free;
  346. use_old_entries = !err;
  347. }
  348. if (ret == ACT_P_CREATED && use_old_entries) {
  349. NL_SET_ERR_MSG(extack, "The entry list is empty");
  350. err = -EINVAL;
  351. goto err_free;
  352. }
  353. if (ret != ACT_P_CREATED) {
  354. rcu_read_lock();
  355. cur_p = rcu_dereference(gact->param);
  356. old_basetime = cur_p->tcfg_basetime;
  357. old_clockid = cur_p->tcfg_clockid;
  358. old_tk_offset = READ_ONCE(gact->tk_offset);
  359. basetime = old_basetime;
  360. cycletime_ext = cur_p->tcfg_cycletime_ext;
  361. prio = cur_p->tcfg_priority;
  362. gflags = cur_p->tcfg_flags;
  363. if (!tb[TCA_GATE_CLOCKID])
  364. clockid = old_clockid;
  365. err = 0;
  366. if (use_old_entries) {
  367. err = tcf_gate_copy_entries(p, cur_p, extack);
  368. if (!err && !tb[TCA_GATE_CYCLE_TIME])
  369. cycletime = cur_p->tcfg_cycletime;
  370. }
  371. rcu_read_unlock();
  372. if (err)
  373. goto err_free;
  374. }
  375. if (tb[TCA_GATE_PRIORITY])
  376. prio = nla_get_s32(tb[TCA_GATE_PRIORITY]);
  377. if (tb[TCA_GATE_BASE_TIME])
  378. basetime = nla_get_u64(tb[TCA_GATE_BASE_TIME]);
  379. if (tb[TCA_GATE_FLAGS])
  380. gflags = nla_get_u32(tb[TCA_GATE_FLAGS]);
  381. if (tb[TCA_GATE_CYCLE_TIME])
  382. cycletime = nla_get_u64(tb[TCA_GATE_CYCLE_TIME]);
  383. if (tb[TCA_GATE_CYCLE_TIME_EXT])
  384. cycletime_ext = nla_get_u64(tb[TCA_GATE_CYCLE_TIME_EXT]);
  385. err = gate_clock_resolve(clockid, &tko, extack);
  386. if (err)
  387. goto err_free;
  388. timer_clockid = clockid;
  389. need_cancel = ret != ACT_P_CREATED &&
  390. gate_timer_needs_cancel(basetime, old_basetime,
  391. tko, old_tk_offset,
  392. timer_clockid, old_clockid);
  393. if (need_cancel)
  394. hrtimer_cancel(&gact->hitimer);
  395. spin_lock_bh(&gact->tcf_lock);
  396. if (!cycletime) {
  397. struct tcfg_gate_entry *entry;
  398. ktime_t cycle = 0;
  399. list_for_each_entry(entry, &p->entries, list)
  400. cycle = ktime_add_ns(cycle, entry->interval);
  401. cycletime = cycle;
  402. }
  403. p->tcfg_cycletime = cycletime;
  404. p->tcfg_cycletime_ext = cycletime_ext;
  405. if (need_cancel || ret == ACT_P_CREATED)
  406. gate_setup_timer(gact, timer_clockid, tko);
  407. p->tcfg_priority = prio;
  408. p->tcfg_flags = gflags;
  409. p->tcfg_basetime = basetime;
  410. p->tcfg_clockid = timer_clockid;
  411. gate_get_start_time(gact, p, &start);
  412. old_p = rcu_replace_pointer(gact->param, p,
  413. lockdep_is_held(&gact->tcf_lock));
  414. gact->current_close_time = start;
  415. gact->current_gate_status = GATE_ACT_GATE_OPEN | GATE_ACT_PENDING;
  416. gact->next_entry = list_first_entry(&p->entries,
  417. struct tcfg_gate_entry, list);
  418. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  419. gate_start_timer(gact, start);
  420. spin_unlock_bh(&gact->tcf_lock);
  421. if (goto_ch)
  422. tcf_chain_put_by_act(goto_ch);
  423. if (old_p)
  424. call_rcu(&old_p->rcu, tcf_gate_params_free_rcu);
  425. return ret;
  426. err_free:
  427. release_entry_list(&p->entries);
  428. kfree(p);
  429. chain_put:
  430. if (goto_ch)
  431. tcf_chain_put_by_act(goto_ch);
  432. release_idr:
  433. /* action is not inserted in any list: it's safe to init hitimer
  434. * without taking tcf_lock.
  435. */
  436. if (ret == ACT_P_CREATED)
  437. gate_setup_timer(gact, timer_clockid, tko);
  438. tcf_idr_release(*a, bind);
  439. return err;
  440. }
  441. static void tcf_gate_params_free_rcu(struct rcu_head *head)
  442. {
  443. struct tcf_gate_params *p = container_of(head, struct tcf_gate_params, rcu);
  444. release_entry_list(&p->entries);
  445. kfree(p);
  446. }
  447. static void tcf_gate_cleanup(struct tc_action *a)
  448. {
  449. struct tcf_gate *gact = to_gate(a);
  450. struct tcf_gate_params *p;
  451. hrtimer_cancel(&gact->hitimer);
  452. p = rcu_dereference_protected(gact->param, 1);
  453. if (p)
  454. call_rcu(&p->rcu, tcf_gate_params_free_rcu);
  455. }
  456. static int dumping_entry(struct sk_buff *skb,
  457. struct tcfg_gate_entry *entry)
  458. {
  459. struct nlattr *item;
  460. item = nla_nest_start_noflag(skb, TCA_GATE_ONE_ENTRY);
  461. if (!item)
  462. return -ENOSPC;
  463. if (nla_put_u32(skb, TCA_GATE_ENTRY_INDEX, entry->index))
  464. goto nla_put_failure;
  465. if (entry->gate_state && nla_put_flag(skb, TCA_GATE_ENTRY_GATE))
  466. goto nla_put_failure;
  467. if (nla_put_u32(skb, TCA_GATE_ENTRY_INTERVAL, entry->interval))
  468. goto nla_put_failure;
  469. if (nla_put_s32(skb, TCA_GATE_ENTRY_MAX_OCTETS, entry->maxoctets))
  470. goto nla_put_failure;
  471. if (nla_put_s32(skb, TCA_GATE_ENTRY_IPV, entry->ipv))
  472. goto nla_put_failure;
  473. return nla_nest_end(skb, item);
  474. nla_put_failure:
  475. nla_nest_cancel(skb, item);
  476. return -1;
  477. }
  478. static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
  479. int bind, int ref)
  480. {
  481. unsigned char *b = skb_tail_pointer(skb);
  482. struct tcf_gate *gact = to_gate(a);
  483. struct tc_gate opt = {
  484. .index = gact->tcf_index,
  485. .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
  486. .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
  487. };
  488. struct tcfg_gate_entry *entry;
  489. struct tcf_gate_params *p;
  490. struct nlattr *entry_list;
  491. struct tcf_t t;
  492. rcu_read_lock();
  493. opt.action = READ_ONCE(gact->tcf_action);
  494. p = rcu_dereference(gact->param);
  495. if (nla_put(skb, TCA_GATE_PARMS, sizeof(opt), &opt))
  496. goto nla_put_failure;
  497. if (nla_put_u64_64bit(skb, TCA_GATE_BASE_TIME,
  498. p->tcfg_basetime, TCA_GATE_PAD))
  499. goto nla_put_failure;
  500. if (nla_put_u64_64bit(skb, TCA_GATE_CYCLE_TIME,
  501. p->tcfg_cycletime, TCA_GATE_PAD))
  502. goto nla_put_failure;
  503. if (nla_put_u64_64bit(skb, TCA_GATE_CYCLE_TIME_EXT,
  504. p->tcfg_cycletime_ext, TCA_GATE_PAD))
  505. goto nla_put_failure;
  506. if (nla_put_s32(skb, TCA_GATE_CLOCKID, p->tcfg_clockid))
  507. goto nla_put_failure;
  508. if (nla_put_u32(skb, TCA_GATE_FLAGS, p->tcfg_flags))
  509. goto nla_put_failure;
  510. if (nla_put_s32(skb, TCA_GATE_PRIORITY, p->tcfg_priority))
  511. goto nla_put_failure;
  512. entry_list = nla_nest_start_noflag(skb, TCA_GATE_ENTRY_LIST);
  513. if (!entry_list)
  514. goto nla_put_failure;
  515. list_for_each_entry(entry, &p->entries, list) {
  516. if (dumping_entry(skb, entry) < 0)
  517. goto nla_put_failure;
  518. }
  519. nla_nest_end(skb, entry_list);
  520. tcf_tm_dump(&t, &gact->tcf_tm);
  521. if (nla_put_64bit(skb, TCA_GATE_TM, sizeof(t), &t, TCA_GATE_PAD))
  522. goto nla_put_failure;
  523. rcu_read_unlock();
  524. return skb->len;
  525. nla_put_failure:
  526. rcu_read_unlock();
  527. nlmsg_trim(skb, b);
  528. return -1;
  529. }
  530. static void tcf_gate_stats_update(struct tc_action *a, u64 bytes, u64 packets,
  531. u64 drops, u64 lastuse, bool hw)
  532. {
  533. struct tcf_gate *gact = to_gate(a);
  534. struct tcf_t *tm = &gact->tcf_tm;
  535. tcf_action_update_stats(a, bytes, packets, drops, hw);
  536. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  537. }
  538. static size_t tcf_gate_get_fill_size(const struct tc_action *act)
  539. {
  540. return nla_total_size(sizeof(struct tc_gate));
  541. }
  542. static void tcf_gate_entry_destructor(void *priv)
  543. {
  544. struct action_gate_entry *oe = priv;
  545. kfree(oe);
  546. }
  547. static int tcf_gate_get_entries(struct flow_action_entry *entry,
  548. const struct tc_action *act)
  549. {
  550. entry->gate.entries = tcf_gate_get_list(act);
  551. if (!entry->gate.entries)
  552. return -EINVAL;
  553. entry->destructor = tcf_gate_entry_destructor;
  554. entry->destructor_priv = entry->gate.entries;
  555. return 0;
  556. }
  557. static int tcf_gate_offload_act_setup(struct tc_action *act, void *entry_data,
  558. u32 *index_inc, bool bind,
  559. struct netlink_ext_ack *extack)
  560. {
  561. int err;
  562. if (bind) {
  563. struct flow_action_entry *entry = entry_data;
  564. entry->id = FLOW_ACTION_GATE;
  565. entry->gate.prio = tcf_gate_prio(act);
  566. entry->gate.basetime = tcf_gate_basetime(act);
  567. entry->gate.cycletime = tcf_gate_cycletime(act);
  568. entry->gate.cycletimeext = tcf_gate_cycletimeext(act);
  569. entry->gate.num_entries = tcf_gate_num_entries(act);
  570. err = tcf_gate_get_entries(entry, act);
  571. if (err)
  572. return err;
  573. *index_inc = 1;
  574. } else {
  575. struct flow_offload_action *fl_action = entry_data;
  576. fl_action->id = FLOW_ACTION_GATE;
  577. }
  578. return 0;
  579. }
  580. static struct tc_action_ops act_gate_ops = {
  581. .kind = "gate",
  582. .id = TCA_ID_GATE,
  583. .owner = THIS_MODULE,
  584. .act = tcf_gate_act,
  585. .dump = tcf_gate_dump,
  586. .init = tcf_gate_init,
  587. .cleanup = tcf_gate_cleanup,
  588. .stats_update = tcf_gate_stats_update,
  589. .get_fill_size = tcf_gate_get_fill_size,
  590. .offload_act_setup = tcf_gate_offload_act_setup,
  591. .size = sizeof(struct tcf_gate),
  592. };
  593. MODULE_ALIAS_NET_ACT("gate");
  594. static __net_init int gate_init_net(struct net *net)
  595. {
  596. struct tc_action_net *tn = net_generic(net, act_gate_ops.net_id);
  597. return tc_action_net_init(net, tn, &act_gate_ops);
  598. }
  599. static void __net_exit gate_exit_net(struct list_head *net_list)
  600. {
  601. tc_action_net_exit(net_list, act_gate_ops.net_id);
  602. }
  603. static struct pernet_operations gate_net_ops = {
  604. .init = gate_init_net,
  605. .exit_batch = gate_exit_net,
  606. .id = &act_gate_ops.net_id,
  607. .size = sizeof(struct tc_action_net),
  608. };
  609. static int __init gate_init_module(void)
  610. {
  611. return tcf_register_action(&act_gate_ops, &gate_net_ops);
  612. }
  613. static void __exit gate_cleanup_module(void)
  614. {
  615. tcf_unregister_action(&act_gate_ops, &gate_net_ops);
  616. }
  617. module_init(gate_init_module);
  618. module_exit(gate_cleanup_module);
  619. MODULE_DESCRIPTION("TC gate action");
  620. MODULE_LICENSE("GPL v2");