ipsec: Reference count the SAs
[vpp.git] / src / plugins / unittest / ipsec_test.c
1 /*
2  * Copyright (c) 2018 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 <vnet/ipsec/ipsec.h>
17 #include <vnet/ipsec/ipsec_sa.h>
18
19 static clib_error_t *
20 test_ipsec_command_fn (vlib_main_t * vm,
21                        unformat_input_t * input, vlib_cli_command_t * cmd)
22 {
23   u64 seq_num;
24   u32 sa_id;
25
26   sa_id = ~0;
27   seq_num = 0;
28
29   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
30     {
31       if (unformat (input, "sa %d", &sa_id))
32         ;
33       else if (unformat (input, "seq 0x%llx", &seq_num))
34         ;
35       else
36         break;
37     }
38
39   if (~0 != sa_id)
40     {
41       ipsec_main_t *im = &ipsec_main;
42       ipsec_sa_t *sa;
43       u32 sa_index;
44
45       sa_index = ipsec_sa_find_and_lock (sa_id);
46       sa = pool_elt_at_index (im->sad, sa_index);
47
48       sa->seq = seq_num & 0xffffffff;
49       sa->seq_hi = seq_num >> 32;
50
51       ipsec_sa_unlock (sa_index);
52     }
53   else
54     {
55       return clib_error_return (0, "unknown SA `%U'",
56                                 format_unformat_error, input);
57     }
58
59   return (NULL);
60 }
61
62 /* *INDENT-OFF* */
63 VLIB_CLI_COMMAND (test_ipsec_command, static) =
64 {
65   .path = "test ipsec",
66   .short_help = "test ipsec sa <ID> seq-num <VALUE>",
67   .function = test_ipsec_command_fn,
68 };
69 /* *INDENT-ON* */
70
71 /*
72  * fd.io coding-style-patch-verification: ON
73  *
74  * Local Variables:
75  * eval: (c-set-style "gnu")
76  * End:
77  */