2 * Copyright (c) 2021 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <vlib/vlib.h>
19 #include <vnet/feature/feature.h>
20 #include <vppinfra/clib_error.h>
21 #include <vnet/ip/ip4_packet.h>
22 #include <vnet/udp/udp.h>
23 #include <vppinfra/bihash_16_8.h>
24 #include <vppinfra/bihash_template.c>
25 #include <vnet/fib/ip4_fib.h>
27 #include <pnat/pnat.api_enum.h> /* For error counters */
28 #include <arpa/inet.h>
29 #include "pnat_test_stubs.h"
32 ** Buffer management in test setup
33 ** Allocate buffers return vector of buffer indicies.
35 ** Setup frame with buffers when calling function.
36 ** Global vector of all buffers with their indicies?
37 ** Convert buffer index to pointer?
42 struct buffers buffers[256];
43 struct buffers expected[256];
44 u32 *buffers_vector = 0;
46 static u32 *buffer_init(u32 *vector, int count) {
48 for (i = 0; i < count; i++) {
53 #define PNAT_TEST_DEBUG 0
55 u32 *results_bi = 0; /* global vector of result buffers */
56 u16 *results_next = 0;
57 vlib_node_runtime_t *node;
59 #define log_info(M, ...) \
60 fprintf(stderr, "\033[32;1m[OK] " M "\033[0m\n", ##__VA_ARGS__)
61 #define log_error(M, ...) \
62 fprintf(stderr, "\033[31;1m[ERROR] (%s:%d:) " M "\033[0m\n", __FILE__, \
63 __LINE__, ##__VA_ARGS__)
64 #define test_assert(A, M, ...) \
66 log_error(M, ##__VA_ARGS__); \
69 log_info(M, ##__VA_ARGS__); \
73 * Always return the frame of generated packets
75 #define vlib_frame_vector_args test_vlib_frame_vector_args
76 void *test_vlib_frame_vector_args(vlib_frame_t *f) { return buffers_vector; }
78 /* Synthetic value for vnet_feature_next */
79 #define NEXT_PASSTHROUGH 4242
81 #define vnet_feature_next_u16 test_vnet_feature_next_u16
82 void vnet_feature_next_u16(u16 *next0, vlib_buffer_t *b0) {
83 *next0 = NEXT_PASSTHROUGH;
86 /* Gather output packets */
87 #define vlib_buffer_enqueue_to_next test_vlib_buffer_enqueue_to_next
88 void test_vlib_buffer_enqueue_to_next(vlib_main_t *vm,
89 vlib_node_runtime_t *node, u32 *buffers,
90 u16 *nexts, uword count) {
91 vec_add(results_next, nexts, count);
92 vec_add(results_bi, buffers, count);
95 pnat_trace_t trace = {0};
96 #define vlib_add_trace test_vlib_add_trace
97 void *test_vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r,
98 vlib_buffer_t *b, u32 n_data_bytes) {
102 #define vlib_get_buffers test_vlib_get_buffers
103 void test_vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b,
106 for (i = 0; i < count; i++) {
107 b[i] = (vlib_buffer_t *)&buffers[bi[i]];
111 vlib_buffer_t *test_vlib_get_buffer(u32 bi) {
112 return (vlib_buffer_t *)&buffers[bi];
115 /* Must be included here to allow the above functions to override */
116 #include "pnat_node.h"
131 test_5tuple_t expect;
132 u32 expect_next_index;
137 .name = "da rewritten",
138 .send = {"1.1.1.1", "2.2.2.2", 17, 80, 6871},
139 .expect = {"1.1.1.1", "1.2.3.4", 17, 80, 6871},
140 .expect_next_index = NEXT_PASSTHROUGH,
144 .send = {"1.1.1.1", "2.2.2.2", 17, 80, 8080},
145 .expect = {"1.1.1.1", "2.2.2.2", 17, 80, 8080},
146 .expect_next_index = NEXT_PASSTHROUGH,
150 .send = {"1.1.1.1", "2.2.2.2", 6, 80, 6871},
151 .expect = {"1.1.1.1", "1.2.3.4", 6, 80, 6871},
152 .expect_next_index = NEXT_PASSTHROUGH,
155 .name = "tcp da ports",
156 .send = {"1.1.1.1", "2.2.2.2", 6, 80, 6872},
157 .expect = {"1.1.1.1", "1.2.3.4", 6, 53, 8000},
158 .expect_next_index = NEXT_PASSTHROUGH,
165 test_5tuple_t rewrite;
172 .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6871},
173 .rewrite = {.dst = "1.2.3.4"},
177 .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6871},
178 .rewrite = {.dst = "1.2.3.4"},
182 .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6872},
183 .rewrite = {.dst = "1.2.3.4", .sport = 53, .dport = 8000},
187 .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6873},
188 .rewrite = {.dst = "1.2.3.4", .sport = 53, .dport = 8000},
193 static int fill_packets(vlib_main_t *vm, vlib_buffer_t *b,
194 test_5tuple_t *test) {
195 b->flags |= VLIB_BUFFER_IS_TRACED;
197 ip4_header_t *ip = (ip4_header_t *)vlib_buffer_get_current(b);
198 memset(ip, 0, sizeof(*ip));
199 ip->ip_version_and_header_length = 0x45;
201 inet_pton(AF_INET, test->src, &ip->src_address.as_u32);
202 inet_pton(AF_INET, test->dst, &ip->dst_address.as_u32);
203 ip->protocol = test->proto;
205 if (test->proto == IP_PROTOCOL_UDP) {
206 udp_header_t *udp = ip4_next_header(ip);
207 memset(udp, 0, sizeof(*udp));
208 udp->dst_port = htons(test->dport);
209 udp->src_port = htons(test->sport);
210 udp->length = htons(8);
211 vnet_buffer(b)->ip.reass.l4_src_port = udp->src_port;
212 vnet_buffer(b)->ip.reass.l4_dst_port = udp->dst_port;
213 b->current_length = 28;
214 ip->length = htons(b->current_length);
215 ip->checksum = ip4_header_checksum(ip);
216 udp->checksum = ip4_tcp_udp_compute_checksum(vm, b, ip);
217 } else if (test->proto == IP_PROTOCOL_TCP) {
218 tcp_header_t *tcp = ip4_next_header(ip);
219 memset(tcp, 0, sizeof(*tcp));
220 tcp->dst_port = htons(test->dport);
221 tcp->src_port = htons(test->sport);
222 vnet_buffer(b)->ip.reass.l4_src_port = tcp->src_port;
223 vnet_buffer(b)->ip.reass.l4_dst_port = tcp->dst_port;
224 b->current_length = sizeof(ip4_header_t) + sizeof(tcp_header_t);
225 ip->length = htons(b->current_length);
226 ip->checksum = ip4_header_checksum(ip);
227 tcp->checksum = ip4_tcp_udp_compute_checksum(vm, b, ip);
229 b->current_length = sizeof(ip4_header_t);
230 ip->length = htons(b->current_length);
231 ip->checksum = ip4_header_checksum(ip);
232 vnet_buffer(b)->ip.reass.l4_src_port = 0;
233 vnet_buffer(b)->ip.reass.l4_dst_port = 0;
239 static void ruleto5tuple(test_5tuple_t *r, pnat_5tuple_t *t) {
241 inet_pton(AF_INET, r->src, &t->src);
245 inet_pton(AF_INET, r->dst, &t->dst);
250 t->mask |= PNAT_DPORT;
254 t->mask |= PNAT_SPORT;
259 static void add_translation(rule_t *r) {
260 pnat_5tuple_t match = {0};
261 pnat_5tuple_t rewrite = {0};
263 ruleto5tuple(&r->match, &match);
264 ruleto5tuple(&r->rewrite, &rewrite);
266 int rv = pnat_binding_add(&match, &rewrite, &r->index);
269 rv = pnat_binding_attach(0, PNAT_IP4_INPUT, r->index);
273 static void del_translation(rule_t *r) {
274 int rv = pnat_binding_detach(0, PNAT_IP4_INPUT, r->index);
277 rv = pnat_binding_del(r->index);
281 static void validate_packet(vlib_main_t *vm, char *name, u32 bi,
282 vlib_buffer_t *expected_b) {
283 vlib_buffer_t *b = test_vlib_get_buffer(bi);
286 ip4_header_t *ip = (ip4_header_t *)vlib_buffer_get_current(b);
287 ip4_header_t *expected_ip =
288 (ip4_header_t *)vlib_buffer_get_current(expected_b);
291 clib_warning("Received packet: %U", format_ip4_header, ip, 20);
292 clib_warning("Expected packet: %U", format_ip4_header, expected_ip, 20);
293 tcp_header_t *tcp = ip4_next_header(ip);
294 clib_warning("IP: %U TCP: %U", format_ip4_header, ip, sizeof(*ip),
295 format_tcp_header, tcp, sizeof(*tcp));
296 tcp = ip4_next_header(expected_ip);
297 clib_warning("IP: %U TCP: %U", format_ip4_header, expected_ip, sizeof(*ip),
298 format_tcp_header, tcp, sizeof(*tcp));
301 u32 flags = ip4_tcp_udp_validate_checksum(vm, b);
302 assert((flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0);
303 flags = ip4_tcp_udp_validate_checksum(vm, expected_b);
304 assert((flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0);
305 assert(b->current_length == expected_b->current_length);
307 test_assert(memcmp(ip, expected_ip, b->current_length) == 0, "%s", name);
310 extern vlib_node_registration_t pnat_input_node;
312 static void test_table(test_t *t, int no_tests) {
313 // walk through table of tests
315 vlib_main_t *vm = &vlib_global_main;
317 /* Generate packet data */
318 for (i = 0; i < no_tests; i++) {
319 // create input buffer(s)
320 fill_packets(vm, (vlib_buffer_t *)&buffers[i], &t[i].send);
321 fill_packets(vm, (vlib_buffer_t *)&expected[i], &t[i].expect);
324 /* send packets through graph node */
325 vlib_frame_t frame = {.n_vectors = no_tests};
326 node->flags |= VLIB_NODE_FLAG_TRACE;
328 pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
331 for (i = 0; i < no_tests; i++) {
332 assert(t[i].expect_next_index == results_next[i]);
333 validate_packet(vm, t[i].name, results_bi[i],
334 (vlib_buffer_t *)&expected[i]);
336 vec_free(results_next);
337 vec_free(results_bi);
340 static void test_performance(void) {
341 pnat_main_t *pm = &pnat_main;
343 vlib_main_t *vm = &vlib_global_main;
345 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
346 add_translation(&rules[i]);
348 assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
350 int no_tests = sizeof(tests) / sizeof(tests[0]);
351 /* Generate packet data */
352 for (i = 0; i < VLIB_FRAME_SIZE; i++) {
353 // create input buffer(s)
354 fill_packets(vm, (vlib_buffer_t *)&buffers[i],
355 &tests[i % no_tests].send);
356 // fill_packets(vm, (vlib_buffer_t *)&expected[i], &tests[i %
357 // no_tests].expect);
360 /* send packets through graph node */
361 vlib_frame_t frame = {.n_vectors = VLIB_FRAME_SIZE};
362 node->flags &= ~VLIB_NODE_FLAG_TRACE;
365 for (j = 0; j < 10000; j++) {
366 pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
369 for (i = 0; i < VLIB_FRAME_SIZE; i++) {
370 assert(tests[i % no_tests].expect_next_index == results_next[i]);
371 validate_packet(vm, tests[i % no_tests].name, results_bi[i], (vlib_buffer_t *)&expected[i]);
374 vec_free(results_next);
375 vec_free(results_bi);
378 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
379 del_translation(&rules[i]);
381 assert(pool_elts(pm->translations) == 0);
382 assert(pool_elts(pm->interfaces) == 0);
385 static void test_packets(void) {
386 pnat_main_t *pm = &pnat_main;
388 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
389 add_translation(&rules[i]);
391 assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
393 test_table(tests, sizeof(tests) / sizeof(tests[0]));
395 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
396 del_translation(&rules[i]);
398 assert(pool_elts(pm->translations) == 0);
399 assert(pool_elts(pm->interfaces) == 0);
401 static void test_attach(void) {
402 pnat_attachment_point_t attachment = PNAT_IP4_INPUT;
403 u32 binding_index = 0;
405 int rv = pnat_binding_attach(sw_if_index, attachment, binding_index);
406 test_assert(rv == -1, "binding_attach - nothing to attach");
408 rv = pnat_binding_detach(sw_if_index, attachment, 1234);
409 test_assert(rv == -1, "binding_detach - nothing to detach");
411 pnat_5tuple_t match = {.mask = PNAT_SA};
412 pnat_5tuple_t rewrite = {.mask = PNAT_SA};
413 rv = pnat_binding_add(&match, &rewrite, &binding_index);
416 rv = pnat_binding_attach(sw_if_index, attachment, binding_index);
417 test_assert(rv == 0, "binding_attach - rule");
419 rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
420 test_assert(rv == 0, "binding_detach - rule");
422 rv = pnat_binding_del(binding_index);
426 static void test_del_before_detach(void) {
427 pnat_attachment_point_t attachment = PNAT_IP4_INPUT;
428 u32 binding_index = 0;
431 /* Ensure 5-tuple here will not duplicate with other tests cause this will
432 * not be removed from flow cache */
434 .match = {.dst = "123.123.123.123", .proto = 17, .dport = 6871},
435 .rewrite = {.dst = "1.2.3.4"},
439 add_translation(&rule);
441 int rv = pnat_binding_del(binding_index);
445 .name = "hit missing rule",
446 .send = {"1.1.1.1", "123.123.123.123", 17, 80, 6871},
447 .expect = {"1.1.1.1", "123.123.123.123", 17, 80, 6871},
448 .expect_next_index = PNAT_NEXT_DROP,
451 test_table(&test, 1);
453 /* For now if you have deleted before detach, can't find key */
454 rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
455 test_assert(rv == -1, "binding_detach - failure");
457 /* Re-add the rule and try again */
458 pnat_5tuple_t match = {0};
459 pnat_5tuple_t rewrite = {0};
460 ruleto5tuple(&rule.match, &match);
461 ruleto5tuple(&rule.rewrite, &rewrite);
462 rv = pnat_binding_add(&match, &rewrite, &binding_index);
464 rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
465 test_assert(rv == 0, "binding_detach - pass");
466 rv = pnat_binding_del(binding_index);
470 static void test_api(void) {
472 test_del_before_detach();
477 * 1) Table of packets and expected outcomes. Run through
478 * 2) Performance tests. Measure instructions, cache behaviour etc.
480 clib_error_t *ip_checksum_init(vlib_main_t *vm);
482 int main(int argc, char **argv) {
484 clib_mem_init(0, 3ULL << 30);
486 vlib_main_t *vm = &vlib_global_main;
488 buffers_vector = buffer_init(buffers_vector, 256);
490 assert(vlib_node_main_init(vm) == 0);
492 ip_checksum_init(vm);
494 u32 node_index = vlib_register_node(vm, &pnat_input_node);
495 node = vlib_node_get_runtime(vm, node_index);