FD.io VPP  v20.01-48-g3e0dafb74
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 <vnet/format_fns.h>
31 #include <svs/svs.api_enum.h>
32 #include <svs/svs.api_types.h>
33 
34 /**
35  * Base message ID fot the plugin
36  */
39 
40 static void
42 {
44  int msg_size = sizeof (*rmp);
46 
48  if (rp == 0)
49  return;
50 
51  rmp = vl_msg_api_alloc (msg_size);
52  clib_memset (rmp, 0, msg_size);
53  rmp->_vl_msg_id =
54  ntohs (VL_API_SVS_PLUGIN_GET_VERSION_REPLY + svs_base_msg_id);
55  rmp->context = mp->context;
56  rmp->major = htonl (SVS_PLUGIN_VERSION_MAJOR);
57  rmp->minor = htonl (SVS_PLUGIN_VERSION_MINOR);
58 
59  vl_api_send_msg (rp, (u8 *) rmp);
60 }
61 
62 static void
64 {
65  vl_api_svs_table_add_del_reply_t *rmp;
66  fib_protocol_t fproto;
67  int rv = 0;
68 
70 
71  if (mp->is_add)
72  {
73  rv = svs_table_add (fproto, ntohl (mp->table_id));
74  }
75  else
76  {
77  rv = svs_table_delete (fproto, ntohl (mp->table_id));
78  }
79 
80  REPLY_MACRO (VL_API_SVS_TABLE_ADD_DEL_REPLY + svs_base_msg_id);
81 }
82 
83 static void
85 {
86  vl_api_svs_route_add_del_reply_t *rmp;
87  fib_prefix_t pfx;
88  int rv = 0;
89 
90  ip_prefix_decode (&mp->prefix, &pfx);
91 
92  if (mp->is_add)
93  {
94  rv = svs_route_add (ntohl (mp->table_id), &pfx,
95  ntohl (mp->source_table_id));
96  }
97  else
98  {
99  rv = svs_route_delete (ntohl (mp->table_id), &pfx);
100  }
101 
102  REPLY_MACRO (VL_API_SVS_ROUTE_ADD_DEL_REPLY + svs_base_msg_id);
103 }
104 
105 static void
107 {
108  vl_api_svs_enable_disable_reply_t *rmp;
109  fib_protocol_t fproto;
110  int rv = 0;
111 
113 
114  fproto = fib_proto_from_api_address_family (mp->af);
115 
116  if (mp->is_enable)
117  {
118  rv = svs_enable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
119  }
120  else
121  {
122  rv =
123  svs_disable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
124  }
125 
127  REPLY_MACRO (VL_API_SVS_ENABLE_DISABLE_REPLY + svs_base_msg_id);
128 }
129 
130 typedef struct svs_dump_walk_ctx_t_
131 {
135 
136 
137 static walk_rc_t
139  u32 table_id, u32 sw_if_index, void *args)
140 {
143 
144  ctx = args;
145 
146  mp = vl_msg_api_alloc (sizeof (*mp));
147  mp->_vl_msg_id = ntohs (VL_API_SVS_DETAILS + svs_base_msg_id);
148 
149  mp->context = ctx->context;
150  mp->sw_if_index = htonl (sw_if_index);
151  mp->table_id = htonl (table_id);
152  mp->af = fib_proto_to_api_address_family (fproto);
153 
154  vl_api_send_msg (ctx->rp, (u8 *) mp);
155 
156  return (WALK_CONTINUE);
157 }
158 
159 static void
161 {
163 
165  if (rp == 0)
166  return;
167 
169  .rp = rp,
170  .context = mp->context,
171  };
172 
173  svs_walk (svs_send_details, &ctx);
174 }
175 
176 #include <svs/svs.api.c>
177 static clib_error_t *
179 {
180  /* Ask for a correctly-sized block of API message decode slots */
182 
183  return 0;
184 }
185 
187 
188 /* *INDENT-OFF* */
190  .version = VPP_BUILD_VER,
191  .description = "Source Virtual Routing and Fowarding (VRF) Select",
192 };
193 /* *INDENT-ON* */
194 
195 /*
196  * fd.io coding-style-patch-verification: ON
197  *
198  * Local Variables:
199  * eval: (c-set-style "gnu")
200  * End:
201  */
#define SVS_PLUGIN_VERSION_MINOR
Definition: svs.h:28
vl_api_address_family_t af
Definition: svs.api:130
void ip_prefix_decode(const vl_api_prefix_t *in, fib_prefix_t *out)
Definition: ip_types_api.c:200
int svs_route_delete(u32 table_id, const fib_prefix_t *pfx)
Definition: svs.c:103
static void vl_api_svs_dump_t_handler(vl_api_svs_dump_t *mp)
Definition: svs_api.c:160
struct svs_dump_walk_ctx_t_ svs_dump_walk_ctx_t
SVS table-id to interface mapping.
Definition: svs.api:125
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
int svs_disable(fib_protocol_t fproto, u32 table_id, u32 sw_if_index)
Definition: svs.c:195
vl_api_interface_index_t sw_if_index
Definition: svs.api:105
void * vl_msg_api_alloc(int nbytes)
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:112
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:60
vl_api_interface_index_t sw_if_index
Definition: gre.api:59
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
fib_protocol_t fib_proto_from_api_address_family(int af)
Definition: fib_api.c:537
vl_api_prefix_t prefix
Definition: svs.api:84
Aggregate 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:118
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:84
int svs_table_delete(fib_protocol_t fproto, u32 table_id)
Definition: svs.c:40
#define REPLY_MACRO(t)
static void vl_api_svs_enable_disable_t_handler(vl_api_svs_enable_disable_t *mp)
Definition: svs_api.c:106
int fib_proto_to_api_address_family(fib_protocol_t fproto)
Definition: fib_api.c:552
vlib_main_t * vm
Definition: in2out_ed.c:1810
u32 client_index
Definition: svs.api:114
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
#define BAD_SW_IF_INDEX_LABEL
vl_api_address_family_t af
Definition: svs.api:103
vl_api_interface_index_t sw_if_index
Definition: svs.api:129
static walk_rc_t svs_send_details(fib_protocol_t fproto, u32 table_id, u32 sw_if_index, void *args)
Definition: svs_api.c:138
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
vl_api_registration_t * rp
Definition: svs_api.c:132
Reply to get the plugin version.
Definition: svs.api:44
static u32 svs_base_msg_id
Base message ID fot the plugin.
Definition: svs_api.c:37
void svs_walk(svs_walk_fn_t fn, void *ctx)
Definition: svs.c:225
static void vl_api_svs_plugin_get_version_t_handler(vl_api_svs_plugin_get_version_t *mp)
Definition: svs_api.c:41
int svs_table_add(fib_protocol_t fproto, u32 table_id)
Definition: svs.c:32
#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
brief Get the plugin version
Definition: svs.api:32
int svs_route_add(u32 table_id, const fib_prefix_t *pfx, u32 source_table_id)
Definition: svs.c:82
Enable SVS on a given interface by using the given table to match RX&#39;d packets&#39; source addresses...
Definition: svs.api:98
static void setup_message_id_table(snat_main_t *sm, api_main_t *am)
Definition: nat_api.c:3410
vl_api_address_family_t af
Definition: svs.api:65
u32 table_id
Definition: fib_types.api:118
static clib_error_t * svs_api_init(vlib_main_t *vm)
Definition: svs_api.c:178
static void vl_api_svs_table_add_del_t_handler(vl_api_svs_table_add_del_t *mp)
Definition: svs_api.c:63
#define VALIDATE_SW_IF_INDEX(mp)
Add a route into the source address matching table.
Definition: svs.api:79