Skip to content

Commit ae1adba

Browse files
committed
Organize and clean code.
1 parent 8c72b06 commit ae1adba

9 files changed

+138
-143
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ libbpf:
6969
# Build and install the Packet Batch common submodule (this includes libyaml).
7070
common:
7171
$(MAKE) -C $(COMMON_DIR)/
72-
72+
7373
common_install:
7474
$(MAKE) -C $(COMMON_DIR)/ install
7575

src/af_xdp.c

+22-37
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <string.h>
4-
#include <unistd.h>
5-
#include <errno.h>
6-
#include <locale.h>
7-
#include <linux/types.h>
8-
9-
#include <net/if.h>
10-
11-
#include <sys/socket.h>
12-
#include <linux/if_link.h>
13-
#include <bpf.h>
14-
#include <xsk.h>
15-
161
#include "af_xdp.h"
172

183
/* Global variables */
194
// The XDP flags to load the AF_XDP/XSK sockets with.
20-
__u32 xdp_flags = XDP_FLAGS_DRV_MODE;
21-
__u32 bind_flags = XDP_USE_NEED_WAKEUP;
5+
u32 xdp_flags = XDP_FLAGS_DRV_MODE;
6+
u32 bind_flags = XDP_USE_NEED_WAKEUP;
227
int is_shared_umem = 0;
23-
__u16 batch_size = 1;
8+
u16 batch_size = 1;
249
int static_queue_id = 0;
2510
int queue_id = 0;
2611

2712
// For shared UMEM.
2813
static unsigned int global_frame_idx = 0;
2914

3015
// Pointers to the umem and XSK sockets for each thread.
31-
struct xsk_umem_info *shared_umem = NULL;
16+
xsk_umem_info_t *shared_umem = NULL;
3217

3318
/**
3419
* Completes the TX call via a syscall and also checks if we need to free the TX buffer.
@@ -37,7 +22,7 @@ struct xsk_umem_info *shared_umem = NULL;
3722
*
3823
* @return Void
3924
**/
40-
static void complete_tx(struct xsk_socket_info *xsk)
25+
static void complete_tx(xsk_socket_info_t *xsk)
4126
{
4227
// Initiate starting variables (completed amount and completion ring index).
4328
unsigned int completed;
@@ -75,10 +60,10 @@ static void complete_tx(struct xsk_socket_info *xsk)
7560
*
7661
* @return Returns a pointer to the UMEM area instead of the XSK UMEM information structure (struct xsk_umem_info).
7762
**/
78-
static struct xsk_umem_info *configure_xsk_umem(void *buffer, __u64 size)
63+
static xsk_umem_info_t *configure_xsk_umem(void *buffer, u64 size)
7964
{
8065
// Create umem pointer and return variable.
81-
struct xsk_umem_info *umem;
66+
xsk_umem_info_t *umem;
8267
int ret;
8368

8469
// Allocate memory space to the umem pointer and check.
@@ -115,12 +100,12 @@ static struct xsk_umem_info *configure_xsk_umem(void *buffer, __u64 size)
115100
*
116101
* @return Returns a pointer to the AF_XDP/XSK socket inside of a the XSK socket info structure (struct xsk_socket_info).
117102
**/
118-
static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem, int queue_id, const char *dev)
103+
static xsk_socket_info_t *xsk_configure_socket(xsk_umem_info_t *umem, int queue_id, const char *dev)
119104
{
120105
// Initialize starting variables.
121106
struct xsk_socket_config xsk_cfg;
122107
struct xsk_socket_info *xsk_info;
123-
__u32 idx;
108+
u32 idx;
124109
int i;
125110
int ret;
126111

@@ -190,10 +175,10 @@ static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem,
190175
*
191176
* @return Returns 0 on success and -1 on failure.
192177
**/
193-
int send_packet(struct xsk_socket_info *xsk, int thread_id, void *pckt, __u16 length, __u8 verbose)
178+
int send_packet(xsk_socket_info_t *xsk, int thread_id, void *pckt, u16 length, u8 verbose)
194179
{
195180
// This represents the TX index.
196-
__u32 tx_idx = 0;
181+
u32 tx_idx = 0;
197182

198183
// Retrieve the TX index from the TX ring to fill.
199184
while (xsk_ring_prod__reserve(&xsk->tx, batch_size, &tx_idx) < batch_size)
@@ -223,7 +208,7 @@ int send_packet(struct xsk_socket_info *xsk, int thread_id, void *pckt, __u16 le
223208
}
224209

225210
// We must retrieve the next available address in the UMEM.
226-
__u64 addrat = get_umem_addr(xsk, idx);
211+
u64 addrat = get_umem_addr(xsk, idx);
227212

228213
// We must copy our packet data to the UMEM area at the specific index (idx * frame size). We did this earlier.
229214
memcpy(get_umem_loc(xsk, addrat), pckt, length);
@@ -262,7 +247,7 @@ int send_packet(struct xsk_socket_info *xsk, int thread_id, void *pckt, __u16 le
262247
*
263248
* @return The socket FD (-1 on failure)
264249
*/
265-
int get_socket_fd(struct xsk_socket_info *xsk)
250+
int get_socket_fd(xsk_socket_info_t *xsk)
266251
{
267252
return xsk_socket__fd(xsk->xsk);
268253
}
@@ -275,7 +260,7 @@ int get_socket_fd(struct xsk_socket_info *xsk)
275260
*
276261
* @return 64-bit address of location.
277262
**/
278-
__u64 get_umem_addr(struct xsk_socket_info *xsk, int idx)
263+
u64 get_umem_addr(xsk_socket_info_t *xsk, int idx)
279264
{
280265
return xsk->umem_frame_addr[idx];
281266
}
@@ -288,7 +273,7 @@ __u64 get_umem_addr(struct xsk_socket_info *xsk, int idx)
288273
*
289274
* @return Pointer to address in memory of UMEM.
290275
**/
291-
void *get_umem_loc(struct xsk_socket_info *xsk, __u64 addr)
276+
void *get_umem_loc(xsk_socket_info_t *xsk, u64 addr)
292277
{
293278
return xsk_umem__get_data(xsk->umem->buffer, addr);
294279
}
@@ -301,7 +286,7 @@ void *get_umem_loc(struct xsk_socket_info *xsk, __u64 addr)
301286
*
302287
* @return Void
303288
**/
304-
void setup_af_xdp_variables(struct cmd_line_af_xdp *cmd_af_xdp, int verbose)
289+
void setup_af_xdp_variables(cmd_line_af_xdp_t *cmd_af_xdp, int verbose)
305290
{
306291
// Check for zero-copy or copy modes.
307292
if (cmd_af_xdp->zero_copy)
@@ -386,11 +371,11 @@ void setup_af_xdp_variables(struct cmd_line_af_xdp *cmd_af_xdp, int verbose)
386371
*
387372
* @return 0 on success and -1 on failure.
388373
**/
389-
struct xsk_umem_info *setup_umem(int thread_id)
374+
xsk_umem_info_t *setup_umem(int thread_id)
390375
{
391376
// This indicates the buffer for frames and frame size for the UMEM area.
392377
void *frame_buffer;
393-
__u64 frame_buffer_size = NUM_FRAMES * FRAME_SIZE;
378+
u64 frame_buffer_size = NUM_FRAMES * FRAME_SIZE;
394379

395380
// Allocate blank memory space for the UMEM (aligned in chunks). Check as well.
396381
if (posix_memalign(&frame_buffer, getpagesize(), frame_buffer_size))
@@ -412,7 +397,7 @@ struct xsk_umem_info *setup_umem(int thread_id)
412397
*
413398
* @return Returns the AF_XDP's socket FD or -1 on failure.
414399
**/
415-
struct xsk_socket_info* setup_socket(const char *dev, __u16 thread_id, int verbose)
400+
xsk_socket_info_t *setup_socket(const char *dev, u16 thread_id, int verbose)
416401
{
417402
// Verbose message.
418403
if (verbose)
@@ -421,7 +406,7 @@ struct xsk_socket_info* setup_socket(const char *dev, __u16 thread_id, int verbo
421406
}
422407

423408
// Configure and create the AF_XDP/XSK socket.
424-
struct xsk_umem_info *umem;
409+
xsk_umem_info_t *umem;
425410

426411
// Check for shared UMEM.
427412
if (is_shared_umem)
@@ -455,7 +440,7 @@ struct xsk_socket_info* setup_socket(const char *dev, __u16 thread_id, int verbo
455440
return NULL;
456441
}
457442

458-
struct xsk_socket_info *xsk = xsk_configure_socket(umem, (static_queue_id) ? queue_id : thread_id, (const char *)dev);
443+
xsk_socket_info_t *xsk = xsk_configure_socket(umem, (static_queue_id) ? queue_id : thread_id, (const char *)dev);
459444

460445
// Check to make sure it's valid.
461446
if (xsk == NULL)
@@ -484,7 +469,7 @@ struct xsk_socket_info* setup_socket(const char *dev, __u16 thread_id, int verbo
484469
*
485470
* @return Void
486471
**/
487-
void cleanup_socket(struct xsk_socket_info *xsk)
472+
void cleanup_socket(xsk_socket_info_t *xsk)
488473
{
489474
// If the AF_XDP/XSK socket isn't NULL, delete it.
490475
if (xsk->xsk != NULL)

src/af_xdp.h

+29-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
#pragma once
22

3-
#include <linux/types.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <unistd.h>
7+
#include <errno.h>
8+
#include <locale.h>
9+
10+
#include <net/if.h>
11+
12+
#include <sys/socket.h>
13+
#include <linux/if_link.h>
14+
#include <bpf.h>
15+
416
#include <xsk.h>
517

18+
#include <simple_types.h>
19+
620
#include "cmd_line.h"
721

822
#define MAX_CPUS 256
@@ -11,42 +25,42 @@
1125
#define INVALID_UMEM_FRAME UINT64_MAX
1226
//#define DEBUG
1327

14-
struct xsk_umem_info
28+
typedef struct xsk_umem_info
1529
{
1630
struct xsk_ring_prod fq;
1731
struct xsk_ring_cons cq;
1832
struct xsk_umem *umem;
1933
void *buffer;
20-
};
34+
} xsk_umem_info_t;
2135

22-
struct xsk_socket
36+
typedef struct xsk_socket
2337
{
2438
struct xsk_ring_cons *rx;
2539
struct xsk_ring_prod *tx;
26-
__u64 outstanding_tx;
40+
u64 outstanding_tx;
2741
struct xsk_ctx *ctx;
2842
struct xsk_socket_config config;
2943
int fd;
30-
};
44+
} xsk_socket_t;
3145

32-
struct xsk_socket_info
46+
typedef struct xsk_socket_info
3347
{
3448
struct xsk_ring_cons rx;
3549
struct xsk_ring_prod tx;
3650
struct xsk_umem_info *umem;
3751
struct xsk_socket *xsk;
3852

39-
__u64 umem_frame_addr[NUM_FRAMES];
40-
__u32 umem_frame_free;
53+
u64 umem_frame_addr[NUM_FRAMES];
54+
u32 umem_frame_free;
4155

42-
__u32 outstanding_tx;
43-
};
56+
u32 outstanding_tx;
57+
} xsk_socket_info_t;
4458

45-
int send_packet(struct xsk_socket_info *xsk, int thread_id, void *pckt, __u16 length, __u8 verbose);
46-
__u64 get_umem_addr(struct xsk_socket_info *xsk, int idx);
47-
void *get_umem_loc(struct xsk_socket_info *xsk, __u64 addr);
59+
int send_packet(struct xsk_socket_info *xsk, int thread_id, void *pckt, u16 length, u8 verbose);
60+
u64 get_umem_addr(struct xsk_socket_info *xsk, int idx);
61+
void *get_umem_loc(struct xsk_socket_info *xsk, u64 addr);
4862
void setup_af_xdp_variables(struct cmd_line_af_xdp *cmd_af_xdp, int verbose);
4963
struct xsk_umem_info *setup_umem(int index);
50-
struct xsk_socket_info *setup_socket(const char *dev, __u16 thread_id, int verbose);
64+
struct xsk_socket_info *setup_socket(const char *dev, u16 thread_id, int verbose);
5165
void cleanup_socket(struct xsk_socket_info *xsk);
5266
int get_socket_fd(struct xsk_socket_info *xsk);

src/cmd_line.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <getopt.h>
4-
51
#include "cmd_line.h"
62

73
static const struct option long_opts[] =
@@ -25,7 +21,7 @@ static const struct option long_opts[] =
2521
*
2622
* @return Void
2723
**/
28-
void parse_cmd_line_af_xdp(struct cmd_line_af_xdp *cmd_af_xdp, int argc, char **argv)
24+
void parse_cmd_line_af_xdp(cmd_line_af_xdp_t *cmd_af_xdp, int argc, char **argv)
2925
{
3026
int c = -1;
3127

src/cmd_line.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#pragma once
22

3-
struct cmd_line_af_xdp
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <getopt.h>
6+
7+
typedef struct cmd_line_af_xdp
48
{
59
unsigned int queue_set : 1;
610
int queue;
@@ -11,6 +15,6 @@ struct cmd_line_af_xdp
1115
unsigned int skb_mode : 1;
1216
unsigned int zero_copy : 1;
1317
unsigned int copy : 1;
14-
};
18+
} cmd_line_af_xdp_t;
1519

1620
void parse_cmd_line_af_xdp(struct cmd_line_af_xdp *cmd_af_xdp, int argc, char **argv);

src/main.c

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <string.h>
4-
#include <unistd.h>
5-
#include <linux/types.h>
6-
#include <getopt.h>
7-
#include <errno.h>
8-
#include <signal.h>
9-
10-
#include <utils.h>
11-
#include <cmd_line.h>
12-
#include <config.h>
13-
14-
#include "sequence.h"
15-
#include "cmd_line.h"
16-
#include "af_xdp.h"
171
#include "main.h"
182

193
struct config *cfg = NULL;
@@ -90,7 +74,7 @@ int main(int argc, char *argv[])
9074
}
9175

9276
// Attempt to parse config.
93-
__u8 log = 1;
77+
u8 log = 1;
9478

9579
if (cmd.cli)
9680
{

src/main.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
#pragma once
22

3-
#define MAX_NAME_LEN 64
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <unistd.h>
7+
#include <getopt.h>
8+
#include <errno.h>
9+
#include <signal.h>
10+
11+
#include <utils.h>
12+
#include <cmd_line.h>
13+
#include <config.h>
14+
#include <simple_types.h>
15+
16+
#include "sequence.h"
17+
#include "cmd_line.h"
18+
#include "af_xdp.h"
419

520
extern int errno;

0 commit comments

Comments
 (0)