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