summaryrefslogtreecommitdiffstats
path: root/src/kern_wg.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/kern_wg.c')
-rw-r--r--src/kern_wg.c391
1 files changed, 0 insertions, 391 deletions
diff --git a/src/kern_wg.c b/src/kern_wg.c
deleted file mode 100644
index f6d8defe89e..00000000000
--- a/src/kern_wg.c
+++ /dev/null
@@ -1,391 +0,0 @@
-/*
- * Copyright (c) 2019 Matt Dunwoodie <ncon@noconroy.net>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <sys/param.h>
-
-#include <sys/antireplay.h>
-#include <sys/bloombucket.h>
-#include <sys/fixedmap.h>
-#include <sys/mpq.h>
-
-/*
- * antireplay.h
- */
-
-/*
- * The following defines assist the antireplay_check function. *
- * ANTIREPLAY_INTEGER: the integer in the bitmap corresponding to num *
- * ANTIREPLAY_INTEGERBIT: the integer with corresponding single bit set
- */
-#define ANTIREPLAY_INTEGER(ctx, num) (ctx->ar_bitmap[num % ARB_BITS / ARI_BITS])
-#define ANTIREPLAY_INTEGERBIT(num) (1llu << (num & (ARI_BITS - 1)))
-
-void
-antireplay_init(struct antireplay *ctx)
-{
- /* We just zero out the struct, expecting that then ctx->ar_head == 0 */
- explicit_bzero(ctx, sizeof(struct antireplay));
-}
-
-int
-antireplay_update(struct antireplay *ctx, uint64_t num)
-{
- /* Bits after ctx->ar_head need to be zeroed. This is called when num is
- * in front of ctx->ar_head, and those bits need to be set to 0 */
- if (num < ctx->ar_head + ARB_BITS / ARI_BITS) {
- for (; ctx->ar_head <= num; ctx->ar_head += ARI_BITS) {
- ANTIREPLAY_INTEGER(ctx, (ctx->ar_head + 1)) = 0;
- }
- } else {
- bzero(ctx->ar_bitmap, ARB_BITS / ARI_BITS);
- }
-
- if (ctx->ar_head > (num + ARB_BITS - ARI_BITS)) {
- /* Expired */
- return 1;
- } else if (ANTIREPLAY_INTEGER(ctx, num) & ANTIREPLAY_INTEGERBIT(num)) {
- /* Replayed */
- return 1;
- } else {
- /* Unseen */
- return 0;
- }
-}
-
-/*
- * bloombucket.h
- */
-
-void
-bb_init(struct bloom_bucket *bb, size_t size, size_t keys, uint8_t interval,
- uint8_t thresh, int type, int flags)
-{
- size_t i;
-
- for (bb->size = 1; bb->size < size; bb->size <<= 1);
-
- bb->type = type;
- bb->nkeys = keys;
- bb->thresh = thresh;
- bb->interval = interval;
- bb->keys = mallocarray(bb->nkeys, sizeof(*bb->keys), bb->type, flags);
- bb->bucket = mallocarray(bb->size, 1, bb->type, flags | M_ZERO);
-
- for (i = 0; i < bb->nkeys; i++)
- arc4random_buf(&bb->keys[i], sizeof(*bb->keys));
-
- timeout_set(&bb->tick, bb_tick, bb);
-}
-
-void
-bb_destroy(struct bloom_bucket *bb)
-{
- free(bb->keys, bb->type, bb->nkeys);
- free(bb->bucket, bb->type, bb->size);
-}
-
-void
-bb_tick(void *_bb)
-{
- struct bloom_bucket *bb = _bb;
- int act;
- size_t i;
-
- for (i = 0, act = 0; i < bb->size; i++) {
- /* Check to protect underflow, as well as flag
- * if action has been taken, therefore should
- * schedule again. */
- if (bb->bucket[i] > 0) {
- bb->bucket[i]--;
- act = 1;
- }
- }
-
- if (act && !timeout_pending(&bb->tick))
- timeout_add_sec(&bb->tick, bb->interval);
-}
-
-int
-bb_recv(struct bloom_bucket *bb, uint8_t *buf, size_t n)
-{
- size_t i;
- uint64_t hash;
- uint8_t *elem, min = 0xff;
-
- for (i = 0; i < bb->nkeys; i++) {
- hash = SipHash24(&bb->keys[i], buf, n);
- elem = &bb->bucket[hash & (bb->size - 1)];
- if (*elem < min)
- min = *elem;
- if (*elem < bb->thresh)
- (*elem)++;
- }
-
- if (!timeout_pending(&bb->tick))
- timeout_add_sec(&bb->tick, bb->interval);
-
- return min == bb->thresh;
-}
-
-uint8_t
-bb_load(struct bloom_bucket *bb)
-{
- size_t i;
- uint8_t log;
- uint64_t sum;
- for (i = 0, sum = 0; i < bb->size; i++)
- sum += bb->bucket[i];
-
- sum *= (0xffffffffffffffff / (bb->size * bb->thresh));
-
- log = 0;
- while (sum >>= 1)
- log++;
-
- return log;
-}
-
-/*
- * fixedmap.h
- */
-
-//#define FMPRINTF(...) printf("fm: " __VA_ARGS__)
-#define FMPRINTF(...) 0
-
-void
-fm_init(struct fixed_map *fm, size_t size, int ipl)
-{
- bzero(fm, sizeof(*fm));
- mtx_init(&fm->mtx, ipl);
- fm_resize(fm, size);
-}
-
-void
-fm_resize(struct fixed_map *fm, size_t size)
-{
- size_t i;
- size_t xsize;
- struct map_item *xmap;
-
- for (xsize = 1; xsize < size; xsize <<= 1);
-
- mtx_enter(&fm->mtx);
- if (xsize < fm->size)
- goto leave;
- mtx_leave(&fm->mtx);
-
- xmap = mallocarray(xsize, sizeof(*xmap), M_DEVBUF, M_WAITOK | M_ZERO);
-
- mtx_enter(&fm->mtx);
- if (fm->map != NULL) {
- for (i = 0; i < fm->size; i++)
- xmap[fm->map[i].key % fm->size] = fm->map[i];
- free(fm->map, M_DEVBUF, 0);
- }
- fm->size = xsize;
- fm->map = xmap;
-leave:
- mtx_leave(&fm->mtx);
-}
-
-void
-fm_destroy(struct fixed_map *fm)
-{
- struct map_item *item;
- FM_FOREACH_FILLED(item, fm)
- panic("non empty fixedmap");
- free(fm->map, M_DEVBUF, 0);
-}
-
-uint32_t
-fm_insert(struct fixed_map *fm, void *v)
-{
- uint32_t k = 0;
- struct map_item *item = NULL, *iter;
-
- mtx_enter(&fm->mtx);
-
- FM_FOREACH_EMPTY(iter, fm) {
- item = iter;
- break;
- }
-
- if (item == NULL)
- panic("fixed map not large enough");
-
- while (k == 0)
- k = (arc4random() & ~(fm->size - 1)) + (item - fm->map);
-
- item->key = k;
- item->state = FM_ITEM_FILLED;
- item->value = v;
- refcnt_init(&item->refcnt);
- mtx_leave(&fm->mtx);
-
- FMPRINTF("insert %x in %p - %d\n", k, fm, item->refcnt.refs);
-
- /* We want to take another reference, as one reference is in the map
- * and the other one gets returned */
- refcnt_take(&item->refcnt);
- return k;
-}
-
-void *
-fm_lookup(struct fixed_map *fm, uint32_t k)
-{
- void *v = NULL;
- struct map_item *item;
-
- mtx_enter(&fm->mtx);
- item = fm->map + (k & (fm->size - 1));
- if (item->key == k) {
- refcnt_take(&item->refcnt);
- v = item->value;
- FMPRINTF("lookup %x in %p - %d\n", k, fm, item->refcnt.refs);
- } else {
- FMPRINTF("lookup %x in %p - failed\n", k, fm);
- }
- mtx_leave(&fm->mtx);
- return v;
-}
-
-void
-fm_put(struct fixed_map *fm, uint32_t k)
-{
- struct map_item *item;
-
- mtx_enter(&fm->mtx);
- item = fm->map + (k & (fm->size - 1));
- if (item->key != k)
- panic("element should be in map");
- refcnt_rele_wake(&item->refcnt);
- FMPRINTF("put %x in %p - %d\n", k, fm, item->refcnt.refs);
- mtx_leave(&fm->mtx);
-}
-
-void
-fm_drop(struct fixed_map *fm, uint32_t k)
-{
- struct map_item *item;
-
- mtx_enter(&fm->mtx);
- item = fm->map + (k & (fm->size - 1));
- if (item->key != k)
- panic("element should be in map");
- FMPRINTF("drop %x in %p - %d\n", k, fm, item->refcnt.refs);
- mtx_leave(&fm->mtx);
-
- refcnt_finalize(&item->refcnt, "fm_drop");
-
- mtx_enter(&fm->mtx);
- item->key = 0;
- item->state = FM_ITEM_EMPTY;
- item->value = NULL;
- mtx_leave(&fm->mtx);
-}
-
-/*
- * mpq.h
- */
-
-void
-mpq_init(struct mpq *mpq, int ipl)
-{
- mpq->mpq_cursor = NULL;
- mpq->mpq_serializer = NULL;
- mtx_init(&mpq->mpq_mtx, ipl);
- ml_init(&mpq->mpq_list);
-}
-
-int
-mpq_full(struct mpq *mpq)
-{
- int full;
- mtx_enter(&mpq->mpq_mtx);
- full = ml_len(&mpq->mpq_list) >= MPQ_LEN;
- mtx_leave(&mpq->mpq_mtx);
- return full;
-}
-
-int
-mpq_serialize_try_enter(struct mpq *mpq)
-{
- int error = 1;
- mtx_enter(&mpq->mpq_mtx);
- if (mpq->mpq_serializer == NULL) {
- mpq->mpq_serializer = curcpu();
- error = 0;
- }
- mtx_leave(&mpq->mpq_mtx);
- return error;
-}
-
-void
-mpq_serialize_leave(struct mpq *mpq)
-{
- mtx_enter(&mpq->mpq_mtx);
- mpq->mpq_serializer = NULL;
- mtx_leave(&mpq->mpq_mtx);
-}
-
-void
-mpq_threaddone(struct mpq *mpq, struct mbuf *m)
-{
- mtx_enter(&mpq->mpq_mtx);
- SET(m->m_flags, M_LINK0);
- mtx_leave(&mpq->mpq_mtx);
-}
-
-void
-mpq_enqueue(struct mpq *mpq, struct mbuf *m)
-{
- CLR(m->m_flags, M_LINK0);
- mtx_enter(&mpq->mpq_mtx);
- if (ml_len(&mpq->mpq_list) < MPQ_LEN) {
- ml_enqueue(&mpq->mpq_list, m);
- if (mpq->mpq_cursor == NULL)
- mpq->mpq_cursor = m;
- } else {
- m_freem(m);
- }
- mtx_leave(&mpq->mpq_mtx);
-}
-
-struct mbuf
-*mpq_dethread(struct mpq *mpq)
-{
- struct mbuf *m;
- mtx_enter(&mpq->mpq_mtx);
- m = mpq->mpq_cursor;
- if (mpq->mpq_cursor != NULL)
- mpq->mpq_cursor = MBUF_LIST_NEXT(mpq->mpq_cursor);
- mtx_leave(&mpq->mpq_mtx);
- return m;
-}
-
-struct mbuf
-*mpq_dequeue(struct mpq *mpq)
-{
- struct mbuf *m;
- mtx_enter(&mpq->mpq_mtx);
- m = MBUF_LIST_FIRST(&mpq->mpq_list);
- if (m != NULL && ISSET(m->m_flags, M_LINK0))
- m = ml_dequeue(&mpq->mpq_list);
- else
- m = NULL;
- mtx_leave(&mpq->mpq_mtx);
- return m;
-}