/src/openvswitch/lib/netnsid.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2017 Red Hat Inc. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at: |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #ifndef NETNSID_H |
18 | | #define NETNSID_H 1 |
19 | | |
20 | | #include <stdbool.h> |
21 | | |
22 | | #include "util.h" |
23 | | |
24 | | #ifdef HAVE_LINUX_NET_NAMESPACE_H |
25 | | #include <linux/net_namespace.h> |
26 | | #endif |
27 | | |
28 | | /* |
29 | | * The network namespace ID is a positive number that identifies the namespace |
30 | | * which the netlink message was sent. It is used to identify if a received |
31 | | * message belongs to a port attached to the bridge. |
32 | | * |
33 | | * There are three port states listed below: |
34 | | * UNSET: A port in this state means that it could be either in same network |
35 | | * namespace as the daemon (LOCAL) or in another namespace (ID). Any operation |
36 | | * on a port in this state that requires the ID will trigger a query to the |
37 | | * kernel to find out in which namespace the port currently is. |
38 | | * |
39 | | * LOCAL: A port in this state means that it is in the same network namespace |
40 | | * as the daemons. |
41 | | * |
42 | | * ID: A port that is not LOCAL and not UNSET has a valid positive (zero |
43 | | * included) remote namespace ID. |
44 | | * |
45 | | * Possible state changes: |
46 | | * |
47 | | * Initial port's state: UNSET. |
48 | | * |
49 | | * UNSET -> LOCAL: The daemon queries the kernel and finds that it's in the |
50 | | * same network namespace as the daemon or the API is not available (older |
51 | | * kernels). |
52 | | * |
53 | | * LOCAL -> UNSET: The kernel sends a deregistering netlink message which |
54 | | * unsets the port. It happens when the port is removed (or moved to another |
55 | | * network namespace). |
56 | | * |
57 | | * UNSET -> ID: The daemon queries the kernel and finds that the port is |
58 | | * in a specific network namespace with ID assigned. |
59 | | * |
60 | | * ID -> UNSET: When it receives a deregistering netlink message from that |
61 | | * namespace indicating the device is being removed (or moved to another |
62 | | * network namespace). |
63 | | */ |
64 | | |
65 | | #ifdef NETNSA_NSID_NOT_ASSIGNED |
66 | 0 | #define NETNSID_LOCAL NETNSA_NSID_NOT_ASSIGNED |
67 | | #else |
68 | | #define NETNSID_LOCAL -1 |
69 | | #endif |
70 | 0 | #define NETNSID_UNSET (NETNSID_LOCAL - 1) |
71 | | |
72 | | /* The nsid that the local kernel assigns to our own network namespace when a |
73 | | * self-referential mapping exists, or NETNSID_LOCAL otherwise. It is defined |
74 | | * in netnsid.c and set once at startup by the platform-specific netdev code |
75 | | * (see netdev-linux.c). netnsid_is_local() treats this value as equivalent |
76 | | * to the local namespace. */ |
77 | | extern int netnsid_self; |
78 | | void netnsid_set_self(int nsid); |
79 | | |
80 | | /* Prototypes */ |
81 | | static inline void netnsid_set_local(int *nsid); |
82 | | static inline bool netnsid_is_local(int nsid); |
83 | | static inline void netnsid_unset(int *nsid); |
84 | | static inline bool netnsid_is_unset(int nsid); |
85 | | static inline bool netnsid_is_remote(int nsid); |
86 | | static inline void netnsid_set(int *nsid, int id); |
87 | | static inline bool netnsid_eq(int nsid1, int nsid2); |
88 | | |
89 | | /* Functions */ |
90 | | static inline void |
91 | | netnsid_set_local(int *nsid) |
92 | 0 | { |
93 | 0 | *nsid = NETNSID_LOCAL; |
94 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_set_local Unexecuted instantiation: netlink-socket.c:netnsid_set_local Unexecuted instantiation: netnsid.c:netnsid_set_local Unexecuted instantiation: dpif-netlink.c:netnsid_set_local |
95 | | |
96 | | static inline bool |
97 | | netnsid_is_local(int nsid) |
98 | 0 | { |
99 | 0 | return nsid == NETNSID_LOCAL || nsid == netnsid_self; |
100 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_is_local Unexecuted instantiation: netlink-socket.c:netnsid_is_local Unexecuted instantiation: netnsid.c:netnsid_is_local Unexecuted instantiation: dpif-netlink.c:netnsid_is_local |
101 | | |
102 | | static inline void |
103 | | netnsid_unset(int *nsid) |
104 | 0 | { |
105 | 0 | *nsid = NETNSID_UNSET; |
106 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_unset Unexecuted instantiation: netlink-socket.c:netnsid_unset Unexecuted instantiation: netnsid.c:netnsid_unset Unexecuted instantiation: dpif-netlink.c:netnsid_unset |
107 | | |
108 | | static inline bool |
109 | | netnsid_is_unset(int nsid) |
110 | 0 | { |
111 | 0 | return nsid == NETNSID_UNSET; |
112 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_is_unset Unexecuted instantiation: netlink-socket.c:netnsid_is_unset Unexecuted instantiation: netnsid.c:netnsid_is_unset Unexecuted instantiation: dpif-netlink.c:netnsid_is_unset |
113 | | |
114 | | static inline bool |
115 | | netnsid_is_remote(int nsid) |
116 | 0 | { |
117 | 0 | if (netnsid_is_unset(nsid) || netnsid_is_local(nsid)) { |
118 | 0 | return false; |
119 | 0 | } |
120 | | |
121 | 0 | return true; |
122 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_is_remote Unexecuted instantiation: netlink-socket.c:netnsid_is_remote Unexecuted instantiation: netnsid.c:netnsid_is_remote Unexecuted instantiation: dpif-netlink.c:netnsid_is_remote |
123 | | |
124 | | static inline void |
125 | | netnsid_set(int *nsid, int id) |
126 | 0 | { |
127 | | /* The kernel only sends positive numbers for valid IDs. */ |
128 | 0 | if (id != NETNSID_LOCAL) { |
129 | 0 | ovs_assert(id >= 0); |
130 | 0 | } |
131 | |
|
132 | 0 | *nsid = id; |
133 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_set Unexecuted instantiation: netlink-socket.c:netnsid_set Unexecuted instantiation: netnsid.c:netnsid_set Unexecuted instantiation: dpif-netlink.c:netnsid_set |
134 | | |
135 | | static inline bool |
136 | | netnsid_eq(int nsid1, int nsid2) |
137 | 0 | { |
138 | 0 | if (netnsid_is_unset(nsid1) || netnsid_is_unset(nsid2)) { |
139 | 0 | return false; |
140 | 0 | } |
141 | | |
142 | | /* The local namespace may be represented either as NETNSID_LOCAL or as the |
143 | | * self-referential nsid the kernel assigned to it (see netnsid_self), so |
144 | | * two local nsids are equal regardless of their exact representation. */ |
145 | 0 | if (netnsid_is_local(nsid1) && netnsid_is_local(nsid2)) { |
146 | 0 | return true; |
147 | 0 | } |
148 | | |
149 | 0 | if (nsid1 == nsid2) { |
150 | 0 | return true; |
151 | 0 | } |
152 | | |
153 | 0 | return false; |
154 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_eq Unexecuted instantiation: netlink-socket.c:netnsid_eq Unexecuted instantiation: netnsid.c:netnsid_eq Unexecuted instantiation: dpif-netlink.c:netnsid_eq |
155 | | |
156 | | #endif |