linux-cp: Linux Interface Mirroring for Control Plane Integration
[vpp.git] / src / plugins / linux-cp / test / lcp_unittest.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 #include <vlib/vlib.h>
16
17 #include <plugins/linux-cp/lcp_interface.h>
18
19 static u32 host_vif;
20 const static char *host_template = "tap%d";
21
22 static clib_error_t *
23 lcp_add_pair_command_fn (vlib_main_t *vm, unformat_input_t *input,
24                          vlib_cli_command_t *cmd)
25 {
26   u32 phy_sw_if_index, host_sw_if_index;
27   u8 is_add, *host_name;
28   vnet_main_t *vnm = vnet_get_main ();
29
30   ++host_vif;
31   host_name = format (NULL, host_template, host_vif);
32   phy_sw_if_index = host_sw_if_index = ~0;
33   is_add = 1;
34   lcp_main.test_mode = 1;
35
36   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
37     {
38       if (unformat (input, "add"))
39         is_add = 1;
40       else if (unformat (input, "del"))
41         is_add = 0;
42       else if (unformat (input, "phy %U", unformat_vnet_sw_interface, vnm,
43                          &phy_sw_if_index))
44         ;
45       else if (unformat (input, "host %U", unformat_vnet_sw_interface, vnm,
46                          &host_sw_if_index))
47         ;
48       else
49         return clib_error_return (0, "unknown input:%U", format_unformat_error,
50                                   input);
51     }
52
53   if (phy_sw_if_index == ~0)
54     return clib_error_return (0, "ERROR; no phy:%U", format_unformat_error,
55                               input);
56
57   lip_host_type_t host_type =
58     (vnet_sw_interface_is_p2p (vnm, phy_sw_if_index) ? LCP_ITF_HOST_TUN :
59                                                        LCP_ITF_HOST_TAP);
60
61   int rv;
62
63   if (is_add)
64     {
65       if (host_sw_if_index == ~0)
66         return clib_error_return (0, "ERROR no-host:%U", format_unformat_error,
67                                   input);
68
69       rv = lcp_itf_pair_add (host_sw_if_index, phy_sw_if_index, host_name,
70                              host_vif, host_type, NULL);
71     }
72   else
73     rv = lcp_itf_pair_del (phy_sw_if_index);
74
75   if (rv)
76     return clib_error_return (0, "ERROR rv:%d", rv);
77
78   return (NULL);
79 }
80
81 VLIB_CLI_COMMAND (test_time_range_command, static) = {
82   .path = "test lcp",
83   .short_help = "lcp [add|del] phy <SW_IF_INDEX> host <SW_IF_INDEX>",
84   .function = lcp_add_pair_command_fn,
85 };
86
87 #include <vnet/plugin/plugin.h>
88 #include <vpp/app/version.h>
89 VLIB_PLUGIN_REGISTER () = {
90   .version = VPP_BUILD_VER,
91   .description = "Linux Control Plane - Unit Test",
92   .default_disabled = 1,
93 };
94
95 /*
96  * fd.io coding-style-patch-verification: ON
97  *
98  * Local Variables:
99  * eval: (c-set-style "gnu")
100  * End:
101  */