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

#include "control_output.h"
#include "graph.h"
#include "ip4.h"
#include "mbuf.h"
#include "trace.h"

#include <rte_arp.h>

enum {
	CONTROL = 0,
	DROP,
	EDGE_COUNT,
};

static uint16_t arp_input_reply_process(
	struct rte_graph *graph,
	struct rte_node *node,
	void **objs,
	uint16_t nb_objs
) {
	const struct iface *iface;
	struct rte_arp_hdr *arp;
	struct nexthop *remote;
	struct rte_mbuf *mbuf;

	for (uint16_t i = 0; i < nb_objs; i++) {
		mbuf = objs[i];

		arp = rte_pktmbuf_mtod(mbuf, struct rte_arp_hdr *);
		if (gr_mbuf_is_traced(mbuf)) {
			struct rte_arp_hdr *t = gr_mbuf_trace_add(mbuf, node, sizeof(*t));
			*t = *arp;
		}

		iface = mbuf_data(mbuf)->iface;
		remote = nh4_lookup(iface->vrf_id, arp->arp_data.arp_sip);
		if (remote != NULL || (iface->flags & GR_IFACE_F_NEIGH_SNOOP)) {
			control_output_set_cb(mbuf, arp_probe_input_cb, 0);
			rte_node_enqueue_x1(graph, node, CONTROL, mbuf);
		} else {
			rte_node_enqueue_x1(graph, node, DROP, mbuf);
		}
	}

	return nb_objs;
}

static struct rte_node_register node = {
	.name = "arp_input_reply",

	.process = arp_input_reply_process,

	.nb_edges = EDGE_COUNT,
	.next_nodes = {
		[CONTROL] = "control_output",
		[DROP] = "arp_input_reply_drop",
	},
};

static struct gr_node_info info = {
	.node = &node,
	.type = GR_NODE_T_CONTROL | GR_NODE_T_L2,
	.trace_format = (gr_trace_format_cb_t)trace_arp_format,
};

GR_NODE_REGISTER(info);

GR_DROP_REGISTER(arp_input_reply_drop);
