tests: fix syntax warning in ipsec tun test
[vpp.git] / src / plugins / nat / pnat / pnat_test.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <stdbool.h>
17 #include <assert.h>
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>
26 #include "pnat.h"
27 #include <pnat/pnat.api_enum.h> /* For error counters */
28 #include <arpa/inet.h>
29 #include "pnat_test_stubs.h"
30
31 /*
32 ** Buffer management in test setup
33 ** Allocate buffers return vector of buffer indicies.
34 **
35 ** Setup frame with buffers when calling function.
36 ** Global vector of all buffers with their indicies?
37 ** Convert buffer index to pointer?
38 */
39 struct buffers {
40     u8 data[2048];
41 };
42 struct buffers buffers[256];
43 struct buffers expected[256];
44 u32 *buffers_vector = 0;
45
46 static u32 *buffer_init(u32 *vector, int count) {
47     int i;
48     for (i = 0; i < count; i++) {
49         vec_add1(vector, i);
50     }
51     return vector;
52 }
53 #define PNAT_TEST_DEBUG 0
54
55 u32 *results_bi = 0; /* global vector of result buffers */
56 u16 *results_next = 0;
57 vlib_node_runtime_t *node;
58
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, ...)                                                 \
65     if (!(A)) {                                                                \
66         log_error(M, ##__VA_ARGS__);                                           \
67         assert(A);                                                             \
68     } else {                                                                   \
69         log_info(M, ##__VA_ARGS__);                                            \
70     }
71
72 /*
73  * Always return the frame of generated packets
74  */
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; }
77
78 /* Synthetic value for vnet_feature_next  */
79 #define NEXT_PASSTHROUGH 4242
80
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;
84 }
85
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);
93 }
94
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) {
99     return &trace;
100 }
101
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,
104                            int count) {
105     int i;
106     for (i = 0; i < count; i++) {
107         b[i] = (vlib_buffer_t *)&buffers[bi[i]];
108     }
109 }
110
111 vlib_buffer_t *test_vlib_get_buffer(u32 bi) {
112     return (vlib_buffer_t *)&buffers[bi];
113 }
114
115 /* Must be included here to allow the above functions to override */
116 #include "pnat_node.h"
117
118 /*** TESTS ***/
119
120 typedef struct {
121     char *src;
122     char *dst;
123     u8 proto;
124     u16 sport;
125     u16 dport;
126 } test_5tuple_t;
127
128 typedef struct {
129     char *name;
130     test_5tuple_t send;
131     test_5tuple_t expect;
132     u32 expect_next_index;
133 } test_t;
134
135 test_t tests[] = {
136     {
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,
141     },
142     {
143         .name = "unchanged",
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,
147     },
148     {
149         .name = "tcp da",
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,
153     },
154     {
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,
159     },
160 };
161
162 /* Rules */
163 typedef struct {
164     test_5tuple_t match;
165     test_5tuple_t rewrite;
166     bool in;
167     u32 index;
168 } rule_t;
169
170 rule_t rules[] = {
171     {
172         .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6871},
173         .rewrite = {.dst = "1.2.3.4"},
174         .in = true,
175     },
176     {
177         .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6871},
178         .rewrite = {.dst = "1.2.3.4"},
179         .in = true,
180     },
181     {
182         .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6872},
183         .rewrite = {.dst = "1.2.3.4", .sport = 53, .dport = 8000},
184         .in = true,
185     },
186     {
187         .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6873},
188         .rewrite = {.dst = "1.2.3.4", .sport = 53, .dport = 8000},
189         .in = true,
190     },
191 };
192
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;
196
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;
200     ip->ttl = 64;
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;
204
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);
228     } else {
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;
234     }
235
236     return 0;
237 }
238
239 static void ruleto5tuple(test_5tuple_t *r, pnat_5tuple_t *t) {
240     if (r->src) {
241         inet_pton(AF_INET, r->src, &t->src);
242         t->mask |= PNAT_SA;
243     }
244     if (r->dst) {
245         inet_pton(AF_INET, r->dst, &t->dst);
246         t->mask |= PNAT_DA;
247     }
248     if (r->dport) {
249         t->dport = r->dport;
250         t->mask |= PNAT_DPORT;
251     }
252     if (r->sport) {
253         t->sport = r->sport;
254         t->mask |= PNAT_SPORT;
255     }
256     t->proto = r->proto;
257 }
258
259 static void add_translation(rule_t *r) {
260     pnat_5tuple_t match = {0};
261     pnat_5tuple_t rewrite = {0};
262
263     ruleto5tuple(&r->match, &match);
264     ruleto5tuple(&r->rewrite, &rewrite);
265
266     int rv = pnat_binding_add(&match, &rewrite, &r->index);
267     assert(rv == 0);
268
269     rv = pnat_binding_attach(0, PNAT_IP4_INPUT, r->index);
270     assert(rv == 0);
271 }
272
273 static void del_translation(rule_t *r) {
274     int rv = pnat_binding_detach(0, PNAT_IP4_INPUT, r->index);
275     assert(rv == 0);
276
277     rv = pnat_binding_del(r->index);
278     assert(rv == 0);
279 }
280
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);
284     assert(b);
285
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);
289
290 #if PNAT_TEST_DEBUG
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));
299 #endif
300
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);
306
307     test_assert(memcmp(ip, expected_ip, b->current_length) == 0, "%s", name);
308 }
309
310 extern vlib_node_registration_t pnat_input_node;
311
312 static void test_table(test_t *t, int no_tests) {
313     // walk through table of tests
314     int i;
315     vlib_main_t *vm = &vlib_global_main;
316
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);
322     }
323
324     /* send packets through graph node */
325     vlib_frame_t frame = {.n_vectors = no_tests};
326     node->flags |= VLIB_NODE_FLAG_TRACE;
327
328     pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
329
330     /* verify tests */
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]);
335     }
336     vec_free(results_next);
337     vec_free(results_bi);
338 }
339
340 static void test_performance(void) {
341     pnat_main_t *pm = &pnat_main;
342     int i;
343     vlib_main_t *vm = &vlib_global_main;
344
345     for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
346         add_translation(&rules[i]);
347     }
348     assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
349
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);
358     }
359
360     /* send packets through graph node */
361     vlib_frame_t frame = {.n_vectors = VLIB_FRAME_SIZE};
362     node->flags &= ~VLIB_NODE_FLAG_TRACE;
363
364     int j;
365     for (j = 0; j < 10000; j++) {
366         pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
367
368 #if 0
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]);
372     }
373 #endif
374         vec_free(results_next);
375         vec_free(results_bi);
376     }
377
378     for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
379         del_translation(&rules[i]);
380     }
381     assert(pool_elts(pm->translations) == 0);
382     assert(pool_elts(pm->interfaces) == 0);
383 }
384
385 static void test_packets(void) {
386     pnat_main_t *pm = &pnat_main;
387     int i;
388     for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
389         add_translation(&rules[i]);
390     }
391     assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
392
393     test_table(tests, sizeof(tests) / sizeof(tests[0]));
394
395     for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
396         del_translation(&rules[i]);
397     }
398     assert(pool_elts(pm->translations) == 0);
399     assert(pool_elts(pm->interfaces) == 0);
400 }
401 static void test_attach(void) {
402     pnat_attachment_point_t attachment = PNAT_IP4_INPUT;
403     u32 binding_index = 0;
404     u32 sw_if_index = 0;
405     int rv = pnat_binding_attach(sw_if_index, attachment, binding_index);
406     test_assert(rv == -1, "binding_attach - nothing to attach");
407
408     rv = pnat_binding_detach(sw_if_index, attachment, 1234);
409     test_assert(rv == -1, "binding_detach - nothing to detach");
410
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);
414     assert(rv == 0);
415
416     rv = pnat_binding_attach(sw_if_index, attachment, binding_index);
417     test_assert(rv == 0, "binding_attach - rule");
418
419     rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
420     test_assert(rv == 0, "binding_detach - rule");
421
422     rv = pnat_binding_del(binding_index);
423     assert(rv == 0);
424 }
425
426 static void test_del_before_detach(void) {
427     pnat_attachment_point_t attachment = PNAT_IP4_INPUT;
428     u32 binding_index = 0;
429     u32 sw_if_index = 0;
430
431     /* Ensure 5-tuple here will not duplicate with other tests cause this will
432      * not be removed from flow cache */
433     rule_t rule = {
434         .match = {.dst = "123.123.123.123", .proto = 17, .dport = 6871},
435         .rewrite = {.dst = "1.2.3.4"},
436         .in = true,
437     };
438
439     add_translation(&rule);
440
441     int rv = pnat_binding_del(binding_index);
442     assert(rv == 0);
443
444     test_t test = {
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,
449     };
450
451     test_table(&test, 1);
452
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");
456
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);
463     assert(rv == 0);
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);
467     assert(rv == 0);
468 }
469
470 static void test_api(void) {
471     test_attach();
472     test_del_before_detach();
473 }
474
475 /*
476  * Unit testing:
477  * 1) Table of packets and expected outcomes. Run through
478  * 2) Performance tests. Measure instructions, cache behaviour etc.
479  */
480 clib_error_t *ip_checksum_init(vlib_main_t *vm);
481
482 int main(int argc, char **argv) {
483
484     clib_mem_init(0, 3ULL << 30);
485
486     vlib_main_t *vm = &vlib_global_main;
487
488     buffers_vector = buffer_init(buffers_vector, 256);
489
490     assert(vlib_node_main_init(vm) == 0);
491
492     ip_checksum_init(vm);
493
494     u32 node_index = vlib_register_node(vm, &pnat_input_node);
495     node = vlib_node_get_runtime(vm, node_index);
496     assert(node);
497
498     /* Test API */
499     test_api();
500
501     test_packets();
502
503     test_performance();
504 }