]>
Commit | Line | Data |
---|---|---|
648f4f1f BP |
1 | /* |
2 | * Copyright (c) 2012 Nicira, Inc. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #ifndef TOKEN_BUCKET_H | |
18 | #define TOKEN_BUCKET_H 1 | |
19 | ||
20 | #include <limits.h> | |
21 | #include <stdbool.h> | |
22 | ||
23 | struct token_bucket { | |
24 | /* Configuration settings. */ | |
25 | unsigned int rate; /* Tokens added per millisecond. */ | |
26 | unsigned int burst; /* Max cumulative tokens credit. */ | |
27 | ||
28 | /* Current status. */ | |
29 | unsigned int tokens; /* Current number of tokens. */ | |
30 | long long int last_fill; /* Last time tokens added. */ | |
31 | }; | |
32 | ||
33 | #define TOKEN_BUCKET_INIT(RATE, BURST) { RATE, BURST, 0, LLONG_MIN } | |
34 | ||
35 | void token_bucket_init(struct token_bucket *, | |
36 | unsigned int rate, unsigned int burst); | |
37 | void token_bucket_set(struct token_bucket *, | |
38 | unsigned int rate, unsigned int burst); | |
39 | bool token_bucket_withdraw(struct token_bucket *, unsigned int n); | |
40 | void token_bucket_wait(struct token_bucket *, unsigned int n); | |
41 | ||
42 | #endif /* token-bucket.h */ |