blob: 1479209369a18c2a0e688a26e54262e3d9717f64 [file] [log] [blame]
Steven9cd2d7a2017-12-20 12:43:01 -08001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef __included_vnet_bonding_node_h__
16#define __included_vnet_bonding_node_h__
17
18#include <vlib/vlib.h>
19#include <vlib/unix/unix.h>
20#include <vppinfra/format.h>
21#include <vppinfra/hash.h>
22#include <vnet/ethernet/ethernet.h>
23#include <vnet/interface.h>
24
25#define LACP_FAST_PERIODIC_TIMER 1.0
26#define LACP_SHORT_TIMOUT_TIME (LACP_FAST_PERIODIC_TIMER * 3)
27#define LACP_SLOW_PERIODIC_TIMER 30.0
28#define LACP_LONG_TIMOUT_TIME (LACP_SLOW_PERIODIC_TIMER * 3)
29
30#ifndef MIN
31#define MIN(x,y) (((x)<(y))?(x):(y))
32#endif
33
Steven0d883012018-05-11 11:06:23 -070034#define BOND_MODULO_SHORTCUT(a) \
Damjan Marion69fdfee2018-10-06 14:33:18 +020035 (is_pow2 (a))
Steven0d883012018-05-11 11:06:23 -070036
Steven9cd2d7a2017-12-20 12:43:01 -080037#define foreach_bond_mode \
38 _ (1, ROUND_ROBIN, "round-robin") \
39 _ (2, ACTIVE_BACKUP, "active-backup") \
40 _ (3, XOR, "xor") \
41 _ (4, BROADCAST, "broadcast") \
42 _ (5, LACP, "lacp")
43
44typedef enum
45{
46#define _(v, f, s) BOND_MODE_##f = v,
47 foreach_bond_mode
48#undef _
49} bond_mode_t;
50
51/* configurable load-balances */
52#define foreach_bond_lb \
53 _ (2, L23, "l23", l23) \
Damjan Marion16de39e2018-09-26 10:15:41 +020054 _ (1, L34 , "l34", l34) \
Steven9cd2d7a2017-12-20 12:43:01 -080055 _ (0, L2, "l2", l2)
56
57/* load-balance functions implemented in bond-output */
58#define foreach_bond_lb_algo \
59 _ (0, L2, "l2", l2) \
Damjan Marion16de39e2018-09-26 10:15:41 +020060 _ (1, L34 , "l34", l34) \
Steven9cd2d7a2017-12-20 12:43:01 -080061 _ (2, L23, "l23", l23) \
62 _ (3, RR, "round-robin", round_robin) \
63 _ (4, BC, "broadcast", broadcast) \
64 _ (5, AB, "active-backup", active_backup)
65
66typedef enum
67{
68#define _(v, f, s, p) BOND_LB_##f = v,
69 foreach_bond_lb_algo
70#undef _
71} bond_load_balance_t;
72
BenoƮt Ganne47727c02019-02-12 13:35:08 +010073typedef enum
Steven9f781d82018-06-05 11:09:32 -070074{
75 BOND_SEND_GARP_NA = 1,
76} bond_send_garp_na_process_event_t;
77
Steven9cd2d7a2017-12-20 12:43:01 -080078typedef struct
79{
Alexander Chernavinad9d5282018-12-13 09:08:09 -050080 u32 id;
Steven9cd2d7a2017-12-20 12:43:01 -080081 u8 hw_addr_set;
82 u8 hw_addr[6];
83 u8 mode;
84 u8 lb;
Zhiyong Yang751e3f32019-06-26 05:49:14 -040085 u8 numa_only;
Steven9cd2d7a2017-12-20 12:43:01 -080086 /* return */
87 u32 sw_if_index;
88 int rv;
89 clib_error_t *error;
90} bond_create_if_args_t;
91
92typedef struct
93{
94 /* slave's sw_if_index */
95 u32 slave;
96 /* bond's sw_if_index */
97 u32 group;
98 u8 is_passive;
99 u8 is_long_timeout;
100 /* return */
101 int rv;
102 clib_error_t *error;
103} bond_enslave_args_t;
104
105typedef struct
106{
107 u32 slave;
108 /* return */
109 int rv;
110 clib_error_t *error;
111} bond_detach_slave_args_t;
112
Steven Luonga1876b82019-08-20 16:58:00 -0700113typedef struct
114{
115 u32 sw_if_index;
116 u32 weight;
117 /* return */
118 int rv;
119 clib_error_t *error;
120} bond_set_intf_weight_args_t;
121
Steven9cd2d7a2017-12-20 12:43:01 -0800122/** BOND interface details struct */
123typedef struct
124{
125 u32 sw_if_index;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500126 u32 id;
Steven9cd2d7a2017-12-20 12:43:01 -0800127 u8 interface_name[64];
128 u8 mode;
129 u8 lb;
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400130 u8 numa_only;
Steven9cd2d7a2017-12-20 12:43:01 -0800131 u32 active_slaves;
132 u32 slaves;
133} bond_interface_details_t;
134
135/** slave interface details struct */
136typedef struct
137{
138 u32 sw_if_index;
139 u8 interface_name[64];
140 u8 is_passive;
141 u8 is_long_timeout;
Steven Luonga1876b82019-08-20 16:58:00 -0700142 u8 is_local_numa;
143 u32 weight;
Steven9cd2d7a2017-12-20 12:43:01 -0800144 u32 active_slaves;
145} slave_interface_details_t;
146
147typedef CLIB_PACKED (struct
148 {
149 u16 system_priority;
150 u8 system[6];
151 u16 key; u16 port_priority; u16 port_number;
152 u8 state;
153 }) lacp_port_info_t;
154
155typedef struct
156{
Stevenc4e99c52018-09-27 20:06:26 -0700157 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
158 u32 buffers[VLIB_FRAME_SIZE];
159 u32 n_buffers;
160} bond_per_port_queue_t;
Stevena005e7f2018-03-22 17:46:58 -0700161
Stevenc4e99c52018-09-27 20:06:26 -0700162typedef struct
163{
164 bond_per_port_queue_t *per_port_queue;
165} bond_per_thread_data_t;
Stevena005e7f2018-03-22 17:46:58 -0700166
167typedef struct
168{
Steven9cd2d7a2017-12-20 12:43:01 -0800169 u8 admin_up;
170 u8 mode;
171 u8 lb;
172
173 /* the last slave index for the rr lb */
174 u32 lb_rr_last_index;
175
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500176 /* Real device instance in interface vector */
Steven9cd2d7a2017-12-20 12:43:01 -0800177 u32 dev_instance;
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500178
179 /* Interface ID being shown to user */
180 u32 id;
181
Steven9cd2d7a2017-12-20 12:43:01 -0800182 u32 hw_if_index;
183 u32 sw_if_index;
184
185 /* Configured slaves */
186 u32 *slaves;
187
188 /* Slaves that are in DISTRIBUTING state */
189 u32 *active_slaves;
190
191 /* rapidly find an active slave */
192 uword *active_slave_by_sw_if_index;
193
194 lacp_port_info_t partner;
195 lacp_port_info_t actor;
196 u8 individual_aggregator;
197
Zhiyong Yang751e3f32019-06-26 05:49:14 -0400198 /* If the flag numa_only is set, it means that only slaves
199 on local numa node works for lacp mode if have at least one,
200 otherwise it works as usual. */
201 u8 numa_only;
202
203 /* How many slaves on local numa node are there in lacp mode? */
204 word n_numa_slaves;
205
Steven9cd2d7a2017-12-20 12:43:01 -0800206 u32 group;
207 uword *port_number_bitmap;
208 u8 use_custom_mac;
209 u8 hw_address[6];
Stevena005e7f2018-03-22 17:46:58 -0700210
211 clib_spinlock_t lockp;
Steven9cd2d7a2017-12-20 12:43:01 -0800212} bond_if_t;
213
214typedef struct
215{
216 u8 persistent_hw_address[6];
217
218 /* neighbor's vlib software interface index */
219 u32 sw_if_index;
220
221 /* Neighbor time-to-live (usually 3s) */
222 f32 ttl_in_seconds;
223
224 /* 1 = interface is configured with long timeout (60s) */
225 u8 is_long_timeout;
226
227 /* 1 = debug is on; 0 = debug is off */
228 u8 debug;
229
230 /* tx packet template id for this neighbor */
231 u8 packet_template_index;
232
233 /* Info we actually keep about each neighbor */
234
235 /* Jenkins hash optimization: avoid tlv scan, send short keepalive msg */
236 u8 last_packet_signature_valid;
237 uword last_packet_signature;
238
239 /* last received lacp packet, for the J-hash optimization */
240 u8 *last_rx_pkt;
241
242 /* last marker packet */
243 u8 *last_marker_pkt;
244
245 /* neighbor vlib hw_if_index */
246 u32 hw_if_index;
247
Steven Luonga1876b82019-08-20 16:58:00 -0700248 /* weight -- valid only for active backup */
249 u32 weight;
250
Steven9cd2d7a2017-12-20 12:43:01 -0800251 /* actor does not initiate the protocol exchange */
252 u8 is_passive;
253
254 /* Partner port information */
255 lacp_port_info_t partner;
256 lacp_port_info_t partner_admin;;
257
Zhiyong Yang52c5f262019-06-13 21:14:33 -0400258 /* Actor port information */
Steven9cd2d7a2017-12-20 12:43:01 -0800259 lacp_port_info_t actor;
260 lacp_port_info_t actor_admin;
261
262 /* Need To Transmit flag */
263 u8 ntt;
264
265 /* Link has been established and Aggregate Port is operable */
266 u8 port_enabled;
267
268 /* Initialization or reinitialization of the lacp protocol entity */
269 u8 begin;
270
271 /* Aggregation Port is operating the lacp */
272 u8 lacp_enabled;
273
274 /* MUX to indicate to the Selection Logic wait_while_timer expired */
275 u8 ready_n;
276
277 /* Selection Logic indicates al Aggregation Ports attached */
278 u8 ready;
279
280 /* Selection Logic selected an Aggregator */
281 int selected;
282
283 /* RX machine indicates an Aggregation Port in PORT_DISABLED state */
284 u8 port_moved;
285
286 /* timer used to detect whether received protocol information has expired */
287 f64 current_while_timer;
288
289 /* timer used to detect actor churn states */
290 f64 actor_churn_timer;
291
292 /* time last lacpdu was sent */
Steven Luong82c5dda2019-03-05 09:38:33 -0800293 f64 last_lacpdu_sent_time;
294
295 /* time last lacpdu was received */
296 f64 last_lacpdu_recd_time;
297
298 /* time last marker pdu was sent */
299 f64 last_marker_pdu_sent_time;
300
301 /* time last marker pdu was received */
302 f64 last_marker_pdu_recd_time;
Steven9cd2d7a2017-12-20 12:43:01 -0800303
304 /* timer used to generate periodic transmission */
305 f64 periodic_timer;
306
307 /* timer used to detect partner churn states */
308 f64 partner_churn_timer;
309
310 /* provides hysteresis before performing an aggregation change */
311 f64 wait_while_timer;
312
313 /* Implemention variables, not in the spec */
314 int rx_state;
315 int tx_state;
316 int mux_state;
317 int ptx_state;
318
319 /* actor admin key */
320 u32 group;
321
322 u32 marker_tx_id;
323
324 u32 bif_dev_instance;
325
326 u8 loopback_port;
327
328 /* bond mode */
329 u8 mode;
Steven Luong82c5dda2019-03-05 09:38:33 -0800330
331 /* good lacp pdu received */
332 u64 pdu_received;
333
334 /* bad lacp pdu received */
335 u64 bad_pdu_received;
336
337 /* pdu sent */
338 u64 pdu_sent;
339
340 /* good marker pdu received */
341 u64 marker_pdu_received;
342
343 /* bad marker pdu received */
344 u64 marker_bad_pdu_received;
345
346 /* pdu sent */
347 u64 marker_pdu_sent;
Steven Luonga1876b82019-08-20 16:58:00 -0700348
349 /* slave is numa node */
350 u8 is_local_numa;
Steven9cd2d7a2017-12-20 12:43:01 -0800351} slave_if_t;
352
353typedef void (*lacp_enable_disable_func) (vlib_main_t * vm, bond_if_t * bif,
354 slave_if_t * sif, u8 enable);
355
356typedef struct
357{
358 /* pool of bonding interfaces */
359 bond_if_t *interfaces;
360
Alexander Chernavinad9d5282018-12-13 09:08:09 -0500361 /* record used interface IDs */
362 uword *id_used;
363
Stevenc4e99c52018-09-27 20:06:26 -0700364 /* pool of slave interfaces */
Steven9cd2d7a2017-12-20 12:43:01 -0800365 slave_if_t *neighbors;
366
Steven9cd2d7a2017-12-20 12:43:01 -0800367 /* rapidly find a bond by vlib software interface index */
368 uword *bond_by_sw_if_index;
369
370 /* convenience variables */
371 vlib_main_t *vlib_main;
372 vnet_main_t *vnet_main;
373
374 /* lacp plugin is loaded */
375 u8 lacp_plugin_loaded;
376
377 lacp_enable_disable_func lacp_enable_disable;
Steven0d883012018-05-11 11:06:23 -0700378
379 uword *slave_by_sw_if_index;
Stevenc4e99c52018-09-27 20:06:26 -0700380
381 bond_per_thread_data_t *per_thread_data;
Steven Luong0f09a822019-08-07 12:20:22 -0700382
383 u32 **stats;
Steven9cd2d7a2017-12-20 12:43:01 -0800384} bond_main_t;
385
386/* bond packet trace capture */
387typedef struct
388{
389 ethernet_header_t ethernet;
390 u32 sw_if_index;
391 u32 bond_sw_if_index;
392} bond_packet_trace_t;
393
394typedef u32 (*load_balance_func) (vlib_main_t * vm,
395 vlib_node_runtime_t * node, bond_if_t * bif,
Steven18c0f222018-03-26 21:52:11 -0700396 vlib_buffer_t * b0, uword slave_count);
Steven9cd2d7a2017-12-20 12:43:01 -0800397
398typedef struct
399{
400 load_balance_func load_balance;
401} bond_load_balance_func_t;
402
403extern vlib_node_registration_t bond_input_node;
Steven9f781d82018-06-05 11:09:32 -0700404extern vlib_node_registration_t bond_process_node;
Steven9cd2d7a2017-12-20 12:43:01 -0800405extern vnet_device_class_t bond_dev_class;
406extern bond_main_t bond_main;
407
408void bond_disable_collecting_distributing (vlib_main_t * vm,
409 slave_if_t * sif);
410void bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif);
411u8 *format_bond_interface_name (u8 * s, va_list * args);
412
Steven Luonga1876b82019-08-20 16:58:00 -0700413void bond_set_intf_weight (vlib_main_t * vm,
414 bond_set_intf_weight_args_t * args);
Steven9cd2d7a2017-12-20 12:43:01 -0800415void bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args);
416int bond_delete_if (vlib_main_t * vm, u32 sw_if_index);
417void bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args);
418void bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args);
419int bond_dump_ifs (bond_interface_details_t ** out_bondids);
420int bond_dump_slave_ifs (slave_interface_details_t ** out_slaveids,
421 u32 bond_sw_if_index);
422
423static inline uword
424unformat_bond_mode (unformat_input_t * input, va_list * args)
425{
426 u8 *r = va_arg (*args, u8 *);
427
428 if (0);
429#define _(v, f, s) else if (unformat (input, s)) *r = BOND_MODE_##f;
430 foreach_bond_mode
431#undef _
432 else
433 return 0;
434
435 return 1;
436}
437
438static inline u8 *
439format_bond_mode (u8 * s, va_list * args)
440{
441 u32 i = va_arg (*args, u32);
442 u8 *t = 0;
443
444 switch (i)
445 {
446#define _(v, f, s) case BOND_MODE_##f: t = (u8 *) s; break;
447 foreach_bond_mode
448#undef _
449 default:
450 return format (s, "unknown");
451 }
452 return format (s, "%s", t);
453}
454
455static inline uword
456unformat_bond_load_balance (unformat_input_t * input, va_list * args)
457{
458 u8 *r = va_arg (*args, u8 *);
459
460 if (0);
461#define _(v, f, s, p) else if (unformat (input, s)) *r = BOND_LB_##f;
462 foreach_bond_lb
463#undef _
464 else
465 return 0;
466
467 return 1;
468}
469
470static inline u8 *
471format_bond_load_balance (u8 * s, va_list * args)
472{
473 u32 i = va_arg (*args, u32);
474 u8 *t = 0;
475
476 switch (i)
477 {
478#define _(v, f, s, p) case BOND_LB_##f: t = (u8 *) s; break;
479 foreach_bond_lb_algo
480#undef _
481 default:
482 return format (s, "unknown");
483 }
484 return format (s, "%s", t);
485}
486
487static inline void
488bond_register_callback (lacp_enable_disable_func func)
489{
490 bond_main_t *bm = &bond_main;
491
492 bm->lacp_plugin_loaded = 1;
493 bm->lacp_enable_disable = func;
494}
495
496static inline bond_if_t *
497bond_get_master_by_sw_if_index (u32 sw_if_index)
498{
499 bond_main_t *bm = &bond_main;
500 uword *p;
501
502 p = hash_get (bm->bond_by_sw_if_index, sw_if_index);
503 if (!p)
504 {
505 return 0;
506 }
507 return pool_elt_at_index (bm->interfaces, p[0]);
508}
509
510static inline bond_if_t *
511bond_get_master_by_dev_instance (u32 dev_instance)
512{
513 bond_main_t *bm = &bond_main;
514
515 return pool_elt_at_index (bm->interfaces, dev_instance);
516}
517
518static inline slave_if_t *
519bond_get_slave_by_sw_if_index (u32 sw_if_index)
520{
521 bond_main_t *bm = &bond_main;
522 slave_if_t *sif = 0;
Steven0d883012018-05-11 11:06:23 -0700523 uword p;
Steven9cd2d7a2017-12-20 12:43:01 -0800524
Steven0d883012018-05-11 11:06:23 -0700525 if (sw_if_index < vec_len (bm->slave_by_sw_if_index))
Steven9cd2d7a2017-12-20 12:43:01 -0800526 {
Steven0d883012018-05-11 11:06:23 -0700527 p = bm->slave_by_sw_if_index[sw_if_index];
528 if (p)
529 sif = pool_elt_at_index (bm->neighbors, p >> 1);
Steven9cd2d7a2017-12-20 12:43:01 -0800530 }
Steven0d883012018-05-11 11:06:23 -0700531
Steven9cd2d7a2017-12-20 12:43:01 -0800532 return sif;
533}
534
535#endif /* __included_vnet_bonding_node_h__ */
536
537/*
538 * fd.io coding-style-patch-verification: ON
539 *
540 * Local Variables:
541 * eval: (c-set-style "gnu")
542 * End:
543 */