]>
Commit | Line | Data |
---|---|---|
994b3371 VB |
1 | /* -*- mode: c; c-file-style: "openbsd" -*- */ |
2 | /* | |
3 | * Copyright (c) 2013 Vincent Bernat <bernat@luffy.cx> | |
4 | * | |
5 | * Permission to use, copy, modify, and/or distribute this software for any | |
6 | * purpose with or without fee is hereby granted, provided that the above | |
7 | * copyright notice and this permission notice appear in all copies. | |
8 | * | |
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
16 | */ | |
17 | ||
18 | #include <unistd.h> | |
19 | #include <string.h> | |
20 | ||
21 | #include "client.h" | |
22 | #include "../log.h" | |
23 | ||
24 | static int | |
25 | cmd_txdelay(struct lldpctl_conn_t *conn, struct writer *w, | |
26 | struct cmd_env *env, void *arg) | |
27 | { | |
28 | log_debug("lldpctl", "set transmit delay"); | |
29 | ||
30 | lldpctl_atom_t *config = lldpctl_get_configuration(conn); | |
31 | if (config == NULL) { | |
32 | log_warnx("lldpctl", "unable to get configuration from lldpd. %s", | |
33 | lldpctl_last_strerror(conn)); | |
34 | return 0; | |
35 | } | |
36 | if (lldpctl_atom_set_str(config, | |
37 | lldpctl_k_config_tx_interval, cmdenv_get(env, "tx-interval")) == NULL) { | |
38 | log_warnx("lldpctl", "unable to set transmit delay. %s", | |
39 | lldpctl_last_strerror(conn)); | |
40 | lldpctl_atom_dec_ref(config); | |
41 | return 0; | |
42 | } | |
43 | log_info("lldpctl", "transmit delay set to new value"); | |
44 | lldpctl_atom_dec_ref(config); | |
45 | return 1; | |
46 | } | |
47 | ||
48 | static int | |
49 | cmd_txhold(struct lldpctl_conn_t *conn, struct writer *w, | |
50 | struct cmd_env *env, void *arg) | |
51 | { | |
52 | log_debug("lldpctl", "set transmit hold"); | |
53 | ||
54 | lldpctl_atom_t *config = lldpctl_get_configuration(conn); | |
55 | if (config == NULL) { | |
56 | log_warnx("lldpctl", "unable to get configuration from lldpd. %s", | |
57 | lldpctl_last_strerror(conn)); | |
58 | return 0; | |
59 | } | |
60 | if (lldpctl_atom_set_str(config, | |
61 | lldpctl_k_config_tx_hold, cmdenv_get(env, "tx-hold")) == NULL) { | |
62 | log_warnx("lldpctl", "unable to set transmit hold. %s", | |
63 | lldpctl_last_strerror(conn)); | |
64 | lldpctl_atom_dec_ref(config); | |
65 | return 0; | |
66 | } | |
67 | log_info("lldpctl", "transmit hold set to new value %s", cmdenv_get(env, "tx-hold")); | |
68 | lldpctl_atom_dec_ref(config); | |
69 | return 1; | |
70 | } | |
71 | ||
8e46010c AA |
72 | static int |
73 | cmd_portid_type_local(struct lldpctl_conn_t *conn, struct writer *w, | |
74 | struct cmd_env *env, void *arg) | |
75 | { | |
76 | lldpctl_atom_t *iface; | |
77 | const char *id = cmdenv_get(env, "port-id"); | |
c267d0f2 | 78 | const char *descr = cmdenv_get(env, "port-descr"); |
8e46010c AA |
79 | |
80 | if (!id || !strlen(id)) { | |
81 | log_warnx("lldpctl", "no id speficied"); | |
82 | return 0; | |
83 | } | |
84 | ||
85 | while ((iface = cmd_iterate_on_interfaces(conn, env))) { | |
86 | lldpctl_atom_t *port = lldpctl_get_port(iface); | |
87 | if (lldpctl_atom_set_str(port, lldpctl_k_port_id, id) == NULL) { | |
88 | log_warnx("lldpctl", "unable to set LLDP PortID." | |
89 | " %s", lldpctl_last_strerror(conn)); | |
90 | } | |
c267d0f2 AA |
91 | if (descr && lldpctl_atom_set_str(port, lldpctl_k_port_descr, descr) == NULL) { |
92 | log_warnx("lldpctl", "unable to set LLDP Port Description." | |
93 | " %s", lldpctl_last_strerror(conn)); | |
94 | } | |
8e46010c AA |
95 | lldpctl_atom_dec_ref(port); |
96 | } | |
97 | ||
98 | return 1; | |
99 | } | |
100 | ||
8fbd3195 ST |
101 | static int |
102 | cmd_portid_type(struct lldpctl_conn_t *conn, struct writer *w, | |
103 | struct cmd_env *env, void *arg) | |
104 | { | |
105 | char *value_str; | |
cf719ce8 | 106 | int value = -1; |
8fbd3195 ST |
107 | |
108 | log_debug("lldpctl", "lldp PortID TLV Subtype"); | |
109 | ||
110 | lldpctl_atom_t *config = lldpctl_get_configuration(conn); | |
111 | if (config == NULL) { | |
112 | log_warnx("lldpctl", | |
113 | "unable to get configuration from lldpd. %s", | |
114 | lldpctl_last_strerror(conn)); | |
115 | return 0; | |
116 | } | |
117 | ||
118 | value_str = arg; | |
119 | for (lldpctl_map_t *b_map = | |
120 | lldpctl_key_get_map(lldpctl_k_config_lldp_portid_type); | |
121 | b_map->string; b_map++) { | |
122 | if (!strcmp(b_map->string, value_str)) { | |
123 | value = b_map->value; | |
124 | break; | |
125 | } | |
126 | } | |
127 | ||
128 | if (value == -1) { | |
129 | log_warnx("lldpctl", "invalid value"); | |
130 | lldpctl_atom_dec_ref(config); | |
131 | return 0; | |
132 | } | |
133 | ||
134 | if (lldpctl_atom_set_int(config, | |
135 | lldpctl_k_config_lldp_portid_type, value) == NULL) { | |
136 | log_warnx("lldpctl", "unable to set LLDP PortID type." | |
137 | " %s", lldpctl_last_strerror(conn)); | |
138 | lldpctl_atom_dec_ref(config); | |
139 | return 0; | |
140 | } | |
141 | ||
ff0a278f | 142 | log_info("lldpctl", "LLDP PortID TLV type set to new value : %s", value_str); |
8fbd3195 ST |
143 | lldpctl_atom_dec_ref(config); |
144 | ||
145 | return 1; | |
146 | } | |
147 | ||
ca838758 AA |
148 | static int |
149 | cmd_chassis_cap_advertise(struct lldpctl_conn_t *conn, struct writer *w, | |
150 | struct cmd_env *env, void *arg) | |
151 | { | |
152 | lldpctl_atom_t *config = lldpctl_get_configuration(conn); | |
153 | if (config == NULL) { | |
154 | log_warnx("lldpctl", "unable to get configuration from lldpd. %s", | |
155 | lldpctl_last_strerror(conn)); | |
156 | return 0; | |
157 | } | |
158 | if (lldpctl_atom_set_int(config, | |
159 | lldpctl_k_config_chassis_cap_advertise, | |
160 | arg?1:0) == NULL) { | |
161 | log_warnx("lldpctl", "unable to %s chassis capabilities advertisement: %s", | |
162 | arg?"enable":"disable", | |
163 | lldpctl_last_strerror(conn)); | |
164 | lldpctl_atom_dec_ref(config); | |
165 | return 0; | |
166 | } | |
167 | log_info("lldpctl", "chassis capabilities advertisement %s", | |
168 | arg?"enabled":"disabled"); | |
169 | lldpctl_atom_dec_ref(config); | |
170 | return 1; | |
171 | } | |
172 | ||
994b3371 VB |
173 | /** |
174 | * Register `configure lldp` commands. | |
175 | * | |
176 | * Those are the commands that are related to the LLDP protocol but not | |
177 | * Dot1/Dot3/MED. Commands not related to LLDP should go in system instead. | |
178 | */ | |
179 | void | |
ca838758 AA |
180 | register_commands_configure_lldp(struct cmd_node *configure, |
181 | struct cmd_node *unconfigure) | |
994b3371 VB |
182 | { |
183 | struct cmd_node *configure_lldp = commands_new( | |
184 | configure, | |
185 | "lldp", "LLDP configuration", | |
186 | NULL, NULL, NULL); | |
ca838758 AA |
187 | struct cmd_node *unconfigure_lldp = commands_new( |
188 | unconfigure, | |
189 | "lldp", "LLDP configuration", | |
190 | NULL, NULL, NULL); | |
994b3371 VB |
191 | |
192 | commands_new( | |
193 | commands_new( | |
194 | commands_new(configure_lldp, | |
195 | "tx-interval", "Set LLDP transmit delay", | |
196 | cmd_check_no_env, NULL, "ports"), | |
197 | NULL, "LLDP transmit delay in seconds", | |
198 | NULL, cmd_store_env_value, "tx-interval"), | |
199 | NEWLINE, "Set LLDP transmit delay", | |
200 | NULL, cmd_txdelay, NULL); | |
201 | ||
202 | commands_new( | |
203 | commands_new( | |
204 | commands_new(configure_lldp, | |
205 | "tx-hold", "Set LLDP transmit hold", | |
206 | cmd_check_no_env, NULL, "ports"), | |
207 | NULL, "LLDP transmit hold in seconds", | |
208 | NULL, cmd_store_env_value, "tx-hold"), | |
209 | NEWLINE, "Set LLDP transmit hold", | |
210 | NULL, cmd_txhold, NULL); | |
8fbd3195 ST |
211 | |
212 | /* Now handle the various portid subtypes we can configure. */ | |
213 | struct cmd_node *configure_lldp_portid_type = commands_new( | |
214 | configure_lldp, | |
215 | "portidsubtype", "LLDP PortID TLV Subtype ", | |
216 | NULL, NULL, NULL); | |
217 | ||
218 | for (lldpctl_map_t *b_map = | |
219 | lldpctl_key_get_map(lldpctl_k_config_lldp_portid_type); | |
220 | b_map->string; b_map++) { | |
221 | if (!strcmp(b_map->string, "ifname")) { | |
222 | commands_new( | |
223 | commands_new(configure_lldp_portid_type, | |
224 | b_map->string, "Interface Name", | |
225 | NULL, NULL, NULL), | |
226 | NEWLINE, NULL, | |
227 | NULL, cmd_portid_type, | |
228 | b_map->string); | |
8e46010c | 229 | } else if (!strcmp(b_map->string, "local")) { |
c267d0f2 AA |
230 | struct cmd_node *port_id = commands_new( |
231 | commands_new(configure_lldp_portid_type, | |
232 | b_map->string, "Local", | |
233 | NULL, NULL, NULL), | |
ec5a3601 | 234 | NULL, "Port ID", |
c267d0f2 AA |
235 | NULL, cmd_store_env_value, "port-id"); |
236 | commands_new(port_id, | |
ec5a3601 | 237 | NEWLINE, "Set local port ID", |
8e46010c AA |
238 | NULL, cmd_portid_type_local, |
239 | b_map->string); | |
ec5a3601 | 240 | commands_new( |
332f8eea VB |
241 | commands_new( |
242 | commands_new(port_id, | |
243 | "description", | |
244 | "Also set port description", | |
245 | NULL, NULL, NULL), | |
246 | NULL, "Port description", | |
247 | NULL, cmd_store_env_value, "port-descr"), | |
ec5a3601 VB |
248 | NEWLINE, "Set local port ID and description", |
249 | NULL, cmd_portid_type_local, NULL); | |
8fbd3195 ST |
250 | } else if (!strcmp(b_map->string, "macaddress")) { |
251 | commands_new( | |
252 | commands_new(configure_lldp_portid_type, | |
253 | b_map->string, "MAC Address", | |
254 | NULL, NULL, NULL), | |
255 | NEWLINE, NULL, | |
256 | NULL, cmd_portid_type, | |
257 | b_map->string); | |
258 | } | |
259 | } | |
ca838758 AA |
260 | |
261 | commands_new( | |
262 | commands_new(configure_lldp, | |
263 | "capabilities-advertisements", | |
264 | "Enable chassis capabilities advertisement", | |
265 | NULL, NULL, NULL), | |
266 | NEWLINE, "Enable chassis capabilities advertisement", | |
267 | NULL, cmd_chassis_cap_advertise, "enable"); | |
268 | commands_new( | |
269 | commands_new(unconfigure_lldp, | |
270 | "capabilities-advertisements", | |
271 | "Don't enable chassis capabilities advertisement", | |
272 | NULL, NULL, NULL), | |
273 | NEWLINE, "Don't enable chassis capabilities advertisement", | |
274 | NULL, cmd_chassis_cap_advertise, NULL); | |
994b3371 | 275 | } |