summaryrefslogtreecommitdiffstats
path: root/extras/deprecated/plugins/l2e/l2e.c
blob: 4c6eac50446afafdf4a94d986b392f2246e21da5 (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
/*
 * l2e.c : Extract L3 packets from the L2 input and feed
 *                   them into the L3 path.
 *
 * 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 <plugins/l2e/l2e.h>
#include <vnet/l2/l2_input.h>
#include <vnet/l2/feat_bitmap.h>
#include <vnet/ip/ip.h>

l2_emulation_main_t l2_emulation_main;

/**
 * A zero'd out struct we can use in the vec_validate
 */
static const l2_emulation_t ezero = { };

__clib_export void
l2_emulation_enable (u32 sw_if_index)
{
  l2_emulation_main_t *em = &l2_emulation_main;
  vec_validate_init_empty (em->l2_emulations, sw_if_index, ezero);

  l2_emulation_t *l23e = &em->l2_emulations[sw_if_index];

  l23e->enabled = 1;

  /*
   * L3 enable the interface - using IP unnumbered from the control
   * plane may not be possible since there may be no BVI interface
   * to which to unnumber
   */
  ip4_sw_interface_enable_disable (sw_if_index, 1);
  ip6_sw_interface_enable_disable (sw_if_index, 1);

  l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_L2_EMULATION, 1);
}


__clib_export void
l2_emulation_disable (u32 sw_if_index)
{
  l2_emulation_main_t *em = &l2_emulation_main;
  if (vec_len (em->l2_emulations) >= sw_if_index)
    {
      l2_emulation_t *l23e = &em->l2_emulations[sw_if_index];
      clib_memset (l23e, 0, sizeof (*l23e));

      l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_L2_EMULATION, 0);
      ip4_sw_interface_enable_disable (sw_if_index, 0);
      ip6_sw_interface_enable_disable (sw_if_index, 0);
    }
}

static clib_error_t *
l2_emulation_interface_add_del (vnet_main_t * vnm,
				u32 sw_if_index, u32 is_add)
{
  l2_emulation_main_t *em = &l2_emulation_main;
  if (is_add)
    {
      vec_validate_init_empty (em->l2_emulations, sw_if_index, ezero);
    }

  return (NULL);
}

VNET_SW_INTERFACE_ADD_DEL_FUNCTION (l2_emulation_interface_add_del);

static clib_error_t *
l2_emulation_cli (vlib_main_t * vm,
		  unformat_input_t * input, vlib_cli_command_t * cmd)
{
  vnet_main_t *vnm = vnet_get_main ();
  u32 sw_if_index = ~0;
  u8 enable = 1;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%U", unformat_vnet_sw_interface,
		    vnm, &sw_if_index))
	;
      else if (unformat (input, "enable"))
	enable = 1;
      else if (unformat (input, "disable"))
	enable = 0;
      else
	break;
    }

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

  if (enable)
    l2_emulation_enable (sw_if_index);
  else
    l2_emulation_disable (sw_if_index);

  return (NULL);
}

/*?
 * Configure l2 emulation.
 *  When the interface is in L2 mode, configure the extraction of L3
 *  packets out of the L2 path and into the L3 path.
 *
 * @cliexpar
 * @cliexstart{set interface l2 input l2-emulation <interface-name> [disable]}
 * @cliexend
 ?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (l2_emulation_cli_node, static) = {
  .path = "set interface l2 l2-emulation",
  .short_help =
  "set interface l2 l2-emulation <interface-name> [disable|enable]\n",
  .function = l2_emulation_cli,
};
/* *INDENT-ON* */

static clib_error_t *
l2_emulation_show (vlib_main_t * vm,
		   unformat_input_t * input, vlib_cli_command_t * cmd)
{
  l2_emulation_main_t *em = &l2_emulation_main;
  vnet_main_t *vnm = vnet_get_main ();
  l2_emulation_t *l23e;
  u32 sw_if_index;

  vec_foreach_index (sw_if_index, em->l2_emulations)
  {
    l23e = &em->l2_emulations[sw_if_index];
    if (l23e->enabled)
      {
	vlib_cli_output (vm, "%U\n",
			 format_vnet_sw_if_index_name, vnm, sw_if_index);
      }
  }
  return (NULL);
}

/*?
 * Show l2 emulation.
 *  When the interface is in L2 mode, configure the extraction of L3
 *  packets out of the L2 path and into the L3 path.
 *
 * @cliexpar
 * @cliexstart{show interface l2 l2-emulation}
 * @cliexend
 ?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (l2_emulation_show_node, static) = {
  .path = "show interface l2 l2-emulation",
  .short_help = "show interface l2 l2-emulation\n",
  .function = l2_emulation_show,
};
/* *INDENT-ON* */

static clib_error_t *
l2_emulation_init (vlib_main_t * vm)
{
  l2_emulation_main_t *em = &l2_emulation_main;
  vlib_node_t *node;

  node = vlib_get_node_by_name (vm, (u8 *) "l2-emulation");
  em->l2_emulation_node_index = node->index;

  /* Initialize the feature next-node indexes */
  feat_bitmap_init_next_nodes (vm,
			       em->l2_emulation_node_index,
			       L2INPUT_N_FEAT,
			       l2input_get_feat_names (),
			       em->l2_input_feat_next);

  return 0;
}

VLIB_INIT_FUNCTION (l2_emulation_init);

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