FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
l3xc_api.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 <stddef.h>
17 
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <l3xc/l3xc.h>
21 #include <vnet/mpls/mpls_types.h>
22 #include <vnet/fib/fib_path_list.h>
23 #include <vnet/fib/fib_api.h>
24 
25 #include <vpp/app/version.h>
26 
27 #include <vlibapi/api.h>
28 #include <vlibmemory/api.h>
29 
30 /* define message IDs */
31 #include <l3xc/l3xc_msg_enum.h>
32 
33 /* define message structures */
34 #define vl_typedefs
35 #include <l3xc/l3xc_all_api_h.h>
36 #undef vl_typedefs
37 
38 /* define generated endian-swappers */
39 #define vl_endianfun
40 #include <l3xc/l3xc_all_api_h.h>
41 #undef vl_endianfun
42 
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
46 #include <l3xc/l3xc_all_api_h.h>
47 #undef vl_printfun
48 
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <l3xc/l3xc_all_api_h.h>
52 #undef vl_api_version
53 
54 /**
55  * Base message ID fot the plugin
56  */
58 
60 
61 /* List of message types that this plugin understands */
62 
63 #define foreach_l3xc_plugin_api_msg \
64  _(L3XC_PLUGIN_GET_VERSION, l3xc_plugin_get_version) \
65  _(L3XC_UPDATE, l3xc_update) \
66  _(L3XC_DEL, l3xc_del) \
67  _(L3XC_DUMP, l3xc_dump)
68 
69 static void
71  mp)
72 {
75 
77  if (rp == 0)
78  return;
79 
80  rmp = vl_msg_api_alloc (sizeof (*rmp));
81  rmp->_vl_msg_id =
82  ntohs (VL_API_L3XC_PLUGIN_GET_VERSION_REPLY + l3xc_base_msg_id);
83  rmp->context = mp->context;
84  rmp->major = htonl (L3XC_PLUGIN_VERSION_MAJOR);
85  rmp->minor = htonl (L3XC_PLUGIN_VERSION_MINOR);
86 
87  vl_api_send_msg (rp, (u8 *) rmp);
88 }
89 
90 static void
92 {
94  fib_route_path_t *paths = NULL, *path;
95  int rv = 0;
96  u8 pi;
97 
99 
100  if (0 == mp->l3xc.n_paths)
101  {
102  rv = VNET_API_ERROR_INVALID_VALUE;
103  goto done;
104  }
105 
106  vec_validate (paths, mp->l3xc.n_paths - 1);
107 
108  for (pi = 0; pi < mp->l3xc.n_paths; pi++)
109  {
110  path = &paths[pi];
111  rv = fib_api_path_decode (&mp->l3xc.paths[pi], path);
112 
113  if (0 != rv)
114  {
115  goto done;
116  }
117  }
118 
119  rv = l3xc_update (ntohl (mp->l3xc.sw_if_index), mp->l3xc.is_ip6, paths);
120 
121 done:
122  vec_free (paths);
123 
125 
126  /* *INDENT-OFF* */
127  REPLY_MACRO2 (VL_API_L3XC_UPDATE_REPLY + l3xc_base_msg_id,
128  ({
129  rmp->stats_index = 0;
130  }))
131  /* *INDENT-ON* */
132 }
133 
134 static void
136 {
137  vl_api_l3xc_del_reply_t *rmp;
138  int rv = 0;
139 
141 
142  rv = l3xc_delete (ntohl (mp->sw_if_index), mp->is_ip6);
143 
145 
146  REPLY_MACRO (VL_API_L3XC_DEL_REPLY + l3xc_base_msg_id);
147 }
148 
149 typedef struct l3xc_dump_walk_ctx_t_
150 {
154 
155 static int
156 l3xc_send_details (u32 l3xci, void *args)
157 {
158  fib_path_encode_ctx_t path_ctx = {
159  .rpaths = NULL,
160  };
163  fib_route_path_t *rpath;
164  vl_api_fib_path_t *fp;
165  size_t msg_size;
166  l3xc_t *l3xc;
167  u8 n_paths;
168 
169  ctx = args;
170  l3xc = l3xc_get (l3xci);
171  n_paths = fib_path_list_get_n_paths (l3xc->l3xc_pl);
172  msg_size = sizeof (*mp) + sizeof (mp->l3xc.paths[0]) * n_paths;
173 
174  mp = vl_msg_api_alloc (msg_size);
175  clib_memset (mp, 0, msg_size);
176  mp->_vl_msg_id = ntohs (VL_API_L3XC_DETAILS + l3xc_base_msg_id);
177 
178  /* fill in the message */
179  mp->context = ctx->context;
180  mp->l3xc.n_paths = n_paths;
181  mp->l3xc.sw_if_index = htonl (l3xc->l3xc_sw_if_index);
182 
183  fib_path_list_walk_w_ext (l3xc->l3xc_pl, NULL, fib_path_encode, &path_ctx);
184 
185  fp = mp->l3xc.paths;
186  vec_foreach (rpath, path_ctx.rpaths)
187  {
188  fib_api_path_encode (rpath, fp);
189  fp++;
190  }
191 
192  vl_api_send_msg (ctx->rp, (u8 *) mp);
193 
194  return (1);
195 }
196 
197 static void
199 {
202 
204  if (rp == 0)
205  return;
206 
208  .rp = rp,
209  .context = mp->context,
210  };
211 
212  sw_if_index = ntohl (mp->sw_if_index);
213 
214  if (~0 == sw_if_index)
216  else
217  {
218  fib_protocol_t fproto;
219  index_t l3xci;
220 
221  FOR_EACH_FIB_IP_PROTOCOL (fproto)
222  {
223  l3xci = l3xc_find (sw_if_index, fproto);
224 
225  if (INDEX_INVALID != l3xci)
226  l3xc_send_details (l3xci, &ctx);
227  }
228  }
229 }
230 
231 #define vl_msg_name_crc_list
232 #include <l3xc/l3xc_all_api_h.h>
233 #undef vl_msg_name_crc_list
234 
235 /* Set up the API message handling tables */
236 static clib_error_t *
238 {
239 #define _(N,n) \
240  vl_msg_api_set_handlers((VL_API_##N + l3xc_base_msg_id), \
241  #n, \
242  vl_api_##n##_t_handler, \
243  vl_noop_handler, \
244  vl_api_##n##_t_endian, \
245  vl_api_##n##_t_print, \
246  sizeof(vl_api_##n##_t), 1);
248 #undef _
249 
250  return 0;
251 }
252 
253 static void
255 {
256 #define _(id,n,crc) \
257  vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + l3xc_base_msg_id);
258  foreach_vl_msg_name_crc_l3xc;
259 #undef _
260 }
261 
262 static clib_error_t *
264 {
265  clib_error_t *error = 0;
266 
267  u8 *name = format (0, "l3xc_%08x%c", api_version, 0);
268 
269  /* Ask for a correctly-sized block of API message decode slots */
270  l3xc_base_msg_id = vl_msg_api_get_msg_ids ((char *) name,
272 
273  error = l3xc_plugin_api_hookup (vm);
274 
275  /* Add our API messages to the global name_crc hash table */
277 
278  vec_free (name);
279 
280  return error;
281 }
282 
284 
285 /* *INDENT-OFF* */
287  .version = VPP_BUILD_VER,
288  .description = "L3 Cross-Connect (L3XC)",
289 };
290 /* *INDENT-ON* */
291 
292 /*
293  * fd.io coding-style-patch-verification: ON
294  *
295  * Local Variables:
296  * eval: (c-set-style "gnu")
297  * End:
298  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
vl_api_l3xc_t l3xc
Definition: l3xc.api:97
#define L3XC_PLUGIN_VERSION_MAJOR
A L3 cross connect will send all traffic that is received on the input interface to the [set of] path...
Definition: l3xc.h:28
Definition: l3xc.h:33
Reply to get the plugin version.
Definition: l3xc.api:41
A representation of a path as described by a route producer.
Definition: fib_types.h:485
void fib_api_path_encode(const fib_route_path_t *rpath, vl_api_fib_path_t *out)
Definition: fib_api.c:358
#define REPLY_MACRO2(t, body)
static void vl_api_l3xc_plugin_get_version_t_handler(vl_api_l3xc_plugin_get_version_t *mp)
Definition: l3xc_api.c:70
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
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:41
int l3xc_delete(u32 sw_if_index, u8 is_ip6)
Delete an L3XC.
Definition: l3xc.c:156
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
static clib_error_t * l3xc_plugin_api_hookup(vlib_main_t *vm)
Definition: l3xc_api.c:237
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
unsigned int u32
Definition: types.h:88
static_always_inline l3xc_t * l3xc_get(u32 index)
Definition: l3xc.h:102
int l3xc_update(u32 sw_if_index, u8 is_ip6, const fib_route_path_t *rpaths)
Create or update an L3XC Policy.
Definition: l3xc.c:83
static clib_error_t * l3xc_api_init(vlib_main_t *vm)
Definition: l3xc_api.c:263
VLIB_PLUGIN_REGISTER()
Get the plugin version.
Definition: l3xc.api:30
#define L3XC_PLUGIN_VERSION_MINOR
Definition: l3xc.h:29
Dump all L3XC policies.
Definition: l3xc.api:85
u32 l3xc_sw_if_index
The input interface.
Definition: l3xc.h:56
long ctx[MAX_CONNS]
Definition: main.c:144
#define foreach_l3xc_plugin_api_msg
Definition: l3xc_api.c:63
vl_api_l3xc_t l3xc
Definition: l3xc.api:65
#define REPLY_MACRO(t)
vl_api_fib_path_t paths[n_paths]
Definition: l3xc.api:58
u8 name[64]
Definition: memclnt.api:152
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:203
static void vl_api_l3xc_update_t_handler(vl_api_l3xc_update_t *mp)
Definition: l3xc_api.c:91
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
#define BAD_SW_IF_INDEX_LABEL
vlib_main_t * vm
Definition: buffer.c:323
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static void vl_api_l3xc_dump_t_handler(vl_api_l3xc_dump_t *mp)
Definition: l3xc_api.c:198
u8 n_paths
Definition: ip.api:457
u32 fib_path_list_get_n_paths(fib_node_index_t path_list_index)
u8 n_paths
Definition: l3xc.api:57
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
void l3xc_walk(l3xc_walk_cb_t cb, void *ctx)
Walk/visit each of the L3XC policies.
Definition: l3xc.c:303
static int l3xc_send_details(u32 l3xci, void *args)
Definition: l3xc_api.c:156
static u32 l3xc_base_msg_id
Base message ID fot the plugin.
Definition: l3xc_api.c:57
index_t l3xc_find(u32 sw_if_index, fib_protocol_t fproto)
Find a L3 XC object from an interfce and FIB protocol.
Definition: l3xc.c:38
int fib_api_path_decode(vl_api_fib_path_t *in, fib_route_path_t *out)
Definition: fib_api.c:150
Path encode context to use when walking a path-list to encode paths.
Definition: fib_path.h:213
vl_api_mfib_path_t paths[n_paths]
Definition: ip.api:458
fib_route_path_t * rpaths
Definition: fib_path.h:215
static void setup_message_id_table(api_main_t *apim)
Definition: l3xc_api.c:254
struct l3xc_dump_walk_ctx_t_ l3xc_dump_walk_ctx_t
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:47
vl_api_registration_t * rp
Definition: l3xc_api.c:151
#define FOR_EACH_FIB_IP_PROTOCOL(_item)
Definition: fib_types.h:70
static void vl_api_l3xc_del_t_handler(vl_api_l3xc_del_t *mp)
Definition: l3xc_api.c:135
u32 sw_if_index
Definition: l3xc.api:55
#define vec_foreach(var, vec)
Vector iterator.
void fib_path_list_walk_w_ext(fib_node_index_t path_list_index, const fib_path_ext_list_t *ext_list, fib_path_list_walk_w_ext_fn_t func, void *ctx)
description returned in the dump
Definition: l3xc.api:94
fib_node_index_t l3xc_pl
The path-list describing how to forward in case of a match.
Definition: l3xc.h:44
api_main_t api_main
Definition: api_shared.c:35
#define VALIDATE_SW_IF_INDEX(mp)
fib_path_list_walk_rc_t fib_path_encode(fib_node_index_t path_list_index, fib_node_index_t path_index, const fib_path_ext_t *path_ext, void *args)
Definition: fib_path.c:2707
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:957