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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (c) 2021 Microsoft Corporation
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <rte_ethdev.h>
#include <rte_ether.h>
#include <rte_mbuf.h>
#include <rte_mempool.h>
#include <rte_net.h>
#include <rte_pcapng.h>
#include <pcap/pcap.h>
#include "test.h"
#define NUM_PACKETS 10
#define DUMMY_MBUF_NUM 3
static rte_pcapng_t *pcapng;
static struct rte_mempool *mp;
static const uint32_t pkt_len = 200;
static uint16_t port_id;
static char file_name[] = "/tmp/pcapng_test_XXXXXX.pcapng";
/* first mbuf in the packet, should always be at offset 0 */
struct dummy_mbuf {
struct rte_mbuf mb[DUMMY_MBUF_NUM];
uint8_t buf[DUMMY_MBUF_NUM][RTE_MBUF_DEFAULT_BUF_SIZE];
};
static void
dummy_mbuf_prep(struct rte_mbuf *mb, uint8_t buf[], uint32_t buf_len,
uint32_t data_len)
{
uint32_t i;
uint8_t *db;
mb->buf_addr = buf;
rte_mbuf_iova_set(mb, (uintptr_t)buf);
mb->buf_len = buf_len;
rte_mbuf_refcnt_set(mb, 1);
/* set pool pointer to dummy value, test doesn't use it */
mb->pool = (void *)buf;
rte_pktmbuf_reset(mb);
db = (uint8_t *)rte_pktmbuf_append(mb, data_len);
for (i = 0; i != data_len; i++)
db[i] = i;
}
/* Make an IP packet consisting of chain of one packets */
static void
mbuf1_prepare(struct dummy_mbuf *dm, uint32_t plen)
{
struct {
struct rte_ether_hdr eth;
struct rte_ipv4_hdr ip;
} pkt = {
.eth = {
.dst_addr.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
.ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4),
},
.ip = {
.version_ihl = RTE_IPV4_VHL_DEF,
.total_length = rte_cpu_to_be_16(plen),
.time_to_live = IPDEFTTL,
.next_proto_id = IPPROTO_RAW,
.src_addr = rte_cpu_to_be_32(RTE_IPV4_LOOPBACK),
.dst_addr = rte_cpu_to_be_32(RTE_IPV4_BROADCAST),
}
};
memset(dm, 0, sizeof(*dm));
dummy_mbuf_prep(&dm->mb[0], dm->buf[0], sizeof(dm->buf[0]), plen);
rte_eth_random_addr(pkt.eth.src_addr.addr_bytes);
memcpy(rte_pktmbuf_mtod(dm->mb, void *), &pkt, RTE_MIN(sizeof(pkt), plen));
/* Idea here is to create mbuf chain big enough that after mbuf deep copy they won't be
* compressed into single mbuf to properly test store of chained mbufs
*/
dummy_mbuf_prep(&dm->mb[1], dm->buf[1], sizeof(dm->buf[1]), pkt_len);
dummy_mbuf_prep(&dm->mb[2], dm->buf[2], sizeof(dm->buf[2]), pkt_len);
rte_pktmbuf_chain(&dm->mb[0], &dm->mb[1]);
rte_pktmbuf_chain(&dm->mb[0], &dm->mb[2]);
}
static int
test_setup(void)
{
int tmp_fd;
port_id = rte_eth_find_next(0);
if (port_id >= RTE_MAX_ETHPORTS) {
fprintf(stderr, "No valid Ether port\n");
return -1;
}
tmp_fd = mkstemps(file_name, strlen(".pcapng"));
if (tmp_fd == -1) {
perror("mkstemps() failure");
return -1;
}
printf("pcapng: output file %s\n", file_name);
/* open a test capture file */
pcapng = rte_pcapng_fdopen(tmp_fd, NULL, NULL, "pcapng_test", NULL);
if (pcapng == NULL) {
fprintf(stderr, "rte_pcapng_fdopen failed\n");
close(tmp_fd);
return -1;
}
/* Make a pool for cloned packets */
mp = rte_pktmbuf_pool_create_by_ops("pcapng_test_pool", IOV_MAX + NUM_PACKETS,
0, 0,
rte_pcapng_mbuf_size(pkt_len),
SOCKET_ID_ANY, "ring_mp_sc");
if (mp == NULL) {
fprintf(stderr, "Cannot create mempool\n");
return -1;
}
return 0;
}
static int
test_write_packets(void)
{
struct rte_mbuf *orig;
struct rte_mbuf *clones[NUM_PACKETS] = { };
struct dummy_mbuf mbfs;
unsigned int i;
ssize_t len;
/* make a dummy packet */
mbuf1_prepare(&mbfs, pkt_len);
/* clone them */
orig = &mbfs.mb[0];
for (i = 0; i < NUM_PACKETS; i++) {
struct rte_mbuf *mc;
mc = rte_pcapng_copy(port_id, 0, orig, mp, rte_pktmbuf_pkt_len(orig),
rte_get_tsc_cycles(), 0);
if (mc == NULL) {
fprintf(stderr, "Cannot copy packet\n");
return -1;
}
clones[i] = mc;
}
/* write it to capture file */
len = rte_pcapng_write_packets(pcapng, clones, NUM_PACKETS);
rte_pktmbuf_free_bulk(clones, NUM_PACKETS);
if (len <= 0) {
fprintf(stderr, "Write of packets failed\n");
return -1;
}
return 0;
}
static int
test_write_stats(void)
{
ssize_t len;
/* write a statistics block */
len = rte_pcapng_write_stats(pcapng, port_id,
NULL, 0, 0,
NUM_PACKETS, 0);
if (len <= 0) {
fprintf(stderr, "Write of statistics failed\n");
return -1;
}
return 0;
}
static void
pkt_print(u_char *user, const struct pcap_pkthdr *h,
const u_char *bytes)
{
unsigned int *countp = (unsigned int *)user;
const struct rte_ether_hdr *eh;
struct tm *tm;
char tbuf[128], src[64], dst[64];
tm = localtime(&h->ts.tv_sec);
if (tm == NULL) {
perror("localtime");
return;
}
if (strftime(tbuf, sizeof(tbuf), "%X", tm) == 0) {
fprintf(stderr, "strftime returned 0!\n");
return;
}
eh = (const struct rte_ether_hdr *)bytes;
rte_ether_format_addr(dst, sizeof(dst), &eh->dst_addr);
rte_ether_format_addr(src, sizeof(src), &eh->src_addr);
printf("%s.%06lu: %s -> %s type %x length %u\n",
tbuf, (unsigned long)h->ts.tv_usec,
src, dst, rte_be_to_cpu_16(eh->ether_type), h->len);
*countp += 1;
}
/*
* Open the resulting pcapng file with libpcap
* Would be better to use capinfos from wireshark
* but that creates an unwanted dependency.
*/
static int
test_validate(void)
{
char errbuf[PCAP_ERRBUF_SIZE];
unsigned int count = 0;
pcap_t *pcap;
int ret;
pcap = pcap_open_offline(file_name, errbuf);
if (pcap == NULL) {
fprintf(stderr, "pcap_open_offline('%s') failed: %s\n",
file_name, errbuf);
return -1;
}
ret = pcap_loop(pcap, 0, pkt_print, (u_char *)&count);
if (ret == 0)
printf("Saw %u packets\n", count);
else
fprintf(stderr, "pcap_dispatch: failed: %s\n",
pcap_geterr(pcap));
pcap_close(pcap);
return ret;
}
static int
test_write_over_limit_iov_max(void)
{
struct rte_mbuf *orig;
struct rte_mbuf *clones[IOV_MAX + NUM_PACKETS] = { };
struct dummy_mbuf mbfs;
unsigned int i;
ssize_t len;
/* make a dummy packet */
mbuf1_prepare(&mbfs, pkt_len);
/* clone them */
orig = &mbfs.mb[0];
for (i = 0; i < IOV_MAX + NUM_PACKETS; i++) {
struct rte_mbuf *mc;
mc = rte_pcapng_copy(port_id, 0, orig, mp, pkt_len,
rte_get_tsc_cycles(), 0);
if (mc == NULL) {
fprintf(stderr, "Cannot copy packet\n");
return -1;
}
clones[i] = mc;
}
/* write it to capture file */
len = rte_pcapng_write_packets(pcapng, clones, IOV_MAX + NUM_PACKETS);
rte_pktmbuf_free_bulk(clones, IOV_MAX + NUM_PACKETS);
if (len <= 0) {
fprintf(stderr, "Write of packets failed\n");
return -1;
}
return 0;
}
static void
test_cleanup(void)
{
rte_mempool_free(mp);
if (pcapng)
rte_pcapng_close(pcapng);
}
static struct
unit_test_suite test_pcapng_suite = {
.setup = test_setup,
.teardown = test_cleanup,
.suite_name = "Test Pcapng Unit Test Suite",
.unit_test_cases = {
TEST_CASE(test_write_packets),
TEST_CASE(test_write_stats),
TEST_CASE(test_validate),
TEST_CASE(test_write_over_limit_iov_max),
TEST_CASES_END()
}
};
static int
test_pcapng(void)
{
return unit_test_suite_runner(&test_pcapng_suite);
}
REGISTER_TEST_COMMAND(pcapng_autotest, test_pcapng);
|