]>
Commit | Line | Data |
---|---|---|
a86bd14e FL |
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 | #define NETNSID_LOCAL NETNSA_NSID_NOT_ASSIGNED | |
65 | #else | |
66 | #define NETNSID_LOCAL -1 | |
67 | #endif | |
68 | #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 | { | |
83 | *nsid = NETNSID_LOCAL; | |
84 | } | |
85 | ||
86 | static inline bool | |
87 | netnsid_is_local(int nsid) | |
88 | { | |
89 | return nsid == NETNSID_LOCAL; | |
90 | } | |
91 | ||
92 | static inline void | |
93 | netnsid_unset(int *nsid) | |
94 | { | |
95 | *nsid = NETNSID_UNSET; | |
96 | } | |
97 | ||
98 | static inline bool | |
99 | netnsid_is_unset(int nsid) | |
100 | { | |
101 | return nsid == NETNSID_UNSET; | |
102 | } | |
103 | ||
104 | static inline bool | |
105 | netnsid_is_remote(int nsid) | |
106 | { | |
107 | if (netnsid_is_unset(nsid) || netnsid_is_local(nsid)) { | |
108 | return false; | |
109 | } | |
110 | ||
111 | return true; | |
112 | } | |
113 | ||
114 | static inline void | |
115 | netnsid_set(int *nsid, int id) | |
116 | { | |
117 | /* The kernel only sends positive numbers for valid IDs. */ | |
118 | if (id != NETNSID_LOCAL) { | |
119 | ovs_assert(id >= 0); | |
120 | } | |
121 | ||
122 | *nsid = id; | |
123 | } | |
124 | ||
125 | static inline bool | |
126 | netnsid_eq(int nsid1, int nsid2) | |
127 | { | |
128 | if (netnsid_is_unset(nsid1) || netnsid_is_unset(nsid2)) { | |
129 | return false; | |
130 | } | |
131 | ||
132 | if (nsid1 == nsid2) { | |
133 | return true; | |
134 | } | |
135 | ||
136 | return false; | |
137 | } | |
138 | ||
139 | #endif |