]> git.proxmox.com Git - mirror_frr.git/blame - lib/ns.h
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / lib / ns.h
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
32bcb8b0
DS
2/*
3 * NS related header.
4 * Copyright (C) 2014 6WIND S.A.
32bcb8b0
DS
5 */
6
7#ifndef _ZEBRA_NS_H
8#define _ZEBRA_NS_H
9
c7fdd84f 10#include "openbsd-tree.h"
32bcb8b0 11#include "linklist.h"
697d3ec7 12#include "vty.h"
32bcb8b0 13
5e244469
RW
14#ifdef __cplusplus
15extern "C" {
16#endif
17
d7c0a89a 18typedef uint32_t ns_id_t;
32bcb8b0 19
3eca551f 20/* the default NS ID */
3eca551f 21#define NS_UNKNOWN UINT32_MAX
32bcb8b0 22
1fbe3e58 23/* Default netns directory (Linux) */
13460c44 24#define NS_RUN_DIR "/var/run/netns"
32bcb8b0 25
ec31f30d
PG
26#ifdef HAVE_NETNS
27#define NS_DEFAULT_NAME "/proc/self/ns/net"
28#else /* !HAVE_NETNS */
c7975431 29#define NS_DEFAULT_NAME "default-netns"
ec31f30d
PG
30#endif /* HAVE_NETNS */
31
d62a17ae 32struct ns {
33 RB_ENTRY(ns) entry;
c7fdd84f 34
d62a17ae 35 /* Identifier, same as the vector index */
36 ns_id_t ns_id;
c7fdd84f 37
03aff2d8
PG
38 /* Identifier, mapped on the NSID value */
39 ns_id_t internal_ns_id;
40
20f4b2b0
PG
41 /* Identifier, value of NSID of default netns,
42 * relative value in that local netns
43 */
44 ns_id_t relative_default_ns;
45
d62a17ae 46 /* Name */
47 char *name;
c7fdd84f 48
d62a17ae 49 /* File descriptor */
50 int fd;
f30c50b9 51
d62a17ae 52 /* Master list of interfaces belonging to this NS */
53 struct list *iflist;
f30c50b9 54
b95c1883
PG
55 /* Back Pointer to VRF */
56 void *vrf_ctxt;
57
d62a17ae 58 /* User data */
59 void *info;
f30c50b9 60};
d62a17ae 61RB_HEAD(ns_head, ns);
62RB_PROTOTYPE(ns_head, ns, entry, ns_compare)
c7fdd84f 63
e26aedbe
PG
64/*
65 * API for managing NETNS. eg from zebra daemon
66 * one want to manage the list of NETNS, etc...
67 */
68
32bcb8b0
DS
69/*
70 * NS hooks
71 */
72
c7975431
IR
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 */
32bcb8b0
DS
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 */
3347430b 86extern void ns_add_hook(int type, int (*)(struct ns *));
32bcb8b0 87
e26aedbe 88
32bcb8b0
DS
89/*
90 * NS initializer/destructor
91 */
e26aedbe 92
d62a17ae 93extern void ns_terminate(void);
32bcb8b0 94
e26aedbe
PG
95/* API to initialize NETNS managerment
96 * parameter is the default ns_id
97 */
03aff2d8 98extern void ns_init_management(ns_id_t ns_id, ns_id_t internal_ns_idx);
e26aedbe
PG
99
100
32bcb8b0
DS
101/*
102 * NS utilities
103 */
104
e26aedbe
PG
105/* Create a socket serving for the given NS
106 */
107int ns_socket(int domain, int type, int protocol, ns_id_t ns_id);
108
109/* return the path of the NETNS */
697d3ec7 110extern char *ns_netns_pathname(struct vty *vty, const char *name);
e26aedbe
PG
111
112/* Parse and execute a function on all the NETNS */
2961d060
PG
113#define NS_WALK_CONTINUE 0
114#define NS_WALK_STOP 1
115
116extern void ns_walk_func(int (*func)(struct ns *,
117 void *,
118 void **),
119 void *param_in,
120 void **param_out);
e26aedbe
PG
121
122/* API to get the NETNS name, from the ns pointer */
81c9005f 123extern const char *ns_get_name(struct ns *ns);
e26aedbe
PG
124
125/* only called from vrf ( when removing netns from vrf)
c7975431 126 * or at VRF termination
e26aedbe
PG
127 */
128extern 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 */
133extern int ns_have_netns(void);
134
135/* API to get context information of a NS */
136extern void *ns_info_lookup(ns_id_t ns_id);
137
03aff2d8
PG
138/* API to map internal ns id value with
139 * user friendly ns id external value
140 */
f0295a54 141extern ns_id_t ns_map_nsid_with_external(ns_id_t ns_id, bool map);
03aff2d8 142
e26aedbe
PG
143/*
144 * NS init routine
145 * should be called from backendx
146 */
147extern void ns_init(void);
148
1eb92f06 149#define NS_DEFAULT 0
e26aedbe
PG
150
151/* API that can be used to change from NS */
ce1be369
PG
152extern int ns_switchback_to_initial(void);
153extern int ns_switch_to_netns(const char *netns_name);
ce1be369 154
e26aedbe
PG
155/*
156 * NS handling routines.
157 * called by modules that use NS backend
158 */
159
160/* API to search for already present NETNS */
161extern struct ns *ns_lookup(ns_id_t ns_id);
162extern struct ns *ns_lookup_name(const char *name);
ec31f30d 163
e26aedbe
PG
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 */
169extern int ns_enable(struct ns *ns, void (*func)(ns_id_t, void *));
170extern struct ns *ns_get_created(struct ns *ns, char *name, ns_id_t ns_id);
b1cc23b2 171extern ns_id_t ns_id_get_absolute(ns_id_t ns_id_reference, ns_id_t link_nsid);
e26aedbe 172extern void ns_disable(struct ns *ns);
2d4e4d39 173extern struct ns *ns_get_default(void);
ec31f30d 174
5e244469
RW
175#ifdef __cplusplus
176}
177#endif
178
32bcb8b0 179#endif /*_ZEBRA_NS_H*/