-
Notifications
You must be signed in to change notification settings - Fork 3
/
scenario_actions.c
183 lines (163 loc) · 6.8 KB
/
scenario_actions.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
/*
* 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 "include/disruptor.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
#include <netinet/in.h>
#include <linux/netfilter.h>
static int16_t sc_random(int16_t max) {
struct timeval t;
gettimeofday(&t,NULL);
srandom(t.tv_usec+t.tv_sec);
return ( ( random() % max ) + 1);
}
int scenario_action_none(struct scenario_s * s, struct disrupt_packet_s * p){
return true;
}
int scenario_action_loss(struct scenario_s * s, struct disrupt_packet_s * p){
int16_t var_rand = 0;
var_rand = sc_random(100);
if( var_rand <= s->init_interval_occurence ){
log_debug("random_scenario[loss]: dropping pkt_id[%d] seq[%d]", p->pkt_id, p->seq);
s->period_pkt_loss++;
nfq_set_verdict(s->qh, p->pkt_id, NF_DROP, 0, NULL);
return false;
}
return true;
}
int scenario_action_loss_rtcp(struct scenario_s * s, struct disrupt_packet_s * p){
int16_t var_rand = 0;
if(!p->rtcp) {
return true;
}
var_rand = sc_random(100);
if( var_rand <= s->init_interval_occurence ){
log_debug("random_scenario[loss_rtcp]: dropping RTCP pkt_id[%d] ssrc[%#x]", p->pkt_id, p->ssrc);
s->period_pkt_loss++;
nfq_set_verdict(s->qh, p->pkt_id, NF_DROP, 0, NULL);
return false;
}
return true;
}
int scenario_action_burst_loss(struct scenario_s * s, struct disrupt_packet_s * p) {
int16_t var_rand = 0;
if(s->pb_state == PB_NONE) {
var_rand = sc_random(s->init_interval_occurence);
}
if ( (var_rand==1) && (s->pb_state == PB_NONE) ) { // scenario random occurance
s->pb_state = PB_INIT;
}
if(s->pb_state == PB_NONE) {
log_debug("stream[%d]scenario_action[burst_loss]: no problem pkt_id[%d] seq[%d] period_cnt[%d]", p->stream->id, p->pkt_id, p->seq , s->period_pkt_count);
} else if(s->pb_state == PB_INIT) {
s->pb_pkt_pos = 0;
s->pb_pkt_start = s->period_pkt_count;
s->pb_state = PB_ACTIVE;
s->pb_pkt_max = sc_random(s->init_max_burst); // in this scenario this is a random amount of packet delayed emulate congestion
log_notice("stream[%d]scenario_action[burst_loss]: problem initialized affecting[%d] packets", p->stream->id, s->pb_pkt_max);
}
if(s->pb_state == PB_ACTIVE){ // pb is initialized, start queing packets
log_debug("stream[%d]scenario_action[burst_loss]: dropping[%d/%d] pkt_id[%d] seq[%d] period_cnt[%d]", p->stream->id, s->pb_pkt_pos, s->pb_pkt_max, p->pkt_id, p->seq, s->period_pkt_count);
s->period_pkt_loss++;
nfq_set_verdict(s->qh, p->pkt_id, NF_DROP, 0, NULL);
if(s->pb_pkt_pos == s->pb_pkt_max){
s->pb_state = PB_STOP;
} else {
s->pb_pkt_pos++;
}
return false;
} else if(s->pb_state == PB_STOP) {
s->pb_state = PB_NONE;
}
return true;
}
int scenario_action_jitter(struct scenario_s * s, struct disrupt_packet_s * p){
int16_t var_rand = 0;
if(s->pb_state == PB_NONE) {
if(s->params & JITTER_FIXED_BURST_INTERVAL) {
log_debug("stream[%d]scenario_action[jitter][%d]-[%d]>=[%d]: no problem pkt_id[%d] seq[%d] period_cnt[%d]",
p->stream->id, s->period_pkt_count, s->pb_pkt_stop, s->init_interval_occurence, p->pkt_id, p->seq , s->period_pkt_count);
if(s->period_pkt_count - s->pb_pkt_stop >= s->init_interval_occurence)
var_rand = 1;
} else {
log_debug("stream[%d]scenario_action[jitter][%d]-[%d]>=[%d]: no problem pkt_id[%d] seq[%d] period_cnt[%d] random_burst[%d]",
p->stream->id, s->period_pkt_count, s->pb_pkt_stop, s->init_interval_occurence, p->pkt_id, p->seq , s->period_pkt_count, s->init_interval_occurence);
if (s->init_interval_occurence == 0) {
log_error("%s invalid scenario", __func__);
return true;
}
var_rand = sc_random(s->init_interval_occurence);
}
}
if ( (var_rand==1) && (s->pb_state == PB_NONE) ) { // scenario random occurance
s->pb_state = PB_INIT;
}
if(s->pb_state == PB_NONE) {
log_debug("stream[%d]scenario_action[jitter]: no problem pkt_id[%d] seq[%d] period_cnt[%d]", p->stream->id, p->pkt_id, p->seq , s->period_pkt_count);
} else if(s->pb_state == PB_INIT) {
s->pb_pkt_pos = 0;
s->pb_pkt_start = s->period_pkt_count;
s->pb_state = PB_ACTIVE;
if(s->params & JITTER_FIXED_BURST_LEN) {
s->pb_pkt_max = s->init_max_burst;
} else {
s->pb_pkt_max = sc_random(s->init_max_burst); // in this scenario this is a random amount of packet delayed emulate congestion
}
log_debug("stream[%d]scenario_action[jitter]: problem initialized affecting[%d] packets", p->stream->id, s->pb_pkt_max);
}
if(s->pb_state == PB_ACTIVE){ // pb is initialized, start queing packets
s->queue_delay[s->pb_pkt_pos] = p->pkt_id;
s->queue_seq[s->pb_pkt_pos] = p->seq;
s->period_pkt_delayed++;
log_debug("stream[%d]scenario_action[jitter]: queueing[%d/%d] pkt_id[%d] seq[%d] period_cnt[%d]", p->stream->id, s->pb_pkt_pos, s->pb_pkt_max, s->queue_delay[s->pb_pkt_pos], p->seq, s->period_pkt_count);
if(s->pb_pkt_pos == s->pb_pkt_max){
s->pb_pkt_stop = s->period_pkt_count;
s->pb_state = PB_STOP;
} else {
s->pb_pkt_pos++;
}
return false;
} else if(s->pb_state == PB_STOP) { // release the packets
uint32_t pkt_id;
int i;
log_debug("stream[%d]scenario_action[jitter]: delayed packets [%d]", p->stream, s->pb_pkt_pos);
if(s->params & JITTER_OUT_OF_ORDER) {
for (i=s->pb_pkt_pos;i>=0;i--){
pkt_id = s->queue_delay[i];
log_debug("stream[%d]scenario_action[jitter]: release(outoforder) delayed packet[%d/%d] id[%d] seq[%d] period_cnt[%d]",
p->stream->id, i, s->pb_pkt_max, pkt_id, s->queue_seq[i], s->period_pkt_count);
nfq_set_verdict(s->qh, pkt_id , NF_ACCEPT, 0, NULL);
}
} else {
for (i=0;i<=s->pb_pkt_pos;i++){
pkt_id = s->queue_delay[i];
log_debug("stream[%d]scenario_action[jitter]: release delayed packet[%d/%d] id[%d] seq[%d] period_cnt[%d]",
p->stream->id, i, s->pb_pkt_max, pkt_id, s->queue_seq[i], s->period_pkt_count);
nfq_set_verdict(s->qh, pkt_id , NF_ACCEPT, 0, NULL);
}
}
s->pb_state = PB_NONE;
}
return true;
}