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