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 bond_main_t bond_main;
26 #define foreach_bond_input_error \
28 _(IF_DOWN, "interface down") \
29 _(PASS_THRU, "pass through (CDP, LLDP, slow protocols)")
33 #define _(f,s) BOND_INPUT_ERROR_##f,
34 foreach_bond_input_error
39 static char *bond_input_error_strings[] = {
41 foreach_bond_input_error
46 format_bond_input_trace (u8 * s, va_list * args)
48 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
49 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
50 bond_packet_trace_t *t = va_arg (*args, bond_packet_trace_t *);
52 s = format (s, "src %U, dst %U, %U -> %U",
53 format_ethernet_address, t->ethernet.src_address,
54 format_ethernet_address, t->ethernet.dst_address,
55 format_vnet_sw_if_index_name, vnet_get_main (),
57 format_vnet_sw_if_index_name, vnet_get_main (),
69 static_always_inline u8
70 packet_is_cdp (ethernet_header_t * eth)
75 llc = (llc_header_t *) (eth + 1);
76 snap = (snap_header_t *) (llc + 1);
78 return ((eth->type == htons (ETHERNET_TYPE_CDP)) ||
79 ((llc->src_sap == 0xAA) && (llc->control == 0x03) &&
80 (snap->protocol == htons (0x2000)) &&
81 (snap->oui[0] == 0) && (snap->oui[1] == 0) &&
82 (snap->oui[2] == 0x0C)));
86 bond_sw_if_idx_rewrite (vlib_main_t * vm, vlib_node_runtime_t * node,
87 vlib_buffer_t * b, u32 bond_sw_if_index)
89 u16 *ethertype_p, ethertype;
90 ethernet_vlan_header_t *vlan;
91 ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b);
93 ethertype = clib_mem_unaligned (ð->type, u16);
94 if (!ethernet_frame_is_tagged (ntohs (ethertype)))
96 // Let some layer2 packets pass through.
97 if (PREDICT_TRUE ((ethertype != htons (ETHERNET_TYPE_SLOW_PROTOCOLS))
98 && !packet_is_cdp (eth)
99 && (ethertype != htons (ETHERNET_TYPE_802_1_LLDP))))
101 /* Change the physical interface to bond interface */
102 vnet_buffer (b)->sw_if_index[VLIB_RX] = bond_sw_if_index;
108 vlan = (void *) (eth + 1);
109 ethertype_p = &vlan->type;
110 ethertype = clib_mem_unaligned (ethertype_p, u16);
111 if (ethertype == ntohs (ETHERNET_TYPE_VLAN))
114 ethertype_p = &vlan->type;
116 ethertype = clib_mem_unaligned (ethertype_p, u16);
117 if (PREDICT_TRUE ((ethertype != htons (ETHERNET_TYPE_SLOW_PROTOCOLS))
118 && (ethertype != htons (ETHERNET_TYPE_CDP))
119 && (ethertype != htons (ETHERNET_TYPE_802_1_LLDP))))
121 /* Change the physical interface to bond interface */
122 vnet_buffer (b)->sw_if_index[VLIB_RX] = bond_sw_if_index;
127 vlib_error_count (vm, node->node_index, BOND_INPUT_ERROR_PASS_THRU, 1);
132 bond_update_next (vlib_main_t * vm, vlib_node_runtime_t * node,
133 u32 * last_slave_sw_if_index, u32 slave_sw_if_index,
135 u32 * bond_sw_if_index, vlib_buffer_t * b,
136 u32 * next_index, vlib_error_t * error)
138 u16 thread_index = vm->thread_index;
142 if (PREDICT_TRUE (*last_slave_sw_if_index == slave_sw_if_index))
146 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
147 VNET_INTERFACE_COUNTER_RX, thread_index,
148 *last_slave_sw_if_index, packet_count);
150 *last_slave_sw_if_index = slave_sw_if_index;
151 *next_index = BOND_INPUT_NEXT_DROP;
153 sif = bond_get_slave_by_sw_if_index (slave_sw_if_index);
156 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
159 ASSERT (vec_len (bif->slaves));
161 if (PREDICT_TRUE (bif->admin_up == 0))
163 *bond_sw_if_index = slave_sw_if_index;
164 *error = node->errors[BOND_INPUT_ERROR_IF_DOWN];
167 *bond_sw_if_index = bif->sw_if_index;
169 vnet_feature_next (next_index, b);
172 VLIB_NODE_FN (bond_input_node) (vlib_main_t * vm,
173 vlib_node_runtime_t * node,
174 vlib_frame_t * frame)
176 u16 thread_index = vm->thread_index;
178 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
179 u32 sw_if_indices[VLIB_FRAME_SIZE], *sw_if_index;
180 u16 nexts[VLIB_FRAME_SIZE], *next;
181 u32 last_slave_sw_if_index = ~0;
182 u32 bond_sw_if_index = 0;
183 vlib_error_t error = 0;
187 /* Vector of buffer / pkt indices we're supposed to process */
188 from = vlib_frame_vector_args (frame);
190 /* Number of buffers / pkts */
191 n_left = frame->n_vectors;
193 vlib_get_buffers (vm, from, bufs, n_left);
197 sw_if_index = sw_if_indices;
202 /* Prefetch next iteration */
203 if (PREDICT_TRUE (n_left >= 16))
205 CLIB_PREFETCH (vlib_buffer_get_current (b[8]),
206 CLIB_CACHE_LINE_BYTES, LOAD);
207 CLIB_PREFETCH (vlib_buffer_get_current (b[9]),
208 CLIB_CACHE_LINE_BYTES, LOAD);
209 CLIB_PREFETCH (vlib_buffer_get_current (b[10]),
210 CLIB_CACHE_LINE_BYTES, LOAD);
211 CLIB_PREFETCH (vlib_buffer_get_current (b[11]),
212 CLIB_CACHE_LINE_BYTES, LOAD);
214 vlib_prefetch_buffer_header (b[12], LOAD);
215 vlib_prefetch_buffer_header (b[13], LOAD);
216 vlib_prefetch_buffer_header (b[14], LOAD);
217 vlib_prefetch_buffer_header (b[15], LOAD);
220 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
221 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
222 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
223 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
225 x |= sw_if_index[0] ^ last_slave_sw_if_index;
226 x |= sw_if_index[1] ^ last_slave_sw_if_index;
227 x |= sw_if_index[2] ^ last_slave_sw_if_index;
228 x |= sw_if_index[3] ^ last_slave_sw_if_index;
230 if (PREDICT_TRUE (x == 0))
232 next[0] = next[1] = next[2] = next[3] = next_index;
233 if (next_index == BOND_INPUT_NEXT_DROP)
243 bond_sw_if_idx_rewrite (vm, node, b[0], bond_sw_if_index);
245 bond_sw_if_idx_rewrite (vm, node, b[1], bond_sw_if_index);
247 bond_sw_if_idx_rewrite (vm, node, b[2], bond_sw_if_index);
249 bond_sw_if_idx_rewrite (vm, node, b[3], bond_sw_if_index);
255 bond_update_next (vm, node, &last_slave_sw_if_index, sw_if_index[0],
256 cnt, &bond_sw_if_index, b[0], &next_index,
258 next[0] = next_index;
259 if (next_index == BOND_INPUT_NEXT_DROP)
262 cnt += bond_sw_if_idx_rewrite (vm, node, b[0], bond_sw_if_index);
264 bond_update_next (vm, node, &last_slave_sw_if_index, sw_if_index[1],
265 cnt, &bond_sw_if_index, b[1], &next_index,
267 next[1] = next_index;
268 if (next_index == BOND_INPUT_NEXT_DROP)
271 cnt += bond_sw_if_idx_rewrite (vm, node, b[1], bond_sw_if_index);
273 bond_update_next (vm, node, &last_slave_sw_if_index, sw_if_index[2],
274 cnt, &bond_sw_if_index, b[2], &next_index,
276 next[2] = next_index;
277 if (next_index == BOND_INPUT_NEXT_DROP)
280 cnt += bond_sw_if_idx_rewrite (vm, node, b[2], bond_sw_if_index);
282 bond_update_next (vm, node, &last_slave_sw_if_index, sw_if_index[3],
283 cnt, &bond_sw_if_index, b[3], &next_index,
285 next[3] = next_index;
286 if (next_index == BOND_INPUT_NEXT_DROP)
289 cnt += bond_sw_if_idx_rewrite (vm, node, b[3], bond_sw_if_index);
292 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
293 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
294 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
295 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
306 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
307 bond_update_next (vm, node, &last_slave_sw_if_index, sw_if_index[0],
308 cnt, &bond_sw_if_index, b[0], &next_index, &error);
309 next[0] = next_index;
310 if (next_index == BOND_INPUT_NEXT_DROP)
313 bond_sw_if_idx_rewrite (vm, node, b[0], bond_sw_if_index);
315 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
324 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
326 n_left = frame->n_vectors; /* number of packets to process */
328 sw_if_index = sw_if_indices;
329 bond_packet_trace_t *t0;
333 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
335 t0 = vlib_add_trace (vm, node, b[0], sizeof (*t0));
336 t0->sw_if_index = sw_if_index[0];
337 clib_memcpy (&t0->ethernet, vlib_buffer_get_current (b[0]),
338 sizeof (ethernet_header_t));
339 t0->bond_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
348 /* increase rx counters */
349 vlib_increment_simple_counter
350 (vnet_main.interface_main.sw_if_counters +
351 VNET_INTERFACE_COUNTER_RX, thread_index, bond_sw_if_index, cnt);
353 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
354 vlib_node_increment_counter (vm, bond_input_node.index,
355 BOND_INPUT_ERROR_NONE, frame->n_vectors);
357 return frame->n_vectors;
360 static clib_error_t *
361 bond_input_init (vlib_main_t * vm)
367 VLIB_REGISTER_NODE (bond_input_node) = {
368 .name = "bond-input",
369 .vector_size = sizeof (u32),
370 .format_buffer = format_ethernet_header_with_length,
371 .format_trace = format_bond_input_trace,
372 .type = VLIB_NODE_TYPE_INTERNAL,
373 .n_errors = BOND_INPUT_N_ERROR,
374 .error_strings = bond_input_error_strings,
375 .n_next_nodes = BOND_INPUT_N_NEXT,
378 [BOND_INPUT_NEXT_DROP] = "error-drop"
382 VLIB_INIT_FUNCTION (bond_input_init);
384 VNET_FEATURE_INIT (bond_input, static) =
386 .arc_name = "device-input",
387 .node_name = "bond-input",
388 .runs_before = VNET_FEATURES ("ethernet-input"),
392 static clib_error_t *
393 bond_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
395 bond_main_t *bm = &bond_main;
397 vlib_main_t *vm = bm->vlib_main;
399 sif = bond_get_slave_by_sw_if_index (sw_if_index);
402 sif->port_enabled = flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP;
403 if (sif->port_enabled == 0)
405 if (sif->lacp_enabled == 0)
407 bond_disable_collecting_distributing (vm, sif);
412 if (sif->lacp_enabled == 0)
414 bond_enable_collecting_distributing (vm, sif);
422 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bond_sw_interface_up_down);
424 static clib_error_t *
425 bond_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
427 bond_main_t *bm = &bond_main;
429 vnet_sw_interface_t *sw;
430 vlib_main_t *vm = bm->vlib_main;
432 sw = vnet_get_hw_sw_interface (vnm, hw_if_index);
433 sif = bond_get_slave_by_sw_if_index (sw->sw_if_index);
436 if (!(flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
438 if (sif->lacp_enabled == 0)
440 bond_disable_collecting_distributing (vm, sif);
445 if (sif->lacp_enabled == 0)
447 bond_enable_collecting_distributing (vm, sif);
455 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bond_hw_interface_up_down);
458 * fd.io coding-style-patch-verification: ON
461 * eval: (c-set-style "gnu")