return dev;
}
+
+void
+device_dump_status(struct blob_buf *b, struct device *dev)
+{
+ void *c, *s;
+
+ if (!dev) {
+ avl_for_each_element(&devices, dev, avl) {
+ if (!dev->present)
+ continue;
+ c = blobmsg_open_table(b, dev->ifname);
+ device_dump_status(b, dev);
+ blobmsg_close_table(b, c);
+ }
+
+ return;
+ }
+
+ if (!dev->present)
+ return;
+
+ blobmsg_add_string(b, "type", dev->type->name);
+ blobmsg_add_u8(b, "up", !!dev->active);
+ if (dev->type->dump_info)
+ dev->type->dump_info(dev, b);
+
+ s = blobmsg_open_table(b, "statistics");
+ if (dev->type->dump_stats)
+ dev->type->dump_stats(dev, b);
+ else
+ system_if_dump_stats(dev, b);
+ blobmsg_close_table(b, s);
+}
+#define _GNU_SOURCE
+
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
return 0;
}
+int system_if_dump_stats(struct device *dev, struct blob_buf *b)
+{
+ const char *const counters[] = {
+ "collisions", "rx_frame_errors", "tx_compressed",
+ "multicast", "rx_length_errors", "tx_dropped",
+ "rx_bytes", "rx_missed_errors", "tx_errors",
+ "rx_compressed", "rx_over_errors", "tx_fifo_errors",
+ "rx_crc_errors", "rx_packets", "tx_heartbeat_errors",
+ "rx_dropped", "tx_aborted_errors", "tx_packets",
+ "rx_errors", "tx_bytes", "tx_window_errors",
+ "rx_fifo_errors", "tx_carrier_errors",
+ };
+ char buf[64];
+ int stats_dir;
+ int i, fd, len;
+
+ snprintf(buf, sizeof(buf), "/sys/class/net/%s/statistics", dev->ifname);
+ stats_dir = open(buf, O_DIRECTORY);
+ if (stats_dir < 0)
+ return -1;
+
+ for (i = 0; i < ARRAY_SIZE(counters); i++) {
+ fd = openat(stats_dir, counters[i], O_RDONLY);
+ if (fd < 0)
+ continue;
+
+retry:
+ len = read(fd, buf, sizeof(buf));
+ if (len < 0) {
+ if (errno == EINTR)
+ goto retry;
+ continue;
+ }
+
+ buf[len] = 0;
+ blobmsg_add_u32(b, counters[i], strtoul(buf, NULL, 0));
+ close(fd);
+ }
+
+ close(stats_dir);
+ return 0;
+}
+
static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
{
int alen = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
/* global object */
-enum {
- DEV_NAME,
- DEV_FORCE,
- __DEV_MAX,
- __DEV_MAX_NOFORCE = __DEV_MAX - 1,
-};
-
-static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
- [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
- [DEV_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_INT8 },
-};
-
-static int
-netifd_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
- struct ubus_request_data *req, const char *method,
- struct blob_attr *msg)
-{
- struct device *dev;
- struct blob_attr *tb[__DEV_MAX];
- bool add = !strncmp(method, "add", 3);
-
- blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
-
- if (!tb[DEV_NAME])
- return UBUS_STATUS_INVALID_ARGUMENT;
-
- dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
- if (!dev)
- return UBUS_STATUS_NOT_FOUND;
-
- if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
- device_set_present(dev, add);
- else
- device_check_state(dev);
-
- return 0;
-}
-
static int
netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
}
static struct ubus_method main_object_methods[] = {
- UBUS_METHOD("add_device", netifd_handle_device, dev_policy),
- UBUS_METHOD("remove_device", netifd_handle_device, dev_policy),
{ .name = "restart", .handler = netifd_handle_restart },
{ .name = "reload", .handler = netifd_handle_reload },
};
.n_methods = ARRAY_SIZE(main_object_methods),
};
+enum {
+ DEV_NAME,
+ __DEV_MAX,
+};
+
+static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
+ [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
+};
+
+static int
+netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct device *dev = NULL;
+ struct blob_attr *tb[__DEV_MAX];
+
+ blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
+
+ if (tb[DEV_NAME]) {
+ dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
+ if (!dev)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+ }
+
+ blob_buf_init(&b, 0);
+ device_dump_status(&b, dev);
+ ubus_send_reply(ctx, req, b.head);
+
+ return 0;
+}
+
+static struct ubus_method dev_object_methods[] = {
+ UBUS_METHOD("status", netifd_dev_status, dev_policy)
+};
+
+static struct ubus_object_type dev_object_type =
+ UBUS_OBJECT_TYPE("device", dev_object_methods);
+
+static struct ubus_object dev_object = {
+ .name = "network.device",
+ .type = &dev_object_type,
+ .methods = dev_object_methods,
+ .n_methods = ARRAY_SIZE(dev_object_methods),
+};
+
int
netifd_ubus_init(const char *path)
{
ubus_add_uloop(ctx);
ret = ubus_add_object(ctx, &main_object);
+ if (ret)
+ goto out;
+
+ ret = ubus_add_object(ctx, &dev_object);
+
+out:
if (ret != 0)
fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
-
- return 0;
+ return ret;
}
void
devinfo = blobmsg_open_table(&b, field);
blobmsg_add_string(&b, "name", dev->ifname);
- if (dev->type->dump_status)
- dev->type->dump_status(dev, &b);
-
blobmsg_close_table(&b, devinfo);
}
{ .name = "down", .handler = netifd_handle_down },
{ .name = "status", .handler = netifd_handle_status },
{ .name = "add_device", .handler = netifd_iface_handle_device,
- .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
+ .policy = dev_policy, .n_policy = __DEV_MAX },
{ .name = "remove_device", .handler = netifd_iface_handle_device,
- .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
+ .policy = dev_policy, .n_policy = __DEV_MAX },
{ .name = "notify_proto", .handler = netifd_iface_notify_proto },
{ .name = "remove", .handler = netifd_iface_remove }
};