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