gbp: VRF scoped contracts
[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
335   return (g);
336 }
337
338 static void
339 vl_api_gbp_bridge_domain_add_t_handler (vl_api_gbp_bridge_domain_add_t * mp)
340 {
341   vl_api_gbp_bridge_domain_add_reply_t *rmp;
342   int rv = 0;
343
344   rv = gbp_bridge_domain_add_and_lock (ntohl (mp->bd.bd_id),
345                                        ntohl (mp->bd.rd_id),
346                                        gbp_bridge_domain_flags_from_api
347                                        (mp->bd.flags),
348                                        ntohl (mp->bd.bvi_sw_if_index),
349                                        ntohl (mp->bd.uu_fwd_sw_if_index),
350                                        ntohl (mp->bd.bm_flood_sw_if_index));
351
352   REPLY_MACRO (VL_API_GBP_BRIDGE_DOMAIN_ADD_REPLY + GBP_MSG_BASE);
353 }
354
355 static void
356 vl_api_gbp_bridge_domain_del_t_handler (vl_api_gbp_bridge_domain_del_t * mp)
357 {
358   vl_api_gbp_bridge_domain_del_reply_t *rmp;
359   int rv = 0;
360
361   rv = gbp_bridge_domain_delete (ntohl (mp->bd_id));
362
363   REPLY_MACRO (VL_API_GBP_BRIDGE_DOMAIN_DEL_REPLY + GBP_MSG_BASE);
364 }
365
366 static void
367 vl_api_gbp_route_domain_add_t_handler (vl_api_gbp_route_domain_add_t * mp)
368 {
369   vl_api_gbp_route_domain_add_reply_t *rmp;
370   int rv = 0;
371
372   rv = gbp_route_domain_add_and_lock (ntohl (mp->rd.rd_id),
373                                       ntohs (mp->rd.scope),
374                                       ntohl (mp->rd.ip4_table_id),
375                                       ntohl (mp->rd.ip6_table_id),
376                                       ntohl (mp->rd.ip4_uu_sw_if_index),
377                                       ntohl (mp->rd.ip6_uu_sw_if_index));
378
379   REPLY_MACRO (VL_API_GBP_ROUTE_DOMAIN_ADD_REPLY + GBP_MSG_BASE);
380 }
381
382 static void
383 vl_api_gbp_route_domain_del_t_handler (vl_api_gbp_route_domain_del_t * mp)
384 {
385   vl_api_gbp_route_domain_del_reply_t *rmp;
386   int rv = 0;
387
388   rv = gbp_route_domain_delete (ntohl (mp->rd_id));
389
390   REPLY_MACRO (VL_API_GBP_ROUTE_DOMAIN_DEL_REPLY + GBP_MSG_BASE);
391 }
392
393 static int
394 gub_subnet_type_from_api (vl_api_gbp_subnet_type_t a, gbp_subnet_type_t * t)
395 {
396   a = clib_net_to_host_u32 (a);
397
398   switch (a)
399     {
400     case GBP_API_SUBNET_TRANSPORT:
401       *t = GBP_SUBNET_TRANSPORT;
402       return (0);
403     case GBP_API_SUBNET_L3_OUT:
404       *t = GBP_SUBNET_L3_OUT;
405       return (0);
406     case GBP_API_SUBNET_STITCHED_INTERNAL:
407       *t = GBP_SUBNET_STITCHED_INTERNAL;
408       return (0);
409     case GBP_API_SUBNET_STITCHED_EXTERNAL:
410       *t = GBP_SUBNET_STITCHED_EXTERNAL;
411       return (0);
412     }
413
414   return (-1);
415 }
416
417 static void
418 vl_api_gbp_subnet_add_del_t_handler (vl_api_gbp_subnet_add_del_t * mp)
419 {
420   vl_api_gbp_subnet_add_del_reply_t *rmp;
421   gbp_subnet_type_t type;
422   fib_prefix_t pfx;
423   int rv = 0;
424
425   ip_prefix_decode (&mp->subnet.prefix, &pfx);
426
427   rv = gub_subnet_type_from_api (mp->subnet.type, &type);
428
429   if (0 != rv)
430     goto out;
431
432   if (mp->is_add)
433     rv = gbp_subnet_add (ntohl (mp->subnet.rd_id),
434                          &pfx, type,
435                          ntohl (mp->subnet.sw_if_index),
436                          ntohs (mp->subnet.sclass));
437   else
438     rv = gbp_subnet_del (ntohl (mp->subnet.rd_id), &pfx);
439
440 out:
441   REPLY_MACRO (VL_API_GBP_SUBNET_ADD_DEL_REPLY + GBP_MSG_BASE);
442 }
443
444 static vl_api_gbp_subnet_type_t
445 gub_subnet_type_to_api (gbp_subnet_type_t t)
446 {
447   vl_api_gbp_subnet_type_t a = 0;
448
449   switch (t)
450     {
451     case GBP_SUBNET_TRANSPORT:
452       a = GBP_API_SUBNET_TRANSPORT;
453       break;
454     case GBP_SUBNET_STITCHED_INTERNAL:
455       a = GBP_API_SUBNET_STITCHED_INTERNAL;
456       break;
457     case GBP_SUBNET_STITCHED_EXTERNAL:
458       a = GBP_API_SUBNET_STITCHED_EXTERNAL;
459       break;
460     case GBP_SUBNET_L3_OUT:
461       a = GBP_API_SUBNET_L3_OUT;
462       break;
463     }
464
465   a = clib_host_to_net_u32 (a);
466
467   return (a);
468 }
469
470 static walk_rc_t
471 gbp_subnet_send_details (u32 rd_id,
472                          const fib_prefix_t * pfx,
473                          gbp_subnet_type_t type,
474                          u32 sw_if_index, sclass_t sclass, void *args)
475 {
476   vl_api_gbp_subnet_details_t *mp;
477   gbp_walk_ctx_t *ctx;
478
479   ctx = args;
480   mp = vl_msg_api_alloc (sizeof (*mp));
481   if (!mp)
482     return 1;
483
484   clib_memset (mp, 0, sizeof (*mp));
485   mp->_vl_msg_id = ntohs (VL_API_GBP_SUBNET_DETAILS + GBP_MSG_BASE);
486   mp->context = ctx->context;
487
488   mp->subnet.type = gub_subnet_type_to_api (type);
489   mp->subnet.sw_if_index = ntohl (sw_if_index);
490   mp->subnet.sclass = ntohs (sclass);
491   mp->subnet.rd_id = ntohl (rd_id);
492   ip_prefix_encode (pfx, &mp->subnet.prefix);
493
494   vl_api_send_msg (ctx->reg, (u8 *) mp);
495
496   return (WALK_CONTINUE);
497 }
498
499 static void
500 vl_api_gbp_subnet_dump_t_handler (vl_api_gbp_subnet_dump_t * mp)
501 {
502   vl_api_registration_t *reg;
503
504   reg = vl_api_client_index_to_registration (mp->client_index);
505   if (!reg)
506     return;
507
508   gbp_walk_ctx_t ctx = {
509     .reg = reg,
510     .context = mp->context,
511   };
512
513   gbp_subnet_walk (gbp_subnet_send_details, &ctx);
514 }
515
516 static int
517 gbp_endpoint_group_send_details (gbp_endpoint_group_t * gg, void *args)
518 {
519   vl_api_gbp_endpoint_group_details_t *mp;
520   gbp_walk_ctx_t *ctx;
521
522   ctx = args;
523   mp = vl_msg_api_alloc (sizeof (*mp));
524   if (!mp)
525     return 1;
526
527   clib_memset (mp, 0, sizeof (*mp));
528   mp->_vl_msg_id = ntohs (VL_API_GBP_ENDPOINT_GROUP_DETAILS + GBP_MSG_BASE);
529   mp->context = ctx->context;
530
531   mp->epg.uplink_sw_if_index = ntohl (gg->gg_uplink_sw_if_index);
532   mp->epg.vnid = ntohl (gg->gg_vnid);
533   mp->epg.sclass = ntohs (gg->gg_sclass);
534   mp->epg.bd_id = ntohl (gbp_endpoint_group_get_bd_id (gg));
535   mp->epg.rd_id = ntohl (gbp_route_domain_get_rd_id (gg->gg_rd));
536
537   vl_api_send_msg (ctx->reg, (u8 *) mp);
538
539   return (1);
540 }
541
542 static void
543 vl_api_gbp_endpoint_group_dump_t_handler (vl_api_gbp_endpoint_group_dump_t *
544                                           mp)
545 {
546   vl_api_registration_t *reg;
547
548   reg = vl_api_client_index_to_registration (mp->client_index);
549   if (!reg)
550     return;
551
552   gbp_walk_ctx_t ctx = {
553     .reg = reg,
554     .context = mp->context,
555   };
556
557   gbp_endpoint_group_walk (gbp_endpoint_group_send_details, &ctx);
558 }
559
560 static int
561 gbp_bridge_domain_send_details (gbp_bridge_domain_t * gb, void *args)
562 {
563   vl_api_gbp_bridge_domain_details_t *mp;
564   gbp_route_domain_t *gr;
565   gbp_walk_ctx_t *ctx;
566
567   ctx = args;
568   mp = vl_msg_api_alloc (sizeof (*mp));
569   if (!mp)
570     return 1;
571
572   memset (mp, 0, sizeof (*mp));
573   mp->_vl_msg_id = ntohs (VL_API_GBP_BRIDGE_DOMAIN_DETAILS + GBP_MSG_BASE);
574   mp->context = ctx->context;
575
576   gr = gbp_route_domain_get (gb->gb_rdi);
577
578   mp->bd.bd_id = ntohl (gb->gb_bd_id);
579   mp->bd.rd_id = ntohl (gr->grd_id);
580   mp->bd.bvi_sw_if_index = ntohl (gb->gb_bvi_sw_if_index);
581   mp->bd.uu_fwd_sw_if_index = ntohl (gb->gb_uu_fwd_sw_if_index);
582   mp->bd.bm_flood_sw_if_index = ntohl (gb->gb_bm_flood_sw_if_index);
583
584   vl_api_send_msg (ctx->reg, (u8 *) mp);
585
586   return (1);
587 }
588
589 static void
590 vl_api_gbp_bridge_domain_dump_t_handler (vl_api_gbp_bridge_domain_dump_t * mp)
591 {
592   vl_api_registration_t *reg;
593
594   reg = vl_api_client_index_to_registration (mp->client_index);
595   if (!reg)
596     return;
597
598   gbp_walk_ctx_t ctx = {
599     .reg = reg,
600     .context = mp->context,
601   };
602
603   gbp_bridge_domain_walk (gbp_bridge_domain_send_details, &ctx);
604 }
605
606 static int
607 gbp_route_domain_send_details (gbp_route_domain_t * grd, void *args)
608 {
609   vl_api_gbp_route_domain_details_t *mp;
610   gbp_walk_ctx_t *ctx;
611
612   ctx = args;
613   mp = vl_msg_api_alloc (sizeof (*mp));
614   if (!mp)
615     return 1;
616
617   memset (mp, 0, sizeof (*mp));
618   mp->_vl_msg_id = ntohs (VL_API_GBP_ROUTE_DOMAIN_DETAILS + GBP_MSG_BASE);
619   mp->context = ctx->context;
620
621   mp->rd.rd_id = ntohl (grd->grd_id);
622   mp->rd.ip4_uu_sw_if_index =
623     ntohl (grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4]);
624   mp->rd.ip6_uu_sw_if_index =
625     ntohl (grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6]);
626
627   vl_api_send_msg (ctx->reg, (u8 *) mp);
628
629   return (1);
630 }
631
632 static void
633 vl_api_gbp_route_domain_dump_t_handler (vl_api_gbp_route_domain_dump_t * mp)
634 {
635   vl_api_registration_t *reg;
636
637   reg = vl_api_client_index_to_registration (mp->client_index);
638   if (!reg)
639     return;
640
641   gbp_walk_ctx_t ctx = {
642     .reg = reg,
643     .context = mp->context,
644   };
645
646   gbp_route_domain_walk (gbp_route_domain_send_details, &ctx);
647 }
648
649 static void
650 vl_api_gbp_recirc_add_del_t_handler (vl_api_gbp_recirc_add_del_t * mp)
651 {
652   vl_api_gbp_recirc_add_del_reply_t *rmp;
653   u32 sw_if_index;
654   int rv = 0;
655
656   sw_if_index = ntohl (mp->recirc.sw_if_index);
657   if (!vnet_sw_if_index_is_api_valid (sw_if_index))
658     goto bad_sw_if_index;
659
660   if (mp->is_add)
661     rv = gbp_recirc_add (sw_if_index,
662                          ntohs (mp->recirc.sclass), mp->recirc.is_ext);
663   else
664     rv = gbp_recirc_delete (sw_if_index);
665
666   BAD_SW_IF_INDEX_LABEL;
667
668   REPLY_MACRO (VL_API_GBP_RECIRC_ADD_DEL_REPLY + GBP_MSG_BASE);
669 }
670
671 static walk_rc_t
672 gbp_recirc_send_details (gbp_recirc_t * gr, void *args)
673 {
674   vl_api_gbp_recirc_details_t *mp;
675   gbp_walk_ctx_t *ctx;
676
677   ctx = args;
678   mp = vl_msg_api_alloc (sizeof (*mp));
679   if (!mp)
680     return (WALK_STOP);
681
682   clib_memset (mp, 0, sizeof (*mp));
683   mp->_vl_msg_id = ntohs (VL_API_GBP_RECIRC_DETAILS + GBP_MSG_BASE);
684   mp->context = ctx->context;
685
686   mp->recirc.sclass = ntohs (gr->gr_sclass);
687   mp->recirc.sw_if_index = ntohl (gr->gr_sw_if_index);
688   mp->recirc.is_ext = gr->gr_is_ext;
689
690   vl_api_send_msg (ctx->reg, (u8 *) mp);
691
692   return (WALK_CONTINUE);
693 }
694
695 static void
696 vl_api_gbp_recirc_dump_t_handler (vl_api_gbp_recirc_dump_t * mp)
697 {
698   vl_api_registration_t *reg;
699
700   reg = vl_api_client_index_to_registration (mp->client_index);
701   if (!reg)
702     return;
703
704   gbp_walk_ctx_t ctx = {
705     .reg = reg,
706     .context = mp->context,
707   };
708
709   gbp_recirc_walk (gbp_recirc_send_details, &ctx);
710 }
711
712 static void
713 vl_api_gbp_ext_itf_add_del_t_handler (vl_api_gbp_ext_itf_add_del_t * mp)
714 {
715   vl_api_gbp_ext_itf_add_del_reply_t *rmp;
716   u32 sw_if_index = ~0;
717   vl_api_gbp_ext_itf_t *ext_itf;
718   int rv = 0;
719
720   ext_itf = &mp->ext_itf;
721   if (ext_itf)
722     sw_if_index = ntohl (ext_itf->sw_if_index);
723
724   if (!vnet_sw_if_index_is_api_valid (sw_if_index))
725     goto bad_sw_if_index;
726
727   if (mp->is_add)
728     rv = gbp_ext_itf_add (sw_if_index,
729                           ntohl (ext_itf->bd_id), ntohl (ext_itf->rd_id));
730   else
731     rv = gbp_ext_itf_delete (sw_if_index);
732
733   BAD_SW_IF_INDEX_LABEL;
734
735   REPLY_MACRO (VL_API_GBP_EXT_ITF_ADD_DEL_REPLY + GBP_MSG_BASE);
736 }
737
738 static walk_rc_t
739 gbp_ext_itf_send_details (gbp_ext_itf_t * gx, void *args)
740 {
741   vl_api_gbp_ext_itf_details_t *mp;
742   gbp_walk_ctx_t *ctx;
743
744   ctx = args;
745   mp = vl_msg_api_alloc (sizeof (*mp));
746   if (!mp)
747     return (WALK_STOP);
748
749   clib_memset (mp, 0, sizeof (*mp));
750   mp->_vl_msg_id = ntohs (VL_API_GBP_EXT_ITF_DETAILS + GBP_MSG_BASE);
751   mp->context = ctx->context;
752
753   mp->ext_itf.bd_id = ntohl (gbp_bridge_domain_get_bd_id (gx->gx_bd));
754   mp->ext_itf.rd_id = ntohl (gbp_route_domain_get_rd_id (gx->gx_rd));
755   mp->ext_itf.sw_if_index = ntohl (gx->gx_itf);
756
757   vl_api_send_msg (ctx->reg, (u8 *) mp);
758
759   return (WALK_CONTINUE);
760 }
761
762 static void
763 vl_api_gbp_ext_itf_dump_t_handler (vl_api_gbp_ext_itf_dump_t * mp)
764 {
765   vl_api_registration_t *reg;
766
767   reg = vl_api_client_index_to_registration (mp->client_index);
768   if (!reg)
769     return;
770
771   gbp_walk_ctx_t ctx = {
772     .reg = reg,
773     .context = mp->context,
774   };
775
776   gbp_ext_itf_walk (gbp_ext_itf_send_details, &ctx);
777 }
778
779 static int
780 gbp_contract_rule_action_deocde (vl_api_gbp_rule_action_t in,
781                                  gbp_rule_action_t * out)
782 {
783   in = clib_net_to_host_u32 (in);
784
785   switch (in)
786     {
787     case GBP_API_RULE_PERMIT:
788       *out = GBP_RULE_PERMIT;
789       return (0);
790     case GBP_API_RULE_DENY:
791       *out = GBP_RULE_DENY;
792       return (0);
793     case GBP_API_RULE_REDIRECT:
794       *out = GBP_RULE_REDIRECT;
795       return (0);
796     }
797
798   return (-1);
799 }
800
801 static int
802 gbp_hash_mode_decode (vl_api_gbp_hash_mode_t in, gbp_hash_mode_t * out)
803 {
804   in = clib_net_to_host_u32 (in);
805
806   switch (in)
807     {
808     case GBP_API_HASH_MODE_SRC_IP:
809       *out = GBP_HASH_MODE_SRC_IP;
810       return (0);
811     case GBP_API_HASH_MODE_DST_IP:
812       *out = GBP_HASH_MODE_DST_IP;
813       return (0);
814     case GBP_API_HASH_MODE_SYMMETRIC:
815       *out = GBP_HASH_MODE_SYMMETRIC;
816       return (0);
817     }
818
819   return (-2);
820 }
821
822 static int
823 gbp_next_hop_decode (const vl_api_gbp_next_hop_t * in, index_t * gnhi)
824 {
825   ip46_address_t ip;
826   mac_address_t mac;
827   index_t grd, gbd;
828
829   gbd = gbp_bridge_domain_find_and_lock (ntohl (in->bd_id));
830
831   if (INDEX_INVALID == gbd)
832     return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
833
834   grd = gbp_route_domain_find_and_lock (ntohl (in->rd_id));
835
836   if (INDEX_INVALID == grd)
837     return (VNET_API_ERROR_NO_SUCH_FIB);
838
839   ip_address_decode (&in->ip, &ip);
840   mac_address_decode (in->mac, &mac);
841
842   *gnhi = gbp_next_hop_alloc (&ip, grd, &mac, gbd);
843
844   return (0);
845 }
846
847 static int
848 gbp_next_hop_set_decode (const vl_api_gbp_next_hop_set_t * in,
849                          gbp_hash_mode_t * hash_mode, index_t ** out)
850 {
851
852   index_t *gnhis = NULL;
853   int rv;
854   u8 ii;
855
856   rv = gbp_hash_mode_decode (in->hash_mode, hash_mode);
857
858   if (0 != rv)
859     return rv;
860
861   vec_validate (gnhis, in->n_nhs - 1);
862
863   for (ii = 0; ii < in->n_nhs; ii++)
864     {
865       rv = gbp_next_hop_decode (&in->nhs[ii], &gnhis[ii]);
866
867       if (0 != rv)
868         {
869           vec_free (gnhis);
870           break;
871         }
872     }
873
874   *out = gnhis;
875   return (rv);
876 }
877
878 static int
879 gbp_contract_rule_decode (const vl_api_gbp_rule_t * in, index_t * gui)
880 {
881   gbp_hash_mode_t hash_mode;
882   gbp_rule_action_t action;
883   index_t *nhs = NULL;
884   int rv;
885
886   rv = gbp_contract_rule_action_deocde (in->action, &action);
887
888   if (0 != rv)
889     return rv;
890
891   if (GBP_RULE_REDIRECT == action)
892     {
893       rv = gbp_next_hop_set_decode (&in->nh_set, &hash_mode, &nhs);
894
895       if (0 != rv)
896         return (rv);
897     }
898   else
899     {
900       hash_mode = GBP_HASH_MODE_SRC_IP;
901     }
902
903   *gui = gbp_rule_alloc (action, hash_mode, nhs);
904
905   return (rv);
906 }
907
908 static int
909 gbp_contract_rules_decode (u8 n_rules,
910                            const vl_api_gbp_rule_t * rules, index_t ** out)
911 {
912   index_t *guis = NULL;
913   int rv;
914   u8 ii;
915
916   if (0 == n_rules)
917     {
918       *out = NULL;
919       return (0);
920     }
921
922   vec_validate (guis, n_rules - 1);
923
924   for (ii = 0; ii < n_rules; ii++)
925     {
926       rv = gbp_contract_rule_decode (&rules[ii], &guis[ii]);
927
928       if (0 != rv)
929         {
930           vec_free (guis);
931           return (rv);
932         }
933     }
934
935   *out = guis;
936   return (rv);
937 }
938
939 static void
940 vl_api_gbp_contract_add_del_t_handler (vl_api_gbp_contract_add_del_t * mp)
941 {
942   vl_api_gbp_contract_add_del_reply_t *rmp;
943   u16 *allowed_ethertypes;
944   u32 stats_index = ~0;
945   index_t *rules;
946   int ii, rv = 0;
947   u8 n_et;
948
949   if (mp->is_add)
950     {
951       rv = gbp_contract_rules_decode (mp->contract.n_rules,
952                                       mp->contract.rules, &rules);
953       if (0 != rv)
954         goto out;
955
956       allowed_ethertypes = NULL;
957
958       /*
959        * allowed ether types
960        */
961       n_et = mp->contract.n_ether_types;
962       vec_validate (allowed_ethertypes, n_et - 1);
963
964       for (ii = 0; ii < n_et; ii++)
965         {
966           /* leave the ether types in network order */
967           allowed_ethertypes[ii] = mp->contract.allowed_ethertypes[ii];
968         }
969
970       rv = gbp_contract_update (ntohs (mp->contract.scope),
971                                 ntohs (mp->contract.sclass),
972                                 ntohs (mp->contract.dclass),
973                                 ntohl (mp->contract.acl_index),
974                                 rules, allowed_ethertypes, &stats_index);
975     }
976   else
977     rv = gbp_contract_delete (ntohs (mp->contract.scope),
978                               ntohs (mp->contract.sclass),
979                               ntohs (mp->contract.dclass));
980
981 out:
982   /* *INDENT-OFF* */
983   REPLY_MACRO2 (VL_API_GBP_CONTRACT_ADD_DEL_REPLY + GBP_MSG_BASE,
984   ({
985     rmp->stats_index = htonl (stats_index);
986   }));
987   /* *INDENT-ON* */
988 }
989
990 static int
991 gbp_contract_send_details (gbp_contract_t * gbpc, void *args)
992 {
993   vl_api_gbp_contract_details_t *mp;
994   gbp_walk_ctx_t *ctx;
995
996   ctx = args;
997   mp = vl_msg_api_alloc (sizeof (*mp));
998   if (!mp)
999     return 1;
1000
1001   clib_memset (mp, 0, sizeof (*mp));
1002   mp->_vl_msg_id = ntohs (VL_API_GBP_CONTRACT_DETAILS + GBP_MSG_BASE);
1003   mp->context = ctx->context;
1004
1005   mp->contract.sclass = ntohs (gbpc->gc_key.gck_src);
1006   mp->contract.dclass = ntohs (gbpc->gc_key.gck_dst);
1007   mp->contract.acl_index = ntohl (gbpc->gc_acl_index);
1008   mp->contract.scope = ntohs (gbpc->gc_key.gck_scope);
1009
1010   vl_api_send_msg (ctx->reg, (u8 *) mp);
1011
1012   return (1);
1013 }
1014
1015 static void
1016 vl_api_gbp_contract_dump_t_handler (vl_api_gbp_contract_dump_t * mp)
1017 {
1018   vl_api_registration_t *reg;
1019
1020   reg = vl_api_client_index_to_registration (mp->client_index);
1021   if (!reg)
1022     return;
1023
1024   gbp_walk_ctx_t ctx = {
1025     .reg = reg,
1026     .context = mp->context,
1027   };
1028
1029   gbp_contract_walk (gbp_contract_send_details, &ctx);
1030 }
1031
1032 static int
1033 gbp_vxlan_tunnel_mode_2_layer (vl_api_gbp_vxlan_tunnel_mode_t mode,
1034                                gbp_vxlan_tunnel_layer_t * l)
1035 {
1036   mode = clib_net_to_host_u32 (mode);
1037
1038   switch (mode)
1039     {
1040     case GBP_VXLAN_TUNNEL_MODE_L2:
1041       *l = GBP_VXLAN_TUN_L2;
1042       return (0);
1043     case GBP_VXLAN_TUNNEL_MODE_L3:
1044       *l = GBP_VXLAN_TUN_L3;
1045       return (0);
1046     }
1047   return (-1);
1048 }
1049
1050 static void
1051 vl_api_gbp_vxlan_tunnel_add_t_handler (vl_api_gbp_vxlan_tunnel_add_t * mp)
1052 {
1053   vl_api_gbp_vxlan_tunnel_add_reply_t *rmp;
1054   gbp_vxlan_tunnel_layer_t layer;
1055   ip4_address_t src;
1056   u32 sw_if_index;
1057   int rv = 0;
1058
1059   ip4_address_decode (mp->tunnel.src, &src);
1060   rv = gbp_vxlan_tunnel_mode_2_layer (mp->tunnel.mode, &layer);
1061
1062   if (0 != rv)
1063     goto out;
1064
1065   rv = gbp_vxlan_tunnel_add (ntohl (mp->tunnel.vni),
1066                              layer,
1067                              ntohl (mp->tunnel.bd_rd_id), &src, &sw_if_index);
1068
1069 out:
1070   /* *INDENT-OFF* */
1071   REPLY_MACRO2 (VL_API_GBP_VXLAN_TUNNEL_ADD_REPLY + GBP_MSG_BASE,
1072   ({
1073     rmp->sw_if_index = htonl (sw_if_index);
1074   }));
1075   /* *INDENT-ON* */
1076 }
1077
1078 static void
1079 vl_api_gbp_vxlan_tunnel_del_t_handler (vl_api_gbp_vxlan_tunnel_add_t * mp)
1080 {
1081   vl_api_gbp_vxlan_tunnel_del_reply_t *rmp;
1082   int rv = 0;
1083
1084   rv = gbp_vxlan_tunnel_del (ntohl (mp->tunnel.vni));
1085
1086   REPLY_MACRO (VL_API_GBP_VXLAN_TUNNEL_DEL_REPLY + GBP_MSG_BASE);
1087 }
1088
1089 static vl_api_gbp_vxlan_tunnel_mode_t
1090 gbp_vxlan_tunnel_layer_2_mode (gbp_vxlan_tunnel_layer_t layer)
1091 {
1092   vl_api_gbp_vxlan_tunnel_mode_t mode = GBP_VXLAN_TUNNEL_MODE_L2;
1093
1094   switch (layer)
1095     {
1096     case GBP_VXLAN_TUN_L2:
1097       mode = GBP_VXLAN_TUNNEL_MODE_L2;
1098       break;
1099     case GBP_VXLAN_TUN_L3:
1100       mode = GBP_VXLAN_TUNNEL_MODE_L3;
1101       break;
1102     }
1103   mode = clib_host_to_net_u32 (mode);
1104
1105   return (mode);
1106 }
1107
1108 static walk_rc_t
1109 gbp_vxlan_tunnel_send_details (gbp_vxlan_tunnel_t * gt, void *args)
1110 {
1111   vl_api_gbp_vxlan_tunnel_details_t *mp;
1112   gbp_walk_ctx_t *ctx;
1113
1114   ctx = args;
1115   mp = vl_msg_api_alloc (sizeof (*mp));
1116   if (!mp)
1117     return 1;
1118
1119   memset (mp, 0, sizeof (*mp));
1120   mp->_vl_msg_id = htons (VL_API_GBP_VXLAN_TUNNEL_DETAILS + GBP_MSG_BASE);
1121   mp->context = ctx->context;
1122
1123   mp->tunnel.vni = htonl (gt->gt_vni);
1124   mp->tunnel.mode = gbp_vxlan_tunnel_layer_2_mode (gt->gt_layer);
1125   mp->tunnel.bd_rd_id = htonl (gt->gt_bd_rd_id);
1126
1127   vl_api_send_msg (ctx->reg, (u8 *) mp);
1128
1129   return (1);
1130 }
1131
1132 static void
1133 vl_api_gbp_vxlan_tunnel_dump_t_handler (vl_api_gbp_vxlan_tunnel_dump_t * mp)
1134 {
1135   vl_api_registration_t *reg;
1136
1137   reg = vl_api_client_index_to_registration (mp->client_index);
1138   if (!reg)
1139     return;
1140
1141   gbp_walk_ctx_t ctx = {
1142     .reg = reg,
1143     .context = mp->context,
1144   };
1145
1146   gbp_vxlan_walk (gbp_vxlan_tunnel_send_details, &ctx);
1147 }
1148
1149 /*
1150  * gbp_api_hookup
1151  * Add vpe's API message handlers to the table.
1152  * vlib has already mapped shared memory and
1153  * added the client registration handlers.
1154  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1155  */
1156 #define vl_msg_name_crc_list
1157 #include <gbp/gbp_all_api_h.h>
1158 #undef vl_msg_name_crc_list
1159
1160 static void
1161 setup_message_id_table (api_main_t * am)
1162 {
1163 #define _(id,n,crc)                                     \
1164   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + GBP_MSG_BASE);
1165   foreach_vl_msg_name_crc_gbp;
1166 #undef _
1167 }
1168
1169 static void
1170 gbp_api_hookup (vlib_main_t * vm)
1171 {
1172 #define _(N,n)                                                  \
1173     vl_msg_api_set_handlers(VL_API_##N + GBP_MSG_BASE,          \
1174                             #n,                                 \
1175                             vl_api_##n##_t_handler,             \
1176                             vl_noop_handler,                    \
1177                             vl_api_##n##_t_endian,              \
1178                             vl_api_##n##_t_print,               \
1179                             sizeof(vl_api_##n##_t), 1);
1180   foreach_gbp_api_msg;
1181 #undef _
1182 }
1183
1184 static clib_error_t *
1185 gbp_init (vlib_main_t * vm)
1186 {
1187   api_main_t *am = &api_main;
1188   gbp_main_t *gbpm = &gbp_main;
1189   u8 *name = format (0, "gbp_%08x%c", api_version, 0);
1190
1191   gbpm->gbp_acl_user_id = ~0;
1192
1193   /* Ask for a correctly-sized block of API message decode slots */
1194   msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
1195                                         VL_MSG_FIRST_AVAILABLE);
1196   gbp_api_hookup (vm);
1197
1198   /* Add our API messages to the global name_crc hash table */
1199   setup_message_id_table (am);
1200
1201   vec_free (name);
1202   return (NULL);
1203 }
1204
1205 VLIB_API_INIT_FUNCTION (gbp_init);
1206
1207 /* *INDENT-OFF* */
1208 VLIB_PLUGIN_REGISTER () = {
1209     .version = VPP_BUILD_VER,
1210     .description = "Group Based Policy (GBP)",
1211 };
1212 /* *INDENT-ON* */
1213
1214
1215 /*
1216  * fd.io coding-style-patch-verification: ON
1217  *
1218  * Local Variables:
1219  * eval: (c-set-style "gnu")
1220  * End:
1221  */