-
Notifications
You must be signed in to change notification settings - Fork 2
/
gcra.c
63 lines (52 loc) · 1.74 KB
/
gcra.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Copyright (c) 2011, Red Hat, Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND RED HAT, INC. DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RED HAT, INC. BE LIABLE
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Author: Jan Friesse <[email protected]>
*/
#include <sys/time.h>
#include <inttypes.h>
#include <string.h>
#include <time.h>
#include "gcra.h"
#include "util.h"
/*
* item is gcra_item to be initialized. Interval is interval in ms in which packet
* will arrive (max), and burst is number of packets which may arrive sooner.
*/
void
gcra_init(struct gcra_item *item, unsigned int interval, unsigned int burst)
{
memset(item, 0, sizeof(*item));
item->tau = burst * interval;
item->interval = interval;
}
/*
* item is gcra item and tv is time of packet arrival.
* Returns 0 if packet is non conforming and should be discarded/put to queue, ..., and 1 if packet
* is conforming.
*/
int
gcra_rl(struct gcra_item *item, struct timeval tv)
{
uint64_t tv_u64;
tv_u64 = util_tv_to_ms(tv);
if (item->tat >= item->tau && tv_u64 < item->tat - item->tau) {
return (0);
} else {
item->tat = ((tv_u64 > item->tat) ? tv_u64 : item->tat) + item->interval;
return (1);
}
}