2 * Copyright 2020 Rubicon Communications, LLC.
4 * SPDX-License-Identifier: Apache-2.0
7 #include <sys/socket.h>
10 #include <vnet/vnet.h>
11 #include <vnet/plugin/plugin.h>
13 #include <vlibapi/api.h>
14 #include <vlibmemory/api.h>
15 #include <vpp/app/version.h>
16 #include <vnet/format_fns.h>
18 #include <linux-cp/lcp_interface.h>
19 #include <linux-cp/lcp.api_enum.h>
20 #include <linux-cp/lcp.api_types.h>
22 static u16 lcp_msg_id_base;
23 #define REPLY_MSG_ID_BASE lcp_msg_id_base
24 #include <vlibapi/api_helper_macros.h>
26 static lip_host_type_t
27 api_decode_host_type (vl_api_lcp_itf_host_type_t type)
29 if (type == LCP_API_ITF_HOST_TUN)
30 return LCP_ITF_HOST_TUN;
32 return LCP_ITF_HOST_TAP;
35 static vl_api_lcp_itf_host_type_t
36 api_encode_host_type (lip_host_type_t type)
38 if (type == LCP_ITF_HOST_TUN)
39 return LCP_API_ITF_HOST_TUN;
41 return LCP_API_ITF_HOST_TAP;
45 vl_api_lcp_itf_pair_add (u32 phy_sw_if_index, lip_host_type_t lip_host_type,
46 u8 *mp_host_if_name, size_t sizeof_host_if_name,
47 u8 *mp_namespace, size_t sizeof_mp_namespace,
48 u32 *host_sw_if_index_p)
50 u8 *host_if_name, *netns;
51 int host_len, netns_len, rv;
53 host_if_name = netns = 0;
55 /* lcp_itf_pair_create expects vec of u8 */
56 host_len = clib_strnlen ((char *) mp_host_if_name, sizeof_host_if_name - 1);
57 vec_add (host_if_name, mp_host_if_name, host_len);
58 vec_add1 (host_if_name, 0);
60 netns_len = clib_strnlen ((char *) mp_namespace, sizeof_mp_namespace - 1);
61 vec_add (netns, mp_namespace, netns_len);
64 rv = lcp_itf_pair_create (phy_sw_if_index, host_if_name, lip_host_type,
65 netns, host_sw_if_index_p);
67 vec_free (host_if_name);
74 vl_api_lcp_itf_pair_add_del_t_handler (vl_api_lcp_itf_pair_add_del_t *mp)
77 vl_api_lcp_itf_pair_add_del_reply_t *rmp;
78 lip_host_type_t lip_host_type;
81 VALIDATE_SW_IF_INDEX_END (mp);
83 phy_sw_if_index = mp->sw_if_index;
84 lip_host_type = api_decode_host_type (mp->host_if_type);
87 rv = vl_api_lcp_itf_pair_add (
88 phy_sw_if_index, lip_host_type, mp->host_if_name,
89 sizeof (mp->host_if_name), mp->netns, sizeof (mp->netns), NULL);
93 rv = lcp_itf_pair_delete (phy_sw_if_index);
96 BAD_SW_IF_INDEX_LABEL;
97 REPLY_MACRO_END (VL_API_LCP_ITF_PAIR_ADD_DEL_REPLY);
101 vl_api_lcp_itf_pair_add_del_v2_t_handler (vl_api_lcp_itf_pair_add_del_v2_t *mp)
103 u32 phy_sw_if_index, host_sw_if_index = ~0;
104 vl_api_lcp_itf_pair_add_del_v2_reply_t *rmp;
105 lip_host_type_t lip_host_type;
108 VALIDATE_SW_IF_INDEX_END (mp);
110 phy_sw_if_index = mp->sw_if_index;
111 lip_host_type = api_decode_host_type (mp->host_if_type);
114 rv = vl_api_lcp_itf_pair_add (phy_sw_if_index, lip_host_type,
116 sizeof (mp->host_if_name), mp->netns,
117 sizeof (mp->netns), &host_sw_if_index);
121 rv = lcp_itf_pair_delete (phy_sw_if_index);
124 BAD_SW_IF_INDEX_LABEL;
125 REPLY_MACRO2_END (VL_API_LCP_ITF_PAIR_ADD_DEL_V2_REPLY,
126 { rmp->host_sw_if_index = host_sw_if_index; });
130 send_lcp_itf_pair_details (index_t lipi, vl_api_registration_t *rp,
133 vl_api_lcp_itf_pair_details_t *rmp;
134 lcp_itf_pair_t *lcp_pair = lcp_itf_pair_get (lipi);
136 REPLY_MACRO_DETAILS4 (
137 VL_API_LCP_ITF_PAIR_DETAILS, rp, context, ({
138 rmp->phy_sw_if_index = ntohl (lcp_pair->lip_phy_sw_if_index);
139 rmp->host_sw_if_index = ntohl (lcp_pair->lip_host_sw_if_index);
140 rmp->vif_index = ntohl (lcp_pair->lip_vif_index);
141 rmp->host_if_type = api_encode_host_type (lcp_pair->lip_host_type);
143 memcpy_s (rmp->host_if_name, sizeof (rmp->host_if_name),
144 lcp_pair->lip_host_name, vec_len (lcp_pair->lip_host_name));
145 rmp->host_if_name[vec_len (lcp_pair->lip_host_name)] = 0;
147 memcpy_s (rmp->netns, sizeof (rmp->netns), lcp_pair->lip_namespace,
148 vec_len (lcp_pair->lip_namespace));
149 rmp->netns[vec_len (lcp_pair->lip_namespace)] = 0;
154 vl_api_lcp_itf_pair_get_t_handler (vl_api_lcp_itf_pair_get_t *mp)
156 vl_api_lcp_itf_pair_get_reply_t *rmp;
159 REPLY_AND_DETAILS_MACRO (
160 VL_API_LCP_ITF_PAIR_GET_REPLY, lcp_itf_pair_pool,
161 ({ send_lcp_itf_pair_details (cursor, rp, mp->context); }));
165 vl_api_lcp_default_ns_set_t_handler (vl_api_lcp_default_ns_set_t *mp)
167 vl_api_lcp_default_ns_set_reply_t *rmp;
170 mp->netns[LCP_NS_LEN - 1] = 0;
171 rv = lcp_set_default_ns (mp->netns);
173 REPLY_MACRO (VL_API_LCP_DEFAULT_NS_SET_REPLY);
177 vl_api_lcp_default_ns_get_t_handler (vl_api_lcp_default_ns_get_t *mp)
179 vl_api_lcp_default_ns_get_reply_t *rmp;
180 vl_api_registration_t *reg;
183 reg = vl_api_client_index_to_registration (mp->client_index);
187 REPLY_MACRO_DETAILS2_END (VL_API_LCP_DEFAULT_NS_GET_REPLY, ({
188 ns = (char *) lcp_get_default_ns ();
190 clib_strncpy ((char *) rmp->netns, ns,
196 vl_api_lcp_itf_pair_replace_begin_t_handler (
197 vl_api_lcp_itf_pair_replace_begin_t *mp)
199 vl_api_lcp_itf_pair_replace_begin_reply_t *rmp;
202 rv = lcp_itf_pair_replace_begin ();
204 REPLY_MACRO (VL_API_LCP_ITF_PAIR_REPLACE_BEGIN_REPLY);
208 vl_api_lcp_itf_pair_replace_end_t_handler (
209 vl_api_lcp_itf_pair_replace_end_t *mp)
211 vl_api_lcp_itf_pair_replace_end_reply_t *rmp;
214 rv = lcp_itf_pair_replace_end ();
216 REPLY_MACRO (VL_API_LCP_ITF_PAIR_REPLACE_END_REPLY);
220 * Set up the API message handling tables
222 #include <linux-cp/lcp.api.c>
224 static clib_error_t *
225 lcp_api_init (vlib_main_t *vm)
227 /* Ask for a correctly-sized block of API message decode slots */
228 lcp_msg_id_base = setup_message_id_table ();
233 VLIB_INIT_FUNCTION (lcp_api_init);
235 #include <vpp/app/version.h>
236 VLIB_PLUGIN_REGISTER () = {
237 .version = VPP_BUILD_VER,
238 .description = "Linux Control Plane - Interface Mirror",
239 .default_disabled = 1,
243 * fd.io coding-style-patch-verification: ON
246 * eval: (c-set-style "gnu")