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++) {
54 u32 *results_bi = 0; /* global vector of result buffers */
55 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_log(A, M, ...) \
66 log_error(M, ##__VA_ARGS__); \
69 log_info(M, ##__VA_ARGS__); \
71 #define test_assert(A, M, ...) \
73 log_error(M, ##__VA_ARGS__); \
78 * Always return the frame of generated packets
80 #define vlib_frame_vector_args test_vlib_frame_vector_args
81 void *test_vlib_frame_vector_args(vlib_frame_t *f) { return buffers_vector; }
83 /* Synthetic value for vnet_feature_next */
84 #define NEXT_PASSTHROUGH 4242
86 #define vnet_feature_next_u16 test_vnet_feature_next_u16
87 void vnet_feature_next_u16(u16 *next0, vlib_buffer_t *b0) {
88 *next0 = NEXT_PASSTHROUGH;
91 /* Gather output packets */
92 #define vlib_buffer_enqueue_to_next test_vlib_buffer_enqueue_to_next
93 void test_vlib_buffer_enqueue_to_next(vlib_main_t *vm,
94 vlib_node_runtime_t *node, u32 *buffers,
95 u16 *nexts, uword count) {
96 vec_add(results_next, nexts, count);
97 vec_add(results_bi, buffers, count);
100 pnat_trace_t trace = {0};
101 #define vlib_add_trace test_vlib_add_trace
102 void *test_vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r,
103 vlib_buffer_t *b, u32 n_data_bytes) {
107 #define vlib_get_buffers test_vlib_get_buffers
108 void test_vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b,
111 for (i = 0; i < count; i++) {
112 b[i] = (vlib_buffer_t *)&buffers[bi[i]];
116 vlib_buffer_t *test_vlib_get_buffer(u32 bi) {
117 return (vlib_buffer_t *)&buffers[bi];
120 /* Must be included here to allow the above functions to override */
121 #include "../pnat_node.h"
131 u32 expect_next_index;
133 #include "test_packets.h"
149 test_5tuple_t rewrite;
156 .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6871},
157 .rewrite = {.dst = "1.2.3.4"},
161 .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6871},
162 .rewrite = {.dst = "1.2.3.4"},
166 .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6872},
167 .rewrite = {.dst = "1.2.3.4", .sport = 53, .dport = 8000},
171 .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6873},
172 .rewrite = {.dst = "1.2.3.4", .sport = 53, .dport = 8000},
176 .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6874},
177 .rewrite = {.from_offset = 15, .to_offset = 19},
181 .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6875},
182 .rewrite = {.from_offset = 15, .to_offset = 50},
186 .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6877},
187 .rewrite = {.dst = "1.2.3.4", .from_offset = 15, .to_offset = 35},
191 .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6876},
192 .rewrite = {.clear_offset = 22},
197 static int fill_packets(vlib_main_t *vm, vlib_buffer_t *b, int n, char *test) {
198 b->flags |= VLIB_BUFFER_IS_TRACED;
200 ip4_header_t *ip = (ip4_header_t *)vlib_buffer_get_current(b);
204 /* Do the work of SVR */
205 vnet_buffer(b)->ip.reass.l4_src_port = 0;
206 vnet_buffer(b)->ip.reass.l4_dst_port = 0;
207 b->current_length = n;
209 if (ip4_is_fragment(ip))
211 if (ip->protocol == IP_PROTOCOL_UDP) {
212 udp_header_t *udp = ip4_next_header(ip);
213 vnet_buffer(b)->ip.reass.l4_src_port = udp->src_port;
214 vnet_buffer(b)->ip.reass.l4_dst_port = udp->dst_port;
215 } else if (ip->protocol == IP_PROTOCOL_TCP) {
216 tcp_header_t *tcp = ip4_next_header(ip);
217 vnet_buffer(b)->ip.reass.l4_src_port = tcp->src_port;
218 vnet_buffer(b)->ip.reass.l4_dst_port = tcp->dst_port;
223 static void ruletomatch(test_5tuple_t *r, pnat_match_tuple_t *t) {
225 inet_pton(AF_INET, r->src, &t->src);
229 inet_pton(AF_INET, r->dst, &t->dst);
234 t->mask |= PNAT_DPORT;
238 t->mask |= PNAT_SPORT;
243 static void ruletorewrite(test_5tuple_t *r, pnat_rewrite_tuple_t *t) {
245 inet_pton(AF_INET, r->src, &t->src);
249 inet_pton(AF_INET, r->dst, &t->dst);
254 t->mask |= PNAT_DPORT;
258 t->mask |= PNAT_SPORT;
260 if (r->to_offset || r->from_offset) {
261 t->to_offset = r->to_offset;
262 t->from_offset = r->from_offset;
263 t->mask |= PNAT_COPY_BYTE;
265 if (r->clear_offset) {
266 t->clear_offset = r->clear_offset;
267 t->mask |= PNAT_CLEAR_BYTE;
271 static void add_translation(rule_t *r) {
272 pnat_match_tuple_t match = {0};
273 pnat_rewrite_tuple_t rewrite = {0};
275 ruletomatch(&r->match, &match);
276 ruletorewrite(&r->rewrite, &rewrite);
278 int rv = pnat_binding_add(&match, &rewrite, &r->index);
281 rv = pnat_binding_attach(0, PNAT_IP4_INPUT, r->index);
285 static void del_translation(rule_t *r) {
286 int rv = pnat_binding_detach(0, PNAT_IP4_INPUT, r->index);
289 rv = pnat_binding_del(r->index);
293 static void validate_packet(vlib_main_t *vm, char *name, u32 bi,
294 vlib_buffer_t *expected_b) {
295 vlib_buffer_t *b = test_vlib_get_buffer(bi);
298 ip4_header_t *ip = (ip4_header_t *)vlib_buffer_get_current(b);
299 ip4_header_t *expected_ip =
300 (ip4_header_t *)vlib_buffer_get_current(expected_b);
302 if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP) {
303 u32 flags = ip4_tcp_udp_validate_checksum(vm, b);
304 test_assert((flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0, "%s",
306 flags = ip4_tcp_udp_validate_checksum(vm, expected_b);
307 test_assert((flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0, "%s",
310 test_assert(b->current_length == expected_b->current_length, "%s %d vs %d",
311 name, b->current_length, expected_b->current_length);
313 if (memcmp(ip, expected_ip, b->current_length) != 0) {
314 if (ip->protocol == IP_PROTOCOL_UDP) {
315 udp_header_t *udp = ip4_next_header(ip);
316 clib_warning("Received: IP: %U UDP: %U", format_ip4_header, ip,
317 sizeof(*ip), format_udp_header, udp, sizeof(*udp));
318 udp = ip4_next_header(expected_ip);
319 clib_warning("%U", format_hexdump, ip, b->current_length);
320 clib_warning("Expected: IP: %U UDP: %U", format_ip4_header,
321 expected_ip, sizeof(*ip), format_udp_header, udp,
323 clib_warning("%U", format_hexdump, expected_ip,
324 expected_b->current_length);
325 } else if (ip->protocol == IP_PROTOCOL_TCP) {
326 tcp_header_t *tcp = ip4_next_header(ip);
327 clib_warning("Received IP: %U TCP: %U", format_ip4_header, ip,
328 sizeof(*ip), format_tcp_header, tcp, sizeof(*tcp));
329 tcp = ip4_next_header(expected_ip);
330 clib_warning("Expected IP: %U TCP: %U", format_ip4_header,
331 expected_ip, sizeof(*ip), format_tcp_header, tcp,
334 clib_warning("Received: IP: %U", format_ip4_header, ip,
336 clib_warning("Expected: IP: %U", format_ip4_header, expected_ip,
339 test_assert_log(0, "%s", name);
341 test_assert_log(1, "%s", name);
345 extern vlib_node_registration_t pnat_input_node;
347 static void test_table(test_t *t, int no_tests) {
348 // walk through table of tests
350 vlib_main_t *vm = &vlib_global_main;
352 /* Generate packet data */
353 for (i = 0; i < no_tests; i++) {
354 // create input buffer(s)
355 fill_packets(vm, (vlib_buffer_t *)&buffers[i], t[i].nsend, t[i].send);
356 fill_packets(vm, (vlib_buffer_t *)&expected[i], t[i].nexpect,
360 /* send packets through graph node */
361 vlib_frame_t frame = {.n_vectors = no_tests};
362 node->flags |= VLIB_NODE_FLAG_TRACE;
364 pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
367 for (i = 0; i < no_tests; i++) {
368 test_assert(t[i].expect_next_index == results_next[i], "%s", t[i].name);
369 validate_packet(vm, t[i].name, results_bi[i],
370 (vlib_buffer_t *)&expected[i]);
372 vec_free(results_next);
373 vec_free(results_bi);
376 void test_performance(void) {
377 pnat_main_t *pm = &pnat_main;
379 vlib_main_t *vm = &vlib_global_main;
381 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
382 add_translation(&rules[i]);
384 assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
386 int no_tests = sizeof(tests_packets) / sizeof(tests_packets[0]);
387 /* Generate packet data */
388 for (i = 0; i < VLIB_FRAME_SIZE; i++) {
389 // create input buffer(s)
390 fill_packets(vm, (vlib_buffer_t *)&buffers[i],
391 tests_packets[i % no_tests].nsend,
392 tests_packets[i % no_tests].send);
393 // fill_packets(vm, (vlib_buffer_t *)&expected[i], &tests[i %
394 // no_tests].expect);
397 /* send packets through graph node */
398 vlib_frame_t frame = {.n_vectors = VLIB_FRAME_SIZE};
399 node->flags &= ~VLIB_NODE_FLAG_TRACE;
402 for (j = 0; j < 10000; j++) {
403 pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
406 for (i = 0; i < VLIB_FRAME_SIZE; i++) {
407 assert(tests[i % no_tests].expect_next_index == results_next[i]);
408 validate_packet(vm, tests[i % no_tests].name, results_bi[i], (vlib_buffer_t *)&expected[i]);
411 vec_free(results_next);
412 vec_free(results_bi);
415 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
416 del_translation(&rules[i]);
418 assert(pool_elts(pm->translations) == 0);
419 assert(pool_elts(pm->interfaces) == 0);
422 void test_packets(void) {
423 pnat_main_t *pm = &pnat_main;
425 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
426 add_translation(&rules[i]);
428 assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
430 test_table(tests_packets, sizeof(tests_packets) / sizeof(tests_packets[0]));
432 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
433 del_translation(&rules[i]);
435 assert(pool_elts(pm->translations) == 0);
436 assert(pool_elts(pm->interfaces) == 0);
439 static void test_attach(void) {
440 pnat_attachment_point_t attachment = PNAT_IP4_INPUT;
441 u32 binding_index = 0;
443 int rv = pnat_binding_attach(sw_if_index, attachment, binding_index);
444 test_assert(rv == -1, "binding_attach - nothing to attach");
446 rv = pnat_binding_detach(sw_if_index, attachment, 1234);
447 test_assert(rv == -1, "binding_detach - nothing to detach");
449 pnat_match_tuple_t match = {.mask = PNAT_SA};
450 pnat_rewrite_tuple_t rewrite = {.mask = PNAT_SA};
451 rv = pnat_binding_add(&match, &rewrite, &binding_index);
454 rv = pnat_binding_attach(sw_if_index, attachment, binding_index);
455 test_assert(rv == 0, "binding_attach - rule");
457 rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
458 test_assert(rv == 0, "binding_detach - rule");
460 rv = pnat_binding_del(binding_index);
464 static void test_del_before_detach(void) {
465 pnat_attachment_point_t attachment = PNAT_IP4_INPUT;
466 u32 binding_index = 0;
469 /* Ensure 5-tuple here will not duplicate with other tests cause this will
470 * not be removed from flow cache */
472 .match = {.dst = "123.123.123.123", .proto = 17, .dport = 6871},
473 .rewrite = {.dst = "1.2.3.4"},
477 add_translation(&rule);
479 int rv = pnat_binding_del(binding_index);
482 test_table(&tests_missing_rule[0], 1);
484 /* For now if you have deleted before detach, can't find key */
485 rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
486 test_assert(rv == -1, "binding_detach - failure");
488 /* Re-add the rule and try again */
489 pnat_match_tuple_t match = {0};
490 pnat_rewrite_tuple_t rewrite = {0};
491 ruletomatch(&rule.match, &match);
492 ruletorewrite(&rule.rewrite, &rewrite);
493 rv = pnat_binding_add(&match, &rewrite, &binding_index);
495 rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
496 test_assert(rv == 0, "binding_detach - pass");
497 rv = pnat_binding_del(binding_index);
501 void test_api(void) {
503 test_del_before_detach();
506 void test_checksum(void) {
508 vlib_main_t *vm = &vlib_global_main;
509 pnat_main_t *pm = &pnat_main;
515 (char[]){0x45, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
516 0x74, 0xcb, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
517 0x00, 0x50, 0x1a, 0xd7, 0x00, 0x08, 0xde, 0xb1},
520 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
521 add_translation(&rules[i]);
523 assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
525 /* send packets through graph node */
526 vlib_frame_t frame = {.n_vectors = 1};
527 node->flags |= VLIB_NODE_FLAG_TRACE;
530 (ip4_header_t *)vlib_buffer_get_current((vlib_buffer_t *)&buffers[0]);
532 for (i = 0; i < 65535; i++) {
534 /* Get a buffer. Loop through 64K variations of it to check checksum */
535 memset(&buffers[0], 0, 2048);
536 fill_packets(vm, (vlib_buffer_t *)&buffers[0], test.nsend, test.send);
538 ip->src_address.as_u32 = i;
540 ip->checksum = ip4_header_checksum(ip);
541 pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
544 test_assert_log(1, "%s", test.name);
546 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
547 del_translation(&rules[i]);
553 * 1) Table of packets and expected outcomes. Run through
554 * 2) Performance tests. Measure instructions, cache behaviour etc.
556 clib_error_t *ip_checksum_init(vlib_main_t *vm);
558 int main(int argc, char **argv) {
560 clib_mem_init(0, 3ULL << 30);
562 vlib_main_t *vm = &vlib_global_main;
564 buffers_vector = buffer_init(buffers_vector, 256);
566 assert(vlib_node_main_init(vm) == 0);
568 ip_checksum_init(vm);
570 u32 node_index = vlib_register_node(vm, &pnat_input_node);
571 node = vlib_node_get_runtime(vm, node_index);
583 * - Chained buffers. Only do rewrite in first buffer
584 * - No interface. Can that really happen?
585 * - IP length shorter than buffer.
586 * - IP length longer than buffer.