Skip to content

Commit 4ee53b1

Browse files
committed
AF_XDP-interaction: Add cmdline options for specifying IPs
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
1 parent 9eefd4b commit 4ee53b1

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

AF_XDP-interaction/af_xdp_user.c

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ static const struct option_wrapper long_options[] = {
291291
{{"progsec", required_argument, NULL, 2 },
292292
"Load program in <section> of the ELF file", "<section>"},
293293

294+
{{"src-ip", required_argument, NULL, 4 },
295+
"Change IPv4 source address in generated packets", "<ip>"},
296+
297+
{{"dst-ip", required_argument, NULL, 5 },
298+
"Change IPv4 destination address in generated packets", "<ip>"},
299+
294300
{{"busy-poll", no_argument, NULL, 'B' },
295301
"Enable socket prefer NAPI busy-poll mode (remember adjust sysctl too)"},
296302

@@ -637,32 +643,17 @@ static void gen_eth_hdr(struct config *cfg, struct ethhdr *eth_hdr)
637643
eth_hdr->h_proto = htons(ETH_P_IP);
638644
}
639645

640-
static bool get_ipv4_u32(char *ip_str, uint32_t *ip_addr)
641-
{
642-
int res;
643-
644-
res = inet_pton(AF_INET, ip_str, ip_addr);
645-
if (res <= 0) {
646-
if (res == 0)
647-
fprintf(stderr, "ERROR: IP%s \"%s\" not in presentation format\n",
648-
"v4", ip_str);
649-
else
650-
perror("inet_pton");
651-
return false;
652-
}
653-
return true;
654-
}
655646

656647
static char *opt_ip_str_src = "192.168.44.1";
657648
static char *opt_ip_str_dst = "192.168.44.3";
658649

659-
static void gen_ip_hdr(struct iphdr *ip_hdr)
650+
static void gen_ip_hdr(struct config *cfg, struct iphdr *ip_hdr)
660651
{
661-
uint32_t saddr;
662-
uint32_t daddr;
652+
if (cfg->opt_ip_src == 0)
653+
get_ipv4_u32(opt_ip_str_src, &cfg->opt_ip_src);
663654

664-
get_ipv4_u32(opt_ip_str_src, &saddr);
665-
get_ipv4_u32(opt_ip_str_dst, &daddr);
655+
if (cfg->opt_ip_dst == 0)
656+
get_ipv4_u32(opt_ip_str_dst, &cfg->opt_ip_dst);
666657

667658
/* IP header */
668659
ip_hdr->version = IPVERSION;
@@ -673,8 +664,8 @@ static void gen_ip_hdr(struct iphdr *ip_hdr)
673664
ip_hdr->frag_off = 0;
674665
ip_hdr->ttl = IPDEFTTL;
675666
ip_hdr->protocol = IPPROTO_UDP;
676-
ip_hdr->saddr = saddr;
677-
ip_hdr->daddr = daddr;
667+
ip_hdr->saddr = cfg->opt_ip_src;
668+
ip_hdr->daddr = cfg->opt_ip_dst;
678669

679670
/* IP header checksum */
680671
ip_hdr->check = 0;
@@ -711,7 +702,7 @@ static void gen_base_pkt(struct config *cfg, uint8_t *pkt_ptr)
711702
sizeof(struct iphdr));
712703

713704
gen_eth_hdr(cfg, eth_hdr);
714-
gen_ip_hdr(ip_hdr);
705+
gen_ip_hdr(cfg, ip_hdr);
715706
gen_udp_hdr(udp_hdr, ip_hdr);
716707
}
717708

AF_XDP-interaction/common_defines.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ struct config {
3232
struct ether_addr opt_tx_dmac;
3333
__u64 interval;
3434
__u32 batch_pkts;
35+
__u32 opt_ip_src;
36+
__u32 opt_ip_dst;
3537
};
3638

3739
#define BATCH_PKTS_MAX 64

AF_XDP-interaction/common_params.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <linux/if_xdp.h>
1212
#include <sched.h>
1313

14+
#include <arpa/inet.h> /* inet_pton */
15+
1416
#include "common_params.h"
1517

1618
int verbose = 1;
@@ -62,6 +64,23 @@ void usage(const char *prog_name, const char *doc,
6264
printf("\n");
6365
}
6466

67+
/* Helper for convert IPv4 address from text to binary form */
68+
bool get_ipv4_u32(char *ip_str, uint32_t *ip_addr)
69+
{
70+
int res;
71+
72+
res = inet_pton(AF_INET, ip_str, ip_addr);
73+
if (res <= 0) {
74+
if (res == 0)
75+
fprintf(stderr, "ERROR: IP%s \"%s\" not in presentation format\n",
76+
"v4", ip_str);
77+
else
78+
perror("inet_pton");
79+
return false;
80+
}
81+
return true;
82+
}
83+
6584
int option_wrappers_to_options(const struct option_wrapper *wrapper,
6685
struct option **options)
6786
{
@@ -87,6 +106,7 @@ void parse_cmdline_args(int argc, char **argv,
87106
{
88107
struct option *long_options;
89108
bool full_help = false;
109+
uint32_t ipv4_tmp = 0;
90110
int longindex = 0;
91111
char *dest;
92112
int opt;
@@ -221,6 +241,16 @@ void parse_cmdline_args(int argc, char **argv,
221241
dest = (char *)&cfg->progsec;
222242
strncpy(dest, optarg, sizeof(cfg->progsec));
223243
break;
244+
case 4: /* --src-ip */
245+
if (!get_ipv4_u32(optarg, &ipv4_tmp))
246+
goto error;
247+
cfg->opt_ip_src = ipv4_tmp;
248+
break;
249+
case 5: /* --dst-ip */
250+
if (!get_ipv4_u32(optarg, &ipv4_tmp))
251+
goto error;
252+
cfg->opt_ip_dst = ipv4_tmp;
253+
break;
224254
case 'L': /* --src-mac */
225255
dest = (char *)&cfg->src_mac;
226256
strncpy(dest, optarg, sizeof(cfg->src_mac));

AF_XDP-interaction/common_params.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ void parse_cmdline_args(int argc, char **argv,
1919
const struct option_wrapper *long_options,
2020
struct config *cfg, const char *doc);
2121

22+
bool get_ipv4_u32(char *ip_str, uint32_t *ip_addr);
23+
2224
#endif /* __COMMON_PARAMS_H */

0 commit comments

Comments
 (0)