1 /*
2  * VRRP debugging.
3  * Copyright (C) 2019 Cumulus Networks, Inc.
4  * Quentin Young
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; see the file COPYING; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 #include <zebra.h>
21 
22 #include "lib/command.h"
23 #include "lib/debug.h"
24 #include "lib/vector.h"
25 
26 #include "vrrp_debug.h"
27 
28 /* clang-format off */
29 struct debug vrrp_dbg_arp = {0, "VRRP ARP"};
30 struct debug vrrp_dbg_auto = {0, "VRRP autoconfiguration events"};
31 struct debug vrrp_dbg_ndisc = {0, "VRRP Neighbor Discovery"};
32 struct debug vrrp_dbg_pkt = {0, "VRRP packets"};
33 struct debug vrrp_dbg_proto = {0, "VRRP protocol events"};
34 struct debug vrrp_dbg_sock = {0, "VRRP sockets"};
35 struct debug vrrp_dbg_zebra = {0, "VRRP Zebra events"};
36 
37 struct debug *vrrp_debugs[] = {
38 	&vrrp_dbg_arp,
39 	&vrrp_dbg_auto,
40 	&vrrp_dbg_ndisc,
41 	&vrrp_dbg_pkt,
42 	&vrrp_dbg_proto,
43 	&vrrp_dbg_sock,
44 	&vrrp_dbg_zebra
45 };
46 
47 const char *vrrp_debugs_conflines[] = {
48 	"debug vrrp arp",
49 	"debug vrrp autoconfigure",
50 	"debug vrrp ndisc",
51 	"debug vrrp packets",
52 	"debug vrrp protocol",
53 	"debug vrrp sockets",
54 	"debug vrrp zebra",
55 };
56 /* clang-format on */
57 
58 /*
59  * Set or unset flags on all debugs for vrrpd.
60  *
61  * flags
62  *    The flags to set
63  *
64  * set
65  *    Whether to set or unset the specified flags
66  */
vrrp_debug_set_all(uint32_t flags,bool set)67 static void vrrp_debug_set_all(uint32_t flags, bool set)
68 {
69 	for (unsigned int i = 0; i < array_size(vrrp_debugs); i++) {
70 		DEBUG_FLAGS_SET(vrrp_debugs[i], flags, set);
71 
72 		/* if all modes have been turned off, don't preserve options */
73 		if (!DEBUG_MODE_CHECK(vrrp_debugs[i], DEBUG_MODE_ALL))
74 			DEBUG_CLEAR(vrrp_debugs[i]);
75 	}
76 }
77 
vrrp_debug_config_write_helper(struct vty * vty,bool config)78 static int vrrp_debug_config_write_helper(struct vty *vty, bool config)
79 {
80 	uint32_t mode = DEBUG_MODE_ALL;
81 
82 	if (config)
83 		mode = DEBUG_MODE_CONF;
84 
85 	for (unsigned int i = 0; i < array_size(vrrp_debugs); i++)
86 		if (DEBUG_MODE_CHECK(vrrp_debugs[i], mode))
87 			vty_out(vty, "%s\n", vrrp_debugs_conflines[i]);
88 
89 	return 0;
90 }
91 
vrrp_config_write_debug(struct vty * vty)92 int vrrp_config_write_debug(struct vty *vty)
93 {
94 	return vrrp_debug_config_write_helper(vty, true);
95 }
96 
vrrp_debug_status_write(struct vty * vty)97 int vrrp_debug_status_write(struct vty *vty)
98 {
99 	return vrrp_debug_config_write_helper(vty, false);
100 }
101 
vrrp_debug_set(struct interface * ifp,uint8_t vrid,int vtynode,bool onoff,bool proto,bool autoconf,bool pkt,bool sock,bool ndisc,bool arp,bool zebra)102 void vrrp_debug_set(struct interface *ifp, uint8_t vrid, int vtynode,
103 		    bool onoff, bool proto, bool autoconf, bool pkt, bool sock,
104 		    bool ndisc, bool arp, bool zebra)
105 {
106 	uint32_t mode = DEBUG_NODE2MODE(vtynode);
107 
108 	if (proto)
109 		DEBUG_MODE_SET(&vrrp_dbg_proto, mode, onoff);
110 	if (autoconf)
111 		DEBUG_MODE_SET(&vrrp_dbg_auto, mode, onoff);
112 	if (pkt)
113 		DEBUG_MODE_SET(&vrrp_dbg_pkt, mode, onoff);
114 	if (sock)
115 		DEBUG_MODE_SET(&vrrp_dbg_sock, mode, onoff);
116 	if (ndisc)
117 		DEBUG_MODE_SET(&vrrp_dbg_ndisc, mode, onoff);
118 	if (arp)
119 		DEBUG_MODE_SET(&vrrp_dbg_arp, mode, onoff);
120 	if (zebra)
121 		DEBUG_MODE_SET(&vrrp_dbg_zebra, mode, onoff);
122 }
123 
124 /* ------------------------------------------------------------------------- */
125 
126 struct debug_callbacks vrrp_dbg_cbs = {.debug_set_all = vrrp_debug_set_all};
127 
vrrp_debug_init(void)128 void vrrp_debug_init(void)
129 {
130 	debug_init(&vrrp_dbg_cbs);
131 }
132