]>
Commit | Line | Data |
---|---|---|
96fba48f | 1 | /* |
4ea96698 | 2 | * Copyright (c) 2008-2018 Nicira, Inc. |
96fba48f BP |
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 | #include <config.h> | |
9fe3b9a2 | 18 | |
93451a0a | 19 | #include "dpif-netlink.h" |
96fba48f | 20 | |
96fba48f BP |
21 | #include <ctype.h> |
22 | #include <errno.h> | |
23 | #include <fcntl.h> | |
24 | #include <inttypes.h> | |
25 | #include <net/if.h> | |
b90fa799 | 26 | #include <linux/types.h> |
aae51f53 | 27 | #include <linux/pkt_sched.h> |
8522ba09 | 28 | #include <poll.h> |
96fba48f | 29 | #include <stdlib.h> |
8522ba09 | 30 | #include <strings.h> |
50f80534 | 31 | #include <sys/epoll.h> |
10dcf8de | 32 | #include <sys/stat.h> |
96fba48f BP |
33 | #include <unistd.h> |
34 | ||
773cd538 | 35 | #include "bitmap.h" |
c4e08753 | 36 | #include "dpif-netlink-rtnl.h" |
0d71302e | 37 | #include "dpif-provider.h" |
1579cf67 | 38 | #include "fat-rwlock.h" |
0d71302e | 39 | #include "flow.h" |
032aa6a3 | 40 | #include "netdev-linux.h" |
b6cabb8f | 41 | #include "netdev-offload.h" |
0d71302e | 42 | #include "netdev-provider.h" |
c3827f61 | 43 | #include "netdev-vport.h" |
0d71302e | 44 | #include "netdev.h" |
c11c9f4a | 45 | #include "netlink-conntrack.h" |
45c8d3a1 | 46 | #include "netlink-notifier.h" |
982b8810 | 47 | #include "netlink-socket.h" |
856081f6 | 48 | #include "netlink.h" |
bfda5239 | 49 | #include "netnsid.h" |
feebdea2 | 50 | #include "odp-util.h" |
0d71302e BP |
51 | #include "openvswitch/dynamic-string.h" |
52 | #include "openvswitch/flow.h" | |
1f161318 | 53 | #include "openvswitch/hmap.h" |
0d71302e | 54 | #include "openvswitch/match.h" |
64c96779 | 55 | #include "openvswitch/ofpbuf.h" |
fd016ae3 | 56 | #include "openvswitch/poll-loop.h" |
ee89ea7b | 57 | #include "openvswitch/shash.h" |
92d0d515 | 58 | #include "openvswitch/thread.h" |
0d71302e BP |
59 | #include "openvswitch/vlog.h" |
60 | #include "packets.h" | |
61 | #include "random.h" | |
b3c01ed3 | 62 | #include "sset.h" |
14b4d2f9 | 63 | #include "timeval.h" |
d6569377 | 64 | #include "unaligned.h" |
96fba48f | 65 | #include "util.h" |
5136ce49 | 66 | |
93451a0a | 67 | VLOG_DEFINE_THIS_MODULE(dpif_netlink); |
09cac43f | 68 | #ifdef _WIN32 |
da467899 | 69 | #include "wmi.h" |
09cac43f NR |
70 | enum { WINDOWS = 1 }; |
71 | #else | |
72 | enum { WINDOWS = 0 }; | |
73 | #endif | |
95b1d73a | 74 | enum { MAX_PORTS = USHRT_MAX }; |
773cd538 | 75 | |
24b019f8 JP |
76 | /* This ethtool flag was introduced in Linux 2.6.24, so it might be |
77 | * missing if we have old headers. */ | |
78 | #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */ | |
79 | ||
f2280b41 | 80 | #define FLOW_DUMP_MAX_BATCH 50 |
8b668ee3 | 81 | #define OPERATE_MAX_OPS 50 |
f2280b41 | 82 | |
69c51582 MC |
83 | #ifndef EPOLLEXCLUSIVE |
84 | #define EPOLLEXCLUSIVE (1u << 28) | |
85 | #endif | |
86 | ||
93451a0a | 87 | struct dpif_netlink_dp { |
aaff4b55 BP |
88 | /* Generic Netlink header. */ |
89 | uint8_t cmd; | |
d6569377 | 90 | |
df2c07f4 | 91 | /* struct ovs_header. */ |
254f2dc8 | 92 | int dp_ifindex; |
d6569377 BP |
93 | |
94 | /* Attributes. */ | |
df2c07f4 | 95 | const char *name; /* OVS_DP_ATTR_NAME. */ |
fcd5d230 | 96 | const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */ |
b7fd5e38 | 97 | uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */ |
6a54dedc BP |
98 | const struct ovs_dp_stats *stats; /* OVS_DP_ATTR_STATS. */ |
99 | const struct ovs_dp_megaflow_stats *megaflow_stats; | |
847108dc | 100 | /* OVS_DP_ATTR_MEGAFLOW_STATS.*/ |
d6569377 BP |
101 | }; |
102 | ||
93451a0a AS |
103 | static void dpif_netlink_dp_init(struct dpif_netlink_dp *); |
104 | static int dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *, | |
105 | const struct ofpbuf *); | |
106 | static void dpif_netlink_dp_dump_start(struct nl_dump *); | |
107 | static int dpif_netlink_dp_transact(const struct dpif_netlink_dp *request, | |
108 | struct dpif_netlink_dp *reply, | |
109 | struct ofpbuf **bufp); | |
110 | static int dpif_netlink_dp_get(const struct dpif *, | |
111 | struct dpif_netlink_dp *reply, | |
112 | struct ofpbuf **bufp); | |
b2ae4069 PB |
113 | static int |
114 | dpif_netlink_set_features(struct dpif *dpif_, uint32_t new_features); | |
93451a0a AS |
115 | |
116 | struct dpif_netlink_flow { | |
37a1300c BP |
117 | /* Generic Netlink header. */ |
118 | uint8_t cmd; | |
d6569377 | 119 | |
df2c07f4 | 120 | /* struct ovs_header. */ |
d6569377 | 121 | unsigned int nlmsg_flags; |
254f2dc8 | 122 | int dp_ifindex; |
d6569377 BP |
123 | |
124 | /* Attributes. | |
125 | * | |
0e70cdcb BP |
126 | * The 'stats' member points to 64-bit data that might only be aligned on |
127 | * 32-bit boundaries, so get_unaligned_u64() should be used to access its | |
128 | * values. | |
d2a23af2 | 129 | * |
df2c07f4 | 130 | * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in |
d2a23af2 | 131 | * the Netlink version of the command, even if actions_len is zero. */ |
df2c07f4 | 132 | const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */ |
d6569377 | 133 | size_t key_len; |
e6cc0bab AZ |
134 | const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */ |
135 | size_t mask_len; | |
df2c07f4 | 136 | const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */ |
d6569377 | 137 | size_t actions_len; |
70e5ed6f JS |
138 | ovs_u128 ufid; /* OVS_FLOW_ATTR_FLOW_ID. */ |
139 | bool ufid_present; /* Is there a UFID? */ | |
140 | bool ufid_terse; /* Skip serializing key/mask/acts? */ | |
df2c07f4 JP |
141 | const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */ |
142 | const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */ | |
0e70cdcb | 143 | const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */ |
df2c07f4 | 144 | bool clear; /* OVS_FLOW_ATTR_CLEAR. */ |
43f9ac0a | 145 | bool probe; /* OVS_FLOW_ATTR_PROBE. */ |
d6569377 BP |
146 | }; |
147 | ||
93451a0a AS |
148 | static void dpif_netlink_flow_init(struct dpif_netlink_flow *); |
149 | static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *, | |
150 | const struct ofpbuf *); | |
151 | static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *, | |
152 | struct ofpbuf *); | |
153 | static int dpif_netlink_flow_transact(struct dpif_netlink_flow *request, | |
154 | struct dpif_netlink_flow *reply, | |
155 | struct ofpbuf **bufp); | |
156 | static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *, | |
157 | struct dpif_flow_stats *); | |
7af12bd7 | 158 | static void dpif_netlink_flow_to_dpif_flow(struct dpif *, struct dpif_flow *, |
93451a0a | 159 | const struct dpif_netlink_flow *); |
d6569377 | 160 | |
989fd548 | 161 | /* One of the dpif channels between the kernel and userspace. */ |
fe3d61b3 | 162 | struct dpif_channel { |
14b4d2f9 | 163 | struct nl_sock *sock; /* Netlink socket. */ |
14b4d2f9 | 164 | long long int last_poll; /* Last time this channel was polled. */ |
fe3d61b3 BP |
165 | }; |
166 | ||
09cac43f NR |
167 | #ifdef _WIN32 |
168 | #define VPORT_SOCK_POOL_SIZE 1 | |
169 | /* On Windows, there is no native support for epoll. There are equivalent | |
170 | * interfaces though, that are not used currently. For simpicity, a pool of | |
171 | * netlink sockets is used. Each socket is represented by 'struct | |
172 | * dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be | |
173 | * sharing the same socket. In the future, we can add a reference count and | |
174 | * such fields. */ | |
175 | struct dpif_windows_vport_sock { | |
176 | struct nl_sock *nl_sock; /* netlink socket. */ | |
177 | }; | |
178 | #endif | |
179 | ||
1579cf67 | 180 | struct dpif_handler { |
1579cf67 AW |
181 | struct epoll_event *epoll_events; |
182 | int epoll_fd; /* epoll fd that includes channel socks. */ | |
183 | int n_events; /* Num events returned by epoll_wait(). */ | |
184 | int event_offset; /* Offset into 'epoll_events'. */ | |
09cac43f NR |
185 | |
186 | #ifdef _WIN32 | |
187 | /* Pool of sockets. */ | |
188 | struct dpif_windows_vport_sock *vport_sock_pool; | |
189 | size_t last_used_pool_idx; /* Index to aid in allocating a | |
190 | socket in the pool to a port. */ | |
191 | #endif | |
1579cf67 | 192 | }; |
14b4d2f9 | 193 | |
96fba48f | 194 | /* Datapath interface for the openvswitch Linux kernel module. */ |
93451a0a | 195 | struct dpif_netlink { |
96fba48f | 196 | struct dpif dpif; |
254f2dc8 | 197 | int dp_ifindex; |
dcdcad68 | 198 | uint32_t user_features; |
e9e28be3 | 199 | |
b063d9f0 | 200 | /* Upcall messages. */ |
1579cf67 AW |
201 | struct fat_rwlock upcall_lock; |
202 | struct dpif_handler *handlers; | |
203 | uint32_t n_handlers; /* Num of upcall handlers. */ | |
69c51582 | 204 | struct dpif_channel *channels; /* Array of channels for each port. */ |
1579cf67 AW |
205 | int uc_array_size; /* Size of 'handler->channels' and */ |
206 | /* 'handler->epoll_events'. */ | |
982b8810 | 207 | |
e9e28be3 | 208 | /* Change notification. */ |
e4516b20 | 209 | struct nl_sock *port_notifier; /* vport multicast group subscriber. */ |
61eae437 | 210 | bool refresh_channels; |
96fba48f BP |
211 | }; |
212 | ||
93451a0a | 213 | static void report_loss(struct dpif_netlink *, struct dpif_channel *, |
9b00386b | 214 | uint32_t ch_idx, uint32_t handler_id); |
1579cf67 | 215 | |
96fba48f BP |
216 | static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5); |
217 | ||
e4516b20 BP |
218 | /* Generic Netlink family numbers for OVS. |
219 | * | |
93451a0a | 220 | * Initialized by dpif_netlink_init(). */ |
df2c07f4 JP |
221 | static int ovs_datapath_family; |
222 | static int ovs_vport_family; | |
223 | static int ovs_flow_family; | |
224 | static int ovs_packet_family; | |
80738e5f | 225 | static int ovs_meter_family; |
906ff9d2 | 226 | static int ovs_ct_limit_family; |
982b8810 | 227 | |
e4516b20 BP |
228 | /* Generic Netlink multicast groups for OVS. |
229 | * | |
93451a0a | 230 | * Initialized by dpif_netlink_init(). */ |
e4516b20 | 231 | static unsigned int ovs_vport_mcgroup; |
982b8810 | 232 | |
921c370a EG |
233 | /* If true, tunnel devices are created using OVS compat/genetlink. |
234 | * If false, tunnel devices are created with rtnetlink and using light weight | |
235 | * tunnels. If we fail to create the tunnel the rtnetlink+LWT, then we fallback | |
236 | * to using the compat interface. */ | |
237 | static bool ovs_tunnels_out_of_tree = true; | |
238 | ||
93451a0a AS |
239 | static int dpif_netlink_init(void); |
240 | static int open_dpif(const struct dpif_netlink_dp *, struct dpif **); | |
241 | static uint32_t dpif_netlink_port_get_pid(const struct dpif *, | |
769b5034 | 242 | odp_port_t port_no); |
09cac43f | 243 | static void dpif_netlink_handler_uninit(struct dpif_handler *handler); |
93451a0a AS |
244 | static int dpif_netlink_refresh_channels(struct dpif_netlink *, |
245 | uint32_t n_handlers); | |
246 | static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *, | |
247 | struct ofpbuf *); | |
248 | static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *, | |
249 | const struct ofpbuf *); | |
921c370a EG |
250 | static int dpif_netlink_port_query__(const struct dpif_netlink *dpif, |
251 | odp_port_t port_no, const char *port_name, | |
252 | struct dpif_port *dpif_port); | |
f0fef760 | 253 | |
d240e46a | 254 | static int |
622ea8fd | 255 | create_nl_sock(struct dpif_netlink *dpif OVS_UNUSED, struct nl_sock **sockp) |
d240e46a AGS |
256 | OVS_REQ_WRLOCK(dpif->upcall_lock) |
257 | { | |
258 | #ifndef _WIN32 | |
622ea8fd | 259 | return nl_sock_create(NETLINK_GENERIC, sockp); |
d240e46a AGS |
260 | #else |
261 | /* Pick netlink sockets to use in a round-robin fashion from each | |
262 | * handler's pool of sockets. */ | |
263 | struct dpif_handler *handler = &dpif->handlers[0]; | |
264 | struct dpif_windows_vport_sock *sock_pool = handler->vport_sock_pool; | |
265 | size_t index = handler->last_used_pool_idx; | |
266 | ||
267 | /* A pool of sockets is allocated when the handler is initialized. */ | |
268 | if (sock_pool == NULL) { | |
622ea8fd | 269 | *sockp = NULL; |
d240e46a AGS |
270 | return EINVAL; |
271 | } | |
272 | ||
273 | ovs_assert(index < VPORT_SOCK_POOL_SIZE); | |
622ea8fd BP |
274 | *sockp = sock_pool[index].nl_sock; |
275 | ovs_assert(*sockp); | |
d240e46a AGS |
276 | index = (index == VPORT_SOCK_POOL_SIZE - 1) ? 0 : index + 1; |
277 | handler->last_used_pool_idx = index; | |
278 | return 0; | |
279 | #endif | |
280 | } | |
281 | ||
282 | static void | |
622ea8fd | 283 | close_nl_sock(struct nl_sock *sock) |
d240e46a AGS |
284 | { |
285 | #ifndef _WIN32 | |
622ea8fd | 286 | nl_sock_destroy(sock); |
d240e46a AGS |
287 | #endif |
288 | } | |
289 | ||
93451a0a AS |
290 | static struct dpif_netlink * |
291 | dpif_netlink_cast(const struct dpif *dpif) | |
96fba48f | 292 | { |
93451a0a AS |
293 | dpif_assert_class(dpif, &dpif_netlink_class); |
294 | return CONTAINER_OF(dpif, struct dpif_netlink, dpif); | |
96fba48f BP |
295 | } |
296 | ||
d3d22744 | 297 | static int |
93451a0a AS |
298 | dpif_netlink_enumerate(struct sset *all_dps, |
299 | const struct dpif_class *dpif_class OVS_UNUSED) | |
d3d22744 | 300 | { |
aaff4b55 | 301 | struct nl_dump dump; |
d57695d7 JS |
302 | uint64_t reply_stub[NL_DUMP_BUFSIZE / 8]; |
303 | struct ofpbuf msg, buf; | |
aaff4b55 | 304 | int error; |
982b8810 | 305 | |
93451a0a | 306 | error = dpif_netlink_init(); |
aaff4b55 BP |
307 | if (error) { |
308 | return error; | |
982b8810 | 309 | } |
d3d22744 | 310 | |
d57695d7 | 311 | ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub); |
93451a0a | 312 | dpif_netlink_dp_dump_start(&dump); |
d57695d7 | 313 | while (nl_dump_next(&dump, &msg, &buf)) { |
93451a0a | 314 | struct dpif_netlink_dp dp; |
d6569377 | 315 | |
93451a0a | 316 | if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) { |
d0c23a1a | 317 | sset_add(all_dps, dp.name); |
d3d22744 BP |
318 | } |
319 | } | |
d57695d7 | 320 | ofpbuf_uninit(&buf); |
aaff4b55 | 321 | return nl_dump_done(&dump); |
d3d22744 BP |
322 | } |
323 | ||
96fba48f | 324 | static int |
93451a0a AS |
325 | dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name, |
326 | bool create, struct dpif **dpifp) | |
96fba48f | 327 | { |
93451a0a | 328 | struct dpif_netlink_dp dp_request, dp; |
c19e6535 | 329 | struct ofpbuf *buf; |
ea36840f | 330 | uint32_t upcall_pid; |
c19e6535 | 331 | int error; |
96fba48f | 332 | |
93451a0a | 333 | error = dpif_netlink_init(); |
982b8810 BP |
334 | if (error) { |
335 | return error; | |
336 | } | |
337 | ||
982b8810 | 338 | /* Create or look up datapath. */ |
93451a0a | 339 | dpif_netlink_dp_init(&dp_request); |
dcdcad68 PB |
340 | upcall_pid = 0; |
341 | dp_request.upcall_pid = &upcall_pid; | |
342 | dp_request.name = name; | |
343 | ||
ea36840f BP |
344 | if (create) { |
345 | dp_request.cmd = OVS_DP_CMD_NEW; | |
ea36840f | 346 | } else { |
dcdcad68 PB |
347 | dp_request.cmd = OVS_DP_CMD_GET; |
348 | ||
349 | error = dpif_netlink_dp_transact(&dp_request, &dp, &buf); | |
350 | if (error) { | |
351 | return error; | |
352 | } | |
353 | dp_request.user_features = dp.user_features; | |
354 | ofpbuf_delete(buf); | |
355 | ||
b7fd5e38 TG |
356 | /* Use OVS_DP_CMD_SET to report user features */ |
357 | dp_request.cmd = OVS_DP_CMD_SET; | |
ea36840f | 358 | } |
dcdcad68 | 359 | |
b7fd5e38 | 360 | dp_request.user_features |= OVS_DP_F_UNALIGNED; |
1579cf67 | 361 | dp_request.user_features |= OVS_DP_F_VPORT_PIDS; |
93451a0a | 362 | error = dpif_netlink_dp_transact(&dp_request, &dp, &buf); |
982b8810 BP |
363 | if (error) { |
364 | return error; | |
c19e6535 | 365 | } |
254f2dc8 | 366 | |
e4516b20 | 367 | error = open_dpif(&dp, dpifp); |
b2ae4069 | 368 | dpif_netlink_set_features(*dpifp, OVS_DP_F_TC_RECIRC_SHARING); |
8f4a4df5 | 369 | ofpbuf_delete(buf); |
b2ae4069 | 370 | |
e4516b20 | 371 | return error; |
c19e6535 BP |
372 | } |
373 | ||
e4516b20 | 374 | static int |
93451a0a | 375 | open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp) |
c19e6535 | 376 | { |
93451a0a | 377 | struct dpif_netlink *dpif; |
c19e6535 | 378 | |
17411ecf | 379 | dpif = xzalloc(sizeof *dpif); |
e4516b20 | 380 | dpif->port_notifier = NULL; |
1579cf67 | 381 | fat_rwlock_init(&dpif->upcall_lock); |
c19e6535 | 382 | |
93451a0a | 383 | dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name, |
254f2dc8 | 384 | dp->dp_ifindex, dp->dp_ifindex); |
c19e6535 | 385 | |
254f2dc8 | 386 | dpif->dp_ifindex = dp->dp_ifindex; |
dcdcad68 | 387 | dpif->user_features = dp->user_features; |
c19e6535 | 388 | *dpifp = &dpif->dpif; |
e4516b20 BP |
389 | |
390 | return 0; | |
96fba48f BP |
391 | } |
392 | ||
09cac43f NR |
393 | #ifdef _WIN32 |
394 | static void | |
395 | vport_delete_sock_pool(struct dpif_handler *handler) | |
396 | OVS_REQ_WRLOCK(dpif->upcall_lock) | |
397 | { | |
398 | if (handler->vport_sock_pool) { | |
399 | uint32_t i; | |
400 | struct dpif_windows_vport_sock *sock_pool = | |
401 | handler->vport_sock_pool; | |
402 | ||
403 | for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) { | |
404 | if (sock_pool[i].nl_sock) { | |
405 | nl_sock_unsubscribe_packets(sock_pool[i].nl_sock); | |
406 | nl_sock_destroy(sock_pool[i].nl_sock); | |
407 | sock_pool[i].nl_sock = NULL; | |
408 | } | |
409 | } | |
410 | ||
411 | free(handler->vport_sock_pool); | |
412 | handler->vport_sock_pool = NULL; | |
413 | } | |
414 | } | |
415 | ||
416 | static int | |
417 | vport_create_sock_pool(struct dpif_handler *handler) | |
418 | OVS_REQ_WRLOCK(dpif->upcall_lock) | |
419 | { | |
420 | struct dpif_windows_vport_sock *sock_pool; | |
421 | size_t i; | |
422 | int error = 0; | |
423 | ||
424 | sock_pool = xzalloc(VPORT_SOCK_POOL_SIZE * sizeof *sock_pool); | |
425 | for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) { | |
426 | error = nl_sock_create(NETLINK_GENERIC, &sock_pool[i].nl_sock); | |
427 | if (error) { | |
428 | goto error; | |
429 | } | |
430 | ||
431 | /* Enable the netlink socket to receive packets. This is equivalent to | |
432 | * calling nl_sock_join_mcgroup() to receive events. */ | |
433 | error = nl_sock_subscribe_packets(sock_pool[i].nl_sock); | |
434 | if (error) { | |
435 | goto error; | |
436 | } | |
437 | } | |
438 | ||
439 | handler->vport_sock_pool = sock_pool; | |
440 | handler->last_used_pool_idx = 0; | |
441 | return 0; | |
442 | ||
443 | error: | |
444 | vport_delete_sock_pool(handler); | |
445 | return error; | |
446 | } | |
09cac43f NR |
447 | #endif /* _WIN32 */ |
448 | ||
69c51582 MC |
449 | /* Given the port number 'port_idx', extracts the pid of netlink socket |
450 | * associated to the port and assigns it to 'upcall_pid'. */ | |
1579cf67 | 451 | static bool |
69c51582 MC |
452 | vport_get_pid(struct dpif_netlink *dpif, uint32_t port_idx, |
453 | uint32_t *upcall_pid) | |
1579cf67 | 454 | { |
1579cf67 | 455 | /* Since the nl_sock can only be assigned in either all |
69c51582 | 456 | * or none "dpif" channels, the following check |
1579cf67 | 457 | * would suffice. */ |
69c51582 | 458 | if (!dpif->channels[port_idx].sock) { |
1579cf67 AW |
459 | return false; |
460 | } | |
09cac43f | 461 | ovs_assert(!WINDOWS || dpif->n_handlers <= 1); |
1579cf67 | 462 | |
69c51582 | 463 | *upcall_pid = nl_sock_pid(dpif->channels[port_idx].sock); |
989fd548 | 464 | |
1579cf67 | 465 | return true; |
989fd548 JP |
466 | } |
467 | ||
468 | static int | |
69c51582 | 469 | vport_add_channel(struct dpif_netlink *dpif, odp_port_t port_no, |
622ea8fd | 470 | struct nl_sock *sock) |
989fd548 JP |
471 | { |
472 | struct epoll_event event; | |
4e022ec0 | 473 | uint32_t port_idx = odp_to_u32(port_no); |
69c51582 | 474 | size_t i; |
1579cf67 | 475 | int error; |
989fd548 | 476 | |
1579cf67 | 477 | if (dpif->handlers == NULL) { |
622ea8fd | 478 | close_nl_sock(sock); |
989fd548 JP |
479 | return 0; |
480 | } | |
481 | ||
1579cf67 AW |
482 | /* We assume that the datapath densely chooses port numbers, which can |
483 | * therefore be used as an index into 'channels' and 'epoll_events' of | |
69c51582 | 484 | * 'dpif'. */ |
4e022ec0 AW |
485 | if (port_idx >= dpif->uc_array_size) { |
486 | uint32_t new_size = port_idx + 1; | |
989fd548 | 487 | |
12d76859 | 488 | if (new_size > MAX_PORTS) { |
989fd548 JP |
489 | VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big", |
490 | dpif_name(&dpif->dpif), port_no); | |
491 | return EFBIG; | |
492 | } | |
493 | ||
69c51582 MC |
494 | dpif->channels = xrealloc(dpif->channels, |
495 | new_size * sizeof *dpif->channels); | |
1579cf67 | 496 | |
69c51582 MC |
497 | for (i = dpif->uc_array_size; i < new_size; i++) { |
498 | dpif->channels[i].sock = NULL; | |
499 | } | |
1579cf67 | 500 | |
69c51582 MC |
501 | for (i = 0; i < dpif->n_handlers; i++) { |
502 | struct dpif_handler *handler = &dpif->handlers[i]; | |
1579cf67 AW |
503 | |
504 | handler->epoll_events = xrealloc(handler->epoll_events, | |
505 | new_size * sizeof *handler->epoll_events); | |
989fd548 | 506 | |
1579cf67 | 507 | } |
989fd548 JP |
508 | dpif->uc_array_size = new_size; |
509 | } | |
510 | ||
511 | memset(&event, 0, sizeof event); | |
69c51582 | 512 | event.events = EPOLLIN | EPOLLEXCLUSIVE; |
4e022ec0 | 513 | event.data.u32 = port_idx; |
989fd548 | 514 | |
1579cf67 AW |
515 | for (i = 0; i < dpif->n_handlers; i++) { |
516 | struct dpif_handler *handler = &dpif->handlers[i]; | |
517 | ||
09cac43f | 518 | #ifndef _WIN32 |
622ea8fd | 519 | if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock), |
1579cf67 AW |
520 | &event) < 0) { |
521 | error = errno; | |
522 | goto error; | |
523 | } | |
93451a0a | 524 | #endif |
1579cf67 | 525 | } |
622ea8fd | 526 | dpif->channels[port_idx].sock = sock; |
69c51582 | 527 | dpif->channels[port_idx].last_poll = LLONG_MIN; |
989fd548 JP |
528 | |
529 | return 0; | |
1579cf67 AW |
530 | |
531 | error: | |
09cac43f | 532 | #ifndef _WIN32 |
69c51582 MC |
533 | while (i--) { |
534 | epoll_ctl(dpif->handlers[i].epoll_fd, EPOLL_CTL_DEL, | |
622ea8fd | 535 | nl_sock_fd(sock), NULL); |
1579cf67 | 536 | } |
69c51582 MC |
537 | #endif |
538 | dpif->channels[port_idx].sock = NULL; | |
1579cf67 AW |
539 | |
540 | return error; | |
989fd548 JP |
541 | } |
542 | ||
543 | static void | |
93451a0a | 544 | vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no) |
989fd548 | 545 | { |
4e022ec0 | 546 | uint32_t port_idx = odp_to_u32(port_no); |
1579cf67 | 547 | size_t i; |
989fd548 | 548 | |
69c51582 MC |
549 | if (!dpif->handlers || port_idx >= dpif->uc_array_size |
550 | || !dpif->channels[port_idx].sock) { | |
989fd548 JP |
551 | return; |
552 | } | |
553 | ||
1579cf67 AW |
554 | for (i = 0; i < dpif->n_handlers; i++) { |
555 | struct dpif_handler *handler = &dpif->handlers[i]; | |
09cac43f | 556 | #ifndef _WIN32 |
1579cf67 | 557 | epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL, |
69c51582 | 558 | nl_sock_fd(dpif->channels[port_idx].sock), NULL); |
09cac43f | 559 | #endif |
1579cf67 AW |
560 | handler->event_offset = handler->n_events = 0; |
561 | } | |
69c51582 MC |
562 | #ifndef _WIN32 |
563 | nl_sock_destroy(dpif->channels[port_idx].sock); | |
564 | #endif | |
565 | dpif->channels[port_idx].sock = NULL; | |
1579cf67 AW |
566 | } |
567 | ||
568 | static void | |
93451a0a AS |
569 | destroy_all_channels(struct dpif_netlink *dpif) |
570 | OVS_REQ_WRLOCK(dpif->upcall_lock) | |
1579cf67 AW |
571 | { |
572 | unsigned int i; | |
573 | ||
574 | if (!dpif->handlers) { | |
575 | return; | |
576 | } | |
577 | ||
578 | for (i = 0; i < dpif->uc_array_size; i++ ) { | |
93451a0a | 579 | struct dpif_netlink_vport vport_request; |
1579cf67 AW |
580 | uint32_t upcall_pids = 0; |
581 | ||
69c51582 | 582 | if (!dpif->channels[i].sock) { |
1579cf67 AW |
583 | continue; |
584 | } | |
585 | ||
586 | /* Turn off upcalls. */ | |
93451a0a | 587 | dpif_netlink_vport_init(&vport_request); |
1579cf67 AW |
588 | vport_request.cmd = OVS_VPORT_CMD_SET; |
589 | vport_request.dp_ifindex = dpif->dp_ifindex; | |
590 | vport_request.port_no = u32_to_odp(i); | |
a78f446a | 591 | vport_request.n_upcall_pids = 1; |
1579cf67 | 592 | vport_request.upcall_pids = &upcall_pids; |
93451a0a | 593 | dpif_netlink_vport_transact(&vport_request, NULL, NULL); |
1579cf67 AW |
594 | |
595 | vport_del_channels(dpif, u32_to_odp(i)); | |
596 | } | |
597 | ||
598 | for (i = 0; i < dpif->n_handlers; i++) { | |
599 | struct dpif_handler *handler = &dpif->handlers[i]; | |
600 | ||
09cac43f | 601 | dpif_netlink_handler_uninit(handler); |
1579cf67 | 602 | free(handler->epoll_events); |
1579cf67 | 603 | } |
69c51582 | 604 | free(dpif->channels); |
1579cf67 AW |
605 | free(dpif->handlers); |
606 | dpif->handlers = NULL; | |
69c51582 | 607 | dpif->channels = NULL; |
1579cf67 AW |
608 | dpif->n_handlers = 0; |
609 | dpif->uc_array_size = 0; | |
17411ecf JG |
610 | } |
611 | ||
96fba48f | 612 | static void |
93451a0a | 613 | dpif_netlink_close(struct dpif *dpif_) |
96fba48f | 614 | { |
93451a0a | 615 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
c7178a0b | 616 | |
e4516b20 | 617 | nl_sock_destroy(dpif->port_notifier); |
1579cf67 AW |
618 | |
619 | fat_rwlock_wrlock(&dpif->upcall_lock); | |
620 | destroy_all_channels(dpif); | |
621 | fat_rwlock_unlock(&dpif->upcall_lock); | |
622 | ||
623 | fat_rwlock_destroy(&dpif->upcall_lock); | |
96fba48f BP |
624 | free(dpif); |
625 | } | |
626 | ||
627 | static int | |
93451a0a | 628 | dpif_netlink_destroy(struct dpif *dpif_) |
96fba48f | 629 | { |
93451a0a AS |
630 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
631 | struct dpif_netlink_dp dp; | |
d6569377 | 632 | |
93451a0a | 633 | dpif_netlink_dp_init(&dp); |
df2c07f4 | 634 | dp.cmd = OVS_DP_CMD_DEL; |
254f2dc8 | 635 | dp.dp_ifindex = dpif->dp_ifindex; |
93451a0a | 636 | return dpif_netlink_dp_transact(&dp, NULL, NULL); |
96fba48f BP |
637 | } |
638 | ||
a36de779 | 639 | static bool |
93451a0a | 640 | dpif_netlink_run(struct dpif *dpif_) |
61eae437 | 641 | { |
93451a0a | 642 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
1579cf67 | 643 | |
61eae437 BP |
644 | if (dpif->refresh_channels) { |
645 | dpif->refresh_channels = false; | |
1579cf67 | 646 | fat_rwlock_wrlock(&dpif->upcall_lock); |
93451a0a | 647 | dpif_netlink_refresh_channels(dpif, dpif->n_handlers); |
1579cf67 | 648 | fat_rwlock_unlock(&dpif->upcall_lock); |
61eae437 | 649 | } |
a36de779 | 650 | return false; |
61eae437 BP |
651 | } |
652 | ||
96fba48f | 653 | static int |
93451a0a | 654 | dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats) |
96fba48f | 655 | { |
93451a0a | 656 | struct dpif_netlink_dp dp; |
d6569377 BP |
657 | struct ofpbuf *buf; |
658 | int error; | |
659 | ||
93451a0a | 660 | error = dpif_netlink_dp_get(dpif_, &dp, &buf); |
d6569377 | 661 | if (!error) { |
6a54dedc BP |
662 | memset(stats, 0, sizeof *stats); |
663 | ||
664 | if (dp.stats) { | |
665 | stats->n_hit = get_32aligned_u64(&dp.stats->n_hit); | |
666 | stats->n_missed = get_32aligned_u64(&dp.stats->n_missed); | |
667 | stats->n_lost = get_32aligned_u64(&dp.stats->n_lost); | |
668 | stats->n_flows = get_32aligned_u64(&dp.stats->n_flows); | |
669 | } | |
670 | ||
671 | if (dp.megaflow_stats) { | |
672 | stats->n_masks = dp.megaflow_stats->n_masks; | |
673 | stats->n_mask_hit = get_32aligned_u64( | |
674 | &dp.megaflow_stats->n_mask_hit); | |
675 | } else { | |
676 | stats->n_masks = UINT32_MAX; | |
677 | stats->n_mask_hit = UINT64_MAX; | |
678 | } | |
d6569377 BP |
679 | ofpbuf_delete(buf); |
680 | } | |
681 | return error; | |
96fba48f BP |
682 | } |
683 | ||
dcdcad68 PB |
684 | static int |
685 | dpif_netlink_set_features(struct dpif *dpif_, uint32_t new_features) | |
686 | { | |
687 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); | |
688 | struct dpif_netlink_dp request, reply; | |
689 | struct ofpbuf *bufp; | |
690 | int error; | |
691 | ||
692 | dpif_netlink_dp_init(&request); | |
693 | request.cmd = OVS_DP_CMD_SET; | |
694 | request.dp_ifindex = dpif->dp_ifindex; | |
695 | request.user_features = dpif->user_features | new_features; | |
696 | ||
697 | error = dpif_netlink_dp_transact(&request, &reply, &bufp); | |
698 | if (!error) { | |
699 | dpif->user_features = reply.user_features; | |
700 | ofpbuf_delete(bufp); | |
701 | if (!(dpif->user_features & new_features)) { | |
702 | return -EOPNOTSUPP; | |
703 | } | |
704 | } | |
705 | ||
706 | return error; | |
707 | } | |
708 | ||
b9ad7294 | 709 | static const char * |
93451a0a | 710 | get_vport_type(const struct dpif_netlink_vport *vport) |
b9ad7294 EJ |
711 | { |
712 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
713 | ||
714 | switch (vport->type) { | |
5ed51209 JS |
715 | case OVS_VPORT_TYPE_NETDEV: { |
716 | const char *type = netdev_get_type_from_name(vport->name); | |
717 | ||
718 | return type ? type : "system"; | |
719 | } | |
b9ad7294 EJ |
720 | |
721 | case OVS_VPORT_TYPE_INTERNAL: | |
722 | return "internal"; | |
723 | ||
c1fc1411 JG |
724 | case OVS_VPORT_TYPE_GENEVE: |
725 | return "geneve"; | |
726 | ||
b9ad7294 EJ |
727 | case OVS_VPORT_TYPE_GRE: |
728 | return "gre"; | |
729 | ||
b9ad7294 EJ |
730 | case OVS_VPORT_TYPE_VXLAN: |
731 | return "vxlan"; | |
732 | ||
a6ae068b LJ |
733 | case OVS_VPORT_TYPE_LISP: |
734 | return "lisp"; | |
735 | ||
4237026e PS |
736 | case OVS_VPORT_TYPE_STT: |
737 | return "stt"; | |
738 | ||
c387d817 | 739 | case OVS_VPORT_TYPE_ERSPAN: |
98514eea WT |
740 | return "erspan"; |
741 | ||
c387d817 | 742 | case OVS_VPORT_TYPE_IP6ERSPAN: |
3b10ceee GR |
743 | return "ip6erspan"; |
744 | ||
c387d817 | 745 | case OVS_VPORT_TYPE_IP6GRE: |
3b10ceee | 746 | return "ip6gre"; |
c387d817 | 747 | |
b9ad7294 EJ |
748 | case OVS_VPORT_TYPE_UNSPEC: |
749 | case __OVS_VPORT_TYPE_MAX: | |
750 | break; | |
751 | } | |
752 | ||
753 | VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u", | |
754 | vport->dp_ifindex, vport->name, (unsigned int) vport->type); | |
755 | return "unknown"; | |
756 | } | |
757 | ||
c4e08753 | 758 | enum ovs_vport_type |
20c57607 | 759 | netdev_to_ovs_vport_type(const char *type) |
c060c4cf | 760 | { |
c060c4cf EJ |
761 | if (!strcmp(type, "tap") || !strcmp(type, "system")) { |
762 | return OVS_VPORT_TYPE_NETDEV; | |
763 | } else if (!strcmp(type, "internal")) { | |
764 | return OVS_VPORT_TYPE_INTERNAL; | |
4237026e PS |
765 | } else if (strstr(type, "stt")) { |
766 | return OVS_VPORT_TYPE_STT; | |
c1fc1411 JG |
767 | } else if (!strcmp(type, "geneve")) { |
768 | return OVS_VPORT_TYPE_GENEVE; | |
c060c4cf EJ |
769 | } else if (!strcmp(type, "vxlan")) { |
770 | return OVS_VPORT_TYPE_VXLAN; | |
a6ae068b LJ |
771 | } else if (!strcmp(type, "lisp")) { |
772 | return OVS_VPORT_TYPE_LISP; | |
7dc18ae9 WT |
773 | } else if (!strcmp(type, "erspan")) { |
774 | return OVS_VPORT_TYPE_ERSPAN; | |
775 | } else if (!strcmp(type, "ip6erspan")) { | |
776 | return OVS_VPORT_TYPE_IP6ERSPAN; | |
3b10ceee GR |
777 | } else if (!strcmp(type, "ip6gre")) { |
778 | return OVS_VPORT_TYPE_IP6GRE; | |
1c385f49 GR |
779 | } else if (!strcmp(type, "gre")) { |
780 | return OVS_VPORT_TYPE_GRE; | |
c060c4cf EJ |
781 | } else { |
782 | return OVS_VPORT_TYPE_UNSPEC; | |
783 | } | |
784 | } | |
785 | ||
96fba48f | 786 | static int |
20c57607 EG |
787 | dpif_netlink_port_add__(struct dpif_netlink *dpif, const char *name, |
788 | enum ovs_vport_type type, | |
789 | struct ofpbuf *options, | |
93451a0a | 790 | odp_port_t *port_nop) |
b90de034 | 791 | OVS_REQ_WRLOCK(dpif->upcall_lock) |
96fba48f | 792 | { |
93451a0a | 793 | struct dpif_netlink_vport request, reply; |
c19e6535 | 794 | struct ofpbuf *buf; |
622ea8fd | 795 | struct nl_sock *sock = NULL; |
790a4372 | 796 | uint32_t upcall_pids = 0; |
1579cf67 | 797 | int error = 0; |
96fba48f | 798 | |
1579cf67 | 799 | if (dpif->handlers) { |
622ea8fd | 800 | error = create_nl_sock(dpif, &sock); |
713a45db | 801 | if (error) { |
989fd548 JP |
802 | return error; |
803 | } | |
804 | } | |
805 | ||
93451a0a | 806 | dpif_netlink_vport_init(&request); |
df2c07f4 | 807 | request.cmd = OVS_VPORT_CMD_NEW; |
254f2dc8 | 808 | request.dp_ifindex = dpif->dp_ifindex; |
20c57607 EG |
809 | request.type = type; |
810 | request.name = name; | |
811 | ||
812 | request.port_no = *port_nop; | |
622ea8fd BP |
813 | if (sock) { |
814 | upcall_pids = nl_sock_pid(sock); | |
790a4372 | 815 | } |
69c51582 MC |
816 | request.n_upcall_pids = 1; |
817 | request.upcall_pids = &upcall_pids; | |
20c57607 EG |
818 | |
819 | if (options) { | |
820 | request.options = options->data; | |
821 | request.options_len = options->size; | |
822 | } | |
823 | ||
824 | error = dpif_netlink_vport_transact(&request, &reply, &buf); | |
825 | if (!error) { | |
826 | *port_nop = reply.port_no; | |
827 | } else { | |
828 | if (error == EBUSY && *port_nop != ODPP_NONE) { | |
829 | VLOG_INFO("%s: requested port %"PRIu32" is in use", | |
830 | dpif_name(&dpif->dpif), *port_nop); | |
831 | } | |
832 | ||
622ea8fd | 833 | close_nl_sock(sock); |
20c57607 EG |
834 | goto exit; |
835 | } | |
836 | ||
622ea8fd | 837 | error = vport_add_channel(dpif, *port_nop, sock); |
69c51582 MC |
838 | if (error) { |
839 | VLOG_INFO("%s: could not add channel for port %s", | |
840 | dpif_name(&dpif->dpif), name); | |
841 | ||
842 | /* Delete the port. */ | |
843 | dpif_netlink_vport_init(&request); | |
844 | request.cmd = OVS_VPORT_CMD_DEL; | |
845 | request.dp_ifindex = dpif->dp_ifindex; | |
846 | request.port_no = *port_nop; | |
847 | dpif_netlink_vport_transact(&request, NULL, NULL); | |
622ea8fd | 848 | close_nl_sock(sock); |
69c51582 | 849 | goto exit; |
20c57607 | 850 | } |
20c57607 EG |
851 | |
852 | exit: | |
853 | ofpbuf_delete(buf); | |
20c57607 EG |
854 | |
855 | return error; | |
856 | } | |
857 | ||
858 | static int | |
859 | dpif_netlink_port_add_compat(struct dpif_netlink *dpif, struct netdev *netdev, | |
860 | odp_port_t *port_nop) | |
861 | OVS_REQ_WRLOCK(dpif->upcall_lock) | |
862 | { | |
863 | const struct netdev_tunnel_config *tnl_cfg; | |
864 | char namebuf[NETDEV_VPORT_NAME_BUFSIZE]; | |
865 | const char *type = netdev_get_type(netdev); | |
866 | uint64_t options_stub[64 / 8]; | |
867 | enum ovs_vport_type ovs_type; | |
868 | struct ofpbuf options; | |
869 | const char *name; | |
870 | ||
871 | name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf); | |
872 | ||
873 | ovs_type = netdev_to_ovs_vport_type(netdev_get_type(netdev)); | |
874 | if (ovs_type == OVS_VPORT_TYPE_UNSPEC) { | |
c283069c BP |
875 | VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has " |
876 | "unsupported type `%s'", | |
9b00386b | 877 | dpif_name(&dpif->dpif), name, type); |
c283069c BP |
878 | return EINVAL; |
879 | } | |
c3827f61 | 880 | |
20c57607 | 881 | if (ovs_type == OVS_VPORT_TYPE_NETDEV) { |
93451a0a | 882 | #ifdef _WIN32 |
09cac43f | 883 | /* XXX : Map appropiate Windows handle */ |
93451a0a | 884 | #else |
24b019f8 | 885 | netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false); |
93451a0a | 886 | #endif |
24b019f8 JP |
887 | } |
888 | ||
da467899 | 889 | #ifdef _WIN32 |
20c57607 | 890 | if (ovs_type == OVS_VPORT_TYPE_INTERNAL) { |
da467899 AS |
891 | if (!create_wmi_port(name)){ |
892 | VLOG_ERR("Could not create wmi internal port with name:%s", name); | |
da467899 AS |
893 | return EINVAL; |
894 | }; | |
895 | } | |
896 | #endif | |
897 | ||
26508d9a | 898 | tnl_cfg = netdev_get_tunnel_config(netdev); |
526df7d8 | 899 | if (tnl_cfg && (tnl_cfg->dst_port != 0 || tnl_cfg->exts)) { |
26508d9a | 900 | ofpbuf_use_stack(&options, options_stub, sizeof options_stub); |
526df7d8 TG |
901 | if (tnl_cfg->dst_port) { |
902 | nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT, | |
903 | ntohs(tnl_cfg->dst_port)); | |
904 | } | |
905 | if (tnl_cfg->exts) { | |
906 | size_t ext_ofs; | |
907 | int i; | |
908 | ||
909 | ext_ofs = nl_msg_start_nested(&options, OVS_TUNNEL_ATTR_EXTENSION); | |
910 | for (i = 0; i < 32; i++) { | |
911 | if (tnl_cfg->exts & (1 << i)) { | |
912 | nl_msg_put_flag(&options, i); | |
913 | } | |
914 | } | |
915 | nl_msg_end_nested(&options, ext_ofs); | |
916 | } | |
20c57607 EG |
917 | return dpif_netlink_port_add__(dpif, name, ovs_type, &options, |
918 | port_nop); | |
2510ba7c | 919 | } else { |
20c57607 | 920 | return dpif_netlink_port_add__(dpif, name, ovs_type, NULL, port_nop); |
78a2d59c | 921 | } |
c3827f61 | 922 | |
20c57607 | 923 | } |
989fd548 | 924 | |
921c370a | 925 | static int |
c4e08753 EG |
926 | dpif_netlink_rtnl_port_create_and_add(struct dpif_netlink *dpif, |
927 | struct netdev *netdev, | |
928 | odp_port_t *port_nop) | |
929 | OVS_REQ_WRLOCK(dpif->upcall_lock) | |
930 | { | |
931 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
932 | char namebuf[NETDEV_VPORT_NAME_BUFSIZE]; | |
933 | const char *name; | |
934 | int error; | |
989fd548 | 935 | |
c4e08753 EG |
936 | error = dpif_netlink_rtnl_port_create(netdev); |
937 | if (error) { | |
938 | if (error != EOPNOTSUPP) { | |
d52ef4eb | 939 | VLOG_WARN_RL(&rl, "Failed to create %s with rtnetlink: %s", |
c4e08753 EG |
940 | netdev_get_name(netdev), ovs_strerror(error)); |
941 | } | |
942 | return error; | |
943 | } | |
1579cf67 | 944 | |
c4e08753 EG |
945 | name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf); |
946 | error = dpif_netlink_port_add__(dpif, name, OVS_VPORT_TYPE_NETDEV, NULL, | |
947 | port_nop); | |
c37cb3ee | 948 | if (error) { |
c4e08753 EG |
949 | dpif_netlink_rtnl_port_destroy(name, netdev_get_type(netdev)); |
950 | } | |
951 | return error; | |
952 | } | |
96fba48f BP |
953 | |
954 | static int | |
93451a0a AS |
955 | dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev, |
956 | odp_port_t *port_nop) | |
9fafa796 | 957 | { |
93451a0a | 958 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
921c370a | 959 | int error = EOPNOTSUPP; |
9fafa796 | 960 | |
1579cf67 | 961 | fat_rwlock_wrlock(&dpif->upcall_lock); |
921c370a EG |
962 | if (!ovs_tunnels_out_of_tree) { |
963 | error = dpif_netlink_rtnl_port_create_and_add(dpif, netdev, port_nop); | |
964 | } | |
c37cb3ee | 965 | if (error) { |
921c370a EG |
966 | error = dpif_netlink_port_add_compat(dpif, netdev, port_nop); |
967 | } | |
1579cf67 | 968 | fat_rwlock_unlock(&dpif->upcall_lock); |
9fafa796 BP |
969 | |
970 | return error; | |
971 | } | |
972 | ||
973 | static int | |
93451a0a | 974 | dpif_netlink_port_del__(struct dpif_netlink *dpif, odp_port_t port_no) |
b90de034 | 975 | OVS_REQ_WRLOCK(dpif->upcall_lock) |
96fba48f | 976 | { |
93451a0a | 977 | struct dpif_netlink_vport vport; |
921c370a | 978 | struct dpif_port dpif_port; |
773cd538 | 979 | int error; |
c19e6535 | 980 | |
921c370a EG |
981 | error = dpif_netlink_port_query__(dpif, port_no, NULL, &dpif_port); |
982 | if (error) { | |
983 | return error; | |
984 | } | |
985 | ||
93451a0a | 986 | dpif_netlink_vport_init(&vport); |
df2c07f4 | 987 | vport.cmd = OVS_VPORT_CMD_DEL; |
254f2dc8 | 988 | vport.dp_ifindex = dpif->dp_ifindex; |
c19e6535 | 989 | vport.port_no = port_no; |
da467899 | 990 | #ifdef _WIN32 |
921c370a EG |
991 | if (!strcmp(dpif_port.type, "internal")) { |
992 | if (!delete_wmi_port(dpif_port.name)) { | |
da467899 | 993 | VLOG_ERR("Could not delete wmi port with name: %s", |
921c370a | 994 | dpif_port.name); |
da467899 AS |
995 | }; |
996 | } | |
997 | #endif | |
93451a0a | 998 | error = dpif_netlink_vport_transact(&vport, NULL, NULL); |
773cd538 | 999 | |
1579cf67 | 1000 | vport_del_channels(dpif, port_no); |
989fd548 | 1001 | |
921c370a EG |
1002 | if (!error && !ovs_tunnels_out_of_tree) { |
1003 | error = dpif_netlink_rtnl_port_destroy(dpif_port.name, dpif_port.type); | |
1004 | if (error == EOPNOTSUPP) { | |
1005 | error = 0; | |
1006 | } | |
1007 | } | |
1008 | ||
1009 | dpif_port_destroy(&dpif_port); | |
1010 | ||
773cd538 | 1011 | return error; |
c3827f61 | 1012 | } |
3abc4a1a | 1013 | |
9fafa796 | 1014 | static int |
93451a0a | 1015 | dpif_netlink_port_del(struct dpif *dpif_, odp_port_t port_no) |
9fafa796 | 1016 | { |
93451a0a | 1017 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
9fafa796 BP |
1018 | int error; |
1019 | ||
1579cf67 | 1020 | fat_rwlock_wrlock(&dpif->upcall_lock); |
93451a0a | 1021 | error = dpif_netlink_port_del__(dpif, port_no); |
1579cf67 | 1022 | fat_rwlock_unlock(&dpif->upcall_lock); |
9fafa796 BP |
1023 | |
1024 | return error; | |
1025 | } | |
1026 | ||
c3827f61 | 1027 | static int |
93451a0a AS |
1028 | dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no, |
1029 | const char *port_name, struct dpif_port *dpif_port) | |
c3827f61 | 1030 | { |
93451a0a AS |
1031 | struct dpif_netlink_vport request; |
1032 | struct dpif_netlink_vport reply; | |
c19e6535 | 1033 | struct ofpbuf *buf; |
4c738a8d BP |
1034 | int error; |
1035 | ||
93451a0a | 1036 | dpif_netlink_vport_init(&request); |
df2c07f4 | 1037 | request.cmd = OVS_VPORT_CMD_GET; |
9b00386b | 1038 | request.dp_ifindex = dpif->dp_ifindex; |
c19e6535 BP |
1039 | request.port_no = port_no; |
1040 | request.name = port_name; | |
4c738a8d | 1041 | |
93451a0a | 1042 | error = dpif_netlink_vport_transact(&request, &reply, &buf); |
c19e6535 | 1043 | if (!error) { |
33db1592 BP |
1044 | if (reply.dp_ifindex != request.dp_ifindex) { |
1045 | /* A query by name reported that 'port_name' is in some datapath | |
1046 | * other than 'dpif', but the caller wants to know about 'dpif'. */ | |
1047 | error = ENODEV; | |
4afba28d | 1048 | } else if (dpif_port) { |
33db1592 | 1049 | dpif_port->name = xstrdup(reply.name); |
b9ad7294 | 1050 | dpif_port->type = xstrdup(get_vport_type(&reply)); |
33db1592 BP |
1051 | dpif_port->port_no = reply.port_no; |
1052 | } | |
c19e6535 | 1053 | ofpbuf_delete(buf); |
3abc4a1a | 1054 | } |
c19e6535 | 1055 | return error; |
96fba48f BP |
1056 | } |
1057 | ||
1058 | static int | |
93451a0a AS |
1059 | dpif_netlink_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no, |
1060 | struct dpif_port *dpif_port) | |
96fba48f | 1061 | { |
93451a0a | 1062 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
9b00386b | 1063 | |
93451a0a | 1064 | return dpif_netlink_port_query__(dpif, port_no, NULL, dpif_port); |
96fba48f BP |
1065 | } |
1066 | ||
1067 | static int | |
93451a0a | 1068 | dpif_netlink_port_query_by_name(const struct dpif *dpif_, const char *devname, |
4c738a8d | 1069 | struct dpif_port *dpif_port) |
96fba48f | 1070 | { |
93451a0a | 1071 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
9b00386b | 1072 | |
93451a0a | 1073 | return dpif_netlink_port_query__(dpif, 0, devname, dpif_port); |
96fba48f BP |
1074 | } |
1075 | ||
98403001 | 1076 | static uint32_t |
93451a0a | 1077 | dpif_netlink_port_get_pid__(const struct dpif_netlink *dpif, |
769b5034 | 1078 | odp_port_t port_no) |
b90de034 | 1079 | OVS_REQ_RDLOCK(dpif->upcall_lock) |
98403001 | 1080 | { |
4e022ec0 | 1081 | uint32_t port_idx = odp_to_u32(port_no); |
9fafa796 | 1082 | uint32_t pid = 0; |
98403001 | 1083 | |
f8fc5489 | 1084 | if (dpif->handlers && dpif->uc_array_size > 0) { |
4e022ec0 | 1085 | /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s |
989fd548 | 1086 | * channel, since it is not heavily loaded. */ |
4e022ec0 | 1087 | uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx; |
1579cf67 | 1088 | |
17f2748d AW |
1089 | /* Needs to check in case the socket pointer is changed in between |
1090 | * the holding of upcall_lock. A known case happens when the main | |
1091 | * thread deletes the vport while the handler thread is handling | |
1092 | * the upcall from that port. */ | |
69c51582 MC |
1093 | if (dpif->channels[idx].sock) { |
1094 | pid = nl_sock_pid(dpif->channels[idx].sock); | |
17f2748d | 1095 | } |
98403001 | 1096 | } |
9fafa796 BP |
1097 | |
1098 | return pid; | |
98403001 BP |
1099 | } |
1100 | ||
b90de034 | 1101 | static uint32_t |
769b5034 | 1102 | dpif_netlink_port_get_pid(const struct dpif *dpif_, odp_port_t port_no) |
b90de034 | 1103 | { |
93451a0a | 1104 | const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
b90de034 AW |
1105 | uint32_t ret; |
1106 | ||
1107 | fat_rwlock_rdlock(&dpif->upcall_lock); | |
769b5034 | 1108 | ret = dpif_netlink_port_get_pid__(dpif, port_no); |
b90de034 AW |
1109 | fat_rwlock_unlock(&dpif->upcall_lock); |
1110 | ||
1111 | return ret; | |
1112 | } | |
1113 | ||
96fba48f | 1114 | static int |
93451a0a | 1115 | dpif_netlink_flow_flush(struct dpif *dpif_) |
96fba48f | 1116 | { |
93451a0a AS |
1117 | const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
1118 | struct dpif_netlink_flow flow; | |
37a1300c | 1119 | |
93451a0a | 1120 | dpif_netlink_flow_init(&flow); |
df2c07f4 | 1121 | flow.cmd = OVS_FLOW_CMD_DEL; |
254f2dc8 | 1122 | flow.dp_ifindex = dpif->dp_ifindex; |
f7dde6df PB |
1123 | |
1124 | if (netdev_is_flow_api_enabled()) { | |
dfaf79dd | 1125 | netdev_ports_flow_flush(dpif_->dpif_class); |
f7dde6df PB |
1126 | } |
1127 | ||
93451a0a | 1128 | return dpif_netlink_flow_transact(&flow, NULL, NULL); |
96fba48f BP |
1129 | } |
1130 | ||
93451a0a | 1131 | struct dpif_netlink_port_state { |
f0fef760 | 1132 | struct nl_dump dump; |
d57695d7 | 1133 | struct ofpbuf buf; |
c19e6535 BP |
1134 | }; |
1135 | ||
222837c4 | 1136 | static void |
93451a0a AS |
1137 | dpif_netlink_port_dump_start__(const struct dpif_netlink *dpif, |
1138 | struct nl_dump *dump) | |
96fba48f | 1139 | { |
93451a0a | 1140 | struct dpif_netlink_vport request; |
f0fef760 BP |
1141 | struct ofpbuf *buf; |
1142 | ||
93451a0a | 1143 | dpif_netlink_vport_init(&request); |
067f1e23 | 1144 | request.cmd = OVS_VPORT_CMD_GET; |
254f2dc8 | 1145 | request.dp_ifindex = dpif->dp_ifindex; |
f0fef760 BP |
1146 | |
1147 | buf = ofpbuf_new(1024); | |
93451a0a | 1148 | dpif_netlink_vport_to_ofpbuf(&request, buf); |
222837c4 | 1149 | nl_dump_start(dump, NETLINK_GENERIC, buf); |
f0fef760 | 1150 | ofpbuf_delete(buf); |
222837c4 BP |
1151 | } |
1152 | ||
1153 | static int | |
93451a0a | 1154 | dpif_netlink_port_dump_start(const struct dpif *dpif_, void **statep) |
222837c4 | 1155 | { |
93451a0a AS |
1156 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
1157 | struct dpif_netlink_port_state *state; | |
222837c4 BP |
1158 | |
1159 | *statep = state = xmalloc(sizeof *state); | |
93451a0a | 1160 | dpif_netlink_port_dump_start__(dpif, &state->dump); |
f0fef760 | 1161 | |
d57695d7 | 1162 | ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE); |
b0ec0f27 BP |
1163 | return 0; |
1164 | } | |
1165 | ||
7c1ef244 | 1166 | static int |
93451a0a AS |
1167 | dpif_netlink_port_dump_next__(const struct dpif_netlink *dpif, |
1168 | struct nl_dump *dump, | |
1169 | struct dpif_netlink_vport *vport, | |
1170 | struct ofpbuf *buffer) | |
222837c4 | 1171 | { |
222837c4 BP |
1172 | struct ofpbuf buf; |
1173 | int error; | |
1174 | ||
d57695d7 | 1175 | if (!nl_dump_next(dump, &buf, buffer)) { |
222837c4 BP |
1176 | return EOF; |
1177 | } | |
1178 | ||
93451a0a | 1179 | error = dpif_netlink_vport_from_ofpbuf(vport, &buf); |
222837c4 BP |
1180 | if (error) { |
1181 | VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)", | |
1182 | dpif_name(&dpif->dpif), ovs_strerror(error)); | |
1183 | } | |
1184 | return error; | |
1185 | } | |
1186 | ||
b0ec0f27 | 1187 | static int |
93451a0a AS |
1188 | dpif_netlink_port_dump_next(const struct dpif *dpif_, void *state_, |
1189 | struct dpif_port *dpif_port) | |
b0ec0f27 | 1190 | { |
93451a0a AS |
1191 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
1192 | struct dpif_netlink_port_state *state = state_; | |
1193 | struct dpif_netlink_vport vport; | |
96fba48f BP |
1194 | int error; |
1195 | ||
93451a0a AS |
1196 | error = dpif_netlink_port_dump_next__(dpif, &state->dump, &vport, |
1197 | &state->buf); | |
c3827f61 | 1198 | if (error) { |
f0fef760 | 1199 | return error; |
c3827f61 | 1200 | } |
ebc56baa | 1201 | dpif_port->name = CONST_CAST(char *, vport.name); |
b9ad7294 | 1202 | dpif_port->type = CONST_CAST(char *, get_vport_type(&vport)); |
f0fef760 BP |
1203 | dpif_port->port_no = vport.port_no; |
1204 | return 0; | |
b0ec0f27 BP |
1205 | } |
1206 | ||
1207 | static int | |
93451a0a | 1208 | dpif_netlink_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_) |
b0ec0f27 | 1209 | { |
93451a0a | 1210 | struct dpif_netlink_port_state *state = state_; |
f0fef760 | 1211 | int error = nl_dump_done(&state->dump); |
8522b383 | 1212 | |
d57695d7 | 1213 | ofpbuf_uninit(&state->buf); |
b0ec0f27 | 1214 | free(state); |
f0fef760 | 1215 | return error; |
96fba48f BP |
1216 | } |
1217 | ||
e9e28be3 | 1218 | static int |
93451a0a | 1219 | dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep) |
e9e28be3 | 1220 | { |
93451a0a | 1221 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
e9e28be3 | 1222 | |
e4516b20 BP |
1223 | /* Lazily create the Netlink socket to listen for notifications. */ |
1224 | if (!dpif->port_notifier) { | |
1225 | struct nl_sock *sock; | |
1226 | int error; | |
1227 | ||
1228 | error = nl_sock_create(NETLINK_GENERIC, &sock); | |
1229 | if (error) { | |
1230 | return error; | |
1231 | } | |
1232 | ||
1233 | error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup); | |
1234 | if (error) { | |
1235 | nl_sock_destroy(sock); | |
1236 | return error; | |
1237 | } | |
1238 | dpif->port_notifier = sock; | |
1239 | ||
1240 | /* We have no idea of the current state so report that everything | |
1241 | * changed. */ | |
1242 | return ENOBUFS; | |
1243 | } | |
1244 | ||
1245 | for (;;) { | |
1246 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
1247 | uint64_t buf_stub[4096 / 8]; | |
1248 | struct ofpbuf buf; | |
1249 | int error; | |
1250 | ||
1251 | ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub); | |
a86bd14e | 1252 | error = nl_sock_recv(dpif->port_notifier, &buf, NULL, false); |
e4516b20 | 1253 | if (!error) { |
93451a0a | 1254 | struct dpif_netlink_vport vport; |
e4516b20 | 1255 | |
93451a0a | 1256 | error = dpif_netlink_vport_from_ofpbuf(&vport, &buf); |
e4516b20 BP |
1257 | if (!error) { |
1258 | if (vport.dp_ifindex == dpif->dp_ifindex | |
1259 | && (vport.cmd == OVS_VPORT_CMD_NEW | |
1260 | || vport.cmd == OVS_VPORT_CMD_DEL | |
1261 | || vport.cmd == OVS_VPORT_CMD_SET)) { | |
1262 | VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8, | |
1263 | dpif->dpif.full_name, vport.name, vport.cmd); | |
1579cf67 | 1264 | if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) { |
61eae437 BP |
1265 | dpif->refresh_channels = true; |
1266 | } | |
e4516b20 | 1267 | *devnamep = xstrdup(vport.name); |
59e0c910 | 1268 | ofpbuf_uninit(&buf); |
e4516b20 | 1269 | return 0; |
e4516b20 BP |
1270 | } |
1271 | } | |
59e0c910 BP |
1272 | } else if (error != EAGAIN) { |
1273 | VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)", | |
1274 | ovs_strerror(error)); | |
1275 | nl_sock_drain(dpif->port_notifier); | |
1276 | error = ENOBUFS; | |
e4516b20 BP |
1277 | } |
1278 | ||
59e0c910 BP |
1279 | ofpbuf_uninit(&buf); |
1280 | if (error) { | |
1281 | return error; | |
1282 | } | |
e9e28be3 | 1283 | } |
e9e28be3 BP |
1284 | } |
1285 | ||
1286 | static void | |
93451a0a | 1287 | dpif_netlink_port_poll_wait(const struct dpif *dpif_) |
e9e28be3 | 1288 | { |
93451a0a | 1289 | const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
e4516b20 BP |
1290 | |
1291 | if (dpif->port_notifier) { | |
1292 | nl_sock_wait(dpif->port_notifier, POLLIN); | |
1293 | } else { | |
e9e28be3 | 1294 | poll_immediate_wake(); |
e9e28be3 BP |
1295 | } |
1296 | } | |
1297 | ||
6fe09f8c | 1298 | static void |
70e5ed6f JS |
1299 | dpif_netlink_flow_init_ufid(struct dpif_netlink_flow *request, |
1300 | const ovs_u128 *ufid, bool terse) | |
1301 | { | |
1302 | if (ufid) { | |
1303 | request->ufid = *ufid; | |
1304 | request->ufid_present = true; | |
1305 | } else { | |
1306 | request->ufid_present = false; | |
1307 | } | |
1308 | request->ufid_terse = terse; | |
1309 | } | |
1310 | ||
1311 | static void | |
1312 | dpif_netlink_init_flow_get__(const struct dpif_netlink *dpif, | |
1313 | const struct nlattr *key, size_t key_len, | |
1314 | const ovs_u128 *ufid, bool terse, | |
1315 | struct dpif_netlink_flow *request) | |
96fba48f | 1316 | { |
93451a0a | 1317 | dpif_netlink_flow_init(request); |
6fe09f8c JS |
1318 | request->cmd = OVS_FLOW_CMD_GET; |
1319 | request->dp_ifindex = dpif->dp_ifindex; | |
1320 | request->key = key; | |
1321 | request->key_len = key_len; | |
70e5ed6f JS |
1322 | dpif_netlink_flow_init_ufid(request, ufid, terse); |
1323 | } | |
1324 | ||
1325 | static void | |
1326 | dpif_netlink_init_flow_get(const struct dpif_netlink *dpif, | |
1327 | const struct dpif_flow_get *get, | |
1328 | struct dpif_netlink_flow *request) | |
1329 | { | |
1330 | dpif_netlink_init_flow_get__(dpif, get->key, get->key_len, get->ufid, | |
1331 | false, request); | |
30053024 BP |
1332 | } |
1333 | ||
1334 | static int | |
70e5ed6f JS |
1335 | dpif_netlink_flow_get__(const struct dpif_netlink *dpif, |
1336 | const struct nlattr *key, size_t key_len, | |
1337 | const ovs_u128 *ufid, bool terse, | |
1338 | struct dpif_netlink_flow *reply, struct ofpbuf **bufp) | |
30053024 | 1339 | { |
93451a0a | 1340 | struct dpif_netlink_flow request; |
30053024 | 1341 | |
70e5ed6f | 1342 | dpif_netlink_init_flow_get__(dpif, key, key_len, ufid, terse, &request); |
93451a0a | 1343 | return dpif_netlink_flow_transact(&request, reply, bufp); |
96fba48f BP |
1344 | } |
1345 | ||
70e5ed6f JS |
1346 | static int |
1347 | dpif_netlink_flow_get(const struct dpif_netlink *dpif, | |
1348 | const struct dpif_netlink_flow *flow, | |
1349 | struct dpif_netlink_flow *reply, struct ofpbuf **bufp) | |
1350 | { | |
1351 | return dpif_netlink_flow_get__(dpif, flow->key, flow->key_len, | |
1352 | flow->ufid_present ? &flow->ufid : NULL, | |
1353 | false, reply, bufp); | |
1354 | } | |
1355 | ||
6bc60024 | 1356 | static void |
93451a0a AS |
1357 | dpif_netlink_init_flow_put(struct dpif_netlink *dpif, |
1358 | const struct dpif_flow_put *put, | |
1359 | struct dpif_netlink_flow *request) | |
6bc60024 | 1360 | { |
d64e176c | 1361 | static const struct nlattr dummy_action; |
6bc60024 | 1362 | |
93451a0a | 1363 | dpif_netlink_flow_init(request); |
89625d1e | 1364 | request->cmd = (put->flags & DPIF_FP_CREATE |
6bc60024 BP |
1365 | ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET); |
1366 | request->dp_ifindex = dpif->dp_ifindex; | |
89625d1e BP |
1367 | request->key = put->key; |
1368 | request->key_len = put->key_len; | |
e6cc0bab AZ |
1369 | request->mask = put->mask; |
1370 | request->mask_len = put->mask_len; | |
70e5ed6f JS |
1371 | dpif_netlink_flow_init_ufid(request, put->ufid, false); |
1372 | ||
6bc60024 | 1373 | /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */ |
d64e176c BP |
1374 | request->actions = (put->actions |
1375 | ? put->actions | |
1376 | : CONST_CAST(struct nlattr *, &dummy_action)); | |
89625d1e BP |
1377 | request->actions_len = put->actions_len; |
1378 | if (put->flags & DPIF_FP_ZERO_STATS) { | |
6bc60024 BP |
1379 | request->clear = true; |
1380 | } | |
43f9ac0a JR |
1381 | if (put->flags & DPIF_FP_PROBE) { |
1382 | request->probe = true; | |
1383 | } | |
89625d1e | 1384 | request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE; |
6bc60024 BP |
1385 | } |
1386 | ||
b99d3cee | 1387 | static void |
70e5ed6f JS |
1388 | dpif_netlink_init_flow_del__(struct dpif_netlink *dpif, |
1389 | const struct nlattr *key, size_t key_len, | |
1390 | const ovs_u128 *ufid, bool terse, | |
1391 | struct dpif_netlink_flow *request) | |
96fba48f | 1392 | { |
93451a0a | 1393 | dpif_netlink_flow_init(request); |
b99d3cee BP |
1394 | request->cmd = OVS_FLOW_CMD_DEL; |
1395 | request->dp_ifindex = dpif->dp_ifindex; | |
70e5ed6f JS |
1396 | request->key = key; |
1397 | request->key_len = key_len; | |
1398 | dpif_netlink_flow_init_ufid(request, ufid, terse); | |
1399 | } | |
1400 | ||
1401 | static void | |
1402 | dpif_netlink_init_flow_del(struct dpif_netlink *dpif, | |
1403 | const struct dpif_flow_del *del, | |
1404 | struct dpif_netlink_flow *request) | |
1405 | { | |
37382aa6 AS |
1406 | dpif_netlink_init_flow_del__(dpif, del->key, del->key_len, |
1407 | del->ufid, del->terse, request); | |
70e5ed6f JS |
1408 | } |
1409 | ||
93451a0a | 1410 | struct dpif_netlink_flow_dump { |
ac64794a BP |
1411 | struct dpif_flow_dump up; |
1412 | struct nl_dump nl_dump; | |
d2ad7ef1 | 1413 | atomic_int status; |
f2280b41 PB |
1414 | struct netdev_flow_dump **netdev_dumps; |
1415 | int netdev_dumps_num; /* Number of netdev_flow_dumps */ | |
1416 | struct ovs_mutex netdev_lock; /* Guards the following. */ | |
1417 | int netdev_current_dump OVS_GUARDED; /* Shared current dump */ | |
a692410a | 1418 | struct dpif_flow_dump_types types; /* Type of dump */ |
e723fd32 JS |
1419 | }; |
1420 | ||
93451a0a AS |
1421 | static struct dpif_netlink_flow_dump * |
1422 | dpif_netlink_flow_dump_cast(struct dpif_flow_dump *dump) | |
e723fd32 | 1423 | { |
93451a0a | 1424 | return CONTAINER_OF(dump, struct dpif_netlink_flow_dump, up); |
e723fd32 JS |
1425 | } |
1426 | ||
f2280b41 PB |
1427 | static void |
1428 | start_netdev_dump(const struct dpif *dpif_, | |
1429 | struct dpif_netlink_flow_dump *dump) | |
1430 | { | |
1431 | ovs_mutex_init(&dump->netdev_lock); | |
1432 | ||
a692410a | 1433 | if (!(dump->types.netdev_flows)) { |
f2280b41 PB |
1434 | dump->netdev_dumps_num = 0; |
1435 | dump->netdev_dumps = NULL; | |
1436 | return; | |
1437 | } | |
1438 | ||
1439 | ovs_mutex_lock(&dump->netdev_lock); | |
1440 | dump->netdev_current_dump = 0; | |
1441 | dump->netdev_dumps | |
dfaf79dd | 1442 | = netdev_ports_flow_dump_create(dpif_->dpif_class, |
f2280b41 PB |
1443 | &dump->netdev_dumps_num); |
1444 | ovs_mutex_unlock(&dump->netdev_lock); | |
1445 | } | |
1446 | ||
a692410a GT |
1447 | static void |
1448 | dpif_netlink_populate_flow_dump_types(struct dpif_netlink_flow_dump *dump, | |
1449 | struct dpif_flow_dump_types *types) | |
1450 | { | |
1451 | if (!types) { | |
1452 | dump->types.ovs_flows = true; | |
1453 | dump->types.netdev_flows = true; | |
1454 | } else { | |
1455 | memcpy(&dump->types, types, sizeof *types); | |
494a7455 | 1456 | } |
7e8b7199 PB |
1457 | } |
1458 | ||
ac64794a | 1459 | static struct dpif_flow_dump * |
7e8b7199 | 1460 | dpif_netlink_flow_dump_create(const struct dpif *dpif_, bool terse, |
a692410a | 1461 | struct dpif_flow_dump_types *types) |
96fba48f | 1462 | { |
93451a0a AS |
1463 | const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
1464 | struct dpif_netlink_flow_dump *dump; | |
1465 | struct dpif_netlink_flow request; | |
37a1300c BP |
1466 | struct ofpbuf *buf; |
1467 | ||
ac64794a BP |
1468 | dump = xmalloc(sizeof *dump); |
1469 | dpif_flow_dump_init(&dump->up, dpif_); | |
37a1300c | 1470 | |
a692410a | 1471 | dpif_netlink_populate_flow_dump_types(dump, types); |
37a1300c | 1472 | |
a692410a | 1473 | if (dump->types.ovs_flows) { |
7e8b7199 PB |
1474 | dpif_netlink_flow_init(&request); |
1475 | request.cmd = OVS_FLOW_CMD_GET; | |
1476 | request.dp_ifindex = dpif->dp_ifindex; | |
1477 | request.ufid_present = false; | |
1478 | request.ufid_terse = terse; | |
1479 | ||
1480 | buf = ofpbuf_new(1024); | |
1481 | dpif_netlink_flow_to_ofpbuf(&request, buf); | |
1482 | nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf); | |
1483 | ofpbuf_delete(buf); | |
1484 | } | |
ac64794a | 1485 | atomic_init(&dump->status, 0); |
64bb477f | 1486 | dump->up.terse = terse; |
30053024 | 1487 | |
f2280b41 PB |
1488 | start_netdev_dump(dpif_, dump); |
1489 | ||
ac64794a | 1490 | return &dump->up; |
704a1e09 BP |
1491 | } |
1492 | ||
1493 | static int | |
93451a0a | 1494 | dpif_netlink_flow_dump_destroy(struct dpif_flow_dump *dump_) |
704a1e09 | 1495 | { |
93451a0a | 1496 | struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_); |
7e8b7199 | 1497 | unsigned int nl_status = 0; |
ac64794a | 1498 | int dump_status; |
96fba48f | 1499 | |
a692410a | 1500 | if (dump->types.ovs_flows) { |
7e8b7199 PB |
1501 | nl_status = nl_dump_done(&dump->nl_dump); |
1502 | } | |
1503 | ||
f2280b41 PB |
1504 | for (int i = 0; i < dump->netdev_dumps_num; i++) { |
1505 | int err = netdev_flow_dump_destroy(dump->netdev_dumps[i]); | |
1506 | ||
1507 | if (err != 0 && err != EOPNOTSUPP) { | |
1508 | VLOG_ERR("failed dumping netdev: %s", ovs_strerror(err)); | |
1509 | } | |
1510 | } | |
1511 | ||
1512 | free(dump->netdev_dumps); | |
1513 | ovs_mutex_destroy(&dump->netdev_lock); | |
1514 | ||
7424fc44 JR |
1515 | /* No other thread has access to 'dump' at this point. */ |
1516 | atomic_read_relaxed(&dump->status, &dump_status); | |
ac64794a BP |
1517 | free(dump); |
1518 | return dump_status ? dump_status : nl_status; | |
1519 | } | |
feebdea2 | 1520 | |
93451a0a | 1521 | struct dpif_netlink_flow_dump_thread { |
ac64794a | 1522 | struct dpif_flow_dump_thread up; |
93451a0a AS |
1523 | struct dpif_netlink_flow_dump *dump; |
1524 | struct dpif_netlink_flow flow; | |
ac64794a BP |
1525 | struct dpif_flow_stats stats; |
1526 | struct ofpbuf nl_flows; /* Always used to store flows. */ | |
1527 | struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */ | |
f2280b41 PB |
1528 | int netdev_dump_idx; /* This thread current netdev dump index */ |
1529 | bool netdev_done; /* If we are finished dumping netdevs */ | |
1530 | ||
1531 | /* (Key/Mask/Actions) Buffers for netdev dumping */ | |
1532 | struct odputil_keybuf keybuf[FLOW_DUMP_MAX_BATCH]; | |
1533 | struct odputil_keybuf maskbuf[FLOW_DUMP_MAX_BATCH]; | |
1534 | struct odputil_keybuf actbuf[FLOW_DUMP_MAX_BATCH]; | |
ac64794a BP |
1535 | }; |
1536 | ||
93451a0a AS |
1537 | static struct dpif_netlink_flow_dump_thread * |
1538 | dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread) | |
ac64794a | 1539 | { |
93451a0a | 1540 | return CONTAINER_OF(thread, struct dpif_netlink_flow_dump_thread, up); |
ac64794a BP |
1541 | } |
1542 | ||
1543 | static struct dpif_flow_dump_thread * | |
93451a0a | 1544 | dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump *dump_) |
ac64794a | 1545 | { |
93451a0a AS |
1546 | struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_); |
1547 | struct dpif_netlink_flow_dump_thread *thread; | |
ac64794a BP |
1548 | |
1549 | thread = xmalloc(sizeof *thread); | |
1550 | dpif_flow_dump_thread_init(&thread->up, &dump->up); | |
1551 | thread->dump = dump; | |
1552 | ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE); | |
1553 | thread->nl_actions = NULL; | |
f2280b41 PB |
1554 | thread->netdev_dump_idx = 0; |
1555 | thread->netdev_done = !(thread->netdev_dump_idx < dump->netdev_dumps_num); | |
ac64794a BP |
1556 | |
1557 | return &thread->up; | |
1558 | } | |
1559 | ||
1560 | static void | |
93451a0a | 1561 | dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_) |
ac64794a | 1562 | { |
93451a0a AS |
1563 | struct dpif_netlink_flow_dump_thread *thread |
1564 | = dpif_netlink_flow_dump_thread_cast(thread_); | |
ac64794a BP |
1565 | |
1566 | ofpbuf_uninit(&thread->nl_flows); | |
1567 | ofpbuf_delete(thread->nl_actions); | |
1568 | free(thread); | |
1569 | } | |
1570 | ||
1571 | static void | |
7af12bd7 | 1572 | dpif_netlink_flow_to_dpif_flow(struct dpif *dpif, struct dpif_flow *dpif_flow, |
7fe98598 | 1573 | const struct dpif_netlink_flow *datapath_flow) |
ac64794a | 1574 | { |
7fe98598 NR |
1575 | dpif_flow->key = datapath_flow->key; |
1576 | dpif_flow->key_len = datapath_flow->key_len; | |
1577 | dpif_flow->mask = datapath_flow->mask; | |
1578 | dpif_flow->mask_len = datapath_flow->mask_len; | |
1579 | dpif_flow->actions = datapath_flow->actions; | |
1580 | dpif_flow->actions_len = datapath_flow->actions_len; | |
70e5ed6f | 1581 | dpif_flow->ufid_present = datapath_flow->ufid_present; |
ec97c2df | 1582 | dpif_flow->pmd_id = PMD_ID_NULL; |
70e5ed6f JS |
1583 | if (datapath_flow->ufid_present) { |
1584 | dpif_flow->ufid = datapath_flow->ufid; | |
1585 | } else { | |
1586 | ovs_assert(datapath_flow->key && datapath_flow->key_len); | |
1587 | dpif_flow_hash(dpif, datapath_flow->key, datapath_flow->key_len, | |
1588 | &dpif_flow->ufid); | |
1589 | } | |
7fe98598 | 1590 | dpif_netlink_flow_get_stats(datapath_flow, &dpif_flow->stats); |
d63ca532 GT |
1591 | dpif_flow->attrs.offloaded = false; |
1592 | dpif_flow->attrs.dp_layer = "ovs"; | |
ac64794a BP |
1593 | } |
1594 | ||
f2280b41 PB |
1595 | /* The design is such that all threads are working together on the first dump |
1596 | * to the last, in order (at first they all on dump 0). | |
1597 | * When the first thread finds that the given dump is finished, | |
1598 | * they all move to the next. If two or more threads find the same dump | |
1599 | * is finished at the same time, the first one will advance the shared | |
1600 | * netdev_current_dump and the others will catch up. */ | |
1601 | static void | |
1602 | dpif_netlink_advance_netdev_dump(struct dpif_netlink_flow_dump_thread *thread) | |
1603 | { | |
1604 | struct dpif_netlink_flow_dump *dump = thread->dump; | |
1605 | ||
1606 | ovs_mutex_lock(&dump->netdev_lock); | |
1607 | /* if we haven't finished (dumped everything) */ | |
1608 | if (dump->netdev_current_dump < dump->netdev_dumps_num) { | |
1609 | /* if we are the first to find that current dump is finished | |
1610 | * advance it. */ | |
1611 | if (thread->netdev_dump_idx == dump->netdev_current_dump) { | |
1612 | thread->netdev_dump_idx = ++dump->netdev_current_dump; | |
1613 | /* did we just finish the last dump? done. */ | |
1614 | if (dump->netdev_current_dump == dump->netdev_dumps_num) { | |
1615 | thread->netdev_done = true; | |
1616 | } | |
1617 | } else { | |
1618 | /* otherwise, we are behind, catch up */ | |
1619 | thread->netdev_dump_idx = dump->netdev_current_dump; | |
1620 | } | |
1621 | } else { | |
1622 | /* some other thread finished */ | |
1623 | thread->netdev_done = true; | |
1624 | } | |
1625 | ovs_mutex_unlock(&dump->netdev_lock); | |
1626 | } | |
1627 | ||
1628 | static int | |
1629 | dpif_netlink_netdev_match_to_dpif_flow(struct match *match, | |
1630 | struct ofpbuf *key_buf, | |
1631 | struct ofpbuf *mask_buf, | |
1632 | struct nlattr *actions, | |
1633 | struct dpif_flow_stats *stats, | |
d63ca532 | 1634 | struct dpif_flow_attrs *attrs, |
f2280b41 PB |
1635 | ovs_u128 *ufid, |
1636 | struct dpif_flow *flow, | |
1637 | bool terse OVS_UNUSED) | |
1638 | { | |
1639 | ||
1640 | struct odp_flow_key_parms odp_parms = { | |
1641 | .flow = &match->flow, | |
1642 | .mask = &match->wc.masks, | |
1643 | .support = { | |
f9885dc5 | 1644 | .max_vlan_headers = 2, |
b2ae4069 | 1645 | .recirc = true, |
576126a9 PB |
1646 | .ct_state = true, |
1647 | .ct_zone = true, | |
9221c721 PB |
1648 | .ct_mark = true, |
1649 | .ct_label = true, | |
f2280b41 PB |
1650 | }, |
1651 | }; | |
1652 | size_t offset; | |
1653 | ||
1654 | memset(flow, 0, sizeof *flow); | |
1655 | ||
1656 | /* Key */ | |
1657 | offset = key_buf->size; | |
1658 | flow->key = ofpbuf_tail(key_buf); | |
1659 | odp_flow_key_from_flow(&odp_parms, key_buf); | |
1660 | flow->key_len = key_buf->size - offset; | |
1661 | ||
1662 | /* Mask */ | |
1663 | offset = mask_buf->size; | |
1664 | flow->mask = ofpbuf_tail(mask_buf); | |
1665 | odp_parms.key_buf = key_buf; | |
1666 | odp_flow_key_from_mask(&odp_parms, mask_buf); | |
1667 | flow->mask_len = mask_buf->size - offset; | |
1668 | ||
1669 | /* Actions */ | |
1670 | flow->actions = nl_attr_get(actions); | |
1671 | flow->actions_len = nl_attr_get_size(actions); | |
1672 | ||
1673 | /* Stats */ | |
1674 | memcpy(&flow->stats, stats, sizeof *stats); | |
1675 | ||
1676 | /* UFID */ | |
1677 | flow->ufid_present = true; | |
1678 | flow->ufid = *ufid; | |
1679 | ||
1680 | flow->pmd_id = PMD_ID_NULL; | |
4742003c | 1681 | |
d63ca532 | 1682 | memcpy(&flow->attrs, attrs, sizeof *attrs); |
4742003c | 1683 | |
f2280b41 PB |
1684 | return 0; |
1685 | } | |
1686 | ||
ac64794a | 1687 | static int |
93451a0a AS |
1688 | dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread *thread_, |
1689 | struct dpif_flow *flows, int max_flows) | |
ac64794a | 1690 | { |
93451a0a AS |
1691 | struct dpif_netlink_flow_dump_thread *thread |
1692 | = dpif_netlink_flow_dump_thread_cast(thread_); | |
1693 | struct dpif_netlink_flow_dump *dump = thread->dump; | |
1694 | struct dpif_netlink *dpif = dpif_netlink_cast(thread->up.dpif); | |
ac64794a BP |
1695 | int n_flows; |
1696 | ||
1697 | ofpbuf_delete(thread->nl_actions); | |
1698 | thread->nl_actions = NULL; | |
1699 | ||
1700 | n_flows = 0; | |
f2280b41 PB |
1701 | max_flows = MIN(max_flows, FLOW_DUMP_MAX_BATCH); |
1702 | ||
1703 | while (!thread->netdev_done && n_flows < max_flows) { | |
1704 | struct odputil_keybuf *maskbuf = &thread->maskbuf[n_flows]; | |
1705 | struct odputil_keybuf *keybuf = &thread->keybuf[n_flows]; | |
1706 | struct odputil_keybuf *actbuf = &thread->actbuf[n_flows]; | |
1707 | struct ofpbuf key, mask, act; | |
1708 | struct dpif_flow *f = &flows[n_flows]; | |
1709 | int cur = thread->netdev_dump_idx; | |
1710 | struct netdev_flow_dump *netdev_dump = dump->netdev_dumps[cur]; | |
1711 | struct match match; | |
1712 | struct nlattr *actions; | |
1713 | struct dpif_flow_stats stats; | |
d63ca532 | 1714 | struct dpif_flow_attrs attrs; |
f2280b41 PB |
1715 | ovs_u128 ufid; |
1716 | bool has_next; | |
1717 | ||
1718 | ofpbuf_use_stack(&key, keybuf, sizeof *keybuf); | |
1719 | ofpbuf_use_stack(&act, actbuf, sizeof *actbuf); | |
1720 | ofpbuf_use_stack(&mask, maskbuf, sizeof *maskbuf); | |
1721 | has_next = netdev_flow_dump_next(netdev_dump, &match, | |
d63ca532 | 1722 | &actions, &stats, &attrs, |
f2280b41 PB |
1723 | &ufid, |
1724 | &thread->nl_flows, | |
1725 | &act); | |
1726 | if (has_next) { | |
1727 | dpif_netlink_netdev_match_to_dpif_flow(&match, | |
1728 | &key, &mask, | |
1729 | actions, | |
1730 | &stats, | |
d63ca532 | 1731 | &attrs, |
f2280b41 PB |
1732 | &ufid, |
1733 | f, | |
1734 | dump->up.terse); | |
1735 | n_flows++; | |
1736 | } else { | |
1737 | dpif_netlink_advance_netdev_dump(thread); | |
1738 | } | |
1739 | } | |
1740 | ||
a692410a | 1741 | if (!(dump->types.ovs_flows)) { |
7e8b7199 PB |
1742 | return n_flows; |
1743 | } | |
1744 | ||
ac64794a | 1745 | while (!n_flows |
6fd6ed71 | 1746 | || (n_flows < max_flows && thread->nl_flows.size)) { |
7fe98598 | 1747 | struct dpif_netlink_flow datapath_flow; |
ac64794a BP |
1748 | struct ofpbuf nl_flow; |
1749 | int error; | |
1750 | ||
1751 | /* Try to grab another flow. */ | |
1752 | if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) { | |
1753 | break; | |
feebdea2 | 1754 | } |
30053024 | 1755 | |
ac64794a | 1756 | /* Convert the flow to our output format. */ |
7fe98598 | 1757 | error = dpif_netlink_flow_from_ofpbuf(&datapath_flow, &nl_flow); |
30053024 | 1758 | if (error) { |
7424fc44 | 1759 | atomic_store_relaxed(&dump->status, error); |
ac64794a | 1760 | break; |
feebdea2 | 1761 | } |
30053024 | 1762 | |
64bb477f JS |
1763 | if (dump->up.terse || datapath_flow.actions) { |
1764 | /* Common case: we don't want actions, or the flow includes | |
1765 | * actions. */ | |
7af12bd7 JS |
1766 | dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++], |
1767 | &datapath_flow); | |
ac64794a BP |
1768 | } else { |
1769 | /* Rare case: the flow does not include actions. Retrieve this | |
1770 | * individual flow again to get the actions. */ | |
70e5ed6f | 1771 | error = dpif_netlink_flow_get(dpif, &datapath_flow, |
7fe98598 | 1772 | &datapath_flow, &thread->nl_actions); |
30053024 BP |
1773 | if (error == ENOENT) { |
1774 | VLOG_DBG("dumped flow disappeared on get"); | |
ac64794a | 1775 | continue; |
30053024 | 1776 | } else if (error) { |
10a89ef0 BP |
1777 | VLOG_WARN("error fetching dumped flow: %s", |
1778 | ovs_strerror(error)); | |
7424fc44 | 1779 | atomic_store_relaxed(&dump->status, error); |
ac64794a | 1780 | break; |
30053024 | 1781 | } |
30053024 | 1782 | |
ac64794a BP |
1783 | /* Save this flow. Then exit, because we only have one buffer to |
1784 | * handle this case. */ | |
7af12bd7 JS |
1785 | dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++], |
1786 | &datapath_flow); | |
ac64794a BP |
1787 | break; |
1788 | } | |
feebdea2 | 1789 | } |
ac64794a | 1790 | return n_flows; |
96fba48f BP |
1791 | } |
1792 | ||
eabe7c68 | 1793 | static void |
93451a0a AS |
1794 | dpif_netlink_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec, |
1795 | struct ofpbuf *buf) | |
96fba48f | 1796 | { |
89625d1e | 1797 | struct ovs_header *k_exec; |
758c456d | 1798 | size_t key_ofs; |
f7cd0081 | 1799 | |
eabe7c68 | 1800 | ofpbuf_prealloc_tailroom(buf, (64 |
cf62fa4c | 1801 | + dp_packet_size(d_exec->packet) |
758c456d | 1802 | + ODP_KEY_METADATA_SIZE |
eabe7c68 | 1803 | + d_exec->actions_len)); |
f7cd0081 | 1804 | |
df2c07f4 | 1805 | nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST, |
69685a88 | 1806 | OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION); |
f7cd0081 | 1807 | |
89625d1e BP |
1808 | k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec); |
1809 | k_exec->dp_ifindex = dp_ifindex; | |
f7cd0081 | 1810 | |
89625d1e | 1811 | nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, |
cf62fa4c PS |
1812 | dp_packet_data(d_exec->packet), |
1813 | dp_packet_size(d_exec->packet)); | |
758c456d JR |
1814 | |
1815 | key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY); | |
beb75a40 | 1816 | odp_key_from_dp_packet(buf, d_exec->packet); |
758c456d JR |
1817 | nl_msg_end_nested(buf, key_ofs); |
1818 | ||
89625d1e BP |
1819 | nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, |
1820 | d_exec->actions, d_exec->actions_len); | |
43f9ac0a | 1821 | if (d_exec->probe) { |
2e460098 | 1822 | nl_msg_put_flag(buf, OVS_PACKET_ATTR_PROBE); |
43f9ac0a | 1823 | } |
27130224 AZ |
1824 | if (d_exec->mtu) { |
1825 | nl_msg_put_u16(buf, OVS_PACKET_ATTR_MRU, d_exec->mtu); | |
1826 | } | |
0442bfb1 TZ |
1827 | |
1828 | if (d_exec->hash) { | |
1829 | nl_msg_put_u64(buf, OVS_PACKET_ATTR_HASH, d_exec->hash); | |
1830 | } | |
6bc60024 BP |
1831 | } |
1832 | ||
0f3358ea BP |
1833 | /* Executes, against 'dpif', up to the first 'n_ops' operations in 'ops'. |
1834 | * Returns the number actually executed (at least 1, if 'n_ops' is | |
1835 | * positive). */ | |
1836 | static size_t | |
93451a0a AS |
1837 | dpif_netlink_operate__(struct dpif_netlink *dpif, |
1838 | struct dpif_op **ops, size_t n_ops) | |
6bc60024 | 1839 | { |
eabe7c68 BP |
1840 | struct op_auxdata { |
1841 | struct nl_transaction txn; | |
72d32ac0 | 1842 | |
eabe7c68 BP |
1843 | struct ofpbuf request; |
1844 | uint64_t request_stub[1024 / 8]; | |
72d32ac0 BP |
1845 | |
1846 | struct ofpbuf reply; | |
1847 | uint64_t reply_stub[1024 / 8]; | |
8b668ee3 | 1848 | } auxes[OPERATE_MAX_OPS]; |
eabe7c68 | 1849 | |
8b668ee3 | 1850 | struct nl_transaction *txnsp[OPERATE_MAX_OPS]; |
6bc60024 BP |
1851 | size_t i; |
1852 | ||
8b668ee3 | 1853 | n_ops = MIN(n_ops, OPERATE_MAX_OPS); |
6bc60024 | 1854 | for (i = 0; i < n_ops; i++) { |
eabe7c68 | 1855 | struct op_auxdata *aux = &auxes[i]; |
c2b565b5 | 1856 | struct dpif_op *op = ops[i]; |
b99d3cee BP |
1857 | struct dpif_flow_put *put; |
1858 | struct dpif_flow_del *del; | |
6fe09f8c | 1859 | struct dpif_flow_get *get; |
93451a0a | 1860 | struct dpif_netlink_flow flow; |
eabe7c68 BP |
1861 | |
1862 | ofpbuf_use_stub(&aux->request, | |
1863 | aux->request_stub, sizeof aux->request_stub); | |
1864 | aux->txn.request = &aux->request; | |
b99d3cee | 1865 | |
72d32ac0 BP |
1866 | ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub); |
1867 | aux->txn.reply = NULL; | |
1868 | ||
b99d3cee BP |
1869 | switch (op->type) { |
1870 | case DPIF_OP_FLOW_PUT: | |
fa37affa | 1871 | put = &op->flow_put; |
93451a0a | 1872 | dpif_netlink_init_flow_put(dpif, put, &flow); |
6bc60024 | 1873 | if (put->stats) { |
eabe7c68 | 1874 | flow.nlmsg_flags |= NLM_F_ECHO; |
72d32ac0 | 1875 | aux->txn.reply = &aux->reply; |
6bc60024 | 1876 | } |
93451a0a | 1877 | dpif_netlink_flow_to_ofpbuf(&flow, &aux->request); |
b99d3cee BP |
1878 | break; |
1879 | ||
1880 | case DPIF_OP_FLOW_DEL: | |
fa37affa | 1881 | del = &op->flow_del; |
93451a0a | 1882 | dpif_netlink_init_flow_del(dpif, del, &flow); |
b99d3cee | 1883 | if (del->stats) { |
eabe7c68 | 1884 | flow.nlmsg_flags |= NLM_F_ECHO; |
72d32ac0 | 1885 | aux->txn.reply = &aux->reply; |
b99d3cee | 1886 | } |
93451a0a | 1887 | dpif_netlink_flow_to_ofpbuf(&flow, &aux->request); |
b99d3cee | 1888 | break; |
6bc60024 | 1889 | |
b99d3cee | 1890 | case DPIF_OP_EXECUTE: |
0f3358ea BP |
1891 | /* Can't execute a packet that won't fit in a Netlink attribute. */ |
1892 | if (OVS_UNLIKELY(nl_attr_oversized( | |
fa37affa | 1893 | dp_packet_size(op->execute.packet)))) { |
0f3358ea BP |
1894 | /* Report an error immediately if this is the first operation. |
1895 | * Otherwise the easiest thing to do is to postpone to the next | |
1896 | * call (when this will be the first operation). */ | |
1897 | if (i == 0) { | |
1898 | VLOG_ERR_RL(&error_rl, | |
1899 | "dropping oversized %"PRIu32"-byte packet", | |
fa37affa | 1900 | dp_packet_size(op->execute.packet)); |
0f3358ea BP |
1901 | op->error = ENOBUFS; |
1902 | return 1; | |
1903 | } | |
1904 | n_ops = i; | |
1905 | } else { | |
fa37affa | 1906 | dpif_netlink_encode_execute(dpif->dp_ifindex, &op->execute, |
0f3358ea BP |
1907 | &aux->request); |
1908 | } | |
b99d3cee BP |
1909 | break; |
1910 | ||
6fe09f8c | 1911 | case DPIF_OP_FLOW_GET: |
fa37affa | 1912 | get = &op->flow_get; |
70e5ed6f | 1913 | dpif_netlink_init_flow_get(dpif, get, &flow); |
6fe09f8c | 1914 | aux->txn.reply = get->buffer; |
93451a0a | 1915 | dpif_netlink_flow_to_ofpbuf(&flow, &aux->request); |
6fe09f8c JS |
1916 | break; |
1917 | ||
b99d3cee | 1918 | default: |
428b2edd | 1919 | OVS_NOT_REACHED(); |
6bc60024 BP |
1920 | } |
1921 | } | |
1922 | ||
6bc60024 | 1923 | for (i = 0; i < n_ops; i++) { |
eabe7c68 | 1924 | txnsp[i] = &auxes[i].txn; |
6bc60024 | 1925 | } |
a88b4e04 | 1926 | nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops); |
6bc60024 | 1927 | |
6bc60024 | 1928 | for (i = 0; i < n_ops; i++) { |
72d32ac0 | 1929 | struct op_auxdata *aux = &auxes[i]; |
eabe7c68 | 1930 | struct nl_transaction *txn = &auxes[i].txn; |
c2b565b5 | 1931 | struct dpif_op *op = ops[i]; |
b99d3cee BP |
1932 | struct dpif_flow_put *put; |
1933 | struct dpif_flow_del *del; | |
6fe09f8c | 1934 | struct dpif_flow_get *get; |
6bc60024 | 1935 | |
b99d3cee | 1936 | op->error = txn->error; |
6bc60024 | 1937 | |
b99d3cee BP |
1938 | switch (op->type) { |
1939 | case DPIF_OP_FLOW_PUT: | |
fa37affa | 1940 | put = &op->flow_put; |
cfceb2b5 | 1941 | if (put->stats) { |
b99d3cee | 1942 | if (!op->error) { |
93451a0a | 1943 | struct dpif_netlink_flow reply; |
cfceb2b5 | 1944 | |
93451a0a AS |
1945 | op->error = dpif_netlink_flow_from_ofpbuf(&reply, |
1946 | txn->reply); | |
cfceb2b5 | 1947 | if (!op->error) { |
93451a0a | 1948 | dpif_netlink_flow_get_stats(&reply, put->stats); |
cfceb2b5 BP |
1949 | } |
1950 | } | |
6bc60024 | 1951 | } |
b99d3cee BP |
1952 | break; |
1953 | ||
1954 | case DPIF_OP_FLOW_DEL: | |
fa37affa | 1955 | del = &op->flow_del; |
cfceb2b5 | 1956 | if (del->stats) { |
b99d3cee | 1957 | if (!op->error) { |
93451a0a | 1958 | struct dpif_netlink_flow reply; |
cfceb2b5 | 1959 | |
93451a0a AS |
1960 | op->error = dpif_netlink_flow_from_ofpbuf(&reply, |
1961 | txn->reply); | |
cfceb2b5 | 1962 | if (!op->error) { |
93451a0a | 1963 | dpif_netlink_flow_get_stats(&reply, del->stats); |
cfceb2b5 BP |
1964 | } |
1965 | } | |
b99d3cee BP |
1966 | } |
1967 | break; | |
1968 | ||
1969 | case DPIF_OP_EXECUTE: | |
1970 | break; | |
1971 | ||
6fe09f8c | 1972 | case DPIF_OP_FLOW_GET: |
fa37affa | 1973 | get = &op->flow_get; |
6fe09f8c | 1974 | if (!op->error) { |
93451a0a | 1975 | struct dpif_netlink_flow reply; |
6fe09f8c | 1976 | |
93451a0a | 1977 | op->error = dpif_netlink_flow_from_ofpbuf(&reply, txn->reply); |
6fe09f8c | 1978 | if (!op->error) { |
7af12bd7 JS |
1979 | dpif_netlink_flow_to_dpif_flow(&dpif->dpif, get->flow, |
1980 | &reply); | |
6fe09f8c JS |
1981 | } |
1982 | } | |
1983 | break; | |
1984 | ||
b99d3cee | 1985 | default: |
428b2edd | 1986 | OVS_NOT_REACHED(); |
6bc60024 BP |
1987 | } |
1988 | ||
72d32ac0 BP |
1989 | ofpbuf_uninit(&aux->request); |
1990 | ofpbuf_uninit(&aux->reply); | |
6bc60024 | 1991 | } |
0f3358ea BP |
1992 | |
1993 | return n_ops; | |
eabe7c68 BP |
1994 | } |
1995 | ||
6c343984 PB |
1996 | static int |
1997 | parse_flow_get(struct dpif_netlink *dpif, struct dpif_flow_get *get) | |
1998 | { | |
1999 | struct dpif_flow *dpif_flow = get->flow; | |
2000 | struct match match; | |
2001 | struct nlattr *actions; | |
2002 | struct dpif_flow_stats stats; | |
d63ca532 | 2003 | struct dpif_flow_attrs attrs; |
6c343984 PB |
2004 | struct ofpbuf buf; |
2005 | uint64_t act_buf[1024 / 8]; | |
2006 | struct odputil_keybuf maskbuf; | |
2007 | struct odputil_keybuf keybuf; | |
2008 | struct odputil_keybuf actbuf; | |
2009 | struct ofpbuf key, mask, act; | |
2010 | int err; | |
2011 | ||
2012 | ofpbuf_use_stack(&buf, &act_buf, sizeof act_buf); | |
dfaf79dd | 2013 | err = netdev_ports_flow_get(dpif->dpif.dpif_class, &match, |
d63ca532 | 2014 | &actions, get->ufid, &stats, &attrs, &buf); |
6c343984 PB |
2015 | if (err) { |
2016 | return err; | |
2017 | } | |
2018 | ||
2019 | VLOG_DBG("found flow from netdev, translating to dpif flow"); | |
2020 | ||
2021 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); | |
2022 | ofpbuf_use_stack(&act, &actbuf, sizeof actbuf); | |
2023 | ofpbuf_use_stack(&mask, &maskbuf, sizeof maskbuf); | |
2024 | dpif_netlink_netdev_match_to_dpif_flow(&match, &key, &mask, actions, | |
d63ca532 | 2025 | &stats, &attrs, |
6c343984 PB |
2026 | (ovs_u128 *) get->ufid, |
2027 | dpif_flow, | |
2028 | false); | |
2029 | ofpbuf_put(get->buffer, nl_attr_get(actions), nl_attr_get_size(actions)); | |
2030 | dpif_flow->actions = ofpbuf_at(get->buffer, 0, 0); | |
2031 | dpif_flow->actions_len = nl_attr_get_size(actions); | |
2032 | ||
2033 | return 0; | |
2034 | } | |
2035 | ||
8b668ee3 PB |
2036 | static int |
2037 | parse_flow_put(struct dpif_netlink *dpif, struct dpif_flow_put *put) | |
2038 | { | |
2039 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
dfaf79dd | 2040 | const struct dpif_class *dpif_class = dpif->dpif.dpif_class; |
8b668ee3 PB |
2041 | struct match match; |
2042 | odp_port_t in_port; | |
2043 | const struct nlattr *nla; | |
2044 | size_t left; | |
8b668ee3 PB |
2045 | struct netdev *dev; |
2046 | struct offload_info info; | |
2047 | ovs_be16 dst_port = 0; | |
d9677a1f | 2048 | uint8_t csum_on = false; |
8b668ee3 PB |
2049 | int err; |
2050 | ||
2051 | if (put->flags & DPIF_FP_PROBE) { | |
2052 | return EOPNOTSUPP; | |
2053 | } | |
2054 | ||
2055 | err = parse_key_and_mask_to_match(put->key, put->key_len, put->mask, | |
2056 | put->mask_len, &match); | |
2057 | if (err) { | |
2058 | return err; | |
2059 | } | |
2060 | ||
8b668ee3 | 2061 | in_port = match.flow.in_port.odp_port; |
dfaf79dd | 2062 | dev = netdev_ports_get(in_port, dpif_class); |
8b668ee3 PB |
2063 | if (!dev) { |
2064 | return EOPNOTSUPP; | |
2065 | } | |
2066 | ||
00a0a011 | 2067 | /* Get tunnel dst port */ |
8b668ee3 PB |
2068 | NL_ATTR_FOR_EACH(nla, left, put->actions, put->actions_len) { |
2069 | if (nl_attr_type(nla) == OVS_ACTION_ATTR_OUTPUT) { | |
2070 | const struct netdev_tunnel_config *tnl_cfg; | |
2071 | struct netdev *outdev; | |
2072 | odp_port_t out_port; | |
2073 | ||
8b668ee3 | 2074 | out_port = nl_attr_get_odp_port(nla); |
dfaf79dd | 2075 | outdev = netdev_ports_get(out_port, dpif_class); |
8b668ee3 PB |
2076 | if (!outdev) { |
2077 | err = EOPNOTSUPP; | |
2078 | goto out; | |
2079 | } | |
2080 | tnl_cfg = netdev_get_tunnel_config(outdev); | |
2081 | if (tnl_cfg && tnl_cfg->dst_port != 0) { | |
2082 | dst_port = tnl_cfg->dst_port; | |
2083 | } | |
d9677a1f EB |
2084 | if (tnl_cfg) { |
2085 | csum_on = tnl_cfg->csum; | |
2086 | } | |
8b668ee3 PB |
2087 | netdev_close(outdev); |
2088 | } | |
2089 | } | |
2090 | ||
dfaf79dd | 2091 | info.dpif_class = dpif_class; |
8b668ee3 | 2092 | info.tp_dst_port = dst_port; |
d9677a1f | 2093 | info.tunnel_csum_on = csum_on; |
b2ae4069 PB |
2094 | info.recirc_id_shared_with_tc = (dpif->user_features |
2095 | & OVS_DP_F_TC_RECIRC_SHARING); | |
8b668ee3 PB |
2096 | err = netdev_flow_put(dev, &match, |
2097 | CONST_CAST(struct nlattr *, put->actions), | |
2098 | put->actions_len, | |
2099 | CONST_CAST(ovs_u128 *, put->ufid), | |
2100 | &info, put->stats); | |
2101 | ||
2102 | if (!err) { | |
2103 | if (put->flags & DPIF_FP_MODIFY) { | |
2104 | struct dpif_op *opp; | |
2105 | struct dpif_op op; | |
2106 | ||
2107 | op.type = DPIF_OP_FLOW_DEL; | |
fa37affa BP |
2108 | op.flow_del.key = put->key; |
2109 | op.flow_del.key_len = put->key_len; | |
2110 | op.flow_del.ufid = put->ufid; | |
2111 | op.flow_del.pmd_id = put->pmd_id; | |
2112 | op.flow_del.stats = NULL; | |
2113 | op.flow_del.terse = false; | |
8b668ee3 PB |
2114 | |
2115 | opp = &op; | |
2116 | dpif_netlink_operate__(dpif, &opp, 1); | |
2117 | } | |
2118 | ||
2119 | VLOG_DBG("added flow"); | |
2120 | } else if (err != EEXIST) { | |
738c785f | 2121 | struct netdev *oor_netdev = NULL; |
1028cb71 | 2122 | enum vlog_level level; |
738c785f SB |
2123 | if (err == ENOSPC && netdev_is_offload_rebalance_policy_enabled()) { |
2124 | /* | |
2125 | * We need to set OOR on the input netdev (i.e, 'dev') for the | |
2126 | * flow. But if the flow has a tunnel attribute (i.e, decap action, | |
2127 | * with a virtual device like a VxLAN interface as its in-port), | |
2128 | * then lookup and set OOR on the underlying tunnel (real) netdev. | |
2129 | */ | |
2130 | oor_netdev = flow_get_tunnel_netdev(&match.flow.tunnel); | |
2131 | if (!oor_netdev) { | |
2132 | /* Not a 'tunnel' flow */ | |
2133 | oor_netdev = dev; | |
2134 | } | |
2135 | netdev_set_hw_info(oor_netdev, HW_INFO_TYPE_OOR, true); | |
2136 | } | |
1028cb71 | 2137 | level = (err == ENOSPC || err == EOPNOTSUPP) ? VLL_DBG : VLL_ERR; |
2138 | VLOG_RL(&rl, level, "failed to offload flow: %s: %s", | |
2139 | ovs_strerror(err), | |
2140 | (oor_netdev ? oor_netdev->name : dev->name)); | |
8b668ee3 PB |
2141 | } |
2142 | ||
2143 | out: | |
2144 | if (err && err != EEXIST && (put->flags & DPIF_FP_MODIFY)) { | |
2145 | /* Modified rule can't be offloaded, try and delete from HW */ | |
2146 | int del_err = netdev_flow_del(dev, put->ufid, put->stats); | |
2147 | ||
2148 | if (!del_err) { | |
2149 | /* Delete from hw success, so old flow was offloaded. | |
2150 | * Change flags to create the flow in kernel */ | |
2151 | put->flags &= ~DPIF_FP_MODIFY; | |
2152 | put->flags |= DPIF_FP_CREATE; | |
2153 | } else if (del_err != ENOENT) { | |
2154 | VLOG_ERR_RL(&rl, "failed to delete offloaded flow: %s", | |
2155 | ovs_strerror(del_err)); | |
2156 | /* stop proccesing the flow in kernel */ | |
2157 | err = 0; | |
2158 | } | |
2159 | } | |
2160 | ||
2161 | netdev_close(dev); | |
2162 | ||
2163 | return err; | |
2164 | } | |
2165 | ||
8b668ee3 PB |
2166 | static int |
2167 | try_send_to_netdev(struct dpif_netlink *dpif, struct dpif_op *op) | |
eabe7c68 | 2168 | { |
8b668ee3 | 2169 | int err = EOPNOTSUPP; |
9b00386b | 2170 | |
8b668ee3 PB |
2171 | switch (op->type) { |
2172 | case DPIF_OP_FLOW_PUT: { | |
fa37affa | 2173 | struct dpif_flow_put *put = &op->flow_put; |
8b668ee3 PB |
2174 | |
2175 | if (!put->ufid) { | |
2176 | break; | |
2177 | } | |
3cd99886 | 2178 | |
8b668ee3 | 2179 | err = parse_flow_put(dpif, put); |
f7392b44 | 2180 | log_flow_put_message(&dpif->dpif, &this_module, put, 0); |
8b668ee3 PB |
2181 | break; |
2182 | } | |
0335a89c | 2183 | case DPIF_OP_FLOW_DEL: { |
fa37affa | 2184 | struct dpif_flow_del *del = &op->flow_del; |
0335a89c PB |
2185 | |
2186 | if (!del->ufid) { | |
2187 | break; | |
2188 | } | |
3cd99886 | 2189 | |
dfaf79dd | 2190 | err = netdev_ports_flow_del(dpif->dpif.dpif_class, del->ufid, |
0335a89c | 2191 | del->stats); |
f7392b44 | 2192 | log_flow_del_message(&dpif->dpif, &this_module, del, 0); |
0335a89c PB |
2193 | break; |
2194 | } | |
6c343984 | 2195 | case DPIF_OP_FLOW_GET: { |
fa37affa | 2196 | struct dpif_flow_get *get = &op->flow_get; |
6c343984 | 2197 | |
fa37affa | 2198 | if (!op->flow_get.ufid) { |
6c343984 PB |
2199 | break; |
2200 | } | |
3cd99886 | 2201 | |
6c343984 | 2202 | err = parse_flow_get(dpif, get); |
f7392b44 | 2203 | log_flow_get_message(&dpif->dpif, &this_module, get, 0); |
6c343984 PB |
2204 | break; |
2205 | } | |
8b668ee3 PB |
2206 | case DPIF_OP_EXECUTE: |
2207 | default: | |
2208 | break; | |
2209 | } | |
2210 | ||
2211 | return err; | |
2212 | } | |
2213 | ||
2214 | static void | |
2215 | dpif_netlink_operate_chunks(struct dpif_netlink *dpif, struct dpif_op **ops, | |
2216 | size_t n_ops) | |
2217 | { | |
eabe7c68 | 2218 | while (n_ops > 0) { |
0f3358ea | 2219 | size_t chunk = dpif_netlink_operate__(dpif, ops, n_ops); |
8b668ee3 | 2220 | |
eabe7c68 BP |
2221 | ops += chunk; |
2222 | n_ops -= chunk; | |
2223 | } | |
6bc60024 BP |
2224 | } |
2225 | ||
8b668ee3 | 2226 | static void |
57924fc9 SB |
2227 | dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops, |
2228 | enum dpif_offload_type offload_type) | |
8b668ee3 PB |
2229 | { |
2230 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); | |
2231 | struct dpif_op *new_ops[OPERATE_MAX_OPS]; | |
2232 | int count = 0; | |
2233 | int i = 0; | |
2234 | int err = 0; | |
2235 | ||
57924fc9 SB |
2236 | if (offload_type == DPIF_OFFLOAD_ALWAYS && !netdev_is_flow_api_enabled()) { |
2237 | VLOG_DBG("Invalid offload_type: %d", offload_type); | |
2238 | return; | |
2239 | } | |
2240 | ||
2241 | if (offload_type != DPIF_OFFLOAD_NEVER && netdev_is_flow_api_enabled()) { | |
8b668ee3 PB |
2242 | while (n_ops > 0) { |
2243 | count = 0; | |
2244 | ||
2245 | while (n_ops > 0 && count < OPERATE_MAX_OPS) { | |
2246 | struct dpif_op *op = ops[i++]; | |
2247 | ||
2248 | err = try_send_to_netdev(dpif, op); | |
2249 | if (err && err != EEXIST) { | |
57924fc9 SB |
2250 | if (offload_type == DPIF_OFFLOAD_ALWAYS) { |
2251 | /* We got an error while offloading an op. Since | |
2252 | * OFFLOAD_ALWAYS is specified, we stop further | |
2253 | * processing and return to the caller without | |
2254 | * invoking kernel datapath as fallback. But the | |
2255 | * interface requires us to process all n_ops; so | |
2256 | * return the same error in the remaining ops too. | |
2257 | */ | |
2258 | op->error = err; | |
2259 | n_ops--; | |
2260 | while (n_ops > 0) { | |
2261 | op = ops[i++]; | |
2262 | op->error = err; | |
2263 | n_ops--; | |
2264 | } | |
2265 | return; | |
2266 | } | |
8b668ee3 PB |
2267 | new_ops[count++] = op; |
2268 | } else { | |
2269 | op->error = err; | |
2270 | } | |
2271 | ||
2272 | n_ops--; | |
2273 | } | |
2274 | ||
2275 | dpif_netlink_operate_chunks(dpif, new_ops, count); | |
2276 | } | |
57924fc9 | 2277 | } else if (offload_type != DPIF_OFFLOAD_ALWAYS) { |
8b668ee3 PB |
2278 | dpif_netlink_operate_chunks(dpif, ops, n_ops); |
2279 | } | |
2280 | } | |
2281 | ||
09cac43f NR |
2282 | #if _WIN32 |
2283 | static void | |
2284 | dpif_netlink_handler_uninit(struct dpif_handler *handler) | |
2285 | { | |
2286 | vport_delete_sock_pool(handler); | |
2287 | } | |
2288 | ||
2289 | static int | |
2290 | dpif_netlink_handler_init(struct dpif_handler *handler) | |
2291 | { | |
2292 | return vport_create_sock_pool(handler); | |
2293 | } | |
2294 | #else | |
2295 | ||
2296 | static int | |
2297 | dpif_netlink_handler_init(struct dpif_handler *handler) | |
2298 | { | |
2299 | handler->epoll_fd = epoll_create(10); | |
2300 | return handler->epoll_fd < 0 ? errno : 0; | |
2301 | } | |
2302 | ||
2303 | static void | |
2304 | dpif_netlink_handler_uninit(struct dpif_handler *handler) | |
2305 | { | |
2306 | close(handler->epoll_fd); | |
2307 | } | |
2308 | #endif | |
2309 | ||
1579cf67 AW |
2310 | /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports |
2311 | * currently in 'dpif' in the kernel, by adding a new set of channels for | |
2312 | * any kernel vport that lacks one and deleting any channels that have no | |
2313 | * backing kernel vports. */ | |
96fba48f | 2314 | static int |
93451a0a | 2315 | dpif_netlink_refresh_channels(struct dpif_netlink *dpif, uint32_t n_handlers) |
b90de034 | 2316 | OVS_REQ_WRLOCK(dpif->upcall_lock) |
96fba48f | 2317 | { |
8381a3d3 | 2318 | unsigned long int *keep_channels; |
93451a0a | 2319 | struct dpif_netlink_vport vport; |
8381a3d3 BP |
2320 | size_t keep_channels_nbits; |
2321 | struct nl_dump dump; | |
d57695d7 JS |
2322 | uint64_t reply_stub[NL_DUMP_BUFSIZE / 8]; |
2323 | struct ofpbuf buf; | |
8381a3d3 BP |
2324 | int retval = 0; |
2325 | size_t i; | |
982b8810 | 2326 | |
09cac43f NR |
2327 | ovs_assert(!WINDOWS || n_handlers <= 1); |
2328 | ovs_assert(!WINDOWS || dpif->n_handlers <= 1); | |
2329 | ||
1579cf67 AW |
2330 | if (dpif->n_handlers != n_handlers) { |
2331 | destroy_all_channels(dpif); | |
2332 | dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers); | |
2333 | for (i = 0; i < n_handlers; i++) { | |
09cac43f | 2334 | int error; |
1579cf67 AW |
2335 | struct dpif_handler *handler = &dpif->handlers[i]; |
2336 | ||
09cac43f NR |
2337 | error = dpif_netlink_handler_init(handler); |
2338 | if (error) { | |
1579cf67 AW |
2339 | size_t j; |
2340 | ||
2341 | for (j = 0; j < i; j++) { | |
aa5c0216 | 2342 | struct dpif_handler *tmp = &dpif->handlers[j]; |
09cac43f | 2343 | dpif_netlink_handler_uninit(tmp); |
1579cf67 AW |
2344 | } |
2345 | free(dpif->handlers); | |
2346 | dpif->handlers = NULL; | |
2347 | ||
09cac43f | 2348 | return error; |
1579cf67 | 2349 | } |
8381a3d3 | 2350 | } |
1579cf67 AW |
2351 | dpif->n_handlers = n_handlers; |
2352 | } | |
2353 | ||
2354 | for (i = 0; i < n_handlers; i++) { | |
2355 | struct dpif_handler *handler = &dpif->handlers[i]; | |
2356 | ||
2357 | handler->event_offset = handler->n_events = 0; | |
17411ecf | 2358 | } |
b063d9f0 | 2359 | |
8381a3d3 BP |
2360 | keep_channels_nbits = dpif->uc_array_size; |
2361 | keep_channels = bitmap_allocate(keep_channels_nbits); | |
982b8810 | 2362 | |
d57695d7 | 2363 | ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub); |
93451a0a AS |
2364 | dpif_netlink_port_dump_start__(dpif, &dump); |
2365 | while (!dpif_netlink_port_dump_next__(dpif, &dump, &vport, &buf)) { | |
8381a3d3 | 2366 | uint32_t port_no = odp_to_u32(vport.port_no); |
69c51582 | 2367 | uint32_t upcall_pid; |
8381a3d3 | 2368 | int error; |
50f80534 | 2369 | |
1579cf67 | 2370 | if (port_no >= dpif->uc_array_size |
69c51582 | 2371 | || !vport_get_pid(dpif, port_no, &upcall_pid)) { |
622ea8fd BP |
2372 | struct nl_sock *sock; |
2373 | error = create_nl_sock(dpif, &sock); | |
1579cf67 | 2374 | |
d240e46a | 2375 | if (error) { |
1579cf67 AW |
2376 | goto error; |
2377 | } | |
2378 | ||
622ea8fd | 2379 | error = vport_add_channel(dpif, vport.port_no, sock); |
b063d9f0 | 2380 | if (error) { |
1579cf67 | 2381 | VLOG_INFO("%s: could not add channels for port %s", |
9b00386b | 2382 | dpif_name(&dpif->dpif), vport.name); |
622ea8fd | 2383 | nl_sock_destroy(sock); |
8381a3d3 BP |
2384 | retval = error; |
2385 | goto error; | |
982b8810 | 2386 | } |
622ea8fd | 2387 | upcall_pid = nl_sock_pid(sock); |
8381a3d3 | 2388 | } |
50f80534 | 2389 | |
8381a3d3 | 2390 | /* Configure the vport to deliver misses to 'sock'. */ |
1579cf67 | 2391 | if (vport.upcall_pids[0] == 0 |
69c51582 MC |
2392 | || vport.n_upcall_pids != 1 |
2393 | || upcall_pid != vport.upcall_pids[0]) { | |
93451a0a | 2394 | struct dpif_netlink_vport vport_request; |
989fd548 | 2395 | |
93451a0a | 2396 | dpif_netlink_vport_init(&vport_request); |
989fd548 JP |
2397 | vport_request.cmd = OVS_VPORT_CMD_SET; |
2398 | vport_request.dp_ifindex = dpif->dp_ifindex; | |
8381a3d3 | 2399 | vport_request.port_no = vport.port_no; |
69c51582 MC |
2400 | vport_request.n_upcall_pids = 1; |
2401 | vport_request.upcall_pids = &upcall_pid; | |
93451a0a | 2402 | error = dpif_netlink_vport_transact(&vport_request, NULL, NULL); |
1579cf67 | 2403 | if (error) { |
989fd548 JP |
2404 | VLOG_WARN_RL(&error_rl, |
2405 | "%s: failed to set upcall pid on port: %s", | |
10a89ef0 | 2406 | dpif_name(&dpif->dpif), ovs_strerror(error)); |
989fd548 | 2407 | |
8381a3d3 BP |
2408 | if (error != ENODEV && error != ENOENT) { |
2409 | retval = error; | |
989fd548 | 2410 | } else { |
8381a3d3 BP |
2411 | /* The vport isn't really there, even though the dump says |
2412 | * it is. Probably we just hit a race after a port | |
2413 | * disappeared. */ | |
989fd548 | 2414 | } |
8381a3d3 | 2415 | goto error; |
50f80534 | 2416 | } |
8381a3d3 | 2417 | } |
14b4d2f9 | 2418 | |
8381a3d3 BP |
2419 | if (port_no < keep_channels_nbits) { |
2420 | bitmap_set1(keep_channels, port_no); | |
2421 | } | |
2422 | continue; | |
2423 | ||
2424 | error: | |
1579cf67 | 2425 | vport_del_channels(dpif, vport.port_no); |
982b8810 | 2426 | } |
8381a3d3 | 2427 | nl_dump_done(&dump); |
d57695d7 | 2428 | ofpbuf_uninit(&buf); |
b063d9f0 | 2429 | |
8381a3d3 BP |
2430 | /* Discard any saved channels that we didn't reuse. */ |
2431 | for (i = 0; i < keep_channels_nbits; i++) { | |
2432 | if (!bitmap_is_set(keep_channels, i)) { | |
1579cf67 | 2433 | vport_del_channels(dpif, u32_to_odp(i)); |
8381a3d3 BP |
2434 | } |
2435 | } | |
2436 | free(keep_channels); | |
2437 | ||
2438 | return retval; | |
2439 | } | |
2440 | ||
2441 | static int | |
93451a0a | 2442 | dpif_netlink_recv_set__(struct dpif_netlink *dpif, bool enable) |
b90de034 | 2443 | OVS_REQ_WRLOCK(dpif->upcall_lock) |
8381a3d3 | 2444 | { |
1579cf67 | 2445 | if ((dpif->handlers != NULL) == enable) { |
8381a3d3 BP |
2446 | return 0; |
2447 | } else if (!enable) { | |
1579cf67 | 2448 | destroy_all_channels(dpif); |
8381a3d3 BP |
2449 | return 0; |
2450 | } else { | |
93451a0a | 2451 | return dpif_netlink_refresh_channels(dpif, 1); |
8381a3d3 | 2452 | } |
96fba48f BP |
2453 | } |
2454 | ||
9fafa796 | 2455 | static int |
93451a0a | 2456 | dpif_netlink_recv_set(struct dpif *dpif_, bool enable) |
9fafa796 | 2457 | { |
93451a0a | 2458 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
9fafa796 BP |
2459 | int error; |
2460 | ||
1579cf67 | 2461 | fat_rwlock_wrlock(&dpif->upcall_lock); |
93451a0a | 2462 | error = dpif_netlink_recv_set__(dpif, enable); |
1579cf67 | 2463 | fat_rwlock_unlock(&dpif->upcall_lock); |
9fafa796 BP |
2464 | |
2465 | return error; | |
2466 | } | |
2467 | ||
1954e6bb | 2468 | static int |
93451a0a | 2469 | dpif_netlink_handlers_set(struct dpif *dpif_, uint32_t n_handlers) |
1954e6bb | 2470 | { |
93451a0a | 2471 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
1579cf67 AW |
2472 | int error = 0; |
2473 | ||
09cac43f NR |
2474 | #ifdef _WIN32 |
2475 | /* Multiple upcall handlers will be supported once kernel datapath supports | |
2476 | * it. */ | |
2477 | if (n_handlers > 1) { | |
2478 | return error; | |
2479 | } | |
2480 | #endif | |
2481 | ||
1579cf67 AW |
2482 | fat_rwlock_wrlock(&dpif->upcall_lock); |
2483 | if (dpif->handlers) { | |
93451a0a | 2484 | error = dpif_netlink_refresh_channels(dpif, n_handlers); |
1579cf67 AW |
2485 | } |
2486 | fat_rwlock_unlock(&dpif->upcall_lock); | |
2487 | ||
2488 | return error; | |
1954e6bb AW |
2489 | } |
2490 | ||
aae51f53 | 2491 | static int |
93451a0a | 2492 | dpif_netlink_queue_to_priority(const struct dpif *dpif OVS_UNUSED, |
aae51f53 BP |
2493 | uint32_t queue_id, uint32_t *priority) |
2494 | { | |
2495 | if (queue_id < 0xf000) { | |
17ee3c1f | 2496 | *priority = TC_H_MAKE(1 << 16, queue_id + 1); |
aae51f53 BP |
2497 | return 0; |
2498 | } else { | |
2499 | return EINVAL; | |
2500 | } | |
2501 | } | |
2502 | ||
96fba48f | 2503 | static int |
7af12bd7 JS |
2504 | parse_odp_packet(const struct dpif_netlink *dpif, struct ofpbuf *buf, |
2505 | struct dpif_upcall *upcall, int *dp_ifindex) | |
856081f6 | 2506 | { |
df2c07f4 | 2507 | static const struct nl_policy ovs_packet_policy[] = { |
856081f6 | 2508 | /* Always present. */ |
df2c07f4 | 2509 | [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC, |
856081f6 | 2510 | .min_len = ETH_HEADER_LEN }, |
df2c07f4 | 2511 | [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED }, |
856081f6 | 2512 | |
df2c07f4 | 2513 | /* OVS_PACKET_CMD_ACTION only. */ |
e995e3df | 2514 | [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true }, |
8b7ea2d4 | 2515 | [OVS_PACKET_ATTR_EGRESS_TUN_KEY] = { .type = NL_A_NESTED, .optional = true }, |
7321bda3 | 2516 | [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true }, |
0442bfb1 TZ |
2517 | [OVS_PACKET_ATTR_MRU] = { .type = NL_A_U16, .optional = true }, |
2518 | [OVS_PACKET_ATTR_HASH] = { .type = NL_A_U64, .optional = true } | |
856081f6 BP |
2519 | }; |
2520 | ||
0a2869d5 BP |
2521 | struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size); |
2522 | struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg); | |
2523 | struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl); | |
2524 | struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header); | |
982b8810 | 2525 | |
0a2869d5 | 2526 | struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)]; |
df2c07f4 JP |
2527 | if (!nlmsg || !genl || !ovs_header |
2528 | || nlmsg->nlmsg_type != ovs_packet_family | |
2529 | || !nl_policy_parse(&b, 0, ovs_packet_policy, a, | |
2530 | ARRAY_SIZE(ovs_packet_policy))) { | |
856081f6 BP |
2531 | return EINVAL; |
2532 | } | |
2533 | ||
0a2869d5 BP |
2534 | int type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS |
2535 | : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION | |
2536 | : -1); | |
aaff4b55 BP |
2537 | if (type < 0) { |
2538 | return EINVAL; | |
2539 | } | |
82272ede | 2540 | |
877c9270 | 2541 | /* (Re)set ALL fields of '*upcall' on successful return. */ |
aaff4b55 | 2542 | upcall->type = type; |
ebc56baa BP |
2543 | upcall->key = CONST_CAST(struct nlattr *, |
2544 | nl_attr_get(a[OVS_PACKET_ATTR_KEY])); | |
df2c07f4 | 2545 | upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]); |
7af12bd7 | 2546 | dpif_flow_hash(&dpif->dpif, upcall->key, upcall->key_len, &upcall->ufid); |
e995e3df | 2547 | upcall->userdata = a[OVS_PACKET_ATTR_USERDATA]; |
8b7ea2d4 | 2548 | upcall->out_tun_key = a[OVS_PACKET_ATTR_EGRESS_TUN_KEY]; |
7321bda3 | 2549 | upcall->actions = a[OVS_PACKET_ATTR_ACTIONS]; |
27130224 | 2550 | upcall->mru = a[OVS_PACKET_ATTR_MRU]; |
0442bfb1 | 2551 | upcall->hash = a[OVS_PACKET_ATTR_HASH]; |
da546e07 JR |
2552 | |
2553 | /* Allow overwriting the netlink attribute header without reallocating. */ | |
cf62fa4c | 2554 | dp_packet_use_stub(&upcall->packet, |
da546e07 JR |
2555 | CONST_CAST(struct nlattr *, |
2556 | nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1, | |
2557 | nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) + | |
2558 | sizeof(struct nlattr)); | |
cf62fa4c PS |
2559 | dp_packet_set_data(&upcall->packet, |
2560 | (char *)dp_packet_data(&upcall->packet) + sizeof(struct nlattr)); | |
2561 | dp_packet_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET])); | |
da546e07 | 2562 | |
2482b0b0 JS |
2563 | if (nl_attr_find__(upcall->key, upcall->key_len, OVS_KEY_ATTR_ETHERNET)) { |
2564 | /* Ethernet frame */ | |
2565 | upcall->packet.packet_type = htonl(PT_ETH); | |
2566 | } else { | |
2567 | /* Non-Ethernet packet. Get the Ethertype from the NL attributes */ | |
2568 | ovs_be16 ethertype = 0; | |
2569 | const struct nlattr *et_nla = nl_attr_find__(upcall->key, | |
2570 | upcall->key_len, | |
2571 | OVS_KEY_ATTR_ETHERTYPE); | |
2572 | if (et_nla) { | |
2573 | ethertype = nl_attr_get_be16(et_nla); | |
2574 | } | |
2575 | upcall->packet.packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, | |
2576 | ntohs(ethertype)); | |
2577 | dp_packet_set_l3(&upcall->packet, dp_packet_data(&upcall->packet)); | |
2578 | } | |
2579 | ||
df2c07f4 | 2580 | *dp_ifindex = ovs_header->dp_ifindex; |
982b8810 | 2581 | |
856081f6 BP |
2582 | return 0; |
2583 | } | |
2584 | ||
09cac43f NR |
2585 | #ifdef _WIN32 |
2586 | #define PACKET_RECV_BATCH_SIZE 50 | |
2587 | static int | |
2588 | dpif_netlink_recv_windows(struct dpif_netlink *dpif, uint32_t handler_id, | |
2589 | struct dpif_upcall *upcall, struct ofpbuf *buf) | |
2590 | OVS_REQ_RDLOCK(dpif->upcall_lock) | |
2591 | { | |
2592 | struct dpif_handler *handler; | |
2593 | int read_tries = 0; | |
2594 | struct dpif_windows_vport_sock *sock_pool; | |
2595 | uint32_t i; | |
2596 | ||
2597 | if (!dpif->handlers) { | |
2598 | return EAGAIN; | |
2599 | } | |
2600 | ||
2601 | /* Only one handler is supported currently. */ | |
2602 | if (handler_id >= 1) { | |
2603 | return EAGAIN; | |
2604 | } | |
2605 | ||
2606 | if (handler_id >= dpif->n_handlers) { | |
2607 | return EAGAIN; | |
2608 | } | |
2609 | ||
2610 | handler = &dpif->handlers[handler_id]; | |
2611 | sock_pool = handler->vport_sock_pool; | |
2612 | ||
2613 | for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) { | |
2614 | for (;;) { | |
2615 | int dp_ifindex; | |
2616 | int error; | |
2617 | ||
2618 | if (++read_tries > PACKET_RECV_BATCH_SIZE) { | |
2619 | return EAGAIN; | |
2620 | } | |
2621 | ||
a86bd14e | 2622 | error = nl_sock_recv(sock_pool[i].nl_sock, buf, NULL, false); |
09cac43f NR |
2623 | if (error == ENOBUFS) { |
2624 | /* ENOBUFS typically means that we've received so many | |
2625 | * packets that the buffer overflowed. Try again | |
2626 | * immediately because there's almost certainly a packet | |
2627 | * waiting for us. */ | |
2628 | /* XXX: report_loss(dpif, ch, idx, handler_id); */ | |
2629 | continue; | |
2630 | } | |
2631 | ||
2632 | /* XXX: ch->last_poll = time_msec(); */ | |
2633 | if (error) { | |
2634 | if (error == EAGAIN) { | |
2635 | break; | |
2636 | } | |
2637 | return error; | |
2638 | } | |
2639 | ||
27edb4aa | 2640 | error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex); |
09cac43f NR |
2641 | if (!error && dp_ifindex == dpif->dp_ifindex) { |
2642 | return 0; | |
2643 | } else if (error) { | |
2644 | return error; | |
2645 | } | |
2646 | } | |
2647 | } | |
2648 | ||
2649 | return EAGAIN; | |
2650 | } | |
2651 | #else | |
856081f6 | 2652 | static int |
93451a0a AS |
2653 | dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id, |
2654 | struct dpif_upcall *upcall, struct ofpbuf *buf) | |
b90de034 | 2655 | OVS_REQ_RDLOCK(dpif->upcall_lock) |
96fba48f | 2656 | { |
1579cf67 | 2657 | struct dpif_handler *handler; |
17411ecf | 2658 | int read_tries = 0; |
96fba48f | 2659 | |
1579cf67 AW |
2660 | if (!dpif->handlers || handler_id >= dpif->n_handlers) { |
2661 | return EAGAIN; | |
982b8810 BP |
2662 | } |
2663 | ||
1579cf67 AW |
2664 | handler = &dpif->handlers[handler_id]; |
2665 | if (handler->event_offset >= handler->n_events) { | |
8522ba09 | 2666 | int retval; |
989fd548 | 2667 | |
1579cf67 | 2668 | handler->event_offset = handler->n_events = 0; |
f6d1465c | 2669 | |
8522ba09 | 2670 | do { |
1579cf67 | 2671 | retval = epoll_wait(handler->epoll_fd, handler->epoll_events, |
989fd548 | 2672 | dpif->uc_array_size, 0); |
8522ba09 | 2673 | } while (retval < 0 && errno == EINTR); |
09cac43f | 2674 | |
8522ba09 BP |
2675 | if (retval < 0) { |
2676 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1); | |
10a89ef0 | 2677 | VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno)); |
989fd548 | 2678 | } else if (retval > 0) { |
1579cf67 | 2679 | handler->n_events = retval; |
8522ba09 | 2680 | } |
8522ba09 BP |
2681 | } |
2682 | ||
1579cf67 AW |
2683 | while (handler->event_offset < handler->n_events) { |
2684 | int idx = handler->epoll_events[handler->event_offset].data.u32; | |
69c51582 | 2685 | struct dpif_channel *ch = &dpif->channels[idx]; |
8522ba09 | 2686 | |
1579cf67 | 2687 | handler->event_offset++; |
17411ecf | 2688 | |
f6d1465c | 2689 | for (;;) { |
8522ba09 | 2690 | int dp_ifindex; |
f6d1465c | 2691 | int error; |
17411ecf | 2692 | |
f6d1465c BP |
2693 | if (++read_tries > 50) { |
2694 | return EAGAIN; | |
2695 | } | |
17411ecf | 2696 | |
a86bd14e | 2697 | error = nl_sock_recv(ch->sock, buf, NULL, false); |
14b4d2f9 BP |
2698 | if (error == ENOBUFS) { |
2699 | /* ENOBUFS typically means that we've received so many | |
2700 | * packets that the buffer overflowed. Try again | |
2701 | * immediately because there's almost certainly a packet | |
2702 | * waiting for us. */ | |
9b00386b | 2703 | report_loss(dpif, ch, idx, handler_id); |
14b4d2f9 BP |
2704 | continue; |
2705 | } | |
2706 | ||
2707 | ch->last_poll = time_msec(); | |
72d32ac0 | 2708 | if (error) { |
72d32ac0 BP |
2709 | if (error == EAGAIN) { |
2710 | break; | |
2711 | } | |
f6d1465c BP |
2712 | return error; |
2713 | } | |
17411ecf | 2714 | |
7af12bd7 | 2715 | error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex); |
a12b3ead | 2716 | if (!error && dp_ifindex == dpif->dp_ifindex) { |
f6d1465c | 2717 | return 0; |
989fd548 | 2718 | } else if (error) { |
f6d1465c | 2719 | return error; |
17411ecf | 2720 | } |
982b8810 | 2721 | } |
50f80534 | 2722 | } |
982b8810 BP |
2723 | |
2724 | return EAGAIN; | |
96fba48f | 2725 | } |
09cac43f | 2726 | #endif |
96fba48f | 2727 | |
9fafa796 | 2728 | static int |
93451a0a AS |
2729 | dpif_netlink_recv(struct dpif *dpif_, uint32_t handler_id, |
2730 | struct dpif_upcall *upcall, struct ofpbuf *buf) | |
9fafa796 | 2731 | { |
93451a0a | 2732 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
9fafa796 BP |
2733 | int error; |
2734 | ||
1579cf67 | 2735 | fat_rwlock_rdlock(&dpif->upcall_lock); |
09cac43f NR |
2736 | #ifdef _WIN32 |
2737 | error = dpif_netlink_recv_windows(dpif, handler_id, upcall, buf); | |
2738 | #else | |
93451a0a | 2739 | error = dpif_netlink_recv__(dpif, handler_id, upcall, buf); |
09cac43f | 2740 | #endif |
1579cf67 | 2741 | fat_rwlock_unlock(&dpif->upcall_lock); |
9fafa796 BP |
2742 | |
2743 | return error; | |
2744 | } | |
2745 | ||
96fba48f | 2746 | static void |
93451a0a | 2747 | dpif_netlink_recv_wait__(struct dpif_netlink *dpif, uint32_t handler_id) |
b90de034 | 2748 | OVS_REQ_RDLOCK(dpif->upcall_lock) |
96fba48f | 2749 | { |
93451a0a | 2750 | #ifdef _WIN32 |
09cac43f NR |
2751 | uint32_t i; |
2752 | struct dpif_windows_vport_sock *sock_pool = | |
2753 | dpif->handlers[handler_id].vport_sock_pool; | |
2754 | ||
2755 | /* Only one handler is supported currently. */ | |
2756 | if (handler_id >= 1) { | |
2757 | return; | |
2758 | } | |
2759 | ||
2760 | for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) { | |
2761 | nl_sock_wait(sock_pool[i].nl_sock, POLLIN); | |
2762 | } | |
93451a0a | 2763 | #else |
1579cf67 AW |
2764 | if (dpif->handlers && handler_id < dpif->n_handlers) { |
2765 | struct dpif_handler *handler = &dpif->handlers[handler_id]; | |
2766 | ||
2767 | poll_fd_wait(handler->epoll_fd, POLLIN); | |
17411ecf | 2768 | } |
93451a0a | 2769 | #endif |
96fba48f BP |
2770 | } |
2771 | ||
1ba530f4 | 2772 | static void |
93451a0a | 2773 | dpif_netlink_recv_wait(struct dpif *dpif_, uint32_t handler_id) |
1ba530f4 | 2774 | { |
93451a0a | 2775 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
17411ecf | 2776 | |
b90de034 | 2777 | fat_rwlock_rdlock(&dpif->upcall_lock); |
93451a0a | 2778 | dpif_netlink_recv_wait__(dpif, handler_id); |
b90de034 AW |
2779 | fat_rwlock_unlock(&dpif->upcall_lock); |
2780 | } | |
2781 | ||
2782 | static void | |
93451a0a | 2783 | dpif_netlink_recv_purge__(struct dpif_netlink *dpif) |
b90de034 AW |
2784 | OVS_REQ_WRLOCK(dpif->upcall_lock) |
2785 | { | |
1579cf67 | 2786 | if (dpif->handlers) { |
69c51582 | 2787 | size_t i; |
1579cf67 | 2788 | |
69c51582 MC |
2789 | if (!dpif->channels[0].sock) { |
2790 | return; | |
2791 | } | |
1579cf67 | 2792 | for (i = 0; i < dpif->uc_array_size; i++ ) { |
1ba530f4 | 2793 | |
69c51582 | 2794 | nl_sock_drain(dpif->channels[i].sock); |
989fd548 | 2795 | } |
1ba530f4 | 2796 | } |
b90de034 AW |
2797 | } |
2798 | ||
2799 | static void | |
93451a0a | 2800 | dpif_netlink_recv_purge(struct dpif *dpif_) |
b90de034 | 2801 | { |
93451a0a | 2802 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
b90de034 AW |
2803 | |
2804 | fat_rwlock_wrlock(&dpif->upcall_lock); | |
93451a0a | 2805 | dpif_netlink_recv_purge__(dpif); |
1579cf67 | 2806 | fat_rwlock_unlock(&dpif->upcall_lock); |
1ba530f4 BP |
2807 | } |
2808 | ||
b5cbbcf6 AZ |
2809 | static char * |
2810 | dpif_netlink_get_datapath_version(void) | |
2811 | { | |
2812 | char *version_str = NULL; | |
2813 | ||
2814 | #ifdef __linux__ | |
2815 | ||
2816 | #define MAX_VERSION_STR_SIZE 80 | |
2817 | #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version" | |
2818 | FILE *f; | |
2819 | ||
2820 | f = fopen(LINUX_DATAPATH_VERSION_FILE, "r"); | |
2821 | if (f) { | |
2822 | char *newline; | |
2823 | char version[MAX_VERSION_STR_SIZE]; | |
2824 | ||
2825 | if (fgets(version, MAX_VERSION_STR_SIZE, f)) { | |
2826 | newline = strchr(version, '\n'); | |
2827 | if (newline) { | |
2828 | *newline = '\0'; | |
2829 | } | |
2830 | version_str = xstrdup(version); | |
2831 | } | |
2832 | fclose(f); | |
2833 | } | |
2834 | #endif | |
2835 | ||
2836 | return version_str; | |
2837 | } | |
2838 | ||
c11c9f4a DDP |
2839 | struct dpif_netlink_ct_dump_state { |
2840 | struct ct_dpif_dump_state up; | |
2841 | struct nl_ct_dump_state *nl_ct_dump; | |
2842 | }; | |
2843 | ||
2844 | static int | |
2845 | dpif_netlink_ct_dump_start(struct dpif *dpif OVS_UNUSED, | |
2846 | struct ct_dpif_dump_state **dump_, | |
ded30c74 | 2847 | const uint16_t *zone, int *ptot_bkts) |
c11c9f4a DDP |
2848 | { |
2849 | struct dpif_netlink_ct_dump_state *dump; | |
2850 | int err; | |
2851 | ||
2852 | dump = xzalloc(sizeof *dump); | |
ded30c74 | 2853 | err = nl_ct_dump_start(&dump->nl_ct_dump, zone, ptot_bkts); |
c11c9f4a DDP |
2854 | if (err) { |
2855 | free(dump); | |
2856 | return err; | |
2857 | } | |
2858 | ||
2859 | *dump_ = &dump->up; | |
2860 | ||
2861 | return 0; | |
2862 | } | |
2863 | ||
2864 | static int | |
2865 | dpif_netlink_ct_dump_next(struct dpif *dpif OVS_UNUSED, | |
2866 | struct ct_dpif_dump_state *dump_, | |
2867 | struct ct_dpif_entry *entry) | |
2868 | { | |
2869 | struct dpif_netlink_ct_dump_state *dump; | |
2870 | ||
2871 | INIT_CONTAINER(dump, dump_, up); | |
2872 | ||
2873 | return nl_ct_dump_next(dump->nl_ct_dump, entry); | |
2874 | } | |
2875 | ||
2876 | static int | |
2877 | dpif_netlink_ct_dump_done(struct dpif *dpif OVS_UNUSED, | |
2878 | struct ct_dpif_dump_state *dump_) | |
2879 | { | |
2880 | struct dpif_netlink_ct_dump_state *dump; | |
c11c9f4a DDP |
2881 | |
2882 | INIT_CONTAINER(dump, dump_, up); | |
2883 | ||
1f161318 | 2884 | int err = nl_ct_dump_done(dump->nl_ct_dump); |
c11c9f4a DDP |
2885 | free(dump); |
2886 | return err; | |
2887 | } | |
15eabc97 DDP |
2888 | |
2889 | static int | |
817a7657 YHW |
2890 | dpif_netlink_ct_flush(struct dpif *dpif OVS_UNUSED, const uint16_t *zone, |
2891 | const struct ct_dpif_tuple *tuple) | |
15eabc97 | 2892 | { |
817a7657 YHW |
2893 | if (tuple) { |
2894 | return nl_ct_flush_tuple(tuple, zone ? *zone : 0); | |
2895 | } else if (zone) { | |
15eabc97 DDP |
2896 | return nl_ct_flush_zone(*zone); |
2897 | } else { | |
2898 | return nl_ct_flush(); | |
2899 | } | |
2900 | } | |
c11c9f4a | 2901 | |
906ff9d2 YHW |
2902 | static int |
2903 | dpif_netlink_ct_set_limits(struct dpif *dpif OVS_UNUSED, | |
2904 | const uint32_t *default_limits, | |
2905 | const struct ovs_list *zone_limits) | |
2906 | { | |
2907 | struct ovs_zone_limit req_zone_limit; | |
2908 | ||
2909 | if (ovs_ct_limit_family < 0) { | |
2910 | return EOPNOTSUPP; | |
2911 | } | |
2912 | ||
2913 | struct ofpbuf *request = ofpbuf_new(NL_DUMP_BUFSIZE); | |
2914 | nl_msg_put_genlmsghdr(request, 0, ovs_ct_limit_family, | |
2915 | NLM_F_REQUEST | NLM_F_ECHO, OVS_CT_LIMIT_CMD_SET, | |
2916 | OVS_CT_LIMIT_VERSION); | |
2917 | ||
2918 | struct ovs_header *ovs_header; | |
2919 | ovs_header = ofpbuf_put_uninit(request, sizeof *ovs_header); | |
2920 | ovs_header->dp_ifindex = 0; | |
2921 | ||
2922 | size_t opt_offset; | |
2923 | opt_offset = nl_msg_start_nested(request, OVS_CT_LIMIT_ATTR_ZONE_LIMIT); | |
2924 | if (default_limits) { | |
2925 | req_zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE; | |
2926 | req_zone_limit.limit = *default_limits; | |
2927 | nl_msg_put(request, &req_zone_limit, sizeof req_zone_limit); | |
2928 | } | |
2929 | ||
2930 | if (!ovs_list_is_empty(zone_limits)) { | |
2931 | struct ct_dpif_zone_limit *zone_limit; | |
2932 | ||
2933 | LIST_FOR_EACH (zone_limit, node, zone_limits) { | |
2934 | req_zone_limit.zone_id = zone_limit->zone; | |
2935 | req_zone_limit.limit = zone_limit->limit; | |
2936 | nl_msg_put(request, &req_zone_limit, sizeof req_zone_limit); | |
2937 | } | |
2938 | } | |
2939 | nl_msg_end_nested(request, opt_offset); | |
2940 | ||
2941 | int err = nl_transact(NETLINK_GENERIC, request, NULL); | |
c225ce22 | 2942 | ofpbuf_delete(request); |
906ff9d2 YHW |
2943 | return err; |
2944 | } | |
2945 | ||
2946 | static int | |
2947 | dpif_netlink_zone_limits_from_ofpbuf(const struct ofpbuf *buf, | |
2948 | uint32_t *default_limit, | |
2949 | struct ovs_list *zone_limits) | |
2950 | { | |
2951 | static const struct nl_policy ovs_ct_limit_policy[] = { | |
2952 | [OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NL_A_NESTED, | |
2953 | .optional = true }, | |
2954 | }; | |
2955 | ||
2956 | struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size); | |
2957 | struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg); | |
2958 | struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl); | |
2959 | struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header); | |
2960 | ||
2961 | struct nlattr *attr[ARRAY_SIZE(ovs_ct_limit_policy)]; | |
2962 | ||
2963 | if (!nlmsg || !genl || !ovs_header | |
2964 | || nlmsg->nlmsg_type != ovs_ct_limit_family | |
2965 | || !nl_policy_parse(&b, 0, ovs_ct_limit_policy, attr, | |
2966 | ARRAY_SIZE(ovs_ct_limit_policy))) { | |
2967 | return EINVAL; | |
2968 | } | |
2969 | ||
2970 | ||
2971 | if (!attr[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) { | |
2972 | return EINVAL; | |
2973 | } | |
2974 | ||
2975 | int rem = NLA_ALIGN( | |
2976 | nl_attr_get_size(attr[OVS_CT_LIMIT_ATTR_ZONE_LIMIT])); | |
2977 | const struct ovs_zone_limit *zone_limit = | |
2978 | nl_attr_get(attr[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]); | |
2979 | ||
2980 | while (rem >= sizeof *zone_limit) { | |
2981 | if (zone_limit->zone_id == OVS_ZONE_LIMIT_DEFAULT_ZONE) { | |
2982 | *default_limit = zone_limit->limit; | |
2983 | } else if (zone_limit->zone_id < OVS_ZONE_LIMIT_DEFAULT_ZONE || | |
2984 | zone_limit->zone_id > UINT16_MAX) { | |
2985 | } else { | |
2986 | ct_dpif_push_zone_limit(zone_limits, zone_limit->zone_id, | |
2987 | zone_limit->limit, zone_limit->count); | |
2988 | } | |
2989 | rem -= NLA_ALIGN(sizeof *zone_limit); | |
2990 | zone_limit = ALIGNED_CAST(struct ovs_zone_limit *, | |
2991 | (unsigned char *) zone_limit + NLA_ALIGN(sizeof *zone_limit)); | |
2992 | } | |
2993 | return 0; | |
2994 | } | |
2995 | ||
2996 | static int | |
2997 | dpif_netlink_ct_get_limits(struct dpif *dpif OVS_UNUSED, | |
2998 | uint32_t *default_limit, | |
2999 | const struct ovs_list *zone_limits_request, | |
3000 | struct ovs_list *zone_limits_reply) | |
3001 | { | |
3002 | if (ovs_ct_limit_family < 0) { | |
3003 | return EOPNOTSUPP; | |
3004 | } | |
3005 | ||
3006 | struct ofpbuf *request = ofpbuf_new(NL_DUMP_BUFSIZE); | |
3007 | nl_msg_put_genlmsghdr(request, 0, ovs_ct_limit_family, | |
3008 | NLM_F_REQUEST | NLM_F_ECHO, OVS_CT_LIMIT_CMD_GET, | |
3009 | OVS_CT_LIMIT_VERSION); | |
3010 | ||
3011 | struct ovs_header *ovs_header; | |
3012 | ovs_header = ofpbuf_put_uninit(request, sizeof *ovs_header); | |
3013 | ovs_header->dp_ifindex = 0; | |
3014 | ||
3015 | if (!ovs_list_is_empty(zone_limits_request)) { | |
3016 | size_t opt_offset = nl_msg_start_nested(request, | |
3017 | OVS_CT_LIMIT_ATTR_ZONE_LIMIT); | |
3018 | ||
3019 | struct ovs_zone_limit req_zone_limit; | |
3020 | req_zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE; | |
3021 | nl_msg_put(request, &req_zone_limit, sizeof req_zone_limit); | |
3022 | ||
3023 | struct ct_dpif_zone_limit *zone_limit; | |
3024 | LIST_FOR_EACH (zone_limit, node, zone_limits_request) { | |
3025 | req_zone_limit.zone_id = zone_limit->zone; | |
3026 | nl_msg_put(request, &req_zone_limit, sizeof req_zone_limit); | |
3027 | } | |
3028 | ||
3029 | nl_msg_end_nested(request, opt_offset); | |
3030 | } | |
3031 | ||
3032 | struct ofpbuf *reply; | |
3033 | int err = nl_transact(NETLINK_GENERIC, request, &reply); | |
3034 | if (err) { | |
3035 | goto out; | |
3036 | } | |
3037 | ||
3038 | err = dpif_netlink_zone_limits_from_ofpbuf(reply, default_limit, | |
3039 | zone_limits_reply); | |
3040 | ||
3041 | out: | |
c225ce22 YS |
3042 | ofpbuf_delete(request); |
3043 | ofpbuf_delete(reply); | |
906ff9d2 YHW |
3044 | return err; |
3045 | } | |
3046 | ||
3047 | static int | |
3048 | dpif_netlink_ct_del_limits(struct dpif *dpif OVS_UNUSED, | |
3049 | const struct ovs_list *zone_limits) | |
3050 | { | |
3051 | if (ovs_ct_limit_family < 0) { | |
3052 | return EOPNOTSUPP; | |
3053 | } | |
3054 | ||
3055 | struct ofpbuf *request = ofpbuf_new(NL_DUMP_BUFSIZE); | |
3056 | nl_msg_put_genlmsghdr(request, 0, ovs_ct_limit_family, | |
3057 | NLM_F_REQUEST | NLM_F_ECHO, OVS_CT_LIMIT_CMD_DEL, | |
3058 | OVS_CT_LIMIT_VERSION); | |
3059 | ||
3060 | struct ovs_header *ovs_header; | |
3061 | ovs_header = ofpbuf_put_uninit(request, sizeof *ovs_header); | |
3062 | ovs_header->dp_ifindex = 0; | |
3063 | ||
3064 | if (!ovs_list_is_empty(zone_limits)) { | |
3065 | size_t opt_offset = | |
3066 | nl_msg_start_nested(request, OVS_CT_LIMIT_ATTR_ZONE_LIMIT); | |
3067 | ||
3068 | struct ct_dpif_zone_limit *zone_limit; | |
3069 | LIST_FOR_EACH (zone_limit, node, zone_limits) { | |
3070 | struct ovs_zone_limit req_zone_limit; | |
3071 | req_zone_limit.zone_id = zone_limit->zone; | |
3072 | nl_msg_put(request, &req_zone_limit, sizeof req_zone_limit); | |
3073 | } | |
3074 | nl_msg_end_nested(request, opt_offset); | |
3075 | } | |
3076 | ||
3077 | int err = nl_transact(NETLINK_GENERIC, request, NULL); | |
3078 | ||
c225ce22 | 3079 | ofpbuf_delete(request); |
906ff9d2 YHW |
3080 | return err; |
3081 | } | |
1f161318 YHW |
3082 | |
3083 | #define NL_TP_NAME_PREFIX "ovs_tp_" | |
3084 | ||
3085 | struct dpif_netlink_timeout_policy_protocol { | |
3086 | uint16_t l3num; | |
3087 | uint8_t l4num; | |
3088 | }; | |
3089 | ||
3090 | enum OVS_PACKED_ENUM dpif_netlink_support_timeout_policy_protocol { | |
3091 | DPIF_NL_TP_AF_INET_TCP, | |
3092 | DPIF_NL_TP_AF_INET_UDP, | |
3093 | DPIF_NL_TP_AF_INET_ICMP, | |
3094 | DPIF_NL_TP_AF_INET6_TCP, | |
3095 | DPIF_NL_TP_AF_INET6_UDP, | |
3096 | DPIF_NL_TP_AF_INET6_ICMPV6, | |
3097 | DPIF_NL_TP_MAX | |
3098 | }; | |
3099 | ||
3100 | #define DPIF_NL_ALL_TP ((1UL << DPIF_NL_TP_MAX) - 1) | |
3101 | ||
3102 | ||
3103 | static struct dpif_netlink_timeout_policy_protocol tp_protos[] = { | |
3104 | [DPIF_NL_TP_AF_INET_TCP] = { .l3num = AF_INET, .l4num = IPPROTO_TCP }, | |
3105 | [DPIF_NL_TP_AF_INET_UDP] = { .l3num = AF_INET, .l4num = IPPROTO_UDP }, | |
3106 | [DPIF_NL_TP_AF_INET_ICMP] = { .l3num = AF_INET, .l4num = IPPROTO_ICMP }, | |
3107 | [DPIF_NL_TP_AF_INET6_TCP] = { .l3num = AF_INET6, .l4num = IPPROTO_TCP }, | |
3108 | [DPIF_NL_TP_AF_INET6_UDP] = { .l3num = AF_INET6, .l4num = IPPROTO_UDP }, | |
3109 | [DPIF_NL_TP_AF_INET6_ICMPV6] = { .l3num = AF_INET6, | |
3110 | .l4num = IPPROTO_ICMPV6 }, | |
3111 | }; | |
3112 | ||
3113 | static void | |
3114 | dpif_netlink_format_tp_name(uint32_t id, uint16_t l3num, uint8_t l4num, | |
187bb41f | 3115 | char **tp_name) |
1f161318 | 3116 | { |
187bb41f YHW |
3117 | struct ds ds = DS_EMPTY_INITIALIZER; |
3118 | ds_put_format(&ds, "%s%"PRIu32"_", NL_TP_NAME_PREFIX, id); | |
3119 | ct_dpif_format_ipproto(&ds, l4num); | |
1f161318 YHW |
3120 | |
3121 | if (l3num == AF_INET) { | |
187bb41f | 3122 | ds_put_cstr(&ds, "4"); |
1f161318 | 3123 | } else if (l3num == AF_INET6 && l4num != IPPROTO_ICMPV6) { |
187bb41f | 3124 | ds_put_cstr(&ds, "6"); |
1f161318 YHW |
3125 | } |
3126 | ||
187bb41f YHW |
3127 | ovs_assert(ds.length < CTNL_TIMEOUT_NAME_MAX); |
3128 | ||
3129 | *tp_name = ds_steal_cstr(&ds); | |
3130 | } | |
3131 | ||
3132 | static int | |
3133 | dpif_netlink_ct_get_timeout_policy_name(struct dpif *dpif OVS_UNUSED, | |
3134 | uint32_t tp_id, uint16_t dl_type, | |
3135 | uint8_t nw_proto, char **tp_name, | |
3136 | bool *is_generic) | |
3137 | { | |
3138 | dpif_netlink_format_tp_name(tp_id, | |
3139 | dl_type == ETH_TYPE_IP ? AF_INET : AF_INET6, | |
3140 | nw_proto, tp_name); | |
3141 | *is_generic = false; | |
3142 | return 0; | |
1f161318 YHW |
3143 | } |
3144 | ||
3145 | #define CT_DPIF_NL_TP_TCP_MAPPINGS \ | |
3146 | CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_SENT, SYN_SENT) \ | |
3147 | CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_RECV, SYN_RECV) \ | |
3148 | CT_DPIF_NL_TP_MAPPING(TCP, TCP, ESTABLISHED, ESTABLISHED) \ | |
3149 | CT_DPIF_NL_TP_MAPPING(TCP, TCP, FIN_WAIT, FIN_WAIT) \ | |
3150 | CT_DPIF_NL_TP_MAPPING(TCP, TCP, CLOSE_WAIT, CLOSE_WAIT) \ | |
3151 | CT_DPIF_NL_TP_MAPPING(TCP, TCP, LAST_ACK, LAST_ACK) \ | |
3152 | CT_DPIF_NL_TP_MAPPING(TCP, TCP, TIME_WAIT, TIME_WAIT) \ | |
3153 | CT_DPIF_NL_TP_MAPPING(TCP, TCP, CLOSE, CLOSE) \ | |
3154 | CT_DPIF_NL_TP_MAPPING(TCP, TCP, SYN_SENT2, SYN_SENT2) \ | |
3155 | CT_DPIF_NL_TP_MAPPING(TCP, TCP, RETRANSMIT, RETRANS) \ | |
3156 | CT_DPIF_NL_TP_MAPPING(TCP, TCP, UNACK, UNACK) | |
3157 | ||
3158 | #define CT_DPIF_NL_TP_UDP_MAPPINGS \ | |
3159 | CT_DPIF_NL_TP_MAPPING(UDP, UDP, SINGLE, UNREPLIED) \ | |
3160 | CT_DPIF_NL_TP_MAPPING(UDP, UDP, MULTIPLE, REPLIED) | |
3161 | ||
3162 | #define CT_DPIF_NL_TP_ICMP_MAPPINGS \ | |
3163 | CT_DPIF_NL_TP_MAPPING(ICMP, ICMP, FIRST, TIMEOUT) | |
3164 | ||
3165 | #define CT_DPIF_NL_TP_ICMPV6_MAPPINGS \ | |
3166 | CT_DPIF_NL_TP_MAPPING(ICMP, ICMPV6, FIRST, TIMEOUT) | |
3167 | ||
3168 | ||
3169 | #define CT_DPIF_NL_TP_MAPPING(PROTO1, PROTO2, ATTR1, ATTR2) \ | |
3170 | if (tp->present & (1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1)) { \ | |
3171 | nl_tp->present |= 1 << CTA_TIMEOUT_##PROTO2##_##ATTR2; \ | |
3172 | nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2] = \ | |
3173 | tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1]; \ | |
3174 | } | |
3175 | ||
3176 | static void | |
3177 | dpif_netlink_get_nl_tp_tcp_attrs(const struct ct_dpif_timeout_policy *tp, | |
3178 | struct nl_ct_timeout_policy *nl_tp) | |
3179 | { | |
3180 | CT_DPIF_NL_TP_TCP_MAPPINGS | |
3181 | } | |
3182 | ||
3183 | static void | |
3184 | dpif_netlink_get_nl_tp_udp_attrs(const struct ct_dpif_timeout_policy *tp, | |
3185 | struct nl_ct_timeout_policy *nl_tp) | |
3186 | { | |
3187 | CT_DPIF_NL_TP_UDP_MAPPINGS | |
3188 | } | |
3189 | ||
3190 | static void | |
3191 | dpif_netlink_get_nl_tp_icmp_attrs(const struct ct_dpif_timeout_policy *tp, | |
3192 | struct nl_ct_timeout_policy *nl_tp) | |
3193 | { | |
3194 | CT_DPIF_NL_TP_ICMP_MAPPINGS | |
3195 | } | |
3196 | ||
3197 | static void | |
3198 | dpif_netlink_get_nl_tp_icmpv6_attrs(const struct ct_dpif_timeout_policy *tp, | |
3199 | struct nl_ct_timeout_policy *nl_tp) | |
3200 | { | |
3201 | CT_DPIF_NL_TP_ICMPV6_MAPPINGS | |
3202 | } | |
3203 | ||
3204 | #undef CT_DPIF_NL_TP_MAPPING | |
3205 | ||
3206 | static void | |
3207 | dpif_netlink_get_nl_tp_attrs(const struct ct_dpif_timeout_policy *tp, | |
3208 | uint8_t l4num, struct nl_ct_timeout_policy *nl_tp) | |
3209 | { | |
3210 | nl_tp->present = 0; | |
3211 | ||
3212 | if (l4num == IPPROTO_TCP) { | |
3213 | dpif_netlink_get_nl_tp_tcp_attrs(tp, nl_tp); | |
3214 | } else if (l4num == IPPROTO_UDP) { | |
3215 | dpif_netlink_get_nl_tp_udp_attrs(tp, nl_tp); | |
3216 | } else if (l4num == IPPROTO_ICMP) { | |
3217 | dpif_netlink_get_nl_tp_icmp_attrs(tp, nl_tp); | |
3218 | } else if (l4num == IPPROTO_ICMPV6) { | |
3219 | dpif_netlink_get_nl_tp_icmpv6_attrs(tp, nl_tp); | |
3220 | } | |
3221 | } | |
3222 | ||
3223 | #define CT_DPIF_NL_TP_MAPPING(PROTO1, PROTO2, ATTR1, ATTR2) \ | |
3224 | if (nl_tp->present & (1 << CTA_TIMEOUT_##PROTO2##_##ATTR2)) { \ | |
3225 | if (tp->present & (1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1)) { \ | |
3226 | if (tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1] != \ | |
3227 | nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2]) { \ | |
3228 | VLOG_WARN_RL(&error_rl, "Inconsistent timeout policy %s " \ | |
3229 | "attribute %s=%"PRIu32" while %s=%"PRIu32, \ | |
3230 | nl_tp->name, "CTA_TIMEOUT_"#PROTO2"_"#ATTR2, \ | |
3231 | nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2], \ | |
3232 | "CT_DPIF_TP_ATTR_"#PROTO1"_"#ATTR1, \ | |
3233 | tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1]); \ | |
3234 | } \ | |
3235 | } else { \ | |
3236 | tp->present |= 1 << CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1; \ | |
3237 | tp->attrs[CT_DPIF_TP_ATTR_##PROTO1##_##ATTR1] = \ | |
3238 | nl_tp->attrs[CTA_TIMEOUT_##PROTO2##_##ATTR2]; \ | |
3239 | } \ | |
3240 | } | |
3241 | ||
3242 | static void | |
3243 | dpif_netlink_set_ct_dpif_tp_tcp_attrs(const struct nl_ct_timeout_policy *nl_tp, | |
3244 | struct ct_dpif_timeout_policy *tp) | |
3245 | { | |
3246 | CT_DPIF_NL_TP_TCP_MAPPINGS | |
3247 | } | |
3248 | ||
3249 | static void | |
3250 | dpif_netlink_set_ct_dpif_tp_udp_attrs(const struct nl_ct_timeout_policy *nl_tp, | |
3251 | struct ct_dpif_timeout_policy *tp) | |
3252 | { | |
3253 | CT_DPIF_NL_TP_UDP_MAPPINGS | |
3254 | } | |
3255 | ||
3256 | static void | |
3257 | dpif_netlink_set_ct_dpif_tp_icmp_attrs( | |
3258 | const struct nl_ct_timeout_policy *nl_tp, | |
3259 | struct ct_dpif_timeout_policy *tp) | |
3260 | { | |
3261 | CT_DPIF_NL_TP_ICMP_MAPPINGS | |
3262 | } | |
3263 | ||
3264 | static void | |
3265 | dpif_netlink_set_ct_dpif_tp_icmpv6_attrs( | |
3266 | const struct nl_ct_timeout_policy *nl_tp, | |
3267 | struct ct_dpif_timeout_policy *tp) | |
3268 | { | |
3269 | CT_DPIF_NL_TP_ICMPV6_MAPPINGS | |
3270 | } | |
3271 | ||
3272 | #undef CT_DPIF_NL_TP_MAPPING | |
3273 | ||
3274 | static void | |
3275 | dpif_netlink_set_ct_dpif_tp_attrs(const struct nl_ct_timeout_policy *nl_tp, | |
3276 | struct ct_dpif_timeout_policy *tp) | |
3277 | { | |
3278 | if (nl_tp->l4num == IPPROTO_TCP) { | |
3279 | dpif_netlink_set_ct_dpif_tp_tcp_attrs(nl_tp, tp); | |
3280 | } else if (nl_tp->l4num == IPPROTO_UDP) { | |
3281 | dpif_netlink_set_ct_dpif_tp_udp_attrs(nl_tp, tp); | |
3282 | } else if (nl_tp->l4num == IPPROTO_ICMP) { | |
3283 | dpif_netlink_set_ct_dpif_tp_icmp_attrs(nl_tp, tp); | |
3284 | } else if (nl_tp->l4num == IPPROTO_ICMPV6) { | |
3285 | dpif_netlink_set_ct_dpif_tp_icmpv6_attrs(nl_tp, tp); | |
3286 | } | |
3287 | } | |
3288 | ||
3289 | #ifdef _WIN32 | |
3290 | static int | |
3291 | dpif_netlink_ct_set_timeout_policy(struct dpif *dpif OVS_UNUSED, | |
3292 | const struct ct_dpif_timeout_policy *tp) | |
3293 | { | |
3294 | return EOPNOTSUPP; | |
3295 | } | |
3296 | ||
3297 | static int | |
3298 | dpif_netlink_ct_get_timeout_policy(struct dpif *dpif OVS_UNUSED, | |
3299 | uint32_t tp_id, | |
3300 | struct ct_dpif_timeout_policy *tp) | |
3301 | { | |
3302 | return EOPNOTSUPP; | |
3303 | } | |
3304 | ||
3305 | static int | |
3306 | dpif_netlink_ct_del_timeout_policy(struct dpif *dpif OVS_UNUSED, | |
3307 | uint32_t tp_id) | |
3308 | { | |
3309 | return EOPNOTSUPP; | |
3310 | } | |
3311 | ||
3312 | static int | |
3313 | dpif_netlink_ct_timeout_policy_dump_start(struct dpif *dpif OVS_UNUSED, | |
3314 | void **statep) | |
3315 | { | |
3316 | return EOPNOTSUPP; | |
3317 | } | |
3318 | ||
3319 | static int | |
3320 | dpif_netlink_ct_timeout_policy_dump_next(struct dpif *dpif OVS_UNUSED, | |
3321 | void *state, | |
3322 | struct ct_dpif_timeout_policy **tp) | |
3323 | { | |
3324 | return EOPNOTSUPP; | |
3325 | } | |
3326 | ||
3327 | static int | |
3328 | dpif_netlink_ct_timeout_policy_dump_done(struct dpif *dpif OVS_UNUSED, | |
3329 | void *state) | |
3330 | { | |
3331 | return EOPNOTSUPP; | |
3332 | } | |
3333 | #else | |
3334 | static int | |
3335 | dpif_netlink_ct_set_timeout_policy(struct dpif *dpif OVS_UNUSED, | |
3336 | const struct ct_dpif_timeout_policy *tp) | |
3337 | { | |
1f161318 YHW |
3338 | int err = 0; |
3339 | ||
3340 | for (int i = 0; i < ARRAY_SIZE(tp_protos); ++i) { | |
187bb41f YHW |
3341 | struct nl_ct_timeout_policy nl_tp; |
3342 | char *nl_tp_name; | |
3343 | ||
1f161318 YHW |
3344 | dpif_netlink_format_tp_name(tp->id, tp_protos[i].l3num, |
3345 | tp_protos[i].l4num, &nl_tp_name); | |
187bb41f YHW |
3346 | ovs_strlcpy(nl_tp.name, nl_tp_name, sizeof nl_tp.name); |
3347 | free(nl_tp_name); | |
3348 | ||
1f161318 YHW |
3349 | nl_tp.l3num = tp_protos[i].l3num; |
3350 | nl_tp.l4num = tp_protos[i].l4num; | |
3351 | dpif_netlink_get_nl_tp_attrs(tp, tp_protos[i].l4num, &nl_tp); | |
3352 | err = nl_ct_set_timeout_policy(&nl_tp); | |
3353 | if (err) { | |
3354 | VLOG_WARN_RL(&error_rl, "failed to add timeout policy %s (%s)", | |
3355 | nl_tp.name, ovs_strerror(err)); | |
3356 | goto out; | |
3357 | } | |
3358 | } | |
3359 | ||
3360 | out: | |
1f161318 YHW |
3361 | return err; |
3362 | } | |
3363 | ||
3364 | static int | |
3365 | dpif_netlink_ct_get_timeout_policy(struct dpif *dpif OVS_UNUSED, | |
3366 | uint32_t tp_id, | |
3367 | struct ct_dpif_timeout_policy *tp) | |
3368 | { | |
1f161318 YHW |
3369 | int err = 0; |
3370 | ||
3371 | tp->id = tp_id; | |
3372 | tp->present = 0; | |
3373 | for (int i = 0; i < ARRAY_SIZE(tp_protos); ++i) { | |
187bb41f YHW |
3374 | struct nl_ct_timeout_policy nl_tp; |
3375 | char *nl_tp_name; | |
3376 | ||
1f161318 YHW |
3377 | dpif_netlink_format_tp_name(tp_id, tp_protos[i].l3num, |
3378 | tp_protos[i].l4num, &nl_tp_name); | |
187bb41f | 3379 | err = nl_ct_get_timeout_policy(nl_tp_name, &nl_tp); |
1f161318 YHW |
3380 | |
3381 | if (err) { | |
3382 | VLOG_WARN_RL(&error_rl, "failed to get timeout policy %s (%s)", | |
187bb41f YHW |
3383 | nl_tp_name, ovs_strerror(err)); |
3384 | free(nl_tp_name); | |
1f161318 YHW |
3385 | goto out; |
3386 | } | |
187bb41f | 3387 | free(nl_tp_name); |
1f161318 YHW |
3388 | dpif_netlink_set_ct_dpif_tp_attrs(&nl_tp, tp); |
3389 | } | |
3390 | ||
3391 | out: | |
1f161318 YHW |
3392 | return err; |
3393 | } | |
3394 | ||
3395 | /* Returns 0 if all the sub timeout policies are deleted or not exist in the | |
3396 | * kernel. Returns 1 if any sub timeout policy deletion failed. */ | |
3397 | static int | |
3398 | dpif_netlink_ct_del_timeout_policy(struct dpif *dpif OVS_UNUSED, | |
3399 | uint32_t tp_id) | |
3400 | { | |
1f161318 YHW |
3401 | int ret = 0; |
3402 | ||
3403 | for (int i = 0; i < ARRAY_SIZE(tp_protos); ++i) { | |
187bb41f | 3404 | char *nl_tp_name; |
1f161318 YHW |
3405 | dpif_netlink_format_tp_name(tp_id, tp_protos[i].l3num, |
3406 | tp_protos[i].l4num, &nl_tp_name); | |
187bb41f | 3407 | int err = nl_ct_del_timeout_policy(nl_tp_name); |
1f161318 YHW |
3408 | if (err == ENOENT) { |
3409 | err = 0; | |
3410 | } | |
3411 | if (err) { | |
3412 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(6, 6); | |
3413 | VLOG_INFO_RL(&rl, "failed to delete timeout policy %s (%s)", | |
187bb41f | 3414 | nl_tp_name, ovs_strerror(err)); |
1f161318 YHW |
3415 | ret = 1; |
3416 | } | |
187bb41f | 3417 | free(nl_tp_name); |
1f161318 YHW |
3418 | } |
3419 | ||
1f161318 YHW |
3420 | return ret; |
3421 | } | |
3422 | ||
3423 | struct dpif_netlink_ct_timeout_policy_dump_state { | |
3424 | struct nl_ct_timeout_policy_dump_state *nl_dump_state; | |
3425 | struct hmap tp_dump_map; | |
3426 | }; | |
3427 | ||
3428 | struct dpif_netlink_tp_dump_node { | |
3429 | struct hmap_node hmap_node; /* node in tp_dump_map. */ | |
3430 | struct ct_dpif_timeout_policy *tp; | |
3431 | uint32_t l3_l4_present; | |
3432 | }; | |
3433 | ||
3434 | static struct dpif_netlink_tp_dump_node * | |
3435 | get_dpif_netlink_tp_dump_node_by_tp_id(uint32_t tp_id, | |
3436 | struct hmap *tp_dump_map) | |
3437 | { | |
3438 | struct dpif_netlink_tp_dump_node *tp_dump_node; | |
3439 | ||
3440 | HMAP_FOR_EACH_WITH_HASH (tp_dump_node, hmap_node, hash_int(tp_id, 0), | |
3441 | tp_dump_map) { | |
3442 | if (tp_dump_node->tp->id == tp_id) { | |
3443 | return tp_dump_node; | |
3444 | } | |
3445 | } | |
3446 | return NULL; | |
3447 | } | |
3448 | ||
3449 | static void | |
3450 | update_dpif_netlink_tp_dump_node( | |
3451 | const struct nl_ct_timeout_policy *nl_tp, | |
3452 | struct dpif_netlink_tp_dump_node *tp_dump_node) | |
3453 | { | |
3454 | dpif_netlink_set_ct_dpif_tp_attrs(nl_tp, tp_dump_node->tp); | |
3455 | for (int i = 0; i < DPIF_NL_TP_MAX; ++i) { | |
3456 | if (nl_tp->l3num == tp_protos[i].l3num && | |
3457 | nl_tp->l4num == tp_protos[i].l4num) { | |
3458 | tp_dump_node->l3_l4_present |= 1 << i; | |
3459 | break; | |
3460 | } | |
3461 | } | |
3462 | } | |
3463 | ||
3464 | static int | |
3465 | dpif_netlink_ct_timeout_policy_dump_start(struct dpif *dpif OVS_UNUSED, | |
3466 | void **statep) | |
3467 | { | |
3468 | struct dpif_netlink_ct_timeout_policy_dump_state *dump_state; | |
3469 | ||
3470 | *statep = dump_state = xzalloc(sizeof *dump_state); | |
3471 | int err = nl_ct_timeout_policy_dump_start(&dump_state->nl_dump_state); | |
3472 | if (err) { | |
3473 | free(dump_state); | |
3474 | return err; | |
3475 | } | |
3476 | hmap_init(&dump_state->tp_dump_map); | |
3477 | return 0; | |
3478 | } | |
3479 | ||
3480 | static void | |
3481 | get_and_cleanup_tp_dump_node(struct hmap *hmap, | |
3482 | struct dpif_netlink_tp_dump_node *tp_dump_node, | |
3483 | struct ct_dpif_timeout_policy *tp) | |
3484 | { | |
3485 | hmap_remove(hmap, &tp_dump_node->hmap_node); | |
3486 | *tp = *tp_dump_node->tp; | |
3487 | free(tp_dump_node->tp); | |
3488 | free(tp_dump_node); | |
3489 | } | |
3490 | ||
3491 | static int | |
3492 | dpif_netlink_ct_timeout_policy_dump_next(struct dpif *dpif OVS_UNUSED, | |
3493 | void *state, | |
3494 | struct ct_dpif_timeout_policy *tp) | |
3495 | { | |
3496 | struct dpif_netlink_ct_timeout_policy_dump_state *dump_state = state; | |
3497 | struct dpif_netlink_tp_dump_node *tp_dump_node; | |
3498 | int err; | |
3499 | ||
3500 | /* Dumps all the timeout policies in the kernel. */ | |
3501 | do { | |
3502 | struct nl_ct_timeout_policy nl_tp; | |
3503 | uint32_t tp_id; | |
3504 | ||
3505 | err = nl_ct_timeout_policy_dump_next(dump_state->nl_dump_state, | |
3506 | &nl_tp); | |
3507 | if (err) { | |
3508 | break; | |
3509 | } | |
3510 | ||
3511 | /* We only interest in OVS installed timeout policies. */ | |
3512 | if (!ovs_scan(nl_tp.name, NL_TP_NAME_PREFIX"%"PRIu32, &tp_id)) { | |
3513 | continue; | |
3514 | } | |
3515 | ||
3516 | tp_dump_node = get_dpif_netlink_tp_dump_node_by_tp_id( | |
3517 | tp_id, &dump_state->tp_dump_map); | |
3518 | if (!tp_dump_node) { | |
3519 | tp_dump_node = xzalloc(sizeof *tp_dump_node); | |
3520 | tp_dump_node->tp = xzalloc(sizeof *tp_dump_node->tp); | |
3521 | tp_dump_node->tp->id = tp_id; | |
3522 | hmap_insert(&dump_state->tp_dump_map, &tp_dump_node->hmap_node, | |
3523 | hash_int(tp_id, 0)); | |
3524 | } | |
3525 | ||
3526 | update_dpif_netlink_tp_dump_node(&nl_tp, tp_dump_node); | |
3527 | ||
3528 | /* Returns one ct_dpif_timeout_policy if we gather all the L3/L4 | |
3529 | * sub-pieces. */ | |
3530 | if (tp_dump_node->l3_l4_present == DPIF_NL_ALL_TP) { | |
3531 | get_and_cleanup_tp_dump_node(&dump_state->tp_dump_map, | |
3532 | tp_dump_node, tp); | |
3533 | break; | |
3534 | } | |
3535 | } while (true); | |
3536 | ||
3537 | /* Dump the incomplete timeout policies. */ | |
3538 | if (err == EOF) { | |
3539 | if (!hmap_is_empty(&dump_state->tp_dump_map)) { | |
3540 | struct hmap_node *hmap_node = hmap_first(&dump_state->tp_dump_map); | |
3541 | tp_dump_node = CONTAINER_OF(hmap_node, | |
3542 | struct dpif_netlink_tp_dump_node, | |
3543 | hmap_node); | |
3544 | get_and_cleanup_tp_dump_node(&dump_state->tp_dump_map, | |
3545 | tp_dump_node, tp); | |
3546 | return 0; | |
3547 | } | |
3548 | } | |
3549 | ||
3550 | return err; | |
3551 | } | |
3552 | ||
3553 | static int | |
3554 | dpif_netlink_ct_timeout_policy_dump_done(struct dpif *dpif OVS_UNUSED, | |
3555 | void *state) | |
3556 | { | |
3557 | struct dpif_netlink_ct_timeout_policy_dump_state *dump_state = state; | |
3558 | struct dpif_netlink_tp_dump_node *tp_dump_node; | |
3559 | ||
3560 | int err = nl_ct_timeout_policy_dump_done(dump_state->nl_dump_state); | |
3561 | HMAP_FOR_EACH_POP (tp_dump_node, hmap_node, &dump_state->tp_dump_map) { | |
3562 | free(tp_dump_node->tp); | |
3563 | free(tp_dump_node); | |
3564 | } | |
3565 | hmap_destroy(&dump_state->tp_dump_map); | |
3566 | free(dump_state); | |
3567 | return err; | |
3568 | } | |
3569 | #endif | |
3570 | ||
5dddf960 JR |
3571 | \f |
3572 | /* Meters */ | |
80738e5f AZ |
3573 | |
3574 | /* Set of supported meter flags */ | |
3575 | #define DP_SUPPORTED_METER_FLAGS_MASK \ | |
3576 | (OFPMF13_STATS | OFPMF13_PKTPS | OFPMF13_KBPS | OFPMF13_BURST) | |
3577 | ||
92d0d515 JP |
3578 | /* Meter support was introduced in Linux 4.15. In some versions of |
3579 | * Linux 4.15, 4.16, and 4.17, there was a bug that never set the id | |
3580 | * when the meter was created, so all meters essentially had an id of | |
3581 | * zero. Check for that condition and disable meters on those kernels. */ | |
3582 | static bool probe_broken_meters(struct dpif *); | |
3583 | ||
5dddf960 | 3584 | static void |
80738e5f AZ |
3585 | dpif_netlink_meter_init(struct dpif_netlink *dpif, struct ofpbuf *buf, |
3586 | void *stub, size_t size, uint32_t command) | |
3587 | { | |
3588 | ofpbuf_use_stub(buf, stub, size); | |
3589 | ||
3590 | nl_msg_put_genlmsghdr(buf, 0, ovs_meter_family, NLM_F_REQUEST | NLM_F_ECHO, | |
3591 | command, OVS_METER_VERSION); | |
3592 | ||
3593 | struct ovs_header *ovs_header; | |
3594 | ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header); | |
3595 | ovs_header->dp_ifindex = dpif->dp_ifindex; | |
3596 | } | |
3597 | ||
3598 | /* Execute meter 'request' in the kernel datapath. If the command | |
3599 | * fails, returns a positive errno value. Otherwise, stores the reply | |
3600 | * in '*replyp', parses the policy according to 'reply_policy' into the | |
3601 | * array of Netlink attribute in 'a', and returns 0. On success, the | |
3602 | * caller is responsible for calling ofpbuf_delete() on '*replyp' | |
3603 | * ('replyp' will contain pointers into 'a'). */ | |
3604 | static int | |
3605 | dpif_netlink_meter_transact(struct ofpbuf *request, struct ofpbuf **replyp, | |
3606 | const struct nl_policy *reply_policy, | |
3607 | struct nlattr **a, size_t size_a) | |
3608 | { | |
3609 | int error = nl_transact(NETLINK_GENERIC, request, replyp); | |
3610 | ofpbuf_uninit(request); | |
3611 | ||
3612 | if (error) { | |
3613 | return error; | |
3614 | } | |
3615 | ||
3616 | struct nlmsghdr *nlmsg = ofpbuf_try_pull(*replyp, sizeof *nlmsg); | |
3617 | struct genlmsghdr *genl = ofpbuf_try_pull(*replyp, sizeof *genl); | |
3618 | struct ovs_header *ovs_header = ofpbuf_try_pull(*replyp, | |
3619 | sizeof *ovs_header); | |
3620 | if (!nlmsg || !genl || !ovs_header | |
3621 | || nlmsg->nlmsg_type != ovs_meter_family | |
3622 | || !nl_policy_parse(*replyp, 0, reply_policy, a, size_a)) { | |
3623 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
3624 | VLOG_DBG_RL(&rl, | |
3625 | "Kernel module response to meter tranaction is invalid"); | |
3626 | return EINVAL; | |
3627 | } | |
3628 | return 0; | |
3629 | } | |
3630 | ||
3631 | static void | |
3632 | dpif_netlink_meter_get_features(const struct dpif *dpif_, | |
5dddf960 JR |
3633 | struct ofputil_meter_features *features) |
3634 | { | |
92d0d515 JP |
3635 | if (probe_broken_meters(CONST_CAST(struct dpif *, dpif_))) { |
3636 | features = NULL; | |
3637 | return; | |
3638 | } | |
3639 | ||
80738e5f AZ |
3640 | struct ofpbuf buf, *msg; |
3641 | uint64_t stub[1024 / 8]; | |
3642 | ||
3643 | static const struct nl_policy ovs_meter_features_policy[] = { | |
3644 | [OVS_METER_ATTR_MAX_METERS] = { .type = NL_A_U32 }, | |
3645 | [OVS_METER_ATTR_MAX_BANDS] = { .type = NL_A_U32 }, | |
3646 | [OVS_METER_ATTR_BANDS] = { .type = NL_A_NESTED, .optional = true }, | |
3647 | }; | |
3648 | struct nlattr *a[ARRAY_SIZE(ovs_meter_features_policy)]; | |
3649 | ||
3650 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); | |
3651 | dpif_netlink_meter_init(dpif, &buf, stub, sizeof stub, | |
3652 | OVS_METER_CMD_FEATURES); | |
3653 | if (dpif_netlink_meter_transact(&buf, &msg, ovs_meter_features_policy, a, | |
3654 | ARRAY_SIZE(ovs_meter_features_policy))) { | |
3655 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
3656 | VLOG_INFO_RL(&rl, | |
3657 | "dpif_netlink_meter_transact OVS_METER_CMD_FEATURES failed"); | |
3658 | return; | |
3659 | } | |
3660 | ||
3661 | features->max_meters = nl_attr_get_u32(a[OVS_METER_ATTR_MAX_METERS]); | |
3662 | features->max_bands = nl_attr_get_u32(a[OVS_METER_ATTR_MAX_BANDS]); | |
3663 | ||
3664 | /* Bands is a nested attribute of zero or more nested | |
3665 | * band attributes. */ | |
3666 | if (a[OVS_METER_ATTR_BANDS]) { | |
3667 | const struct nlattr *nla; | |
3668 | size_t left; | |
3669 | ||
3670 | NL_NESTED_FOR_EACH (nla, left, a[OVS_METER_ATTR_BANDS]) { | |
3671 | const struct nlattr *band_nla; | |
3672 | size_t band_left; | |
3673 | ||
3674 | NL_NESTED_FOR_EACH (band_nla, band_left, nla) { | |
3675 | if (nl_attr_type(band_nla) == OVS_BAND_ATTR_TYPE) { | |
3676 | if (nl_attr_get_size(band_nla) == sizeof(uint32_t)) { | |
3677 | switch (nl_attr_get_u32(band_nla)) { | |
3678 | case OVS_METER_BAND_TYPE_DROP: | |
3679 | features->band_types |= 1 << OFPMBT13_DROP; | |
3680 | break; | |
3681 | } | |
3682 | } | |
3683 | } | |
3684 | } | |
3685 | } | |
3686 | } | |
3687 | features->capabilities = DP_SUPPORTED_METER_FLAGS_MASK; | |
3688 | ||
3689 | ofpbuf_delete(msg); | |
5dddf960 JR |
3690 | } |
3691 | ||
3692 | static int | |
60ebc04d JP |
3693 | dpif_netlink_meter_set__(struct dpif *dpif_, ofproto_meter_id meter_id, |
3694 | struct ofputil_meter_config *config) | |
5dddf960 | 3695 | { |
80738e5f AZ |
3696 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
3697 | struct ofpbuf buf, *msg; | |
3698 | uint64_t stub[1024 / 8]; | |
3699 | ||
3700 | static const struct nl_policy ovs_meter_set_response_policy[] = { | |
3701 | [OVS_METER_ATTR_ID] = { .type = NL_A_U32 }, | |
3702 | }; | |
3703 | struct nlattr *a[ARRAY_SIZE(ovs_meter_set_response_policy)]; | |
3704 | ||
3705 | if (config->flags & ~DP_SUPPORTED_METER_FLAGS_MASK) { | |
3706 | return EBADF; /* Unsupported flags set */ | |
3707 | } | |
3708 | ||
3709 | for (size_t i = 0; i < config->n_bands; i++) { | |
3710 | switch (config->bands[i].type) { | |
3711 | case OFPMBT13_DROP: | |
3712 | break; | |
3713 | default: | |
3714 | return ENODEV; /* Unsupported band type */ | |
3715 | } | |
3716 | } | |
3717 | ||
3718 | dpif_netlink_meter_init(dpif, &buf, stub, sizeof stub, OVS_METER_CMD_SET); | |
3719 | ||
8101f03f JP |
3720 | nl_msg_put_u32(&buf, OVS_METER_ATTR_ID, meter_id.uint32); |
3721 | ||
80738e5f AZ |
3722 | if (config->flags & OFPMF13_KBPS) { |
3723 | nl_msg_put_flag(&buf, OVS_METER_ATTR_KBPS); | |
3724 | } | |
3725 | ||
3726 | size_t bands_offset = nl_msg_start_nested(&buf, OVS_METER_ATTR_BANDS); | |
3727 | /* Bands */ | |
3728 | for (size_t i = 0; i < config->n_bands; ++i) { | |
3729 | struct ofputil_meter_band * band = &config->bands[i]; | |
3730 | uint32_t band_type; | |
3731 | ||
3732 | size_t band_offset = nl_msg_start_nested(&buf, OVS_BAND_ATTR_UNSPEC); | |
3733 | ||
3734 | switch (band->type) { | |
3735 | case OFPMBT13_DROP: | |
3736 | band_type = OVS_METER_BAND_TYPE_DROP; | |
3737 | break; | |
3738 | default: | |
3739 | band_type = OVS_METER_BAND_TYPE_UNSPEC; | |
3740 | } | |
3741 | nl_msg_put_u32(&buf, OVS_BAND_ATTR_TYPE, band_type); | |
3742 | nl_msg_put_u32(&buf, OVS_BAND_ATTR_RATE, band->rate); | |
3743 | nl_msg_put_u32(&buf, OVS_BAND_ATTR_BURST, | |
3744 | config->flags & OFPMF13_BURST ? | |
3745 | band->burst_size : band->rate); | |
3746 | nl_msg_end_nested(&buf, band_offset); | |
3747 | } | |
3748 | nl_msg_end_nested(&buf, bands_offset); | |
3749 | ||
3750 | int error = dpif_netlink_meter_transact(&buf, &msg, | |
3751 | ovs_meter_set_response_policy, a, | |
3752 | ARRAY_SIZE(ovs_meter_set_response_policy)); | |
3753 | if (error) { | |
3754 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
3755 | VLOG_INFO_RL(&rl, | |
3756 | "dpif_netlink_meter_transact OVS_METER_CMD_SET failed"); | |
3757 | return error; | |
3758 | } | |
3759 | ||
8101f03f JP |
3760 | if (nl_attr_get_u32(a[OVS_METER_ATTR_ID]) != meter_id.uint32) { |
3761 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
3762 | VLOG_INFO_RL(&rl, | |
3763 | "Kernel returned a different meter id than requested"); | |
3764 | } | |
80738e5f AZ |
3765 | ofpbuf_delete(msg); |
3766 | return 0; | |
5dddf960 JR |
3767 | } |
3768 | ||
60ebc04d JP |
3769 | static int |
3770 | dpif_netlink_meter_set(struct dpif *dpif_, ofproto_meter_id meter_id, | |
3771 | struct ofputil_meter_config *config) | |
3772 | { | |
3773 | if (probe_broken_meters(dpif_)) { | |
3774 | return ENOMEM; | |
3775 | } | |
3776 | ||
3777 | return dpif_netlink_meter_set__(dpif_, meter_id, config); | |
3778 | } | |
3779 | ||
80738e5f AZ |
3780 | /* Retrieve statistics and/or delete meter 'meter_id'. Statistics are |
3781 | * stored in 'stats', if it is not null. If 'command' is | |
3782 | * OVS_METER_CMD_DEL, the meter is deleted and statistics are optionally | |
3783 | * retrieved. If 'command' is OVS_METER_CMD_GET, then statistics are | |
3784 | * simply retrieved. */ | |
5dddf960 | 3785 | static int |
80738e5f AZ |
3786 | dpif_netlink_meter_get_stats(const struct dpif *dpif_, |
3787 | ofproto_meter_id meter_id, | |
3788 | struct ofputil_meter_stats *stats, | |
3789 | uint16_t max_bands, | |
3790 | enum ovs_meter_cmd command) | |
5dddf960 | 3791 | { |
80738e5f AZ |
3792 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
3793 | struct ofpbuf buf, *msg; | |
3794 | uint64_t stub[1024 / 8]; | |
3795 | ||
3796 | static const struct nl_policy ovs_meter_stats_policy[] = { | |
3797 | [OVS_METER_ATTR_ID] = { .type = NL_A_U32, .optional = true}, | |
3798 | [OVS_METER_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats), | |
3799 | .optional = true}, | |
3800 | [OVS_METER_ATTR_BANDS] = { .type = NL_A_NESTED, .optional = true }, | |
3801 | }; | |
3802 | struct nlattr *a[ARRAY_SIZE(ovs_meter_stats_policy)]; | |
3803 | ||
3804 | dpif_netlink_meter_init(dpif, &buf, stub, sizeof stub, command); | |
3805 | ||
3806 | nl_msg_put_u32(&buf, OVS_METER_ATTR_ID, meter_id.uint32); | |
3807 | ||
3808 | int error = dpif_netlink_meter_transact(&buf, &msg, | |
3809 | ovs_meter_stats_policy, a, | |
3810 | ARRAY_SIZE(ovs_meter_stats_policy)); | |
3811 | if (error) { | |
3812 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
3813 | VLOG_INFO_RL(&rl, "dpif_netlink_meter_transact %s failed", | |
3814 | command == OVS_METER_CMD_GET ? "get" : "del"); | |
3815 | return error; | |
3816 | } | |
3817 | ||
3818 | if (stats | |
3819 | && a[OVS_METER_ATTR_ID] | |
3820 | && a[OVS_METER_ATTR_STATS] | |
3821 | && nl_attr_get_u32(a[OVS_METER_ATTR_ID]) == meter_id.uint32) { | |
3822 | /* return stats */ | |
3823 | const struct ovs_flow_stats *stat; | |
3824 | const struct nlattr *nla; | |
3825 | size_t left; | |
3826 | ||
3827 | stat = nl_attr_get(a[OVS_METER_ATTR_STATS]); | |
3828 | stats->packet_in_count = get_32aligned_u64(&stat->n_packets); | |
3829 | stats->byte_in_count = get_32aligned_u64(&stat->n_bytes); | |
3830 | ||
3831 | if (a[OVS_METER_ATTR_BANDS]) { | |
3832 | size_t n_bands = 0; | |
3833 | NL_NESTED_FOR_EACH (nla, left, a[OVS_METER_ATTR_BANDS]) { | |
3834 | const struct nlattr *band_nla; | |
3835 | band_nla = nl_attr_find_nested(nla, OVS_BAND_ATTR_STATS); | |
3836 | if (band_nla && nl_attr_get_size(band_nla) \ | |
3837 | == sizeof(struct ovs_flow_stats)) { | |
3838 | stat = nl_attr_get(band_nla); | |
3839 | ||
3840 | if (n_bands < max_bands) { | |
3841 | stats->bands[n_bands].packet_count | |
3842 | = get_32aligned_u64(&stat->n_packets); | |
3843 | stats->bands[n_bands].byte_count | |
3844 | = get_32aligned_u64(&stat->n_bytes); | |
3845 | ++n_bands; | |
3846 | } | |
3847 | } else { | |
3848 | stats->bands[n_bands].packet_count = 0; | |
3849 | stats->bands[n_bands].byte_count = 0; | |
3850 | ++n_bands; | |
3851 | } | |
3852 | } | |
3853 | stats->n_bands = n_bands; | |
3854 | } else { | |
3855 | /* For a non-existent meter, return 0 stats. */ | |
3856 | stats->n_bands = 0; | |
3857 | } | |
3858 | } | |
3859 | ||
3860 | ofpbuf_delete(msg); | |
3861 | return error; | |
5dddf960 JR |
3862 | } |
3863 | ||
3864 | static int | |
80738e5f AZ |
3865 | dpif_netlink_meter_get(const struct dpif *dpif, ofproto_meter_id meter_id, |
3866 | struct ofputil_meter_stats *stats, uint16_t max_bands) | |
5dddf960 | 3867 | { |
80738e5f AZ |
3868 | return dpif_netlink_meter_get_stats(dpif, meter_id, stats, max_bands, |
3869 | OVS_METER_CMD_GET); | |
3870 | } | |
3871 | ||
3872 | static int | |
3873 | dpif_netlink_meter_del(struct dpif *dpif, ofproto_meter_id meter_id, | |
3874 | struct ofputil_meter_stats *stats, uint16_t max_bands) | |
3875 | { | |
3876 | return dpif_netlink_meter_get_stats(dpif, meter_id, stats, max_bands, | |
3877 | OVS_METER_CMD_DEL); | |
5dddf960 JR |
3878 | } |
3879 | ||
92d0d515 JP |
3880 | static bool |
3881 | probe_broken_meters__(struct dpif *dpif) | |
3882 | { | |
3883 | /* This test is destructive if a probe occurs while ovs-vswitchd is | |
3884 | * running (e.g., an ovs-dpctl meter command is called), so choose a | |
3885 | * random high meter id to make this less likely to occur. */ | |
3886 | ofproto_meter_id id1 = { 54545401 }; | |
3887 | ofproto_meter_id id2 = { 54545402 }; | |
3888 | struct ofputil_meter_band band = {OFPMBT13_DROP, 0, 1, 0}; | |
3889 | struct ofputil_meter_config config1 = { 1, OFPMF13_KBPS, 1, &band}; | |
3890 | struct ofputil_meter_config config2 = { 2, OFPMF13_KBPS, 1, &band}; | |
3891 | ||
3892 | /* Try adding two meters and make sure that they both come back with | |
60ebc04d JP |
3893 | * the proper meter id. Use the "__" version so that we don't cause |
3894 | * a recurve deadlock. */ | |
3895 | dpif_netlink_meter_set__(dpif, id1, &config1); | |
3896 | dpif_netlink_meter_set__(dpif, id2, &config2); | |
92d0d515 JP |
3897 | |
3898 | if (dpif_netlink_meter_get(dpif, id1, NULL, 0) | |
3899 | || dpif_netlink_meter_get(dpif, id2, NULL, 0)) { | |
3900 | VLOG_INFO("The kernel module has a broken meter implementation."); | |
3901 | return true; | |
3902 | } | |
3903 | ||
3904 | dpif_netlink_meter_del(dpif, id1, NULL, 0); | |
3905 | dpif_netlink_meter_del(dpif, id2, NULL, 0); | |
3906 | ||
3907 | return false; | |
3908 | } | |
3909 | ||
3910 | static bool | |
3911 | probe_broken_meters(struct dpif *dpif) | |
3912 | { | |
3913 | /* This is a once-only test because currently OVS only has at most a single | |
3914 | * Netlink capable datapath on any given platform. */ | |
3915 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; | |
3916 | ||
3917 | static bool broken_meters = false; | |
3918 | if (ovsthread_once_start(&once)) { | |
3919 | broken_meters = probe_broken_meters__(dpif); | |
3920 | ovsthread_once_done(&once); | |
3921 | } | |
3922 | return broken_meters; | |
3923 | } | |
5dddf960 | 3924 | \f |
93451a0a | 3925 | const struct dpif_class dpif_netlink_class = { |
1a6f1e2a | 3926 | "system", |
f87c1357 | 3927 | false, /* cleanup_required */ |
c8973eb6 | 3928 | NULL, /* init */ |
93451a0a | 3929 | dpif_netlink_enumerate, |
0aeaabc8 | 3930 | NULL, |
93451a0a AS |
3931 | dpif_netlink_open, |
3932 | dpif_netlink_close, | |
3933 | dpif_netlink_destroy, | |
3934 | dpif_netlink_run, | |
e4516b20 | 3935 | NULL, /* wait */ |
93451a0a | 3936 | dpif_netlink_get_stats, |
dcdcad68 | 3937 | dpif_netlink_set_features, |
93451a0a AS |
3938 | dpif_netlink_port_add, |
3939 | dpif_netlink_port_del, | |
91364d18 | 3940 | NULL, /* port_set_config */ |
93451a0a AS |
3941 | dpif_netlink_port_query_by_number, |
3942 | dpif_netlink_port_query_by_name, | |
3943 | dpif_netlink_port_get_pid, | |
3944 | dpif_netlink_port_dump_start, | |
3945 | dpif_netlink_port_dump_next, | |
3946 | dpif_netlink_port_dump_done, | |
3947 | dpif_netlink_port_poll, | |
3948 | dpif_netlink_port_poll_wait, | |
3949 | dpif_netlink_flow_flush, | |
3950 | dpif_netlink_flow_dump_create, | |
3951 | dpif_netlink_flow_dump_destroy, | |
3952 | dpif_netlink_flow_dump_thread_create, | |
3953 | dpif_netlink_flow_dump_thread_destroy, | |
3954 | dpif_netlink_flow_dump_next, | |
3955 | dpif_netlink_operate, | |
3956 | dpif_netlink_recv_set, | |
3957 | dpif_netlink_handlers_set, | |
d4f6865c | 3958 | NULL, /* set_config */ |
93451a0a AS |
3959 | dpif_netlink_queue_to_priority, |
3960 | dpif_netlink_recv, | |
3961 | dpif_netlink_recv_wait, | |
3962 | dpif_netlink_recv_purge, | |
e4e74c3a | 3963 | NULL, /* register_dp_purge_cb */ |
6b31e073 RW |
3964 | NULL, /* register_upcall_cb */ |
3965 | NULL, /* enable_upcall */ | |
3966 | NULL, /* disable_upcall */ | |
b5cbbcf6 | 3967 | dpif_netlink_get_datapath_version, /* get_datapath_version */ |
c11c9f4a DDP |
3968 | dpif_netlink_ct_dump_start, |
3969 | dpif_netlink_ct_dump_next, | |
3970 | dpif_netlink_ct_dump_done, | |
5dddf960 | 3971 | dpif_netlink_ct_flush, |
c92339ad DB |
3972 | NULL, /* ct_set_maxconns */ |
3973 | NULL, /* ct_get_maxconns */ | |
875075b3 | 3974 | NULL, /* ct_get_nconns */ |
64207120 DB |
3975 | NULL, /* ct_set_tcp_seq_chk */ |
3976 | NULL, /* ct_get_tcp_seq_chk */ | |
906ff9d2 YHW |
3977 | dpif_netlink_ct_set_limits, |
3978 | dpif_netlink_ct_get_limits, | |
3979 | dpif_netlink_ct_del_limits, | |
1f161318 YHW |
3980 | dpif_netlink_ct_set_timeout_policy, |
3981 | dpif_netlink_ct_get_timeout_policy, | |
3982 | dpif_netlink_ct_del_timeout_policy, | |
3983 | dpif_netlink_ct_timeout_policy_dump_start, | |
3984 | dpif_netlink_ct_timeout_policy_dump_next, | |
3985 | dpif_netlink_ct_timeout_policy_dump_done, | |
187bb41f | 3986 | dpif_netlink_ct_get_timeout_policy_name, |
4ea96698 DB |
3987 | NULL, /* ipf_set_enabled */ |
3988 | NULL, /* ipf_set_min_frag */ | |
3989 | NULL, /* ipf_set_max_nfrags */ | |
3990 | NULL, /* ipf_get_status */ | |
3991 | NULL, /* ipf_dump_start */ | |
3992 | NULL, /* ipf_dump_next */ | |
3993 | NULL, /* ipf_dump_done */ | |
5dddf960 JR |
3994 | dpif_netlink_meter_get_features, |
3995 | dpif_netlink_meter_set, | |
3996 | dpif_netlink_meter_get, | |
3997 | dpif_netlink_meter_del, | |
96fba48f | 3998 | }; |
93451a0a | 3999 | |
96fba48f | 4000 | static int |
93451a0a | 4001 | dpif_netlink_init(void) |
96fba48f | 4002 | { |
eb8ed438 BP |
4003 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
4004 | static int error; | |
982b8810 | 4005 | |
eb8ed438 | 4006 | if (ovsthread_once_start(&once)) { |
df2c07f4 JP |
4007 | error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY, |
4008 | &ovs_datapath_family); | |
37a1300c | 4009 | if (error) { |
e0e2410d | 4010 | VLOG_INFO("Generic Netlink family '%s' does not exist. " |
cae7529c CL |
4011 | "The Open vSwitch kernel module is probably not loaded.", |
4012 | OVS_DATAPATH_FAMILY); | |
37a1300c | 4013 | } |
f0fef760 | 4014 | if (!error) { |
df2c07f4 | 4015 | error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family); |
f0fef760 | 4016 | } |
37a1300c | 4017 | if (!error) { |
df2c07f4 | 4018 | error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family); |
37a1300c | 4019 | } |
aaff4b55 | 4020 | if (!error) { |
df2c07f4 JP |
4021 | error = nl_lookup_genl_family(OVS_PACKET_FAMILY, |
4022 | &ovs_packet_family); | |
aaff4b55 | 4023 | } |
c7178a0b EJ |
4024 | if (!error) { |
4025 | error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP, | |
b3dcb73c | 4026 | &ovs_vport_mcgroup); |
c7178a0b | 4027 | } |
80738e5f AZ |
4028 | if (!error) { |
4029 | if (nl_lookup_genl_family(OVS_METER_FAMILY, &ovs_meter_family)) { | |
4030 | VLOG_INFO("The kernel module does not support meters."); | |
4031 | } | |
4032 | } | |
906ff9d2 YHW |
4033 | if (nl_lookup_genl_family(OVS_CT_LIMIT_FAMILY, |
4034 | &ovs_ct_limit_family) < 0) { | |
4035 | VLOG_INFO("Generic Netlink family '%s' does not exist. " | |
4036 | "Please update the Open vSwitch kernel module to enable " | |
4037 | "the conntrack limit feature.", OVS_CT_LIMIT_FAMILY); | |
4038 | } | |
eb8ed438 | 4039 | |
921c370a EG |
4040 | ovs_tunnels_out_of_tree = dpif_netlink_rtnl_probe_oot_tunnels(); |
4041 | ||
eb8ed438 | 4042 | ovsthread_once_done(&once); |
982b8810 BP |
4043 | } |
4044 | ||
4045 | return error; | |
96fba48f BP |
4046 | } |
4047 | ||
c19e6535 | 4048 | bool |
93451a0a | 4049 | dpif_netlink_is_internal_device(const char *name) |
9fe3b9a2 | 4050 | { |
93451a0a | 4051 | struct dpif_netlink_vport reply; |
c19e6535 | 4052 | struct ofpbuf *buf; |
9fe3b9a2 | 4053 | int error; |
96fba48f | 4054 | |
93451a0a | 4055 | error = dpif_netlink_vport_get(name, &reply, &buf); |
c19e6535 BP |
4056 | if (!error) { |
4057 | ofpbuf_delete(buf); | |
141d9ce4 | 4058 | } else if (error != ENODEV && error != ENOENT) { |
c19e6535 | 4059 | VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)", |
10a89ef0 | 4060 | name, ovs_strerror(error)); |
96fba48f BP |
4061 | } |
4062 | ||
df2c07f4 | 4063 | return reply.type == OVS_VPORT_TYPE_INTERNAL; |
96fba48f | 4064 | } |
e0467f6d | 4065 | |
df2c07f4 | 4066 | /* Parses the contents of 'buf', which contains a "struct ovs_header" followed |
c19e6535 BP |
4067 | * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a |
4068 | * positive errno value. | |
4069 | * | |
4070 | * 'vport' will contain pointers into 'buf', so the caller should not free | |
4071 | * 'buf' while 'vport' is still in use. */ | |
4072 | static int | |
93451a0a | 4073 | dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport, |
c19e6535 BP |
4074 | const struct ofpbuf *buf) |
4075 | { | |
df2c07f4 JP |
4076 | static const struct nl_policy ovs_vport_policy[] = { |
4077 | [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 }, | |
4078 | [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 }, | |
4079 | [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ }, | |
1579cf67 | 4080 | [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC }, |
f7df9823 | 4081 | [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats), |
c19e6535 | 4082 | .optional = true }, |
df2c07f4 | 4083 | [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true }, |
bfda5239 | 4084 | [OVS_VPORT_ATTR_NETNSID] = { .type = NL_A_U32, .optional = true }, |
c19e6535 BP |
4085 | }; |
4086 | ||
93451a0a | 4087 | dpif_netlink_vport_init(vport); |
c19e6535 | 4088 | |
0a2869d5 BP |
4089 | struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size); |
4090 | struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg); | |
4091 | struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl); | |
4092 | struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header); | |
4093 | ||
4094 | struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)]; | |
df2c07f4 JP |
4095 | if (!nlmsg || !genl || !ovs_header |
4096 | || nlmsg->nlmsg_type != ovs_vport_family | |
4097 | || !nl_policy_parse(&b, 0, ovs_vport_policy, a, | |
4098 | ARRAY_SIZE(ovs_vport_policy))) { | |
c19e6535 BP |
4099 | return EINVAL; |
4100 | } | |
c19e6535 | 4101 | |
f0fef760 | 4102 | vport->cmd = genl->cmd; |
df2c07f4 | 4103 | vport->dp_ifindex = ovs_header->dp_ifindex; |
4e022ec0 | 4104 | vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]); |
df2c07f4 JP |
4105 | vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]); |
4106 | vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]); | |
b063d9f0 | 4107 | if (a[OVS_VPORT_ATTR_UPCALL_PID]) { |
1579cf67 AW |
4108 | vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID]) |
4109 | / (sizeof *vport->upcall_pids); | |
4110 | vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]); | |
4111 | ||
b063d9f0 | 4112 | } |
df2c07f4 JP |
4113 | if (a[OVS_VPORT_ATTR_STATS]) { |
4114 | vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]); | |
4115 | } | |
df2c07f4 JP |
4116 | if (a[OVS_VPORT_ATTR_OPTIONS]) { |
4117 | vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]); | |
4118 | vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]); | |
c19e6535 | 4119 | } |
bfda5239 FL |
4120 | if (a[OVS_VPORT_ATTR_NETNSID]) { |
4121 | netnsid_set(&vport->netnsid, | |
4122 | nl_attr_get_u32(a[OVS_VPORT_ATTR_NETNSID])); | |
4123 | } else { | |
4124 | netnsid_set_local(&vport->netnsid); | |
4125 | } | |
c19e6535 BP |
4126 | return 0; |
4127 | } | |
4128 | ||
df2c07f4 | 4129 | /* Appends to 'buf' (which must initially be empty) a "struct ovs_header" |
c19e6535 BP |
4130 | * followed by Netlink attributes corresponding to 'vport'. */ |
4131 | static void | |
93451a0a AS |
4132 | dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *vport, |
4133 | struct ofpbuf *buf) | |
c19e6535 | 4134 | { |
df2c07f4 | 4135 | struct ovs_header *ovs_header; |
f0fef760 | 4136 | |
df2c07f4 | 4137 | nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO, |
69685a88 | 4138 | vport->cmd, OVS_VPORT_VERSION); |
c19e6535 | 4139 | |
df2c07f4 JP |
4140 | ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header); |
4141 | ovs_header->dp_ifindex = vport->dp_ifindex; | |
c19e6535 | 4142 | |
4e022ec0 AW |
4143 | if (vport->port_no != ODPP_NONE) { |
4144 | nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no); | |
c19e6535 BP |
4145 | } |
4146 | ||
df2c07f4 JP |
4147 | if (vport->type != OVS_VPORT_TYPE_UNSPEC) { |
4148 | nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type); | |
c19e6535 BP |
4149 | } |
4150 | ||
4151 | if (vport->name) { | |
df2c07f4 | 4152 | nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name); |
c19e6535 BP |
4153 | } |
4154 | ||
1579cf67 AW |
4155 | if (vport->upcall_pids) { |
4156 | nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID, | |
4157 | vport->upcall_pids, | |
4158 | vport->n_upcall_pids * sizeof *vport->upcall_pids); | |
a24a6574 | 4159 | } |
b063d9f0 | 4160 | |
c19e6535 | 4161 | if (vport->stats) { |
df2c07f4 | 4162 | nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS, |
c19e6535 BP |
4163 | vport->stats, sizeof *vport->stats); |
4164 | } | |
4165 | ||
c19e6535 | 4166 | if (vport->options) { |
df2c07f4 | 4167 | nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS, |
c19e6535 BP |
4168 | vport->options, vport->options_len); |
4169 | } | |
c19e6535 BP |
4170 | } |
4171 | ||
4172 | /* Clears 'vport' to "empty" values. */ | |
4173 | void | |
93451a0a | 4174 | dpif_netlink_vport_init(struct dpif_netlink_vport *vport) |
c19e6535 BP |
4175 | { |
4176 | memset(vport, 0, sizeof *vport); | |
4e022ec0 | 4177 | vport->port_no = ODPP_NONE; |
c19e6535 BP |
4178 | } |
4179 | ||
4180 | /* Executes 'request' in the kernel datapath. If the command fails, returns a | |
4181 | * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0 | |
4182 | * without doing anything else. If 'reply' and 'bufp' are nonnull, then the | |
df2c07f4 | 4183 | * result of the command is expected to be an ovs_vport also, which is decoded |
c19e6535 BP |
4184 | * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the |
4185 | * reply is no longer needed ('reply' will contain pointers into '*bufp'). */ | |
4186 | int | |
93451a0a AS |
4187 | dpif_netlink_vport_transact(const struct dpif_netlink_vport *request, |
4188 | struct dpif_netlink_vport *reply, | |
4189 | struct ofpbuf **bufp) | |
c19e6535 | 4190 | { |
f0fef760 | 4191 | struct ofpbuf *request_buf; |
c19e6535 BP |
4192 | int error; |
4193 | ||
cb22974d | 4194 | ovs_assert((reply != NULL) == (bufp != NULL)); |
c19e6535 | 4195 | |
93451a0a | 4196 | error = dpif_netlink_init(); |
42bb6c72 BP |
4197 | if (error) { |
4198 | if (reply) { | |
4199 | *bufp = NULL; | |
93451a0a | 4200 | dpif_netlink_vport_init(reply); |
42bb6c72 BP |
4201 | } |
4202 | return error; | |
4203 | } | |
4204 | ||
f0fef760 | 4205 | request_buf = ofpbuf_new(1024); |
93451a0a | 4206 | dpif_netlink_vport_to_ofpbuf(request, request_buf); |
a88b4e04 | 4207 | error = nl_transact(NETLINK_GENERIC, request_buf, bufp); |
f0fef760 | 4208 | ofpbuf_delete(request_buf); |
c19e6535 | 4209 | |
f0fef760 BP |
4210 | if (reply) { |
4211 | if (!error) { | |
93451a0a | 4212 | error = dpif_netlink_vport_from_ofpbuf(reply, *bufp); |
f0fef760 | 4213 | } |
c19e6535 | 4214 | if (error) { |
93451a0a | 4215 | dpif_netlink_vport_init(reply); |
f0fef760 BP |
4216 | ofpbuf_delete(*bufp); |
4217 | *bufp = NULL; | |
c19e6535 | 4218 | } |
c19e6535 BP |
4219 | } |
4220 | return error; | |
4221 | } | |
4222 | ||
4223 | /* Obtains information about the kernel vport named 'name' and stores it into | |
4224 | * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no | |
4225 | * longer needed ('reply' will contain pointers into '*bufp'). */ | |
4226 | int | |
93451a0a AS |
4227 | dpif_netlink_vport_get(const char *name, struct dpif_netlink_vport *reply, |
4228 | struct ofpbuf **bufp) | |
c19e6535 | 4229 | { |
93451a0a | 4230 | struct dpif_netlink_vport request; |
c19e6535 | 4231 | |
93451a0a | 4232 | dpif_netlink_vport_init(&request); |
df2c07f4 | 4233 | request.cmd = OVS_VPORT_CMD_GET; |
c19e6535 BP |
4234 | request.name = name; |
4235 | ||
93451a0a | 4236 | return dpif_netlink_vport_transact(&request, reply, bufp); |
c19e6535 | 4237 | } |
93451a0a | 4238 | |
df2c07f4 | 4239 | /* Parses the contents of 'buf', which contains a "struct ovs_header" followed |
aaff4b55 BP |
4240 | * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a |
4241 | * positive errno value. | |
d6569377 BP |
4242 | * |
4243 | * 'dp' will contain pointers into 'buf', so the caller should not free 'buf' | |
4244 | * while 'dp' is still in use. */ | |
4245 | static int | |
93451a0a | 4246 | dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *dp, const struct ofpbuf *buf) |
d6569377 | 4247 | { |
df2c07f4 JP |
4248 | static const struct nl_policy ovs_datapath_policy[] = { |
4249 | [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ }, | |
f7df9823 | 4250 | [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats), |
d6569377 | 4251 | .optional = true }, |
847108dc AZ |
4252 | [OVS_DP_ATTR_MEGAFLOW_STATS] = { |
4253 | NL_POLICY_FOR(struct ovs_dp_megaflow_stats), | |
4254 | .optional = true }, | |
dcdcad68 PB |
4255 | [OVS_DP_ATTR_USER_FEATURES] = { |
4256 | .type = NL_A_U32, | |
4257 | .optional = true }, | |
d6569377 BP |
4258 | }; |
4259 | ||
93451a0a | 4260 | dpif_netlink_dp_init(dp); |
d6569377 | 4261 | |
0a2869d5 BP |
4262 | struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size); |
4263 | struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg); | |
4264 | struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl); | |
4265 | struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header); | |
4266 | ||
4267 | struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)]; | |
df2c07f4 JP |
4268 | if (!nlmsg || !genl || !ovs_header |
4269 | || nlmsg->nlmsg_type != ovs_datapath_family | |
4270 | || !nl_policy_parse(&b, 0, ovs_datapath_policy, a, | |
4271 | ARRAY_SIZE(ovs_datapath_policy))) { | |
d6569377 BP |
4272 | return EINVAL; |
4273 | } | |
d6569377 | 4274 | |
aaff4b55 | 4275 | dp->cmd = genl->cmd; |
df2c07f4 JP |
4276 | dp->dp_ifindex = ovs_header->dp_ifindex; |
4277 | dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]); | |
4278 | if (a[OVS_DP_ATTR_STATS]) { | |
6a54dedc | 4279 | dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]); |
d6569377 | 4280 | } |
982b8810 | 4281 | |
847108dc | 4282 | if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) { |
6a54dedc | 4283 | dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]); |
847108dc AZ |
4284 | } |
4285 | ||
dcdcad68 PB |
4286 | if (a[OVS_DP_ATTR_USER_FEATURES]) { |
4287 | dp->user_features = nl_attr_get_u32(a[OVS_DP_ATTR_USER_FEATURES]); | |
4288 | } | |
4289 | ||
d6569377 BP |
4290 | return 0; |
4291 | } | |
4292 | ||
aaff4b55 | 4293 | /* Appends to 'buf' the Generic Netlink message described by 'dp'. */ |
d6569377 | 4294 | static void |
93451a0a | 4295 | dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp *dp, struct ofpbuf *buf) |
d6569377 | 4296 | { |
df2c07f4 | 4297 | struct ovs_header *ovs_header; |
d6569377 | 4298 | |
df2c07f4 | 4299 | nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family, |
69685a88 JG |
4300 | NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, |
4301 | OVS_DATAPATH_VERSION); | |
aaff4b55 | 4302 | |
df2c07f4 JP |
4303 | ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header); |
4304 | ovs_header->dp_ifindex = dp->dp_ifindex; | |
d6569377 BP |
4305 | |
4306 | if (dp->name) { | |
df2c07f4 | 4307 | nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name); |
d6569377 BP |
4308 | } |
4309 | ||
a24a6574 BP |
4310 | if (dp->upcall_pid) { |
4311 | nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid); | |
4312 | } | |
b063d9f0 | 4313 | |
b7fd5e38 TG |
4314 | if (dp->user_features) { |
4315 | nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features); | |
4316 | } | |
4317 | ||
df2c07f4 | 4318 | /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */ |
d6569377 BP |
4319 | } |
4320 | ||
4321 | /* Clears 'dp' to "empty" values. */ | |
d3d8f1f7 | 4322 | static void |
93451a0a | 4323 | dpif_netlink_dp_init(struct dpif_netlink_dp *dp) |
d6569377 BP |
4324 | { |
4325 | memset(dp, 0, sizeof *dp); | |
d6569377 BP |
4326 | } |
4327 | ||
aaff4b55 | 4328 | static void |
93451a0a | 4329 | dpif_netlink_dp_dump_start(struct nl_dump *dump) |
aaff4b55 | 4330 | { |
93451a0a | 4331 | struct dpif_netlink_dp request; |
aaff4b55 BP |
4332 | struct ofpbuf *buf; |
4333 | ||
93451a0a | 4334 | dpif_netlink_dp_init(&request); |
df2c07f4 | 4335 | request.cmd = OVS_DP_CMD_GET; |
aaff4b55 BP |
4336 | |
4337 | buf = ofpbuf_new(1024); | |
93451a0a | 4338 | dpif_netlink_dp_to_ofpbuf(&request, buf); |
a88b4e04 | 4339 | nl_dump_start(dump, NETLINK_GENERIC, buf); |
aaff4b55 BP |
4340 | ofpbuf_delete(buf); |
4341 | } | |
4342 | ||
d6569377 BP |
4343 | /* Executes 'request' in the kernel datapath. If the command fails, returns a |
4344 | * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0 | |
4345 | * without doing anything else. If 'reply' and 'bufp' are nonnull, then the | |
aaff4b55 BP |
4346 | * result of the command is expected to be of the same form, which is decoded |
4347 | * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the | |
4348 | * reply is no longer needed ('reply' will contain pointers into '*bufp'). */ | |
d3d8f1f7 | 4349 | static int |
93451a0a AS |
4350 | dpif_netlink_dp_transact(const struct dpif_netlink_dp *request, |
4351 | struct dpif_netlink_dp *reply, struct ofpbuf **bufp) | |
d6569377 | 4352 | { |
aaff4b55 | 4353 | struct ofpbuf *request_buf; |
d6569377 | 4354 | int error; |
d6569377 | 4355 | |
cb22974d | 4356 | ovs_assert((reply != NULL) == (bufp != NULL)); |
d6569377 | 4357 | |
aaff4b55 | 4358 | request_buf = ofpbuf_new(1024); |
93451a0a | 4359 | dpif_netlink_dp_to_ofpbuf(request, request_buf); |
a88b4e04 | 4360 | error = nl_transact(NETLINK_GENERIC, request_buf, bufp); |
aaff4b55 | 4361 | ofpbuf_delete(request_buf); |
d6569377 | 4362 | |
aaff4b55 | 4363 | if (reply) { |
93451a0a | 4364 | dpif_netlink_dp_init(reply); |
aaff4b55 | 4365 | if (!error) { |
93451a0a | 4366 | error = dpif_netlink_dp_from_ofpbuf(reply, *bufp); |
aaff4b55 | 4367 | } |
d6569377 | 4368 | if (error) { |
aaff4b55 BP |
4369 | ofpbuf_delete(*bufp); |
4370 | *bufp = NULL; | |
d6569377 | 4371 | } |
d6569377 BP |
4372 | } |
4373 | return error; | |
4374 | } | |
4375 | ||
4376 | /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'. | |
4377 | * The caller must free '*bufp' when the reply is no longer needed ('reply' | |
4378 | * will contain pointers into '*bufp'). */ | |
d3d8f1f7 | 4379 | static int |
93451a0a AS |
4380 | dpif_netlink_dp_get(const struct dpif *dpif_, struct dpif_netlink_dp *reply, |
4381 | struct ofpbuf **bufp) | |
d6569377 | 4382 | { |
93451a0a AS |
4383 | struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); |
4384 | struct dpif_netlink_dp request; | |
d6569377 | 4385 | |
93451a0a | 4386 | dpif_netlink_dp_init(&request); |
df2c07f4 | 4387 | request.cmd = OVS_DP_CMD_GET; |
254f2dc8 | 4388 | request.dp_ifindex = dpif->dp_ifindex; |
d6569377 | 4389 | |
93451a0a | 4390 | return dpif_netlink_dp_transact(&request, reply, bufp); |
d6569377 | 4391 | } |
93451a0a | 4392 | |
df2c07f4 | 4393 | /* Parses the contents of 'buf', which contains a "struct ovs_header" followed |
37a1300c | 4394 | * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a |
d6569377 BP |
4395 | * positive errno value. |
4396 | * | |
4397 | * 'flow' will contain pointers into 'buf', so the caller should not free 'buf' | |
4398 | * while 'flow' is still in use. */ | |
4399 | static int | |
93451a0a AS |
4400 | dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *flow, |
4401 | const struct ofpbuf *buf) | |
d6569377 | 4402 | { |
70e5ed6f JS |
4403 | static const struct nl_policy ovs_flow_policy[__OVS_FLOW_ATTR_MAX] = { |
4404 | [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED, .optional = true }, | |
e6cc0bab | 4405 | [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true }, |
df2c07f4 | 4406 | [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true }, |
f7df9823 | 4407 | [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats), |
d6569377 | 4408 | .optional = true }, |
df2c07f4 JP |
4409 | [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true }, |
4410 | [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true }, | |
ab79d262 | 4411 | [OVS_FLOW_ATTR_UFID] = { .type = NL_A_U128, .optional = true }, |
df2c07f4 | 4412 | /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */ |
43f9ac0a | 4413 | /* The kernel never uses OVS_FLOW_ATTR_PROBE. */ |
70e5ed6f | 4414 | /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */ |
d6569377 BP |
4415 | }; |
4416 | ||
93451a0a | 4417 | dpif_netlink_flow_init(flow); |
d6569377 | 4418 | |
0a2869d5 BP |
4419 | struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size); |
4420 | struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg); | |
4421 | struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl); | |
4422 | struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header); | |
4423 | ||
4424 | struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)]; | |
df2c07f4 JP |
4425 | if (!nlmsg || !genl || !ovs_header |
4426 | || nlmsg->nlmsg_type != ovs_flow_family | |
4427 | || !nl_policy_parse(&b, 0, ovs_flow_policy, a, | |
4428 | ARRAY_SIZE(ovs_flow_policy))) { | |
d6569377 BP |
4429 | return EINVAL; |
4430 | } | |
70e5ed6f JS |
4431 | if (!a[OVS_FLOW_ATTR_KEY] && !a[OVS_FLOW_ATTR_UFID]) { |
4432 | return EINVAL; | |
4433 | } | |
d6569377 | 4434 | |
37a1300c | 4435 | flow->nlmsg_flags = nlmsg->nlmsg_flags; |
df2c07f4 | 4436 | flow->dp_ifindex = ovs_header->dp_ifindex; |
70e5ed6f JS |
4437 | if (a[OVS_FLOW_ATTR_KEY]) { |
4438 | flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]); | |
4439 | flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]); | |
4440 | } | |
e6cc0bab | 4441 | |
70e5ed6f | 4442 | if (a[OVS_FLOW_ATTR_UFID]) { |
ab79d262 | 4443 | flow->ufid = nl_attr_get_u128(a[OVS_FLOW_ATTR_UFID]); |
70e5ed6f JS |
4444 | flow->ufid_present = true; |
4445 | } | |
e6cc0bab AZ |
4446 | if (a[OVS_FLOW_ATTR_MASK]) { |
4447 | flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]); | |
4448 | flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]); | |
4449 | } | |
df2c07f4 JP |
4450 | if (a[OVS_FLOW_ATTR_ACTIONS]) { |
4451 | flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]); | |
4452 | flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]); | |
d6569377 | 4453 | } |
df2c07f4 JP |
4454 | if (a[OVS_FLOW_ATTR_STATS]) { |
4455 | flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]); | |
d6569377 | 4456 | } |
df2c07f4 JP |
4457 | if (a[OVS_FLOW_ATTR_TCP_FLAGS]) { |
4458 | flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]); | |
d6569377 | 4459 | } |
df2c07f4 JP |
4460 | if (a[OVS_FLOW_ATTR_USED]) { |
4461 | flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]); | |
9e980142 | 4462 | } |
d6569377 BP |
4463 | return 0; |
4464 | } | |
4465 | ||
beb75a40 JS |
4466 | |
4467 | /* | |
a8a3eee4 JS |
4468 | * If PACKET_TYPE attribute is present in 'data', it filters PACKET_TYPE out. |
4469 | * If the flow is not Ethernet, the OVS_KEY_ATTR_PACKET_TYPE is converted to | |
4470 | * OVS_KEY_ATTR_ETHERTYPE. Puts 'data' to 'buf'. | |
beb75a40 JS |
4471 | */ |
4472 | static void | |
4473 | put_exclude_packet_type(struct ofpbuf *buf, uint16_t type, | |
4474 | const struct nlattr *data, uint16_t data_len) | |
4475 | { | |
4476 | const struct nlattr *packet_type; | |
4477 | ||
4478 | packet_type = nl_attr_find__(data, data_len, OVS_KEY_ATTR_PACKET_TYPE); | |
4479 | ||
4480 | if (packet_type) { | |
4481 | /* exclude PACKET_TYPE Netlink attribute. */ | |
4482 | ovs_assert(NLA_ALIGN(packet_type->nla_len) == NL_A_U32_SIZE); | |
4483 | size_t packet_type_len = NL_A_U32_SIZE; | |
4484 | size_t first_chunk_size = (uint8_t *)packet_type - (uint8_t *)data; | |
4485 | size_t second_chunk_size = data_len - first_chunk_size | |
4486 | - packet_type_len; | |
beb75a40 | 4487 | struct nlattr *next_attr = nl_attr_next(packet_type); |
1ca5b61b | 4488 | size_t ofs; |
beb75a40 | 4489 | |
1ca5b61b JS |
4490 | ofs = nl_msg_start_nested(buf, type); |
4491 | nl_msg_put(buf, data, first_chunk_size); | |
4492 | nl_msg_put(buf, next_attr, second_chunk_size); | |
a8a3eee4 JS |
4493 | if (!nl_attr_find__(data, data_len, OVS_KEY_ATTR_ETHERNET)) { |
4494 | ovs_be16 pt = pt_ns_type_be(nl_attr_get_be32(packet_type)); | |
4495 | const struct nlattr *nla; | |
4496 | ||
7c5793e6 | 4497 | nla = nl_attr_find(buf, ofs + NLA_HDRLEN, OVS_KEY_ATTR_ETHERTYPE); |
a8a3eee4 JS |
4498 | if (nla) { |
4499 | ovs_be16 *ethertype; | |
4500 | ||
4501 | ethertype = CONST_CAST(ovs_be16 *, nl_attr_get(nla)); | |
4502 | *ethertype = pt; | |
4503 | } else { | |
4504 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, pt); | |
4505 | } | |
4506 | } | |
1ca5b61b | 4507 | nl_msg_end_nested(buf, ofs); |
beb75a40 JS |
4508 | } else { |
4509 | nl_msg_put_unspec(buf, type, data, data_len); | |
4510 | } | |
4511 | } | |
4512 | ||
df2c07f4 | 4513 | /* Appends to 'buf' (which must initially be empty) a "struct ovs_header" |
d6569377 BP |
4514 | * followed by Netlink attributes corresponding to 'flow'. */ |
4515 | static void | |
93451a0a AS |
4516 | dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *flow, |
4517 | struct ofpbuf *buf) | |
d6569377 | 4518 | { |
df2c07f4 | 4519 | struct ovs_header *ovs_header; |
d6569377 | 4520 | |
df2c07f4 | 4521 | nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family, |
30b44744 | 4522 | NLM_F_REQUEST | flow->nlmsg_flags, |
69685a88 | 4523 | flow->cmd, OVS_FLOW_VERSION); |
37a1300c | 4524 | |
df2c07f4 JP |
4525 | ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header); |
4526 | ovs_header->dp_ifindex = flow->dp_ifindex; | |
d6569377 | 4527 | |
70e5ed6f | 4528 | if (flow->ufid_present) { |
ab79d262 | 4529 | nl_msg_put_u128(buf, OVS_FLOW_ATTR_UFID, flow->ufid); |
70e5ed6f JS |
4530 | } |
4531 | if (flow->ufid_terse) { | |
4532 | nl_msg_put_u32(buf, OVS_FLOW_ATTR_UFID_FLAGS, | |
4533 | OVS_UFID_F_OMIT_KEY | OVS_UFID_F_OMIT_MASK | |
4534 | | OVS_UFID_F_OMIT_ACTIONS); | |
4535 | } | |
64bb477f JS |
4536 | if (!flow->ufid_terse || !flow->ufid_present) { |
4537 | if (flow->key_len) { | |
beb75a40 JS |
4538 | put_exclude_packet_type(buf, OVS_FLOW_ATTR_KEY, flow->key, |
4539 | flow->key_len); | |
64bb477f | 4540 | } |
64bb477f | 4541 | if (flow->mask_len) { |
beb75a40 JS |
4542 | put_exclude_packet_type(buf, OVS_FLOW_ATTR_MASK, flow->mask, |
4543 | flow->mask_len); | |
64bb477f JS |
4544 | } |
4545 | if (flow->actions || flow->actions_len) { | |
4546 | nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS, | |
4547 | flow->actions, flow->actions_len); | |
4548 | } | |
d6569377 BP |
4549 | } |
4550 | ||
4551 | /* We never need to send these to the kernel. */ | |
cb22974d BP |
4552 | ovs_assert(!flow->stats); |
4553 | ovs_assert(!flow->tcp_flags); | |
4554 | ovs_assert(!flow->used); | |
d6569377 BP |
4555 | |
4556 | if (flow->clear) { | |
df2c07f4 | 4557 | nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR); |
d6569377 | 4558 | } |
43f9ac0a JR |
4559 | if (flow->probe) { |
4560 | nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE); | |
4561 | } | |
d6569377 BP |
4562 | } |
4563 | ||
4564 | /* Clears 'flow' to "empty" values. */ | |
d3d8f1f7 | 4565 | static void |
93451a0a | 4566 | dpif_netlink_flow_init(struct dpif_netlink_flow *flow) |
d6569377 BP |
4567 | { |
4568 | memset(flow, 0, sizeof *flow); | |
4569 | } | |
4570 | ||
4571 | /* Executes 'request' in the kernel datapath. If the command fails, returns a | |
4572 | * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0 | |
4573 | * without doing anything else. If 'reply' and 'bufp' are nonnull, then the | |
37a1300c BP |
4574 | * result of the command is expected to be a flow also, which is decoded and |
4575 | * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply | |
4576 | * is no longer needed ('reply' will contain pointers into '*bufp'). */ | |
d3d8f1f7 | 4577 | static int |
93451a0a AS |
4578 | dpif_netlink_flow_transact(struct dpif_netlink_flow *request, |
4579 | struct dpif_netlink_flow *reply, | |
4580 | struct ofpbuf **bufp) | |
d6569377 | 4581 | { |
37a1300c | 4582 | struct ofpbuf *request_buf; |
d6569377 | 4583 | int error; |
d6569377 | 4584 | |
cb22974d | 4585 | ovs_assert((reply != NULL) == (bufp != NULL)); |
d6569377 | 4586 | |
30b44744 BP |
4587 | if (reply) { |
4588 | request->nlmsg_flags |= NLM_F_ECHO; | |
4589 | } | |
4590 | ||
37a1300c | 4591 | request_buf = ofpbuf_new(1024); |
93451a0a | 4592 | dpif_netlink_flow_to_ofpbuf(request, request_buf); |
a88b4e04 | 4593 | error = nl_transact(NETLINK_GENERIC, request_buf, bufp); |
37a1300c | 4594 | ofpbuf_delete(request_buf); |
d6569377 | 4595 | |
37a1300c BP |
4596 | if (reply) { |
4597 | if (!error) { | |
93451a0a | 4598 | error = dpif_netlink_flow_from_ofpbuf(reply, *bufp); |
37a1300c | 4599 | } |
d6569377 | 4600 | if (error) { |
93451a0a | 4601 | dpif_netlink_flow_init(reply); |
37a1300c BP |
4602 | ofpbuf_delete(*bufp); |
4603 | *bufp = NULL; | |
d6569377 | 4604 | } |
d6569377 BP |
4605 | } |
4606 | return error; | |
4607 | } | |
4608 | ||
4609 | static void | |
93451a0a AS |
4610 | dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *flow, |
4611 | struct dpif_flow_stats *stats) | |
d6569377 BP |
4612 | { |
4613 | if (flow->stats) { | |
6a54dedc BP |
4614 | stats->n_packets = get_32aligned_u64(&flow->stats->n_packets); |
4615 | stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes); | |
d6569377 BP |
4616 | } else { |
4617 | stats->n_packets = 0; | |
4618 | stats->n_bytes = 0; | |
4619 | } | |
0e70cdcb | 4620 | stats->used = flow->used ? get_32aligned_u64(flow->used) : 0; |
d6569377 BP |
4621 | stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0; |
4622 | } | |
e0467f6d | 4623 | |
14b4d2f9 BP |
4624 | /* Logs information about a packet that was recently lost in 'ch' (in |
4625 | * 'dpif_'). */ | |
4626 | static void | |
93451a0a | 4627 | report_loss(struct dpif_netlink *dpif, struct dpif_channel *ch, uint32_t ch_idx, |
1579cf67 | 4628 | uint32_t handler_id) |
14b4d2f9 | 4629 | { |
14b4d2f9 | 4630 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); |
14b4d2f9 BP |
4631 | struct ds s; |
4632 | ||
8d675c5a | 4633 | if (VLOG_DROP_WARN(&rl)) { |
14b4d2f9 BP |
4634 | return; |
4635 | } | |
4636 | ||
4637 | ds_init(&s); | |
4638 | if (ch->last_poll != LLONG_MIN) { | |
4639 | ds_put_format(&s, " (last polled %lld ms ago)", | |
4640 | time_msec() - ch->last_poll); | |
4641 | } | |
14b4d2f9 | 4642 | |
1579cf67 | 4643 | VLOG_WARN("%s: lost packet on port channel %u of handler %u", |
9b00386b | 4644 | dpif_name(&dpif->dpif), ch_idx, handler_id); |
14b4d2f9 BP |
4645 | ds_destroy(&s); |
4646 | } |