15a781ccbSVinicius Costa Gomes // SPDX-License-Identifier: GPL-2.0 25a781ccbSVinicius Costa Gomes 35a781ccbSVinicius Costa Gomes /* net/sched/sch_taprio.c Time Aware Priority Scheduler 45a781ccbSVinicius Costa Gomes * 55a781ccbSVinicius Costa Gomes * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com> 65a781ccbSVinicius Costa Gomes * 75a781ccbSVinicius Costa Gomes */ 85a781ccbSVinicius Costa Gomes 95a781ccbSVinicius Costa Gomes #include <linux/types.h> 105a781ccbSVinicius Costa Gomes #include <linux/slab.h> 115a781ccbSVinicius Costa Gomes #include <linux/kernel.h> 125a781ccbSVinicius Costa Gomes #include <linux/string.h> 135a781ccbSVinicius Costa Gomes #include <linux/list.h> 145a781ccbSVinicius Costa Gomes #include <linux/errno.h> 155a781ccbSVinicius Costa Gomes #include <linux/skbuff.h> 1623bddf69SJakub Kicinski #include <linux/math64.h> 175a781ccbSVinicius Costa Gomes #include <linux/module.h> 185a781ccbSVinicius Costa Gomes #include <linux/spinlock.h> 19a3d43c0dSVinicius Costa Gomes #include <linux/rcupdate.h> 205a781ccbSVinicius Costa Gomes #include <net/netlink.h> 215a781ccbSVinicius Costa Gomes #include <net/pkt_sched.h> 225a781ccbSVinicius Costa Gomes #include <net/pkt_cls.h> 235a781ccbSVinicius Costa Gomes #include <net/sch_generic.h> 244cfd5779SVedang Patel #include <net/sock.h> 2554002066SVedang Patel #include <net/tcp.h> 265a781ccbSVinicius Costa Gomes 277b9eba7bSLeandro Dorileo static LIST_HEAD(taprio_list); 287b9eba7bSLeandro Dorileo static DEFINE_SPINLOCK(taprio_list_lock); 297b9eba7bSLeandro Dorileo 305a781ccbSVinicius Costa Gomes #define TAPRIO_ALL_GATES_OPEN -1 315a781ccbSVinicius Costa Gomes 324cfd5779SVedang Patel #define FLAGS_VALID(flags) (!((flags) & ~TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)) 334cfd5779SVedang Patel #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) 344cfd5779SVedang Patel 355a781ccbSVinicius Costa Gomes struct sched_entry { 365a781ccbSVinicius Costa Gomes struct list_head list; 375a781ccbSVinicius Costa Gomes 385a781ccbSVinicius Costa Gomes /* The instant that this entry "closes" and the next one 395a781ccbSVinicius Costa Gomes * should open, the qdisc will make some effort so that no 405a781ccbSVinicius Costa Gomes * packet leaves after this time. 415a781ccbSVinicius Costa Gomes */ 425a781ccbSVinicius Costa Gomes ktime_t close_time; 434cfd5779SVedang Patel ktime_t next_txtime; 445a781ccbSVinicius Costa Gomes atomic_t budget; 455a781ccbSVinicius Costa Gomes int index; 465a781ccbSVinicius Costa Gomes u32 gate_mask; 475a781ccbSVinicius Costa Gomes u32 interval; 485a781ccbSVinicius Costa Gomes u8 command; 495a781ccbSVinicius Costa Gomes }; 505a781ccbSVinicius Costa Gomes 51a3d43c0dSVinicius Costa Gomes struct sched_gate_list { 52a3d43c0dSVinicius Costa Gomes struct rcu_head rcu; 53a3d43c0dSVinicius Costa Gomes struct list_head entries; 54a3d43c0dSVinicius Costa Gomes size_t num_entries; 556ca6a665SVinicius Costa Gomes ktime_t cycle_close_time; 566ca6a665SVinicius Costa Gomes s64 cycle_time; 57c25031e9SVinicius Costa Gomes s64 cycle_time_extension; 58a3d43c0dSVinicius Costa Gomes s64 base_time; 59a3d43c0dSVinicius Costa Gomes }; 60a3d43c0dSVinicius Costa Gomes 615a781ccbSVinicius Costa Gomes struct taprio_sched { 625a781ccbSVinicius Costa Gomes struct Qdisc **qdiscs; 635a781ccbSVinicius Costa Gomes struct Qdisc *root; 644cfd5779SVedang Patel u32 flags; 657ede7b03SVedang Patel enum tk_offsets tk_offset; 665a781ccbSVinicius Costa Gomes int clockid; 677b9eba7bSLeandro Dorileo atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+ 685a781ccbSVinicius Costa Gomes * speeds it's sub-nanoseconds per byte 695a781ccbSVinicius Costa Gomes */ 705a781ccbSVinicius Costa Gomes 715a781ccbSVinicius Costa Gomes /* Protects the update side of the RCU protected current_entry */ 725a781ccbSVinicius Costa Gomes spinlock_t current_entry_lock; 735a781ccbSVinicius Costa Gomes struct sched_entry __rcu *current_entry; 74a3d43c0dSVinicius Costa Gomes struct sched_gate_list __rcu *oper_sched; 75a3d43c0dSVinicius Costa Gomes struct sched_gate_list __rcu *admin_sched; 765a781ccbSVinicius Costa Gomes struct hrtimer advance_timer; 777b9eba7bSLeandro Dorileo struct list_head taprio_list; 78*a5b64700SVedang Patel u32 txtime_delay; 795a781ccbSVinicius Costa Gomes }; 805a781ccbSVinicius Costa Gomes 81a3d43c0dSVinicius Costa Gomes static ktime_t sched_base_time(const struct sched_gate_list *sched) 82a3d43c0dSVinicius Costa Gomes { 83a3d43c0dSVinicius Costa Gomes if (!sched) 84a3d43c0dSVinicius Costa Gomes return KTIME_MAX; 85a3d43c0dSVinicius Costa Gomes 86a3d43c0dSVinicius Costa Gomes return ns_to_ktime(sched->base_time); 87a3d43c0dSVinicius Costa Gomes } 88a3d43c0dSVinicius Costa Gomes 897ede7b03SVedang Patel static ktime_t taprio_get_time(struct taprio_sched *q) 907ede7b03SVedang Patel { 917ede7b03SVedang Patel ktime_t mono = ktime_get(); 927ede7b03SVedang Patel 937ede7b03SVedang Patel switch (q->tk_offset) { 947ede7b03SVedang Patel case TK_OFFS_MAX: 957ede7b03SVedang Patel return mono; 967ede7b03SVedang Patel default: 977ede7b03SVedang Patel return ktime_mono_to_any(mono, q->tk_offset); 987ede7b03SVedang Patel } 997ede7b03SVedang Patel 1007ede7b03SVedang Patel return KTIME_MAX; 1017ede7b03SVedang Patel } 1027ede7b03SVedang Patel 103a3d43c0dSVinicius Costa Gomes static void taprio_free_sched_cb(struct rcu_head *head) 104a3d43c0dSVinicius Costa Gomes { 105a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu); 106a3d43c0dSVinicius Costa Gomes struct sched_entry *entry, *n; 107a3d43c0dSVinicius Costa Gomes 108a3d43c0dSVinicius Costa Gomes if (!sched) 109a3d43c0dSVinicius Costa Gomes return; 110a3d43c0dSVinicius Costa Gomes 111a3d43c0dSVinicius Costa Gomes list_for_each_entry_safe(entry, n, &sched->entries, list) { 112a3d43c0dSVinicius Costa Gomes list_del(&entry->list); 113a3d43c0dSVinicius Costa Gomes kfree(entry); 114a3d43c0dSVinicius Costa Gomes } 115a3d43c0dSVinicius Costa Gomes 116a3d43c0dSVinicius Costa Gomes kfree(sched); 117a3d43c0dSVinicius Costa Gomes } 118a3d43c0dSVinicius Costa Gomes 119a3d43c0dSVinicius Costa Gomes static void switch_schedules(struct taprio_sched *q, 120a3d43c0dSVinicius Costa Gomes struct sched_gate_list **admin, 121a3d43c0dSVinicius Costa Gomes struct sched_gate_list **oper) 122a3d43c0dSVinicius Costa Gomes { 123a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->oper_sched, *admin); 124a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->admin_sched, NULL); 125a3d43c0dSVinicius Costa Gomes 126a3d43c0dSVinicius Costa Gomes if (*oper) 127a3d43c0dSVinicius Costa Gomes call_rcu(&(*oper)->rcu, taprio_free_sched_cb); 128a3d43c0dSVinicius Costa Gomes 129a3d43c0dSVinicius Costa Gomes *oper = *admin; 130a3d43c0dSVinicius Costa Gomes *admin = NULL; 131a3d43c0dSVinicius Costa Gomes } 132a3d43c0dSVinicius Costa Gomes 1334cfd5779SVedang Patel /* Get how much time has been already elapsed in the current cycle. */ 1344cfd5779SVedang Patel static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time) 1354cfd5779SVedang Patel { 1364cfd5779SVedang Patel ktime_t time_since_sched_start; 1374cfd5779SVedang Patel s32 time_elapsed; 1384cfd5779SVedang Patel 1394cfd5779SVedang Patel time_since_sched_start = ktime_sub(time, sched->base_time); 1404cfd5779SVedang Patel div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed); 1414cfd5779SVedang Patel 1424cfd5779SVedang Patel return time_elapsed; 1434cfd5779SVedang Patel } 1444cfd5779SVedang Patel 1454cfd5779SVedang Patel static ktime_t get_interval_end_time(struct sched_gate_list *sched, 1464cfd5779SVedang Patel struct sched_gate_list *admin, 1474cfd5779SVedang Patel struct sched_entry *entry, 1484cfd5779SVedang Patel ktime_t intv_start) 1494cfd5779SVedang Patel { 1504cfd5779SVedang Patel s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start); 1514cfd5779SVedang Patel ktime_t intv_end, cycle_ext_end, cycle_end; 1524cfd5779SVedang Patel 1534cfd5779SVedang Patel cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed); 1544cfd5779SVedang Patel intv_end = ktime_add_ns(intv_start, entry->interval); 1554cfd5779SVedang Patel cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension); 1564cfd5779SVedang Patel 1574cfd5779SVedang Patel if (ktime_before(intv_end, cycle_end)) 1584cfd5779SVedang Patel return intv_end; 1594cfd5779SVedang Patel else if (admin && admin != sched && 1604cfd5779SVedang Patel ktime_after(admin->base_time, cycle_end) && 1614cfd5779SVedang Patel ktime_before(admin->base_time, cycle_ext_end)) 1624cfd5779SVedang Patel return admin->base_time; 1634cfd5779SVedang Patel else 1644cfd5779SVedang Patel return cycle_end; 1654cfd5779SVedang Patel } 1664cfd5779SVedang Patel 1674cfd5779SVedang Patel static int length_to_duration(struct taprio_sched *q, int len) 1684cfd5779SVedang Patel { 1694cfd5779SVedang Patel return div_u64(len * atomic64_read(&q->picos_per_byte), 1000); 1704cfd5779SVedang Patel } 1714cfd5779SVedang Patel 1724cfd5779SVedang Patel /* Returns the entry corresponding to next available interval. If 1734cfd5779SVedang Patel * validate_interval is set, it only validates whether the timestamp occurs 1744cfd5779SVedang Patel * when the gate corresponding to the skb's traffic class is open. 1754cfd5779SVedang Patel */ 1764cfd5779SVedang Patel static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb, 1774cfd5779SVedang Patel struct Qdisc *sch, 1784cfd5779SVedang Patel struct sched_gate_list *sched, 1794cfd5779SVedang Patel struct sched_gate_list *admin, 1804cfd5779SVedang Patel ktime_t time, 1814cfd5779SVedang Patel ktime_t *interval_start, 1824cfd5779SVedang Patel ktime_t *interval_end, 1834cfd5779SVedang Patel bool validate_interval) 1844cfd5779SVedang Patel { 1854cfd5779SVedang Patel ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time; 1864cfd5779SVedang Patel ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time; 1874cfd5779SVedang Patel struct sched_entry *entry = NULL, *entry_found = NULL; 1884cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 1894cfd5779SVedang Patel struct net_device *dev = qdisc_dev(sch); 1904cfd5779SVedang Patel bool entry_available = false; 1914cfd5779SVedang Patel s32 cycle_elapsed; 1924cfd5779SVedang Patel int tc, n; 1934cfd5779SVedang Patel 1944cfd5779SVedang Patel tc = netdev_get_prio_tc_map(dev, skb->priority); 1954cfd5779SVedang Patel packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb)); 1964cfd5779SVedang Patel 1974cfd5779SVedang Patel *interval_start = 0; 1984cfd5779SVedang Patel *interval_end = 0; 1994cfd5779SVedang Patel 2004cfd5779SVedang Patel if (!sched) 2014cfd5779SVedang Patel return NULL; 2024cfd5779SVedang Patel 2034cfd5779SVedang Patel cycle = sched->cycle_time; 2044cfd5779SVedang Patel cycle_elapsed = get_cycle_time_elapsed(sched, time); 2054cfd5779SVedang Patel curr_intv_end = ktime_sub_ns(time, cycle_elapsed); 2064cfd5779SVedang Patel cycle_end = ktime_add_ns(curr_intv_end, cycle); 2074cfd5779SVedang Patel 2084cfd5779SVedang Patel list_for_each_entry(entry, &sched->entries, list) { 2094cfd5779SVedang Patel curr_intv_start = curr_intv_end; 2104cfd5779SVedang Patel curr_intv_end = get_interval_end_time(sched, admin, entry, 2114cfd5779SVedang Patel curr_intv_start); 2124cfd5779SVedang Patel 2134cfd5779SVedang Patel if (ktime_after(curr_intv_start, cycle_end)) 2144cfd5779SVedang Patel break; 2154cfd5779SVedang Patel 2164cfd5779SVedang Patel if (!(entry->gate_mask & BIT(tc)) || 2174cfd5779SVedang Patel packet_transmit_time > entry->interval) 2184cfd5779SVedang Patel continue; 2194cfd5779SVedang Patel 2204cfd5779SVedang Patel txtime = entry->next_txtime; 2214cfd5779SVedang Patel 2224cfd5779SVedang Patel if (ktime_before(txtime, time) || validate_interval) { 2234cfd5779SVedang Patel transmit_end_time = ktime_add_ns(time, packet_transmit_time); 2244cfd5779SVedang Patel if ((ktime_before(curr_intv_start, time) && 2254cfd5779SVedang Patel ktime_before(transmit_end_time, curr_intv_end)) || 2264cfd5779SVedang Patel (ktime_after(curr_intv_start, time) && !validate_interval)) { 2274cfd5779SVedang Patel entry_found = entry; 2284cfd5779SVedang Patel *interval_start = curr_intv_start; 2294cfd5779SVedang Patel *interval_end = curr_intv_end; 2304cfd5779SVedang Patel break; 2314cfd5779SVedang Patel } else if (!entry_available && !validate_interval) { 2324cfd5779SVedang Patel /* Here, we are just trying to find out the 2334cfd5779SVedang Patel * first available interval in the next cycle. 2344cfd5779SVedang Patel */ 2354cfd5779SVedang Patel entry_available = 1; 2364cfd5779SVedang Patel entry_found = entry; 2374cfd5779SVedang Patel *interval_start = ktime_add_ns(curr_intv_start, cycle); 2384cfd5779SVedang Patel *interval_end = ktime_add_ns(curr_intv_end, cycle); 2394cfd5779SVedang Patel } 2404cfd5779SVedang Patel } else if (ktime_before(txtime, earliest_txtime) && 2414cfd5779SVedang Patel !entry_available) { 2424cfd5779SVedang Patel earliest_txtime = txtime; 2434cfd5779SVedang Patel entry_found = entry; 2444cfd5779SVedang Patel n = div_s64(ktime_sub(txtime, curr_intv_start), cycle); 2454cfd5779SVedang Patel *interval_start = ktime_add(curr_intv_start, n * cycle); 2464cfd5779SVedang Patel *interval_end = ktime_add(curr_intv_end, n * cycle); 2474cfd5779SVedang Patel } 2484cfd5779SVedang Patel } 2494cfd5779SVedang Patel 2504cfd5779SVedang Patel return entry_found; 2514cfd5779SVedang Patel } 2524cfd5779SVedang Patel 2534cfd5779SVedang Patel static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch) 2544cfd5779SVedang Patel { 2554cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 2564cfd5779SVedang Patel struct sched_gate_list *sched, *admin; 2574cfd5779SVedang Patel ktime_t interval_start, interval_end; 2584cfd5779SVedang Patel struct sched_entry *entry; 2594cfd5779SVedang Patel 2604cfd5779SVedang Patel rcu_read_lock(); 2614cfd5779SVedang Patel sched = rcu_dereference(q->oper_sched); 2624cfd5779SVedang Patel admin = rcu_dereference(q->admin_sched); 2634cfd5779SVedang Patel 2644cfd5779SVedang Patel entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp, 2654cfd5779SVedang Patel &interval_start, &interval_end, true); 2664cfd5779SVedang Patel rcu_read_unlock(); 2674cfd5779SVedang Patel 2684cfd5779SVedang Patel return entry; 2694cfd5779SVedang Patel } 2704cfd5779SVedang Patel 27154002066SVedang Patel /* This returns the tstamp value set by TCP in terms of the set clock. */ 27254002066SVedang Patel static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb) 27354002066SVedang Patel { 27454002066SVedang Patel unsigned int offset = skb_network_offset(skb); 27554002066SVedang Patel const struct ipv6hdr *ipv6h; 27654002066SVedang Patel const struct iphdr *iph; 27754002066SVedang Patel struct ipv6hdr _ipv6h; 27854002066SVedang Patel 27954002066SVedang Patel ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 28054002066SVedang Patel if (!ipv6h) 28154002066SVedang Patel return 0; 28254002066SVedang Patel 28354002066SVedang Patel if (ipv6h->version == 4) { 28454002066SVedang Patel iph = (struct iphdr *)ipv6h; 28554002066SVedang Patel offset += iph->ihl * 4; 28654002066SVedang Patel 28754002066SVedang Patel /* special-case 6in4 tunnelling, as that is a common way to get 28854002066SVedang Patel * v6 connectivity in the home 28954002066SVedang Patel */ 29054002066SVedang Patel if (iph->protocol == IPPROTO_IPV6) { 29154002066SVedang Patel ipv6h = skb_header_pointer(skb, offset, 29254002066SVedang Patel sizeof(_ipv6h), &_ipv6h); 29354002066SVedang Patel 29454002066SVedang Patel if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP) 29554002066SVedang Patel return 0; 29654002066SVedang Patel } else if (iph->protocol != IPPROTO_TCP) { 29754002066SVedang Patel return 0; 29854002066SVedang Patel } 29954002066SVedang Patel } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) { 30054002066SVedang Patel return 0; 30154002066SVedang Patel } 30254002066SVedang Patel 30354002066SVedang Patel return ktime_mono_to_any(skb->skb_mstamp_ns, q->tk_offset); 30454002066SVedang Patel } 30554002066SVedang Patel 3064cfd5779SVedang Patel /* There are a few scenarios where we will have to modify the txtime from 3074cfd5779SVedang Patel * what is read from next_txtime in sched_entry. They are: 3084cfd5779SVedang Patel * 1. If txtime is in the past, 3094cfd5779SVedang Patel * a. The gate for the traffic class is currently open and packet can be 3104cfd5779SVedang Patel * transmitted before it closes, schedule the packet right away. 3114cfd5779SVedang Patel * b. If the gate corresponding to the traffic class is going to open later 3124cfd5779SVedang Patel * in the cycle, set the txtime of packet to the interval start. 3134cfd5779SVedang Patel * 2. If txtime is in the future, there are packets corresponding to the 3144cfd5779SVedang Patel * current traffic class waiting to be transmitted. So, the following 3154cfd5779SVedang Patel * possibilities exist: 3164cfd5779SVedang Patel * a. We can transmit the packet before the window containing the txtime 3174cfd5779SVedang Patel * closes. 3184cfd5779SVedang Patel * b. The window might close before the transmission can be completed 3194cfd5779SVedang Patel * successfully. So, schedule the packet in the next open window. 3204cfd5779SVedang Patel */ 3214cfd5779SVedang Patel static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch) 3224cfd5779SVedang Patel { 32354002066SVedang Patel ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp; 3244cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 3254cfd5779SVedang Patel struct sched_gate_list *sched, *admin; 3264cfd5779SVedang Patel ktime_t minimum_time, now, txtime; 3274cfd5779SVedang Patel int len, packet_transmit_time; 3284cfd5779SVedang Patel struct sched_entry *entry; 3294cfd5779SVedang Patel bool sched_changed; 3304cfd5779SVedang Patel 3317ede7b03SVedang Patel now = taprio_get_time(q); 3324cfd5779SVedang Patel minimum_time = ktime_add_ns(now, q->txtime_delay); 3334cfd5779SVedang Patel 33454002066SVedang Patel tcp_tstamp = get_tcp_tstamp(q, skb); 33554002066SVedang Patel minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp); 33654002066SVedang Patel 3374cfd5779SVedang Patel rcu_read_lock(); 3384cfd5779SVedang Patel admin = rcu_dereference(q->admin_sched); 3394cfd5779SVedang Patel sched = rcu_dereference(q->oper_sched); 3404cfd5779SVedang Patel if (admin && ktime_after(minimum_time, admin->base_time)) 3414cfd5779SVedang Patel switch_schedules(q, &admin, &sched); 3424cfd5779SVedang Patel 3434cfd5779SVedang Patel /* Until the schedule starts, all the queues are open */ 3444cfd5779SVedang Patel if (!sched || ktime_before(minimum_time, sched->base_time)) { 3454cfd5779SVedang Patel txtime = minimum_time; 3464cfd5779SVedang Patel goto done; 3474cfd5779SVedang Patel } 3484cfd5779SVedang Patel 3494cfd5779SVedang Patel len = qdisc_pkt_len(skb); 3504cfd5779SVedang Patel packet_transmit_time = length_to_duration(q, len); 3514cfd5779SVedang Patel 3524cfd5779SVedang Patel do { 3534cfd5779SVedang Patel sched_changed = 0; 3544cfd5779SVedang Patel 3554cfd5779SVedang Patel entry = find_entry_to_transmit(skb, sch, sched, admin, 3564cfd5779SVedang Patel minimum_time, 3574cfd5779SVedang Patel &interval_start, &interval_end, 3584cfd5779SVedang Patel false); 3594cfd5779SVedang Patel if (!entry) { 3604cfd5779SVedang Patel txtime = 0; 3614cfd5779SVedang Patel goto done; 3624cfd5779SVedang Patel } 3634cfd5779SVedang Patel 3644cfd5779SVedang Patel txtime = entry->next_txtime; 3654cfd5779SVedang Patel txtime = max_t(ktime_t, txtime, minimum_time); 3664cfd5779SVedang Patel txtime = max_t(ktime_t, txtime, interval_start); 3674cfd5779SVedang Patel 3684cfd5779SVedang Patel if (admin && admin != sched && 3694cfd5779SVedang Patel ktime_after(txtime, admin->base_time)) { 3704cfd5779SVedang Patel sched = admin; 3714cfd5779SVedang Patel sched_changed = 1; 3724cfd5779SVedang Patel continue; 3734cfd5779SVedang Patel } 3744cfd5779SVedang Patel 3754cfd5779SVedang Patel transmit_end_time = ktime_add(txtime, packet_transmit_time); 3764cfd5779SVedang Patel minimum_time = transmit_end_time; 3774cfd5779SVedang Patel 3784cfd5779SVedang Patel /* Update the txtime of current entry to the next time it's 3794cfd5779SVedang Patel * interval starts. 3804cfd5779SVedang Patel */ 3814cfd5779SVedang Patel if (ktime_after(transmit_end_time, interval_end)) 3824cfd5779SVedang Patel entry->next_txtime = ktime_add(interval_start, sched->cycle_time); 3834cfd5779SVedang Patel } while (sched_changed || ktime_after(transmit_end_time, interval_end)); 3844cfd5779SVedang Patel 3854cfd5779SVedang Patel entry->next_txtime = transmit_end_time; 3864cfd5779SVedang Patel 3874cfd5779SVedang Patel done: 3884cfd5779SVedang Patel rcu_read_unlock(); 3894cfd5779SVedang Patel return txtime; 3904cfd5779SVedang Patel } 3914cfd5779SVedang Patel 3925a781ccbSVinicius Costa Gomes static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch, 3935a781ccbSVinicius Costa Gomes struct sk_buff **to_free) 3945a781ccbSVinicius Costa Gomes { 3955a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 3965a781ccbSVinicius Costa Gomes struct Qdisc *child; 3975a781ccbSVinicius Costa Gomes int queue; 3985a781ccbSVinicius Costa Gomes 3995a781ccbSVinicius Costa Gomes queue = skb_get_queue_mapping(skb); 4005a781ccbSVinicius Costa Gomes 4015a781ccbSVinicius Costa Gomes child = q->qdiscs[queue]; 4025a781ccbSVinicius Costa Gomes if (unlikely(!child)) 4035a781ccbSVinicius Costa Gomes return qdisc_drop(skb, sch, to_free); 4045a781ccbSVinicius Costa Gomes 4054cfd5779SVedang Patel if (skb->sk && sock_flag(skb->sk, SOCK_TXTIME)) { 4064cfd5779SVedang Patel if (!is_valid_interval(skb, sch)) 4074cfd5779SVedang Patel return qdisc_drop(skb, sch, to_free); 4084cfd5779SVedang Patel } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 4094cfd5779SVedang Patel skb->tstamp = get_packet_txtime(skb, sch); 4104cfd5779SVedang Patel if (!skb->tstamp) 4114cfd5779SVedang Patel return qdisc_drop(skb, sch, to_free); 4124cfd5779SVedang Patel } 4134cfd5779SVedang Patel 4145a781ccbSVinicius Costa Gomes qdisc_qstats_backlog_inc(sch, skb); 4155a781ccbSVinicius Costa Gomes sch->q.qlen++; 4165a781ccbSVinicius Costa Gomes 4175a781ccbSVinicius Costa Gomes return qdisc_enqueue(skb, child, to_free); 4185a781ccbSVinicius Costa Gomes } 4195a781ccbSVinicius Costa Gomes 4205a781ccbSVinicius Costa Gomes static struct sk_buff *taprio_peek(struct Qdisc *sch) 4215a781ccbSVinicius Costa Gomes { 4225a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 4235a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 4245a781ccbSVinicius Costa Gomes struct sched_entry *entry; 4255a781ccbSVinicius Costa Gomes struct sk_buff *skb; 4265a781ccbSVinicius Costa Gomes u32 gate_mask; 4275a781ccbSVinicius Costa Gomes int i; 4285a781ccbSVinicius Costa Gomes 4295a781ccbSVinicius Costa Gomes rcu_read_lock(); 4305a781ccbSVinicius Costa Gomes entry = rcu_dereference(q->current_entry); 4312684d1b7SAndre Guedes gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN; 4325a781ccbSVinicius Costa Gomes rcu_read_unlock(); 4335a781ccbSVinicius Costa Gomes 4345a781ccbSVinicius Costa Gomes if (!gate_mask) 4355a781ccbSVinicius Costa Gomes return NULL; 4365a781ccbSVinicius Costa Gomes 4375a781ccbSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 4385a781ccbSVinicius Costa Gomes struct Qdisc *child = q->qdiscs[i]; 4395a781ccbSVinicius Costa Gomes int prio; 4405a781ccbSVinicius Costa Gomes u8 tc; 4415a781ccbSVinicius Costa Gomes 4425a781ccbSVinicius Costa Gomes if (unlikely(!child)) 4435a781ccbSVinicius Costa Gomes continue; 4445a781ccbSVinicius Costa Gomes 4455a781ccbSVinicius Costa Gomes skb = child->ops->peek(child); 4465a781ccbSVinicius Costa Gomes if (!skb) 4475a781ccbSVinicius Costa Gomes continue; 4485a781ccbSVinicius Costa Gomes 4494cfd5779SVedang Patel if (TXTIME_ASSIST_IS_ENABLED(q->flags)) 4504cfd5779SVedang Patel return skb; 4514cfd5779SVedang Patel 4525a781ccbSVinicius Costa Gomes prio = skb->priority; 4535a781ccbSVinicius Costa Gomes tc = netdev_get_prio_tc_map(dev, prio); 4545a781ccbSVinicius Costa Gomes 4555a781ccbSVinicius Costa Gomes if (!(gate_mask & BIT(tc))) 4562684d1b7SAndre Guedes continue; 4575a781ccbSVinicius Costa Gomes 4585a781ccbSVinicius Costa Gomes return skb; 4595a781ccbSVinicius Costa Gomes } 4605a781ccbSVinicius Costa Gomes 4615a781ccbSVinicius Costa Gomes return NULL; 4625a781ccbSVinicius Costa Gomes } 4635a781ccbSVinicius Costa Gomes 46423bddf69SJakub Kicinski static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry) 46523bddf69SJakub Kicinski { 46623bddf69SJakub Kicinski atomic_set(&entry->budget, 46723bddf69SJakub Kicinski div64_u64((u64)entry->interval * 1000, 46823bddf69SJakub Kicinski atomic64_read(&q->picos_per_byte))); 4695a781ccbSVinicius Costa Gomes } 4705a781ccbSVinicius Costa Gomes 4715a781ccbSVinicius Costa Gomes static struct sk_buff *taprio_dequeue(struct Qdisc *sch) 4725a781ccbSVinicius Costa Gomes { 4735a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 4745a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 4758c79f0eaSVinicius Costa Gomes struct sk_buff *skb = NULL; 4765a781ccbSVinicius Costa Gomes struct sched_entry *entry; 4775a781ccbSVinicius Costa Gomes u32 gate_mask; 4785a781ccbSVinicius Costa Gomes int i; 4795a781ccbSVinicius Costa Gomes 4807b9eba7bSLeandro Dorileo if (atomic64_read(&q->picos_per_byte) == -1) { 4817b9eba7bSLeandro Dorileo WARN_ONCE(1, "taprio: dequeue() called with unknown picos per byte."); 4827b9eba7bSLeandro Dorileo return NULL; 4837b9eba7bSLeandro Dorileo } 4847b9eba7bSLeandro Dorileo 4855a781ccbSVinicius Costa Gomes rcu_read_lock(); 4865a781ccbSVinicius Costa Gomes entry = rcu_dereference(q->current_entry); 4875a781ccbSVinicius Costa Gomes /* if there's no entry, it means that the schedule didn't 4885a781ccbSVinicius Costa Gomes * start yet, so force all gates to be open, this is in 4895a781ccbSVinicius Costa Gomes * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5 4905a781ccbSVinicius Costa Gomes * "AdminGateSates" 4915a781ccbSVinicius Costa Gomes */ 4925a781ccbSVinicius Costa Gomes gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN; 4935a781ccbSVinicius Costa Gomes 4945a781ccbSVinicius Costa Gomes if (!gate_mask) 4958c79f0eaSVinicius Costa Gomes goto done; 4965a781ccbSVinicius Costa Gomes 4975a781ccbSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 4985a781ccbSVinicius Costa Gomes struct Qdisc *child = q->qdiscs[i]; 4995a781ccbSVinicius Costa Gomes ktime_t guard; 5005a781ccbSVinicius Costa Gomes int prio; 5015a781ccbSVinicius Costa Gomes int len; 5025a781ccbSVinicius Costa Gomes u8 tc; 5035a781ccbSVinicius Costa Gomes 5045a781ccbSVinicius Costa Gomes if (unlikely(!child)) 5055a781ccbSVinicius Costa Gomes continue; 5065a781ccbSVinicius Costa Gomes 5074cfd5779SVedang Patel if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 5084cfd5779SVedang Patel skb = child->ops->dequeue(child); 5094cfd5779SVedang Patel if (!skb) 5104cfd5779SVedang Patel continue; 5114cfd5779SVedang Patel goto skb_found; 5124cfd5779SVedang Patel } 5134cfd5779SVedang Patel 5145a781ccbSVinicius Costa Gomes skb = child->ops->peek(child); 5155a781ccbSVinicius Costa Gomes if (!skb) 5165a781ccbSVinicius Costa Gomes continue; 5175a781ccbSVinicius Costa Gomes 5185a781ccbSVinicius Costa Gomes prio = skb->priority; 5195a781ccbSVinicius Costa Gomes tc = netdev_get_prio_tc_map(dev, prio); 5205a781ccbSVinicius Costa Gomes 5215a781ccbSVinicius Costa Gomes if (!(gate_mask & BIT(tc))) 5225a781ccbSVinicius Costa Gomes continue; 5235a781ccbSVinicius Costa Gomes 5245a781ccbSVinicius Costa Gomes len = qdisc_pkt_len(skb); 5257ede7b03SVedang Patel guard = ktime_add_ns(taprio_get_time(q), 5265a781ccbSVinicius Costa Gomes length_to_duration(q, len)); 5275a781ccbSVinicius Costa Gomes 5285a781ccbSVinicius Costa Gomes /* In the case that there's no gate entry, there's no 5295a781ccbSVinicius Costa Gomes * guard band ... 5305a781ccbSVinicius Costa Gomes */ 5315a781ccbSVinicius Costa Gomes if (gate_mask != TAPRIO_ALL_GATES_OPEN && 5325a781ccbSVinicius Costa Gomes ktime_after(guard, entry->close_time)) 5336e734c82SAndre Guedes continue; 5345a781ccbSVinicius Costa Gomes 5355a781ccbSVinicius Costa Gomes /* ... and no budget. */ 5365a781ccbSVinicius Costa Gomes if (gate_mask != TAPRIO_ALL_GATES_OPEN && 5375a781ccbSVinicius Costa Gomes atomic_sub_return(len, &entry->budget) < 0) 5386e734c82SAndre Guedes continue; 5395a781ccbSVinicius Costa Gomes 5405a781ccbSVinicius Costa Gomes skb = child->ops->dequeue(child); 5415a781ccbSVinicius Costa Gomes if (unlikely(!skb)) 5428c79f0eaSVinicius Costa Gomes goto done; 5435a781ccbSVinicius Costa Gomes 5444cfd5779SVedang Patel skb_found: 5455a781ccbSVinicius Costa Gomes qdisc_bstats_update(sch, skb); 5465a781ccbSVinicius Costa Gomes qdisc_qstats_backlog_dec(sch, skb); 5475a781ccbSVinicius Costa Gomes sch->q.qlen--; 5485a781ccbSVinicius Costa Gomes 5498c79f0eaSVinicius Costa Gomes goto done; 5505a781ccbSVinicius Costa Gomes } 5515a781ccbSVinicius Costa Gomes 5528c79f0eaSVinicius Costa Gomes done: 5538c79f0eaSVinicius Costa Gomes rcu_read_unlock(); 5548c79f0eaSVinicius Costa Gomes 5558c79f0eaSVinicius Costa Gomes return skb; 5565a781ccbSVinicius Costa Gomes } 5575a781ccbSVinicius Costa Gomes 5586ca6a665SVinicius Costa Gomes static bool should_restart_cycle(const struct sched_gate_list *oper, 5596ca6a665SVinicius Costa Gomes const struct sched_entry *entry) 5606ca6a665SVinicius Costa Gomes { 5616ca6a665SVinicius Costa Gomes if (list_is_last(&entry->list, &oper->entries)) 5626ca6a665SVinicius Costa Gomes return true; 5636ca6a665SVinicius Costa Gomes 5646ca6a665SVinicius Costa Gomes if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0) 5656ca6a665SVinicius Costa Gomes return true; 5666ca6a665SVinicius Costa Gomes 5676ca6a665SVinicius Costa Gomes return false; 5686ca6a665SVinicius Costa Gomes } 5696ca6a665SVinicius Costa Gomes 570a3d43c0dSVinicius Costa Gomes static bool should_change_schedules(const struct sched_gate_list *admin, 571a3d43c0dSVinicius Costa Gomes const struct sched_gate_list *oper, 572a3d43c0dSVinicius Costa Gomes ktime_t close_time) 573a3d43c0dSVinicius Costa Gomes { 574c25031e9SVinicius Costa Gomes ktime_t next_base_time, extension_time; 575a3d43c0dSVinicius Costa Gomes 576a3d43c0dSVinicius Costa Gomes if (!admin) 577a3d43c0dSVinicius Costa Gomes return false; 578a3d43c0dSVinicius Costa Gomes 579a3d43c0dSVinicius Costa Gomes next_base_time = sched_base_time(admin); 580a3d43c0dSVinicius Costa Gomes 581a3d43c0dSVinicius Costa Gomes /* This is the simple case, the close_time would fall after 582a3d43c0dSVinicius Costa Gomes * the next schedule base_time. 583a3d43c0dSVinicius Costa Gomes */ 584a3d43c0dSVinicius Costa Gomes if (ktime_compare(next_base_time, close_time) <= 0) 585a3d43c0dSVinicius Costa Gomes return true; 586a3d43c0dSVinicius Costa Gomes 587c25031e9SVinicius Costa Gomes /* This is the cycle_time_extension case, if the close_time 588c25031e9SVinicius Costa Gomes * plus the amount that can be extended would fall after the 589c25031e9SVinicius Costa Gomes * next schedule base_time, we can extend the current schedule 590c25031e9SVinicius Costa Gomes * for that amount. 591c25031e9SVinicius Costa Gomes */ 592c25031e9SVinicius Costa Gomes extension_time = ktime_add_ns(close_time, oper->cycle_time_extension); 593c25031e9SVinicius Costa Gomes 594c25031e9SVinicius Costa Gomes /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about 595c25031e9SVinicius Costa Gomes * how precisely the extension should be made. So after 596c25031e9SVinicius Costa Gomes * conformance testing, this logic may change. 597c25031e9SVinicius Costa Gomes */ 598c25031e9SVinicius Costa Gomes if (ktime_compare(next_base_time, extension_time) <= 0) 599c25031e9SVinicius Costa Gomes return true; 600c25031e9SVinicius Costa Gomes 601a3d43c0dSVinicius Costa Gomes return false; 602a3d43c0dSVinicius Costa Gomes } 603a3d43c0dSVinicius Costa Gomes 6045a781ccbSVinicius Costa Gomes static enum hrtimer_restart advance_sched(struct hrtimer *timer) 6055a781ccbSVinicius Costa Gomes { 6065a781ccbSVinicius Costa Gomes struct taprio_sched *q = container_of(timer, struct taprio_sched, 6075a781ccbSVinicius Costa Gomes advance_timer); 608a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin; 6095a781ccbSVinicius Costa Gomes struct sched_entry *entry, *next; 6105a781ccbSVinicius Costa Gomes struct Qdisc *sch = q->root; 6115a781ccbSVinicius Costa Gomes ktime_t close_time; 6125a781ccbSVinicius Costa Gomes 6135a781ccbSVinicius Costa Gomes spin_lock(&q->current_entry_lock); 6145a781ccbSVinicius Costa Gomes entry = rcu_dereference_protected(q->current_entry, 6155a781ccbSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 616a3d43c0dSVinicius Costa Gomes oper = rcu_dereference_protected(q->oper_sched, 617a3d43c0dSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 618a3d43c0dSVinicius Costa Gomes admin = rcu_dereference_protected(q->admin_sched, 619a3d43c0dSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 6205a781ccbSVinicius Costa Gomes 621a3d43c0dSVinicius Costa Gomes if (!oper) 622a3d43c0dSVinicius Costa Gomes switch_schedules(q, &admin, &oper); 623a3d43c0dSVinicius Costa Gomes 624a3d43c0dSVinicius Costa Gomes /* This can happen in two cases: 1. this is the very first run 625a3d43c0dSVinicius Costa Gomes * of this function (i.e. we weren't running any schedule 626a3d43c0dSVinicius Costa Gomes * previously); 2. The previous schedule just ended. The first 627a3d43c0dSVinicius Costa Gomes * entry of all schedules are pre-calculated during the 628a3d43c0dSVinicius Costa Gomes * schedule initialization. 6295a781ccbSVinicius Costa Gomes */ 630a3d43c0dSVinicius Costa Gomes if (unlikely(!entry || entry->close_time == oper->base_time)) { 631a3d43c0dSVinicius Costa Gomes next = list_first_entry(&oper->entries, struct sched_entry, 6325a781ccbSVinicius Costa Gomes list); 6335a781ccbSVinicius Costa Gomes close_time = next->close_time; 6345a781ccbSVinicius Costa Gomes goto first_run; 6355a781ccbSVinicius Costa Gomes } 6365a781ccbSVinicius Costa Gomes 6376ca6a665SVinicius Costa Gomes if (should_restart_cycle(oper, entry)) { 638a3d43c0dSVinicius Costa Gomes next = list_first_entry(&oper->entries, struct sched_entry, 6395a781ccbSVinicius Costa Gomes list); 6406ca6a665SVinicius Costa Gomes oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time, 6416ca6a665SVinicius Costa Gomes oper->cycle_time); 6426ca6a665SVinicius Costa Gomes } else { 6435a781ccbSVinicius Costa Gomes next = list_next_entry(entry, list); 6446ca6a665SVinicius Costa Gomes } 6455a781ccbSVinicius Costa Gomes 6465a781ccbSVinicius Costa Gomes close_time = ktime_add_ns(entry->close_time, next->interval); 6476ca6a665SVinicius Costa Gomes close_time = min_t(ktime_t, close_time, oper->cycle_close_time); 6485a781ccbSVinicius Costa Gomes 649a3d43c0dSVinicius Costa Gomes if (should_change_schedules(admin, oper, close_time)) { 650a3d43c0dSVinicius Costa Gomes /* Set things so the next time this runs, the new 651a3d43c0dSVinicius Costa Gomes * schedule runs. 652a3d43c0dSVinicius Costa Gomes */ 653a3d43c0dSVinicius Costa Gomes close_time = sched_base_time(admin); 654a3d43c0dSVinicius Costa Gomes switch_schedules(q, &admin, &oper); 655a3d43c0dSVinicius Costa Gomes } 656a3d43c0dSVinicius Costa Gomes 6575a781ccbSVinicius Costa Gomes next->close_time = close_time; 65823bddf69SJakub Kicinski taprio_set_budget(q, next); 6595a781ccbSVinicius Costa Gomes 6605a781ccbSVinicius Costa Gomes first_run: 6615a781ccbSVinicius Costa Gomes rcu_assign_pointer(q->current_entry, next); 6625a781ccbSVinicius Costa Gomes spin_unlock(&q->current_entry_lock); 6635a781ccbSVinicius Costa Gomes 6645a781ccbSVinicius Costa Gomes hrtimer_set_expires(&q->advance_timer, close_time); 6655a781ccbSVinicius Costa Gomes 6665a781ccbSVinicius Costa Gomes rcu_read_lock(); 6675a781ccbSVinicius Costa Gomes __netif_schedule(sch); 6685a781ccbSVinicius Costa Gomes rcu_read_unlock(); 6695a781ccbSVinicius Costa Gomes 6705a781ccbSVinicius Costa Gomes return HRTIMER_RESTART; 6715a781ccbSVinicius Costa Gomes } 6725a781ccbSVinicius Costa Gomes 6735a781ccbSVinicius Costa Gomes static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { 6745a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 }, 6755a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 }, 6765a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 }, 6775a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 }, 6785a781ccbSVinicius Costa Gomes }; 6795a781ccbSVinicius Costa Gomes 6805a781ccbSVinicius Costa Gomes static const struct nla_policy entry_list_policy[TCA_TAPRIO_SCHED_MAX + 1] = { 6815a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY] = { .type = NLA_NESTED }, 6825a781ccbSVinicius Costa Gomes }; 6835a781ccbSVinicius Costa Gomes 6845a781ccbSVinicius Costa Gomes static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = { 6855a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_PRIOMAP] = { 6865a781ccbSVinicius Costa Gomes .len = sizeof(struct tc_mqprio_qopt) 6875a781ccbSVinicius Costa Gomes }, 6885a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED }, 6895a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 }, 6905a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED }, 6915a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 }, 6926ca6a665SVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 }, 693c25031e9SVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 }, 6945a781ccbSVinicius Costa Gomes }; 6955a781ccbSVinicius Costa Gomes 6965a781ccbSVinicius Costa Gomes static int fill_sched_entry(struct nlattr **tb, struct sched_entry *entry, 6975a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 6985a781ccbSVinicius Costa Gomes { 6995a781ccbSVinicius Costa Gomes u32 interval = 0; 7005a781ccbSVinicius Costa Gomes 7015a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD]) 7025a781ccbSVinicius Costa Gomes entry->command = nla_get_u8( 7035a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_CMD]); 7045a781ccbSVinicius Costa Gomes 7055a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]) 7065a781ccbSVinicius Costa Gomes entry->gate_mask = nla_get_u32( 7075a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]); 7085a781ccbSVinicius Costa Gomes 7095a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]) 7105a781ccbSVinicius Costa Gomes interval = nla_get_u32( 7115a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]); 7125a781ccbSVinicius Costa Gomes 7135a781ccbSVinicius Costa Gomes if (interval == 0) { 7145a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry"); 7155a781ccbSVinicius Costa Gomes return -EINVAL; 7165a781ccbSVinicius Costa Gomes } 7175a781ccbSVinicius Costa Gomes 7185a781ccbSVinicius Costa Gomes entry->interval = interval; 7195a781ccbSVinicius Costa Gomes 7205a781ccbSVinicius Costa Gomes return 0; 7215a781ccbSVinicius Costa Gomes } 7225a781ccbSVinicius Costa Gomes 7235a781ccbSVinicius Costa Gomes static int parse_sched_entry(struct nlattr *n, struct sched_entry *entry, 7245a781ccbSVinicius Costa Gomes int index, struct netlink_ext_ack *extack) 7255a781ccbSVinicius Costa Gomes { 7265a781ccbSVinicius Costa Gomes struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { }; 7275a781ccbSVinicius Costa Gomes int err; 7285a781ccbSVinicius Costa Gomes 7298cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n, 7305a781ccbSVinicius Costa Gomes entry_policy, NULL); 7315a781ccbSVinicius Costa Gomes if (err < 0) { 7325a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Could not parse nested entry"); 7335a781ccbSVinicius Costa Gomes return -EINVAL; 7345a781ccbSVinicius Costa Gomes } 7355a781ccbSVinicius Costa Gomes 7365a781ccbSVinicius Costa Gomes entry->index = index; 7375a781ccbSVinicius Costa Gomes 7385a781ccbSVinicius Costa Gomes return fill_sched_entry(tb, entry, extack); 7395a781ccbSVinicius Costa Gomes } 7405a781ccbSVinicius Costa Gomes 7415a781ccbSVinicius Costa Gomes static int parse_sched_list(struct nlattr *list, 742a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, 7435a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 7445a781ccbSVinicius Costa Gomes { 7455a781ccbSVinicius Costa Gomes struct nlattr *n; 7465a781ccbSVinicius Costa Gomes int err, rem; 7475a781ccbSVinicius Costa Gomes int i = 0; 7485a781ccbSVinicius Costa Gomes 7495a781ccbSVinicius Costa Gomes if (!list) 7505a781ccbSVinicius Costa Gomes return -EINVAL; 7515a781ccbSVinicius Costa Gomes 7525a781ccbSVinicius Costa Gomes nla_for_each_nested(n, list, rem) { 7535a781ccbSVinicius Costa Gomes struct sched_entry *entry; 7545a781ccbSVinicius Costa Gomes 7555a781ccbSVinicius Costa Gomes if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) { 7565a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'"); 7575a781ccbSVinicius Costa Gomes continue; 7585a781ccbSVinicius Costa Gomes } 7595a781ccbSVinicius Costa Gomes 7605a781ccbSVinicius Costa Gomes entry = kzalloc(sizeof(*entry), GFP_KERNEL); 7615a781ccbSVinicius Costa Gomes if (!entry) { 7625a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Not enough memory for entry"); 7635a781ccbSVinicius Costa Gomes return -ENOMEM; 7645a781ccbSVinicius Costa Gomes } 7655a781ccbSVinicius Costa Gomes 7665a781ccbSVinicius Costa Gomes err = parse_sched_entry(n, entry, i, extack); 7675a781ccbSVinicius Costa Gomes if (err < 0) { 7685a781ccbSVinicius Costa Gomes kfree(entry); 7695a781ccbSVinicius Costa Gomes return err; 7705a781ccbSVinicius Costa Gomes } 7715a781ccbSVinicius Costa Gomes 772a3d43c0dSVinicius Costa Gomes list_add_tail(&entry->list, &sched->entries); 7735a781ccbSVinicius Costa Gomes i++; 7745a781ccbSVinicius Costa Gomes } 7755a781ccbSVinicius Costa Gomes 776a3d43c0dSVinicius Costa Gomes sched->num_entries = i; 7775a781ccbSVinicius Costa Gomes 7785a781ccbSVinicius Costa Gomes return i; 7795a781ccbSVinicius Costa Gomes } 7805a781ccbSVinicius Costa Gomes 781a3d43c0dSVinicius Costa Gomes static int parse_taprio_schedule(struct nlattr **tb, 782a3d43c0dSVinicius Costa Gomes struct sched_gate_list *new, 7835a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 7845a781ccbSVinicius Costa Gomes { 7855a781ccbSVinicius Costa Gomes int err = 0; 7865a781ccbSVinicius Costa Gomes 787a3d43c0dSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) { 788a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Adding a single entry is not supported"); 789a3d43c0dSVinicius Costa Gomes return -ENOTSUPP; 790a3d43c0dSVinicius Costa Gomes } 7915a781ccbSVinicius Costa Gomes 7925a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]) 793a3d43c0dSVinicius Costa Gomes new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]); 7945a781ccbSVinicius Costa Gomes 795c25031e9SVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]) 796c25031e9SVinicius Costa Gomes new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]); 797c25031e9SVinicius Costa Gomes 7986ca6a665SVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]) 7996ca6a665SVinicius Costa Gomes new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]); 8006ca6a665SVinicius Costa Gomes 8015a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]) 8025a781ccbSVinicius Costa Gomes err = parse_sched_list( 803a3d43c0dSVinicius Costa Gomes tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST], new, extack); 804a3d43c0dSVinicius Costa Gomes if (err < 0) 8055a781ccbSVinicius Costa Gomes return err; 806a3d43c0dSVinicius Costa Gomes 807037be037SVedang Patel if (!new->cycle_time) { 808037be037SVedang Patel struct sched_entry *entry; 809037be037SVedang Patel ktime_t cycle = 0; 810037be037SVedang Patel 811037be037SVedang Patel list_for_each_entry(entry, &new->entries, list) 812037be037SVedang Patel cycle = ktime_add_ns(cycle, entry->interval); 813037be037SVedang Patel new->cycle_time = cycle; 814037be037SVedang Patel } 815037be037SVedang Patel 816a3d43c0dSVinicius Costa Gomes return 0; 8175a781ccbSVinicius Costa Gomes } 8185a781ccbSVinicius Costa Gomes 8195a781ccbSVinicius Costa Gomes static int taprio_parse_mqprio_opt(struct net_device *dev, 8205a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt *qopt, 8214cfd5779SVedang Patel struct netlink_ext_ack *extack, 8224cfd5779SVedang Patel u32 taprio_flags) 8235a781ccbSVinicius Costa Gomes { 8245a781ccbSVinicius Costa Gomes int i, j; 8255a781ccbSVinicius Costa Gomes 826a3d43c0dSVinicius Costa Gomes if (!qopt && !dev->num_tc) { 8275a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary"); 8285a781ccbSVinicius Costa Gomes return -EINVAL; 8295a781ccbSVinicius Costa Gomes } 8305a781ccbSVinicius Costa Gomes 831a3d43c0dSVinicius Costa Gomes /* If num_tc is already set, it means that the user already 832a3d43c0dSVinicius Costa Gomes * configured the mqprio part 833a3d43c0dSVinicius Costa Gomes */ 834a3d43c0dSVinicius Costa Gomes if (dev->num_tc) 835a3d43c0dSVinicius Costa Gomes return 0; 836a3d43c0dSVinicius Costa Gomes 8375a781ccbSVinicius Costa Gomes /* Verify num_tc is not out of max range */ 8385a781ccbSVinicius Costa Gomes if (qopt->num_tc > TC_MAX_QUEUE) { 8395a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range"); 8405a781ccbSVinicius Costa Gomes return -EINVAL; 8415a781ccbSVinicius Costa Gomes } 8425a781ccbSVinicius Costa Gomes 8435a781ccbSVinicius Costa Gomes /* taprio imposes that traffic classes map 1:n to tx queues */ 8445a781ccbSVinicius Costa Gomes if (qopt->num_tc > dev->num_tx_queues) { 8455a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues"); 8465a781ccbSVinicius Costa Gomes return -EINVAL; 8475a781ccbSVinicius Costa Gomes } 8485a781ccbSVinicius Costa Gomes 8495a781ccbSVinicius Costa Gomes /* Verify priority mapping uses valid tcs */ 8505a781ccbSVinicius Costa Gomes for (i = 0; i < TC_BITMASK + 1; i++) { 8515a781ccbSVinicius Costa Gomes if (qopt->prio_tc_map[i] >= qopt->num_tc) { 8525a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping"); 8535a781ccbSVinicius Costa Gomes return -EINVAL; 8545a781ccbSVinicius Costa Gomes } 8555a781ccbSVinicius Costa Gomes } 8565a781ccbSVinicius Costa Gomes 8575a781ccbSVinicius Costa Gomes for (i = 0; i < qopt->num_tc; i++) { 8585a781ccbSVinicius Costa Gomes unsigned int last = qopt->offset[i] + qopt->count[i]; 8595a781ccbSVinicius Costa Gomes 8605a781ccbSVinicius Costa Gomes /* Verify the queue count is in tx range being equal to the 8615a781ccbSVinicius Costa Gomes * real_num_tx_queues indicates the last queue is in use. 8625a781ccbSVinicius Costa Gomes */ 8635a781ccbSVinicius Costa Gomes if (qopt->offset[i] >= dev->num_tx_queues || 8645a781ccbSVinicius Costa Gomes !qopt->count[i] || 8655a781ccbSVinicius Costa Gomes last > dev->real_num_tx_queues) { 8665a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping"); 8675a781ccbSVinicius Costa Gomes return -EINVAL; 8685a781ccbSVinicius Costa Gomes } 8695a781ccbSVinicius Costa Gomes 8704cfd5779SVedang Patel if (TXTIME_ASSIST_IS_ENABLED(taprio_flags)) 8714cfd5779SVedang Patel continue; 8724cfd5779SVedang Patel 8735a781ccbSVinicius Costa Gomes /* Verify that the offset and counts do not overlap */ 8745a781ccbSVinicius Costa Gomes for (j = i + 1; j < qopt->num_tc; j++) { 8755a781ccbSVinicius Costa Gomes if (last > qopt->offset[j]) { 8765a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping"); 8775a781ccbSVinicius Costa Gomes return -EINVAL; 8785a781ccbSVinicius Costa Gomes } 8795a781ccbSVinicius Costa Gomes } 8805a781ccbSVinicius Costa Gomes } 8815a781ccbSVinicius Costa Gomes 8825a781ccbSVinicius Costa Gomes return 0; 8835a781ccbSVinicius Costa Gomes } 8845a781ccbSVinicius Costa Gomes 885a3d43c0dSVinicius Costa Gomes static int taprio_get_start_time(struct Qdisc *sch, 886a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, 887a3d43c0dSVinicius Costa Gomes ktime_t *start) 8885a781ccbSVinicius Costa Gomes { 8895a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 8905a781ccbSVinicius Costa Gomes ktime_t now, base, cycle; 8915a781ccbSVinicius Costa Gomes s64 n; 8925a781ccbSVinicius Costa Gomes 893a3d43c0dSVinicius Costa Gomes base = sched_base_time(sched); 8947ede7b03SVedang Patel now = taprio_get_time(q); 8958599099fSAndre Guedes 8968599099fSAndre Guedes if (ktime_after(base, now)) { 8978599099fSAndre Guedes *start = base; 8988599099fSAndre Guedes return 0; 8998599099fSAndre Guedes } 9005a781ccbSVinicius Costa Gomes 901037be037SVedang Patel cycle = sched->cycle_time; 9025a781ccbSVinicius Costa Gomes 9038599099fSAndre Guedes /* The qdisc is expected to have at least one sched_entry. Moreover, 9048599099fSAndre Guedes * any entry must have 'interval' > 0. Thus if the cycle time is zero, 9058599099fSAndre Guedes * something went really wrong. In that case, we should warn about this 9068599099fSAndre Guedes * inconsistent state and return error. 9078599099fSAndre Guedes */ 9088599099fSAndre Guedes if (WARN_ON(!cycle)) 9098599099fSAndre Guedes return -EFAULT; 9105a781ccbSVinicius Costa Gomes 9115a781ccbSVinicius Costa Gomes /* Schedule the start time for the beginning of the next 9125a781ccbSVinicius Costa Gomes * cycle. 9135a781ccbSVinicius Costa Gomes */ 9145a781ccbSVinicius Costa Gomes n = div64_s64(ktime_sub_ns(now, base), cycle); 9158599099fSAndre Guedes *start = ktime_add_ns(base, (n + 1) * cycle); 9168599099fSAndre Guedes return 0; 9175a781ccbSVinicius Costa Gomes } 9185a781ccbSVinicius Costa Gomes 919a3d43c0dSVinicius Costa Gomes static void setup_first_close_time(struct taprio_sched *q, 920a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, ktime_t base) 9215a781ccbSVinicius Costa Gomes { 9225a781ccbSVinicius Costa Gomes struct sched_entry *first; 9236ca6a665SVinicius Costa Gomes ktime_t cycle; 9245a781ccbSVinicius Costa Gomes 925a3d43c0dSVinicius Costa Gomes first = list_first_entry(&sched->entries, 926a3d43c0dSVinicius Costa Gomes struct sched_entry, list); 9275a781ccbSVinicius Costa Gomes 928037be037SVedang Patel cycle = sched->cycle_time; 9296ca6a665SVinicius Costa Gomes 9306ca6a665SVinicius Costa Gomes /* FIXME: find a better place to do this */ 9316ca6a665SVinicius Costa Gomes sched->cycle_close_time = ktime_add_ns(base, cycle); 9326ca6a665SVinicius Costa Gomes 933a3d43c0dSVinicius Costa Gomes first->close_time = ktime_add_ns(base, first->interval); 93423bddf69SJakub Kicinski taprio_set_budget(q, first); 9355a781ccbSVinicius Costa Gomes rcu_assign_pointer(q->current_entry, NULL); 936a3d43c0dSVinicius Costa Gomes } 9375a781ccbSVinicius Costa Gomes 938a3d43c0dSVinicius Costa Gomes static void taprio_start_sched(struct Qdisc *sch, 939a3d43c0dSVinicius Costa Gomes ktime_t start, struct sched_gate_list *new) 940a3d43c0dSVinicius Costa Gomes { 941a3d43c0dSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 942a3d43c0dSVinicius Costa Gomes ktime_t expires; 943a3d43c0dSVinicius Costa Gomes 944a3d43c0dSVinicius Costa Gomes expires = hrtimer_get_expires(&q->advance_timer); 945a3d43c0dSVinicius Costa Gomes if (expires == 0) 946a3d43c0dSVinicius Costa Gomes expires = KTIME_MAX; 947a3d43c0dSVinicius Costa Gomes 948a3d43c0dSVinicius Costa Gomes /* If the new schedule starts before the next expiration, we 949a3d43c0dSVinicius Costa Gomes * reprogram it to the earliest one, so we change the admin 950a3d43c0dSVinicius Costa Gomes * schedule to the operational one at the right time. 951a3d43c0dSVinicius Costa Gomes */ 952a3d43c0dSVinicius Costa Gomes start = min_t(ktime_t, start, expires); 9535a781ccbSVinicius Costa Gomes 9545a781ccbSVinicius Costa Gomes hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS); 9555a781ccbSVinicius Costa Gomes } 9565a781ccbSVinicius Costa Gomes 9577b9eba7bSLeandro Dorileo static void taprio_set_picos_per_byte(struct net_device *dev, 9587b9eba7bSLeandro Dorileo struct taprio_sched *q) 9597b9eba7bSLeandro Dorileo { 9607b9eba7bSLeandro Dorileo struct ethtool_link_ksettings ecmd; 9617b9eba7bSLeandro Dorileo int picos_per_byte = -1; 9627b9eba7bSLeandro Dorileo 9637b9eba7bSLeandro Dorileo if (!__ethtool_get_link_ksettings(dev, &ecmd) && 9647b9eba7bSLeandro Dorileo ecmd.base.speed != SPEED_UNKNOWN) 9657b9eba7bSLeandro Dorileo picos_per_byte = div64_s64(NSEC_PER_SEC * 1000LL * 8, 9667b9eba7bSLeandro Dorileo ecmd.base.speed * 1000 * 1000); 9677b9eba7bSLeandro Dorileo 9687b9eba7bSLeandro Dorileo atomic64_set(&q->picos_per_byte, picos_per_byte); 9697b9eba7bSLeandro Dorileo netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n", 9707b9eba7bSLeandro Dorileo dev->name, (long long)atomic64_read(&q->picos_per_byte), 9717b9eba7bSLeandro Dorileo ecmd.base.speed); 9727b9eba7bSLeandro Dorileo } 9737b9eba7bSLeandro Dorileo 9747b9eba7bSLeandro Dorileo static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event, 9757b9eba7bSLeandro Dorileo void *ptr) 9767b9eba7bSLeandro Dorileo { 9777b9eba7bSLeandro Dorileo struct net_device *dev = netdev_notifier_info_to_dev(ptr); 9787b9eba7bSLeandro Dorileo struct net_device *qdev; 9797b9eba7bSLeandro Dorileo struct taprio_sched *q; 9807b9eba7bSLeandro Dorileo bool found = false; 9817b9eba7bSLeandro Dorileo 9827b9eba7bSLeandro Dorileo ASSERT_RTNL(); 9837b9eba7bSLeandro Dorileo 9847b9eba7bSLeandro Dorileo if (event != NETDEV_UP && event != NETDEV_CHANGE) 9857b9eba7bSLeandro Dorileo return NOTIFY_DONE; 9867b9eba7bSLeandro Dorileo 9877b9eba7bSLeandro Dorileo spin_lock(&taprio_list_lock); 9887b9eba7bSLeandro Dorileo list_for_each_entry(q, &taprio_list, taprio_list) { 9897b9eba7bSLeandro Dorileo qdev = qdisc_dev(q->root); 9907b9eba7bSLeandro Dorileo if (qdev == dev) { 9917b9eba7bSLeandro Dorileo found = true; 9927b9eba7bSLeandro Dorileo break; 9937b9eba7bSLeandro Dorileo } 9947b9eba7bSLeandro Dorileo } 9957b9eba7bSLeandro Dorileo spin_unlock(&taprio_list_lock); 9967b9eba7bSLeandro Dorileo 9977b9eba7bSLeandro Dorileo if (found) 9987b9eba7bSLeandro Dorileo taprio_set_picos_per_byte(dev, q); 9997b9eba7bSLeandro Dorileo 10007b9eba7bSLeandro Dorileo return NOTIFY_DONE; 10017b9eba7bSLeandro Dorileo } 10027b9eba7bSLeandro Dorileo 10034cfd5779SVedang Patel static void setup_txtime(struct taprio_sched *q, 10044cfd5779SVedang Patel struct sched_gate_list *sched, ktime_t base) 10054cfd5779SVedang Patel { 10064cfd5779SVedang Patel struct sched_entry *entry; 10074cfd5779SVedang Patel u32 interval = 0; 10084cfd5779SVedang Patel 10094cfd5779SVedang Patel list_for_each_entry(entry, &sched->entries, list) { 10104cfd5779SVedang Patel entry->next_txtime = ktime_add_ns(base, interval); 10114cfd5779SVedang Patel interval += entry->interval; 10124cfd5779SVedang Patel } 10134cfd5779SVedang Patel } 10144cfd5779SVedang Patel 10155a781ccbSVinicius Costa Gomes static int taprio_change(struct Qdisc *sch, struct nlattr *opt, 10165a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 10175a781ccbSVinicius Costa Gomes { 10185a781ccbSVinicius Costa Gomes struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { }; 1019a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin, *new_admin; 10205a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 10215a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 10225a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt *mqprio = NULL; 10234cfd5779SVedang Patel u32 taprio_flags = 0; 1024a3d43c0dSVinicius Costa Gomes int i, err, clockid; 1025a3d43c0dSVinicius Costa Gomes unsigned long flags; 10265a781ccbSVinicius Costa Gomes ktime_t start; 10275a781ccbSVinicius Costa Gomes 10288cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt, 10295a781ccbSVinicius Costa Gomes taprio_policy, extack); 10305a781ccbSVinicius Costa Gomes if (err < 0) 10315a781ccbSVinicius Costa Gomes return err; 10325a781ccbSVinicius Costa Gomes 10335a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_PRIOMAP]) 10345a781ccbSVinicius Costa Gomes mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]); 10355a781ccbSVinicius Costa Gomes 10364cfd5779SVedang Patel if (tb[TCA_TAPRIO_ATTR_FLAGS]) { 10374cfd5779SVedang Patel taprio_flags = nla_get_u32(tb[TCA_TAPRIO_ATTR_FLAGS]); 10384cfd5779SVedang Patel 10394cfd5779SVedang Patel if (q->flags != 0 && q->flags != taprio_flags) { 10404cfd5779SVedang Patel NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported"); 10414cfd5779SVedang Patel return -EOPNOTSUPP; 10424cfd5779SVedang Patel } else if (!FLAGS_VALID(taprio_flags)) { 10434cfd5779SVedang Patel NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid"); 10444cfd5779SVedang Patel return -EINVAL; 10454cfd5779SVedang Patel } 10464cfd5779SVedang Patel 10474cfd5779SVedang Patel q->flags = taprio_flags; 10484cfd5779SVedang Patel } 10494cfd5779SVedang Patel 10504cfd5779SVedang Patel err = taprio_parse_mqprio_opt(dev, mqprio, extack, taprio_flags); 10515a781ccbSVinicius Costa Gomes if (err < 0) 10525a781ccbSVinicius Costa Gomes return err; 10535a781ccbSVinicius Costa Gomes 1054a3d43c0dSVinicius Costa Gomes new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL); 1055a3d43c0dSVinicius Costa Gomes if (!new_admin) { 1056a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule"); 1057a3d43c0dSVinicius Costa Gomes return -ENOMEM; 1058a3d43c0dSVinicius Costa Gomes } 1059a3d43c0dSVinicius Costa Gomes INIT_LIST_HEAD(&new_admin->entries); 10605a781ccbSVinicius Costa Gomes 1061a3d43c0dSVinicius Costa Gomes rcu_read_lock(); 1062a3d43c0dSVinicius Costa Gomes oper = rcu_dereference(q->oper_sched); 1063a3d43c0dSVinicius Costa Gomes admin = rcu_dereference(q->admin_sched); 1064a3d43c0dSVinicius Costa Gomes rcu_read_unlock(); 10655a781ccbSVinicius Costa Gomes 1066a3d43c0dSVinicius Costa Gomes if (mqprio && (oper || admin)) { 1067a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported"); 1068a3d43c0dSVinicius Costa Gomes err = -ENOTSUPP; 1069a3d43c0dSVinicius Costa Gomes goto free_sched; 10705a781ccbSVinicius Costa Gomes } 10715a781ccbSVinicius Costa Gomes 1072a3d43c0dSVinicius Costa Gomes err = parse_taprio_schedule(tb, new_admin, extack); 1073a3d43c0dSVinicius Costa Gomes if (err < 0) 1074a3d43c0dSVinicius Costa Gomes goto free_sched; 10755a781ccbSVinicius Costa Gomes 1076a3d43c0dSVinicius Costa Gomes if (new_admin->num_entries == 0) { 1077a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule"); 1078a3d43c0dSVinicius Costa Gomes err = -EINVAL; 1079a3d43c0dSVinicius Costa Gomes goto free_sched; 1080a3d43c0dSVinicius Costa Gomes } 10815a781ccbSVinicius Costa Gomes 1082a3d43c0dSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 1083a3d43c0dSVinicius Costa Gomes clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]); 10845a781ccbSVinicius Costa Gomes 1085a3d43c0dSVinicius Costa Gomes /* We only support static clockids and we don't allow 1086a3d43c0dSVinicius Costa Gomes * for it to be modified after the first init. 1087a3d43c0dSVinicius Costa Gomes */ 1088a3d43c0dSVinicius Costa Gomes if (clockid < 0 || 1089a3d43c0dSVinicius Costa Gomes (q->clockid != -1 && q->clockid != clockid)) { 1090a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Changing the 'clockid' of a running schedule is not supported"); 1091a3d43c0dSVinicius Costa Gomes err = -ENOTSUPP; 1092a3d43c0dSVinicius Costa Gomes goto free_sched; 1093a3d43c0dSVinicius Costa Gomes } 1094a3d43c0dSVinicius Costa Gomes 1095a3d43c0dSVinicius Costa Gomes q->clockid = clockid; 1096a3d43c0dSVinicius Costa Gomes } 1097a3d43c0dSVinicius Costa Gomes 1098a3d43c0dSVinicius Costa Gomes if (q->clockid == -1 && !tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 1099a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory"); 1100a3d43c0dSVinicius Costa Gomes err = -EINVAL; 1101a3d43c0dSVinicius Costa Gomes goto free_sched; 1102a3d43c0dSVinicius Costa Gomes } 1103a3d43c0dSVinicius Costa Gomes 1104a3d43c0dSVinicius Costa Gomes taprio_set_picos_per_byte(dev, q); 1105a3d43c0dSVinicius Costa Gomes 1106a3d43c0dSVinicius Costa Gomes /* Protects against enqueue()/dequeue() */ 1107a3d43c0dSVinicius Costa Gomes spin_lock_bh(qdisc_lock(sch)); 1108a3d43c0dSVinicius Costa Gomes 11094cfd5779SVedang Patel if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) { 11104cfd5779SVedang Patel if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) { 11114cfd5779SVedang Patel NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled"); 11124cfd5779SVedang Patel err = -EINVAL; 11134cfd5779SVedang Patel goto unlock; 11144cfd5779SVedang Patel } 11154cfd5779SVedang Patel 1116*a5b64700SVedang Patel q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]); 11174cfd5779SVedang Patel } 11184cfd5779SVedang Patel 11194cfd5779SVedang Patel if (!TXTIME_ASSIST_IS_ENABLED(taprio_flags) && 11204cfd5779SVedang Patel !hrtimer_active(&q->advance_timer)) { 1121a3d43c0dSVinicius Costa Gomes hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS); 1122a3d43c0dSVinicius Costa Gomes q->advance_timer.function = advance_sched; 11235a781ccbSVinicius Costa Gomes } 11245a781ccbSVinicius Costa Gomes 11255a781ccbSVinicius Costa Gomes if (mqprio) { 11265a781ccbSVinicius Costa Gomes netdev_set_num_tc(dev, mqprio->num_tc); 11275a781ccbSVinicius Costa Gomes for (i = 0; i < mqprio->num_tc; i++) 11285a781ccbSVinicius Costa Gomes netdev_set_tc_queue(dev, i, 11295a781ccbSVinicius Costa Gomes mqprio->count[i], 11305a781ccbSVinicius Costa Gomes mqprio->offset[i]); 11315a781ccbSVinicius Costa Gomes 11325a781ccbSVinicius Costa Gomes /* Always use supplied priority mappings */ 11335a781ccbSVinicius Costa Gomes for (i = 0; i < TC_BITMASK + 1; i++) 11345a781ccbSVinicius Costa Gomes netdev_set_prio_tc_map(dev, i, 11355a781ccbSVinicius Costa Gomes mqprio->prio_tc_map[i]); 11365a781ccbSVinicius Costa Gomes } 11375a781ccbSVinicius Costa Gomes 1138a3d43c0dSVinicius Costa Gomes switch (q->clockid) { 1139a3d43c0dSVinicius Costa Gomes case CLOCK_REALTIME: 11407ede7b03SVedang Patel q->tk_offset = TK_OFFS_REAL; 1141a3d43c0dSVinicius Costa Gomes break; 1142a3d43c0dSVinicius Costa Gomes case CLOCK_MONOTONIC: 11437ede7b03SVedang Patel q->tk_offset = TK_OFFS_MAX; 1144a3d43c0dSVinicius Costa Gomes break; 1145a3d43c0dSVinicius Costa Gomes case CLOCK_BOOTTIME: 11467ede7b03SVedang Patel q->tk_offset = TK_OFFS_BOOT; 1147a3d43c0dSVinicius Costa Gomes break; 1148a3d43c0dSVinicius Costa Gomes case CLOCK_TAI: 11497ede7b03SVedang Patel q->tk_offset = TK_OFFS_TAI; 1150a3d43c0dSVinicius Costa Gomes break; 1151a3d43c0dSVinicius Costa Gomes default: 1152a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid 'clockid'"); 1153a3d43c0dSVinicius Costa Gomes err = -EINVAL; 1154a3d43c0dSVinicius Costa Gomes goto unlock; 11558599099fSAndre Guedes } 11565a781ccbSVinicius Costa Gomes 1157a3d43c0dSVinicius Costa Gomes err = taprio_get_start_time(sch, new_admin, &start); 1158a3d43c0dSVinicius Costa Gomes if (err < 0) { 1159a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Internal error: failed get start time"); 1160a3d43c0dSVinicius Costa Gomes goto unlock; 1161a3d43c0dSVinicius Costa Gomes } 11625a781ccbSVinicius Costa Gomes 11634cfd5779SVedang Patel if (TXTIME_ASSIST_IS_ENABLED(taprio_flags)) { 11644cfd5779SVedang Patel setup_txtime(q, new_admin, start); 11654cfd5779SVedang Patel 11664cfd5779SVedang Patel if (!oper) { 11674cfd5779SVedang Patel rcu_assign_pointer(q->oper_sched, new_admin); 11684cfd5779SVedang Patel err = 0; 11694cfd5779SVedang Patel new_admin = NULL; 11704cfd5779SVedang Patel goto unlock; 11714cfd5779SVedang Patel } 11724cfd5779SVedang Patel 11734cfd5779SVedang Patel rcu_assign_pointer(q->admin_sched, new_admin); 11744cfd5779SVedang Patel if (admin) 11754cfd5779SVedang Patel call_rcu(&admin->rcu, taprio_free_sched_cb); 11764cfd5779SVedang Patel } else { 1177a3d43c0dSVinicius Costa Gomes setup_first_close_time(q, new_admin, start); 1178a3d43c0dSVinicius Costa Gomes 1179a3d43c0dSVinicius Costa Gomes /* Protects against advance_sched() */ 1180a3d43c0dSVinicius Costa Gomes spin_lock_irqsave(&q->current_entry_lock, flags); 1181a3d43c0dSVinicius Costa Gomes 1182a3d43c0dSVinicius Costa Gomes taprio_start_sched(sch, start, new_admin); 1183a3d43c0dSVinicius Costa Gomes 1184a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->admin_sched, new_admin); 1185a3d43c0dSVinicius Costa Gomes if (admin) 1186a3d43c0dSVinicius Costa Gomes call_rcu(&admin->rcu, taprio_free_sched_cb); 1187a3d43c0dSVinicius Costa Gomes 1188a3d43c0dSVinicius Costa Gomes spin_unlock_irqrestore(&q->current_entry_lock, flags); 11894cfd5779SVedang Patel } 1190a3d43c0dSVinicius Costa Gomes 11914cfd5779SVedang Patel new_admin = NULL; 1192a3d43c0dSVinicius Costa Gomes err = 0; 1193a3d43c0dSVinicius Costa Gomes 1194a3d43c0dSVinicius Costa Gomes unlock: 1195a3d43c0dSVinicius Costa Gomes spin_unlock_bh(qdisc_lock(sch)); 1196a3d43c0dSVinicius Costa Gomes 1197a3d43c0dSVinicius Costa Gomes free_sched: 1198a3d43c0dSVinicius Costa Gomes kfree(new_admin); 1199a3d43c0dSVinicius Costa Gomes 1200a3d43c0dSVinicius Costa Gomes return err; 12015a781ccbSVinicius Costa Gomes } 12025a781ccbSVinicius Costa Gomes 12035a781ccbSVinicius Costa Gomes static void taprio_destroy(struct Qdisc *sch) 12045a781ccbSVinicius Costa Gomes { 12055a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 12065a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 12075a781ccbSVinicius Costa Gomes unsigned int i; 12085a781ccbSVinicius Costa Gomes 12097b9eba7bSLeandro Dorileo spin_lock(&taprio_list_lock); 12107b9eba7bSLeandro Dorileo list_del(&q->taprio_list); 12117b9eba7bSLeandro Dorileo spin_unlock(&taprio_list_lock); 12127b9eba7bSLeandro Dorileo 12135a781ccbSVinicius Costa Gomes hrtimer_cancel(&q->advance_timer); 12145a781ccbSVinicius Costa Gomes 12155a781ccbSVinicius Costa Gomes if (q->qdiscs) { 12165a781ccbSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues && q->qdiscs[i]; i++) 12175a781ccbSVinicius Costa Gomes qdisc_put(q->qdiscs[i]); 12185a781ccbSVinicius Costa Gomes 12195a781ccbSVinicius Costa Gomes kfree(q->qdiscs); 12205a781ccbSVinicius Costa Gomes } 12215a781ccbSVinicius Costa Gomes q->qdiscs = NULL; 12225a781ccbSVinicius Costa Gomes 12235a781ccbSVinicius Costa Gomes netdev_set_num_tc(dev, 0); 12245a781ccbSVinicius Costa Gomes 1225a3d43c0dSVinicius Costa Gomes if (q->oper_sched) 1226a3d43c0dSVinicius Costa Gomes call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb); 1227a3d43c0dSVinicius Costa Gomes 1228a3d43c0dSVinicius Costa Gomes if (q->admin_sched) 1229a3d43c0dSVinicius Costa Gomes call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb); 12305a781ccbSVinicius Costa Gomes } 12315a781ccbSVinicius Costa Gomes 12325a781ccbSVinicius Costa Gomes static int taprio_init(struct Qdisc *sch, struct nlattr *opt, 12335a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 12345a781ccbSVinicius Costa Gomes { 12355a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 12365a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 1237a3d43c0dSVinicius Costa Gomes int i; 12385a781ccbSVinicius Costa Gomes 12395a781ccbSVinicius Costa Gomes spin_lock_init(&q->current_entry_lock); 12405a781ccbSVinicius Costa Gomes 12415a781ccbSVinicius Costa Gomes hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS); 1242a3d43c0dSVinicius Costa Gomes q->advance_timer.function = advance_sched; 12435a781ccbSVinicius Costa Gomes 12445a781ccbSVinicius Costa Gomes q->root = sch; 12455a781ccbSVinicius Costa Gomes 12465a781ccbSVinicius Costa Gomes /* We only support static clockids. Use an invalid value as default 12475a781ccbSVinicius Costa Gomes * and get the valid one on taprio_change(). 12485a781ccbSVinicius Costa Gomes */ 12495a781ccbSVinicius Costa Gomes q->clockid = -1; 12505a781ccbSVinicius Costa Gomes 12515a781ccbSVinicius Costa Gomes if (sch->parent != TC_H_ROOT) 12525a781ccbSVinicius Costa Gomes return -EOPNOTSUPP; 12535a781ccbSVinicius Costa Gomes 12545a781ccbSVinicius Costa Gomes if (!netif_is_multiqueue(dev)) 12555a781ccbSVinicius Costa Gomes return -EOPNOTSUPP; 12565a781ccbSVinicius Costa Gomes 12575a781ccbSVinicius Costa Gomes /* pre-allocate qdisc, attachment can't fail */ 12585a781ccbSVinicius Costa Gomes q->qdiscs = kcalloc(dev->num_tx_queues, 12595a781ccbSVinicius Costa Gomes sizeof(q->qdiscs[0]), 12605a781ccbSVinicius Costa Gomes GFP_KERNEL); 12615a781ccbSVinicius Costa Gomes 12625a781ccbSVinicius Costa Gomes if (!q->qdiscs) 12635a781ccbSVinicius Costa Gomes return -ENOMEM; 12645a781ccbSVinicius Costa Gomes 12655a781ccbSVinicius Costa Gomes if (!opt) 12665a781ccbSVinicius Costa Gomes return -EINVAL; 12675a781ccbSVinicius Costa Gomes 12687b9eba7bSLeandro Dorileo spin_lock(&taprio_list_lock); 12697b9eba7bSLeandro Dorileo list_add(&q->taprio_list, &taprio_list); 12707b9eba7bSLeandro Dorileo spin_unlock(&taprio_list_lock); 12717b9eba7bSLeandro Dorileo 1272a3d43c0dSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 1273a3d43c0dSVinicius Costa Gomes struct netdev_queue *dev_queue; 1274a3d43c0dSVinicius Costa Gomes struct Qdisc *qdisc; 1275a3d43c0dSVinicius Costa Gomes 1276a3d43c0dSVinicius Costa Gomes dev_queue = netdev_get_tx_queue(dev, i); 1277a3d43c0dSVinicius Costa Gomes qdisc = qdisc_create_dflt(dev_queue, 1278a3d43c0dSVinicius Costa Gomes &pfifo_qdisc_ops, 1279a3d43c0dSVinicius Costa Gomes TC_H_MAKE(TC_H_MAJ(sch->handle), 1280a3d43c0dSVinicius Costa Gomes TC_H_MIN(i + 1)), 1281a3d43c0dSVinicius Costa Gomes extack); 1282a3d43c0dSVinicius Costa Gomes if (!qdisc) 1283a3d43c0dSVinicius Costa Gomes return -ENOMEM; 1284a3d43c0dSVinicius Costa Gomes 1285a3d43c0dSVinicius Costa Gomes if (i < dev->real_num_tx_queues) 1286a3d43c0dSVinicius Costa Gomes qdisc_hash_add(qdisc, false); 1287a3d43c0dSVinicius Costa Gomes 1288a3d43c0dSVinicius Costa Gomes q->qdiscs[i] = qdisc; 1289a3d43c0dSVinicius Costa Gomes } 1290a3d43c0dSVinicius Costa Gomes 12915a781ccbSVinicius Costa Gomes return taprio_change(sch, opt, extack); 12925a781ccbSVinicius Costa Gomes } 12935a781ccbSVinicius Costa Gomes 12945a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_queue_get(struct Qdisc *sch, 12955a781ccbSVinicius Costa Gomes unsigned long cl) 12965a781ccbSVinicius Costa Gomes { 12975a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 12985a781ccbSVinicius Costa Gomes unsigned long ntx = cl - 1; 12995a781ccbSVinicius Costa Gomes 13005a781ccbSVinicius Costa Gomes if (ntx >= dev->num_tx_queues) 13015a781ccbSVinicius Costa Gomes return NULL; 13025a781ccbSVinicius Costa Gomes 13035a781ccbSVinicius Costa Gomes return netdev_get_tx_queue(dev, ntx); 13045a781ccbSVinicius Costa Gomes } 13055a781ccbSVinicius Costa Gomes 13065a781ccbSVinicius Costa Gomes static int taprio_graft(struct Qdisc *sch, unsigned long cl, 13075a781ccbSVinicius Costa Gomes struct Qdisc *new, struct Qdisc **old, 13085a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 13095a781ccbSVinicius Costa Gomes { 13105a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 13115a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 13125a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 13135a781ccbSVinicius Costa Gomes 13145a781ccbSVinicius Costa Gomes if (!dev_queue) 13155a781ccbSVinicius Costa Gomes return -EINVAL; 13165a781ccbSVinicius Costa Gomes 13175a781ccbSVinicius Costa Gomes if (dev->flags & IFF_UP) 13185a781ccbSVinicius Costa Gomes dev_deactivate(dev); 13195a781ccbSVinicius Costa Gomes 13205a781ccbSVinicius Costa Gomes *old = q->qdiscs[cl - 1]; 13215a781ccbSVinicius Costa Gomes q->qdiscs[cl - 1] = new; 13225a781ccbSVinicius Costa Gomes 13235a781ccbSVinicius Costa Gomes if (new) 13245a781ccbSVinicius Costa Gomes new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 13255a781ccbSVinicius Costa Gomes 13265a781ccbSVinicius Costa Gomes if (dev->flags & IFF_UP) 13275a781ccbSVinicius Costa Gomes dev_activate(dev); 13285a781ccbSVinicius Costa Gomes 13295a781ccbSVinicius Costa Gomes return 0; 13305a781ccbSVinicius Costa Gomes } 13315a781ccbSVinicius Costa Gomes 13325a781ccbSVinicius Costa Gomes static int dump_entry(struct sk_buff *msg, 13335a781ccbSVinicius Costa Gomes const struct sched_entry *entry) 13345a781ccbSVinicius Costa Gomes { 13355a781ccbSVinicius Costa Gomes struct nlattr *item; 13365a781ccbSVinicius Costa Gomes 1337ae0be8deSMichal Kubecek item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY); 13385a781ccbSVinicius Costa Gomes if (!item) 13395a781ccbSVinicius Costa Gomes return -ENOSPC; 13405a781ccbSVinicius Costa Gomes 13415a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index)) 13425a781ccbSVinicius Costa Gomes goto nla_put_failure; 13435a781ccbSVinicius Costa Gomes 13445a781ccbSVinicius Costa Gomes if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command)) 13455a781ccbSVinicius Costa Gomes goto nla_put_failure; 13465a781ccbSVinicius Costa Gomes 13475a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK, 13485a781ccbSVinicius Costa Gomes entry->gate_mask)) 13495a781ccbSVinicius Costa Gomes goto nla_put_failure; 13505a781ccbSVinicius Costa Gomes 13515a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL, 13525a781ccbSVinicius Costa Gomes entry->interval)) 13535a781ccbSVinicius Costa Gomes goto nla_put_failure; 13545a781ccbSVinicius Costa Gomes 13555a781ccbSVinicius Costa Gomes return nla_nest_end(msg, item); 13565a781ccbSVinicius Costa Gomes 13575a781ccbSVinicius Costa Gomes nla_put_failure: 13585a781ccbSVinicius Costa Gomes nla_nest_cancel(msg, item); 13595a781ccbSVinicius Costa Gomes return -1; 13605a781ccbSVinicius Costa Gomes } 13615a781ccbSVinicius Costa Gomes 1362a3d43c0dSVinicius Costa Gomes static int dump_schedule(struct sk_buff *msg, 1363a3d43c0dSVinicius Costa Gomes const struct sched_gate_list *root) 1364a3d43c0dSVinicius Costa Gomes { 1365a3d43c0dSVinicius Costa Gomes struct nlattr *entry_list; 1366a3d43c0dSVinicius Costa Gomes struct sched_entry *entry; 1367a3d43c0dSVinicius Costa Gomes 1368a3d43c0dSVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME, 1369a3d43c0dSVinicius Costa Gomes root->base_time, TCA_TAPRIO_PAD)) 1370a3d43c0dSVinicius Costa Gomes return -1; 1371a3d43c0dSVinicius Costa Gomes 13726ca6a665SVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME, 13736ca6a665SVinicius Costa Gomes root->cycle_time, TCA_TAPRIO_PAD)) 13746ca6a665SVinicius Costa Gomes return -1; 13756ca6a665SVinicius Costa Gomes 1376c25031e9SVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION, 1377c25031e9SVinicius Costa Gomes root->cycle_time_extension, TCA_TAPRIO_PAD)) 1378c25031e9SVinicius Costa Gomes return -1; 1379c25031e9SVinicius Costa Gomes 1380a3d43c0dSVinicius Costa Gomes entry_list = nla_nest_start_noflag(msg, 1381a3d43c0dSVinicius Costa Gomes TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST); 1382a3d43c0dSVinicius Costa Gomes if (!entry_list) 1383a3d43c0dSVinicius Costa Gomes goto error_nest; 1384a3d43c0dSVinicius Costa Gomes 1385a3d43c0dSVinicius Costa Gomes list_for_each_entry(entry, &root->entries, list) { 1386a3d43c0dSVinicius Costa Gomes if (dump_entry(msg, entry) < 0) 1387a3d43c0dSVinicius Costa Gomes goto error_nest; 1388a3d43c0dSVinicius Costa Gomes } 1389a3d43c0dSVinicius Costa Gomes 1390a3d43c0dSVinicius Costa Gomes nla_nest_end(msg, entry_list); 1391a3d43c0dSVinicius Costa Gomes return 0; 1392a3d43c0dSVinicius Costa Gomes 1393a3d43c0dSVinicius Costa Gomes error_nest: 1394a3d43c0dSVinicius Costa Gomes nla_nest_cancel(msg, entry_list); 1395a3d43c0dSVinicius Costa Gomes return -1; 1396a3d43c0dSVinicius Costa Gomes } 1397a3d43c0dSVinicius Costa Gomes 13985a781ccbSVinicius Costa Gomes static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb) 13995a781ccbSVinicius Costa Gomes { 14005a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 14015a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 1402a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin; 14035a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt opt = { 0 }; 1404a3d43c0dSVinicius Costa Gomes struct nlattr *nest, *sched_nest; 14055a781ccbSVinicius Costa Gomes unsigned int i; 14065a781ccbSVinicius Costa Gomes 1407a3d43c0dSVinicius Costa Gomes rcu_read_lock(); 1408a3d43c0dSVinicius Costa Gomes oper = rcu_dereference(q->oper_sched); 1409a3d43c0dSVinicius Costa Gomes admin = rcu_dereference(q->admin_sched); 1410a3d43c0dSVinicius Costa Gomes 14115a781ccbSVinicius Costa Gomes opt.num_tc = netdev_get_num_tc(dev); 14125a781ccbSVinicius Costa Gomes memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map)); 14135a781ccbSVinicius Costa Gomes 14145a781ccbSVinicius Costa Gomes for (i = 0; i < netdev_get_num_tc(dev); i++) { 14155a781ccbSVinicius Costa Gomes opt.count[i] = dev->tc_to_txq[i].count; 14165a781ccbSVinicius Costa Gomes opt.offset[i] = dev->tc_to_txq[i].offset; 14175a781ccbSVinicius Costa Gomes } 14185a781ccbSVinicius Costa Gomes 1419ae0be8deSMichal Kubecek nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 14205a781ccbSVinicius Costa Gomes if (!nest) 1421a3d43c0dSVinicius Costa Gomes goto start_error; 14225a781ccbSVinicius Costa Gomes 14235a781ccbSVinicius Costa Gomes if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt)) 14245a781ccbSVinicius Costa Gomes goto options_error; 14255a781ccbSVinicius Costa Gomes 14265a781ccbSVinicius Costa Gomes if (nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid)) 14275a781ccbSVinicius Costa Gomes goto options_error; 14285a781ccbSVinicius Costa Gomes 14294cfd5779SVedang Patel if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags)) 14304cfd5779SVedang Patel goto options_error; 14314cfd5779SVedang Patel 14324cfd5779SVedang Patel if (q->txtime_delay && 1433*a5b64700SVedang Patel nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay)) 14344cfd5779SVedang Patel goto options_error; 14354cfd5779SVedang Patel 1436a3d43c0dSVinicius Costa Gomes if (oper && dump_schedule(skb, oper)) 14375a781ccbSVinicius Costa Gomes goto options_error; 14385a781ccbSVinicius Costa Gomes 1439a3d43c0dSVinicius Costa Gomes if (!admin) 1440a3d43c0dSVinicius Costa Gomes goto done; 14415a781ccbSVinicius Costa Gomes 1442a3d43c0dSVinicius Costa Gomes sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED); 1443e4acf427SColin Ian King if (!sched_nest) 1444e4acf427SColin Ian King goto options_error; 1445a3d43c0dSVinicius Costa Gomes 1446a3d43c0dSVinicius Costa Gomes if (dump_schedule(skb, admin)) 1447a3d43c0dSVinicius Costa Gomes goto admin_error; 1448a3d43c0dSVinicius Costa Gomes 1449a3d43c0dSVinicius Costa Gomes nla_nest_end(skb, sched_nest); 1450a3d43c0dSVinicius Costa Gomes 1451a3d43c0dSVinicius Costa Gomes done: 1452a3d43c0dSVinicius Costa Gomes rcu_read_unlock(); 14535a781ccbSVinicius Costa Gomes 14545a781ccbSVinicius Costa Gomes return nla_nest_end(skb, nest); 14555a781ccbSVinicius Costa Gomes 1456a3d43c0dSVinicius Costa Gomes admin_error: 1457a3d43c0dSVinicius Costa Gomes nla_nest_cancel(skb, sched_nest); 1458a3d43c0dSVinicius Costa Gomes 14595a781ccbSVinicius Costa Gomes options_error: 14605a781ccbSVinicius Costa Gomes nla_nest_cancel(skb, nest); 1461a3d43c0dSVinicius Costa Gomes 1462a3d43c0dSVinicius Costa Gomes start_error: 1463a3d43c0dSVinicius Costa Gomes rcu_read_unlock(); 1464a3d43c0dSVinicius Costa Gomes return -ENOSPC; 14655a781ccbSVinicius Costa Gomes } 14665a781ccbSVinicius Costa Gomes 14675a781ccbSVinicius Costa Gomes static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl) 14685a781ccbSVinicius Costa Gomes { 14695a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 14705a781ccbSVinicius Costa Gomes 14715a781ccbSVinicius Costa Gomes if (!dev_queue) 14725a781ccbSVinicius Costa Gomes return NULL; 14735a781ccbSVinicius Costa Gomes 14745a781ccbSVinicius Costa Gomes return dev_queue->qdisc_sleeping; 14755a781ccbSVinicius Costa Gomes } 14765a781ccbSVinicius Costa Gomes 14775a781ccbSVinicius Costa Gomes static unsigned long taprio_find(struct Qdisc *sch, u32 classid) 14785a781ccbSVinicius Costa Gomes { 14795a781ccbSVinicius Costa Gomes unsigned int ntx = TC_H_MIN(classid); 14805a781ccbSVinicius Costa Gomes 14815a781ccbSVinicius Costa Gomes if (!taprio_queue_get(sch, ntx)) 14825a781ccbSVinicius Costa Gomes return 0; 14835a781ccbSVinicius Costa Gomes return ntx; 14845a781ccbSVinicius Costa Gomes } 14855a781ccbSVinicius Costa Gomes 14865a781ccbSVinicius Costa Gomes static int taprio_dump_class(struct Qdisc *sch, unsigned long cl, 14875a781ccbSVinicius Costa Gomes struct sk_buff *skb, struct tcmsg *tcm) 14885a781ccbSVinicius Costa Gomes { 14895a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 14905a781ccbSVinicius Costa Gomes 14915a781ccbSVinicius Costa Gomes tcm->tcm_parent = TC_H_ROOT; 14925a781ccbSVinicius Costa Gomes tcm->tcm_handle |= TC_H_MIN(cl); 14935a781ccbSVinicius Costa Gomes tcm->tcm_info = dev_queue->qdisc_sleeping->handle; 14945a781ccbSVinicius Costa Gomes 14955a781ccbSVinicius Costa Gomes return 0; 14965a781ccbSVinicius Costa Gomes } 14975a781ccbSVinicius Costa Gomes 14985a781ccbSVinicius Costa Gomes static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl, 14995a781ccbSVinicius Costa Gomes struct gnet_dump *d) 15005a781ccbSVinicius Costa Gomes __releases(d->lock) 15015a781ccbSVinicius Costa Gomes __acquires(d->lock) 15025a781ccbSVinicius Costa Gomes { 15035a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 15045a781ccbSVinicius Costa Gomes 15055a781ccbSVinicius Costa Gomes sch = dev_queue->qdisc_sleeping; 15065a781ccbSVinicius Costa Gomes if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 || 15075dd431b6SPaolo Abeni qdisc_qstats_copy(d, sch) < 0) 15085a781ccbSVinicius Costa Gomes return -1; 15095a781ccbSVinicius Costa Gomes return 0; 15105a781ccbSVinicius Costa Gomes } 15115a781ccbSVinicius Costa Gomes 15125a781ccbSVinicius Costa Gomes static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 15135a781ccbSVinicius Costa Gomes { 15145a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 15155a781ccbSVinicius Costa Gomes unsigned long ntx; 15165a781ccbSVinicius Costa Gomes 15175a781ccbSVinicius Costa Gomes if (arg->stop) 15185a781ccbSVinicius Costa Gomes return; 15195a781ccbSVinicius Costa Gomes 15205a781ccbSVinicius Costa Gomes arg->count = arg->skip; 15215a781ccbSVinicius Costa Gomes for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) { 15225a781ccbSVinicius Costa Gomes if (arg->fn(sch, ntx + 1, arg) < 0) { 15235a781ccbSVinicius Costa Gomes arg->stop = 1; 15245a781ccbSVinicius Costa Gomes break; 15255a781ccbSVinicius Costa Gomes } 15265a781ccbSVinicius Costa Gomes arg->count++; 15275a781ccbSVinicius Costa Gomes } 15285a781ccbSVinicius Costa Gomes } 15295a781ccbSVinicius Costa Gomes 15305a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_select_queue(struct Qdisc *sch, 15315a781ccbSVinicius Costa Gomes struct tcmsg *tcm) 15325a781ccbSVinicius Costa Gomes { 15335a781ccbSVinicius Costa Gomes return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); 15345a781ccbSVinicius Costa Gomes } 15355a781ccbSVinicius Costa Gomes 15365a781ccbSVinicius Costa Gomes static const struct Qdisc_class_ops taprio_class_ops = { 15375a781ccbSVinicius Costa Gomes .graft = taprio_graft, 15385a781ccbSVinicius Costa Gomes .leaf = taprio_leaf, 15395a781ccbSVinicius Costa Gomes .find = taprio_find, 15405a781ccbSVinicius Costa Gomes .walk = taprio_walk, 15415a781ccbSVinicius Costa Gomes .dump = taprio_dump_class, 15425a781ccbSVinicius Costa Gomes .dump_stats = taprio_dump_class_stats, 15435a781ccbSVinicius Costa Gomes .select_queue = taprio_select_queue, 15445a781ccbSVinicius Costa Gomes }; 15455a781ccbSVinicius Costa Gomes 15465a781ccbSVinicius Costa Gomes static struct Qdisc_ops taprio_qdisc_ops __read_mostly = { 15475a781ccbSVinicius Costa Gomes .cl_ops = &taprio_class_ops, 15485a781ccbSVinicius Costa Gomes .id = "taprio", 15495a781ccbSVinicius Costa Gomes .priv_size = sizeof(struct taprio_sched), 15505a781ccbSVinicius Costa Gomes .init = taprio_init, 1551a3d43c0dSVinicius Costa Gomes .change = taprio_change, 15525a781ccbSVinicius Costa Gomes .destroy = taprio_destroy, 15535a781ccbSVinicius Costa Gomes .peek = taprio_peek, 15545a781ccbSVinicius Costa Gomes .dequeue = taprio_dequeue, 15555a781ccbSVinicius Costa Gomes .enqueue = taprio_enqueue, 15565a781ccbSVinicius Costa Gomes .dump = taprio_dump, 15575a781ccbSVinicius Costa Gomes .owner = THIS_MODULE, 15585a781ccbSVinicius Costa Gomes }; 15595a781ccbSVinicius Costa Gomes 15607b9eba7bSLeandro Dorileo static struct notifier_block taprio_device_notifier = { 15617b9eba7bSLeandro Dorileo .notifier_call = taprio_dev_notifier, 15627b9eba7bSLeandro Dorileo }; 15637b9eba7bSLeandro Dorileo 15645a781ccbSVinicius Costa Gomes static int __init taprio_module_init(void) 15655a781ccbSVinicius Costa Gomes { 15667b9eba7bSLeandro Dorileo int err = register_netdevice_notifier(&taprio_device_notifier); 15677b9eba7bSLeandro Dorileo 15687b9eba7bSLeandro Dorileo if (err) 15697b9eba7bSLeandro Dorileo return err; 15707b9eba7bSLeandro Dorileo 15715a781ccbSVinicius Costa Gomes return register_qdisc(&taprio_qdisc_ops); 15725a781ccbSVinicius Costa Gomes } 15735a781ccbSVinicius Costa Gomes 15745a781ccbSVinicius Costa Gomes static void __exit taprio_module_exit(void) 15755a781ccbSVinicius Costa Gomes { 15765a781ccbSVinicius Costa Gomes unregister_qdisc(&taprio_qdisc_ops); 15777b9eba7bSLeandro Dorileo unregister_netdevice_notifier(&taprio_device_notifier); 15785a781ccbSVinicius Costa Gomes } 15795a781ccbSVinicius Costa Gomes 15805a781ccbSVinicius Costa Gomes module_init(taprio_module_init); 15815a781ccbSVinicius Costa Gomes module_exit(taprio_module_exit); 15825a781ccbSVinicius Costa Gomes MODULE_LICENSE("GPL"); 1583