FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
lcp_api.c
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Rubicon Communications, LLC.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <sys/socket.h>
8 #include <linux/if.h>
9 
10 #include <vnet/vnet.h>
11 #include <vnet/plugin/plugin.h>
12 
13 #include <vlibapi/api.h>
14 #include <vlibmemory/api.h>
15 #include <vpp/app/version.h>
16 #include <vnet/format_fns.h>
17 
18 #include <linux-cp/lcp_interface.h>
19 #include <linux-cp/lcp.api_enum.h>
20 #include <linux-cp/lcp.api_types.h>
21 
23 #define REPLY_MSG_ID_BASE lcp_msg_id_base
25 
26 static lip_host_type_t
27 api_decode_host_type (vl_api_lcp_itf_host_type_t type)
28 {
30  return LCP_ITF_HOST_TUN;
31 
32  return LCP_ITF_HOST_TAP;
33 }
34 
35 static vl_api_lcp_itf_host_type_t
37 {
38  if (type == LCP_ITF_HOST_TUN)
39  return LCP_API_ITF_HOST_TUN;
40 
41  return LCP_API_ITF_HOST_TAP;
42 }
43 
44 static int
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)
49 {
50  u8 *host_if_name, *netns;
51  int host_len, netns_len, rv;
52 
53  host_if_name = netns = 0;
54 
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);
59 
60  netns_len = clib_strnlen ((char *) mp_namespace, sizeof_mp_namespace - 1);
61  vec_add (netns, mp_namespace, netns_len);
62  vec_add1 (netns, 0);
63 
64  rv = lcp_itf_pair_create (phy_sw_if_index, host_if_name, lip_host_type,
65  netns, host_sw_if_index_p);
66 
67  vec_free (host_if_name);
68  vec_free (netns);
69 
70  return rv;
71 }
72 
73 static void
75 {
76  u32 phy_sw_if_index;
77  vl_api_lcp_itf_pair_add_del_reply_t *rmp;
78  lip_host_type_t lip_host_type;
79  int rv;
80 
82  {
83  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
84  goto bad_sw_if_index;
85  }
86 
87  phy_sw_if_index = mp->sw_if_index;
88  lip_host_type = api_decode_host_type (mp->host_if_type);
89  if (mp->is_add)
90  {
91  rv =
92  vl_api_lcp_itf_pair_add (phy_sw_if_index, lip_host_type,
93  mp->host_if_name, sizeof (mp->host_if_name),
94  mp->namespace, sizeof (mp->namespace), NULL);
95  }
96  else
97  {
98  rv = lcp_itf_pair_delete (phy_sw_if_index);
99  }
100 
102  REPLY_MACRO (VL_API_LCP_ITF_PAIR_ADD_DEL_REPLY);
103 }
104 
105 static void
107 {
108  u32 phy_sw_if_index, host_sw_if_index = ~0;
110  lip_host_type_t lip_host_type;
111  int rv;
112 
114  {
115  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
116  goto bad_sw_if_index;
117  }
118 
119  phy_sw_if_index = mp->sw_if_index;
120  lip_host_type = api_decode_host_type (mp->host_if_type);
121  if (mp->is_add)
122  {
123  rv = vl_api_lcp_itf_pair_add (phy_sw_if_index, lip_host_type,
124  mp->host_if_name,
125  sizeof (mp->host_if_name), mp->namespace,
126  sizeof (mp->namespace), &host_sw_if_index);
127  }
128  else
129  {
130  rv = lcp_itf_pair_delete (phy_sw_if_index);
131  }
132 
134  REPLY_MACRO2 (VL_API_LCP_ITF_PAIR_ADD_DEL_V2_REPLY,
135  { rmp->host_sw_if_index = ntohl (host_sw_if_index); });
136 }
137 
138 static void
140  u32 context)
141 {
143  lcp_itf_pair_t *lcp_pair = lcp_itf_pair_get (lipi);
144 
146  VL_API_LCP_ITF_PAIR_DETAILS, rp, context, ({
147  rmp->phy_sw_if_index = lcp_pair->lip_phy_sw_if_index;
148  rmp->host_sw_if_index = lcp_pair->lip_host_sw_if_index;
149  rmp->vif_index = lcp_pair->lip_vif_index;
151 
152  memcpy_s (rmp->host_if_name, sizeof (rmp->host_if_name),
153  lcp_pair->lip_host_name, vec_len (lcp_pair->lip_host_name));
154 
155  clib_strncpy ((char *) rmp->namespace, (char *) lcp_pair->lip_namespace,
156  vec_len (lcp_pair->lip_namespace));
157  }));
158 }
159 
160 static void
162 {
164  i32 rv = 0;
165 
167  VL_API_LCP_ITF_PAIR_GET_REPLY, lcp_itf_pair_pool,
168  ({ send_lcp_itf_pair_details (cursor, rp, mp->context); }));
169 }
170 
171 static void
173 {
174  vl_api_lcp_default_ns_set_reply_t *rmp;
175  int rv;
176 
177  mp->namespace[LCP_NS_LEN - 1] = 0;
178  rv = lcp_set_default_ns (mp->namespace);
179 
180  REPLY_MACRO (VL_API_LCP_DEFAULT_NS_SET_REPLY);
181 }
182 
183 static void
185 {
186  lcp_main_t *lcpm = &lcp_main;
189  char *ns;
190 
192  if (!reg)
193  return;
194 
195  rmp = vl_msg_api_alloc (sizeof (*rmp));
196  clib_memset (rmp, 0, sizeof (*rmp));
197  rmp->_vl_msg_id = (VL_API_LCP_DEFAULT_NS_GET_REPLY + lcpm->msg_id_base);
198  rmp->context = mp->context;
199 
200  ns = (char *) lcp_get_default_ns ();
201  if (ns)
202  clib_strncpy ((char *) rmp->namespace, ns, LCP_NS_LEN - 1);
203 
204  vl_api_send_msg (reg, (u8 *) rmp);
205 }
206 
207 static void
210 {
211  vl_api_lcp_itf_pair_replace_begin_reply_t *rmp;
212  int rv;
213 
215 
216  REPLY_MACRO (VL_API_LCP_ITF_PAIR_REPLACE_BEGIN_REPLY);
217 }
218 
219 static void
222 {
223  vl_api_lcp_itf_pair_replace_end_reply_t *rmp;
224  int rv = 0;
225 
227 
228  REPLY_MACRO (VL_API_LCP_ITF_PAIR_REPLACE_END_REPLY);
229 }
230 
231 /*
232  * Set up the API message handling tables
233  */
234 #include <linux-cp/lcp.api.c>
235 
236 static clib_error_t *
238 {
239  /* Ask for a correctly-sized block of API message decode slots */
241 
242  return (NULL);
243 }
244 
246 
247 #include <vpp/app/version.h>
249  .version = VPP_BUILD_VER,
250  .description = "Linux Control Plane - Interface Mirror",
251  .default_disabled = 1,
252 };
253 
254 /*
255  * fd.io coding-style-patch-verification: ON
256  *
257  * Local Variables:
258  * eval: (c-set-style "gnu")
259  * End:
260  */
clib_strncpy
#define clib_strncpy(d, s, n)
Definition: string.h:992
vl_api_lcp_itf_pair_details_t
Linux Control Plane interface pair dump response.
Definition: lcp.api:126
lcp_itf_pair_t_::lip_namespace
u8 * lip_namespace
Definition: lcp_interface.h:55
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
api.h
vec_add
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:688
lcp_itf_pair_t_
A pair of interfaces.
Definition: lcp_interface.h:49
REPLY_MACRO2
#define REPLY_MACRO2(t, body)
Definition: api_helper_macros.h:65
vl_api_lcp_itf_pair_add_del_v2_reply_t
Definition: lcp.api:92
api_decode_host_type
static lip_host_type_t api_decode_host_type(vl_api_lcp_itf_host_type_t type)
Definition: lcp_api.c:27
vl_api_send_msg
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
lcp_main_s::msg_id_base
u16 msg_id_base
Definition: lcp.h:24
lcp_get_default_ns
u8 * lcp_get_default_ns(void)
Definition: lcp.c:27
vl_api_lcp_itf_pair_get_t
Dump Linux Control Plane interface pair data.
Definition: lcp.api:104
memcpy_s
__clib_export errno_t memcpy_s(void *__restrict__ dest, rsize_t dmax, const void *__restrict__ src, rsize_t n)
copy src to dest, at most n bytes, up to dmax
Definition: string.c:120
vl_api_lcp_itf_pair_replace_end_t
Definition: lcp.api:149
api_encode_host_type
static vl_api_lcp_itf_host_type_t api_encode_host_type(lip_host_type_t type)
Definition: lcp_api.c:36
vl_api_lcp_itf_pair_add
static int vl_api_lcp_itf_pair_add(u32 phy_sw_if_index, lip_host_type_t lip_host_type, u8 *mp_host_if_name, size_t sizeof_host_if_name, u8 *mp_namespace, size_t sizeof_mp_namespace, u32 *host_sw_if_index_p)
Definition: lcp_api.c:45
vl_api_lcp_itf_pair_add_del_t_handler
static void vl_api_lcp_itf_pair_add_del_t_handler(vl_api_lcp_itf_pair_add_del_t *mp)
Definition: lcp_api.c:74
vl_api_lcp_itf_pair_details_t::vif_index
u32 vif_index
Definition: lcp.api:131
u16
unsigned short u16
Definition: types.h:57
vl_api_lcp_itf_pair_details_t::host_if_type
vl_api_lcp_itf_host_type_t host_if_type
Definition: lcp.api:133
vl_api_lcp_itf_pair_add_del_t::is_add
bool is_add
Definition: lcp.api:76
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
api.h
vl_api_lcp_default_ns_get_reply_t
get the default Linux Control Plane namespace
Definition: lcp.api:51
vl_api_lcp_itf_pair_get_t_handler
static void vl_api_lcp_itf_pair_get_t_handler(vl_api_lcp_itf_pair_get_t *mp)
Definition: lcp_api.c:161
lcp_itf_pair_t_::lip_phy_sw_if_index
u32 lip_phy_sw_if_index
Definition: lcp_interface.h:52
lcp_main_s
Definition: lcp.h:22
vl_api_lcp_itf_pair_get_reply_t
Definition: lcp.api:110
vl_api_lcp_itf_pair_details_t::host_if_name
string host_if_name[16]
Definition: lcp.api:132
i32
signed int i32
Definition: types.h:77
lcp_itf_pair_get
lcp_itf_pair_t * lcp_itf_pair_get(u32 index)
Get an interface-pair object from its VPP index.
Definition: lcp_interface.c:156
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
lcp_itf_pair_create
int lcp_itf_pair_create(u32 phy_sw_if_index, u8 *host_if_name, lip_host_type_t host_if_type, u8 *ns, u32 *host_sw_if_indexp)
Create an interface-pair from PHY sw_if_index and tap name.
Definition: lcp_interface.c:727
vl_api_lcp_itf_pair_add_del_t
Add or delete a Linux Conrol Plane interface pair.
Definition: lcp.api:72
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
vl_api_lcp_itf_pair_add_del_v2_t::host_if_name
string host_if_name[16]
Definition: lcp.api:88
vl_api_lcp_itf_pair_add_del_v2_reply_t::host_sw_if_index
vl_api_interface_index_t host_sw_if_index
Definition: lcp.api:96
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
REPLY_MACRO
#define REPLY_MACRO(t)
Definition: api_helper_macros.h:30
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: sr_mpls_api.c:174
vl_api_lcp_itf_pair_replace_begin_t_handler
static void vl_api_lcp_itf_pair_replace_begin_t_handler(vl_api_lcp_itf_pair_replace_begin_t *mp)
Definition: lcp_api.c:208
index_t
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:43
REPLY_MACRO_DETAILS4
#define REPLY_MACRO_DETAILS4(t, rp, context, body)
Definition: api_helper_macros.h:132
lcp_itf_pair_t_::lip_host_name
u8 * lip_host_name
Definition: lcp_interface.h:53
lcp_plugin_api_hookup
static clib_error_t * lcp_plugin_api_hookup(vlib_main_t *vm)
Definition: lcp_api.c:237
lcp_itf_pair_replace_begin
int lcp_itf_pair_replace_begin(void)
Begin and End the replace process.
Definition: lcp_interface.c:1049
lip_host_type_t
lip_host_type_t
Definition: lcp_interface.h:33
REPLY_AND_DETAILS_MACRO
#define REPLY_AND_DETAILS_MACRO(t, p, body)
Definition: api_helper_macros.h:204
lcp_itf_pair_t_::lip_vif_index
u32 lip_vif_index
Definition: lcp_interface.h:54
vl_api_lcp_default_ns_get_reply_t::context
u32 context
Definition: lcp.api:53
vl_api_lcp_default_ns_set_t_handler
static void vl_api_lcp_default_ns_set_t_handler(vl_api_lcp_default_ns_set_t *mp)
Definition: lcp_api.c:172
lcp_itf_pair_t_::lip_host_sw_if_index
u32 lip_host_sw_if_index
Definition: lcp_interface.h:51
LCP_ITF_HOST_TUN
@ LCP_ITF_HOST_TUN
Definition: lcp_interface.h:36
vl_api_lcp_itf_pair_details_t::phy_sw_if_index
vl_api_interface_index_t phy_sw_if_index
Definition: lcp.api:129
vl_api_lcp_itf_pair_add_del_v2_t_handler
static void vl_api_lcp_itf_pair_add_del_v2_t_handler(vl_api_lcp_itf_pair_add_del_v2_t *mp)
Definition: lcp_api.c:106
LCP_NS_LEN
#define LCP_NS_LEN
Definition: lcp.h:20
vl_api_lcp_default_ns_get_t_handler
static void vl_api_lcp_default_ns_get_t_handler(vl_api_lcp_default_ns_get_t *mp)
Definition: lcp_api.c:184
BAD_SW_IF_INDEX_LABEL
#define BAD_SW_IF_INDEX_LABEL
Definition: api_helper_macros.h:289
lcp_itf_pair_t_::lip_host_type
lip_host_type_t lip_host_type
Definition: lcp_interface.h:56
plugin.h
lcp_msg_id_base
static u16 lcp_msg_id_base
Definition: lcp_api.c:22
vnet_sw_if_index_is_api_valid
static uword vnet_sw_if_index_is_api_valid(u32 sw_if_index)
Definition: api_helper_macros.h:276
lcp_itf_pair_delete
int lcp_itf_pair_delete(u32 phy_sw_if_index)
Delete a LCP_ITF_PAIR.
Definition: lcp_interface.c:499
clib_strnlen
#define clib_strnlen(s, m)
Definition: string.h:782
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
vl_api_lcp_itf_pair_replace_end_t_handler
static void vl_api_lcp_itf_pair_replace_end_t_handler(vl_api_lcp_itf_pair_replace_end_t *mp)
Definition: lcp_api.c:220
format_fns.h
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
api_helper_macros.h
vl_api_lcp_itf_pair_add_del_v2_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: lcp.api:87
vl_api_lcp_default_ns_get_t
get the default Linux Control Plane namespace
Definition: lcp.api:40
vl_api_lcp_itf_pair_add_del_v2_t::is_add
bool is_add
Definition: lcp.api:86
vl_api_lcp_default_ns_get_t::context
u32 context
Definition: lcp.api:43
vl_api_lcp_itf_pair_add_del_t::host_if_type
vl_api_lcp_itf_host_type_t host_if_type
Definition: lcp.api:79
LCP_API_ITF_HOST_TAP
@ LCP_API_ITF_HOST_TAP
Definition: lcp.api:59
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
lcp_main
lcp_main_t lcp_main
Definition: lcp.c:24
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
VLIB_PLUGIN_REGISTER
VLIB_PLUGIN_REGISTER()
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
vl_api_lcp_default_ns_get_t::client_index
u32 client_index
Definition: lcp.api:42
vl_api_lcp_itf_pair_get_t::context
u32 context
Definition: lcp.api:107
lcp_itf_pair_pool
lcp_itf_pair_t * lcp_itf_pair_pool
Pool of LIP objects.
Definition: lcp_interface.c:47
context
u32 context
Definition: ip.api:852
rv
int __clib_unused rv
Definition: application.c:491
vnet.h
vl_api_lcp_default_ns_set_t
Set the default Linux Control Plane namespace.
Definition: lcp.api:29
send_lcp_itf_pair_details
static void send_lcp_itf_pair_details(index_t lipi, vl_api_registration_t *rp, u32 context)
Definition: lcp_api.c:139
vl_api_lcp_itf_pair_add_del_t::host_if_name
string host_if_name[16]
Definition: lcp.api:78
LCP_ITF_HOST_TAP
@ LCP_ITF_HOST_TAP
Definition: lcp_interface.h:35
vl_api_lcp_itf_pair_add_del_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: lcp.api:77
vl_api_lcp_itf_pair_add_del_v2_t::host_if_type
vl_api_lcp_itf_host_type_t host_if_type
Definition: lcp.api:89
lcp_itf_pair_replace_end
int lcp_itf_pair_replace_end(void)
Definition: lcp_interface.c:1076
lcp_set_default_ns
int lcp_set_default_ns(u8 *ns)
Get/Set the default namespace for LCP host taps.
Definition: lcp.c:49
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
LCP_API_ITF_HOST_TUN
@ LCP_API_ITF_HOST_TUN
Definition: lcp.api:60
lcp_interface.h
vl_api_lcp_itf_pair_details_t::host_sw_if_index
vl_api_interface_index_t host_sw_if_index
Definition: lcp.api:130
vl_api_lcp_itf_pair_replace_begin_t
Replace end/begin.
Definition: lcp.api:144
vl_api_lcp_itf_pair_add_del_v2_t
Definition: lcp.api:82
vl_msg_api_alloc
void * vl_msg_api_alloc(int nbytes)
Definition: memory_shared.c:199