]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-or-later | |
2 | /* | |
3 | * NS related header. | |
4 | * Copyright (C) 2014 6WIND S.A. | |
5 | */ | |
6 | ||
7 | #ifndef _ZEBRA_NS_H | |
8 | #define _ZEBRA_NS_H | |
9 | ||
10 | #include "openbsd-tree.h" | |
11 | #include "linklist.h" | |
12 | #include "vty.h" | |
13 | ||
14 | #ifdef __cplusplus | |
15 | extern "C" { | |
16 | #endif | |
17 | ||
18 | typedef uint32_t ns_id_t; | |
19 | ||
20 | /* the default NS ID */ | |
21 | #define NS_UNKNOWN UINT32_MAX | |
22 | ||
23 | /* Default netns directory (Linux) */ | |
24 | #define NS_RUN_DIR "/var/run/netns" | |
25 | ||
26 | #ifdef HAVE_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 */ | |
31 | ||
32 | struct ns { | |
33 | RB_ENTRY(ns) entry; | |
34 | ||
35 | /* Identifier, same as the vector index */ | |
36 | ns_id_t ns_id; | |
37 | ||
38 | /* Identifier, mapped on the NSID value */ | |
39 | ns_id_t internal_ns_id; | |
40 | ||
41 | /* Identifier, value of NSID of default netns, | |
42 | * relative value in that local netns | |
43 | */ | |
44 | ns_id_t relative_default_ns; | |
45 | ||
46 | /* Name */ | |
47 | char *name; | |
48 | ||
49 | /* File descriptor */ | |
50 | int fd; | |
51 | ||
52 | /* Master list of interfaces belonging to this NS */ | |
53 | struct list *iflist; | |
54 | ||
55 | /* Back Pointer to VRF */ | |
56 | void *vrf_ctxt; | |
57 | ||
58 | /* User data */ | |
59 | void *info; | |
60 | }; | |
61 | RB_HEAD(ns_head, ns); | |
62 | RB_PROTOTYPE(ns_head, ns, entry, ns_compare) | |
63 | ||
64 | /* | |
65 | * API for managing NETNS. eg from zebra daemon | |
66 | * one want to manage the list of NETNS, etc... | |
67 | */ | |
68 | ||
69 | /* | |
70 | * NS hooks | |
71 | */ | |
72 | ||
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 */ | |
77 | ||
78 | /* | |
79 | * Add a specific hook ns module. | |
80 | * @param1: hook type | |
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) | |
85 | */ | |
86 | extern void ns_add_hook(int type, int (*)(struct ns *)); | |
87 | ||
88 | ||
89 | /* | |
90 | * NS initializer/destructor | |
91 | */ | |
92 | ||
93 | extern void ns_terminate(void); | |
94 | ||
95 | /* API to initialize NETNS managerment | |
96 | * parameter is the default ns_id | |
97 | */ | |
98 | extern void ns_init_management(ns_id_t ns_id, ns_id_t internal_ns_idx); | |
99 | ||
100 | ||
101 | /* | |
102 | * NS utilities | |
103 | */ | |
104 | ||
105 | /* Create a socket serving for the given NS | |
106 | */ | |
107 | int ns_socket(int domain, int type, int protocol, ns_id_t ns_id); | |
108 | ||
109 | /* return the path of the NETNS */ | |
110 | extern char *ns_netns_pathname(struct vty *vty, const char *name); | |
111 | ||
112 | /* Parse and execute a function on all the NETNS */ | |
113 | #define NS_WALK_CONTINUE 0 | |
114 | #define NS_WALK_STOP 1 | |
115 | ||
116 | extern void ns_walk_func(int (*func)(struct ns *, | |
117 | void *, | |
118 | void **), | |
119 | void *param_in, | |
120 | void **param_out); | |
121 | ||
122 | /* API to get the NETNS name, from the ns pointer */ | |
123 | extern const char *ns_get_name(struct ns *ns); | |
124 | ||
125 | /* only called from vrf ( when removing netns from vrf) | |
126 | * or at VRF termination | |
127 | */ | |
128 | extern void ns_delete(struct ns *ns); | |
129 | ||
130 | /* return > 0 if netns is available | |
131 | * called by VRF to check netns backend is available for VRF | |
132 | */ | |
133 | extern int ns_have_netns(void); | |
134 | ||
135 | /* API to get context information of a NS */ | |
136 | extern void *ns_info_lookup(ns_id_t ns_id); | |
137 | ||
138 | /* API to map internal ns id value with | |
139 | * user friendly ns id external value | |
140 | */ | |
141 | extern ns_id_t ns_map_nsid_with_external(ns_id_t ns_id, bool map); | |
142 | ||
143 | /* | |
144 | * NS init routine | |
145 | * should be called from backendx | |
146 | */ | |
147 | extern void ns_init(void); | |
148 | ||
149 | #define NS_DEFAULT 0 | |
150 | ||
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); | |
154 | ||
155 | /* | |
156 | * NS handling routines. | |
157 | * called by modules that use NS backend | |
158 | */ | |
159 | ||
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); | |
163 | ||
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 | |
168 | */ | |
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); | |
174 | ||
175 | #ifdef __cplusplus | |
176 | } | |
177 | #endif | |
178 | ||
179 | #endif /*_ZEBRA_NS_H*/ |