]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib, zebra: move "struct vrf" to be a lib module
authorFeng Lu <lu.feng@6wind.com>
Fri, 22 May 2015 09:39:56 +0000 (11:39 +0200)
committerVipin Kumar <vipin@cumulusnetworks.com>
Thu, 29 Oct 2015 23:45:10 +0000 (16:45 -0700)
Previously "struct vrf" is defined locally in zebra. Now it is moved
to be a lib module.

This is the first step to support multi-VRF in quagga. The
implementation is splitted into small patches for the purpose of
easy review.

* lib:
    "struct vrf" with basic members is defined in vrf.c. The member
    "void *info" is for user data.

    Some basic functions are defined in vrf.c for adding/deleting/
    looking up a VRF, scanning the VRF table and initializing the
    VRF module.

    The type "vrf_id_t" is defined specificly for VRF ID.

* zebra:
    The previous "struct vrf" is re-defined as "struct zebra_vrf";
    and previous "vrf" variables are renamed to "zvrf".

    The previous "struct vrf" related functions are removed from
    zbera_rib.c. New functions are defined to maintain the new
    "struct zebra_vrf".

    The names vrf_xxx are reserved for the functions in VRF module.
    So:
    - the previous vrf_table() are renamed to zebra_vrf_table();
    - the previous vrf_static_table() are renamed to
      zebra_vrf_static_table().

    The main logic is not changed.

    BTW: Add a statement to zebra_snmp.c telling that the SNMP is
         running only for the MIBs in the default VRF.

Signed-off-by: Feng Lu <lu.feng@6wind.com>
Reviewed-by: Alain Ritoux <alain.ritoux@6wind.com>
Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
Acked-by: Vincent JARDIN <vincent.jardin@6wind.com>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Conflicts:
lib/Makefile.am
zebra/zebra_rib.c
zebra/zebra_vty.c

Conflicts:
lib/Makefile.am
lib/memtypes.c
zebra/rib.h
zebra/zebra_rib.c
zebra/zebra_rnh.c
zebra/zebra_rnh.h
zebra/zebra_vty.c

16 files changed:
lib/Makefile.am
lib/memtypes.c
lib/vrf.c [new file with mode: 0644]
lib/vrf.h [new file with mode: 0644]
lib/zebra.h
zebra/main.c
zebra/redistribute.c
zebra/rib.h
zebra/test_main.c
zebra/zebra_fpm.c
zebra/zebra_fpm_netlink.c
zebra/zebra_rib.c
zebra/zebra_rnh.c
zebra/zebra_rnh.h
zebra/zebra_snmp.c
zebra/zebra_vty.c

index 19ae8f96df0e29279687d7ebff13edd49f5998d3..49a721f7f9a059e0dad0d686b6f677fea998b823 100644 (file)
@@ -14,7 +14,7 @@ libzebra_la_SOURCES = \
        filter.c routemap.c distribute.c stream.c str.c log.c plist.c \
        zclient.c sockopt.c smux.c agentx.c snmp.c md5.c if_rmap.c keychain.c privs.c \
        sigevent.c pqueue.c jhash.c memtypes.c workqueue.c nexthop.c json.c \
-       ptm_lib.c csv.c bfd.c
+       ptm_lib.c csv.c bfd.c vrf.c
 
 BUILT_SOURCES = memtypes.h route_types.h gitversion.h
 
@@ -30,7 +30,7 @@ pkginclude_HEADERS = \
        plist.h zclient.h sockopt.h smux.h md5.h if_rmap.h keychain.h \
        privs.h sigevent.h pqueue.h jhash.h zassert.h memtypes.h \
        workqueue.h route_types.h libospf.h nexthop.h json.h \
-       ptm_lib.h csv.h bfd.h
+       ptm_lib.h csv.h bfd.h vrf.h
 
 EXTRA_DIST = \
        regex.c regex-gnu.h \
index 5b0c12040add896346e1f95a0470f3996f90e4ab..dafd471dd280260c4f3352965d031444641273cb 100644 (file)
@@ -73,14 +73,15 @@ struct memory_list memory_list_lib[] =
   { MTYPE_PQUEUE_DATA,         "Priority queue data"           },
   { MTYPE_HOST,                        "Host config"                   },
   { MTYPE_BFD_INFO,             "BFD info"                     },
+  { MTYPE_VRF,                 "VRF"                           },
+  { MTYPE_VRF_NAME,            "VRF name"                      },
   { -1, NULL },
 };
 
 struct memory_list memory_list_zebra[] = 
 {
   { MTYPE_RTADV_PREFIX,                "Router Advertisement Prefix"   },
-  { MTYPE_VRF,                 "VRF"                           },
-  { MTYPE_VRF_NAME,            "VRF name"                      },
+  { MTYPE_ZEBRA_VRF,           "ZEBRA VRF"                             },
   { MTYPE_NEXTHOP,             "Nexthop"                       },
   { MTYPE_RIB,                 "RIB"                           },
   { MTYPE_RIB_QUEUE,           "RIB process work queue"        },
diff --git a/lib/vrf.c b/lib/vrf.c
new file mode 100644 (file)
index 0000000..3ccbb99
--- /dev/null
+++ b/lib/vrf.c
@@ -0,0 +1,268 @@
+/*
+ * VRF functions.
+ * Copyright (C) 2014 6WIND S.A.
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * GNU Zebra is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Zebra; see the file COPYING.  If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <zebra.h>
+
+#include "vrf.h"
+#include "prefix.h"
+#include "table.h"
+#include "log.h"
+#include "memory.h"
+
+struct vrf
+{
+  /* Identifier, same as the vector index */
+  vrf_id_t vrf_id;
+  /* Name */
+  char *name;
+
+  /* User data */
+  void *info;
+};
+
+/* Holding VRF hooks  */
+struct vrf_master
+{
+  int (*vrf_new_hook) (vrf_id_t, void **);
+  int (*vrf_delete_hook) (vrf_id_t, void **);
+} vrf_master = {0,};
+
+/* VRF table */
+struct route_table *vrf_table = NULL;
+
+/* Build the table key */
+static void
+vrf_build_key (vrf_id_t vrf_id, struct prefix *p)
+{
+  p->family = AF_INET;
+  p->prefixlen = IPV4_MAX_BITLEN;
+  p->u.prefix4.s_addr = vrf_id;
+}
+
+/* Get a VRF. If not found, create one. */
+static struct vrf *
+vrf_get (vrf_id_t vrf_id)
+{
+  struct prefix p;
+  struct route_node *rn;
+  struct vrf *vrf;
+
+  vrf_build_key (vrf_id, &p);
+  rn = route_node_get (vrf_table, &p);
+  if (rn->info)
+    {
+      vrf = (struct vrf *)rn->info;
+      route_unlock_node (rn); /* get */
+      return vrf;
+    }
+
+  vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf));
+  vrf->vrf_id = vrf_id;
+  rn->info = vrf;
+
+  zlog_info ("VRF %u is created.", vrf_id);
+
+  if (vrf_master.vrf_new_hook)
+    (*vrf_master.vrf_new_hook) (vrf_id, &vrf->info);
+
+  return vrf;
+}
+
+/* Delete a VRF. This is called in vrf_terminate(). */
+static void
+vrf_delete (struct vrf *vrf)
+{
+  zlog_info ("VRF %u is to be deleted.", vrf->vrf_id);
+
+  if (vrf_master.vrf_delete_hook)
+    (*vrf_master.vrf_delete_hook) (vrf->vrf_id, &vrf->info);
+
+  if (vrf->name)
+    XFREE (MTYPE_VRF_NAME, vrf->name);
+
+  XFREE (MTYPE_VRF, vrf);
+}
+
+/* Look up a VRF by identifier. */
+static struct vrf *
+vrf_lookup (vrf_id_t vrf_id)
+{
+  struct prefix p;
+  struct route_node *rn;
+  struct vrf *vrf = NULL;
+
+  vrf_build_key (vrf_id, &p);
+  rn = route_node_lookup (vrf_table, &p);
+  if (rn)
+    {
+      vrf = (struct vrf *)rn->info;
+      route_unlock_node (rn); /* lookup */
+    }
+  return vrf;
+}
+
+/* Add a VRF hook. Please add hooks before calling vrf_init(). */
+void
+vrf_add_hook (int type, int (*func)(vrf_id_t, void **))
+{
+  switch (type) {
+  case VRF_NEW_HOOK:
+    vrf_master.vrf_new_hook = func;
+    break;
+  case VRF_DELETE_HOOK:
+    vrf_master.vrf_delete_hook = func;
+    break;
+  default:
+    break;
+  }
+}
+
+/* Return the iterator of the first VRF. */
+vrf_iter_t
+vrf_first (void)
+{
+  struct route_node *rn;
+
+  for (rn = route_top (vrf_table); rn; rn = route_next (rn))
+    if (rn->info)
+      {
+        route_unlock_node (rn); /* top/next */
+        return (vrf_iter_t)rn;
+      }
+  return VRF_ITER_INVALID;
+}
+
+/* Return the next VRF iterator to the given iterator. */
+vrf_iter_t
+vrf_next (vrf_iter_t iter)
+{
+  struct route_node *rn = NULL;
+
+  /* Lock it first because route_next() will unlock it. */
+  if (iter != VRF_ITER_INVALID)
+    rn = route_next (route_lock_node ((struct route_node *)iter));
+
+  for (; rn; rn = route_next (rn))
+    if (rn->info)
+      {
+        route_unlock_node (rn); /* next */
+        return (vrf_iter_t)rn;
+      }
+  return VRF_ITER_INVALID;
+}
+
+/* Return the VRF iterator of the given VRF ID. If it does not exist,
+ * the iterator of the next existing VRF is returned. */
+vrf_iter_t
+vrf_iterator (vrf_id_t vrf_id)
+{
+  struct prefix p;
+  struct route_node *rn;
+
+  vrf_build_key (vrf_id, &p);
+  rn = route_node_get (vrf_table, &p);
+  if (rn->info)
+    {
+      /* OK, the VRF exists. */
+      route_unlock_node (rn); /* get */
+      return (vrf_iter_t)rn;
+    }
+
+  /* Find the next VRF. */
+  for (rn = route_next (rn); rn; rn = route_next (rn))
+    if (rn->info)
+      {
+        route_unlock_node (rn); /* next */
+        return (vrf_iter_t)rn;
+      }
+
+  return VRF_ITER_INVALID;
+}
+
+/* Obtain the VRF ID from the given VRF iterator. */
+vrf_id_t
+vrf_iter2id (vrf_iter_t iter)
+{
+  struct route_node *rn = (struct route_node *) iter;
+  return (rn && rn->info) ? ((struct vrf *)rn->info)->vrf_id : VRF_DEFAULT;
+}
+
+/* Obtain the data pointer from the given VRF iterator. */
+void *
+vrf_iter2info (vrf_iter_t iter)
+{
+  struct route_node *rn = (struct route_node *) iter;
+  return (rn && rn->info) ? ((struct vrf *)rn->info)->info : NULL;
+}
+
+/* Get the data pointer of the specified VRF. If not found, create one. */
+void *
+vrf_info_get (vrf_id_t vrf_id)
+{
+  struct vrf *vrf = vrf_get (vrf_id);
+  return vrf->info;
+}
+
+/* Look up the data pointer of the specified VRF. */
+void *
+vrf_info_lookup (vrf_id_t vrf_id)
+{
+  struct vrf *vrf = vrf_lookup (vrf_id);
+  return vrf ? vrf->info : NULL;
+}
+
+/* Initialize VRF module. */
+void
+vrf_init (void)
+{
+  struct vrf *default_vrf;
+
+  /* Allocate VRF table.  */
+  vrf_table = route_table_init ();
+
+  /* The default VRF always exists. */
+  default_vrf = vrf_get (VRF_DEFAULT);
+  if (!default_vrf)
+    {
+      zlog_err ("vrf_init: failed to create the default VRF!");
+      exit (1);
+    }
+
+  /* Set the default VRF name. */
+  default_vrf->name = XSTRDUP (MTYPE_VRF_NAME, "Default-IP-Routing-Table");
+}
+
+/* Terminate VRF module. */
+void
+vrf_terminate (void)
+{
+  struct route_node *rn;
+  struct vrf *vrf;
+
+  for (rn = route_top (vrf_table); rn; rn = route_next (rn))
+    if ((vrf = rn->info) != NULL)
+      vrf_delete (vrf);
+
+  route_table_finish (vrf_table);
+  vrf_table = NULL;
+}
+
diff --git a/lib/vrf.h b/lib/vrf.h
new file mode 100644 (file)
index 0000000..7e05099
--- /dev/null
+++ b/lib/vrf.h
@@ -0,0 +1,96 @@
+/*
+ * VRF related header.
+ * Copyright (C) 2014 6WIND S.A.
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * GNU Zebra is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Zebra; see the file COPYING.  If not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _ZEBRA_VRF_H
+#define _ZEBRA_VRF_H
+
+/* The default VRF ID */
+#define VRF_DEFAULT 0
+
+/*
+ * VRF hooks
+ */
+
+#define VRF_NEW_HOOK        0   /* a new VRF is just created */
+#define VRF_DELETE_HOOK     1   /* a VRF is to be deleted */
+
+/*
+ * Add a specific hook to VRF module.
+ * @param1: hook type
+ * @param2: the callback function
+ *          - param 1: the VRF ID
+ *          - param 2: the address of the user data pointer (the user data
+ *                     can be stored in or freed from there)
+ */
+extern void vrf_add_hook (int, int (*)(vrf_id_t, void **));
+
+/*
+ * VRF iteration
+ */
+
+typedef void *              vrf_iter_t;
+#define VRF_ITER_INVALID    NULL    /* invalid value of the iterator */
+
+/*
+ * VRF iteration utilities. Example for the usage:
+ *
+ *   vrf_iter_t iter = vrf_first();
+ *   for (; iter != VRF_ITER_INVALID; iter = vrf_next (iter))
+ *
+ * or
+ *
+ *   vrf_iter_t iter = vrf_iterator (<a given VRF ID>);
+ *   for (; iter != VRF_ITER_INVALID; iter = vrf_next (iter))
+ */
+
+/* Return the iterator of the first VRF. */
+extern vrf_iter_t vrf_first (void);
+/* Return the next VRF iterator to the given iterator. */
+extern vrf_iter_t vrf_next (vrf_iter_t);
+/* Return the VRF iterator of the given VRF ID. If it does not exist,
+ * the iterator of the next existing VRF is returned. */
+extern vrf_iter_t vrf_iterator (vrf_id_t);
+
+/*
+ * VRF iterator to properties
+ */
+extern vrf_id_t vrf_iter2id (vrf_iter_t);
+extern void *vrf_iter2info (vrf_iter_t);
+
+/*
+ * Utilities to obtain the user data
+ */
+
+/* Get the data pointer of the specified VRF. If not found, create one. */
+extern void *vrf_info_get (vrf_id_t);
+/* Look up the data pointer of the specified VRF. */
+extern void *vrf_info_lookup (vrf_id_t);
+
+/*
+ * VRF initializer/destructor
+ */
+/* Please add hooks before calling vrf_init(). */
+extern void vrf_init (void);
+extern void vrf_terminate (void);
+
+#endif /*_ZEBRA_VRF_H*/
+
index 0a1dd7f46c7351770b78d8dd07df835103b35150..a3ac78cbce237bea560b53864b1655d6a3152b2b 100644 (file)
@@ -556,6 +556,9 @@ typedef u_int8_t safi_t;
 typedef u_int16_t zebra_size_t;
 typedef u_int16_t zebra_command_t;
 
+/* VRF ID type. */
+typedef u_int16_t vrf_id_t;
+
 /* FIFO -- first in first out structure and macros.  */
 struct fifo
 {
index 45fe38052b9076cdcad84027ccf6faea07cd3432..92a0c0b31be13b380c963eacbea81f232d138bc5 100644 (file)
@@ -32,6 +32,7 @@
 #include "plist.h"
 #include "privs.h"
 #include "sigevent.h"
+#include "vrf.h"
 
 #include "zebra/rib.h"
 #include "zebra/zserv.h"
@@ -213,6 +214,29 @@ struct quagga_signal_t zebra_signals[] =
   },
 };
 
+/* Callback upon creating a new VRF. */
+static int
+zebra_vrf_new (vrf_id_t vrf_id, void **info)
+{
+  struct zebra_vrf *zvrf = *info;
+
+  if (! zvrf)
+    {
+      zvrf = zebra_vrf_alloc (vrf_id);
+      *info = (void *)zvrf;
+    }
+
+  return 0;
+}
+
+/* Zebra VRF initialization. */
+static void
+zebra_vrf_init (void)
+{
+  vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new);
+  vrf_init ();
+}
+
 /* Main startup routine. */
 int
 main (int argc, char **argv)
@@ -354,7 +378,8 @@ main (int argc, char **argv)
   /* For debug purpose. */
   /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
 
-  /* Make kernel routing socket. */
+  /* Initialize VRF module, and make kernel routing socket. */
+  zebra_vrf_init ();
   kernel_init ();
   interface_list ();
   route_read ();
index 5a2a0668b7ca52c4a39a3767a71ff468fdb2a54b..4b4db1dcee0c4a6365b4d150e3665d27698468d7 100644 (file)
@@ -30,6 +30,7 @@
 #include "zclient.h"
 #include "linklist.h"
 #include "log.h"
+#include "vrf.h"
 
 #include "zebra/rib.h"
 #include "zebra/zserv.h"
@@ -96,7 +97,7 @@ zebra_redistribute_default (struct zserv *client)
   p.family = AF_INET;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (table)
     {
       rn = route_node_lookup (table, (struct prefix *)&p);
@@ -116,7 +117,7 @@ zebra_redistribute_default (struct zserv *client)
   p6.family = AF_INET6;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (table)
     {
       rn = route_node_lookup (table, (struct prefix *)&p6);
@@ -140,7 +141,7 @@ zebra_redistribute (struct zserv *client, int type, u_short instance)
   struct route_table *table;
   struct route_node *rn;
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (table)
     for (rn = route_top (table); rn; rn = route_next (rn))
       RNODE_FOREACH_RIB (rn, newrib)
@@ -155,7 +156,7 @@ zebra_redistribute (struct zserv *client, int type, u_short instance)
          }
   
 #ifdef HAVE_IPV6
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (table)
     for (rn = route_top (table); rn; rn = route_next (rn))
       RNODE_FOREACH_RIB (rn, newrib)
@@ -561,7 +562,7 @@ zebra_import_table (afi_t afi, u_int32_t table_id, u_int32_t distance, int add)
   if (afi >= AFI_MAX)
     return (-1);
 
-  table = vrf_other_route_table(afi, table_id, 0);
+  table = zebra_vrf_other_route_table(afi, table_id, VRF_DEFAULT);
   if (table == NULL)
     {
       return 0;
index fa37f7d8c3497a3a1e0e4445881eb178d5203bf7..8c82a7eec356d2a3f1a3fb334c38a240d97601c3 100644 (file)
@@ -278,10 +278,10 @@ struct static_ipv6
                        : (((recursing) = 0),((tnexthop) = (tnexthop)->next)))
 
 /* Routing table instance.  */
-struct vrf
+struct zebra_vrf
 {
-  /* Identifier.  This is same as routing table vector index.  */
-  u_int32_t id;
+  /* Identifier. */
+  vrf_id_t vrf_id;
 
   /* Routing table name.  */
   char *name;
@@ -318,9 +318,9 @@ typedef struct rib_table_info_t_
 {
 
   /*
-   * Back pointer to vrf.
+   * Back pointer to zebra_vrf.
    */
-  struct vrf *vrf;
+  struct zebra_vrf *zvrf;
   afi_t afi;
   safi_t safi;
 
@@ -339,7 +339,7 @@ typedef enum
  */
 typedef struct rib_tables_iter_t_
 {
-  uint32_t vrf_id;
+  vrf_id_t vrf_id;
   int afi_safi_ix;
 
   rib_tables_iter_state_t state;
@@ -386,11 +386,12 @@ rib_bogus_ipv6 (int type, struct prefix_ipv6 *p,
                 struct in6_addr *gate, unsigned int ifindex, int table);
 #endif /* HAVE_IPV6 */
 
-extern struct vrf *vrf_lookup (u_int32_t);
-extern struct route_table *vrf_table (afi_t afi, safi_t safi, u_int32_t id);
-extern struct route_table *vrf_static_table (afi_t afi, safi_t safi, u_int32_t id);
-extern struct route_table *vrf_other_route_table (afi_t afi, u_int32_t table_id,
-                                                 u_int32_t vrf_id);
+extern struct zebra_vrf *zebra_vrf_lookup (vrf_id_t vrf_id);
+extern struct zebra_vrf *zebra_vrf_alloc (vrf_id_t);
+extern struct route_table *zebra_vrf_table (afi_t, safi_t, vrf_id_t);
+extern struct route_table *zebra_vrf_static_table (afi_t, safi_t, vrf_id_t);
+extern struct route_table *zebra_vrf_other_route_table (afi_t afi, u_int32_t table_id,
+                                                       vrf_id_t vrf_id);
 extern int is_zebra_valid_kernel_table(u_int32_t table_id);
 extern int is_zebra_main_routing_table(u_int32_t table_id);
 extern int zebra_check_addr (struct prefix *p);
@@ -539,10 +540,10 @@ rib_dest_table (rib_dest_t *dest)
 /*
  * rib_dest_vrf
  */
-static inline struct vrf *
+static inline struct zebra_vrf *
 rib_dest_vrf (rib_dest_t *dest)
 {
-  return rib_table_info (rib_dest_table (dest))->vrf;
+  return rib_table_info (rib_dest_table (dest))->zvrf;
 }
 
 /*
index d0a210bcbebfd9b999b37080d7765152f0e8d758..f4a8ca2bad2bd540b15366e2fb158c96d14f70c7 100644 (file)
@@ -29,6 +29,7 @@
 #include "log.h"
 #include "privs.h"
 #include "sigevent.h"
+#include "vrf.h"
 
 #include "zebra/rib.h"
 #include "zebra/zserv.h"
@@ -201,6 +202,29 @@ struct quagga_signal_t zebra_signals[] =
   },
 };
 
+/* Callback upon creating a new VRF. */
+static int
+zebra_vrf_new (vrf_id_t vrf_id, void **info)
+{
+  struct zebra_vrf *zvrf = *info;
+
+  if (! zvrf)
+    {
+      zvrf = zebra_vrf_alloc (vrf_id);
+      *info = (void *)zvrf;
+    }
+
+  return 0;
+}
+
+/* Zebra VRF initialization. */
+static void
+zebra_vrf_init (void)
+{
+  vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new);
+  vrf_init ();
+}
+
 /* Main startup routine. */
 int
 main (int argc, char **argv)
@@ -302,6 +326,7 @@ main (int argc, char **argv)
   access_list_init ();
 
   /* Make kernel routing socket. */
+  zebra_vrf_init ();
   kernel_init ();
   route_read ();
   zebra_vty_init();
index e02d174559c898f6f3a6d475e9016dab40b5a9cc..54a13654c6db64881fdee8d45ce0da4d7cb94000 100644 (file)
@@ -330,7 +330,7 @@ zfpm_is_table_for_fpm (struct route_table *table)
    * We only send the unicast tables in the main instance to the FPM
    * at this point.
    */
-  if (info->vrf->id != 0)
+  if (info->zvrf->vrf_id != 0)
     return 0;
 
   if (info->safi != SAFI_UNICAST)
index 1de75be4b03bdd16e21c98a61f322cd3db238ee8..1eec867041536f388b87f8f183c651670efd009c 100644 (file)
@@ -246,7 +246,7 @@ netlink_route_info_fill (netlink_route_info_t *ri, int cmd,
   ri->af = rib_dest_af (dest);
 
   ri->nlmsg_type = cmd;
-  ri->rtm_table = rib_dest_vrf (dest)->id;
+  ri->rtm_table = rib_dest_vrf (dest)->vrf_id;
   ri->rtm_protocol = RTPROT_UNSPEC;
 
   /*
index a69dead1a6cef7fe7938ab9602faf2fa87e13d83..b15cbd3cdb5f15adeb526eb9fd8bd4e1c0cbb080 100644 (file)
@@ -35,6 +35,7 @@
 #include "prefix.h"
 #include "routemap.h"
 #include "nexthop.h"
+#include "vrf.h"
 
 #include "zebra/rib.h"
 #include "zebra/rt.h"
@@ -79,136 +80,31 @@ static const struct
 };
 
 /* Vector for routing table.  */
-static vector vrf_vector;
+static vector zebra_vrf_vector;
 
 /*
- * vrf_table_create
+ * nexthop_type_to_str
  */
-static void
-vrf_table_create (struct vrf *vrf, afi_t afi, safi_t safi)
-{
-  rib_table_info_t *info;
-  struct route_table *table;
-
-  assert (!vrf->table[afi][safi]);
-
-  table = route_table_init ();
-  vrf->table[afi][safi] = table;
-
-  info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info));
-  info->vrf = vrf;
-  info->afi = afi;
-  info->safi = safi;
-  table->info = info;
-}
-
-/* Allocate new VRF.  */
-static struct vrf *
-vrf_alloc (const char *name)
-{
-  struct vrf *vrf;
-
-  vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf));
-
-  /* Put name.  */
-  if (name)
-    vrf->name = XSTRDUP (MTYPE_VRF_NAME, name);
-
-  /* Allocate routing table and static table.  */
-  vrf_table_create (vrf, AFI_IP, SAFI_UNICAST);
-  vrf_table_create (vrf, AFI_IP6, SAFI_UNICAST);
-  vrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init ();
-  vrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init ();
-  vrf_table_create (vrf, AFI_IP, SAFI_MULTICAST);
-  vrf_table_create (vrf, AFI_IP6, SAFI_MULTICAST);
-  vrf->stable[AFI_IP][SAFI_MULTICAST] = route_table_init ();
-  vrf->stable[AFI_IP6][SAFI_MULTICAST] = route_table_init ();
-
-  vrf->rnh_table[AFI_IP] = route_table_init();
-  vrf->rnh_table[AFI_IP6] = route_table_init();
-
-  vrf->import_check_table[AFI_IP] = route_table_init();
-  vrf->import_check_table[AFI_IP6] = route_table_init();
-
-  return vrf;
-}
-
-/* Lookup VRF by identifier.  */
-struct vrf *
-vrf_lookup (u_int32_t id)
-{
-  return vector_lookup (vrf_vector, id);
-}
-
-/* Initialize VRF.  */
-static void
-vrf_init (void)
+const char *
+nexthop_type_to_str (enum nexthop_types_t nh_type)
 {
-  struct vrf *default_table;
-
-  /* Allocate VRF vector.  */
-  vrf_vector = vector_init (1);
-
-  /* Allocate default main table.  */
-  default_table = vrf_alloc ("Default-IP-Routing-Table");
-
-  /* Default table index must be 0.  */
-  vector_set_index (vrf_vector, 0, default_table);
-}
-
-/* Lookup route table.  */
-struct route_table *
-vrf_table (afi_t afi, safi_t safi, u_int32_t id)
-{
-  struct vrf *vrf;
-
-  vrf = vrf_lookup (id);
-  if (! vrf)
-    return NULL;
-
-  if( afi >= AFI_MAX  || safi >= SAFI_MAX )
-    return NULL;
-
-  return vrf->table[afi][safi];
-}
-
-/* Lookup static route table.  */
-struct route_table *
-vrf_static_table (afi_t afi, safi_t safi, u_int32_t id)
-{
-  struct vrf *vrf;
-
-  vrf = vrf_lookup (id);
-  if (! vrf)
-    return NULL;
-
-  if( afi >= AFI_MAX  || safi >= SAFI_MAX )
-    return NULL;
-
-  return vrf->stable[afi][safi];
-}
-
-struct route_table *
-vrf_other_route_table (afi_t afi, u_int32_t table_id, u_int32_t vrf_id)
-{
-  struct vrf *vrf;
-
-  vrf = vrf_lookup (vrf_id);
-  if (! vrf)
-    return NULL;
-
-  if(afi >= AFI_MAX)
-    return NULL;
-
-  if (table_id >= ZEBRA_KERNEL_TABLE_MAX)
-    return NULL;
+  static const char *desc[] = {
+    "none",
+    "Directly connected",
+    "Interface route",
+    "IPv4 nexthop",
+    "IPv4 nexthop with ifindex",
+    "IPv4 nexthop with ifname",
+    "IPv6 nexthop",
+    "IPv6 nexthop with ifindex",
+    "IPv6 nexthop with ifname",
+    "Null0 nexthop",
+  };
 
-  if (vrf->other_table[afi][table_id] == NULL)
-    {
-      vrf->other_table[afi][table_id] = route_table_init();
-    }
+  if (nh_type >= ZEBRA_NUM_OF (desc))
+    return "<Invalid nh type>";
 
-  return (vrf->other_table[afi][table_id]);
+  return desc[nh_type];
 }
 
 int
@@ -587,7 +483,7 @@ nexthop_active_ipv4 (struct rib *rib, struct nexthop *nexthop, int set,
   p.prefix = nexthop->gate.ipv4;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return 0;
 
@@ -796,7 +692,7 @@ nexthop_active_ipv6 (struct rib *rib, struct nexthop *nexthop, int set,
   p.prefix = nexthop->gate.ipv6;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return 0;
 
@@ -960,7 +856,7 @@ rib_match_ipv4 (struct in_addr addr)
   int recursing;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return 0;
 
@@ -1021,7 +917,7 @@ rib_lookup_ipv4 (struct prefix_ipv4 *p)
   int recursing;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return 0;
 
@@ -1078,7 +974,7 @@ rib_lookup_ipv4_route (struct prefix_ipv4 *p, union sockunion * qgate)
   int nexthops_active;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return ZEBRA_RIB_LOOKUP_ERROR;
 
@@ -1144,7 +1040,7 @@ rib_match_ipv6 (struct in6_addr *addr)
   int recursing;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return 0;
 
@@ -2266,11 +2162,11 @@ rib_add_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
   /* Lookup table.  */
   if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
     {
-      table = vrf_table (AFI_IP, safi, 0);
+      table = zebra_vrf_table (AFI_IP, safi, VRF_DEFAULT);
     }
   else
     {
-      table = vrf_other_route_table (AFI_IP, table_id, 0);
+      table = zebra_vrf_other_route_table (AFI_IP, table_id, VRF_DEFAULT);
     }
   if (! table)
     return 0;
@@ -2442,10 +2338,10 @@ void rib_lookup_and_dump (struct prefix_ipv4 * p)
   char prefix_buf[INET_ADDRSTRLEN];
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
   {
-    zlog_err ("%s: vrf_table() returned NULL", __func__);
+    zlog_err ("%s: zebra_vrf_table() returned NULL", __func__);
     return;
   }
 
@@ -2491,9 +2387,9 @@ void rib_lookup_and_pushup (struct prefix_ipv4 * p)
   struct rib *rib;
   unsigned changed = 0;
 
-  if (NULL == (table = vrf_table (AFI_IP, SAFI_UNICAST, 0)))
+  if (NULL == (table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT)))
   {
-    zlog_err ("%s: vrf_table() returned NULL", __func__);
+    zlog_err ("%s: zebra_vrf_table() returned NULL", __func__);
     return;
   }
 
@@ -2543,11 +2439,11 @@ rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib, safi_t safi)
   /* Lookup table.  */
   if ((rib->table == zebrad.rtm_table_default) || (rib->table == RT_TABLE_MAIN))
     {
-      table = vrf_table (AFI_IP, safi, 0);
+      table = zebra_vrf_table (AFI_IP, safi, VRF_DEFAULT);
     }
   else
     {
-      table = vrf_other_route_table (AFI_IP, rib->table, 0);
+      table = zebra_vrf_other_route_table (AFI_IP, rib->table, VRF_DEFAULT);
     }
   if (! table)
     return 0;
@@ -2633,11 +2529,11 @@ rib_delete_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
   /* Lookup table.  */
   if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
     {
-      table = vrf_table (AFI_IP, safi, 0);
+      table = zebra_vrf_table (AFI_IP, safi, VRF_DEFAULT);
     }
   else
     {
-      table = vrf_other_route_table(AFI_IP, table_id, 0);
+      table = zebra_vrf_other_route_table(AFI_IP, table_id, VRF_DEFAULT);
     }
   if (! table)
     return 0;
@@ -2795,7 +2691,7 @@ static_install_ipv4 (struct prefix *p, struct static_ipv4 *si)
   struct prefix nh_p;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return;
 
@@ -2903,7 +2799,7 @@ static_uninstall_ipv4 (struct prefix *p, struct static_ipv4 *si)
   struct prefix nh_p;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return;
   
@@ -2991,7 +2887,7 @@ static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
   struct route_table *stable;
 
   /* Lookup table.  */
-  stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id);
+  stable = zebra_vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id);
   if (! stable)
     return -1;
   
@@ -3084,7 +2980,7 @@ static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
   struct route_table *stable;
 
   /* Lookup table.  */
-  stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id);
+  stable = zebra_vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id);
   if (! stable)
     return -1;
 
@@ -3175,11 +3071,11 @@ rib_add_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
   /* Lookup table.  */
   if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
     {
-      table = vrf_table (AFI_IP6, safi, 0);
+      table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
     }
   else
     {
-      table = vrf_other_route_table(AFI_IP6, table_id, 0);
+      table = zebra_vrf_other_route_table(AFI_IP6, table_id, VRF_DEFAULT);
     }
   if (! table)
     return 0;
@@ -3295,7 +3191,7 @@ rib_add_ipv6_multipath (struct prefix *p, struct rib *rib, safi_t safi,
       if (!rib)
         return 0;
 
-      table = vrf_table (AFI_IP, safi, 0);
+      table = zebra_vrf_table (AFI_IP, safi, VRF_DEFAULT);
       if (!table)
         return 0;
       /* Make it sure prefixlen is applied to the prefix. */
@@ -3311,11 +3207,11 @@ rib_add_ipv6_multipath (struct prefix *p, struct rib *rib, safi_t safi,
       /* Lookup table.  */
       if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
         {
-          table = vrf_table (AFI_IP6, safi, 0);
+          table = zebra_vrf_table (AFI_IP6, safi, VRF_DEFAULT);
         }
       else
         {
-          table = vrf_other_route_table(AFI_IP6, table_id, 0);
+          table = zebra_vrf_other_route_table(AFI_IP6, table_id, VRF_DEFAULT);
         }
 
       if (! table)
@@ -3416,11 +3312,11 @@ rib_delete_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
   /* Lookup table.  */
   if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
     {
-      table = vrf_table (AFI_IP6, safi, 0);
+      table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
     }
   else
     {
-      table = vrf_other_route_table(AFI_IP6, table_id, 0);
+      table = zebra_vrf_other_route_table(AFI_IP6, table_id, VRF_DEFAULT);
     }
   if (! table)
     return 0;
@@ -3561,7 +3457,7 @@ static_install_ipv6 (struct prefix *p, struct static_ipv6 *si)
   struct prefix nh_p;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return;
 
@@ -3671,7 +3567,7 @@ static_uninstall_ipv6 (struct prefix *p, struct static_ipv6 *si)
   struct prefix nh_p;
 
   /* Lookup table.  */
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return;
 
@@ -3757,7 +3653,7 @@ static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
   struct route_table *stable;
 
   /* Lookup table.  */
-  stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
+  stable = zebra_vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
   if (! stable)
     return -1;
     
@@ -3845,7 +3741,7 @@ static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
   struct route_table *stable;
 
   /* Lookup table.  */
-  stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
+  stable = zebra_vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
   if (! stable)
     return -1;
 
@@ -3898,7 +3794,7 @@ rib_update_static (void)
   struct route_table *table;
   struct rib *rib, *next;
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (table)
     for (rn = route_top (table); rn; rn = route_next (rn))
       RNODE_FOREACH_RIB_SAFE (rn, rib, next)
@@ -3908,7 +3804,7 @@ rib_update_static (void)
             break;
           }
 
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (table)
     for (rn = route_top (table); rn; rn = route_next (rn))
       RNODE_FOREACH_RIB_SAFE (rn, rib, next)
@@ -3926,13 +3822,13 @@ rib_update (void)
   struct route_node *rn;
   struct route_table *table;
   
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (table)
     for (rn = route_top (table); rn; rn = route_next (rn))
       if (rnode_to_ribs (rn))
         rib_queue_add (&zebrad, rn);
 
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (table)
     for (rn = route_top (table); rn; rn = route_next (rn))
       if (rnode_to_ribs (rn))
@@ -3965,8 +3861,8 @@ rib_weed_table (struct route_table *table)
 void
 rib_weed_tables (void)
 {
-  rib_weed_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
-  rib_weed_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
+  rib_weed_table (zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT));
+  rib_weed_table (zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT));
 }
 
 /* Delete self installed routes after zebra is relaunched.  */
@@ -3999,8 +3895,8 @@ rib_sweep_table (struct route_table *table)
 void
 rib_sweep_route (void)
 {
-  rib_sweep_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
-  rib_sweep_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
+  rib_sweep_table (zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT));
+  rib_sweep_table (zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT));
 }
 
 /* Remove specific by protocol routes from 'table'. */
@@ -4032,8 +3928,8 @@ rib_score_proto_table (u_char proto, u_short instance, struct route_table *table
 unsigned long
 rib_score_proto (u_char proto, u_short instance)
 {
-  return  rib_score_proto_table (proto, instance, vrf_table (AFI_IP,  SAFI_UNICAST, 0))
-         +rib_score_proto_table (proto, instance, vrf_table (AFI_IP6, SAFI_UNICAST, 0));
+  return  rib_score_proto_table (proto, instance, zebra_vrf_table (AFI_IP,  SAFI_UNICAST, VRF_DEFAULT))
+         +rib_score_proto_table (proto, instance, zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT));
 }
 
 /* Close RIB and clean up kernel routes. */
@@ -4064,8 +3960,8 @@ rib_close (void)
   struct listnode *node, *nnode;
   struct interface *ifp;
 
-  rib_close_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
-  rib_close_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
+  rib_close_table (zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT));
+  rib_close_table (zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT));
 
   for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
     if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
@@ -4076,8 +3972,6 @@ void
 rib_init (void)
 {
   rib_queue_init (&zebrad);
-  /* VRF initialization.  */
-  vrf_init ();
 }
 
 /*
@@ -4088,15 +3982,19 @@ rib_init (void)
  * Returns TRUE if a vrf id was found, FALSE otherwise.
  */
 static inline int
-vrf_id_get_next (uint32_t id, uint32_t *next_id_p)
+vrf_id_get_next (vrf_id_t vrf_id, vrf_id_t *next_id_p)
 {
-  while (++id < vector_active (vrf_vector))
+  vrf_iter_t iter = vrf_iterator (vrf_id);
+  struct zebra_vrf *zvrf = vrf_iter2info (iter);
+
+  /* The same one ? Then find out the next. */
+  if (zvrf && (zvrf->vrf_id == vrf_id))
+    zvrf = vrf_iter2info (vrf_next (iter));
+
+  if (zvrf)
     {
-      if (vrf_lookup (id))
-       {
-         *next_id_p = id;
-         return 1;
-       }
+      *next_id_p = zvrf->vrf_id;
+      return 1;
     }
 
   return 0;
@@ -4144,7 +4042,7 @@ rib_tables_iter_next (rib_tables_iter_t *iter)
 
          while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis))
            {
-             table = vrf_table (afi_safis[iter->afi_safi_ix].afi,
+             table = zebra_vrf_table (afi_safis[iter->afi_safi_ix].afi,
                                 afi_safis[iter->afi_safi_ix].safi,
                                 iter->vrf_id);
              if (table)
@@ -4182,3 +4080,116 @@ rib_tables_iter_next (rib_tables_iter_t *iter)
 
   return table;
 }
+
+/*
+ * Create a routing table for the specific AFI/SAFI in the given VRF.
+ */
+static void
+zebra_vrf_table_create (struct zebra_vrf *zvrf, afi_t afi, safi_t safi)
+{
+  rib_table_info_t *info;
+  struct route_table *table;
+
+  assert (!zvrf->table[afi][safi]);
+
+  table = route_table_init ();
+  zvrf->table[afi][safi] = table;
+
+  info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info));
+  info->zvrf = zvrf;
+  info->afi = afi;
+  info->safi = safi;
+  table->info = info;
+}
+
+/* Allocate new zebra VRF. */
+struct zebra_vrf *
+zebra_vrf_alloc (vrf_id_t vrf_id)
+{
+  struct zebra_vrf *zvrf;
+
+  zvrf = XCALLOC (MTYPE_ZEBRA_VRF, sizeof (struct zebra_vrf));
+
+  /* Allocate routing table and static table.  */
+  zebra_vrf_table_create (zvrf, AFI_IP, SAFI_UNICAST);
+  zebra_vrf_table_create (zvrf, AFI_IP6, SAFI_UNICAST);
+  zvrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init ();
+  zvrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init ();
+  zebra_vrf_table_create (zvrf, AFI_IP, SAFI_MULTICAST);
+  zebra_vrf_table_create (zvrf, AFI_IP6, SAFI_MULTICAST);
+  zvrf->stable[AFI_IP][SAFI_MULTICAST] = route_table_init ();
+  zvrf->stable[AFI_IP6][SAFI_MULTICAST] = route_table_init ();
+
+  zvrf->rnh_table[AFI_IP] = route_table_init();
+  zvrf->rnh_table[AFI_IP6] = route_table_init();
+
+  zvrf->import_check_table[AFI_IP] = route_table_init();
+  zvrf->import_check_table[AFI_IP6] = route_table_init();
+
+  /* Set VRF ID */
+  zvrf->vrf_id = vrf_id;
+
+  return zvrf;
+}
+
+/* Lookup VRF by identifier.  */
+struct zebra_vrf *
+zebra_vrf_lookup (vrf_id_t vrf_id)
+{
+  return vector_lookup (zebra_vrf_vector, vrf_id);
+}
+
+/* Lookup the routing table in an enabled VRF. */
+struct route_table *
+zebra_vrf_table (afi_t afi, safi_t safi, vrf_id_t vrf_id)
+{
+  struct zebra_vrf *zvrf = vrf_info_lookup (vrf_id);
+
+  if (!zvrf)
+    return NULL;
+
+  if (afi >= AFI_MAX || safi >= SAFI_MAX)
+    return NULL;
+
+  return zvrf->table[afi][safi];
+}
+
+/* Lookup the static routing table in a VRF. */
+struct route_table *
+zebra_vrf_static_table (afi_t afi, safi_t safi, vrf_id_t vrf_id)
+{
+  struct zebra_vrf *zvrf = vrf_info_lookup (vrf_id);
+
+  if (!zvrf)
+    return NULL;
+
+  if (afi >= AFI_MAX || safi >= SAFI_MAX)
+    return NULL;
+
+  return zvrf->stable[afi][safi];
+}
+
+struct route_table *
+zebra_vrf_other_route_table (afi_t afi, u_int32_t table_id, vrf_id_t vrf_id)
+{
+  struct zebra_vrf *zvrf;
+
+  zvrf = vrf_info_lookup (vrf_id);
+  if (! zvrf)
+    return NULL;
+
+  if(afi >= AFI_MAX)
+    return NULL;
+
+  if (table_id >= ZEBRA_KERNEL_TABLE_MAX)
+    return NULL;
+
+  if (zvrf->other_table[afi][table_id] == NULL)
+    {
+      zvrf->other_table[afi][table_id] = route_table_init();
+    }
+
+  return (zvrf->other_table[afi][table_id]);
+}
+
+
index eea7759589efa6ccf6bbbb8f7f18a1815e908c13..09d2947c4e67533c63c0469cf0e90660b5536ead 100644 (file)
@@ -36,6 +36,7 @@
 #include "routemap.h"
 #include "stream.h"
 #include "nexthop.h"
+#include "vrf.h"
 
 #include "zebra/rib.h"
 #include "zebra/rt.h"
@@ -50,6 +51,16 @@ extern struct zebra_t zebrad;
 static void free_state(struct rib *rib, struct route_node *rn);
 static void copy_state(struct rnh *rnh, struct rib *rib,
                       struct route_node *rn);
+#define lookup_rnh_table(v, f)                  \
+({                                              \
+  struct zebra_vrf *zvrf;                        \
+  struct route_table *t = NULL;                  \
+  zvrf = zebra_vrf_lookup(v);                    \
+  if (zvrf)                                      \
+    t = zvrf->rnh_table[family2afi(f)];                 \
+  t;                                             \
+})
+
 static int compare_state(struct rib *r1, struct rib *r2);
 static int send_client(struct rnh *rnh, struct zserv *client, rnh_type_t type);
 static void print_rnh(struct route_node *rn, struct vty *vty);
@@ -57,21 +68,21 @@ static void print_rnh(struct route_node *rn, struct vty *vty);
 int zebra_rnh_ip_default_route = 0;
 int zebra_rnh_ipv6_default_route = 0;
 
-static inline struct route_table *get_rnh_table(int vrfid, int family,
+static inline struct route_table *get_rnh_table(vrf_id_t vrfid, int family,
                                                rnh_type_t type)
 {
-  struct vrf *vrf;
+  struct zebra_vrf *zvrf;
   struct route_table *t = NULL;
 
-  vrf = vrf_lookup(vrfid);
-  if (vrf)
+  zvrf = zebra_vrf_lookup(vrfid);
+  if (zvrf)
     switch (type)
       {
       case RNH_NEXTHOP_TYPE:
-       t = vrf->rnh_table[family2afi(family)];
+       t = zvrf->rnh_table[family2afi(family)];
        break;
       case RNH_IMPORT_CHECK_TYPE:
-       t = vrf->import_check_table[family2afi(family)];
+       t = zvrf->import_check_table[family2afi(family)];
        break;
       }
 
@@ -85,7 +96,7 @@ char *rnh_str (struct rnh *rnh, char *buf, int size)
 }
 
 struct rnh *
-zebra_add_rnh (struct prefix *p, u_int32_t vrfid, rnh_type_t type)
+zebra_add_rnh (struct prefix *p, vrf_id_t vrfid, rnh_type_t type)
 {
   struct route_table *table;
   struct route_node *rn;
@@ -125,7 +136,7 @@ zebra_add_rnh (struct prefix *p, u_int32_t vrfid, rnh_type_t type)
 }
 
 struct rnh *
-zebra_lookup_rnh (struct prefix *p, u_int32_t vrfid, rnh_type_t type)
+zebra_lookup_rnh (struct prefix *p, vrf_id_t vrfid, rnh_type_t type)
 {
   struct route_table *table;
   struct route_node *rn;
@@ -262,7 +273,7 @@ zebra_evaluate_rnh_nexthops(int family, struct rib *rib, struct route_node *prn,
 }
 
 int
-zebra_evaluate_rnh (int vrfid, int family, int force, rnh_type_t type,
+zebra_evaluate_rnh (vrf_id_t vrfid, int family, int force, rnh_type_t type,
                    struct prefix *p)
 {
   struct route_table *ptable;
@@ -289,7 +300,7 @@ zebra_evaluate_rnh (int vrfid, int family, int force, rnh_type_t type,
       return -1;
     }
 
-  ptable = vrf_table(family2afi(family), SAFI_UNICAST, vrfid);
+  ptable = zebra_vrf_table(family2afi(family), SAFI_UNICAST, vrfid);
   if (!ptable)
     {
       zlog_debug("evaluate_rnh_table: prefix table not found\n");
@@ -557,7 +568,7 @@ zebra_evaluate_rnh (int vrfid, int family, int force, rnh_type_t type,
 }
 
 int
-zebra_dispatch_rnh_table (int vrfid, int family, struct zserv *client,
+zebra_dispatch_rnh_table (vrf_id_t vrfid, int family, struct zserv *client,
                          rnh_type_t type)
 {
   struct route_table *ntable;
@@ -591,7 +602,7 @@ zebra_dispatch_rnh_table (int vrfid, int family, struct zserv *client,
 }
 
 void
-zebra_print_rnh_table (int vrfid, int af, struct vty *vty, rnh_type_t type)
+zebra_print_rnh_table (vrf_id_t vrfid, int af, struct vty *vty, rnh_type_t type)
 {
   struct route_table *table;
   struct route_node *rn;
@@ -609,7 +620,7 @@ zebra_print_rnh_table (int vrfid, int af, struct vty *vty, rnh_type_t type)
 }
 
 int
-zebra_cleanup_rnh_client (int vrfid, int family, struct zserv *client,
+zebra_cleanup_rnh_client (vrf_id_t vrfid, int family, struct zserv *client,
                          rnh_type_t type)
 {
   struct route_table *ntable;
index 494be8366ca219795055543b2a7585f5e0c7418c..6deb8780840e0af130d0421309a92f3ed624da4a 100644 (file)
@@ -52,9 +52,9 @@ typedef enum
 extern int zebra_rnh_ip_default_route;
 extern int zebra_rnh_ipv6_default_route;
 
-extern struct rnh *zebra_add_rnh(struct prefix *p, u_int32_t vrfid,
+extern struct rnh *zebra_add_rnh(struct prefix *p, vrf_id_t vrfid,
                                 rnh_type_t type);
-extern struct rnh *zebra_lookup_rnh(struct prefix *p, u_int32_t vrfid,
+extern struct rnh *zebra_lookup_rnh(struct prefix *p, vrf_id_t vrfid,
                                    rnh_type_t type);
 extern void zebra_delete_rnh(struct rnh *rnh, rnh_type_t type);
 extern void zebra_add_rnh_client(struct rnh *rnh, struct zserv *client, rnh_type_t type);
@@ -62,11 +62,11 @@ extern void zebra_register_rnh_static_nh(struct prefix *, struct route_node *);
 extern void zebra_deregister_rnh_static_nh(struct prefix *, struct route_node *);
 extern void zebra_remove_rnh_client(struct rnh *rnh, struct zserv *client,
                                    rnh_type_t type);
-extern int zebra_evaluate_rnh(int vrfid, int family, int force, rnh_type_t type,
+extern int zebra_evaluate_rnh(vrf_id_t vrfid, int family, int force, rnh_type_t type,
                              struct prefix *p);
-extern int zebra_dispatch_rnh_table(int vrfid, int family, struct zserv *cl, rnh_type_t);
-extern void zebra_print_rnh_table(int vrfid, int family, struct vty *vty, rnh_type_t);
+extern int zebra_dispatch_rnh_table(vrf_id_t vrfid, int family, struct zserv *cl, rnh_type_t);
+extern void zebra_print_rnh_table(vrf_id_t vrfid, int family, struct vty *vty, rnh_type_t);
 extern char *rnh_str(struct rnh *rnh, char *buf, int size);
-extern int zebra_cleanup_rnh_client(int vrf, int family, struct zserv *client,
+extern int zebra_cleanup_rnh_client(vrf_id_t vrf, int family, struct zserv *client,
                                    rnh_type_t type);
 #endif /*_ZEBRA_RNH_H */
index f0d3e7e5825c30eafdc61aa0804511f3fde48580..3d005aa55abfa350a8525e669bb3c82a3e5e329f 100644 (file)
  * 02111-1307, USA.  
  */
 
+/*
+ * Currently SNMP is only running properly for MIBs in the default VRF.
+ */
+
 #include <zebra.h>
 
 #ifdef HAVE_SNMP
@@ -31,6 +35,7 @@
 #include "command.h"
 #include "smux.h"
 #include "table.h"
+#include "vrf.h"
 
 #include "zebra/rib.h"
 #include "zebra/zserv.h"
@@ -143,7 +148,7 @@ ipFwNumber (struct variable *v, oid objid[], size_t *objid_len,
   if (smux_header_generic(v, objid, objid_len, exact, val_len, write_method) == MATCH_FAILED)
     return NULL;
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return NULL;
 
@@ -168,7 +173,7 @@ ipCidrNumber (struct variable *v, oid objid[], size_t *objid_len,
   if (smux_header_generic(v, objid, objid_len, exact, val_len, write_method) == MATCH_FAILED)
     return NULL;
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return 0;
 
@@ -330,7 +335,7 @@ get_fwtable_route_node(struct variable *v, oid objid[], size_t *objid_len,
   if (exact && (*objid_len != (unsigned) v->namelen + 10))
     return;
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return;
 
index a797b88391a74a812ef884d9b541b4d4f7d1eef4..9728a28c0e517c0073ff71ddfa13f567700bf57e 100644 (file)
@@ -28,6 +28,7 @@
 #include "table.h"
 #include "rib.h"
 #include "nexthop.h"
+#include "vrf.h"
 
 #include "zebra/zserv.h"
 #include "zebra/zebra_rnh.h"
@@ -1184,7 +1185,7 @@ DEFUN (show_ip_route,
   struct rib *rib;
   int first = 1;
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -1305,7 +1306,7 @@ DEFUN (show_ip_route_tag,
   if (argv[0])
     tag = atoi(argv[0]);
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -1349,7 +1350,7 @@ DEFUN (show_ip_route_prefix_longer,
       return CMD_WARNING;
     }
   
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -1382,7 +1383,7 @@ DEFUN (show_ip_route_supernets,
   u_int32_t addr; 
   int first = 1;
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -1428,7 +1429,7 @@ DEFUN (show_ip_route_protocol,
       return CMD_WARNING;
     }
   
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -1464,7 +1465,7 @@ DEFUN (show_ip_route_ospf_instance,
 
   VTY_GET_INTEGER ("Instance", instance, argv[0]);
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -1503,7 +1504,7 @@ DEFUN (show_ip_route_addr,
       return CMD_WARNING;
     }
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -1541,7 +1542,7 @@ DEFUN (show_ip_route_prefix,
       return CMD_WARNING;
     }
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -1712,7 +1713,7 @@ DEFUN (show_ip_route_summary,
 {
   struct route_table *table;
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -1733,7 +1734,7 @@ DEFUN (show_ip_route_summary_prefix,
 {
   struct route_table *table;
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -1754,7 +1755,7 @@ static_config_ipv4 (struct vty *vty)
   write = 0;
 
   /* Lookup table.  */
-  stable = vrf_static_table (AFI_IP, SAFI_UNICAST, 0);
+  stable = zebra_vrf_static_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
   if (! stable)
     return -1;
 
@@ -1816,7 +1817,7 @@ DEFUN (show_ip_mroute,
   struct rib *rib;
   int first = 1;
 
-  table = vrf_table (AFI_IP, SAFI_MULTICAST, 0);
+  table = zebra_vrf_table (AFI_IP, SAFI_MULTICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -2613,7 +2614,7 @@ DEFUN (show_ipv6_route,
   struct rib *rib;
   int first = 1;
 
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -2649,7 +2650,7 @@ DEFUN (show_ipv6_route_tag,
   if (argv[0])
     tag = atoi(argv[0]);
 
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -2686,7 +2687,7 @@ DEFUN (show_ipv6_route_prefix_longer,
   int ret;
   int first = 1;
 
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -2733,7 +2734,7 @@ DEFUN (show_ipv6_route_protocol,
       return CMD_WARNING;
     }
   
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -2772,7 +2773,7 @@ DEFUN (show_ipv6_route_addr,
       return CMD_WARNING;
     }
 
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -2810,7 +2811,7 @@ DEFUN (show_ipv6_route_prefix,
       return CMD_WARNING;
     }
 
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -2839,7 +2840,7 @@ DEFUN (show_ipv6_route_summary,
 {
   struct route_table *table;
 
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -2860,7 +2861,7 @@ DEFUN (show_ipv6_route_summary_prefix,
 {
   struct route_table *table;
 
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -2886,7 +2887,7 @@ DEFUN (show_ipv6_mroute,
   struct rib *rib;
   int first = 1;
 
-  table = vrf_table (AFI_IP6, SAFI_MULTICAST, 0);
+  table = zebra_vrf_table (AFI_IP6, SAFI_MULTICAST, VRF_DEFAULT);
   if (! table)
     return CMD_SUCCESS;
 
@@ -2917,7 +2918,7 @@ static_config_ipv6 (struct vty *vty)
   write = 0;
 
   /* Lookup table.  */
-  stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, 0);
+  stable = zebra_vrf_static_table (AFI_IP6, SAFI_UNICAST, VRF_DEFAULT);
   if (! stable)
     return -1;