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