/****************************************************************************
 * net/netforward/netfwd_alloc.c
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.  The
 * ASF licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <string.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>

#include <nuttx/mm/mempool.h>
#include <nuttx/net/tcp.h>
#include <nuttx/net/udp.h>
#include <nuttx/net/icmp.h>
#include <nuttx/net/icmpv6.h>

#include "netforward/netforward.h"

#ifdef CONFIG_NET_IPFORWARD

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#ifdef CONFIG_NET_IPv6
#  define L2_MAXHDRLEN IPv6_HDRLEN
#else
#  define L2_MAXHDRLEN IPv4_HDRLEN
#endif

#if defined(CONFIG_NET_TCP)
#  define L3_MAXHDRLEN TCP_HDRLEN /* Could be up to TCP_MAX_HDRLEN */
#elif defined(CONFIG_NET_UDP)
#  define L3_MAXHDRLEN UDP_HDRLEN
#elif defined(CONFIG_NET_ICMPv6)
#  define L3_MAXHDRLEN ICMPv6_HDRLEN
#elif defined(CONFIG_NET_ICMP)
#  define L3_MAXHDRLEN ICMP_HDRLEN
#endif

#define MAX_HDRLEN (L2_MAXHDRLEN + L3_MAXHDRLEN)

static_assert(MAX_HDRLEN <= CONFIG_IOB_BUFSIZE, "IOB buffer size too small");

/****************************************************************************
 * Private Data
 ****************************************************************************/

/* This is the state of the global forwarding structures */

MEMPOOL_DEFINE(g_fwdpool, sizeof(struct forward_s),
               CONFIG_NET_FORWARD_NSTRUCT,
               CONFIG_IOB_NBUFFERS - CONFIG_IOB_THROTTLE,
               CONFIG_NET_FORWARD_ALLOC_STRUCT);

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: netfwd_alloc
 *
 * Description:
 *   Allocate a forwarding structure by removing a pre-allocated entry from
 *   a free list.
 *
 * Assumptions:
 *   Caller holds the network lock.  Mutually excluvive access to the free
 *   list is assured by this lock.
 *
 ****************************************************************************/

FAR struct forward_s *netfwd_alloc(void)
{
  return mempool_zallocate(&g_fwdpool, 0);
}

/****************************************************************************
 * Name: netfwd_free
 *
 * Description:
 *   Free a forwarding structure by adding it to a free list.
 *
 * Assumptions:
 *   Caller holds the network lock.  Mutually excluvive access to the free
 *   list is assured by this lock.
 *
 ****************************************************************************/

void netfwd_free(FAR struct forward_s *fwd)
{
  mempool_release(&g_fwdpool, fwd);
}

#endif /* CONFIG_NET_IPFORWARD */