2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
25 #define NUM_POLICE_RESULTS 3
31 QOS_ACTION_MARK_AND_TRANSMIT
32 } __clib_packed qos_action_type_en;
34 // This is the hardware representation of the policer.
35 // To be multithread-safe, the policer is accessed through a spin-lock
36 // on the lock field. (For a policer update operation, 24B needs to be
37 // modified and this would be a challenge to do with atomic instructions.)
38 // The structure is padded so that no other data is put into the same
39 // 64B cache-line. This reduces cache-thrashing between threads.
42 // The HW TSC tick is roughly one CPU clock cycle.
43 // This is shifted to create a larger period, with a goal to be around 50usec.
44 // The period time will vary based on CPU clock speed.
45 // CPU speeds of 1Ghz to 8Ghz are targetted.
46 // The shift amount is a constant 17 bits, resulting in a period between
47 // 16usec (8Ghz CPU) and 131usec (1Ghz CPU).
48 // The token_per_period computation takes into account the clock speed.
50 // The 32-bit bucket/limit supports about 850ms of burst on a 40GE port,
51 // or 340ms on a 100GE port. If a larger burst is configured, then the
52 // programmed value is simply capped at 2^32-1. If we needed to support
53 // more than that, the bucket and limit fields could be expanded.
55 // tokens_per_period should be > 1000 to support 0.1% granularity.
56 // To support lower rates (which would not meet this requirement), the packet
57 // length, bucket, and limit values can be scaled. The scale is a power of 2
58 // so the multiplication can be implemented as a shift. The control plane
59 // computes the shift amount be the largest possible that still supports the
60 // burst size. This makes the rate accuracy as high as possible.
62 // The 64-bit last_update_time supports a 4Ghz CPU without rollover for 100
65 // The lock field should be used for a spin-lock on the struct. Alternatively,
66 // a thread index field is provided so that policed packets may be handed
67 // off to a single worker thread.
69 #define POLICER_TICKS_PER_PERIOD_SHIFT 17
70 #define POLICER_TICKS_PER_PERIOD (1 << POLICER_TICKS_PER_PERIOD_SHIFT)
75 u32 lock; // for exclusive access to the struct
77 u32 single_rate; // 1 = single rate policer, 0 = two rate policer
78 u32 color_aware; // for hierarchical policing
79 u32 scale; // power-of-2 shift amount for lower rates
80 qos_action_type_en action[3];
81 ip_dscp_t mark_dscp[3];
84 // Fields are marked as 2R if they are only used for a 2-rate policer,
85 // and MOD if they are modified as part of the update operation.
88 u32 cir_tokens_per_period; // # of tokens for each period
89 u32 pir_tokens_per_period; // 2R
92 u32 current_bucket; // MOD
94 u32 extended_bucket; // MOD
96 u64 last_update_time; // MOD
97 u32 thread_index; // Tie policer to a thread, rather than lock
100 } policer_read_response_type_st;
102 static inline policer_result_e
103 vnet_police_packet (policer_read_response_type_st * policer,
105 policer_result_e packet_color, u64 time)
108 u64 current_tokens, extended_tokens;
109 policer_result_e result;
111 // Scale packet length to support a wide range of speeds
112 packet_length = packet_length << policer->scale;
114 // Compute the number of policer periods that have passed since the last
116 n_periods = time - policer->last_update_time;
117 policer->last_update_time = time;
119 // Since there is no background last-update-time adjustment, n_periods
120 // could grow large if the policer is idle for a long time. This could
121 // cause a 64-bit overflow when computing tokens_per_period * num_periods.
122 // It will overflow if log2(n_periods) + log2(tokens_per_period) > 64.
124 // To mitigate this, the policer configuration algorithm insures that
125 // tokens_per_period is less than 2^22, i.e. this is a 22 bit value not
126 // a 32-bit value. Thus overflow will only occur if n_periods > 64-22 or
127 // 42. 2^42 min-sized periods is 16us * 2^42, or 2 years. So this can
128 // rarely occur. If overflow does happen, the only effect will be that
129 // fewer tokens than the max burst will be added to the bucket for this
130 // packet. This constraint on tokens_per_period lets the ucode omit
131 // code to dynamically check for or prevent the overflow.
133 if (policer->single_rate)
136 // Compute number of tokens for this time period
138 policer->current_bucket + n_periods * policer->cir_tokens_per_period;
139 if (current_tokens > policer->current_limit)
141 current_tokens = policer->current_limit;
145 policer->extended_bucket + n_periods * policer->cir_tokens_per_period;
146 if (extended_tokens > policer->extended_limit)
148 extended_tokens = policer->extended_limit;
153 if ((!policer->color_aware || (packet_color == POLICE_CONFORM))
154 && (current_tokens >= packet_length))
156 policer->current_bucket = current_tokens - packet_length;
157 policer->extended_bucket = extended_tokens - packet_length;
158 result = POLICE_CONFORM;
160 else if ((!policer->color_aware || (packet_color != POLICE_VIOLATE))
161 && (extended_tokens >= packet_length))
163 policer->current_bucket = current_tokens;
164 policer->extended_bucket = extended_tokens - packet_length;
165 result = POLICE_EXCEED;
169 policer->current_bucket = current_tokens;
170 policer->extended_bucket = extended_tokens;
171 result = POLICE_VIOLATE;
179 // Compute number of tokens for this time period
181 policer->current_bucket + n_periods * policer->cir_tokens_per_period;
183 policer->extended_bucket + n_periods * policer->pir_tokens_per_period;
184 if (current_tokens > policer->current_limit)
186 current_tokens = policer->current_limit;
188 if (extended_tokens > policer->extended_limit)
190 extended_tokens = policer->extended_limit;
195 if ((policer->color_aware && (packet_color == POLICE_VIOLATE))
196 || (extended_tokens < packet_length))
198 policer->current_bucket = current_tokens;
199 policer->extended_bucket = extended_tokens;
200 result = POLICE_VIOLATE;
202 else if ((policer->color_aware && (packet_color == POLICE_EXCEED))
203 || (current_tokens < packet_length))
205 policer->current_bucket = current_tokens;
206 policer->extended_bucket = extended_tokens - packet_length;
207 result = POLICE_EXCEED;
211 policer->current_bucket = current_tokens - packet_length;
212 policer->extended_bucket = extended_tokens - packet_length;
213 result = POLICE_CONFORM;
219 #endif // __POLICE_H__
222 * fd.io coding-style-patch-verification: ON
225 * eval: (c-set-style "gnu")