FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
gbp_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * gbp_api.c - layer 2 emulation api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <vnet/vnet.h>
21 #include <vnet/plugin/plugin.h>
22 
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vpp/app/version.h>
26 
27 #include <gbp/gbp.h>
28 
29 #include <vlibapi/api.h>
30 #include <vlibmemory/api.h>
31 
32 /* define message IDs */
33 #include <gbp/gbp_msg_enum.h>
34 
35 #define vl_typedefs /* define message structures */
36 #include <gbp/gbp_all_api_h.h>
37 #undef vl_typedefs
38 
39 #define vl_endianfun /* define message structures */
40 #include <gbp/gbp_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 <gbp/gbp_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 <acl/acl_all_api_h.h>
52 #undef vl_api_version
53 
55 
56 #define foreach_gbp_api_msg \
57  _(GBP_ENDPOINT_ADD_DEL, gbp_endpoint_add_del) \
58  _(GBP_ENDPOINT_DUMP, gbp_endpoint_dump) \
59  _(GBP_CONTRACT_ADD_DEL, gbp_contract_add_del) \
60  _(GBP_CONTRACT_DUMP, gbp_contract_dump)
61 
62 /**
63  * L2 Emulation Main
64  */
65 typedef struct gbp_main_t_
66 {
68 } gbp_main_t;
69 
71 
72 #define GBP_MSG_BASE gbp_main.msg_id_base
73 
74 static void
76 {
77  vl_api_gbp_endpoint_add_del_reply_t *rmp;
78  ip46_address_t ip = { };
79  u32 sw_if_index;
80  int rv = 0;
81 
82  sw_if_index = ntohl (mp->endpoint.sw_if_index);
83  if (!vnet_sw_if_index_is_api_valid (sw_if_index))
84  goto bad_sw_if_index;
85 
86  if (mp->endpoint.is_ip6)
87  {
88  clib_memcpy (&ip.ip6, mp->endpoint.address, sizeof (ip.ip6));
89  }
90  else
91  {
92  clib_memcpy (&ip.ip4, mp->endpoint.address, sizeof (ip.ip4));
93  }
94 
95  if (mp->is_add)
96  {
97  gbp_endpoint_update (sw_if_index, &ip, ntohl (mp->endpoint.epg_id));
98  }
99  else
100  {
101  gbp_endpoint_delete (sw_if_index, &ip);
102  }
103 
105 
106  REPLY_MACRO (VL_API_GBP_ENDPOINT_ADD_DEL_REPLY + GBP_MSG_BASE);
107 }
108 
109 typedef struct gbp_walk_ctx_t_
110 {
114 
115 static int
117 {
120 
121  ctx = args;
122  mp = vl_msg_api_alloc (sizeof (*mp));
123  if (!mp)
124  return 1;
125 
126  memset (mp, 0, sizeof (*mp));
127  mp->_vl_msg_id = ntohs (VL_API_GBP_ENDPOINT_DETAILS + GBP_MSG_BASE);
128  mp->context = ctx->context;
129 
130  mp->endpoint.sw_if_index = ntohl (gbpe->ge_key->gek_sw_if_index);
132  if (mp->endpoint.is_ip6)
134  &gbpe->ge_key->gek_ip.ip6,
135  sizeof (gbpe->ge_key->gek_ip.ip6));
136  else
138  &gbpe->ge_key->gek_ip.ip4,
139  sizeof (gbpe->ge_key->gek_ip.ip4));
140 
141  mp->endpoint.epg_id = ntohl (gbpe->ge_epg_id);
142 
143  vl_api_send_msg (ctx->reg, (u8 *) mp);
144 
145  return (1);
146 }
147 
148 static void
150 {
152 
154  if (!reg)
155  return;
156 
157  gbp_walk_ctx_t ctx = {
158  .reg = reg,
159  .context = mp->context,
160  };
161 
163 }
164 
165 static void
167 {
168  vl_api_gbp_contract_add_del_reply_t *rmp;
169  int rv = 0;
170 
171  if (mp->is_add)
172  gbp_contract_update (ntohl (mp->contract.src_epg),
173  ntohl (mp->contract.dst_epg),
174  ntohl (mp->contract.acl_index));
175  else
176  gbp_contract_delete (ntohl (mp->contract.src_epg),
177  ntohl (mp->contract.dst_epg));
178 
179  REPLY_MACRO (VL_API_GBP_CONTRACT_ADD_DEL_REPLY + GBP_MSG_BASE);
180 }
181 
182 static int
184 {
187 
188  ctx = args;
189  mp = vl_msg_api_alloc (sizeof (*mp));
190  if (!mp)
191  return 1;
192 
193  memset (mp, 0, sizeof (*mp));
194  mp->_vl_msg_id = ntohs (VL_API_GBP_CONTRACT_DETAILS + GBP_MSG_BASE);
195  mp->context = ctx->context;
196 
197  mp->contract.src_epg = ntohl (gbpc->gc_key.gck_src);
198  mp->contract.dst_epg = ntohl (gbpc->gc_key.gck_dst);
199  mp->contract.acl_index = ntohl (gbpc->gc_acl_index);
200 
201  vl_api_send_msg (ctx->reg, (u8 *) mp);
202 
203  return (1);
204 }
205 
206 static void
208 {
210 
212  if (!reg)
213  return;
214 
215  gbp_walk_ctx_t ctx = {
216  .reg = reg,
217  .context = mp->context,
218  };
219 
221 }
222 
223 /*
224  * gbp_api_hookup
225  * Add vpe's API message handlers to the table.
226  * vlib has alread mapped shared memory and
227  * added the client registration handlers.
228  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
229  */
230 #define vl_msg_name_crc_list
231 #include <gbp/gbp_all_api_h.h>
232 #undef vl_msg_name_crc_list
233 
234 static void
236 {
237 #define _(id,n,crc) \
238  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + GBP_MSG_BASE);
239  foreach_vl_msg_name_crc_gbp;
240 #undef _
241 }
242 
243 static void
245 {
246 #define _(N,n) \
247  vl_msg_api_set_handlers(VL_API_##N + GBP_MSG_BASE, \
248  #n, \
249  vl_api_##n##_t_handler, \
250  vl_noop_handler, \
251  vl_api_##n##_t_endian, \
252  vl_api_##n##_t_print, \
253  sizeof(vl_api_##n##_t), 1);
255 #undef _
256 }
257 
258 static clib_error_t *
260 {
261  api_main_t *am = &api_main;
262  gbp_main_t *gbpm = &gbp_main;
263  u8 *name = format (0, "gbp_%08x%c", api_version, 0);
264 
265  /* Ask for a correctly-sized block of API message decode slots */
266  gbpm->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
268 
269  gbp_api_hookup (vm);
270 
271  /* Add our API messages to the global name_crc hash table */
273 
274  vec_free (name);
275  return (NULL);
276 }
277 
279 
280 /* *INDENT-OFF* */
282  .version = VPP_BUILD_VER,
283  .description = "Group Based Policy",
284 };
285 /* *INDENT-ON* */
286 
287 
288 /*
289  * fd.io coding-style-patch-verification: ON
290  *
291  * Local Variables:
292  * eval: (c-set-style "gnu")
293  * End:
294  */
VLIB_PLUGIN_REGISTER()
static int gbp_contract_send_details(gbp_contract_t *gbpc, void *args)
Definition: gbp_api.c:183
A Group Based Policy Endpoint.
Definition: gbp.h:62
gbp_contract_key_t gc_key
source and destination EPGs
Definition: gbp.h:111
static gbp_main_t gbp_main
Definition: gbp_api.c:70
#define NULL
Definition: clib.h:55
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:34
gbp_endpoint_key_t * ge_key
The endpoint&#39;s interface and IP address.
Definition: gbp.h:67
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
void * vl_msg_api_alloc(int nbytes)
static void vl_api_gbp_contract_add_del_t_handler(vl_api_gbp_contract_add_del_t *mp)
Definition: gbp_api.c:166
void gbp_contract_walk(gbp_contract_cb_t cb, void *ctx)
Definition: gbp.c:260
u32 gek_sw_if_index
The interface on which the EP is connected.
Definition: gbp.h:49
static void setup_message_id_table(api_main_t *am)
Definition: gbp_api.c:235
void gbp_contract_delete(epg_id_t src_epg, epg_id_t dst_epg)
Definition: gbp.c:249
void gbp_endpoint_delete(u32 sw_if_index, const ip46_address_t *ip)
Definition: gbp.c:197
u16 msg_id_base
Definition: gbp_api.c:67
L2 Emulation Main.
Definition: gbp_api.c:65
static void vl_api_gbp_contract_dump_t_handler(vl_api_gbp_contract_dump_t *mp)
Definition: gbp_api.c:207
#define REPLY_MACRO(t)
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:199
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:76
struct gbp_main_t_ gbp_main_t
L2 Emulation Main.
An API client registration, only in vpp/vlib.
Definition: api_common.h:44
#define BAD_SW_IF_INDEX_LABEL
static uword vnet_sw_if_index_is_api_valid(u32 sw_if_index)
ip46_address_t gek_ip
The IP[46] address of the endpoint.
Definition: gbp.h:54
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
#define clib_memcpy(a, b, c)
Definition: string.h:75
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:56
epg_id_t gck_src
source and destination EPGs for which the ACL applies
Definition: gbp.h:95
static void vl_api_gbp_endpoint_dump_t_handler(vl_api_gbp_endpoint_dump_t *mp)
Definition: gbp_api.c:149
vl_api_gbp_contract_t contract
Definition: gbp.api:64
vl_api_gbp_endpoint_t endpoint
Definition: gbp.api:37
unsigned int u32
Definition: types.h:88
long ctx[MAX_CONNS]
Definition: main.c:126
static clib_error_t * gbp_init(vlib_main_t *vm)
Definition: gbp_api.c:259
static void gbp_api_hookup(vlib_main_t *vm)
Definition: gbp_api.c:244
static void vl_api_gbp_endpoint_add_del_t_handler(vl_api_gbp_endpoint_add_del_t *mp)
Definition: gbp_api.c:75
static int gbp_endpoint_send_details(gbp_endpoint_t *gbpe, void *args)
Definition: gbp_api.c:116
vl_api_gbp_contract_t contract
Definition: gbp.api:76
void gbp_endpoint_update(u32 sw_if_index, const ip46_address_t *ip, epg_id_t epg_id)
Definition: gbp.c:164
unsigned short u16
Definition: types.h:57
vl_api_gbp_endpoint_t endpoint
Definition: gbp.api:49
unsigned char u8
Definition: types.h:56
epg_id_t ge_epg_id
The endpoint&#39;s designated EPG.
Definition: gbp.h:72
vl_api_registration_t * reg
Definition: gbp_api.c:111
u32 gc_acl_index
The ACL to apply for packets from the source to the destination EPG.
Definition: gbp.h:116
#define GBP_MSG_BASE
Definition: gbp_api.c:72
void gbp_endpoint_walk(gbp_endpoint_cb_t cb, void *ctx)
Definition: gbp.c:224
VLIB_API_INIT_FUNCTION(gbp_init)
struct gbp_walk_ctx_t_ gbp_walk_ctx_t
api_main_t api_main
Definition: api_shared.c:35
void gbp_contract_update(epg_id_t src_epg, epg_id_t dst_epg, u32 acl_index)
Definition: gbp.c:238
epg_id_t gck_dst
Definition: gbp.h:96
A Group Based Policy Contract.
Definition: gbp.h:106
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:872
#define foreach_gbp_api_msg
Definition: gbp_api.c:56