1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2014 6WIND S.A.
10 #include "openbsd-tree.h"
18 typedef uint32_t ns_id_t
;
20 /* the default NS ID */
21 #define NS_UNKNOWN UINT32_MAX
23 /* Default netns directory (Linux) */
24 #define NS_RUN_DIR "/var/run/netns"
27 #define NS_DEFAULT_NAME "/proc/self/ns/net"
28 #else /* !HAVE_NETNS */
29 #define NS_DEFAULT_NAME "default-netns"
30 #endif /* HAVE_NETNS */
35 /* Identifier, same as the vector index */
38 /* Identifier, mapped on the NSID value */
39 ns_id_t internal_ns_id
;
41 /* Identifier, value of NSID of default netns,
42 * relative value in that local netns
44 ns_id_t relative_default_ns
;
52 /* Master list of interfaces belonging to this NS */
55 /* Back Pointer to VRF */
62 RB_PROTOTYPE(ns_head
, ns
, entry
, ns_compare
)
65 * API for managing NETNS. eg from zebra daemon
66 * one want to manage the list of NETNS, etc...
73 #define NS_NEW_HOOK 0 /* a new netns is just created */
74 #define NS_DELETE_HOOK 1 /* a netns is to be deleted */
75 #define NS_ENABLE_HOOK 2 /* a netns is ready to use */
76 #define NS_DISABLE_HOOK 3 /* a netns is to be unusable */
79 * Add a specific hook ns module.
81 * @param2: the callback function
82 * - param 1: the NS ID
83 * - param 2: the address of the user data pointer (the user data
84 * can be stored in or freed from there)
86 extern void ns_add_hook(int type
, int (*)(struct ns
*));
90 * NS initializer/destructor
93 extern void ns_terminate(void);
95 /* API to initialize NETNS managerment
96 * parameter is the default ns_id
98 extern void ns_init_management(ns_id_t ns_id
, ns_id_t internal_ns_idx
);
105 /* Create a socket serving for the given NS
107 int ns_socket(int domain
, int type
, int protocol
, ns_id_t ns_id
);
109 /* return the path of the NETNS */
110 extern char *ns_netns_pathname(struct vty
*vty
, const char *name
);
112 /* Parse and execute a function on all the NETNS */
113 #define NS_WALK_CONTINUE 0
114 #define NS_WALK_STOP 1
116 extern void ns_walk_func(int (*func
)(struct ns
*,
122 /* API to get the NETNS name, from the ns pointer */
123 extern const char *ns_get_name(struct ns
*ns
);
125 /* only called from vrf ( when removing netns from vrf)
126 * or at VRF termination
128 extern void ns_delete(struct ns
*ns
);
130 /* return > 0 if netns is available
131 * called by VRF to check netns backend is available for VRF
133 extern int ns_have_netns(void);
135 /* API to get context information of a NS */
136 extern void *ns_info_lookup(ns_id_t ns_id
);
138 /* API to map internal ns id value with
139 * user friendly ns id external value
141 extern ns_id_t
ns_map_nsid_with_external(ns_id_t ns_id
, bool map
);
145 * should be called from backendx
147 extern void ns_init(void);
151 /* API that can be used to change from NS */
152 extern int ns_switchback_to_initial(void);
153 extern int ns_switch_to_netns(const char *netns_name
);
156 * NS handling routines.
157 * called by modules that use NS backend
160 /* API to search for already present NETNS */
161 extern struct ns
*ns_lookup(ns_id_t ns_id
);
162 extern struct ns
*ns_lookup_name(const char *name
);
164 /* API to handle NS : creation, enable, disable
165 * for enable, a callback function is passed as parameter
166 * the callback belongs to the module that uses NS as backend
167 * upon enabling the NETNS, the upper layer is informed
169 extern int ns_enable(struct ns
*ns
, void (*func
)(ns_id_t
, void *));
170 extern struct ns
*ns_get_created(struct ns
*ns
, char *name
, ns_id_t ns_id
);
171 extern ns_id_t
ns_id_get_absolute(ns_id_t ns_id_reference
, ns_id_t link_nsid
);
172 extern void ns_disable(struct ns
*ns
);
173 extern struct ns
*ns_get_default(void);
179 #endif /*_ZEBRA_NS_H*/