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