// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023 Robin Jarry

#include "cli.h"
#include "cli_iface.h"
#include "display.h"

#include <gr_api.h>
#include <gr_infra.h>
#include <gr_net_types.h>

#include <ecoli.h>

#include <errno.h>
#include <sys/queue.h>

static void vlan_show(struct gr_api_client *c, const struct gr_iface *iface, struct gr_object *o) {
	const struct gr_iface_info_vlan *vlan = PAYLOAD(iface);

	gr_object_field(o, "vlan_parent", 0, "%s", iface_name_from_id(c, vlan->parent_id));
	gr_object_field(o, "vlan_id", GR_DISP_INT, "%u", vlan->vlan_id);
}

static void
vlan_list_info(struct gr_api_client *c, const struct gr_iface *iface, char *buf, size_t len) {
	const struct gr_iface_info_vlan *vlan = PAYLOAD(iface);

	snprintf(
		buf,
		len,
		"parent=%s vlan_id=%u",
		iface_name_from_id(c, vlan->parent_id),
		vlan->vlan_id
	);
}

static struct cli_iface_type vlan_type = {
	.type_id = GR_IFACE_TYPE_VLAN,
	.show = vlan_show,
	.list_info = vlan_list_info,
};

static uint64_t parse_vlan_args(
	struct gr_api_client *c,
	const struct ec_pnode *p,
	struct gr_iface *iface,
	bool update
) {
	struct gr_iface_info_vlan *vlan;
	uint64_t set_attrs;

	set_attrs = parse_iface_args(c, p, iface, sizeof(*vlan), update);
	vlan = (struct gr_iface_info_vlan *)iface->info;

	if (arg_str(p, "PARENT") != NULL) {
		if (arg_iface(c, p, "PARENT", GR_IFACE_TYPE_UNDEF, &vlan->parent_id) < 0)
			return 0;
		set_attrs |= GR_VLAN_SET_PARENT;
	}

	if (arg_u16(p, "VLAN", &vlan->vlan_id) == 0)
		set_attrs |= GR_VLAN_SET_VLAN;

	if (arg_eth_addr(p, "MAC", &vlan->mac) == 0) {
		set_attrs |= GR_VLAN_SET_MAC;
	} else if (!update) {
		struct gr_iface *parent = iface_from_id(c, vlan->parent_id);
		const struct gr_iface_info_port *port;
		const struct gr_iface_info_bond *bond;
		if (parent == NULL)
			return 0;
		switch (parent->type) {
		case GR_IFACE_TYPE_PORT:
			port = (const struct gr_iface_info_port *)parent->info;
			vlan->mac = port->mac;
			break;
		case GR_IFACE_TYPE_BOND:
			bond = (const struct gr_iface_info_bond *)parent->info;
			vlan->mac = bond->mac;
			break;
		default:
			errno = EMEDIUMTYPE;
			free(parent);
			return 0;
		}
		set_attrs |= GR_VLAN_SET_MAC;
		free(parent);
	}

	if (set_attrs == 0)
		errno = EINVAL;
	return set_attrs;
}

static cmd_status_t vlan_add(struct gr_api_client *c, const struct ec_pnode *p) {
	const struct gr_iface_add_resp *resp;
	struct gr_iface_add_req *req = NULL;
	void *resp_ptr = NULL;
	size_t len;

	len = sizeof(*req) + sizeof(struct gr_iface_info_vlan);
	if ((req = calloc(1, len)) == NULL)
		goto err;

	req->iface.type = GR_IFACE_TYPE_VLAN;
	req->iface.flags = GR_IFACE_F_UP;

	if (parse_vlan_args(c, p, &req->iface, false) == 0)
		goto err;

	if (gr_api_client_send_recv(c, GR_IFACE_ADD, len, req, &resp_ptr) < 0)
		goto err;

	free(req);
	resp = resp_ptr;
	printf("Created interface %u\n", resp->iface_id);
	free(resp_ptr);
	return CMD_SUCCESS;
err:
	free(req);
	return CMD_ERROR;
}

static cmd_status_t vlan_set(struct gr_api_client *c, const struct ec_pnode *p) {
	struct gr_iface_set_req *req = NULL;
	cmd_status_t ret = CMD_ERROR;
	size_t len;

	len = sizeof(*req) + sizeof(struct gr_iface_info_vlan);
	if ((req = calloc(1, len)) == NULL)
		goto out;

	if ((req->set_attrs = parse_vlan_args(c, p, &req->iface, true)) == 0)
		goto out;

	if (gr_api_client_send_recv(c, GR_IFACE_SET, len, req, NULL) < 0)
		goto out;

	ret = CMD_SUCCESS;
out:
	free(req);
	return ret;
}

#define VLAN_ATTRS_CMD IFACE_ATTRS_CMD ",(mac MAC)"

#define VLAN_ATTRS_ARGS                                                                            \
	IFACE_ATTRS_ARGS, with_help("Set the ethernet address.", ec_node_re("MAC", ETH_ADDR_RE))

static int ctx_init(struct ec_node *root) {
	int ret;

	ret = CLI_COMMAND(
		INTERFACE_ADD_CTX(root),
		"vlan NAME parent PARENT vlan_id VLAN [" VLAN_ATTRS_CMD "]",
		vlan_add,
		"Create a new VLAN sub interface.",
		with_help("Interface name.", ec_node("any", "NAME")),
		with_help(
			"Parent port interface.",
			ec_node_dyn("PARENT", complete_iface_names, INT2PTR(GR_IFACE_TYPE_PORT))
		),
		with_help("VLAN ID.", ec_node_uint("VLAN", 1, 4095, 10)),
		VLAN_ATTRS_ARGS
	);
	if (ret < 0)
		return ret;
	ret = CLI_COMMAND(
		INTERFACE_SET_CTX(root),
		"vlan NAME (name NEW_NAME),(parent PARENT),(vlan_id VLAN)," VLAN_ATTRS_CMD,
		vlan_set,
		"Modify VLAN parameters.",
		with_help(
			"Interface name.",
			ec_node_dyn("NAME", complete_iface_names, INT2PTR(GR_IFACE_TYPE_VLAN))
		),
		with_help("New interface name.", ec_node("any", "NEW_NAME")),
		with_help(
			"Parent port interface.",
			ec_node_dyn("PARENT", complete_iface_names, INT2PTR(GR_IFACE_TYPE_PORT))
		),
		with_help("VLAN ID.", ec_node_uint("VLAN", 1, 4095, 10)),
		VLAN_ATTRS_ARGS
	);
	if (ret < 0)
		return ret;

	return 0;
}

static struct cli_context ctx = {
	.name = "infra vlan",
	.init = ctx_init,
};

static void __attribute__((constructor, used)) init(void) {
	cli_context_register(&ctx);
	register_iface_type(&vlan_type);
}
