2 * Copyright (c) 2001-2002
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
5 * Copyright (c) 2003-2004
9 * Author: Hartmut Brandt <harti@freebsd.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "atmconfig.h"
37 #include "atmconfig_device.h"
41 #include <bsnmp/asn1.h>
42 #include <bsnmp/snmp.h>
43 #include <bsnmp/snmpclient.h>
46 * Description of the begemotAtmIfTable
48 static const struct snmp_table atmif_table = {
49 OIDX_begemotAtmIfTable,
50 OIDX_begemotAtmIfTableLastChange, 2,
55 SNMP_SYNTAX_INTEGER, offsetof(struct atmif, index) },
56 { OID_begemotAtmIfName,
57 SNMP_SYNTAX_OCTETSTRING, offsetof(struct atmif, ifname) },
58 { OID_begemotAtmIfPcr,
59 SNMP_SYNTAX_GAUGE, offsetof(struct atmif, pcr) },
60 { OID_begemotAtmIfMedia,
61 SNMP_SYNTAX_INTEGER, offsetof(struct atmif, media) },
62 { OID_begemotAtmIfVpiBits,
63 SNMP_SYNTAX_GAUGE, offsetof(struct atmif, vpi_bits) },
64 { OID_begemotAtmIfVciBits,
65 SNMP_SYNTAX_GAUGE, offsetof(struct atmif, vci_bits) },
66 { OID_begemotAtmIfMaxVpcs,
67 SNMP_SYNTAX_GAUGE, offsetof(struct atmif, max_vpcs) },
68 { OID_begemotAtmIfMaxVccs,
69 SNMP_SYNTAX_GAUGE, offsetof(struct atmif, max_vccs) },
70 { OID_begemotAtmIfEsi,
71 SNMP_SYNTAX_OCTETSTRING, offsetof(struct atmif, esi) },
72 { OID_begemotAtmIfCarrierStatus,
73 SNMP_SYNTAX_INTEGER, offsetof(struct atmif, carrier) },
74 { OID_begemotAtmIfMode,
75 SNMP_SYNTAX_INTEGER, offsetof(struct atmif, mode) },
76 { 0, SNMP_SYNTAX_NULL, 0 }
80 /* List of all ATM interfaces */
81 struct atmif_list atmif_list = TAILQ_HEAD_INITIALIZER(atmif_list);
87 TAILQ_ENTRY(atmhw) link;
96 uint32_t soft_version;
98 TAILQ_HEAD(atmhw_list, atmhw);
100 /* list of ATM hardware */
101 static struct atmhw_list atmhw_list;
104 * Read ATM hardware table
106 const struct snmp_table atmhw_table = {
107 OIDX_begemotAtmHWTable,
108 OIDX_begemotAtmIfTableLastChange, 2,
109 sizeof(struct atmhw),
113 SNMP_SYNTAX_INTEGER, offsetof(struct atmhw, index) },
114 { OID_begemotAtmHWVendor,
115 SNMP_SYNTAX_OCTETSTRING, offsetof(struct atmhw, vendor) },
116 { OID_begemotAtmHWDevice,
117 SNMP_SYNTAX_OCTETSTRING, offsetof(struct atmhw, device) },
118 { OID_begemotAtmHWSerial,
119 SNMP_SYNTAX_GAUGE, offsetof(struct atmhw, serial) },
120 { OID_begemotAtmHWVersion,
121 SNMP_SYNTAX_GAUGE, offsetof(struct atmhw, version) },
122 { OID_begemotAtmHWSoftVersion,
123 SNMP_SYNTAX_GAUGE, offsetof(struct atmhw, soft_version) },
124 { 0, SNMP_SYNTAX_NULL, 0 }
128 static void device_status(int, char *[]);
129 static void device_hardware(int, char *[]);
130 static void device_modify(int, char *[]);
132 static const struct cmdtab device_tab[] = {
133 { "hardware", NULL, device_hardware },
134 { "status", NULL, device_status },
135 { "modify", NULL, device_modify },
139 static const struct cmdtab entry =
140 { "device", device_tab, NULL };
142 static DEF_MODULE(&entry);
145 * Carrier state to string
147 static const struct penum strcarrier[] = {
155 * SUNI mode to string
157 static const struct penum strsunimode[] = {
167 static const struct asn_oid
168 oid_begemotAtmIfMode = OIDX_begemotAtmIfMode;
171 * Print 1st status line
174 dev_status1(const struct atmif *aif)
178 printf("%-5u %-8s %-6u %-4u %-5u %-4u %-5u "
179 "%02x:%02x:%02x:%02x:%02x:%02x %s\n", aif->index,
180 aif->ifname, aif->pcr,
181 (1 << aif->vpi_bits) - 1, (1 << aif->vci_bits) - 1,
182 aif->max_vpcs, aif->max_vccs, aif->esi[0],
183 aif->esi[1], aif->esi[2], aif->esi[3], aif->esi[4], aif->esi[5],
184 penum(aif->carrier, strcarrier, buf));
188 * Print 2nd status line
191 dev_status2(const struct atmif *aif)
195 printf("%-5u %-8s %s\n", aif->index, aif->ifname,
196 penum(aif->mode, strsunimode, buf));
200 * Implement the 'device status' command
203 device_status(int argc, char *argv[])
207 static const struct option opts[] = {
212 "Interface Max Max\n"
213 "Index Name PCR VPI VCI VPCs VCCs ESI Carrier\n";
218 while ((opt = parse_options(&argc, &argv, opts)) != -1)
222 snmp_open(NULL, NULL, NULL, NULL);
227 if (TAILQ_EMPTY(&atmif_list))
228 errx(1, "no ATM interfaces found");
232 for (i = 0; i < argc; i++) {
233 if ((aif = atmif_find_name(argv[i])) == NULL) {
234 warnx("%s: no such ATM interface", argv[i]);
241 for (i = 0; i < argc; i++) {
242 if ((aif = atmif_find_name(argv[i])) == NULL)
249 TAILQ_FOREACH(aif, &atmif_list, link) {
254 TAILQ_FOREACH(aif, &atmif_list, link) {
262 * Print hardware info line
265 dev_hardware(const struct atmif *aif)
267 const struct atmhw *hw;
269 TAILQ_FOREACH(hw, &atmhw_list, link)
270 if (aif->index == hw->index)
273 warnx("hardware info not found for '%s'", aif->ifname);
277 printf("%-5u %-8s %-16s%-10s %-10u %-10u %u\n", aif->index,
278 aif->ifname, hw->vendor, hw->device, hw->serial,
279 hw->version, hw->soft_version);
283 * Show hardware configuration
286 device_hardware(int argc, char *argv[])
291 static const struct option opts[] = {
295 static const char headline[] =
297 "Index Name Vendor Card Serial HW SW\n";
299 while ((opt = parse_options(&argc, &argv, opts)) != -1)
303 snmp_open(NULL, NULL, NULL, NULL);
308 if (snmp_table_fetch(&atmhw_table, &atmhw_list) != 0)
309 errx(1, "AtmHW table: %s", snmp_client.error);
313 for (i = 0; i < argc; i++) {
314 if ((aif = atmif_find_name(argv[i])) == NULL) {
315 warnx("interface not found '%s'", argv[i]);
323 TAILQ_FOREACH(aif, &atmif_list, link) {
331 * Change device parameters
334 device_modify(int argc, char *argv[])
340 struct snmp_pdu pdu, resp;
342 static const struct option opts[] = {
343 #define MODIFY_MODE 0
344 { "mode", OPT_STRING, NULL },
348 while ((opt = parse_options(&argc, &argv, opts)) != -1)
352 if (pparse(&mode, strsunimode, optarg) == -1 ||
354 errx(1, "illegal mode for -m '%s'", optarg);
359 errx(1, "device modify needs one argument");
361 snmp_open(NULL, NULL, NULL, NULL);
366 if ((aif = atmif_find_name(argv[0])) == NULL)
367 errx(1, "%s: no such ATM interface", argv[0]);
369 snmp_pdu_create(&pdu, SNMP_PDU_SET);
371 n = snmp_add_binding(&pdu,
372 &oid_begemotAtmIfMode, SNMP_SYNTAX_INTEGER,
374 snmp_oid_append(&pdu.bindings[n + 0].var, "i",
375 (asn_subid_t)aif->index);
376 pdu.bindings[n + 0].v.integer = mode;
379 if (pdu.nbindings == 0)
380 errx(1, "must specify something to modify");
382 if (snmp_dialog(&pdu, &resp))
383 errx(1, "No response from '%s': %s", snmp_client.chost,
386 if (snmp_pdu_check(&pdu, &resp) <= 0)
387 errx(1, "Error modifying device");
389 snmp_pdu_free(&resp);
393 /* XXX while this is compiled in */
395 device_register(void)
397 register_module(&amodule_1);
401 * Fetch the ATM interface table
404 atmif_fetchtable(void)
408 while ((aif = TAILQ_FIRST(&atmif_list)) != NULL) {
411 TAILQ_REMOVE(&atmif_list, aif, link);
415 if (snmp_table_fetch(&atmif_table, &atmif_list) != 0)
416 errx(1, "AtmIf table: %s", snmp_client.error);
420 * Find a named ATM interface
423 atmif_find_name(const char *ifname)
427 TAILQ_FOREACH(atmif, &atmif_list, link)
428 if (strcmp(atmif->ifname, ifname) == 0)
433 * find an ATM interface by index
436 atmif_find(u_int idx)
440 TAILQ_FOREACH(atmif, &atmif_list, link)
441 if (atmif->index == (int32_t)idx)