Annotation of embedaddon/bird2/filter/f-util.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Filters: utility functions
! 3: *
! 4: * Copyright 1998 Pavel Machek <pavel@ucw.cz>
! 5: * 2017 Jan Maria Matejka <mq@ucw.cz>
! 6: *
! 7: * Can be freely distributed and used under the terms of the GNU GPL.
! 8: */
! 9:
! 10: #include "nest/bird.h"
! 11: #include "conf/conf.h"
! 12: #include "filter/filter.h"
! 13: #include "filter/f-inst.h"
! 14: #include "lib/idm.h"
! 15: #include "nest/protocol.h"
! 16: #include "nest/route.h"
! 17:
! 18: #define P(a,b) ((a<<8) | b)
! 19:
! 20: const char *
! 21: filter_name(const struct filter *filter)
! 22: {
! 23: if (!filter)
! 24: return "ACCEPT";
! 25: else if (filter == FILTER_REJECT)
! 26: return "REJECT";
! 27: else if (!filter->sym)
! 28: return "(unnamed)";
! 29: else
! 30: return filter->sym->name;
! 31: }
! 32:
! 33: struct filter *f_new_where(struct f_inst *where)
! 34: {
! 35: struct f_inst *cond = f_new_inst(FI_CONDITION, where,
! 36: f_new_inst(FI_DIE, F_ACCEPT),
! 37: f_new_inst(FI_DIE, F_REJECT));
! 38:
! 39: struct filter *f = cfg_allocz(sizeof(struct filter));
! 40: f->root = f_linearize(cond);
! 41: return f;
! 42: }
! 43:
! 44: #define CA_KEY(n) n->name, n->fda.type
! 45: #define CA_NEXT(n) n->next
! 46: #define CA_EQ(na,ta,nb,tb) (!strcmp(na,nb) && (ta == tb))
! 47: #define CA_FN(n,t) (mem_hash(n, strlen(n)) ^ (t*0xaae99453U))
! 48: #define CA_ORDER 8 /* Fixed */
! 49:
! 50: struct ca_storage {
! 51: struct ca_storage *next;
! 52: struct f_dynamic_attr fda;
! 53: u32 uc;
! 54: char name[0];
! 55: };
! 56:
! 57: HASH(struct ca_storage) ca_hash;
! 58:
! 59: static struct idm ca_idm;
! 60: static struct ca_storage **ca_storage;
! 61: static uint ca_storage_max;
! 62:
! 63: static void
! 64: ca_free(resource *r)
! 65: {
! 66: struct custom_attribute *ca = (void *) r;
! 67: struct ca_storage *cas = HASH_FIND(ca_hash, CA, ca->name, ca->fda->type);
! 68: ASSERT(cas);
! 69:
! 70: ca->name = NULL;
! 71: ca->fda = NULL;
! 72: if (!--cas->uc) {
! 73: uint id = EA_CUSTOM_ID(cas->fda.ea_code);
! 74: idm_free(&ca_idm, id);
! 75: HASH_REMOVE(ca_hash, CA, cas);
! 76: ca_storage[id] = NULL;
! 77: mb_free(cas);
! 78: }
! 79: }
! 80:
! 81: static void
! 82: ca_dump(resource *r)
! 83: {
! 84: struct custom_attribute *ca = (void *) r;
! 85: debug("name \"%s\" id 0x%04x ea_type 0x%02x f_type 0x%02x\n",
! 86: ca->name, ca->fda->ea_code, ca->fda->type, ca->fda->f_type);
! 87: }
! 88:
! 89: static struct resclass ca_class = {
! 90: .name = "Custom attribute",
! 91: .size = sizeof(struct custom_attribute),
! 92: .free = ca_free,
! 93: .dump = ca_dump,
! 94: .lookup = NULL,
! 95: .memsize = NULL,
! 96: };
! 97:
! 98: struct custom_attribute *
! 99: ca_lookup(pool *p, const char *name, int f_type)
! 100: {
! 101: int ea_type;
! 102:
! 103: switch (f_type) {
! 104: case T_INT:
! 105: ea_type = EAF_TYPE_INT;
! 106: break;
! 107: case T_IP:
! 108: ea_type = EAF_TYPE_IP_ADDRESS;
! 109: break;
! 110: case T_QUAD:
! 111: ea_type = EAF_TYPE_ROUTER_ID;
! 112: break;
! 113: case T_PATH:
! 114: ea_type = EAF_TYPE_AS_PATH;
! 115: break;
! 116: case T_CLIST:
! 117: ea_type = EAF_TYPE_INT_SET;
! 118: break;
! 119: case T_ECLIST:
! 120: ea_type = EAF_TYPE_EC_SET;
! 121: break;
! 122: case T_LCLIST:
! 123: ea_type = EAF_TYPE_LC_SET;
! 124: break;
! 125: default:
! 126: cf_error("Custom route attribute of unsupported type");
! 127: }
! 128:
! 129: static int inited = 0;
! 130: if (!inited) {
! 131: idm_init(&ca_idm, &root_pool, 8);
! 132: HASH_INIT(ca_hash, &root_pool, CA_ORDER);
! 133:
! 134: ca_storage_max = 256;
! 135: ca_storage = mb_allocz(&root_pool, sizeof(struct ca_storage *) * ca_storage_max);
! 136:
! 137: inited++;
! 138: }
! 139:
! 140: struct ca_storage *cas = HASH_FIND(ca_hash, CA, name, ea_type);
! 141: if (cas) {
! 142: cas->uc++;
! 143: } else {
! 144:
! 145: uint id = idm_alloc(&ca_idm);
! 146:
! 147: if (id >= EA_CUSTOM_BIT)
! 148: cf_error("Too many custom attributes.");
! 149:
! 150: if (id >= ca_storage_max) {
! 151: ca_storage_max *= 2;
! 152: ca_storage = mb_realloc(ca_storage, sizeof(struct ca_storage *) * ca_storage_max * 2);
! 153: }
! 154:
! 155: cas = mb_allocz(&root_pool, sizeof(struct ca_storage) + strlen(name) + 1);
! 156: cas->fda = f_new_dynamic_attr(ea_type, f_type, EA_CUSTOM(id));
! 157: cas->uc = 1;
! 158:
! 159: strcpy(cas->name, name);
! 160: ca_storage[id] = cas;
! 161:
! 162: HASH_INSERT(ca_hash, CA, cas);
! 163: }
! 164:
! 165: struct custom_attribute *ca = ralloc(p, &ca_class);
! 166: ca->fda = &(cas->fda);
! 167: ca->name = cas->name;
! 168: return ca;
! 169: }
! 170:
! 171: const char *
! 172: ea_custom_name(uint ea)
! 173: {
! 174: uint id = EA_CUSTOM_ID(ea);
! 175: if (id >= ca_storage_max)
! 176: return NULL;
! 177:
! 178: if (!ca_storage[id])
! 179: return NULL;
! 180:
! 181: return ca_storage[id]->name;
! 182: }
! 183:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>