GBP: l3-out subnets
[vpp.git] / src / plugins / gbp / gbp_ext_itf.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <plugins/gbp/gbp_ext_itf.h>
17 #include <plugins/gbp/gbp_bridge_domain.h>
18 #include <plugins/gbp/gbp_route_domain.h>
19 #include <plugins/gbp/gbp_itf.h>
20
21 /**
22  * Pool of GBP ext_itfs
23  */
24 gbp_ext_itf_t *gbp_ext_itf_pool;
25
26 /**
27  * external interface configs keyed by sw_if_index
28  */
29 index_t *gbp_ext_itf_db;
30
31 #define GBP_EXT_ITF_ID 0x00000080
32
33 /**
34  * logger
35  */
36 vlib_log_class_t gx_logger;
37
38 #define GBP_EXT_ITF_DBG(...)                           \
39     vlib_log_debug (gx_logger, __VA_ARGS__);
40
41 u8 *
42 format_gbp_ext_itf (u8 * s, va_list * args)
43 {
44   gbp_ext_itf_t *gx = va_arg (*args, gbp_ext_itf_t *);
45
46   return (format (s, "%U in %U",
47                   format_gbp_itf, gx->gx_itf,
48                   format_gbp_bridge_domain, gx->gx_bd));
49 }
50
51 int
52 gbp_ext_itf_add (u32 sw_if_index, u32 bd_id, u32 rd_id)
53 {
54   gbp_ext_itf_t *gx;
55   index_t gxi;
56
57   vec_validate_init_empty (gbp_ext_itf_db, sw_if_index, INDEX_INVALID);
58
59   gxi = gbp_ext_itf_db[sw_if_index];
60
61   if (INDEX_INVALID == gxi)
62     {
63       gbp_bridge_domain_t *gb;
64       gbp_route_domain_t *gr;
65       fib_protocol_t fproto;
66       index_t gbi, gri;
67
68       gbi = gbp_bridge_domain_find_and_lock (bd_id);
69
70       if (INDEX_INVALID == gbi)
71         return (VNET_API_ERROR_NO_SUCH_ENTRY);
72
73       gri = gbp_route_domain_find_and_lock (rd_id);
74
75       if (INDEX_INVALID == gri)
76         {
77           gbp_bridge_domain_unlock (gbi);
78           return (VNET_API_ERROR_NO_SUCH_ENTRY);
79         }
80
81       pool_get_zero (gbp_ext_itf_pool, gx);
82       gxi = gx - gbp_ext_itf_pool;
83
84       gb = gbp_bridge_domain_get (gbi);
85       gr = gbp_route_domain_get (gri);
86
87       gx->gx_bd = gbi;
88       gx->gx_rd = gri;
89
90       FOR_EACH_FIB_IP_PROTOCOL (fproto)
91       {
92         gx->gx_fib_index[fproto] =
93           gr->grd_fib_index[fib_proto_to_dpo (fproto)];
94       }
95
96       gx->gx_itf = gbp_itf_add_and_lock (sw_if_index, gb->gb_bd_index);
97       gbp_itf_set_l2_input_feature (gx->gx_itf, (gxi | GBP_EXT_ITF_ID),
98                                     L2INPUT_FEAT_GBP_LPM_CLASSIFY);
99
100       gbp_ext_itf_db[sw_if_index] = gxi;
101
102       GBP_EXT_ITF_DBG ("add: %U", format_gbp_ext_itf, gx);
103
104       return (0);
105     }
106
107   return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
108 }
109
110 int
111 gbp_ext_itf_delete (u32 sw_if_index)
112 {
113   gbp_ext_itf_t *gx;
114   index_t gxi;
115
116   gxi = gbp_ext_itf_db[sw_if_index];
117
118   if (INDEX_INVALID != gxi)
119     {
120       gx = pool_elt_at_index (gbp_ext_itf_pool, gxi);
121
122       GBP_EXT_ITF_DBG ("del: %U", format_gbp_ext_itf, gx);
123
124       gbp_itf_set_l2_input_feature (gx->gx_itf,
125                                     (gxi | GBP_EXT_ITF_ID),
126                                     L2INPUT_FEAT_NONE);
127       gbp_itf_unlock (gx->gx_itf);
128
129       gbp_route_domain_unlock (gx->gx_rd);
130       gbp_bridge_domain_unlock (gx->gx_bd);
131
132       gbp_ext_itf_db[sw_if_index] = INDEX_INVALID;
133       pool_put (gbp_ext_itf_pool, gx);
134
135       return (0);
136     }
137   return (VNET_API_ERROR_NO_SUCH_ENTRY);
138 }
139
140 void
141 gbp_ext_itf_walk (gbp_ext_itf_cb_t cb, void *ctx)
142 {
143   gbp_ext_itf_t *ge;
144
145   /* *INDENT-OFF* */
146   pool_foreach(ge, gbp_ext_itf_pool,
147   {
148     if (!cb(ge, ctx))
149       break;
150   });
151   /* *INDENT-ON* */
152 }
153
154 static walk_rc_t
155 gbp_ext_itf_show_one (gbp_ext_itf_t * gx, void *ctx)
156 {
157   vlib_cli_output (ctx, "  %U", format_gbp_ext_itf, gx);
158
159   return (WALK_CONTINUE);
160 }
161
162 static clib_error_t *
163 gbp_ext_itf_show (vlib_main_t * vm,
164                   unformat_input_t * input, vlib_cli_command_t * cmd)
165 {
166   vlib_cli_output (vm, "External-Interfaces:");
167   gbp_ext_itf_walk (gbp_ext_itf_show_one, vm);
168
169   return (NULL);
170 }
171
172 /*?
173  * Show Group Based Policy external interface and derived information
174  *
175  * @cliexpar
176  * @cliexstart{show gbp ext-itf}
177  * @cliexend
178  ?*/
179 /* *INDENT-OFF* */
180 VLIB_CLI_COMMAND (gbp_ext_itf_show_node, static) = {
181   .path = "show gbp ext-itf",
182   .short_help = "show gbp ext-itf\n",
183   .function = gbp_ext_itf_show,
184 };
185 /* *INDENT-ON* */
186
187 static clib_error_t *
188 gbp_ext_itf_init (vlib_main_t * vm)
189 {
190   gx_logger = vlib_log_register_class ("gbp", "ext-itf");
191
192   return (NULL);
193 }
194
195 VLIB_INIT_FUNCTION (gbp_ext_itf_init);
196
197 /*
198  * fd.io coding-style-patch-verification: ON
199  *
200  * Local Variables:
201  * eval: (c-set-style "gnu")
202  * End:
203  */