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