|
| 1 | +#include "pse.h" |
| 2 | +#include "ethtool.h" |
| 3 | +#include "../nl.h" |
| 4 | + |
| 5 | +#include <libubus.h> |
| 6 | +#include <libubox/blobmsg.h> |
| 7 | +#include <linux/ethtool_netlink.h> |
| 8 | +#include <string.h> |
| 9 | +#include <net/if.h> |
| 10 | + |
| 11 | +/* Keep your pse_fields exactly as agreed */ |
| 12 | +static const struct nl_field pse_fields[] = { |
| 13 | + { |
| 14 | + .name = "admin_state", |
| 15 | + .attr = ETHTOOL_A_C33_PSE_ADMIN_STATE, |
| 16 | + .type = NL_T_U32, |
| 17 | + .dir = NL_F_GET | NL_F_SET, |
| 18 | + }, |
| 19 | + { |
| 20 | + .name = "power_limit_max", |
| 21 | + .attr = ETHTOOL_A_C33_PSE_PW_LIMIT_MAX, |
| 22 | + .type = NL_T_U32, |
| 23 | + .dir = NL_F_GET, |
| 24 | + }, |
| 25 | + { |
| 26 | + .name = "power_limit_min", |
| 27 | + .attr = ETHTOOL_A_C33_PSE_PW_LIMIT_MIN, |
| 28 | + .type = NL_T_U32, |
| 29 | + .dir = NL_F_GET, |
| 30 | + }, |
| 31 | + { |
| 32 | + .name = "actual_power", |
| 33 | + .attr = ETHTOOL_A_C33_PSE_ACTUAL_PW, |
| 34 | + .type = NL_T_U32, |
| 35 | + .dir = NL_F_GET, |
| 36 | + }, |
| 37 | + { 0 } |
| 38 | +}; |
| 39 | + |
| 40 | +/* GET command */ |
| 41 | +static const struct nl_cmd pse_get_cmd = { |
| 42 | + .cmd = ETHTOOL_MSG_PSE_GET, |
| 43 | + .flags = NLM_F_REQUEST | NLM_F_ACK, |
| 44 | + .fields = pse_fields, |
| 45 | +}; |
| 46 | + |
| 47 | +/* SET command (sparse updates) */ |
| 48 | +static const struct nl_cmd pse_set_cmd = { |
| 49 | + .cmd = ETHTOOL_MSG_PSE_SET, |
| 50 | + .flags = NLM_F_REQUEST | NLM_F_ACK, |
| 51 | + .fields = pse_fields, |
| 52 | +}; |
| 53 | + |
| 54 | +/* UBUS policy: expect "ifname" string in GET/SET requests */ |
| 55 | +static const struct blobmsg_policy pse_policy[] = { |
| 56 | + { .name = "ifname", .type = BLOBMSG_TYPE_STRING }, |
| 57 | +}; |
| 58 | + |
| 59 | +/* GET method */ |
| 60 | +int pse_get(struct ubus_context *ctx, |
| 61 | + struct ubus_object *obj, |
| 62 | + struct ubus_request_data *req, |
| 63 | + const char *method, |
| 64 | + struct blob_attr *msg) |
| 65 | +{ |
| 66 | + struct blob_attr *tb[ARRAY_SIZE(pse_policy)]; |
| 67 | + char ifname[IFNAMSIZ]; |
| 68 | + struct blob_buf reply; |
| 69 | + |
| 70 | + blobmsg_parse(pse_policy, ARRAY_SIZE(pse_policy), tb, blob_data(msg), blob_len(msg)); |
| 71 | + if (!tb[0]) |
| 72 | + return UBUS_STATUS_INVALID_ARGUMENT; |
| 73 | + |
| 74 | + strncpy(ifname, blobmsg_get_string(tb[0]), sizeof(ifname) - 1); |
| 75 | + ifname[sizeof(ifname) - 1] = '\0'; |
| 76 | + |
| 77 | + blob_buf_init(&reply, 0); |
| 78 | + |
| 79 | + if (nl_do_get(NULL, ðtool_nl_family, &pse_get_cmd, |
| 80 | + ETHTOOL_A_HEADER_DEV_NAME, ifname, &reply) < 0) |
| 81 | + return UBUS_STATUS_UNKNOWN_ERROR; |
| 82 | + |
| 83 | + ubus_send_reply(ctx, req, reply.head); |
| 84 | + blob_buf_free(&reply); |
| 85 | + |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +/* SET method */ |
| 90 | +int pse_set(struct ubus_context *ctx, |
| 91 | + struct ubus_object *obj, |
| 92 | + struct ubus_request_data *req, |
| 93 | + const char *method, |
| 94 | + struct blob_attr *msg) |
| 95 | +{ |
| 96 | + struct blob_attr *tb[ARRAY_SIZE(pse_policy)]; |
| 97 | + char ifname[IFNAMSIZ]; |
| 98 | + const void *vals[] = { msg }; |
| 99 | + |
| 100 | + blobmsg_parse(pse_policy, ARRAY_SIZE(pse_policy), tb, blob_data(msg), blob_len(msg)); |
| 101 | + if (!tb[0]) |
| 102 | + return UBUS_STATUS_INVALID_ARGUMENT; |
| 103 | + |
| 104 | + strncpy(ifname, blobmsg_get_string(tb[0]), sizeof(ifname) - 1); |
| 105 | + ifname[sizeof(ifname) - 1] = '\0'; |
| 106 | + |
| 107 | + if (nl_do_set(NULL, ðtool_nl_family, &pse_set_cmd, |
| 108 | + ETHTOOL_A_HEADER_DEV_NAME, ifname, |
| 109 | + pse_fields, vals) < 0) |
| 110 | + return UBUS_STATUS_UNKNOWN_ERROR; |
| 111 | + |
| 112 | + return 0; |
| 113 | +} |
| 114 | + |
| 115 | +/* DUMP method (no arguments) */ |
| 116 | +int pse_dump(struct ubus_context *ctx, |
| 117 | + struct ubus_object *obj, |
| 118 | + struct ubus_request_data *req, |
| 119 | + const char *method, |
| 120 | + struct blob_attr *msg) |
| 121 | +{ |
| 122 | + struct blob_buf reply; |
| 123 | + blob_buf_init(&reply, 0); |
| 124 | + |
| 125 | + /* nl_do_dump: 5 arguments now, last is blob_buf* */ |
| 126 | + if (nl_do_dump(NULL, ðtool_nl_family, &pse_get_cmd, |
| 127 | + ETHTOOL_A_HEADER_DEV_NAME, &reply) < 0) { |
| 128 | + blob_buf_free(&reply); |
| 129 | + return UBUS_STATUS_UNKNOWN_ERROR; |
| 130 | + } |
| 131 | + |
| 132 | + ubus_send_reply(ctx, req, reply.head); |
| 133 | + blob_buf_free(&reply); |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +/* UBUS methods array */ |
| 140 | +static struct ubus_method pse_methods[] = { |
| 141 | + UBUS_METHOD("get", pse_get, pse_policy), |
| 142 | + UBUS_METHOD("set", pse_set, pse_policy), |
| 143 | + UBUS_METHOD_NOARG("dump", pse_dump), |
| 144 | +}; |
| 145 | + |
| 146 | +/* UBUS object type and object */ |
| 147 | +struct ubus_object_type pse_object_type = |
| 148 | + UBUS_OBJECT_TYPE("netlink.ethtool.pse", pse_methods); |
| 149 | + |
| 150 | +struct ubus_object ethtool_pse_object = { |
| 151 | + .name = "netlink.ethtool.pse", |
| 152 | + .type = &pse_object_type, |
| 153 | + .methods = pse_methods, |
| 154 | + .n_methods = ARRAY_SIZE(pse_methods), |
| 155 | +}; |
0 commit comments