xref: /linux/include/net/tc_act/tc_gate.h (revision 57885276cc16a2e2b76282c808a4e84cbecb3aae)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Copyright 2020 NXP */
3 
4 #ifndef __NET_TC_GATE_H
5 #define __NET_TC_GATE_H
6 
7 #include <net/act_api.h>
8 #include <linux/tc_act/tc_gate.h>
9 
10 struct action_gate_entry {
11 	u8			gate_state;
12 	u32			interval;
13 	s32			ipv;
14 	s32			maxoctets;
15 };
16 
17 struct tcfg_gate_entry {
18 	int			index;
19 	u8			gate_state;
20 	u32			interval;
21 	s32			ipv;
22 	s32			maxoctets;
23 	struct list_head	list;
24 };
25 
26 struct tcf_gate_params {
27 	s32			tcfg_priority;
28 	u64			tcfg_basetime;
29 	u64			tcfg_cycletime;
30 	u64			tcfg_cycletime_ext;
31 	u32			tcfg_flags;
32 	s32			tcfg_clockid;
33 	size_t			num_entries;
34 	struct list_head	entries;
35 	struct rcu_head		rcu;
36 };
37 
38 #define GATE_ACT_GATE_OPEN	BIT(0)
39 #define GATE_ACT_PENDING	BIT(1)
40 
41 struct tcf_gate {
42 	struct tc_action	common;
43 	struct tcf_gate_params __rcu *param;
44 	u8			current_gate_status;
45 	ktime_t			current_close_time;
46 	u32			current_entry_octets;
47 	s32			current_max_octets;
48 	struct tcfg_gate_entry	*next_entry;
49 	struct hrtimer		hitimer;
50 	enum tk_offsets		tk_offset;
51 };
52 
53 #define to_gate(a) ((struct tcf_gate *)a)
54 
55 static inline struct tcf_gate_params *tcf_gate_params_locked(const struct tc_action *a)
56 {
57 	struct tcf_gate *gact = to_gate(a);
58 
59 	return rcu_dereference_protected(gact->param,
60 					 lockdep_is_held(&gact->tcf_lock));
61 }
62 
63 static inline s32 tcf_gate_prio(const struct tc_action *a)
64 {
65 	struct tcf_gate_params *p;
66 	s32 tcfg_prio;
67 
68 	p = tcf_gate_params_locked(a);
69 	tcfg_prio = p->tcfg_priority;
70 
71 	return tcfg_prio;
72 }
73 
74 static inline u64 tcf_gate_basetime(const struct tc_action *a)
75 {
76 	struct tcf_gate_params *p;
77 	u64 tcfg_basetime;
78 
79 	p = tcf_gate_params_locked(a);
80 	tcfg_basetime = p->tcfg_basetime;
81 
82 	return tcfg_basetime;
83 }
84 
85 static inline u64 tcf_gate_cycletime(const struct tc_action *a)
86 {
87 	struct tcf_gate_params *p;
88 	u64 tcfg_cycletime;
89 
90 	p = tcf_gate_params_locked(a);
91 	tcfg_cycletime = p->tcfg_cycletime;
92 
93 	return tcfg_cycletime;
94 }
95 
96 static inline u64 tcf_gate_cycletimeext(const struct tc_action *a)
97 {
98 	struct tcf_gate_params *p;
99 	u64 tcfg_cycletimeext;
100 
101 	p = tcf_gate_params_locked(a);
102 	tcfg_cycletimeext = p->tcfg_cycletime_ext;
103 
104 	return tcfg_cycletimeext;
105 }
106 
107 static inline u32 tcf_gate_num_entries(const struct tc_action *a)
108 {
109 	struct tcf_gate_params *p;
110 	u32 num_entries;
111 
112 	p = tcf_gate_params_locked(a);
113 	num_entries = p->num_entries;
114 
115 	return num_entries;
116 }
117 
118 static inline struct action_gate_entry
119 			*tcf_gate_get_list(const struct tc_action *a)
120 {
121 	struct action_gate_entry *oe;
122 	struct tcf_gate_params *p;
123 	struct tcfg_gate_entry *entry;
124 	u32 num_entries;
125 	int i = 0;
126 
127 	p = tcf_gate_params_locked(a);
128 	num_entries = p->num_entries;
129 
130 	list_for_each_entry(entry, &p->entries, list)
131 		i++;
132 
133 	if (i != num_entries)
134 		return NULL;
135 
136 	oe = kzalloc_objs(*oe, num_entries, GFP_ATOMIC);
137 	if (!oe)
138 		return NULL;
139 
140 	i = 0;
141 	list_for_each_entry(entry, &p->entries, list) {
142 		oe[i].gate_state = entry->gate_state;
143 		oe[i].interval = entry->interval;
144 		oe[i].ipv = entry->ipv;
145 		oe[i].maxoctets = entry->maxoctets;
146 		i++;
147 	}
148 
149 	return oe;
150 }
151 #endif
152