*/
 #include <linux/types.h>
 #include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/idr.h>
+#include <linux/rhashtable.h>
 
 #include <net/xdp.h>
 
 #define REG_STATE_UNREGISTERED 0x2
 #define REG_STATE_UNUSED       0x3
 
+static DEFINE_IDA(mem_id_pool);
+static DEFINE_MUTEX(mem_id_lock);
+#define MEM_ID_MAX 0xFFFE
+#define MEM_ID_MIN 1
+static int mem_id_next = MEM_ID_MIN;
+
+static bool mem_id_init; /* false */
+static struct rhashtable *mem_id_ht;
+
+struct xdp_mem_allocator {
+       struct xdp_mem_info mem;
+       void *allocator;
+       struct rhash_head node;
+       struct rcu_head rcu;
+};
+
+static u32 xdp_mem_id_hashfn(const void *data, u32 len, u32 seed)
+{
+       const u32 *k = data;
+       const u32 key = *k;
+
+       BUILD_BUG_ON(FIELD_SIZEOF(struct xdp_mem_allocator, mem.id)
+                    != sizeof(u32));
+
+       /* Use cyclic increasing ID as direct hash key, see rht_bucket_index */
+       return key << RHT_HASH_RESERVED_SPACE;
+}
+
+static int xdp_mem_id_cmp(struct rhashtable_compare_arg *arg,
+                         const void *ptr)
+{
+       const struct xdp_mem_allocator *xa = ptr;
+       u32 mem_id = *(u32 *)arg->key;
+
+       return xa->mem.id != mem_id;
+}
+
+static const struct rhashtable_params mem_id_rht_params = {
+       .nelem_hint = 64,
+       .head_offset = offsetof(struct xdp_mem_allocator, node),
+       .key_offset  = offsetof(struct xdp_mem_allocator, mem.id),
+       .key_len = FIELD_SIZEOF(struct xdp_mem_allocator, mem.id),
+       .max_size = MEM_ID_MAX,
+       .min_size = 8,
+       .automatic_shrinking = true,
+       .hashfn    = xdp_mem_id_hashfn,
+       .obj_cmpfn = xdp_mem_id_cmp,
+};
+
+static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu)
+{
+       struct xdp_mem_allocator *xa;
+
+       xa = container_of(rcu, struct xdp_mem_allocator, rcu);
+
+       /* Allow this ID to be reused */
+       ida_simple_remove(&mem_id_pool, xa->mem.id);
+
+       /* TODO: Depending on allocator type/pointer free resources */
+
+       /* Poison memory */
+       xa->mem.id = 0xFFFF;
+       xa->mem.type = 0xF0F0;
+       xa->allocator = (void *)0xDEAD9001;
+
+       kfree(xa);
+}
+
+static void __xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
+{
+       struct xdp_mem_allocator *xa;
+       int id = xdp_rxq->mem.id;
+       int err;
+
+       if (id == 0)
+               return;
+
+       mutex_lock(&mem_id_lock);
+
+       xa = rhashtable_lookup(mem_id_ht, &id, mem_id_rht_params);
+       if (!xa) {
+               mutex_unlock(&mem_id_lock);
+               return;
+       }
+
+       err = rhashtable_remove_fast(mem_id_ht, &xa->node, mem_id_rht_params);
+       WARN_ON(err);
+
+       call_rcu(&xa->rcu, __xdp_mem_allocator_rcu_free);
+
+       mutex_unlock(&mem_id_lock);
+}
+
 void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
 {
        /* Simplify driver cleanup code paths, allow unreg "unused" */
 
        WARN(!(xdp_rxq->reg_state == REG_STATE_REGISTERED), "Driver BUG");
 
+       __xdp_rxq_info_unreg_mem_model(xdp_rxq);
+
        xdp_rxq->reg_state = REG_STATE_UNREGISTERED;
        xdp_rxq->dev = NULL;
+
+       /* Reset mem info to defaults */
+       xdp_rxq->mem.id = 0;
+       xdp_rxq->mem.type = 0;
 }
 EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg);
 
 }
 EXPORT_SYMBOL_GPL(xdp_rxq_info_is_reg);
 
+static int __mem_id_init_hash_table(void)
+{
+       struct rhashtable *rht;
+       int ret;
+
+       if (unlikely(mem_id_init))
+               return 0;
+
+       rht = kzalloc(sizeof(*rht), GFP_KERNEL);
+       if (!rht)
+               return -ENOMEM;
+
+       ret = rhashtable_init(rht, &mem_id_rht_params);
+       if (ret < 0) {
+               kfree(rht);
+               return ret;
+       }
+       mem_id_ht = rht;
+       smp_mb(); /* mutex lock should provide enough pairing */
+       mem_id_init = true;
+
+       return 0;
+}
+
+/* Allocate a cyclic ID that maps to allocator pointer.
+ * See: https://www.kernel.org/doc/html/latest/core-api/idr.html
+ *
+ * Caller must lock mem_id_lock.
+ */
+static int __mem_id_cyclic_get(gfp_t gfp)
+{
+       int retries = 1;
+       int id;
+
+again:
+       id = ida_simple_get(&mem_id_pool, mem_id_next, MEM_ID_MAX, gfp);
+       if (id < 0) {
+               if (id == -ENOSPC) {
+                       /* Cyclic allocator, reset next id */
+                       if (retries--) {
+                               mem_id_next = MEM_ID_MIN;
+                               goto again;
+                       }
+               }
+               return id; /* errno */
+       }
+       mem_id_next = id + 1;
+
+       return id;
+}
+
 int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
                               enum xdp_mem_type type, void *allocator)
 {
+       struct xdp_mem_allocator *xdp_alloc;
+       gfp_t gfp = GFP_KERNEL;
+       int id, errno, ret;
+       void *ptr;
+
+       if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
+               WARN(1, "Missing register, driver bug");
+               return -EFAULT;
+       }
+
        if (type >= MEM_TYPE_MAX)
                return -EINVAL;
 
        xdp_rxq->mem.type = type;
 
-       if (allocator)
-               return -EOPNOTSUPP;
+       if (!allocator)
+               return 0;
+
+       /* Delay init of rhashtable to save memory if feature isn't used */
+       if (!mem_id_init) {
+               mutex_lock(&mem_id_lock);
+               ret = __mem_id_init_hash_table();
+               mutex_unlock(&mem_id_lock);
+               if (ret < 0) {
+                       WARN_ON(1);
+                       return ret;
+               }
+       }
+
+       xdp_alloc = kzalloc(sizeof(*xdp_alloc), gfp);
+       if (!xdp_alloc)
+               return -ENOMEM;
+
+       mutex_lock(&mem_id_lock);
+       id = __mem_id_cyclic_get(gfp);
+       if (id < 0) {
+               errno = id;
+               goto err;
+       }
+       xdp_rxq->mem.id = id;
+       xdp_alloc->mem  = xdp_rxq->mem;
+       xdp_alloc->allocator = allocator;
+
+       /* Insert allocator into ID lookup table */
+       ptr = rhashtable_insert_slow(mem_id_ht, &id, &xdp_alloc->node);
+       if (IS_ERR(ptr)) {
+               errno = PTR_ERR(ptr);
+               goto err;
+       }
+
+       mutex_unlock(&mem_id_lock);
 
-       /* TODO: Allocate an ID that maps to allocator pointer
-        * See: https://www.kernel.org/doc/html/latest/core-api/idr.html
-        */
        return 0;
+err:
+       mutex_unlock(&mem_id_lock);
+       kfree(xdp_alloc);
+       return errno;
 }
 EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
+
+void xdp_return_frame(void *data, struct xdp_mem_info *mem)
+{
+       if (mem->type == MEM_TYPE_PAGE_SHARED) {
+               page_frag_free(data);
+               return;
+       }
+
+       if (mem->type == MEM_TYPE_PAGE_ORDER0) {
+               struct page *page = virt_to_page(data); /* Assumes order0 page*/
+
+               put_page(page);
+       }
+}
+EXPORT_SYMBOL_GPL(xdp_return_frame);