]> git.ipfire.org Git - thirdparty/bird.git/blobdiff - filter/f-util.c
Doc: Update prefix set comment
[thirdparty/bird.git] / filter / f-util.c
index 7856d7f6b7ab4e76b842b4ec5abaff83c97223f9..410999a640ce009895edf47e684bb9f26e9ad20d 100644 (file)
  *     Filters: utility functions
  *
  *     Copyright 1998 Pavel Machek <pavel@ucw.cz>
+ *               2017 Jan Maria Matejka <mq@ucw.cz>
  *
  *     Can be freely distributed and used under the terms of the GNU GPL.
  */
 
-#include <stdio.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/signal.h>
-#include <setjmp.h>
-
 #include "nest/bird.h"
-#include "lib/lists.h"
-#include "lib/resource.h"
-#include "lib/socket.h"
-#include "nest/route.h"
-#include "nest/protocol.h"
-#include "nest/iface.h"
 #include "conf/conf.h"
 #include "filter/filter.h"
+#include "filter/f-inst.h"
+#include "lib/idm.h"
+#include "nest/protocol.h"
+#include "nest/route.h"
+
+#define P(a,b) ((a<<8) | b)
+
+const char *
+filter_name(const struct filter *filter)
+{
+  if (!filter)
+    return "ACCEPT";
+  else if (filter == FILTER_REJECT)
+    return "REJECT";
+  else if (!filter->sym)
+    return "(unnamed)";
+  else
+    return filter->sym->name;
+}
+
+struct filter *f_new_where(struct f_inst *where)
+{
+  struct f_inst *cond = f_new_inst(FI_CONDITION, where,
+                                  f_new_inst(FI_DIE, F_ACCEPT),
+                                  f_new_inst(FI_DIE, F_REJECT));
+
+  struct filter *f = cfg_allocz(sizeof(struct filter));
+  f->root = f_linearize(cond);
+  return f;
+}
 
-struct f_inst *autoexec_func = NULL;
+#define CA_KEY(n)      n->name, n->fda.type
+#define CA_NEXT(n)     n->next
+#define CA_EQ(na,ta,nb,tb)     (!strcmp(na,nb) && (ta == tb))
+#define CA_FN(n,t)     (mem_hash(n, strlen(n)) ^ (t*0xaae99453U))
+#define CA_ORDER       8 /* Fixed */
 
-#define runtime(x) do { \
-    log( L_ERR, x ); \
-    res.type = T_RETURN; \
-    res.val.i = F_ERROR; \
-    return res; \
-  } while(0)
+struct ca_storage {
+  struct ca_storage *next;
+  struct f_dynamic_attr fda;
+  u32 uc;
+  char name[0];
+};
 
-#define ARG(x,y) \
-       x = interpret(what->y); \
-       if (x.type == T_RETURN) \
-               return x;
+HASH(struct ca_storage) ca_hash;
 
-#define ONEARG ARG(v1, arg1)
-#define TWOARGS ARG(v1, arg1) \
-               ARG(v2, arg2)
+static struct idm ca_idm;
+static struct ca_storage **ca_storage;
+static uint ca_storage_max;
 
-static struct f_val
-interpret(struct f_inst *what)
+static void
+ca_free(resource *r)
 {
-  struct symbol *sym;
-  struct f_val v1, v2, res;
-
-  res.type = T_VOID;
-  if (!what)
-    return res;
-
-  switch(what->code) {
-  case ',':
-    TWOARGS;
-    break;
-  case '+':
-    TWOARGS;
-    if (v1.type != v2.type)
-      runtime( "Can not operate with values of incompatible types" );
-
-    switch (res.type = v1.type) {
-    case T_VOID: runtime( "Can not operate with values of type void" );
-    case T_INT: res.val.i = v1.val.i + v2.val.i; break;
-    default: runtime( "Usage of unknown type" );
-    }
-    break;
-  case '=':
-    ARG(v2, arg2);
-    sym = what->arg1;
-    switch (res.type = v2.type) {
-    case T_VOID: runtime( "Can not assign void values" );
-    case T_INT: 
-      if (sym->class != SYM_VARIABLE_INT)
-       runtime( "Variable of bad type" );
-      sym->aux = v2.val.i; 
+  struct custom_attribute *ca = (void *) r;
+  struct ca_storage *cas = HASH_FIND(ca_hash, CA, ca->name, ca->fda->type);
+  ASSERT(cas);
+
+  ca->name = NULL;
+  ca->fda = NULL;
+  if (!--cas->uc) {
+    uint id = EA_CUSTOM_ID(cas->fda.ea_code);
+    idm_free(&ca_idm, id);
+    HASH_REMOVE(ca_hash, CA, cas);
+    ca_storage[id] = NULL;
+    mb_free(cas);
+  }
+}
+
+static void
+ca_dump(resource *r)
+{
+  struct custom_attribute *ca = (void *) r;
+  debug("name \"%s\" id 0x%04x ea_type 0x%02x f_type 0x%02x\n",
+      ca->name, ca->fda->ea_code, ca->fda->type, ca->fda->f_type);
+}
+
+static struct resclass ca_class = {
+  .name = "Custom attribute",
+  .size = sizeof(struct custom_attribute),
+  .free = ca_free,
+  .dump = ca_dump,
+  .lookup = NULL,
+  .memsize = NULL,
+};
+
+struct custom_attribute *
+ca_lookup(pool *p, const char *name, int f_type)
+{
+  int ea_type;
+
+  switch (f_type) {
+    case T_INT:
+      ea_type = EAF_TYPE_INT;
       break;
-    }
-    break;
-  case 'c':
-    res.type = T_INT;
-    res.val.i = (int) what->arg1;
-    break;
-  case 'i':
-    res.type = T_INT;
-    res.val.i = * ((int *) what->arg1);
-    break;
-  case 'p':
-    ONEARG;
-    printf( "Printing: " );
-    switch (v1.type) {
-    case T_VOID: printf( "(void)" ); break;
-    case T_INT: printf( "%d", v1.val.i ); break;
-    default: runtime( "Print of variable of unknown type" );
-    }
-    printf( "\n" );
-    break;
-  case '?':
-    ONEARG;
-    if (v1.type != T_INT)
-      runtime( "If requires integer expression" );
-    if (v1.val.i) {
-      ARG(res,arg2);
-    }
-    break;
-  case 'D':
-    printf( "DEBUGGING PRINT\n" );
-    break;
-  case '0':
-    printf( "No operation\n" );
-    break;
-  case 'd':
-    printf( "Puts: %s\n", (char *) what->arg1 );
-    break;
-  case '!':
-    switch ((int) what->arg1) {
-    case F_QUITBIRD:
-      die( "Filter asked me to die" );
-    case F_ACCEPT:
-      /* Should take care about turning ACCEPT into MODIFY */
-    case F_ERROR:
-    case F_REJECT:
-      res.type = T_RETURN;
-      res.val.i = (int) what->arg1;
+    case T_IP:
+      ea_type = EAF_TYPE_IP_ADDRESS;
+      break;
+    case T_QUAD:
+      ea_type = EAF_TYPE_ROUTER_ID;
+      break;
+    case T_PATH:
+      ea_type = EAF_TYPE_AS_PATH;
+      break;
+    case T_CLIST:
+      ea_type = EAF_TYPE_INT_SET;
+      break;
+    case T_ECLIST:
+      ea_type = EAF_TYPE_EC_SET;
+      break;
+    case T_LCLIST:
+      ea_type = EAF_TYPE_LC_SET;
       break;
     default:
-      bug( "unknown return type: can not happen");
+      cf_error("Custom route attribute of unsupported type");
+  }
+
+  static int inited = 0;
+  if (!inited) {
+    idm_init(&ca_idm, &root_pool, 8);
+    HASH_INIT(ca_hash, &root_pool, CA_ORDER);
+
+    ca_storage_max = 256;
+    ca_storage = mb_allocz(&root_pool, sizeof(struct ca_storage *) * ca_storage_max);
+
+    inited++;
+  }
+
+  struct ca_storage *cas = HASH_FIND(ca_hash, CA, name, ea_type);
+  if (cas) {
+    cas->uc++;
+  } else {
+
+    uint id = idm_alloc(&ca_idm);
+
+    if (id >= EA_CUSTOM_BIT)
+      cf_error("Too many custom attributes.");
+
+    if (id >= ca_storage_max) {
+      ca_storage_max *= 2;
+      ca_storage = mb_realloc(ca_storage, sizeof(struct ca_storage *) * ca_storage_max * 2);
     }
-    break;
-  default:
-    bug( "Unknown instruction %d (%c)", what->code, what->code & 0xff);
+
+    cas = mb_allocz(&root_pool, sizeof(struct ca_storage) + strlen(name) + 1);
+    cas->fda = f_new_dynamic_attr(ea_type, f_type, EA_CUSTOM(id));
+    cas->uc = 1;
+
+    strcpy(cas->name, name);
+    ca_storage[id] = cas;
+
+    HASH_INSERT(ca_hash, CA, cas);
   }
-  if (what->next)
-    return interpret(what->next);
-  return res;
-}
 
-struct f_inst *
-f_new_inst(void)
-{
-  struct f_inst * ret;
-  ret = cfg_alloc(sizeof(struct f_inst));
-  ret->code = 0;
-  ret->arg1 = ret->arg2 = ret->next = NULL;
-  return ret;
+  struct custom_attribute *ca = ralloc(p, &ca_class);
+  ca->fda = &(cas->fda);
+  ca->name = cas->name;
+  return ca;
 }
 
-int
-f_run(struct symbol *filter, struct rte *rtein, struct rte **rteout)
+const char *
+ea_custom_name(uint ea)
 {
-  struct f_inst *inst;
-  struct f_val res;
-  debug( "Running filter `%s'...", filter->name );
-
-  inst = filter->def;
-  res = interpret(inst);
-  if (res.type != T_RETURN)
-    return F_ERROR;
-  debug( "done (%d)\n", res.val.i );
-  return res.val.i;
+  uint id = EA_CUSTOM_ID(ea);
+  if (id >= ca_storage_max)
+    return NULL;
+
+  if (!ca_storage[id])
+    return NULL;
+
+  return ca_storage[id]->name;
 }
 
-void
-filters_postconfig(void)
-{
-  if (autoexec_func)
-    interpret(autoexec_func);
-}