FD.io VPP  v18.10-34-gcce845e
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  */
57 
59 
60 /* List of message types that this plugin understands */
61 
62 #define foreach_svs_plugin_api_msg \
63  _(SVS_PLUGIN_GET_VERSION, svs_plugin_get_version) \
64  _(SVS_TABLE_ADD_DEL, svs_table_add_del) \
65  _(SVS_ROUTE_ADD_DEL, svs_route_add_del) \
66  _(SVS_ENABLE_DISABLE, svs_enable_disable) \
67  _(SVS_DUMP, svs_dump)
68 
69 static void
71 {
73  int msg_size = sizeof (*rmp);
75 
77  if (q == 0)
78  {
79  return;
80  }
81 
82  rmp = vl_msg_api_alloc (msg_size);
83  memset (rmp, 0, msg_size);
84  rmp->_vl_msg_id =
85  ntohs (VL_API_SVS_PLUGIN_GET_VERSION_REPLY + svs_base_msg_id);
86  rmp->context = mp->context;
87  rmp->major = htonl (SVS_PLUGIN_VERSION_MAJOR);
88  rmp->minor = htonl (SVS_PLUGIN_VERSION_MINOR);
89 
90  vl_msg_api_send_shmem (q, (u8 *) & rmp);
91 }
92 
93 static void
95 {
96  vl_api_svs_table_add_del_reply_t *rmp;
97  fib_protocol_t fproto;
98  int rv = 0;
99 
100  fproto = fib_proto_from_api_address_family (mp->af);
101 
102  if (mp->is_add)
103  {
104  rv = svs_table_add (fproto, ntohl (mp->table_id));
105  }
106  else
107  {
108  rv = svs_table_delete (fproto, ntohl (mp->table_id));
109  }
110 
111  REPLY_MACRO (VL_API_SVS_TABLE_ADD_DEL_REPLY + svs_base_msg_id);
112 }
113 
114 static void
116 {
117  vl_api_svs_route_add_del_reply_t *rmp;
118  fib_prefix_t pfx;
119  int rv = 0;
120 
121  ip_prefix_decode (&mp->prefix, &pfx);
122 
123  if (mp->is_add)
124  {
125  rv = svs_route_add (ntohl (mp->table_id), &pfx,
126  ntohl (mp->source_table_id));
127  }
128  else
129  {
130  rv = svs_route_delete (ntohl (mp->table_id), &pfx);
131  }
132 
133  REPLY_MACRO (VL_API_SVS_ROUTE_ADD_DEL_REPLY + svs_base_msg_id);
134 }
135 
136 static void
138 {
139  vl_api_svs_enable_disable_reply_t *rmp;
140  fib_protocol_t fproto;
141  int rv = 0;
142 
144 
145  fproto = fib_proto_from_api_address_family (mp->af);
146 
147  if (mp->is_enable)
148  {
149  rv = svs_enable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
150  }
151  else
152  {
153  rv =
154  svs_disable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
155  }
156 
158  REPLY_MACRO (VL_API_SVS_ENABLE_DISABLE_REPLY + svs_base_msg_id);
159 }
160 
161 typedef struct svs_dump_walk_ctx_t_
162 {
166 
167 
168 static walk_rc_t
170  u32 table_id, u32 sw_if_index, void *args)
171 {
174 
175  ctx = args;
176 
177  mp = vl_msg_api_alloc (sizeof (*mp));
178  mp->_vl_msg_id = ntohs (VL_API_SVS_DETAILS + svs_base_msg_id);
179 
180  mp->context = ctx->context;
181  mp->sw_if_index = htonl (sw_if_index);
182  mp->table_id = htonl (table_id);
183  mp->af = fib_proto_to_api_address_family (fproto);
184 
185  vl_msg_api_send_shmem (ctx->q, (u8 *) & mp);
186 
187  return (WALK_CONTINUE);
188 }
189 
190 static void
192 {
194 
196  if (q == 0)
197  {
198  return;
199  }
200 
202  .q = q,
203  .context = mp->context,
204  };
205 
206  svs_walk (svs_send_details, &ctx);
207 }
208 
209 #define vl_msg_name_crc_list
210 #include <svs/svs_all_api_h.h>
211 #undef vl_msg_name_crc_list
212 
213 /* Set up the API message handling tables */
214 static clib_error_t *
216 {
217 #define _(N,n) \
218  vl_msg_api_set_handlers((VL_API_##N + svs_base_msg_id), \
219  #n, \
220  vl_api_##n##_t_handler, \
221  vl_noop_handler, \
222  vl_api_##n##_t_endian, \
223  vl_api_##n##_t_print, \
224  sizeof(vl_api_##n##_t), 1);
226 #undef _
227 
228  return 0;
229 }
230 
231 static void
233 {
234 #define _(id,n,crc) \
235  vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + svs_base_msg_id);
236  foreach_vl_msg_name_crc_svs;
237 #undef _
238 }
239 
240 static clib_error_t *
242 {
243  clib_error_t *error = 0;
244 
245  u8 *name = format (0, "svs_%08x%c", api_version, 0);
246 
247  /* Ask for a correctly-sized block of API message decode slots */
248  svs_base_msg_id = vl_msg_api_get_msg_ids ((char *) name,
250 
251  error = svs_plugin_api_hookup (vm);
252 
253  /* Add our API messages to the global name_crc hash table */
255 
256  vec_free (name);
257 
258  return error;
259 }
260 
262 
263 /* *INDENT-OFF* */
265  .version = VPP_BUILD_VER,
266  .description = "Source VRF Select",
267 };
268 /* *INDENT-ON* */
269 
270 /*
271  * fd.io coding-style-patch-verification: ON
272  *
273  * Local Variables:
274  * eval: (c-set-style "gnu")
275  * End:
276  */
#define SVS_PLUGIN_VERSION_MINOR
Definition: svs.h:28
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:98
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:191
struct svs_dump_walk_ctx_t_ svs_dump_walk_ctx_t
SVS table-id to interface mapping.
Definition: svs.api:124
int svs_disable(fib_protocol_t fproto, u32 table_id, u32 sw_if_index)
Definition: svs.c:193
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
void * vl_msg_api_alloc(int nbytes)
#define foreach_svs_plugin_api_msg
Definition: svs_api.c:62
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
memset(h->entries, 0, sizeof(h->entries[0])*entries)
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
u32 sw_if_index
Definition: vxlan_gbp.api:39
fib_protocol_t fib_proto_from_api_address_family(int af)
Definition: fib_api.c:280
svm_queue_t unix_shared_memory_queue_t
Definition: queue.h:132
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:115
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:137
int fib_proto_to_api_address_family(fib_protocol_t fproto)
Definition: fib_api.c:295
static clib_error_t * svs_plugin_api_hookup(vlib_main_t *vm)
Definition: svs_api.c:215
u8 name[64]
Definition: memclnt.api:151
u32 client_index
Definition: svs.api:113
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:201
#define BAD_SW_IF_INDEX_LABEL
void vl_msg_api_send_shmem(svm_queue_t *q, u8 *elem)
vlib_main_t * vm
Definition: buffer.c:294
svm_queue_t * vl_api_client_index_to_input_queue(u32 index)
Definition: memory_api.c:759
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
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:169
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:218
unix_shared_memory_queue_t * q
Definition: svs_api.c:163
static void vl_api_svs_plugin_get_version_t_handler(vl_api_svs_plugin_get_version_t *mp)
Definition: svs_api.c:70
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:232
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:241
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:94
#define VALIDATE_SW_IF_INDEX(mp)
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:865
Add a route into the source address matching table.
Definition: svs.api:78