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