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_subif_add_del_function (vnet_main_t * vnm, u32 hw_if_index,
78 struct vnet_sw_interface_t *st, int is_add)
85 bond_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
87 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
88 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
89 bond_main_t *bm = &bond_main;
90 bond_if_t *bif = pool_elt_at_index (bm->interfaces, hif->dev_instance);
92 bif->admin_up = is_up;
93 if (is_up && vec_len (bif->active_slaves))
94 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
95 VNET_HW_INTERFACE_FLAG_LINK_UP);
100 bond_load_balance_broadcast (vlib_main_t * vm, vlib_node_runtime_t * node,
101 bond_if_t * bif, vlib_buffer_t * b0,
104 vnet_main_t *vnm = vnet_get_main ();
112 for (i = 1; i < slave_count; i++)
114 sw_if_index = *vec_elt_at_index (bif->active_slaves, i);
115 f = vnet_get_frame_to_sw_interface (vnm, sw_if_index);
116 to_next = vlib_frame_vector_args (f);
117 to_next += f->n_vectors;
118 c0 = vlib_buffer_copy (vm, b0);
119 if (PREDICT_TRUE (c0 != 0))
121 vnet_buffer (c0)->sw_if_index[VLIB_TX] = sw_if_index;
122 to_next[0] = vlib_get_buffer_index (vm, c0);
124 vnet_put_frame_to_sw_interface (vnm, sw_if_index, f);
132 bond_load_balance_l2 (vlib_main_t * vm, vlib_node_runtime_t * node,
133 bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
135 ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
136 u32 a = 0, b = 0, c = 0, t1, t2;
139 memcpy (&t1, eth->src_address, sizeof (t1));
140 memcpy (&t11, ð->src_address[4], sizeof (t11));
143 memcpy (&t2, eth->dst_address, sizeof (t2));
144 memcpy (&t22, ð->dst_address[4], sizeof (t22));
147 hash_v3_mix32 (a, b, c);
148 hash_v3_finalize32 (a, b, c);
150 return c % slave_count;
154 bond_locate_ethertype (ethernet_header_t * eth)
157 ethernet_vlan_header_t *vlan;
159 if (!ethernet_frame_is_tagged (clib_net_to_host_u16 (eth->type)))
161 ethertype_p = ð->type;
165 vlan = (void *) (eth + 1);
166 ethertype_p = &vlan->type;
167 if (*ethertype_p == ntohs (ETHERNET_TYPE_VLAN))
170 ethertype_p = &vlan->type;
177 bond_load_balance_l23 (vlib_main_t * vm, vlib_node_runtime_t * node,
178 bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
180 ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
183 u16 ethertype, *ethertype_p;
185 ethertype_p = bond_locate_ethertype (eth);
186 ethertype = *ethertype_p;
188 if ((ethertype != htons (ETHERNET_TYPE_IP4)) &&
189 (ethertype != htons (ETHERNET_TYPE_IP6)))
190 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
192 ip4 = (ip4_header_t *) (ethertype_p + 1);
193 ip_version = (ip4->ip_version_and_header_length >> 4);
195 if (ip_version == 0x4)
198 u32 a = 0, b = 0, c = 0, t1, t2;
200 memcpy (&t1, eth->src_address, sizeof (t1));
201 memcpy (&t11, ð->src_address[4], sizeof (t11));
204 memcpy (&t2, eth->dst_address, sizeof (t2));
205 memcpy (&t22, ð->dst_address[4], sizeof (t22));
208 c = ip4->src_address.data_u32 ^ ip4->dst_address.data_u32;
210 hash_v3_mix32 (a, b, c);
211 hash_v3_finalize32 (a, b, c);
213 return c % slave_count;
215 else if (ip_version == 0x6)
219 ip6_header_t *ip6 = (ip6_header_t *) (eth + 1);
221 memcpy (&t1, eth->src_address, sizeof (eth->src_address));
222 memcpy (&t2, eth->dst_address, sizeof (eth->dst_address));
225 b = (ip6->src_address.as_u64[0] ^ ip6->src_address.as_u64[1]);
226 c = (ip6->dst_address.as_u64[0] ^ ip6->dst_address.as_u64[1]);
228 hash_mix64 (a, b, c);
229 return c % slave_count;
231 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
235 bond_load_balance_l34 (vlib_main_t * vm, vlib_node_runtime_t * node,
236 bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
238 ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
240 uword is_tcp_udp = 0;
242 u16 ethertype, *ethertype_p;
244 ethertype_p = bond_locate_ethertype (eth);
245 ethertype = *ethertype_p;
247 if ((ethertype != htons (ETHERNET_TYPE_IP4)) &&
248 (ethertype != htons (ETHERNET_TYPE_IP6)))
249 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
251 ip4 = (ip4_header_t *) (ethertype_p + 1);
252 ip_version = (ip4->ip_version_and_header_length >> 4);
254 if (ip_version == 0x4)
256 u32 a = 0, b = 0, c = 0, t1, t2;
257 tcp_header_t *tcp = (void *) (ip4 + 1);
258 is_tcp_udp = (ip4->protocol == IP_PROTOCOL_TCP) ||
259 (ip4->protocol == IP_PROTOCOL_UDP);
261 a = ip4->src_address.data_u32 ^ ip4->dst_address.data_u32;
263 t1 = is_tcp_udp ? tcp->src : 0;
264 t2 = is_tcp_udp ? tcp->dst : 0;
267 hash_v3_mix32 (a, b, c);
268 hash_v3_finalize32 (a, b, c);
270 return c % slave_count;
272 else if (ip_version == 0x6)
276 ip6_header_t *ip6 = (ip6_header_t *) (eth + 1);
277 tcp_header_t *tcp = (void *) (ip6 + 1);
279 if (PREDICT_TRUE ((ip6->protocol == IP_PROTOCOL_TCP) ||
280 (ip6->protocol == IP_PROTOCOL_UDP)))
283 tcp = (void *) (ip6 + 1);
285 else if (ip6->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS)
287 ip6_hop_by_hop_header_t *hbh =
288 (ip6_hop_by_hop_header_t *) (ip6 + 1);
289 if ((hbh->protocol == IP_PROTOCOL_TCP)
290 || (hbh->protocol == IP_PROTOCOL_UDP))
293 tcp = (tcp_header_t *) ((u8 *) hbh + ((hbh->length + 1) << 3));
296 a = (ip6->src_address.as_u64[0] ^ ip6->src_address.as_u64[1]);
297 b = (ip6->dst_address.as_u64[0] ^ ip6->dst_address.as_u64[1]);
299 t1 = is_tcp_udp ? tcp->src : 0;
300 t2 = is_tcp_udp ? tcp->dst : 0;
302 hash_mix64 (a, b, c);
304 return c % slave_count;
307 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
311 bond_load_balance_round_robin (vlib_main_t * vm,
312 vlib_node_runtime_t * node,
313 bond_if_t * bif, vlib_buffer_t * b0,
316 bif->lb_rr_last_index++;
317 bif->lb_rr_last_index %= slave_count;
319 return bif->lb_rr_last_index;
323 bond_load_balance_active_backup (vlib_main_t * vm,
324 vlib_node_runtime_t * node,
325 bond_if_t * bif, vlib_buffer_t * b0,
328 /* First interface is the active, the rest is backup */
332 static bond_load_balance_func_t bond_load_balance_table[] = {
333 #define _(v,f,s, p) { bond_load_balance_##p },
339 bond_tx_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
340 vlib_frame_t * frame)
342 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
343 bond_main_t *bm = &bond_main;
344 bond_if_t *bif = pool_elt_at_index (bm->interfaces, rund->dev_instance);
345 u32 bi0, bi1, bi2, bi3;
346 vlib_buffer_t *b0, *b1, *b2, *b3;
347 u32 *from = vlib_frame_vector_args (frame);
349 ethernet_header_t *eth;
350 u32 next0 = 0, next1 = 0, next2 = 0, next3 = 0;
351 u32 port, port1, port2, port3;
352 u32 sw_if_index, sw_if_index1, sw_if_index2, sw_if_index3;
353 bond_packet_trace_t *t0;
354 uword n_trace = vlib_get_trace_count (vm, node);
355 u16 thread_index = vlib_get_thread_index ();
356 vnet_main_t *vnm = vnet_get_main ();
358 u32 sif_if_index, sif_if_index1, sif_if_index2, sif_if_index3;
362 if (PREDICT_FALSE (bif->admin_up == 0))
364 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
365 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
366 VNET_INTERFACE_COUNTER_DROP,
367 thread_index, bif->sw_if_index,
369 vlib_error_count (vm, node->node_index, BOND_TX_ERROR_IF_DOWN,
371 return frame->n_vectors;
374 clib_spinlock_lock_if_init (&bif->lockp);
375 slave_count = vec_len (bif->active_slaves);
376 if (PREDICT_FALSE (slave_count == 0))
379 b0 = vlib_get_buffer (vm, bi0);
380 vlib_increment_combined_counter
381 (vnet_main.interface_main.combined_sw_if_counters
382 + VNET_INTERFACE_COUNTER_TX, thread_index, bif->sw_if_index,
383 frame->n_vectors, b0->current_length);
385 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
386 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
387 VNET_INTERFACE_COUNTER_DROP,
388 thread_index, bif->sw_if_index,
390 vlib_error_count (vm, node->node_index, BOND_TX_ERROR_NO_SLAVE,
392 clib_spinlock_unlock_if_init (&bif->lockp);
393 return frame->n_vectors;
396 vec_validate_aligned (bif->per_thread_info[thread_index].frame, slave_count,
397 CLIB_CACHE_LINE_BYTES);
399 /* Number of buffers / pkts */
400 n_left_from = frame->n_vectors;
402 while (n_left_from > 0)
404 while (n_left_from >= 4)
406 // Prefetch next iteration
407 if (n_left_from >= 8)
409 vlib_buffer_t *p4, *p5, *p6, *p7;
411 p4 = vlib_get_buffer (vm, from[4]);
412 p5 = vlib_get_buffer (vm, from[5]);
413 p6 = vlib_get_buffer (vm, from[6]);
414 p7 = vlib_get_buffer (vm, from[7]);
416 vlib_prefetch_buffer_header (p4, STORE);
417 vlib_prefetch_buffer_header (p5, STORE);
418 vlib_prefetch_buffer_header (p6, STORE);
419 vlib_prefetch_buffer_header (p7, STORE);
421 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, LOAD);
422 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, LOAD);
423 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, LOAD);
424 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, LOAD);
432 b0 = vlib_get_buffer (vm, bi0);
433 b1 = vlib_get_buffer (vm, bi1);
434 b2 = vlib_get_buffer (vm, bi2);
435 b3 = vlib_get_buffer (vm, bi3);
437 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
438 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
439 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b2);
440 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b3);
442 sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
443 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
444 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
445 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
448 (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif,
451 (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif,
454 (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif,
457 (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif,
460 sif_if_index = *vec_elt_at_index (bif->active_slaves, port);
461 sif_if_index1 = *vec_elt_at_index (bif->active_slaves, port1);
462 sif_if_index2 = *vec_elt_at_index (bif->active_slaves, port2);
463 sif_if_index3 = *vec_elt_at_index (bif->active_slaves, port3);
465 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sif_if_index;
466 vnet_buffer (b1)->sw_if_index[VLIB_TX] = sif_if_index1;
467 vnet_buffer (b2)->sw_if_index[VLIB_TX] = sif_if_index2;
468 vnet_buffer (b3)->sw_if_index[VLIB_TX] = sif_if_index3;
470 if (bif->per_thread_info[thread_index].frame[port] == 0)
471 bif->per_thread_info[thread_index].frame[port] =
472 vnet_get_frame_to_sw_interface (vnm, sif_if_index);
474 if (bif->per_thread_info[thread_index].frame[port1] == 0)
475 bif->per_thread_info[thread_index].frame[port1] =
476 vnet_get_frame_to_sw_interface (vnm, sif_if_index1);
478 if (bif->per_thread_info[thread_index].frame[port2] == 0)
479 bif->per_thread_info[thread_index].frame[port2] =
480 vnet_get_frame_to_sw_interface (vnm, sif_if_index2);
482 if (bif->per_thread_info[thread_index].frame[port3] == 0)
483 bif->per_thread_info[thread_index].frame[port3] =
484 vnet_get_frame_to_sw_interface (vnm, sif_if_index3);
486 f = bif->per_thread_info[thread_index].frame[port];
487 to_next = vlib_frame_vector_args (f);
488 to_next += f->n_vectors;
489 to_next[0] = vlib_get_buffer_index (vm, b0);
492 f = bif->per_thread_info[thread_index].frame[port1];
493 to_next = vlib_frame_vector_args (f);
494 to_next += f->n_vectors;
495 to_next[0] = vlib_get_buffer_index (vm, b1);
498 f = bif->per_thread_info[thread_index].frame[port2];
499 to_next = vlib_frame_vector_args (f);
500 to_next += f->n_vectors;
501 to_next[0] = vlib_get_buffer_index (vm, b2);
504 f = bif->per_thread_info[thread_index].frame[port3];
505 to_next = vlib_frame_vector_args (f);
506 to_next += f->n_vectors;
507 to_next[0] = vlib_get_buffer_index (vm, b3);
510 if (PREDICT_FALSE (n_trace > 0))
512 vlib_trace_buffer (vm, node, next0, b0, 0 /* follow_chain */ );
513 vlib_set_trace_count (vm, node, --n_trace);
514 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
515 eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
517 t0->sw_if_index = sw_if_index;
518 t0->bond_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
520 if (PREDICT_TRUE (n_trace > 0))
522 vlib_trace_buffer (vm, node, next1, b1,
523 0 /* follow_chain */ );
524 vlib_set_trace_count (vm, node, --n_trace);
525 t0 = vlib_add_trace (vm, node, b1, sizeof (*t0));
526 eth = (ethernet_header_t *) vlib_buffer_get_current (b1);
528 t0->sw_if_index = sw_if_index1;
529 t0->bond_sw_if_index =
530 vnet_buffer (b1)->sw_if_index[VLIB_TX];
532 if (PREDICT_TRUE (n_trace > 0))
534 vlib_trace_buffer (vm, node, next2, b2,
535 0 /* follow_chain */ );
536 vlib_set_trace_count (vm, node, --n_trace);
537 t0 = vlib_add_trace (vm, node, b2, sizeof (*t0));
539 (ethernet_header_t *) vlib_buffer_get_current (b2);
541 t0->sw_if_index = sw_if_index2;
542 t0->bond_sw_if_index =
543 vnet_buffer (b2)->sw_if_index[VLIB_TX];
545 if (PREDICT_TRUE (n_trace > 0))
547 vlib_trace_buffer (vm, node, next3, b3,
548 0 /* follow_chain */ );
549 vlib_set_trace_count (vm, node, --n_trace);
550 t0 = vlib_add_trace (vm, node, b3, sizeof (*t0));
552 (ethernet_header_t *)
553 vlib_buffer_get_current (b3);
555 t0->sw_if_index = sw_if_index3;
556 t0->bond_sw_if_index =
557 vnet_buffer (b3)->sw_if_index[VLIB_TX];
566 while (n_left_from > 0)
568 // Prefetch next iteration
573 p2 = vlib_get_buffer (vm, from[1]);
574 vlib_prefetch_buffer_header (p2, STORE);
575 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
579 b0 = vlib_get_buffer (vm, bi0);
581 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
583 sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
586 (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif,
588 sif_if_index = *vec_elt_at_index (bif->active_slaves, port);
589 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sif_if_index;
590 if (bif->per_thread_info[thread_index].frame[port] == 0)
591 bif->per_thread_info[thread_index].frame[port] =
592 vnet_get_frame_to_sw_interface (vnm, sif_if_index);
593 f = bif->per_thread_info[thread_index].frame[port];
594 to_next = vlib_frame_vector_args (f);
595 to_next += f->n_vectors;
596 to_next[0] = vlib_get_buffer_index (vm, b0);
599 if (PREDICT_FALSE (n_trace > 0))
601 vlib_trace_buffer (vm, node, next0, b0, 0 /* follow_chain */ );
602 vlib_set_trace_count (vm, node, --n_trace);
603 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
604 eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
606 t0->sw_if_index = sw_if_index;
607 t0->bond_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
615 for (port = 0; port < slave_count; port++)
617 f = bif->per_thread_info[thread_index].frame[port];
621 sw_if_index = *vec_elt_at_index (bif->active_slaves, port);
622 vnet_put_frame_to_sw_interface (vnm, sw_if_index, f);
623 bif->per_thread_info[thread_index].frame[port] = 0;
626 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters
627 + VNET_INTERFACE_COUNTER_TX, thread_index,
628 bif->sw_if_index, frame->n_vectors);
630 clib_spinlock_unlock_if_init (&bif->lockp);
631 return frame->n_vectors;
635 VNET_DEVICE_CLASS (bond_dev_class) = {
637 .tx_function = bond_tx_fn,
638 .tx_function_n_errors = BOND_TX_N_ERROR,
639 .tx_function_error_strings = bond_tx_error_strings,
640 .format_device_name = format_bond_interface_name,
641 .admin_up_down_function = bond_interface_admin_up_down,
642 .subif_add_del_function = bond_subif_add_del_function,
643 .format_tx_trace = format_bond_tx_trace,
646 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (bond_dev_class, bond_tx_fn)
650 * fd.io coding-style-patch-verification: ON
653 * eval: (c-set-style "gnu")