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