Flowgrind
Advanced TCP traffic generator
trafgen.c
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2010-2013 Christian Samsel <christian.samsel@rwth-aachen.de>
8  *
9  * This file is part of Flowgrind.
10  *
11  * Flowgrind is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * Flowgrind is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with Flowgrind. If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25 
26 #ifndef HAVE_CONFIG_H
27 #include "config.h"
28 #endif /* HAVE_CONFIG_H */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <float.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <syslog.h>
42 
43 #include "daemon.h"
44 #include "debug.h"
45 #include "fg_math.h"
46 #include "trafgen.h"
47 
48 #define MAX_RUNS_PER_DISTRIBUTION 10
49 
50 inline static double calculate(struct flow *flow, enum distribution_t type, double param_one, double param_two) {
51 
52  double val = 0;
53 
54  switch (type) {
55  case NORMAL:
56  val = dist_normal (flow, param_one, param_two );
57  DEBUG_MSG(LOG_DEBUG, "calculated normal distribution "
58  "value %f for parameters %f,%f", val,
59  param_one, param_two);
60  break;
61 
62  case UNIFORM:
63  val = dist_uniform (flow, param_one, param_two );
64  DEBUG_MSG(LOG_DEBUG, "calculated uniform distribution "
65  "value %f", val);
66  break;
67 
68  case WEIBULL:
69  val = dist_weibull (flow, param_one, param_two );
70  DEBUG_MSG(LOG_DEBUG, "calculated weibull distribution "
71  "value %f for parameters %f,%f", val,
72  param_one, param_two);
73  break;
74 
75  case EXPONENTIAL:
76  val = dist_exponential (flow, param_one);
77  DEBUG_MSG(LOG_DEBUG, "calculated exponential "
78  "distribution value %f for parameters %f",
79  val, param_one);
80  break;
81 
82  case PARETO:
83  val = dist_pareto (flow, param_one, param_two);
84  DEBUG_MSG(LOG_DEBUG, "calculated pareto distribution "
85  "value %f for parameters %f,%f", val,
86  param_one, param_two);
87  break;
88 
89  case LOGNORMAL:
90  val = dist_normal (flow, param_one, param_two );
91  DEBUG_MSG(LOG_DEBUG, "calculated lognormal "
92  "distribution value %f for parameters %f,%f",
93  val, param_one, param_two);
94  break;
95 
96  case CONSTANT:
97  /* constant is default */
98  default:
99  val = param_one;
100  DEBUG_MSG(LOG_DEBUG, "constant value %f", val);
101 
102  }
103 
104  return val;
105 
106 }
108 {
109  int bs = 0;
110  int i = 0;
111  /* recalculate values to match prequisits, but at most 10 times */
112  while (( bs < MIN_BLOCK_SIZE || bs > flow->settings.maximum_block_size) && i < MAX_RUNS_PER_DISTRIBUTION) {
113 
114  bs = round(calculate(
115  flow,
119  ));
120  i++;
121  }
122 
123  /* sanity checks */
124  if (i >= MAX_RUNS_PER_DISTRIBUTION && bs < MIN_BLOCK_SIZE) {
125  bs = MIN_BLOCK_SIZE;
126  DEBUG_MSG(LOG_WARNING, "applied minimal request size limit %d "
127  "for flow %d", bs, flow->id);
128  }
129 
130  if (i >= MAX_RUNS_PER_DISTRIBUTION && bs > flow->settings.maximum_block_size) {
131  bs = flow->settings.maximum_block_size;
132  DEBUG_MSG(LOG_WARNING, "applied maximal request size limit %d "
133  "for flow %d", bs, flow->id);
134 
135  }
136 
137  DEBUG_MSG(LOG_NOTICE, "calculated request size %d for flow %d after %d "
138  "runs", bs, flow->id, i);
139 
140  return bs;
141 }
142 
144 {
145  int bs = round(calculate(
146  flow,
150  ));
151 
152  /* sanity checks */
153  if (bs && bs < MIN_BLOCK_SIZE) {
154  bs = MIN_BLOCK_SIZE;
155  DEBUG_MSG(LOG_WARNING, "applied minimal response size limit "
156  "%d for flow %d", bs, flow->id);
157  }
158  if (bs > flow->settings.maximum_block_size) {
159  bs = flow->settings.maximum_block_size;
160  DEBUG_MSG(LOG_WARNING, "applied maximal response size limit "
161  "%d for flow %d", bs, flow->id);
162 
163  }
164 
165  if (bs)
166  DEBUG_MSG(LOG_NOTICE, "calculated response size %d for flow "
167  "%d", bs, flow->id);
168 
169  return bs;
170 
171 }
172 
173 double next_interpacket_gap(struct flow *flow) {
174 
175  double gap = 0.0;
176  if (flow->settings.write_rate)
177  gap = ((double)flow->settings.maximum_block_size)/flow->settings.write_rate;
178  else
179  gap = calculate(flow,
183  );
184 
185  if (gap)
186  DEBUG_MSG(LOG_NOTICE, "calculated next interpacket gap %.6fs "
187  "for flow %d", gap, flow->id);
188 
189  return gap;
190 }
int maximum_block_size
Application buffer size in bytes (option -U).
Definition: common.h:201
Routines used by the Flowgrind daemon.
Debugging routines for Flowgrind controller and daemon.
double dist_uniform(struct flow *flow, const double minval, const double maxval)
Definition: fg_math.c:136
int write_rate
The actual rate we should send.
Definition: common.h:220
Routines for statistics and advanced traffic generation.
int next_response_block_size(struct flow *flow)
Definition: trafgen.c:143
struct flow_settings settings
Definition: daemon.h:83
double dist_exponential(struct flow *flow, const double mu)
Definition: fg_math.c:123
double param_two
Second mathematical parameter of the distribution, if required.
Definition: common.h:172
#define DEBUG_MSG(LVL, MSG,...)
Print debug message to standard error.
Definition: debug.h:49
Log Normal distribution.
Definition: common.h:144
double dist_weibull(struct flow *flow, const double alpha, const double beta)
Definition: fg_math.c:206
#define MAX_RUNS_PER_DISTRIBUTION
Definition: trafgen.c:48
#define MIN_BLOCK_SIZE
Minium block (message) size we can send.
Definition: common.h:79
double dist_pareto(struct flow *flow, const double k, const double x_min)
Definition: fg_math.c:190
Uniform distribution.
Definition: common.h:138
Exponential distribution.
Definition: common.h:140
Normal distribution.
Definition: common.h:134
struct trafgen_options response_trafgen_options
Stochastic traffic generation settings for the response size.
Definition: common.h:251
struct trafgen_options interpacket_gap_trafgen_options
Stochastic traffic generation settings for the interpacket gap.
Definition: common.h:253
distribution_t
Stochastic distributions for traffic generation.
Definition: common.h:130
Weibull distribution.
Definition: common.h:136
static double calculate(struct flow *flow, enum distribution_t type, double param_one, double param_two)
Definition: trafgen.c:50
Routines used by the Flowgrind daemon for advanced traffic generation.
double next_interpacket_gap(struct flow *flow)
Definition: trafgen.c:173
No stochastic distribution.
Definition: common.h:132
double param_one
First mathemathical parameter of the distribution.
Definition: common.h:170
enum distribution_t distribution
The stochastic distribution to draw values from.
Definition: common.h:168
Definition: daemon.h:73
int next_request_block_size(struct flow *flow)
Definition: trafgen.c:107
int id
Definition: daemon.h:75
Pareto distribution.
Definition: common.h:142
struct trafgen_options request_trafgen_options
Stochastic traffic generation settings for the request size.
Definition: common.h:249
double dist_normal(struct flow *flow, const double mu, const double sigma_square)
Definition: fg_math.c:149