b9504424ee8c081cb49bd10d4cbf9f2e0699f1eb
[vpp.git] / src / vnet / bonding / node.h
1 /*
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
34 #define BOND_MODULO_SHORTCUT(a) \
35   (is_pow2 (a))
36
37 #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
44 typedef 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)  \
54   _ (1, L34 , "l34", l34) \
55   _ (0, L2, "l2", l2)
56
57 /* load-balance functions implemented in bond-output */
58 #define foreach_bond_lb_algo                     \
59   _ (0, L2, "l2", l2)                            \
60   _ (1, L34 , "l34", l34)                        \
61   _ (2, L23, "l23", l23)                         \
62   _ (3, RR, "round-robin", round_robin)          \
63   _ (4, BC, "broadcast", broadcast)              \
64   _ (5, AB, "active-backup", active_backup)
65
66 typedef 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
73 enum
74 {
75   BOND_SEND_GARP_NA = 1,
76 } bond_send_garp_na_process_event_t;
77
78 typedef struct
79 {
80   u8 hw_addr_set;
81   u8 hw_addr[6];
82   u8 mode;
83   u8 lb;
84   /* return */
85   u32 sw_if_index;
86   int rv;
87   clib_error_t *error;
88 } bond_create_if_args_t;
89
90 typedef struct
91 {
92   /* slave's sw_if_index */
93   u32 slave;
94   /* bond's sw_if_index */
95   u32 group;
96   u8 is_passive;
97   u8 is_long_timeout;
98   /* return */
99   int rv;
100   clib_error_t *error;
101 } bond_enslave_args_t;
102
103 typedef struct
104 {
105   u32 slave;
106   /* return */
107   int rv;
108   clib_error_t *error;
109 } bond_detach_slave_args_t;
110
111 /** BOND interface details struct */
112 typedef struct
113 {
114   u32 sw_if_index;
115   u8 interface_name[64];
116   u8 mode;
117   u8 lb;
118   u32 active_slaves;
119   u32 slaves;
120 } bond_interface_details_t;
121
122 /** slave interface details struct */
123 typedef struct
124 {
125   u32 sw_if_index;
126   u8 interface_name[64];
127   u8 is_passive;
128   u8 is_long_timeout;
129   u32 active_slaves;
130 } slave_interface_details_t;
131
132 typedef CLIB_PACKED (struct
133                      {
134                      u16 system_priority;
135                      u8 system[6];
136                      u16 key; u16 port_priority; u16 port_number;
137                      u8 state;
138                      }) lacp_port_info_t;
139
140 typedef struct
141 {
142   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
143   u32 buffers[VLIB_FRAME_SIZE];
144   u32 n_buffers;
145 } bond_per_port_queue_t;
146
147 typedef struct
148 {
149   bond_per_port_queue_t *per_port_queue;
150 } bond_per_thread_data_t;
151
152 typedef struct
153 {
154   u8 admin_up;
155   u8 mode;
156   u8 lb;
157
158   /* the last slave index for the rr lb */
159   u32 lb_rr_last_index;
160
161   u32 dev_instance;
162   u32 hw_if_index;
163   u32 sw_if_index;
164
165   /* Configured slaves */
166   u32 *slaves;
167
168   /* Slaves that are in DISTRIBUTING state */
169   u32 *active_slaves;
170
171   /* rapidly find an active slave */
172   uword *active_slave_by_sw_if_index;
173
174   lacp_port_info_t partner;
175   lacp_port_info_t actor;
176   u8 individual_aggregator;
177
178   u32 group;
179   uword *port_number_bitmap;
180   u8 use_custom_mac;
181   u8 hw_address[6];
182
183   clib_spinlock_t lockp;
184 } bond_if_t;
185
186 typedef struct
187 {
188   u8 persistent_hw_address[6];
189
190   /* neighbor's vlib software interface index */
191   u32 sw_if_index;
192
193   /* Neighbor time-to-live (usually 3s) */
194   f32 ttl_in_seconds;
195
196   /* 1 = interface is configured with long timeout (60s) */
197   u8 is_long_timeout;
198
199   /* 1 = debug is on; 0 = debug is off */
200   u8 debug;
201
202   /* tx packet template id for this neighbor */
203   u8 packet_template_index;
204
205   /* Info we actually keep about each neighbor */
206
207   /* Jenkins hash optimization: avoid tlv scan, send short keepalive msg */
208   u8 last_packet_signature_valid;
209   uword last_packet_signature;
210
211   /* last received lacp packet, for the J-hash optimization */
212   u8 *last_rx_pkt;
213
214   /* last marker packet */
215   u8 *last_marker_pkt;
216
217   /* neighbor vlib hw_if_index */
218   u32 hw_if_index;
219
220   /* actor does not initiate the protocol exchange */
221   u8 is_passive;
222
223   /* Partner port information */
224   lacp_port_info_t partner;
225   lacp_port_info_t partner_admin;;
226
227   /* Partner port information */
228   lacp_port_info_t actor;
229   lacp_port_info_t actor_admin;
230
231   /* Need To Transmit flag */
232   u8 ntt;
233
234   /* Link has been established and Aggregate Port is operable */
235   u8 port_enabled;
236
237   /* Initialization or reinitialization of the lacp protocol entity */
238   u8 begin;
239
240   /* Aggregation Port is operating the lacp */
241   u8 lacp_enabled;
242
243   /* MUX to indicate to the Selection Logic wait_while_timer expired */
244   u8 ready_n;
245
246   /* Selection Logic indicates al Aggregation Ports attached */
247   u8 ready;
248
249   /* Selection Logic selected an Aggregator */
250   int selected;
251
252   /* RX machine indicates an Aggregation Port in PORT_DISABLED state */
253   u8 port_moved;
254
255   /* timer used to detect whether received protocol information has expired */
256   f64 current_while_timer;
257
258   /* timer used to detect actor churn states */
259   f64 actor_churn_timer;
260
261   /* time last lacpdu was sent */
262   f64 last_lacpdu_time;
263
264   /* timer used to generate periodic transmission */
265   f64 periodic_timer;
266
267   /* timer used to detect partner churn states */
268   f64 partner_churn_timer;
269
270   /* provides hysteresis before performing an aggregation change */
271   f64 wait_while_timer;
272
273   /* Implemention variables, not in the spec */
274   int rx_state;
275   int tx_state;
276   int mux_state;
277   int ptx_state;
278
279   /* actor admin key */
280   u32 group;
281
282   u32 marker_tx_id;
283
284   u32 bif_dev_instance;
285
286   u8 loopback_port;
287
288   /* bond mode */
289   u8 mode;
290 } slave_if_t;
291
292 typedef void (*lacp_enable_disable_func) (vlib_main_t * vm, bond_if_t * bif,
293                                           slave_if_t * sif, u8 enable);
294
295 typedef struct
296 {
297   /* pool of bonding interfaces */
298   bond_if_t *interfaces;
299
300   /* pool of slave interfaces */
301   slave_if_t *neighbors;
302
303   /* rapidly find a bond by vlib software interface index */
304   uword *bond_by_sw_if_index;
305
306   /* convenience variables */
307   vlib_main_t *vlib_main;
308   vnet_main_t *vnet_main;
309
310   /* lacp plugin is loaded */
311   u8 lacp_plugin_loaded;
312
313   lacp_enable_disable_func lacp_enable_disable;
314
315   uword *slave_by_sw_if_index;
316
317   bond_per_thread_data_t *per_thread_data;
318 } bond_main_t;
319
320 /* bond packet trace capture */
321 typedef struct
322 {
323   ethernet_header_t ethernet;
324   u32 sw_if_index;
325   u32 bond_sw_if_index;
326 } bond_packet_trace_t;
327
328 typedef u32 (*load_balance_func) (vlib_main_t * vm,
329                                   vlib_node_runtime_t * node, bond_if_t * bif,
330                                   vlib_buffer_t * b0, uword slave_count);
331
332 typedef struct
333 {
334   load_balance_func load_balance;
335 } bond_load_balance_func_t;
336
337 extern vlib_node_registration_t bond_input_node;
338 extern vlib_node_registration_t bond_process_node;
339 extern vnet_device_class_t bond_dev_class;
340 extern bond_main_t bond_main;
341
342 void bond_disable_collecting_distributing (vlib_main_t * vm,
343                                            slave_if_t * sif);
344 void bond_enable_collecting_distributing (vlib_main_t * vm, slave_if_t * sif);
345 u8 *format_bond_interface_name (u8 * s, va_list * args);
346
347 void bond_create_if (vlib_main_t * vm, bond_create_if_args_t * args);
348 int bond_delete_if (vlib_main_t * vm, u32 sw_if_index);
349 void bond_enslave (vlib_main_t * vm, bond_enslave_args_t * args);
350 void bond_detach_slave (vlib_main_t * vm, bond_detach_slave_args_t * args);
351 int bond_dump_ifs (bond_interface_details_t ** out_bondids);
352 int bond_dump_slave_ifs (slave_interface_details_t ** out_slaveids,
353                          u32 bond_sw_if_index);
354
355 static inline uword
356 unformat_bond_mode (unformat_input_t * input, va_list * args)
357 {
358   u8 *r = va_arg (*args, u8 *);
359
360   if (0);
361 #define _(v, f, s) else if (unformat (input, s)) *r = BOND_MODE_##f;
362   foreach_bond_mode
363 #undef _
364     else
365     return 0;
366
367   return 1;
368 }
369
370 static inline u8 *
371 format_bond_mode (u8 * s, va_list * args)
372 {
373   u32 i = va_arg (*args, u32);
374   u8 *t = 0;
375
376   switch (i)
377     {
378 #define _(v, f, s) case BOND_MODE_##f: t = (u8 *) s; break;
379       foreach_bond_mode
380 #undef _
381     default:
382       return format (s, "unknown");
383     }
384   return format (s, "%s", t);
385 }
386
387 static inline uword
388 unformat_bond_load_balance (unformat_input_t * input, va_list * args)
389 {
390   u8 *r = va_arg (*args, u8 *);
391
392   if (0);
393 #define _(v, f, s, p) else if (unformat (input, s)) *r = BOND_LB_##f;
394   foreach_bond_lb
395 #undef _
396     else
397     return 0;
398
399   return 1;
400 }
401
402 static inline u8 *
403 format_bond_load_balance (u8 * s, va_list * args)
404 {
405   u32 i = va_arg (*args, u32);
406   u8 *t = 0;
407
408   switch (i)
409     {
410 #define _(v, f, s, p) case BOND_LB_##f: t = (u8 *) s; break;
411       foreach_bond_lb_algo
412 #undef _
413     default:
414       return format (s, "unknown");
415     }
416   return format (s, "%s", t);
417 }
418
419 static inline void
420 bond_register_callback (lacp_enable_disable_func func)
421 {
422   bond_main_t *bm = &bond_main;
423
424   bm->lacp_plugin_loaded = 1;
425   bm->lacp_enable_disable = func;
426 }
427
428 static inline bond_if_t *
429 bond_get_master_by_sw_if_index (u32 sw_if_index)
430 {
431   bond_main_t *bm = &bond_main;
432   uword *p;
433
434   p = hash_get (bm->bond_by_sw_if_index, sw_if_index);
435   if (!p)
436     {
437       return 0;
438     }
439   return pool_elt_at_index (bm->interfaces, p[0]);
440 }
441
442 static inline bond_if_t *
443 bond_get_master_by_dev_instance (u32 dev_instance)
444 {
445   bond_main_t *bm = &bond_main;
446
447   return pool_elt_at_index (bm->interfaces, dev_instance);
448 }
449
450 static inline slave_if_t *
451 bond_get_slave_by_sw_if_index (u32 sw_if_index)
452 {
453   bond_main_t *bm = &bond_main;
454   slave_if_t *sif = 0;
455   uword p;
456
457   if (sw_if_index < vec_len (bm->slave_by_sw_if_index))
458     {
459       p = bm->slave_by_sw_if_index[sw_if_index];
460       if (p)
461         sif = pool_elt_at_index (bm->neighbors, p >> 1);
462     }
463
464   return sif;
465 }
466
467 #endif /* __included_vnet_bonding_node_h__ */
468
469 /*
470  * fd.io coding-style-patch-verification: ON
471  *
472  * Local Variables:
473  * eval: (c-set-style "gnu")
474  * End:
475  */