aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/gbp/gbp_ext_itf.c
blob: f3c54fcf470318bdd8df59555d3d117b7bb3642e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <plugins/gbp/gbp_ext_itf.h>
#include <plugins/gbp/gbp_bridge_domain.h>
#include <plugins/gbp/gbp_route_domain.h>
#include <plugins/gbp/gbp_itf.h>

/**
 * Pool of GBP ext_itfs
 */
gbp_ext_itf_t *gbp_ext_itf_pool;

/**
 * external interface configs keyed by sw_if_index
 */
index_t *gbp_ext_itf_db;

#define GBP_EXT_ITF_ID 0x00000080

/**
 * logger
 */
vlib_log_class_t gx_logger;

#define GBP_EXT_ITF_DBG(...)                           \
    vlib_log_debug (gx_logger, __VA_ARGS__);

u8 *
format_gbp_ext_itf (u8 * s, va_list * args)
{
  gbp_ext_itf_t *gx = va_arg (*args, gbp_ext_itf_t *);

  return (format (s, "%U%s in %U",
		  format_gbp_itf, gx->gx_itf,
		  (gx->gx_flags & GBP_EXT_ITF_F_ANON) ? " [anon]" : "",
		  format_gbp_bridge_domain, gx->gx_bd));
}

int
gbp_ext_itf_add (u32 sw_if_index, u32 bd_id, u32 rd_id, u32 flags)
{
  gbp_ext_itf_t *gx;
  index_t gxi;

  vec_validate_init_empty (gbp_ext_itf_db, sw_if_index, INDEX_INVALID);

  gxi = gbp_ext_itf_db[sw_if_index];

  if (INDEX_INVALID == gxi)
    {
      gbp_route_domain_t *gr;
      fib_protocol_t fproto;
      index_t gbi, gri;

      gbi = gbp_bridge_domain_find_and_lock (bd_id);

      if (INDEX_INVALID == gbi)
	return (VNET_API_ERROR_NO_SUCH_ENTRY);

      gri = gbp_route_domain_find_and_lock (rd_id);

      if (INDEX_INVALID == gri)
	{
	  gbp_bridge_domain_unlock (gbi);
	  return (VNET_API_ERROR_NO_SUCH_ENTRY);
	}

      pool_get_zero (gbp_ext_itf_pool, gx);
      gxi = gx - gbp_ext_itf_pool;

      gr = gbp_route_domain_get (gri);

      gx->gx_bd = gbi;
      gx->gx_rd = gri;
      gx->gx_itf = sw_if_index;

      FOR_EACH_FIB_IP_PROTOCOL (fproto)
      {
	gx->gx_fib_index[fproto] =
	  gr->grd_fib_index[fib_proto_to_dpo (fproto)];
      }

      if (flags & GBP_EXT_ITF_F_ANON)
	{
	  /* add interface to the BD */
	  index_t itf = gbp_itf_add_and_lock (sw_if_index,
					      gbp_bridge_domain_get
					      (gbi)->gb_bd_index);
	  /* setup GBP L2 features on this interface */
	  gbp_itf_set_l2_input_feature (itf, 0,
					L2INPUT_FEAT_GBP_LPM_ANON_CLASSIFY |
					L2INPUT_FEAT_LEARN);
	  gbp_itf_set_l2_output_feature (itf, 0,
					 L2OUTPUT_FEAT_GBP_POLICY_LPM);
	}

      gx->gx_flags = flags;

      gbp_ext_itf_db[sw_if_index] = gxi;

      GBP_EXT_ITF_DBG ("add: %U", format_gbp_ext_itf, gx);

      return (0);
    }

  return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
}

int
gbp_ext_itf_delete (u32 sw_if_index)
{
  gbp_ext_itf_t *gx;
  index_t gxi;

  if (vec_len (gbp_ext_itf_db) <= sw_if_index)
    return (VNET_API_ERROR_INVALID_SW_IF_INDEX);

  gxi = gbp_ext_itf_db[sw_if_index];

  if (INDEX_INVALID != gxi)
    {
      gx = pool_elt_at_index (gbp_ext_itf_pool, gxi);

      GBP_EXT_ITF_DBG ("del: %U", format_gbp_ext_itf, gx);

      if (gx->gx_flags & GBP_EXT_ITF_F_ANON)
	gbp_itf_unlock (gx->gx_itf);

      gbp_route_domain_unlock (gx->gx_rd);
      gbp_bridge_domain_unlock (gx->gx_bd);

      gbp_ext_itf_db[sw_if_index] = INDEX_INVALID;
      pool_put (gbp_ext_itf_pool, gx);

      return (0);
    }
  return (VNET_API_ERROR_NO_SUCH_ENTRY);
}

static clib_error_t *
gbp_ext_itf_add_del_cli (vlib_main_t * vm,
			 unformat_input_t * input, vlib_cli_command_t * cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  u32 sw_if_index = ~0, bd_id = ~0, rd_id = ~0, flags = 0;
  int add = 1;
  int rv;

  /* Get a line of input. */
  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "del"))
	add = 0;
      else
	if (unformat
	    (line_input, "%U", unformat_vnet_sw_interface, vnet_get_main (),
	     &sw_if_index))
	;
      else if (unformat (line_input, "bd %d", &bd_id))
	;
      else if (unformat (line_input, "rd %d", &rd_id))
	;
      else if (unformat (line_input, "anon-l3-out"))
	flags |= GBP_EXT_ITF_F_ANON;
      else
	return clib_error_return (0, "unknown input `%U'",
				  format_unformat_error, line_input);
    }
  unformat_free (line_input);

  if (~0 == sw_if_index)
    return clib_error_return (0, "interface must be specified");

  if (add)
    {
      if (~0 == bd_id)
	return clib_error_return (0, "BD-ID must be specified");
      if (~0 == rd_id)
	return clib_error_return (0, "RD-ID must be specified");
      rv = gbp_ext_itf_add (sw_if_index, bd_id, rd_id, flags);
    }
  else
    rv = gbp_ext_itf_delete (sw_if_index);

  switch (rv)
    {
    case 0:
      return 0;
    case VNET_API_ERROR_ENTRY_ALREADY_EXISTS:
      return clib_error_return (0, "interface already exists");
    case VNET_API_ERROR_NO_SUCH_ENTRY:	/* fallthrough */
    case VNET_API_ERROR_INVALID_SW_IF_INDEX:
      return clib_error_return (0, "unknown interface");
    default:
      return clib_error_return (0, "error %d", rv);
    }

  /* never reached */
  return 0;
}

/*?
 * Add Group Based Interface as anonymous L3out interface
 *
 * @cliexpar
 * @cliexstart{gbp interface [del] anon-l3out <interface> bd <ID>}
 * @cliexend
 ?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (gbp_itf_anon_l3out_add_del_node, static) = {
  .path = "gbp ext-itf",
  .short_help = "gbp ext-itf [del] <interface> bd <ID> rd <ID> [anon-l3-out]\n",
  .function = gbp_ext_itf_add_del_cli,
};
/* *INDENT-ON* */

void
gbp_ext_itf_walk (gbp_ext_itf_cb_t cb, void *ctx)
{
  gbp_ext_itf_t *ge;

  /* *INDENT-OFF* */
  pool_foreach(ge, gbp_ext_itf_pool,
  {
    if (!cb(ge, ctx))
      break;
  });
  /* *INDENT-ON* */
}

static walk_rc_t
gbp_ext_itf_show_one (gbp_ext_itf_t * gx, void *ctx)
{
  vlib_cli_output (ctx, "  %U", format_gbp_ext_itf, gx);

  return (WALK_CONTINUE);
}

static clib_error_t *
gbp_ext_itf_show (vlib_main_t * vm,
		  unformat_input_t * input, vlib_cli_command_t * cmd)
{
  vlib_cli_output (vm, "External-Interfaces:");
  gbp_ext_itf_walk (gbp_ext_itf_show_one, vm);

  return (NULL);
}

/*?
 * Show Group Based Policy external interface and derived information
 *
 * @cliexpar
 * @cliexstart{show gbp ext-itf}
 * @cliexend
 ?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (gbp_ext_itf_show_node, static) = {
  .path = "show gbp ext-itf",
  .short_help = "show gbp ext-itf\n",
  .function = gbp_ext_itf_show,
};
/* *INDENT-ON* */

static clib_error_t *
gbp_ext_itf_init (vlib_main_t * vm)
{
  gx_logger = vlib_log_register_class ("gbp", "ext-itf");

  return (NULL);
}

VLIB_INIT_FUNCTION (gbp_ext_itf_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */