blob: 735f960cbd30b98c6ab7875413b3791120b07209 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * srp_interface.c: srp interfaces
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
40#include <vnet/vnet.h>
41#include <vnet/pg/pg.h>
42#include <vnet/srp/srp.h>
43
Neale Rannsb80c5362016-10-08 13:03:40 +010044static u8*
45srp_build_rewrite (vnet_main_t * vnm,
46 u32 sw_if_index,
47 vnet_link_t link_type,
48 const void * dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -070049{
50 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
51 srp_main_t * sm = &srp_main;
Neale Rannsb80c5362016-10-08 13:03:40 +010052 srp_and_ethernet_header_t * h;
53 u8* rewrite = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 u16 type;
55 uword n_bytes = sizeof (h[0]);
56
Neale Rannsb80c5362016-10-08 13:03:40 +010057 switch (link_type) {
58#define _(a,b) case VNET_LINK_##a: type = ETHERNET_TYPE_##b; break
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 _ (IP4, IP4);
60 _ (IP6, IP6);
Neale Ranns0f26c5a2017-03-01 15:12:11 -080061 _ (MPLS, MPLS);
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 _ (ARP, ARP);
63#undef _
64 default:
Neale Rannsb80c5362016-10-08 13:03:40 +010065 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 }
67
Neale Rannsb80c5362016-10-08 13:03:40 +010068 vec_validate(rewrite, n_bytes-1);
69 h = (srp_and_ethernet_header_t *)rewrite;
70
Damjan Marionf1213b82016-03-13 02:22:06 +010071 clib_memcpy (h->ethernet.src_address, hw->hw_address, sizeof (h->ethernet.src_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 if (dst_address)
Damjan Marionf1213b82016-03-13 02:22:06 +010073 clib_memcpy (h->ethernet.dst_address, dst_address, sizeof (h->ethernet.dst_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 else
75 memset (h->ethernet.dst_address, ~0, sizeof (h->ethernet.dst_address)); /* broadcast */
76
77 h->ethernet.type = clib_host_to_net_u16 (type);
78
79 h->srp.as_u16 = 0;
80 h->srp.mode = SRP_MODE_data;
81 h->srp.ttl = sm->default_data_ttl;
82 srp_header_compute_parity (&h->srp);
83
Neale Rannsb80c5362016-10-08 13:03:40 +010084 return (rewrite);
Ed Warnickecb9cada2015-12-08 15:45:58 -070085}
86
87static void srp_register_interface_helper (u32 * hw_if_indices_by_side, u32 redistribute);
88
89void serialize_srp_main (serialize_main_t * m, va_list * va)
90{
91 srp_main_t * sm = &srp_main;
92 srp_interface_t * si;
93
94 serialize_integer (m, pool_elts (sm->interface_pool), sizeof (u32));
95 pool_foreach (si, sm->interface_pool, ({
96 serialize_integer (m, si->rings[SRP_RING_OUTER].hw_if_index, sizeof (u32));
97 serialize_integer (m, si->rings[SRP_RING_INNER].hw_if_index, sizeof (u32));
98 }));
99}
100
101void unserialize_srp_main (serialize_main_t * m, va_list * va)
102{
103 u32 i, n_ifs, hw_if_indices[SRP_N_RING];
104
105 unserialize_integer (m, &n_ifs, sizeof (u32));
106 for (i = 0; i < n_ifs; i++)
107 {
108 unserialize_integer (m, &hw_if_indices[SRP_RING_OUTER], sizeof (u32));
109 unserialize_integer (m, &hw_if_indices[SRP_RING_INNER], sizeof (u32));
110 srp_register_interface_helper (hw_if_indices, /* redistribute */ 0);
111 }
112}
113
114static void serialize_srp_register_interface_msg (serialize_main_t * m, va_list * va)
115{
116 u32 * hw_if_indices = va_arg (*va, u32 *);
117 serialize_integer (m, hw_if_indices[SRP_SIDE_A], sizeof (hw_if_indices[SRP_SIDE_A]));
118 serialize_integer (m, hw_if_indices[SRP_SIDE_B], sizeof (hw_if_indices[SRP_SIDE_B]));
119}
120
121static void unserialize_srp_register_interface_msg (serialize_main_t * m, va_list * va)
122{
123 CLIB_UNUSED (mc_main_t * mcm) = va_arg (*va, mc_main_t *);
124 u32 hw_if_indices[SRP_N_SIDE];
125 srp_main_t * sm = &srp_main;
126 uword * p;
127
128 unserialize_integer (m, &hw_if_indices[SRP_SIDE_A], sizeof (hw_if_indices[SRP_SIDE_A]));
129 unserialize_integer (m, &hw_if_indices[SRP_SIDE_B], sizeof (hw_if_indices[SRP_SIDE_B]));
130
131 p = hash_get (sm->srp_register_interface_waiting_process_pool_index_by_hw_if_index,
132 hw_if_indices[0]);
133 if (p)
134 {
135 vlib_one_time_waiting_process_t * wp = pool_elt_at_index (sm->srp_register_interface_waiting_process_pool, p[0]);
136 vlib_signal_one_time_waiting_process (mcm->vlib_main, wp);
137 pool_put (sm->srp_register_interface_waiting_process_pool, wp);
138 hash_unset (sm->srp_register_interface_waiting_process_pool_index_by_hw_if_index,
139 hw_if_indices[0]);
140 }
141 else
142 srp_register_interface_helper (hw_if_indices, /* redistribute */ 0);
143}
144
145MC_SERIALIZE_MSG (srp_register_interface_msg, static) = {
146 .name = "vnet_srp_register_interface",
147 .serialize = serialize_srp_register_interface_msg,
148 .unserialize = unserialize_srp_register_interface_msg,
149};
150
151static void srp_register_interface_helper (u32 * hw_if_indices_by_side, u32 redistribute)
152{
153 vnet_main_t * vnm = vnet_get_main();
154 srp_main_t * sm = &srp_main;
155 vlib_main_t * vm = sm->vlib_main;
156 srp_interface_t * si;
157 vnet_hw_interface_t * hws[SRP_N_RING];
158 uword s, * p;
159
160 if (vm->mc_main && redistribute)
161 {
162 vlib_one_time_waiting_process_t * wp;
163 mc_serialize (vm->mc_main, &srp_register_interface_msg, hw_if_indices_by_side);
164 pool_get (sm->srp_register_interface_waiting_process_pool, wp);
165 hash_set (sm->srp_register_interface_waiting_process_pool_index_by_hw_if_index,
166 hw_if_indices_by_side[0],
167 wp - sm->srp_register_interface_waiting_process_pool);
168 vlib_current_process_wait_for_one_time_event (vm, wp);
169 }
170
171 /* Check if interface has already been registered. */
172 p = hash_get (sm->interface_index_by_hw_if_index, hw_if_indices_by_side[0]);
173 if (p)
174 {
175 si = pool_elt_at_index (sm->interface_pool, p[0]);
176 }
177 else
178 {
179 pool_get (sm->interface_pool, si);
180 memset (si, 0, sizeof (si[0]));
181 }
182 for (s = 0; s < SRP_N_SIDE; s++)
183 {
184 hws[s] = vnet_get_hw_interface (vnm, hw_if_indices_by_side[s]);
185 si->rings[s].ring = s;
186 si->rings[s].hw_if_index = hw_if_indices_by_side[s];
187 si->rings[s].sw_if_index = hws[s]->sw_if_index;
188 hash_set (sm->interface_index_by_hw_if_index, hw_if_indices_by_side[s], si - sm->interface_pool);
189 }
190
191 /* Inherit MAC address from outer ring. */
Damjan Marionf1213b82016-03-13 02:22:06 +0100192 clib_memcpy (si->my_address, hws[SRP_RING_OUTER]->hw_address,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 vec_len (hws[SRP_RING_OUTER]->hw_address));
194
195 /* Default time to wait to restore signal. */
196 si->config.wait_to_restore_idle_delay = 60;
197 si->config.ips_tx_interval = 1;
198}
199
200void srp_register_interface (u32 * hw_if_indices_by_side)
201{
202 srp_register_interface_helper (hw_if_indices_by_side, /* redistribute */ 1);
203}
204
205void srp_interface_set_hw_wrap_function (u32 hw_if_index, srp_hw_wrap_function_t * f)
206{
207 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
208 si->hw_wrap_function = f;
209}
210
211void srp_interface_set_hw_enable_function (u32 hw_if_index, srp_hw_enable_function_t * f)
212{
213 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
214 si->hw_enable_function = f;
215}
216
217void srp_interface_enable_ips (u32 hw_if_index)
218{
219 srp_main_t * sm = &srp_main;
220 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
221
222 si->ips_process_enable = 1;
223
224 vlib_node_set_state (sm->vlib_main, srp_ips_process_node.index, VLIB_NODE_STATE_POLLING);
225}
226
227static uword
228srp_is_valid_class_for_interface (vnet_main_t * vnm, u32 hw_if_index, u32 hw_class_index)
229{
230 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
231
232 if (! si)
233 return 0;
234
235 /* Both sides must be admin down. */
236 if (vnet_sw_interface_is_admin_up (vnm, si->rings[SRP_RING_OUTER].sw_if_index))
237 return 0;
238 if (vnet_sw_interface_is_admin_up (vnm, si->rings[SRP_RING_INNER].sw_if_index))
239 return 0;
240
241 return 1;
242}
243
244static void
245srp_interface_hw_class_change (vnet_main_t * vnm, u32 hw_if_index,
246 u32 old_hw_class_index, u32 new_hw_class_index)
247{
248 srp_main_t * sm = &srp_main;
249 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
250 vnet_hw_interface_t * hi;
251 vnet_device_class_t * dc;
252 u32 r, to_srp;
253
254 if (!si) {
255 clib_warning ("srp interface no set si = 0");
256 return;
257 }
258
259 to_srp = new_hw_class_index == srp_hw_interface_class.index;
260
261 /* Changing class on either outer or inner rings implies changing the class
262 of the other. */
263 for (r = 0; r < SRP_N_RING; r++)
264 {
265 srp_interface_ring_t * ir = &si->rings[r];
266
267 hi = vnet_get_hw_interface (vnm, ir->hw_if_index);
268 dc = vnet_get_device_class (vnm, hi->dev_class_index);
269
270 /* hw_if_index itself will be handled by caller. */
271 if (ir->hw_if_index != hw_if_index)
272 {
273 vnet_hw_interface_init_for_class (vnm, ir->hw_if_index,
274 new_hw_class_index,
275 to_srp ? si - sm->interface_pool : ~0);
276
277 if (dc->hw_class_change)
278 dc->hw_class_change (vnm, ir->hw_if_index, new_hw_class_index);
279 }
280 else
281 hi->hw_instance = to_srp ? si - sm->interface_pool : ~0;
282 }
283
284 if (si->hw_enable_function)
285 si->hw_enable_function (si, /* enable */ to_srp);
286}
287
288VNET_HW_INTERFACE_CLASS (srp_hw_interface_class) = {
289 .name = "SRP",
290 .format_address = format_ethernet_address,
291 .format_header = format_srp_header_with_length,
292 .format_device = format_srp_device,
293 .unformat_hw_address = unformat_ethernet_address,
294 .unformat_header = unformat_srp_header,
Neale Rannsb80c5362016-10-08 13:03:40 +0100295 .build_rewrite = srp_build_rewrite,
296 .update_adjacency = ethernet_update_adjacency,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 .is_valid_class_for_interface = srp_is_valid_class_for_interface,
298 .hw_class_change = srp_interface_hw_class_change,
299};
300
301static void serialize_srp_interface_config_msg (serialize_main_t * m, va_list * va)
302{
303 srp_interface_t * si = va_arg (*va, srp_interface_t *);
304 srp_main_t * sm = &srp_main;
305
306 ASSERT (! pool_is_free (sm->interface_pool, si));
307 serialize_integer (m, si - sm->interface_pool, sizeof (u32));
308 serialize (m, serialize_f64, si->config.wait_to_restore_idle_delay);
309 serialize (m, serialize_f64, si->config.ips_tx_interval);
310}
311
312static void unserialize_srp_interface_config_msg (serialize_main_t * m, va_list * va)
313{
314 CLIB_UNUSED (mc_main_t * mcm) = va_arg (*va, mc_main_t *);
315 srp_main_t * sm = &srp_main;
316 srp_interface_t * si;
317 u32 si_index;
318
319 unserialize_integer (m, &si_index, sizeof (u32));
320 si = pool_elt_at_index (sm->interface_pool, si_index);
321 unserialize (m, unserialize_f64, &si->config.wait_to_restore_idle_delay);
322 unserialize (m, unserialize_f64, &si->config.ips_tx_interval);
323}
324
325MC_SERIALIZE_MSG (srp_interface_config_msg, static) = {
326 .name = "vnet_srp_interface_config",
327 .serialize = serialize_srp_interface_config_msg,
328 .unserialize = unserialize_srp_interface_config_msg,
329};
330
331void srp_interface_get_interface_config (u32 hw_if_index, srp_interface_config_t * c)
332{
333 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
334 ASSERT (si != 0);
335 c[0] = si->config;
336}
337
338void srp_interface_set_interface_config (u32 hw_if_index, srp_interface_config_t * c)
339{
340 srp_main_t * sm = &srp_main;
341 vlib_main_t * vm = sm->vlib_main;
342 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
343 ASSERT (si != 0);
344 if (memcmp (&si->config, &c[0], sizeof (c[0])))
345 {
346 si->config = c[0];
347 if (vm->mc_main)
348 mc_serialize (vm->mc_main, &srp_interface_config_msg, si);
349 }
350}
351
352#if DEBUG > 0
353
354#define VNET_SIMULATED_SRP_TX_NEXT_SRP_INPUT VNET_INTERFACE_TX_N_NEXT
355
356/* Echo packets back to srp input. */
357static uword
358simulated_srp_interface_tx (vlib_main_t * vm,
359 vlib_node_runtime_t * node,
360 vlib_frame_t * frame)
361{
362 u32 n_left_from, n_left_to_next, n_copy, * from, * to_next;
363 u32 next_index = VNET_SIMULATED_SRP_TX_NEXT_SRP_INPUT;
364 u32 i;
365 vlib_buffer_t * b;
366
367 n_left_from = frame->n_vectors;
368 from = vlib_frame_args (frame);
369
370 while (n_left_from > 0)
371 {
372 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
373
374 n_copy = clib_min (n_left_from, n_left_to_next);
375
Damjan Marionf1213b82016-03-13 02:22:06 +0100376 clib_memcpy (to_next, from, n_copy * sizeof (from[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377 n_left_to_next -= n_copy;
378 n_left_from -= n_copy;
379 for (i = 0; i < n_copy; i++)
380 {
381 b = vlib_get_buffer (vm, from[i]);
382 /* TX interface will be fake eth; copy to RX for benefit of srp-input. */
383 b->sw_if_index[VLIB_RX] = b->sw_if_index[VLIB_TX];
384 }
385
386 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
387 }
388
389 return n_left_from;
390}
391
392static u8 * format_simulated_srp_name (u8 * s, va_list * args)
393{
394 u32 dev_instance = va_arg (*args, u32);
395 return format (s, "fake-srp%d", dev_instance);
396}
397
398VNET_DEVICE_CLASS (srp_simulated_device_class,static) = {
399 .name = "Simulated srp",
400 .format_device_name = format_simulated_srp_name,
401 .tx_function = simulated_srp_interface_tx,
402};
403
404static clib_error_t *
405create_simulated_srp_interfaces (vlib_main_t * vm,
406 unformat_input_t * input,
407 vlib_cli_command_t * cmd)
408{
409 vnet_main_t * vnm = vnet_get_main();
410 u8 address[6];
411 u32 hw_if_index;
412 vnet_hw_interface_t * hi;
413 static u32 instance;
414
415 if (! unformat_user (input, unformat_ethernet_address, &address))
416 {
417 memset (address, 0, sizeof (address));
418 address[0] = 0xde;
419 address[1] = 0xad;
420 address[5] = instance;
421 }
422
423 hw_if_index = vnet_register_interface (vnm,
424 srp_simulated_device_class.index,
425 instance++,
426 srp_hw_interface_class.index, 0);
427
428 hi = vnet_get_hw_interface (vnm, hw_if_index);
429
430 srp_setup_node (vm, hi->output_node_index);
431
432 hi->min_packet_bytes = 40 + 16;
433
434 /* Standard default ethernet MTU. */
Ole Troand7231612018-06-07 10:17:57 +0200435 vnet_sw_interface_set_mtu (vnm, sw_if_index, 1500);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436
437 vec_free (hi->hw_address);
438 vec_add (hi->hw_address, address, sizeof (address));
439
440 {
441 uword slot;
442
443 slot = vlib_node_add_named_next_with_slot
444 (vm, hi->tx_node_index,
445 "srp-input",
446 VNET_SIMULATED_SRP_TX_NEXT_SRP_INPUT);
447 ASSERT (slot == VNET_SIMULATED_SRP_TX_NEXT_SRP_INPUT);
448 }
449
450 return /* no error */ 0;
451}
452
453static VLIB_CLI_COMMAND (create_simulated_srp_interface_command) = {
454 .path = "srp create-interfaces",
455 .short_help = "Create simulated srp interface",
456 .function = create_simulated_srp_interfaces,
457};
458#endif