]> git.proxmox.com Git - mirror_frr.git/blame - vrrpd/vrrp.h
*: Convert event.h to frrevent.h
[mirror_frr.git] / vrrpd / vrrp.h
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
5435a2bf 2/*
63d4bd12
QY
3 * VRRP global definitions and state machine.
4 * Copyright (C) 2018-2019 Cumulus Networks, Inc.
5 * Quentin Young
5435a2bf 6 */
63d4bd12
QY
7#ifndef __VRRP_H__
8#define __VRRP_H__
5435a2bf
QY
9
10#include <zebra.h>
91188ca6 11#include <netinet/ip.h>
1d21789e 12
7c136b08 13#include "lib/memory.h"
1d21789e
QY
14#include "lib/hash.h"
15#include "lib/hook.h"
16#include "lib/if.h"
17#include "lib/linklist.h"
f495425b 18#include "lib/northbound.h"
1d21789e 19#include "lib/privs.h"
91188ca6 20#include "lib/stream.h"
24a58196 21#include "lib/frrevent.h"
f828842a 22#include "lib/vty.h"
5435a2bf
QY
23
24/* Global definitions */
f3fe0047 25#define VRRP_RADV_INT 16
5435a2bf 26#define VRRP_PRIO_MASTER 255
862f2f37
QY
27#define VRRP_MCASTV4_GROUP_STR "224.0.0.18"
28#define VRRP_MCASTV6_GROUP_STR "ff02:0:0:0:0:0:0:12"
29#define VRRP_MCASTV4_GROUP 0xe0000012
30#define VRRP_MCASTV6_GROUP 0xff020000000000000000000000000012
5435a2bf
QY
31#define IPPROTO_VRRP 112
32
613b45b0
QY
33#define VRRP_LOGPFX_VRID "[VRID %u] "
34#define VRRP_LOGPFX_FAM "[%s] "
4ec94408 35
8cd1d277 36/* Default defaults */
f495425b
QY
37#define VRRP_XPATH_FULL "/frr-interface:lib/interface/frr-vrrpd:vrrp/vrrp-group"
38#define VRRP_XPATH "./frr-vrrpd:vrrp/vrrp-group"
8cd1d277
QY
39#define VRRP_DEFAULT_PRIORITY 100
40#define VRRP_DEFAULT_ADVINT 100
41#define VRRP_DEFAULT_PREEMPT true
42#define VRRP_DEFAULT_ACCEPT true
9f2379bd 43#define VRRP_DEFAULT_CHECKSUM_WITH_IPV4_PSEUDOHEADER true
8cd1d277
QY
44#define VRRP_DEFAULT_SHUTDOWN false
45
cb44d476
QY
46/* User compatibility constant */
47#define CS2MS 10
48
bf8d3d6a 49DECLARE_MGROUP(VRRPD);
7c136b08 50
f495425b
QY
51/* Northbound */
52extern const struct frr_yang_module_info frr_vrrpd_info;
53
8cd1d277
QY
54/* Configured defaults */
55struct vrrp_defaults {
f495425b 56 uint8_t version;
8cd1d277
QY
57 uint8_t priority;
58 uint16_t advertisement_interval;
59 bool preempt_mode;
60 bool accept_mode;
9f2379bd 61 bool checksum_with_ipv4_pseudoheader;
8cd1d277
QY
62 bool shutdown;
63};
64
65extern struct vrrp_defaults vd;
66
5435a2bf 67/* threadmaster */
cd9d0537 68extern struct event_loop *master;
5435a2bf 69
b6029d6a
QY
70/* privileges */
71extern struct zebra_privs_t vrrp_privs;
72
5435a2bf 73/* Global hash of all Virtual Routers */
27fd8827 74extern struct hash *vrrp_vrouters_hash;
5435a2bf 75
27fd8827
QY
76/*
77 * VRRP Router.
78 *
79 * This struct contains all state for a particular VRRP Router operating
80 * in a Virtual Router for either IPv4 or IPv6.
81 */
82struct vrrp_router {
862f2f37
QY
83 /*
84 * Whether this VRRP Router is active.
85 */
86 bool is_active;
87
10133a59
QY
88 /* Whether we are the address owner */
89 bool is_owner;
90
dad18a2f
QY
91 /* Rx socket: Rx from parent of mvl_ifp */
92 int sock_rx;
93 /* Tx socket; Tx from mvl_ifp */
94 int sock_tx;
95
96 /* macvlan interface */
97 struct interface *mvl_ifp;
5435a2bf 98
8071d5c3
QY
99 /* Source address for advertisements */
100 struct ipaddr src;
101
91188ca6
QY
102 /* Socket read buffer */
103 uint8_t ibuf[IP_MAXPACKET];
104
862f2f37
QY
105 /*
106 * Address family of this Virtual Router.
107 * Either AF_INET or AF_INET6.
108 */
109 int family;
5435a2bf
QY
110
111 /*
862f2f37 112 * Virtual Router this VRRP Router is participating in.
5435a2bf 113 */
862f2f37 114 struct vrrp_vrouter *vr;
5435a2bf 115
862f2f37
QY
116 /*
117 * One or more IPvX addresses associated with this Virtual
118 * Router. The first address must be the "primary" address this
119 * Virtual Router is backing up in the case of IPv4. In the case of
120 * IPv6 it must be the link-local address of vr->ifp.
121 *
122 * Type: struct ipaddr *
123 */
124 struct list *addrs;
5435a2bf 125
d60b2ffd
QY
126 /*
127 * This flag says whether we are waiting on an interface up
128 * notification from Zebra before we send an ADVERTISEMENT.
129 */
130 bool advert_pending;
131
132 /*
133 * If this is an IPv4 VRRP router, this flag says whether we are
134 * waiting on an interface up notification from Zebra before we send
135 * gratuitous ARP packets for all our addresses. Should never be true
136 * if family == AF_INET6.
137 */
138 bool garp_pending;
139 /*
140 * If this is an IPv6 VRRP router, this flag says whether we are
141 * waiting on an interface up notification from Zebra before we send
142 * Unsolicited Neighbor Advertisement packets for all our addresses.
143 * Should never be true if family == AF_INET.
144 */
145 bool ndisc_pending;
146
5d3730c5
QY
147 /*
148 * Effective priority
862f2f37 149 * => vr->priority if we are Backup
5d3730c5
QY
150 * => 255 if we are Master
151 */
5435a2bf
QY
152 uint8_t priority;
153
5435a2bf
QY
154 /*
155 * Advertisement interval contained in ADVERTISEMENTS received from the
156 * Master (centiseconds)
157 */
158 uint16_t master_adver_interval;
159
160 /*
161 * Time to skew Master_Down_Interval in centiseconds. Calculated as:
162 * (((256 - priority) * Master_Adver_Interval) / 256)
163 */
164 uint16_t skew_time;
165
166 /*
167 * Time interval for Backup to declare Master down (centiseconds).
168 * Calculated as:
169 * (3 * Master_Adver_Interval) + Skew_time
170 */
171 uint16_t master_down_interval;
172
862f2f37
QY
173 /*
174 * The MAC address used for the source MAC address in VRRP
175 * advertisements, advertised in ARP requests/responses, and advertised
176 * in ND Neighbor Advertisements.
177 */
178 struct ethaddr vmac;
179
180 struct {
181 int state;
182 } fsm;
183
6332c77f
QY
184 struct {
185 /* Total number of advertisements sent and received */
186 uint32_t adver_tx_cnt;
187 uint32_t adver_rx_cnt;
188 /* Total number of gratuitous ARPs sent */
189 uint32_t garp_tx_cnt;
190 /* Total number of unsolicited Neighbor Advertisements sent */
191 uint32_t una_tx_cnt;
192 /* Total number of state transitions */
193 uint32_t trans_cnt;
194 } stats;
195
e6685141
DS
196 struct event *t_master_down_timer;
197 struct event *t_adver_timer;
198 struct event *t_read;
199 struct event *t_write;
862f2f37
QY
200};
201
202/*
203 * VRRP Virtual Router.
204 *
205 * This struct contains all state and configuration for a given Virtual Router
206 * Identifier on a given interface, both v4 and v6.
207 *
208 * RFC5798 s. 1 states:
209 * "Within a VRRP router, the virtual routers in each of the IPv4 and IPv6
210 * address families are a domain unto themselves and do not overlap."
211 *
212 * This implementation has chosen the tuple (interface, VRID) as the key for a
213 * particular VRRP Router, and the rest of the program is designed around this
214 * assumption. Additionally, base protocol configuration parameters such as the
215 * advertisement interval and (configured) priority are shared between v4 and
216 * v6 instances. This corresponds to the choice made by other industrial
217 * implementations.
218 */
219struct vrrp_vrouter {
53e60e5c
QY
220 /* Whether this instance was automatically configured */
221 bool autoconf;
222
6e93585e
QY
223 /* Whether this VRRP router is in administrative shutdown */
224 bool shutdown;
225
862f2f37
QY
226 /* Interface */
227 struct interface *ifp;
228
91188ca6
QY
229 /* Version */
230 uint8_t version;
231
862f2f37
QY
232 /* Virtual Router Identifier */
233 uint32_t vrid;
234
235 /* Configured priority */
236 uint8_t priority;
237
238 /*
239 * Time interval between ADVERTISEMENTS (centiseconds). Default is 100
240 * centiseconds (1 second).
241 */
242 uint16_t advertisement_interval;
243
5435a2bf
QY
244 /*
245 * Controls whether a (starting or restarting) higher-priority Backup
246 * router preempts a lower-priority Master router. Values are True to
247 * allow preemption and False to prohibit preemption. Default is True.
248 */
249 bool preempt_mode;
250
251 /*
252 * Controls whether a virtual router in Master state will accept
253 * packets addressed to the address owner's IPvX address as its own if
254 * it is not the IPvX address owner. The default is False.
255 */
256 bool accept_mode;
257
9f2379bd
SY
258 /*
259 * Indicates whether this router computes and accepts VRRPv3 checksums
260 * without pseudoheader, for device interoperability.
261 *
262 * This option should only affect IPv4 virtual routers.
263 */
264 bool checksum_with_ipv4_pseudoheader;
265
862f2f37
QY
266 struct vrrp_router *v4;
267 struct vrrp_router *v6;
5435a2bf
QY
268};
269
5435a2bf
QY
270/*
271 * Initialize VRRP global datastructures.
272 */
273void vrrp_init(void);
274
f1175ba9
QY
275/*
276 * Destroy all VRRP instances and gracefully shutdown.
277 *
278 * For instances in Master state, VRRP advertisements with 0 priority will be
279 * sent if possible to notify Backup routers that we are going away.
280 */
281void vrrp_fini(void);
282
1d21789e
QY
283
284/* Creation and destruction ------------------------------------------------ */
285
5435a2bf
QY
286/*
287 * Create and register a new VRRP Virtual Router.
1d21789e
QY
288 *
289 * ifp
290 * Base interface to configure VRRP on
291 *
292 * vrid
293 * Virtual Router Identifier
5435a2bf 294 */
99966840
QY
295struct vrrp_vrouter *vrrp_vrouter_create(struct interface *ifp, uint8_t vrid,
296 uint8_t version);
5435a2bf 297
c23edd74 298/*
6287cefe
QY
299 * Destroy a VRRP Virtual Router, freeing all its resources.
300 *
301 * If there are any running VRRP instances, these are stopped and destroyed.
c23edd74
QY
302 */
303void vrrp_vrouter_destroy(struct vrrp_vrouter *vr);
304
1d21789e
QY
305
306/* Configuration controllers ----------------------------------------------- */
307
6e93585e
QY
308/*
309 * Check if a Virtual Router ought to be started, and if so, start it.
310 *
311 * vr
312 * Virtual Router to checkstart
313 */
314void vrrp_check_start(struct vrrp_vrouter *vr);
315
c23edd74 316/*
5d3730c5 317 * Change the configured priority of a VRRP Virtual Router.
c23edd74 318 *
5d3730c5
QY
319 * Note that this only changes the configured priority of the Virtual Router.
320 * The currently effective priority will not be changed; to change the
321 * effective priority, the Virtual Router must be restarted by issuing a
322 * VRRP_EVENT_SHUTDOWN followed by a VRRP_EVENT_STARTUP.
c23edd74 323 *
1d21789e
QY
324 * vr
325 * Virtual Router to change priority of
c23edd74 326 *
1d21789e
QY
327 * priority
328 * New priority
c23edd74 329 */
1d21789e 330void vrrp_set_priority(struct vrrp_vrouter *vr, uint8_t priority);
c23edd74
QY
331
332/*
1d21789e 333 * Set Advertisement Interval on this Virtual Router.
c23edd74
QY
334 *
335 * vr
336 * Virtual Router to change priority of
337 *
1d21789e
QY
338 * advertisement_interval
339 * New advertisement interval
c23edd74 340 */
1d21789e
QY
341void vrrp_set_advertisement_interval(struct vrrp_vrouter *vr,
342 uint16_t advertisement_interval);
c23edd74
QY
343
344/*
862f2f37
QY
345 * Add an IPvX address to a VRRP Virtual Router.
346 *
671fd2a3 347 * vr
862f2f37
QY
348 * Virtual Router to add IPvx address to
349 *
350 * ip
351 * Address to add
2cd90902
QY
352 *
353 * activate
354 * Whether to automatically start the VRRP router if this is the first IP
355 * address added.
356 *
357 * Returns:
358 * -1 on error
359 * 0 otherwise
862f2f37 360 */
671fd2a3 361int vrrp_add_ip(struct vrrp_vrouter *vr, struct ipaddr *ip);
862f2f37
QY
362
363/*
364 * Add an IPv4 address to a VRRP Virtual Router.
c23edd74
QY
365 *
366 * vr
367 * Virtual Router to add IPv4 address to
368 *
369 * v4
370 * Address to add
2cd90902
QY
371 *
372 * activate
373 * Whether to automatically start the VRRP router if this is the first IP
374 * address added.
375 *
376 * Returns:
377 * -1 on error
378 * 0 otherwise
c23edd74 379 */
6e93585e 380int vrrp_add_ipv4(struct vrrp_vrouter *vr, struct in_addr v4);
862f2f37
QY
381
382/*
383 * Add an IPv6 address to a VRRP Virtual Router.
384 *
385 * vr
386 * Virtual Router to add IPv6 address to
387 *
388 * v6
389 * Address to add
2cd90902
QY
390 *
391 * activate
392 * Whether to automatically start the VRRP router if this is the first IP
393 * address added.
394 *
395 * Returns:
396 * -1 on error
397 * 0 otherwise
398 */
6e93585e 399int vrrp_add_ipv6(struct vrrp_vrouter *vr, struct in6_addr v6);
2cd90902
QY
400
401/*
402 * Remove an IP address from a VRRP Virtual Router.
403 *
671fd2a3 404 * vr
2cd90902
QY
405 * Virtual Router to remove IP address from
406 *
407 * ip
408 * Address to remove
409 *
410 * deactivate
411 * Whether to automatically stop the VRRP router if removing v4 would leave
412 * us with an empty address list. If this is not true and ip is the only IP
413 * address backed up by this virtual router, this function will not remove
414 * the address and return failure.
415 *
416 * Returns:
417 * -1 on error
418 * 0 otherwise
419 */
671fd2a3 420int vrrp_del_ip(struct vrrp_vrouter *vr, struct ipaddr *ip);
2cd90902
QY
421
422/*
423 * Remove an IPv4 address from a VRRP Virtual Router.
424 *
425 * vr
426 * Virtual Router to remove IPv4 address from
427 *
428 * v4
429 * Address to remove
430 *
431 * deactivate
432 * Whether to automatically stop the VRRP router if removing v4 would leave
433 * us with an empty address list. If this is not true and v4 is the only
434 * IPv4 address backed up by this virtual router, this function will not
435 * remove the address and return failure.
436 *
437 * Returns:
438 * -1 on error
439 * 0 otherwise
862f2f37 440 */
6e93585e 441int vrrp_del_ipv4(struct vrrp_vrouter *vr, struct in_addr v4);
c23edd74 442
2cd90902
QY
443/*
444 * Remove an IPv6 address from a VRRP Virtual Router.
445 *
446 * vr
447 * Virtual Router to remove IPv6 address from
448 *
449 * v6
450 * Address to remove
451 *
452 * deactivate
453 * Whether to automatically stop the VRRP router if removing v5 would leave
454 * us with an empty address list. If this is not true and v4 is the only
455 * IPv6 address backed up by this virtual router, this function will not
456 * remove the address and return failure.
457 *
458 * Returns:
459 * -1 on error
460 * 0 otherwise
461 */
6e93585e 462int vrrp_del_ipv6(struct vrrp_vrouter *vr, struct in6_addr v6);
1d21789e
QY
463
464/* State machine ----------------------------------------------------------- */
465
4ec94408
QY
466#define VRRP_STATE_INITIALIZE 0
467#define VRRP_STATE_MASTER 1
468#define VRRP_STATE_BACKUP 2
469#define VRRP_EVENT_STARTUP 0
470#define VRRP_EVENT_SHUTDOWN 1
471
2b64873d 472extern const char *const vrrp_state_names[3];
1d21789e 473
5435a2bf 474/*
1d21789e
QY
475 * This hook called whenever the state of a Virtual Router changes, after the
476 * specific internal state handlers have run.
477 *
478 * Use this if you need to react to state changes to perform non-critical
479 * tasks. Critical tasks should go in the internal state change handlers.
5435a2bf 480 */
2fff50ec 481DECLARE_HOOK(vrrp_change_state_hook, (struct vrrp_router *r, int to), (r, to));
5435a2bf
QY
482
483/*
1d21789e
QY
484 * Trigger a VRRP event on a given Virtual Router..
485 *
486 * vr
487 * Virtual Router to operate on
488 *
489 * event
490 * Event to kick off. All event related processing will have completed upon
491 * return of this function.
492 *
493 * Returns:
494 * < 0 if the event created an error
495 * 0 otherwise
5435a2bf 496 */
862f2f37 497int vrrp_event(struct vrrp_router *r, int event);
5435a2bf 498
27fd8827 499/* Autoconfig -------------------------------------------------------------- */
1d21789e 500
53e60e5c
QY
501/*
502 * Search for and automatically configure VRRP instances on interfaces.
503 *
504 * ifp
505 * Interface to autoconfig. If it is a macvlan interface and has a VRRP MAC,
506 * a VRRP instance corresponding to VMAC assigned to macvlan will be created
507 * on the parent interface and all addresses on the macvlan interface except
508 * the v6 link local will be configured as VRRP addresses. If NULL, this
509 * treatment will be applied to all existing interfaces matching the above
510 * criterion.
511 *
512 * Returns:
513 * -1 on failure
514 * 0 otherwise
515 */
27fd8827
QY
516int vrrp_autoconfig(void);
517
518/*
519 * Enable autoconfiguration.
520 *
521 * Calling this function will cause vrrpd to automatically configure VRRP
522 * instances on existing compatible macvlan interfaces. These instances will
523 * react to interface up/down and address add/delete events to keep themselves
524 * in sync with the available interfaces.
525 *
526 * version
527 * VRRP version to use for autoconfigured instances. Must be 2 or 3.
528 */
529void vrrp_autoconfig_on(int version);
530
531/*
532 * Disable autoconfiguration.
533 *
534 * Calling this function will delete all existing autoconfigured VRRP instances.
535 */
536void vrrp_autoconfig_off(void);
537
6e93585e 538/* Interface Tracking ------------------------------------------------------ */
27fd8827 539
6e93585e
QY
540void vrrp_if_add(struct interface *ifp);
541void vrrp_if_del(struct interface *ifp);
542void vrrp_if_up(struct interface *ifp);
543void vrrp_if_down(struct interface *ifp);
544void vrrp_if_address_add(struct interface *ifp);
545void vrrp_if_address_del(struct interface *ifp);
27fd8827
QY
546
547/* Other ------------------------------------------------------------------- */
53e60e5c 548
f828842a
QY
549/*
550 * Write global level configuration to vty.
551 *
552 * vty
553 * vty to write config to
554 *
555 * Returns:
556 * # of lines written
557 */
558int vrrp_config_write_global(struct vty *vty);
559
1d21789e
QY
560/*
561 * Find VRRP Virtual Router by Virtual Router ID
562 */
fb8b348c 563struct vrrp_vrouter *vrrp_lookup(const struct interface *ifp, uint8_t vrid);
1d21789e 564
63d4bd12 565#endif /* __VRRP_H__ */