/src/openvswitch/lib/netnsid.h
Line | Count | Source (jump to first uncovered line) |
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 | | #ifdef HAVE_LINUX_NET_NAMESPACE_H |
23 | | #include <linux/net_namespace.h> |
24 | | #endif |
25 | | |
26 | | /* |
27 | | * The network namespace ID is a positive number that identifies the namespace |
28 | | * which the netlink message was sent. It is used to identify if a received |
29 | | * message belongs to a port attached to the bridge. |
30 | | * |
31 | | * There are three port states listed below: |
32 | | * UNSET: A port in this state means that it could be either in same network |
33 | | * namespace as the daemon (LOCAL) or in another namespace (ID). Any operation |
34 | | * on a port in this state that requires the ID will trigger a query to the |
35 | | * kernel to find out in which namespace the port currently is. |
36 | | * |
37 | | * LOCAL: A port in this state means that it is in the same network namespace |
38 | | * as the daemons. |
39 | | * |
40 | | * ID: A port that is not LOCAL and not UNSET has a valid positive (zero |
41 | | * included) remote namespace ID. |
42 | | * |
43 | | * Possible state changes: |
44 | | * |
45 | | * Initial port's state: UNSET. |
46 | | * |
47 | | * UNSET -> LOCAL: The daemon queries the kernel and finds that it's in the |
48 | | * same network namespace as the daemon or the API is not available (older |
49 | | * kernels). |
50 | | * |
51 | | * LOCAL -> UNSET: The kernel sends a deregistering netlink message which |
52 | | * unsets the port. It happens when the port is removed (or moved to another |
53 | | * network namespace). |
54 | | * |
55 | | * UNSET -> ID: The daemon queries the kernel and finds that the port is |
56 | | * in a specific network namespace with ID assigned. |
57 | | * |
58 | | * ID -> UNSET: When it receives a deregistering netlink message from that |
59 | | * namespace indicating the device is being removed (or moved to another |
60 | | * network namespace). |
61 | | */ |
62 | | |
63 | | #ifdef NETNSA_NSID_NOT_ASSIGNED |
64 | 0 | #define NETNSID_LOCAL NETNSA_NSID_NOT_ASSIGNED |
65 | | #else |
66 | | #define NETNSID_LOCAL -1 |
67 | | #endif |
68 | 0 | #define NETNSID_UNSET (NETNSID_LOCAL - 1) |
69 | | |
70 | | /* Prototypes */ |
71 | | static inline void netnsid_set_local(int *nsid); |
72 | | static inline bool netnsid_is_local(int nsid); |
73 | | static inline void netnsid_unset(int *nsid); |
74 | | static inline bool netnsid_is_unset(int nsid); |
75 | | static inline bool netnsid_is_remote(int nsid); |
76 | | static inline void netnsid_set(int *nsid, int id); |
77 | | static inline bool netnsid_eq(int nsid1, int nsid2); |
78 | | |
79 | | /* Functions */ |
80 | | static inline void |
81 | | netnsid_set_local(int *nsid) |
82 | 0 | { |
83 | 0 | *nsid = NETNSID_LOCAL; |
84 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_set_local Unexecuted instantiation: netlink-socket.c:netnsid_set_local Unexecuted instantiation: dpif-netlink.c:netnsid_set_local |
85 | | |
86 | | static inline bool |
87 | | netnsid_is_local(int nsid) |
88 | 0 | { |
89 | 0 | return nsid == NETNSID_LOCAL; |
90 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_is_local Unexecuted instantiation: netlink-socket.c:netnsid_is_local Unexecuted instantiation: dpif-netlink.c:netnsid_is_local |
91 | | |
92 | | static inline void |
93 | | netnsid_unset(int *nsid) |
94 | 0 | { |
95 | 0 | *nsid = NETNSID_UNSET; |
96 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_unset Unexecuted instantiation: netlink-socket.c:netnsid_unset Unexecuted instantiation: dpif-netlink.c:netnsid_unset |
97 | | |
98 | | static inline bool |
99 | | netnsid_is_unset(int nsid) |
100 | 0 | { |
101 | 0 | return nsid == NETNSID_UNSET; |
102 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_is_unset Unexecuted instantiation: netlink-socket.c:netnsid_is_unset Unexecuted instantiation: dpif-netlink.c:netnsid_is_unset |
103 | | |
104 | | static inline bool |
105 | | netnsid_is_remote(int nsid) |
106 | 0 | { |
107 | 0 | if (netnsid_is_unset(nsid) || netnsid_is_local(nsid)) { |
108 | 0 | return false; |
109 | 0 | } |
110 | | |
111 | 0 | return true; |
112 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_is_remote Unexecuted instantiation: netlink-socket.c:netnsid_is_remote Unexecuted instantiation: dpif-netlink.c:netnsid_is_remote |
113 | | |
114 | | static inline void |
115 | | netnsid_set(int *nsid, int id) |
116 | 0 | { |
117 | | /* The kernel only sends positive numbers for valid IDs. */ |
118 | 0 | if (id != NETNSID_LOCAL) { |
119 | 0 | ovs_assert(id >= 0); |
120 | 0 | } |
121 | |
|
122 | 0 | *nsid = id; |
123 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_set Unexecuted instantiation: netlink-socket.c:netnsid_set Unexecuted instantiation: dpif-netlink.c:netnsid_set |
124 | | |
125 | | static inline bool |
126 | | netnsid_eq(int nsid1, int nsid2) |
127 | 0 | { |
128 | 0 | if (netnsid_is_unset(nsid1) || netnsid_is_unset(nsid2)) { |
129 | 0 | return false; |
130 | 0 | } |
131 | | |
132 | 0 | if (nsid1 == nsid2) { |
133 | 0 | return true; |
134 | 0 | } |
135 | | |
136 | 0 | return false; |
137 | 0 | } Unexecuted instantiation: netdev-linux.c:netnsid_eq Unexecuted instantiation: netlink-socket.c:netnsid_eq Unexecuted instantiation: dpif-netlink.c:netnsid_eq |
138 | | |
139 | | #endif |