]> git.proxmox.com Git - mirror_ovs.git/blame - lib/dpif-netlink.c
doc: Remove final markdown references
[mirror_ovs.git] / lib / dpif-netlink.c
CommitLineData
96fba48f 1/*
0a2869d5 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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"
96fba48f 36#include "dpif-provider.h"
3e8a2ad1 37#include "openvswitch/dynamic-string.h"
eb8b28e7 38#include "flow.h"
1579cf67 39#include "fat-rwlock.h"
3abc4a1a 40#include "netdev.h"
032aa6a3 41#include "netdev-linux.h"
c3827f61 42#include "netdev-vport.h"
c11c9f4a 43#include "netlink-conntrack.h"
45c8d3a1 44#include "netlink-notifier.h"
982b8810 45#include "netlink-socket.h"
856081f6 46#include "netlink.h"
feebdea2 47#include "odp-util.h"
64c96779 48#include "openvswitch/ofpbuf.h"
856081f6 49#include "packets.h"
96fba48f 50#include "poll-loop.h"
17411ecf 51#include "random.h"
ee89ea7b 52#include "openvswitch/shash.h"
b3c01ed3 53#include "sset.h"
14b4d2f9 54#include "timeval.h"
d6569377 55#include "unaligned.h"
96fba48f 56#include "util.h"
e6211adc 57#include "openvswitch/vlog.h"
5136ce49 58
93451a0a 59VLOG_DEFINE_THIS_MODULE(dpif_netlink);
09cac43f
NR
60#ifdef _WIN32
61enum { WINDOWS = 1 };
62#else
63enum { WINDOWS = 0 };
64#endif
95b1d73a 65enum { MAX_PORTS = USHRT_MAX };
773cd538 66
24b019f8
JP
67/* This ethtool flag was introduced in Linux 2.6.24, so it might be
68 * missing if we have old headers. */
69#define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
70
93451a0a 71struct dpif_netlink_dp {
aaff4b55
BP
72 /* Generic Netlink header. */
73 uint8_t cmd;
d6569377 74
df2c07f4 75 /* struct ovs_header. */
254f2dc8 76 int dp_ifindex;
d6569377
BP
77
78 /* Attributes. */
df2c07f4 79 const char *name; /* OVS_DP_ATTR_NAME. */
fcd5d230 80 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
b7fd5e38 81 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
6a54dedc
BP
82 const struct ovs_dp_stats *stats; /* OVS_DP_ATTR_STATS. */
83 const struct ovs_dp_megaflow_stats *megaflow_stats;
847108dc 84 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
d6569377
BP
85};
86
93451a0a
AS
87static void dpif_netlink_dp_init(struct dpif_netlink_dp *);
88static int dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *,
89 const struct ofpbuf *);
90static void dpif_netlink_dp_dump_start(struct nl_dump *);
91static int dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
92 struct dpif_netlink_dp *reply,
93 struct ofpbuf **bufp);
94static int dpif_netlink_dp_get(const struct dpif *,
95 struct dpif_netlink_dp *reply,
96 struct ofpbuf **bufp);
97
98struct dpif_netlink_flow {
37a1300c
BP
99 /* Generic Netlink header. */
100 uint8_t cmd;
d6569377 101
df2c07f4 102 /* struct ovs_header. */
d6569377 103 unsigned int nlmsg_flags;
254f2dc8 104 int dp_ifindex;
d6569377
BP
105
106 /* Attributes.
107 *
0e70cdcb
BP
108 * The 'stats' member points to 64-bit data that might only be aligned on
109 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
110 * values.
d2a23af2 111 *
df2c07f4 112 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
d2a23af2 113 * the Netlink version of the command, even if actions_len is zero. */
df2c07f4 114 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
d6569377 115 size_t key_len;
e6cc0bab
AZ
116 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
117 size_t mask_len;
df2c07f4 118 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
d6569377 119 size_t actions_len;
70e5ed6f
JS
120 ovs_u128 ufid; /* OVS_FLOW_ATTR_FLOW_ID. */
121 bool ufid_present; /* Is there a UFID? */
122 bool ufid_terse; /* Skip serializing key/mask/acts? */
df2c07f4
JP
123 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
124 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
0e70cdcb 125 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
df2c07f4 126 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
43f9ac0a 127 bool probe; /* OVS_FLOW_ATTR_PROBE. */
d6569377
BP
128};
129
93451a0a
AS
130static void dpif_netlink_flow_init(struct dpif_netlink_flow *);
131static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *,
132 const struct ofpbuf *);
133static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *,
134 struct ofpbuf *);
135static int dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
136 struct dpif_netlink_flow *reply,
137 struct ofpbuf **bufp);
138static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *,
139 struct dpif_flow_stats *);
7af12bd7 140static void dpif_netlink_flow_to_dpif_flow(struct dpif *, struct dpif_flow *,
93451a0a 141 const struct dpif_netlink_flow *);
d6569377 142
989fd548 143/* One of the dpif channels between the kernel and userspace. */
fe3d61b3 144struct dpif_channel {
14b4d2f9 145 struct nl_sock *sock; /* Netlink socket. */
14b4d2f9 146 long long int last_poll; /* Last time this channel was polled. */
fe3d61b3
BP
147};
148
09cac43f
NR
149#ifdef _WIN32
150#define VPORT_SOCK_POOL_SIZE 1
151/* On Windows, there is no native support for epoll. There are equivalent
152 * interfaces though, that are not used currently. For simpicity, a pool of
153 * netlink sockets is used. Each socket is represented by 'struct
154 * dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be
155 * sharing the same socket. In the future, we can add a reference count and
156 * such fields. */
157struct dpif_windows_vport_sock {
158 struct nl_sock *nl_sock; /* netlink socket. */
159};
160#endif
161
1579cf67
AW
162struct dpif_handler {
163 struct dpif_channel *channels;/* Array of channels for each handler. */
164 struct epoll_event *epoll_events;
165 int epoll_fd; /* epoll fd that includes channel socks. */
166 int n_events; /* Num events returned by epoll_wait(). */
167 int event_offset; /* Offset into 'epoll_events'. */
09cac43f
NR
168
169#ifdef _WIN32
170 /* Pool of sockets. */
171 struct dpif_windows_vport_sock *vport_sock_pool;
172 size_t last_used_pool_idx; /* Index to aid in allocating a
173 socket in the pool to a port. */
174#endif
1579cf67 175};
14b4d2f9 176
96fba48f 177/* Datapath interface for the openvswitch Linux kernel module. */
93451a0a 178struct dpif_netlink {
96fba48f 179 struct dpif dpif;
254f2dc8 180 int dp_ifindex;
e9e28be3 181
b063d9f0 182 /* Upcall messages. */
1579cf67
AW
183 struct fat_rwlock upcall_lock;
184 struct dpif_handler *handlers;
185 uint32_t n_handlers; /* Num of upcall handlers. */
186 int uc_array_size; /* Size of 'handler->channels' and */
187 /* 'handler->epoll_events'. */
982b8810 188
e9e28be3 189 /* Change notification. */
e4516b20 190 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
61eae437 191 bool refresh_channels;
96fba48f
BP
192};
193
93451a0a 194static void report_loss(struct dpif_netlink *, struct dpif_channel *,
9b00386b 195 uint32_t ch_idx, uint32_t handler_id);
1579cf67 196
96fba48f
BP
197static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
198
e4516b20
BP
199/* Generic Netlink family numbers for OVS.
200 *
93451a0a 201 * Initialized by dpif_netlink_init(). */
df2c07f4
JP
202static int ovs_datapath_family;
203static int ovs_vport_family;
204static int ovs_flow_family;
205static int ovs_packet_family;
982b8810 206
e4516b20
BP
207/* Generic Netlink multicast groups for OVS.
208 *
93451a0a 209 * Initialized by dpif_netlink_init(). */
e4516b20 210static unsigned int ovs_vport_mcgroup;
982b8810 211
93451a0a
AS
212static int dpif_netlink_init(void);
213static int open_dpif(const struct dpif_netlink_dp *, struct dpif **);
214static uint32_t dpif_netlink_port_get_pid(const struct dpif *,
215 odp_port_t port_no, uint32_t hash);
09cac43f 216static void dpif_netlink_handler_uninit(struct dpif_handler *handler);
93451a0a
AS
217static int dpif_netlink_refresh_channels(struct dpif_netlink *,
218 uint32_t n_handlers);
219static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *,
220 struct ofpbuf *);
221static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *,
222 const struct ofpbuf *);
f0fef760 223
93451a0a
AS
224static struct dpif_netlink *
225dpif_netlink_cast(const struct dpif *dpif)
96fba48f 226{
93451a0a
AS
227 dpif_assert_class(dpif, &dpif_netlink_class);
228 return CONTAINER_OF(dpif, struct dpif_netlink, dpif);
96fba48f
BP
229}
230
d3d22744 231static int
93451a0a
AS
232dpif_netlink_enumerate(struct sset *all_dps,
233 const struct dpif_class *dpif_class OVS_UNUSED)
d3d22744 234{
aaff4b55 235 struct nl_dump dump;
d57695d7
JS
236 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
237 struct ofpbuf msg, buf;
aaff4b55 238 int error;
982b8810 239
93451a0a 240 error = dpif_netlink_init();
aaff4b55
BP
241 if (error) {
242 return error;
982b8810 243 }
d3d22744 244
d57695d7 245 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
93451a0a 246 dpif_netlink_dp_dump_start(&dump);
d57695d7 247 while (nl_dump_next(&dump, &msg, &buf)) {
93451a0a 248 struct dpif_netlink_dp dp;
d6569377 249
93451a0a 250 if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) {
d0c23a1a 251 sset_add(all_dps, dp.name);
d3d22744
BP
252 }
253 }
d57695d7 254 ofpbuf_uninit(&buf);
aaff4b55 255 return nl_dump_done(&dump);
d3d22744
BP
256}
257
96fba48f 258static int
93451a0a
AS
259dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name,
260 bool create, struct dpif **dpifp)
96fba48f 261{
93451a0a 262 struct dpif_netlink_dp dp_request, dp;
c19e6535 263 struct ofpbuf *buf;
ea36840f 264 uint32_t upcall_pid;
c19e6535 265 int error;
96fba48f 266
93451a0a 267 error = dpif_netlink_init();
982b8810
BP
268 if (error) {
269 return error;
270 }
271
982b8810 272 /* Create or look up datapath. */
93451a0a 273 dpif_netlink_dp_init(&dp_request);
ea36840f
BP
274 if (create) {
275 dp_request.cmd = OVS_DP_CMD_NEW;
276 upcall_pid = 0;
277 dp_request.upcall_pid = &upcall_pid;
278 } else {
b7fd5e38
TG
279 /* Use OVS_DP_CMD_SET to report user features */
280 dp_request.cmd = OVS_DP_CMD_SET;
ea36840f 281 }
254f2dc8 282 dp_request.name = name;
b7fd5e38 283 dp_request.user_features |= OVS_DP_F_UNALIGNED;
1579cf67 284 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
93451a0a 285 error = dpif_netlink_dp_transact(&dp_request, &dp, &buf);
982b8810
BP
286 if (error) {
287 return error;
c19e6535 288 }
254f2dc8 289
e4516b20 290 error = open_dpif(&dp, dpifp);
8f4a4df5 291 ofpbuf_delete(buf);
e4516b20 292 return error;
c19e6535
BP
293}
294
e4516b20 295static int
93451a0a 296open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp)
c19e6535 297{
93451a0a 298 struct dpif_netlink *dpif;
c19e6535 299
17411ecf 300 dpif = xzalloc(sizeof *dpif);
e4516b20 301 dpif->port_notifier = NULL;
1579cf67 302 fat_rwlock_init(&dpif->upcall_lock);
c19e6535 303
93451a0a 304 dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name,
254f2dc8 305 dp->dp_ifindex, dp->dp_ifindex);
c19e6535 306
254f2dc8 307 dpif->dp_ifindex = dp->dp_ifindex;
c19e6535 308 *dpifp = &dpif->dpif;
e4516b20
BP
309
310 return 0;
96fba48f
BP
311}
312
1579cf67
AW
313/* Destroys the netlink sockets pointed by the elements in 'socksp'
314 * and frees the 'socksp'. */
17411ecf 315static void
09cac43f 316vport_del_socksp__(struct nl_sock **socksp, uint32_t n_socks)
17411ecf 317{
1579cf67 318 size_t i;
17411ecf 319
1579cf67
AW
320 for (i = 0; i < n_socks; i++) {
321 nl_sock_destroy(socksp[i]);
50f80534 322 }
989fd548 323
1579cf67
AW
324 free(socksp);
325}
989fd548 326
1579cf67
AW
327/* Creates an array of netlink sockets. Returns an array of the
328 * corresponding pointers. Records the error in 'error'. */
329static struct nl_sock **
09cac43f 330vport_create_socksp__(uint32_t n_socks, int *error)
1579cf67
AW
331{
332 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
333 size_t i;
334
335 for (i = 0; i < n_socks; i++) {
336 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
337 if (*error) {
338 goto error;
989fd548 339 }
1579cf67 340 }
989fd548 341
1579cf67 342 return socksp;
9fafa796 343
1579cf67 344error:
09cac43f 345 vport_del_socksp__(socksp, n_socks);
989fd548 346
1579cf67
AW
347 return NULL;
348}
349
09cac43f
NR
350#ifdef _WIN32
351static void
352vport_delete_sock_pool(struct dpif_handler *handler)
353 OVS_REQ_WRLOCK(dpif->upcall_lock)
354{
355 if (handler->vport_sock_pool) {
356 uint32_t i;
357 struct dpif_windows_vport_sock *sock_pool =
358 handler->vport_sock_pool;
359
360 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
361 if (sock_pool[i].nl_sock) {
362 nl_sock_unsubscribe_packets(sock_pool[i].nl_sock);
363 nl_sock_destroy(sock_pool[i].nl_sock);
364 sock_pool[i].nl_sock = NULL;
365 }
366 }
367
368 free(handler->vport_sock_pool);
369 handler->vport_sock_pool = NULL;
370 }
371}
372
373static int
374vport_create_sock_pool(struct dpif_handler *handler)
375 OVS_REQ_WRLOCK(dpif->upcall_lock)
376{
377 struct dpif_windows_vport_sock *sock_pool;
378 size_t i;
379 int error = 0;
380
381 sock_pool = xzalloc(VPORT_SOCK_POOL_SIZE * sizeof *sock_pool);
382 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
383 error = nl_sock_create(NETLINK_GENERIC, &sock_pool[i].nl_sock);
384 if (error) {
385 goto error;
386 }
387
388 /* Enable the netlink socket to receive packets. This is equivalent to
389 * calling nl_sock_join_mcgroup() to receive events. */
390 error = nl_sock_subscribe_packets(sock_pool[i].nl_sock);
391 if (error) {
392 goto error;
393 }
394 }
395
396 handler->vport_sock_pool = sock_pool;
397 handler->last_used_pool_idx = 0;
398 return 0;
399
400error:
401 vport_delete_sock_pool(handler);
402 return error;
403}
404
405/* Returns an array pointers to netlink sockets. The sockets are picked from a
406 * pool. Records the error in 'error'. */
407static struct nl_sock **
408vport_create_socksp_windows(struct dpif_netlink *dpif, int *error)
409 OVS_REQ_WRLOCK(dpif->upcall_lock)
410{
411 uint32_t n_socks = dpif->n_handlers;
412 struct nl_sock **socksp;
413 size_t i;
414
415 ovs_assert(n_socks <= 1);
416 socksp = xzalloc(n_socks * sizeof *socksp);
417
418 /* Pick netlink sockets to use in a round-robin fashion from each
419 * handler's pool of sockets. */
420 for (i = 0; i < n_socks; i++) {
421 struct dpif_handler *handler = &dpif->handlers[i];
422 struct dpif_windows_vport_sock *sock_pool = handler->vport_sock_pool;
423 size_t index = handler->last_used_pool_idx;
424
425 /* A pool of sockets is allocated when the handler is initialized. */
426 if (sock_pool == NULL) {
427 free(socksp);
428 *error = EINVAL;
429 return NULL;
430 }
431
432 ovs_assert(index < VPORT_SOCK_POOL_SIZE);
433 socksp[i] = sock_pool[index].nl_sock;
434 socksp[i] = sock_pool[index].nl_sock;
435 ovs_assert(socksp[i]);
436 index = (index == VPORT_SOCK_POOL_SIZE - 1) ? 0 : index + 1;
437 handler->last_used_pool_idx = index;
438 }
439
440 return socksp;
441}
442
443static void
444vport_del_socksp_windows(struct dpif_netlink *dpif, struct nl_sock **socksp)
445{
446 free(socksp);
447}
448#endif /* _WIN32 */
449
450static struct nl_sock **
451vport_create_socksp(struct dpif_netlink *dpif, int *error)
452{
453#ifdef _WIN32
454 return vport_create_socksp_windows(dpif, error);
455#else
456 return vport_create_socksp__(dpif->n_handlers, error);
457#endif
458}
459
460static void
461vport_del_socksp(struct dpif_netlink *dpif, struct nl_sock **socksp)
462{
463#ifdef _WIN32
464 vport_del_socksp_windows(dpif, socksp);
465#else
466 vport_del_socksp__(socksp, dpif->n_handlers);
467#endif
468}
469
1579cf67
AW
470/* Given the array of pointers to netlink sockets 'socksp', returns
471 * the array of corresponding pids. If the 'socksp' is NULL, returns
472 * a single-element array of value 0. */
473static uint32_t *
474vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
475{
476 uint32_t *pids;
477
478 if (!socksp) {
479 pids = xzalloc(sizeof *pids);
480 } else {
481 size_t i;
482
483 pids = xzalloc(n_socks * sizeof *pids);
484 for (i = 0; i < n_socks; i++) {
485 pids[i] = nl_sock_pid(socksp[i]);
486 }
17411ecf 487 }
989fd548 488
1579cf67
AW
489 return pids;
490}
491
492/* Given the port number 'port_idx', extracts the pids of netlink sockets
493 * associated to the port and assigns it to 'upcall_pids'. */
494static bool
93451a0a 495vport_get_pids(struct dpif_netlink *dpif, uint32_t port_idx,
1579cf67
AW
496 uint32_t **upcall_pids)
497{
498 uint32_t *pids;
499 size_t i;
989fd548 500
1579cf67
AW
501 /* Since the nl_sock can only be assigned in either all
502 * or none "dpif->handlers" channels, the following check
503 * would suffice. */
504 if (!dpif->handlers[0].channels[port_idx].sock) {
505 return false;
506 }
09cac43f 507 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
1579cf67
AW
508
509 pids = xzalloc(dpif->n_handlers * sizeof *pids);
510
511 for (i = 0; i < dpif->n_handlers; i++) {
512 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
513 }
514
515 *upcall_pids = pids;
989fd548 516
1579cf67 517 return true;
989fd548
JP
518}
519
520static int
93451a0a 521vport_add_channels(struct dpif_netlink *dpif, odp_port_t port_no,
1579cf67 522 struct nl_sock **socksp)
989fd548
JP
523{
524 struct epoll_event event;
4e022ec0 525 uint32_t port_idx = odp_to_u32(port_no);
1579cf67
AW
526 size_t i, j;
527 int error;
989fd548 528
1579cf67 529 if (dpif->handlers == NULL) {
989fd548
JP
530 return 0;
531 }
532
1579cf67
AW
533 /* We assume that the datapath densely chooses port numbers, which can
534 * therefore be used as an index into 'channels' and 'epoll_events' of
535 * 'dpif->handler'. */
4e022ec0
AW
536 if (port_idx >= dpif->uc_array_size) {
537 uint32_t new_size = port_idx + 1;
989fd548 538
12d76859 539 if (new_size > MAX_PORTS) {
989fd548
JP
540 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
541 dpif_name(&dpif->dpif), port_no);
542 return EFBIG;
543 }
544
1579cf67
AW
545 for (i = 0; i < dpif->n_handlers; i++) {
546 struct dpif_handler *handler = &dpif->handlers[i];
547
548 handler->channels = xrealloc(handler->channels,
549 new_size * sizeof *handler->channels);
550
551 for (j = dpif->uc_array_size; j < new_size; j++) {
552 handler->channels[j].sock = NULL;
553 }
554
555 handler->epoll_events = xrealloc(handler->epoll_events,
556 new_size * sizeof *handler->epoll_events);
989fd548 557
1579cf67 558 }
989fd548
JP
559 dpif->uc_array_size = new_size;
560 }
561
562 memset(&event, 0, sizeof event);
563 event.events = EPOLLIN;
4e022ec0 564 event.data.u32 = port_idx;
989fd548 565
1579cf67
AW
566 for (i = 0; i < dpif->n_handlers; i++) {
567 struct dpif_handler *handler = &dpif->handlers[i];
568
09cac43f 569#ifndef _WIN32
1579cf67
AW
570 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
571 &event) < 0) {
572 error = errno;
573 goto error;
574 }
93451a0a 575#endif
1579cf67
AW
576 dpif->handlers[i].channels[port_idx].sock = socksp[i];
577 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
578 }
989fd548
JP
579
580 return 0;
1579cf67
AW
581
582error:
583 for (j = 0; j < i; j++) {
09cac43f 584#ifndef _WIN32
1579cf67
AW
585 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
586 nl_sock_fd(socksp[j]), NULL);
93451a0a 587#endif
1579cf67
AW
588 dpif->handlers[j].channels[port_idx].sock = NULL;
589 }
590
591 return error;
989fd548
JP
592}
593
594static void
93451a0a 595vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no)
989fd548 596{
4e022ec0 597 uint32_t port_idx = odp_to_u32(port_no);
1579cf67 598 size_t i;
989fd548 599
1579cf67 600 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
989fd548
JP
601 return;
602 }
603
1579cf67
AW
604 /* Since the sock can only be assigned in either all or none
605 * of "dpif->handlers" channels, the following check would
606 * suffice. */
607 if (!dpif->handlers[0].channels[port_idx].sock) {
989fd548
JP
608 return;
609 }
610
1579cf67
AW
611 for (i = 0; i < dpif->n_handlers; i++) {
612 struct dpif_handler *handler = &dpif->handlers[i];
09cac43f 613#ifndef _WIN32
1579cf67
AW
614 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
615 nl_sock_fd(handler->channels[port_idx].sock), NULL);
616 nl_sock_destroy(handler->channels[port_idx].sock);
09cac43f 617#endif
1579cf67
AW
618 handler->channels[port_idx].sock = NULL;
619 handler->event_offset = handler->n_events = 0;
620 }
621}
622
623static void
93451a0a
AS
624destroy_all_channels(struct dpif_netlink *dpif)
625 OVS_REQ_WRLOCK(dpif->upcall_lock)
1579cf67
AW
626{
627 unsigned int i;
628
629 if (!dpif->handlers) {
630 return;
631 }
632
633 for (i = 0; i < dpif->uc_array_size; i++ ) {
93451a0a 634 struct dpif_netlink_vport vport_request;
1579cf67
AW
635 uint32_t upcall_pids = 0;
636
637 /* Since the sock can only be assigned in either all or none
638 * of "dpif->handlers" channels, the following check would
639 * suffice. */
640 if (!dpif->handlers[0].channels[i].sock) {
641 continue;
642 }
643
644 /* Turn off upcalls. */
93451a0a 645 dpif_netlink_vport_init(&vport_request);
1579cf67
AW
646 vport_request.cmd = OVS_VPORT_CMD_SET;
647 vport_request.dp_ifindex = dpif->dp_ifindex;
648 vport_request.port_no = u32_to_odp(i);
a78f446a 649 vport_request.n_upcall_pids = 1;
1579cf67 650 vport_request.upcall_pids = &upcall_pids;
93451a0a 651 dpif_netlink_vport_transact(&vport_request, NULL, NULL);
1579cf67
AW
652
653 vport_del_channels(dpif, u32_to_odp(i));
654 }
655
656 for (i = 0; i < dpif->n_handlers; i++) {
657 struct dpif_handler *handler = &dpif->handlers[i];
658
09cac43f 659 dpif_netlink_handler_uninit(handler);
1579cf67
AW
660 free(handler->epoll_events);
661 free(handler->channels);
662 }
989fd548 663
1579cf67
AW
664 free(dpif->handlers);
665 dpif->handlers = NULL;
666 dpif->n_handlers = 0;
667 dpif->uc_array_size = 0;
17411ecf
JG
668}
669
96fba48f 670static void
93451a0a 671dpif_netlink_close(struct dpif *dpif_)
96fba48f 672{
93451a0a 673 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
c7178a0b 674
e4516b20 675 nl_sock_destroy(dpif->port_notifier);
1579cf67
AW
676
677 fat_rwlock_wrlock(&dpif->upcall_lock);
678 destroy_all_channels(dpif);
679 fat_rwlock_unlock(&dpif->upcall_lock);
680
681 fat_rwlock_destroy(&dpif->upcall_lock);
96fba48f
BP
682 free(dpif);
683}
684
685static int
93451a0a 686dpif_netlink_destroy(struct dpif *dpif_)
96fba48f 687{
93451a0a
AS
688 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
689 struct dpif_netlink_dp dp;
d6569377 690
93451a0a 691 dpif_netlink_dp_init(&dp);
df2c07f4 692 dp.cmd = OVS_DP_CMD_DEL;
254f2dc8 693 dp.dp_ifindex = dpif->dp_ifindex;
93451a0a 694 return dpif_netlink_dp_transact(&dp, NULL, NULL);
96fba48f
BP
695}
696
a36de779 697static bool
93451a0a 698dpif_netlink_run(struct dpif *dpif_)
61eae437 699{
93451a0a 700 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1579cf67 701
61eae437
BP
702 if (dpif->refresh_channels) {
703 dpif->refresh_channels = false;
1579cf67 704 fat_rwlock_wrlock(&dpif->upcall_lock);
93451a0a 705 dpif_netlink_refresh_channels(dpif, dpif->n_handlers);
1579cf67 706 fat_rwlock_unlock(&dpif->upcall_lock);
61eae437 707 }
a36de779 708 return false;
61eae437
BP
709}
710
96fba48f 711static int
93451a0a 712dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
96fba48f 713{
93451a0a 714 struct dpif_netlink_dp dp;
d6569377
BP
715 struct ofpbuf *buf;
716 int error;
717
93451a0a 718 error = dpif_netlink_dp_get(dpif_, &dp, &buf);
d6569377 719 if (!error) {
6a54dedc
BP
720 memset(stats, 0, sizeof *stats);
721
722 if (dp.stats) {
723 stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
724 stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
725 stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
726 stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
727 }
728
729 if (dp.megaflow_stats) {
730 stats->n_masks = dp.megaflow_stats->n_masks;
731 stats->n_mask_hit = get_32aligned_u64(
732 &dp.megaflow_stats->n_mask_hit);
733 } else {
734 stats->n_masks = UINT32_MAX;
735 stats->n_mask_hit = UINT64_MAX;
736 }
d6569377
BP
737 ofpbuf_delete(buf);
738 }
739 return error;
96fba48f
BP
740}
741
b9ad7294 742static const char *
93451a0a 743get_vport_type(const struct dpif_netlink_vport *vport)
b9ad7294
EJ
744{
745 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
746
747 switch (vport->type) {
5ed51209
JS
748 case OVS_VPORT_TYPE_NETDEV: {
749 const char *type = netdev_get_type_from_name(vport->name);
750
751 return type ? type : "system";
752 }
b9ad7294
EJ
753
754 case OVS_VPORT_TYPE_INTERNAL:
755 return "internal";
756
c1fc1411
JG
757 case OVS_VPORT_TYPE_GENEVE:
758 return "geneve";
759
b9ad7294
EJ
760 case OVS_VPORT_TYPE_GRE:
761 return "gre";
762
b9ad7294
EJ
763 case OVS_VPORT_TYPE_VXLAN:
764 return "vxlan";
765
a6ae068b
LJ
766 case OVS_VPORT_TYPE_LISP:
767 return "lisp";
768
4237026e
PS
769 case OVS_VPORT_TYPE_STT:
770 return "stt";
771
b9ad7294
EJ
772 case OVS_VPORT_TYPE_UNSPEC:
773 case __OVS_VPORT_TYPE_MAX:
774 break;
775 }
776
777 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
778 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
779 return "unknown";
780}
781
c060c4cf
EJ
782static enum ovs_vport_type
783netdev_to_ovs_vport_type(const struct netdev *netdev)
784{
785 const char *type = netdev_get_type(netdev);
786
787 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
788 return OVS_VPORT_TYPE_NETDEV;
789 } else if (!strcmp(type, "internal")) {
790 return OVS_VPORT_TYPE_INTERNAL;
4237026e
PS
791 } else if (strstr(type, "stt")) {
792 return OVS_VPORT_TYPE_STT;
c1fc1411
JG
793 } else if (!strcmp(type, "geneve")) {
794 return OVS_VPORT_TYPE_GENEVE;
c060c4cf
EJ
795 } else if (strstr(type, "gre")) {
796 return OVS_VPORT_TYPE_GRE;
c060c4cf
EJ
797 } else if (!strcmp(type, "vxlan")) {
798 return OVS_VPORT_TYPE_VXLAN;
a6ae068b
LJ
799 } else if (!strcmp(type, "lisp")) {
800 return OVS_VPORT_TYPE_LISP;
c060c4cf
EJ
801 } else {
802 return OVS_VPORT_TYPE_UNSPEC;
803 }
804}
805
96fba48f 806static int
93451a0a
AS
807dpif_netlink_port_add__(struct dpif_netlink *dpif, struct netdev *netdev,
808 odp_port_t *port_nop)
b90de034 809 OVS_REQ_WRLOCK(dpif->upcall_lock)
96fba48f 810{
26508d9a 811 const struct netdev_tunnel_config *tnl_cfg;
3aa30359
BP
812 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
813 const char *name = netdev_vport_get_dpif_port(netdev,
814 namebuf, sizeof namebuf);
c3827f61 815 const char *type = netdev_get_type(netdev);
93451a0a 816 struct dpif_netlink_vport request, reply;
c19e6535 817 struct ofpbuf *buf;
26508d9a
KM
818 uint64_t options_stub[64 / 8];
819 struct ofpbuf options;
1579cf67
AW
820 struct nl_sock **socksp = NULL;
821 uint32_t *upcall_pids;
822 int error = 0;
96fba48f 823
1579cf67 824 if (dpif->handlers) {
09cac43f 825 socksp = vport_create_socksp(dpif, &error);
1579cf67 826 if (!socksp) {
989fd548
JP
827 return error;
828 }
829 }
830
93451a0a 831 dpif_netlink_vport_init(&request);
df2c07f4 832 request.cmd = OVS_VPORT_CMD_NEW;
254f2dc8 833 request.dp_ifindex = dpif->dp_ifindex;
c060c4cf 834 request.type = netdev_to_ovs_vport_type(netdev);
df2c07f4 835 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
c283069c
BP
836 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
837 "unsupported type `%s'",
9b00386b 838 dpif_name(&dpif->dpif), name, type);
09cac43f 839 vport_del_socksp(dpif, socksp);
c283069c
BP
840 return EINVAL;
841 }
c19e6535 842 request.name = name;
c3827f61 843
24b019f8 844 if (request.type == OVS_VPORT_TYPE_NETDEV) {
93451a0a 845#ifdef _WIN32
09cac43f 846 /* XXX : Map appropiate Windows handle */
93451a0a 847#else
24b019f8 848 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
93451a0a 849#endif
24b019f8
JP
850 }
851
26508d9a 852 tnl_cfg = netdev_get_tunnel_config(netdev);
526df7d8 853 if (tnl_cfg && (tnl_cfg->dst_port != 0 || tnl_cfg->exts)) {
26508d9a 854 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
526df7d8
TG
855 if (tnl_cfg->dst_port) {
856 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
857 ntohs(tnl_cfg->dst_port));
858 }
859 if (tnl_cfg->exts) {
860 size_t ext_ofs;
861 int i;
862
863 ext_ofs = nl_msg_start_nested(&options, OVS_TUNNEL_ATTR_EXTENSION);
864 for (i = 0; i < 32; i++) {
865 if (tnl_cfg->exts & (1 << i)) {
866 nl_msg_put_flag(&options, i);
867 }
868 }
869 nl_msg_end_nested(&options, ext_ofs);
870 }
6fd6ed71
PS
871 request.options = options.data;
872 request.options_len = options.size;
26508d9a
KM
873 }
874
78a2d59c 875 request.port_no = *port_nop;
1579cf67 876 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
aeaae11f 877 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
1579cf67 878 request.upcall_pids = upcall_pids;
95b1d73a 879
93451a0a 880 error = dpif_netlink_vport_transact(&request, &reply, &buf);
78a2d59c
JP
881 if (!error) {
882 *port_nop = reply.port_no;
2510ba7c 883 } else {
4e022ec0 884 if (error == EBUSY && *port_nop != ODPP_NONE) {
2510ba7c 885 VLOG_INFO("%s: requested port %"PRIu32" is in use",
9b00386b 886 dpif_name(&dpif->dpif), *port_nop);
2510ba7c 887 }
1579cf67 888
09cac43f 889 vport_del_socksp(dpif, socksp);
1579cf67 890 goto exit;
78a2d59c 891 }
c3827f61 892
1579cf67
AW
893 if (socksp) {
894 error = vport_add_channels(dpif, *port_nop, socksp);
989fd548
JP
895 if (error) {
896 VLOG_INFO("%s: could not add channel for port %s",
9b00386b 897 dpif_name(&dpif->dpif), name);
989fd548
JP
898
899 /* Delete the port. */
93451a0a 900 dpif_netlink_vport_init(&request);
989fd548
JP
901 request.cmd = OVS_VPORT_CMD_DEL;
902 request.dp_ifindex = dpif->dp_ifindex;
903 request.port_no = *port_nop;
93451a0a 904 dpif_netlink_vport_transact(&request, NULL, NULL);
09cac43f 905 vport_del_socksp(dpif, socksp);
1579cf67 906 goto exit;
989fd548
JP
907 }
908 }
1579cf67 909 free(socksp);
989fd548 910
1579cf67
AW
911exit:
912 ofpbuf_delete(buf);
913 free(upcall_pids);
914
915 return error;
96fba48f
BP
916}
917
918static int
93451a0a
AS
919dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev,
920 odp_port_t *port_nop)
9fafa796 921{
93451a0a 922 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
9fafa796
BP
923 int error;
924
1579cf67 925 fat_rwlock_wrlock(&dpif->upcall_lock);
93451a0a 926 error = dpif_netlink_port_add__(dpif, netdev, port_nop);
1579cf67 927 fat_rwlock_unlock(&dpif->upcall_lock);
9fafa796
BP
928
929 return error;
930}
931
932static int
93451a0a 933dpif_netlink_port_del__(struct dpif_netlink *dpif, odp_port_t port_no)
b90de034 934 OVS_REQ_WRLOCK(dpif->upcall_lock)
96fba48f 935{
93451a0a 936 struct dpif_netlink_vport vport;
773cd538 937 int error;
c19e6535 938
93451a0a 939 dpif_netlink_vport_init(&vport);
df2c07f4 940 vport.cmd = OVS_VPORT_CMD_DEL;
254f2dc8 941 vport.dp_ifindex = dpif->dp_ifindex;
c19e6535 942 vport.port_no = port_no;
93451a0a 943 error = dpif_netlink_vport_transact(&vport, NULL, NULL);
773cd538 944
1579cf67 945 vport_del_channels(dpif, port_no);
989fd548 946
773cd538 947 return error;
c3827f61 948}
3abc4a1a 949
9fafa796 950static int
93451a0a 951dpif_netlink_port_del(struct dpif *dpif_, odp_port_t port_no)
9fafa796 952{
93451a0a 953 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
9fafa796
BP
954 int error;
955
1579cf67 956 fat_rwlock_wrlock(&dpif->upcall_lock);
93451a0a 957 error = dpif_netlink_port_del__(dpif, port_no);
1579cf67 958 fat_rwlock_unlock(&dpif->upcall_lock);
9fafa796
BP
959
960 return error;
961}
962
c3827f61 963static int
93451a0a
AS
964dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no,
965 const char *port_name, struct dpif_port *dpif_port)
c3827f61 966{
93451a0a
AS
967 struct dpif_netlink_vport request;
968 struct dpif_netlink_vport reply;
c19e6535 969 struct ofpbuf *buf;
4c738a8d
BP
970 int error;
971
93451a0a 972 dpif_netlink_vport_init(&request);
df2c07f4 973 request.cmd = OVS_VPORT_CMD_GET;
9b00386b 974 request.dp_ifindex = dpif->dp_ifindex;
c19e6535
BP
975 request.port_no = port_no;
976 request.name = port_name;
4c738a8d 977
93451a0a 978 error = dpif_netlink_vport_transact(&request, &reply, &buf);
c19e6535 979 if (!error) {
33db1592
BP
980 if (reply.dp_ifindex != request.dp_ifindex) {
981 /* A query by name reported that 'port_name' is in some datapath
982 * other than 'dpif', but the caller wants to know about 'dpif'. */
983 error = ENODEV;
4afba28d 984 } else if (dpif_port) {
33db1592 985 dpif_port->name = xstrdup(reply.name);
b9ad7294 986 dpif_port->type = xstrdup(get_vport_type(&reply));
33db1592
BP
987 dpif_port->port_no = reply.port_no;
988 }
c19e6535 989 ofpbuf_delete(buf);
3abc4a1a 990 }
c19e6535 991 return error;
96fba48f
BP
992}
993
994static int
93451a0a
AS
995dpif_netlink_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
996 struct dpif_port *dpif_port)
96fba48f 997{
93451a0a 998 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
9b00386b 999
93451a0a 1000 return dpif_netlink_port_query__(dpif, port_no, NULL, dpif_port);
96fba48f
BP
1001}
1002
1003static int
93451a0a 1004dpif_netlink_port_query_by_name(const struct dpif *dpif_, const char *devname,
4c738a8d 1005 struct dpif_port *dpif_port)
96fba48f 1006{
93451a0a 1007 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
9b00386b 1008
93451a0a 1009 return dpif_netlink_port_query__(dpif, 0, devname, dpif_port);
96fba48f
BP
1010}
1011
98403001 1012static uint32_t
93451a0a
AS
1013dpif_netlink_port_get_pid__(const struct dpif_netlink *dpif,
1014 odp_port_t port_no, uint32_t hash)
b90de034 1015 OVS_REQ_RDLOCK(dpif->upcall_lock)
98403001 1016{
4e022ec0 1017 uint32_t port_idx = odp_to_u32(port_no);
9fafa796 1018 uint32_t pid = 0;
98403001 1019
f8fc5489 1020 if (dpif->handlers && dpif->uc_array_size > 0) {
4e022ec0 1021 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
989fd548 1022 * channel, since it is not heavily loaded. */
4e022ec0 1023 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
1579cf67
AW
1024 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
1025
17f2748d
AW
1026 /* Needs to check in case the socket pointer is changed in between
1027 * the holding of upcall_lock. A known case happens when the main
1028 * thread deletes the vport while the handler thread is handling
1029 * the upcall from that port. */
1030 if (h->channels[idx].sock) {
1031 pid = nl_sock_pid(h->channels[idx].sock);
1032 }
98403001 1033 }
9fafa796
BP
1034
1035 return pid;
98403001
BP
1036}
1037
b90de034 1038static uint32_t
93451a0a
AS
1039dpif_netlink_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
1040 uint32_t hash)
b90de034 1041{
93451a0a 1042 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
b90de034
AW
1043 uint32_t ret;
1044
1045 fat_rwlock_rdlock(&dpif->upcall_lock);
93451a0a 1046 ret = dpif_netlink_port_get_pid__(dpif, port_no, hash);
b90de034
AW
1047 fat_rwlock_unlock(&dpif->upcall_lock);
1048
1049 return ret;
1050}
1051
96fba48f 1052static int
93451a0a 1053dpif_netlink_flow_flush(struct dpif *dpif_)
96fba48f 1054{
93451a0a
AS
1055 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1056 struct dpif_netlink_flow flow;
37a1300c 1057
93451a0a 1058 dpif_netlink_flow_init(&flow);
df2c07f4 1059 flow.cmd = OVS_FLOW_CMD_DEL;
254f2dc8 1060 flow.dp_ifindex = dpif->dp_ifindex;
93451a0a 1061 return dpif_netlink_flow_transact(&flow, NULL, NULL);
96fba48f
BP
1062}
1063
93451a0a 1064struct dpif_netlink_port_state {
f0fef760 1065 struct nl_dump dump;
d57695d7 1066 struct ofpbuf buf;
c19e6535
BP
1067};
1068
222837c4 1069static void
93451a0a
AS
1070dpif_netlink_port_dump_start__(const struct dpif_netlink *dpif,
1071 struct nl_dump *dump)
96fba48f 1072{
93451a0a 1073 struct dpif_netlink_vport request;
f0fef760
BP
1074 struct ofpbuf *buf;
1075
93451a0a 1076 dpif_netlink_vport_init(&request);
067f1e23 1077 request.cmd = OVS_VPORT_CMD_GET;
254f2dc8 1078 request.dp_ifindex = dpif->dp_ifindex;
f0fef760
BP
1079
1080 buf = ofpbuf_new(1024);
93451a0a 1081 dpif_netlink_vport_to_ofpbuf(&request, buf);
222837c4 1082 nl_dump_start(dump, NETLINK_GENERIC, buf);
f0fef760 1083 ofpbuf_delete(buf);
222837c4
BP
1084}
1085
1086static int
93451a0a 1087dpif_netlink_port_dump_start(const struct dpif *dpif_, void **statep)
222837c4 1088{
93451a0a
AS
1089 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1090 struct dpif_netlink_port_state *state;
222837c4
BP
1091
1092 *statep = state = xmalloc(sizeof *state);
93451a0a 1093 dpif_netlink_port_dump_start__(dpif, &state->dump);
f0fef760 1094
d57695d7 1095 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
b0ec0f27
BP
1096 return 0;
1097}
1098
7c1ef244 1099static int
93451a0a
AS
1100dpif_netlink_port_dump_next__(const struct dpif_netlink *dpif,
1101 struct nl_dump *dump,
1102 struct dpif_netlink_vport *vport,
1103 struct ofpbuf *buffer)
222837c4 1104{
222837c4
BP
1105 struct ofpbuf buf;
1106 int error;
1107
d57695d7 1108 if (!nl_dump_next(dump, &buf, buffer)) {
222837c4
BP
1109 return EOF;
1110 }
1111
93451a0a 1112 error = dpif_netlink_vport_from_ofpbuf(vport, &buf);
222837c4
BP
1113 if (error) {
1114 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
1115 dpif_name(&dpif->dpif), ovs_strerror(error));
1116 }
1117 return error;
1118}
1119
b0ec0f27 1120static int
93451a0a
AS
1121dpif_netlink_port_dump_next(const struct dpif *dpif_, void *state_,
1122 struct dpif_port *dpif_port)
b0ec0f27 1123{
93451a0a
AS
1124 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1125 struct dpif_netlink_port_state *state = state_;
1126 struct dpif_netlink_vport vport;
96fba48f
BP
1127 int error;
1128
93451a0a
AS
1129 error = dpif_netlink_port_dump_next__(dpif, &state->dump, &vport,
1130 &state->buf);
c3827f61 1131 if (error) {
f0fef760 1132 return error;
c3827f61 1133 }
ebc56baa 1134 dpif_port->name = CONST_CAST(char *, vport.name);
b9ad7294 1135 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
f0fef760
BP
1136 dpif_port->port_no = vport.port_no;
1137 return 0;
b0ec0f27
BP
1138}
1139
1140static int
93451a0a 1141dpif_netlink_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
b0ec0f27 1142{
93451a0a 1143 struct dpif_netlink_port_state *state = state_;
f0fef760 1144 int error = nl_dump_done(&state->dump);
8522b383 1145
d57695d7 1146 ofpbuf_uninit(&state->buf);
b0ec0f27 1147 free(state);
f0fef760 1148 return error;
96fba48f
BP
1149}
1150
e9e28be3 1151static int
93451a0a 1152dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep)
e9e28be3 1153{
93451a0a 1154 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
e9e28be3 1155
e4516b20
BP
1156 /* Lazily create the Netlink socket to listen for notifications. */
1157 if (!dpif->port_notifier) {
1158 struct nl_sock *sock;
1159 int error;
1160
1161 error = nl_sock_create(NETLINK_GENERIC, &sock);
1162 if (error) {
1163 return error;
1164 }
1165
1166 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
1167 if (error) {
1168 nl_sock_destroy(sock);
1169 return error;
1170 }
1171 dpif->port_notifier = sock;
1172
1173 /* We have no idea of the current state so report that everything
1174 * changed. */
1175 return ENOBUFS;
1176 }
1177
1178 for (;;) {
1179 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1180 uint64_t buf_stub[4096 / 8];
1181 struct ofpbuf buf;
1182 int error;
1183
1184 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
1185 error = nl_sock_recv(dpif->port_notifier, &buf, false);
1186 if (!error) {
93451a0a 1187 struct dpif_netlink_vport vport;
e4516b20 1188
93451a0a 1189 error = dpif_netlink_vport_from_ofpbuf(&vport, &buf);
e4516b20
BP
1190 if (!error) {
1191 if (vport.dp_ifindex == dpif->dp_ifindex
1192 && (vport.cmd == OVS_VPORT_CMD_NEW
1193 || vport.cmd == OVS_VPORT_CMD_DEL
1194 || vport.cmd == OVS_VPORT_CMD_SET)) {
1195 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1196 dpif->dpif.full_name, vport.name, vport.cmd);
1579cf67 1197 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
61eae437
BP
1198 dpif->refresh_channels = true;
1199 }
e4516b20 1200 *devnamep = xstrdup(vport.name);
59e0c910 1201 ofpbuf_uninit(&buf);
e4516b20 1202 return 0;
e4516b20
BP
1203 }
1204 }
59e0c910
BP
1205 } else if (error != EAGAIN) {
1206 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1207 ovs_strerror(error));
1208 nl_sock_drain(dpif->port_notifier);
1209 error = ENOBUFS;
e4516b20
BP
1210 }
1211
59e0c910
BP
1212 ofpbuf_uninit(&buf);
1213 if (error) {
1214 return error;
1215 }
e9e28be3 1216 }
e9e28be3
BP
1217}
1218
1219static void
93451a0a 1220dpif_netlink_port_poll_wait(const struct dpif *dpif_)
e9e28be3 1221{
93451a0a 1222 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
e4516b20
BP
1223
1224 if (dpif->port_notifier) {
1225 nl_sock_wait(dpif->port_notifier, POLLIN);
1226 } else {
e9e28be3 1227 poll_immediate_wake();
e9e28be3
BP
1228 }
1229}
1230
6fe09f8c 1231static void
70e5ed6f
JS
1232dpif_netlink_flow_init_ufid(struct dpif_netlink_flow *request,
1233 const ovs_u128 *ufid, bool terse)
1234{
1235 if (ufid) {
1236 request->ufid = *ufid;
1237 request->ufid_present = true;
1238 } else {
1239 request->ufid_present = false;
1240 }
1241 request->ufid_terse = terse;
1242}
1243
1244static void
1245dpif_netlink_init_flow_get__(const struct dpif_netlink *dpif,
1246 const struct nlattr *key, size_t key_len,
1247 const ovs_u128 *ufid, bool terse,
1248 struct dpif_netlink_flow *request)
96fba48f 1249{
93451a0a 1250 dpif_netlink_flow_init(request);
6fe09f8c
JS
1251 request->cmd = OVS_FLOW_CMD_GET;
1252 request->dp_ifindex = dpif->dp_ifindex;
1253 request->key = key;
1254 request->key_len = key_len;
70e5ed6f
JS
1255 dpif_netlink_flow_init_ufid(request, ufid, terse);
1256}
1257
1258static void
1259dpif_netlink_init_flow_get(const struct dpif_netlink *dpif,
1260 const struct dpif_flow_get *get,
1261 struct dpif_netlink_flow *request)
1262{
1263 dpif_netlink_init_flow_get__(dpif, get->key, get->key_len, get->ufid,
1264 false, request);
30053024
BP
1265}
1266
1267static int
70e5ed6f
JS
1268dpif_netlink_flow_get__(const struct dpif_netlink *dpif,
1269 const struct nlattr *key, size_t key_len,
1270 const ovs_u128 *ufid, bool terse,
1271 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
30053024 1272{
93451a0a 1273 struct dpif_netlink_flow request;
30053024 1274
70e5ed6f 1275 dpif_netlink_init_flow_get__(dpif, key, key_len, ufid, terse, &request);
93451a0a 1276 return dpif_netlink_flow_transact(&request, reply, bufp);
96fba48f
BP
1277}
1278
70e5ed6f
JS
1279static int
1280dpif_netlink_flow_get(const struct dpif_netlink *dpif,
1281 const struct dpif_netlink_flow *flow,
1282 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1283{
1284 return dpif_netlink_flow_get__(dpif, flow->key, flow->key_len,
1285 flow->ufid_present ? &flow->ufid : NULL,
1286 false, reply, bufp);
1287}
1288
6bc60024 1289static void
93451a0a
AS
1290dpif_netlink_init_flow_put(struct dpif_netlink *dpif,
1291 const struct dpif_flow_put *put,
1292 struct dpif_netlink_flow *request)
6bc60024 1293{
d64e176c 1294 static const struct nlattr dummy_action;
6bc60024 1295
93451a0a 1296 dpif_netlink_flow_init(request);
89625d1e 1297 request->cmd = (put->flags & DPIF_FP_CREATE
6bc60024
BP
1298 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1299 request->dp_ifindex = dpif->dp_ifindex;
89625d1e
BP
1300 request->key = put->key;
1301 request->key_len = put->key_len;
e6cc0bab
AZ
1302 request->mask = put->mask;
1303 request->mask_len = put->mask_len;
70e5ed6f
JS
1304 dpif_netlink_flow_init_ufid(request, put->ufid, false);
1305
6bc60024 1306 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
d64e176c
BP
1307 request->actions = (put->actions
1308 ? put->actions
1309 : CONST_CAST(struct nlattr *, &dummy_action));
89625d1e
BP
1310 request->actions_len = put->actions_len;
1311 if (put->flags & DPIF_FP_ZERO_STATS) {
6bc60024
BP
1312 request->clear = true;
1313 }
43f9ac0a
JR
1314 if (put->flags & DPIF_FP_PROBE) {
1315 request->probe = true;
1316 }
89625d1e 1317 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
6bc60024
BP
1318}
1319
b99d3cee 1320static void
70e5ed6f
JS
1321dpif_netlink_init_flow_del__(struct dpif_netlink *dpif,
1322 const struct nlattr *key, size_t key_len,
1323 const ovs_u128 *ufid, bool terse,
1324 struct dpif_netlink_flow *request)
96fba48f 1325{
93451a0a 1326 dpif_netlink_flow_init(request);
b99d3cee
BP
1327 request->cmd = OVS_FLOW_CMD_DEL;
1328 request->dp_ifindex = dpif->dp_ifindex;
70e5ed6f
JS
1329 request->key = key;
1330 request->key_len = key_len;
1331 dpif_netlink_flow_init_ufid(request, ufid, terse);
1332}
1333
1334static void
1335dpif_netlink_init_flow_del(struct dpif_netlink *dpif,
1336 const struct dpif_flow_del *del,
1337 struct dpif_netlink_flow *request)
1338{
37382aa6
AS
1339 dpif_netlink_init_flow_del__(dpif, del->key, del->key_len,
1340 del->ufid, del->terse, request);
70e5ed6f
JS
1341}
1342
93451a0a 1343struct dpif_netlink_flow_dump {
ac64794a
BP
1344 struct dpif_flow_dump up;
1345 struct nl_dump nl_dump;
d2ad7ef1 1346 atomic_int status;
e723fd32
JS
1347};
1348
93451a0a
AS
1349static struct dpif_netlink_flow_dump *
1350dpif_netlink_flow_dump_cast(struct dpif_flow_dump *dump)
e723fd32 1351{
93451a0a 1352 return CONTAINER_OF(dump, struct dpif_netlink_flow_dump, up);
e723fd32
JS
1353}
1354
ac64794a 1355static struct dpif_flow_dump *
64bb477f 1356dpif_netlink_flow_dump_create(const struct dpif *dpif_, bool terse)
96fba48f 1357{
93451a0a
AS
1358 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1359 struct dpif_netlink_flow_dump *dump;
1360 struct dpif_netlink_flow request;
37a1300c
BP
1361 struct ofpbuf *buf;
1362
ac64794a
BP
1363 dump = xmalloc(sizeof *dump);
1364 dpif_flow_dump_init(&dump->up, dpif_);
37a1300c 1365
93451a0a 1366 dpif_netlink_flow_init(&request);
067f1e23 1367 request.cmd = OVS_FLOW_CMD_GET;
254f2dc8 1368 request.dp_ifindex = dpif->dp_ifindex;
64bb477f
JS
1369 request.ufid_present = false;
1370 request.ufid_terse = terse;
37a1300c
BP
1371
1372 buf = ofpbuf_new(1024);
93451a0a 1373 dpif_netlink_flow_to_ofpbuf(&request, buf);
ac64794a 1374 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
37a1300c 1375 ofpbuf_delete(buf);
ac64794a 1376 atomic_init(&dump->status, 0);
64bb477f 1377 dump->up.terse = terse;
30053024 1378
ac64794a 1379 return &dump->up;
704a1e09
BP
1380}
1381
1382static int
93451a0a 1383dpif_netlink_flow_dump_destroy(struct dpif_flow_dump *dump_)
704a1e09 1384{
93451a0a 1385 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
ac64794a
BP
1386 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1387 int dump_status;
96fba48f 1388
7424fc44
JR
1389 /* No other thread has access to 'dump' at this point. */
1390 atomic_read_relaxed(&dump->status, &dump_status);
ac64794a
BP
1391 free(dump);
1392 return dump_status ? dump_status : nl_status;
1393}
feebdea2 1394
93451a0a 1395struct dpif_netlink_flow_dump_thread {
ac64794a 1396 struct dpif_flow_dump_thread up;
93451a0a
AS
1397 struct dpif_netlink_flow_dump *dump;
1398 struct dpif_netlink_flow flow;
ac64794a
BP
1399 struct dpif_flow_stats stats;
1400 struct ofpbuf nl_flows; /* Always used to store flows. */
1401 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1402};
1403
93451a0a
AS
1404static struct dpif_netlink_flow_dump_thread *
1405dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
ac64794a 1406{
93451a0a 1407 return CONTAINER_OF(thread, struct dpif_netlink_flow_dump_thread, up);
ac64794a
BP
1408}
1409
1410static struct dpif_flow_dump_thread *
93451a0a 1411dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump *dump_)
ac64794a 1412{
93451a0a
AS
1413 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1414 struct dpif_netlink_flow_dump_thread *thread;
ac64794a
BP
1415
1416 thread = xmalloc(sizeof *thread);
1417 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1418 thread->dump = dump;
1419 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1420 thread->nl_actions = NULL;
1421
1422 return &thread->up;
1423}
1424
1425static void
93451a0a 1426dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
ac64794a 1427{
93451a0a
AS
1428 struct dpif_netlink_flow_dump_thread *thread
1429 = dpif_netlink_flow_dump_thread_cast(thread_);
ac64794a
BP
1430
1431 ofpbuf_uninit(&thread->nl_flows);
1432 ofpbuf_delete(thread->nl_actions);
1433 free(thread);
1434}
1435
1436static void
7af12bd7 1437dpif_netlink_flow_to_dpif_flow(struct dpif *dpif, struct dpif_flow *dpif_flow,
7fe98598 1438 const struct dpif_netlink_flow *datapath_flow)
ac64794a 1439{
7fe98598
NR
1440 dpif_flow->key = datapath_flow->key;
1441 dpif_flow->key_len = datapath_flow->key_len;
1442 dpif_flow->mask = datapath_flow->mask;
1443 dpif_flow->mask_len = datapath_flow->mask_len;
1444 dpif_flow->actions = datapath_flow->actions;
1445 dpif_flow->actions_len = datapath_flow->actions_len;
70e5ed6f 1446 dpif_flow->ufid_present = datapath_flow->ufid_present;
ec97c2df 1447 dpif_flow->pmd_id = PMD_ID_NULL;
70e5ed6f
JS
1448 if (datapath_flow->ufid_present) {
1449 dpif_flow->ufid = datapath_flow->ufid;
1450 } else {
1451 ovs_assert(datapath_flow->key && datapath_flow->key_len);
1452 dpif_flow_hash(dpif, datapath_flow->key, datapath_flow->key_len,
1453 &dpif_flow->ufid);
1454 }
7fe98598 1455 dpif_netlink_flow_get_stats(datapath_flow, &dpif_flow->stats);
ac64794a
BP
1456}
1457
1458static int
93451a0a
AS
1459dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1460 struct dpif_flow *flows, int max_flows)
ac64794a 1461{
93451a0a
AS
1462 struct dpif_netlink_flow_dump_thread *thread
1463 = dpif_netlink_flow_dump_thread_cast(thread_);
1464 struct dpif_netlink_flow_dump *dump = thread->dump;
1465 struct dpif_netlink *dpif = dpif_netlink_cast(thread->up.dpif);
ac64794a
BP
1466 int n_flows;
1467
1468 ofpbuf_delete(thread->nl_actions);
1469 thread->nl_actions = NULL;
1470
1471 n_flows = 0;
1472 while (!n_flows
6fd6ed71 1473 || (n_flows < max_flows && thread->nl_flows.size)) {
7fe98598 1474 struct dpif_netlink_flow datapath_flow;
ac64794a
BP
1475 struct ofpbuf nl_flow;
1476 int error;
1477
1478 /* Try to grab another flow. */
1479 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1480 break;
feebdea2 1481 }
30053024 1482
ac64794a 1483 /* Convert the flow to our output format. */
7fe98598 1484 error = dpif_netlink_flow_from_ofpbuf(&datapath_flow, &nl_flow);
30053024 1485 if (error) {
7424fc44 1486 atomic_store_relaxed(&dump->status, error);
ac64794a 1487 break;
feebdea2 1488 }
30053024 1489
64bb477f
JS
1490 if (dump->up.terse || datapath_flow.actions) {
1491 /* Common case: we don't want actions, or the flow includes
1492 * actions. */
7af12bd7
JS
1493 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1494 &datapath_flow);
ac64794a
BP
1495 } else {
1496 /* Rare case: the flow does not include actions. Retrieve this
1497 * individual flow again to get the actions. */
70e5ed6f 1498 error = dpif_netlink_flow_get(dpif, &datapath_flow,
7fe98598 1499 &datapath_flow, &thread->nl_actions);
30053024
BP
1500 if (error == ENOENT) {
1501 VLOG_DBG("dumped flow disappeared on get");
ac64794a 1502 continue;
30053024 1503 } else if (error) {
10a89ef0
BP
1504 VLOG_WARN("error fetching dumped flow: %s",
1505 ovs_strerror(error));
7424fc44 1506 atomic_store_relaxed(&dump->status, error);
ac64794a 1507 break;
30053024 1508 }
30053024 1509
ac64794a
BP
1510 /* Save this flow. Then exit, because we only have one buffer to
1511 * handle this case. */
7af12bd7
JS
1512 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1513 &datapath_flow);
ac64794a
BP
1514 break;
1515 }
feebdea2 1516 }
ac64794a 1517 return n_flows;
96fba48f
BP
1518}
1519
eabe7c68 1520static void
93451a0a
AS
1521dpif_netlink_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1522 struct ofpbuf *buf)
96fba48f 1523{
89625d1e 1524 struct ovs_header *k_exec;
758c456d 1525 size_t key_ofs;
f7cd0081 1526
eabe7c68 1527 ofpbuf_prealloc_tailroom(buf, (64
cf62fa4c 1528 + dp_packet_size(d_exec->packet)
758c456d 1529 + ODP_KEY_METADATA_SIZE
eabe7c68 1530 + d_exec->actions_len));
f7cd0081 1531
df2c07f4 1532 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
69685a88 1533 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
f7cd0081 1534
89625d1e
BP
1535 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1536 k_exec->dp_ifindex = dp_ifindex;
f7cd0081 1537
89625d1e 1538 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
cf62fa4c
PS
1539 dp_packet_data(d_exec->packet),
1540 dp_packet_size(d_exec->packet));
758c456d
JR
1541
1542 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
cf62fa4c 1543 odp_key_from_pkt_metadata(buf, &d_exec->packet->md);
758c456d
JR
1544 nl_msg_end_nested(buf, key_ofs);
1545
89625d1e
BP
1546 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1547 d_exec->actions, d_exec->actions_len);
43f9ac0a 1548 if (d_exec->probe) {
2e460098 1549 nl_msg_put_flag(buf, OVS_PACKET_ATTR_PROBE);
43f9ac0a 1550 }
27130224
AZ
1551 if (d_exec->mtu) {
1552 nl_msg_put_u16(buf, OVS_PACKET_ATTR_MRU, d_exec->mtu);
1553 }
6bc60024
BP
1554}
1555
0f3358ea
BP
1556/* Executes, against 'dpif', up to the first 'n_ops' operations in 'ops'.
1557 * Returns the number actually executed (at least 1, if 'n_ops' is
1558 * positive). */
1559static size_t
93451a0a
AS
1560dpif_netlink_operate__(struct dpif_netlink *dpif,
1561 struct dpif_op **ops, size_t n_ops)
6bc60024 1562{
0f3358ea
BP
1563 enum { MAX_OPS = 50 };
1564
eabe7c68
BP
1565 struct op_auxdata {
1566 struct nl_transaction txn;
72d32ac0 1567
eabe7c68
BP
1568 struct ofpbuf request;
1569 uint64_t request_stub[1024 / 8];
72d32ac0
BP
1570
1571 struct ofpbuf reply;
1572 uint64_t reply_stub[1024 / 8];
eabe7c68
BP
1573 } auxes[MAX_OPS];
1574
1575 struct nl_transaction *txnsp[MAX_OPS];
6bc60024
BP
1576 size_t i;
1577
0f3358ea 1578 n_ops = MIN(n_ops, MAX_OPS);
6bc60024 1579 for (i = 0; i < n_ops; i++) {
eabe7c68 1580 struct op_auxdata *aux = &auxes[i];
c2b565b5 1581 struct dpif_op *op = ops[i];
b99d3cee
BP
1582 struct dpif_flow_put *put;
1583 struct dpif_flow_del *del;
6fe09f8c 1584 struct dpif_flow_get *get;
93451a0a 1585 struct dpif_netlink_flow flow;
eabe7c68
BP
1586
1587 ofpbuf_use_stub(&aux->request,
1588 aux->request_stub, sizeof aux->request_stub);
1589 aux->txn.request = &aux->request;
b99d3cee 1590
72d32ac0
BP
1591 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1592 aux->txn.reply = NULL;
1593
b99d3cee
BP
1594 switch (op->type) {
1595 case DPIF_OP_FLOW_PUT:
1596 put = &op->u.flow_put;
93451a0a 1597 dpif_netlink_init_flow_put(dpif, put, &flow);
6bc60024 1598 if (put->stats) {
eabe7c68 1599 flow.nlmsg_flags |= NLM_F_ECHO;
72d32ac0 1600 aux->txn.reply = &aux->reply;
6bc60024 1601 }
93451a0a 1602 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
b99d3cee
BP
1603 break;
1604
1605 case DPIF_OP_FLOW_DEL:
1606 del = &op->u.flow_del;
93451a0a 1607 dpif_netlink_init_flow_del(dpif, del, &flow);
b99d3cee 1608 if (del->stats) {
eabe7c68 1609 flow.nlmsg_flags |= NLM_F_ECHO;
72d32ac0 1610 aux->txn.reply = &aux->reply;
b99d3cee 1611 }
93451a0a 1612 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
b99d3cee 1613 break;
6bc60024 1614
b99d3cee 1615 case DPIF_OP_EXECUTE:
0f3358ea
BP
1616 /* Can't execute a packet that won't fit in a Netlink attribute. */
1617 if (OVS_UNLIKELY(nl_attr_oversized(
cf62fa4c 1618 dp_packet_size(op->u.execute.packet)))) {
0f3358ea
BP
1619 /* Report an error immediately if this is the first operation.
1620 * Otherwise the easiest thing to do is to postpone to the next
1621 * call (when this will be the first operation). */
1622 if (i == 0) {
1623 VLOG_ERR_RL(&error_rl,
1624 "dropping oversized %"PRIu32"-byte packet",
cf62fa4c 1625 dp_packet_size(op->u.execute.packet));
0f3358ea
BP
1626 op->error = ENOBUFS;
1627 return 1;
1628 }
1629 n_ops = i;
1630 } else {
1631 dpif_netlink_encode_execute(dpif->dp_ifindex, &op->u.execute,
1632 &aux->request);
1633 }
b99d3cee
BP
1634 break;
1635
6fe09f8c
JS
1636 case DPIF_OP_FLOW_GET:
1637 get = &op->u.flow_get;
70e5ed6f 1638 dpif_netlink_init_flow_get(dpif, get, &flow);
6fe09f8c 1639 aux->txn.reply = get->buffer;
93451a0a 1640 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
6fe09f8c
JS
1641 break;
1642
b99d3cee 1643 default:
428b2edd 1644 OVS_NOT_REACHED();
6bc60024
BP
1645 }
1646 }
1647
6bc60024 1648 for (i = 0; i < n_ops; i++) {
eabe7c68 1649 txnsp[i] = &auxes[i].txn;
6bc60024 1650 }
a88b4e04 1651 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
6bc60024 1652
6bc60024 1653 for (i = 0; i < n_ops; i++) {
72d32ac0 1654 struct op_auxdata *aux = &auxes[i];
eabe7c68 1655 struct nl_transaction *txn = &auxes[i].txn;
c2b565b5 1656 struct dpif_op *op = ops[i];
b99d3cee
BP
1657 struct dpif_flow_put *put;
1658 struct dpif_flow_del *del;
6fe09f8c 1659 struct dpif_flow_get *get;
6bc60024 1660
b99d3cee 1661 op->error = txn->error;
6bc60024 1662
b99d3cee
BP
1663 switch (op->type) {
1664 case DPIF_OP_FLOW_PUT:
1665 put = &op->u.flow_put;
cfceb2b5 1666 if (put->stats) {
b99d3cee 1667 if (!op->error) {
93451a0a 1668 struct dpif_netlink_flow reply;
cfceb2b5 1669
93451a0a
AS
1670 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1671 txn->reply);
cfceb2b5 1672 if (!op->error) {
93451a0a 1673 dpif_netlink_flow_get_stats(&reply, put->stats);
cfceb2b5
BP
1674 }
1675 }
6bc60024 1676 }
b99d3cee
BP
1677 break;
1678
1679 case DPIF_OP_FLOW_DEL:
1680 del = &op->u.flow_del;
cfceb2b5 1681 if (del->stats) {
b99d3cee 1682 if (!op->error) {
93451a0a 1683 struct dpif_netlink_flow reply;
cfceb2b5 1684
93451a0a
AS
1685 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1686 txn->reply);
cfceb2b5 1687 if (!op->error) {
93451a0a 1688 dpif_netlink_flow_get_stats(&reply, del->stats);
cfceb2b5
BP
1689 }
1690 }
b99d3cee
BP
1691 }
1692 break;
1693
1694 case DPIF_OP_EXECUTE:
1695 break;
1696
6fe09f8c
JS
1697 case DPIF_OP_FLOW_GET:
1698 get = &op->u.flow_get;
1699 if (!op->error) {
93451a0a 1700 struct dpif_netlink_flow reply;
6fe09f8c 1701
93451a0a 1702 op->error = dpif_netlink_flow_from_ofpbuf(&reply, txn->reply);
6fe09f8c 1703 if (!op->error) {
7af12bd7
JS
1704 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, get->flow,
1705 &reply);
6fe09f8c
JS
1706 }
1707 }
1708 break;
1709
b99d3cee 1710 default:
428b2edd 1711 OVS_NOT_REACHED();
6bc60024
BP
1712 }
1713
72d32ac0
BP
1714 ofpbuf_uninit(&aux->request);
1715 ofpbuf_uninit(&aux->reply);
6bc60024 1716 }
0f3358ea
BP
1717
1718 return n_ops;
eabe7c68
BP
1719}
1720
1721static void
93451a0a 1722dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
eabe7c68 1723{
93451a0a 1724 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
9b00386b 1725
eabe7c68 1726 while (n_ops > 0) {
0f3358ea 1727 size_t chunk = dpif_netlink_operate__(dpif, ops, n_ops);
eabe7c68
BP
1728 ops += chunk;
1729 n_ops -= chunk;
1730 }
6bc60024
BP
1731}
1732
09cac43f
NR
1733#if _WIN32
1734static void
1735dpif_netlink_handler_uninit(struct dpif_handler *handler)
1736{
1737 vport_delete_sock_pool(handler);
1738}
1739
1740static int
1741dpif_netlink_handler_init(struct dpif_handler *handler)
1742{
1743 return vport_create_sock_pool(handler);
1744}
1745#else
1746
1747static int
1748dpif_netlink_handler_init(struct dpif_handler *handler)
1749{
1750 handler->epoll_fd = epoll_create(10);
1751 return handler->epoll_fd < 0 ? errno : 0;
1752}
1753
1754static void
1755dpif_netlink_handler_uninit(struct dpif_handler *handler)
1756{
1757 close(handler->epoll_fd);
1758}
1759#endif
1760
1579cf67
AW
1761/* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1762 * currently in 'dpif' in the kernel, by adding a new set of channels for
1763 * any kernel vport that lacks one and deleting any channels that have no
1764 * backing kernel vports. */
96fba48f 1765static int
93451a0a 1766dpif_netlink_refresh_channels(struct dpif_netlink *dpif, uint32_t n_handlers)
b90de034 1767 OVS_REQ_WRLOCK(dpif->upcall_lock)
96fba48f 1768{
8381a3d3 1769 unsigned long int *keep_channels;
93451a0a 1770 struct dpif_netlink_vport vport;
8381a3d3
BP
1771 size_t keep_channels_nbits;
1772 struct nl_dump dump;
d57695d7
JS
1773 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1774 struct ofpbuf buf;
8381a3d3
BP
1775 int retval = 0;
1776 size_t i;
982b8810 1777
09cac43f
NR
1778 ovs_assert(!WINDOWS || n_handlers <= 1);
1779 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
1780
1579cf67
AW
1781 if (dpif->n_handlers != n_handlers) {
1782 destroy_all_channels(dpif);
1783 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1784 for (i = 0; i < n_handlers; i++) {
09cac43f 1785 int error;
1579cf67
AW
1786 struct dpif_handler *handler = &dpif->handlers[i];
1787
09cac43f
NR
1788 error = dpif_netlink_handler_init(handler);
1789 if (error) {
1579cf67 1790 size_t j;
09cac43f
NR
1791 struct dpif_handler *tmp = &dpif->handlers[i];
1792
1579cf67
AW
1793
1794 for (j = 0; j < i; j++) {
09cac43f 1795 dpif_netlink_handler_uninit(tmp);
1579cf67
AW
1796 }
1797 free(dpif->handlers);
1798 dpif->handlers = NULL;
1799
09cac43f 1800 return error;
1579cf67 1801 }
8381a3d3 1802 }
1579cf67
AW
1803 dpif->n_handlers = n_handlers;
1804 }
1805
1806 for (i = 0; i < n_handlers; i++) {
1807 struct dpif_handler *handler = &dpif->handlers[i];
1808
1809 handler->event_offset = handler->n_events = 0;
17411ecf 1810 }
b063d9f0 1811
8381a3d3
BP
1812 keep_channels_nbits = dpif->uc_array_size;
1813 keep_channels = bitmap_allocate(keep_channels_nbits);
982b8810 1814
d57695d7 1815 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
93451a0a
AS
1816 dpif_netlink_port_dump_start__(dpif, &dump);
1817 while (!dpif_netlink_port_dump_next__(dpif, &dump, &vport, &buf)) {
8381a3d3 1818 uint32_t port_no = odp_to_u32(vport.port_no);
1579cf67 1819 uint32_t *upcall_pids = NULL;
8381a3d3 1820 int error;
50f80534 1821
1579cf67
AW
1822 if (port_no >= dpif->uc_array_size
1823 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
09cac43f 1824 struct nl_sock **socksp = vport_create_socksp(dpif, &error);
1579cf67
AW
1825
1826 if (!socksp) {
1827 goto error;
1828 }
1829
1830 error = vport_add_channels(dpif, vport.port_no, socksp);
b063d9f0 1831 if (error) {
1579cf67 1832 VLOG_INFO("%s: could not add channels for port %s",
9b00386b 1833 dpif_name(&dpif->dpif), vport.name);
09cac43f 1834 vport_del_socksp(dpif, socksp);
8381a3d3
BP
1835 retval = error;
1836 goto error;
982b8810 1837 }
1579cf67
AW
1838 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1839 free(socksp);
8381a3d3 1840 }
50f80534 1841
8381a3d3 1842 /* Configure the vport to deliver misses to 'sock'. */
1579cf67
AW
1843 if (vport.upcall_pids[0] == 0
1844 || vport.n_upcall_pids != dpif->n_handlers
1845 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1846 *upcall_pids)) {
93451a0a 1847 struct dpif_netlink_vport vport_request;
989fd548 1848
93451a0a 1849 dpif_netlink_vport_init(&vport_request);
989fd548
JP
1850 vport_request.cmd = OVS_VPORT_CMD_SET;
1851 vport_request.dp_ifindex = dpif->dp_ifindex;
8381a3d3 1852 vport_request.port_no = vport.port_no;
1579cf67
AW
1853 vport_request.n_upcall_pids = dpif->n_handlers;
1854 vport_request.upcall_pids = upcall_pids;
93451a0a 1855 error = dpif_netlink_vport_transact(&vport_request, NULL, NULL);
1579cf67 1856 if (error) {
989fd548
JP
1857 VLOG_WARN_RL(&error_rl,
1858 "%s: failed to set upcall pid on port: %s",
10a89ef0 1859 dpif_name(&dpif->dpif), ovs_strerror(error));
989fd548 1860
8381a3d3
BP
1861 if (error != ENODEV && error != ENOENT) {
1862 retval = error;
989fd548 1863 } else {
8381a3d3
BP
1864 /* The vport isn't really there, even though the dump says
1865 * it is. Probably we just hit a race after a port
1866 * disappeared. */
989fd548 1867 }
8381a3d3 1868 goto error;
50f80534 1869 }
8381a3d3 1870 }
14b4d2f9 1871
8381a3d3
BP
1872 if (port_no < keep_channels_nbits) {
1873 bitmap_set1(keep_channels, port_no);
1874 }
1579cf67 1875 free(upcall_pids);
8381a3d3
BP
1876 continue;
1877
1878 error:
1579cf67
AW
1879 free(upcall_pids);
1880 vport_del_channels(dpif, vport.port_no);
982b8810 1881 }
8381a3d3 1882 nl_dump_done(&dump);
d57695d7 1883 ofpbuf_uninit(&buf);
b063d9f0 1884
8381a3d3
BP
1885 /* Discard any saved channels that we didn't reuse. */
1886 for (i = 0; i < keep_channels_nbits; i++) {
1887 if (!bitmap_is_set(keep_channels, i)) {
1579cf67 1888 vport_del_channels(dpif, u32_to_odp(i));
8381a3d3
BP
1889 }
1890 }
1891 free(keep_channels);
1892
1893 return retval;
1894}
1895
1896static int
93451a0a 1897dpif_netlink_recv_set__(struct dpif_netlink *dpif, bool enable)
b90de034 1898 OVS_REQ_WRLOCK(dpif->upcall_lock)
8381a3d3 1899{
1579cf67 1900 if ((dpif->handlers != NULL) == enable) {
8381a3d3
BP
1901 return 0;
1902 } else if (!enable) {
1579cf67 1903 destroy_all_channels(dpif);
8381a3d3
BP
1904 return 0;
1905 } else {
93451a0a 1906 return dpif_netlink_refresh_channels(dpif, 1);
8381a3d3 1907 }
96fba48f
BP
1908}
1909
9fafa796 1910static int
93451a0a 1911dpif_netlink_recv_set(struct dpif *dpif_, bool enable)
9fafa796 1912{
93451a0a 1913 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
9fafa796
BP
1914 int error;
1915
1579cf67 1916 fat_rwlock_wrlock(&dpif->upcall_lock);
93451a0a 1917 error = dpif_netlink_recv_set__(dpif, enable);
1579cf67 1918 fat_rwlock_unlock(&dpif->upcall_lock);
9fafa796
BP
1919
1920 return error;
1921}
1922
1954e6bb 1923static int
93451a0a 1924dpif_netlink_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1954e6bb 1925{
93451a0a 1926 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1579cf67
AW
1927 int error = 0;
1928
09cac43f
NR
1929#ifdef _WIN32
1930 /* Multiple upcall handlers will be supported once kernel datapath supports
1931 * it. */
1932 if (n_handlers > 1) {
1933 return error;
1934 }
1935#endif
1936
1579cf67
AW
1937 fat_rwlock_wrlock(&dpif->upcall_lock);
1938 if (dpif->handlers) {
93451a0a 1939 error = dpif_netlink_refresh_channels(dpif, n_handlers);
1579cf67
AW
1940 }
1941 fat_rwlock_unlock(&dpif->upcall_lock);
1942
1943 return error;
1954e6bb
AW
1944}
1945
aae51f53 1946static int
93451a0a 1947dpif_netlink_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
aae51f53
BP
1948 uint32_t queue_id, uint32_t *priority)
1949{
1950 if (queue_id < 0xf000) {
17ee3c1f 1951 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
aae51f53
BP
1952 return 0;
1953 } else {
1954 return EINVAL;
1955 }
1956}
1957
96fba48f 1958static int
7af12bd7
JS
1959parse_odp_packet(const struct dpif_netlink *dpif, struct ofpbuf *buf,
1960 struct dpif_upcall *upcall, int *dp_ifindex)
856081f6 1961{
df2c07f4 1962 static const struct nl_policy ovs_packet_policy[] = {
856081f6 1963 /* Always present. */
df2c07f4 1964 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
856081f6 1965 .min_len = ETH_HEADER_LEN },
df2c07f4 1966 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
856081f6 1967
df2c07f4 1968 /* OVS_PACKET_CMD_ACTION only. */
e995e3df 1969 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
8b7ea2d4 1970 [OVS_PACKET_ATTR_EGRESS_TUN_KEY] = { .type = NL_A_NESTED, .optional = true },
7321bda3 1971 [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
27130224 1972 [OVS_PACKET_ATTR_MRU] = { .type = NL_A_U16, .optional = true }
856081f6
BP
1973 };
1974
0a2869d5
BP
1975 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
1976 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1977 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
1978 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
982b8810 1979
0a2869d5 1980 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
df2c07f4
JP
1981 if (!nlmsg || !genl || !ovs_header
1982 || nlmsg->nlmsg_type != ovs_packet_family
1983 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1984 ARRAY_SIZE(ovs_packet_policy))) {
856081f6
BP
1985 return EINVAL;
1986 }
1987
0a2869d5
BP
1988 int type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1989 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1990 : -1);
aaff4b55
BP
1991 if (type < 0) {
1992 return EINVAL;
1993 }
82272ede 1994
877c9270 1995 /* (Re)set ALL fields of '*upcall' on successful return. */
aaff4b55 1996 upcall->type = type;
ebc56baa
BP
1997 upcall->key = CONST_CAST(struct nlattr *,
1998 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
df2c07f4 1999 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
7af12bd7 2000 dpif_flow_hash(&dpif->dpif, upcall->key, upcall->key_len, &upcall->ufid);
e995e3df 2001 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
8b7ea2d4 2002 upcall->out_tun_key = a[OVS_PACKET_ATTR_EGRESS_TUN_KEY];
7321bda3 2003 upcall->actions = a[OVS_PACKET_ATTR_ACTIONS];
27130224 2004 upcall->mru = a[OVS_PACKET_ATTR_MRU];
da546e07
JR
2005
2006 /* Allow overwriting the netlink attribute header without reallocating. */
cf62fa4c 2007 dp_packet_use_stub(&upcall->packet,
da546e07
JR
2008 CONST_CAST(struct nlattr *,
2009 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
2010 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
2011 sizeof(struct nlattr));
cf62fa4c
PS
2012 dp_packet_set_data(&upcall->packet,
2013 (char *)dp_packet_data(&upcall->packet) + sizeof(struct nlattr));
2014 dp_packet_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
da546e07 2015
df2c07f4 2016 *dp_ifindex = ovs_header->dp_ifindex;
982b8810 2017
856081f6
BP
2018 return 0;
2019}
2020
09cac43f
NR
2021#ifdef _WIN32
2022#define PACKET_RECV_BATCH_SIZE 50
2023static int
2024dpif_netlink_recv_windows(struct dpif_netlink *dpif, uint32_t handler_id,
2025 struct dpif_upcall *upcall, struct ofpbuf *buf)
2026 OVS_REQ_RDLOCK(dpif->upcall_lock)
2027{
2028 struct dpif_handler *handler;
2029 int read_tries = 0;
2030 struct dpif_windows_vport_sock *sock_pool;
2031 uint32_t i;
2032
2033 if (!dpif->handlers) {
2034 return EAGAIN;
2035 }
2036
2037 /* Only one handler is supported currently. */
2038 if (handler_id >= 1) {
2039 return EAGAIN;
2040 }
2041
2042 if (handler_id >= dpif->n_handlers) {
2043 return EAGAIN;
2044 }
2045
2046 handler = &dpif->handlers[handler_id];
2047 sock_pool = handler->vport_sock_pool;
2048
2049 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2050 for (;;) {
2051 int dp_ifindex;
2052 int error;
2053
2054 if (++read_tries > PACKET_RECV_BATCH_SIZE) {
2055 return EAGAIN;
2056 }
2057
2058 error = nl_sock_recv(sock_pool[i].nl_sock, buf, false);
2059 if (error == ENOBUFS) {
2060 /* ENOBUFS typically means that we've received so many
2061 * packets that the buffer overflowed. Try again
2062 * immediately because there's almost certainly a packet
2063 * waiting for us. */
2064 /* XXX: report_loss(dpif, ch, idx, handler_id); */
2065 continue;
2066 }
2067
2068 /* XXX: ch->last_poll = time_msec(); */
2069 if (error) {
2070 if (error == EAGAIN) {
2071 break;
2072 }
2073 return error;
2074 }
2075
27edb4aa 2076 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
09cac43f
NR
2077 if (!error && dp_ifindex == dpif->dp_ifindex) {
2078 return 0;
2079 } else if (error) {
2080 return error;
2081 }
2082 }
2083 }
2084
2085 return EAGAIN;
2086}
2087#else
856081f6 2088static int
93451a0a
AS
2089dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id,
2090 struct dpif_upcall *upcall, struct ofpbuf *buf)
b90de034 2091 OVS_REQ_RDLOCK(dpif->upcall_lock)
96fba48f 2092{
1579cf67 2093 struct dpif_handler *handler;
17411ecf 2094 int read_tries = 0;
96fba48f 2095
1579cf67
AW
2096 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
2097 return EAGAIN;
982b8810
BP
2098 }
2099
1579cf67
AW
2100 handler = &dpif->handlers[handler_id];
2101 if (handler->event_offset >= handler->n_events) {
8522ba09 2102 int retval;
989fd548 2103
1579cf67 2104 handler->event_offset = handler->n_events = 0;
f6d1465c 2105
8522ba09 2106 do {
1579cf67 2107 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
989fd548 2108 dpif->uc_array_size, 0);
8522ba09 2109 } while (retval < 0 && errno == EINTR);
09cac43f 2110
8522ba09
BP
2111 if (retval < 0) {
2112 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
10a89ef0 2113 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
989fd548 2114 } else if (retval > 0) {
1579cf67 2115 handler->n_events = retval;
8522ba09 2116 }
8522ba09
BP
2117 }
2118
1579cf67
AW
2119 while (handler->event_offset < handler->n_events) {
2120 int idx = handler->epoll_events[handler->event_offset].data.u32;
2121 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
8522ba09 2122
1579cf67 2123 handler->event_offset++;
17411ecf 2124
f6d1465c 2125 for (;;) {
8522ba09 2126 int dp_ifindex;
f6d1465c 2127 int error;
17411ecf 2128
f6d1465c
BP
2129 if (++read_tries > 50) {
2130 return EAGAIN;
2131 }
17411ecf 2132
fe3d61b3 2133 error = nl_sock_recv(ch->sock, buf, false);
14b4d2f9
BP
2134 if (error == ENOBUFS) {
2135 /* ENOBUFS typically means that we've received so many
2136 * packets that the buffer overflowed. Try again
2137 * immediately because there's almost certainly a packet
2138 * waiting for us. */
9b00386b 2139 report_loss(dpif, ch, idx, handler_id);
14b4d2f9
BP
2140 continue;
2141 }
2142
2143 ch->last_poll = time_msec();
72d32ac0 2144 if (error) {
72d32ac0
BP
2145 if (error == EAGAIN) {
2146 break;
2147 }
f6d1465c
BP
2148 return error;
2149 }
17411ecf 2150
7af12bd7 2151 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
a12b3ead 2152 if (!error && dp_ifindex == dpif->dp_ifindex) {
f6d1465c 2153 return 0;
989fd548 2154 } else if (error) {
f6d1465c 2155 return error;
17411ecf 2156 }
982b8810 2157 }
50f80534 2158 }
982b8810
BP
2159
2160 return EAGAIN;
96fba48f 2161}
09cac43f 2162#endif
96fba48f 2163
9fafa796 2164static int
93451a0a
AS
2165dpif_netlink_recv(struct dpif *dpif_, uint32_t handler_id,
2166 struct dpif_upcall *upcall, struct ofpbuf *buf)
9fafa796 2167{
93451a0a 2168 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
9fafa796
BP
2169 int error;
2170
1579cf67 2171 fat_rwlock_rdlock(&dpif->upcall_lock);
09cac43f
NR
2172#ifdef _WIN32
2173 error = dpif_netlink_recv_windows(dpif, handler_id, upcall, buf);
2174#else
93451a0a 2175 error = dpif_netlink_recv__(dpif, handler_id, upcall, buf);
09cac43f 2176#endif
1579cf67 2177 fat_rwlock_unlock(&dpif->upcall_lock);
9fafa796
BP
2178
2179 return error;
2180}
2181
96fba48f 2182static void
93451a0a 2183dpif_netlink_recv_wait__(struct dpif_netlink *dpif, uint32_t handler_id)
b90de034 2184 OVS_REQ_RDLOCK(dpif->upcall_lock)
96fba48f 2185{
93451a0a 2186#ifdef _WIN32
09cac43f
NR
2187 uint32_t i;
2188 struct dpif_windows_vport_sock *sock_pool =
2189 dpif->handlers[handler_id].vport_sock_pool;
2190
2191 /* Only one handler is supported currently. */
2192 if (handler_id >= 1) {
2193 return;
2194 }
2195
2196 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2197 nl_sock_wait(sock_pool[i].nl_sock, POLLIN);
2198 }
93451a0a 2199#else
1579cf67
AW
2200 if (dpif->handlers && handler_id < dpif->n_handlers) {
2201 struct dpif_handler *handler = &dpif->handlers[handler_id];
2202
2203 poll_fd_wait(handler->epoll_fd, POLLIN);
17411ecf 2204 }
93451a0a 2205#endif
96fba48f
BP
2206}
2207
1ba530f4 2208static void
93451a0a 2209dpif_netlink_recv_wait(struct dpif *dpif_, uint32_t handler_id)
1ba530f4 2210{
93451a0a 2211 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
17411ecf 2212
b90de034 2213 fat_rwlock_rdlock(&dpif->upcall_lock);
93451a0a 2214 dpif_netlink_recv_wait__(dpif, handler_id);
b90de034
AW
2215 fat_rwlock_unlock(&dpif->upcall_lock);
2216}
2217
2218static void
93451a0a 2219dpif_netlink_recv_purge__(struct dpif_netlink *dpif)
b90de034
AW
2220 OVS_REQ_WRLOCK(dpif->upcall_lock)
2221{
1579cf67
AW
2222 if (dpif->handlers) {
2223 size_t i, j;
2224
2225 for (i = 0; i < dpif->uc_array_size; i++ ) {
2226 if (!dpif->handlers[0].channels[i].sock) {
2227 continue;
2228 }
1ba530f4 2229
1579cf67
AW
2230 for (j = 0; j < dpif->n_handlers; j++) {
2231 nl_sock_drain(dpif->handlers[j].channels[i].sock);
9fafa796 2232 }
989fd548 2233 }
1ba530f4 2234 }
b90de034
AW
2235}
2236
2237static void
93451a0a 2238dpif_netlink_recv_purge(struct dpif *dpif_)
b90de034 2239{
93451a0a 2240 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
b90de034
AW
2241
2242 fat_rwlock_wrlock(&dpif->upcall_lock);
93451a0a 2243 dpif_netlink_recv_purge__(dpif);
1579cf67 2244 fat_rwlock_unlock(&dpif->upcall_lock);
1ba530f4
BP
2245}
2246
b5cbbcf6
AZ
2247static char *
2248dpif_netlink_get_datapath_version(void)
2249{
2250 char *version_str = NULL;
2251
2252#ifdef __linux__
2253
2254#define MAX_VERSION_STR_SIZE 80
2255#define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2256 FILE *f;
2257
2258 f = fopen(LINUX_DATAPATH_VERSION_FILE, "r");
2259 if (f) {
2260 char *newline;
2261 char version[MAX_VERSION_STR_SIZE];
2262
2263 if (fgets(version, MAX_VERSION_STR_SIZE, f)) {
2264 newline = strchr(version, '\n');
2265 if (newline) {
2266 *newline = '\0';
2267 }
2268 version_str = xstrdup(version);
2269 }
2270 fclose(f);
2271 }
2272#endif
2273
2274 return version_str;
2275}
2276
c11c9f4a
DDP
2277struct dpif_netlink_ct_dump_state {
2278 struct ct_dpif_dump_state up;
2279 struct nl_ct_dump_state *nl_ct_dump;
2280};
2281
2282static int
2283dpif_netlink_ct_dump_start(struct dpif *dpif OVS_UNUSED,
2284 struct ct_dpif_dump_state **dump_,
2285 const uint16_t *zone)
2286{
2287 struct dpif_netlink_ct_dump_state *dump;
2288 int err;
2289
2290 dump = xzalloc(sizeof *dump);
2291 err = nl_ct_dump_start(&dump->nl_ct_dump, zone);
2292 if (err) {
2293 free(dump);
2294 return err;
2295 }
2296
2297 *dump_ = &dump->up;
2298
2299 return 0;
2300}
2301
2302static int
2303dpif_netlink_ct_dump_next(struct dpif *dpif OVS_UNUSED,
2304 struct ct_dpif_dump_state *dump_,
2305 struct ct_dpif_entry *entry)
2306{
2307 struct dpif_netlink_ct_dump_state *dump;
2308
2309 INIT_CONTAINER(dump, dump_, up);
2310
2311 return nl_ct_dump_next(dump->nl_ct_dump, entry);
2312}
2313
2314static int
2315dpif_netlink_ct_dump_done(struct dpif *dpif OVS_UNUSED,
2316 struct ct_dpif_dump_state *dump_)
2317{
2318 struct dpif_netlink_ct_dump_state *dump;
2319 int err;
2320
2321 INIT_CONTAINER(dump, dump_, up);
2322
2323 err = nl_ct_dump_done(dump->nl_ct_dump);
2324 free(dump);
2325 return err;
2326}
15eabc97
DDP
2327
2328static int
2329dpif_netlink_ct_flush(struct dpif *dpif OVS_UNUSED, const uint16_t *zone)
2330{
2331 if (zone) {
2332 return nl_ct_flush_zone(*zone);
2333 } else {
2334 return nl_ct_flush();
2335 }
2336}
c11c9f4a 2337
93451a0a 2338const struct dpif_class dpif_netlink_class = {
1a6f1e2a 2339 "system",
c8973eb6 2340 NULL, /* init */
93451a0a 2341 dpif_netlink_enumerate,
0aeaabc8 2342 NULL,
93451a0a
AS
2343 dpif_netlink_open,
2344 dpif_netlink_close,
2345 dpif_netlink_destroy,
2346 dpif_netlink_run,
e4516b20 2347 NULL, /* wait */
93451a0a
AS
2348 dpif_netlink_get_stats,
2349 dpif_netlink_port_add,
2350 dpif_netlink_port_del,
91364d18 2351 NULL, /* port_set_config */
93451a0a
AS
2352 dpif_netlink_port_query_by_number,
2353 dpif_netlink_port_query_by_name,
2354 dpif_netlink_port_get_pid,
2355 dpif_netlink_port_dump_start,
2356 dpif_netlink_port_dump_next,
2357 dpif_netlink_port_dump_done,
2358 dpif_netlink_port_poll,
2359 dpif_netlink_port_poll_wait,
2360 dpif_netlink_flow_flush,
2361 dpif_netlink_flow_dump_create,
2362 dpif_netlink_flow_dump_destroy,
2363 dpif_netlink_flow_dump_thread_create,
2364 dpif_netlink_flow_dump_thread_destroy,
2365 dpif_netlink_flow_dump_next,
2366 dpif_netlink_operate,
2367 dpif_netlink_recv_set,
2368 dpif_netlink_handlers_set,
f2eee189 2369 NULL, /* poll_thread_set */
93451a0a
AS
2370 dpif_netlink_queue_to_priority,
2371 dpif_netlink_recv,
2372 dpif_netlink_recv_wait,
2373 dpif_netlink_recv_purge,
e4e74c3a 2374 NULL, /* register_dp_purge_cb */
6b31e073
RW
2375 NULL, /* register_upcall_cb */
2376 NULL, /* enable_upcall */
2377 NULL, /* disable_upcall */
b5cbbcf6 2378 dpif_netlink_get_datapath_version, /* get_datapath_version */
c11c9f4a
DDP
2379 dpif_netlink_ct_dump_start,
2380 dpif_netlink_ct_dump_next,
2381 dpif_netlink_ct_dump_done,
e0467f6d 2382 dpif_netlink_ct_flush
96fba48f 2383};
93451a0a 2384
96fba48f 2385static int
93451a0a 2386dpif_netlink_init(void)
96fba48f 2387{
eb8ed438
BP
2388 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2389 static int error;
982b8810 2390
eb8ed438 2391 if (ovsthread_once_start(&once)) {
df2c07f4
JP
2392 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
2393 &ovs_datapath_family);
37a1300c 2394 if (error) {
cae7529c
CL
2395 VLOG_WARN("Generic Netlink family '%s' does not exist. "
2396 "The Open vSwitch kernel module is probably not loaded.",
2397 OVS_DATAPATH_FAMILY);
37a1300c 2398 }
f0fef760 2399 if (!error) {
df2c07f4 2400 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
f0fef760 2401 }
37a1300c 2402 if (!error) {
df2c07f4 2403 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
37a1300c 2404 }
aaff4b55 2405 if (!error) {
df2c07f4
JP
2406 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
2407 &ovs_packet_family);
aaff4b55 2408 }
c7178a0b
EJ
2409 if (!error) {
2410 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
b3dcb73c 2411 &ovs_vport_mcgroup);
c7178a0b 2412 }
eb8ed438
BP
2413
2414 ovsthread_once_done(&once);
982b8810
BP
2415 }
2416
2417 return error;
96fba48f
BP
2418}
2419
c19e6535 2420bool
93451a0a 2421dpif_netlink_is_internal_device(const char *name)
9fe3b9a2 2422{
93451a0a 2423 struct dpif_netlink_vport reply;
c19e6535 2424 struct ofpbuf *buf;
9fe3b9a2 2425 int error;
96fba48f 2426
93451a0a 2427 error = dpif_netlink_vport_get(name, &reply, &buf);
c19e6535
BP
2428 if (!error) {
2429 ofpbuf_delete(buf);
141d9ce4 2430 } else if (error != ENODEV && error != ENOENT) {
c19e6535 2431 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
10a89ef0 2432 name, ovs_strerror(error));
96fba48f
BP
2433 }
2434
df2c07f4 2435 return reply.type == OVS_VPORT_TYPE_INTERNAL;
96fba48f 2436}
e0467f6d 2437
df2c07f4 2438/* Parses the contents of 'buf', which contains a "struct ovs_header" followed
c19e6535
BP
2439 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
2440 * positive errno value.
2441 *
2442 * 'vport' will contain pointers into 'buf', so the caller should not free
2443 * 'buf' while 'vport' is still in use. */
2444static int
93451a0a 2445dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport,
c19e6535
BP
2446 const struct ofpbuf *buf)
2447{
df2c07f4
JP
2448 static const struct nl_policy ovs_vport_policy[] = {
2449 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
2450 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
2451 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1579cf67 2452 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
f7df9823 2453 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
c19e6535 2454 .optional = true },
df2c07f4 2455 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
c19e6535
BP
2456 };
2457
93451a0a 2458 dpif_netlink_vport_init(vport);
c19e6535 2459
0a2869d5
BP
2460 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
2461 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2462 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
2463 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2464
2465 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
df2c07f4
JP
2466 if (!nlmsg || !genl || !ovs_header
2467 || nlmsg->nlmsg_type != ovs_vport_family
2468 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2469 ARRAY_SIZE(ovs_vport_policy))) {
c19e6535
BP
2470 return EINVAL;
2471 }
c19e6535 2472
f0fef760 2473 vport->cmd = genl->cmd;
df2c07f4 2474 vport->dp_ifindex = ovs_header->dp_ifindex;
4e022ec0 2475 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
df2c07f4
JP
2476 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2477 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
b063d9f0 2478 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1579cf67
AW
2479 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2480 / (sizeof *vport->upcall_pids);
2481 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2482
b063d9f0 2483 }
df2c07f4
JP
2484 if (a[OVS_VPORT_ATTR_STATS]) {
2485 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2486 }
df2c07f4
JP
2487 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2488 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2489 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
c19e6535 2490 }
c19e6535
BP
2491 return 0;
2492}
2493
df2c07f4 2494/* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
c19e6535
BP
2495 * followed by Netlink attributes corresponding to 'vport'. */
2496static void
93451a0a
AS
2497dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *vport,
2498 struct ofpbuf *buf)
c19e6535 2499{
df2c07f4 2500 struct ovs_header *ovs_header;
f0fef760 2501
df2c07f4 2502 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
69685a88 2503 vport->cmd, OVS_VPORT_VERSION);
c19e6535 2504
df2c07f4
JP
2505 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2506 ovs_header->dp_ifindex = vport->dp_ifindex;
c19e6535 2507
4e022ec0
AW
2508 if (vport->port_no != ODPP_NONE) {
2509 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
c19e6535
BP
2510 }
2511
df2c07f4
JP
2512 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2513 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
c19e6535
BP
2514 }
2515
2516 if (vport->name) {
df2c07f4 2517 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
c19e6535
BP
2518 }
2519
1579cf67
AW
2520 if (vport->upcall_pids) {
2521 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2522 vport->upcall_pids,
2523 vport->n_upcall_pids * sizeof *vport->upcall_pids);
a24a6574 2524 }
b063d9f0 2525
c19e6535 2526 if (vport->stats) {
df2c07f4 2527 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
c19e6535
BP
2528 vport->stats, sizeof *vport->stats);
2529 }
2530
c19e6535 2531 if (vport->options) {
df2c07f4 2532 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
c19e6535
BP
2533 vport->options, vport->options_len);
2534 }
c19e6535
BP
2535}
2536
2537/* Clears 'vport' to "empty" values. */
2538void
93451a0a 2539dpif_netlink_vport_init(struct dpif_netlink_vport *vport)
c19e6535
BP
2540{
2541 memset(vport, 0, sizeof *vport);
4e022ec0 2542 vport->port_no = ODPP_NONE;
c19e6535
BP
2543}
2544
2545/* Executes 'request' in the kernel datapath. If the command fails, returns a
2546 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2547 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
df2c07f4 2548 * result of the command is expected to be an ovs_vport also, which is decoded
c19e6535
BP
2549 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2550 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2551int
93451a0a
AS
2552dpif_netlink_vport_transact(const struct dpif_netlink_vport *request,
2553 struct dpif_netlink_vport *reply,
2554 struct ofpbuf **bufp)
c19e6535 2555{
f0fef760 2556 struct ofpbuf *request_buf;
c19e6535
BP
2557 int error;
2558
cb22974d 2559 ovs_assert((reply != NULL) == (bufp != NULL));
c19e6535 2560
93451a0a 2561 error = dpif_netlink_init();
42bb6c72
BP
2562 if (error) {
2563 if (reply) {
2564 *bufp = NULL;
93451a0a 2565 dpif_netlink_vport_init(reply);
42bb6c72
BP
2566 }
2567 return error;
2568 }
2569
f0fef760 2570 request_buf = ofpbuf_new(1024);
93451a0a 2571 dpif_netlink_vport_to_ofpbuf(request, request_buf);
a88b4e04 2572 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
f0fef760 2573 ofpbuf_delete(request_buf);
c19e6535 2574
f0fef760
BP
2575 if (reply) {
2576 if (!error) {
93451a0a 2577 error = dpif_netlink_vport_from_ofpbuf(reply, *bufp);
f0fef760 2578 }
c19e6535 2579 if (error) {
93451a0a 2580 dpif_netlink_vport_init(reply);
f0fef760
BP
2581 ofpbuf_delete(*bufp);
2582 *bufp = NULL;
c19e6535 2583 }
c19e6535
BP
2584 }
2585 return error;
2586}
2587
2588/* Obtains information about the kernel vport named 'name' and stores it into
2589 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2590 * longer needed ('reply' will contain pointers into '*bufp'). */
2591int
93451a0a
AS
2592dpif_netlink_vport_get(const char *name, struct dpif_netlink_vport *reply,
2593 struct ofpbuf **bufp)
c19e6535 2594{
93451a0a 2595 struct dpif_netlink_vport request;
c19e6535 2596
93451a0a 2597 dpif_netlink_vport_init(&request);
df2c07f4 2598 request.cmd = OVS_VPORT_CMD_GET;
c19e6535
BP
2599 request.name = name;
2600
93451a0a 2601 return dpif_netlink_vport_transact(&request, reply, bufp);
c19e6535 2602}
93451a0a 2603
df2c07f4 2604/* Parses the contents of 'buf', which contains a "struct ovs_header" followed
aaff4b55
BP
2605 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2606 * positive errno value.
d6569377
BP
2607 *
2608 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2609 * while 'dp' is still in use. */
2610static int
93451a0a 2611dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *dp, const struct ofpbuf *buf)
d6569377 2612{
df2c07f4
JP
2613 static const struct nl_policy ovs_datapath_policy[] = {
2614 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
f7df9823 2615 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
d6569377 2616 .optional = true },
847108dc
AZ
2617 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2618 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2619 .optional = true },
d6569377
BP
2620 };
2621
93451a0a 2622 dpif_netlink_dp_init(dp);
d6569377 2623
0a2869d5
BP
2624 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
2625 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2626 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
2627 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2628
2629 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
df2c07f4
JP
2630 if (!nlmsg || !genl || !ovs_header
2631 || nlmsg->nlmsg_type != ovs_datapath_family
2632 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2633 ARRAY_SIZE(ovs_datapath_policy))) {
d6569377
BP
2634 return EINVAL;
2635 }
d6569377 2636
aaff4b55 2637 dp->cmd = genl->cmd;
df2c07f4
JP
2638 dp->dp_ifindex = ovs_header->dp_ifindex;
2639 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2640 if (a[OVS_DP_ATTR_STATS]) {
6a54dedc 2641 dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]);
d6569377 2642 }
982b8810 2643
847108dc 2644 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
6a54dedc 2645 dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]);
847108dc
AZ
2646 }
2647
d6569377
BP
2648 return 0;
2649}
2650
aaff4b55 2651/* Appends to 'buf' the Generic Netlink message described by 'dp'. */
d6569377 2652static void
93451a0a 2653dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp *dp, struct ofpbuf *buf)
d6569377 2654{
df2c07f4 2655 struct ovs_header *ovs_header;
d6569377 2656
df2c07f4 2657 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
69685a88
JG
2658 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2659 OVS_DATAPATH_VERSION);
aaff4b55 2660
df2c07f4
JP
2661 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2662 ovs_header->dp_ifindex = dp->dp_ifindex;
d6569377
BP
2663
2664 if (dp->name) {
df2c07f4 2665 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
d6569377
BP
2666 }
2667
a24a6574
BP
2668 if (dp->upcall_pid) {
2669 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2670 }
b063d9f0 2671
b7fd5e38
TG
2672 if (dp->user_features) {
2673 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2674 }
2675
df2c07f4 2676 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
d6569377
BP
2677}
2678
2679/* Clears 'dp' to "empty" values. */
d3d8f1f7 2680static void
93451a0a 2681dpif_netlink_dp_init(struct dpif_netlink_dp *dp)
d6569377
BP
2682{
2683 memset(dp, 0, sizeof *dp);
d6569377
BP
2684}
2685
aaff4b55 2686static void
93451a0a 2687dpif_netlink_dp_dump_start(struct nl_dump *dump)
aaff4b55 2688{
93451a0a 2689 struct dpif_netlink_dp request;
aaff4b55
BP
2690 struct ofpbuf *buf;
2691
93451a0a 2692 dpif_netlink_dp_init(&request);
df2c07f4 2693 request.cmd = OVS_DP_CMD_GET;
aaff4b55
BP
2694
2695 buf = ofpbuf_new(1024);
93451a0a 2696 dpif_netlink_dp_to_ofpbuf(&request, buf);
a88b4e04 2697 nl_dump_start(dump, NETLINK_GENERIC, buf);
aaff4b55
BP
2698 ofpbuf_delete(buf);
2699}
2700
d6569377
BP
2701/* Executes 'request' in the kernel datapath. If the command fails, returns a
2702 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2703 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
aaff4b55
BP
2704 * result of the command is expected to be of the same form, which is decoded
2705 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2706 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
d3d8f1f7 2707static int
93451a0a
AS
2708dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
2709 struct dpif_netlink_dp *reply, struct ofpbuf **bufp)
d6569377 2710{
aaff4b55 2711 struct ofpbuf *request_buf;
d6569377 2712 int error;
d6569377 2713
cb22974d 2714 ovs_assert((reply != NULL) == (bufp != NULL));
d6569377 2715
aaff4b55 2716 request_buf = ofpbuf_new(1024);
93451a0a 2717 dpif_netlink_dp_to_ofpbuf(request, request_buf);
a88b4e04 2718 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
aaff4b55 2719 ofpbuf_delete(request_buf);
d6569377 2720
aaff4b55 2721 if (reply) {
93451a0a 2722 dpif_netlink_dp_init(reply);
aaff4b55 2723 if (!error) {
93451a0a 2724 error = dpif_netlink_dp_from_ofpbuf(reply, *bufp);
aaff4b55 2725 }
d6569377 2726 if (error) {
aaff4b55
BP
2727 ofpbuf_delete(*bufp);
2728 *bufp = NULL;
d6569377 2729 }
d6569377
BP
2730 }
2731 return error;
2732}
2733
2734/* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2735 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2736 * will contain pointers into '*bufp'). */
d3d8f1f7 2737static int
93451a0a
AS
2738dpif_netlink_dp_get(const struct dpif *dpif_, struct dpif_netlink_dp *reply,
2739 struct ofpbuf **bufp)
d6569377 2740{
93451a0a
AS
2741 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2742 struct dpif_netlink_dp request;
d6569377 2743
93451a0a 2744 dpif_netlink_dp_init(&request);
df2c07f4 2745 request.cmd = OVS_DP_CMD_GET;
254f2dc8 2746 request.dp_ifindex = dpif->dp_ifindex;
d6569377 2747
93451a0a 2748 return dpif_netlink_dp_transact(&request, reply, bufp);
d6569377 2749}
93451a0a 2750
df2c07f4 2751/* Parses the contents of 'buf', which contains a "struct ovs_header" followed
37a1300c 2752 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
d6569377
BP
2753 * positive errno value.
2754 *
2755 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2756 * while 'flow' is still in use. */
2757static int
93451a0a
AS
2758dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *flow,
2759 const struct ofpbuf *buf)
d6569377 2760{
70e5ed6f
JS
2761 static const struct nl_policy ovs_flow_policy[__OVS_FLOW_ATTR_MAX] = {
2762 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED, .optional = true },
e6cc0bab 2763 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
df2c07f4 2764 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
f7df9823 2765 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
d6569377 2766 .optional = true },
df2c07f4
JP
2767 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2768 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
70e5ed6f
JS
2769 [OVS_FLOW_ATTR_UFID] = { .type = NL_A_UNSPEC, .optional = true,
2770 .min_len = sizeof(ovs_u128) },
df2c07f4 2771 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
43f9ac0a 2772 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
70e5ed6f 2773 /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */
d6569377
BP
2774 };
2775
93451a0a 2776 dpif_netlink_flow_init(flow);
d6569377 2777
0a2869d5
BP
2778 struct ofpbuf b = ofpbuf_const_initializer(buf->data, buf->size);
2779 struct nlmsghdr *nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2780 struct genlmsghdr *genl = ofpbuf_try_pull(&b, sizeof *genl);
2781 struct ovs_header *ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2782
2783 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
df2c07f4
JP
2784 if (!nlmsg || !genl || !ovs_header
2785 || nlmsg->nlmsg_type != ovs_flow_family
2786 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2787 ARRAY_SIZE(ovs_flow_policy))) {
d6569377
BP
2788 return EINVAL;
2789 }
70e5ed6f
JS
2790 if (!a[OVS_FLOW_ATTR_KEY] && !a[OVS_FLOW_ATTR_UFID]) {
2791 return EINVAL;
2792 }
d6569377 2793
37a1300c 2794 flow->nlmsg_flags = nlmsg->nlmsg_flags;
df2c07f4 2795 flow->dp_ifindex = ovs_header->dp_ifindex;
70e5ed6f
JS
2796 if (a[OVS_FLOW_ATTR_KEY]) {
2797 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2798 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2799 }
e6cc0bab 2800
70e5ed6f
JS
2801 if (a[OVS_FLOW_ATTR_UFID]) {
2802 const ovs_u128 *ufid;
2803
2804 ufid = nl_attr_get_unspec(a[OVS_FLOW_ATTR_UFID],
2805 nl_attr_get_size(a[OVS_FLOW_ATTR_UFID]));
2806 flow->ufid = *ufid;
2807 flow->ufid_present = true;
2808 }
e6cc0bab
AZ
2809 if (a[OVS_FLOW_ATTR_MASK]) {
2810 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2811 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2812 }
df2c07f4
JP
2813 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2814 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2815 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
d6569377 2816 }
df2c07f4
JP
2817 if (a[OVS_FLOW_ATTR_STATS]) {
2818 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
d6569377 2819 }
df2c07f4
JP
2820 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2821 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
d6569377 2822 }
df2c07f4
JP
2823 if (a[OVS_FLOW_ATTR_USED]) {
2824 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
9e980142 2825 }
d6569377
BP
2826 return 0;
2827}
2828
df2c07f4 2829/* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
d6569377
BP
2830 * followed by Netlink attributes corresponding to 'flow'. */
2831static void
93451a0a
AS
2832dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *flow,
2833 struct ofpbuf *buf)
d6569377 2834{
df2c07f4 2835 struct ovs_header *ovs_header;
d6569377 2836
df2c07f4 2837 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
30b44744 2838 NLM_F_REQUEST | flow->nlmsg_flags,
69685a88 2839 flow->cmd, OVS_FLOW_VERSION);
37a1300c 2840
df2c07f4
JP
2841 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2842 ovs_header->dp_ifindex = flow->dp_ifindex;
d6569377 2843
70e5ed6f
JS
2844 if (flow->ufid_present) {
2845 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_UFID, &flow->ufid,
2846 sizeof flow->ufid);
2847 }
2848 if (flow->ufid_terse) {
2849 nl_msg_put_u32(buf, OVS_FLOW_ATTR_UFID_FLAGS,
2850 OVS_UFID_F_OMIT_KEY | OVS_UFID_F_OMIT_MASK
2851 | OVS_UFID_F_OMIT_ACTIONS);
2852 }
64bb477f
JS
2853 if (!flow->ufid_terse || !flow->ufid_present) {
2854 if (flow->key_len) {
2855 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY,
2856 flow->key, flow->key_len);
2857 }
e6cc0bab 2858
64bb477f
JS
2859 if (flow->mask_len) {
2860 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK,
2861 flow->mask, flow->mask_len);
2862 }
2863 if (flow->actions || flow->actions_len) {
2864 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2865 flow->actions, flow->actions_len);
2866 }
d6569377
BP
2867 }
2868
2869 /* We never need to send these to the kernel. */
cb22974d
BP
2870 ovs_assert(!flow->stats);
2871 ovs_assert(!flow->tcp_flags);
2872 ovs_assert(!flow->used);
d6569377
BP
2873
2874 if (flow->clear) {
df2c07f4 2875 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
d6569377 2876 }
43f9ac0a
JR
2877 if (flow->probe) {
2878 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
2879 }
d6569377
BP
2880}
2881
2882/* Clears 'flow' to "empty" values. */
d3d8f1f7 2883static void
93451a0a 2884dpif_netlink_flow_init(struct dpif_netlink_flow *flow)
d6569377
BP
2885{
2886 memset(flow, 0, sizeof *flow);
2887}
2888
2889/* Executes 'request' in the kernel datapath. If the command fails, returns a
2890 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2891 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
37a1300c
BP
2892 * result of the command is expected to be a flow also, which is decoded and
2893 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2894 * is no longer needed ('reply' will contain pointers into '*bufp'). */
d3d8f1f7 2895static int
93451a0a
AS
2896dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
2897 struct dpif_netlink_flow *reply,
2898 struct ofpbuf **bufp)
d6569377 2899{
37a1300c 2900 struct ofpbuf *request_buf;
d6569377 2901 int error;
d6569377 2902
cb22974d 2903 ovs_assert((reply != NULL) == (bufp != NULL));
d6569377 2904
30b44744
BP
2905 if (reply) {
2906 request->nlmsg_flags |= NLM_F_ECHO;
2907 }
2908
37a1300c 2909 request_buf = ofpbuf_new(1024);
93451a0a 2910 dpif_netlink_flow_to_ofpbuf(request, request_buf);
a88b4e04 2911 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
37a1300c 2912 ofpbuf_delete(request_buf);
d6569377 2913
37a1300c
BP
2914 if (reply) {
2915 if (!error) {
93451a0a 2916 error = dpif_netlink_flow_from_ofpbuf(reply, *bufp);
37a1300c 2917 }
d6569377 2918 if (error) {
93451a0a 2919 dpif_netlink_flow_init(reply);
37a1300c
BP
2920 ofpbuf_delete(*bufp);
2921 *bufp = NULL;
d6569377 2922 }
d6569377
BP
2923 }
2924 return error;
2925}
2926
2927static void
93451a0a
AS
2928dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *flow,
2929 struct dpif_flow_stats *stats)
d6569377
BP
2930{
2931 if (flow->stats) {
6a54dedc
BP
2932 stats->n_packets = get_32aligned_u64(&flow->stats->n_packets);
2933 stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes);
d6569377
BP
2934 } else {
2935 stats->n_packets = 0;
2936 stats->n_bytes = 0;
2937 }
0e70cdcb 2938 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
d6569377
BP
2939 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2940}
e0467f6d 2941
14b4d2f9
BP
2942/* Logs information about a packet that was recently lost in 'ch' (in
2943 * 'dpif_'). */
2944static void
93451a0a 2945report_loss(struct dpif_netlink *dpif, struct dpif_channel *ch, uint32_t ch_idx,
1579cf67 2946 uint32_t handler_id)
14b4d2f9 2947{
14b4d2f9 2948 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
14b4d2f9
BP
2949 struct ds s;
2950
8d675c5a 2951 if (VLOG_DROP_WARN(&rl)) {
14b4d2f9
BP
2952 return;
2953 }
2954
2955 ds_init(&s);
2956 if (ch->last_poll != LLONG_MIN) {
2957 ds_put_format(&s, " (last polled %lld ms ago)",
2958 time_msec() - ch->last_poll);
2959 }
14b4d2f9 2960
1579cf67 2961 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
9b00386b 2962 dpif_name(&dpif->dpif), ch_idx, handler_id);
14b4d2f9
BP
2963 ds_destroy(&s);
2964}