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
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright 2021 6WIND S.A.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <rte_net.h>
#include <rte_mbuf.h>
#include <rte_ip.h>
#include "test.h"
#define MEMPOOL_CACHE_SIZE 0
#define MBUF_DATA_SIZE 256
#define NB_MBUF 128
/*
* Test L3/L4 checksum API.
*/
#define GOTO_FAIL(str, ...) do { \
printf("cksum test FAILED (l.%d): <" str ">\n", \
__LINE__, ##__VA_ARGS__); \
goto fail; \
} while (0)
/* generated in scapy with Ether()/IP()/TCP())) */
static const char test_cksum_ipv4_tcp[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x45, 0x00,
0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
0x7c, 0xcd, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00,
0x00, 0x01, 0x00, 0x14, 0x00, 0x50, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
0x20, 0x00, 0x91, 0x7c, 0x00, 0x00,
};
/* generated in scapy with Ether()/IPv6()/TCP()) */
static const char test_cksum_ipv6_tcp[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00, 0x14, 0x06, 0x40, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x14,
0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x50, 0x02, 0x20, 0x00, 0x8f, 0x7d,
0x00, 0x00,
};
/* generated in scapy with Ether()/IP()/UDP()/Raw('x')) */
static const char test_cksum_ipv4_udp[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x45, 0x00,
0x00, 0x1d, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
0x7c, 0xcd, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00,
0x00, 0x01, 0x00, 0x35, 0x00, 0x35, 0x00, 0x09,
0x89, 0x6f, 0x78,
};
/* generated in scapy with Ether()/IPv6()/UDP()/Raw('x')) */
static const char test_cksum_ipv6_udp[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x86, 0xdd, 0x60, 0x00,
0x00, 0x00, 0x00, 0x09, 0x11, 0x40, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x35,
0x00, 0x35, 0x00, 0x09, 0x87, 0x70, 0x78,
};
/* generated in scapy with Ether()/IP(options='\x00')/UDP()/Raw('x')) */
static const char test_cksum_ipv4_opts_udp[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x46, 0x00,
0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
0x7b, 0xc9, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x35,
0x00, 0x35, 0x00, 0x09, 0x89, 0x6f, 0x78,
};
/* test l3/l4 checksum api */
static int
test_l4_cksum(struct rte_mempool *pktmbuf_pool, const char *pktdata, size_t len)
{
struct rte_net_hdr_lens hdr_lens;
struct rte_mbuf *m = NULL;
uint32_t packet_type;
uint16_t prev_cksum;
void *l3_hdr;
void *l4_hdr;
uint32_t l3;
uint32_t l4;
char *data;
m = rte_pktmbuf_alloc(pktmbuf_pool);
if (m == NULL)
GOTO_FAIL("Cannot allocate mbuf");
data = rte_pktmbuf_append(m, len);
if (data == NULL)
GOTO_FAIL("Cannot append data");
memcpy(data, pktdata, len);
packet_type = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
l3 = packet_type & RTE_PTYPE_L3_MASK;
l4 = packet_type & RTE_PTYPE_L4_MASK;
l3_hdr = rte_pktmbuf_mtod_offset(m, void *, hdr_lens.l2_len);
l4_hdr = rte_pktmbuf_mtod_offset(m, void *,
hdr_lens.l2_len + hdr_lens.l3_len);
if (l3 == RTE_PTYPE_L3_IPV4 || l3 == RTE_PTYPE_L3_IPV4_EXT) {
struct rte_ipv4_hdr *ip = l3_hdr;
/* verify IPv4 checksum */
if (rte_ipv4_cksum(l3_hdr) != 0)
GOTO_FAIL("invalid IPv4 checksum verification");
/* verify bad IPv4 checksum */
ip->hdr_checksum++;
if (rte_ipv4_cksum(l3_hdr) == 0)
GOTO_FAIL("invalid IPv4 bad checksum verification");
ip->hdr_checksum--;
/* recalculate IPv4 checksum */
prev_cksum = ip->hdr_checksum;
ip->hdr_checksum = 0;
ip->hdr_checksum = rte_ipv4_cksum(ip);
if (ip->hdr_checksum != prev_cksum)
GOTO_FAIL("invalid IPv4 checksum calculation");
/* verify L4 checksum */
if (rte_ipv4_udptcp_cksum_verify(l3_hdr, l4_hdr) != 0)
GOTO_FAIL("invalid L4 checksum verification");
if (l4 == RTE_PTYPE_L4_TCP) {
struct rte_tcp_hdr *tcp = l4_hdr;
/* verify bad TCP checksum */
tcp->cksum++;
if (rte_ipv4_udptcp_cksum_verify(l3_hdr, l4_hdr) == 0)
GOTO_FAIL("invalid bad TCP checksum verification");
tcp->cksum--;
/* recalculate TCP checksum */
prev_cksum = tcp->cksum;
tcp->cksum = 0;
tcp->cksum = rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
if (tcp->cksum != prev_cksum)
GOTO_FAIL("invalid TCP checksum calculation");
} else if (l4 == RTE_PTYPE_L4_UDP) {
struct rte_udp_hdr *udp = l4_hdr;
/* verify bad UDP checksum */
udp->dgram_cksum++;
if (rte_ipv4_udptcp_cksum_verify(l3_hdr, l4_hdr) == 0)
GOTO_FAIL("invalid bad UDP checksum verification");
udp->dgram_cksum--;
/* recalculate UDP checksum */
prev_cksum = udp->dgram_cksum;
udp->dgram_cksum = 0;
udp->dgram_cksum = rte_ipv4_udptcp_cksum(l3_hdr,
l4_hdr);
if (udp->dgram_cksum != prev_cksum)
GOTO_FAIL("invalid TCP checksum calculation");
}
} else if (l3 == RTE_PTYPE_L3_IPV6 || l3 == RTE_PTYPE_L3_IPV6_EXT) {
if (rte_ipv6_udptcp_cksum_verify(l3_hdr, l4_hdr) != 0)
GOTO_FAIL("invalid L4 checksum verification");
if (l4 == RTE_PTYPE_L4_TCP) {
struct rte_tcp_hdr *tcp = l4_hdr;
/* verify bad TCP checksum */
tcp->cksum++;
if (rte_ipv6_udptcp_cksum_verify(l3_hdr, l4_hdr) == 0)
GOTO_FAIL("invalid bad TCP checksum verification");
tcp->cksum--;
/* recalculate TCP checksum */
prev_cksum = tcp->cksum;
tcp->cksum = 0;
tcp->cksum = rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
if (tcp->cksum != prev_cksum)
GOTO_FAIL("invalid TCP checksum calculation");
} else if (l4 == RTE_PTYPE_L4_UDP) {
struct rte_udp_hdr *udp = l4_hdr;
/* verify bad UDP checksum */
udp->dgram_cksum++;
if (rte_ipv6_udptcp_cksum_verify(l3_hdr, l4_hdr) == 0)
GOTO_FAIL("invalid bad UDP checksum verification");
udp->dgram_cksum--;
/* recalculate UDP checksum */
prev_cksum = udp->dgram_cksum;
udp->dgram_cksum = 0;
udp->dgram_cksum = rte_ipv6_udptcp_cksum(l3_hdr,
l4_hdr);
if (udp->dgram_cksum != prev_cksum)
GOTO_FAIL("invalid TCP checksum calculation");
}
}
rte_pktmbuf_free(m);
return 0;
fail:
rte_pktmbuf_free(m);
return -1;
}
static int
test_cksum(void)
{
struct rte_mempool *pktmbuf_pool = NULL;
/* create pktmbuf pool if it does not exist */
pktmbuf_pool = rte_pktmbuf_pool_create("test_cksum_mbuf_pool",
NB_MBUF, MEMPOOL_CACHE_SIZE, 0, MBUF_DATA_SIZE,
SOCKET_ID_ANY);
if (pktmbuf_pool == NULL)
GOTO_FAIL("cannot allocate mbuf pool");
if (test_l4_cksum(pktmbuf_pool, test_cksum_ipv4_tcp,
sizeof(test_cksum_ipv4_tcp)) < 0)
GOTO_FAIL("checksum error on ipv4_tcp");
if (test_l4_cksum(pktmbuf_pool, test_cksum_ipv6_tcp,
sizeof(test_cksum_ipv6_tcp)) < 0)
GOTO_FAIL("checksum error on ipv6_tcp");
if (test_l4_cksum(pktmbuf_pool, test_cksum_ipv4_udp,
sizeof(test_cksum_ipv4_udp)) < 0)
GOTO_FAIL("checksum error on ipv4_udp");
if (test_l4_cksum(pktmbuf_pool, test_cksum_ipv6_udp,
sizeof(test_cksum_ipv6_udp)) < 0)
GOTO_FAIL("checksum error on ipv6_udp");
if (test_l4_cksum(pktmbuf_pool, test_cksum_ipv4_opts_udp,
sizeof(test_cksum_ipv4_opts_udp)) < 0)
GOTO_FAIL("checksum error on ipv4_opts_udp");
rte_mempool_free(pktmbuf_pool);
return 0;
fail:
rte_mempool_free(pktmbuf_pool);
return -1;
}
#undef GOTO_FAIL
REGISTER_FAST_TEST(cksum_autotest, true, true, test_cksum);
|