aboutsummaryrefslogtreecommitdiffstats
path: root/vnet/vnet/l2/l2_vtr.c
blob: a74990410099409ab6c34d4ceab652d5532b5050 (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * l2_vtr.c : layer 2 vlan tag rewrite configuration
 *
 * Copyright (c) 2013 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 <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/ethernet/packet.h>
#include <vnet/l2/l2_input.h>
#include <vnet/l2/l2_output.h>
#include <vnet/l2/feat_bitmap.h>
#include <vnet/l2/l2_vtr.h>
#include <vnet/l2/l2_input_vtr.h>
#include <vnet/l2/l2_output.h>

#include <vppinfra/error.h>
#include <vlib/cli.h>


// Just a placeholder. Also insures file is not eliminated by linker.
clib_error_t *l2_vtr_init (vlib_main_t *vm)
{
  return 0;
}

VLIB_INIT_FUNCTION(l2_vtr_init);


// Configure vtag tag rewrite on the given interface.
// Return 1 if there is an error, 0 if ok
u32 l2vtr_configure (vlib_main_t * vlib_main,
                     vnet_main_t * vnet_main,
                     u32           sw_if_index,
                     u32           vtr_op,
                     u32           push_dot1q, // ethertype of first pushed tag is dot1q/dot1ad
                     u32           vtr_tag1,   // first pushed tag
                     u32           vtr_tag2)   // second pushed tag
{
  vnet_hw_interface_t * hi;
  vnet_sw_interface_t * si;
  u32 hw_no_tags;
  u32 error = 0;
  vtr_config_t * in_config;
  vtr_config_t * out_config;
  u32 enable;
  u32 push_inner_et;
  u32 push_outer_et;
  u32 cfg_tags;

  hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
  if (!hi || (hi->hw_class_index != ethernet_hw_interface_class.index)) {
    error = VNET_API_ERROR_INVALID_INTERFACE;  // non-ethernet interface
    goto done;
  }

  // Init the config for this interface
  vec_validate (l2output_main.configs, sw_if_index);
  in_config = &(vec_elt_at_index(l2output_main.configs, sw_if_index)->input_vtr);
  out_config = &(vec_elt_at_index(l2output_main.configs, sw_if_index)->output_vtr);
  in_config->raw_tags = 0;
  out_config->raw_tags = 0;

  // Get the configured tags for the interface
  si = vnet_get_sw_interface (vnet_main, sw_if_index);
  hw_no_tags = (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE);

  // Construct the input tag-rewrite config

  push_outer_et = clib_net_to_host_u16 (push_dot1q ? ETHERNET_TYPE_VLAN : ETHERNET_TYPE_DOT1AD);
  push_inner_et = clib_net_to_host_u16 (ETHERNET_TYPE_VLAN);
  vtr_tag1 = clib_net_to_host_u16 (vtr_tag1);
  vtr_tag2 = clib_net_to_host_u16 (vtr_tag2);

  // Determine number of vlan tags with explictly configured values
  cfg_tags = 0;
  if (hw_no_tags || si->sub.eth.flags.no_tags) {
    cfg_tags = 0;
  } else if (si->sub.eth.flags.one_tag) {
    cfg_tags = 1;
    if (si->sub.eth.flags.outer_vlan_id_any) {
      cfg_tags = 0;
    }
  } else if (si->sub.eth.flags.two_tags) {
    cfg_tags = 2;
    if (si->sub.eth.flags.inner_vlan_id_any) {
      cfg_tags = 1;
    }
    if (si->sub.eth.flags.outer_vlan_id_any) {
      cfg_tags = 0;
    }
  } 

  switch (vtr_op) {
  case L2_VTR_DISABLED:
    in_config->push_and_pop_bytes = 0;
    break;

  case L2_VTR_POP_1:
    if (cfg_tags < 1) {
      error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT; // Need one or two tags
      goto done;
    }
    in_config->pop_bytes = 4;
    in_config->push_bytes = 0;
    break;

  case L2_VTR_POP_2:
    if (cfg_tags < 2) {
      error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT; // Need two tags
      goto done;
    }
    in_config->pop_bytes = 8;
    in_config->push_bytes = 0;

    out_config->push_bytes = in_config->pop_bytes;
    out_config->pop_bytes = in_config->push_bytes;
    break;

  case L2_VTR_PUSH_1:
    in_config->pop_bytes = 0;
    in_config->push_bytes = 4;
    in_config->tags[1].priority_cfi_and_id = vtr_tag1;
    in_config->tags[1].type = push_outer_et;
    break;

  case L2_VTR_PUSH_2:
    in_config->pop_bytes = 0;
    in_config->push_bytes = 8;
    in_config->tags[0].priority_cfi_and_id = vtr_tag1;
    in_config->tags[0].type = push_outer_et;
    in_config->tags[1].priority_cfi_and_id = vtr_tag2;
    in_config->tags[1].type = push_inner_et;
    break;

  case L2_VTR_TRANSLATE_1_1:
    if (cfg_tags < 1) {
      error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT; // Need one or two tags
      goto done;
    }
    in_config->pop_bytes = 4;
    in_config->push_bytes = 4;
    in_config->tags[1].priority_cfi_and_id = vtr_tag1;
    in_config->tags[1].type = push_outer_et;
    break;

  case L2_VTR_TRANSLATE_1_2:
    if (cfg_tags < 1) {
      error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT; // Need one or two tags
      goto done;
    }
    in_config->pop_bytes = 4;
    in_config->push_bytes = 8;
    in_config->tags[0].priority_cfi_and_id = vtr_tag1;
    in_config->tags[0].type = push_outer_et;
    in_config->tags[1].priority_cfi_and_id = vtr_tag2;
    in_config->tags[1].type = push_inner_et;
    break;

  case L2_VTR_TRANSLATE_2_1:
    if (cfg_tags < 2) {
      error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT; // Need two tags
      goto done;
    }
    in_config->pop_bytes = 8;
    in_config->push_bytes = 4;
    in_config->tags[1].priority_cfi_and_id = vtr_tag1;
    in_config->tags[1].type = push_outer_et;
    break;

  case L2_VTR_TRANSLATE_2_2:
    if (cfg_tags < 2) {
      error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT; // Need two tags
      goto done;
    }
    in_config->pop_bytes = 8;
    in_config->push_bytes = 8;
    in_config->tags[0].priority_cfi_and_id = vtr_tag1;
    in_config->tags[0].type = push_outer_et;
    in_config->tags[1].priority_cfi_and_id = vtr_tag2;
    in_config->tags[1].type = push_inner_et;
    break;
  }

  // Construct the output tag-rewrite config

  // The push/pop values are always reversed
  out_config->push_bytes = in_config->pop_bytes;
  out_config->pop_bytes = in_config->push_bytes;

  // Any pushed tags are derived from the subinterface config
  push_outer_et = clib_net_to_host_u16 (si->sub.eth.flags.dot1ad ? ETHERNET_TYPE_DOT1AD : ETHERNET_TYPE_VLAN);
  push_inner_et = clib_net_to_host_u16 (ETHERNET_TYPE_VLAN);
  vtr_tag1 = clib_net_to_host_u16 (si->sub.eth.outer_vlan_id);
  vtr_tag2 = clib_net_to_host_u16 (si->sub.eth.inner_vlan_id);

  if (out_config->push_bytes == 4) {
    out_config->tags[1].priority_cfi_and_id = vtr_tag1;
    out_config->tags[1].type = push_outer_et;
  } else if (out_config->push_bytes == 8) {
    out_config->tags[0].priority_cfi_and_id = vtr_tag1;
    out_config->tags[0].type = push_outer_et;
    out_config->tags[1].priority_cfi_and_id = vtr_tag2;
    out_config->tags[1].type = push_inner_et;
  }
  
  // set the interface enable flags
  enable = (vtr_op != L2_VTR_DISABLED);
  l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VTR, enable);
  // output vtr enable is checked explicitly in l2_output

 done:
  return error;
}

// Get vtag tag rewrite on the given interface.
// Return 1 if there is an error, 0 if ok
u32 l2vtr_get (vlib_main_t * vlib_main,
               vnet_main_t * vnet_main,
               u32           sw_if_index,
               u32           *vtr_op,
               u32           *push_dot1q, // ethertype of first pushed tag is dot1q/dot1ad
               u32           *vtr_tag1,   // first pushed tag
               u32           *vtr_tag2)   // second pushed tag
{
  vnet_hw_interface_t * hi;
  u32 error = 0;
  vtr_config_t * in_config;

  if (!vtr_op || !push_dot1q || !vtr_tag1 || !vtr_tag2) {
    clib_warning ("invalid arguments");
    error = VNET_API_ERROR_INVALID_ARGUMENT;
    goto done;
  }

  *vtr_op = L2_VTR_DISABLED;
  *vtr_tag1 = 0;
  *vtr_tag2 = 0;
  *push_dot1q = 0;

  hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
  if (!hi || (hi->hw_class_index != ethernet_hw_interface_class.index)) {
    // non-ethernet interface
    goto done;
  }

  if (sw_if_index >= vec_len(l2output_main.configs)) {
    // no specific config (return disabled)
    goto done;
  }

  // Get the config for this interface
  in_config = &(vec_elt_at_index(l2output_main.configs, sw_if_index)->input_vtr);

  // DISABLED
  if (in_config->push_and_pop_bytes == 0) {
    goto done;
  }

  // find out vtr_op
  switch (in_config->pop_bytes) {
    case 0:
      switch (in_config->push_bytes) {
        case 0:
          // DISABLED
          goto done;
        case 4:
          *vtr_op = L2_VTR_PUSH_1;
          *vtr_tag1 = clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
          *push_dot1q = (ETHERNET_TYPE_VLAN == clib_host_to_net_u16 (in_config->tags[1].type));
          break;
        case 8:
          *vtr_op = L2_VTR_PUSH_2;
          *vtr_tag1 = clib_host_to_net_u16 (in_config->tags[0].priority_cfi_and_id);
          *vtr_tag2 = clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
          *push_dot1q = (ETHERNET_TYPE_VLAN == clib_host_to_net_u16 (in_config->tags[0].type));
          break;
        default:
          clib_warning ("invalid push_bytes count: %d", in_config->push_bytes);
          error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
          goto done;
      }
      break;

    case 4:
      switch (in_config->push_bytes) {
        case 0:
          *vtr_op = L2_VTR_POP_1;
          break;
        case 4:
          *vtr_op = L2_VTR_TRANSLATE_1_1;
          *vtr_tag1 = clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
          *push_dot1q = (ETHERNET_TYPE_VLAN == clib_host_to_net_u16 (in_config->tags[1].type));
          break;
        case 8:
          *vtr_op = L2_VTR_TRANSLATE_1_2;
          *vtr_tag1 = clib_host_to_net_u16 (in_config->tags[0].priority_cfi_and_id);
          *vtr_tag2 = clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
          *push_dot1q = (ETHERNET_TYPE_VLAN == clib_host_to_net_u16 (in_config->tags[0].type));
          break;
        default:
          clib_warning ("invalid push_bytes count: %d", in_config->push_bytes);
          error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
          goto done;
      }
      break;

    case 8:
      switch (in_config->push_bytes) {
        case 0:
          *vtr_op = L2_VTR_POP_2;
          break;
        case 4:
          *vtr_op = L2_VTR_TRANSLATE_2_1;
          *vtr_tag1 = clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
          *push_dot1q = (ETHERNET_TYPE_VLAN == clib_host_to_net_u16 (in_config->tags[1].type));
          break;
        case 8:
          *vtr_op = L2_VTR_TRANSLATE_2_2;
          *vtr_tag1 = clib_host_to_net_u16 (in_config->tags[0].priority_cfi_and_id);
          *vtr_tag2 = clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
          *push_dot1q = (ETHERNET_TYPE_VLAN == clib_host_to_net_u16 (in_config->tags[0].type));
          break;
        default:
          clib_warning ("invalid push_bytes count: %d", in_config->push_bytes);
          error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
          goto done;
      }
      break;

    default:
      clib_warning ("invalid pop_bytes count: %d", in_config->pop_bytes);
      error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
      goto done;
  }

 done:
  return error;
}

// set subinterface vtr enable/disable
// The CLI format is:
//    set interface l2 tag-rewrite <interface> [disable | pop 1 | pop 2 | push {dot1q|dot1ad} <tag> [<tag>]]
// "push" can also be replaced by "translate-{1|2}-{1|2}"
static clib_error_t *
int_l2_vtr (vlib_main_t * vm,
            unformat_input_t * input,
            vlib_cli_command_t * cmd)
{
  vnet_main_t * vnm = vnet_get_main();
  clib_error_t * error = 0;
  u32 sw_if_index;
  u32 vtr_op;
  u32 push_dot1q = 0;
  u32 tag1 = 0, tag2 = 0;

  if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
    {
      error = clib_error_return (0, "unknown interface `%U'",
                                 format_unformat_error, input);
      goto done;
    }

  vtr_op = L2_VTR_DISABLED;

  if (unformat (input, "disable")) {
    vtr_op = L2_VTR_DISABLED;
  } else if (unformat (input, "pop 1")) {
    vtr_op = L2_VTR_POP_1;
  } else if (unformat (input, "pop 2")) {
    vtr_op = L2_VTR_POP_2;

  } else if (unformat (input, "push dot1q %d %d", &tag1, &tag2)) {
    vtr_op = L2_VTR_PUSH_2;
    push_dot1q = 1;
  } else if (unformat (input, "push dot1ad %d %d", &tag1, &tag2)) {
    vtr_op = L2_VTR_PUSH_2;

  } else if (unformat (input, "push dot1q %d", &tag1)) {
    vtr_op = L2_VTR_PUSH_1;
    push_dot1q = 1;
  } else if (unformat (input, "push dot1ad %d", &tag1)) {
    vtr_op = L2_VTR_PUSH_1;

  } else if (unformat (input, "translate 1-1 dot1q %d", &tag1)) {
    vtr_op = L2_VTR_TRANSLATE_1_1;
    push_dot1q = 1;
  } else if (unformat (input, "translate 1-1 dot1ad %d", &tag1)) {
    vtr_op = L2_VTR_TRANSLATE_1_1;

  } else if (unformat (input, "translate 2-1 dot1q %d", &tag1)) {
    vtr_op = L2_VTR_TRANSLATE_2_1;
    push_dot1q = 1;
  } else if (unformat (input, "translate 2-1 dot1ad %d", &tag1)) {
    vtr_op = L2_VTR_TRANSLATE_2_1;

  } else if (unformat (input, "translate 2-2 dot1q %d %d", &tag1, &tag2)) {
    vtr_op = L2_VTR_TRANSLATE_2_2;
    push_dot1q = 1;
  } else if (unformat (input, "translate 2-2 dot1ad %d %d", &tag1, &tag2)) {
    vtr_op = L2_VTR_TRANSLATE_2_2;

  } else if (unformat (input, "translate 1-2 dot1q %d %d", &tag1, &tag2)) {
    vtr_op = L2_VTR_TRANSLATE_1_2;
    push_dot1q = 1;
  } else if (unformat (input, "translate 1-2 dot1ad %d %d", &tag1, &tag2)) {
    vtr_op = L2_VTR_TRANSLATE_1_2;

  } else {
    error = clib_error_return (0, "expecting [disable | pop 1 | pop 2 | push {dot1q|dot1ah} <tag> [<tag>]\n"
                               " | translate {1|2}-{1|2} {dot1q|dot1ah} <tag> [<tag>]] but got `%U'",
                               format_unformat_error, input);
    goto done;
  }

  if (l2vtr_configure (vm,
                       vnm,
                       sw_if_index,
                       vtr_op,
                       push_dot1q,
                       tag1,
                       tag2)) {
    error = clib_error_return (0, "vlan tag rewrite is not compatible with interface");
    goto done;
  }

 done:
  return error;
}

VLIB_CLI_COMMAND (int_l2_vtr_cli, static) = {
  .path = "set interface l2 tag-rewrite",
  .short_help = "set interface l2 tag-rewrite <interface> [disable | pop {1|2} | push {dot1q|dot1ad} <tag> <tag>]",
  .function = int_l2_vtr,
};