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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * srp_interface.c: srp interfaces
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/vnet.h>
41 #include <vnet/pg/pg.h>
42 #include <vnet/srp/srp.h>
45 srp_build_rewrite (vnet_main_t * vnm,
47 vnet_link_t link_type,
48 const void * dst_address)
50 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
51 srp_main_t * sm = &srp_main;
52 srp_and_ethernet_header_t * h;
55 uword n_bytes = sizeof (h[0]);
58 #define _(a,b) case VNET_LINK_##a: type = ETHERNET_TYPE_##b; break
68 vec_validate(rewrite, n_bytes-1);
69 h = (srp_and_ethernet_header_t *)rewrite;
71 clib_memcpy (h->ethernet.src_address, hw->hw_address, sizeof (h->ethernet.src_address));
73 clib_memcpy (h->ethernet.dst_address, dst_address, sizeof (h->ethernet.dst_address));
75 clib_memset (h->ethernet.dst_address, ~0, sizeof (h->ethernet.dst_address)); /* broadcast */
77 h->ethernet.type = clib_host_to_net_u16 (type);
80 h->srp.mode = SRP_MODE_data;
81 h->srp.ttl = sm->default_data_ttl;
82 srp_header_compute_parity (&h->srp);
87 static void srp_register_interface_helper (u32 * hw_if_indices_by_side, u32 redistribute);
89 void serialize_srp_main (serialize_main_t * m, va_list * va)
91 srp_main_t * sm = &srp_main;
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));
101 void unserialize_srp_main (serialize_main_t * m, va_list * va)
103 u32 i, n_ifs, hw_if_indices[SRP_N_RING];
105 unserialize_integer (m, &n_ifs, sizeof (u32));
106 for (i = 0; i < n_ifs; i++)
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);
114 static void srp_register_interface_helper (u32 * hw_if_indices_by_side, u32 redistribute)
116 vnet_main_t * vnm = vnet_get_main();
117 srp_main_t * sm = &srp_main;
118 srp_interface_t * si;
119 vnet_hw_interface_t * hws[SRP_N_RING];
122 /* Check if interface has already been registered. */
123 p = hash_get (sm->interface_index_by_hw_if_index, hw_if_indices_by_side[0]);
126 si = pool_elt_at_index (sm->interface_pool, p[0]);
130 pool_get (sm->interface_pool, si);
131 clib_memset (si, 0, sizeof (si[0]));
133 for (s = 0; s < SRP_N_SIDE; s++)
135 hws[s] = vnet_get_hw_interface (vnm, hw_if_indices_by_side[s]);
136 si->rings[s].ring = s;
137 si->rings[s].hw_if_index = hw_if_indices_by_side[s];
138 si->rings[s].sw_if_index = hws[s]->sw_if_index;
139 hash_set (sm->interface_index_by_hw_if_index, hw_if_indices_by_side[s], si - sm->interface_pool);
142 /* Inherit MAC address from outer ring. */
143 clib_memcpy (si->my_address, hws[SRP_RING_OUTER]->hw_address,
144 vec_len (hws[SRP_RING_OUTER]->hw_address));
146 /* Default time to wait to restore signal. */
147 si->config.wait_to_restore_idle_delay = 60;
148 si->config.ips_tx_interval = 1;
151 void srp_register_interface (u32 * hw_if_indices_by_side)
153 srp_register_interface_helper (hw_if_indices_by_side, /* redistribute */ 1);
156 void srp_interface_set_hw_wrap_function (u32 hw_if_index, srp_hw_wrap_function_t * f)
158 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
159 si->hw_wrap_function = f;
162 void srp_interface_set_hw_enable_function (u32 hw_if_index, srp_hw_enable_function_t * f)
164 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
165 si->hw_enable_function = f;
168 void srp_interface_enable_ips (u32 hw_if_index)
170 srp_main_t * sm = &srp_main;
171 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
173 si->ips_process_enable = 1;
175 vlib_node_set_state (sm->vlib_main, srp_ips_process_node.index, VLIB_NODE_STATE_POLLING);
179 srp_is_valid_class_for_interface (vnet_main_t * vnm, u32 hw_if_index, u32 hw_class_index)
181 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
186 /* Both sides must be admin down. */
187 if (vnet_sw_interface_is_admin_up (vnm, si->rings[SRP_RING_OUTER].sw_if_index))
189 if (vnet_sw_interface_is_admin_up (vnm, si->rings[SRP_RING_INNER].sw_if_index))
196 srp_interface_hw_class_change (vnet_main_t * vnm, u32 hw_if_index,
197 u32 old_hw_class_index, u32 new_hw_class_index)
199 srp_main_t * sm = &srp_main;
200 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
201 vnet_hw_interface_t * hi;
202 vnet_device_class_t * dc;
206 clib_warning ("srp interface no set si = 0");
210 to_srp = new_hw_class_index == srp_hw_interface_class.index;
212 /* Changing class on either outer or inner rings implies changing the class
214 for (r = 0; r < SRP_N_RING; r++)
216 srp_interface_ring_t * ir = &si->rings[r];
218 hi = vnet_get_hw_interface (vnm, ir->hw_if_index);
219 dc = vnet_get_device_class (vnm, hi->dev_class_index);
221 /* hw_if_index itself will be handled by caller. */
222 if (ir->hw_if_index != hw_if_index)
224 vnet_hw_interface_init_for_class (vnm, ir->hw_if_index,
226 to_srp ? si - sm->interface_pool : ~0);
228 if (dc->hw_class_change)
229 dc->hw_class_change (vnm, ir->hw_if_index, new_hw_class_index);
232 hi->hw_instance = to_srp ? si - sm->interface_pool : ~0;
235 if (si->hw_enable_function)
236 si->hw_enable_function (si, /* enable */ to_srp);
239 VNET_HW_INTERFACE_CLASS (srp_hw_interface_class) = {
241 .format_address = format_ethernet_address,
242 .format_header = format_srp_header_with_length,
243 .format_device = format_srp_device,
244 .unformat_hw_address = unformat_ethernet_address,
245 .unformat_header = unformat_srp_header,
246 .build_rewrite = srp_build_rewrite,
247 .update_adjacency = ethernet_update_adjacency,
248 .is_valid_class_for_interface = srp_is_valid_class_for_interface,
249 .hw_class_change = srp_interface_hw_class_change,
252 void srp_interface_get_interface_config (u32 hw_if_index, srp_interface_config_t * c)
254 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
259 void srp_interface_set_interface_config (u32 hw_if_index, srp_interface_config_t * c)
261 srp_interface_t * si = srp_get_interface_from_vnet_hw_interface (hw_if_index);
263 if (memcmp (&si->config, &c[0], sizeof (c[0])))
271 #define VNET_SIMULATED_SRP_TX_NEXT_SRP_INPUT VNET_INTERFACE_TX_N_NEXT
272 /* Echo packets back to srp input. */
274 simulated_srp_interface_tx (vlib_main_t * vm,
275 vlib_node_runtime_t * node,
276 vlib_frame_t * frame)
278 u32 n_left_from, n_left_to_next, n_copy, * from, * to_next;
279 u32 next_index = VNET_SIMULATED_SRP_TX_NEXT_SRP_INPUT;
283 n_left_from = frame->n_vectors;
284 from = vlib_frame_vector_args (frame);
286 while (n_left_from > 0)
288 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
290 n_copy = clib_min (n_left_from, n_left_to_next);
292 clib_memcpy_fast (to_next, from, n_copy * sizeof (from[0]));
293 n_left_to_next -= n_copy;
294 n_left_from -= n_copy;
295 for (i = 0; i < n_copy; i++)
297 b = vlib_get_buffer (vm, from[i]);
298 /* TX interface will be fake eth; copy to RX for benefit of srp-input. */
299 b->sw_if_index[VLIB_RX] = b->sw_if_index[VLIB_TX];
302 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
308 static u8 * format_simulated_srp_name (u8 * s, va_list * args)
310 u32 dev_instance = va_arg (*args, u32);
311 return format (s, "fake-srp%d", dev_instance);
314 VNET_DEVICE_CLASS (srp_simulated_device_class,static) = {
315 .name = "Simulated srp",
316 .format_device_name = format_simulated_srp_name,
317 .tx_function = simulated_srp_interface_tx,
320 static clib_error_t *
321 create_simulated_srp_interfaces (vlib_main_t * vm,
322 unformat_input_t * input,
323 vlib_cli_command_t * cmd)
325 vnet_main_t * vnm = vnet_get_main();
328 vnet_hw_interface_t * hi;
331 if (! unformat_user (input, unformat_ethernet_address, &address))
333 clib_memset (address, 0, sizeof (address));
336 address[5] = instance;
339 hw_if_index = vnet_register_interface (vnm,
340 srp_simulated_device_class.index,
342 srp_hw_interface_class.index, 0);
344 hi = vnet_get_hw_interface (vnm, hw_if_index);
346 srp_setup_node (vm, hi->output_node_index);
348 hi->min_packet_bytes = 40 + 16;
350 /* Standard default ethernet MTU. */
351 vnet_sw_interface_set_mtu (vnm, sw_if_index, 1500);
353 vec_free (hi->hw_address);
354 vec_add (hi->hw_address, address, sizeof (address));
359 slot = vlib_node_add_named_next_with_slot
360 (vm, hi->tx_node_index,
362 VNET_SIMULATED_SRP_TX_NEXT_SRP_INPUT);
363 ASSERT (slot == VNET_SIMULATED_SRP_TX_NEXT_SRP_INPUT);
366 return /* no error */ 0;
369 static VLIB_CLI_COMMAND (create_simulated_srp_interface_command) = {
370 .path = "srp create-interfaces",
371 .short_help = "Create simulated srp interface",
372 .function = create_simulated_srp_interfaces,