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
|
/*
* Copyright 2016 Intel Corporation
*
* 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 "tap_inject.h"
#include <vnet/mfib/mfib_table.h>
#include <vnet/ip/ip.h>
#include <vnet/ip/lookup.h>
#include <vnet/fib/fib.h>
static tap_inject_main_t tap_inject_main;
extern dpo_type_t tap_inject_dpo_type;
tap_inject_main_t *
tap_inject_get_main (void)
{
return &tap_inject_main;
}
void
tap_inject_insert_tap (u32 sw_if_index, u32 tap_fd, u32 tap_if_index)
{
tap_inject_main_t * im = tap_inject_get_main ();
vec_validate_init_empty (im->sw_if_index_to_tap_fd, sw_if_index, ~0);
vec_validate_init_empty (im->sw_if_index_to_tap_if_index, sw_if_index, ~0);
vec_validate_init_empty (im->tap_fd_to_sw_if_index, tap_fd, ~0);
im->sw_if_index_to_tap_fd[sw_if_index] = tap_fd;
im->sw_if_index_to_tap_if_index[sw_if_index] = tap_if_index;
im->tap_fd_to_sw_if_index[tap_fd] = sw_if_index;
hash_set (im->tap_if_index_to_sw_if_index, tap_if_index, sw_if_index);
}
void
tap_inject_delete_tap (u32 sw_if_index)
{
tap_inject_main_t * im = tap_inject_get_main ();
u32 tap_fd = im->sw_if_index_to_tap_fd[sw_if_index];
u32 tap_if_index = im->sw_if_index_to_tap_if_index[sw_if_index];
im->sw_if_index_to_tap_if_index[sw_if_index] = ~0;
im->sw_if_index_to_tap_fd[sw_if_index] = ~0;
im->tap_fd_to_sw_if_index[tap_fd] = ~0;
hash_unset (im->tap_if_index_to_sw_if_index, tap_if_index);
}
u32
tap_inject_lookup_tap_fd (u32 sw_if_index)
{
tap_inject_main_t * im = tap_inject_get_main ();
vec_validate_init_empty (im->sw_if_index_to_tap_fd, sw_if_index, ~0);
return im->sw_if_index_to_tap_fd[sw_if_index];
}
u32
tap_inject_lookup_sw_if_index_from_tap_fd (u32 tap_fd)
{
tap_inject_main_t * im = tap_inject_get_main ();
vec_validate_init_empty (im->tap_fd_to_sw_if_index, tap_fd, ~0);
return im->tap_fd_to_sw_if_index[tap_fd];
}
u32
tap_inject_lookup_sw_if_index_from_tap_if_index (u32 tap_if_index)
{
tap_inject_main_t * im = tap_inject_get_main ();
uword * sw_if_index;
sw_if_index = hash_get (im->tap_if_index_to_sw_if_index, tap_if_index);
return sw_if_index ? *(u32 *)sw_if_index : ~0;
}
/* *INDENT-OFF* */
VLIB_PLUGIN_REGISTER () = {
// .version = VPP_BUILD_VER, FIXME
.description = "router",
};
/* *INDENT-ON* */
static void
tap_inject_disable (void)
{
tap_inject_main_t * im = tap_inject_get_main ();
im->flags &= ~TAP_INJECT_F_ENABLED;
clib_warning ("tap-inject is not actually disabled.");
}
static clib_error_t *
tap_inject_enable (void)
{
vlib_main_t * vm = vlib_get_main ();
tap_inject_main_t * im = tap_inject_get_main ();
if (tap_inject_is_enabled ())
return 0;
tap_inject_enable_netlink ();
/* Only enable netlink? */
if (im->flags & TAP_INJECT_F_CONFIG_NETLINK)
{
im->flags |= TAP_INJECT_F_ENABLED;
return 0;
}
/* Register ARP and ICMP6 as neighbor nodes. */
ethernet_register_input_type (vm, ETHERNET_TYPE_ARP, im->neighbor_node_index);
ip6_register_protocol (IP_PROTOCOL_ICMP6, im->neighbor_node_index);
/* Register remaining protocols. */
ip4_register_protocol (IP_PROTOCOL_ICMP, im->tx_node_index);
ip4_register_protocol (IP_PROTOCOL_OSPF, im->tx_node_index);
ip4_register_protocol (IP_PROTOCOL_TCP, im->tx_node_index);
ip4_register_protocol (IP_PROTOCOL_UDP, im->tx_node_index);
ip6_register_protocol (IP_PROTOCOL_OSPF, im->tx_node_index);
ip6_register_protocol (IP_PROTOCOL_TCP, im->tx_node_index);
ip6_register_protocol (IP_PROTOCOL_UDP, im->tx_node_index);
{
dpo_id_t dpo = DPO_INVALID;
const mfib_prefix_t pfx_224_0_0_0 = {
.fp_len = 24,
.fp_proto = FIB_PROTOCOL_IP4,
.fp_grp_addr = {
.ip4.as_u32 = clib_host_to_net_u32(0xe0000000),
},
.fp_src_addr = {
.ip4.as_u32 = 0,
},
};
dpo_set(&dpo, tap_inject_dpo_type, DPO_PROTO_IP4, ~0);
index_t repi = replicate_create(1, DPO_PROTO_IP4);
replicate_set_bucket(repi, 0, &dpo);
mfib_table_entry_special_add(0,
&pfx_224_0_0_0,
MFIB_SOURCE_API,
MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF,
repi);
dpo_reset(&dpo);
}
im->flags |= TAP_INJECT_F_ENABLED;
return 0;
}
static uword
tap_inject_iface_isr (vlib_main_t * vm, vlib_node_runtime_t * node,
vlib_frame_t * f)
{
tap_inject_main_t * im = tap_inject_get_main ();
vnet_hw_interface_t * hw;
u32 * hw_if_index;
clib_error_t * err = 0;
vec_foreach (hw_if_index, im->interfaces_to_enable)
{
hw = vnet_get_hw_interface (vnet_get_main (), *hw_if_index);
if (hw->hw_class_index == ethernet_hw_interface_class.index)
{
err = tap_inject_tap_connect (hw);
if (err)
break;
}
}
vec_foreach (hw_if_index, im->interfaces_to_disable)
tap_inject_tap_disconnect (*hw_if_index);
vec_free (im->interfaces_to_enable);
vec_free (im->interfaces_to_disable);
return err ? -1 : 0;
}
VLIB_REGISTER_NODE (tap_inject_iface_isr_node, static) = {
.function = tap_inject_iface_isr,
.name = "tap-inject-iface-isr",
.type = VLIB_NODE_TYPE_INPUT,
.state = VLIB_NODE_STATE_INTERRUPT,
.vector_size = sizeof (u32),
};
static clib_error_t *
tap_inject_interface_add_del (struct vnet_main_t * vnet_main, u32 hw_if_index,
u32 add)
{
vlib_main_t * vm = vlib_get_main ();
tap_inject_main_t * im = tap_inject_get_main ();
if (!tap_inject_is_config_enabled ())
return 0;
tap_inject_enable ();
if (add)
vec_add1 (im->interfaces_to_enable, hw_if_index);
else
vec_add1 (im->interfaces_to_disable, hw_if_index);
vlib_node_set_interrupt_pending (vm, tap_inject_iface_isr_node.index);
return 0;
}
VNET_HW_INTERFACE_ADD_DEL_FUNCTION (tap_inject_interface_add_del);
static clib_error_t *
tap_inject_enable_disable_all_interfaces (int enable)
{
vnet_main_t * vnet_main = vnet_get_main ();
tap_inject_main_t * im = tap_inject_get_main ();
vnet_hw_interface_t * interfaces;
vnet_hw_interface_t * hw;
u32 ** indices;
if (enable)
tap_inject_enable ();
else
tap_inject_disable ();
/* Collect all the interface indices. */
interfaces = vnet_main->interface_main.hw_interfaces;
indices = enable ? &im->interfaces_to_enable : &im->interfaces_to_disable;
pool_foreach (hw, interfaces, vec_add1 (*indices, hw - interfaces));
if (tap_inject_iface_isr (vlib_get_main (), 0, 0))
return clib_error_return (0, "tap-inject interface add del isr failed");
return 0;
}
static clib_error_t *
tap_inject_cli (vlib_main_t * vm, unformat_input_t * input,
vlib_cli_command_t * cmd)
{
tap_inject_main_t * im = tap_inject_get_main ();
if (cmd->function_arg)
{
clib_error_t * err;
if (tap_inject_is_config_disabled ())
return clib_error_return (0,
"tap-inject is disabled in config, thus cannot be enabled.");
/* Enable */
err = tap_inject_enable_disable_all_interfaces (1);
if (err)
{
tap_inject_enable_disable_all_interfaces (0);
return err;
}
im->flags |= TAP_INJECT_F_CONFIG_ENABLE;
}
else
{
/* Disable */
tap_inject_enable_disable_all_interfaces (0);
im->flags &= ~TAP_INJECT_F_CONFIG_ENABLE;
}
return 0;
}
VLIB_CLI_COMMAND (tap_inject_enable_cmd, static) = {
.path = "enable tap-inject",
.short_help = "enable tap-inject",
.function = tap_inject_cli,
.function_arg = 1,
};
VLIB_CLI_COMMAND (tap_inject_disable_cmd, static) = {
.path = "disable tap-inject",
.short_help = "disable tap-inject",
.function = tap_inject_cli,
.function_arg = 0,
};
static clib_error_t *
show_tap_inject (vlib_main_t * vm, unformat_input_t * input,
vlib_cli_command_t * cmd)
{
vnet_main_t * vnet_main = vnet_get_main ();
tap_inject_main_t * im = tap_inject_get_main ();
u32 k, v;
if (tap_inject_is_config_disabled ())
{
vlib_cli_output (vm, "tap-inject is disabled in config.\n");
return 0;
}
if (!tap_inject_is_enabled ())
{
vlib_cli_output (vm, "tap-inject is not enabled.\n");
return 0;
}
hash_foreach (k, v, im->tap_if_index_to_sw_if_index, {
vlib_cli_output (vm, "%U -> %U",
format_vnet_sw_interface_name, vnet_main,
vnet_get_sw_interface (vnet_main, v),
format_tap_inject_tap_name, k);
});
return 0;
}
VLIB_CLI_COMMAND (show_tap_inject_cmd, static) = {
.path = "show tap-inject",
.short_help = "show tap-inject",
.function = show_tap_inject,
};
static clib_error_t *
tap_inject_config (vlib_main_t * vm, unformat_input_t * input)
{
tap_inject_main_t * im = tap_inject_get_main ();
while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
{
if (unformat (input, "enable"))
im->flags |= TAP_INJECT_F_CONFIG_ENABLE;
else if (unformat (input, "disable"))
im->flags |= TAP_INJECT_F_CONFIG_DISABLE;
else if (unformat (input, "netlink-only"))
im->flags |= TAP_INJECT_F_CONFIG_NETLINK;
else
return clib_error_return (0, "syntax error `%U'",
format_unformat_error, input);
}
if (tap_inject_is_config_enabled () && tap_inject_is_config_disabled ())
return clib_error_return (0,
"tap-inject cannot be both enabled and disabled.");
return 0;
}
VLIB_CONFIG_FUNCTION (tap_inject_config, "tap-inject");
|