1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2019 Mellanox Technologies. */
7 #include <linux/module.h>
10 * Number of events between DIM iterations.
11 * Causes a moderation of the algorithm run.
13 #define DIM_NEVENTS 64
16 * Is a difference between values justifies taking an action.
17 * We consider 10% difference as significant.
19 #define IS_SIGNIFICANT_DIFF(val, ref) \
20 (((100UL * abs((val) - (ref))) / (ref)) > 10)
23 * Calculate the gap between two values.
24 * Take wrap-around and variable size into consideration.
26 #define BIT_GAP(bits, end, start) ((((end) - (start)) + BIT_ULL(bits)) \
27 & (BIT_ULL(bits) - 1))
30 * Structure for CQ moderation values.
31 * Used for communications between DIM and its consumer.
33 * @usec: CQ timer suggestion (by DIM)
34 * @pkts: CQ packet counter suggestion (by DIM)
35 * @cq_period_mode: CQ priod count mode (from CQE/EQE)
44 * Structure for DIM sample data.
45 * Used for communications between DIM and its consumer.
47 * @time: Sample timestamp
48 * @pkt_ctr: Number of packets
49 * @byte_ctr: Number of bytes
50 * @event_ctr: Number of events
60 * Structure for DIM stats.
61 * Used for holding current measured rates.
63 * @ppms: Packets per msec
64 * @bpms: Bytes per msec
65 * @epms: Events per msec
74 * Main structure for dynamic interrupt moderation (DIM).
75 * Used for holding all information about a specific DIM instance.
77 * @state: Algorithm state (see below)
78 * @prev_stats: Measured rates from previous iteration (for comparison)
79 * @start_sample: Sampled data at start of current iteration
80 * @work: Work to perform on action required
81 * @profile_ix: Current moderation profile
82 * @mode: CQ period count mode
83 * @tune_state: Algorithm tuning state (see below)
84 * @steps_right: Number of steps taken towards higher moderation
85 * @steps_left: Number of steps taken towards lower moderation
86 * @tired: Parking depth counter
90 struct dim_stats prev_stats;
91 struct dim_sample start_sample;
92 struct work_struct work;
102 * enum dim_cq_period_mode
104 * These are the modes for CQ period count.
106 * @DIM_CQ_PERIOD_MODE_START_FROM_EQE: Start counting from EQE
107 * @DIM_CQ_PERIOD_MODE_START_FROM_CQE: Start counting from CQE (implies timer reset)
108 * @DIM_CQ_PERIOD_NUM_MODES: Number of modes
111 DIM_CQ_PERIOD_MODE_START_FROM_EQE = 0x0,
112 DIM_CQ_PERIOD_MODE_START_FROM_CQE = 0x1,
113 DIM_CQ_PERIOD_NUM_MODES
119 * These are the DIM algorithm states.
120 * These will determine if the algorithm is in a valid state to start an iteration.
122 * @DIM_START_MEASURE: This is the first iteration (also after applying a new profile)
123 * @DIM_MEASURE_IN_PROGRESS: Algorithm is already in progress - check if
124 * need to perform an action
125 * @DIM_APPLY_NEW_PROFILE: DIM consumer is currently applying a profile - no need to measure
129 DIM_MEASURE_IN_PROGRESS,
130 DIM_APPLY_NEW_PROFILE,
134 * enum dim_tune_state
136 * These are the DIM algorithm tune states.
137 * These will determine which action the algorithm should perform.
139 * @DIM_PARKING_ON_TOP: Algorithm found a local top point - exit on significant difference
140 * @DIM_PARKING_TIRED: Algorithm found a deep top point - don't exit if tired > 0
141 * @DIM_GOING_RIGHT: Algorithm is currently trying higher moderation levels
142 * @DIM_GOING_LEFT: Algorithm is currently trying lower moderation levels
152 * enum dim_stats_state
154 * These are the DIM algorithm statistics states.
155 * These will determine the verdict of current iteration.
157 * @DIM_STATS_WORSE: Current iteration shows worse performance than before
158 * @DIM_STATS_WORSE: Current iteration shows same performance than before
159 * @DIM_STATS_WORSE: Current iteration shows better performance than before
168 * enum dim_step_result
170 * These are the DIM algorithm step results.
171 * These describe the result of a step.
173 * @DIM_STEPPED: Performed a regular step
174 * @DIM_TOO_TIRED: Same kind of step was done multiple times - should go to
176 * @DIM_ON_EDGE: Stepped to the most left/right profile
185 * dim_on_top - check if current state is a good place to stop (top location)
188 * Check if current profile is a good place to park at.
189 * This will result in reducing the DIM checks frequency as we assume we
190 * shouldn't probably change profiles, unless traffic pattern wasn't changed.
192 bool dim_on_top(struct dim *dim);
195 * dim_turn - change profile alterning direction
198 * Go left if we were going right and vice-versa.
199 * Do nothing if currently parking.
201 void dim_turn(struct dim *dim);
204 * dim_park_on_top - enter a parking state on a top location
207 * Enter parking state.
208 * Clear all movement history.
210 void dim_park_on_top(struct dim *dim);
213 * dim_park_tired - enter a tired parking state
216 * Enter parking state.
217 * Clear all movement history and cause DIM checks frequency to reduce.
219 void dim_park_tired(struct dim *dim);
222 * dim_calc_stats - calculate the difference between two samples
223 * @start: start sample
225 * @curr_stats: delta between samples
227 * Calculate the delta between two samples (in data rates).
228 * Takes into consideration counter wrap-around.
230 void dim_calc_stats(struct dim_sample *start, struct dim_sample *end,
231 struct dim_stats *curr_stats);
234 * dim_update_sample - set a sample's fields with give values
235 * @event_ctr: number of events to set
236 * @packets: number of packets to set
237 * @bytes: number of bytes to set
241 dim_update_sample(u16 event_ctr, u64 packets, u64 bytes, struct dim_sample *s)
243 s->time = ktime_get();
244 s->pkt_ctr = packets;
246 s->event_ctr = event_ctr;
253 * There are different set of profiles for each CQ period mode.
254 * There are different set of profiles for RX/TX CQs.
255 * Each profile size must be of NET_DIM_PARAMS_NUM_PROFILES
257 #define NET_DIM_PARAMS_NUM_PROFILES 5
258 #define NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE 256
259 #define NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE 128
260 #define NET_DIM_DEF_PROFILE_CQE 1
261 #define NET_DIM_DEF_PROFILE_EQE 1
263 #define NET_DIM_RX_EQE_PROFILES { \
264 {1, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
265 {8, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
266 {64, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
267 {128, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
268 {256, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
271 #define NET_DIM_RX_CQE_PROFILES { \
279 #define NET_DIM_TX_EQE_PROFILES { \
280 {1, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
281 {8, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
282 {32, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
283 {64, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
284 {128, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE} \
287 #define NET_DIM_TX_CQE_PROFILES { \
295 static const struct dim_cq_moder
296 rx_profile[DIM_CQ_PERIOD_NUM_MODES][NET_DIM_PARAMS_NUM_PROFILES] = {
297 NET_DIM_RX_EQE_PROFILES,
298 NET_DIM_RX_CQE_PROFILES,
301 static const struct dim_cq_moder
302 tx_profile[DIM_CQ_PERIOD_NUM_MODES][NET_DIM_PARAMS_NUM_PROFILES] = {
303 NET_DIM_TX_EQE_PROFILES,
304 NET_DIM_TX_CQE_PROFILES,
308 * net_dim_get_rx_moderation - provide a CQ moderation object for the given RX profile
309 * @cq_period_mode: CQ period mode
312 struct dim_cq_moder net_dim_get_rx_moderation(u8 cq_period_mode, int ix);
315 * net_dim_get_def_rx_moderation - provide the default RX moderation
316 * @cq_period_mode: CQ period mode
318 struct dim_cq_moder net_dim_get_def_rx_moderation(u8 cq_period_mode);
321 * net_dim_get_tx_moderation - provide a CQ moderation object for the given TX profile
322 * @cq_period_mode: CQ period mode
325 struct dim_cq_moder net_dim_get_tx_moderation(u8 cq_period_mode, int ix);
328 * net_dim_get_def_tx_moderation - provide the default TX moderation
329 * @cq_period_mode: CQ period mode
331 struct dim_cq_moder net_dim_get_def_tx_moderation(u8 cq_period_mode);
334 * net_dim - main DIM algorithm entry point
335 * @dim: DIM instance information
336 * @end_sample: Current data measurement
338 * Called by the consumer.
339 * This is the main logic of the algorithm, where data is processed in order to decide on next
342 void net_dim(struct dim *dim, struct dim_sample end_sample);