18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __NET_PKT_SCHED_H
38c2ecf20Sopenharmony_ci#define __NET_PKT_SCHED_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
68c2ecf20Sopenharmony_ci#include <linux/ktime.h>
78c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
88c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
98c2ecf20Sopenharmony_ci#include <net/sch_generic.h>
108c2ecf20Sopenharmony_ci#include <net/net_namespace.h>
118c2ecf20Sopenharmony_ci#include <uapi/linux/pkt_sched.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define DEFAULT_TX_QUEUE_LEN	1000
148c2ecf20Sopenharmony_ci#define STAB_SIZE_LOG_MAX	30
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistruct qdisc_walker {
178c2ecf20Sopenharmony_ci	int	stop;
188c2ecf20Sopenharmony_ci	int	skip;
198c2ecf20Sopenharmony_ci	int	count;
208c2ecf20Sopenharmony_ci	int	(*fn)(struct Qdisc *, unsigned long cl, struct qdisc_walker *);
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic inline void *qdisc_priv(struct Qdisc *q)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	return &q->privdata;
268c2ecf20Sopenharmony_ci}
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci   Timer resolution MUST BE < 10% of min_schedulable_packet_size/bandwidth
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci   Normal IP packet size ~ 512byte, hence:
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci   0.5Kbyte/1Mbyte/sec = 0.5msec, so that we need 50usec timer for
348c2ecf20Sopenharmony_ci   10Mbit ethernet.
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci   10msec resolution -> <50Kbit/sec.
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci   The result: [34]86 is not good choice for QoS router :-(
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci   The things are not so bad, because we may use artificial
418c2ecf20Sopenharmony_ci   clock evaluated by integration of network data flow
428c2ecf20Sopenharmony_ci   in the most critical places.
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_citypedef u64	psched_time_t;
468c2ecf20Sopenharmony_citypedef long	psched_tdiff_t;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* Avoid doing 64 bit divide */
498c2ecf20Sopenharmony_ci#define PSCHED_SHIFT			6
508c2ecf20Sopenharmony_ci#define PSCHED_TICKS2NS(x)		((s64)(x) << PSCHED_SHIFT)
518c2ecf20Sopenharmony_ci#define PSCHED_NS2TICKS(x)		((x) >> PSCHED_SHIFT)
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define PSCHED_TICKS_PER_SEC		PSCHED_NS2TICKS(NSEC_PER_SEC)
548c2ecf20Sopenharmony_ci#define PSCHED_PASTPERFECT		0
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic inline psched_time_t psched_get_time(void)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	return PSCHED_NS2TICKS(ktime_get_ns());
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic inline psched_tdiff_t
628c2ecf20Sopenharmony_cipsched_tdiff_bounded(psched_time_t tv1, psched_time_t tv2, psched_time_t bound)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	return min(tv1 - tv2, bound);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistruct qdisc_watchdog {
688c2ecf20Sopenharmony_ci	u64		last_expires;
698c2ecf20Sopenharmony_ci	struct hrtimer	timer;
708c2ecf20Sopenharmony_ci	struct Qdisc	*qdisc;
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_civoid qdisc_watchdog_init_clockid(struct qdisc_watchdog *wd, struct Qdisc *qdisc,
748c2ecf20Sopenharmony_ci				 clockid_t clockid);
758c2ecf20Sopenharmony_civoid qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_civoid qdisc_watchdog_schedule_range_ns(struct qdisc_watchdog *wd, u64 expires,
788c2ecf20Sopenharmony_ci				      u64 delta_ns);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic inline void qdisc_watchdog_schedule_ns(struct qdisc_watchdog *wd,
818c2ecf20Sopenharmony_ci					      u64 expires)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	return qdisc_watchdog_schedule_range_ns(wd, expires, 0ULL);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic inline void qdisc_watchdog_schedule(struct qdisc_watchdog *wd,
878c2ecf20Sopenharmony_ci					   psched_time_t expires)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	qdisc_watchdog_schedule_ns(wd, PSCHED_TICKS2NS(expires));
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_civoid qdisc_watchdog_cancel(struct qdisc_watchdog *wd);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciextern struct Qdisc_ops pfifo_qdisc_ops;
958c2ecf20Sopenharmony_ciextern struct Qdisc_ops bfifo_qdisc_ops;
968c2ecf20Sopenharmony_ciextern struct Qdisc_ops pfifo_head_drop_qdisc_ops;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciint fifo_set_limit(struct Qdisc *q, unsigned int limit);
998c2ecf20Sopenharmony_cistruct Qdisc *fifo_create_dflt(struct Qdisc *sch, struct Qdisc_ops *ops,
1008c2ecf20Sopenharmony_ci			       unsigned int limit,
1018c2ecf20Sopenharmony_ci			       struct netlink_ext_ack *extack);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciint register_qdisc(struct Qdisc_ops *qops);
1048c2ecf20Sopenharmony_ciint unregister_qdisc(struct Qdisc_ops *qops);
1058c2ecf20Sopenharmony_civoid qdisc_get_default(char *id, size_t len);
1068c2ecf20Sopenharmony_ciint qdisc_set_default(const char *id);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_civoid qdisc_hash_add(struct Qdisc *q, bool invisible);
1098c2ecf20Sopenharmony_civoid qdisc_hash_del(struct Qdisc *q);
1108c2ecf20Sopenharmony_cistruct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle);
1118c2ecf20Sopenharmony_cistruct Qdisc *qdisc_lookup_rcu(struct net_device *dev, u32 handle);
1128c2ecf20Sopenharmony_cistruct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r,
1138c2ecf20Sopenharmony_ci					struct nlattr *tab,
1148c2ecf20Sopenharmony_ci					struct netlink_ext_ack *extack);
1158c2ecf20Sopenharmony_civoid qdisc_put_rtab(struct qdisc_rate_table *tab);
1168c2ecf20Sopenharmony_civoid qdisc_put_stab(struct qdisc_size_table *tab);
1178c2ecf20Sopenharmony_civoid qdisc_warn_nonwc(const char *txt, struct Qdisc *qdisc);
1188c2ecf20Sopenharmony_cibool sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
1198c2ecf20Sopenharmony_ci		     struct net_device *dev, struct netdev_queue *txq,
1208c2ecf20Sopenharmony_ci		     spinlock_t *root_lock, bool validate);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_civoid __qdisc_run(struct Qdisc *q);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic inline void qdisc_run(struct Qdisc *q)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	if (qdisc_run_begin(q)) {
1278c2ecf20Sopenharmony_ci		__qdisc_run(q);
1288c2ecf20Sopenharmony_ci		qdisc_run_end(q);
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciextern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/* Calculate maximal size of packet seen by hard_start_xmit
1358c2ecf20Sopenharmony_ci   routine of this device.
1368c2ecf20Sopenharmony_ci */
1378c2ecf20Sopenharmony_cistatic inline unsigned int psched_mtu(const struct net_device *dev)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	return READ_ONCE(dev->mtu) + dev->hard_header_len;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic inline struct net *qdisc_net(struct Qdisc *q)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	return dev_net(q->dev_queue->dev);
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistruct tc_cbs_qopt_offload {
1488c2ecf20Sopenharmony_ci	u8 enable;
1498c2ecf20Sopenharmony_ci	s32 queue;
1508c2ecf20Sopenharmony_ci	s32 hicredit;
1518c2ecf20Sopenharmony_ci	s32 locredit;
1528c2ecf20Sopenharmony_ci	s32 idleslope;
1538c2ecf20Sopenharmony_ci	s32 sendslope;
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistruct tc_etf_qopt_offload {
1578c2ecf20Sopenharmony_ci	u8 enable;
1588c2ecf20Sopenharmony_ci	s32 queue;
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistruct tc_taprio_sched_entry {
1628c2ecf20Sopenharmony_ci	u8 command; /* TC_TAPRIO_CMD_* */
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* The gate_mask in the offloading side refers to traffic classes */
1658c2ecf20Sopenharmony_ci	u32 gate_mask;
1668c2ecf20Sopenharmony_ci	u32 interval;
1678c2ecf20Sopenharmony_ci};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistruct tc_taprio_qopt_offload {
1708c2ecf20Sopenharmony_ci	u8 enable;
1718c2ecf20Sopenharmony_ci	ktime_t base_time;
1728c2ecf20Sopenharmony_ci	u64 cycle_time;
1738c2ecf20Sopenharmony_ci	u64 cycle_time_extension;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	size_t num_entries;
1768c2ecf20Sopenharmony_ci	struct tc_taprio_sched_entry entries[];
1778c2ecf20Sopenharmony_ci};
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/* Reference counting */
1808c2ecf20Sopenharmony_cistruct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1818c2ecf20Sopenharmony_ci						  *offload);
1828c2ecf20Sopenharmony_civoid taprio_offload_free(struct tc_taprio_qopt_offload *offload);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/* Ensure skb_mstamp_ns, which might have been populated with the txtime, is
1858c2ecf20Sopenharmony_ci * not mistaken for a software timestamp, because this will otherwise prevent
1868c2ecf20Sopenharmony_ci * the dispatch of hardware timestamps to the socket.
1878c2ecf20Sopenharmony_ci */
1888c2ecf20Sopenharmony_cistatic inline void skb_txtime_consumed(struct sk_buff *skb)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	skb->tstamp = ktime_set(0, 0);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci#endif
194