]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/ns.h
Merge pull request #3397 from mjstapp/fix_stream_macros
[mirror_frr.git] / lib / ns.h
index 3fac739861093e1f39c1944d0252d3b79df13e21..b3810f069b46b8914b24c565b5c8daa524247990 100644 (file)
--- a/lib/ns.h
+++ b/lib/ns.h
  * 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.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #ifndef _ZEBRA_NS_H
 #define _ZEBRA_NS_H
 
+#include "openbsd-tree.h"
 #include "linklist.h"
+#include "vty.h"
 
-typedef u_int16_t ns_id_t;
+typedef uint32_t ns_id_t;
 
-/* The default NS ID */
-#define NS_DEFAULT 0
+/* the default NS ID */
+#define NS_UNKNOWN UINT32_MAX
 
-/*
- * The command strings
- */
+/* Default netns directory (Linux) */
 #define NS_RUN_DIR         "/var/run/netns"
-#define NS_CMD_STR         "logical-router <0-65535>"
-#define NS_CMD_HELP_STR    "Specify the Logical-Router\nThe Logical-Router ID\n"
 
-#define NS_ALL_CMD_STR         "logical-router all"
-#define NS_ALL_CMD_HELP_STR    "Specify the logical-router\nAll logical-router's\n"
+#ifdef HAVE_NETNS
+#define NS_DEFAULT_NAME    "/proc/self/ns/net"
+#else  /* !HAVE_NETNS */
+#define NS_DEFAULT_NAME    "Default-logical-router"
+#endif /* HAVE_NETNS */
+
+struct ns {
+       RB_ENTRY(ns) entry;
+
+       /* Identifier, same as the vector index */
+       ns_id_t ns_id;
+
+       /* Identifier, mapped on the NSID value */
+       ns_id_t internal_ns_id;
+
+       /* Name */
+       char *name;
+
+       /* File descriptor */
+       int fd;
+
+       /* Master list of interfaces belonging to this NS */
+       struct list *iflist;
+
+       /* Back Pointer to VRF */
+       void *vrf_ctxt;
+
+       /* User data */
+       void *info;
+};
+RB_HEAD(ns_head, ns);
+RB_PROTOTYPE(ns_head, ns, entry, ns_compare)
+
+extern struct ns_head ns_tree;
+
+/*
+ * API for managing NETNS. eg from zebra daemon
+ * one want to manage the list of NETNS, etc...
+ */
 
 /*
  * NS hooks
@@ -57,86 +91,87 @@ typedef u_int16_t ns_id_t;
  *          - param 2: the address of the user data pointer (the user data
  *                     can be stored in or freed from there)
  */
-extern void ns_add_hook (int, int (*)(ns_id_t, void **));
+extern void ns_add_hook(int type, int (*)(struct ns *));
+
 
 /*
- * NS iteration
+ * NS initializer/destructor
  */
 
-typedef void *              ns_iter_t;
-#define NS_ITER_INVALID    NULL    /* invalid value of the iterator */
+extern void ns_terminate(void);
 
-/*
- * NS iteration utilities. Example for the usage:
- *
- *   ns_iter_t iter = ns_first();
- *   for (; iter != NS_ITER_INVALID; iter = ns_next (iter))
- *
- * or
- *
- *   ns_iter_t iter = ns_iterator (<a given NS ID>);
- *   for (; iter != NS_ITER_INVALID; iter = ns_next (iter))
+/* API to initialize NETNS managerment
+ * parameter is the default ns_id
  */
+extern void ns_init_management(ns_id_t ns_id, ns_id_t internal_ns_idx);
 
-/* Return the iterator of the first NS. */
-extern ns_iter_t ns_first (void);
-/* Return the next NS iterator to the given iterator. */
-extern ns_iter_t ns_next (ns_iter_t);
-/* Return the NS iterator of the given NS ID. If it does not exist,
- * the iterator of the next existing NS is returned. */
-extern ns_iter_t ns_iterator (ns_id_t);
 
 /*
- * NS iterator to properties
+ * NS utilities
  */
-extern ns_id_t ns_iter2id (ns_iter_t);
-extern void *ns_iter2info (ns_iter_t);
-extern struct list *ns_iter2iflist (ns_iter_t);
 
-/*
- * Utilities to obtain the user data
+/* Create a socket serving for the given NS
  */
+int ns_socket(int domain, int type, int protocol, ns_id_t ns_id);
 
-/* Get the data pointer of the specified NS. If not found, create one. */
-extern void *ns_info_get (ns_id_t);
-/* Look up the data pointer of the specified NS. */
-extern void *ns_info_lookup (ns_id_t);
+/* return the path of the NETNS */
+extern char *ns_netns_pathname(struct vty *vty, const char *name);
 
-/*
- * Utilities to obtain the interface list
- */
+/* Parse and execute a function on all the NETNS */
+extern void ns_walk_func(int (*func)(struct ns *));
 
-/* Look up the interface list of the specified NS. */
-extern struct list *ns_iflist (ns_id_t);
-/* Get the interface list of the specified NS. Create one if not find. */
-extern struct list *ns_iflist_get (ns_id_t);
+/* API to get the NETNS name, from the ns pointer */
+extern const char *ns_get_name(struct ns *ns);
 
-/*
- * NS bit-map: maintaining flags, one bit per NS ID
+/* only called from vrf ( when removing netns from vrf)
+ * or at VRF or logical router termination
+ */
+extern void ns_delete(struct ns *ns);
+
+/* return > 0 if netns is available
+ * called by VRF to check netns backend is available for VRF
  */
+extern int ns_have_netns(void);
 
-typedef void *              ns_bitmap_t;
-#define NS_BITMAP_NULL     NULL
+/* API to get context information of a NS */
+extern void *ns_info_lookup(ns_id_t ns_id);
 
-extern ns_bitmap_t ns_bitmap_init (void);
-extern void ns_bitmap_free (ns_bitmap_t);
-extern void ns_bitmap_set (ns_bitmap_t, ns_id_t);
-extern void ns_bitmap_unset (ns_bitmap_t, ns_id_t);
-extern int ns_bitmap_check (ns_bitmap_t, ns_id_t);
+/* API to map internal ns id value with
+ * user friendly ns id external value
+ */
+extern ns_id_t ns_map_nsid_with_external(ns_id_t ns_id, bool map);
 
 /*
- * NS initializer/destructor
+ * NS init routine
+ * should be called from backendx
  */
-/* Please add hooks before calling ns_init(). */
-extern void ns_init (void);
-extern void ns_terminate (void);
+extern void ns_init(void);
+
+/* API to retrieve default NS */
+extern ns_id_t ns_get_default_id(void);
+
+#define NS_DEFAULT ns_get_default_id()
+
+/* API that can be used to change from NS */
+extern int ns_switchback_to_initial(void);
+extern int ns_switch_to_netns(const char *netns_name);
 
 /*
- * NS utilities
+ * NS handling routines.
+ * called by modules that use NS backend
  */
 
-/* Create a socket serving for the given NS */
-extern int ns_socket (int, int, int, ns_id_t);
+/* API to search for already present NETNS */
+extern struct ns *ns_lookup(ns_id_t ns_id);
+extern struct ns *ns_lookup_name(const char *name);
 
-#endif /*_ZEBRA_NS_H*/
+/* API to handle NS : creation, enable, disable
+ * for enable, a callback function is passed as parameter
+ * the callback belongs to the module that uses NS as backend
+ * upon enabling the NETNS, the upper layer is informed
+ */
+extern int ns_enable(struct ns *ns, void (*func)(ns_id_t, void *));
+extern struct ns *ns_get_created(struct ns *ns, char *name, ns_id_t ns_id);
+extern void ns_disable(struct ns *ns);
 
+#endif /*_ZEBRA_NS_H*/