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