-
Notifications
You must be signed in to change notification settings - Fork 3
/
scenario.c
205 lines (186 loc) · 6.99 KB
/
scenario.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (C) 2015-2016 Julien Chavanton
*
* This file is part of Disruptor, a network impairment server.
*
* Disruptor is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Disruptor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "include/scenario.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
#include <netinet/in.h>
#include <linux/netfilter.h>
#include "ezxml/ezxml.h"
#include "include/disruptor.h"
static const char* scenario_action_to_string(scenario_action_t a) {
if (a == A_NONE) {
return "none";
} else if(a = A_JITTER) {
return "jitter";
} else if(a = A_LOSS) {
return "loss";
} else if(a = A_BURST_LOSS) {
return "burst_loss";
} else if(a = A_LOSS_RTCP) {
return "loss_rtcp";
}
return "none";
};
void scenario_init(struct scenario_s *s){
if(!s->queue_seq){
s->queue_seq = malloc(sizeof(unsigned int) * q_max_pkt);
}
if(!s->queue_delay){
s->queue_delay = malloc(sizeof(unsigned int) * q_max_pkt);
}
}
void scenario_period_init(struct scenario_s *s){
s->pb_state = PB_NONE; // starting without active problem
s->pb_pkt_pos = 0;
s->pb_pkt_start = 0;
s->pb_pkt_stop = 0;
s->pb_pkt_max = 0;
s->period_pkt_count = 0;
s->period_pkt_delayed = 0;
s->period_pkt_loss = 0;
srand ( time(NULL) ); // random seed
}
void scenario_set_action(struct scenario_s * s){
switch(s->action){ /* set the function pointer to the action */
case A_NONE:
s->scenario_action = scenario_action_none;
break;
case A_JITTER:
s->scenario_action = scenario_action_jitter;
break;
case A_LOSS:
s->scenario_action = scenario_action_loss;
break;
case A_BURST_LOSS:
s->scenario_action = scenario_action_burst_loss;
break;
case A_LOSS_RTCP:
s->scenario_action = scenario_action_loss_rtcp;
break;
default :
s->scenario_action = NULL;
break;
}
return;
}
void scenario_init_xml(struct disrupt_stream_s *stream) {
log_debug("scenario_init_xml");
stream->scenario.scenario_xml = ezxml_parse_file(stream->scenario.filename);
stream->scenario.scenario_period_xml = ezxml_child(stream->scenario.scenario_xml, "period");
scenario_init(&stream->scenario);
scenario_read_period_xml(&stream->scenario,0);
}
bool scenario_read_period_xml(struct scenario_s * s, int32_t stream_duration) {
ezxml_t action;
const char *action_name;
const char *period_duration;
if(s->scenario_period_xml) {
period_duration = ezxml_attr(s->scenario_period_xml, "duration");
for (action = ezxml_child(s->scenario_period_xml, "action"); action; action = action->next) {
action_name = ezxml_attr(action, "name");
break;
}
s->period_start = stream_duration;
s->period_duration = atoi(period_duration);
s->period_pkt_count =0;
s->period_bytes_received=0;
scenario_period_init(s);
s->scenario_period_xml = s->scenario_period_xml->next;
} else {
log_debug("scenario_read_period_xml: no period found.\n");
return false;
}
//ezxml_free(scenario_xml);
log_debug("scenario_read_xml: period[%ss] action[%s]", period_duration, action_name);
if(strcasecmp(action_name,"jitter") == 0){
s->action = A_JITTER;
if(ezxml_attr(action, "burst_max")) {
s->init_max_burst = atoi(ezxml_attr(action, "burst_max"));
s->params &= ~JITTER_FIXED_BURST_LEN;
} else if(ezxml_attr(action, "burst_size")){
s->init_max_burst = atoi(ezxml_attr(action, "burst_size"));
s->params |= JITTER_FIXED_BURST_LEN;
}
if(ezxml_attr(action, "interval_max")) {
s->init_interval_occurence = atoi(ezxml_attr(action, "interval_max"));
s->params &= ~JITTER_FIXED_BURST_INTERVAL;
} else if(ezxml_attr(action, "interval_size")) {
s->init_interval_occurence = atoi(ezxml_attr(action, "interval_size"));
s->params |= JITTER_FIXED_BURST_INTERVAL;
}
if(ezxml_attr(action, "outoforder")){
s->params |= JITTER_OUT_OF_ORDER;
} else {
s->params &= ~JITTER_OUT_OF_ORDER;
}
log_debug(" burst[%d]random[%d] interval[%d]random[%d]",
s->init_max_burst, (s->params & JITTER_FIXED_BURST_LEN)?0:1,
s->init_interval_occurence, (s->params & JITTER_FIXED_BURST_INTERVAL)?0:1);
} else if(strcasecmp(action_name,"burst_loss") == 0){
s->action = A_BURST_LOSS;
s->init_max_burst = atoi(ezxml_attr(action, "max"));
s->init_interval_occurence = atoi(ezxml_attr(action, "rand"));
log_debug(" max_burst[%d] random_occurence[%d]", s->init_max_burst, s->init_interval_occurence);
} else if(strcasecmp(action_name,"loss") == 0){
s->action = A_LOSS;
s->init_interval_occurence = atoi(ezxml_attr(action, "rand"));
log_debug(" percentage[%d]", s->init_interval_occurence);
} else if(strcasecmp(action_name,"loss_rtcp") == 0){
s->action = A_LOSS_RTCP;
s->init_interval_occurence = atoi(ezxml_attr(action, "rand"));
log_debug(" percentage[%d]", s->init_interval_occurence);
} else {
s->action = A_NONE;
}
scenario_set_action(s);
return true;
}
int scenario_check_pkt(struct scenario_s * s, struct disrupt_packet_s * packet, int32_t stream_duration, int32_t stream_id){
if(s == NULL || s->scenario_action == NULL)
return true;
s->period_pkt_count++;
s->period_bytes_received = s->period_bytes_received + packet->size;
if( stream_duration - s->period_start >= s->period_duration){
int32_t bps=0;
/* some scenario actions may need to release packets before ending the period transition */
if(s->pb_state == PB_ACTIVE){
log_debug("scenario period completed stopping action active state[%d]", s->pb_state);
s->pb_state = PB_STOP;
s->scenario_action(s, packet);
}
log_notice("scenario period completed [%ds]to[%ds] stream[%d] action[%s]loss[%d%%]delayed[%d]received[%d]bytes[%d] bandwidth[%dKbps]",
s->period_start, stream_duration, stream_id, scenario_action_to_string(s->action),
s->period_pkt_loss*100/s->period_pkt_count,
s->period_pkt_delayed,
s->period_pkt_count,
s->period_bytes_received,
s->period_bytes_received*8/(s->period_duration*1024)
);
if(!scenario_read_period_xml(s, stream_duration)) {
//s->action=NONE;
//scenario_set_action(s);
s->scenario_action = NULL;
}
return true;
}
return s->scenario_action(s, packet);
}