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 */
29 #include <sys/socket.h>
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #endif /* __FreeBSD__ */
33 #include <arpa/inet.h>
34 #include "pnat_test_stubs.h"
37 ** Buffer management in test setup
38 ** Allocate buffers return vector of buffer indicies.
40 ** Setup frame with buffers when calling function.
41 ** Global vector of all buffers with their indicies?
42 ** Convert buffer index to pointer?
47 struct buffers buffers[256];
48 struct buffers expected[256];
49 u32 *buffers_vector = 0;
51 static u32 *buffer_init(u32 *vector, int count) {
53 for (i = 0; i < count; i++) {
59 u32 *results_bi = 0; /* global vector of result buffers */
60 u16 *results_next = 0;
62 vlib_node_runtime_t *node;
64 #define log_info(M, ...) \
65 fprintf(stderr, "\033[32;1m[OK] " M "\033[0m\n", ##__VA_ARGS__)
66 #define log_error(M, ...) \
67 fprintf(stderr, "\033[31;1m[ERROR] (%s:%d:) " M "\033[0m\n", __FILE__, \
68 __LINE__, ##__VA_ARGS__)
69 #define test_assert_log(A, M, ...) \
71 log_error(M, ##__VA_ARGS__); \
74 log_info(M, ##__VA_ARGS__); \
76 #define test_assert(A, M, ...) \
78 log_error(M, ##__VA_ARGS__); \
83 * Always return the frame of generated packets
85 #define vlib_frame_vector_args test_vlib_frame_vector_args
86 void *test_vlib_frame_vector_args(vlib_frame_t *f) { return buffers_vector; }
88 /* Synthetic value for vnet_feature_next */
89 #define NEXT_PASSTHROUGH 4242
91 #define vnet_feature_next_u16 test_vnet_feature_next_u16
92 void vnet_feature_next_u16(u16 *next0, vlib_buffer_t *b0) {
93 *next0 = NEXT_PASSTHROUGH;
96 /* Gather output packets */
97 #define vlib_buffer_enqueue_to_next test_vlib_buffer_enqueue_to_next
98 void test_vlib_buffer_enqueue_to_next(vlib_main_t *vm,
99 vlib_node_runtime_t *node, u32 *buffers,
100 u16 *nexts, uword count) {
101 vec_add(results_next, nexts, count);
102 vec_add(results_bi, buffers, count);
105 pnat_trace_t trace = {0};
106 #define vlib_add_trace test_vlib_add_trace
107 void *test_vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r,
108 vlib_buffer_t *b, u32 n_data_bytes) {
112 #define vlib_get_buffers test_vlib_get_buffers
113 void test_vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b,
116 for (i = 0; i < count; i++) {
117 b[i] = (vlib_buffer_t *)&buffers[bi[i]];
121 vlib_buffer_t *test_vlib_get_buffer(u32 bi) {
122 return (vlib_buffer_t *)&buffers[bi];
125 /* Must be included here to allow the above functions to override */
126 #include "../pnat_node.h"
136 u32 expect_next_index;
138 #include "test_packets.h"
154 test_5tuple_t rewrite;
161 .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6871},
162 .rewrite = {.dst = "1.2.3.4"},
166 .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6871},
167 .rewrite = {.dst = "1.2.3.4"},
171 .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6872},
172 .rewrite = {.dst = "1.2.3.4", .sport = 53, .dport = 8000},
176 .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6873},
177 .rewrite = {.dst = "1.2.3.4", .sport = 53, .dport = 8000},
181 .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6874},
182 .rewrite = {.from_offset = 15, .to_offset = 19},
186 .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6875},
187 .rewrite = {.from_offset = 15, .to_offset = 50},
191 .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6877},
192 .rewrite = {.dst = "1.2.3.4", .from_offset = 15, .to_offset = 35},
196 .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6876},
197 .rewrite = {.clear_offset = 22},
202 static int fill_packets(vlib_main_t *vm, vlib_buffer_t *b, int n, char *test) {
203 b->flags |= VLIB_BUFFER_IS_TRACED;
205 ip4_header_t *ip = (ip4_header_t *)vlib_buffer_get_current(b);
209 /* Do the work of SVR */
210 vnet_buffer(b)->ip.reass.l4_src_port = 0;
211 vnet_buffer(b)->ip.reass.l4_dst_port = 0;
212 b->current_length = n;
214 if (ip4_is_fragment(ip))
216 if (ip->protocol == IP_PROTOCOL_UDP) {
217 udp_header_t *udp = ip4_next_header(ip);
218 vnet_buffer(b)->ip.reass.l4_src_port = udp->src_port;
219 vnet_buffer(b)->ip.reass.l4_dst_port = udp->dst_port;
220 } else if (ip->protocol == IP_PROTOCOL_TCP) {
221 tcp_header_t *tcp = ip4_next_header(ip);
222 vnet_buffer(b)->ip.reass.l4_src_port = tcp->src_port;
223 vnet_buffer(b)->ip.reass.l4_dst_port = tcp->dst_port;
228 static void ruletomatch(test_5tuple_t *r, pnat_match_tuple_t *t) {
230 inet_pton(AF_INET, r->src, &t->src);
234 inet_pton(AF_INET, r->dst, &t->dst);
239 t->mask |= PNAT_DPORT;
243 t->mask |= PNAT_SPORT;
248 static void ruletorewrite(test_5tuple_t *r, pnat_rewrite_tuple_t *t) {
250 inet_pton(AF_INET, r->src, &t->src);
254 inet_pton(AF_INET, r->dst, &t->dst);
259 t->mask |= PNAT_DPORT;
263 t->mask |= PNAT_SPORT;
265 if (r->to_offset || r->from_offset) {
266 t->to_offset = r->to_offset;
267 t->from_offset = r->from_offset;
268 t->mask |= PNAT_COPY_BYTE;
270 if (r->clear_offset) {
271 t->clear_offset = r->clear_offset;
272 t->mask |= PNAT_CLEAR_BYTE;
276 static void add_translation(rule_t *r) {
277 pnat_match_tuple_t match = {0};
278 pnat_rewrite_tuple_t rewrite = {0};
280 ruletomatch(&r->match, &match);
281 ruletorewrite(&r->rewrite, &rewrite);
283 int rv = pnat_binding_add(&match, &rewrite, &r->index);
286 rv = pnat_binding_attach(0, PNAT_IP4_INPUT, r->index);
290 static void del_translation(rule_t *r) {
291 int rv = pnat_binding_detach(0, PNAT_IP4_INPUT, r->index);
294 rv = pnat_binding_del(r->index);
298 static void validate_packet(vlib_main_t *vm, char *name, u32 bi,
299 vlib_buffer_t *expected_b) {
300 vlib_buffer_t *b = test_vlib_get_buffer(bi);
303 ip4_header_t *ip = (ip4_header_t *)vlib_buffer_get_current(b);
304 ip4_header_t *expected_ip =
305 (ip4_header_t *)vlib_buffer_get_current(expected_b);
307 if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP) {
308 u32 flags = ip4_tcp_udp_validate_checksum(vm, b);
309 test_assert((flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0, "%s",
311 flags = ip4_tcp_udp_validate_checksum(vm, expected_b);
312 test_assert((flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0, "%s",
315 test_assert(b->current_length == expected_b->current_length, "%s %d vs %d",
316 name, b->current_length, expected_b->current_length);
318 if (memcmp(ip, expected_ip, b->current_length) != 0) {
319 if (ip->protocol == IP_PROTOCOL_UDP) {
320 udp_header_t *udp = ip4_next_header(ip);
321 clib_warning("Received: IP: %U UDP: %U", format_ip4_header, ip,
322 sizeof(*ip), format_udp_header, udp, sizeof(*udp));
323 udp = ip4_next_header(expected_ip);
324 clib_warning("%U", format_hexdump, ip, b->current_length);
325 clib_warning("Expected: IP: %U UDP: %U", format_ip4_header,
326 expected_ip, sizeof(*ip), format_udp_header, udp,
328 clib_warning("%U", format_hexdump, expected_ip,
329 expected_b->current_length);
330 } else if (ip->protocol == IP_PROTOCOL_TCP) {
331 tcp_header_t *tcp = ip4_next_header(ip);
332 clib_warning("Received IP: %U TCP: %U", format_ip4_header, ip,
333 sizeof(*ip), format_tcp_header, tcp, sizeof(*tcp));
334 tcp = ip4_next_header(expected_ip);
335 clib_warning("Expected IP: %U TCP: %U", format_ip4_header,
336 expected_ip, sizeof(*ip), format_tcp_header, tcp,
339 clib_warning("Received: IP: %U", format_ip4_header, ip,
341 clib_warning("Expected: IP: %U", format_ip4_header, expected_ip,
344 test_assert_log(0, "%s", name);
346 test_assert_log(1, "%s", name);
350 extern vlib_node_registration_t pnat_input_node;
352 static void test_table(test_t *t, int no_tests) {
353 // walk through table of tests
356 vlib_main_t *vm = vlib_get_first_main();
358 /* Generate packet data */
359 for (i = 0; i < no_tests; i++) {
360 // create input buffer(s)
361 fill_packets(vm, (vlib_buffer_t *)&buffers[i], t[i].nsend, t[i].send);
362 fill_packets(vm, (vlib_buffer_t *)&expected[i], t[i].nexpect,
366 /* send packets through graph node */
367 vlib_frame_t frame = {.n_vectors = no_tests};
368 node->flags |= VLIB_NODE_FLAG_TRACE;
370 pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
373 for (i = 0; i < no_tests; i++) {
374 test_assert(t[i].expect_next_index == results_next[i], "%s", t[i].name);
375 validate_packet(vm, t[i].name, results_bi[i],
376 (vlib_buffer_t *)&expected[i]);
378 vec_free(results_next);
379 vec_free(results_bi);
382 void test_performance(void) {
383 pnat_main_t *pm = &pnat_main;
385 vlib_main_t *vm = vlib_get_first_main();
387 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
388 add_translation(&rules[i]);
390 assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
392 int no_tests = sizeof(tests_packets) / sizeof(tests_packets[0]);
393 /* Generate packet data */
394 for (i = 0; i < VLIB_FRAME_SIZE; i++) {
395 // create input buffer(s)
396 fill_packets(vm, (vlib_buffer_t *)&buffers[i],
397 tests_packets[i % no_tests].nsend,
398 tests_packets[i % no_tests].send);
399 // fill_packets(vm, (vlib_buffer_t *)&expected[i], &tests[i %
400 // no_tests].expect);
403 /* send packets through graph node */
404 vlib_frame_t frame = {.n_vectors = VLIB_FRAME_SIZE};
405 node->flags &= ~VLIB_NODE_FLAG_TRACE;
408 for (j = 0; j < 10000; j++) {
409 pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
412 for (i = 0; i < VLIB_FRAME_SIZE; i++) {
413 assert(tests[i % no_tests].expect_next_index == results_next[i]);
414 validate_packet(vm, tests[i % no_tests].name, results_bi[i], (vlib_buffer_t *)&expected[i]);
417 vec_free(results_next);
418 vec_free(results_bi);
421 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
422 del_translation(&rules[i]);
424 assert(pool_elts(pm->translations) == 0);
425 assert(pool_elts(pm->interfaces) == 0);
428 void test_packets(void) {
429 pnat_main_t *pm = &pnat_main;
431 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
432 add_translation(&rules[i]);
434 assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
436 test_table(tests_packets, sizeof(tests_packets) / sizeof(tests_packets[0]));
438 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
439 del_translation(&rules[i]);
441 assert(pool_elts(pm->translations) == 0);
442 assert(pool_elts(pm->interfaces) == 0);
445 static void test_attach(void) {
446 pnat_attachment_point_t attachment = PNAT_IP4_INPUT;
447 u32 binding_index = 0;
449 int rv = pnat_binding_attach(sw_if_index, attachment, binding_index);
450 test_assert(rv == -1, "binding_attach - nothing to attach");
452 rv = pnat_binding_detach(sw_if_index, attachment, 1234);
453 test_assert(rv == -1, "binding_detach - nothing to detach");
455 pnat_match_tuple_t match = {.mask = PNAT_SA};
456 pnat_rewrite_tuple_t rewrite = {.mask = PNAT_SA};
457 rv = pnat_binding_add(&match, &rewrite, &binding_index);
460 rv = pnat_binding_attach(sw_if_index, attachment, binding_index);
461 test_assert(rv == 0, "binding_attach - rule");
463 rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
464 test_assert(rv == 0, "binding_detach - rule");
466 rv = pnat_binding_del(binding_index);
470 static void test_del_before_detach(void) {
471 pnat_attachment_point_t attachment = PNAT_IP4_INPUT;
472 u32 binding_index = 0;
475 /* Ensure 5-tuple here will not duplicate with other tests cause this will
476 * not be removed from flow cache */
478 .match = {.dst = "123.123.123.123", .proto = 17, .dport = 6871},
479 .rewrite = {.dst = "1.2.3.4"},
483 add_translation(&rule);
485 int rv = pnat_binding_del(binding_index);
488 test_table(&tests_missing_rule[0], 1);
490 /* For now if you have deleted before detach, can't find key */
491 rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
492 test_assert(rv == -1, "binding_detach - failure");
494 /* Re-add the rule and try again */
495 pnat_match_tuple_t match = {0};
496 pnat_rewrite_tuple_t rewrite = {0};
497 ruletomatch(&rule.match, &match);
498 ruletorewrite(&rule.rewrite, &rewrite);
499 rv = pnat_binding_add(&match, &rewrite, &binding_index);
501 rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
502 test_assert(rv == 0, "binding_detach - pass");
503 rv = pnat_binding_del(binding_index);
507 void test_api(void) {
509 test_del_before_detach();
512 void test_checksum(void) {
514 vlib_main_t *vm = vlib_get_first_main();
515 pnat_main_t *pm = &pnat_main;
521 (char[]){0x45, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
522 0x74, 0xcb, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
523 0x00, 0x50, 0x1a, 0xd7, 0x00, 0x08, 0xde, 0xb1},
526 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
527 add_translation(&rules[i]);
529 assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
531 /* send packets through graph node */
532 vlib_frame_t frame = {.n_vectors = 1};
533 node->flags |= VLIB_NODE_FLAG_TRACE;
536 (ip4_header_t *)vlib_buffer_get_current((vlib_buffer_t *)&buffers[0]);
538 for (i = 0; i < 65535; i++) {
540 /* Get a buffer. Loop through 64K variations of it to check checksum */
541 memset(&buffers[0], 0, 2048);
542 fill_packets(vm, (vlib_buffer_t *)&buffers[0], test.nsend, test.send);
544 ip->src_address.as_u32 = i;
546 ip->checksum = ip4_header_checksum(ip);
547 pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
550 test_assert_log(1, "%s", test.name);
552 for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
553 del_translation(&rules[i]);
559 * 1) Table of packets and expected outcomes. Run through
560 * 2) Performance tests. Measure instructions, cache behaviour etc.
562 clib_error_t *ip_checksum_init(vlib_main_t *vm);
564 int main(int argc, char **argv) {
566 clib_mem_init(0, 3ULL << 30);
569 vlib_main_t *vm = vlib_get_first_main();
571 buffers_vector = buffer_init(buffers_vector, 256);
573 assert(vlib_node_main_init(vm) == 0);
575 ip_checksum_init(vm);
578 vlib_register_node(vm, &pnat_input_node, "%s", pnat_input_node.name);
579 node = vlib_node_get_runtime(vm, node_index);
591 * - Chained buffers. Only do rewrite in first buffer
592 * - No interface. Can that really happen?
593 * - IP length shorter than buffer.
594 * - IP length longer than buffer.