gbp: Add bd flags
[vpp.git] / src / plugins / gbp / gbp_api.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20
21 #include <vnet/interface.h>
22 #include <vnet/api_errno.h>
23 #include <vnet/ip/ip_types_api.h>
24 #include <vnet/ethernet/ethernet_types_api.h>
25 #include <vpp/app/version.h>
26
27 #include <gbp/gbp.h>
28 #include <gbp/gbp_learn.h>
29 #include <gbp/gbp_itf.h>
30 #include <gbp/gbp_vxlan.h>
31 #include <gbp/gbp_bridge_domain.h>
32 #include <gbp/gbp_route_domain.h>
33 #include <gbp/gbp_ext_itf.h>
34
35 #include <vlibapi/api.h>
36 #include <vlibmemory/api.h>
37
38 /* define message IDs */
39 #include <gbp/gbp_msg_enum.h>
40
41 #define vl_typedefs             /* define message structures */
42 #include <gbp/gbp_all_api_h.h>
43 #undef vl_typedefs
44
45 #define vl_endianfun            /* define message structures */
46 #include <gbp/gbp_all_api_h.h>
47 #undef vl_endianfun
48
49 /* instantiate all the print functions we know about */
50 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
51 #define vl_printfun
52 #include <gbp/gbp_all_api_h.h>
53 #undef vl_printfun
54
55 /* Get the API version number */
56 #define vl_api_version(n,v) static u32 api_version=(v);
57 #include <gbp/gbp_all_api_h.h>
58 #undef vl_api_version
59
60 #include <vlibapi/api_helper_macros.h>
61
62 #define foreach_gbp_api_msg                                 \
63   _(GBP_ENDPOINT_ADD, gbp_endpoint_add)                     \
64   _(GBP_ENDPOINT_DEL, gbp_endpoint_del)                     \
65   _(GBP_ENDPOINT_DUMP, gbp_endpoint_dump)                   \
66   _(GBP_SUBNET_ADD_DEL, gbp_subnet_add_del)                 \
67   _(GBP_SUBNET_DUMP, gbp_subnet_dump)                       \
68   _(GBP_ENDPOINT_GROUP_ADD, gbp_endpoint_group_add)         \
69   _(GBP_ENDPOINT_GROUP_DEL, gbp_endpoint_group_del)         \
70   _(GBP_ENDPOINT_GROUP_DUMP, gbp_endpoint_group_dump)       \
71   _(GBP_BRIDGE_DOMAIN_ADD, gbp_bridge_domain_add)           \
72   _(GBP_BRIDGE_DOMAIN_DEL, gbp_bridge_domain_del)           \
73   _(GBP_BRIDGE_DOMAIN_DUMP, gbp_bridge_domain_dump)         \
74   _(GBP_ROUTE_DOMAIN_ADD, gbp_route_domain_add)             \
75   _(GBP_ROUTE_DOMAIN_DEL, gbp_route_domain_del)             \
76   _(GBP_ROUTE_DOMAIN_DUMP, gbp_route_domain_dump)           \
77   _(GBP_RECIRC_ADD_DEL, gbp_recirc_add_del)                 \
78   _(GBP_RECIRC_DUMP, gbp_recirc_dump)                       \
79   _(GBP_EXT_ITF_ADD_DEL, gbp_ext_itf_add_del)               \
80   _(GBP_EXT_ITF_DUMP, gbp_ext_itf_dump)                     \
81   _(GBP_CONTRACT_ADD_DEL, gbp_contract_add_del)             \
82   _(GBP_CONTRACT_DUMP, gbp_contract_dump)                   \
83   _(GBP_VXLAN_TUNNEL_ADD, gbp_vxlan_tunnel_add)             \
84   _(GBP_VXLAN_TUNNEL_DEL, gbp_vxlan_tunnel_del)             \
85   _(GBP_VXLAN_TUNNEL_DUMP, gbp_vxlan_tunnel_dump)
86
87 gbp_main_t gbp_main;
88
89 static u16 msg_id_base;
90
91 #define GBP_MSG_BASE msg_id_base
92
93 static gbp_endpoint_flags_t
94 gbp_endpoint_flags_decode (vl_api_gbp_endpoint_flags_t v)
95 {
96   gbp_endpoint_flags_t f = GBP_ENDPOINT_FLAG_NONE;
97
98   v = ntohl (v);
99
100   if (v & GBP_API_ENDPOINT_FLAG_BOUNCE)
101     f |= GBP_ENDPOINT_FLAG_BOUNCE;
102   if (v & GBP_API_ENDPOINT_FLAG_REMOTE)
103     f |= GBP_ENDPOINT_FLAG_REMOTE;
104   if (v & GBP_API_ENDPOINT_FLAG_LEARNT)
105     f |= GBP_ENDPOINT_FLAG_LEARNT;
106   if (v & GBP_API_ENDPOINT_FLAG_EXTERNAL)
107     f |= GBP_ENDPOINT_FLAG_EXTERNAL;
108
109   return (f);
110 }
111
112 static vl_api_gbp_endpoint_flags_t
113 gbp_endpoint_flags_encode (gbp_endpoint_flags_t f)
114 {
115   vl_api_gbp_endpoint_flags_t v = 0;
116
117
118   if (f & GBP_ENDPOINT_FLAG_BOUNCE)
119     v |= GBP_API_ENDPOINT_FLAG_BOUNCE;
120   if (f & GBP_ENDPOINT_FLAG_REMOTE)
121     v |= GBP_API_ENDPOINT_FLAG_REMOTE;
122   if (f & GBP_ENDPOINT_FLAG_LEARNT)
123     v |= GBP_API_ENDPOINT_FLAG_LEARNT;
124   if (f & GBP_ENDPOINT_FLAG_EXTERNAL)
125     v |= GBP_API_ENDPOINT_FLAG_EXTERNAL;
126
127   v = htonl (v);
128
129   return (v);
130 }
131
132 static void
133 vl_api_gbp_endpoint_add_t_handler (vl_api_gbp_endpoint_add_t * mp)
134 {
135   vl_api_gbp_endpoint_add_reply_t *rmp;
136   gbp_endpoint_flags_t gef;
137   u32 sw_if_index, handle;
138   ip46_address_t *ips;
139   mac_address_t mac;
140   int rv = 0, ii;
141
142   VALIDATE_SW_IF_INDEX (&(mp->endpoint));
143
144   gef = gbp_endpoint_flags_decode (mp->endpoint.flags), ips = NULL;
145   sw_if_index = ntohl (mp->endpoint.sw_if_index);
146
147   if (mp->endpoint.n_ips)
148     {
149       vec_validate (ips, mp->endpoint.n_ips - 1);
150
151       vec_foreach_index (ii, ips)
152       {
153         ip_address_decode (&mp->endpoint.ips[ii], &ips[ii]);
154       }
155     }
156   mac_address_decode (mp->endpoint.mac, &mac);
157
158   if (GBP_ENDPOINT_FLAG_REMOTE & gef)
159     {
160       ip46_address_t tun_src, tun_dst;
161
162       ip_address_decode (&mp->endpoint.tun.src, &tun_src);
163       ip_address_decode (&mp->endpoint.tun.dst, &tun_dst);
164
165       rv = gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_CP,
166                                          sw_if_index, ips, &mac,
167                                          INDEX_INVALID, INDEX_INVALID,
168                                          ntohs (mp->endpoint.sclass),
169                                          gef, &tun_src, &tun_dst, &handle);
170     }
171   else
172     {
173       rv = gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_CP,
174                                          sw_if_index, ips, &mac,
175                                          INDEX_INVALID, INDEX_INVALID,
176                                          ntohs (mp->endpoint.sclass),
177                                          gef, NULL, NULL, &handle);
178     }
179   vec_free (ips);
180   BAD_SW_IF_INDEX_LABEL;
181
182   /* *INDENT-OFF* */
183   REPLY_MACRO2 (VL_API_GBP_ENDPOINT_ADD_REPLY + GBP_MSG_BASE,
184   ({
185     rmp->handle = htonl (handle);
186   }));
187   /* *INDENT-ON* */
188 }
189
190 static void
191 vl_api_gbp_endpoint_del_t_handler (vl_api_gbp_endpoint_del_t * mp)
192 {
193   vl_api_gbp_endpoint_del_reply_t *rmp;
194   int rv = 0;
195
196   gbp_endpoint_unlock (GBP_ENDPOINT_SRC_CP, ntohl (mp->handle));
197
198   REPLY_MACRO (VL_API_GBP_ENDPOINT_DEL_REPLY + GBP_MSG_BASE);
199 }
200
201 typedef struct gbp_walk_ctx_t_
202 {
203   vl_api_registration_t *reg;
204   u32 context;
205 } gbp_walk_ctx_t;
206
207 static walk_rc_t
208 gbp_endpoint_send_details (index_t gei, void *args)
209 {
210   vl_api_gbp_endpoint_details_t *mp;
211   gbp_endpoint_loc_t *gel;
212   gbp_endpoint_fwd_t *gef;
213   gbp_endpoint_t *ge;
214   gbp_walk_ctx_t *ctx;
215   u8 n_ips, ii;
216
217   ctx = args;
218   ge = gbp_endpoint_get (gei);
219
220   n_ips = vec_len (ge->ge_key.gek_ips);
221   mp = vl_msg_api_alloc (sizeof (*mp) + (sizeof (*mp->endpoint.ips) * n_ips));
222   if (!mp)
223     return 1;
224
225   clib_memset (mp, 0, sizeof (*mp));
226   mp->_vl_msg_id = ntohs (VL_API_GBP_ENDPOINT_DETAILS + GBP_MSG_BASE);
227   mp->context = ctx->context;
228
229   gel = &ge->ge_locs[0];
230   gef = &ge->ge_fwd;
231
232   if (gbp_endpoint_is_remote (ge))
233     {
234       mp->endpoint.sw_if_index = ntohl (gel->tun.gel_parent_sw_if_index);
235       ip_address_encode (&gel->tun.gel_src, IP46_TYPE_ANY,
236                          &mp->endpoint.tun.src);
237       ip_address_encode (&gel->tun.gel_dst, IP46_TYPE_ANY,
238                          &mp->endpoint.tun.dst);
239     }
240   else
241     {
242       mp->endpoint.sw_if_index = ntohl (gef->gef_itf);
243     }
244   mp->endpoint.sclass = ntohs (ge->ge_fwd.gef_sclass);
245   mp->endpoint.n_ips = n_ips;
246   mp->endpoint.flags = gbp_endpoint_flags_encode (gef->gef_flags);
247   mp->handle = htonl (gei);
248   mp->age = vlib_time_now (vlib_get_main ()) - ge->ge_last_time;
249   mac_address_encode (&ge->ge_key.gek_mac, mp->endpoint.mac);
250
251   vec_foreach_index (ii, ge->ge_key.gek_ips)
252   {
253     ip_address_encode (&ge->ge_key.gek_ips[ii].fp_addr,
254                        IP46_TYPE_ANY, &mp->endpoint.ips[ii]);
255   }
256
257   vl_api_send_msg (ctx->reg, (u8 *) mp);
258
259   return (WALK_CONTINUE);
260 }
261
262 static void
263 vl_api_gbp_endpoint_dump_t_handler (vl_api_gbp_endpoint_dump_t * mp)
264 {
265   vl_api_registration_t *reg;
266
267   reg = vl_api_client_index_to_registration (mp->client_index);
268   if (!reg)
269     return;
270
271   gbp_walk_ctx_t ctx = {
272     .reg = reg,
273     .context = mp->context,
274   };
275
276   gbp_endpoint_walk (gbp_endpoint_send_details, &ctx);
277 }
278
279 static void
280 gbp_retention_decode (const vl_api_gbp_endpoint_retention_t * in,
281                       gbp_endpoint_retention_t * out)
282 {
283   out->remote_ep_timeout = ntohl (in->remote_ep_timeout);
284 }
285
286 static void
287   vl_api_gbp_endpoint_group_add_t_handler
288   (vl_api_gbp_endpoint_group_add_t * mp)
289 {
290   vl_api_gbp_endpoint_group_add_reply_t *rmp;
291   gbp_endpoint_retention_t retention;
292   int rv = 0;
293
294   gbp_retention_decode (&mp->epg.retention, &retention);
295
296   rv = gbp_endpoint_group_add_and_lock (ntohl (mp->epg.vnid),
297                                         ntohs (mp->epg.sclass),
298                                         ntohl (mp->epg.bd_id),
299                                         ntohl (mp->epg.rd_id),
300                                         ntohl (mp->epg.uplink_sw_if_index),
301                                         &retention);
302
303   REPLY_MACRO (VL_API_GBP_ENDPOINT_GROUP_ADD_REPLY + GBP_MSG_BASE);
304 }
305
306 static void
307   vl_api_gbp_endpoint_group_del_t_handler
308   (vl_api_gbp_endpoint_group_del_t * mp)
309 {
310   vl_api_gbp_endpoint_group_del_reply_t *rmp;
311   int rv = 0;
312
313   rv = gbp_endpoint_group_delete (ntohs (mp->sclass));
314
315   REPLY_MACRO (VL_API_GBP_ENDPOINT_GROUP_DEL_REPLY + GBP_MSG_BASE);
316 }
317
318 static gbp_bridge_domain_flags_t
319 gbp_bridge_domain_flags_from_api (vl_api_gbp_bridge_domain_flags_t a)
320 {
321   gbp_bridge_domain_flags_t g;
322
323   g = GBP_BD_FLAG_NONE;
324   a = clib_net_to_host_u32 (a);
325
326   if (a & GBP_BD_API_FLAG_DO_NOT_LEARN)
327     g |= GBP_BD_FLAG_DO_NOT_LEARN;
328   if (a & GBP_BD_API_FLAG_UU_FWD_DROP)
329     g |= GBP_BD_FLAG_UU_FWD_DROP;
330   if (a & GBP_BD_API_FLAG_MCAST_DROP)
331     g |= GBP_BD_FLAG_MCAST_DROP;
332   if (a & GBP_BD_API_FLAG_UCAST_ARP)
333     g |= GBP_BD_FLAG_UCAST_ARP;
334   return (g);
335 }
336
337 static void
338 vl_api_gbp_bridge_domain_add_t_handler (vl_api_gbp_bridge_domain_add_t * mp)
339 {
340   vl_api_gbp_bridge_domain_add_reply_t *rmp;
341   int rv = 0;
342
343   rv = gbp_bridge_domain_add_and_lock (ntohl (mp->bd.bd_id),
344                                        gbp_bridge_domain_flags_from_api
345                                        (mp->bd.flags),
346                                        ntohl (mp->bd.bvi_sw_if_index),
347                                        ntohl (mp->bd.uu_fwd_sw_if_index),
348                                        ntohl (mp->bd.bm_flood_sw_if_index));
349
350   REPLY_MACRO (VL_API_GBP_BRIDGE_DOMAIN_ADD_REPLY + GBP_MSG_BASE);
351 }
352
353 static void
354 vl_api_gbp_bridge_domain_del_t_handler (vl_api_gbp_bridge_domain_del_t * mp)
355 {
356   vl_api_gbp_bridge_domain_del_reply_t *rmp;
357   int rv = 0;
358
359   rv = gbp_bridge_domain_delete (ntohl (mp->bd_id));
360
361   REPLY_MACRO (VL_API_GBP_BRIDGE_DOMAIN_DEL_REPLY + GBP_MSG_BASE);
362 }
363
364 static void
365 vl_api_gbp_route_domain_add_t_handler (vl_api_gbp_route_domain_add_t * mp)
366 {
367   vl_api_gbp_route_domain_add_reply_t *rmp;
368   int rv = 0;
369
370   rv = gbp_route_domain_add_and_lock (ntohl (mp->rd.rd_id),
371                                       ntohl (mp->rd.ip4_table_id),
372                                       ntohl (mp->rd.ip6_table_id),
373                                       ntohl (mp->rd.ip4_uu_sw_if_index),
374                                       ntohl (mp->rd.ip6_uu_sw_if_index));
375
376   REPLY_MACRO (VL_API_GBP_ROUTE_DOMAIN_ADD_REPLY + GBP_MSG_BASE);
377 }
378
379 static void
380 vl_api_gbp_route_domain_del_t_handler (vl_api_gbp_route_domain_del_t * mp)
381 {
382   vl_api_gbp_route_domain_del_reply_t *rmp;
383   int rv = 0;
384
385   rv = gbp_route_domain_delete (ntohl (mp->rd_id));
386
387   REPLY_MACRO (VL_API_GBP_ROUTE_DOMAIN_DEL_REPLY + GBP_MSG_BASE);
388 }
389
390 static int
391 gub_subnet_type_from_api (vl_api_gbp_subnet_type_t a, gbp_subnet_type_t * t)
392 {
393   a = clib_net_to_host_u32 (a);
394
395   switch (a)
396     {
397     case GBP_API_SUBNET_TRANSPORT:
398       *t = GBP_SUBNET_TRANSPORT;
399       return (0);
400     case GBP_API_SUBNET_L3_OUT:
401       *t = GBP_SUBNET_L3_OUT;
402       return (0);
403     case GBP_API_SUBNET_STITCHED_INTERNAL:
404       *t = GBP_SUBNET_STITCHED_INTERNAL;
405       return (0);
406     case GBP_API_SUBNET_STITCHED_EXTERNAL:
407       *t = GBP_SUBNET_STITCHED_EXTERNAL;
408       return (0);
409     }
410
411   return (-1);
412 }
413
414 static void
415 vl_api_gbp_subnet_add_del_t_handler (vl_api_gbp_subnet_add_del_t * mp)
416 {
417   vl_api_gbp_subnet_add_del_reply_t *rmp;
418   gbp_subnet_type_t type;
419   fib_prefix_t pfx;
420   int rv = 0;
421
422   ip_prefix_decode (&mp->subnet.prefix, &pfx);
423
424   rv = gub_subnet_type_from_api (mp->subnet.type, &type);
425
426   if (0 != rv)
427     goto out;
428
429   if (mp->is_add)
430     rv = gbp_subnet_add (ntohl (mp->subnet.rd_id),
431                          &pfx, type,
432                          ntohl (mp->subnet.sw_if_index),
433                          ntohs (mp->subnet.sclass));
434   else
435     rv = gbp_subnet_del (ntohl (mp->subnet.rd_id), &pfx);
436
437 out:
438   REPLY_MACRO (VL_API_GBP_SUBNET_ADD_DEL_REPLY + GBP_MSG_BASE);
439 }
440
441 static vl_api_gbp_subnet_type_t
442 gub_subnet_type_to_api (gbp_subnet_type_t t)
443 {
444   vl_api_gbp_subnet_type_t a = 0;
445
446   switch (t)
447     {
448     case GBP_SUBNET_TRANSPORT:
449       a = GBP_API_SUBNET_TRANSPORT;
450       break;
451     case GBP_SUBNET_STITCHED_INTERNAL:
452       a = GBP_API_SUBNET_STITCHED_INTERNAL;
453       break;
454     case GBP_SUBNET_STITCHED_EXTERNAL:
455       a = GBP_API_SUBNET_STITCHED_EXTERNAL;
456       break;
457     case GBP_SUBNET_L3_OUT:
458       a = GBP_API_SUBNET_L3_OUT;
459       break;
460     }
461
462   a = clib_host_to_net_u32 (a);
463
464   return (a);
465 }
466
467 static walk_rc_t
468 gbp_subnet_send_details (u32 rd_id,
469                          const fib_prefix_t * pfx,
470                          gbp_subnet_type_t type,
471                          u32 sw_if_index, sclass_t sclass, void *args)
472 {
473   vl_api_gbp_subnet_details_t *mp;
474   gbp_walk_ctx_t *ctx;
475
476   ctx = args;
477   mp = vl_msg_api_alloc (sizeof (*mp));
478   if (!mp)
479     return 1;
480
481   clib_memset (mp, 0, sizeof (*mp));
482   mp->_vl_msg_id = ntohs (VL_API_GBP_SUBNET_DETAILS + GBP_MSG_BASE);
483   mp->context = ctx->context;
484
485   mp->subnet.type = gub_subnet_type_to_api (type);
486   mp->subnet.sw_if_index = ntohl (sw_if_index);
487   mp->subnet.sclass = ntohs (sclass);
488   mp->subnet.rd_id = ntohl (rd_id);
489   ip_prefix_encode (pfx, &mp->subnet.prefix);
490
491   vl_api_send_msg (ctx->reg, (u8 *) mp);
492
493   return (WALK_CONTINUE);
494 }
495
496 static void
497 vl_api_gbp_subnet_dump_t_handler (vl_api_gbp_subnet_dump_t * mp)
498 {
499   vl_api_registration_t *reg;
500
501   reg = vl_api_client_index_to_registration (mp->client_index);
502   if (!reg)
503     return;
504
505   gbp_walk_ctx_t ctx = {
506     .reg = reg,
507     .context = mp->context,
508   };
509
510   gbp_subnet_walk (gbp_subnet_send_details, &ctx);
511 }
512
513 static int
514 gbp_endpoint_group_send_details (gbp_endpoint_group_t * gg, void *args)
515 {
516   vl_api_gbp_endpoint_group_details_t *mp;
517   gbp_walk_ctx_t *ctx;
518
519   ctx = args;
520   mp = vl_msg_api_alloc (sizeof (*mp));
521   if (!mp)
522     return 1;
523
524   clib_memset (mp, 0, sizeof (*mp));
525   mp->_vl_msg_id = ntohs (VL_API_GBP_ENDPOINT_GROUP_DETAILS + GBP_MSG_BASE);
526   mp->context = ctx->context;
527
528   mp->epg.uplink_sw_if_index = ntohl (gg->gg_uplink_sw_if_index);
529   mp->epg.vnid = ntohl (gg->gg_vnid);
530   mp->epg.sclass = ntohs (gg->gg_sclass);
531   mp->epg.bd_id = ntohl (gbp_endpoint_group_get_bd_id (gg));
532   mp->epg.rd_id = ntohl (gbp_route_domain_get_rd_id (gg->gg_rd));
533
534   vl_api_send_msg (ctx->reg, (u8 *) mp);
535
536   return (1);
537 }
538
539 static void
540 vl_api_gbp_endpoint_group_dump_t_handler (vl_api_gbp_endpoint_group_dump_t *
541                                           mp)
542 {
543   vl_api_registration_t *reg;
544
545   reg = vl_api_client_index_to_registration (mp->client_index);
546   if (!reg)
547     return;
548
549   gbp_walk_ctx_t ctx = {
550     .reg = reg,
551     .context = mp->context,
552   };
553
554   gbp_endpoint_group_walk (gbp_endpoint_group_send_details, &ctx);
555 }
556
557 static int
558 gbp_bridge_domain_send_details (gbp_bridge_domain_t * gb, void *args)
559 {
560   vl_api_gbp_bridge_domain_details_t *mp;
561   gbp_walk_ctx_t *ctx;
562
563   ctx = args;
564   mp = vl_msg_api_alloc (sizeof (*mp));
565   if (!mp)
566     return 1;
567
568   memset (mp, 0, sizeof (*mp));
569   mp->_vl_msg_id = ntohs (VL_API_GBP_BRIDGE_DOMAIN_DETAILS + GBP_MSG_BASE);
570   mp->context = ctx->context;
571
572   mp->bd.bd_id = ntohl (gb->gb_bd_id);
573   mp->bd.bvi_sw_if_index = ntohl (gb->gb_bvi_sw_if_index);
574   mp->bd.uu_fwd_sw_if_index = ntohl (gb->gb_uu_fwd_sw_if_index);
575   mp->bd.bm_flood_sw_if_index = ntohl (gb->gb_bm_flood_sw_if_index);
576
577   vl_api_send_msg (ctx->reg, (u8 *) mp);
578
579   return (1);
580 }
581
582 static void
583 vl_api_gbp_bridge_domain_dump_t_handler (vl_api_gbp_bridge_domain_dump_t * mp)
584 {
585   vl_api_registration_t *reg;
586
587   reg = vl_api_client_index_to_registration (mp->client_index);
588   if (!reg)
589     return;
590
591   gbp_walk_ctx_t ctx = {
592     .reg = reg,
593     .context = mp->context,
594   };
595
596   gbp_bridge_domain_walk (gbp_bridge_domain_send_details, &ctx);
597 }
598
599 static int
600 gbp_route_domain_send_details (gbp_route_domain_t * grd, void *args)
601 {
602   vl_api_gbp_route_domain_details_t *mp;
603   gbp_walk_ctx_t *ctx;
604
605   ctx = args;
606   mp = vl_msg_api_alloc (sizeof (*mp));
607   if (!mp)
608     return 1;
609
610   memset (mp, 0, sizeof (*mp));
611   mp->_vl_msg_id = ntohs (VL_API_GBP_ROUTE_DOMAIN_DETAILS + GBP_MSG_BASE);
612   mp->context = ctx->context;
613
614   mp->rd.rd_id = ntohl (grd->grd_id);
615   mp->rd.ip4_uu_sw_if_index =
616     ntohl (grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4]);
617   mp->rd.ip6_uu_sw_if_index =
618     ntohl (grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6]);
619
620   vl_api_send_msg (ctx->reg, (u8 *) mp);
621
622   return (1);
623 }
624
625 static void
626 vl_api_gbp_route_domain_dump_t_handler (vl_api_gbp_route_domain_dump_t * mp)
627 {
628   vl_api_registration_t *reg;
629
630   reg = vl_api_client_index_to_registration (mp->client_index);
631   if (!reg)
632     return;
633
634   gbp_walk_ctx_t ctx = {
635     .reg = reg,
636     .context = mp->context,
637   };
638
639   gbp_route_domain_walk (gbp_route_domain_send_details, &ctx);
640 }
641
642 static void
643 vl_api_gbp_recirc_add_del_t_handler (vl_api_gbp_recirc_add_del_t * mp)
644 {
645   vl_api_gbp_recirc_add_del_reply_t *rmp;
646   u32 sw_if_index;
647   int rv = 0;
648
649   sw_if_index = ntohl (mp->recirc.sw_if_index);
650   if (!vnet_sw_if_index_is_api_valid (sw_if_index))
651     goto bad_sw_if_index;
652
653   if (mp->is_add)
654     rv = gbp_recirc_add (sw_if_index,
655                          ntohs (mp->recirc.sclass), mp->recirc.is_ext);
656   else
657     rv = gbp_recirc_delete (sw_if_index);
658
659   BAD_SW_IF_INDEX_LABEL;
660
661   REPLY_MACRO (VL_API_GBP_RECIRC_ADD_DEL_REPLY + GBP_MSG_BASE);
662 }
663
664 static walk_rc_t
665 gbp_recirc_send_details (gbp_recirc_t * gr, void *args)
666 {
667   vl_api_gbp_recirc_details_t *mp;
668   gbp_walk_ctx_t *ctx;
669
670   ctx = args;
671   mp = vl_msg_api_alloc (sizeof (*mp));
672   if (!mp)
673     return (WALK_STOP);
674
675   clib_memset (mp, 0, sizeof (*mp));
676   mp->_vl_msg_id = ntohs (VL_API_GBP_RECIRC_DETAILS + GBP_MSG_BASE);
677   mp->context = ctx->context;
678
679   mp->recirc.sclass = ntohs (gr->gr_sclass);
680   mp->recirc.sw_if_index = ntohl (gr->gr_sw_if_index);
681   mp->recirc.is_ext = gr->gr_is_ext;
682
683   vl_api_send_msg (ctx->reg, (u8 *) mp);
684
685   return (WALK_CONTINUE);
686 }
687
688 static void
689 vl_api_gbp_recirc_dump_t_handler (vl_api_gbp_recirc_dump_t * mp)
690 {
691   vl_api_registration_t *reg;
692
693   reg = vl_api_client_index_to_registration (mp->client_index);
694   if (!reg)
695     return;
696
697   gbp_walk_ctx_t ctx = {
698     .reg = reg,
699     .context = mp->context,
700   };
701
702   gbp_recirc_walk (gbp_recirc_send_details, &ctx);
703 }
704
705 static void
706 vl_api_gbp_ext_itf_add_del_t_handler (vl_api_gbp_ext_itf_add_del_t * mp)
707 {
708   vl_api_gbp_ext_itf_add_del_reply_t *rmp;
709   u32 sw_if_index = ~0;
710   vl_api_gbp_ext_itf_t *ext_itf;
711   int rv = 0;
712
713   ext_itf = &mp->ext_itf;
714   if (ext_itf)
715     sw_if_index = ntohl (ext_itf->sw_if_index);
716
717   if (!vnet_sw_if_index_is_api_valid (sw_if_index))
718     goto bad_sw_if_index;
719
720   if (mp->is_add)
721     rv = gbp_ext_itf_add (sw_if_index,
722                           ntohl (ext_itf->bd_id), ntohl (ext_itf->rd_id));
723   else
724     rv = gbp_ext_itf_delete (sw_if_index);
725
726   BAD_SW_IF_INDEX_LABEL;
727
728   REPLY_MACRO (VL_API_GBP_EXT_ITF_ADD_DEL_REPLY + GBP_MSG_BASE);
729 }
730
731 static walk_rc_t
732 gbp_ext_itf_send_details (gbp_ext_itf_t * gx, void *args)
733 {
734   vl_api_gbp_ext_itf_details_t *mp;
735   gbp_walk_ctx_t *ctx;
736
737   ctx = args;
738   mp = vl_msg_api_alloc (sizeof (*mp));
739   if (!mp)
740     return (WALK_STOP);
741
742   clib_memset (mp, 0, sizeof (*mp));
743   mp->_vl_msg_id = ntohs (VL_API_GBP_EXT_ITF_DETAILS + GBP_MSG_BASE);
744   mp->context = ctx->context;
745
746   mp->ext_itf.bd_id = ntohl (gbp_bridge_domain_get_bd_id (gx->gx_bd));
747   mp->ext_itf.rd_id = ntohl (gbp_route_domain_get_rd_id (gx->gx_rd));
748   mp->ext_itf.sw_if_index = ntohl (gx->gx_itf);
749
750   vl_api_send_msg (ctx->reg, (u8 *) mp);
751
752   return (WALK_CONTINUE);
753 }
754
755 static void
756 vl_api_gbp_ext_itf_dump_t_handler (vl_api_gbp_ext_itf_dump_t * mp)
757 {
758   vl_api_registration_t *reg;
759
760   reg = vl_api_client_index_to_registration (mp->client_index);
761   if (!reg)
762     return;
763
764   gbp_walk_ctx_t ctx = {
765     .reg = reg,
766     .context = mp->context,
767   };
768
769   gbp_ext_itf_walk (gbp_ext_itf_send_details, &ctx);
770 }
771
772 static int
773 gbp_contract_rule_action_deocde (vl_api_gbp_rule_action_t in,
774                                  gbp_rule_action_t * out)
775 {
776   in = clib_net_to_host_u32 (in);
777
778   switch (in)
779     {
780     case GBP_API_RULE_PERMIT:
781       *out = GBP_RULE_PERMIT;
782       return (0);
783     case GBP_API_RULE_DENY:
784       *out = GBP_RULE_DENY;
785       return (0);
786     case GBP_API_RULE_REDIRECT:
787       *out = GBP_RULE_REDIRECT;
788       return (0);
789     }
790
791   return (-1);
792 }
793
794 static int
795 gbp_hash_mode_decode (vl_api_gbp_hash_mode_t in, gbp_hash_mode_t * out)
796 {
797   in = clib_net_to_host_u32 (in);
798
799   switch (in)
800     {
801     case GBP_API_HASH_MODE_SRC_IP:
802       *out = GBP_HASH_MODE_SRC_IP;
803       return (0);
804     case GBP_API_HASH_MODE_DST_IP:
805       *out = GBP_HASH_MODE_DST_IP;
806       return (0);
807     case GBP_API_HASH_MODE_SYMMETRIC:
808       *out = GBP_HASH_MODE_SYMMETRIC;
809       return (0);
810     }
811
812   return (-2);
813 }
814
815 static int
816 gbp_next_hop_decode (const vl_api_gbp_next_hop_t * in, index_t * gnhi)
817 {
818   ip46_address_t ip;
819   mac_address_t mac;
820   index_t grd, gbd;
821
822   gbd = gbp_bridge_domain_find_and_lock (ntohl (in->bd_id));
823
824   if (INDEX_INVALID == gbd)
825     return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
826
827   grd = gbp_route_domain_find_and_lock (ntohl (in->rd_id));
828
829   if (INDEX_INVALID == grd)
830     return (VNET_API_ERROR_NO_SUCH_FIB);
831
832   ip_address_decode (&in->ip, &ip);
833   mac_address_decode (in->mac, &mac);
834
835   *gnhi = gbp_next_hop_alloc (&ip, grd, &mac, gbd);
836
837   return (0);
838 }
839
840 static int
841 gbp_next_hop_set_decode (const vl_api_gbp_next_hop_set_t * in,
842                          gbp_hash_mode_t * hash_mode, index_t ** out)
843 {
844
845   index_t *gnhis = NULL;
846   int rv;
847   u8 ii;
848
849   rv = gbp_hash_mode_decode (in->hash_mode, hash_mode);
850
851   if (0 != rv)
852     return rv;
853
854   vec_validate (gnhis, in->n_nhs - 1);
855
856   for (ii = 0; ii < in->n_nhs; ii++)
857     {
858       rv = gbp_next_hop_decode (&in->nhs[ii], &gnhis[ii]);
859
860       if (0 != rv)
861         {
862           vec_free (gnhis);
863           break;
864         }
865     }
866
867   *out = gnhis;
868   return (rv);
869 }
870
871 static int
872 gbp_contract_rule_decode (const vl_api_gbp_rule_t * in, index_t * gui)
873 {
874   gbp_hash_mode_t hash_mode;
875   gbp_rule_action_t action;
876   index_t *nhs = NULL;
877   int rv;
878
879   rv = gbp_contract_rule_action_deocde (in->action, &action);
880
881   if (0 != rv)
882     return rv;
883
884   if (GBP_RULE_REDIRECT == action)
885     {
886       rv = gbp_next_hop_set_decode (&in->nh_set, &hash_mode, &nhs);
887
888       if (0 != rv)
889         return (rv);
890     }
891   else
892     {
893       hash_mode = GBP_HASH_MODE_SRC_IP;
894     }
895
896   *gui = gbp_rule_alloc (action, hash_mode, nhs);
897
898   return (rv);
899 }
900
901 static int
902 gbp_contract_rules_decode (u8 n_rules,
903                            const vl_api_gbp_rule_t * rules, index_t ** out)
904 {
905   index_t *guis = NULL;
906   int rv;
907   u8 ii;
908
909   if (0 == n_rules)
910     {
911       *out = NULL;
912       return (0);
913     }
914
915   vec_validate (guis, n_rules - 1);
916
917   for (ii = 0; ii < n_rules; ii++)
918     {
919       rv = gbp_contract_rule_decode (&rules[ii], &guis[ii]);
920
921       if (0 != rv)
922         {
923           vec_free (guis);
924           return (rv);
925         }
926     }
927
928   *out = guis;
929   return (rv);
930 }
931
932 static void
933 vl_api_gbp_contract_add_del_t_handler (vl_api_gbp_contract_add_del_t * mp)
934 {
935   vl_api_gbp_contract_add_del_reply_t *rmp;
936   u16 *allowed_ethertypes;
937   index_t *rules;
938   int ii, rv = 0;
939   u8 n_et;
940
941   if (mp->is_add)
942     {
943       rv = gbp_contract_rules_decode (mp->contract.n_rules,
944                                       mp->contract.rules, &rules);
945       if (0 != rv)
946         goto out;
947
948       allowed_ethertypes = NULL;
949
950       /*
951        * allowed ether types
952        */
953       n_et = mp->contract.n_ether_types;
954       vec_validate (allowed_ethertypes, n_et - 1);
955
956       for (ii = 0; ii < n_et; ii++)
957         {
958           /* leave the ether types in network order */
959           allowed_ethertypes[ii] = mp->contract.allowed_ethertypes[ii];
960         }
961
962       rv = gbp_contract_update (ntohs (mp->contract.sclass),
963                                 ntohs (mp->contract.dclass),
964                                 ntohl (mp->contract.acl_index),
965                                 rules, allowed_ethertypes);
966     }
967   else
968     rv = gbp_contract_delete (ntohs (mp->contract.sclass),
969                               ntohs (mp->contract.dclass));
970
971 out:
972   REPLY_MACRO (VL_API_GBP_CONTRACT_ADD_DEL_REPLY + GBP_MSG_BASE);
973 }
974
975 static int
976 gbp_contract_send_details (gbp_contract_t * gbpc, void *args)
977 {
978   vl_api_gbp_contract_details_t *mp;
979   gbp_walk_ctx_t *ctx;
980
981   ctx = args;
982   mp = vl_msg_api_alloc (sizeof (*mp));
983   if (!mp)
984     return 1;
985
986   clib_memset (mp, 0, sizeof (*mp));
987   mp->_vl_msg_id = ntohs (VL_API_GBP_CONTRACT_DETAILS + GBP_MSG_BASE);
988   mp->context = ctx->context;
989
990   mp->contract.sclass = ntohs (gbpc->gc_key.gck_src);
991   mp->contract.dclass = ntohs (gbpc->gc_key.gck_dst);
992   mp->contract.acl_index = ntohl (gbpc->gc_acl_index);
993
994   vl_api_send_msg (ctx->reg, (u8 *) mp);
995
996   return (1);
997 }
998
999 static void
1000 vl_api_gbp_contract_dump_t_handler (vl_api_gbp_contract_dump_t * mp)
1001 {
1002   vl_api_registration_t *reg;
1003
1004   reg = vl_api_client_index_to_registration (mp->client_index);
1005   if (!reg)
1006     return;
1007
1008   gbp_walk_ctx_t ctx = {
1009     .reg = reg,
1010     .context = mp->context,
1011   };
1012
1013   gbp_contract_walk (gbp_contract_send_details, &ctx);
1014 }
1015
1016 static int
1017 gbp_vxlan_tunnel_mode_2_layer (vl_api_gbp_vxlan_tunnel_mode_t mode,
1018                                gbp_vxlan_tunnel_layer_t * l)
1019 {
1020   mode = clib_net_to_host_u32 (mode);
1021
1022   switch (mode)
1023     {
1024     case GBP_VXLAN_TUNNEL_MODE_L2:
1025       *l = GBP_VXLAN_TUN_L2;
1026       return (0);
1027     case GBP_VXLAN_TUNNEL_MODE_L3:
1028       *l = GBP_VXLAN_TUN_L3;
1029       return (0);
1030     }
1031   return (-1);
1032 }
1033
1034 static void
1035 vl_api_gbp_vxlan_tunnel_add_t_handler (vl_api_gbp_vxlan_tunnel_add_t * mp)
1036 {
1037   vl_api_gbp_vxlan_tunnel_add_reply_t *rmp;
1038   gbp_vxlan_tunnel_layer_t layer;
1039   ip4_address_t src;
1040   u32 sw_if_index;
1041   int rv = 0;
1042
1043   ip4_address_decode (mp->tunnel.src, &src);
1044   rv = gbp_vxlan_tunnel_mode_2_layer (mp->tunnel.mode, &layer);
1045
1046   if (0 != rv)
1047     goto out;
1048
1049   rv = gbp_vxlan_tunnel_add (ntohl (mp->tunnel.vni),
1050                              layer,
1051                              ntohl (mp->tunnel.bd_rd_id), &src, &sw_if_index);
1052
1053 out:
1054   /* *INDENT-OFF* */
1055   REPLY_MACRO2 (VL_API_GBP_VXLAN_TUNNEL_ADD_REPLY + GBP_MSG_BASE,
1056   ({
1057     rmp->sw_if_index = htonl (sw_if_index);
1058   }));
1059   /* *INDENT-ON* */
1060 }
1061
1062 static void
1063 vl_api_gbp_vxlan_tunnel_del_t_handler (vl_api_gbp_vxlan_tunnel_add_t * mp)
1064 {
1065   vl_api_gbp_vxlan_tunnel_del_reply_t *rmp;
1066   int rv = 0;
1067
1068   rv = gbp_vxlan_tunnel_del (ntohl (mp->tunnel.vni));
1069
1070   REPLY_MACRO (VL_API_GBP_VXLAN_TUNNEL_DEL_REPLY + GBP_MSG_BASE);
1071 }
1072
1073 static vl_api_gbp_vxlan_tunnel_mode_t
1074 gbp_vxlan_tunnel_layer_2_mode (gbp_vxlan_tunnel_layer_t layer)
1075 {
1076   vl_api_gbp_vxlan_tunnel_mode_t mode = GBP_VXLAN_TUNNEL_MODE_L2;
1077
1078   switch (layer)
1079     {
1080     case GBP_VXLAN_TUN_L2:
1081       mode = GBP_VXLAN_TUNNEL_MODE_L2;
1082       break;
1083     case GBP_VXLAN_TUN_L3:
1084       mode = GBP_VXLAN_TUNNEL_MODE_L3;
1085       break;
1086     }
1087   mode = clib_host_to_net_u32 (mode);
1088
1089   return (mode);
1090 }
1091
1092 static walk_rc_t
1093 gbp_vxlan_tunnel_send_details (gbp_vxlan_tunnel_t * gt, void *args)
1094 {
1095   vl_api_gbp_vxlan_tunnel_details_t *mp;
1096   gbp_walk_ctx_t *ctx;
1097
1098   ctx = args;
1099   mp = vl_msg_api_alloc (sizeof (*mp));
1100   if (!mp)
1101     return 1;
1102
1103   memset (mp, 0, sizeof (*mp));
1104   mp->_vl_msg_id = htons (VL_API_GBP_VXLAN_TUNNEL_DETAILS + GBP_MSG_BASE);
1105   mp->context = ctx->context;
1106
1107   mp->tunnel.vni = htonl (gt->gt_vni);
1108   mp->tunnel.mode = gbp_vxlan_tunnel_layer_2_mode (gt->gt_layer);
1109   mp->tunnel.bd_rd_id = htonl (gt->gt_bd_rd_id);
1110
1111   vl_api_send_msg (ctx->reg, (u8 *) mp);
1112
1113   return (1);
1114 }
1115
1116 static void
1117 vl_api_gbp_vxlan_tunnel_dump_t_handler (vl_api_gbp_vxlan_tunnel_dump_t * mp)
1118 {
1119   vl_api_registration_t *reg;
1120
1121   reg = vl_api_client_index_to_registration (mp->client_index);
1122   if (!reg)
1123     return;
1124
1125   gbp_walk_ctx_t ctx = {
1126     .reg = reg,
1127     .context = mp->context,
1128   };
1129
1130   gbp_vxlan_walk (gbp_vxlan_tunnel_send_details, &ctx);
1131 }
1132
1133 /*
1134  * gbp_api_hookup
1135  * Add vpe's API message handlers to the table.
1136  * vlib has already mapped shared memory and
1137  * added the client registration handlers.
1138  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1139  */
1140 #define vl_msg_name_crc_list
1141 #include <gbp/gbp_all_api_h.h>
1142 #undef vl_msg_name_crc_list
1143
1144 static void
1145 setup_message_id_table (api_main_t * am)
1146 {
1147 #define _(id,n,crc)                                     \
1148   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + GBP_MSG_BASE);
1149   foreach_vl_msg_name_crc_gbp;
1150 #undef _
1151 }
1152
1153 static void
1154 gbp_api_hookup (vlib_main_t * vm)
1155 {
1156 #define _(N,n)                                                  \
1157     vl_msg_api_set_handlers(VL_API_##N + GBP_MSG_BASE,          \
1158                             #n,                                 \
1159                             vl_api_##n##_t_handler,             \
1160                             vl_noop_handler,                    \
1161                             vl_api_##n##_t_endian,              \
1162                             vl_api_##n##_t_print,               \
1163                             sizeof(vl_api_##n##_t), 1);
1164   foreach_gbp_api_msg;
1165 #undef _
1166 }
1167
1168 static clib_error_t *
1169 gbp_init (vlib_main_t * vm)
1170 {
1171   api_main_t *am = &api_main;
1172   gbp_main_t *gbpm = &gbp_main;
1173   u8 *name = format (0, "gbp_%08x%c", api_version, 0);
1174
1175   gbpm->gbp_acl_user_id = ~0;
1176
1177   /* Ask for a correctly-sized block of API message decode slots */
1178   msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
1179                                         VL_MSG_FIRST_AVAILABLE);
1180   gbp_api_hookup (vm);
1181
1182   /* Add our API messages to the global name_crc hash table */
1183   setup_message_id_table (am);
1184
1185   vec_free (name);
1186   return (NULL);
1187 }
1188
1189 VLIB_API_INIT_FUNCTION (gbp_init);
1190
1191 /* *INDENT-OFF* */
1192 VLIB_PLUGIN_REGISTER () = {
1193     .version = VPP_BUILD_VER,
1194     .description = "Group Based Policy",
1195 };
1196 /* *INDENT-ON* */
1197
1198
1199 /*
1200  * fd.io coding-style-patch-verification: ON
1201  *
1202  * Local Variables:
1203  * eval: (c-set-style "gnu")
1204  * End:
1205  */