]>
Commit | Line | Data |
---|---|---|
3155489a DL |
1 | /* |
2 | * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc. | |
3 | * | |
4 | * Permission to use, copy, modify, and distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
7 | * | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
15 | */ | |
16 | ||
17 | #ifndef _FRR_FERR_H | |
18 | #define _FRR_FERR_H | |
19 | ||
20 | /*********************************************************** | |
21 | * scroll down to the end of this file for a full example! * | |
22 | ***********************************************************/ | |
23 | ||
24 | #include <stdint.h> | |
25 | #include <limits.h> | |
26 | #include <errno.h> | |
27 | ||
7b526b61 QY |
28 | #include "vty.h" |
29 | ||
3155489a DL |
30 | /* return type when this error indication stuff is used. |
31 | * | |
32 | * guaranteed to have boolean evaluation to "false" when OK, "true" when error | |
33 | * (i.e. can be changed to pointer in the future if neccessary) | |
34 | * | |
35 | * For checking, always use "if (value)", nothing else. | |
36 | * Do _NOT_ use any integer constant (!= 0), or sign check (< 0). | |
37 | */ | |
38 | typedef int ferr_r; | |
39 | ||
40 | /* rough category of error indication */ | |
41 | enum ferr_kind { | |
42 | /* no error */ | |
43 | FERR_OK = 0, | |
44 | ||
45 | /* something isn't the way it's supposed to be. | |
46 | * (things that might otherwise be asserts, really) | |
47 | */ | |
48 | FERR_CODE_BUG, | |
49 | ||
50 | /* user-supplied parameters don't make sense or is inconsistent | |
51 | * if you can express a rule for it (e.g. "holdtime > 2 * keepalive"), | |
52 | * it's this category. | |
53 | */ | |
54 | FERR_CONFIG_INVALID, | |
55 | ||
56 | /* user-supplied parameters don't line up with reality | |
57 | * (IP address or interface not available, etc.) | |
58 | * NB: these are really TODOs where the code needs to be fixed to | |
59 | * respond to future changes! | |
60 | */ | |
61 | FERR_CONFIG_REALITY, | |
62 | ||
63 | /* out of some system resource (probably memory) | |
64 | * aka "you didn't spend enough money error" */ | |
65 | FERR_RESOURCE, | |
66 | ||
67 | /* system error (permission denied, etc.) */ | |
68 | FERR_SYSTEM, | |
69 | ||
70 | /* error return from some external library | |
71 | * (FERR_SYSTEM and FERR_LIBRARY are not strongly distinct) */ | |
72 | FERR_LIBRARY, | |
73 | }; | |
74 | ||
75 | struct ferr { | |
76 | /* code location */ | |
77 | const char *file; | |
78 | const char *func; | |
79 | int line; | |
80 | ||
81 | enum ferr_kind kind; | |
82 | ||
83 | /* unique_id is calculated as a checksum of source filename and error | |
84 | * message format (*before* calling vsnprintf). Line number and | |
85 | * function name are not used; this keeps the number reasonably static | |
86 | * across changes. | |
87 | */ | |
88 | uint32_t unique_id; | |
89 | ||
90 | char message[384]; | |
91 | ||
92 | /* valid if != 0. note "errno" might be preprocessor foobar. */ | |
93 | int errno_val; | |
94 | /* valid if pathname[0] != '\0' */ | |
95 | char pathname[PATH_MAX]; | |
96 | }; | |
97 | ||
7b526b61 | 98 | /* Numeric ranges assigned to daemons for use as error codes. */ |
0f41a262 DS |
99 | #define BABEL_FERR_START 0x01000001 |
100 | #define BABEL_FRRR_END 0x01FFFFFF | |
101 | #define BGP_FERR_START 0x02000001 | |
102 | #define BGP_FERR_END 0x02FFFFFF | |
103 | #define EIGRP_FERR_START 0x03000001 | |
104 | #define EIGRP_FERR_END 0x03FFFFFF | |
105 | #define ISIS_FERR_START 0x04000001 | |
106 | #define ISIS_FERR_END 0x04FFFFFF | |
107 | #define LDP_FERR_START 0x05000001 | |
108 | #define LDP_FERR_END 0x05FFFFFF | |
109 | #define LIB_FERR_START 0x06000001 | |
110 | #define LIB_FERR_END 0x06FFFFFF | |
111 | #define NHRP_FERR_START 0x07000001 | |
112 | #define NHRP_FERR_END 0x07FFFFFF | |
113 | #define OSPF_FERR_START 0x08000001 | |
114 | #define OSPF_FERR_END 0x08FFFFFF | |
115 | #define OSPFV3_FERR_START 0x09000001 | |
116 | #define OSPFV3_FERR_END 0x09FFFFFF | |
117 | #define PBR_FERR_START 0x0A000001 | |
118 | #define PBR_FERR_END 0x0AFFFFFF | |
119 | #define PIM_FERR_START 0x0B000001 | |
120 | #define PIM_FERR_STOP 0x0BFFFFFF | |
121 | #define RIP_FERR_START 0x0C000001 | |
122 | #define RIP_FERR_STOP 0x0CFFFFFF | |
123 | #define RIPNG_FERR_START 0x0D000001 | |
124 | #define RIPNG_FERR_STOP 0x0DFFFFFF | |
125 | #define SHARP_FERR_START 0x0E000001 | |
126 | #define SHARP_FERR_END 0x0EFFFFFF | |
127 | #define VTYSH_FERR_START 0x0F000001 | |
128 | #define VTYSH_FRR_END 0x0FFFFFFF | |
129 | #define WATCHFRR_FERR_START 0x10000001 | |
130 | #define WATCHFRR_FERR_END 0x10FFFFFF | |
131 | #define ZEBRA_FERR_START 0xF1000001 | |
132 | #define ZEBRA_FERR_END 0xF1FFFFFF | |
133 | #define END_FERR 0xFFFFFFFF | |
7b526b61 | 134 | |
85cd2f9f | 135 | struct log_ref { |
7b526b61 QY |
136 | /* Unique error code displayed to end user as a reference. -1 means |
137 | * this is an uncoded error that does not have reference material. */ | |
138 | uint32_t code; | |
139 | /* Ultra brief title */ | |
140 | const char *title; | |
141 | /* Brief description of error */ | |
142 | const char *description; | |
143 | /* Remedial suggestion */ | |
144 | const char *suggestion; | |
145 | }; | |
146 | ||
85cd2f9f QY |
147 | void log_ref_add(struct log_ref *ref); |
148 | struct log_ref *log_ref_get(uint32_t code); | |
149 | void log_ref_display(struct vty *vty, uint32_t code, bool json); | |
b66d022e DS |
150 | |
151 | /* | |
152 | * This function should be called by the | |
153 | * code in libfrr.c | |
154 | */ | |
85cd2f9f QY |
155 | void log_ref_init(void); |
156 | void log_ref_fini(void); | |
7b526b61 | 157 | |
3155489a DL |
158 | /* get error details. |
159 | * | |
160 | * NB: errval/ferr_r does NOT carry the full error information. It's only | |
161 | * passed around for future API flexibility. ferr_get_last always returns | |
162 | * the last error set in the current thread. | |
163 | */ | |
164 | const struct ferr *ferr_get_last(ferr_r errval); | |
165 | ||
af4d3437 QY |
166 | /* |
167 | * Can optionally be called at strategic locations. | |
168 | * Always returns 0. | |
169 | */ | |
3155489a DL |
170 | ferr_r ferr_clear(void); |
171 | ||
172 | /* do NOT call these functions directly. only for macro use! */ | |
173 | ferr_r ferr_set_internal(const char *file, int line, const char *func, | |
996c9314 | 174 | enum ferr_kind kind, const char *text, ...); |
3155489a | 175 | ferr_r ferr_set_internal_ext(const char *file, int line, const char *func, |
996c9314 LB |
176 | enum ferr_kind kind, const char *pathname, |
177 | int errno_val, const char *text, ...); | |
3155489a | 178 | |
996c9314 | 179 | #define ferr_ok() 0 |
3155489a DL |
180 | |
181 | /* Report an error. | |
182 | * | |
183 | * If you need to do cleanup (free memory, etc.), save the return value in a | |
184 | * variable of type ferr_r. | |
185 | * | |
186 | * Don't put a \n at the end of the error message. | |
187 | */ | |
996c9314 LB |
188 | #define ferr_code_bug(...) \ |
189 | ferr_set_internal(__FILE__, __LINE__, __func__, FERR_CODE_BUG, \ | |
190 | __VA_ARGS__) | |
191 | #define ferr_cfg_invalid(...) \ | |
192 | ferr_set_internal(__FILE__, __LINE__, __func__, FERR_CONFIG_INVALID, \ | |
193 | __VA_ARGS__) | |
194 | #define ferr_cfg_reality(...) \ | |
195 | ferr_set_internal(__FILE__, __LINE__, __func__, FERR_CONFIG_REALITY, \ | |
196 | __VA_ARGS__) | |
197 | #define ferr_cfg_resource(...) \ | |
198 | ferr_set_internal(__FILE__, __LINE__, __func__, FERR_RESOURCE, \ | |
199 | __VA_ARGS__) | |
200 | #define ferr_system(...) \ | |
201 | ferr_set_internal(__FILE__, __LINE__, __func__, FERR_SYSTEM, \ | |
202 | __VA_ARGS__) | |
203 | #define ferr_library(...) \ | |
204 | ferr_set_internal(__FILE__, __LINE__, __func__, FERR_LIBRARY, \ | |
205 | __VA_ARGS__) | |
3155489a DL |
206 | |
207 | /* extended information variants */ | |
996c9314 LB |
208 | #define ferr_system_errno(...) \ |
209 | ferr_set_internal_ext(__FILE__, __LINE__, __func__, FERR_SYSTEM, NULL, \ | |
210 | errno, __VA_ARGS__) | |
211 | #define ferr_system_path_errno(path, ...) \ | |
212 | ferr_set_internal_ext(__FILE__, __LINE__, __func__, FERR_SYSTEM, path, \ | |
213 | errno, __VA_ARGS__) | |
3155489a DL |
214 | |
215 | #include "vty.h" | |
216 | /* print error message to vty; $ERR is replaced by the error's message */ | |
217 | void vty_print_error(struct vty *vty, ferr_r err, const char *msg, ...); | |
218 | ||
996c9314 LB |
219 | #define CMD_FERR_DO(func, action, ...) \ |
220 | do { \ | |
221 | ferr_r cmd_retval = func; \ | |
222 | if (cmd_retval) { \ | |
223 | vty_print_error(vty, cmd_retval, __VA_ARGS__); \ | |
224 | action; \ | |
225 | } \ | |
3155489a DL |
226 | } while (0) |
227 | ||
996c9314 | 228 | #define CMD_FERR_RETURN(func, ...) \ |
7eb09438 | 229 | CMD_FERR_DO(func, return CMD_WARNING_CONFIG_FAILED, __VA_ARGS__) |
996c9314 | 230 | #define CMD_FERR_GOTO(func, label, ...) \ |
3155489a DL |
231 | CMD_FERR_DO(func, goto label, __VA_ARGS__) |
232 | ||
e894f9fd LB |
233 | /* example: uses bogus #define to keep indent.py happy */ |
234 | #ifdef THIS_IS_AN_EXAMPLE | |
3155489a DL |
235 | ferr_r foo_bar_set(struct object *obj, int bar) |
236 | { | |
237 | if (bar < 1 || bar >= 100) | |
e894f9fd LB |
238 | return ferr_config_invalid("bar setting (%d) must be 0<x<100", |
239 | bar); | |
3155489a | 240 | obj->bar = bar; |
e894f9fd | 241 | if (ioctl(obj->fd, bar)) |
3155489a DL |
242 | return ferr_system_errno("couldn't set bar to %d", bar); |
243 | ||
244 | return ferr_ok(); | |
245 | } | |
246 | ||
247 | DEFUN("bla") | |
248 | { | |
249 | CMD_FERR_RETURN(foo_bar_set(obj, atoi(argv[1])), | |
996c9314 | 250 | "command failed: $ERR\n"); |
3155489a DL |
251 | return CMD_SUCCESS; |
252 | } | |
253 | ||
e894f9fd | 254 | #endif /* THIS_IS_AN_EXAMPLE */ |
3155489a DL |
255 | |
256 | #endif /* _FERR_H */ |