Flowgrind
Advanced TCP traffic generator
daemon.h File Reference

Routines used by the Flowgrind daemon. More...

#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <gsl/gsl_rng.h>
#include "common.h"
#include "fg_list.h"
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/server.h>
#include <xmlrpc-c/server_abyss.h>
#include <xmlrpc-c/util.h>

Go to the source code of this file.

Data Structures

struct  flow
 
struct  flow_source_settings
 
struct  request
 
struct  request_add_flow_destination
 
struct  request_add_flow_source
 
struct  request_get_status
 
struct  request_get_uuid
 structure for getting the UUID. More...
 
struct  request_start_flows
 
struct  request_stop_flow
 
struct  flow::statistics
 

Macros

#define DEFAULT_SELECT_TIMEOUT   10000000
 Time select() will block waiting for a file descriptor to become ready. More...
 
#define REQUEST_ADD_DESTINATION   0
 
#define REQUEST_ADD_SOURCE   1
 
#define REQUEST_GET_STATUS   4
 
#define REQUEST_GET_UUID   5
 
#define REQUEST_START_FLOWS   2
 
#define REQUEST_STOP_FLOW   3
 

Enumerations

enum  flow_state_t { GRIND_WAIT_CONNECT = 0, GRIND_WAIT_ACCEPT, GRIND }
 

Functions

void add_report (struct report *report)
 
void * daemon_main (void *ptr)
 
int dispatch_request (struct request *request, int type)
 Dispatch a request to daemon loop. More...
 
void flow_error (struct flow *flow, const char *fmt,...)
 
struct reportget_reports (int *has_more)
 
void get_uuid_string (char *uuid_str)
 To generate daemon UUID. More...
 
void request_error (struct request *request, const char *fmt,...)
 
int set_flow_tcp_options (struct flow *flow)
 

Variables

int daemon_pipe [2]
 
pthread_t daemon_thread
 
char * dump_dir
 
char * dump_prefix
 
struct linked_list flows
 
pthread_mutex_t mutex
 
unsigned pending_reports
 
struct reportreports
 
struct reportreports_last
 
struct requestrequests
 
struct requestrequests_last
 
char started
 

Detailed Description

Routines used by the Flowgrind daemon.

Definition in file daemon.h.

Macro Definition Documentation

#define DEFAULT_SELECT_TIMEOUT   10000000

Time select() will block waiting for a file descriptor to become ready.

Definition at line 51 of file daemon.h.

#define REQUEST_ADD_DESTINATION   0

Definition at line 173 of file daemon.h.

#define REQUEST_ADD_SOURCE   1

Definition at line 174 of file daemon.h.

#define REQUEST_GET_STATUS   4

Definition at line 177 of file daemon.h.

#define REQUEST_GET_UUID   5

Definition at line 178 of file daemon.h.

#define REQUEST_START_FLOWS   2

Definition at line 175 of file daemon.h.

#define REQUEST_STOP_FLOW   3

Definition at line 176 of file daemon.h.

Enumeration Type Documentation

Enumerator
GRIND_WAIT_CONNECT 
GRIND_WAIT_ACCEPT 
GRIND 

Definition at line 53 of file daemon.h.

54 {
55  /* SOURCE */
57  /* DESTINATION */
59  /* RUN */
60  GRIND,
61 };
Definition: daemon.h:60

Function Documentation

void add_report ( struct report report)

Definition at line 823 of file daemon.c.

824 {
825  DEBUG_MSG(LOG_DEBUG, "add_report trying to lock mutex");
826  pthread_mutex_lock(&mutex);
827  DEBUG_MSG(LOG_DEBUG, "add_report aquired mutex");
828  /* Do not keep too much data */
829  if (pending_reports >= 250 && report->type != FINAL) {
830  free(report);
831  pthread_mutex_unlock(&mutex);
832  return;
833  }
834 
835  report->next = 0;
836 
837  if (reports_last)
838  reports_last->next = report;
839  else
840  reports = report;
841 
842  reports_last = report;
843  pending_reports++;
844 
845  pthread_mutex_unlock(&mutex);
846  DEBUG_MSG(LOG_DEBUG, "add_report unlocked mutex");
847 }
struct report * reports
Definition: daemon.c:95
struct report * next
Definition: common.h:338
pthread_mutex_t mutex
Definition: daemon.c:89
#define DEBUG_MSG(LVL, MSG,...)
Print debug message to standard error.
Definition: debug.h:49
unsigned pending_reports
Definition: daemon.c:97
enum report_t type
Report type - either INTERVAL or FINAL report.
Definition: common.h:291
struct report * reports_last
Definition: daemon.c:96
Final report.
Definition: common.h:116
void* daemon_main ( void *  ptr)
int dispatch_request ( struct request request,
int  type 
)

Dispatch a request to daemon loop.

Is called by the rpc server to feed in requests to the daemon.

Definition at line 1485 of file daemon.c.

1486 {
1487  pthread_cond_t cond;
1488 
1489  request->error = NULL;
1490  request->type = type;
1491  request->next = NULL;
1492 
1493  /* Create synchronization mutex */
1494  if (pthread_cond_init(&cond, NULL)) {
1495  request_error(request, "Could not create synchronization mutex");
1496  return -1;
1497  }
1498  request->condition = &cond;
1499 
1500  pthread_mutex_lock(&mutex);
1501 
1502  if (!requests) {
1503  requests = request;
1504  requests_last = request;
1505  } else {
1506  requests_last->next = request;
1507  requests_last = request;
1508  }
1509  if (write(daemon_pipe[1], &type, 1) != 1) /* Doesn't matter what we write */
1510  return -1;
1511  /* Wait until the daemon thread has processed the request */
1512  pthread_cond_wait(&cond, &mutex);
1513 
1514  pthread_mutex_unlock(&mutex);
1515 
1516  if (request->error)
1517  return -1;
1518 
1519  return 0;
1520 }
struct request * next
Definition: daemon.h:189
pthread_mutex_t mutex
Definition: daemon.c:89
struct request * requests
Definition: daemon.c:90
char type
Definition: daemon.h:181
char * error
Definition: daemon.h:187
pthread_cond_t * condition
Definition: daemon.h:185
struct request * requests_last
Definition: daemon.c:90
int daemon_pipe[2]
Definition: daemon.c:87
void request_error(struct request *request, const char *fmt,...)
Definition: daemon.c:128
void flow_error ( struct flow flow,
const char *  fmt,
  ... 
)

Definition at line 115 of file daemon.c.

116 {
117  char str[1000];
118  va_list ap;
119 
120  va_start(ap, fmt);
121  vsnprintf(str, 1000, fmt, ap);
122  va_end(ap);
123  str[sizeof(str) - 1] = 0;
124  flow->error = malloc(strlen(str) + 1);
125  strcpy(flow->error, str);
126 }
char * error
Definition: daemon.h:170
struct report* get_reports ( int *  has_more)

Definition at line 849 of file daemon.c.

850 {
851  const unsigned max_reports = 50;
852 
853  struct report* ret;
854  DEBUG_MSG(LOG_DEBUG, "get_reports trying to lock mutex");
855  pthread_mutex_lock(&mutex);
856  DEBUG_MSG(LOG_DEBUG, "get_reports aquired mutex");
857  ret = reports;
858 
859  if (pending_reports <= max_reports) {
860  *has_more = 0;
861  pending_reports = 0;
862  reports = NULL;
863  reports_last = 0;
864  } else {
865  /* Split off first 50 items */
866  struct report* tmp;
867  for (unsigned i = 0; i < max_reports - 1; i++)
868  reports = reports->next;
869  tmp = reports->next;
870  reports->next = 0;
871  reports = tmp;
872 
873  pending_reports -= max_reports;
874  *has_more = 1;
875  }
876 
877  pthread_mutex_unlock(&mutex);
878  DEBUG_MSG(LOG_DEBUG, "get_reports unlocked mutex");
879  return ret;
880 }
struct report * reports
Definition: daemon.c:95
struct report * next
Definition: common.h:338
Definition: common.h:286
pthread_mutex_t mutex
Definition: daemon.c:89
#define DEBUG_MSG(LVL, MSG,...)
Print debug message to standard error.
Definition: debug.h:49
unsigned pending_reports
Definition: daemon.c:97
struct report * reports_last
Definition: daemon.c:96
void get_uuid_string ( char *  uuid_str)

To generate daemon UUID.

Generate the daemon UUID and convert the UUID to a string data. UUID is generated by daemon only once and stored in the global variable. The daemon return the same UUID for all the flows it maintaining. This UUID is taken as a reference to identify the daemon in the controller.

Parameters
[in,out]uuid_strdaemons UUID

Definition at line 1532 of file daemon.c.

1533 {
1534  uuid_t uuid;
1535  static char server_uuid[38] = "";
1536 
1537  if (!strlen(server_uuid)) {
1538  uuid_generate_time(uuid);
1539  uuid_unparse(uuid,uuid_str);
1540  memset(server_uuid,0,sizeof(server_uuid));
1541  strcpy(server_uuid,uuid_str);
1542  return;
1543  }
1544  strcpy(uuid_str,server_uuid);
1545 }
void request_error ( struct request request,
const char *  fmt,
  ... 
)

Definition at line 128 of file daemon.c.

129 {
130  char str[1000];
131  va_list ap;
132 
133  va_start(ap, fmt);
134  vsnprintf(str, 1000, fmt, ap);
135  va_end(ap);
136  str[sizeof(str) - 1] = 0;
137  request->error = malloc(strlen(str) + 1);
138  strcpy(request->error, str);
139 }
char * error
Definition: daemon.h:187
int set_flow_tcp_options ( struct flow flow)

Definition at line 1420 of file daemon.c.

1421 {
1422  set_non_blocking(flow->fd);
1423 
1424  if (*flow->settings.cc_alg &&
1425  set_congestion_control(flow->fd, flow->settings.cc_alg) == -1) {
1426  flow_error(flow, "Unable to set congestion control "
1427  "algorithm: %s", strerror(errno));
1428  return -1;
1429  }
1430  if (flow->settings.elcn &&
1431  set_so_elcn(flow->fd, flow->settings.elcn) == -1) {
1432  flow_error(flow, "Unable to set TCP_ELCN: %s",
1433  strerror(errno));
1434  return -1;
1435  }
1436  if (flow->settings.lcd && set_so_lcd(flow->fd) == -1) {
1437  flow_error(flow, "Unable to set TCP_LCD: %s",
1438  strerror(errno));
1439  return -1;
1440  }
1441  if (flow->settings.cork && set_tcp_cork(flow->fd) == -1) {
1442  flow_error(flow, "Unable to set TCP_CORK: %s",
1443  strerror(errno));
1444  return -1;
1445  }
1446  if (flow->settings.so_debug && set_so_debug(flow->fd) == -1) {
1447  flow_error(flow, "Unable to set SO_DEBUG: %s",
1448  strerror(errno));
1449  return -1;
1450  }
1451  if (flow->settings.mtcp && set_tcp_mtcp(flow->fd) == -1) {
1452  flow_error(flow, "Unable to set TCP_MTCP: %s",
1453  strerror(errno));
1454  return -1;
1455  }
1456  if (flow->settings.nonagle && set_tcp_nodelay(flow->fd) == -1) {
1457  flow_error(flow, "Unable to set TCP_NODELAY: %s",
1458  strerror(errno));
1459  return -1;
1460  }
1461  if (flow->settings.route_record && set_route_record(flow->fd) == -1) {
1462  flow_error(flow, "Unable to set route record option: %s",
1463  strerror(errno));
1464  return -1;
1465  }
1466  if (flow->settings.dscp &&
1467  set_dscp(flow->fd, flow->settings.dscp) == -1) {
1468  flow_error(flow, "Unable to set DSCP value: %s",
1469  strerror(errno));
1470  return -1;
1471  }
1472  if (flow->settings.ipmtudiscover &&
1473  set_ip_mtu_discover(flow->fd) == -1) {
1474  flow_error(flow, "Unable to set IP_MTU_DISCOVER value: %s",
1475  strerror(errno));
1476  return -1;
1477  }
1478  if (apply_extra_socket_options(flow) == -1)
1479  return -1;
1480 
1481  return 0;
1482 }
int mtcp
Set TCP_MTCP (15) on test socket (option -O).
Definition: common.h:242
int set_so_debug(int fd)
Definition: fg_socket.c:366
int set_dscp(int fd, int dscp)
Definition: fg_socket.c:132
int set_so_lcd(int fd)
Definition: fg_socket.c:288
int lcd
Set TCP_LCD (21) on test socket (option -O).
Definition: common.h:240
int set_tcp_nodelay(int fd)
Definition: fg_socket.c:358
int set_tcp_mtcp(int fd)
Definition: fg_socket.c:347
int set_tcp_cork(int fd)
Definition: fg_socket.c:317
int set_route_record(int fd)
Definition: fg_socket.c:149
struct flow_settings settings
Definition: daemon.h:83
int cork
Sets SO_DEBUG on test socket (option -O).
Definition: common.h:232
char cc_alg[TCP_CA_NAME_MAX]
Set congestion control algorithm ALG on test socket (option -O).
Definition: common.h:236
int ipmtudiscover
Set IP_MTU_DISCOVER on test socket (option -O).
Definition: common.h:246
int dscp
DSCP value for TOS byte (option -D).
Definition: common.h:244
int route_record
Sets ROUTE_RECORD on test socket (option -O).
Definition: common.h:208
int set_ip_mtu_discover(int fd)
Definition: fg_socket.c:300
int set_so_elcn(int fd, int val)
Definition: fg_socket.c:278
int set_congestion_control(int fd, const char *cc_alg)
Definition: fg_socket.c:265
int elcn
Set TCP_ELCN (20) on test socket (option -O).
Definition: common.h:238
int fd
Definition: daemon.h:80
int so_debug
Sets SO_DEBUG on test socket (option -O).
Definition: common.h:206
int apply_extra_socket_options(struct flow *flow)
Definition: daemon.c:1374
int nonagle
Disable nagle algorithm on test socket (option -O).
Definition: common.h:234
int set_non_blocking(int fd)
Definition: fg_socket.c:172
void flow_error(struct flow *flow, const char *fmt,...)
Definition: daemon.c:115

Variable Documentation

int daemon_pipe[2]

Definition at line 87 of file daemon.c.

pthread_t daemon_thread

Definition at line 255 of file daemon.h.

char* dump_dir

Definition at line 273 of file daemon.h.

char* dump_prefix
Bug:
: shouldn't be global?

Definition at line 272 of file daemon.h.

struct linked_list flows

Definition at line 99 of file daemon.c.

pthread_mutex_t mutex

Definition at line 89 of file daemon.c.

unsigned pending_reports

Definition at line 97 of file daemon.c.

struct report* reports

Definition at line 95 of file daemon.c.

struct report* reports_last

Definition at line 96 of file daemon.c.

struct request* requests

Definition at line 90 of file daemon.c.

struct request * requests_last

Definition at line 90 of file daemon.c.

char started

Definition at line 101 of file daemon.c.