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/ip-neighbor/ip4_neighbor.h>
21 #include <vnet/ip-neighbor/ip6_neighbor.h>
22 #include <vnet/bonding/node.h>
24 #define foreach_bond_tx_error \
25 _ (NONE, "no error") \
26 _ (IF_DOWN, "interface down") \
27 _ (BAD_LB_MODE, "bad load balance mode") \
28 _ (NO_MEMBER, "no member")
32 #define _(f,s) BOND_TX_ERROR_##f,
38 static char *bond_tx_error_strings[] = {
45 format_bond_tx_trace (u8 * s, va_list * args)
47 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49 bond_packet_trace_t *t = va_arg (*args, bond_packet_trace_t *);
50 vnet_hw_interface_t *hw, *hw1;
51 vnet_main_t *vnm = vnet_get_main ();
53 hw = vnet_get_sup_hw_interface (vnm, t->sw_if_index);
54 hw1 = vnet_get_sup_hw_interface (vnm, t->bond_sw_if_index);
55 s = format (s, "src %U, dst %U, %s -> %s",
56 format_ethernet_address, t->ethernet.src_address,
57 format_ethernet_address, t->ethernet.dst_address,
63 #ifndef CLIB_MARCH_VARIANT
65 format_bond_interface_name (u8 * s, va_list * args)
67 u32 dev_instance = va_arg (*args, u32);
68 bond_main_t *bm = &bond_main;
69 bond_if_t *bif = pool_elt_at_index (bm->interfaces, dev_instance);
71 s = format (s, "BondEthernet%lu", bif->id);
77 static __clib_unused clib_error_t *
78 bond_set_l2_mode_function (vnet_main_t * vnm,
79 struct vnet_hw_interface_t *bif_hw,
84 struct vnet_hw_interface_t *mif_hw;
86 bif = bond_get_bond_if_by_sw_if_index (bif_hw->sw_if_index);
90 if ((bif_hw->l2_if_count == 1) && (l2_if_adjust == 1))
92 /* Just added first L2 interface on this port */
93 vec_foreach (sw_if_index, bif->members)
95 mif_hw = vnet_get_sup_hw_interface (vnm, *sw_if_index);
96 ethernet_set_flags (vnm, mif_hw->hw_if_index,
97 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
100 else if ((bif_hw->l2_if_count == 0) && (l2_if_adjust == -1))
102 /* Just removed last L2 subinterface on this port */
103 vec_foreach (sw_if_index, bif->members)
105 mif_hw = vnet_get_sup_hw_interface (vnm, *sw_if_index);
106 ethernet_set_flags (vnm, mif_hw->hw_if_index,
107 /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
114 static clib_error_t *
115 bond_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
117 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
118 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
119 bond_main_t *bm = &bond_main;
120 bond_if_t *bif = pool_elt_at_index (bm->interfaces, hif->dev_instance);
122 bif->admin_up = is_up;
124 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
125 VNET_HW_INTERFACE_FLAG_LINK_UP);
129 static clib_error_t *
130 bond_add_del_mac_address (vnet_hw_interface_t * hi, const u8 * address,
133 vnet_main_t *vnm = vnet_get_main ();
135 clib_error_t *error = 0;
136 vnet_hw_interface_t *s_hi;
140 bif = bond_get_bond_if_by_sw_if_index (hi->sw_if_index);
143 return clib_error_return (0,
144 "No bond interface found for sw_if_index %u",
148 /* Add/del address on each member hw intf, they control the hardware */
149 vec_foreach_index (i, bif->members)
151 s_hi = vnet_get_sup_hw_interface (vnm, vec_elt (bif->members, i));
152 error = vnet_hw_interface_add_del_mac_address (vnm, s_hi->hw_if_index,
159 /* undo any that were completed before the failure */
160 for (j = i - 1; j > -1; j--)
162 s_hi = vnet_get_sup_hw_interface (vnm, vec_elt (bif->members, j));
163 vnet_hw_interface_add_del_mac_address (vnm, s_hi->hw_if_index,
174 static_always_inline void
175 bond_tx_add_to_queue (bond_per_thread_data_t * ptd, u32 port, u32 bi)
177 u32 idx = ptd->per_port_queue[port].n_buffers++;
178 ptd->per_port_queue[port].buffers[idx] = bi;
181 static_always_inline u32
182 bond_lb_broadcast (vlib_main_t *vm, bond_if_t *bif, vlib_buffer_t *b0,
185 bond_main_t *bm = &bond_main;
189 u16 thread_index = vm->thread_index;
190 bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
193 for (port = 1; port < n_members; port++)
195 sw_if_index = *vec_elt_at_index (bif->active_members, port);
196 c0 = vlib_buffer_copy (vm, b0);
197 if (PREDICT_TRUE (c0 != 0))
199 vnet_buffer (c0)->sw_if_index[VLIB_TX] = sw_if_index;
200 bond_tx_add_to_queue (ptd, port, vlib_get_buffer_index (vm, c0));
207 static_always_inline u32
208 bond_lb_round_robin (bond_if_t *bif, vlib_buffer_t *b0, uword n_members)
210 bif->lb_rr_last_index++;
211 if (bif->lb_rr_last_index >= n_members)
212 bif->lb_rr_last_index = 0;
214 return bif->lb_rr_last_index;
217 static_always_inline void
218 bond_tx_hash (vlib_main_t *vm, bond_per_thread_data_t *ptd, bond_if_t *bif,
219 vlib_buffer_t **b, u32 *h, u32 n_left)
221 u32 n_left_from = n_left;
224 ASSERT (bif->hash_func != 0);
226 vec_validate_aligned (ptd->data, n_left - 1, CLIB_CACHE_LINE_BYTES);
230 // Prefetch next iteration
231 vlib_prefetch_buffer_header (b[4], LOAD);
232 vlib_prefetch_buffer_header (b[5], LOAD);
233 vlib_prefetch_buffer_header (b[6], LOAD);
234 vlib_prefetch_buffer_header (b[7], LOAD);
236 data[0] = vlib_buffer_get_current (b[0]);
237 data[1] = vlib_buffer_get_current (b[1]);
238 data[2] = vlib_buffer_get_current (b[2]);
239 data[3] = vlib_buffer_get_current (b[3]);
248 data[0] = vlib_buffer_get_current (b[0]);
255 bif->hash_func (ptd->data, h, n_left_from);
256 vec_reset_length (ptd->data);
259 static_always_inline void
260 bond_tx_no_hash (vlib_main_t *vm, bond_if_t *bif, vlib_buffer_t **b, u32 *h,
261 u32 n_left, uword n_members, u32 lb_alg)
265 // Prefetch next iteration
266 vlib_prefetch_buffer_header (b[4], LOAD);
267 vlib_prefetch_buffer_header (b[5], LOAD);
268 vlib_prefetch_buffer_header (b[6], LOAD);
269 vlib_prefetch_buffer_header (b[7], LOAD);
271 clib_prefetch_load (b[4]->data);
272 clib_prefetch_load (b[5]->data);
273 clib_prefetch_load (b[6]->data);
274 clib_prefetch_load (b[7]->data);
276 if (lb_alg == BOND_LB_RR)
278 h[0] = bond_lb_round_robin (bif, b[0], n_members);
279 h[1] = bond_lb_round_robin (bif, b[1], n_members);
280 h[2] = bond_lb_round_robin (bif, b[2], n_members);
281 h[3] = bond_lb_round_robin (bif, b[3], n_members);
283 else if (lb_alg == BOND_LB_BC)
285 h[0] = bond_lb_broadcast (vm, bif, b[0], n_members);
286 h[1] = bond_lb_broadcast (vm, bif, b[1], n_members);
287 h[2] = bond_lb_broadcast (vm, bif, b[2], n_members);
288 h[3] = bond_lb_broadcast (vm, bif, b[3], n_members);
302 if (bif->lb == BOND_LB_RR)
303 h[0] = bond_lb_round_robin (bif, b[0], n_members);
304 else if (bif->lb == BOND_LB_BC)
305 h[0] = bond_lb_broadcast (vm, bif, b[0], n_members);
317 static_always_inline void
318 bond_hash_to_port (u32 * h, u32 n_left, u32 n_members,
319 int use_modulo_shortcut)
321 u32 mask = n_members - 1;
325 if (use_modulo_shortcut)
344 if (use_modulo_shortcut)
353 static_always_inline void
354 bond_update_sw_if_index (bond_per_thread_data_t * ptd, bond_if_t * bif,
355 u32 * bi, vlib_buffer_t ** b, u32 * data, u32 n_left,
356 int single_sw_if_index)
358 u32 sw_if_index = data[0];
363 // Prefetch next iteration
364 vlib_prefetch_buffer_header (b[4], LOAD);
365 vlib_prefetch_buffer_header (b[5], LOAD);
366 vlib_prefetch_buffer_header (b[6], LOAD);
367 vlib_prefetch_buffer_header (b[7], LOAD);
369 if (PREDICT_FALSE (single_sw_if_index))
371 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = sw_if_index;
372 vnet_buffer (b[1])->sw_if_index[VLIB_TX] = sw_if_index;
373 vnet_buffer (b[2])->sw_if_index[VLIB_TX] = sw_if_index;
374 vnet_buffer (b[3])->sw_if_index[VLIB_TX] = sw_if_index;
376 bond_tx_add_to_queue (ptd, 0, bi[0]);
377 bond_tx_add_to_queue (ptd, 0, bi[1]);
378 bond_tx_add_to_queue (ptd, 0, bi[2]);
379 bond_tx_add_to_queue (ptd, 0, bi[3]);
383 vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
384 *vec_elt_at_index (bif->active_members, h[0]);
385 vnet_buffer (b[1])->sw_if_index[VLIB_TX] =
386 *vec_elt_at_index (bif->active_members, h[1]);
387 vnet_buffer (b[2])->sw_if_index[VLIB_TX] =
388 *vec_elt_at_index (bif->active_members, h[2]);
389 vnet_buffer (b[3])->sw_if_index[VLIB_TX] =
390 *vec_elt_at_index (bif->active_members, h[3]);
392 bond_tx_add_to_queue (ptd, h[0], bi[0]);
393 bond_tx_add_to_queue (ptd, h[1], bi[1]);
394 bond_tx_add_to_queue (ptd, h[2], bi[2]);
395 bond_tx_add_to_queue (ptd, h[3], bi[3]);
405 if (PREDICT_FALSE (single_sw_if_index))
407 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = sw_if_index;
408 bond_tx_add_to_queue (ptd, 0, bi[0]);
412 vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
413 *vec_elt_at_index (bif->active_members, h[0]);
414 bond_tx_add_to_queue (ptd, h[0], bi[0]);
424 static_always_inline void
425 bond_tx_trace (vlib_main_t * vm, vlib_node_runtime_t * node, bond_if_t * bif,
426 vlib_buffer_t ** b, u32 n_left, u32 * h)
428 uword n_trace = vlib_get_trace_count (vm, node);
430 while (n_trace > 0 && n_left > 0)
433 (vlib_trace_buffer (vm, node, 0, b[0], 0 /* follow_chain */ )))
435 bond_packet_trace_t *t0;
436 ethernet_header_t *eth;
438 vlib_set_trace_count (vm, node, --n_trace);
439 t0 = vlib_add_trace (vm, node, b[0], sizeof (*t0));
440 eth = vlib_buffer_get_current (b[0]);
442 t0->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
445 t0->bond_sw_if_index =
446 *vec_elt_at_index (bif->active_members, 0);
450 t0->bond_sw_if_index =
451 *vec_elt_at_index (bif->active_members, h[0]);
460 VNET_DEVICE_CLASS_TX_FN (bond_dev_class) (vlib_main_t * vm,
461 vlib_node_runtime_t * node,
462 vlib_frame_t * frame)
464 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
465 bond_main_t *bm = &bond_main;
466 u16 thread_index = vm->thread_index;
467 bond_if_t *bif = pool_elt_at_index (bm->interfaces, rund->dev_instance);
469 vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
470 u32 *from = vlib_frame_vector_args (frame);
471 u32 n_left = frame->n_vectors;
472 u32 hashes[VLIB_FRAME_SIZE], *h;
473 vnet_main_t *vnm = vnet_get_main ();
474 bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
478 if (PREDICT_FALSE (bif->admin_up == 0))
480 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
481 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
482 VNET_INTERFACE_COUNTER_DROP,
483 thread_index, bif->sw_if_index,
485 vlib_error_count (vm, node->node_index, BOND_TX_ERROR_IF_DOWN,
487 return frame->n_vectors;
490 n_members = vec_len (bif->active_members);
491 if (PREDICT_FALSE (n_members == 0))
493 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
494 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
495 VNET_INTERFACE_COUNTER_DROP,
496 thread_index, bif->sw_if_index,
498 vlib_error_count (vm, node->node_index, BOND_TX_ERROR_NO_MEMBER,
500 return frame->n_vectors;
503 vlib_get_buffers (vm, from, bufs, n_left);
505 /* active-backup mode, ship everything to first sw if index */
506 if ((bif->lb == BOND_LB_AB) || PREDICT_FALSE (n_members == 1))
508 sw_if_index = *vec_elt_at_index (bif->active_members, 0);
510 bond_tx_trace (vm, node, bif, bufs, frame->n_vectors, 0);
511 bond_update_sw_if_index (ptd, bif, from, bufs, &sw_if_index, n_left,
512 /* single_sw_if_index */ 1);
516 if (bif->lb == BOND_LB_BC)
518 sw_if_index = *vec_elt_at_index (bif->active_members, 0);
520 bond_tx_no_hash (vm, bif, bufs, hashes, n_left, n_members, BOND_LB_BC);
521 bond_tx_trace (vm, node, bif, bufs, frame->n_vectors, 0);
522 bond_update_sw_if_index (ptd, bif, from, bufs, &sw_if_index, n_left,
523 /* single_sw_if_index */ 1);
527 /* if have at least one member on local numa node, only members on local numa
528 node will transmit pkts when bif->local_numa_only is enabled */
529 if (bif->n_numa_members >= 1)
530 n_members = bif->n_numa_members;
532 if (bif->lb == BOND_LB_RR)
533 bond_tx_no_hash (vm, bif, bufs, hashes, n_left, n_members, BOND_LB_RR);
535 bond_tx_hash (vm, ptd, bif, bufs, hashes, n_left);
537 /* calculate port out of hash */
539 if (BOND_MODULO_SHORTCUT (n_members))
540 bond_hash_to_port (h, frame->n_vectors, n_members, 1);
542 bond_hash_to_port (h, frame->n_vectors, n_members, 0);
544 bond_tx_trace (vm, node, bif, bufs, frame->n_vectors, h);
546 bond_update_sw_if_index (ptd, bif, from, bufs, hashes, frame->n_vectors,
547 /* single_sw_if_index */ 0);
550 for (p = 0; p < n_members; p++)
555 sw_if_index = *vec_elt_at_index (bif->active_members, p);
556 if (PREDICT_TRUE (ptd->per_port_queue[p].n_buffers))
558 f = vnet_get_frame_to_sw_interface (vnm, sw_if_index);
559 f->n_vectors = ptd->per_port_queue[p].n_buffers;
560 to_next = vlib_frame_vector_args (f);
561 clib_memcpy_fast (to_next, ptd->per_port_queue[p].buffers,
562 f->n_vectors * sizeof (u32));
563 vnet_put_frame_to_sw_interface (vnm, sw_if_index, f);
564 ptd->per_port_queue[p].n_buffers = 0;
567 return frame->n_vectors;
571 bond_active_interface_switch_cb (vnet_main_t * vnm, u32 sw_if_index,
574 bond_main_t *bm = &bond_main;
576 ip4_neighbor_advertise (bm->vlib_main, bm->vnet_main, sw_if_index,
577 vlib_get_thread_index (), NULL);
578 ip6_neighbor_advertise (bm->vlib_main, bm->vnet_main, sw_if_index,
579 vlib_get_thread_index (), NULL);
581 return (WALK_CONTINUE);
585 bond_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
587 vnet_main_t *vnm = vnet_get_main ();
588 uword event_type, *event_data = 0;
595 vlib_process_wait_for_event (vm);
596 event_type = vlib_process_get_events (vm, &event_data);
597 ASSERT (event_type == BOND_SEND_GARP_NA);
598 for (i = 0; i < vec_len (event_data); i++)
600 hw_if_index = event_data[i];
601 if (vnet_get_hw_interface_or_null (vnm, hw_if_index))
602 /* walk hw interface to process all subinterfaces */
603 vnet_hw_interface_walk_sw (vnm, hw_if_index,
604 bond_active_interface_switch_cb, 0);
606 vec_reset_length (event_data);
612 VLIB_REGISTER_NODE (bond_process_node) = {
613 .function = bond_process,
614 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
615 .type = VLIB_NODE_TYPE_PROCESS,
616 .name = "bond-process",
621 VNET_DEVICE_CLASS (bond_dev_class) = {
623 .tx_function_n_errors = BOND_TX_N_ERROR,
624 .tx_function_error_strings = bond_tx_error_strings,
625 .format_device_name = format_bond_interface_name,
626 .set_l2_mode_function = bond_set_l2_mode_function,
627 .admin_up_down_function = bond_interface_admin_up_down,
628 .format_tx_trace = format_bond_tx_trace,
629 .mac_addr_add_del_function = bond_add_del_mac_address,
634 static clib_error_t *
635 bond_member_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
637 bond_main_t *bm = &bond_main;
639 bond_detach_member_args_t args = { 0 };
643 mif = bond_get_member_by_sw_if_index (sw_if_index);
646 args.member = sw_if_index;
647 bond_detach_member (bm->vlib_main, &args);
651 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bond_member_interface_add_del);
654 * fd.io coding-style-patch-verification: ON
657 * eval: (c-set-style "gnu")