Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 1 | /* |
| 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: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <stdint.h> |
| 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> |
| 25 | |
| 26 | #define foreach_bond_tx_error \ |
| 27 | _(NONE, "no error") \ |
| 28 | _(IF_DOWN, "interface down") \ |
| 29 | _(NO_SLAVE, "no slave") |
| 30 | |
| 31 | typedef enum |
| 32 | { |
| 33 | #define _(f,s) BOND_TX_ERROR_##f, |
| 34 | foreach_bond_tx_error |
| 35 | #undef _ |
| 36 | BOND_TX_N_ERROR, |
| 37 | } bond_tx_error_t; |
| 38 | |
| 39 | static char *bond_tx_error_strings[] = { |
| 40 | #define _(n,s) s, |
| 41 | foreach_bond_tx_error |
| 42 | #undef _ |
| 43 | }; |
| 44 | |
| 45 | static u8 * |
| 46 | format_bond_tx_trace (u8 * s, va_list * args) |
| 47 | { |
| 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 (); |
| 53 | |
| 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, |
| 59 | hw->name, hw1->name); |
| 60 | |
| 61 | return s; |
| 62 | } |
| 63 | |
| 64 | u8 * |
| 65 | format_bond_interface_name (u8 * s, va_list * args) |
| 66 | { |
| 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); |
| 70 | |
| 71 | s = format (s, "BondEthernet%lu", bif->dev_instance); |
| 72 | |
| 73 | return s; |
| 74 | } |
| 75 | |
| 76 | static __clib_unused clib_error_t * |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 77 | bond_set_l2_mode_function (vnet_main_t * vnm, |
| 78 | struct vnet_hw_interface_t *bif_hw, |
| 79 | i32 l2_if_adjust) |
| 80 | { |
| 81 | bond_if_t *bif; |
| 82 | u32 *sw_if_index; |
| 83 | struct vnet_hw_interface_t *sif_hw; |
| 84 | |
| 85 | bif = bond_get_master_by_sw_if_index (bif_hw->sw_if_index); |
| 86 | if (!bif) |
| 87 | return 0; |
| 88 | |
| 89 | if ((bif_hw->l2_if_count == 1) && (l2_if_adjust == 1)) |
| 90 | { |
| 91 | /* Just added first L2 interface on this port */ |
| 92 | vec_foreach (sw_if_index, bif->slaves) |
| 93 | { |
| 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); |
| 97 | |
| 98 | /* ensure all packets go to ethernet-input */ |
| 99 | ethernet_set_rx_redirect (vnm, sif_hw, 1); |
| 100 | } |
| 101 | } |
| 102 | else if ((bif_hw->l2_if_count == 0) && (l2_if_adjust == -1)) |
| 103 | { |
| 104 | /* Just removed last L2 subinterface on this port */ |
| 105 | vec_foreach (sw_if_index, bif->slaves) |
| 106 | { |
| 107 | sif_hw = vnet_get_sup_hw_interface (vnm, *sw_if_index); |
| 108 | ethernet_set_flags (vnm, sif_hw->hw_if_index, 0); |
| 109 | |
| 110 | /* Allow ip packets to go directly to ip4-input etc */ |
| 111 | ethernet_set_rx_redirect (vnm, sif_hw, 0); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static __clib_unused clib_error_t * |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 119 | bond_subif_add_del_function (vnet_main_t * vnm, u32 hw_if_index, |
| 120 | struct vnet_sw_interface_t *st, int is_add) |
| 121 | { |
| 122 | /* Nothing for now */ |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static clib_error_t * |
| 127 | bond_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 128 | { |
| 129 | vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index); |
| 130 | uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0; |
| 131 | bond_main_t *bm = &bond_main; |
| 132 | bond_if_t *bif = pool_elt_at_index (bm->interfaces, hif->dev_instance); |
| 133 | |
| 134 | bif->admin_up = is_up; |
| 135 | if (is_up && vec_len (bif->active_slaves)) |
| 136 | vnet_hw_interface_set_flags (vnm, bif->hw_if_index, |
| 137 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static inline u32 |
| 142 | bond_load_balance_broadcast (vlib_main_t * vm, vlib_node_runtime_t * node, |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 143 | bond_if_t * bif, vlib_buffer_t * b0, |
| 144 | uword slave_count) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 145 | { |
| 146 | vnet_main_t *vnm = vnet_get_main (); |
| 147 | vlib_buffer_t *c0; |
Steven | 22b5be0 | 2018-04-11 15:32:15 -0700 | [diff] [blame] | 148 | int port; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 149 | u32 *to_next = 0; |
| 150 | u32 sw_if_index; |
| 151 | vlib_frame_t *f; |
Steven | 22b5be0 | 2018-04-11 15:32:15 -0700 | [diff] [blame] | 152 | u16 thread_index = vlib_get_thread_index (); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 153 | |
Steven | 22b5be0 | 2018-04-11 15:32:15 -0700 | [diff] [blame] | 154 | for (port = 1; port < slave_count; port++) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 155 | { |
Steven | 22b5be0 | 2018-04-11 15:32:15 -0700 | [diff] [blame] | 156 | sw_if_index = *vec_elt_at_index (bif->active_slaves, port); |
| 157 | if (bif->per_thread_info[thread_index].frame[port] == 0) |
| 158 | bif->per_thread_info[thread_index].frame[port] = |
| 159 | vnet_get_frame_to_sw_interface (vnm, sw_if_index); |
| 160 | f = bif->per_thread_info[thread_index].frame[port]; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 161 | to_next = vlib_frame_vector_args (f); |
| 162 | to_next += f->n_vectors; |
| 163 | c0 = vlib_buffer_copy (vm, b0); |
| 164 | if (PREDICT_TRUE (c0 != 0)) |
| 165 | { |
| 166 | vnet_buffer (c0)->sw_if_index[VLIB_TX] = sw_if_index; |
| 167 | to_next[0] = vlib_get_buffer_index (vm, c0); |
| 168 | f->n_vectors++; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static inline u32 |
| 176 | bond_load_balance_l2 (vlib_main_t * vm, vlib_node_runtime_t * node, |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 177 | bond_if_t * bif, vlib_buffer_t * b0, uword slave_count) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 178 | { |
| 179 | ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0); |
| 180 | u32 a = 0, b = 0, c = 0, t1, t2; |
| 181 | u16 t11, t22; |
| 182 | |
| 183 | memcpy (&t1, eth->src_address, sizeof (t1)); |
| 184 | memcpy (&t11, ð->src_address[4], sizeof (t11)); |
| 185 | a = t1 ^ t11; |
| 186 | |
| 187 | memcpy (&t2, eth->dst_address, sizeof (t2)); |
| 188 | memcpy (&t22, ð->dst_address[4], sizeof (t22)); |
| 189 | b = t2 ^ t22; |
| 190 | |
| 191 | hash_v3_mix32 (a, b, c); |
| 192 | hash_v3_finalize32 (a, b, c); |
| 193 | |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 194 | return c % slave_count; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static inline u16 * |
| 198 | bond_locate_ethertype (ethernet_header_t * eth) |
| 199 | { |
| 200 | u16 *ethertype_p; |
| 201 | ethernet_vlan_header_t *vlan; |
| 202 | |
| 203 | if (!ethernet_frame_is_tagged (clib_net_to_host_u16 (eth->type))) |
| 204 | { |
| 205 | ethertype_p = ð->type; |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | vlan = (void *) (eth + 1); |
| 210 | ethertype_p = &vlan->type; |
| 211 | if (*ethertype_p == ntohs (ETHERNET_TYPE_VLAN)) |
| 212 | { |
| 213 | vlan++; |
| 214 | ethertype_p = &vlan->type; |
| 215 | } |
| 216 | } |
| 217 | return ethertype_p; |
| 218 | } |
| 219 | |
| 220 | static inline u32 |
| 221 | bond_load_balance_l23 (vlib_main_t * vm, vlib_node_runtime_t * node, |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 222 | bond_if_t * bif, vlib_buffer_t * b0, uword slave_count) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 223 | { |
| 224 | ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0); |
| 225 | u8 ip_version; |
| 226 | ip4_header_t *ip4; |
| 227 | u16 ethertype, *ethertype_p; |
| 228 | |
| 229 | ethertype_p = bond_locate_ethertype (eth); |
| 230 | ethertype = *ethertype_p; |
| 231 | |
| 232 | if ((ethertype != htons (ETHERNET_TYPE_IP4)) && |
| 233 | (ethertype != htons (ETHERNET_TYPE_IP6))) |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 234 | return (bond_load_balance_l2 (vm, node, bif, b0, slave_count)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 235 | |
| 236 | ip4 = (ip4_header_t *) (ethertype_p + 1); |
| 237 | ip_version = (ip4->ip_version_and_header_length >> 4); |
| 238 | |
| 239 | if (ip_version == 0x4) |
| 240 | { |
| 241 | u16 t11, t22; |
| 242 | u32 a = 0, b = 0, c = 0, t1, t2; |
| 243 | |
| 244 | memcpy (&t1, eth->src_address, sizeof (t1)); |
| 245 | memcpy (&t11, ð->src_address[4], sizeof (t11)); |
| 246 | a = t1 ^ t11; |
| 247 | |
| 248 | memcpy (&t2, eth->dst_address, sizeof (t2)); |
| 249 | memcpy (&t22, ð->dst_address[4], sizeof (t22)); |
| 250 | b = t2 ^ t22; |
| 251 | |
| 252 | c = ip4->src_address.data_u32 ^ ip4->dst_address.data_u32; |
| 253 | |
| 254 | hash_v3_mix32 (a, b, c); |
| 255 | hash_v3_finalize32 (a, b, c); |
| 256 | |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 257 | return c % slave_count; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 258 | } |
| 259 | else if (ip_version == 0x6) |
| 260 | { |
| 261 | u64 a, b, c; |
| 262 | u64 t1 = 0, t2 = 0; |
| 263 | ip6_header_t *ip6 = (ip6_header_t *) (eth + 1); |
| 264 | |
| 265 | memcpy (&t1, eth->src_address, sizeof (eth->src_address)); |
| 266 | memcpy (&t2, eth->dst_address, sizeof (eth->dst_address)); |
| 267 | a = t1 ^ t2; |
| 268 | |
| 269 | b = (ip6->src_address.as_u64[0] ^ ip6->src_address.as_u64[1]); |
| 270 | c = (ip6->dst_address.as_u64[0] ^ ip6->dst_address.as_u64[1]); |
| 271 | |
| 272 | hash_mix64 (a, b, c); |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 273 | return c % slave_count; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 274 | } |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 275 | return (bond_load_balance_l2 (vm, node, bif, b0, slave_count)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static inline u32 |
| 279 | bond_load_balance_l34 (vlib_main_t * vm, vlib_node_runtime_t * node, |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 280 | bond_if_t * bif, vlib_buffer_t * b0, uword slave_count) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 281 | { |
| 282 | ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0); |
| 283 | u8 ip_version; |
| 284 | uword is_tcp_udp = 0; |
| 285 | ip4_header_t *ip4; |
| 286 | u16 ethertype, *ethertype_p; |
| 287 | |
| 288 | ethertype_p = bond_locate_ethertype (eth); |
| 289 | ethertype = *ethertype_p; |
| 290 | |
| 291 | if ((ethertype != htons (ETHERNET_TYPE_IP4)) && |
| 292 | (ethertype != htons (ETHERNET_TYPE_IP6))) |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 293 | return (bond_load_balance_l2 (vm, node, bif, b0, slave_count)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 294 | |
| 295 | ip4 = (ip4_header_t *) (ethertype_p + 1); |
| 296 | ip_version = (ip4->ip_version_and_header_length >> 4); |
| 297 | |
| 298 | if (ip_version == 0x4) |
| 299 | { |
| 300 | u32 a = 0, b = 0, c = 0, t1, t2; |
| 301 | tcp_header_t *tcp = (void *) (ip4 + 1); |
| 302 | is_tcp_udp = (ip4->protocol == IP_PROTOCOL_TCP) || |
| 303 | (ip4->protocol == IP_PROTOCOL_UDP); |
| 304 | |
| 305 | a = ip4->src_address.data_u32 ^ ip4->dst_address.data_u32; |
| 306 | |
| 307 | t1 = is_tcp_udp ? tcp->src : 0; |
| 308 | t2 = is_tcp_udp ? tcp->dst : 0; |
| 309 | b = t1 + (t2 << 16); |
| 310 | |
| 311 | hash_v3_mix32 (a, b, c); |
| 312 | hash_v3_finalize32 (a, b, c); |
| 313 | |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 314 | return c % slave_count; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 315 | } |
| 316 | else if (ip_version == 0x6) |
| 317 | { |
| 318 | u64 a, b, c; |
| 319 | u64 t1, t2; |
| 320 | ip6_header_t *ip6 = (ip6_header_t *) (eth + 1); |
| 321 | tcp_header_t *tcp = (void *) (ip6 + 1); |
| 322 | |
| 323 | if (PREDICT_TRUE ((ip6->protocol == IP_PROTOCOL_TCP) || |
| 324 | (ip6->protocol == IP_PROTOCOL_UDP))) |
| 325 | { |
| 326 | is_tcp_udp = 1; |
| 327 | tcp = (void *) (ip6 + 1); |
| 328 | } |
| 329 | else if (ip6->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
| 330 | { |
| 331 | ip6_hop_by_hop_header_t *hbh = |
| 332 | (ip6_hop_by_hop_header_t *) (ip6 + 1); |
| 333 | if ((hbh->protocol == IP_PROTOCOL_TCP) |
| 334 | || (hbh->protocol == IP_PROTOCOL_UDP)) |
| 335 | { |
| 336 | is_tcp_udp = 1; |
| 337 | tcp = (tcp_header_t *) ((u8 *) hbh + ((hbh->length + 1) << 3)); |
| 338 | } |
| 339 | } |
| 340 | a = (ip6->src_address.as_u64[0] ^ ip6->src_address.as_u64[1]); |
| 341 | b = (ip6->dst_address.as_u64[0] ^ ip6->dst_address.as_u64[1]); |
| 342 | |
| 343 | t1 = is_tcp_udp ? tcp->src : 0; |
| 344 | t2 = is_tcp_udp ? tcp->dst : 0; |
| 345 | c = (t2 << 16) | t1; |
| 346 | hash_mix64 (a, b, c); |
| 347 | |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 348 | return c % slave_count; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 351 | return (bond_load_balance_l2 (vm, node, bif, b0, slave_count)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static inline u32 |
| 355 | bond_load_balance_round_robin (vlib_main_t * vm, |
| 356 | vlib_node_runtime_t * node, |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 357 | bond_if_t * bif, vlib_buffer_t * b0, |
| 358 | uword slave_count) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 359 | { |
| 360 | bif->lb_rr_last_index++; |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 361 | bif->lb_rr_last_index %= slave_count; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 362 | |
| 363 | return bif->lb_rr_last_index; |
| 364 | } |
| 365 | |
| 366 | static inline u32 |
| 367 | bond_load_balance_active_backup (vlib_main_t * vm, |
| 368 | vlib_node_runtime_t * node, |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 369 | bond_if_t * bif, vlib_buffer_t * b0, |
| 370 | uword slave_count) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 371 | { |
| 372 | /* First interface is the active, the rest is backup */ |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static bond_load_balance_func_t bond_load_balance_table[] = { |
| 377 | #define _(v,f,s, p) { bond_load_balance_##p }, |
| 378 | foreach_bond_lb_algo |
| 379 | #undef _ |
| 380 | }; |
| 381 | |
| 382 | static uword |
| 383 | bond_tx_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 384 | vlib_frame_t * frame) |
| 385 | { |
| 386 | vnet_interface_output_runtime_t *rund = (void *) node->runtime_data; |
| 387 | bond_main_t *bm = &bond_main; |
| 388 | bond_if_t *bif = pool_elt_at_index (bm->interfaces, rund->dev_instance); |
| 389 | u32 bi0, bi1, bi2, bi3; |
| 390 | vlib_buffer_t *b0, *b1, *b2, *b3; |
| 391 | u32 *from = vlib_frame_vector_args (frame); |
| 392 | u32 n_left_from; |
| 393 | ethernet_header_t *eth; |
| 394 | u32 next0 = 0, next1 = 0, next2 = 0, next3 = 0; |
| 395 | u32 port, port1, port2, port3; |
| 396 | u32 sw_if_index, sw_if_index1, sw_if_index2, sw_if_index3; |
| 397 | bond_packet_trace_t *t0; |
| 398 | uword n_trace = vlib_get_trace_count (vm, node); |
| 399 | u16 thread_index = vlib_get_thread_index (); |
| 400 | vnet_main_t *vnm = vnet_get_main (); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 401 | u32 *to_next; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 402 | u32 sif_if_index, sif_if_index1, sif_if_index2, sif_if_index3; |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 403 | vlib_frame_t *f; |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 404 | uword slave_count; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 405 | |
| 406 | if (PREDICT_FALSE (bif->admin_up == 0)) |
| 407 | { |
| 408 | vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors); |
| 409 | vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters + |
| 410 | VNET_INTERFACE_COUNTER_DROP, |
| 411 | thread_index, bif->sw_if_index, |
| 412 | frame->n_vectors); |
| 413 | vlib_error_count (vm, node->node_index, BOND_TX_ERROR_IF_DOWN, |
| 414 | frame->n_vectors); |
| 415 | return frame->n_vectors; |
| 416 | } |
| 417 | |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 418 | clib_spinlock_lock_if_init (&bif->lockp); |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 419 | slave_count = vec_len (bif->active_slaves); |
| 420 | if (PREDICT_FALSE (slave_count == 0)) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 421 | { |
| 422 | bi0 = from[0]; |
| 423 | b0 = vlib_get_buffer (vm, bi0); |
| 424 | vlib_increment_combined_counter |
| 425 | (vnet_main.interface_main.combined_sw_if_counters |
| 426 | + VNET_INTERFACE_COUNTER_TX, thread_index, bif->sw_if_index, |
| 427 | frame->n_vectors, b0->current_length); |
| 428 | |
| 429 | vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors); |
| 430 | vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters + |
| 431 | VNET_INTERFACE_COUNTER_DROP, |
| 432 | thread_index, bif->sw_if_index, |
| 433 | frame->n_vectors); |
| 434 | vlib_error_count (vm, node->node_index, BOND_TX_ERROR_NO_SLAVE, |
| 435 | frame->n_vectors); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 436 | clib_spinlock_unlock_if_init (&bif->lockp); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 437 | return frame->n_vectors; |
| 438 | } |
| 439 | |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 440 | vec_validate_aligned (bif->per_thread_info[thread_index].frame, slave_count, |
| 441 | CLIB_CACHE_LINE_BYTES); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 442 | |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 443 | /* Number of buffers / pkts */ |
| 444 | n_left_from = frame->n_vectors; |
| 445 | |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 446 | while (n_left_from > 0) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 447 | { |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 448 | while (n_left_from >= 4) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 449 | { |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 450 | // Prefetch next iteration |
| 451 | if (n_left_from >= 8) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 452 | { |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 453 | vlib_buffer_t *p4, *p5, *p6, *p7; |
| 454 | |
| 455 | p4 = vlib_get_buffer (vm, from[4]); |
| 456 | p5 = vlib_get_buffer (vm, from[5]); |
| 457 | p6 = vlib_get_buffer (vm, from[6]); |
| 458 | p7 = vlib_get_buffer (vm, from[7]); |
| 459 | |
| 460 | vlib_prefetch_buffer_header (p4, STORE); |
| 461 | vlib_prefetch_buffer_header (p5, STORE); |
| 462 | vlib_prefetch_buffer_header (p6, STORE); |
| 463 | vlib_prefetch_buffer_header (p7, STORE); |
| 464 | |
| 465 | CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, LOAD); |
| 466 | CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, LOAD); |
| 467 | CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, LOAD); |
| 468 | CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, LOAD); |
| 469 | } |
| 470 | |
| 471 | bi0 = from[0]; |
| 472 | bi1 = from[1]; |
| 473 | bi2 = from[2]; |
| 474 | bi3 = from[3]; |
| 475 | |
| 476 | b0 = vlib_get_buffer (vm, bi0); |
| 477 | b1 = vlib_get_buffer (vm, bi1); |
| 478 | b2 = vlib_get_buffer (vm, bi2); |
| 479 | b3 = vlib_get_buffer (vm, bi3); |
| 480 | |
| 481 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
| 482 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1); |
| 483 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b2); |
| 484 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b3); |
| 485 | |
| 486 | sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX]; |
| 487 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX]; |
| 488 | sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_TX]; |
| 489 | sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_TX]; |
| 490 | |
| 491 | port = |
| 492 | (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif, |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 493 | b0, slave_count); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 494 | port1 = |
| 495 | (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif, |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 496 | b1, slave_count); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 497 | port2 = |
| 498 | (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif, |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 499 | b2, slave_count); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 500 | port3 = |
| 501 | (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif, |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 502 | b3, slave_count); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 503 | |
| 504 | sif_if_index = *vec_elt_at_index (bif->active_slaves, port); |
| 505 | sif_if_index1 = *vec_elt_at_index (bif->active_slaves, port1); |
| 506 | sif_if_index2 = *vec_elt_at_index (bif->active_slaves, port2); |
| 507 | sif_if_index3 = *vec_elt_at_index (bif->active_slaves, port3); |
| 508 | |
| 509 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = sif_if_index; |
| 510 | vnet_buffer (b1)->sw_if_index[VLIB_TX] = sif_if_index1; |
| 511 | vnet_buffer (b2)->sw_if_index[VLIB_TX] = sif_if_index2; |
| 512 | vnet_buffer (b3)->sw_if_index[VLIB_TX] = sif_if_index3; |
| 513 | |
| 514 | if (bif->per_thread_info[thread_index].frame[port] == 0) |
| 515 | bif->per_thread_info[thread_index].frame[port] = |
| 516 | vnet_get_frame_to_sw_interface (vnm, sif_if_index); |
| 517 | |
| 518 | if (bif->per_thread_info[thread_index].frame[port1] == 0) |
| 519 | bif->per_thread_info[thread_index].frame[port1] = |
| 520 | vnet_get_frame_to_sw_interface (vnm, sif_if_index1); |
| 521 | |
| 522 | if (bif->per_thread_info[thread_index].frame[port2] == 0) |
| 523 | bif->per_thread_info[thread_index].frame[port2] = |
| 524 | vnet_get_frame_to_sw_interface (vnm, sif_if_index2); |
| 525 | |
| 526 | if (bif->per_thread_info[thread_index].frame[port3] == 0) |
| 527 | bif->per_thread_info[thread_index].frame[port3] = |
| 528 | vnet_get_frame_to_sw_interface (vnm, sif_if_index3); |
| 529 | |
| 530 | f = bif->per_thread_info[thread_index].frame[port]; |
| 531 | to_next = vlib_frame_vector_args (f); |
| 532 | to_next += f->n_vectors; |
| 533 | to_next[0] = vlib_get_buffer_index (vm, b0); |
| 534 | f->n_vectors++; |
| 535 | |
| 536 | f = bif->per_thread_info[thread_index].frame[port1]; |
| 537 | to_next = vlib_frame_vector_args (f); |
| 538 | to_next += f->n_vectors; |
| 539 | to_next[0] = vlib_get_buffer_index (vm, b1); |
| 540 | f->n_vectors++; |
| 541 | |
| 542 | f = bif->per_thread_info[thread_index].frame[port2]; |
| 543 | to_next = vlib_frame_vector_args (f); |
| 544 | to_next += f->n_vectors; |
| 545 | to_next[0] = vlib_get_buffer_index (vm, b2); |
| 546 | f->n_vectors++; |
| 547 | |
| 548 | f = bif->per_thread_info[thread_index].frame[port3]; |
| 549 | to_next = vlib_frame_vector_args (f); |
| 550 | to_next += f->n_vectors; |
| 551 | to_next[0] = vlib_get_buffer_index (vm, b3); |
| 552 | f->n_vectors++; |
| 553 | |
| 554 | if (PREDICT_FALSE (n_trace > 0)) |
| 555 | { |
| 556 | vlib_trace_buffer (vm, node, next0, b0, 0 /* follow_chain */ ); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 557 | vlib_set_trace_count (vm, node, --n_trace); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 558 | t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 559 | eth = (ethernet_header_t *) vlib_buffer_get_current (b0); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 560 | t0->ethernet = *eth; |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 561 | t0->sw_if_index = sw_if_index; |
| 562 | t0->bond_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX]; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 563 | |
| 564 | if (PREDICT_TRUE (n_trace > 0)) |
| 565 | { |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 566 | vlib_trace_buffer (vm, node, next1, b1, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 567 | 0 /* follow_chain */ ); |
| 568 | vlib_set_trace_count (vm, node, --n_trace); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 569 | t0 = vlib_add_trace (vm, node, b1, sizeof (*t0)); |
| 570 | eth = (ethernet_header_t *) vlib_buffer_get_current (b1); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 571 | t0->ethernet = *eth; |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 572 | t0->sw_if_index = sw_if_index1; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 573 | t0->bond_sw_if_index = |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 574 | vnet_buffer (b1)->sw_if_index[VLIB_TX]; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 575 | |
| 576 | if (PREDICT_TRUE (n_trace > 0)) |
| 577 | { |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 578 | vlib_trace_buffer (vm, node, next2, b2, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 579 | 0 /* follow_chain */ ); |
| 580 | vlib_set_trace_count (vm, node, --n_trace); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 581 | t0 = vlib_add_trace (vm, node, b2, sizeof (*t0)); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 582 | eth = |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 583 | (ethernet_header_t *) vlib_buffer_get_current (b2); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 584 | t0->ethernet = *eth; |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 585 | t0->sw_if_index = sw_if_index2; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 586 | t0->bond_sw_if_index = |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 587 | vnet_buffer (b2)->sw_if_index[VLIB_TX]; |
| 588 | |
| 589 | if (PREDICT_TRUE (n_trace > 0)) |
| 590 | { |
| 591 | vlib_trace_buffer (vm, node, next3, b3, |
| 592 | 0 /* follow_chain */ ); |
| 593 | vlib_set_trace_count (vm, node, --n_trace); |
| 594 | t0 = vlib_add_trace (vm, node, b3, sizeof (*t0)); |
| 595 | eth = |
| 596 | (ethernet_header_t *) |
| 597 | vlib_buffer_get_current (b3); |
| 598 | t0->ethernet = *eth; |
| 599 | t0->sw_if_index = sw_if_index3; |
| 600 | t0->bond_sw_if_index = |
| 601 | vnet_buffer (b3)->sw_if_index[VLIB_TX]; |
| 602 | } |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | } |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 606 | from += 4; |
| 607 | n_left_from -= 4; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 610 | while (n_left_from > 0) |
| 611 | { |
| 612 | // Prefetch next iteration |
| 613 | if (n_left_from > 1) |
| 614 | { |
| 615 | vlib_buffer_t *p2; |
| 616 | |
| 617 | p2 = vlib_get_buffer (vm, from[1]); |
| 618 | vlib_prefetch_buffer_header (p2, STORE); |
| 619 | CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD); |
| 620 | } |
| 621 | |
| 622 | bi0 = from[0]; |
| 623 | b0 = vlib_get_buffer (vm, bi0); |
| 624 | |
| 625 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
| 626 | |
| 627 | sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX]; |
| 628 | |
| 629 | port = |
| 630 | (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif, |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 631 | b0, slave_count); |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 632 | sif_if_index = *vec_elt_at_index (bif->active_slaves, port); |
| 633 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = sif_if_index; |
| 634 | if (bif->per_thread_info[thread_index].frame[port] == 0) |
| 635 | bif->per_thread_info[thread_index].frame[port] = |
| 636 | vnet_get_frame_to_sw_interface (vnm, sif_if_index); |
| 637 | f = bif->per_thread_info[thread_index].frame[port]; |
| 638 | to_next = vlib_frame_vector_args (f); |
| 639 | to_next += f->n_vectors; |
| 640 | to_next[0] = vlib_get_buffer_index (vm, b0); |
| 641 | f->n_vectors++; |
| 642 | |
| 643 | if (PREDICT_FALSE (n_trace > 0)) |
| 644 | { |
| 645 | vlib_trace_buffer (vm, node, next0, b0, 0 /* follow_chain */ ); |
| 646 | vlib_set_trace_count (vm, node, --n_trace); |
| 647 | t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 648 | eth = (ethernet_header_t *) vlib_buffer_get_current (b0); |
| 649 | t0->ethernet = *eth; |
| 650 | t0->sw_if_index = sw_if_index; |
| 651 | t0->bond_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX]; |
| 652 | } |
| 653 | |
| 654 | from += 1; |
| 655 | n_left_from -= 1; |
| 656 | } |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Steven | 18c0f22 | 2018-03-26 21:52:11 -0700 | [diff] [blame] | 659 | for (port = 0; port < slave_count; port++) |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 660 | { |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 661 | f = bif->per_thread_info[thread_index].frame[port]; |
| 662 | if (f == 0) |
| 663 | continue; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 664 | |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 665 | sw_if_index = *vec_elt_at_index (bif->active_slaves, port); |
| 666 | vnet_put_frame_to_sw_interface (vnm, sw_if_index, f); |
| 667 | bif->per_thread_info[thread_index].frame[port] = 0; |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters |
| 671 | + VNET_INTERFACE_COUNTER_TX, thread_index, |
| 672 | bif->sw_if_index, frame->n_vectors); |
| 673 | |
Steven | a005e7f | 2018-03-22 17:46:58 -0700 | [diff] [blame] | 674 | clib_spinlock_unlock_if_init (&bif->lockp); |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 675 | return frame->n_vectors; |
| 676 | } |
| 677 | |
| 678 | /* *INDENT-OFF* */ |
| 679 | VNET_DEVICE_CLASS (bond_dev_class) = { |
| 680 | .name = "bond", |
| 681 | .tx_function = bond_tx_fn, |
| 682 | .tx_function_n_errors = BOND_TX_N_ERROR, |
| 683 | .tx_function_error_strings = bond_tx_error_strings, |
| 684 | .format_device_name = format_bond_interface_name, |
Steven | 4f8863b | 2018-04-12 19:36:19 -0700 | [diff] [blame] | 685 | .set_l2_mode_function = bond_set_l2_mode_function, |
Steven | 9cd2d7a | 2017-12-20 12:43:01 -0800 | [diff] [blame] | 686 | .admin_up_down_function = bond_interface_admin_up_down, |
| 687 | .subif_add_del_function = bond_subif_add_del_function, |
| 688 | .format_tx_trace = format_bond_tx_trace, |
| 689 | }; |
| 690 | |
| 691 | VLIB_DEVICE_TX_FUNCTION_MULTIARCH (bond_dev_class, bond_tx_fn) |
| 692 | /* *INDENT-ON* */ |
| 693 | |
| 694 | /* |
| 695 | * fd.io coding-style-patch-verification: ON |
| 696 | * |
| 697 | * Local Variables: |
| 698 | * eval: (c-set-style "gnu") |
| 699 | * End: |
| 700 | */ |