2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
20 #include <vnet/llc/llc.h>
21 #include <vnet/snap/snap.h>
22 #include <vnet/bonding/node.h>
24 #ifndef CLIB_MARCH_VARIANT
25 bond_main_t bond_main;
26 #endif /* CLIB_MARCH_VARIANT */
28 #define foreach_bond_input_error \
30 _(IF_DOWN, "interface down") \
31 _(PASS_THRU, "pass through (CDP, LLDP, slow protocols)")
35 #define _(f,s) BOND_INPUT_ERROR_##f,
36 foreach_bond_input_error
41 static char *bond_input_error_strings[] = {
43 foreach_bond_input_error
48 format_bond_input_trace (u8 * s, va_list * args)
50 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
51 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
52 bond_packet_trace_t *t = va_arg (*args, bond_packet_trace_t *);
54 s = format (s, "src %U, dst %U, %U -> %U",
55 format_ethernet_address, t->ethernet.src_address,
56 format_ethernet_address, t->ethernet.dst_address,
57 format_vnet_sw_if_index_name, vnet_get_main (),
59 format_vnet_sw_if_index_name, vnet_get_main (),
71 static_always_inline u8
72 packet_is_cdp (ethernet_header_t * eth)
77 llc = (llc_header_t *) (eth + 1);
78 snap = (snap_header_t *) (llc + 1);
80 return ((eth->type == htons (ETHERNET_TYPE_CDP)) ||
81 ((llc->src_sap == 0xAA) && (llc->control == 0x03) &&
82 (snap->protocol == htons (0x2000)) &&
83 (snap->oui[0] == 0) && (snap->oui[1] == 0) &&
84 (snap->oui[2] == 0x0C)));
88 bond_sw_if_idx_rewrite (vlib_main_t * vm, vlib_node_runtime_t * node,
89 vlib_buffer_t * b, u32 bond_sw_if_index,
90 u32 * n_rx_packets, u32 * n_rx_bytes)
92 u16 *ethertype_p, ethertype;
93 ethernet_vlan_header_t *vlan;
94 ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b);
97 *n_rx_bytes += b->current_length;
98 ethertype = clib_mem_unaligned (ð->type, u16);
99 if (!ethernet_frame_is_tagged (ntohs (ethertype)))
101 // Let some layer2 packets pass through.
102 if (PREDICT_TRUE ((ethertype != htons (ETHERNET_TYPE_SLOW_PROTOCOLS))
103 && !packet_is_cdp (eth)
104 && (ethertype != htons (ETHERNET_TYPE_802_1_LLDP))))
106 /* Change the physical interface to bond interface */
107 vnet_buffer (b)->sw_if_index[VLIB_RX] = bond_sw_if_index;
113 vlan = (void *) (eth + 1);
114 ethertype_p = &vlan->type;
115 ethertype = clib_mem_unaligned (ethertype_p, u16);
116 if (ethertype == ntohs (ETHERNET_TYPE_VLAN))
119 ethertype_p = &vlan->type;
121 ethertype = clib_mem_unaligned (ethertype_p, u16);
122 if (PREDICT_TRUE ((ethertype != htons (ETHERNET_TYPE_SLOW_PROTOCOLS))
123 && (ethertype != htons (ETHERNET_TYPE_CDP))
124 && (ethertype != htons (ETHERNET_TYPE_802_1_LLDP))))
126 /* Change the physical interface to bond interface */
127 vnet_buffer (b)->sw_if_index[VLIB_RX] = bond_sw_if_index;
132 vlib_error_count (vm, node->node_index, BOND_INPUT_ERROR_PASS_THRU, 1);
137 bond_update_next (vlib_main_t * vm, vlib_node_runtime_t * node,
138 u32 * last_slave_sw_if_index, u32 slave_sw_if_index,
139 u32 * bond_sw_if_index, vlib_buffer_t * b,
140 u32 * next_index, vlib_error_t * error)
145 if (PREDICT_TRUE (*last_slave_sw_if_index == slave_sw_if_index))
148 *last_slave_sw_if_index = slave_sw_if_index;
149 *next_index = BOND_INPUT_NEXT_DROP;
151 sif = bond_get_slave_by_sw_if_index (slave_sw_if_index);
154 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
157 ASSERT (vec_len (bif->slaves));
159 if (PREDICT_TRUE (bif->admin_up == 0))
161 *bond_sw_if_index = slave_sw_if_index;
162 *error = node->errors[BOND_INPUT_ERROR_IF_DOWN];
165 *bond_sw_if_index = bif->sw_if_index;
167 vnet_feature_next (next_index, b);
170 VLIB_NODE_FN (bond_input_node) (vlib_main_t * vm,
171 vlib_node_runtime_t * node,
172 vlib_frame_t * frame)
174 u16 thread_index = vm->thread_index;
176 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
177 u32 sw_if_indices[VLIB_FRAME_SIZE], *sw_if_index;
178 u16 nexts[VLIB_FRAME_SIZE], *next;
179 u32 last_slave_sw_if_index = ~0;
180 u32 bond_sw_if_index = 0;
181 vlib_error_t error = 0;
183 u32 n_rx_bytes = 0, n_rx_packets = 0;
185 /* Vector of buffer / pkt indices we're supposed to process */
186 from = vlib_frame_vector_args (frame);
188 /* Number of buffers / pkts */
189 n_left = frame->n_vectors;
191 vlib_get_buffers (vm, from, bufs, n_left);
195 sw_if_index = sw_if_indices;
200 /* Prefetch next iteration */
201 if (PREDICT_TRUE (n_left >= 16))
203 vlib_prefetch_buffer_data (b[8], LOAD);
204 vlib_prefetch_buffer_data (b[9], LOAD);
205 vlib_prefetch_buffer_data (b[10], LOAD);
206 vlib_prefetch_buffer_data (b[11], LOAD);
208 vlib_prefetch_buffer_header (b[12], LOAD);
209 vlib_prefetch_buffer_header (b[13], LOAD);
210 vlib_prefetch_buffer_header (b[14], LOAD);
211 vlib_prefetch_buffer_header (b[15], LOAD);
214 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
215 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
216 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
217 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
219 x |= sw_if_index[0] ^ last_slave_sw_if_index;
220 x |= sw_if_index[1] ^ last_slave_sw_if_index;
221 x |= sw_if_index[2] ^ last_slave_sw_if_index;
222 x |= sw_if_index[3] ^ last_slave_sw_if_index;
224 if (PREDICT_TRUE (x == 0))
226 next[0] = next[1] = next[2] = next[3] = next_index;
227 if (next_index == BOND_INPUT_NEXT_DROP)
236 bond_sw_if_idx_rewrite (vm, node, b[0], bond_sw_if_index,
237 &n_rx_packets, &n_rx_bytes);
238 bond_sw_if_idx_rewrite (vm, node, b[1], bond_sw_if_index,
239 &n_rx_packets, &n_rx_bytes);
240 bond_sw_if_idx_rewrite (vm, node, b[2], bond_sw_if_index,
241 &n_rx_packets, &n_rx_bytes);
242 bond_sw_if_idx_rewrite (vm, node, b[3], bond_sw_if_index,
243 &n_rx_packets, &n_rx_bytes);
248 bond_update_next (vm, node, &last_slave_sw_if_index, sw_if_index[0],
249 &bond_sw_if_index, b[0], &next_index, &error);
250 next[0] = next_index;
251 if (next_index == BOND_INPUT_NEXT_DROP)
254 bond_sw_if_idx_rewrite (vm, node, b[0], bond_sw_if_index,
255 &n_rx_packets, &n_rx_bytes);
257 bond_update_next (vm, node, &last_slave_sw_if_index, sw_if_index[1],
258 &bond_sw_if_index, b[1], &next_index, &error);
259 next[1] = next_index;
260 if (next_index == BOND_INPUT_NEXT_DROP)
263 bond_sw_if_idx_rewrite (vm, node, b[1], bond_sw_if_index,
264 &n_rx_packets, &n_rx_bytes);
266 bond_update_next (vm, node, &last_slave_sw_if_index, sw_if_index[2],
267 &bond_sw_if_index, b[2], &next_index, &error);
268 next[2] = next_index;
269 if (next_index == BOND_INPUT_NEXT_DROP)
272 bond_sw_if_idx_rewrite (vm, node, b[2], bond_sw_if_index,
273 &n_rx_packets, &n_rx_bytes);
275 bond_update_next (vm, node, &last_slave_sw_if_index, sw_if_index[3],
276 &bond_sw_if_index, b[3], &next_index, &error);
277 next[3] = next_index;
278 if (next_index == BOND_INPUT_NEXT_DROP)
281 bond_sw_if_idx_rewrite (vm, node, b[3], bond_sw_if_index,
282 &n_rx_packets, &n_rx_bytes);
285 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
286 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
287 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
288 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
299 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
300 bond_update_next (vm, node, &last_slave_sw_if_index, sw_if_index[0],
301 &bond_sw_if_index, b[0], &next_index, &error);
302 next[0] = next_index;
303 if (next_index == BOND_INPUT_NEXT_DROP)
306 bond_sw_if_idx_rewrite (vm, node, b[0], bond_sw_if_index,
307 &n_rx_packets, &n_rx_bytes);
309 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
318 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
320 n_left = frame->n_vectors; /* number of packets to process */
322 sw_if_index = sw_if_indices;
323 bond_packet_trace_t *t0;
327 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
329 t0 = vlib_add_trace (vm, node, b[0], sizeof (*t0));
330 t0->sw_if_index = sw_if_index[0];
331 clib_memcpy_fast (&t0->ethernet, vlib_buffer_get_current (b[0]),
332 sizeof (ethernet_header_t));
333 t0->bond_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
342 /* increase rx counters */
343 vlib_increment_combined_counter
344 (vnet_main.interface_main.combined_sw_if_counters +
345 VNET_INTERFACE_COUNTER_RX, thread_index, bond_sw_if_index, n_rx_packets,
348 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
349 vlib_node_increment_counter (vm, bond_input_node.index,
350 BOND_INPUT_ERROR_NONE, frame->n_vectors);
352 return frame->n_vectors;
355 static clib_error_t *
356 bond_input_init (vlib_main_t * vm)
362 VLIB_REGISTER_NODE (bond_input_node) = {
363 .name = "bond-input",
364 .vector_size = sizeof (u32),
365 .format_buffer = format_ethernet_header_with_length,
366 .format_trace = format_bond_input_trace,
367 .type = VLIB_NODE_TYPE_INTERNAL,
368 .n_errors = BOND_INPUT_N_ERROR,
369 .error_strings = bond_input_error_strings,
370 .n_next_nodes = BOND_INPUT_N_NEXT,
373 [BOND_INPUT_NEXT_DROP] = "error-drop"
377 VLIB_INIT_FUNCTION (bond_input_init);
379 VNET_FEATURE_INIT (bond_input, static) =
381 .arc_name = "device-input",
382 .node_name = "bond-input",
383 .runs_before = VNET_FEATURES ("ethernet-input"),
387 static clib_error_t *
388 bond_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
390 bond_main_t *bm = &bond_main;
392 vlib_main_t *vm = bm->vlib_main;
394 sif = bond_get_slave_by_sw_if_index (sw_if_index);
397 if (sif->lacp_enabled)
400 /* port_enabled is both admin up and hw link up */
401 sif->port_enabled = ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) &&
402 vnet_sw_interface_is_link_up (vnm, sw_if_index));
403 if (sif->port_enabled == 0)
404 bond_disable_collecting_distributing (vm, sif);
406 bond_enable_collecting_distributing (vm, sif);
412 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bond_sw_interface_up_down);
414 static clib_error_t *
415 bond_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
417 bond_main_t *bm = &bond_main;
419 vnet_sw_interface_t *sw;
420 vlib_main_t *vm = bm->vlib_main;
422 sw = vnet_get_hw_sw_interface (vnm, hw_if_index);
423 sif = bond_get_slave_by_sw_if_index (sw->sw_if_index);
426 if (sif->lacp_enabled)
429 /* port_enabled is both admin up and hw link up */
430 sif->port_enabled = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) &&
431 vnet_sw_interface_is_admin_up (vnm,
433 if (sif->port_enabled == 0)
434 bond_disable_collecting_distributing (vm, sif);
436 bond_enable_collecting_distributing (vm, sif);
442 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bond_hw_interface_up_down);
445 * fd.io coding-style-patch-verification: ON
448 * eval: (c-set-style "gnu")