Skip to content

Commit

Permalink
Organize and clean code.
Browse files Browse the repository at this point in the history
  • Loading branch information
gamemann committed May 13, 2024
1 parent 8c72b06 commit ae1adba
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 143 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ libbpf:
# Build and install the Packet Batch common submodule (this includes libyaml).
common:
$(MAKE) -C $(COMMON_DIR)/

common_install:
$(MAKE) -C $(COMMON_DIR)/ install

Expand Down
59 changes: 22 additions & 37 deletions src/af_xdp.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <locale.h>
#include <linux/types.h>

#include <net/if.h>

#include <sys/socket.h>
#include <linux/if_link.h>
#include <bpf.h>
#include <xsk.h>

#include "af_xdp.h"

/* Global variables */
// The XDP flags to load the AF_XDP/XSK sockets with.
__u32 xdp_flags = XDP_FLAGS_DRV_MODE;
__u32 bind_flags = XDP_USE_NEED_WAKEUP;
u32 xdp_flags = XDP_FLAGS_DRV_MODE;
u32 bind_flags = XDP_USE_NEED_WAKEUP;
int is_shared_umem = 0;
__u16 batch_size = 1;
u16 batch_size = 1;
int static_queue_id = 0;
int queue_id = 0;

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

// Pointers to the umem and XSK sockets for each thread.
struct xsk_umem_info *shared_umem = NULL;
xsk_umem_info_t *shared_umem = NULL;

/**
* Completes the TX call via a syscall and also checks if we need to free the TX buffer.
Expand All @@ -37,7 +22,7 @@ struct xsk_umem_info *shared_umem = NULL;
*
* @return Void
**/
static void complete_tx(struct xsk_socket_info *xsk)
static void complete_tx(xsk_socket_info_t *xsk)
{
// Initiate starting variables (completed amount and completion ring index).
unsigned int completed;
Expand Down Expand Up @@ -75,10 +60,10 @@ static void complete_tx(struct xsk_socket_info *xsk)
*
* @return Returns a pointer to the UMEM area instead of the XSK UMEM information structure (struct xsk_umem_info).
**/
static struct xsk_umem_info *configure_xsk_umem(void *buffer, __u64 size)
static xsk_umem_info_t *configure_xsk_umem(void *buffer, u64 size)
{
// Create umem pointer and return variable.
struct xsk_umem_info *umem;
xsk_umem_info_t *umem;
int ret;

// Allocate memory space to the umem pointer and check.
Expand Down Expand Up @@ -115,12 +100,12 @@ static struct xsk_umem_info *configure_xsk_umem(void *buffer, __u64 size)
*
* @return Returns a pointer to the AF_XDP/XSK socket inside of a the XSK socket info structure (struct xsk_socket_info).
**/
static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem, int queue_id, const char *dev)
static xsk_socket_info_t *xsk_configure_socket(xsk_umem_info_t *umem, int queue_id, const char *dev)
{
// Initialize starting variables.
struct xsk_socket_config xsk_cfg;
struct xsk_socket_info *xsk_info;
__u32 idx;
u32 idx;
int i;
int ret;

Expand Down Expand Up @@ -190,10 +175,10 @@ static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem,
*
* @return Returns 0 on success and -1 on failure.
**/
int send_packet(struct xsk_socket_info *xsk, int thread_id, void *pckt, __u16 length, __u8 verbose)
int send_packet(xsk_socket_info_t *xsk, int thread_id, void *pckt, u16 length, u8 verbose)
{
// This represents the TX index.
__u32 tx_idx = 0;
u32 tx_idx = 0;

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

// We must retrieve the next available address in the UMEM.
__u64 addrat = get_umem_addr(xsk, idx);
u64 addrat = get_umem_addr(xsk, idx);

// We must copy our packet data to the UMEM area at the specific index (idx * frame size). We did this earlier.
memcpy(get_umem_loc(xsk, addrat), pckt, length);
Expand Down Expand Up @@ -262,7 +247,7 @@ int send_packet(struct xsk_socket_info *xsk, int thread_id, void *pckt, __u16 le
*
* @return The socket FD (-1 on failure)
*/
int get_socket_fd(struct xsk_socket_info *xsk)
int get_socket_fd(xsk_socket_info_t *xsk)
{
return xsk_socket__fd(xsk->xsk);
}
Expand All @@ -275,7 +260,7 @@ int get_socket_fd(struct xsk_socket_info *xsk)
*
* @return 64-bit address of location.
**/
__u64 get_umem_addr(struct xsk_socket_info *xsk, int idx)
u64 get_umem_addr(xsk_socket_info_t *xsk, int idx)
{
return xsk->umem_frame_addr[idx];
}
Expand All @@ -288,7 +273,7 @@ __u64 get_umem_addr(struct xsk_socket_info *xsk, int idx)
*
* @return Pointer to address in memory of UMEM.
**/
void *get_umem_loc(struct xsk_socket_info *xsk, __u64 addr)
void *get_umem_loc(xsk_socket_info_t *xsk, u64 addr)
{
return xsk_umem__get_data(xsk->umem->buffer, addr);
}
Expand All @@ -301,7 +286,7 @@ void *get_umem_loc(struct xsk_socket_info *xsk, __u64 addr)
*
* @return Void
**/
void setup_af_xdp_variables(struct cmd_line_af_xdp *cmd_af_xdp, int verbose)
void setup_af_xdp_variables(cmd_line_af_xdp_t *cmd_af_xdp, int verbose)
{
// Check for zero-copy or copy modes.
if (cmd_af_xdp->zero_copy)
Expand Down Expand Up @@ -386,11 +371,11 @@ void setup_af_xdp_variables(struct cmd_line_af_xdp *cmd_af_xdp, int verbose)
*
* @return 0 on success and -1 on failure.
**/
struct xsk_umem_info *setup_umem(int thread_id)
xsk_umem_info_t *setup_umem(int thread_id)
{
// This indicates the buffer for frames and frame size for the UMEM area.
void *frame_buffer;
__u64 frame_buffer_size = NUM_FRAMES * FRAME_SIZE;
u64 frame_buffer_size = NUM_FRAMES * FRAME_SIZE;

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

// Configure and create the AF_XDP/XSK socket.
struct xsk_umem_info *umem;
xsk_umem_info_t *umem;

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

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

// Check to make sure it's valid.
if (xsk == NULL)
Expand Down Expand Up @@ -484,7 +469,7 @@ struct xsk_socket_info* setup_socket(const char *dev, __u16 thread_id, int verbo
*
* @return Void
**/
void cleanup_socket(struct xsk_socket_info *xsk)
void cleanup_socket(xsk_socket_info_t *xsk)
{
// If the AF_XDP/XSK socket isn't NULL, delete it.
if (xsk->xsk != NULL)
Expand Down
44 changes: 29 additions & 15 deletions src/af_xdp.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
#pragma once

#include <linux/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <locale.h>

#include <net/if.h>

#include <sys/socket.h>
#include <linux/if_link.h>
#include <bpf.h>

#include <xsk.h>

#include <simple_types.h>

#include "cmd_line.h"

#define MAX_CPUS 256
Expand All @@ -11,42 +25,42 @@
#define INVALID_UMEM_FRAME UINT64_MAX
//#define DEBUG

struct xsk_umem_info
typedef struct xsk_umem_info
{
struct xsk_ring_prod fq;
struct xsk_ring_cons cq;
struct xsk_umem *umem;
void *buffer;
};
} xsk_umem_info_t;

struct xsk_socket
typedef struct xsk_socket
{
struct xsk_ring_cons *rx;
struct xsk_ring_prod *tx;
__u64 outstanding_tx;
u64 outstanding_tx;
struct xsk_ctx *ctx;
struct xsk_socket_config config;
int fd;
};
} xsk_socket_t;

struct xsk_socket_info
typedef struct xsk_socket_info
{
struct xsk_ring_cons rx;
struct xsk_ring_prod tx;
struct xsk_umem_info *umem;
struct xsk_socket *xsk;

__u64 umem_frame_addr[NUM_FRAMES];
__u32 umem_frame_free;
u64 umem_frame_addr[NUM_FRAMES];
u32 umem_frame_free;

__u32 outstanding_tx;
};
u32 outstanding_tx;
} xsk_socket_info_t;

int send_packet(struct xsk_socket_info *xsk, int thread_id, void *pckt, __u16 length, __u8 verbose);
__u64 get_umem_addr(struct xsk_socket_info *xsk, int idx);
void *get_umem_loc(struct xsk_socket_info *xsk, __u64 addr);
int send_packet(struct xsk_socket_info *xsk, int thread_id, void *pckt, u16 length, u8 verbose);
u64 get_umem_addr(struct xsk_socket_info *xsk, int idx);
void *get_umem_loc(struct xsk_socket_info *xsk, u64 addr);
void setup_af_xdp_variables(struct cmd_line_af_xdp *cmd_af_xdp, int verbose);
struct xsk_umem_info *setup_umem(int index);
struct xsk_socket_info *setup_socket(const char *dev, __u16 thread_id, int verbose);
struct xsk_socket_info *setup_socket(const char *dev, u16 thread_id, int verbose);
void cleanup_socket(struct xsk_socket_info *xsk);
int get_socket_fd(struct xsk_socket_info *xsk);
6 changes: 1 addition & 5 deletions src/cmd_line.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

#include "cmd_line.h"

static const struct option long_opts[] =
Expand All @@ -25,7 +21,7 @@ static const struct option long_opts[] =
*
* @return Void
**/
void parse_cmd_line_af_xdp(struct cmd_line_af_xdp *cmd_af_xdp, int argc, char **argv)
void parse_cmd_line_af_xdp(cmd_line_af_xdp_t *cmd_af_xdp, int argc, char **argv)
{
int c = -1;

Expand Down
8 changes: 6 additions & 2 deletions src/cmd_line.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#pragma once

struct cmd_line_af_xdp
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

typedef struct cmd_line_af_xdp
{
unsigned int queue_set : 1;
int queue;
Expand All @@ -11,6 +15,6 @@ struct cmd_line_af_xdp
unsigned int skb_mode : 1;
unsigned int zero_copy : 1;
unsigned int copy : 1;
};
} cmd_line_af_xdp_t;

void parse_cmd_line_af_xdp(struct cmd_line_af_xdp *cmd_af_xdp, int argc, char **argv);
18 changes: 1 addition & 17 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/types.h>
#include <getopt.h>
#include <errno.h>
#include <signal.h>

#include <utils.h>
#include <cmd_line.h>
#include <config.h>

#include "sequence.h"
#include "cmd_line.h"
#include "af_xdp.h"
#include "main.h"

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

// Attempt to parse config.
__u8 log = 1;
u8 log = 1;

if (cmd.cli)
{
Expand Down
17 changes: 16 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#pragma once

#define MAX_NAME_LEN 64
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <signal.h>

#include <utils.h>
#include <cmd_line.h>
#include <config.h>
#include <simple_types.h>

#include "sequence.h"
#include "cmd_line.h"
#include "af_xdp.h"

extern int errno;
Loading

0 comments on commit ae1adba

Please sign in to comment.