summaryrefslogtreecommitdiffstats
path: root/src/plugins/gbp/gbp_sclass.c
blob: 10ecf1fa42eeaf3e8ec4daadf59257e0c405a86e (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * 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.h>
#include <vnet/l2/l2_input.h>
#include <vnet/l2/l2_output.h>

/**
 * Grouping of global data for the GBP source EPG classification feature
 */
typedef struct gbp_sclass_main_t_
{
  /**
   * Next nodes for L2 output features
   */
  u32 gel_l2_input_feat_next[32];
  u32 gel_l2_output_feat_next[32];
} gbp_sclass_main_t;

static gbp_sclass_main_t gbp_sclass_main;

#define foreach_gbp_sclass                      \
  _(DROP,    "drop")


typedef enum
{
#define _(sym,str) GBP_SCLASS_NEXT_##sym,
  foreach_gbp_sclass
#undef _
    GBP_SCLASS_N_NEXT,
} gbp_sclass_next_t;

typedef struct gbp_sclass_trace_t_
{
  /* per-pkt trace data */
  u32 epg;
  u32 sclass;
} gbp_sclass_trace_t;

always_inline uword
gbp_sclass_inline (vlib_main_t * vm,
		   vlib_node_runtime_t * node,
		   vlib_frame_t * frame, int is_id_2_sclass, int is_l2)
{
  u32 n_left_from, *from, *to_next, next_index;
  gbp_sclass_main_t *glm;

  glm = &gbp_sclass_main;
  next_index = 0;
  n_left_from = frame->n_vectors;
  from = vlib_frame_vector_args (frame);

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  gbp_sclass_next_t next0;
	  vlib_buffer_t *b0;
	  epg_id_t epg0;
	  u16 sclass0;
	  u32 bi0;

	  next0 = GBP_SCLASS_NEXT_DROP;
	  bi0 = from[0];
	  to_next[0] = bi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);

	  if (is_id_2_sclass)
	    {
	      // output direction - convert from the SRC-EPD to the sclass
	      gbp_endpoint_group_t *gg;

	      epg0 = vnet_buffer2 (b0)->gbp.src_epg;
	      gg = gbp_epg_get (epg0);

	      if (NULL != gg)
		{
		  sclass0 = vnet_buffer2 (b0)->gbp.sclass = gg->gg_sclass;
		  if (is_l2)
		    next0 =
		      vnet_l2_feature_next (b0, glm->gel_l2_output_feat_next,
					    L2OUTPUT_FEAT_GBP_ID_2_SCLASS);
		  else
		    vnet_feature_next (&next0, b0);
		}
	      else
		sclass0 = 0;
	    }
	  else
	    {
	      /* input direction - convert from the sclass to the SRC-EGD */
	      sclass0 = vnet_buffer2 (b0)->gbp.sclass;
	      vnet_buffer2 (b0)->gbp.src_epg =
		gbp_epg_sclass_2_id (vnet_buffer2 (b0)->gbp.sclass);
	      epg0 = vnet_buffer2 (b0)->gbp.src_epg;

	      if (EPG_INVALID != epg0)
		{
		  if (is_l2)
		    next0 =
		      vnet_l2_feature_next (b0, glm->gel_l2_input_feat_next,
					    L2INPUT_FEAT_GBP_SCLASS_2_ID);
		  else
		    vnet_feature_next (&next0, b0);
		}
	    }

	  if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
	    {
	      gbp_sclass_trace_t *t =
		vlib_add_trace (vm, node, b0, sizeof (*t));
	      t->epg = epg0;
	      t->sclass = sclass0;
	    }

	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
					   to_next, n_left_to_next,
					   bi0, next0);
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  return frame->n_vectors;
}

always_inline uword
l2_gbp_id_2_sclass (vlib_main_t * vm,
		    vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (gbp_sclass_inline (vm, node, frame, 1, 1));
}

always_inline uword
l2_gbp_sclass_2_id (vlib_main_t * vm,
		    vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (gbp_sclass_inline (vm, node, frame, 0, 1));
}

always_inline uword
ip4_gbp_id_2_sclass (vlib_main_t * vm,
		     vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (gbp_sclass_inline (vm, node, frame, 1, 0));
}

always_inline uword
ip4_gbp_sclass_2_id (vlib_main_t * vm,
		     vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (gbp_sclass_inline (vm, node, frame, 0, 0));
}

always_inline uword
ip6_gbp_id_2_sclass (vlib_main_t * vm,
		     vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (gbp_sclass_inline (vm, node, frame, 1, 0));
}

always_inline uword
ip6_gbp_sclass_2_id (vlib_main_t * vm,
		     vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  return (gbp_sclass_inline (vm, node, frame, 0, 0));
}

/* packet trace format function */
static u8 *
format_gbp_sclass_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  gbp_sclass_trace_t *t = va_arg (*args, gbp_sclass_trace_t *);

  s = format (s, "epg:%d sclass:%d", t->epg, t->sclass);

  return s;
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (l2_gbp_id_2_sclass_node) = {
  .function = l2_gbp_id_2_sclass,
  .name = "l2-gbp-id-2-sclass",
  .vector_size = sizeof (u32),
  .format_trace = format_gbp_sclass_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_next_nodes = GBP_SCLASS_N_NEXT,

  .next_nodes = {
    [GBP_SCLASS_NEXT_DROP] = "error-drop",
  },
};
VLIB_REGISTER_NODE (l2_gbp_sclass_2_id_node) = {
  .function = l2_gbp_sclass_2_id,
  .name = "l2-gbp-sclass-2-id",
  .vector_size = sizeof (u32),
  .format_trace = format_gbp_sclass_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_next_nodes = GBP_SCLASS_N_NEXT,

  .next_nodes = {
    [GBP_SCLASS_NEXT_DROP] = "error-drop",
  },
};

VLIB_REGISTER_NODE (ip4_gbp_id_2_sclass_node) = {
  .function = ip4_gbp_id_2_sclass,
  .name = "ip4-gbp-id-2-sclass",
  .vector_size = sizeof (u32),
  .format_trace = format_gbp_sclass_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_next_nodes = GBP_SCLASS_N_NEXT,

  .next_nodes = {
    [GBP_SCLASS_NEXT_DROP] = "error-drop",
  },
};
VLIB_REGISTER_NODE (ip4_gbp_sclass_2_id_node) = {
  .function = ip4_gbp_sclass_2_id,
  .name = "ip4-gbp-sclass-2-id",
  .vector_size = sizeof (u32),
  .format_trace = format_gbp_sclass_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_next_nodes = GBP_SCLASS_N_NEXT,

  .next_nodes = {
    [GBP_SCLASS_NEXT_DROP] = "error-drop",
  },
};

VLIB_REGISTER_NODE (ip6_gbp_id_2_sclass_node) = {
  .function = ip6_gbp_id_2_sclass,
  .name = "ip6-gbp-id-2-sclass",
  .vector_size = sizeof (u32),
  .format_trace = format_gbp_sclass_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_next_nodes = GBP_SCLASS_N_NEXT,

  .next_nodes = {
    [GBP_SCLASS_NEXT_DROP] = "error-drop",
  },
};
VLIB_REGISTER_NODE (ip6_gbp_sclass_2_id_node) = {
  .function = ip6_gbp_sclass_2_id,
  .name = "ip6-gbp-sclass-2-id",
  .vector_size = sizeof (u32),
  .format_trace = format_gbp_sclass_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_next_nodes = GBP_SCLASS_N_NEXT,

  .next_nodes = {
    [GBP_SCLASS_NEXT_DROP] = "error-drop",
  },
};

VLIB_NODE_FUNCTION_MULTIARCH (l2_gbp_id_2_sclass_node, l2_gbp_id_2_sclass);
VLIB_NODE_FUNCTION_MULTIARCH (l2_gbp_sclass_2_id_node, l2_gbp_sclass_2_id);

VLIB_NODE_FUNCTION_MULTIARCH (ip4_gbp_id_2_sclass_node, ip4_gbp_id_2_sclass);
VLIB_NODE_FUNCTION_MULTIARCH (ip4_gbp_sclass_2_id_node, ip4_gbp_sclass_2_id);
VLIB_NODE_FUNCTION_MULTIARCH (ip6_gbp_id_2_sclass_node, ip6_gbp_id_2_sclass);
VLIB_NODE_FUNCTION_MULTIARCH (ip6_gbp_sclass_2_id_node, ip6_gbp_sclass_2_id);

VNET_FEATURE_INIT (ip4_gbp_sclass_2_id_feat, static) =
{
  .arc_name = "ip4-unicast",
  .node_name = "ip4-gbp-sclass-2-id",
  .runs_before = VNET_FEATURES ("gbp-learn-ip4"),
};
VNET_FEATURE_INIT (ip6_gbp_sclass_2_id_feat, static) =
{
  .arc_name = "ip6-unicast",
  .node_name = "ip6-gbp-sclass-2-id",
  .runs_before = VNET_FEATURES ("gbp-learn-ip6"),
};
VNET_FEATURE_INIT (ip4_gbp_id_2_sclass_feat, static) =
{
  .arc_name = "ip4-output",
  .node_name = "ip4-gbp-id-2-sclass",
};
VNET_FEATURE_INIT (ip6_gbp_id_2_sclass_feat, static) =
{
  .arc_name = "ip6-output",
  .node_name = "ip6-gbp-id-2-sclass",
};
/* *INDENT-ON* */

void
gbp_sclass_enable_l2 (u32 sw_if_index)
{
  l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_GBP_SCLASS_2_ID, 1);
  l2output_intf_bitmap_enable (sw_if_index, L2OUTPUT_FEAT_GBP_ID_2_SCLASS, 1);
}

void
gbp_sclass_disable_l2 (u32 sw_if_index)
{
  l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_GBP_SCLASS_2_ID, 0);
  l2output_intf_bitmap_enable (sw_if_index, L2OUTPUT_FEAT_GBP_ID_2_SCLASS, 0);
}

void
gbp_sclass_enable_ip (u32 sw_if_index)
{
  vnet_feature_enable_disable ("ip4-unicast",
			       "ip4-gbp-sclass-2-id", sw_if_index, 1, 0, 0);
  vnet_feature_enable_disable ("ip6-unicast",
			       "ip6-gbp-sclass-2-id", sw_if_index, 1, 0, 0);
  vnet_feature_enable_disable ("ip4-output",
			       "ip4-gbp-id-2-sclass", sw_if_index, 1, 0, 0);
  vnet_feature_enable_disable ("ip6-output",
			       "ip6-gbp-id-2-sclass", sw_if_index, 1, 0, 0);
}

void
gbp_sclass_disable_ip (u32 sw_if_index)
{
  vnet_feature_enable_disable ("ip4-unicast",
			       "ip4-gbp-sclass-2-id", sw_if_index, 0, 0, 0);
  vnet_feature_enable_disable ("ip6-unicast",
			       "ip6-gbp-sclass-2-id", sw_if_index, 0, 0, 0);
  vnet_feature_enable_disable ("ip4-output",
			       "ip4-gbp-id-2-sclass", sw_if_index, 0, 0, 0);
  vnet_feature_enable_disable ("ip6-output",
			       "ip6-gbp-id-2-sclass", sw_if_index, 0, 0, 0);
}

static clib_error_t *
gbp_sclass_init (vlib_main_t * vm)
{
  gbp_sclass_main_t *glm = &gbp_sclass_main;

  /* Initialize the feature next-node indices */
  feat_bitmap_init_next_nodes (vm,
			       l2_gbp_sclass_2_id_node.index,
			       L2INPUT_N_FEAT,
			       l2input_get_feat_names (),
			       glm->gel_l2_input_feat_next);
  feat_bitmap_init_next_nodes (vm,
			       l2_gbp_id_2_sclass_node.index,
			       L2OUTPUT_N_FEAT,
			       l2output_get_feat_names (),
			       glm->gel_l2_output_feat_next);

  return (NULL);
}

VLIB_INIT_FUNCTION (gbp_sclass_init);

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