blob: a9c5b7ae33882d41e15f0bf4bb222167d36ec195 [file] [log] [blame]
Steven9cd2d7a2017-12-20 12:43:01 -08001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
18#define _GNU_SOURCE
19#include <stdint.h>
Neale Rannsdc617b82020-08-20 08:22:56 +000020#include <vnet/ip-neighbor/ip4_neighbor.h>
21#include <vnet/ip-neighbor/ip6_neighbor.h>
Steven Luong05a68d62021-12-03 12:05:45 -080022#include <vnet/bonding/node.h>
Steven9cd2d7a2017-12-20 12:43:01 -080023
Steven Luong8d462192021-03-03 19:03:38 -080024#define foreach_bond_tx_error \
25 _ (NONE, "no error") \
26 _ (IF_DOWN, "interface down") \
27 _ (BAD_LB_MODE, "bad load balance mode") \
28 _ (NO_MEMBER, "no member")
Steven9cd2d7a2017-12-20 12:43:01 -080029
30typedef enum
31{
32#define _(f,s) BOND_TX_ERROR_##f,
33 foreach_bond_tx_error
34#undef _
35 BOND_TX_N_ERROR,
36} bond_tx_error_t;
37
38static char *bond_tx_error_strings[] = {
39#define _(n,s) s,
40 foreach_bond_tx_error
41#undef _
42};
43
44static u8 *
45format_bond_tx_trace (u8 * s, va_list * args)
46{
47 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49 bond_packet_trace_t *t = va_arg (*args, bond_packet_trace_t *);
50 vnet_hw_interface_t *hw, *hw1;
51 vnet_main_t *vnm = vnet_get_main ();
52
53 hw = vnet_get_sup_hw_interface (vnm, t->sw_if_index);
54 hw1 = vnet_get_sup_hw_interface (vnm, t->bond_sw_if_index);
55 s = format (s, "src %U, dst %U, %s -> %s",
56 format_ethernet_address, t->ethernet.src_address,
57 format_ethernet_address, t->ethernet.dst_address,
58 hw->name, hw1->name);
59
60 return s;
61}
62
Damjan Marioncefe1342018-09-21 18:11:33 +020063#ifndef CLIB_MARCH_VARIANT
Steven9cd2d7a2017-12-20 12:43:01 -080064u8 *
65format_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
Alexander Chernavinad9d5282018-12-13 09:08:09 -050071 s = format (s, "BondEthernet%lu", bif->id);
Steven9cd2d7a2017-12-20 12:43:01 -080072
73 return s;
74}
Damjan Marioncefe1342018-09-21 18:11:33 +020075#endif
Steven9cd2d7a2017-12-20 12:43:01 -080076
77static __clib_unused clib_error_t *
Steven4f8863b2018-04-12 19:36:19 -070078bond_set_l2_mode_function (vnet_main_t * vnm,
79 struct vnet_hw_interface_t *bif_hw,
80 i32 l2_if_adjust)
81{
82 bond_if_t *bif;
83 u32 *sw_if_index;
Steven Luong4c4223e2020-07-15 08:44:54 -070084 struct vnet_hw_interface_t *mif_hw;
Steven4f8863b2018-04-12 19:36:19 -070085
Steven Luong4c4223e2020-07-15 08:44:54 -070086 bif = bond_get_bond_if_by_sw_if_index (bif_hw->sw_if_index);
Steven4f8863b2018-04-12 19:36:19 -070087 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 */
Steven Luong4c4223e2020-07-15 08:44:54 -070093 vec_foreach (sw_if_index, bif->members)
Steven4f8863b2018-04-12 19:36:19 -070094 {
Steven Luong4c4223e2020-07-15 08:44:54 -070095 mif_hw = vnet_get_sup_hw_interface (vnm, *sw_if_index);
96 ethernet_set_flags (vnm, mif_hw->hw_if_index,
Steven4f8863b2018-04-12 19:36:19 -070097 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
Steven4f8863b2018-04-12 19:36:19 -070098 }
99 }
Steven Luonga1f9ee82019-04-09 12:18:46 -0700100 else if ((bif_hw->l2_if_count == 0) && (l2_if_adjust == -1))
101 {
102 /* Just removed last L2 subinterface on this port */
Steven Luong4c4223e2020-07-15 08:44:54 -0700103 vec_foreach (sw_if_index, bif->members)
Steven Luonga1f9ee82019-04-09 12:18:46 -0700104 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700105 mif_hw = vnet_get_sup_hw_interface (vnm, *sw_if_index);
106 ethernet_set_flags (vnm, mif_hw->hw_if_index,
John Lof415a3b2020-05-14 15:02:16 -0400107 /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
Steven Luonga1f9ee82019-04-09 12:18:46 -0700108 }
109 }
Steven4f8863b2018-04-12 19:36:19 -0700110
111 return 0;
112}
113
Steven9cd2d7a2017-12-20 12:43:01 -0800114static clib_error_t *
115bond_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;
Steven Luongdc2abbe2020-07-28 12:28:03 -0700123 if (is_up)
Steven9cd2d7a2017-12-20 12:43:01 -0800124 vnet_hw_interface_set_flags (vnm, bif->hw_if_index,
125 VNET_HW_INTERFACE_FLAG_LINK_UP);
126 return 0;
127}
128
Matthew Smithe83aa452019-11-14 10:36:02 -0600129static clib_error_t *
130bond_add_del_mac_address (vnet_hw_interface_t * hi, const u8 * address,
131 u8 is_add)
132{
133 vnet_main_t *vnm = vnet_get_main ();
134 bond_if_t *bif;
135 clib_error_t *error = 0;
136 vnet_hw_interface_t *s_hi;
137 int i;
138
139
Steven Luong4c4223e2020-07-15 08:44:54 -0700140 bif = bond_get_bond_if_by_sw_if_index (hi->sw_if_index);
Matthew Smithe83aa452019-11-14 10:36:02 -0600141 if (!bif)
142 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700143 return clib_error_return (0,
144 "No bond interface found for sw_if_index %u",
Matthew Smithe83aa452019-11-14 10:36:02 -0600145 hi->sw_if_index);
146 }
147
Steven Luong4c4223e2020-07-15 08:44:54 -0700148 /* Add/del address on each member hw intf, they control the hardware */
149 vec_foreach_index (i, bif->members)
Matthew Smithe83aa452019-11-14 10:36:02 -0600150 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700151 s_hi = vnet_get_sup_hw_interface (vnm, vec_elt (bif->members, i));
Matthew Smithe83aa452019-11-14 10:36:02 -0600152 error = vnet_hw_interface_add_del_mac_address (vnm, s_hi->hw_if_index,
153 address, is_add);
154
155 if (error)
156 {
157 int j;
158
159 /* undo any that were completed before the failure */
160 for (j = i - 1; j > -1; j--)
161 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700162 s_hi = vnet_get_sup_hw_interface (vnm, vec_elt (bif->members, j));
Matthew Smithe83aa452019-11-14 10:36:02 -0600163 vnet_hw_interface_add_del_mac_address (vnm, s_hi->hw_if_index,
164 address, !(is_add));
165 }
166
167 return error;
168 }
169 }
170
171 return 0;
172}
173
Damjan Marion69fdfee2018-10-06 14:33:18 +0200174static_always_inline void
175bond_tx_add_to_queue (bond_per_thread_data_t * ptd, u32 port, u32 bi)
176{
177 u32 idx = ptd->per_port_queue[port].n_buffers++;
178 ptd->per_port_queue[port].buffers[idx] = bi;
179}
180
Steven0d883012018-05-11 11:06:23 -0700181static_always_inline u32
Steven Luong05a68d62021-12-03 12:05:45 -0800182bond_lb_broadcast (vlib_main_t *vm, bond_if_t *bif, vlib_buffer_t *b0,
183 uword n_members)
Steven9cd2d7a2017-12-20 12:43:01 -0800184{
Stevenc4e99c52018-09-27 20:06:26 -0700185 bond_main_t *bm = &bond_main;
Steven9cd2d7a2017-12-20 12:43:01 -0800186 vlib_buffer_t *c0;
Steven22b5be02018-04-11 15:32:15 -0700187 int port;
Steven9cd2d7a2017-12-20 12:43:01 -0800188 u32 sw_if_index;
Damjan Marion067cd622018-07-11 12:47:43 +0200189 u16 thread_index = vm->thread_index;
Stevenc4e99c52018-09-27 20:06:26 -0700190 bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
191 thread_index);
Steven9cd2d7a2017-12-20 12:43:01 -0800192
Steven Luong4c4223e2020-07-15 08:44:54 -0700193 for (port = 1; port < n_members; port++)
Steven9cd2d7a2017-12-20 12:43:01 -0800194 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700195 sw_if_index = *vec_elt_at_index (bif->active_members, port);
Steven9cd2d7a2017-12-20 12:43:01 -0800196 c0 = vlib_buffer_copy (vm, b0);
197 if (PREDICT_TRUE (c0 != 0))
198 {
199 vnet_buffer (c0)->sw_if_index[VLIB_TX] = sw_if_index;
Damjan Marion69fdfee2018-10-06 14:33:18 +0200200 bond_tx_add_to_queue (ptd, port, vlib_get_buffer_index (vm, c0));
Steven9cd2d7a2017-12-20 12:43:01 -0800201 }
202 }
203
204 return 0;
205}
206
Steven0d883012018-05-11 11:06:23 -0700207static_always_inline u32
Steven Luong05a68d62021-12-03 12:05:45 -0800208bond_lb_round_robin (bond_if_t *bif, vlib_buffer_t *b0, uword n_members)
Steven9cd2d7a2017-12-20 12:43:01 -0800209{
210 bif->lb_rr_last_index++;
Steven Luong4c4223e2020-07-15 08:44:54 -0700211 if (bif->lb_rr_last_index >= n_members)
Damjan Marion69fdfee2018-10-06 14:33:18 +0200212 bif->lb_rr_last_index = 0;
Steven9cd2d7a2017-12-20 12:43:01 -0800213
214 return bif->lb_rr_last_index;
215}
216
Damjan Marion16de39e2018-09-26 10:15:41 +0200217static_always_inline void
Steven Luong05a68d62021-12-03 12:05:45 -0800218bond_tx_hash (vlib_main_t *vm, bond_per_thread_data_t *ptd, bond_if_t *bif,
219 vlib_buffer_t **b, u32 *h, u32 n_left)
Steven9cd2d7a2017-12-20 12:43:01 -0800220{
Steven Luong05a68d62021-12-03 12:05:45 -0800221 u32 n_left_from = n_left;
222 void **data;
223
224 ASSERT (bif->hash_func != 0);
225
226 vec_validate_aligned (ptd->data, n_left - 1, CLIB_CACHE_LINE_BYTES);
227 data = ptd->data;
228 while (n_left >= 8)
Steven9cd2d7a2017-12-20 12:43:01 -0800229 {
Damjan Marioncefe1342018-09-21 18:11:33 +0200230 // Prefetch next iteration
Steven Luong05a68d62021-12-03 12:05:45 -0800231 vlib_prefetch_buffer_header (b[4], LOAD);
232 vlib_prefetch_buffer_header (b[5], LOAD);
233 vlib_prefetch_buffer_header (b[6], LOAD);
234 vlib_prefetch_buffer_header (b[7], LOAD);
Steven0d883012018-05-11 11:06:23 -0700235
Steven Luong05a68d62021-12-03 12:05:45 -0800236 data[0] = vlib_buffer_get_current (b[0]);
237 data[1] = vlib_buffer_get_current (b[1]);
238 data[2] = vlib_buffer_get_current (b[2]);
239 data[3] = vlib_buffer_get_current (b[3]);
Damjan Marioncefe1342018-09-21 18:11:33 +0200240
Steven Luong05a68d62021-12-03 12:05:45 -0800241 n_left -= 4;
242 b += 4;
243 data += 4;
244 }
Damjan Marioncefe1342018-09-21 18:11:33 +0200245
Steven Luong05a68d62021-12-03 12:05:45 -0800246 while (n_left > 0)
247 {
248 data[0] = vlib_buffer_get_current (b[0]);
249
250 n_left -= 1;
251 b += 1;
252 data += 1;
253 }
254
255 bif->hash_func (ptd->data, h, n_left_from);
256 vec_reset_length (ptd->data);
257}
258
259static_always_inline void
260bond_tx_no_hash (vlib_main_t *vm, bond_if_t *bif, vlib_buffer_t **b, u32 *h,
261 u32 n_left, uword n_members, u32 lb_alg)
262{
263 while (n_left >= 8)
264 {
265 // Prefetch next iteration
266 vlib_prefetch_buffer_header (b[4], LOAD);
267 vlib_prefetch_buffer_header (b[5], LOAD);
268 vlib_prefetch_buffer_header (b[6], LOAD);
269 vlib_prefetch_buffer_header (b[7], LOAD);
270
271 clib_prefetch_load (b[4]->data);
272 clib_prefetch_load (b[5]->data);
273 clib_prefetch_load (b[6]->data);
274 clib_prefetch_load (b[7]->data);
275
276 if (lb_alg == BOND_LB_RR)
Damjan Marion69fdfee2018-10-06 14:33:18 +0200277 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700278 h[0] = bond_lb_round_robin (bif, b[0], n_members);
279 h[1] = bond_lb_round_robin (bif, b[1], n_members);
280 h[2] = bond_lb_round_robin (bif, b[2], n_members);
281 h[3] = bond_lb_round_robin (bif, b[3], n_members);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200282 }
283 else if (lb_alg == BOND_LB_BC)
284 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700285 h[0] = bond_lb_broadcast (vm, bif, b[0], n_members);
286 h[1] = bond_lb_broadcast (vm, bif, b[1], n_members);
287 h[2] = bond_lb_broadcast (vm, bif, b[2], n_members);
288 h[3] = bond_lb_broadcast (vm, bif, b[3], n_members);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200289 }
290 else
291 {
292 ASSERT (0);
293 }
Stevenc4e99c52018-09-27 20:06:26 -0700294
Damjan Marioncefe1342018-09-21 18:11:33 +0200295 n_left -= 4;
296 b += 4;
Damjan Marion69fdfee2018-10-06 14:33:18 +0200297 h += 4;
Damjan Marioncefe1342018-09-21 18:11:33 +0200298 }
Steven9cd2d7a2017-12-20 12:43:01 -0800299
Damjan Marioncefe1342018-09-21 18:11:33 +0200300 while (n_left > 0)
301 {
Steven Luong05a68d62021-12-03 12:05:45 -0800302 if (bif->lb == BOND_LB_RR)
Steven Luong4c4223e2020-07-15 08:44:54 -0700303 h[0] = bond_lb_round_robin (bif, b[0], n_members);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200304 else if (bif->lb == BOND_LB_BC)
Steven Luong4c4223e2020-07-15 08:44:54 -0700305 h[0] = bond_lb_broadcast (vm, bif, b[0], n_members);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200306 else
Damjan Marion16de39e2018-09-26 10:15:41 +0200307 {
Damjan Marion69fdfee2018-10-06 14:33:18 +0200308 ASSERT (0);
Damjan Marion16de39e2018-09-26 10:15:41 +0200309 }
310
Damjan Marioncefe1342018-09-21 18:11:33 +0200311 n_left -= 1;
312 b += 1;
Steven Luongde0302c2019-10-04 14:18:37 -0700313 h += 1;
Steven9cd2d7a2017-12-20 12:43:01 -0800314 }
Damjan Marion69fdfee2018-10-06 14:33:18 +0200315}
Steven9cd2d7a2017-12-20 12:43:01 -0800316
Damjan Marion69fdfee2018-10-06 14:33:18 +0200317static_always_inline void
Steven Luong4c4223e2020-07-15 08:44:54 -0700318bond_hash_to_port (u32 * h, u32 n_left, u32 n_members,
319 int use_modulo_shortcut)
Damjan Marion69fdfee2018-10-06 14:33:18 +0200320{
Steven Luong4c4223e2020-07-15 08:44:54 -0700321 u32 mask = n_members - 1;
Damjan Marion69fdfee2018-10-06 14:33:18 +0200322
Damjan Marion69fdfee2018-10-06 14:33:18 +0200323 while (n_left > 4)
324 {
325 if (use_modulo_shortcut)
326 {
327 h[0] &= mask;
328 h[1] &= mask;
329 h[2] &= mask;
330 h[3] &= mask;
331 }
332 else
333 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700334 h[0] %= n_members;
335 h[1] %= n_members;
336 h[2] %= n_members;
337 h[3] %= n_members;
Damjan Marion69fdfee2018-10-06 14:33:18 +0200338 }
339 n_left -= 4;
340 h += 4;
341 }
342 while (n_left)
343 {
344 if (use_modulo_shortcut)
345 h[0] &= mask;
346 else
Steven Luong4c4223e2020-07-15 08:44:54 -0700347 h[0] %= n_members;
Damjan Marion69fdfee2018-10-06 14:33:18 +0200348 n_left -= 1;
349 h += 1;
350 }
351}
352
353static_always_inline void
354bond_update_sw_if_index (bond_per_thread_data_t * ptd, bond_if_t * bif,
355 u32 * bi, vlib_buffer_t ** b, u32 * data, u32 n_left,
356 int single_sw_if_index)
357{
358 u32 sw_if_index = data[0];
359 u32 *h = data;
360
Steven Luong05a68d62021-12-03 12:05:45 -0800361 while (n_left >= 8)
Damjan Marion69fdfee2018-10-06 14:33:18 +0200362 {
363 // Prefetch next iteration
Steven Luong05a68d62021-12-03 12:05:45 -0800364 vlib_prefetch_buffer_header (b[4], LOAD);
365 vlib_prefetch_buffer_header (b[5], LOAD);
366 vlib_prefetch_buffer_header (b[6], LOAD);
367 vlib_prefetch_buffer_header (b[7], LOAD);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200368
369 if (PREDICT_FALSE (single_sw_if_index))
370 {
371 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = sw_if_index;
372 vnet_buffer (b[1])->sw_if_index[VLIB_TX] = sw_if_index;
373 vnet_buffer (b[2])->sw_if_index[VLIB_TX] = sw_if_index;
374 vnet_buffer (b[3])->sw_if_index[VLIB_TX] = sw_if_index;
375
376 bond_tx_add_to_queue (ptd, 0, bi[0]);
377 bond_tx_add_to_queue (ptd, 0, bi[1]);
378 bond_tx_add_to_queue (ptd, 0, bi[2]);
379 bond_tx_add_to_queue (ptd, 0, bi[3]);
380 }
381 else
382 {
Steven Luong05a68d62021-12-03 12:05:45 -0800383 vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
384 *vec_elt_at_index (bif->active_members, h[0]);
385 vnet_buffer (b[1])->sw_if_index[VLIB_TX] =
386 *vec_elt_at_index (bif->active_members, h[1]);
387 vnet_buffer (b[2])->sw_if_index[VLIB_TX] =
388 *vec_elt_at_index (bif->active_members, h[2]);
389 vnet_buffer (b[3])->sw_if_index[VLIB_TX] =
390 *vec_elt_at_index (bif->active_members, h[3]);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200391
392 bond_tx_add_to_queue (ptd, h[0], bi[0]);
393 bond_tx_add_to_queue (ptd, h[1], bi[1]);
394 bond_tx_add_to_queue (ptd, h[2], bi[2]);
395 bond_tx_add_to_queue (ptd, h[3], bi[3]);
396 }
397
398 bi += 4;
399 h += 4;
400 b += 4;
401 n_left -= 4;
402 }
403 while (n_left)
404 {
405 if (PREDICT_FALSE (single_sw_if_index))
406 {
407 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = sw_if_index;
408 bond_tx_add_to_queue (ptd, 0, bi[0]);
409 }
410 else
411 {
Steven Luong05a68d62021-12-03 12:05:45 -0800412 vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
413 *vec_elt_at_index (bif->active_members, h[0]);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200414 bond_tx_add_to_queue (ptd, h[0], bi[0]);
415 }
416
417 bi += 1;
418 h += 1;
419 b += 1;
420 n_left -= 1;
421 }
422}
423
424static_always_inline void
425bond_tx_trace (vlib_main_t * vm, vlib_node_runtime_t * node, bond_if_t * bif,
426 vlib_buffer_t ** b, u32 n_left, u32 * h)
427{
428 uword n_trace = vlib_get_trace_count (vm, node);
429
430 while (n_trace > 0 && n_left > 0)
431 {
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200432 if (PREDICT_TRUE
433 (vlib_trace_buffer (vm, node, 0, b[0], 0 /* follow_chain */ )))
434 {
435 bond_packet_trace_t *t0;
436 ethernet_header_t *eth;
Damjan Marion69fdfee2018-10-06 14:33:18 +0200437
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200438 vlib_set_trace_count (vm, node, --n_trace);
439 t0 = vlib_add_trace (vm, node, b[0], sizeof (*t0));
440 eth = vlib_buffer_get_current (b[0]);
441 t0->ethernet = *eth;
442 t0->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
443 if (!h)
444 {
445 t0->bond_sw_if_index =
446 *vec_elt_at_index (bif->active_members, 0);
447 }
448 else
449 {
450 t0->bond_sw_if_index =
451 *vec_elt_at_index (bif->active_members, h[0]);
452 h++;
453 }
Damjan Marion69fdfee2018-10-06 14:33:18 +0200454 }
455 b++;
456 n_left--;
457 }
Damjan Marion16de39e2018-09-26 10:15:41 +0200458}
459
460VNET_DEVICE_CLASS_TX_FN (bond_dev_class) (vlib_main_t * vm,
461 vlib_node_runtime_t * node,
462 vlib_frame_t * frame)
463{
464 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
465 bond_main_t *bm = &bond_main;
466 u16 thread_index = vm->thread_index;
467 bond_if_t *bif = pool_elt_at_index (bm->interfaces, rund->dev_instance);
Steven Luong4c4223e2020-07-15 08:44:54 -0700468 uword n_members;
Damjan Marion69fdfee2018-10-06 14:33:18 +0200469 vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
470 u32 *from = vlib_frame_vector_args (frame);
471 u32 n_left = frame->n_vectors;
472 u32 hashes[VLIB_FRAME_SIZE], *h;
473 vnet_main_t *vnm = vnet_get_main ();
474 bond_per_thread_data_t *ptd = vec_elt_at_index (bm->per_thread_data,
475 thread_index);
476 u32 p, sw_if_index;
Damjan Marion16de39e2018-09-26 10:15:41 +0200477
478 if (PREDICT_FALSE (bif->admin_up == 0))
479 {
Damjan Mariona3d59862018-11-10 10:23:00 +0100480 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
Damjan Marion16de39e2018-09-26 10:15:41 +0200481 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
482 VNET_INTERFACE_COUNTER_DROP,
483 thread_index, bif->sw_if_index,
484 frame->n_vectors);
485 vlib_error_count (vm, node->node_index, BOND_TX_ERROR_IF_DOWN,
486 frame->n_vectors);
487 return frame->n_vectors;
488 }
489
Steven Luong4c4223e2020-07-15 08:44:54 -0700490 n_members = vec_len (bif->active_members);
491 if (PREDICT_FALSE (n_members == 0))
Damjan Marion16de39e2018-09-26 10:15:41 +0200492 {
Damjan Mariona3d59862018-11-10 10:23:00 +0100493 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
Damjan Marion16de39e2018-09-26 10:15:41 +0200494 vlib_increment_simple_counter (vnet_main.interface_main.sw_if_counters +
495 VNET_INTERFACE_COUNTER_DROP,
496 thread_index, bif->sw_if_index,
497 frame->n_vectors);
Steven Luong4c4223e2020-07-15 08:44:54 -0700498 vlib_error_count (vm, node->node_index, BOND_TX_ERROR_NO_MEMBER,
Damjan Marion16de39e2018-09-26 10:15:41 +0200499 frame->n_vectors);
500 return frame->n_vectors;
501 }
502
Damjan Marion69fdfee2018-10-06 14:33:18 +0200503 vlib_get_buffers (vm, from, bufs, n_left);
504
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700505 /* active-backup mode, ship everything to first sw if index */
Steven Luong4c4223e2020-07-15 08:44:54 -0700506 if ((bif->lb == BOND_LB_AB) || PREDICT_FALSE (n_members == 1))
Damjan Marion69fdfee2018-10-06 14:33:18 +0200507 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700508 sw_if_index = *vec_elt_at_index (bif->active_members, 0);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200509
510 bond_tx_trace (vm, node, bif, bufs, frame->n_vectors, 0);
511 bond_update_sw_if_index (ptd, bif, from, bufs, &sw_if_index, n_left,
512 /* single_sw_if_index */ 1);
513 goto done;
514 }
515
516 if (bif->lb == BOND_LB_BC)
517 {
Steven Luong4c4223e2020-07-15 08:44:54 -0700518 sw_if_index = *vec_elt_at_index (bif->active_members, 0);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200519
Steven Luong05a68d62021-12-03 12:05:45 -0800520 bond_tx_no_hash (vm, bif, bufs, hashes, n_left, n_members, BOND_LB_BC);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200521 bond_tx_trace (vm, node, bif, bufs, frame->n_vectors, 0);
522 bond_update_sw_if_index (ptd, bif, from, bufs, &sw_if_index, n_left,
523 /* single_sw_if_index */ 1);
524 goto done;
525 }
526
Steven Luong4c4223e2020-07-15 08:44:54 -0700527 /* if have at least one member on local numa node, only members on local numa
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400528 node will transmit pkts when bif->local_numa_only is enabled */
Steven Luong4c4223e2020-07-15 08:44:54 -0700529 if (bif->n_numa_members >= 1)
530 n_members = bif->n_numa_members;
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400531
Steven Luong05a68d62021-12-03 12:05:45 -0800532 if (bif->lb == BOND_LB_RR)
533 bond_tx_no_hash (vm, bif, bufs, hashes, n_left, n_members, BOND_LB_RR);
Damjan Marion16de39e2018-09-26 10:15:41 +0200534 else
Steven Luong05a68d62021-12-03 12:05:45 -0800535 bond_tx_hash (vm, ptd, bif, bufs, hashes, n_left);
Steven9cd2d7a2017-12-20 12:43:01 -0800536
Damjan Marion69fdfee2018-10-06 14:33:18 +0200537 /* calculate port out of hash */
538 h = hashes;
Steven Luong4c4223e2020-07-15 08:44:54 -0700539 if (BOND_MODULO_SHORTCUT (n_members))
540 bond_hash_to_port (h, frame->n_vectors, n_members, 1);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200541 else
Steven Luong4c4223e2020-07-15 08:44:54 -0700542 bond_hash_to_port (h, frame->n_vectors, n_members, 0);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200543
544 bond_tx_trace (vm, node, bif, bufs, frame->n_vectors, h);
545
546 bond_update_sw_if_index (ptd, bif, from, bufs, hashes, frame->n_vectors,
547 /* single_sw_if_index */ 0);
548
549done:
Steven Luong4c4223e2020-07-15 08:44:54 -0700550 for (p = 0; p < n_members; p++)
Damjan Marion69fdfee2018-10-06 14:33:18 +0200551 {
552 vlib_frame_t *f;
553 u32 *to_next;
554
Steven Luong4c4223e2020-07-15 08:44:54 -0700555 sw_if_index = *vec_elt_at_index (bif->active_members, p);
Damjan Marion69fdfee2018-10-06 14:33:18 +0200556 if (PREDICT_TRUE (ptd->per_port_queue[p].n_buffers))
557 {
558 f = vnet_get_frame_to_sw_interface (vnm, sw_if_index);
559 f->n_vectors = ptd->per_port_queue[p].n_buffers;
560 to_next = vlib_frame_vector_args (f);
Dave Barach178cf492018-11-13 16:34:13 -0500561 clib_memcpy_fast (to_next, ptd->per_port_queue[p].buffers,
562 f->n_vectors * sizeof (u32));
Damjan Marion69fdfee2018-10-06 14:33:18 +0200563 vnet_put_frame_to_sw_interface (vnm, sw_if_index, f);
564 ptd->per_port_queue[p].n_buffers = 0;
565 }
566 }
Steven9cd2d7a2017-12-20 12:43:01 -0800567 return frame->n_vectors;
568}
569
Steven9f781d82018-06-05 11:09:32 -0700570static walk_rc_t
571bond_active_interface_switch_cb (vnet_main_t * vnm, u32 sw_if_index,
572 void *arg)
573{
574 bond_main_t *bm = &bond_main;
575
Neale Rannsfd2417b2021-07-16 14:00:16 +0000576 ip4_neighbor_advertise (bm->vlib_main, bm->vnet_main, sw_if_index,
577 vlib_get_thread_index (), NULL);
578 ip6_neighbor_advertise (bm->vlib_main, bm->vnet_main, sw_if_index,
579 vlib_get_thread_index (), NULL);
Steven9f781d82018-06-05 11:09:32 -0700580
581 return (WALK_CONTINUE);
582}
583
584static uword
585bond_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
586{
587 vnet_main_t *vnm = vnet_get_main ();
588 uword event_type, *event_data = 0;
589
590 while (1)
591 {
592 u32 i;
593 u32 hw_if_index;
594
595 vlib_process_wait_for_event (vm);
596 event_type = vlib_process_get_events (vm, &event_data);
597 ASSERT (event_type == BOND_SEND_GARP_NA);
598 for (i = 0; i < vec_len (event_data); i++)
599 {
600 hw_if_index = event_data[i];
Steven Luongbac326c2019-08-05 09:47:58 -0700601 if (vnet_get_hw_interface_or_null (vnm, hw_if_index))
602 /* walk hw interface to process all subinterfaces */
603 vnet_hw_interface_walk_sw (vnm, hw_if_index,
604 bond_active_interface_switch_cb, 0);
Steven9f781d82018-06-05 11:09:32 -0700605 }
606 vec_reset_length (event_data);
607 }
608 return 0;
609}
610
611/* *INDENT-OFF* */
612VLIB_REGISTER_NODE (bond_process_node) = {
613 .function = bond_process,
Damjan Marion7ca5aaa2019-09-24 18:10:49 +0200614 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Steven9f781d82018-06-05 11:09:32 -0700615 .type = VLIB_NODE_TYPE_PROCESS,
616 .name = "bond-process",
617};
618/* *INDENT-ON* */
619
Steven9cd2d7a2017-12-20 12:43:01 -0800620/* *INDENT-OFF* */
621VNET_DEVICE_CLASS (bond_dev_class) = {
622 .name = "bond",
Steven9cd2d7a2017-12-20 12:43:01 -0800623 .tx_function_n_errors = BOND_TX_N_ERROR,
624 .tx_function_error_strings = bond_tx_error_strings,
625 .format_device_name = format_bond_interface_name,
Steven4f8863b2018-04-12 19:36:19 -0700626 .set_l2_mode_function = bond_set_l2_mode_function,
Steven9cd2d7a2017-12-20 12:43:01 -0800627 .admin_up_down_function = bond_interface_admin_up_down,
Steven9cd2d7a2017-12-20 12:43:01 -0800628 .format_tx_trace = format_bond_tx_trace,
Matthew Smithe83aa452019-11-14 10:36:02 -0600629 .mac_addr_add_del_function = bond_add_del_mac_address,
Steven9cd2d7a2017-12-20 12:43:01 -0800630};
631
Steven9cd2d7a2017-12-20 12:43:01 -0800632/* *INDENT-ON* */
633
Steven Luongbac326c2019-08-05 09:47:58 -0700634static clib_error_t *
Steven Luong4c4223e2020-07-15 08:44:54 -0700635bond_member_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
Steven Luongbac326c2019-08-05 09:47:58 -0700636{
637 bond_main_t *bm = &bond_main;
Steven Luong4c4223e2020-07-15 08:44:54 -0700638 member_if_t *mif;
639 bond_detach_member_args_t args = { 0 };
Steven Luongbac326c2019-08-05 09:47:58 -0700640
641 if (is_add)
642 return 0;
Steven Luong4c4223e2020-07-15 08:44:54 -0700643 mif = bond_get_member_by_sw_if_index (sw_if_index);
644 if (!mif)
Steven Luongbac326c2019-08-05 09:47:58 -0700645 return 0;
Steven Luong4c4223e2020-07-15 08:44:54 -0700646 args.member = sw_if_index;
647 bond_detach_member (bm->vlib_main, &args);
Steven Luongbac326c2019-08-05 09:47:58 -0700648 return args.error;
649}
650
Steven Luong4c4223e2020-07-15 08:44:54 -0700651VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bond_member_interface_add_del);
Steven Luongbac326c2019-08-05 09:47:58 -0700652
Steven9cd2d7a2017-12-20 12:43:01 -0800653/*
654 * fd.io coding-style-patch-verification: ON
655 *
656 * Local Variables:
657 * eval: (c-set-style "gnu")
658 * End:
659 */