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/ethernet/ethernet.h>
21 #include <vnet/ip/ip4_packet.h>
22 #include <vnet/ip/ip6_packet.h>
23 #include <vnet/ip/ip6_hop_by_hop_packet.h>
24 #include <vnet/bonding/node.h>
26 #define foreach_bond_tx_error \
28 _(IF_DOWN, "interface down") \
29 _(NO_SLAVE, "no slave")
33 #define _(f,s) BOND_TX_ERROR_##f,
39 static char *bond_tx_error_strings[] = {
46 format_bond_tx_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 *);
51 vnet_hw_interface_t *hw, *hw1;
52 vnet_main_t *vnm = vnet_get_main ();
54 hw = vnet_get_sup_hw_interface (vnm, t->sw_if_index);
55 hw1 = vnet_get_sup_hw_interface (vnm, t->bond_sw_if_index);
56 s = format (s, "src %U, dst %U, %s -> %s",
57 format_ethernet_address, t->ethernet.src_address,
58 format_ethernet_address, t->ethernet.dst_address,
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->dev_instance);
76 static __clib_unused clib_error_t *
77 bond_set_l2_mode_function (vnet_main_t * vnm,
78 struct vnet_hw_interface_t *bif_hw,
83 struct vnet_hw_interface_t *sif_hw;
85 bif = bond_get_master_by_sw_if_index (bif_hw->sw_if_index);
89 if ((bif_hw->l2_if_count == 1) && (l2_if_adjust == 1))
91 /* Just added first L2 interface on this port */
92 vec_foreach (sw_if_index, bif->slaves)
94 sif_hw = vnet_get_sup_hw_interface (vnm, *sw_if_index);
95 ethernet_set_flags (vnm, sif_hw->hw_if_index,
96 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
98 /* ensure all packets go to ethernet-input */
99 ethernet_set_rx_redirect (vnm, sif_hw, 1);
106 static __clib_unused clib_error_t *
107 bond_subif_add_del_function (vnet_main_t * vnm, u32 hw_if_index,
108 struct vnet_sw_interface_t *st, int is_add)
110 /* Nothing for now */
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;
123 if (is_up && vec_len (bif->active_slaves))
124 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
125 VNET_HW_INTERFACE_FLAG_LINK_UP);
130 bond_load_balance_broadcast (vlib_main_t * vm, vlib_node_runtime_t * node,
131 bond_if_t * bif, vlib_buffer_t * b0,
134 vnet_main_t *vnm = vnet_get_main ();
140 u16 thread_index = vlib_get_thread_index ();
142 for (port = 1; port < slave_count; port++)
144 sw_if_index = *vec_elt_at_index (bif->active_slaves, port);
145 if (bif->per_thread_info[thread_index].frame[port] == 0)
146 bif->per_thread_info[thread_index].frame[port] =
147 vnet_get_frame_to_sw_interface (vnm, sw_if_index);
148 f = bif->per_thread_info[thread_index].frame[port];
149 to_next = vlib_frame_vector_args (f);
150 to_next += f->n_vectors;
151 c0 = vlib_buffer_copy (vm, b0);
152 if (PREDICT_TRUE (c0 != 0))
154 vnet_buffer (c0)->sw_if_index[VLIB_TX] = sw_if_index;
155 to_next[0] = vlib_get_buffer_index (vm, c0);
164 bond_load_balance_l2 (vlib_main_t * vm, vlib_node_runtime_t * node,
165 bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
167 ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
168 u32 a = 0, b = 0, c = 0, t1, t2;
171 memcpy (&t1, eth->src_address, sizeof (t1));
172 memcpy (&t11, ð->src_address[4], sizeof (t11));
175 memcpy (&t2, eth->dst_address, sizeof (t2));
176 memcpy (&t22, ð->dst_address[4], sizeof (t22));
179 hash_v3_mix32 (a, b, c);
180 hash_v3_finalize32 (a, b, c);
182 return c % slave_count;
186 bond_locate_ethertype (ethernet_header_t * eth)
189 ethernet_vlan_header_t *vlan;
191 if (!ethernet_frame_is_tagged (clib_net_to_host_u16 (eth->type)))
193 ethertype_p = ð->type;
197 vlan = (void *) (eth + 1);
198 ethertype_p = &vlan->type;
199 if (*ethertype_p == ntohs (ETHERNET_TYPE_VLAN))
202 ethertype_p = &vlan->type;
209 bond_load_balance_l23 (vlib_main_t * vm, vlib_node_runtime_t * node,
210 bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
212 ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
215 u16 ethertype, *ethertype_p;
217 ethertype_p = bond_locate_ethertype (eth);
218 ethertype = *ethertype_p;
220 if ((ethertype != htons (ETHERNET_TYPE_IP4)) &&
221 (ethertype != htons (ETHERNET_TYPE_IP6)))
222 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
224 ip4 = (ip4_header_t *) (ethertype_p + 1);
225 ip_version = (ip4->ip_version_and_header_length >> 4);
227 if (ip_version == 0x4)
230 u32 a = 0, b = 0, c = 0, t1, t2;
232 memcpy (&t1, eth->src_address, sizeof (t1));
233 memcpy (&t11, ð->src_address[4], sizeof (t11));
236 memcpy (&t2, eth->dst_address, sizeof (t2));
237 memcpy (&t22, ð->dst_address[4], sizeof (t22));
240 c = ip4->src_address.data_u32 ^ ip4->dst_address.data_u32;
242 hash_v3_mix32 (a, b, c);
243 hash_v3_finalize32 (a, b, c);
245 return c % slave_count;
247 else if (ip_version == 0x6)
251 ip6_header_t *ip6 = (ip6_header_t *) (eth + 1);
253 memcpy (&t1, eth->src_address, sizeof (eth->src_address));
254 memcpy (&t2, eth->dst_address, sizeof (eth->dst_address));
257 b = (ip6->src_address.as_u64[0] ^ ip6->src_address.as_u64[1]);
258 c = (ip6->dst_address.as_u64[0] ^ ip6->dst_address.as_u64[1]);
260 hash_mix64 (a, b, c);
261 return c % slave_count;
263 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
267 bond_load_balance_l34 (vlib_main_t * vm, vlib_node_runtime_t * node,
268 bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
270 ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
272 uword is_tcp_udp = 0;
274 u16 ethertype, *ethertype_p;
276 ethertype_p = bond_locate_ethertype (eth);
277 ethertype = *ethertype_p;
279 if ((ethertype != htons (ETHERNET_TYPE_IP4)) &&
280 (ethertype != htons (ETHERNET_TYPE_IP6)))
281 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
283 ip4 = (ip4_header_t *) (ethertype_p + 1);
284 ip_version = (ip4->ip_version_and_header_length >> 4);
286 if (ip_version == 0x4)
288 u32 a = 0, b = 0, c = 0, t1, t2;
289 tcp_header_t *tcp = (void *) (ip4 + 1);
290 is_tcp_udp = (ip4->protocol == IP_PROTOCOL_TCP) ||
291 (ip4->protocol == IP_PROTOCOL_UDP);
293 a = ip4->src_address.data_u32 ^ ip4->dst_address.data_u32;
295 t1 = is_tcp_udp ? tcp->src : 0;
296 t2 = is_tcp_udp ? tcp->dst : 0;
299 hash_v3_mix32 (a, b, c);
300 hash_v3_finalize32 (a, b, c);
302 return c % slave_count;
304 else if (ip_version == 0x6)
308 ip6_header_t *ip6 = (ip6_header_t *) (eth + 1);
309 tcp_header_t *tcp = (void *) (ip6 + 1);
311 if (PREDICT_TRUE ((ip6->protocol == IP_PROTOCOL_TCP) ||
312 (ip6->protocol == IP_PROTOCOL_UDP)))
315 tcp = (void *) (ip6 + 1);
317 else if (ip6->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS)
319 ip6_hop_by_hop_header_t *hbh =
320 (ip6_hop_by_hop_header_t *) (ip6 + 1);
321 if ((hbh->protocol == IP_PROTOCOL_TCP)
322 || (hbh->protocol == IP_PROTOCOL_UDP))
325 tcp = (tcp_header_t *) ((u8 *) hbh + ((hbh->length + 1) << 3));
328 a = (ip6->src_address.as_u64[0] ^ ip6->src_address.as_u64[1]);
329 b = (ip6->dst_address.as_u64[0] ^ ip6->dst_address.as_u64[1]);
331 t1 = is_tcp_udp ? tcp->src : 0;
332 t2 = is_tcp_udp ? tcp->dst : 0;
334 hash_mix64 (a, b, c);
336 return c % slave_count;
339 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
343 bond_load_balance_round_robin (vlib_main_t * vm,
344 vlib_node_runtime_t * node,
345 bond_if_t * bif, vlib_buffer_t * b0,
348 bif->lb_rr_last_index++;
349 bif->lb_rr_last_index %= slave_count;
351 return bif->lb_rr_last_index;
355 bond_load_balance_active_backup (vlib_main_t * vm,
356 vlib_node_runtime_t * node,
357 bond_if_t * bif, vlib_buffer_t * b0,
360 /* First interface is the active, the rest is backup */
364 static bond_load_balance_func_t bond_load_balance_table[] = {
365 #define _(v,f,s, p) { bond_load_balance_##p },
371 bond_tx_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
372 vlib_frame_t * frame)
374 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
375 bond_main_t *bm = &bond_main;
376 bond_if_t *bif = pool_elt_at_index (bm->interfaces, rund->dev_instance);
377 u32 bi0, bi1, bi2, bi3;
378 vlib_buffer_t *b0, *b1, *b2, *b3;
379 u32 *from = vlib_frame_vector_args (frame);
381 ethernet_header_t *eth;
382 u32 next0 = 0, next1 = 0, next2 = 0, next3 = 0;
383 u32 port, port1, port2, port3;
384 u32 sw_if_index, sw_if_index1, sw_if_index2, sw_if_index3;
385 bond_packet_trace_t *t0;
386 uword n_trace = vlib_get_trace_count (vm, node);
387 u16 thread_index = vlib_get_thread_index ();
388 vnet_main_t *vnm = vnet_get_main ();
390 u32 sif_if_index, sif_if_index1, sif_if_index2, sif_if_index3;
394 if (PREDICT_FALSE (bif->admin_up == 0))
396 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
397 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
398 VNET_INTERFACE_COUNTER_DROP,
399 thread_index, bif->sw_if_index,
401 vlib_error_count (vm, node->node_index, BOND_TX_ERROR_IF_DOWN,
403 return frame->n_vectors;
406 clib_spinlock_lock_if_init (&bif->lockp);
407 slave_count = vec_len (bif->active_slaves);
408 if (PREDICT_FALSE (slave_count == 0))
411 b0 = vlib_get_buffer (vm, bi0);
412 vlib_increment_combined_counter
413 (vnet_main.interface_main.combined_sw_if_counters
414 + VNET_INTERFACE_COUNTER_TX, thread_index, bif->sw_if_index,
415 frame->n_vectors, b0->current_length);
417 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
418 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
419 VNET_INTERFACE_COUNTER_DROP,
420 thread_index, bif->sw_if_index,
422 vlib_error_count (vm, node->node_index, BOND_TX_ERROR_NO_SLAVE,
424 clib_spinlock_unlock_if_init (&bif->lockp);
425 return frame->n_vectors;
428 vec_validate_aligned (bif->per_thread_info[thread_index].frame, slave_count,
429 CLIB_CACHE_LINE_BYTES);
431 /* Number of buffers / pkts */
432 n_left_from = frame->n_vectors;
434 while (n_left_from > 0)
436 while (n_left_from >= 4)
438 // Prefetch next iteration
439 if (n_left_from >= 8)
441 vlib_buffer_t *p4, *p5, *p6, *p7;
443 p4 = vlib_get_buffer (vm, from[4]);
444 p5 = vlib_get_buffer (vm, from[5]);
445 p6 = vlib_get_buffer (vm, from[6]);
446 p7 = vlib_get_buffer (vm, from[7]);
448 vlib_prefetch_buffer_header (p4, STORE);
449 vlib_prefetch_buffer_header (p5, STORE);
450 vlib_prefetch_buffer_header (p6, STORE);
451 vlib_prefetch_buffer_header (p7, STORE);
453 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, LOAD);
454 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, LOAD);
455 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, LOAD);
456 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, LOAD);
464 b0 = vlib_get_buffer (vm, bi0);
465 b1 = vlib_get_buffer (vm, bi1);
466 b2 = vlib_get_buffer (vm, bi2);
467 b3 = vlib_get_buffer (vm, bi3);
469 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
470 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
471 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b2);
472 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b3);
474 sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
475 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
476 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
477 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
480 (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif,
483 (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif,
486 (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif,
489 (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif,
492 sif_if_index = *vec_elt_at_index (bif->active_slaves, port);
493 sif_if_index1 = *vec_elt_at_index (bif->active_slaves, port1);
494 sif_if_index2 = *vec_elt_at_index (bif->active_slaves, port2);
495 sif_if_index3 = *vec_elt_at_index (bif->active_slaves, port3);
497 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sif_if_index;
498 vnet_buffer (b1)->sw_if_index[VLIB_TX] = sif_if_index1;
499 vnet_buffer (b2)->sw_if_index[VLIB_TX] = sif_if_index2;
500 vnet_buffer (b3)->sw_if_index[VLIB_TX] = sif_if_index3;
502 if (bif->per_thread_info[thread_index].frame[port] == 0)
503 bif->per_thread_info[thread_index].frame[port] =
504 vnet_get_frame_to_sw_interface (vnm, sif_if_index);
506 if (bif->per_thread_info[thread_index].frame[port1] == 0)
507 bif->per_thread_info[thread_index].frame[port1] =
508 vnet_get_frame_to_sw_interface (vnm, sif_if_index1);
510 if (bif->per_thread_info[thread_index].frame[port2] == 0)
511 bif->per_thread_info[thread_index].frame[port2] =
512 vnet_get_frame_to_sw_interface (vnm, sif_if_index2);
514 if (bif->per_thread_info[thread_index].frame[port3] == 0)
515 bif->per_thread_info[thread_index].frame[port3] =
516 vnet_get_frame_to_sw_interface (vnm, sif_if_index3);
518 f = bif->per_thread_info[thread_index].frame[port];
519 to_next = vlib_frame_vector_args (f);
520 to_next += f->n_vectors;
521 to_next[0] = vlib_get_buffer_index (vm, b0);
524 f = bif->per_thread_info[thread_index].frame[port1];
525 to_next = vlib_frame_vector_args (f);
526 to_next += f->n_vectors;
527 to_next[0] = vlib_get_buffer_index (vm, b1);
530 f = bif->per_thread_info[thread_index].frame[port2];
531 to_next = vlib_frame_vector_args (f);
532 to_next += f->n_vectors;
533 to_next[0] = vlib_get_buffer_index (vm, b2);
536 f = bif->per_thread_info[thread_index].frame[port3];
537 to_next = vlib_frame_vector_args (f);
538 to_next += f->n_vectors;
539 to_next[0] = vlib_get_buffer_index (vm, b3);
542 if (PREDICT_FALSE (n_trace > 0))
544 vlib_trace_buffer (vm, node, next0, b0, 0 /* follow_chain */ );
545 vlib_set_trace_count (vm, node, --n_trace);
546 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
547 eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
549 t0->sw_if_index = sw_if_index;
550 t0->bond_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
552 if (PREDICT_TRUE (n_trace > 0))
554 vlib_trace_buffer (vm, node, next1, b1,
555 0 /* follow_chain */ );
556 vlib_set_trace_count (vm, node, --n_trace);
557 t0 = vlib_add_trace (vm, node, b1, sizeof (*t0));
558 eth = (ethernet_header_t *) vlib_buffer_get_current (b1);
560 t0->sw_if_index = sw_if_index1;
561 t0->bond_sw_if_index =
562 vnet_buffer (b1)->sw_if_index[VLIB_TX];
564 if (PREDICT_TRUE (n_trace > 0))
566 vlib_trace_buffer (vm, node, next2, b2,
567 0 /* follow_chain */ );
568 vlib_set_trace_count (vm, node, --n_trace);
569 t0 = vlib_add_trace (vm, node, b2, sizeof (*t0));
571 (ethernet_header_t *) vlib_buffer_get_current (b2);
573 t0->sw_if_index = sw_if_index2;
574 t0->bond_sw_if_index =
575 vnet_buffer (b2)->sw_if_index[VLIB_TX];
577 if (PREDICT_TRUE (n_trace > 0))
579 vlib_trace_buffer (vm, node, next3, b3,
580 0 /* follow_chain */ );
581 vlib_set_trace_count (vm, node, --n_trace);
582 t0 = vlib_add_trace (vm, node, b3, sizeof (*t0));
584 (ethernet_header_t *)
585 vlib_buffer_get_current (b3);
587 t0->sw_if_index = sw_if_index3;
588 t0->bond_sw_if_index =
589 vnet_buffer (b3)->sw_if_index[VLIB_TX];
598 while (n_left_from > 0)
600 // Prefetch next iteration
605 p2 = vlib_get_buffer (vm, from[1]);
606 vlib_prefetch_buffer_header (p2, STORE);
607 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
611 b0 = vlib_get_buffer (vm, bi0);
613 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
615 sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
618 (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif,
620 sif_if_index = *vec_elt_at_index (bif->active_slaves, port);
621 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sif_if_index;
622 if (bif->per_thread_info[thread_index].frame[port] == 0)
623 bif->per_thread_info[thread_index].frame[port] =
624 vnet_get_frame_to_sw_interface (vnm, sif_if_index);
625 f = bif->per_thread_info[thread_index].frame[port];
626 to_next = vlib_frame_vector_args (f);
627 to_next += f->n_vectors;
628 to_next[0] = vlib_get_buffer_index (vm, b0);
631 if (PREDICT_FALSE (n_trace > 0))
633 vlib_trace_buffer (vm, node, next0, b0, 0 /* follow_chain */ );
634 vlib_set_trace_count (vm, node, --n_trace);
635 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
636 eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
638 t0->sw_if_index = sw_if_index;
639 t0->bond_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
647 for (port = 0; port < slave_count; port++)
649 f = bif->per_thread_info[thread_index].frame[port];
653 sw_if_index = *vec_elt_at_index (bif->active_slaves, port);
654 vnet_put_frame_to_sw_interface (vnm, sw_if_index, f);
655 bif->per_thread_info[thread_index].frame[port] = 0;
658 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters
659 + VNET_INTERFACE_COUNTER_TX, thread_index,
660 bif->sw_if_index, frame->n_vectors);
662 clib_spinlock_unlock_if_init (&bif->lockp);
663 return frame->n_vectors;
667 VNET_DEVICE_CLASS (bond_dev_class) = {
669 .tx_function = bond_tx_fn,
670 .tx_function_n_errors = BOND_TX_N_ERROR,
671 .tx_function_error_strings = bond_tx_error_strings,
672 .format_device_name = format_bond_interface_name,
673 .set_l2_mode_function = bond_set_l2_mode_function,
674 .admin_up_down_function = bond_interface_admin_up_down,
675 .subif_add_del_function = bond_subif_add_del_function,
676 .format_tx_trace = format_bond_tx_trace,
679 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (bond_dev_class, bond_tx_fn)
683 * fd.io coding-style-patch-verification: ON
686 * eval: (c-set-style "gnu")