]> git.proxmox.com Git - mirror_frr.git/blob - vrrpd/vrrp_vty.c
vrrpd: improve logging
[mirror_frr.git] / vrrpd / vrrp_vty.c
1 /*
2 * VRRP commands
3 * Copyright (C) 2018 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 "command.h"
23 #include "vty.h"
24 #include "if.h"
25 #include "termtable.h"
26 #include "prefix.h"
27
28 #include "vrrp.h"
29 #include "vrrp_vty.h"
30 #include "vrrp_memory.h"
31 #ifndef VTYSH_EXTRACT_PL
32 #include "vrrpd/vrrp_vty_clippy.c"
33 #endif
34
35
36 #define VRRP_STR "Virtual Router Redundancy Protocol\n"
37 #define VRRP_VRID_STR "Virtual Router ID\n"
38 #define VRRP_PRIORITY_STR "Virtual Router Priority\n"
39 #define VRRP_IP_STR "Virtual Router IPv4 address\n"
40
41 #define VROUTER_GET_VTY(_vty, _vrid, _vr) \
42 do { \
43 _vr = vrrp_lookup(_vrid); \
44 if (!_vr) { \
45 vty_out(_vty, \
46 "%% Please configure VRRP instance %u\n", \
47 (unsigned int)_vrid); \
48 return CMD_WARNING_CONFIG_FAILED; \
49 } \
50 } while (0);
51
52 DEFUN_NOSH (show_debugging_vrrpd,
53 show_debugging_vrrpd_cmd,
54 "show debugging [vrrp]",
55 SHOW_STR
56 DEBUG_STR
57 "VRRP information\n")
58 {
59 vty_out(vty, "VRRP debugging status\n");
60
61 return CMD_SUCCESS;
62 }
63
64 DEFPY(vrrp_vrid,
65 vrrp_vrid_cmd,
66 "[no] vrrp (1-255)$vrid",
67 NO_STR
68 VRRP_STR
69 VRRP_VRID_STR)
70 {
71 VTY_DECLVAR_CONTEXT(interface, ifp);
72
73 vrrp_vrouter_create(ifp, vrid);
74
75 return CMD_SUCCESS;
76 }
77
78 DEFPY(vrrp_priority,
79 vrrp_priority_cmd,
80 "[no] vrrp (1-255)$vrid priority (1-255)",
81 NO_STR
82 VRRP_STR
83 VRRP_VRID_STR
84 VRRP_PRIORITY_STR
85 "Priority value; set 255 to designate this Virtual Router as Master\n")
86 {
87 struct vrrp_vrouter *vr;
88 bool need_restart = false;
89 int ret = CMD_SUCCESS;
90
91 VROUTER_GET_VTY(vty, vrid, vr);
92
93 need_restart = (vr->fsm.state != VRRP_STATE_INITIALIZE);
94
95 if (need_restart) {
96 vty_out(vty,
97 "%% WARNING: Restarting Virtual Router %ld to update priority\n",
98 vrid);
99 (void) vrrp_event(vr, VRRP_EVENT_SHUTDOWN);
100 }
101
102 vrrp_set_priority(vr, priority);
103
104 if (need_restart) {
105 ret = vrrp_event(vr, VRRP_EVENT_STARTUP);
106 if (ret < 0)
107 vty_out(vty, "%% Failed to start Virtual Router %ld\n",
108 vrid);
109 }
110
111 return CMD_SUCCESS;
112 }
113
114 DEFPY(vrrp_advertisement_interval,
115 vrrp_advertisement_interval_cmd,
116 "[no] vrrp (1-255)$vrid advertisement-interval (1-4096)",
117 NO_STR
118 VRRP_STR
119 VRRP_VRID_STR
120 VRRP_PRIORITY_STR
121 "Priority value; set 255 to designate this Virtual Router as Master\n")
122 {
123 struct vrrp_vrouter *vr;
124
125 VROUTER_GET_VTY(vty, vrid, vr);
126 vrrp_set_advertisement_interval(vr, advertisement_interval);
127
128 return CMD_SUCCESS;
129 }
130
131 DEFPY(vrrp_ip,
132 vrrp_ip_cmd,
133 "[no] vrrp (1-255)$vrid ip A.B.C.D$ip",
134 NO_STR
135 VRRP_STR
136 VRRP_VRID_STR
137 "Add IP address\n"
138 VRRP_IP_STR)
139 {
140 struct vrrp_vrouter *vr;
141 int ret;
142
143 VROUTER_GET_VTY(vty, vrid, vr);
144 vrrp_add_ip(vr, ip);
145
146 if (vr->fsm.state == VRRP_STATE_INITIALIZE) {
147 vty_out(vty, "%% Activating Virtual Router %ld\n", vrid);
148 ret = vrrp_event(vr, VRRP_EVENT_STARTUP);
149 ret = ret < 0 ? CMD_WARNING_CONFIG_FAILED : CMD_SUCCESS;
150
151 if (ret == CMD_WARNING_CONFIG_FAILED)
152 vty_out(vty, "%% Failed to start Virtual Router %ld\n",
153 vrid);
154 } else {
155 ret = CMD_SUCCESS;
156 }
157
158 return ret;
159 }
160
161 static void vrrp_show(struct vty *vty, struct vrrp_vrouter *vr)
162 {
163 char ethstr[ETHER_ADDR_STRLEN];
164 char ipstr[INET_ADDRSTRLEN];
165 const char *stastr = vrrp_state_names[vr->fsm.state];
166
167 struct ttable *tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);
168
169 ttable_add_row(tt, "%s|%" PRIu32, "Virtual Router ID", vr->vrid);
170 prefix_mac2str(&vr->vr_mac_v4, ethstr, sizeof(ethstr));
171 ttable_add_row(tt, "%s|%s", "Virtual MAC", ethstr);
172 ttable_add_row(tt, "%s|%s", "Status", stastr);
173 ttable_add_row(tt, "%s|%" PRIu8, "Priority", vr->priority);
174 ttable_add_row(tt, "%s|%s", "Preempt Mode",
175 vr->preempt_mode ? "Yes" : "No");
176 ttable_add_row(tt, "%s|%s", "Accept Mode",
177 vr->accept_mode ? "Yes" : "No");
178 ttable_add_row(tt, "%s|%" PRIu16" cs", "Advertisement Interval",
179 vr->advertisement_interval);
180 ttable_add_row(tt, "%s|%" PRIu16" cs", "Master Advertisement Interval",
181 vr->master_adver_interval);
182 ttable_add_row(tt, "%s|%" PRIu16" cs", "Skew Time", vr->skew_time);
183 ttable_add_row(tt, "%s|%" PRIu16" cs", "Master Down Interval",
184 vr->master_down_interval);
185 ttable_add_row(tt, "%s|%u", "IPv4 Addresses", vr->v4->count);
186
187 char *table = ttable_dump(tt, "\n");
188 vty_out(vty, "\n%s\n", table);
189 XFREE(MTYPE_TMP, table);
190 ttable_del(tt);
191
192 /* Dump IPv4 Addresses */
193 if (vr->v4->count) {
194 vty_out(vty, " IPv4 Addresses\n");
195 vty_out(vty, " --------------\n");
196 struct listnode *ln;
197 struct in_addr *v4;
198 for (ALL_LIST_ELEMENTS_RO(vr->v4, ln, v4)) {
199 inet_ntop(AF_INET, v4, ipstr, sizeof(ipstr));
200 vty_out(vty, " %s\n", ipstr);
201 }
202 vty_out(vty, "\n");
203 }
204 }
205
206 DEFPY(vrrp_vrid_show,
207 vrrp_vrid_show_cmd,
208 "show vrrp [(1-255)$vrid]",
209 SHOW_STR
210 VRRP_STR
211 VRRP_VRID_STR)
212 {
213 struct vrrp_vrouter *vr;
214
215 if (vrid) {
216 VROUTER_GET_VTY(vty, vrid, vr);
217 vrrp_show(vty, vr);
218 } else {
219 struct list *ll = hash_to_list(vrrp_vrouters_hash);
220 struct listnode *ln;
221
222 for (ALL_LIST_ELEMENTS_RO(ll, ln, vr))
223 vrrp_show(vty, vr);
224 }
225
226 return CMD_SUCCESS;
227 }
228
229 static struct cmd_node interface_node = {
230 INTERFACE_NODE,
231 "%s(config-if)# ", 1
232 };
233
234 void vrrp_vty_init(void)
235 {
236 install_node(&interface_node, NULL);
237 if_cmd_init();
238 install_element(VIEW_NODE, &show_debugging_vrrpd_cmd);
239 install_element(VIEW_NODE, &vrrp_vrid_show_cmd);
240 install_element(INTERFACE_NODE, &vrrp_vrid_cmd);
241 install_element(INTERFACE_NODE, &vrrp_priority_cmd);
242 install_element(INTERFACE_NODE, &vrrp_advertisement_interval_cmd);
243 install_element(INTERFACE_NODE, &vrrp_ip_cmd);
244 }