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