-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod_drop.c
97 lines (77 loc) · 1.7 KB
/
mod_drop.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* mod_drop.c
*
* Copyright (c) 2001 Dug Song <[email protected]>
*
* $Id: mod_drop.c,v 1.3 2002/04/07 22:55:20 dugsong Exp $
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pkt.h"
#include "mod.h"
#define DROP_FIRST 1
#define DROP_LAST 2
#define DROP_RANDOM 3
struct drop_data {
rand_t *rnd;
int which;
int percent;
};
void *
drop_close(void *d)
{
struct drop_data *data = (struct drop_data *)d;
if (data != NULL) {
rand_close(data->rnd);
free(data);
}
return (NULL);
}
void *
drop_open(int argc, char *argv[])
{
struct drop_data *data;
if (argc != 3)
return (NULL);
if ((data = calloc(1, sizeof(*data))) == NULL)
return (NULL);
data->rnd = rand_open();
if (strcasecmp(argv[1], "first") == 0)
data->which = DROP_FIRST;
else if (strcasecmp(argv[1], "last") == 0)
data->which = DROP_LAST;
else if (strcasecmp(argv[1], "random") == 0)
data->which = DROP_RANDOM;
else
return (drop_close(data));
if ((data->percent = atoi(argv[2])) <= 0 || data->percent > 100)
return (drop_close(data));
return (data);
}
int
drop_apply(void *d, struct pktq *pktq)
{
struct drop_data *data = (struct drop_data *)d;
struct pkt *pkt;
if (data->percent < 100 &&
(rand_uint16(data->rnd) % 100) > data->percent)
return (0);
if (data->which == DROP_FIRST)
pkt = TAILQ_FIRST(pktq);
else if (data->which == DROP_LAST)
pkt = TAILQ_LAST(pktq, pktq);
else
pkt = pktq_random(data->rnd, pktq);
TAILQ_REMOVE(pktq, pkt, pkt_next);
pkt_free(pkt);
return (0);
}
struct mod mod_drop = {
"drop", /* name */
"drop first|last|random <prob-%>", /* usage */
drop_open, /* open */
drop_apply, /* apply */
drop_close /* close */
};