bond: performance harvesting
[vpp.git] / src / vnet / bonding / device.c
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 #include <vppinfra/lb_hash_hash.h>
26
27 #define foreach_bond_tx_error     \
28   _(NONE, "no error")             \
29   _(IF_DOWN, "interface down")    \
30   _(NO_SLAVE, "no slave")
31
32 typedef 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
40 static char *bond_tx_error_strings[] = {
41 #define _(n,s) s,
42   foreach_bond_tx_error
43 #undef _
44 };
45
46 static u8 *
47 format_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
65 u8 *
66 format_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
77 static __clib_unused clib_error_t *
78 bond_set_l2_mode_function (vnet_main_t * vnm,
79                            struct vnet_hw_interface_t *bif_hw,
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     }
103
104   return 0;
105 }
106
107 static __clib_unused clib_error_t *
108 bond_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
115 static clib_error_t *
116 bond_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
130 static_always_inline u32
131 bond_load_balance_broadcast (vlib_main_t * vm, vlib_node_runtime_t * node,
132                              bond_if_t * bif, vlib_buffer_t * b0,
133                              uword slave_count)
134 {
135   vnet_main_t *vnm = vnet_get_main ();
136   vlib_buffer_t *c0;
137   int port;
138   u32 *to_next = 0;
139   u32 sw_if_index;
140   vlib_frame_t *f;
141   u16 thread_index = vlib_get_thread_index ();
142
143   for (port = 1; port < slave_count; port++)
144     {
145       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];
150       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++;
158         }
159     }
160
161   return 0;
162 }
163
164 static_always_inline u32
165 bond_load_balance_l2 (vlib_main_t * vm, vlib_node_runtime_t * node,
166                       bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
167 {
168   ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
169   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);
174
175   c = lb_hash_hash_2_tuples (a, b);
176
177   if (BOND_MODULO_SHORTCUT (slave_count))
178     return (c & (slave_count - 1));
179   else
180     return c % slave_count;
181 }
182
183 static_always_inline u16 *
184 bond_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
206 static_always_inline u32
207 bond_load_balance_l23 (vlib_main_t * vm, vlib_node_runtime_t * node,
208                        bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
209 {
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;
214   u32 *mac1, *mac2, *mac3;
215
216   ethertype_p = bond_locate_ethertype (eth);
217   ethertype = clib_mem_unaligned (ethertype_p, u16);
218
219   if ((ethertype != htons (ETHERNET_TYPE_IP4)) &&
220       (ethertype != htons (ETHERNET_TYPE_IP6)))
221     return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
222
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     {
228       u32 a, c;
229
230       mac1 = (u32 *) & eth->dst_address[0];
231       mac2 = (u32 *) & eth->dst_address[4];
232       mac3 = (u32 *) & eth->src_address[2];
233
234       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;
243     }
244   else if (ip_version == 0x6)
245     {
246       u64 a;
247       u32 c;
248       ip6_header_t *ip6 = (ip6_header_t *) (eth + 1);
249
250       mac1 = (u32 *) & eth->dst_address[0];
251       mac2 = (u32 *) & eth->dst_address[4];
252       mac3 = (u32 *) & eth->src_address[2];
253
254       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;
269     }
270   return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
271 }
272
273 static_always_inline u32
274 bond_load_balance_l34 (vlib_main_t * vm, vlib_node_runtime_t * node,
275                        bond_if_t * bif, vlib_buffer_t * b0, uword slave_count)
276 {
277   ethernet_header_t *eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
278   u8 ip_version;
279   uword is_tcp_udp;
280   ip4_header_t *ip4;
281   u16 ethertype, *ethertype_p;
282
283   ethertype_p = bond_locate_ethertype (eth);
284   ethertype = clib_mem_unaligned (ethertype_p, u16);
285
286   if ((ethertype != htons (ETHERNET_TYPE_IP4)) &&
287       (ethertype != htons (ETHERNET_TYPE_IP6)))
288     return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
289
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     {
295       u32 a, c, t1, t2;
296       tcp_header_t *tcp = (void *) (ip4 + 1);
297
298       is_tcp_udp = (ip4->protocol == IP_PROTOCOL_TCP) ||
299         (ip4->protocol == IP_PROTOCOL_UDP);
300       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;
310     }
311   else if (ip_version == 0x6)
312     {
313       u64 a;
314       u32 c, t1, t2;
315       ip6_header_t *ip6 = (ip6_header_t *) (eth + 1);
316       tcp_header_t *tcp = (void *) (ip6 + 1);
317
318       is_tcp_udp = 0;
319       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         }
336       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;
352     }
353
354   return (bond_load_balance_l2 (vm, node, bif, b0, slave_count));
355 }
356
357 static_always_inline u32
358 bond_load_balance_round_robin (vlib_main_t * vm,
359                                vlib_node_runtime_t * node,
360                                bond_if_t * bif, vlib_buffer_t * b0,
361                                uword slave_count)
362 {
363   bif->lb_rr_last_index++;
364   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;
368
369   return bif->lb_rr_last_index;
370 }
371
372 static_always_inline u32
373 bond_load_balance_active_backup (vlib_main_t * vm,
374                                  vlib_node_runtime_t * node,
375                                  bond_if_t * bif, vlib_buffer_t * b0,
376                                  uword slave_count)
377 {
378   /* First interface is the active, the rest is backup */
379   return 0;
380 }
381
382 static 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
388 static uword
389 bond_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;
400   u32 port;
401   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 ();
406   u32 *to_next;
407   u32 sif_if_index, sif_if_index1, sif_if_index2, sif_if_index3;
408   vlib_frame_t *f;
409   uword slave_count;
410
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
423   clib_spinlock_lock_if_init (&bif->lockp);
424   slave_count = vec_len (bif->active_slaves);
425   if (PREDICT_FALSE (slave_count == 0))
426     {
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);
441       clib_spinlock_unlock_if_init (&bif->lockp);
442       return frame->n_vectors;
443     }
444
445   vec_validate_aligned (bif->per_thread_info[thread_index].frame, slave_count,
446                         CLIB_CACHE_LINE_BYTES);
447
448   /* Number of buffers / pkts */
449   n_left_from = frame->n_vectors;
450
451   while (n_left_from > 0)
452     {
453       while (n_left_from >= 4)
454         {
455           u32 next0 = 0, next1 = 0, next2 = 0, next3 = 0;
456           u32 port0 = 0, port1 = 0, port2 = 0, port3 = 0;
457
458           // Prefetch next iteration
459           if (n_left_from >= 8)
460             {
461               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
468               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);
472
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
499           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             }
518
519           sif_if_index = *vec_elt_at_index (bif->active_slaves, port0);
520           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
529           if (PREDICT_FALSE ((bif->per_thread_info[thread_index].frame[port0]
530                               == 0)))
531             bif->per_thread_info[thread_index].frame[port0] =
532               vnet_get_frame_to_sw_interface (vnm, sif_if_index);
533
534           if (PREDICT_FALSE ((bif->per_thread_info[thread_index].frame[port1]
535                               == 0)))
536             bif->per_thread_info[thread_index].frame[port1] =
537               vnet_get_frame_to_sw_interface (vnm, sif_if_index1);
538
539           if (PREDICT_FALSE ((bif->per_thread_info[thread_index].frame[port2]
540                               == 0)))
541             bif->per_thread_info[thread_index].frame[port2] =
542               vnet_get_frame_to_sw_interface (vnm, sif_if_index2);
543
544           if (PREDICT_FALSE ((bif->per_thread_info[thread_index].frame[port3]
545                               == 0)))
546             bif->per_thread_info[thread_index].frame[port3] =
547               vnet_get_frame_to_sw_interface (vnm, sif_if_index3);
548
549           f = bif->per_thread_info[thread_index].frame[port0];
550           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 */ );
576               vlib_set_trace_count (vm, node, --n_trace);
577               t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
578               eth = (ethernet_header_t *) vlib_buffer_get_current (b0);
579               t0->ethernet = *eth;
580               t0->sw_if_index = sw_if_index;
581               t0->bond_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
582
583               if (PREDICT_TRUE (n_trace > 0))
584                 {
585                   vlib_trace_buffer (vm, node, next1, b1,
586                                      0 /* follow_chain */ );
587                   vlib_set_trace_count (vm, node, --n_trace);
588                   t0 = vlib_add_trace (vm, node, b1, sizeof (*t0));
589                   eth = (ethernet_header_t *) vlib_buffer_get_current (b1);
590                   t0->ethernet = *eth;
591                   t0->sw_if_index = sw_if_index1;
592                   t0->bond_sw_if_index =
593                     vnet_buffer (b1)->sw_if_index[VLIB_TX];
594
595                   if (PREDICT_TRUE (n_trace > 0))
596                     {
597                       vlib_trace_buffer (vm, node, next2, b2,
598                                          0 /* follow_chain */ );
599                       vlib_set_trace_count (vm, node, --n_trace);
600                       t0 = vlib_add_trace (vm, node, b2, sizeof (*t0));
601                       eth =
602                         (ethernet_header_t *) vlib_buffer_get_current (b2);
603                       t0->ethernet = *eth;
604                       t0->sw_if_index = sw_if_index2;
605                       t0->bond_sw_if_index =
606                         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                         }
622                     }
623                 }
624             }
625           from += 4;
626           n_left_from -= 4;
627         }
628
629       while (n_left_from > 0)
630         {
631           u32 next0 = 0;
632           u32 port0 = 0;
633
634           // Prefetch next iteration
635           if (n_left_from > 1)
636             {
637               vlib_buffer_t *p2;
638
639               p2 = vlib_get_buffer (vm, from[1]);
640               vlib_prefetch_buffer_header (p2, LOAD);
641               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
651           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);
657           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sif_if_index;
658           if (PREDICT_FALSE
659               ((bif->per_thread_info[thread_index].frame[port0] == 0)))
660             bif->per_thread_info[thread_index].frame[port0] =
661               vnet_get_frame_to_sw_interface (vnm, sif_if_index);
662           f = bif->per_thread_info[thread_index].frame[port0];
663           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         }
682     }
683
684   for (port = 0; port < slave_count; port++)
685     {
686       f = bif->per_thread_info[thread_index].frame[port];
687       if (f == 0)
688         continue;
689
690       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;
693     }
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
699   clib_spinlock_unlock_if_init (&bif->lockp);
700   return frame->n_vectors;
701 }
702
703 /* *INDENT-OFF* */
704 VNET_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,
710   .set_l2_mode_function = bond_set_l2_mode_function,
711   .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
716 VLIB_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  */