mirror of
https://github.com/xdp-project/bpf-examples.git
synced 2024-05-06 15:54:53 +00:00
AF_XDP-interaction: Add --pktinfo option that print info on pkt
To verify the contents of the incomming packets add a function print_pkt_info() that decode part of the packet headers and print IP-header src+dst (both IPv4 and IPv6). Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
#include <net/if.h>
|
||||
#include <linux/if_link.h>
|
||||
#include <linux/if_ether.h>
|
||||
#include <linux/ip.h>
|
||||
#include <linux/ipv6.h>
|
||||
#include <linux/icmpv6.h>
|
||||
|
||||
@@ -112,6 +113,9 @@ static const struct option_wrapper long_options[] = {
|
||||
{{"quiet", no_argument, NULL, 'q' },
|
||||
"Quiet mode (no output)"},
|
||||
|
||||
{{"pktinfo", no_argument, NULL, 'P' },
|
||||
"Print packet info output mode (debug)"},
|
||||
|
||||
{{"filename", required_argument, NULL, 1 },
|
||||
"Load program from <file>", "<file>"},
|
||||
|
||||
@@ -290,11 +294,42 @@ static inline void csum_replace2(__sum16 *sum, __be16 old, __be16 new)
|
||||
*sum = ~csum16_add(csum16_sub(~(*sum), old), new);
|
||||
}
|
||||
|
||||
/* As debug tool print some info about packet */
|
||||
static void print_pkt_info(uint8_t *pkt, uint32_t len)
|
||||
{
|
||||
struct ethhdr *eth = (struct ethhdr *) pkt;
|
||||
__u16 proto = ntohs(eth->h_proto);
|
||||
|
||||
char *fmt = "DEBUG-pkt len=%04d Eth-proto:0x%X %s "
|
||||
"src:%s -> dst:%s\n";
|
||||
char src_str[128] = { 0 };
|
||||
char dst_str[128] = { 0 };
|
||||
|
||||
if (proto == ETH_P_IP) {
|
||||
struct iphdr *ipv4 = (struct iphdr *) (eth + 1);
|
||||
inet_ntop(AF_INET, &ipv4->saddr, src_str, sizeof(src_str));
|
||||
inet_ntop(AF_INET, &ipv4->daddr, dst_str, sizeof(dst_str));
|
||||
printf(fmt, len, proto, "IPv4", src_str, dst_str);
|
||||
} else if (proto == ETH_P_ARP) {
|
||||
printf(fmt, len, proto, "ARP", "", "");
|
||||
} else if (proto == ETH_P_IPV6) {
|
||||
struct ipv6hdr *ipv6 = (struct ipv6hdr *) (eth + 1);
|
||||
inet_ntop(AF_INET6, &ipv6->saddr, src_str, sizeof(src_str));
|
||||
inet_ntop(AF_INET6, &ipv6->daddr, dst_str, sizeof(dst_str));
|
||||
printf(fmt, len, proto, "IPv6", src_str, dst_str);
|
||||
} else {
|
||||
printf(fmt, len, proto, "Unknown", "", "");
|
||||
}
|
||||
}
|
||||
|
||||
static bool process_packet(struct xsk_socket_info *xsk,
|
||||
uint64_t addr, uint32_t len)
|
||||
{
|
||||
uint8_t *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
|
||||
|
||||
if (debug_pkt)
|
||||
print_pkt_info(pkt, len);
|
||||
|
||||
/* Lesson#3: Write an IPv6 ICMP ECHO parser to send responses
|
||||
*
|
||||
* Some assumptions to make it easier:
|
||||
|
@@ -27,6 +27,7 @@ struct config {
|
||||
|
||||
/* Defined in common_params.o */
|
||||
extern int verbose;
|
||||
extern int debug_pkt;
|
||||
|
||||
/* Exit return codes */
|
||||
#define EXIT_OK 0 /* == EXIT_SUCCESS (stdlib.h) man exit(3) */
|
||||
|
@@ -13,6 +13,7 @@
|
||||
#include "common_params.h"
|
||||
|
||||
int verbose = 1;
|
||||
int debug_pkt = 0;
|
||||
|
||||
#define BUFSIZE 30
|
||||
|
||||
@@ -157,6 +158,9 @@ void parse_cmdline_args(int argc, char **argv,
|
||||
case 'q':
|
||||
verbose = false;
|
||||
break;
|
||||
case 'P':
|
||||
debug_pkt = true;
|
||||
break;
|
||||
case 'Q':
|
||||
cfg->xsk_if_queue = atoi(optarg);
|
||||
break;
|
||||
|
Reference in New Issue
Block a user