summaryrefslogtreecommitdiff
path: root/include/main.h
blob: 71a2cf08a8b7e95bd71ffb6cb8fc4275d52b8ac1 (plain)
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
/*
  Copyright 2018 Jonathan Ribas, FraudBuster. All rights reserved.

  Redistribution and use in source and binary forms, with or without modification,
  are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors
  may be used to endorse or promote products derived from this software without
  specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __COMMON_H__
#define __COMMON_H__

#include <stdint.h>
#include <semaphore.h>

#define MBUF_CACHE_SZ   32
#define TX_QUEUE_SIZE   4096
#define NB_TX_QUEUES    64 /* ^2 needed to make fast modulos % */
#define BURST_SZ        128
#define NB_RETRY_TX     (NB_TX_QUEUES * 2)

#define TX_PTHRESH 36 // Default value of TX prefetch threshold register.
#define TX_HTHRESH 0  // Default value of TX host threshold register.
#define TX_WTHRESH 0  // Default value of TX write-back threshold register.

#ifndef min
#define min(x, y) (x < y ? x : y)
#endif /* min */
#ifndef max
#define max(x, y) (x > y ? x : y)
#endif /* max */

/* struct to store the command line args */
struct cmd_opts {
    char**          pcicards;
    int             nb_pcicards;
    int             numacore;
    int             nbruns;
    unsigned int    maxbitrate;
    char*           trace;
};

/* struct to store the cpus context */
struct                  cpus_bindings {
    int                 numacores; /* nb of numacores of the system */
    int                 numacore; /* wanted numacore to run */
    unsigned int        nb_available_cpus;
    unsigned int        nb_needed_cpus;
    unsigned int*       cpus_to_use;
    char*               prefix;
    char*               suffix;
    uint64_t            coremask;
};

/* struct corresponding to a cache for one NIC port */
struct                  pcap_cache {
    struct rte_mbuf**   mbufs;
};

/* struct to store dpdk context */
struct                  dpdk_ctx {
    unsigned int        nb_mbuf; /* number of needed mbuf (see main.c) */
    unsigned int        mbuf_sz; /* wanted/needed size for the mbuf (see main.c) */
    unsigned int        pool_sz; /* mempool wanted/needed size (see main.c) */
    struct rte_mempool* pktmbuf_pool;

    /* pcap file caches */
    long int            pcap_sz; /* size of the capture */
    struct pcap_cache*  pcap_caches; /* tab of caches, one per NIC port */
};

/* struct to store threads context */
struct                  thread_ctx {
    sem_t*              sem;
    pthread_t           thread;
    int                 tx_port_id; /* assigned tx port id */
    int                 nbruns;
    unsigned int        nb_pkt;
    int                 nb_tx_queues;
    /* results */
    double              duration;
    unsigned int        total_drop;
    unsigned int        total_drop_sz;
    struct pcap_cache*  pcap_cache;
};

struct                  pcap_ctx {
    int                 fd;
    unsigned int        nb_pkts;
    unsigned int        max_pkt_sz;
    size_t              cap_sz;
};

/*
  FUNC PROTOTYPES
*/

/* CPUS.C */
int             init_cpus(const struct cmd_opts* opts, struct cpus_bindings* cpus);

/* DPDK.C */
int             init_dpdk_eal_mempool(const struct cmd_opts* opts,
                                      const struct cpus_bindings* cpus,
                                      struct dpdk_ctx* dpdk);
int             init_dpdk_ports(struct cpus_bindings* cpus);
void*           myrealloc(void* ptr, size_t new_size);
int             start_tx_threads(const struct cmd_opts* opts,
                                 const struct cpus_bindings* cpus,
                                 const struct dpdk_ctx* dpdk,
                                 const struct pcap_ctx *pcap);
void            dpdk_cleanup(struct dpdk_ctx* dpdk, struct cpus_bindings* cpus);

/* PCAP.C */
int             preload_pcap(const struct cmd_opts* opts, struct pcap_ctx* pcap);
int             load_pcap(const struct cmd_opts* opts, struct pcap_ctx* pcap,
                          const struct cpus_bindings* cpus, struct dpdk_ctx* dpdk);
void            clean_pcap_ctx(struct pcap_ctx* pcap);

/* UTILS.C */
char*           nb_oct_to_human_str(float size);
unsigned int    get_next_power_of_2(const unsigned int nb);

#endif /* __COMMON_H__ */