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

◆ DEFAULT_SELECT_TIMEOUT

#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.

◆ REQUEST_ADD_DESTINATION

#define REQUEST_ADD_DESTINATION   0

Definition at line 173 of file daemon.h.

◆ REQUEST_ADD_SOURCE

#define REQUEST_ADD_SOURCE   1

Definition at line 174 of file daemon.h.

◆ REQUEST_GET_STATUS

#define REQUEST_GET_STATUS   4

Definition at line 177 of file daemon.h.

◆ REQUEST_GET_UUID

#define REQUEST_GET_UUID   5

Definition at line 178 of file daemon.h.

◆ REQUEST_START_FLOWS

#define REQUEST_START_FLOWS   2

Definition at line 175 of file daemon.h.

◆ REQUEST_STOP_FLOW

#define REQUEST_STOP_FLOW   3

Definition at line 176 of file daemon.h.

Enumeration Type Documentation

◆ flow_state_t

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 };

Function Documentation

◆ add_report()

void add_report ( struct report report)

Definition at line 827 of file daemon.c.

828 {
829  DEBUG_MSG(LOG_DEBUG, "add_report trying to lock mutex");
830  pthread_mutex_lock(&mutex);
831  DEBUG_MSG(LOG_DEBUG, "add_report aquired mutex");
832  /* Do not keep too much data */
833  if (pending_reports >= 250 && report->type != FINAL) {
834  free(report);
835  pthread_mutex_unlock(&mutex);
836  return;
837  }
838 
839  report->next = 0;
840 
841  if (reports_last)
843  else
844  reports = report;
845 
847  pending_reports++;
848 
849  pthread_mutex_unlock(&mutex);
850  DEBUG_MSG(LOG_DEBUG, "add_report unlocked mutex");
851 }

◆ daemon_main()

void* daemon_main ( void *  ptr)

◆ dispatch_request()

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 1489 of file daemon.c.

1490 {
1491  pthread_cond_t cond;
1492 
1493  request->error = NULL;
1494  request->type = type;
1495  request->next = NULL;
1496 
1497  /* Create synchronization mutex */
1498  if (pthread_cond_init(&cond, NULL)) {
1499  request_error(request, "Could not create synchronization mutex");
1500  return -1;
1501  }
1502  request->condition = &cond;
1503 
1504  pthread_mutex_lock(&mutex);
1505 
1506  if (!requests) {
1507  requests = request;
1509  } else {
1512  }
1513  if (write(daemon_pipe[1], &type, 1) != 1) /* Doesn't matter what we write */
1514  return -1;
1515  /* Wait until the daemon thread has processed the request */
1516  pthread_cond_wait(&cond, &mutex);
1517 
1518  pthread_mutex_unlock(&mutex);
1519 
1520  if (request->error)
1521  return -1;
1522 
1523  return 0;
1524 }

◆ flow_error()

void flow_error ( struct flow flow,
const char *  fmt,
  ... 
)

Definition at line 119 of file daemon.c.

120 {
121  char str[1000];
122  va_list ap;
123 
124  va_start(ap, fmt);
125  vsnprintf(str, 1000, fmt, ap);
126  va_end(ap);
127  str[sizeof(str) - 1] = 0;
128  flow->error = malloc(strlen(str) + 1);
129  strcpy(flow->error, str);
130 }

◆ get_reports()

struct report* get_reports ( int *  has_more)

Definition at line 853 of file daemon.c.

854 {
855  const unsigned max_reports = 50;
856 
857  struct report* ret;
858  DEBUG_MSG(LOG_DEBUG, "get_reports trying to lock mutex");
859  pthread_mutex_lock(&mutex);
860  DEBUG_MSG(LOG_DEBUG, "get_reports aquired mutex");
861  ret = reports;
862 
863  if (pending_reports <= max_reports) {
864  *has_more = 0;
865  pending_reports = 0;
866  reports = NULL;
867  reports_last = 0;
868  } else {
869  /* Split off first 50 items */
870  struct report* tmp;
871  for (unsigned i = 0; i < max_reports - 1; i++)
872  reports = reports->next;
873  tmp = reports->next;
874  reports->next = 0;
875  reports = tmp;
876 
877  pending_reports -= max_reports;
878  *has_more = 1;
879  }
880 
881  pthread_mutex_unlock(&mutex);
882  DEBUG_MSG(LOG_DEBUG, "get_reports unlocked mutex");
883  return ret;
884 }

◆ get_uuid_string()

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 1536 of file daemon.c.

1537 {
1538  uuid_t uuid;
1539  static char server_uuid[38] = "";
1540 
1541  if (!strlen(server_uuid)) {
1542  uuid_generate_time(uuid);
1543  uuid_unparse(uuid,uuid_str);
1544  memset(server_uuid,0,sizeof(server_uuid));
1545  strcpy(server_uuid,uuid_str);
1546  return;
1547  }
1548  strcpy(uuid_str,server_uuid);
1549 }

◆ request_error()

void request_error ( struct request request,
const char *  fmt,
  ... 
)

Definition at line 132 of file daemon.c.

133 {
134  char str[1000];
135  va_list ap;
136 
137  va_start(ap, fmt);
138  vsnprintf(str, 1000, fmt, ap);
139  va_end(ap);
140  str[sizeof(str) - 1] = 0;
141  request->error = malloc(strlen(str) + 1);
142  strcpy(request->error, str);
143 }

◆ set_flow_tcp_options()

int set_flow_tcp_options ( struct flow flow)

Definition at line 1424 of file daemon.c.

1425 {
1427 
1428  if (*flow->settings.cc_alg &&
1430  flow_error(flow, "Unable to set congestion control "
1431  "algorithm: %s", strerror(errno));
1432  return -1;
1433  }
1434  if (flow->settings.elcn &&
1435  set_so_elcn(flow->fd, flow->settings.elcn) == -1) {
1436  flow_error(flow, "Unable to set TCP_ELCN: %s",
1437  strerror(errno));
1438  return -1;
1439  }
1440  if (flow->settings.lcd && set_so_lcd(flow->fd) == -1) {
1441  flow_error(flow, "Unable to set TCP_LCD: %s",
1442  strerror(errno));
1443  return -1;
1444  }
1445  if (flow->settings.cork && set_tcp_cork(flow->fd) == -1) {
1446  flow_error(flow, "Unable to set TCP_CORK: %s",
1447  strerror(errno));
1448  return -1;
1449  }
1450  if (flow->settings.so_debug && set_so_debug(flow->fd) == -1) {
1451  flow_error(flow, "Unable to set SO_DEBUG: %s",
1452  strerror(errno));
1453  return -1;
1454  }
1455  if (flow->settings.mtcp && set_tcp_mtcp(flow->fd) == -1) {
1456  flow_error(flow, "Unable to set TCP_MTCP: %s",
1457  strerror(errno));
1458  return -1;
1459  }
1460  if (flow->settings.nonagle && set_tcp_nodelay(flow->fd) == -1) {
1461  flow_error(flow, "Unable to set TCP_NODELAY: %s",
1462  strerror(errno));
1463  return -1;
1464  }
1465  if (flow->settings.route_record && set_route_record(flow->fd) == -1) {
1466  flow_error(flow, "Unable to set route record option: %s",
1467  strerror(errno));
1468  return -1;
1469  }
1470  if (flow->settings.dscp &&
1471  set_dscp(flow->fd, flow->settings.dscp) == -1) {
1472  flow_error(flow, "Unable to set DSCP value: %s",
1473  strerror(errno));
1474  return -1;
1475  }
1476  if (flow->settings.ipmtudiscover &&
1477  set_ip_mtu_discover(flow->fd) == -1) {
1478  flow_error(flow, "Unable to set IP_MTU_DISCOVER value: %s",
1479  strerror(errno));
1480  return -1;
1481  }
1482  if (apply_extra_socket_options(flow) == -1)
1483  return -1;
1484 
1485  return 0;
1486 }

Variable Documentation

◆ daemon_pipe

int daemon_pipe[2]

Definition at line 87 of file daemon.c.

◆ daemon_thread

pthread_t daemon_thread

Definition at line 89 of file daemon.c.

◆ dump_dir

char* dump_dir

Definition at line 91 of file daemon.c.

◆ dump_prefix

char* dump_prefix

Definition at line 90 of file daemon.c.

◆ flows

struct linked_list flows

Definition at line 103 of file daemon.c.

◆ mutex

pthread_mutex_t mutex

Definition at line 93 of file daemon.c.

◆ pending_reports

unsigned pending_reports

Definition at line 101 of file daemon.c.

◆ reports

struct report* reports

Definition at line 99 of file daemon.c.

◆ reports_last

struct report* reports_last

Definition at line 100 of file daemon.c.

◆ requests

struct request* requests

Definition at line 94 of file daemon.c.

◆ requests_last

struct request * requests_last

Definition at line 191 of file daemon.h.

◆ started

char started

Definition at line 105 of file daemon.c.

DEBUG_MSG
#define DEBUG_MSG(LVL, MSG,...)
Print debug message to standard error.
Definition: debug.h:49
FINAL
@ FINAL
Final report.
Definition: common.h:116
apply_extra_socket_options
int apply_extra_socket_options(struct flow *flow)
Definition: daemon.c:1378
flow
Definition: daemon.h:73
request_error
void request_error(struct request *request, const char *fmt,...)
Definition: daemon.c:132
flow_settings::dscp
int dscp
DSCP value for TOS byte (option -D).
Definition: common.h:244
set_non_blocking
int set_non_blocking(int fd)
Definition: fg_socket.c:172
flow_settings::cork
int cork
Sets SO_DEBUG on test socket (option -O).
Definition: common.h:232
GRIND
@ GRIND
Definition: daemon.h:60
flow_settings::cc_alg
char cc_alg[TCP_CA_NAME_MAX]
Set congestion control algorithm ALG on test socket (option -O).
Definition: common.h:236
flow::settings
struct flow_settings settings
Definition: daemon.h:83
report::next
struct report * next
Definition: common.h:338
set_dscp
int set_dscp(int fd, int dscp)
Definition: fg_socket.c:132
set_tcp_cork
int set_tcp_cork(int fd)
Definition: fg_socket.c:317
request::type
char type
Definition: daemon.h:181
report
Definition: common.h:286
reports_last
struct report * reports_last
Definition: daemon.c:100
mutex
pthread_mutex_t mutex
Definition: daemon.c:93
flow_settings::ipmtudiscover
int ipmtudiscover
Set IP_MTU_DISCOVER on test socket (option -O).
Definition: common.h:246
set_tcp_nodelay
int set_tcp_nodelay(int fd)
Definition: fg_socket.c:358
set_so_debug
int set_so_debug(int fd)
Definition: fg_socket.c:366
set_so_elcn
int set_so_elcn(int fd, int val)
Definition: fg_socket.c:278
pending_reports
unsigned pending_reports
Definition: daemon.c:101
request
Definition: daemon.h:179
flow::error
char * error
Definition: daemon.h:170
request::next
struct request * next
Definition: daemon.h:189
GRIND_WAIT_CONNECT
@ GRIND_WAIT_CONNECT
Definition: daemon.h:56
requests_last
struct request * requests_last
Definition: daemon.c:94
GRIND_WAIT_ACCEPT
@ GRIND_WAIT_ACCEPT
Definition: daemon.h:58
requests
struct request * requests
Definition: daemon.c:94
flow_settings::nonagle
int nonagle
Disable nagle algorithm on test socket (option -O).
Definition: common.h:234
request::error
char * error
Definition: daemon.h:187
set_so_lcd
int set_so_lcd(int fd)
Definition: fg_socket.c:288
set_ip_mtu_discover
int set_ip_mtu_discover(int fd)
Definition: fg_socket.c:300
set_route_record
int set_route_record(int fd)
Definition: fg_socket.c:149
flow_settings::elcn
int elcn
Set TCP_ELCN (20) on test socket (option -O).
Definition: common.h:238
flow::fd
int fd
Definition: daemon.h:80
flow_error
void flow_error(struct flow *flow, const char *fmt,...)
Definition: daemon.c:119
reports
struct report * reports
Definition: daemon.c:99
flow_settings::lcd
int lcd
Set TCP_LCD (21) on test socket (option -O).
Definition: common.h:240
request::condition
pthread_cond_t * condition
Definition: daemon.h:185
daemon_pipe
int daemon_pipe[2]
Definition: daemon.c:87
report::type
enum report_t type
Report type - either INTERVAL or FINAL report.
Definition: common.h:291
flow_settings::mtcp
int mtcp
Set TCP_MTCP (15) on test socket (option -O).
Definition: common.h:242
set_congestion_control
int set_congestion_control(int fd, const char *cc_alg)
Definition: fg_socket.c:265
flow_settings::so_debug
int so_debug
Sets SO_DEBUG on test socket (option -O).
Definition: common.h:206
set_tcp_mtcp
int set_tcp_mtcp(int fd)
Definition: fg_socket.c:347
flow_settings::route_record
int route_record
Sets ROUTE_RECORD on test socket (option -O).
Definition: common.h:208