GBP: Endpoints with VLAN tags and birdges that don't learn
[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 (sw_if_index, ips, &mac,
160                                 ntohs (mp->endpoint.epg_id),
161                                 gef, &tun_src, &tun_dst, &handle);
162     }
163   else
164     {
165       rv = gbp_endpoint_update (sw_if_index, ips, &mac,
166                                 ntohs (mp->endpoint.epg_id),
167                                 gef, NULL, NULL, &handle);
168     }
169   BAD_SW_IF_INDEX_LABEL;
170
171   /* *INDENT-OFF* */
172   REPLY_MACRO2 (VL_API_GBP_ENDPOINT_ADD_REPLY + GBP_MSG_BASE,
173   ({
174     rmp->handle = htonl (handle);
175   }));
176   /* *INDENT-ON* */
177 }
178
179 static void
180 vl_api_gbp_endpoint_del_t_handler (vl_api_gbp_endpoint_del_t * mp)
181 {
182   vl_api_gbp_endpoint_del_reply_t *rmp;
183   int rv = 0;
184
185   gbp_endpoint_delete (ntohl (mp->handle));
186
187   REPLY_MACRO (VL_API_GBP_ENDPOINT_DEL_REPLY + GBP_MSG_BASE);
188 }
189
190 static void
191   vl_api_gbp_endpoint_learn_set_inactive_threshold_t_handler
192   (vl_api_gbp_endpoint_learn_set_inactive_threshold_t * mp)
193 {
194   vl_api_gbp_endpoint_learn_set_inactive_threshold_reply_t *rmp;
195   int rv = 0;
196
197   gbp_learn_set_inactive_threshold (ntohl (mp->threshold));
198
199   REPLY_MACRO (VL_API_GBP_ENDPOINT_LEARN_SET_INACTIVE_THRESHOLD_REPLY +
200                GBP_MSG_BASE);
201 }
202
203 typedef struct gbp_walk_ctx_t_
204 {
205   vl_api_registration_t *reg;
206   u32 context;
207 } gbp_walk_ctx_t;
208
209 static walk_rc_t
210 gbp_endpoint_send_details (index_t gei, void *args)
211 {
212   vl_api_gbp_endpoint_details_t *mp;
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_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   if (gbp_endpoint_is_remote (ge))
230     {
231       mp->endpoint.sw_if_index = ntohl (ge->tun.ge_parent_sw_if_index);
232       ip_address_encode (&ge->tun.ge_src, IP46_TYPE_ANY,
233                          &mp->endpoint.tun.src);
234       ip_address_encode (&ge->tun.ge_dst, IP46_TYPE_ANY,
235                          &mp->endpoint.tun.dst);
236     }
237   else
238     {
239       mp->endpoint.sw_if_index = ntohl (ge->ge_sw_if_index);
240     }
241   mp->endpoint.epg_id = ntohs (ge->ge_epg_id);
242   mp->endpoint.n_ips = n_ips;
243   mp->endpoint.flags = gbp_endpoint_flags_encode (ge->ge_flags);
244   mp->handle = htonl (gei);
245   mp->age = vlib_time_now (vlib_get_main ()) - ge->ge_last_time;
246   mac_address_encode (&ge->ge_mac, &mp->endpoint.mac);
247
248   vec_foreach_index (ii, ge->ge_ips)
249   {
250     ip_address_encode (&ge->ge_ips[ii], IP46_TYPE_ANY, &mp->endpoint.ips[ii]);
251   }
252
253   vl_api_send_msg (ctx->reg, (u8 *) mp);
254
255   return (WALK_CONTINUE);
256 }
257
258 static void
259 vl_api_gbp_endpoint_dump_t_handler (vl_api_gbp_endpoint_dump_t * mp)
260 {
261   vl_api_registration_t *reg;
262
263   reg = vl_api_client_index_to_registration (mp->client_index);
264   if (!reg)
265     return;
266
267   gbp_walk_ctx_t ctx = {
268     .reg = reg,
269     .context = mp->context,
270   };
271
272   gbp_endpoint_walk (gbp_endpoint_send_details, &ctx);
273 }
274
275 static void
276   vl_api_gbp_endpoint_group_add_t_handler
277   (vl_api_gbp_endpoint_group_add_t * mp)
278 {
279   vl_api_gbp_endpoint_group_add_reply_t *rmp;
280   int rv = 0;
281
282   rv = gbp_endpoint_group_add_and_lock (ntohs (mp->epg.epg_id),
283                                         ntohl (mp->epg.bd_id),
284                                         ntohl (mp->epg.rd_id),
285                                         ntohl (mp->epg.uplink_sw_if_index));
286
287   REPLY_MACRO (VL_API_GBP_ENDPOINT_GROUP_ADD_REPLY + GBP_MSG_BASE);
288 }
289
290 static void
291   vl_api_gbp_endpoint_group_del_t_handler
292   (vl_api_gbp_endpoint_group_del_t * mp)
293 {
294   vl_api_gbp_endpoint_group_del_reply_t *rmp;
295   int rv = 0;
296
297   rv = gbp_endpoint_group_delete (ntohs (mp->epg_id));
298
299   REPLY_MACRO (VL_API_GBP_ENDPOINT_GROUP_DEL_REPLY + GBP_MSG_BASE);
300 }
301
302 static gbp_bridge_domain_flags_t
303 gbp_bridge_domain_flags_from_api (vl_api_gbp_bridge_domain_flags_t a)
304 {
305   gbp_bridge_domain_flags_t g;
306
307   g = GBP_BD_FLAG_NONE;
308   a = clib_net_to_host_u32 (a);
309
310   if (a & GBP_BD_API_FLAG_DO_NOT_LEARN)
311     g |= GBP_BD_FLAG_DO_NOT_LEARN;
312
313   return (g);
314 }
315
316 static void
317 vl_api_gbp_bridge_domain_add_t_handler (vl_api_gbp_bridge_domain_add_t * mp)
318 {
319   vl_api_gbp_bridge_domain_add_reply_t *rmp;
320   int rv = 0;
321
322   rv = gbp_bridge_domain_add_and_lock (ntohl (mp->bd.bd_id),
323                                        gbp_bridge_domain_flags_from_api
324                                        (mp->bd.flags),
325                                        ntohl (mp->bd.bvi_sw_if_index),
326                                        ntohl (mp->bd.uu_fwd_sw_if_index));
327
328   REPLY_MACRO (VL_API_GBP_BRIDGE_DOMAIN_ADD_REPLY + GBP_MSG_BASE);
329 }
330
331 static void
332 vl_api_gbp_bridge_domain_del_t_handler (vl_api_gbp_bridge_domain_del_t * mp)
333 {
334   vl_api_gbp_bridge_domain_del_reply_t *rmp;
335   int rv = 0;
336
337   rv = gbp_bridge_domain_delete (ntohl (mp->bd_id));
338
339   REPLY_MACRO (VL_API_GBP_BRIDGE_DOMAIN_DEL_REPLY + GBP_MSG_BASE);
340 }
341
342 static void
343 vl_api_gbp_route_domain_add_t_handler (vl_api_gbp_route_domain_add_t * mp)
344 {
345   vl_api_gbp_route_domain_add_reply_t *rmp;
346   int rv = 0;
347
348   rv = gbp_route_domain_add_and_lock (ntohl (mp->rd.rd_id),
349                                       ntohl (mp->rd.ip4_table_id),
350                                       ntohl (mp->rd.ip6_table_id),
351                                       ntohl (mp->rd.ip4_uu_sw_if_index),
352                                       ntohl (mp->rd.ip6_uu_sw_if_index));
353
354   REPLY_MACRO (VL_API_GBP_ROUTE_DOMAIN_ADD_REPLY + GBP_MSG_BASE);
355 }
356
357 static void
358 vl_api_gbp_route_domain_del_t_handler (vl_api_gbp_route_domain_del_t * mp)
359 {
360   vl_api_gbp_route_domain_del_reply_t *rmp;
361   int rv = 0;
362
363   rv = gbp_route_domain_delete (ntohl (mp->rd_id));
364
365   REPLY_MACRO (VL_API_GBP_ROUTE_DOMAIN_DEL_REPLY + GBP_MSG_BASE);
366 }
367
368 static int
369 gub_subnet_type_from_api (vl_api_gbp_subnet_type_t a, gbp_subnet_type_t * t)
370 {
371   a = clib_net_to_host_u32 (a);
372
373   switch (a)
374     {
375     case GBP_API_SUBNET_TRANSPORT:
376       *t = GBP_SUBNET_TRANSPORT;
377       return (0);
378     case GBP_API_SUBNET_STITCHED_INTERNAL:
379       *t = GBP_SUBNET_STITCHED_INTERNAL;
380       return (0);
381     case GBP_API_SUBNET_STITCHED_EXTERNAL:
382       *t = GBP_SUBNET_STITCHED_EXTERNAL;
383       return (0);
384     }
385
386   return (-1);
387 }
388
389 static void
390 vl_api_gbp_subnet_add_del_t_handler (vl_api_gbp_subnet_add_del_t * mp)
391 {
392   vl_api_gbp_subnet_add_del_reply_t *rmp;
393   gbp_subnet_type_t type;
394   fib_prefix_t pfx;
395   int rv = 0;
396
397   ip_prefix_decode (&mp->subnet.prefix, &pfx);
398
399   rv = gub_subnet_type_from_api (mp->subnet.type, &type);
400
401   if (0 != rv)
402     goto out;
403
404   if (mp->is_add)
405     rv = gbp_subnet_add (ntohl (mp->subnet.rd_id),
406                          &pfx, type,
407                          ntohl (mp->subnet.sw_if_index),
408                          ntohs (mp->subnet.epg_id));
409   else
410     rv = gbp_subnet_del (ntohl (mp->subnet.rd_id), &pfx);
411
412 out:
413   REPLY_MACRO (VL_API_GBP_SUBNET_ADD_DEL_REPLY + GBP_MSG_BASE);
414 }
415
416 static vl_api_gbp_subnet_type_t
417 gub_subnet_type_to_api (gbp_subnet_type_t t)
418 {
419   vl_api_gbp_subnet_type_t a = 0;
420
421   switch (t)
422     {
423     case GBP_SUBNET_TRANSPORT:
424       a = GBP_API_SUBNET_TRANSPORT;
425       break;
426     case GBP_SUBNET_STITCHED_INTERNAL:
427       a = GBP_API_SUBNET_STITCHED_INTERNAL;
428       break;
429     case GBP_SUBNET_STITCHED_EXTERNAL:
430       a = GBP_API_SUBNET_STITCHED_EXTERNAL;
431       break;
432     }
433
434   a = clib_host_to_net_u32 (a);
435
436   return (a);
437 }
438
439 static walk_rc_t
440 gbp_subnet_send_details (u32 rd_id,
441                          const fib_prefix_t * pfx,
442                          gbp_subnet_type_t type,
443                          u32 sw_if_index, epg_id_t epg, void *args)
444 {
445   vl_api_gbp_subnet_details_t *mp;
446   gbp_walk_ctx_t *ctx;
447
448   ctx = args;
449   mp = vl_msg_api_alloc (sizeof (*mp));
450   if (!mp)
451     return 1;
452
453   clib_memset (mp, 0, sizeof (*mp));
454   mp->_vl_msg_id = ntohs (VL_API_GBP_SUBNET_DETAILS + GBP_MSG_BASE);
455   mp->context = ctx->context;
456
457   mp->subnet.type = gub_subnet_type_to_api (type);
458   mp->subnet.sw_if_index = ntohl (sw_if_index);
459   mp->subnet.epg_id = ntohs (epg);
460   mp->subnet.rd_id = ntohl (rd_id);
461   ip_prefix_encode (pfx, &mp->subnet.prefix);
462
463   vl_api_send_msg (ctx->reg, (u8 *) mp);
464
465   return (WALK_CONTINUE);
466 }
467
468 static void
469 vl_api_gbp_subnet_dump_t_handler (vl_api_gbp_subnet_dump_t * mp)
470 {
471   vl_api_registration_t *reg;
472
473   reg = vl_api_client_index_to_registration (mp->client_index);
474   if (!reg)
475     return;
476
477   gbp_walk_ctx_t ctx = {
478     .reg = reg,
479     .context = mp->context,
480   };
481
482   gbp_subnet_walk (gbp_subnet_send_details, &ctx);
483 }
484
485 static int
486 gbp_endpoint_group_send_details (gbp_endpoint_group_t * gg, void *args)
487 {
488   vl_api_gbp_endpoint_group_details_t *mp;
489   gbp_walk_ctx_t *ctx;
490
491   ctx = args;
492   mp = vl_msg_api_alloc (sizeof (*mp));
493   if (!mp)
494     return 1;
495
496   clib_memset (mp, 0, sizeof (*mp));
497   mp->_vl_msg_id = ntohs (VL_API_GBP_ENDPOINT_GROUP_DETAILS + GBP_MSG_BASE);
498   mp->context = ctx->context;
499
500   mp->epg.uplink_sw_if_index = ntohl (gg->gg_uplink_sw_if_index);
501   mp->epg.epg_id = ntohs (gg->gg_id);
502   mp->epg.bd_id = ntohl (gbp_endpoint_group_get_bd_id (gg));
503   mp->epg.rd_id = ntohl (gg->gg_rd);
504
505   vl_api_send_msg (ctx->reg, (u8 *) mp);
506
507   return (1);
508 }
509
510 static void
511 vl_api_gbp_endpoint_group_dump_t_handler (vl_api_gbp_endpoint_group_dump_t *
512                                           mp)
513 {
514   vl_api_registration_t *reg;
515
516   reg = vl_api_client_index_to_registration (mp->client_index);
517   if (!reg)
518     return;
519
520   gbp_walk_ctx_t ctx = {
521     .reg = reg,
522     .context = mp->context,
523   };
524
525   gbp_endpoint_group_walk (gbp_endpoint_group_send_details, &ctx);
526 }
527
528 static int
529 gbp_bridge_domain_send_details (gbp_bridge_domain_t * gb, void *args)
530 {
531   vl_api_gbp_bridge_domain_details_t *mp;
532   gbp_walk_ctx_t *ctx;
533
534   ctx = args;
535   mp = vl_msg_api_alloc (sizeof (*mp));
536   if (!mp)
537     return 1;
538
539   memset (mp, 0, sizeof (*mp));
540   mp->_vl_msg_id = ntohs (VL_API_GBP_BRIDGE_DOMAIN_DETAILS + GBP_MSG_BASE);
541   mp->context = ctx->context;
542
543   mp->bd.bd_id = ntohl (gb->gb_bd_id);
544   mp->bd.bvi_sw_if_index = ntohl (gb->gb_bvi_sw_if_index);
545   mp->bd.uu_fwd_sw_if_index = ntohl (gb->gb_uu_fwd_sw_if_index);
546
547   vl_api_send_msg (ctx->reg, (u8 *) mp);
548
549   return (1);
550 }
551
552 static void
553 vl_api_gbp_bridge_domain_dump_t_handler (vl_api_gbp_bridge_domain_dump_t * mp)
554 {
555   vl_api_registration_t *reg;
556
557   reg = vl_api_client_index_to_registration (mp->client_index);
558   if (!reg)
559     return;
560
561   gbp_walk_ctx_t ctx = {
562     .reg = reg,
563     .context = mp->context,
564   };
565
566   gbp_bridge_domain_walk (gbp_bridge_domain_send_details, &ctx);
567 }
568
569 static int
570 gbp_route_domain_send_details (gbp_route_domain_t * grd, void *args)
571 {
572   vl_api_gbp_route_domain_details_t *mp;
573   gbp_walk_ctx_t *ctx;
574
575   ctx = args;
576   mp = vl_msg_api_alloc (sizeof (*mp));
577   if (!mp)
578     return 1;
579
580   memset (mp, 0, sizeof (*mp));
581   mp->_vl_msg_id = ntohs (VL_API_GBP_ROUTE_DOMAIN_DETAILS + GBP_MSG_BASE);
582   mp->context = ctx->context;
583
584   mp->rd.rd_id = ntohl (grd->grd_id);
585   mp->rd.ip4_uu_sw_if_index =
586     ntohl (grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP4]);
587   mp->rd.ip6_uu_sw_if_index =
588     ntohl (grd->grd_uu_sw_if_index[FIB_PROTOCOL_IP6]);
589
590   vl_api_send_msg (ctx->reg, (u8 *) mp);
591
592   return (1);
593 }
594
595 static void
596 vl_api_gbp_route_domain_dump_t_handler (vl_api_gbp_route_domain_dump_t * mp)
597 {
598   vl_api_registration_t *reg;
599
600   reg = vl_api_client_index_to_registration (mp->client_index);
601   if (!reg)
602     return;
603
604   gbp_walk_ctx_t ctx = {
605     .reg = reg,
606     .context = mp->context,
607   };
608
609   gbp_route_domain_walk (gbp_route_domain_send_details, &ctx);
610 }
611
612 static void
613 vl_api_gbp_recirc_add_del_t_handler (vl_api_gbp_recirc_add_del_t * mp)
614 {
615   vl_api_gbp_recirc_add_del_reply_t *rmp;
616   u32 sw_if_index;
617   int rv = 0;
618
619   sw_if_index = ntohl (mp->recirc.sw_if_index);
620   if (!vnet_sw_if_index_is_api_valid (sw_if_index))
621     goto bad_sw_if_index;
622
623   if (mp->is_add)
624     gbp_recirc_add (sw_if_index,
625                     ntohs (mp->recirc.epg_id), mp->recirc.is_ext);
626   else
627     gbp_recirc_delete (sw_if_index);
628
629   BAD_SW_IF_INDEX_LABEL;
630
631   REPLY_MACRO (VL_API_GBP_RECIRC_ADD_DEL_REPLY + GBP_MSG_BASE);
632 }
633
634 static int
635 gbp_recirc_send_details (gbp_recirc_t * gr, void *args)
636 {
637   vl_api_gbp_recirc_details_t *mp;
638   gbp_walk_ctx_t *ctx;
639
640   ctx = args;
641   mp = vl_msg_api_alloc (sizeof (*mp));
642   if (!mp)
643     return 1;
644
645   clib_memset (mp, 0, sizeof (*mp));
646   mp->_vl_msg_id = ntohs (VL_API_GBP_RECIRC_DETAILS + GBP_MSG_BASE);
647   mp->context = ctx->context;
648
649   mp->recirc.epg_id = ntohs (gr->gr_epg);
650   mp->recirc.sw_if_index = ntohl (gr->gr_sw_if_index);
651   mp->recirc.is_ext = ntohl (gr->gr_is_ext);
652
653   vl_api_send_msg (ctx->reg, (u8 *) mp);
654
655   return (1);
656 }
657
658 static void
659 vl_api_gbp_recirc_dump_t_handler (vl_api_gbp_recirc_dump_t * mp)
660 {
661   vl_api_registration_t *reg;
662
663   reg = vl_api_client_index_to_registration (mp->client_index);
664   if (!reg)
665     return;
666
667   gbp_walk_ctx_t ctx = {
668     .reg = reg,
669     .context = mp->context,
670   };
671
672   gbp_recirc_walk (gbp_recirc_send_details, &ctx);
673 }
674
675 static void
676 vl_api_gbp_contract_add_del_t_handler (vl_api_gbp_contract_add_del_t * mp)
677 {
678   vl_api_gbp_contract_add_del_reply_t *rmp;
679   int rv = 0;
680
681   if (mp->is_add)
682     gbp_contract_update (ntohs (mp->contract.src_epg),
683                          ntohs (mp->contract.dst_epg),
684                          ntohl (mp->contract.acl_index));
685   else
686     gbp_contract_delete (ntohs (mp->contract.src_epg),
687                          ntohs (mp->contract.dst_epg));
688
689   REPLY_MACRO (VL_API_GBP_CONTRACT_ADD_DEL_REPLY + GBP_MSG_BASE);
690 }
691
692 static int
693 gbp_contract_send_details (gbp_contract_t * gbpc, void *args)
694 {
695   vl_api_gbp_contract_details_t *mp;
696   gbp_walk_ctx_t *ctx;
697
698   ctx = args;
699   mp = vl_msg_api_alloc (sizeof (*mp));
700   if (!mp)
701     return 1;
702
703   clib_memset (mp, 0, sizeof (*mp));
704   mp->_vl_msg_id = ntohs (VL_API_GBP_CONTRACT_DETAILS + GBP_MSG_BASE);
705   mp->context = ctx->context;
706
707   mp->contract.src_epg = ntohs (gbpc->gc_key.gck_src);
708   mp->contract.dst_epg = ntohs (gbpc->gc_key.gck_dst);
709   mp->contract.acl_index = ntohl (gbpc->gc_value.gc_acl_index);
710
711   vl_api_send_msg (ctx->reg, (u8 *) mp);
712
713   return (1);
714 }
715
716 static void
717 vl_api_gbp_contract_dump_t_handler (vl_api_gbp_contract_dump_t * mp)
718 {
719   vl_api_registration_t *reg;
720
721   reg = vl_api_client_index_to_registration (mp->client_index);
722   if (!reg)
723     return;
724
725   gbp_walk_ctx_t ctx = {
726     .reg = reg,
727     .context = mp->context,
728   };
729
730   gbp_contract_walk (gbp_contract_send_details, &ctx);
731 }
732
733 static int
734 gbp_vxlan_tunnel_mode_2_layer (vl_api_gbp_vxlan_tunnel_mode_t mode,
735                                gbp_vxlan_tunnel_layer_t * l)
736 {
737   mode = clib_net_to_host_u32 (mode);
738
739   switch (mode)
740     {
741     case GBP_VXLAN_TUNNEL_MODE_L2:
742       *l = GBP_VXLAN_TUN_L2;
743       return (0);
744     case GBP_VXLAN_TUNNEL_MODE_L3:
745       *l = GBP_VXLAN_TUN_L3;
746       return (0);
747     }
748   return (-1);
749 }
750
751 static void
752 vl_api_gbp_vxlan_tunnel_add_t_handler (vl_api_gbp_vxlan_tunnel_add_t * mp)
753 {
754   vl_api_gbp_vxlan_tunnel_add_reply_t *rmp;
755   gbp_vxlan_tunnel_layer_t layer;
756   u32 sw_if_index;
757   int rv = 0;
758
759   rv = gbp_vxlan_tunnel_mode_2_layer (mp->tunnel.mode, &layer);
760
761   if (0 != rv)
762     goto out;
763
764   rv = gbp_vxlan_tunnel_add (ntohl (mp->tunnel.vni),
765                              layer,
766                              ntohl (mp->tunnel.bd_rd_id), &sw_if_index);
767
768 out:
769   /* *INDENT-OFF* */
770   REPLY_MACRO2 (VL_API_GBP_VXLAN_TUNNEL_ADD_REPLY + GBP_MSG_BASE,
771   ({
772     rmp->sw_if_index = htonl (sw_if_index);
773   }));
774   /* *INDENT-ON* */
775 }
776
777 static void
778 vl_api_gbp_vxlan_tunnel_del_t_handler (vl_api_gbp_vxlan_tunnel_add_t * mp)
779 {
780   vl_api_gbp_vxlan_tunnel_del_reply_t *rmp;
781   int rv = 0;
782
783   rv = gbp_vxlan_tunnel_del (ntohl (mp->tunnel.vni));
784
785   REPLY_MACRO (VL_API_GBP_VXLAN_TUNNEL_DEL_REPLY + GBP_MSG_BASE);
786 }
787
788 static vl_api_gbp_vxlan_tunnel_mode_t
789 gbp_vxlan_tunnel_layer_2_mode (gbp_vxlan_tunnel_layer_t layer)
790 {
791   vl_api_gbp_vxlan_tunnel_mode_t mode = GBP_VXLAN_TUNNEL_MODE_L2;
792
793   switch (layer)
794     {
795     case GBP_VXLAN_TUN_L2:
796       mode = GBP_VXLAN_TUNNEL_MODE_L2;
797       break;
798     case GBP_VXLAN_TUN_L3:
799       mode = GBP_VXLAN_TUNNEL_MODE_L3;
800       break;
801     }
802   mode = clib_host_to_net_u32 (mode);
803
804   return (mode);
805 }
806
807 static walk_rc_t
808 gbp_vxlan_tunnel_send_details (gbp_vxlan_tunnel_t * gt, void *args)
809 {
810   vl_api_gbp_vxlan_tunnel_details_t *mp;
811   gbp_walk_ctx_t *ctx;
812
813   ctx = args;
814   mp = vl_msg_api_alloc (sizeof (*mp));
815   if (!mp)
816     return 1;
817
818   memset (mp, 0, sizeof (*mp));
819   mp->_vl_msg_id = htons (VL_API_GBP_VXLAN_TUNNEL_DETAILS + GBP_MSG_BASE);
820   mp->context = ctx->context;
821
822   mp->tunnel.vni = htonl (gt->gt_vni);
823   mp->tunnel.mode = gbp_vxlan_tunnel_layer_2_mode (gt->gt_layer);
824   mp->tunnel.bd_rd_id = htonl (gt->gt_bd_rd_id);
825
826   vl_api_send_msg (ctx->reg, (u8 *) mp);
827
828   return (1);
829 }
830
831 static void
832 vl_api_gbp_vxlan_tunnel_dump_t_handler (vl_api_gbp_vxlan_tunnel_dump_t * mp)
833 {
834   vl_api_registration_t *reg;
835
836   reg = vl_api_client_index_to_registration (mp->client_index);
837   if (!reg)
838     return;
839
840   gbp_walk_ctx_t ctx = {
841     .reg = reg,
842     .context = mp->context,
843   };
844
845   gbp_vxlan_walk (gbp_vxlan_tunnel_send_details, &ctx);
846 }
847
848 /*
849  * gbp_api_hookup
850  * Add vpe's API message handlers to the table.
851  * vlib has already mapped shared memory and
852  * added the client registration handlers.
853  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
854  */
855 #define vl_msg_name_crc_list
856 #include <gbp/gbp_all_api_h.h>
857 #undef vl_msg_name_crc_list
858
859 static void
860 setup_message_id_table (api_main_t * am)
861 {
862 #define _(id,n,crc)                                     \
863   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + GBP_MSG_BASE);
864   foreach_vl_msg_name_crc_gbp;
865 #undef _
866 }
867
868 static void
869 gbp_api_hookup (vlib_main_t * vm)
870 {
871 #define _(N,n)                                                  \
872     vl_msg_api_set_handlers(VL_API_##N + GBP_MSG_BASE,          \
873                             #n,                                 \
874                             vl_api_##n##_t_handler,             \
875                             vl_noop_handler,                    \
876                             vl_api_##n##_t_endian,              \
877                             vl_api_##n##_t_print,               \
878                             sizeof(vl_api_##n##_t), 1);
879   foreach_gbp_api_msg;
880 #undef _
881 }
882
883 static clib_error_t *
884 gbp_init (vlib_main_t * vm)
885 {
886   api_main_t *am = &api_main;
887   gbp_main_t *gbpm = &gbp_main;
888   u8 *name = format (0, "gbp_%08x%c", api_version, 0);
889
890   gbpm->gbp_acl_user_id = ~0;
891
892   /* Ask for a correctly-sized block of API message decode slots */
893   msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
894                                         VL_MSG_FIRST_AVAILABLE);
895   gbp_api_hookup (vm);
896
897   /* Add our API messages to the global name_crc hash table */
898   setup_message_id_table (am);
899
900   vec_free (name);
901   return (NULL);
902 }
903
904 VLIB_API_INIT_FUNCTION (gbp_init);
905
906 /* *INDENT-OFF* */
907 VLIB_PLUGIN_REGISTER () = {
908     .version = VPP_BUILD_VER,
909     .description = "Group Based Policy",
910 };
911 /* *INDENT-ON* */
912
913
914 /*
915  * fd.io coding-style-patch-verification: ON
916  *
917  * Local Variables:
918  * eval: (c-set-style "gnu")
919  * End:
920  */