blob: 8ddec80850ac218c1f74eeb17560ab033ff3b4d9 [file] [log] [blame]
Steven9cd2d7a2017-12-20 12:43:01 -08001/*
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>
Steven0d883012018-05-11 11:06:23 -070025#include <vppinfra/lb_hash_hash.h>
Steven9cd2d7a2017-12-20 12:43:01 -080026
27#define foreach_bond_tx_error \
28 _(NONE, "no error") \
29 _(IF_DOWN, "interface down") \
30 _(NO_SLAVE, "no slave")
31
32typedef enum
33{
34#define _(f,s) BOND_TX_ERROR_##f,
35 foreach_bond_tx_error
36#undef _
37 BOND_TX_N_ERROR,
38} bond_tx_error_t;
39
40static char *bond_tx_error_strings[] = {
41#define _(n,s) s,
42 foreach_bond_tx_error
43#undef _
44};
45
46static u8 *
47format_bond_tx_trace (u8 * s, va_list * args)
48{
49 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
50 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
51 bond_packet_trace_t *t = va_arg (*args, bond_packet_trace_t *);
52 vnet_hw_interface_t *hw, *hw1;
53 vnet_main_t *vnm = vnet_get_main ();
54
55 hw = vnet_get_sup_hw_interface (vnm, t->sw_if_index);
56 hw1 = vnet_get_sup_hw_interface (vnm, t->bond_sw_if_index);
57 s = format (s, "src %U, dst %U, %s -> %s",
58 format_ethernet_address, t->ethernet.src_address,
59 format_ethernet_address, t->ethernet.dst_address,
60 hw->name, hw1->name);
61
62 return s;
63}
64
65u8 *
66format_bond_interface_name (u8 * s, va_list * args)
67{
68 u32 dev_instance = va_arg (*args, u32);
69 bond_main_t *bm = &bond_main;
70 bond_if_t *bif = pool_elt_at_index (bm->interfaces, dev_instance);
71
72 s = format (s, "BondEthernet%lu", bif->dev_instance);
73
74 return s;
75}
76
77static __clib_unused clib_error_t *
Steven4f8863b2018-04-12 19:36:19 -070078bond_set_l2_mode_function (vnet_main_t * vnm,
79 struct vnet_hw_interface_t *bif_hw,
80 i32 l2_if_adjust)
81{
82 bond_if_t *bif;
83 u32 *sw_if_index;
84 struct vnet_hw_interface_t *sif_hw;
85
86 bif = bond_get_master_by_sw_if_index (bif_hw->sw_if_index);
87 if (!bif)
88 return 0;
89
90 if ((bif_hw->l2_if_count == 1) && (l2_if_adjust == 1))
91 {
92 /* Just added first L2 interface on this port */
93 vec_foreach (sw_if_index, bif->slaves)
94 {
95 sif_hw = vnet_get_sup_hw_interface (vnm, *sw_if_index);
96 ethernet_set_flags (vnm, sif_hw->hw_if_index,
97 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
98
99 /* ensure all packets go to ethernet-input */
100 ethernet_set_rx_redirect (vnm, sif_hw, 1);
101 }
102 }
Steven4f8863b2018-04-12 19:36:19 -0700103
104 return 0;
105}
106
107static __clib_unused clib_error_t *
Steven9cd2d7a2017-12-20 12:43:01 -0800108bond_subif_add_del_function (vnet_main_t * vnm, u32 hw_if_index,
109 struct vnet_sw_interface_t *st, int is_add)
110{
111 /* Nothing for now */
112 return 0;
113}
114
115static clib_error_t *
116bond_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
117{
118 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
119 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
120 bond_main_t *bm = &bond_main;
121 bond_if_t *bif = pool_elt_at_index (bm->interfaces, hif->dev_instance);
122
123 bif->admin_up = is_up;
124 if (is_up && vec_len (bif->active_slaves))
125 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
126 VNET_HW_INTERFACE_FLAG_LINK_UP);
127 return 0;
128}
129
Steven0d883012018-05-11 11:06:23 -0700130static_always_inline u32
Steven9cd2d7a2017-12-20 12:43:01 -0800131bond_load_balance_broadcast (vlib_main_t * vm, vlib_node_runtime_t * node,
Steven18c0f222018-03-26 21:52:11 -0700132 bond_if_t * bif, vlib_buffer_t * b0,
133 uword slave_count)
Steven9cd2d7a2017-12-20 12:43:01 -0800134{
135 vnet_main_t *vnm = vnet_get_main ();
136 vlib_buffer_t *c0;
Steven22b5be02018-04-11 15:32:15 -0700137 int port;
Steven9cd2d7a2017-12-20 12:43:01 -0800138 u32 *to_next = 0;
139 u32 sw_if_index;
140 vlib_frame_t *f;
Steven22b5be02018-04-11 15:32:15 -0700141 u16 thread_index = vlib_get_thread_index ();
Steven9cd2d7a2017-12-20 12:43:01 -0800142
Steven22b5be02018-04-11 15:32:15 -0700143 for (port = 1; port < slave_count; port++)
Steven9cd2d7a2017-12-20 12:43:01 -0800144 {
Steven22b5be02018-04-11 15:32:15 -0700145 sw_if_index = *vec_elt_at_index (bif->active_slaves, port);
146 if (bif->per_thread_info[thread_index].frame[port] == 0)
147 bif->per_thread_info[thread_index].frame[port] =
148 vnet_get_frame_to_sw_interface (vnm, sw_if_index);
149 f = bif->per_thread_info[thread_index].frame[port];
Steven9cd2d7a2017-12-20 12:43:01 -0800150 to_next = vlib_frame_vector_args (f);
151 to_next += f->n_vectors;
152 c0 = vlib_buffer_copy (vm, b0);
153 if (PREDICT_TRUE (c0 != 0))
154 {
155 vnet_buffer (c0)->sw_if_index[VLIB_TX] = sw_if_index;
156 to_next[0] = vlib_get_buffer_index (vm, c0);
157 f->n_vectors++;
Steven9cd2d7a2017-12-20 12:43:01 -0800158 }
159 }
160
161 return 0;
162}
163
Steven0d883012018-05-11 11:06:23 -0700164static_always_inline u32
Steven9cd2d7a2017-12-20 12:43:01 -0800165bond_load_balance_l2 (vlib_main_t * vm, vlib_node_runtime_t * node,
Steven18c0f222018-03-26 21:52:11 -0700166 bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
Steven9cd2d7a2017-12-20 12:43:01 -0800167{
168 ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
Steven0d883012018-05-11 11:06:23 -0700169 u32 c;
170 u64 *dst = (u64 *) & eth->dst_address[0];
171 u64 a = clib_mem_unaligned (dst, u64);
172 u32 *src = (u32 *) & eth->src_address[2];
173 u32 b = clib_mem_unaligned (src, u32);
Steven9cd2d7a2017-12-20 12:43:01 -0800174
Steven0d883012018-05-11 11:06:23 -0700175 c = lb_hash_hash_2_tuples (a, b);
Steven9cd2d7a2017-12-20 12:43:01 -0800176
Steven0d883012018-05-11 11:06:23 -0700177 if (BOND_MODULO_SHORTCUT (slave_count))
178 return (c & (slave_count - 1));
179 else
180 return c % slave_count;
Steven9cd2d7a2017-12-20 12:43:01 -0800181}
182
Steven0d883012018-05-11 11:06:23 -0700183static_always_inline u16 *
Steven9cd2d7a2017-12-20 12:43:01 -0800184bond_locate_ethertype (ethernet_header_t * eth)
185{
186 u16 *ethertype_p;
187 ethernet_vlan_header_t *vlan;
188
189 if (!ethernet_frame_is_tagged (clib_net_to_host_u16 (eth->type)))
190 {
191 ethertype_p = &eth->type;
192 }
193 else
194 {
195 vlan = (void *) (eth + 1);
196 ethertype_p = &vlan->type;
197 if (*ethertype_p == ntohs (ETHERNET_TYPE_VLAN))
198 {
199 vlan++;
200 ethertype_p = &vlan->type;
201 }
202 }
203 return ethertype_p;
204}
205
Steven0d883012018-05-11 11:06:23 -0700206static_always_inline u32
Steven9cd2d7a2017-12-20 12:43:01 -0800207bond_load_balance_l23 (vlib_main_t * vm, vlib_node_runtime_t * node,
Steven18c0f222018-03-26 21:52:11 -0700208 bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
Steven9cd2d7a2017-12-20 12:43:01 -0800209{
210 ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
211 u8 ip_version;
212 ip4_header_t *ip4;
213 u16 ethertype, *ethertype_p;
Steven0d883012018-05-11 11:06:23 -0700214 u32 *mac1, *mac2, *mac3;
Steven9cd2d7a2017-12-20 12:43:01 -0800215
216 ethertype_p = bond_locate_ethertype (eth);
Steven0d883012018-05-11 11:06:23 -0700217 ethertype = clib_mem_unaligned (ethertype_p, u16);
Steven9cd2d7a2017-12-20 12:43:01 -0800218
219 if ((ethertype != htons (ETHERNET_TYPE_IP4)) &&
220 (ethertype != htons (ETHERNET_TYPE_IP6)))
Steven18c0f222018-03-26 21:52:11 -0700221 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
Steven9cd2d7a2017-12-20 12:43:01 -0800222
223 ip4 = (ip4_header_t *) (ethertype_p + 1);
224 ip_version = (ip4->ip_version_and_header_length >> 4);
225
226 if (ip_version == 0x4)
227 {
Steven0d883012018-05-11 11:06:23 -0700228 u32 a, c;
Steven9cd2d7a2017-12-20 12:43:01 -0800229
Steven0d883012018-05-11 11:06:23 -0700230 mac1 = (u32 *) & eth->dst_address[0];
231 mac2 = (u32 *) & eth->dst_address[4];
232 mac3 = (u32 *) & eth->src_address[2];
Steven9cd2d7a2017-12-20 12:43:01 -0800233
Steven0d883012018-05-11 11:06:23 -0700234 a = clib_mem_unaligned (mac1, u32) ^ clib_mem_unaligned (mac2, u32) ^
235 clib_mem_unaligned (mac3, u32);
236 c =
237 lb_hash_hash_2_tuples (clib_mem_unaligned (&ip4->address_pair, u64),
238 a);
239 if (BOND_MODULO_SHORTCUT (slave_count))
240 return (c & (slave_count - 1));
241 else
242 return c % slave_count;
Steven9cd2d7a2017-12-20 12:43:01 -0800243 }
244 else if (ip_version == 0x6)
245 {
Steven0d883012018-05-11 11:06:23 -0700246 u64 a;
247 u32 c;
Steven9cd2d7a2017-12-20 12:43:01 -0800248 ip6_header_t *ip6 = (ip6_header_t *) (eth + 1);
249
Steven0d883012018-05-11 11:06:23 -0700250 mac1 = (u32 *) & eth->dst_address[0];
251 mac2 = (u32 *) & eth->dst_address[4];
252 mac3 = (u32 *) & eth->src_address[2];
Steven9cd2d7a2017-12-20 12:43:01 -0800253
Steven0d883012018-05-11 11:06:23 -0700254 a = clib_mem_unaligned (mac1, u32) ^ clib_mem_unaligned (mac2, u32) ^
255 clib_mem_unaligned (mac3, u32);
256 c =
257 lb_hash_hash (clib_mem_unaligned
258 (&ip6->src_address.as_uword[0], uword),
259 clib_mem_unaligned (&ip6->src_address.as_uword[1],
260 uword),
261 clib_mem_unaligned (&ip6->dst_address.as_uword[0],
262 uword),
263 clib_mem_unaligned (&ip6->dst_address.as_uword[1],
264 uword), a);
265 if (BOND_MODULO_SHORTCUT (slave_count))
266 return (c & (slave_count - 1));
267 else
268 return c % slave_count;
Steven9cd2d7a2017-12-20 12:43:01 -0800269 }
Steven18c0f222018-03-26 21:52:11 -0700270 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
Steven9cd2d7a2017-12-20 12:43:01 -0800271}
272
Steven0d883012018-05-11 11:06:23 -0700273static_always_inline u32
Steven9cd2d7a2017-12-20 12:43:01 -0800274bond_load_balance_l34 (vlib_main_t * vm, vlib_node_runtime_t * node,
Steven18c0f222018-03-26 21:52:11 -0700275 bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
Steven9cd2d7a2017-12-20 12:43:01 -0800276{
277 ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
278 u8 ip_version;
Steven0d883012018-05-11 11:06:23 -0700279 uword is_tcp_udp;
Steven9cd2d7a2017-12-20 12:43:01 -0800280 ip4_header_t *ip4;
281 u16 ethertype, *ethertype_p;
282
283 ethertype_p = bond_locate_ethertype (eth);
Steven0d883012018-05-11 11:06:23 -0700284 ethertype = clib_mem_unaligned (ethertype_p, u16);
Steven9cd2d7a2017-12-20 12:43:01 -0800285
286 if ((ethertype != htons (ETHERNET_TYPE_IP4)) &&
287 (ethertype != htons (ETHERNET_TYPE_IP6)))
Steven18c0f222018-03-26 21:52:11 -0700288 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
Steven9cd2d7a2017-12-20 12:43:01 -0800289
290 ip4 = (ip4_header_t *) (ethertype_p + 1);
291 ip_version = (ip4->ip_version_and_header_length >> 4);
292
293 if (ip_version == 0x4)
294 {
Steven0d883012018-05-11 11:06:23 -0700295 u32 a, c, t1, t2;
Steven9cd2d7a2017-12-20 12:43:01 -0800296 tcp_header_t *tcp = (void *) (ip4 + 1);
Steven0d883012018-05-11 11:06:23 -0700297
Steven9cd2d7a2017-12-20 12:43:01 -0800298 is_tcp_udp = (ip4->protocol == IP_PROTOCOL_TCP) ||
299 (ip4->protocol == IP_PROTOCOL_UDP);
Steven0d883012018-05-11 11:06:23 -0700300 t1 = is_tcp_udp ? clib_mem_unaligned (&tcp->src, u16) : 0;
301 t2 = is_tcp_udp ? clib_mem_unaligned (&tcp->dst, u16) : 0;
302 a = t1 ^ t2;
303 c =
304 lb_hash_hash_2_tuples (clib_mem_unaligned (&ip4->address_pair, u64),
305 a);
306 if (BOND_MODULO_SHORTCUT (slave_count))
307 return (c & (slave_count - 1));
308 else
309 return c % slave_count;
Steven9cd2d7a2017-12-20 12:43:01 -0800310 }
311 else if (ip_version == 0x6)
312 {
Steven0d883012018-05-11 11:06:23 -0700313 u64 a;
314 u32 c, t1, t2;
Steven9cd2d7a2017-12-20 12:43:01 -0800315 ip6_header_t *ip6 = (ip6_header_t *) (eth + 1);
316 tcp_header_t *tcp = (void *) (ip6 + 1);
317
Steven0d883012018-05-11 11:06:23 -0700318 is_tcp_udp = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800319 if (PREDICT_TRUE ((ip6->protocol == IP_PROTOCOL_TCP) ||
320 (ip6->protocol == IP_PROTOCOL_UDP)))
321 {
322 is_tcp_udp = 1;
323 tcp = (void *) (ip6 + 1);
324 }
325 else if (ip6->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS)
326 {
327 ip6_hop_by_hop_header_t *hbh =
328 (ip6_hop_by_hop_header_t *) (ip6 + 1);
329 if ((hbh->protocol == IP_PROTOCOL_TCP)
330 || (hbh->protocol == IP_PROTOCOL_UDP))
331 {
332 is_tcp_udp = 1;
333 tcp = (tcp_header_t *) ((u8 *) hbh + ((hbh->length + 1) << 3));
334 }
335 }
Steven0d883012018-05-11 11:06:23 -0700336 t1 = is_tcp_udp ? clib_mem_unaligned (&tcp->src, u16) : 0;
337 t2 = is_tcp_udp ? clib_mem_unaligned (&tcp->dst, u16) : 0;
338 a = t1 ^ t2;
339 c =
340 lb_hash_hash (clib_mem_unaligned
341 (&ip6->src_address.as_uword[0], uword),
342 clib_mem_unaligned (&ip6->src_address.as_uword[1],
343 uword),
344 clib_mem_unaligned (&ip6->dst_address.as_uword[0],
345 uword),
346 clib_mem_unaligned (&ip6->dst_address.as_uword[1],
347 uword), a);
348 if (BOND_MODULO_SHORTCUT (slave_count))
349 return (c & (slave_count - 1));
350 else
351 return c % slave_count;
Steven9cd2d7a2017-12-20 12:43:01 -0800352 }
353
Steven18c0f222018-03-26 21:52:11 -0700354 return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
Steven9cd2d7a2017-12-20 12:43:01 -0800355}
356
Steven0d883012018-05-11 11:06:23 -0700357static_always_inline u32
Steven9cd2d7a2017-12-20 12:43:01 -0800358bond_load_balance_round_robin (vlib_main_t * vm,
359 vlib_node_runtime_t * node,
Steven18c0f222018-03-26 21:52:11 -0700360 bond_if_t * bif, vlib_buffer_t * b0,
361 uword slave_count)
Steven9cd2d7a2017-12-20 12:43:01 -0800362{
363 bif->lb_rr_last_index++;
Steven0d883012018-05-11 11:06:23 -0700364 if (BOND_MODULO_SHORTCUT (slave_count))
365 bif->lb_rr_last_index &= slave_count - 1;
366 else
367 bif->lb_rr_last_index %= slave_count;
Steven9cd2d7a2017-12-20 12:43:01 -0800368
369 return bif->lb_rr_last_index;
370}
371
Steven0d883012018-05-11 11:06:23 -0700372static_always_inline u32
Steven9cd2d7a2017-12-20 12:43:01 -0800373bond_load_balance_active_backup (vlib_main_t * vm,
374 vlib_node_runtime_t * node,
Steven18c0f222018-03-26 21:52:11 -0700375 bond_if_t * bif, vlib_buffer_t * b0,
376 uword slave_count)
Steven9cd2d7a2017-12-20 12:43:01 -0800377{
378 /* First interface is the active, the rest is backup */
379 return 0;
380}
381
382static bond_load_balance_func_t bond_load_balance_table[] = {
383#define _(v,f,s, p) { bond_load_balance_##p },
384 foreach_bond_lb_algo
385#undef _
386};
387
388static uword
389bond_tx_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
390 vlib_frame_t * frame)
391{
392 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
393 bond_main_t *bm = &bond_main;
394 bond_if_t *bif = pool_elt_at_index (bm->interfaces, rund->dev_instance);
395 u32 bi0, bi1, bi2, bi3;
396 vlib_buffer_t *b0, *b1, *b2, *b3;
397 u32 *from = vlib_frame_vector_args (frame);
398 u32 n_left_from;
399 ethernet_header_t *eth;
Steven0d883012018-05-11 11:06:23 -0700400 u32 port;
Steven9cd2d7a2017-12-20 12:43:01 -0800401 u32 sw_if_index, sw_if_index1, sw_if_index2, sw_if_index3;
402 bond_packet_trace_t *t0;
403 uword n_trace = vlib_get_trace_count (vm, node);
404 u16 thread_index = vlib_get_thread_index ();
405 vnet_main_t *vnm = vnet_get_main ();
Stevena005e7f2018-03-22 17:46:58 -0700406 u32 *to_next;
Steven9cd2d7a2017-12-20 12:43:01 -0800407 u32 sif_if_index, sif_if_index1, sif_if_index2, sif_if_index3;
Stevena005e7f2018-03-22 17:46:58 -0700408 vlib_frame_t *f;
Steven18c0f222018-03-26 21:52:11 -0700409 uword slave_count;
Steven9cd2d7a2017-12-20 12:43:01 -0800410
411 if (PREDICT_FALSE (bif->admin_up == 0))
412 {
413 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
414 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
415 VNET_INTERFACE_COUNTER_DROP,
416 thread_index, bif->sw_if_index,
417 frame->n_vectors);
418 vlib_error_count (vm, node->node_index, BOND_TX_ERROR_IF_DOWN,
419 frame->n_vectors);
420 return frame->n_vectors;
421 }
422
Stevena005e7f2018-03-22 17:46:58 -0700423 clib_spinlock_lock_if_init (&bif->lockp);
Steven18c0f222018-03-26 21:52:11 -0700424 slave_count = vec_len (bif->active_slaves);
425 if (PREDICT_FALSE (slave_count == 0))
Steven9cd2d7a2017-12-20 12:43:01 -0800426 {
427 bi0 = from[0];
428 b0 = vlib_get_buffer (vm, bi0);
429 vlib_increment_combined_counter
430 (vnet_main.interface_main.combined_sw_if_counters
431 + VNET_INTERFACE_COUNTER_TX, thread_index, bif->sw_if_index,
432 frame->n_vectors, b0->current_length);
433
434 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
435 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
436 VNET_INTERFACE_COUNTER_DROP,
437 thread_index, bif->sw_if_index,
438 frame->n_vectors);
439 vlib_error_count (vm, node->node_index, BOND_TX_ERROR_NO_SLAVE,
440 frame->n_vectors);
Stevena005e7f2018-03-22 17:46:58 -0700441 clib_spinlock_unlock_if_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -0800442 return frame->n_vectors;
443 }
444
Steven18c0f222018-03-26 21:52:11 -0700445 vec_validate_aligned (bif->per_thread_info[thread_index].frame, slave_count,
446 CLIB_CACHE_LINE_BYTES);
Stevena005e7f2018-03-22 17:46:58 -0700447
Steven9cd2d7a2017-12-20 12:43:01 -0800448 /* Number of buffers / pkts */
449 n_left_from = frame->n_vectors;
450
Stevena005e7f2018-03-22 17:46:58 -0700451 while (n_left_from > 0)
Steven9cd2d7a2017-12-20 12:43:01 -0800452 {
Stevena005e7f2018-03-22 17:46:58 -0700453 while (n_left_from >= 4)
Steven9cd2d7a2017-12-20 12:43:01 -0800454 {
Steven0d883012018-05-11 11:06:23 -0700455 u32 next0 = 0, next1 = 0, next2 = 0, next3 = 0;
456 u32 port0 = 0, port1 = 0, port2 = 0, port3 = 0;
457
Stevena005e7f2018-03-22 17:46:58 -0700458 // Prefetch next iteration
459 if (n_left_from >= 8)
Steven9cd2d7a2017-12-20 12:43:01 -0800460 {
Stevena005e7f2018-03-22 17:46:58 -0700461 vlib_buffer_t *p4, *p5, *p6, *p7;
462
463 p4 = vlib_get_buffer (vm, from[4]);
464 p5 = vlib_get_buffer (vm, from[5]);
465 p6 = vlib_get_buffer (vm, from[6]);
466 p7 = vlib_get_buffer (vm, from[7]);
467
Steven0d883012018-05-11 11:06:23 -0700468 vlib_prefetch_buffer_header (p4, LOAD);
469 vlib_prefetch_buffer_header (p5, LOAD);
470 vlib_prefetch_buffer_header (p6, LOAD);
471 vlib_prefetch_buffer_header (p7, LOAD);
Stevena005e7f2018-03-22 17:46:58 -0700472
473 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, LOAD);
474 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, LOAD);
475 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, LOAD);
476 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, LOAD);
477 }
478
479 bi0 = from[0];
480 bi1 = from[1];
481 bi2 = from[2];
482 bi3 = from[3];
483
484 b0 = vlib_get_buffer (vm, bi0);
485 b1 = vlib_get_buffer (vm, bi1);
486 b2 = vlib_get_buffer (vm, bi2);
487 b3 = vlib_get_buffer (vm, bi3);
488
489 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
490 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
491 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b2);
492 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b3);
493
494 sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
495 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
496 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
497 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
498
Steven0d883012018-05-11 11:06:23 -0700499 if (PREDICT_TRUE (slave_count != 1))
500 {
501 port0 =
502 (bond_load_balance_table[bif->lb]).load_balance (vm, node,
503 bif, b0,
504 slave_count);
505 port1 =
506 (bond_load_balance_table[bif->lb]).load_balance (vm, node,
507 bif, b1,
508 slave_count);
509 port2 =
510 (bond_load_balance_table[bif->lb]).load_balance (vm, node,
511 bif, b2,
512 slave_count);
513 port3 =
514 (bond_load_balance_table[bif->lb]).load_balance (vm, node,
515 bif, b3,
516 slave_count);
517 }
Stevena005e7f2018-03-22 17:46:58 -0700518
Steven0d883012018-05-11 11:06:23 -0700519 sif_if_index = *vec_elt_at_index (bif->active_slaves, port0);
Stevena005e7f2018-03-22 17:46:58 -0700520 sif_if_index1 = *vec_elt_at_index (bif->active_slaves, port1);
521 sif_if_index2 = *vec_elt_at_index (bif->active_slaves, port2);
522 sif_if_index3 = *vec_elt_at_index (bif->active_slaves, port3);
523
524 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sif_if_index;
525 vnet_buffer (b1)->sw_if_index[VLIB_TX] = sif_if_index1;
526 vnet_buffer (b2)->sw_if_index[VLIB_TX] = sif_if_index2;
527 vnet_buffer (b3)->sw_if_index[VLIB_TX] = sif_if_index3;
528
Steven0d883012018-05-11 11:06:23 -0700529 if (PREDICT_FALSE ((bif->per_thread_info[thread_index].frame[port0]
530 == 0)))
531 bif->per_thread_info[thread_index].frame[port0] =
Stevena005e7f2018-03-22 17:46:58 -0700532 vnet_get_frame_to_sw_interface (vnm, sif_if_index);
533
Steven0d883012018-05-11 11:06:23 -0700534 if (PREDICT_FALSE ((bif->per_thread_info[thread_index].frame[port1]
535 == 0)))
Stevena005e7f2018-03-22 17:46:58 -0700536 bif->per_thread_info[thread_index].frame[port1] =
537 vnet_get_frame_to_sw_interface (vnm, sif_if_index1);
538
Steven0d883012018-05-11 11:06:23 -0700539 if (PREDICT_FALSE ((bif->per_thread_info[thread_index].frame[port2]
540 == 0)))
Stevena005e7f2018-03-22 17:46:58 -0700541 bif->per_thread_info[thread_index].frame[port2] =
542 vnet_get_frame_to_sw_interface (vnm, sif_if_index2);
543
Steven0d883012018-05-11 11:06:23 -0700544 if (PREDICT_FALSE ((bif->per_thread_info[thread_index].frame[port3]
545 == 0)))
Stevena005e7f2018-03-22 17:46:58 -0700546 bif->per_thread_info[thread_index].frame[port3] =
547 vnet_get_frame_to_sw_interface (vnm, sif_if_index3);
548
Steven0d883012018-05-11 11:06:23 -0700549 f = bif->per_thread_info[thread_index].frame[port0];
Stevena005e7f2018-03-22 17:46:58 -0700550 to_next = vlib_frame_vector_args (f);
551 to_next += f->n_vectors;
552 to_next[0] = vlib_get_buffer_index (vm, b0);
553 f->n_vectors++;
554
555 f = bif->per_thread_info[thread_index].frame[port1];
556 to_next = vlib_frame_vector_args (f);
557 to_next += f->n_vectors;
558 to_next[0] = vlib_get_buffer_index (vm, b1);
559 f->n_vectors++;
560
561 f = bif->per_thread_info[thread_index].frame[port2];
562 to_next = vlib_frame_vector_args (f);
563 to_next += f->n_vectors;
564 to_next[0] = vlib_get_buffer_index (vm, b2);
565 f->n_vectors++;
566
567 f = bif->per_thread_info[thread_index].frame[port3];
568 to_next = vlib_frame_vector_args (f);
569 to_next += f->n_vectors;
570 to_next[0] = vlib_get_buffer_index (vm, b3);
571 f->n_vectors++;
572
573 if (PREDICT_FALSE (n_trace > 0))
574 {
575 vlib_trace_buffer (vm, node, next0, b0, 0 /* follow_chain */ );
Steven9cd2d7a2017-12-20 12:43:01 -0800576 vlib_set_trace_count (vm, node, --n_trace);
Stevena005e7f2018-03-22 17:46:58 -0700577 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
578 eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
Steven9cd2d7a2017-12-20 12:43:01 -0800579 t0->ethernet = *eth;
Stevena005e7f2018-03-22 17:46:58 -0700580 t0->sw_if_index = sw_if_index;
581 t0->bond_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Steven9cd2d7a2017-12-20 12:43:01 -0800582
583 if (PREDICT_TRUE (n_trace > 0))
584 {
Stevena005e7f2018-03-22 17:46:58 -0700585 vlib_trace_buffer (vm, node, next1, b1,
Steven9cd2d7a2017-12-20 12:43:01 -0800586 0 /* follow_chain */ );
587 vlib_set_trace_count (vm, node, --n_trace);
Stevena005e7f2018-03-22 17:46:58 -0700588 t0 = vlib_add_trace (vm, node, b1, sizeof (*t0));
589 eth = (ethernet_header_t *) vlib_buffer_get_current (b1);
Steven9cd2d7a2017-12-20 12:43:01 -0800590 t0->ethernet = *eth;
Stevena005e7f2018-03-22 17:46:58 -0700591 t0->sw_if_index = sw_if_index1;
Steven9cd2d7a2017-12-20 12:43:01 -0800592 t0->bond_sw_if_index =
Stevena005e7f2018-03-22 17:46:58 -0700593 vnet_buffer (b1)->sw_if_index[VLIB_TX];
Steven9cd2d7a2017-12-20 12:43:01 -0800594
595 if (PREDICT_TRUE (n_trace > 0))
596 {
Stevena005e7f2018-03-22 17:46:58 -0700597 vlib_trace_buffer (vm, node, next2, b2,
Steven9cd2d7a2017-12-20 12:43:01 -0800598 0 /* follow_chain */ );
599 vlib_set_trace_count (vm, node, --n_trace);
Stevena005e7f2018-03-22 17:46:58 -0700600 t0 = vlib_add_trace (vm, node, b2, sizeof (*t0));
Steven9cd2d7a2017-12-20 12:43:01 -0800601 eth =
Stevena005e7f2018-03-22 17:46:58 -0700602 (ethernet_header_t *) vlib_buffer_get_current (b2);
Steven9cd2d7a2017-12-20 12:43:01 -0800603 t0->ethernet = *eth;
Stevena005e7f2018-03-22 17:46:58 -0700604 t0->sw_if_index = sw_if_index2;
Steven9cd2d7a2017-12-20 12:43:01 -0800605 t0->bond_sw_if_index =
Stevena005e7f2018-03-22 17:46:58 -0700606 vnet_buffer (b2)->sw_if_index[VLIB_TX];
607
608 if (PREDICT_TRUE (n_trace > 0))
609 {
610 vlib_trace_buffer (vm, node, next3, b3,
611 0 /* follow_chain */ );
612 vlib_set_trace_count (vm, node, --n_trace);
613 t0 = vlib_add_trace (vm, node, b3, sizeof (*t0));
614 eth =
615 (ethernet_header_t *)
616 vlib_buffer_get_current (b3);
617 t0->ethernet = *eth;
618 t0->sw_if_index = sw_if_index3;
619 t0->bond_sw_if_index =
620 vnet_buffer (b3)->sw_if_index[VLIB_TX];
621 }
Steven9cd2d7a2017-12-20 12:43:01 -0800622 }
623 }
624 }
Stevena005e7f2018-03-22 17:46:58 -0700625 from += 4;
626 n_left_from -= 4;
Steven9cd2d7a2017-12-20 12:43:01 -0800627 }
628
Stevena005e7f2018-03-22 17:46:58 -0700629 while (n_left_from > 0)
630 {
Steven0d883012018-05-11 11:06:23 -0700631 u32 next0 = 0;
632 u32 port0 = 0;
633
Stevena005e7f2018-03-22 17:46:58 -0700634 // Prefetch next iteration
635 if (n_left_from > 1)
636 {
637 vlib_buffer_t *p2;
638
639 p2 = vlib_get_buffer (vm, from[1]);
Steven0d883012018-05-11 11:06:23 -0700640 vlib_prefetch_buffer_header (p2, LOAD);
Stevena005e7f2018-03-22 17:46:58 -0700641 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
642 }
643
644 bi0 = from[0];
645 b0 = vlib_get_buffer (vm, bi0);
646
647 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
648
649 sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
650
Steven0d883012018-05-11 11:06:23 -0700651 if (PREDICT_TRUE (slave_count != 1))
652 port0 =
653 (bond_load_balance_table[bif->lb]).load_balance (vm, node, bif,
654 b0,
655 slave_count);
656 sif_if_index = *vec_elt_at_index (bif->active_slaves, port0);
Stevena005e7f2018-03-22 17:46:58 -0700657 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sif_if_index;
Steven0d883012018-05-11 11:06:23 -0700658 if (PREDICT_FALSE
659 ((bif->per_thread_info[thread_index].frame[port0] == 0)))
660 bif->per_thread_info[thread_index].frame[port0] =
Stevena005e7f2018-03-22 17:46:58 -0700661 vnet_get_frame_to_sw_interface (vnm, sif_if_index);
Steven0d883012018-05-11 11:06:23 -0700662 f = bif->per_thread_info[thread_index].frame[port0];
Stevena005e7f2018-03-22 17:46:58 -0700663 to_next = vlib_frame_vector_args (f);
664 to_next += f->n_vectors;
665 to_next[0] = vlib_get_buffer_index (vm, b0);
666 f->n_vectors++;
667
668 if (PREDICT_FALSE (n_trace > 0))
669 {
670 vlib_trace_buffer (vm, node, next0, b0, 0 /* follow_chain */ );
671 vlib_set_trace_count (vm, node, --n_trace);
672 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
673 eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
674 t0->ethernet = *eth;
675 t0->sw_if_index = sw_if_index;
676 t0->bond_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
677 }
678
679 from += 1;
680 n_left_from -= 1;
681 }
Steven9cd2d7a2017-12-20 12:43:01 -0800682 }
683
Steven18c0f222018-03-26 21:52:11 -0700684 for (port = 0; port < slave_count; port++)
Steven9cd2d7a2017-12-20 12:43:01 -0800685 {
Stevena005e7f2018-03-22 17:46:58 -0700686 f = bif->per_thread_info[thread_index].frame[port];
687 if (f == 0)
688 continue;
Steven9cd2d7a2017-12-20 12:43:01 -0800689
Stevena005e7f2018-03-22 17:46:58 -0700690 sw_if_index = *vec_elt_at_index (bif->active_slaves, port);
691 vnet_put_frame_to_sw_interface (vnm, sw_if_index, f);
692 bif->per_thread_info[thread_index].frame[port] = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800693 }
694
695 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters
696 + VNET_INTERFACE_COUNTER_TX, thread_index,
697 bif->sw_if_index, frame->n_vectors);
698
Stevena005e7f2018-03-22 17:46:58 -0700699 clib_spinlock_unlock_if_init (&bif->lockp);
Steven9cd2d7a2017-12-20 12:43:01 -0800700 return frame->n_vectors;
701}
702
703/* *INDENT-OFF* */
704VNET_DEVICE_CLASS (bond_dev_class) = {
705 .name = "bond",
706 .tx_function = bond_tx_fn,
707 .tx_function_n_errors = BOND_TX_N_ERROR,
708 .tx_function_error_strings = bond_tx_error_strings,
709 .format_device_name = format_bond_interface_name,
Steven4f8863b2018-04-12 19:36:19 -0700710 .set_l2_mode_function = bond_set_l2_mode_function,
Steven9cd2d7a2017-12-20 12:43:01 -0800711 .admin_up_down_function = bond_interface_admin_up_down,
712 .subif_add_del_function = bond_subif_add_del_function,
713 .format_tx_trace = format_bond_tx_trace,
714};
715
716VLIB_DEVICE_TX_FUNCTION_MULTIARCH (bond_dev_class, bond_tx_fn)
717/* *INDENT-ON* */
718
719/*
720 * fd.io coding-style-patch-verification: ON
721 *
722 * Local Variables:
723 * eval: (c-set-style "gnu")
724 * End:
725 */