1 /* -*- mode: c; c-file-style: "openbsd" -*- */
3 * Copyright (c) 2013 Vincent Bernat <bernat@luffy.cx>
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.
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.
20 #include <sys/utsname.h>
26 cmd_iface_pattern(struct lldpctl_conn_t
*conn
, struct writer
*w
,
27 struct cmd_env
*env
, void *arg
)
29 log_debug("lldpctl", "set iface pattern");
31 lldpctl_atom_t
*config
= lldpctl_get_configuration(conn
);
33 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
34 lldpctl_last_strerror(conn
));
38 const char *value
= cmdenv_get(env
, "iface-pattern");
39 if (lldpctl_atom_set_str(config
,
40 lldpctl_k_config_iface_pattern
,
42 log_warnx("lldpctl", "unable to set iface-pattern. %s",
43 lldpctl_last_strerror(conn
));
44 lldpctl_atom_dec_ref(config
);
47 log_info("lldpctl", "iface-pattern set to new value %s",
48 value
?value
:"(none)");
49 lldpctl_atom_dec_ref(config
);
54 cmd_iface_promisc(struct lldpctl_conn_t
*conn
, struct writer
*w
,
55 struct cmd_env
*env
, void *arg
)
57 lldpctl_atom_t
*config
= lldpctl_get_configuration(conn
);
59 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
60 lldpctl_last_strerror(conn
));
63 if (lldpctl_atom_set_int(config
,
64 lldpctl_k_config_iface_promisc
,
66 log_warnx("lldpctl", "unable to %s promiscuous mode: %s",
67 arg
?"enable":"disable",
68 lldpctl_last_strerror(conn
));
69 lldpctl_atom_dec_ref(config
);
72 log_info("lldpctl", "interface promiscuous mode %s",
73 arg
?"enabled":"disabled");
74 lldpctl_atom_dec_ref(config
);
79 cmd_system_description(struct lldpctl_conn_t
*conn
, struct writer
*w
,
80 struct cmd_env
*env
, void *arg
)
83 const char *what
= arg
;
85 if (!strcmp(what
, "system")) {
86 value
= cmdenv_get(env
, "description");
88 value
= cmdenv_get(env
, "platform");
91 log_debug("lldpctl", "set %s description", what
);
92 lldpctl_atom_t
*config
= lldpctl_get_configuration(conn
);
94 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
95 lldpctl_last_strerror(conn
));
98 if (lldpctl_atom_set_str(config
,
99 platform
?lldpctl_k_config_platform
:lldpctl_k_config_description
,
101 log_warnx("lldpctl", "unable to set description. %s",
102 lldpctl_last_strerror(conn
));
103 lldpctl_atom_dec_ref(config
);
106 log_info("lldpctl", "description set to new value %s",
107 value
?value
:"(none)");
108 lldpctl_atom_dec_ref(config
);
113 cmd_management(struct lldpctl_conn_t
*conn
, struct writer
*w
,
114 struct cmd_env
*env
, void *arg
)
116 log_debug("lldpctl", "set management pattern");
118 lldpctl_atom_t
*config
= lldpctl_get_configuration(conn
);
119 if (config
== NULL
) {
120 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
121 lldpctl_last_strerror(conn
));
125 const char *value
= cmdenv_get(env
, "management-pattern");
126 if (lldpctl_atom_set_str(config
,
127 lldpctl_k_config_mgmt_pattern
, value
) == NULL
) {
128 log_warnx("lldpctl", "unable to set management pattern. %s",
129 lldpctl_last_strerror(conn
));
130 lldpctl_atom_dec_ref(config
);
133 log_info("lldpctl", "management pattaren set to new value %s",
134 value
?value
:"(none)");
135 lldpctl_atom_dec_ref(config
);
140 cmd_hostname(struct lldpctl_conn_t
*conn
, struct writer
*w
,
141 struct cmd_env
*env
, void *arg
)
144 log_debug("lldpctl", "set system name");
146 lldpctl_atom_t
*config
= lldpctl_get_configuration(conn
);
147 if (config
== NULL
) {
148 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
149 lldpctl_last_strerror(conn
));
153 const char *value
= cmdenv_get(env
, "hostname");
154 if (value
&& strlen(value
) == 1 && value
[0] == '.') {
155 if (uname(&un
) < 0) {
156 log_warn("lldpctl", "cannot get node name");
161 if (lldpctl_atom_set_str(config
,
162 lldpctl_k_config_hostname
, value
) == NULL
) {
163 log_warnx("lldpctl", "unable to set system name. %s",
164 lldpctl_last_strerror(conn
));
165 lldpctl_atom_dec_ref(config
);
168 log_info("lldpctl", "system name set to new value %s",
169 value
?value
:"(none)");
170 lldpctl_atom_dec_ref(config
);
175 cmd_update_descriptions(struct lldpctl_conn_t
*conn
, struct writer
*w
,
176 struct cmd_env
*env
, void *arg
)
178 lldpctl_atom_t
*config
= lldpctl_get_configuration(conn
);
179 if (config
== NULL
) {
180 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
181 lldpctl_last_strerror(conn
));
184 if (lldpctl_atom_set_int(config
,
185 lldpctl_k_config_ifdescr_update
,
187 log_warnx("lldpctl", "unable to %s interface description update: %s",
188 arg
?"enable":"disable",
189 lldpctl_last_strerror(conn
));
190 lldpctl_atom_dec_ref(config
);
193 log_info("lldpctl", "interface description update %s",
194 arg
?"enabled":"disabled");
195 lldpctl_atom_dec_ref(config
);
200 cmd_bondslave_srcmac_type(struct lldpctl_conn_t
*conn
, struct writer
*w
,
201 struct cmd_env
*env
, void *arg
)
206 log_debug("lldpctl", "bond slave src mac");
208 lldpctl_atom_t
*config
= lldpctl_get_configuration(conn
);
209 if (config
== NULL
) {
211 "unable to get configuration from lldpd. %s",
212 lldpctl_last_strerror(conn
));
217 for (lldpctl_map_t
*b_map
=
218 lldpctl_key_get_map(lldpctl_k_config_bond_slave_src_mac_type
);
219 b_map
->string
; b_map
++) {
220 if (!strcmp(b_map
->string
, value_str
)) {
221 value
= b_map
->value
;
227 log_warnx("lldpctl", "invalid value");
231 if (lldpctl_atom_set_int(config
,
232 lldpctl_k_config_bond_slave_src_mac_type
, value
) == NULL
) {
233 log_warnx("lldpctl", "unable to set bond slave src mac type."
234 " %s", lldpctl_last_strerror(conn
));
235 lldpctl_atom_dec_ref(config
);
239 log_info("lldpctl", "bond slave src mac set to new value: %s",
241 lldpctl_atom_dec_ref(config
);
247 * Register `configure system bond-slave-src-mac-type`
250 register_commands_srcmac_type(struct cmd_node
*configure
)
252 struct cmd_node
*bond_slave_src_mac_type
=
253 commands_new(configure
,
254 "bond-slave-src-mac-type",
255 "Set LLDP bond slave source MAC type",
258 for (lldpctl_map_t
*b_map
=
259 lldpctl_key_get_map(lldpctl_k_config_bond_slave_src_mac_type
);
260 b_map
->string
; b_map
++) {
261 if (!strcmp(b_map
->string
, "real")) {
263 commands_new(bond_slave_src_mac_type
,
264 b_map
->string
, "Real mac",
267 NULL
, cmd_bondslave_srcmac_type
,
269 } else if (!strcmp(b_map
->string
, "zero")) {
271 commands_new(bond_slave_src_mac_type
,
272 b_map
->string
, "All zero mac",
275 NULL
, cmd_bondslave_srcmac_type
,
277 } else if (!strcmp(b_map
->string
, "fixed")) {
279 commands_new(bond_slave_src_mac_type
,
280 b_map
->string
, "Fixed value (3Com card)",
283 NULL
, cmd_bondslave_srcmac_type
,
285 } else if (!strcmp(b_map
->string
, "local")) {
287 commands_new(bond_slave_src_mac_type
,
288 b_map
->string
, "Real Mac with locally "
289 "administered bit set",
292 NULL
, cmd_bondslave_srcmac_type
,
299 * Register `configure system` commands.
301 * Those are the commands to configure protocol-independant stuff.
304 register_commands_configure_system(struct cmd_node
*configure
,
305 struct cmd_node
*unconfigure
)
307 struct cmd_node
*configure_system
= commands_new(
309 "system", "System configuration",
310 cmd_check_no_env
, NULL
, "ports");
311 struct cmd_node
*unconfigure_system
= commands_new(
313 "system", "System configuration",
314 cmd_check_no_env
, NULL
, "ports");
315 struct cmd_node
*configure_interface
= commands_new(
317 "interface", "Interface related items",
319 struct cmd_node
*unconfigure_interface
= commands_new(
321 "interface", "Interface related items",
326 commands_new(configure_system
,
327 "description", "Override chassis description",
329 NULL
, "Chassis description",
330 NULL
, cmd_store_env_value
, "description"),
331 NEWLINE
, "Override chassis description",
332 NULL
, cmd_system_description
, "system");
334 commands_new(unconfigure_system
,
335 "description", "Don't override chassis description",
337 NEWLINE
, "Don't override chassis description",
338 NULL
, cmd_system_description
, "system");
342 commands_new(configure_system
,
343 "platform", "Override platform description",
345 NULL
, "Platform description (CDP)",
346 NULL
, cmd_store_env_value
, "platform"),
347 NEWLINE
, "Override platform description",
348 NULL
, cmd_system_description
, "platform");
350 commands_new(unconfigure_system
,
351 "platform", "Don't override platform description",
353 NEWLINE
, "Don't override platform description",
354 NULL
, cmd_system_description
, "platform");
358 commands_new(configure_system
,
359 "hostname", "Override system name",
362 NULL
, cmd_store_env_value
, "hostname"),
363 NEWLINE
, "Override system name",
364 NULL
, cmd_hostname
, NULL
);
366 commands_new(unconfigure_system
,
367 "hostname", "Don't override system name",
369 NEWLINE
, "Don't override system name",
370 NULL
, cmd_hostname
, NULL
);
376 commands_new(configure_system
,
377 "ip", "IP related options",
379 "management", "IP management related options",
381 "pattern", "Set IP management pattern",
383 NULL
, "IP management pattern (comma-separated list of wildcards)",
384 NULL
, cmd_store_env_value
, "management-pattern"),
385 NEWLINE
, "Set IP management pattern",
386 NULL
, cmd_management
, NULL
);
390 commands_new(unconfigure_system
,
391 "ip", "IP related options",
393 "management", "IP management related options",
395 "pattern", "Delete any IP management pattern",
397 NEWLINE
, "Delete any IP management pattern",
398 NULL
, cmd_management
, NULL
);
402 commands_new(configure_interface
,
403 "pattern", "Set active interface pattern",
405 NULL
, "Interface pattern (comma-separated list of wildcards)",
406 NULL
, cmd_store_env_value
, "iface-pattern"),
407 NEWLINE
, "Set active interface pattern",
408 NULL
, cmd_iface_pattern
, NULL
);
410 commands_new(unconfigure_interface
,
411 "pattern", "Delete any interface pattern",
413 NEWLINE
, "Delete any interface pattern",
414 NULL
, cmd_iface_pattern
, NULL
);
417 commands_new(configure_interface
,
418 "description", "Update interface descriptions with neighbor name",
420 NEWLINE
, "Update interface descriptions with neighbor name",
421 NULL
, cmd_update_descriptions
, "enable");
423 commands_new(unconfigure_interface
,
424 "description", "Don't update interface descriptions with neighbor name",
426 NEWLINE
, "Don't update interface descriptions with neighbor name",
427 NULL
, cmd_update_descriptions
, NULL
);
430 commands_new(configure_interface
,
431 "promiscuous", "Enable promiscuous mode on managed interfaces",
433 NEWLINE
, "Enable promiscuous mode on managed interfaces",
434 NULL
, cmd_iface_promisc
, "enable");
436 commands_new(unconfigure_interface
,
437 "promiscuous", "Don't enable promiscuous mode on managed interfaces",
439 NEWLINE
, "Don't enable promiscuous mode on managed interfaces",
440 NULL
, cmd_iface_promisc
, NULL
);
442 register_commands_srcmac_type(configure_system
);