pnat: coverity errors
[vpp.git] / src / plugins / nat / pnat / tests / 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
54 u32 *results_bi = 0; /* global vector of result buffers */
55 u16 *results_next = 0;
56
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_log(A, M, ...)                                             \
65     if (!(A)) {                                                                \
66         log_error(M, ##__VA_ARGS__);                                           \
67         assert(A);                                                             \
68     } else {                                                                   \
69         log_info(M, ##__VA_ARGS__);                                            \
70     }
71 #define test_assert(A, M, ...)                                                 \
72     if (!(A)) {                                                                \
73         log_error(M, ##__VA_ARGS__);                                           \
74         assert(A);                                                             \
75     }
76
77 /*
78  * Always return the frame of generated packets
79  */
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; }
82
83 /* Synthetic value for vnet_feature_next  */
84 #define NEXT_PASSTHROUGH 4242
85
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;
89 }
90
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);
98 }
99
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) {
104     return &trace;
105 }
106
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,
109                            int count) {
110     int i;
111     for (i = 0; i < count; i++) {
112         b[i] = (vlib_buffer_t *)&buffers[bi[i]];
113     }
114 }
115
116 vlib_buffer_t *test_vlib_get_buffer(u32 bi) {
117     return (vlib_buffer_t *)&buffers[bi];
118 }
119
120 /* Must be included here to allow the above functions to override */
121 #include "../pnat_node.h"
122
123 /*** TESTS ***/
124
125 typedef struct {
126     char *name;
127     int nsend;
128     char *send;
129     int nexpect;
130     char *expect;
131     u32 expect_next_index;
132 } test_t;
133 #include "test_packets.h"
134
135 /* Rules */
136 typedef struct {
137     char *src;
138     char *dst;
139     u8 proto;
140     u16 sport;
141     u16 dport;
142     u8 from_offset;
143     u8 to_offset;
144     u8 clear_offset;
145 } test_5tuple_t;
146
147 typedef struct {
148     test_5tuple_t match;
149     test_5tuple_t rewrite;
150     bool in;
151     u32 index;
152 } rule_t;
153
154 rule_t rules[] = {
155     {
156         .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6871},
157         .rewrite = {.dst = "1.2.3.4"},
158         .in = true,
159     },
160     {
161         .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6871},
162         .rewrite = {.dst = "1.2.3.4"},
163         .in = true,
164     },
165     {
166         .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6872},
167         .rewrite = {.dst = "1.2.3.4", .sport = 53, .dport = 8000},
168         .in = true,
169     },
170     {
171         .match = {.dst = "2.2.2.2", .proto = 6, .dport = 6873},
172         .rewrite = {.dst = "1.2.3.4", .sport = 53, .dport = 8000},
173         .in = true,
174     },
175     {
176         .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6874},
177         .rewrite = {.from_offset = 15, .to_offset = 19},
178         .in = true,
179     },
180     {
181         .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6875},
182         .rewrite = {.from_offset = 15, .to_offset = 50},
183         .in = true,
184     },
185     {
186         .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6877},
187         .rewrite = {.dst = "1.2.3.4", .from_offset = 15, .to_offset = 35},
188         .in = true,
189     },
190     {
191         .match = {.dst = "2.2.2.2", .proto = 17, .dport = 6876},
192         .rewrite = {.clear_offset = 22},
193         .in = true,
194     },
195 };
196
197 static int fill_packets(vlib_main_t *vm, vlib_buffer_t *b, int n, char *test) {
198     b->flags |= VLIB_BUFFER_IS_TRACED;
199
200     ip4_header_t *ip = (ip4_header_t *)vlib_buffer_get_current(b);
201
202     memcpy(ip, test, n);
203
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;
208
209     if (ip4_is_fragment(ip))
210         return 0;
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;
219     }
220     return 0;
221 }
222
223 static void ruletomatch(test_5tuple_t *r, pnat_match_tuple_t *t) {
224     if (r->src) {
225         inet_pton(AF_INET, r->src, &t->src);
226         t->mask |= PNAT_SA;
227     }
228     if (r->dst) {
229         inet_pton(AF_INET, r->dst, &t->dst);
230         t->mask |= PNAT_DA;
231     }
232     if (r->dport) {
233         t->dport = r->dport;
234         t->mask |= PNAT_DPORT;
235     }
236     if (r->sport) {
237         t->sport = r->sport;
238         t->mask |= PNAT_SPORT;
239     }
240     t->proto = r->proto;
241 }
242
243 static void ruletorewrite(test_5tuple_t *r, pnat_rewrite_tuple_t *t) {
244     if (r->src) {
245         inet_pton(AF_INET, r->src, &t->src);
246         t->mask |= PNAT_SA;
247     }
248     if (r->dst) {
249         inet_pton(AF_INET, r->dst, &t->dst);
250         t->mask |= PNAT_DA;
251     }
252     if (r->dport) {
253         t->dport = r->dport;
254         t->mask |= PNAT_DPORT;
255     }
256     if (r->sport) {
257         t->sport = r->sport;
258         t->mask |= PNAT_SPORT;
259     }
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;
264     }
265     if (r->clear_offset) {
266         t->clear_offset = r->clear_offset;
267         t->mask |= PNAT_CLEAR_BYTE;
268     }
269 }
270
271 static void add_translation(rule_t *r) {
272     pnat_match_tuple_t match = {0};
273     pnat_rewrite_tuple_t rewrite = {0};
274
275     ruletomatch(&r->match, &match);
276     ruletorewrite(&r->rewrite, &rewrite);
277
278     int rv = pnat_binding_add(&match, &rewrite, &r->index);
279     assert(rv == 0);
280
281     rv = pnat_binding_attach(0, PNAT_IP4_INPUT, r->index);
282     assert(rv == 0);
283 }
284
285 static void del_translation(rule_t *r) {
286     int rv = pnat_binding_detach(0, PNAT_IP4_INPUT, r->index);
287     assert(rv == 0);
288
289     rv = pnat_binding_del(r->index);
290     assert(rv == 0);
291 }
292
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);
296     assert(b);
297
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);
301
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",
305                     name);
306         flags = ip4_tcp_udp_validate_checksum(vm, expected_b);
307         test_assert((flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0, "%s",
308                     name);
309     }
310     test_assert(b->current_length == expected_b->current_length, "%s %d vs %d",
311                 name, b->current_length, expected_b->current_length);
312
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,
322                          sizeof(*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,
332                          sizeof(*tcp));
333         } else {
334             clib_warning("Received: IP: %U", format_ip4_header, ip,
335                          sizeof(*ip));
336             clib_warning("Expected: IP: %U", format_ip4_header, expected_ip,
337                          sizeof(*ip));
338         }
339         test_assert_log(0, "%s", name);
340     } else {
341         test_assert_log(1, "%s", name);
342     }
343 }
344
345 extern vlib_node_registration_t pnat_input_node;
346
347 static void test_table(test_t *t, int no_tests) {
348     // walk through table of tests
349     int i;
350     vlib_main_init();
351     vlib_main_t *vm = vlib_get_first_main();
352
353     /* Generate packet data */
354     for (i = 0; i < no_tests; i++) {
355         // create input buffer(s)
356         fill_packets(vm, (vlib_buffer_t *)&buffers[i], t[i].nsend, t[i].send);
357         fill_packets(vm, (vlib_buffer_t *)&expected[i], t[i].nexpect,
358                      t[i].expect);
359     }
360
361     /* send packets through graph node */
362     vlib_frame_t frame = {.n_vectors = no_tests};
363     node->flags |= VLIB_NODE_FLAG_TRACE;
364
365     pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
366
367     /* verify tests */
368     for (i = 0; i < no_tests; i++) {
369         test_assert(t[i].expect_next_index == results_next[i], "%s", t[i].name);
370         validate_packet(vm, t[i].name, results_bi[i],
371                         (vlib_buffer_t *)&expected[i]);
372     }
373     vec_free(results_next);
374     vec_free(results_bi);
375 }
376
377 void test_performance(void) {
378     pnat_main_t *pm = &pnat_main;
379     int i;
380     vlib_main_t *vm = vlib_get_first_main();
381
382     for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
383         add_translation(&rules[i]);
384     }
385     assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
386
387     int no_tests = sizeof(tests_packets) / sizeof(tests_packets[0]);
388     /* Generate packet data */
389     for (i = 0; i < VLIB_FRAME_SIZE; i++) {
390         // create input buffer(s)
391         fill_packets(vm, (vlib_buffer_t *)&buffers[i],
392                      tests_packets[i % no_tests].nsend,
393                      tests_packets[i % no_tests].send);
394         // fill_packets(vm, (vlib_buffer_t *)&expected[i], &tests[i %
395         // no_tests].expect);
396     }
397
398     /* send packets through graph node */
399     vlib_frame_t frame = {.n_vectors = VLIB_FRAME_SIZE};
400     node->flags &= ~VLIB_NODE_FLAG_TRACE;
401
402     int j;
403     for (j = 0; j < 10000; j++) {
404         pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
405
406 #if 0
407     for (i = 0; i < VLIB_FRAME_SIZE; i++) {
408         assert(tests[i % no_tests].expect_next_index == results_next[i]);
409         validate_packet(vm, tests[i % no_tests].name, results_bi[i], (vlib_buffer_t *)&expected[i]);
410     }
411 #endif
412         vec_free(results_next);
413         vec_free(results_bi);
414     }
415
416     for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
417         del_translation(&rules[i]);
418     }
419     assert(pool_elts(pm->translations) == 0);
420     assert(pool_elts(pm->interfaces) == 0);
421 }
422
423 void test_packets(void) {
424     pnat_main_t *pm = &pnat_main;
425     int i;
426     for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
427         add_translation(&rules[i]);
428     }
429     assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
430
431     test_table(tests_packets, sizeof(tests_packets) / sizeof(tests_packets[0]));
432
433     for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
434         del_translation(&rules[i]);
435     }
436     assert(pool_elts(pm->translations) == 0);
437     assert(pool_elts(pm->interfaces) == 0);
438 }
439
440 static void test_attach(void) {
441     pnat_attachment_point_t attachment = PNAT_IP4_INPUT;
442     u32 binding_index = 0;
443     u32 sw_if_index = 0;
444     int rv = pnat_binding_attach(sw_if_index, attachment, binding_index);
445     test_assert(rv == -1, "binding_attach - nothing to attach");
446
447     rv = pnat_binding_detach(sw_if_index, attachment, 1234);
448     test_assert(rv == -1, "binding_detach - nothing to detach");
449
450     pnat_match_tuple_t match = {.mask = PNAT_SA};
451     pnat_rewrite_tuple_t rewrite = {.mask = PNAT_SA};
452     rv = pnat_binding_add(&match, &rewrite, &binding_index);
453     assert(rv == 0);
454
455     rv = pnat_binding_attach(sw_if_index, attachment, binding_index);
456     test_assert(rv == 0, "binding_attach - rule");
457
458     rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
459     test_assert(rv == 0, "binding_detach - rule");
460
461     rv = pnat_binding_del(binding_index);
462     assert(rv == 0);
463 }
464
465 static void test_del_before_detach(void) {
466     pnat_attachment_point_t attachment = PNAT_IP4_INPUT;
467     u32 binding_index = 0;
468     u32 sw_if_index = 0;
469
470     /* Ensure 5-tuple here will not duplicate with other tests cause this will
471      * not be removed from flow cache */
472     rule_t rule = {
473         .match = {.dst = "123.123.123.123", .proto = 17, .dport = 6871},
474         .rewrite = {.dst = "1.2.3.4"},
475         .in = true,
476     };
477
478     add_translation(&rule);
479
480     int rv = pnat_binding_del(binding_index);
481     assert(rv == 0);
482
483     test_table(&tests_missing_rule[0], 1);
484
485     /* For now if you have deleted before detach, can't find key */
486     rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
487     test_assert(rv == -1, "binding_detach - failure");
488
489     /* Re-add the rule and try again */
490     pnat_match_tuple_t match = {0};
491     pnat_rewrite_tuple_t rewrite = {0};
492     ruletomatch(&rule.match, &match);
493     ruletorewrite(&rule.rewrite, &rewrite);
494     rv = pnat_binding_add(&match, &rewrite, &binding_index);
495     assert(rv == 0);
496     rv = pnat_binding_detach(sw_if_index, attachment, binding_index);
497     test_assert(rv == 0, "binding_detach - pass");
498     rv = pnat_binding_del(binding_index);
499     assert(rv == 0);
500 }
501
502 void test_api(void) {
503     test_attach();
504     test_del_before_detach();
505 }
506
507 void test_checksum(void) {
508     int i;
509     vlib_main_t *vm = vlib_get_first_main();
510     pnat_main_t *pm = &pnat_main;
511
512     test_t test = {
513         .name = "checksum",
514         .nsend = 28,
515         .send =
516             (char[]){0x45, 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
517                      0x74, 0xcb, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
518                      0x00, 0x50, 0x1a, 0xd7, 0x00, 0x08, 0xde, 0xb1},
519     };
520
521     for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
522         add_translation(&rules[i]);
523     }
524     assert(pool_elts(pm->translations) == sizeof(rules) / sizeof(rules[0]));
525
526     /* send packets through graph node */
527     vlib_frame_t frame = {.n_vectors = 1};
528     node->flags |= VLIB_NODE_FLAG_TRACE;
529
530     ip4_header_t *ip =
531         (ip4_header_t *)vlib_buffer_get_current((vlib_buffer_t *)&buffers[0]);
532
533     for (i = 0; i < 65535; i++) {
534
535         /* Get a buffer. Loop through 64K variations of it to check checksum */
536         memset(&buffers[0], 0, 2048);
537         fill_packets(vm, (vlib_buffer_t *)&buffers[0], test.nsend, test.send);
538
539         ip->src_address.as_u32 = i;
540         ip->checksum = 0;
541         ip->checksum = ip4_header_checksum(ip);
542         pnat_node_inline(vm, node, &frame, PNAT_IP4_INPUT, VLIB_RX);
543     }
544
545     test_assert_log(1, "%s", test.name);
546
547     for (i = 0; i < sizeof(rules) / sizeof(rules[0]); i++) {
548         del_translation(&rules[i]);
549     }
550 }
551
552 /*
553  * Unit testing:
554  * 1) Table of packets and expected outcomes. Run through
555  * 2) Performance tests. Measure instructions, cache behaviour etc.
556  */
557 clib_error_t *ip_checksum_init(vlib_main_t *vm);
558
559 int main(int argc, char **argv) {
560
561     clib_mem_init(0, 3ULL << 30);
562
563     vlib_main_init();
564     vlib_main_t *vm = vlib_get_first_main();
565
566     buffers_vector = buffer_init(buffers_vector, 256);
567
568     assert(vlib_node_main_init(vm) == 0);
569
570     ip_checksum_init(vm);
571
572     u32 node_index = vlib_register_node(vm, &pnat_input_node);
573     node = vlib_node_get_runtime(vm, node_index);
574     assert(node);
575
576     /* Test API */
577     test_api();
578     test_packets();
579     test_checksum();
580     test_performance();
581 }
582
583 /*
584  * NEW TESTS:
585  * - Chained buffers. Only do rewrite in first buffer
586  * - No interface. Can that really happen?
587  * - IP length shorter than buffer.
588  * - IP length longer than buffer.
589  */