0eda5d66164ec6c452913dc7e129a23cbf515a4a
[vpp.git] / src / plugins / vrrp / vrrp.h
1
2 /*
3  * vrrp.h - vrrp plug-in header file
4  *
5  * Copyright 2019-2020 Rubicon Communications, LLC (Netgate)
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  *
9  */
10 #ifndef __included_vrrp_h__
11 #define __included_vrrp_h__
12
13 #include <vnet/vnet.h>
14 #include <vnet/ip/ip.h>
15 #include <vnet/ethernet/ethernet.h>
16
17 #include <vppinfra/hash.h>
18 #include <vppinfra/error.h>
19
20 /* VRRP configuration */
21 typedef enum vrrp_vr_flags
22 {
23   VRRP_VR_PREEMPT = 0x1,
24   VRRP_VR_ACCEPT = 0x2,
25   VRRP_VR_UNICAST = 0x4,
26   VRRP_VR_IPV6 = 0x8,
27 } vrrp_vr_flags_t;
28
29 typedef struct vrrp_vr_key
30 {
31   u32 sw_if_index;
32   u8 vr_id;
33   u8 is_ipv6;
34 } vrrp_vr_key_t;
35
36 /* *INDENT-OFF* */
37 typedef CLIB_PACKED
38 (struct vrrp4_arp_key {
39   union {
40     struct {
41       u32 sw_if_index;
42       ip4_address_t addr;
43     };
44     u64 as_u64;
45   };
46 }) vrrp4_arp_key_t;
47 /* *INDENT-ON* */
48
49 /* *INDENT-OFF* */
50 typedef CLIB_PACKED
51 (struct vrrp6_nd_key {
52   u32 sw_if_index;
53   ip6_address_t addr;
54 }) vrrp6_nd_key_t;
55 /* *INDENT-ON* */
56
57 typedef struct vrrp_vr_tracking_if
58 {
59   u32 sw_if_index;
60   u8 priority;
61 } vrrp_vr_tracking_if_t;
62
63 typedef struct vrrp_vr_tracking
64 {
65   vrrp_vr_tracking_if_t *interfaces;
66   u32 interfaces_dec;
67 } vrrp_vr_tracking_t;
68
69 typedef struct vrrp_vr_config
70 {
71   u32 sw_if_index;
72   u8 vr_id;
73   u8 priority;
74   u16 adv_interval;
75   vrrp_vr_flags_t flags;
76   ip46_address_t *vr_addrs;
77   ip46_address_t *peer_addrs;
78 } vrrp_vr_config_t;
79
80 #define foreach_vrrp_vr_state           \
81 _(0, INIT, "Initialize")                \
82 _(1, BACKUP, "Backup")                  \
83 _(2, MASTER, "Master")                  \
84 _(3, INTF_DOWN, "Interface Down")
85
86 /* VRRP runtime data */
87 typedef enum vrrp_vr_state
88 {
89 #define _(v,f,n) VRRP_VR_STATE_##f = v,
90   foreach_vrrp_vr_state
91 #undef _
92 } vrrp_vr_state_t;
93
94 typedef struct vrrp_vr_runtime
95 {
96   vrrp_vr_state_t state;
97   u16 master_adv_int;
98   u16 skew;
99   u16 master_down_int;
100   mac_address_t mac;
101   f64 last_sent;
102   u32 timer_index;
103 } vrrp_vr_runtime_t;
104
105 /* Per-VR data */
106 typedef struct vrrp_vr
107 {
108   vrrp_vr_config_t config;
109   vrrp_vr_runtime_t runtime;
110   vrrp_vr_tracking_t tracking;
111 } vrrp_vr_t;
112
113 /* Timers */
114 typedef enum vrrp_vr_timer_type
115 {
116   VRRP_VR_TIMER_ADV,
117   VRRP_VR_TIMER_MASTER_DOWN,
118 } vrrp_vr_timer_type_t;
119
120 typedef struct vrrp_vr_timer
121 {
122   u32 vr_index;
123   f64 expire_time;              /* monotonic, relative to vlib_time_now() */
124   vrrp_vr_timer_type_t type;
125 } vrrp_vr_timer_t;
126
127 typedef struct
128 {
129   /* vectors of vr indices which are configured on this interface
130    * 0 -> ipv4, 1 -> ipv6 */
131   u32 *vr_indices[2];
132
133   /* vector of VR indices which track the state of this interface
134    * 0 -> ipv4, 1*/
135   u32 *tracking_vrs[2];
136
137   /* multicast adjacency indices. 0 -> ipv4, 1 -> ipv6 */
138   adj_index_t mcast_adj_index[2];
139
140   /* number of VRs in master state on sw intf. 0 -> ipv4, 1 -> ipv6 */
141   u8 n_master_vrs[2];
142
143 } vrrp_intf_t;
144
145 typedef struct
146 {
147   /* API message ID base */
148   u16 msg_id_base;
149
150   /* pool of VRs */
151   vrrp_vr_t *vrs;
152
153   /* pool of timers and ordered vector of pool indices */
154   vrrp_vr_timer_t *vr_timers;
155   u32 *pending_timers;
156
157   /* number of running VRs - don't register for VRRP proto if not running */
158   u16 n_vrs_started;
159
160   /* hash mapping a VR key to a pool entry */
161   mhash_t vr_index_by_key;
162
163   /* hashes mapping sw_if_index and address to a vr index */
164   uword *vrrp4_arp_lookup;
165   uword *vrrp6_nd_lookup;
166
167   /* vector of interface data indexed by sw_if_index */
168   vrrp_intf_t *vrrp_intfs;
169
170   /* convenience */
171   vlib_main_t *vlib_main;
172   vnet_main_t *vnet_main;
173   ethernet_main_t *ethernet_main;
174
175   u32 intf_output_node_idx;
176 } vrrp_main_t;
177
178 extern vrrp_main_t vrrp_main;
179
180 extern vlib_node_registration_t vrrp_node;
181 extern vlib_node_registration_t vrrp_periodic_node;
182
183 /* Periodic function events */
184 #define VRRP_EVENT_VR_TIMER_UPDATE 1
185 #define VRRP_EVENT_VR_STOP 2
186 #define VRRP_EVENT_PERIODIC_ENABLE_DISABLE 3
187
188 clib_error_t *vrrp_plugin_api_hookup (vlib_main_t * vm);
189
190 int vrrp_vr_add_del (u8 is_add, vrrp_vr_config_t * conf);
191 int vrrp_vr_start_stop (u8 is_start, vrrp_vr_key_t * vr_key);
192 extern u8 *format_vrrp_vr (u8 * s, va_list * args);
193 extern u8 *format_vrrp_vr_key (u8 * s, va_list * args);
194 extern u8 *format_vrrp_vr_state (u8 * s, va_list * args);
195 extern u8 *format_vrrp_packet_hdr (u8 * s, va_list * args);
196 void vrrp_vr_timer_set (vrrp_vr_t * vr, vrrp_vr_timer_type_t type);
197 void vrrp_vr_timer_cancel (vrrp_vr_t * vr);
198 void vrrp_vr_transition (vrrp_vr_t * vr, vrrp_vr_state_t new_state,
199                          void *data);
200 int vrrp_vr_set_peers (vrrp_vr_key_t * key, ip46_address_t * peers);
201 int vrrp_vr_multicast_group_join (vrrp_vr_t * vr);
202 int vrrp_adv_send (vrrp_vr_t * vr, int shutdown);
203 int vrrp_garp_or_na_send (vrrp_vr_t * vr);
204 u16 vrrp_adv_csum (void *l3_hdr, void *payload, u8 is_ipv6, u16 len);
205 int vrrp_vr_tracking_if_add_del (vrrp_vr_t * vr, u32 sw_if_index,
206                                  u8 priority, u8 is_add);
207 int vrrp_vr_tracking_ifs_add_del (vrrp_vr_t * vr,
208                                   vrrp_vr_tracking_if_t * track_ifs,
209                                   u8 is_add);
210
211
212 always_inline void
213 vrrp_vr_skew_compute (vrrp_vr_t * vr)
214 {
215   vrrp_vr_config_t *vrc = &vr->config;
216   vrrp_vr_runtime_t *vrt = &vr->runtime;
217
218   vrt->skew = (((256 - vrc->priority) * vrt->master_adv_int) / 256);
219 }
220
221 always_inline void
222 vrrp_vr_master_down_compute (vrrp_vr_t * vr)
223 {
224   vrrp_vr_runtime_t *vrt = &vr->runtime;
225
226   vrt->master_down_int = (3 * vrt->master_adv_int) + vrt->skew;
227 }
228
229 always_inline vrrp_vr_t *
230 vrrp_vr_lookup (u32 sw_if_index, u8 vr_id, u8 is_ipv6)
231 {
232   vrrp_main_t *vmp = &vrrp_main;
233   vrrp_vr_key_t key;
234   uword *p;
235
236   clib_memset (&key, 0, sizeof (key));
237
238   key.sw_if_index = sw_if_index;
239   key.vr_id = vr_id;
240   key.is_ipv6 = (is_ipv6 != 0);
241
242   p = mhash_get (&vmp->vr_index_by_key, &key);
243   if (p)
244     return pool_elt_at_index (vmp->vrs, p[0]);
245
246   return 0;
247 }
248
249 always_inline vrrp_vr_t *
250 vrrp_vr_lookup_index (u32 vr_index)
251 {
252   vrrp_main_t *vmp = &vrrp_main;
253
254   if (pool_is_free_index (vmp->vrs, vr_index))
255     return 0;
256
257   return pool_elt_at_index (vmp->vrs, vr_index);
258 }
259
260 always_inline u32
261 vrrp_vr_lookup_address (u32 sw_if_index, u8 is_ipv6, void *addr)
262 {
263   vrrp_main_t *vmp = &vrrp_main;
264   uword *p;
265   vrrp4_arp_key_t key4;
266   vrrp6_nd_key_t key6;
267
268   if (is_ipv6)
269     {
270       key6.sw_if_index = sw_if_index;
271       key6.addr = ((ip6_address_t *) addr)[0];
272       p = hash_get_mem (vmp->vrrp6_nd_lookup, &key6);
273     }
274   else
275     {
276       key4.sw_if_index = sw_if_index;
277       key4.addr = ((ip4_address_t *) addr)[0];
278       p = hash_get (vmp->vrrp4_arp_lookup, key4.as_u64);
279     }
280
281   if (p)
282     return p[0];
283
284   return ~0;
285 }
286
287 always_inline vrrp_intf_t *
288 vrrp_intf_get (u32 sw_if_index)
289 {
290   vrrp_main_t *vrm = &vrrp_main;
291
292   if (sw_if_index == ~0)
293     return NULL;
294
295   vec_validate (vrm->vrrp_intfs, sw_if_index);
296   return vec_elt_at_index (vrm->vrrp_intfs, sw_if_index);
297 }
298
299 always_inline int
300 vrrp_intf_num_vrs (u32 sw_if_index, u8 is_ipv6)
301 {
302   vrrp_intf_t *intf = vrrp_intf_get (sw_if_index);
303
304   if (intf)
305     return vec_len (intf->vr_indices[is_ipv6]);
306
307   return 0;
308 }
309
310 always_inline u8
311 vrrp_vr_is_ipv6 (vrrp_vr_t * vr)
312 {
313   return ((vr->config.flags & VRRP_VR_IPV6) != 0);
314 }
315
316 always_inline u8
317 vrrp_vr_is_unicast (vrrp_vr_t * vr)
318 {
319   return ((vr->config.flags & VRRP_VR_UNICAST) != 0);
320 }
321
322 always_inline u8
323 vrrp_vr_is_owner (vrrp_vr_t * vr)
324 {
325   return (vr->config.priority == 255);
326 }
327
328 always_inline u8
329 vrrp_vr_n_vr_addrs (vrrp_vr_t * vr)
330 {
331   return vec_len (vr->config.vr_addrs);
332 }
333
334 always_inline u8
335 vrrp_vr_n_peer_addrs (vrrp_vr_t * vr)
336 {
337   return vec_len (vr->config.peer_addrs);
338 }
339
340 always_inline u8
341 vrrp_vr_accept_mode_enabled (vrrp_vr_t * vr)
342 {
343   return ((vr->config.flags & VRRP_VR_ACCEPT) != 0);
344 }
345
346 always_inline u32
347 vrrp_vr_index (vrrp_vr_t * vr)
348 {
349   vrrp_main_t *vmp = &vrrp_main;
350
351   return vr - vmp->vrs;
352 }
353
354 always_inline u8
355 vrrp_vr_priority (vrrp_vr_t * vr)
356 {
357   u8 rv;
358
359   if (vr->tracking.interfaces_dec < (u32) vr->config.priority)
360     rv = vr->config.priority - vr->tracking.interfaces_dec;
361   else
362     rv = 1;
363
364   return rv;
365 }
366
367 #endif /* __included_vrrp_h__ */
368
369 /*
370  * fd.io coding-style-patch-verification: ON
371  *
372  * Local Variables:
373  * eval: (c-set-style "gnu")
374  * End:
375  */