summaryrefslogtreecommitdiffstats
path: root/src/vnet/bonding/node.h
blob: c6602ef01b969fc16cd4803b770546c9c87e5046 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef __included_vnet_bonding_node_h__
#define __included_vnet_bonding_node_h__

#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vppinfra/format.h>
#include <vppinfra/hash.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/interface.h>
#include <vnet/hash/hash.h>

#define LACP_FAST_PERIODIC_TIMER        1.0
#define LACP_SHORT_TIMOUT_TIME          (LACP_FAST_PERIODIC_TIMER * 3)
#define LACP_SLOW_PERIODIC_TIMER        30.0
#define LACP_LONG_TIMOUT_TIME           (LACP_SLOW_PERIODIC_TIMER * 3)

#ifndef MIN
#define MIN(x,y) (((x)<(y))?(x):(y))
#endif

#define BOND_MODULO_SHORTCUT(a) \
  (is_pow2 (a))

#define foreach_bond_mode	    \
  _ (1, ROUND_ROBIN, "round-robin") \
  _ (2, ACTIVE_BACKUP, "active-backup") \
  _ (3, XOR, "xor") \
  _ (4, BROADCAST, "broadcast") \
  _ (5, LACP, "lacp")

typedef enum
{
#define _(v, f, s) BOND_MODE_##f = v,
  foreach_bond_mode
#undef _
} bond_mode_t;

/* configurable load-balances */
#define foreach_bond_lb	  \
  _ (2, L23, "l23", l23)  \
  _ (1, L34 , "l34", l34) \
  _ (0, L2, "l2", l2)

/* load-balance functions implemented in bond-output */
#define foreach_bond_lb_algo			 \
  _ (0, L2, "l2", l2)                            \
  _ (1, L34 , "l34", l34)                        \
  _ (2, L23, "l23", l23)                         \
  _ (3, RR, "round-robin", round_robin)          \
  _ (4, BC, "broadcast", broadcast)              \
  _ (5, AB, "active-backup", active_backup)

typedef enum
{
#define _(v, f, s, p) BOND_LB_##f = v,
  foreach_bond_lb_algo
#undef _
} bond_load_balance_t;

typedef enum
{
  BOND_SEND_GARP_NA = 1,
} bond_send_garp_na_process_event_t;

typedef struct
{
  u32 id;
  u8 hw_addr_set;
  u8 hw_addr[6];
  u8 mode;
  u8 lb;
  u8 numa_only;
  u8 gso;
  /* return */
  u32 sw_if_index;
  int rv;
  clib_error_t *error;
} bond_create_if_args_t;

typedef struct
{
  /* member's sw_if_index */
  u32 member;
  /* bond's sw_if_index */
  u32 group;
  u8 is_passive;
  u8 is_long_timeout;
  /* return */
  int rv;
  clib_error_t *error;
} bond_add_member_args_t;

typedef struct
{
  u32 member;
  /* return */
  int rv;
  clib_error_t *error;
} bond_detach_member_args_t;

typedef struct
{
  u32 sw_if_index;
  u32 weight;
  /* return */
  int rv;
  clib_error_t *error;
} bond_set_intf_weight_args_t;

/** BOND interface details struct */
typedef struct
{
  u32 sw_if_index;
  u32 id;
  u8 interface_name[64];
  u32 mode;
  u32 lb;
  u8 numa_only;
  u32 active_members;
  u32 members;
} bond_interface_details_t;

/** member interface details struct */
typedef struct
{
  u32 sw_if_index;
  u8 interface_name[64];
  u8 is_passive;
  u8 is_long_timeout;
  u8 is_local_numa;
  u32 weight;
  u32 active_members;
} member_interface_details_t;

typedef CLIB_PACKED (struct
		     {
		     u16 system_priority;
		     u8 system[6];
		     u16 key; u16 port_priority; u16 port_number;
		     u8 state;
		     }) lacp_port_info_t;

typedef struct
{
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
  u32 buffers[VLIB_FRAME_SIZE];
  u32 n_buffers;
} bond_per_port_queue_t;

typedef struct
{
  bond_per_port_queue_t *per_port_queue;
  void **data;
} bond_per_thread_data_t;

typedef struct
{
  u8 admin_up;
  u8 mode;
  u8 lb;

  /* the last member index for the rr lb */
  u32 lb_rr_last_index;

  /* Real device instance in interface vector */
  u32 dev_instance;

  /* Interface ID being shown to user */
  u32 id;

  u32 hw_if_index;
  u32 sw_if_index;

  /* Configured members */
  u32 *members;

  /* Members that are in DISTRIBUTING state */
  u32 *active_members;

  lacp_port_info_t partner;
  lacp_port_info_t actor;
  u8 individual_aggregator;

  /* If the flag numa_only is set, it means that only members
     on local numa node works for lacp mode if have at least one,
     otherwise it works as usual. */
  u8 numa_only;
  u8 gso;

  /* How many members on local numa node are there in lacp mode? */
  word n_numa_members;

  u32 group;
  uword *port_number_bitmap;
  u8 use_custom_mac;
  u8 hw_address[6];

  clib_spinlock_t lockp;
  vnet_hash_fn_t hash_func;
} bond_if_t;

typedef struct
{
  u8 persistent_hw_address[6];

  /* neighbor's vlib software interface index */
  u32 sw_if_index;

  /* Neighbor time-to-live (usually 3s) */
  f32 ttl_in_seconds;

  /* 1 = interface is configured with long timeout (60s) */
  u8 is_long_timeout;

  /* 1 = debug is on; 0 = debug is off */
  u8 debug;

  /* tx packet template id for this neighbor */
  u8 packet_template_index;

  /* Info we actually keep about each neighbor */

  /* Jenkins hash optimization: avoid tlv scan, send short keepalive msg */
  u8 last_packet_signature_valid;
  uword last_packet_signature;

  /* last received lacp packet, for the J-hash optimization */
  u8 *last_rx_pkt;

  /* last marker packet */
  u8 *last_marker_pkt;

  /* neighbor vlib hw_if_index */
  u32 hw_if_index;

  /* weight -- valid only for active backup */
  u32 weight;

  /* actor does not initiate the protocol exchange */
  u8 is_passive;

  /* Partner port information */
  lacp_port_info_t partner;
  lacp_port_info_t partner_admin;;

  /* Actor port information */
  lacp_port_info_t actor;
  lacp_port_info_t actor_admin;

  /* Need To Transmit flag */
  u8 ntt;

  /* Link has been established and Aggregate Port is operable */
  u8 port_enabled;

  /* Initialization or reinitialization of the lacp protocol entity */
  u8 begin;

  /* Aggregation Port is operating the lacp */
  u8 lacp_enabled;

  /* MUX to indicate to the Selection Logic wait_while_timer expired */
  u8 ready_n;

  /* Selection Logic indicates al Aggregation Ports attached */
  u8 ready;

  /* Selection Logic selected an Aggregator */
  int selected;

  /* RX machine indicates an Aggregation Port in PORT_DISABLED state */
  u8 port_moved;

  /* timer used to detect whether received protocol information has expired */
  f64 current_while_timer;

  /* timer used to detect actor churn states */
  f64 actor_churn_timer;

  /* time last lacpdu was sent */
  f64 last_lacpdu_sent_time;

  /* time last lacpdu was received */
  f64 last_lacpdu_recd_time;

  /* time last marker pdu was sent */
  f64 last_marker_pdu_sent_time;

  /* time last marker pdu was received */
  f64 last_marker_pdu_recd_time;

  /* timer used to generate periodic transmission */
  f64 periodic_timer;

  /* timer used to detect partner churn states */
  f64 partner_churn_timer;

  /* provides hysteresis before performing an aggregation change */
  f64 wait_while_timer;

  /* Implemention variables, not in the spec */
  int rx_state;
  int tx_state;
  int mux_state;
  int ptx_state;

  /* actor admin key */
  u32 group;

  u32 marker_tx_id;

  u32 bif_dev_instance;

  u8 loopback_port;

  /* bond mode */
  u8 mode;

  /* good lacp pdu received */
  u64 pdu_received;

  /* bad lacp pdu received */
  u64 bad_pdu_received;

  /* pdu sent */
  u64 pdu_sent;

  /* good marker pdu received */
  u64 marker_pdu_received;

  /* bad marker pdu received */
  u64 marker_bad_pdu_received;

  /* pdu sent */
  u64 marker_pdu_sent;

  /* member is numa node */
  u8 is_local_numa;
} member_if_t;

typedef void (*lacp_enable_disable_func) (vlib_main_t * vm, bond_if_t * bif,
					  member_if_t * mif, u8 enable);

typedef struct
{
  u32 partner_state;
  u32 actor_state;
} lacp_stats_t;

typedef struct
{
  /* pool of bonding interfaces */
  bond_if_t *interfaces;

  /* record used interface IDs */
  uword *id_used;

  /* pool of member interfaces */
  member_if_t *neighbors;

  /* rapidly find a bond by vlib software interface index */
  uword *bond_by_sw_if_index;

  /* convenience variables */
  vlib_main_t *vlib_main;
  vnet_main_t *vnet_main;

  /* lacp plugin is loaded */
  u8 lacp_plugin_loaded;

  lacp_enable_disable_func lacp_enable_disable;

  uword *member_by_sw_if_index;

  bond_per_thread_data_t *per_thread_data;

  lacp_stats_t **stats;
} bond_main_t;

/* bond packet trace capture */
typedef struct
{
  ethernet_header_t ethernet;
  u32 sw_if_index;
  u32 bond_sw_if_index;
} bond_packet_trace_t;

typedef u32 (*load_balance_func) (vlib_main_t * vm,
				  vlib_node_runtime_t * node, bond_if_t * bif,
				  vlib_buffer_t * b0, uword member_count);

typedef struct
{
  load_balance_func load_balance;
} bond_load_balance_func_t;

extern vlib_node_registration_t bond_input_node;
extern vlib_node_registration_t bond_process_node;
extern vnet_device_class_t bond_dev_class;
extern bond_main_t bond_main;

void bond_disable_collecting_distributing (vlib_main_t * vm,
					   member_if_t * mif);
void bond_enable_collecting_distributing (vlib_main_t * vm,
					  member_if_t * mif);
u8 *format_bond_interface_name (u8 * s, va_list * args);

void bond_set_intf_weight (vlib_main_t * vm,
			   bond_set_intf_weight_args_t * args);
void bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args);
int bond_delete_if (vlib_main_t * vm, u32 sw_if_index);
void bond_add_member (vlib_main_t * vm, bond_add_member_args_t * args);
void bond_detach_member (vlib_main_t * vm, bond_detach_member_args_t * args);
int bond_dump_ifs (bond_interface_details_t ** out_bondids);
int bond_dump_member_ifs (member_interface_details_t ** out_memberids,
			  u32 bond_sw_if_index);

static inline uword
unformat_bond_mode (unformat_input_t * input, va_list * args)
{
  u8 *r = va_arg (*args, u8 *);

  if (0);
#define _(v, f, s) else if (unformat (input, s)) *r = BOND_MODE_##f;
  foreach_bond_mode
#undef _
    else
    return 0;

  return 1;
}

static inline u8 *
format_bond_mode (u8 * s, va_list * args)
{
  u32 i = va_arg (*args, u32);
  u8 *t = 0;

  switch (i)
    {
#define _(v, f, s) case BOND_MODE_##f: t = (u8 *) s; break;
      foreach_bond_mode
#undef _
    default:
      return format (s, "unknown");
    }
  return format (s, "%s", t);
}

static inline uword
unformat_bond_load_balance (unformat_input_t * input, va_list * args)
{
  u8 *r = va_arg (*args, u8 *);

  if (0);
#define _(v, f, s, p) else if (unformat (input, s)) *r = BOND_LB_##f;
  foreach_bond_lb
#undef _
    else
    return 0;

  return 1;
}

static inline u8 *
format_bond_load_balance (u8 * s, va_list * args)
{
  u32 i = va_arg (*args, u32);
  u8 *t = 0;

  switch (i)
    {
#define _(v, f, s, p) case BOND_LB_##f: t = (u8 *) s; break;
      foreach_bond_lb_algo
#undef _
    default:
      return format (s, "unknown");
    }
  return format (s, "%s", t);
}

static inline void
bond_register_callback (lacp_enable_disable_func func)
{
  bond_main_t *bm = &bond_main;

  bm->lacp_plugin_loaded = 1;
  bm->lacp_enable_disable = func;
}

static inline bond_if_t *
bond_get_bond_if_by_sw_if_index (u32 sw_if_index)
{
  bond_main_t *bm = &bond_main;
  uword *p;

  p = hash_get (bm->bond_by_sw_if_index, sw_if_index);
  if (!p)
    {
      return 0;
    }
  return pool_elt_at_index (bm->interfaces, p[0]);
}

static inline bond_if_t *
bond_get_bond_if_by_dev_instance (u32 dev_instance)
{
  bond_main_t *bm = &bond_main;

  return pool_elt_at_index (bm->interfaces, dev_instance);
}

static inline member_if_t *
bond_get_member_by_sw_if_index (u32 sw_if_index)
{
  bond_main_t *bm = &bond_main;
  member_if_t *mif = 0;
  uword p;

  if (sw_if_index < vec_len (bm->member_by_sw_if_index))
    {
      p = bm->member_by_sw_if_index[sw_if_index];
      if (p)
	mif = pool_elt_at_index (bm->neighbors, p >> 1);
    }

  return mif;
}

#endif /* __included_vnet_bonding_node_h__ */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */