]> git.proxmox.com Git - mirror_frr.git/blob - lib/ns.h
Merge pull request #5746 from donaldsharp/bgp_sa
[mirror_frr.git] / lib / ns.h
1 /*
2 * NS related header.
3 * Copyright (C) 2014 6WIND S.A.
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifndef _ZEBRA_NS_H
23 #define _ZEBRA_NS_H
24
25 #include "openbsd-tree.h"
26 #include "linklist.h"
27 #include "vty.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 typedef uint32_t ns_id_t;
34
35 /* the default NS ID */
36 #define NS_UNKNOWN UINT32_MAX
37
38 /* Default netns directory (Linux) */
39 #define NS_RUN_DIR "/var/run/netns"
40
41 #ifdef HAVE_NETNS
42 #define NS_DEFAULT_NAME "/proc/self/ns/net"
43 #else /* !HAVE_NETNS */
44 #define NS_DEFAULT_NAME "default-netns"
45 #endif /* HAVE_NETNS */
46
47 struct ns {
48 RB_ENTRY(ns) entry;
49
50 /* Identifier, same as the vector index */
51 ns_id_t ns_id;
52
53 /* Identifier, mapped on the NSID value */
54 ns_id_t internal_ns_id;
55
56 /* Name */
57 char *name;
58
59 /* File descriptor */
60 int fd;
61
62 /* Master list of interfaces belonging to this NS */
63 struct list *iflist;
64
65 /* Back Pointer to VRF */
66 void *vrf_ctxt;
67
68 /* User data */
69 void *info;
70 };
71 RB_HEAD(ns_head, ns);
72 RB_PROTOTYPE(ns_head, ns, entry, ns_compare)
73
74 /*
75 * API for managing NETNS. eg from zebra daemon
76 * one want to manage the list of NETNS, etc...
77 */
78
79 /*
80 * NS hooks
81 */
82
83 #define NS_NEW_HOOK 0 /* a new netns is just created */
84 #define NS_DELETE_HOOK 1 /* a netns is to be deleted */
85 #define NS_ENABLE_HOOK 2 /* a netns is ready to use */
86 #define NS_DISABLE_HOOK 3 /* a netns is to be unusable */
87
88 /*
89 * Add a specific hook ns module.
90 * @param1: hook type
91 * @param2: the callback function
92 * - param 1: the NS ID
93 * - param 2: the address of the user data pointer (the user data
94 * can be stored in or freed from there)
95 */
96 extern void ns_add_hook(int type, int (*)(struct ns *));
97
98
99 /*
100 * NS initializer/destructor
101 */
102
103 extern void ns_terminate(void);
104
105 /* API to initialize NETNS managerment
106 * parameter is the default ns_id
107 */
108 extern void ns_init_management(ns_id_t ns_id, ns_id_t internal_ns_idx);
109
110
111 /*
112 * NS utilities
113 */
114
115 /* Create a socket serving for the given NS
116 */
117 int ns_socket(int domain, int type, int protocol, ns_id_t ns_id);
118
119 /* return the path of the NETNS */
120 extern char *ns_netns_pathname(struct vty *vty, const char *name);
121
122 /* Parse and execute a function on all the NETNS */
123 extern void ns_walk_func(int (*func)(struct ns *));
124
125 /* API to get the NETNS name, from the ns pointer */
126 extern const char *ns_get_name(struct ns *ns);
127
128 /* only called from vrf ( when removing netns from vrf)
129 * or at VRF termination
130 */
131 extern void ns_delete(struct ns *ns);
132
133 /* return > 0 if netns is available
134 * called by VRF to check netns backend is available for VRF
135 */
136 extern int ns_have_netns(void);
137
138 /* API to get context information of a NS */
139 extern void *ns_info_lookup(ns_id_t ns_id);
140
141 /* API to map internal ns id value with
142 * user friendly ns id external value
143 */
144 extern ns_id_t ns_map_nsid_with_external(ns_id_t ns_id, bool map);
145
146 /*
147 * NS init routine
148 * should be called from backendx
149 */
150 extern void ns_init(void);
151
152 /* API to retrieve default NS */
153 extern ns_id_t ns_get_default_id(void);
154
155 #define NS_DEFAULT ns_get_default_id()
156
157 /* API that can be used to change from NS */
158 extern int ns_switchback_to_initial(void);
159 extern int ns_switch_to_netns(const char *netns_name);
160
161 /*
162 * NS handling routines.
163 * called by modules that use NS backend
164 */
165
166 /* API to search for already present NETNS */
167 extern struct ns *ns_lookup(ns_id_t ns_id);
168 extern struct ns *ns_lookup_name(const char *name);
169
170 /* API to handle NS : creation, enable, disable
171 * for enable, a callback function is passed as parameter
172 * the callback belongs to the module that uses NS as backend
173 * upon enabling the NETNS, the upper layer is informed
174 */
175 extern int ns_enable(struct ns *ns, void (*func)(ns_id_t, void *));
176 extern struct ns *ns_get_created(struct ns *ns, char *name, ns_id_t ns_id);
177 extern void ns_disable(struct ns *ns);
178
179 #ifdef __cplusplus
180 }
181 #endif
182
183 #endif /*_ZEBRA_NS_H*/