FD.io VPP  v19.04.2-12-g66b1689
Vector Packet Processing
svs_api.c
Go to the documentation of this file.
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 <stddef.h>
17 
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <svs/svs.h>
21 #include <vnet/fib/fib_api.h>
22 #include <vnet/ip/ip_types_api.h>
23 
24 #include <vpp/app/version.h>
25 
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 
29 /* define message IDs */
30 #include <svs/svs_msg_enum.h>
31 
32 /* define message structures */
33 #define vl_typedefs
34 #include <svs/svs_all_api_h.h>
35 #undef vl_typedefs
36 
37 /* define generated endian-swappers */
38 #define vl_endianfun
39 #include <svs/svs_all_api_h.h>
40 #undef vl_endianfun
41 
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <svs/svs_all_api_h.h>
46 #undef vl_printfun
47 
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <svs/svs_all_api_h.h>
51 #undef vl_api_version
52 
53 /**
54  * Base message ID fot the plugin
55  */
58 
59 /* List of message types that this plugin understands */
60 
61 #define foreach_svs_plugin_api_msg \
62  _(SVS_PLUGIN_GET_VERSION, svs_plugin_get_version) \
63  _(SVS_TABLE_ADD_DEL, svs_table_add_del) \
64  _(SVS_ROUTE_ADD_DEL, svs_route_add_del) \
65  _(SVS_ENABLE_DISABLE, svs_enable_disable) \
66  _(SVS_DUMP, svs_dump)
67 
68 static void
70 {
72  int msg_size = sizeof (*rmp);
74 
76  if (rp == 0)
77  return;
78 
79  rmp = vl_msg_api_alloc (msg_size);
80  clib_memset (rmp, 0, msg_size);
81  rmp->_vl_msg_id =
82  ntohs (VL_API_SVS_PLUGIN_GET_VERSION_REPLY + svs_base_msg_id);
83  rmp->context = mp->context;
84  rmp->major = htonl (SVS_PLUGIN_VERSION_MAJOR);
85  rmp->minor = htonl (SVS_PLUGIN_VERSION_MINOR);
86 
87  vl_api_send_msg (rp, (u8 *) rmp);
88 }
89 
90 static void
92 {
93  vl_api_svs_table_add_del_reply_t *rmp;
94  fib_protocol_t fproto;
95  int rv = 0;
96 
98 
99  if (mp->is_add)
100  {
101  rv = svs_table_add (fproto, ntohl (mp->table_id));
102  }
103  else
104  {
105  rv = svs_table_delete (fproto, ntohl (mp->table_id));
106  }
107 
108  REPLY_MACRO (VL_API_SVS_TABLE_ADD_DEL_REPLY + svs_base_msg_id);
109 }
110 
111 static void
113 {
114  vl_api_svs_route_add_del_reply_t *rmp;
115  fib_prefix_t pfx;
116  int rv = 0;
117 
118  ip_prefix_decode (&mp->prefix, &pfx);
119 
120  if (mp->is_add)
121  {
122  rv = svs_route_add (ntohl (mp->table_id), &pfx,
123  ntohl (mp->source_table_id));
124  }
125  else
126  {
127  rv = svs_route_delete (ntohl (mp->table_id), &pfx);
128  }
129 
130  REPLY_MACRO (VL_API_SVS_ROUTE_ADD_DEL_REPLY + svs_base_msg_id);
131 }
132 
133 static void
135 {
136  vl_api_svs_enable_disable_reply_t *rmp;
137  fib_protocol_t fproto;
138  int rv = 0;
139 
141 
142  fproto = fib_proto_from_api_address_family (mp->af);
143 
144  if (mp->is_enable)
145  {
146  rv = svs_enable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
147  }
148  else
149  {
150  rv =
151  svs_disable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
152  }
153 
155  REPLY_MACRO (VL_API_SVS_ENABLE_DISABLE_REPLY + svs_base_msg_id);
156 }
157 
158 typedef struct svs_dump_walk_ctx_t_
159 {
163 
164 
165 static walk_rc_t
167  u32 table_id, u32 sw_if_index, void *args)
168 {
171 
172  ctx = args;
173 
174  mp = vl_msg_api_alloc (sizeof (*mp));
175  mp->_vl_msg_id = ntohs (VL_API_SVS_DETAILS + svs_base_msg_id);
176 
177  mp->context = ctx->context;
178  mp->sw_if_index = htonl (sw_if_index);
179  mp->table_id = htonl (table_id);
180  mp->af = fib_proto_to_api_address_family (fproto);
181 
182  vl_api_send_msg (ctx->rp, (u8 *) mp);
183 
184  return (WALK_CONTINUE);
185 }
186 
187 static void
189 {
191 
193  if (rp == 0)
194  return;
195 
197  .rp = rp,
198  .context = mp->context,
199  };
200 
201  svs_walk (svs_send_details, &ctx);
202 }
203 
204 #define vl_msg_name_crc_list
205 #include <svs/svs_all_api_h.h>
206 #undef vl_msg_name_crc_list
207 
208 /* Set up the API message handling tables */
209 static clib_error_t *
211 {
212 #define _(N,n) \
213  vl_msg_api_set_handlers((VL_API_##N + svs_base_msg_id), \
214  #n, \
215  vl_api_##n##_t_handler, \
216  vl_noop_handler, \
217  vl_api_##n##_t_endian, \
218  vl_api_##n##_t_print, \
219  sizeof(vl_api_##n##_t), 1);
221 #undef _
222 
223  return 0;
224 }
225 
226 static void
228 {
229 #define _(id,n,crc) \
230  vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + svs_base_msg_id);
231  foreach_vl_msg_name_crc_svs;
232 #undef _
233 }
234 
235 static clib_error_t *
237 {
238  clib_error_t *error = 0;
239 
240  u8 *name = format (0, "svs_%08x%c", api_version, 0);
241 
242  /* Ask for a correctly-sized block of API message decode slots */
243  svs_base_msg_id = vl_msg_api_get_msg_ids ((char *) name,
245 
246  error = svs_plugin_api_hookup (vm);
247 
248  /* Add our API messages to the global name_crc hash table */
250 
251  vec_free (name);
252 
253  return error;
254 }
255 
257 
258 /* *INDENT-OFF* */
260  .version = VPP_BUILD_VER,
261  .description = "Source Virtual Routing and Fowarding (VRF) Select",
262 };
263 /* *INDENT-ON* */
264 
265 /*
266  * fd.io coding-style-patch-verification: ON
267  *
268  * Local Variables:
269  * eval: (c-set-style "gnu")
270  * End:
271  */
#define SVS_PLUGIN_VERSION_MINOR
Definition: svs.h:28
u32 sw_if_index
Definition: ipsec_gre.api:37
vl_api_address_family_t af
Definition: svs.api:129
void ip_prefix_decode(const vl_api_prefix_t *in, fib_prefix_t *out)
Definition: ip_types_api.c:122
int svs_route_delete(u32 table_id, const fib_prefix_t *pfx)
Definition: svs.c:101
static void vl_api_svs_dump_t_handler(vl_api_svs_dump_t *mp)
Definition: svs_api.c:188
struct svs_dump_walk_ctx_t_ svs_dump_walk_ctx_t
SVS table-id to interface mapping.
Definition: svs.api:124
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:34
int svs_disable(fib_protocol_t fproto, u32 table_id, u32 sw_if_index)
Definition: svs.c:193
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
void * vl_msg_api_alloc(int nbytes)
#define foreach_svs_plugin_api_msg
Definition: svs_api.c:61
unsigned char u8
Definition: types.h:56
Dump the SVS table mappings of table_id to interface To see the routes added to a given table use ip_...
Definition: svs.api:111
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
enum walk_rc_t_ walk_rc_t
Walk return code.
Add a table in which to add routes that will match against source addresses.
Definition: svs.api:59
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
fib_protocol_t fib_proto_from_api_address_family(int af)
Definition: fib_api.c:326
vl_api_prefix_t prefix
Definition: svs.api:83
Aggregrate type for a prefix.
Definition: fib_types.h:203
unsigned int u32
Definition: types.h:88
int svs_enable(fib_protocol_t fproto, u32 table_id, u32 sw_if_index)
Definition: svs.c:116
VLIB_PLUGIN_REGISTER()
long ctx[MAX_CONNS]
Definition: main.c:144
static void vl_api_svs_route_add_del_t_handler(vl_api_svs_route_add_del_t *mp)
Definition: svs_api.c:112
int svs_table_delete(fib_protocol_t fproto, u32 table_id)
Definition: svs.c:38
#define REPLY_MACRO(t)
static void vl_api_svs_enable_disable_t_handler(vl_api_svs_enable_disable_t *mp)
Definition: svs_api.c:134
int fib_proto_to_api_address_family(fib_protocol_t fproto)
Definition: fib_api.c:341
static clib_error_t * svs_plugin_api_hookup(vlib_main_t *vm)
Definition: svs_api.c:210
u8 name[64]
Definition: memclnt.api:152
u32 client_index
Definition: svs.api:113
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:202
An API client registration, only in vpp/vlib.
Definition: api_common.h:45
#define BAD_SW_IF_INDEX_LABEL
vlib_main_t * vm
Definition: buffer.c:312
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
vl_api_address_family_t af
Definition: svs.api:102
static walk_rc_t svs_send_details(fib_protocol_t fproto, u32 table_id, u32 sw_if_index, void *args)
Definition: svs_api.c:166
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:56
vl_api_registration_t * rp
Definition: svs_api.c:160
Reply to get the plugin version.
Definition: svs.api:43
static u32 svs_base_msg_id
Base message ID fot the plugin.
Definition: svs_api.c:56
void svs_walk(svs_walk_fn_t fn, void *ctx)
Definition: svs.c:223
static void vl_api_svs_plugin_get_version_t_handler(vl_api_svs_plugin_get_version_t *mp)
Definition: svs_api.c:69
int svs_table_add(fib_protocol_t fproto, u32 table_id)
Definition: svs.c:30
#define SVS_PLUGIN_VERSION_MAJOR
Source VRF Selection matches against a packet&#39;s source address to set the VRF in which the subsequnet...
Definition: svs.h:27
static void setup_message_id_table(api_main_t *apim)
Definition: svs_api.c:227
brief Get the plugin version
Definition: svs.api:31
int svs_route_add(u32 table_id, const fib_prefix_t *pfx, u32 source_table_id)
Definition: svs.c:80
Enable SVS on a given interface by using the given table to match RX&#39;d packets&#39; source addresses...
Definition: svs.api:97
vl_api_address_family_t af
Definition: svs.api:64
static clib_error_t * svs_api_init(vlib_main_t *vm)
Definition: svs_api.c:236
api_main_t api_main
Definition: api_shared.c:35
static void vl_api_svs_table_add_del_t_handler(vl_api_svs_table_add_del_t *mp)
Definition: svs_api.c:91
#define VALIDATE_SW_IF_INDEX(mp)
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:880
Add a route into the source address matching table.
Definition: svs.api:78