-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.h
175 lines (154 loc) · 7.25 KB
/
main.h
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
/*
* License: MIT
*
* Copyright (c) 2017-2020 James Bensley.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _ETHERATE_MT_H_
#define _ETHERATE_MT_H_
#define _GNU_SOURCE // Required for pthread_attr_setaffinity_np()
#include <errno.h> // errno
#include <net/ethernet.h> // ETH_P_ALL
#include <net/if.h> // IF_NAMESIZE, struct ifreq
#include <linux/if_packet.h> // struct packet_mreq, sockaddr_ll, tpacket_req, tpacket2_hdr, tpacket3_hdr, tpacket_req3
#include <ifaddrs.h> // freeifaddrs(), getifaddrs()
#include <arpa/inet.h> // htons()
#include <inttypes.h> // PRIuN
#include <sys/ioctl.h> // ioctl()
#include <math.h> // floor()
#include <sys/mman.h> // mmap()
#include <linux/net_tstamp.h> // struct hwtstamp_config
#include <poll.h> // poll()
#include <pthread.h> // pthread_*()
#include <sys/socket.h> // socket()
#include <linux/sockios.h> // SIOCSHWTSTAMP
#include <signal.h> // signal()
#include <stdlib.h> // calloc(), exit(), EXIT_FAILURE, EXIT_SUCCESS, rand(), RAND_MAX, strtoul()
#include <stdio.h> // FILE, fclose(), fopen(), fscanf(), perror(), printf()
#include <string.h> // memcpy(), memset(), strncpy()
#include <sys/random.h> // getrandom()
#include <sys/syscall.h> // SYS_gettid
#include <sys/sysinfo.h> // get_nprocs()
#include "sysexits.h" // EX_NOPERM, EX_PROTOCOL, EX_SOFTWARE
#include <unistd.h> // getpagesize(), getpid(), getuid(), read(), sleep()
#include <linux/version.h> // KERNEL_VERSION(), LINUX_VERSION_CODE
// Global constants:
#define app_version "MT 1.0 2020-02"
// Global defaults:
#define DEF_FRM_SZ 1514 // Default Ethernet frame size at layer 2 excluding FCS
#define DEF_BLK_FRM_SZ 2096 // Default frame size in a block, data + TPACKET2_HDRLEN (52).
#define DEF_BLK_SZ getpagesize() // Default block size
#define DEF_BLK_NR 256 // Default number of blocks per ring
#define DEF_ERR_LEN 128 // Default length of string from errno
#define DEF_FRM_SZ_MAX 10000 // Max frame size with headers
#define DEF_MSGVEC_LEN 256 // Default msgvec_vlen for sendmmsg()/recvmmsg()
#define DEF_THD_NR 1 // Default number of worker threads
// Flags for socket mode:
#define SKT_RX 0 // Run in Rx mode
#define SKT_TX 1 // Run in Tx mode
#define SKT_BIDI 2 // Run in bidirectional mode (Tx and Rx)
// Flags for socket type:
#define SKT_PACKET 0 // Use read()/sendto()
#define SKT_PACKET_MMAP2 1 // Use PACKET_MMAP v2 Tx/Rx rings
#define SKT_SENDMSG 2 // Use sendmsg()/recvmsg()
#define SKT_SENDMMSG 3 // Use sendmmsg()/recvmmsg()
#define SKT_PACKET_MMAP3 4 // Use PACKET_MMAP v3 Tx/Rx rings
#define DEF_SKT_TYPE SKT_PACKET // Default mode
// Application behaviour options:
struct app_opt {
uint8_t err_len;
char *err_str;
int32_t fanout_grp; // CPU fanout group for AF_PACKET sockets
uint8_t sk_mode; // Tx/Rx/Bidi
uint8_t sk_type; // PACKET_MMAP, send(), sendmmsg() etc.
pthread_t *thd;
uint8_t thd_affin; ///// Add CLI arg, try to avoid split NUMA node?
pthread_attr_t *thd_attr; // pthread_attr_t
uint16_t thd_nr; // Number of worker threads to run
uint8_t verbose; // Verbose debugging toggle
};
// Frame and ring buffer options:
struct frm_opt {
uint32_t block_frm_sz; // Size of frame in block (frame_sz + TPACKET2_HDRLEN)
uint32_t block_nr; // Number of frame blocks per ring
uint32_t block_sz; // Size of frame block in ring
uint8_t custom_frame; // Bool to load a customer frame form file
uint16_t frame_sz; // Frame size (layer 2 headers + layer 2 payload)
uint32_t frame_nr; // Total number of frames in ring
uint8_t *tx_buffer; // Point to frame copied into ring
};
// Socket specific options:
struct sk_opt {
int32_t if_index;
uint8_t if_name[IF_NAMESIZE];
uint32_t msgvec_vlen;
};
// A copy of the values required for each thread
struct thd_opt {
int32_t affinity; // CPU this thread runs on or -1 for no affinity
struct sockaddr_ll bind_addr;
uint32_t block_frm_sz;
uint32_t block_nr;
uint32_t block_sz;
uint8_t err_len;
char *err_str;
uint32_t fanout_grp; // CPU fanout group the socket is joined to
uint32_t frame_nr;
uint16_t frame_sz;
uint16_t frm_sz_max;
int32_t if_index; // bind() a socket() to IfIndex
uint8_t if_name[IF_NAMESIZE];
uint8_t* mmap_buf; // Buffer used for PACKET_MMAP ring
uint32_t msgvec_vlen;
struct iovec* ring; // PACKET_MMAP ring
uint8_t quit; // Signal stats thread to exit
uint8_t *rx_buffer;
uint64_t rx_bytes; // Total bytes received
uint64_t rx_frms; // Total frames received
uint64_t sk_err; // Number of send/receive syscall errors
uint8_t sk_mode; // Tx/Rx/Bidi
uint8_t sk_type; // PACKET_MMAP, send(), sendmmsg() etc.
int32_t sock; // Socket file descriptor
uint8_t started; // Has Tx or Rx loop started?
uint8_t stalling; // Socket is returning ENOBUFS
uint32_t thd_id; // Thread ID of "this" thread
uint16_t thd_nr; // If >1, join a FANOUT group
void *thd_ret; // Thread exit status
int32_t tpacket_ver; // TPACKET_V2 || TPACKET_V3
void *tpacket_req3; // TPACKET V3
uint8_t tpacket_req3_sz; // TPACKET V3
void *tpacket_req; // TPACKET V2
uint8_t tpacket_req_sz; // TPACKET V2
uint16_t ring_type; // PACKET_TX_RING/PACKET_RX_RING
uint8_t *tx_buffer; // Tx frame buffer
uint64_t tx_bytes; // Total bytes sent
uint64_t tx_frms; // Total packets sent
uint8_t verbose; // Enable verbose output
};
struct etherate {
struct app_opt app_opt;
struct frm_opt frm_opt;
struct ifreq ifr;
struct sk_opt sk_opt;
struct thd_opt *thd_opt;
};
struct etherate *eth_p;
#endif // _ETHERATE_MT_H_